1 /**
2  * Copyright (c) 2020 Paul-Louis Ageneau
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 #ifndef JUICE_H
20 #define JUICE_H
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 #include <stdbool.h>
27 #include <stddef.h>
28 #include <stdint.h>
29 
30 #ifdef JUICE_HAS_EXPORT_HEADER
31 #include "juice_export.h"
32 #endif
33 
34 #ifndef JUICE_EXPORT
35 #ifdef _WIN32
36 #define JUICE_EXPORT __declspec(dllexport)
37 #else
38 #define JUICE_EXPORT
39 #endif
40 #endif
41 
42 #define JUICE_ERR_SUCCESS 0
43 #define JUICE_ERR_INVALID -1   // invalid argument
44 #define JUICE_ERR_FAILED -2    // runtime error
45 #define JUICE_ERR_NOT_AVAIL -3 // element not available
46 
47 // ICE Agent
48 
49 #define JUICE_MAX_ADDRESS_STRING_LEN 64
50 #define JUICE_MAX_CANDIDATE_SDP_STRING_LEN 256
51 #define JUICE_MAX_SDP_STRING_LEN 4096
52 
53 typedef struct juice_agent juice_agent_t;
54 
55 typedef enum juice_state {
56 	JUICE_STATE_DISCONNECTED,
57 	JUICE_STATE_GATHERING,
58 	JUICE_STATE_CONNECTING,
59 	JUICE_STATE_CONNECTED,
60 	JUICE_STATE_COMPLETED,
61 	JUICE_STATE_FAILED
62 } juice_state_t;
63 
64 typedef void (*juice_cb_state_changed_t)(juice_agent_t *agent, juice_state_t state, void *user_ptr);
65 typedef void (*juice_cb_candidate_t)(juice_agent_t *agent, const char *sdp, void *user_ptr);
66 typedef void (*juice_cb_gathering_done_t)(juice_agent_t *agent, void *user_ptr);
67 typedef void (*juice_cb_recv_t)(juice_agent_t *agent, const char *data, size_t size,
68                                 void *user_ptr);
69 
70 typedef struct juice_turn_server {
71 	const char *host;
72 	const char *username;
73 	const char *password;
74 	uint16_t port;
75 } juice_turn_server_t;
76 
77 typedef struct juice_config {
78 	const char *stun_server_host;
79 	uint16_t stun_server_port;
80 
81 	juice_turn_server_t *turn_servers;
82 	int turn_servers_count;
83 
84 	const char *bind_address;
85 
86 	uint16_t local_port_range_begin;
87 	uint16_t local_port_range_end;
88 
89 	juice_cb_state_changed_t cb_state_changed;
90 	juice_cb_candidate_t cb_candidate;
91 	juice_cb_gathering_done_t cb_gathering_done;
92 	juice_cb_recv_t cb_recv;
93 
94 	void *user_ptr;
95 
96 } juice_config_t;
97 
98 JUICE_EXPORT juice_agent_t *juice_create(const juice_config_t *config);
99 JUICE_EXPORT void juice_destroy(juice_agent_t *agent);
100 
101 JUICE_EXPORT int juice_gather_candidates(juice_agent_t *agent);
102 JUICE_EXPORT int juice_get_local_description(juice_agent_t *agent, char *buffer, size_t size);
103 JUICE_EXPORT int juice_set_remote_description(juice_agent_t *agent, const char *sdp);
104 JUICE_EXPORT int juice_add_remote_candidate(juice_agent_t *agent, const char *sdp);
105 JUICE_EXPORT int juice_set_remote_gathering_done(juice_agent_t *agent);
106 JUICE_EXPORT int juice_send(juice_agent_t *agent, const char *data, size_t size);
107 JUICE_EXPORT int juice_send_diffserv(juice_agent_t *agent, const char *data, size_t size, int ds);
108 JUICE_EXPORT juice_state_t juice_get_state(juice_agent_t *agent);
109 JUICE_EXPORT int juice_get_selected_candidates(juice_agent_t *agent, char *local, size_t local_size,
110                                                char *remote, size_t remote_size);
111 JUICE_EXPORT int juice_get_selected_addresses(juice_agent_t *agent, char *local, size_t local_size,
112                                               char *remote, size_t remote_size);
113 JUICE_EXPORT const char *juice_state_to_string(juice_state_t state);
114 
115 // ICE server
116 
117 typedef struct juice_server juice_server_t;
118 
119 typedef struct juice_server_credentials {
120 	const char *username;
121 	const char *password;
122 	int allocations_quota;
123 } juice_server_credentials_t;
124 
125 typedef struct juice_server_config {
126 	juice_server_credentials_t *credentials;
127 	int credentials_count;
128 
129 	int max_allocations;
130 	int max_peers;
131 
132 	const char *bind_address;
133 	const char *external_address;
134 	uint16_t port;
135 
136 	uint16_t relay_port_range_begin;
137 	uint16_t relay_port_range_end;
138 
139 	const char *realm;
140 
141 } juice_server_config_t;
142 
143 JUICE_EXPORT juice_server_t *juice_server_create(const juice_server_config_t *config);
144 JUICE_EXPORT void juice_server_destroy(juice_server_t *server);
145 
146 JUICE_EXPORT uint16_t juice_server_get_port(juice_server_t *server);
147 JUICE_EXPORT int juice_server_add_credentials(juice_server_t *server,
148                                               const juice_server_credentials_t *credentials,
149                                               unsigned long lifetime_ms);
150 
151 // Logging
152 
153 typedef enum {
154 	JUICE_LOG_LEVEL_VERBOSE,
155 	JUICE_LOG_LEVEL_DEBUG,
156 	JUICE_LOG_LEVEL_INFO,
157 	JUICE_LOG_LEVEL_WARN,
158 	JUICE_LOG_LEVEL_ERROR,
159 	JUICE_LOG_LEVEL_FATAL,
160 	JUICE_LOG_LEVEL_NONE
161 } juice_log_level_t;
162 
163 typedef void (*juice_log_cb_t)(juice_log_level_t level, const char *message);
164 
165 JUICE_EXPORT void juice_set_log_level(juice_log_level_t level);
166 JUICE_EXPORT void juice_set_log_handler(juice_log_cb_t cb);
167 
168 #ifdef __cplusplus
169 }
170 #endif
171 
172 #endif
173