diff options
Diffstat (limited to 'toxav')
-rw-r--r-- | toxav/Makefile.inc | 75 | ||||
-rw-r--r-- | toxav/event.c | 384 | ||||
-rw-r--r-- | toxav/event.h | 49 | ||||
-rw-r--r-- | toxav/media.c | 314 | ||||
-rw-r--r-- | toxav/media.h | 80 | ||||
-rw-r--r-- | toxav/msi.c | 1424 | ||||
-rw-r--r-- | toxav/msi.h | 233 | ||||
-rw-r--r-- | toxav/phone.c | 1352 | ||||
-rw-r--r-- | toxav/rtp.c | 934 | ||||
-rw-r--r-- | toxav/rtp.h | 219 | ||||
-rw-r--r-- | toxav/toxav.c | 551 | ||||
-rw-r--r-- | toxav/toxav.h | 300 |
12 files changed, 5915 insertions, 0 deletions
diff --git a/toxav/Makefile.inc b/toxav/Makefile.inc new file mode 100644 index 00000000..30879f65 --- /dev/null +++ b/toxav/Makefile.inc | |||
@@ -0,0 +1,75 @@ | |||
1 | if BUILD_AV | ||
2 | |||
3 | lib_LTLIBRARIES += libtoxav.la | ||
4 | libtoxav_la_include_HEADERS = ../toxav/toxav.h | ||
5 | libtoxav_la_includedir = $(includedir)/tox | ||
6 | |||
7 | libtoxav_la_SOURCES = ../toxav/event.h \ | ||
8 | ../toxav/event.c \ | ||
9 | ../toxav/rtp.h \ | ||
10 | ../toxav/rtp.c \ | ||
11 | ../toxav/msi.h \ | ||
12 | ../toxav/msi.c \ | ||
13 | ../toxav/media.h \ | ||
14 | ../toxav/media.c \ | ||
15 | ../toxav/toxav.h \ | ||
16 | ../toxav/toxav.c | ||
17 | |||
18 | |||
19 | libtoxav_la_CFLAGS = -I../toxcore \ | ||
20 | -I../toxav \ | ||
21 | $(NACL_CFLAGS) \ | ||
22 | $(OPUS_CFLAGS) \ | ||
23 | $(VPX_CFLAGS) \ | ||
24 | $(PTHREAD_CFLAGS) | ||
25 | |||
26 | libtoxav_la_LDFLAGS = $(TOXAV_LT_LDFLAGS) \ | ||
27 | $(NACL_LDFLAGS) \ | ||
28 | $(EXTRA_LT_LDFLAGS) | ||
29 | |||
30 | libtoxav_la_LIBS = $(NACL_LIBS) \ | ||
31 | $(OPUS_LIBS) \ | ||
32 | $(VPX_LIBS) \ | ||
33 | $(PTHREAD_LIBS) | ||
34 | |||
35 | |||
36 | endif | ||
37 | |||
38 | |||
39 | |||
40 | |||
41 | |||
42 | |||
43 | if BUILD_PHONE | ||
44 | |||
45 | |||
46 | noinst_PROGRAMS += phone | ||
47 | |||
48 | phone_SOURCES = ../toxav/phone.c | ||
49 | |||
50 | phone_CFLAGS = -I../toxcore \ | ||
51 | -I../toxav \ | ||
52 | $(AVFORMAT_CFLAGS) \ | ||
53 | $(AVCODEC_CFLAGS) \ | ||
54 | $(AVUTIL_CFLAGS) \ | ||
55 | $(AVDEVICE_CFLAGS) \ | ||
56 | $(SWSCALE_CFLAGS) \ | ||
57 | $(SDL_CFLAGS) \ | ||
58 | $(OPENAL_CFLAGS) | ||
59 | |||
60 | phone_LDADD = libtoxav.la \ | ||
61 | libtoxcore.la \ | ||
62 | $(AVFORMAT_LIBS) \ | ||
63 | $(AVCODEC_LIBS) \ | ||
64 | $(AVUTIL_LIBS) \ | ||
65 | $(AVDEVICE_LIBS) \ | ||
66 | $(SWSCALE_LIBS) \ | ||
67 | $(SDL_LIBS) \ | ||
68 | $(OPENAL_LIBS) \ | ||
69 | $(OPUS_LIBS) \ | ||
70 | $(VPX_LIBS)\ | ||
71 | $(PTHREAD_LIBS)\ | ||
72 | $(NACL_LIBS) | ||
73 | |||
74 | |||
75 | endif \ No newline at end of file | ||
diff --git a/toxav/event.c b/toxav/event.c new file mode 100644 index 00000000..f79fd60e --- /dev/null +++ b/toxav/event.c | |||
@@ -0,0 +1,384 @@ | |||
1 | /** event.c | ||
2 | * | ||
3 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
4 | * | ||
5 | * This file is part of Tox. | ||
6 | * | ||
7 | * Tox is free software: you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation, either version 3 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * Tox is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | * | ||
21 | * Report bugs/suggestions at #tox-dev @ freenode.net:6667 | ||
22 | */ | ||
23 | |||
24 | |||
25 | #ifdef HAVE_CONFIG_H | ||
26 | #include "config.h" | ||
27 | #endif /* HAVE_CONFIG_H */ | ||
28 | |||
29 | #include <stdlib.h> | ||
30 | #include "event.h" | ||
31 | |||
32 | #include "../toxcore/util.h" | ||
33 | #include "../toxcore/network.h" | ||
34 | |||
35 | #define _GNU_SOURCE | ||
36 | |||
37 | #include <assert.h> | ||
38 | #include <unistd.h> | ||
39 | #include <stddef.h> | ||
40 | #include <inttypes.h> | ||
41 | #include <pthread.h> | ||
42 | #include <stdio.h> | ||
43 | |||
44 | #define RUN_IN_THREAD(func, args) { pthread_t _tid; \ | ||
45 | pthread_create(&_tid, NULL, func, args); assert( pthread_detach(_tid) == 0 ); } | ||
46 | |||
47 | #define LOCK(event_handler) pthread_mutex_lock (&event_handler->mutex) | ||
48 | #define UNLOCK(event_handler) pthread_mutex_unlock(&event_handler->mutex) | ||
49 | |||
50 | #define FREQUENCY 10000 | ||
51 | |||
52 | #define inline__ inline __attribute__((always_inline)) | ||
53 | |||
54 | |||
55 | typedef struct _EventContainer { | ||
56 | void *(*func)(void *); | ||
57 | void *func_args; | ||
58 | unsigned timeout; | ||
59 | long long id; | ||
60 | |||
61 | } EventContainer; | ||
62 | |||
63 | typedef struct _EventHandler { | ||
64 | EventContainer *timed_events; | ||
65 | size_t timed_events_count; | ||
66 | |||
67 | int running; | ||
68 | |||
69 | pthread_mutex_t mutex; | ||
70 | |||
71 | } EventHandler; | ||
72 | |||
73 | int throw_event( void * (func)(void *), void *arg ); | ||
74 | int reset_timer_event ( int id, uint32_t timeout ); | ||
75 | int throw_timer_event ( void * (func)(void *), void *arg, unsigned timeout); | ||
76 | int cancel_timer_event ( int id ); | ||
77 | int execute_timer_event ( int id ); | ||
78 | |||
79 | struct _Event event = { | ||
80 | throw_event, | ||
81 | /* reset_timer_event */ NULL, | ||
82 | throw_timer_event, | ||
83 | cancel_timer_event, | ||
84 | /*execute_timer_event*/ NULL | ||
85 | }; | ||
86 | |||
87 | /* | ||
88 | * Random functions used by this file | ||
89 | */ | ||
90 | void clear_events (EventContainer **event_container, size_t *counter) | ||
91 | { | ||
92 | free(*event_container ); | ||
93 | |||
94 | *event_container = NULL; | ||
95 | *counter = 0; | ||
96 | } | ||
97 | |||
98 | int pop_id ( EventContainer **event_container, size_t *counter, int id ) | ||
99 | { | ||
100 | if ( !*event_container || !*counter || !id ) | ||
101 | return -1; | ||
102 | |||
103 | EventContainer *_it = *event_container; | ||
104 | int i; | ||
105 | |||
106 | for ( i = *counter; i; -- i ) { | ||
107 | if ( _it->id == id ) { /* Hit! */ | ||
108 | break; | ||
109 | } | ||
110 | |||
111 | ++_it; | ||
112 | } | ||
113 | |||
114 | if ( i ) { | ||
115 | for ( ; i; -- i ) { | ||
116 | *_it = *(_it + 1); | ||
117 | ++_it; | ||
118 | } | ||
119 | |||
120 | -- (*counter ); | ||
121 | |||
122 | if ( !(*counter)) { /* Free and set to NULL */ | ||
123 | free(*event_container); | ||
124 | *event_container = NULL; | ||
125 | } else { | ||
126 | void *_result = realloc(*event_container, sizeof(EventContainer) * (*counter )); /* resize */ | ||
127 | |||
128 | |||
129 | if ( _result != NULL ) { | ||
130 | *event_container = _result; | ||
131 | return 0; | ||
132 | } else { | ||
133 | /* Not sure what would happen next so abort execution. | ||
134 | */ | ||
135 | fprintf(stderr, "CRITICAL! Failed to reallocate memory in %s():%d, aborting...", __func__, __LINE__); | ||
136 | abort(); | ||
137 | return -1; | ||
138 | } | ||
139 | } | ||
140 | } | ||
141 | |||
142 | /* not found here */ | ||
143 | |||
144 | return -1; | ||
145 | } | ||
146 | |||
147 | void push_event ( EventContainer **container, size_t *counter, void * (func)(void *), void *arg ) | ||
148 | { | ||
149 | EventContainer *_new = realloc((*container ), sizeof(EventContainer) * ((*counter ) + 1)); | ||
150 | |||
151 | if ( _new == NULL ) { | ||
152 | /* Not sure what would happen next so abort execution. | ||
153 | * TODO: This could notice the calling function | ||
154 | * about realloc failing. | ||
155 | */ | ||
156 | fprintf(stderr, "CRITICAL! Failed to reallocate memory in %s():%d, aborting...", __func__, __LINE__); | ||
157 | abort(); | ||
158 | } | ||
159 | |||
160 | _new[*counter].func = func; | ||
161 | _new[*counter].func_args = arg; | ||
162 | _new[*counter].timeout = 0; | ||
163 | _new[*counter].id = 0; | ||
164 | |||
165 | (*container) = _new; | ||
166 | |||
167 | (*counter )++; | ||
168 | } | ||
169 | |||
170 | void reorder_events ( size_t counter, EventContainer *container, unsigned timeout ) | ||
171 | { | ||
172 | if ( counter > 1 ) { | ||
173 | |||
174 | int i = counter - 1; | ||
175 | |||
176 | /* start from behind excluding last added member */ | ||
177 | EventContainer *_it = &container[i - 1]; | ||
178 | |||
179 | EventContainer _last_added = container[i]; | ||
180 | |||
181 | for ( ; i; --i ) { | ||
182 | if ( _it->timeout > timeout ) { | ||
183 | *(_it + 1) = *_it; | ||
184 | *_it = _last_added; | ||
185 | -- _it; | ||
186 | } | ||
187 | } | ||
188 | |||
189 | } | ||
190 | } | ||
191 | |||
192 | /* ============================================= */ | ||
193 | |||
194 | /* main poll for event execution */ | ||
195 | void *event_poll( void *arg ) | ||
196 | { | ||
197 | EventHandler *_event_handler = arg; | ||
198 | |||
199 | while ( _event_handler->running ) { | ||
200 | |||
201 | LOCK( _event_handler ); | ||
202 | |||
203 | if ( _event_handler->timed_events ) { | ||
204 | |||
205 | uint32_t _time = ((uint32_t)(current_time() / 1000)); | ||
206 | |||
207 | if ( _event_handler->timed_events[0].timeout < _time ) { | ||
208 | |||
209 | RUN_IN_THREAD ( _event_handler->timed_events[0].func, | ||
210 | _event_handler->timed_events[0].func_args ); | ||
211 | |||
212 | pop_id(&_event_handler->timed_events, | ||
213 | &_event_handler->timed_events_count, | ||
214 | _event_handler->timed_events[0].id); | ||
215 | |||
216 | } | ||
217 | |||
218 | } | ||
219 | |||
220 | UNLOCK( _event_handler ); | ||
221 | |||
222 | usleep(FREQUENCY); | ||
223 | } | ||
224 | |||
225 | LOCK( _event_handler ); | ||
226 | |||
227 | clear_events(&_event_handler->timed_events, &_event_handler->timed_events_count); | ||
228 | |||
229 | UNLOCK( _event_handler ); | ||
230 | |||
231 | _event_handler->running = -1; | ||
232 | pthread_exit(NULL); | ||
233 | } | ||
234 | |||
235 | int throw_event( void * (func)(void *), void *arg ) | ||
236 | { | ||
237 | pthread_t _tid; | ||
238 | int _rc = | ||
239 | pthread_create(&_tid, NULL, func, arg ); | ||
240 | |||
241 | return (0 != _rc ) ? _rc : pthread_detach(_tid); | ||
242 | } | ||
243 | |||
244 | EventHandler event_handler; | ||
245 | |||
246 | /* Place and order array of timers */ | ||
247 | int throw_timer_event ( void * (func)(void *), void *arg, unsigned timeout) | ||
248 | { | ||
249 | static int _unique_id = 1; | ||
250 | |||
251 | push_event(&event_handler.timed_events, &(event_handler.timed_events_count), func, arg ); | ||
252 | |||
253 | size_t _counter = event_handler.timed_events_count; | ||
254 | |||
255 | event_handler.timed_events[_counter - 1].timeout = timeout + ((uint32_t)(current_time() / 1000)); | ||
256 | event_handler.timed_events[_counter - 1].id = _unique_id; | ||
257 | ++_unique_id; | ||
258 | |||
259 | |||
260 | /* reorder */ | ||
261 | |||
262 | reorder_events(_counter, event_handler.timed_events, timeout ); | ||
263 | |||
264 | return _unique_id - 1; | ||
265 | } | ||
266 | |||
267 | int execute_timer_event ( int id ) | ||
268 | { | ||
269 | int _status; | ||
270 | |||
271 | LOCK((&event_handler)); | ||
272 | EventContainer *_it = event_handler.timed_events; | ||
273 | |||
274 | int _i = event_handler.timed_events_count; | ||
275 | |||
276 | /* Find it and execute */ | ||
277 | for ( ; _i; _i-- ) { | ||
278 | if ( _it->id == id ) { | ||
279 | RUN_IN_THREAD ( _it->func, _it->func_args ); | ||
280 | break; | ||
281 | } | ||
282 | |||
283 | ++_it; | ||
284 | } | ||
285 | |||
286 | /* Now remove it from the queue */ | ||
287 | |||
288 | if ( _i ) { | ||
289 | for ( ; _i; -- _i ) { | ||
290 | *_it = *(_it + 1); | ||
291 | ++_it; | ||
292 | } | ||
293 | |||
294 | -- event_handler.timed_events_count; | ||
295 | |||
296 | if ( !event_handler.timed_events_count ) { /* Free and set to null */ | ||
297 | free(event_handler.timed_events); | ||
298 | event_handler.timed_events = NULL; | ||
299 | } else { | ||
300 | void *_result = realloc(event_handler.timed_events, | ||
301 | sizeof(EventContainer) * event_handler.timed_events_count); /* resize */ | ||
302 | |||
303 | if ( _result != NULL ) { | ||
304 | event_handler.timed_events = _result; | ||
305 | } else { | ||
306 | /* Not sure what would happen next so abort execution. | ||
307 | */ | ||
308 | fprintf(stderr, "CRITICAL! Failed to reallocate memory in %s():%d, aborting...", __func__, __LINE__); | ||
309 | abort(); | ||
310 | return -1; | ||
311 | } | ||
312 | } | ||
313 | |||
314 | _status = 0; | ||
315 | |||
316 | } else _status = -1; | ||
317 | |||
318 | UNLOCK((&event_handler)); | ||
319 | |||
320 | return _status; | ||
321 | } | ||
322 | |||
323 | int reset_timer_event ( int id, uint32_t timeout ) | ||
324 | { | ||
325 | int _status; | ||
326 | |||
327 | LOCK((&event_handler)); | ||
328 | |||
329 | EventContainer *_it = event_handler.timed_events; | ||
330 | |||
331 | int _i = event_handler.timed_events_count; | ||
332 | |||
333 | /* Find it and change */ | ||
334 | for ( ; _i; _i-- ) { | ||
335 | if ( _it->id == id ) { | ||
336 | _it->timeout = timeout + ((uint32_t)(current_time() / 1000)); | ||
337 | break; | ||
338 | } | ||
339 | |||
340 | ++_it; | ||
341 | } | ||
342 | |||
343 | _status = _i ? -1 : 0; | ||
344 | |||
345 | UNLOCK((&event_handler)); | ||
346 | |||
347 | return _status; | ||
348 | } | ||
349 | |||
350 | /* Remove timer from array */ | ||
351 | inline__ int cancel_timer_event ( int id ) | ||
352 | { | ||
353 | return pop_id (&event_handler.timed_events, &event_handler.timed_events_count, id ); | ||
354 | } | ||
355 | |||
356 | |||
357 | /* Initialization and termination of event polls | ||
358 | * This will be run at the beginning and the end of the program execution. | ||
359 | * I think that's the best way to do it. | ||
360 | */ | ||
361 | |||
362 | void __attribute__((constructor)) init_event_poll () | ||
363 | { | ||
364 | event_handler.timed_events = NULL; | ||
365 | event_handler.timed_events_count = 0; | ||
366 | |||
367 | event_handler.running = 1; | ||
368 | |||
369 | pthread_mutex_init(&event_handler.mutex, NULL); | ||
370 | |||
371 | RUN_IN_THREAD(event_poll, &event_handler); | ||
372 | } | ||
373 | |||
374 | /* NOTE: Do we need this? */ | ||
375 | void __attribute__((destructor)) terminate_event_poll() | ||
376 | { | ||
377 | /* Exit thread */ | ||
378 | event_handler.running = 0; | ||
379 | |||
380 | /* Give it enought time to exit */ | ||
381 | usleep(FREQUENCY * 2); | ||
382 | |||
383 | pthread_mutex_destroy( &event_handler.mutex ); | ||
384 | } \ No newline at end of file | ||
diff --git a/toxav/event.h b/toxav/event.h new file mode 100644 index 00000000..0fb2d120 --- /dev/null +++ b/toxav/event.h | |||
@@ -0,0 +1,49 @@ | |||
1 | /** event.h | ||
2 | * | ||
3 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
4 | * | ||
5 | * This file is part of Tox. | ||
6 | * | ||
7 | * Tox is free software: you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation, either version 3 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * Tox is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | * | ||
21 | * Report bugs/suggestions at #tox-dev @ freenode.net:6667 | ||
22 | */ | ||
23 | |||
24 | |||
25 | #ifndef __TOXEVENT | ||
26 | #define __TOXEVENT | ||
27 | |||
28 | |||
29 | /** | ||
30 | * - Events are, in fact, ran in their own threads upon execution. | ||
31 | * - Event handler is initialized at the start, before the main() function | ||
32 | * and terminated after it's execution. | ||
33 | * - Timers are checked for timeout every ~10000 ns. | ||
34 | * - Timers can be canceled or ran immediately via | ||
35 | * timer_release() or timer_now() functions. | ||
36 | * - Timeout is measured in milliseconds. | ||
37 | * | ||
38 | * NOTE: timer_reset () and timer_now() are not tested nor usable atm | ||
39 | * | ||
40 | */ | ||
41 | extern struct _Event { | ||
42 | int (*rise) (void * ( func ) ( void * ), void *arg); | ||
43 | int (*timer_reset ) ( int id, unsigned timeout ); | ||
44 | int (*timer_alloc) (void * ( func ) ( void * ), void *arg, unsigned timeout); | ||
45 | int (*timer_release) (int id); | ||
46 | int (*timer_now) ( int id ); | ||
47 | } event; | ||
48 | |||
49 | #endif /* _MSI__EVENT_H_ */ | ||
diff --git a/toxav/media.c b/toxav/media.c new file mode 100644 index 00000000..2594f99a --- /dev/null +++ b/toxav/media.c | |||
@@ -0,0 +1,314 @@ | |||
1 | /** media.c | ||
2 | * | ||
3 | * Audio and video codec intitialization, encoding/decoding and playback | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | |||
25 | #ifdef HAVE_CONFIG_H | ||
26 | #include "config.h" | ||
27 | #endif /* HAVE_CONFIG_H */ | ||
28 | |||
29 | #include <stdio.h> | ||
30 | #include <stdlib.h> | ||
31 | #include <math.h> | ||
32 | #include <assert.h> | ||
33 | |||
34 | #include "rtp.h" | ||
35 | #include "media.h" | ||
36 | |||
37 | struct jitter_buffer { | ||
38 | RTPMessage **queue; | ||
39 | uint16_t capacity; | ||
40 | uint16_t size; | ||
41 | uint16_t front; | ||
42 | uint16_t rear; | ||
43 | uint8_t queue_ready; | ||
44 | uint16_t current_id; | ||
45 | uint32_t current_ts; | ||
46 | uint8_t id_set; | ||
47 | }; | ||
48 | |||
49 | |||
50 | struct jitter_buffer *create_queue(int capacity) | ||
51 | { | ||
52 | struct jitter_buffer *q; | ||
53 | q = (struct jitter_buffer *)calloc(sizeof(struct jitter_buffer), 1); | ||
54 | q->queue = (RTPMessage **)calloc(sizeof(RTPMessage *), capacity); | ||
55 | int i = 0; | ||
56 | |||
57 | for (i = 0; i < capacity; ++i) { | ||
58 | q->queue[i] = NULL; | ||
59 | } | ||
60 | |||
61 | q->size = 0; | ||
62 | q->capacity = capacity; | ||
63 | q->front = 0; | ||
64 | q->rear = -1; | ||
65 | q->queue_ready = 0; | ||
66 | q->current_id = 0; | ||
67 | q->current_ts = 0; | ||
68 | q->id_set = 0; | ||
69 | return q; | ||
70 | } | ||
71 | |||
72 | /* returns 1 if 'a' has a higher sequence number than 'b' */ | ||
73 | uint8_t sequence_number_older(uint16_t sn_a, uint16_t sn_b, uint32_t ts_a, uint32_t ts_b) | ||
74 | { | ||
75 | /* TODO: There is already this kind of function in toxrtp.c. | ||
76 | * Maybe merge? | ||
77 | */ | ||
78 | return (sn_a > sn_b || ts_a > ts_b); | ||
79 | } | ||
80 | |||
81 | /* success is 0 when there is nothing to dequeue, 1 when there's a good packet, 2 when there's a lost packet */ | ||
82 | RTPMessage *dequeue(struct jitter_buffer *q, int *success) | ||
83 | { | ||
84 | if (q->size == 0 || q->queue_ready == 0) { | ||
85 | q->queue_ready = 0; | ||
86 | *success = 0; | ||
87 | return NULL; | ||
88 | } | ||
89 | |||
90 | int front = q->front; | ||
91 | |||
92 | if (q->id_set == 0) { | ||
93 | q->current_id = q->queue[front]->header->sequnum; | ||
94 | q->current_ts = q->queue[front]->header->timestamp; | ||
95 | q->id_set = 1; | ||
96 | } else { | ||
97 | int next_id = q->queue[front]->header->sequnum; | ||
98 | int next_ts = q->queue[front]->header->timestamp; | ||
99 | |||
100 | /* if this packet is indeed the expected packet */ | ||
101 | if (next_id == (q->current_id + 1) % MAX_SEQU_NUM) { | ||
102 | q->current_id = next_id; | ||
103 | q->current_ts = next_ts; | ||
104 | } else { | ||
105 | if (sequence_number_older(next_id, q->current_id, next_ts, q->current_ts)) { | ||
106 | printf("nextid: %d current: %d\n", next_id, q->current_id); | ||
107 | q->current_id = (q->current_id + 1) % MAX_SEQU_NUM; | ||
108 | *success = 2; /* tell the decoder the packet is lost */ | ||
109 | return NULL; | ||
110 | } else { | ||
111 | /* packet too old */ | ||
112 | printf("packet too old\n"); | ||
113 | *success = 0; | ||
114 | return NULL; | ||
115 | } | ||
116 | } | ||
117 | } | ||
118 | |||
119 | q->size--; | ||
120 | q->front++; | ||
121 | |||
122 | if (q->front == q->capacity) | ||
123 | q->front = 0; | ||
124 | |||
125 | *success = 1; | ||
126 | q->current_id = q->queue[front]->header->sequnum; | ||
127 | q->current_ts = q->queue[front]->header->timestamp; | ||
128 | return q->queue[front]; | ||
129 | } | ||
130 | |||
131 | int empty_queue(struct jitter_buffer *q) | ||
132 | { | ||
133 | while (q->size > 0) { | ||
134 | q->size--; | ||
135 | rtp_free_msg(NULL, q->queue[q->front]); | ||
136 | q->front++; | ||
137 | |||
138 | if (q->front == q->capacity) | ||
139 | q->front = 0; | ||
140 | } | ||
141 | |||
142 | q->id_set = 0; | ||
143 | q->queue_ready = 0; | ||
144 | return 0; | ||
145 | } | ||
146 | |||
147 | int queue(struct jitter_buffer *q, RTPMessage *pk) | ||
148 | { | ||
149 | if (q->size == q->capacity) { | ||
150 | printf("buffer full, emptying buffer...\n"); | ||
151 | empty_queue(q); | ||
152 | return 0; | ||
153 | } | ||
154 | |||
155 | if (q->size > 8) | ||
156 | q->queue_ready = 1; | ||
157 | |||
158 | ++q->size; | ||
159 | ++q->rear; | ||
160 | |||
161 | if (q->rear == q->capacity) | ||
162 | q->rear = 0; | ||
163 | |||
164 | q->queue[q->rear] = pk; | ||
165 | |||
166 | int a; | ||
167 | int b; | ||
168 | int j; | ||
169 | a = q->rear; | ||
170 | |||
171 | for (j = 0; j < q->size - 1; ++j) { | ||
172 | b = a - 1; | ||
173 | |||
174 | if (b < 0) | ||
175 | b += q->capacity; | ||
176 | |||
177 | if (sequence_number_older(q->queue[b]->header->sequnum, q->queue[a]->header->sequnum, | ||
178 | q->queue[b]->header->timestamp, q->queue[a]->header->timestamp)) { | ||
179 | RTPMessage *temp; | ||
180 | temp = q->queue[a]; | ||
181 | q->queue[a] = q->queue[b]; | ||
182 | q->queue[b] = temp; | ||
183 | printf("had to swap\n"); | ||
184 | } else { | ||
185 | break; | ||
186 | } | ||
187 | |||
188 | a -= 1; | ||
189 | |||
190 | if (a < 0) | ||
191 | a += q->capacity; | ||
192 | } | ||
193 | |||
194 | if (pk) | ||
195 | return 1; | ||
196 | |||
197 | return 0; | ||
198 | } | ||
199 | |||
200 | |||
201 | int init_video_decoder(CodecState *cs) | ||
202 | { | ||
203 | if (vpx_codec_dec_init_ver(&cs->v_decoder, VIDEO_CODEC_DECODER_INTERFACE, NULL, 0, | ||
204 | VPX_DECODER_ABI_VERSION) != VPX_CODEC_OK) { | ||
205 | fprintf(stderr, "Init video_decoder failed!\n"); | ||
206 | return -1; | ||
207 | } | ||
208 | |||
209 | return 0; | ||
210 | } | ||
211 | |||
212 | int init_audio_decoder(CodecState *cs, uint32_t audio_channels) | ||
213 | { | ||
214 | int rc; | ||
215 | cs->audio_decoder = opus_decoder_create(cs->audio_sample_rate, audio_channels, &rc ); | ||
216 | |||
217 | if ( rc != OPUS_OK ) { | ||
218 | fprintf(stderr, "Error while starting audio decoder!\n"); | ||
219 | return -1; | ||
220 | } | ||
221 | |||
222 | return 0; | ||
223 | } | ||
224 | |||
225 | |||
226 | int init_video_encoder(CodecState *cs, uint16_t width, uint16_t height, uint32_t video_bitrate) | ||
227 | { | ||
228 | vpx_codec_enc_cfg_t cfg; | ||
229 | int res = vpx_codec_enc_config_default(VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0); | ||
230 | |||
231 | if (res) { | ||
232 | printf("Failed to get config: %s\n", vpx_codec_err_to_string(res)); | ||
233 | return -1; | ||
234 | } | ||
235 | |||
236 | cfg.rc_target_bitrate = video_bitrate; | ||
237 | cfg.g_w = width; | ||
238 | cfg.g_h = height; | ||
239 | |||
240 | if (vpx_codec_enc_init_ver(&cs->v_encoder, VIDEO_CODEC_ENCODER_INTERFACE, &cfg, 0, | ||
241 | VPX_ENCODER_ABI_VERSION) != VPX_CODEC_OK) { | ||
242 | fprintf(stderr, "Failed to initialize encoder\n"); | ||
243 | return -1; | ||
244 | } | ||
245 | |||
246 | return 0; | ||
247 | } | ||
248 | |||
249 | int init_audio_encoder(CodecState *cs, uint32_t audio_channels) | ||
250 | { | ||
251 | int err = OPUS_OK; | ||
252 | cs->audio_encoder = opus_encoder_create(cs->audio_sample_rate, audio_channels, OPUS_APPLICATION_AUDIO, &err); | ||
253 | err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_BITRATE(cs->audio_bitrate)); | ||
254 | err = opus_encoder_ctl(cs->audio_encoder, OPUS_SET_COMPLEXITY(10)); | ||
255 | |||
256 | |||
257 | return err == OPUS_OK ? 0 : -1; | ||
258 | } | ||
259 | |||
260 | |||
261 | CodecState *codec_init_session ( uint32_t audio_bitrate, | ||
262 | uint16_t audio_frame_duration, | ||
263 | uint32_t audio_sample_rate, | ||
264 | uint32_t audio_channels, | ||
265 | uint16_t video_width, | ||
266 | uint16_t video_height, | ||
267 | uint32_t video_bitrate ) | ||
268 | { | ||
269 | CodecState *_retu = calloc(sizeof(CodecState), 1); | ||
270 | assert(_retu); | ||
271 | |||
272 | _retu->audio_bitrate = audio_bitrate; | ||
273 | _retu->audio_sample_rate = audio_sample_rate; | ||
274 | |||
275 | /* Encoders */ | ||
276 | if (!video_width || !video_height) { | ||
277 | video_width = 320; | ||
278 | video_height = 240; | ||
279 | } | ||
280 | |||
281 | if ( 0 == init_video_encoder(_retu, video_width, video_height, video_bitrate) ) | ||
282 | printf("Video encoder initialized!\n"); | ||
283 | |||
284 | if ( 0 == init_audio_encoder(_retu, audio_channels) ) | ||
285 | printf("Audio encoder initialized!\n"); | ||
286 | |||
287 | |||
288 | /* Decoders */ | ||
289 | if ( 0 == init_video_decoder(_retu) ) | ||
290 | printf("Video decoder initialized!\n"); | ||
291 | |||
292 | if ( 0 == init_audio_decoder(_retu, audio_channels) ) | ||
293 | printf("Audio decoder initialized!\n"); | ||
294 | |||
295 | |||
296 | return _retu; | ||
297 | } | ||
298 | |||
299 | void codec_terminate_session ( CodecState *cs ) | ||
300 | { | ||
301 | if ( cs->audio_encoder ) { | ||
302 | opus_encoder_destroy(cs->audio_encoder); | ||
303 | printf("Terminated encoder!\n"); | ||
304 | } | ||
305 | |||
306 | if ( cs->audio_decoder ) { | ||
307 | opus_decoder_destroy(cs->audio_decoder); | ||
308 | printf("Terminated decoder!\n"); | ||
309 | } | ||
310 | |||
311 | /* TODO: Terminate video */ | ||
312 | vpx_codec_destroy(&cs->v_decoder); | ||
313 | vpx_codec_destroy(&cs->v_encoder); | ||
314 | } | ||
diff --git a/toxav/media.h b/toxav/media.h new file mode 100644 index 00000000..aed57ea2 --- /dev/null +++ b/toxav/media.h | |||
@@ -0,0 +1,80 @@ | |||
1 | /** media.h | ||
2 | * | ||
3 | * Audio and video codec intitialization, encoding/decoding and playback | ||
4 | * | ||
5 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
6 | * | ||
7 | * This file is part of Tox. | ||
8 | * | ||
9 | * Tox is free software: you can redistribute it and/or modify | ||
10 | * it under the terms of the GNU General Public License as published by | ||
11 | * the Free Software Foundation, either version 3 of the License, or | ||
12 | * (at your option) any later version. | ||
13 | * | ||
14 | * Tox is distributed in the hope that it will be useful, | ||
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
17 | * GNU General Public License for more details. | ||
18 | * | ||
19 | * You should have received a copy of the GNU General Public License | ||
20 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
21 | * | ||
22 | */ | ||
23 | |||
24 | #ifndef _AVCODEC_H_ | ||
25 | #define _AVCODEC_H_ | ||
26 | |||
27 | #include <stdio.h> | ||
28 | #include <math.h> | ||
29 | #include <pthread.h> | ||
30 | |||
31 | #include <vpx/vpx_decoder.h> | ||
32 | #include <vpx/vpx_encoder.h> | ||
33 | #include <vpx/vp8dx.h> | ||
34 | #include <vpx/vp8cx.h> | ||
35 | #define VIDEO_CODEC_DECODER_INTERFACE (vpx_codec_vp8_dx()) | ||
36 | #define VIDEO_CODEC_ENCODER_INTERFACE (vpx_codec_vp8_cx()) | ||
37 | |||
38 | /* Audio encoding/decoding */ | ||
39 | #include <opus/opus.h> | ||
40 | |||
41 | |||
42 | typedef struct _CodecState { | ||
43 | |||
44 | /* video encoding */ | ||
45 | vpx_codec_ctx_t v_encoder; | ||
46 | uint32_t frame_counter; | ||
47 | |||
48 | /* video decoding */ | ||
49 | vpx_codec_ctx_t v_decoder; | ||
50 | |||
51 | /* audio encoding */ | ||
52 | OpusEncoder *audio_encoder; | ||
53 | int audio_bitrate; | ||
54 | int audio_sample_rate; | ||
55 | |||
56 | /* audio decoding */ | ||
57 | OpusDecoder *audio_decoder; | ||
58 | |||
59 | } CodecState; | ||
60 | |||
61 | typedef struct _RTPMessage RTPMessage; | ||
62 | |||
63 | struct jitter_buffer *create_queue(int capacity); | ||
64 | int empty_queue(struct jitter_buffer *q); | ||
65 | |||
66 | int queue(struct jitter_buffer *q, RTPMessage *pk); | ||
67 | RTPMessage *dequeue(struct jitter_buffer *q, int *success); | ||
68 | |||
69 | |||
70 | CodecState *codec_init_session ( uint32_t audio_bitrate, | ||
71 | uint16_t audio_frame_duration, | ||
72 | uint32_t audio_sample_rate, | ||
73 | uint32_t audio_channels, | ||
74 | uint16_t video_width, | ||
75 | uint16_t video_height, | ||
76 | uint32_t video_bitrate ); | ||
77 | |||
78 | void codec_terminate_session(CodecState *cs); | ||
79 | |||
80 | #endif | ||
diff --git a/toxav/msi.c b/toxav/msi.c new file mode 100644 index 00000000..8f69d942 --- /dev/null +++ b/toxav/msi.c | |||
@@ -0,0 +1,1424 @@ | |||
1 | /** toxmsi.c | ||
2 | * | ||
3 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
4 | * | ||
5 | * This file is part of Tox. | ||
6 | * | ||
7 | * Tox is free software: you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation, either version 3 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * Tox is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | * | ||
21 | * Report bugs/suggestions at #tox-dev @ freenode.net:6667 | ||
22 | */ | ||
23 | |||
24 | |||
25 | #ifdef HAVE_CONFIG_H | ||
26 | #include "config.h" | ||
27 | #endif /* HAVE_CONFIG_H */ | ||
28 | |||
29 | #define _BSD_SOURCE | ||
30 | |||
31 | #include "msi.h" | ||
32 | #include "event.h" | ||
33 | #include "../toxcore/util.h" | ||
34 | #include "../toxcore/network.h" | ||
35 | #include "../toxcore/Messenger.h" | ||
36 | |||
37 | #include <assert.h> | ||
38 | #include <unistd.h> | ||
39 | #include <string.h> | ||
40 | #include <stdlib.h> | ||
41 | |||
42 | #define same(x, y) strcmp((const char*) x, (const char*) y) == 0 | ||
43 | |||
44 | #define MSI_MAXMSG_SIZE 1024 | ||
45 | |||
46 | #define TYPE_REQUEST 1 | ||
47 | #define TYPE_RESPONSE 2 | ||
48 | |||
49 | unsigned char *VERSION_STRING = (unsigned char *)"0.3.1"; | ||
50 | #define VERSION_STRLEN 5 | ||
51 | |||
52 | #define CT_AUDIO_HEADER_VALUE "AUDIO" | ||
53 | #define CT_VIDEO_HEADER_VALUE "VIDEO" | ||
54 | |||
55 | |||
56 | /* Define default timeout for a request. | ||
57 | * There is no behavior specified by the msi on what will | ||
58 | * client do on timeout, but to call timeout callback. | ||
59 | */ | ||
60 | #define m_deftout 10000 /* in milliseconds */ | ||
61 | |||
62 | /** | ||
63 | * Protocol: | ||
64 | * | ||
65 | * | desc. ( 1 byte ) | length ( 2 bytes ) | value ( length bytes ) | | ||
66 | * | ||
67 | * ie. | ||
68 | * | ||
69 | * | 0x1 | 0x0 0x7 | "version" | ||
70 | * | ||
71 | * Means: it's field value with length of 7 bytes and value of "version" | ||
72 | * It's similar to amp protocol | ||
73 | */ | ||
74 | |||
75 | |||
76 | #define GENERIC_HEADER(header) \ | ||
77 | typedef struct _MSIHeader##header { \ | ||
78 | uint8_t* header_value; \ | ||
79 | uint16_t size; \ | ||
80 | } MSIHeader##header; | ||
81 | |||
82 | |||
83 | GENERIC_HEADER ( Version ) | ||
84 | GENERIC_HEADER ( Request ) | ||
85 | GENERIC_HEADER ( Response ) | ||
86 | GENERIC_HEADER ( CallType ) | ||
87 | GENERIC_HEADER ( UserAgent ) | ||
88 | GENERIC_HEADER ( CallId ) | ||
89 | GENERIC_HEADER ( Info ) | ||
90 | GENERIC_HEADER ( Reason ) | ||
91 | GENERIC_HEADER ( CryptoKey ) | ||
92 | GENERIC_HEADER ( Nonce ) | ||
93 | |||
94 | |||
95 | /** | ||
96 | * @brief This is the message structure. It contains all of the headers and | ||
97 | * destination/source of the message stored in friend_id. | ||
98 | * | ||
99 | */ | ||
100 | typedef struct _MSIMessage { | ||
101 | |||
102 | MSIHeaderVersion version; | ||
103 | MSIHeaderRequest request; | ||
104 | MSIHeaderResponse response; | ||
105 | MSIHeaderCallType calltype; | ||
106 | MSIHeaderUserAgent useragent; | ||
107 | MSIHeaderInfo info; | ||
108 | MSIHeaderReason reason; | ||
109 | MSIHeaderCallId callid; | ||
110 | MSIHeaderCryptoKey cryptokey; | ||
111 | MSIHeaderNonce nonce; | ||
112 | |||
113 | struct _MSIMessage *next; | ||
114 | |||
115 | int friend_id; | ||
116 | |||
117 | } MSIMessage; | ||
118 | |||
119 | |||
120 | |||
121 | static MSICallback callbacks[10] = {0}; | ||
122 | |||
123 | |||
124 | /* define strings for the identifiers */ | ||
125 | #define VERSION_FIELD "Version" | ||
126 | #define REQUEST_FIELD "Request" | ||
127 | #define RESPONSE_FIELD "Response" | ||
128 | #define INFO_FIELD "INFO" | ||
129 | #define REASON_FIELD "Reason" | ||
130 | #define CALLTYPE_FIELD "Call-type" | ||
131 | #define USERAGENT_FIELD "User-agent" | ||
132 | #define CALLID_FIELD "Call-id" | ||
133 | #define CRYPTOKEY_FIELD "Crypto-key" | ||
134 | #define NONCE_FIELD "Nonce" | ||
135 | |||
136 | /* protocol descriptors */ | ||
137 | #define end_byte 0x0 | ||
138 | #define field_byte 0x1 | ||
139 | #define value_byte 0x2 | ||
140 | |||
141 | |||
142 | typedef enum { | ||
143 | invite, | ||
144 | start, | ||
145 | cancel, | ||
146 | reject, | ||
147 | end, | ||
148 | |||
149 | } MSIRequest; | ||
150 | |||
151 | |||
152 | /** | ||
153 | * @brief Get string value for request. | ||
154 | * | ||
155 | * @param request The request. | ||
156 | * @return const uint8_t* The string | ||
157 | */ | ||
158 | static inline const uint8_t *stringify_request ( MSIRequest request ) | ||
159 | { | ||
160 | static const uint8_t *strings[] = { | ||
161 | ( uint8_t * ) "INVITE", | ||
162 | ( uint8_t * ) "START", | ||
163 | ( uint8_t * ) "CANCEL", | ||
164 | ( uint8_t * ) "REJECT", | ||
165 | ( uint8_t * ) "END" | ||
166 | }; | ||
167 | |||
168 | return strings[request]; | ||
169 | } | ||
170 | |||
171 | |||
172 | typedef enum { | ||
173 | ringing, | ||
174 | starting, | ||
175 | ending, | ||
176 | error | ||
177 | |||
178 | } MSIResponse; | ||
179 | |||
180 | |||
181 | /** | ||
182 | * @brief Get string value for response. | ||
183 | * | ||
184 | * @param response The response. | ||
185 | * @return const uint8_t* The string | ||
186 | */ | ||
187 | static inline const uint8_t *stringify_response ( MSIResponse response ) | ||
188 | { | ||
189 | static const uint8_t *strings[] = { | ||
190 | ( uint8_t * ) "ringing", | ||
191 | ( uint8_t * ) "starting", | ||
192 | ( uint8_t * ) "ending", | ||
193 | ( uint8_t * ) "error" | ||
194 | }; | ||
195 | |||
196 | return strings[response]; | ||
197 | } | ||
198 | |||
199 | |||
200 | #define ON_HEADER(iterator, header, descriptor, size_const) \ | ||
201 | ( memcmp(iterator, descriptor, size_const) == 0){ /* Okay */ \ | ||
202 | iterator += size_const; /* Set iterator at begining of value part */ \ | ||
203 | if ( *iterator != value_byte ) { assert(0); return -1; }\ | ||
204 | iterator ++;\ | ||
205 | uint16_t _value_size = (uint16_t) *(iterator ) << 8 | \ | ||
206 | (uint16_t) *(iterator + 1); \ | ||
207 | header.header_value = calloc(sizeof(uint8_t), _value_size); \ | ||
208 | header.size = _value_size; \ | ||
209 | memcpy(header.header_value, iterator + 2, _value_size);\ | ||
210 | iterator = iterator + 2 + _value_size; /* set iterator at new header or end_byte */ \ | ||
211 | } | ||
212 | |||
213 | /** | ||
214 | * @brief Parse raw 'data' received from socket into MSIMessage struct. | ||
215 | * Every message has to have end value of 'end_byte' or _undefined_ behavior | ||
216 | * occures. The best practice is to check the end of the message at the handle_packet. | ||
217 | * | ||
218 | * @param msg Container. | ||
219 | * @param data The data. | ||
220 | * @return int | ||
221 | * @retval -1 Error occured. | ||
222 | * @retval 0 Success. | ||
223 | */ | ||
224 | int parse_raw_data ( MSIMessage *msg, const uint8_t *data, uint16_t length ) | ||
225 | { | ||
226 | assert ( msg ); | ||
227 | |||
228 | if ( data[length - 1] ) /* End byte must have value 0 */ | ||
229 | return -1; | ||
230 | |||
231 | const uint8_t *_it = data; | ||
232 | |||
233 | while ( *_it ) {/* until end_byte is hit */ | ||
234 | |||
235 | uint16_t itedlen = (_it - data) + 2; | ||
236 | |||
237 | if ( *_it == field_byte && itedlen < length ) { | ||
238 | |||
239 | uint16_t _size = ( uint16_t ) * ( _it + 1 ) << 8 | | ||
240 | ( uint16_t ) * ( _it + 2 ); | ||
241 | |||
242 | if ( itedlen + _size > length ) return -1; | ||
243 | |||
244 | _it += 3; /* place it at the field value beginning */ | ||
245 | |||
246 | switch ( _size ) { /* Compare the size of the hardcoded values ( vary fast and convenient ) */ | ||
247 | |||
248 | case 4: { /* INFO header */ | ||
249 | if ON_HEADER ( _it, msg->info, INFO_FIELD, 4 ) | ||
250 | } | ||
251 | break; | ||
252 | |||
253 | case 5: { /* NONCE header */ | ||
254 | if ON_HEADER ( _it, msg->nonce, NONCE_FIELD, 5 ) | ||
255 | } | ||
256 | break; | ||
257 | |||
258 | case 6: { /* Reason header */ | ||
259 | if ON_HEADER ( _it, msg->reason, REASON_FIELD, 6 ) | ||
260 | } | ||
261 | break; | ||
262 | |||
263 | case 7: { /* Version, Request, Call-id headers */ | ||
264 | if ON_HEADER ( _it, msg->version, VERSION_FIELD, 7 ) | ||
265 | else if ON_HEADER ( _it, msg->request, REQUEST_FIELD, 7 ) | ||
266 | else if ON_HEADER ( _it, msg->callid, CALLID_FIELD, 7 ) | ||
267 | } | ||
268 | break; | ||
269 | |||
270 | case 8: { /* Response header */ | ||
271 | if ON_HEADER ( _it, msg->response, RESPONSE_FIELD, 8 ) | ||
272 | } | ||
273 | break; | ||
274 | |||
275 | case 9: { /* Call-type header */ | ||
276 | if ON_HEADER ( _it, msg->calltype, CALLTYPE_FIELD, 9 ) | ||
277 | } | ||
278 | break; | ||
279 | |||
280 | case 10: { /* User-agent, Crypto-key headers */ | ||
281 | if ON_HEADER ( _it, msg->useragent, USERAGENT_FIELD, 10 ) | ||
282 | else if ON_HEADER ( _it, msg->cryptokey, CRYPTOKEY_FIELD, 10 ) | ||
283 | } | ||
284 | break; | ||
285 | |||
286 | default: | ||
287 | return -1; | ||
288 | } | ||
289 | } else return -1; | ||
290 | |||
291 | /* If it's anything else return failure as the message is invalid */ | ||
292 | |||
293 | } | ||
294 | |||
295 | return 0; | ||
296 | } | ||
297 | |||
298 | |||
299 | #define ALLOCATE_HEADER( var, mheader_value, t_size) \ | ||
300 | var.header_value = calloc(sizeof *mheader_value, t_size); \ | ||
301 | memcpy(var.header_value, mheader_value, t_size); \ | ||
302 | var.size = t_size; | ||
303 | |||
304 | |||
305 | /** | ||
306 | * @brief Speaks for it self. | ||
307 | * | ||
308 | * @param msg The message. | ||
309 | * @return void | ||
310 | */ | ||
311 | void free_message ( MSIMessage *msg ) | ||
312 | { | ||
313 | assert ( msg ); | ||
314 | |||
315 | free ( msg->calltype.header_value ); | ||
316 | free ( msg->request.header_value ); | ||
317 | free ( msg->response.header_value ); | ||
318 | free ( msg->useragent.header_value ); | ||
319 | free ( msg->version.header_value ); | ||
320 | free ( msg->info.header_value ); | ||
321 | free ( msg->cryptokey.header_value ); | ||
322 | free ( msg->nonce.header_value ); | ||
323 | free ( msg->reason.header_value ); | ||
324 | free ( msg->callid.header_value ); | ||
325 | |||
326 | free ( msg ); | ||
327 | } | ||
328 | |||
329 | |||
330 | /** | ||
331 | * @brief Create the message. | ||
332 | * | ||
333 | * @param type Request or response. | ||
334 | * @param type_id Type of request/response. | ||
335 | * @return MSIMessage* Created message. | ||
336 | * @retval NULL Error occured. | ||
337 | */ | ||
338 | MSIMessage *msi_new_message ( uint8_t type, const uint8_t *type_id ) | ||
339 | { | ||
340 | MSIMessage *_retu = calloc ( sizeof ( MSIMessage ), 1 ); | ||
341 | assert ( _retu ); | ||
342 | |||
343 | if ( type == TYPE_REQUEST ) { | ||
344 | ALLOCATE_HEADER ( _retu->request, type_id, strlen ( (const char *)type_id ) ) | ||
345 | |||
346 | } else if ( type == TYPE_RESPONSE ) { | ||
347 | ALLOCATE_HEADER ( _retu->response, type_id, strlen ( (const char *)type_id ) ) | ||
348 | |||
349 | } else { | ||
350 | free_message ( _retu ); | ||
351 | return NULL; | ||
352 | } | ||
353 | |||
354 | ALLOCATE_HEADER ( _retu->version, VERSION_STRING, strlen ( (const char *)VERSION_STRING ) ) | ||
355 | |||
356 | return _retu; | ||
357 | } | ||
358 | |||
359 | |||
360 | /** | ||
361 | * @brief Parse data from handle_packet. | ||
362 | * | ||
363 | * @param data The data. | ||
364 | * @return MSIMessage* Parsed message. | ||
365 | * @retval NULL Error occured. | ||
366 | */ | ||
367 | MSIMessage *parse_message ( const uint8_t *data, uint16_t length ) | ||
368 | { | ||
369 | assert ( data ); | ||
370 | |||
371 | MSIMessage *_retu = calloc ( sizeof ( MSIMessage ), 1 ); | ||
372 | assert ( _retu ); | ||
373 | |||
374 | memset ( _retu, 0, sizeof ( MSIMessage ) ); | ||
375 | |||
376 | if ( parse_raw_data ( _retu, data, length ) == -1 ) { | ||
377 | |||
378 | free_message ( _retu ); | ||
379 | return NULL; | ||
380 | } | ||
381 | |||
382 | if ( !_retu->version.header_value || VERSION_STRLEN != _retu->version.size || | ||
383 | memcmp ( _retu->version.header_value, VERSION_STRING, VERSION_STRLEN ) != 0 ) { | ||
384 | |||
385 | free_message ( _retu ); | ||
386 | return NULL; | ||
387 | } | ||
388 | |||
389 | return _retu; | ||
390 | } | ||
391 | |||
392 | |||
393 | |||
394 | /** | ||
395 | * @brief Speaks for it self. | ||
396 | * | ||
397 | * @param dest Container. | ||
398 | * @param header_field Field. | ||
399 | * @param header_value Field value. | ||
400 | * @param value_len Length of field value. | ||
401 | * @param length Pointer to container length. | ||
402 | * @return uint8_t* Iterated container. | ||
403 | */ | ||
404 | uint8_t *append_header_to_string ( | ||
405 | uint8_t *dest, | ||
406 | const uint8_t *header_field, | ||
407 | const uint8_t *header_value, | ||
408 | uint16_t value_len, | ||
409 | uint16_t *length ) | ||
410 | { | ||
411 | assert ( dest ); | ||
412 | assert ( header_value ); | ||
413 | assert ( header_field ); | ||
414 | |||
415 | const uint8_t *_hvit = header_value; | ||
416 | uint16_t _total = 6 + value_len; /* 6 is known plus header value len + field len*/ | ||
417 | |||
418 | *dest = field_byte; /* Set the first byte */ | ||
419 | |||
420 | uint8_t *_getback_byte = dest + 1; /* remeber the byte we were on */ | ||
421 | dest += 3; /* swith to 4th byte where field value starts */ | ||
422 | |||
423 | /* Now set the field value and calculate it's length */ | ||
424 | uint16_t _i = 0; | ||
425 | |||
426 | for ( ; header_field[_i]; ++_i ) { | ||
427 | *dest = header_field[_i]; | ||
428 | ++dest; | ||
429 | }; | ||
430 | |||
431 | _total += _i; | ||
432 | |||
433 | /* Now set the length of the field byte */ | ||
434 | *_getback_byte = ( uint8_t ) _i >> 8; | ||
435 | |||
436 | _getback_byte++; | ||
437 | |||
438 | *_getback_byte = ( uint8_t ) _i; | ||
439 | |||
440 | /* for value part do it regulary */ | ||
441 | *dest = value_byte; | ||
442 | |||
443 | dest++; | ||
444 | |||
445 | *dest = ( uint8_t ) value_len >> 8; | ||
446 | |||
447 | dest++; | ||
448 | |||
449 | *dest = ( uint8_t ) value_len; | ||
450 | |||
451 | dest++; | ||
452 | |||
453 | for ( _i = value_len; _i; --_i ) { | ||
454 | *dest = *_hvit; | ||
455 | ++_hvit; | ||
456 | ++dest; | ||
457 | } | ||
458 | |||
459 | *length += _total; | ||
460 | return dest; | ||
461 | } | ||
462 | |||
463 | |||
464 | #define CLEAN_ASSIGN(added, var, field, header)\ | ||
465 | if ( header.header_value ) { var = append_header_to_string(var, (const uint8_t*)field, header.header_value, header.size, &added); } | ||
466 | |||
467 | |||
468 | /** | ||
469 | * @brief Convert MSIMessage struct to _sendable_ string. | ||
470 | * | ||
471 | * @param msg The message. | ||
472 | * @param dest Destination. | ||
473 | * @return uint16_t It's final size. | ||
474 | */ | ||
475 | uint16_t message_to_string ( MSIMessage *msg, uint8_t *dest ) | ||
476 | { | ||
477 | assert ( msg ); | ||
478 | assert ( dest ); | ||
479 | |||
480 | uint8_t *_iterated = dest; | ||
481 | uint16_t _size = 0; | ||
482 | |||
483 | CLEAN_ASSIGN ( _size, _iterated, VERSION_FIELD, msg->version ); | ||
484 | CLEAN_ASSIGN ( _size, _iterated, REQUEST_FIELD, msg->request ); | ||
485 | CLEAN_ASSIGN ( _size, _iterated, RESPONSE_FIELD, msg->response ); | ||
486 | CLEAN_ASSIGN ( _size, _iterated, CALLTYPE_FIELD, msg->calltype ); | ||
487 | CLEAN_ASSIGN ( _size, _iterated, USERAGENT_FIELD, msg->useragent ); | ||
488 | CLEAN_ASSIGN ( _size, _iterated, INFO_FIELD, msg->info ); | ||
489 | CLEAN_ASSIGN ( _size, _iterated, CALLID_FIELD, msg->callid ); | ||
490 | CLEAN_ASSIGN ( _size, _iterated, REASON_FIELD, msg->reason ); | ||
491 | CLEAN_ASSIGN ( _size, _iterated, CRYPTOKEY_FIELD, msg->cryptokey ); | ||
492 | CLEAN_ASSIGN ( _size, _iterated, NONCE_FIELD, msg->nonce ); | ||
493 | |||
494 | *_iterated = end_byte; | ||
495 | _size ++; | ||
496 | |||
497 | return _size; | ||
498 | } | ||
499 | |||
500 | |||
501 | #define GENERIC_SETTER_DEFINITION(header) \ | ||
502 | void msi_msg_set_##header ( MSIMessage* _msg, const uint8_t* header_value, uint16_t _size ) \ | ||
503 | { assert(_msg); assert(header_value); \ | ||
504 | free(_msg->header.header_value); \ | ||
505 | ALLOCATE_HEADER( _msg->header, header_value, _size )} | ||
506 | |||
507 | GENERIC_SETTER_DEFINITION ( calltype ) | ||
508 | GENERIC_SETTER_DEFINITION ( useragent ) | ||
509 | GENERIC_SETTER_DEFINITION ( reason ) | ||
510 | GENERIC_SETTER_DEFINITION ( info ) | ||
511 | GENERIC_SETTER_DEFINITION ( callid ) | ||
512 | GENERIC_SETTER_DEFINITION ( cryptokey ) | ||
513 | GENERIC_SETTER_DEFINITION ( nonce ) | ||
514 | |||
515 | |||
516 | /** | ||
517 | * @brief Generate _random_ alphanumerical string. | ||
518 | * | ||
519 | * @param str Destination. | ||
520 | * @param size Size of string. | ||
521 | * @return void | ||
522 | */ | ||
523 | void t_randomstr ( uint8_t *str, size_t size ) | ||
524 | { | ||
525 | assert ( str ); | ||
526 | |||
527 | static const uint8_t _bytes[] = | ||
528 | "0123456789" | ||
529 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | ||
530 | "abcdefghijklmnopqrstuvwxyz"; | ||
531 | |||
532 | size_t _it = 0; | ||
533 | |||
534 | for ( ; _it < size; _it++ ) { | ||
535 | str[_it] = _bytes[ random_int() % 61 ]; | ||
536 | } | ||
537 | } | ||
538 | |||
539 | |||
540 | typedef enum { | ||
541 | error_deadcall = 1, /* has call id but it's from old call */ | ||
542 | error_id_mismatch, /* non-existing call */ | ||
543 | |||
544 | error_no_callid, /* not having call id */ | ||
545 | error_no_call, /* no call in session */ | ||
546 | error_no_crypto_key, /* no crypto key */ | ||
547 | |||
548 | error_busy | ||
549 | |||
550 | } MSICallError; /* Error codes */ | ||
551 | |||
552 | |||
553 | /** | ||
554 | * @brief Stringify error code. | ||
555 | * | ||
556 | * @param error_code The code. | ||
557 | * @return const uint8_t* The string. | ||
558 | */ | ||
559 | static inline const uint8_t *stringify_error ( MSICallError error_code ) | ||
560 | { | ||
561 | static const uint8_t *strings[] = { | ||
562 | ( uint8_t * ) "", | ||
563 | ( uint8_t * ) "Using dead call", | ||
564 | ( uint8_t * ) "Call id not set to any call", | ||
565 | ( uint8_t * ) "Call id not available", | ||
566 | ( uint8_t * ) "No active call in session", | ||
567 | ( uint8_t * ) "No Crypto-key set", | ||
568 | ( uint8_t * ) "Callee busy" | ||
569 | }; | ||
570 | |||
571 | return strings[error_code]; | ||
572 | } | ||
573 | |||
574 | |||
575 | /** | ||
576 | * @brief Convert error_code into string. | ||
577 | * | ||
578 | * @param error_code The code. | ||
579 | * @return const uint8_t* The string. | ||
580 | */ | ||
581 | static inline const uint8_t *stringify_error_code ( MSICallError error_code ) | ||
582 | { | ||
583 | static const uint8_t *strings[] = { | ||
584 | ( uint8_t * ) "", | ||
585 | ( uint8_t * ) "1", | ||
586 | ( uint8_t * ) "2", | ||
587 | ( uint8_t * ) "3", | ||
588 | ( uint8_t * ) "4", | ||
589 | ( uint8_t * ) "5", | ||
590 | ( uint8_t * ) "6" | ||
591 | }; | ||
592 | |||
593 | return strings[error_code]; | ||
594 | } | ||
595 | |||
596 | |||
597 | /** | ||
598 | * @brief Speaks for it self. | ||
599 | * | ||
600 | * @param session Control session. | ||
601 | * @param msg The message. | ||
602 | * @param to Where to. | ||
603 | * @return int | ||
604 | * @retval -1 Error occured. | ||
605 | * @retval 0 Success. | ||
606 | */ | ||
607 | int send_message ( MSISession *session, MSIMessage *msg, uint32_t to ) | ||
608 | { | ||
609 | msi_msg_set_callid ( msg, session->call->id, CALL_ID_LEN ); | ||
610 | |||
611 | uint8_t _msg_string_final [MSI_MAXMSG_SIZE]; | ||
612 | uint16_t _length = message_to_string ( msg, _msg_string_final ); | ||
613 | |||
614 | return m_msi_packet(session->messenger_handle, to, _msg_string_final, _length) ? 0 : -1; | ||
615 | } | ||
616 | |||
617 | |||
618 | /** | ||
619 | * @brief Speaks for it self. | ||
620 | * | ||
621 | * @param session Control session. | ||
622 | * @param msg The message. | ||
623 | * @param peer_id The peer. | ||
624 | * @return void | ||
625 | */ | ||
626 | void flush_peer_type ( MSISession *session, MSIMessage *msg, int peer_id ) | ||
627 | { | ||
628 | if ( msg->calltype.header_value ) { | ||
629 | if ( strcmp ( ( const char * ) msg->calltype.header_value, CT_AUDIO_HEADER_VALUE ) == 0 ) { | ||
630 | session->call->type_peer[peer_id] = type_audio; | ||
631 | |||
632 | } else if ( strcmp ( ( const char * ) msg->calltype.header_value, CT_VIDEO_HEADER_VALUE ) == 0 ) { | ||
633 | session->call->type_peer[peer_id] = type_video; | ||
634 | } else {} /* Error */ | ||
635 | } else {} /* Error */ | ||
636 | } | ||
637 | |||
638 | void handle_remote_connection_change(Messenger *messenger, int friend_num, uint8_t status, void *session_p) | ||
639 | { | ||
640 | MSISession *session = session_p; | ||
641 | |||
642 | switch ( status ) { | ||
643 | case 0: { /* Went offline */ | ||
644 | if ( session->call ) { | ||
645 | int i = 0; | ||
646 | |||
647 | for ( ; i < session->call->peer_count; i ++ ) | ||
648 | if ( session->call->peers[i] == friend_num ) { | ||
649 | msi_stopcall(session); /* Stop the call for now */ | ||
650 | return; | ||
651 | } | ||
652 | } | ||
653 | } | ||
654 | break; | ||
655 | |||
656 | default: | ||
657 | break; | ||
658 | } | ||
659 | } | ||
660 | |||
661 | /** | ||
662 | * @brief Sends error response to peer. | ||
663 | * | ||
664 | * @param session The session. | ||
665 | * @param errid The id. | ||
666 | * @param to Where to? | ||
667 | * @return int | ||
668 | * @retval 0 It's always success. | ||
669 | */ | ||
670 | int handle_error ( MSISession *session, MSICallError errid, uint32_t to ) | ||
671 | { | ||
672 | MSIMessage *_msg_error = msi_new_message ( TYPE_RESPONSE, stringify_response ( error ) ); | ||
673 | |||
674 | const uint8_t *_error_code_str = stringify_error_code ( errid ); | ||
675 | |||
676 | msi_msg_set_reason ( _msg_error, _error_code_str, strlen ( ( const char * ) _error_code_str ) ); | ||
677 | send_message ( session, _msg_error, to ); | ||
678 | free_message ( _msg_error ); | ||
679 | |||
680 | session->last_error_id = errid; | ||
681 | session->last_error_str = stringify_error ( errid ); | ||
682 | |||
683 | event.rise ( callbacks[MSI_OnError], session->agent_handler ); | ||
684 | |||
685 | return 0; | ||
686 | } | ||
687 | |||
688 | |||
689 | /** | ||
690 | * @brief Determine the error if any. | ||
691 | * | ||
692 | * @param session Control session. | ||
693 | * @param msg The message. | ||
694 | * @return int | ||
695 | * @retval -1 No error. | ||
696 | * @retval 0 Error occured and response sent. | ||
697 | */ | ||
698 | int has_call_error ( MSISession *session, MSIMessage *msg ) | ||
699 | { | ||
700 | if ( !msg->callid.header_value ) { | ||
701 | return handle_error ( session, error_no_callid, msg->friend_id ); | ||
702 | |||
703 | } else if ( !session->call ) { | ||
704 | return handle_error ( session, error_no_call, msg->friend_id ); | ||
705 | |||
706 | } else if ( memcmp ( session->call->id, msg->callid.header_value, CALL_ID_LEN ) != 0 ) { | ||
707 | return handle_error ( session, error_id_mismatch, msg->friend_id ); | ||
708 | |||
709 | } | ||
710 | |||
711 | return -1; | ||
712 | } | ||
713 | |||
714 | |||
715 | /** | ||
716 | * @brief Function called at request timeout. | ||
717 | * | ||
718 | * @param arg Control session | ||
719 | * @return void* | ||
720 | */ | ||
721 | void *handle_timeout ( void *arg ) | ||
722 | { | ||
723 | /* Send hangup either way */ | ||
724 | MSISession *_session = arg; | ||
725 | |||
726 | if ( _session && _session->call ) { | ||
727 | |||
728 | uint32_t *_peers = _session->call->peers; | ||
729 | uint16_t _peer_count = _session->call->peer_count; | ||
730 | |||
731 | |||
732 | /* Cancel all? */ | ||
733 | uint16_t _it = 0; | ||
734 | |||
735 | for ( ; _it < _peer_count; _it++ ) | ||
736 | msi_cancel ( arg, _peers[_it], (const uint8_t *)"Timeout" ); | ||
737 | |||
738 | } | ||
739 | |||
740 | ( *callbacks[MSI_OnRequestTimeout] ) ( _session->agent_handler ); | ||
741 | ( *callbacks[MSI_OnEnding ] ) ( _session->agent_handler ); | ||
742 | |||
743 | return NULL; | ||
744 | } | ||
745 | |||
746 | |||
747 | /** | ||
748 | * @brief Add peer to peer list. | ||
749 | * | ||
750 | * @param call What call. | ||
751 | * @param peer_id Its id. | ||
752 | * @return void | ||
753 | */ | ||
754 | void add_peer( MSICall *call, int peer_id ) | ||
755 | { | ||
756 | if ( !call->peers ) { | ||
757 | call->peers = calloc(sizeof(int), 1); | ||
758 | call->peer_count = 1; | ||
759 | } else { | ||
760 | call->peer_count ++; | ||
761 | call->peers = realloc( call->peers, sizeof(int) * call->peer_count); | ||
762 | } | ||
763 | |||
764 | call->peers[call->peer_count - 1] = peer_id; | ||
765 | } | ||
766 | |||
767 | |||
768 | /** | ||
769 | * @brief Speaks for it self. | ||
770 | * | ||
771 | * @param session Control session. | ||
772 | * @param peers Amount of peers. (Currently it only supports 1) | ||
773 | * @param ringing_timeout Ringing timeout. | ||
774 | * @return MSICall* The created call. | ||
775 | */ | ||
776 | MSICall *init_call ( MSISession *session, int peers, int ringing_timeout ) | ||
777 | { | ||
778 | assert ( session ); | ||
779 | assert ( peers ); | ||
780 | |||
781 | MSICall *_call = calloc ( sizeof ( MSICall ), 1 ); | ||
782 | _call->type_peer = calloc ( sizeof ( MSICallType ), peers ); | ||
783 | |||
784 | assert ( _call ); | ||
785 | assert ( _call->type_peer ); | ||
786 | |||
787 | /*_call->_participant_count = _peers;*/ | ||
788 | |||
789 | _call->request_timer_id = 0; | ||
790 | _call->ringing_timer_id = 0; | ||
791 | |||
792 | _call->key_local = NULL; | ||
793 | _call->key_peer = NULL; | ||
794 | _call->nonce_local = NULL; | ||
795 | _call->nonce_peer = NULL; | ||
796 | |||
797 | _call->ringing_tout_ms = ringing_timeout; | ||
798 | |||
799 | pthread_mutex_init ( &_call->mutex, NULL ); | ||
800 | |||
801 | return _call; | ||
802 | } | ||
803 | |||
804 | |||
805 | /** | ||
806 | * @brief Terminate the call. | ||
807 | * | ||
808 | * @param session Control session. | ||
809 | * @return int | ||
810 | * @retval -1 Error occured. | ||
811 | * @retval 0 Success. | ||
812 | */ | ||
813 | int terminate_call ( MSISession *session ) | ||
814 | { | ||
815 | assert ( session ); | ||
816 | |||
817 | if ( !session->call ) | ||
818 | return -1; | ||
819 | |||
820 | |||
821 | /* Check event loop and cancel timed events if there are any | ||
822 | * NOTE: This has to be done before possibly | ||
823 | * locking the mutex the second time | ||
824 | */ | ||
825 | event.timer_release ( session->call->request_timer_id ); | ||
826 | event.timer_release ( session->call->ringing_timer_id ); | ||
827 | |||
828 | /* Get a handle */ | ||
829 | pthread_mutex_lock ( &session->call->mutex ); | ||
830 | |||
831 | MSICall *_call = session->call; | ||
832 | session->call = NULL; | ||
833 | |||
834 | free ( _call->type_peer ); | ||
835 | free ( _call->key_local ); | ||
836 | free ( _call->key_peer ); | ||
837 | free ( _call->peers); | ||
838 | |||
839 | /* Release handle */ | ||
840 | pthread_mutex_unlock ( &_call->mutex ); | ||
841 | |||
842 | pthread_mutex_destroy ( &_call->mutex ); | ||
843 | |||
844 | free ( _call ); | ||
845 | |||
846 | return 0; | ||
847 | } | ||
848 | |||
849 | |||
850 | /********** Request handlers **********/ | ||
851 | int handle_recv_invite ( MSISession *session, MSIMessage *msg ) | ||
852 | { | ||
853 | assert ( session ); | ||
854 | |||
855 | if ( session->call ) { | ||
856 | handle_error ( session, error_busy, msg->friend_id ); | ||
857 | return 0; | ||
858 | } | ||
859 | |||
860 | if ( !msg->callid.header_value ) { | ||
861 | handle_error ( session, error_no_callid, msg->friend_id ); | ||
862 | return 0; | ||
863 | } | ||
864 | |||
865 | session->call = init_call ( session, 1, 0 ); | ||
866 | memcpy ( session->call->id, msg->callid.header_value, CALL_ID_LEN ); | ||
867 | session->call->state = call_starting; | ||
868 | |||
869 | add_peer( session->call, msg->friend_id); | ||
870 | |||
871 | flush_peer_type ( session, msg, 0 ); | ||
872 | |||
873 | MSIMessage *_msg_ringing = msi_new_message ( TYPE_RESPONSE, stringify_response ( ringing ) ); | ||
874 | send_message ( session, _msg_ringing, msg->friend_id ); | ||
875 | free_message ( _msg_ringing ); | ||
876 | |||
877 | event.rise ( callbacks[MSI_OnInvite], session->agent_handler ); | ||
878 | |||
879 | return 1; | ||
880 | } | ||
881 | int handle_recv_start ( MSISession *session, MSIMessage *msg ) | ||
882 | { | ||
883 | assert ( session ); | ||
884 | |||
885 | if ( has_call_error ( session, msg ) == 0 ) | ||
886 | return 0; | ||
887 | |||
888 | if ( !msg->cryptokey.header_value ) | ||
889 | return handle_error ( session, error_no_crypto_key, msg->friend_id ); | ||
890 | |||
891 | session->call->state = call_active; | ||
892 | |||
893 | session->call->key_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); | ||
894 | memcpy ( session->call->key_peer, msg->cryptokey.header_value, crypto_secretbox_KEYBYTES ); | ||
895 | |||
896 | session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); | ||
897 | memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES ); | ||
898 | |||
899 | flush_peer_type ( session, msg, 0 ); | ||
900 | |||
901 | event.rise ( callbacks[MSI_OnStart], session->agent_handler ); | ||
902 | |||
903 | return 1; | ||
904 | } | ||
905 | int handle_recv_reject ( MSISession *session, MSIMessage *msg ) | ||
906 | { | ||
907 | assert ( session ); | ||
908 | |||
909 | if ( has_call_error ( session, msg ) == 0 ) | ||
910 | return 0; | ||
911 | |||
912 | |||
913 | MSIMessage *_msg_end = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) ); | ||
914 | send_message ( session, _msg_end, msg->friend_id ); | ||
915 | free_message ( _msg_end ); | ||
916 | |||
917 | event.timer_release ( session->call->request_timer_id ); | ||
918 | event.rise ( callbacks[MSI_OnReject], session->agent_handler ); | ||
919 | session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout ); | ||
920 | |||
921 | return 1; | ||
922 | } | ||
923 | int handle_recv_cancel ( MSISession *session, MSIMessage *msg ) | ||
924 | { | ||
925 | assert ( session ); | ||
926 | |||
927 | if ( has_call_error ( session, msg ) == 0 ) | ||
928 | return 0; | ||
929 | |||
930 | |||
931 | terminate_call ( session ); | ||
932 | |||
933 | event.rise ( callbacks[MSI_OnCancel], session->agent_handler ); | ||
934 | |||
935 | return 1; | ||
936 | } | ||
937 | int handle_recv_end ( MSISession *session, MSIMessage *msg ) | ||
938 | { | ||
939 | assert ( session ); | ||
940 | |||
941 | if ( has_call_error ( session, msg ) == 0 ) | ||
942 | return 0; | ||
943 | |||
944 | |||
945 | MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) ); | ||
946 | send_message ( session, _msg_ending, msg->friend_id ); | ||
947 | free_message ( _msg_ending ); | ||
948 | |||
949 | terminate_call ( session ); | ||
950 | |||
951 | event.rise ( callbacks[MSI_OnEnd], session->agent_handler ); | ||
952 | |||
953 | return 1; | ||
954 | } | ||
955 | |||
956 | /********** Response handlers **********/ | ||
957 | int handle_recv_ringing ( MSISession *session, MSIMessage *msg ) | ||
958 | { | ||
959 | assert ( session ); | ||
960 | |||
961 | if ( has_call_error ( session, msg ) == 0 ) | ||
962 | return 0; | ||
963 | |||
964 | session->call->ringing_timer_id = event.timer_alloc ( handle_timeout, session, session->call->ringing_tout_ms ); | ||
965 | event.rise ( callbacks[MSI_OnRinging], session->agent_handler ); | ||
966 | |||
967 | return 1; | ||
968 | } | ||
969 | int handle_recv_starting ( MSISession *session, MSIMessage *msg ) | ||
970 | { | ||
971 | assert ( session ); | ||
972 | |||
973 | if ( has_call_error ( session, msg ) == 0 ) | ||
974 | return 0; | ||
975 | |||
976 | if ( !msg->cryptokey.header_value ) { | ||
977 | return handle_error ( session, error_no_crypto_key, msg->friend_id ); | ||
978 | } | ||
979 | |||
980 | /* Generate local key/nonce to send */ | ||
981 | session->call->key_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); | ||
982 | new_symmetric_key ( session->call->key_local ); | ||
983 | |||
984 | session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); | ||
985 | new_nonce ( session->call->nonce_local ); | ||
986 | |||
987 | /* Save peer key/nonce */ | ||
988 | session->call->key_peer = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); | ||
989 | memcpy ( session->call->key_peer, msg->cryptokey.header_value, crypto_secretbox_KEYBYTES ); | ||
990 | |||
991 | session->call->nonce_peer = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); | ||
992 | memcpy ( session->call->nonce_peer, msg->nonce.header_value, crypto_box_NONCEBYTES ); | ||
993 | |||
994 | session->call->state = call_active; | ||
995 | |||
996 | MSIMessage *_msg_start = msi_new_message ( TYPE_REQUEST, stringify_request ( start ) ); | ||
997 | msi_msg_set_cryptokey ( _msg_start, session->call->key_local, crypto_secretbox_KEYBYTES ); | ||
998 | msi_msg_set_nonce ( _msg_start, session->call->nonce_local, crypto_box_NONCEBYTES ); | ||
999 | send_message ( session, _msg_start, msg->friend_id ); | ||
1000 | free_message ( _msg_start ); | ||
1001 | |||
1002 | flush_peer_type ( session, msg, 0 ); | ||
1003 | |||
1004 | event.rise ( callbacks[MSI_OnStarting], session->agent_handler ); | ||
1005 | event.timer_release ( session->call->ringing_timer_id ); | ||
1006 | |||
1007 | return 1; | ||
1008 | } | ||
1009 | int handle_recv_ending ( MSISession *session, MSIMessage *msg ) | ||
1010 | { | ||
1011 | assert ( session ); | ||
1012 | |||
1013 | if ( has_call_error ( session, msg ) == 0 ) | ||
1014 | return 0; | ||
1015 | |||
1016 | |||
1017 | terminate_call ( session ); | ||
1018 | |||
1019 | event.rise ( callbacks[MSI_OnEnding], session->agent_handler ); | ||
1020 | |||
1021 | return 1; | ||
1022 | } | ||
1023 | int handle_recv_error ( MSISession *session, MSIMessage *msg ) | ||
1024 | { | ||
1025 | assert ( session ); | ||
1026 | assert ( session->call ); | ||
1027 | |||
1028 | /* Handle error accordingly */ | ||
1029 | if ( msg->reason.header_value ) { | ||
1030 | session->last_error_id = atoi ( ( const char * ) msg->reason.header_value ); | ||
1031 | session->last_error_str = stringify_error ( session->last_error_id ); | ||
1032 | } | ||
1033 | |||
1034 | terminate_call ( session ); | ||
1035 | |||
1036 | event.rise ( callbacks[MSI_OnEnding], session->agent_handler ); | ||
1037 | |||
1038 | return 1; | ||
1039 | } | ||
1040 | |||
1041 | |||
1042 | /** | ||
1043 | * @brief BASIC call flow: | ||
1044 | * | ||
1045 | * ALICE BOB | ||
1046 | * | invite --> | | ||
1047 | * | | | ||
1048 | * | <-- ringing | | ||
1049 | * | | | ||
1050 | * | <-- starting | | ||
1051 | * | | | ||
1052 | * | start --> | | ||
1053 | * | | | ||
1054 | * | <-- MEDIA TRANS --> | | ||
1055 | * | | | ||
1056 | * | end --> | | ||
1057 | * | | | ||
1058 | * | <-- ending | | ||
1059 | * | ||
1060 | * Alice calls Bob by sending invite packet. | ||
1061 | * Bob recvs the packet and sends an ringing packet; | ||
1062 | * which notifies Alice that her invite is acknowledged. | ||
1063 | * Ringing screen shown on both sides. | ||
1064 | * Bob accepts the invite for a call by sending starting packet. | ||
1065 | * Alice recvs the starting packet and sends the started packet to | ||
1066 | * inform Bob that she recved the starting packet. | ||
1067 | * Now the media transmission is established ( i.e. RTP transmission ). | ||
1068 | * Alice hangs up and sends end packet. | ||
1069 | * Bob recves the end packet and sends ending packet | ||
1070 | * as the acknowledgement that the call is ending. | ||
1071 | * | ||
1072 | * | ||
1073 | */ | ||
1074 | void msi_handle_packet ( Messenger *messenger, int source, uint8_t *data, uint16_t length, void *object ) | ||
1075 | { | ||
1076 | /* Unused */ | ||
1077 | (void)messenger; | ||
1078 | |||
1079 | MSISession *_session = object; | ||
1080 | MSIMessage *_msg; | ||
1081 | |||
1082 | if ( !length ) return; | ||
1083 | |||
1084 | _msg = parse_message ( data, length ); | ||
1085 | |||
1086 | if ( !_msg ) return; | ||
1087 | |||
1088 | _msg->friend_id = source; | ||
1089 | |||
1090 | |||
1091 | /* Now handle message */ | ||
1092 | |||
1093 | if ( _msg->request.header_value ) { /* Handle request */ | ||
1094 | |||
1095 | const uint8_t *_request_value = _msg->request.header_value; | ||
1096 | |||
1097 | if ( same ( _request_value, stringify_request ( invite ) ) ) { | ||
1098 | handle_recv_invite ( _session, _msg ); | ||
1099 | |||
1100 | } else if ( same ( _request_value, stringify_request ( start ) ) ) { | ||
1101 | handle_recv_start ( _session, _msg ); | ||
1102 | |||
1103 | } else if ( same ( _request_value, stringify_request ( cancel ) ) ) { | ||
1104 | handle_recv_cancel ( _session, _msg ); | ||
1105 | |||
1106 | } else if ( same ( _request_value, stringify_request ( reject ) ) ) { | ||
1107 | handle_recv_reject ( _session, _msg ); | ||
1108 | |||
1109 | } else if ( same ( _request_value, stringify_request ( end ) ) ) { | ||
1110 | handle_recv_end ( _session, _msg ); | ||
1111 | } | ||
1112 | |||
1113 | else { | ||
1114 | free_message ( _msg ); | ||
1115 | return; | ||
1116 | } | ||
1117 | |||
1118 | } else if ( _msg->response.header_value ) { /* Handle response */ | ||
1119 | |||
1120 | const uint8_t *_response_value = _msg->response.header_value; | ||
1121 | |||
1122 | if ( same ( _response_value, stringify_response ( ringing ) ) ) { | ||
1123 | handle_recv_ringing ( _session, _msg ); | ||
1124 | |||
1125 | } else if ( same ( _response_value, stringify_response ( starting ) ) ) { | ||
1126 | handle_recv_starting ( _session, _msg ); | ||
1127 | |||
1128 | } else if ( same ( _response_value, stringify_response ( ending ) ) ) { | ||
1129 | handle_recv_ending ( _session, _msg ); | ||
1130 | |||
1131 | } else if ( same ( _response_value, stringify_response ( error ) ) ) { | ||
1132 | handle_recv_error ( _session, _msg ); | ||
1133 | } else { | ||
1134 | free_message ( _msg ); | ||
1135 | return; | ||
1136 | } | ||
1137 | |||
1138 | /* Got response so cancel timer */ | ||
1139 | if ( _session->call ) | ||
1140 | event.timer_release ( _session->call->request_timer_id ); | ||
1141 | |||
1142 | } | ||
1143 | |||
1144 | free_message ( _msg ); | ||
1145 | } | ||
1146 | |||
1147 | |||
1148 | /******************************************************************************************************************** | ||
1149 | * ******************************************************************************************************************* | ||
1150 | ******************************************************************************************************************** | ||
1151 | ******************************************************************************************************************** | ||
1152 | ******************************************************************************************************************** | ||
1153 | * | ||
1154 | * | ||
1155 | * | ||
1156 | * PUBLIC API FUNCTIONS IMPLEMENTATIONS | ||
1157 | * | ||
1158 | * | ||
1159 | * | ||
1160 | ******************************************************************************************************************** | ||
1161 | ******************************************************************************************************************** | ||
1162 | ******************************************************************************************************************** | ||
1163 | ******************************************************************************************************************** | ||
1164 | ********************************************************************************************************************/ | ||
1165 | |||
1166 | |||
1167 | |||
1168 | |||
1169 | |||
1170 | |||
1171 | |||
1172 | |||
1173 | /** | ||
1174 | * @brief Callback setter. | ||
1175 | * | ||
1176 | * @param callback The callback. | ||
1177 | * @param id The id. | ||
1178 | * @return void | ||
1179 | */ | ||
1180 | void msi_register_callback ( MSICallback callback, MSICallbackID id ) | ||
1181 | { | ||
1182 | callbacks[id] = callback; | ||
1183 | } | ||
1184 | |||
1185 | |||
1186 | /** | ||
1187 | * @brief Start the control session. | ||
1188 | * | ||
1189 | * @param messenger Tox* object. | ||
1190 | * @param user_agent User agent, i.e. 'Venom'; 'QT-gui' | ||
1191 | * @return MSISession* The created session. | ||
1192 | * @retval NULL Error occured. | ||
1193 | */ | ||
1194 | MSISession *msi_init_session ( Messenger *messenger, const uint8_t *ua_name ) | ||
1195 | { | ||
1196 | assert ( messenger ); | ||
1197 | |||
1198 | MSISession *_retu = calloc ( sizeof ( MSISession ), 1 ); | ||
1199 | assert ( _retu ); | ||
1200 | |||
1201 | _retu->ua_name = ua_name; | ||
1202 | _retu->messenger_handle = messenger; | ||
1203 | _retu->agent_handler = NULL; | ||
1204 | |||
1205 | _retu->call = NULL; | ||
1206 | |||
1207 | _retu->frequ = 10000; /* default value? */ | ||
1208 | _retu->call_timeout = 30000; /* default value? */ | ||
1209 | |||
1210 | |||
1211 | m_callback_msi_packet(messenger, msi_handle_packet, _retu ); | ||
1212 | |||
1213 | /* This is called when remote terminates session */ | ||
1214 | m_callback_connectionstatus_internal_av(messenger, handle_remote_connection_change, _retu); | ||
1215 | |||
1216 | return _retu; | ||
1217 | } | ||
1218 | |||
1219 | |||
1220 | /** | ||
1221 | * @brief Terminate control session. | ||
1222 | * | ||
1223 | * @param session The session | ||
1224 | * @return int | ||
1225 | */ | ||
1226 | int msi_terminate_session ( MSISession *session ) | ||
1227 | { | ||
1228 | assert ( session ); | ||
1229 | |||
1230 | int _status = 0; | ||
1231 | |||
1232 | terminate_call ( session ); | ||
1233 | m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL); | ||
1234 | |||
1235 | |||
1236 | /* TODO: Clean it up more? */ | ||
1237 | |||
1238 | free ( session ); | ||
1239 | return _status; | ||
1240 | } | ||
1241 | |||
1242 | |||
1243 | /** | ||
1244 | * @brief Send invite request to friend_id. | ||
1245 | * | ||
1246 | * @param session Control session. | ||
1247 | * @param call_type Type of the call. Audio or Video(both audio and video) | ||
1248 | * @param rngsec Ringing timeout. | ||
1249 | * @param friend_id The friend. | ||
1250 | * @return int | ||
1251 | */ | ||
1252 | int msi_invite ( MSISession *session, MSICallType call_type, uint32_t rngsec, uint32_t friend_id ) | ||
1253 | { | ||
1254 | assert ( session ); | ||
1255 | |||
1256 | MSIMessage *_msg_invite = msi_new_message ( TYPE_REQUEST, stringify_request ( invite ) ); | ||
1257 | |||
1258 | session->call = init_call ( session, 1, rngsec ); /* Just one for now */ | ||
1259 | t_randomstr ( session->call->id, CALL_ID_LEN ); | ||
1260 | |||
1261 | add_peer(session->call, friend_id ); | ||
1262 | |||
1263 | session->call->type_local = call_type; | ||
1264 | /* Do whatever with message */ | ||
1265 | |||
1266 | if ( call_type == type_audio ) { | ||
1267 | msi_msg_set_calltype | ||
1268 | ( _msg_invite, ( const uint8_t * ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) ); | ||
1269 | } else { | ||
1270 | msi_msg_set_calltype | ||
1271 | ( _msg_invite, ( const uint8_t * ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) ); | ||
1272 | } | ||
1273 | |||
1274 | send_message ( session, _msg_invite, friend_id ); | ||
1275 | free_message ( _msg_invite ); | ||
1276 | |||
1277 | session->call->state = call_inviting; | ||
1278 | |||
1279 | session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout ); | ||
1280 | |||
1281 | return 0; | ||
1282 | } | ||
1283 | |||
1284 | |||
1285 | /** | ||
1286 | * @brief Hangup active call. | ||
1287 | * | ||
1288 | * @param session Control session. | ||
1289 | * @return int | ||
1290 | * @retval -1 Error occured. | ||
1291 | * @retval 0 Success. | ||
1292 | */ | ||
1293 | int msi_hangup ( MSISession *session ) | ||
1294 | { | ||
1295 | assert ( session ); | ||
1296 | |||
1297 | if ( !session->call || session->call->state != call_active ) | ||
1298 | return -1; | ||
1299 | |||
1300 | MSIMessage *_msg_ending = msi_new_message ( TYPE_REQUEST, stringify_request ( end ) ); | ||
1301 | |||
1302 | /* hangup for each peer */ | ||
1303 | int _it = 0; | ||
1304 | |||
1305 | for ( ; _it < session->call->peer_count; _it ++ ) | ||
1306 | send_message ( session, _msg_ending, session->call->peers[_it] ); | ||
1307 | |||
1308 | |||
1309 | free_message ( _msg_ending ); | ||
1310 | |||
1311 | session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout ); | ||
1312 | |||
1313 | return 0; | ||
1314 | } | ||
1315 | |||
1316 | |||
1317 | /** | ||
1318 | * @brief Answer active call request. | ||
1319 | * | ||
1320 | * @param session Control session. | ||
1321 | * @param call_type Answer with Audio or Video(both). | ||
1322 | * @return int | ||
1323 | */ | ||
1324 | int msi_answer ( MSISession *session, MSICallType call_type ) | ||
1325 | { | ||
1326 | assert ( session ); | ||
1327 | |||
1328 | MSIMessage *_msg_starting = msi_new_message ( TYPE_RESPONSE, stringify_response ( starting ) ); | ||
1329 | session->call->type_local = call_type; | ||
1330 | |||
1331 | if ( call_type == type_audio ) { | ||
1332 | msi_msg_set_calltype | ||
1333 | ( _msg_starting, ( const uint8_t * ) CT_AUDIO_HEADER_VALUE, strlen ( CT_AUDIO_HEADER_VALUE ) ); | ||
1334 | } else { | ||
1335 | msi_msg_set_calltype | ||
1336 | ( _msg_starting, ( const uint8_t * ) CT_VIDEO_HEADER_VALUE, strlen ( CT_VIDEO_HEADER_VALUE ) ); | ||
1337 | } | ||
1338 | |||
1339 | /* Now set the local encryption key and pass it with STARTING message */ | ||
1340 | |||
1341 | session->call->key_local = calloc ( sizeof ( uint8_t ), crypto_secretbox_KEYBYTES ); | ||
1342 | new_symmetric_key ( session->call->key_local ); | ||
1343 | |||
1344 | session->call->nonce_local = calloc ( sizeof ( uint8_t ), crypto_box_NONCEBYTES ); | ||
1345 | new_nonce ( session->call->nonce_local ); | ||
1346 | |||
1347 | msi_msg_set_cryptokey ( _msg_starting, session->call->key_local, crypto_secretbox_KEYBYTES ); | ||
1348 | msi_msg_set_nonce ( _msg_starting, session->call->nonce_local, crypto_box_NONCEBYTES ); | ||
1349 | |||
1350 | send_message ( session, _msg_starting, session->call->peers[session->call->peer_count - 1] ); | ||
1351 | free_message ( _msg_starting ); | ||
1352 | |||
1353 | session->call->state = call_active; | ||
1354 | |||
1355 | return 0; | ||
1356 | } | ||
1357 | |||
1358 | |||
1359 | /** | ||
1360 | * @brief Cancel request. | ||
1361 | * | ||
1362 | * @param session Control session. | ||
1363 | * @param reason Set optional reason header. Pass NULL if none. | ||
1364 | * @return int | ||
1365 | */ | ||
1366 | int msi_cancel ( MSISession *session, uint32_t peer, const uint8_t *reason ) | ||
1367 | { | ||
1368 | assert ( session ); | ||
1369 | |||
1370 | MSIMessage *_msg_cancel = msi_new_message ( TYPE_REQUEST, stringify_request ( cancel ) ); | ||
1371 | |||
1372 | if ( reason ) msi_msg_set_reason(_msg_cancel, reason, strlen((const char *)reason)); | ||
1373 | |||
1374 | send_message ( session, _msg_cancel, peer ); | ||
1375 | free_message ( _msg_cancel ); | ||
1376 | |||
1377 | terminate_call ( session ); | ||
1378 | |||
1379 | return 0; | ||
1380 | } | ||
1381 | |||
1382 | |||
1383 | /** | ||
1384 | * @brief Reject request. | ||
1385 | * | ||
1386 | * @param session Control session. | ||
1387 | * @return int | ||
1388 | */ | ||
1389 | int msi_reject ( MSISession *session, const uint8_t *reason ) | ||
1390 | { | ||
1391 | assert ( session ); | ||
1392 | |||
1393 | MSIMessage *_msg_reject = msi_new_message ( TYPE_REQUEST, stringify_request ( reject ) ); | ||
1394 | |||
1395 | if ( reason ) msi_msg_set_reason(_msg_reject, reason, strlen((const char *)reason) + 1); | ||
1396 | |||
1397 | send_message ( session, _msg_reject, session->call->peers[session->call->peer_count - 1] ); | ||
1398 | free_message ( _msg_reject ); | ||
1399 | |||
1400 | session->call->request_timer_id = event.timer_alloc ( handle_timeout, session, m_deftout ); | ||
1401 | |||
1402 | return 0; | ||
1403 | } | ||
1404 | |||
1405 | |||
1406 | /** | ||
1407 | * @brief Terminate the current call. | ||
1408 | * | ||
1409 | * @param session Control session. | ||
1410 | * @return int | ||
1411 | */ | ||
1412 | int msi_stopcall ( MSISession *session ) | ||
1413 | { | ||
1414 | assert ( session ); | ||
1415 | |||
1416 | if ( !session->call ) | ||
1417 | return -1; | ||
1418 | |||
1419 | /* just terminate it */ | ||
1420 | |||
1421 | terminate_call ( session ); | ||
1422 | |||
1423 | return 0; | ||
1424 | } \ No newline at end of file | ||
diff --git a/toxav/msi.h b/toxav/msi.h new file mode 100644 index 00000000..84a30f63 --- /dev/null +++ b/toxav/msi.h | |||
@@ -0,0 +1,233 @@ | |||
1 | /** toxmsi.h | ||
2 | * | ||
3 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
4 | * | ||
5 | * This file is part of Tox. | ||
6 | * | ||
7 | * Tox is free software: you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation, either version 3 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * Tox is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | * | ||
21 | * Report bugs/suggestions at #tox-dev @ freenode.net:6667 | ||
22 | */ | ||
23 | |||
24 | #ifndef __TOXMSI | ||
25 | #define __TOXMSI | ||
26 | |||
27 | #include <inttypes.h> | ||
28 | #include <pthread.h> | ||
29 | |||
30 | #include "../toxcore/Messenger.h" | ||
31 | |||
32 | /* define size for call_id */ | ||
33 | #define CALL_ID_LEN 12 | ||
34 | |||
35 | |||
36 | typedef void *( *MSICallback ) ( void *arg ); | ||
37 | |||
38 | |||
39 | /** | ||
40 | * @brief Call type identifier. Also used as rtp callback prefix. | ||
41 | */ | ||
42 | typedef enum { | ||
43 | type_audio = 70, | ||
44 | type_video | ||
45 | } MSICallType; | ||
46 | |||
47 | |||
48 | /** | ||
49 | * @brief Call state identifiers. | ||
50 | */ | ||
51 | typedef enum { | ||
52 | call_inviting, /* when sending call invite */ | ||
53 | call_starting, /* when getting call invite */ | ||
54 | call_active, | ||
55 | call_hold | ||
56 | |||
57 | } MSICallState; | ||
58 | |||
59 | |||
60 | |||
61 | /** | ||
62 | * @brief The call struct. | ||
63 | * | ||
64 | */ | ||
65 | typedef struct _MSICall { /* Call info structure */ | ||
66 | MSICallState state; | ||
67 | |||
68 | MSICallType type_local; /* Type of payload user is ending */ | ||
69 | MSICallType *type_peer; /* Type of payload others are sending */ | ||
70 | |||
71 | uint8_t id[CALL_ID_LEN]; /* Random value identifying the call */ | ||
72 | |||
73 | uint8_t *key_local; /* The key for encryption */ | ||
74 | uint8_t *key_peer; /* The key for decryption */ | ||
75 | |||
76 | uint8_t *nonce_local; /* Local nonce */ | ||
77 | uint8_t *nonce_peer; /* Peer nonce */ | ||
78 | |||
79 | int ringing_tout_ms; /* Ringing timeout in ms */ | ||
80 | |||
81 | int request_timer_id; /* Timer id for outgoing request/action */ | ||
82 | int ringing_timer_id; /* Timer id for ringing timeout */ | ||
83 | |||
84 | pthread_mutex_t mutex; /* It's to be assumed that call will have | ||
85 | * seperate thread so add mutex | ||
86 | */ | ||
87 | uint32_t *peers; | ||
88 | uint16_t peer_count; | ||
89 | |||
90 | |||
91 | } MSICall; | ||
92 | |||
93 | |||
94 | /** | ||
95 | * @brief Control session struct | ||
96 | * | ||
97 | */ | ||
98 | typedef struct _MSISession { | ||
99 | |||
100 | /* Call handler */ | ||
101 | struct _MSICall *call; | ||
102 | |||
103 | int last_error_id; /* Determine the last error */ | ||
104 | const uint8_t *last_error_str; | ||
105 | |||
106 | const uint8_t *ua_name; | ||
107 | |||
108 | void *agent_handler; /* Pointer to an object that is handling msi */ | ||
109 | Messenger *messenger_handle; | ||
110 | |||
111 | uint32_t frequ; | ||
112 | uint32_t call_timeout; /* Time of the timeout for some action to end; 0 if infinite */ | ||
113 | |||
114 | |||
115 | } MSISession; | ||
116 | |||
117 | |||
118 | /** | ||
119 | * @brief Callbacks ids that handle the states | ||
120 | */ | ||
121 | typedef enum { | ||
122 | /* Requests */ | ||
123 | MSI_OnInvite, | ||
124 | MSI_OnStart, | ||
125 | MSI_OnCancel, | ||
126 | MSI_OnReject, | ||
127 | MSI_OnEnd, | ||
128 | |||
129 | /* Responses */ | ||
130 | MSI_OnRinging, | ||
131 | MSI_OnStarting, | ||
132 | MSI_OnEnding, | ||
133 | |||
134 | /* Protocol */ | ||
135 | MSI_OnError, | ||
136 | MSI_OnRequestTimeout | ||
137 | |||
138 | } MSICallbackID; | ||
139 | |||
140 | |||
141 | /** | ||
142 | * @brief Callback setter. | ||
143 | * | ||
144 | * @param callback The callback. | ||
145 | * @param id The id. | ||
146 | * @return void | ||
147 | */ | ||
148 | void msi_register_callback(MSICallback callback, MSICallbackID id); | ||
149 | |||
150 | |||
151 | /** | ||
152 | * @brief Start the control session. | ||
153 | * | ||
154 | * @param messenger Tox* object. | ||
155 | * @param user_agent User agent, i.e. 'Venom'; 'QT-gui' | ||
156 | * @return MSISession* The created session. | ||
157 | * @retval NULL Error occured. | ||
158 | */ | ||
159 | MSISession *msi_init_session ( Messenger *messenger, const uint8_t *ua_name ); | ||
160 | |||
161 | |||
162 | /** | ||
163 | * @brief Terminate control session. | ||
164 | * | ||
165 | * @param session The session | ||
166 | * @return int | ||
167 | */ | ||
168 | int msi_terminate_session ( MSISession *session ); | ||
169 | |||
170 | |||
171 | /** | ||
172 | * @brief Send invite request to friend_id. | ||
173 | * | ||
174 | * @param session Control session. | ||
175 | * @param call_type Type of the call. Audio or Video(both audio and video) | ||
176 | * @param rngsec Ringing timeout. | ||
177 | * @param friend_id The friend. | ||
178 | * @return int | ||
179 | */ | ||
180 | int msi_invite ( MSISession *session, MSICallType call_type, uint32_t rngsec, uint32_t friend_id ); | ||
181 | |||
182 | |||
183 | /** | ||
184 | * @brief Hangup active call. | ||
185 | * | ||
186 | * @param session Control session. | ||
187 | * @return int | ||
188 | * @retval -1 Error occured. | ||
189 | * @retval 0 Success. | ||
190 | */ | ||
191 | int msi_hangup ( MSISession *session ); | ||
192 | |||
193 | |||
194 | /** | ||
195 | * @brief Answer active call request. | ||
196 | * | ||
197 | * @param session Control session. | ||
198 | * @param call_type Answer with Audio or Video(both). | ||
199 | * @return int | ||
200 | */ | ||
201 | int msi_answer ( MSISession *session, MSICallType call_type ); | ||
202 | |||
203 | |||
204 | /** | ||
205 | * @brief Cancel request. | ||
206 | * | ||
207 | * @param session Control session. | ||
208 | * @param peer To which peer. | ||
209 | * @param reason Set optional reason header. Pass NULL if none. | ||
210 | * @return int | ||
211 | */ | ||
212 | int msi_cancel ( MSISession *session, uint32_t peer, const uint8_t *reason ); | ||
213 | |||
214 | |||
215 | /** | ||
216 | * @brief Reject request. | ||
217 | * | ||
218 | * @param session Control session. | ||
219 | * @param reason Set optional reason header. Pass NULL if none. | ||
220 | * @return int | ||
221 | */ | ||
222 | int msi_reject ( MSISession *session, const uint8_t *reason ); | ||
223 | |||
224 | |||
225 | /** | ||
226 | * @brief Terminate the current call. | ||
227 | * | ||
228 | * @param session Control session. | ||
229 | * @return int | ||
230 | */ | ||
231 | int msi_stopcall ( MSISession *session ); | ||
232 | |||
233 | #endif /* __TOXMSI */ | ||
diff --git a/toxav/phone.c b/toxav/phone.c new file mode 100644 index 00000000..ec7dd143 --- /dev/null +++ b/toxav/phone.c | |||
@@ -0,0 +1,1352 @@ | |||
1 | /** phone.c | ||
2 | * | ||
3 | * NOTE NOTE NOTE NOTE NOTE NOTE | ||
4 | * | ||
5 | * This file is for testing/reference purposes only, hence | ||
6 | * it is _poorly_ designed and it does not fully reflect the | ||
7 | * quaility of msi nor rtp. Although toxmsi* and toxrtp* are tested | ||
8 | * there is always possiblity of crashes. If crash occures, | ||
9 | * contact me ( mannol ) on either irc channel #tox-dev @ freenode.net:6667 | ||
10 | * or eniz_vukovic@hotmail.com | ||
11 | * | ||
12 | * NOTE NOTE NOTE NOTE NOTE NOTE | ||
13 | * | ||
14 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
15 | * | ||
16 | * This file is part of Tox. | ||
17 | * | ||
18 | * Tox is free software: you can redistribute it and/or modify | ||
19 | * it under the terms of the GNU General Public License as published by | ||
20 | * the Free Software Foundation, either version 3 of the License, or | ||
21 | * (at your option) any later version. | ||
22 | * | ||
23 | * Tox is distributed in the hope that it will be useful, | ||
24 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
25 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
26 | * GNU General Public License for more details. | ||
27 | * | ||
28 | * You should have received a copy of the GNU General Public License | ||
29 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
30 | * | ||
31 | */ | ||
32 | |||
33 | #ifdef HAVE_CONFIG_H | ||
34 | #include "config.h" | ||
35 | #endif /* HAVE_CONFIG_H */ | ||
36 | |||
37 | #define _BSD_SOURCE | ||
38 | |||
39 | #include <stdio.h> | ||
40 | #include <string.h> | ||
41 | #include <stdlib.h> | ||
42 | #include <stdarg.h> | ||
43 | #include <unistd.h> | ||
44 | #include <assert.h> | ||
45 | #include <math.h> | ||
46 | #include <pthread.h> | ||
47 | |||
48 | //#include "media.h" | ||
49 | #include "toxav.h" | ||
50 | #include "event.h" | ||
51 | #include "../toxcore/tox.h" | ||
52 | |||
53 | #ifdef TOX_FFMPEG | ||
54 | /* Video encoding/decoding */ | ||
55 | #include <libavcodec/avcodec.h> | ||
56 | #include <libavformat/avformat.h> | ||
57 | #include <libswscale/swscale.h> | ||
58 | #include <libavdevice/avdevice.h> | ||
59 | #include <libavutil/opt.h> | ||
60 | #endif | ||
61 | |||
62 | #include <AL/al.h> | ||
63 | #include <AL/alc.h> | ||
64 | #include <SDL/SDL.h> | ||
65 | #include <SDL/SDL_thread.h> | ||
66 | |||
67 | /* the quit event for SDL */ | ||
68 | #define FF_QUIT_EVENT (SDL_USEREVENT + 2) | ||
69 | |||
70 | #ifdef __linux__ | ||
71 | #define VIDEO_DRIVER "video4linux2" | ||
72 | #define DEFAULT_WEBCAM "/dev/video0" | ||
73 | #endif | ||
74 | |||
75 | #if defined(_WIN32) || defined(__WIN32__) || defined (WIN32) | ||
76 | #define VIDEO_DRIVER "vfwcap" | ||
77 | #define DEFAULT_WEBCAM "0" | ||
78 | #endif | ||
79 | |||
80 | |||
81 | /* Define client version */ | ||
82 | #define _USERAGENT "v.0.3.0" | ||
83 | |||
84 | |||
85 | struct SDL_Surface *screen; | ||
86 | |||
87 | typedef struct { | ||
88 | struct SDL_Overlay *bmp; | ||
89 | int width, height; | ||
90 | } VideoPicture; | ||
91 | |||
92 | |||
93 | typedef struct av_friend_s { | ||
94 | int _id; | ||
95 | int _active; /* 0=false; 1=true; */ | ||
96 | } av_friend_t; | ||
97 | |||
98 | typedef struct av_session_s { | ||
99 | /* Encoding/decoding/capturing/playing */ | ||
100 | ToxAv *av; | ||
101 | |||
102 | VideoPicture video_picture; | ||
103 | struct ALCdevice *audio_capture_device; | ||
104 | |||
105 | /* context for converting image format to something SDL can use*/ | ||
106 | struct SwsContext *sws_SDL_r_ctx; | ||
107 | |||
108 | /* context for converting webcam image format to something the video encoder can use */ | ||
109 | struct SwsContext *sws_ctx; | ||
110 | |||
111 | /* Thread running control */ | ||
112 | int running_decaud, running_encaud, | ||
113 | running_decvid, running_encvid; | ||
114 | |||
115 | pthread_mutex_t _mutex; | ||
116 | |||
117 | Tox *_messenger; | ||
118 | av_friend_t *_friends; | ||
119 | int _friend_cout; | ||
120 | char _my_public_id[200]; | ||
121 | #ifdef TOX_FFMPEG | ||
122 | AVInputFormat *video_input_format; | ||
123 | AVFormatContext *video_format_ctx; | ||
124 | uint8_t video_stream; | ||
125 | AVCodecContext *webcam_decoder_ctx; | ||
126 | AVCodec *webcam_decoder; | ||
127 | #endif | ||
128 | } av_session_t; | ||
129 | |||
130 | |||
131 | void av_allocate_friend(av_session_t *_phone, int _id, int _active) | ||
132 | { | ||
133 | static int _new_id = 0; | ||
134 | |||
135 | if ( !_phone->_friends ) { | ||
136 | _phone->_friends = calloc(sizeof(av_friend_t), 1); | ||
137 | _phone->_friend_cout = 1; | ||
138 | } else { | ||
139 | _phone->_friend_cout ++; | ||
140 | _phone->_friends = realloc(_phone->_friends, sizeof(av_friend_t) * _phone->_friend_cout); | ||
141 | } | ||
142 | |||
143 | if ( _id == -1 ) { | ||
144 | _phone->_friends->_id = _new_id; | ||
145 | _new_id ++; | ||
146 | } else _phone->_friends->_id = _id; | ||
147 | |||
148 | _phone->_friends->_active = _active; | ||
149 | } | ||
150 | av_friend_t *av_get_friend(av_session_t *_phone, int _id) | ||
151 | { | ||
152 | av_friend_t *_friends = _phone->_friends; | ||
153 | |||
154 | if ( !_friends ) return NULL; | ||
155 | |||
156 | int _it = 0; | ||
157 | |||
158 | for (; _it < _phone->_friend_cout; _it ++) | ||
159 | if ( _friends[_it]._id == _id ) | ||
160 | return _friends + _it; | ||
161 | |||
162 | return NULL; | ||
163 | } | ||
164 | |||
165 | |||
166 | /***************** MISC *****************/ | ||
167 | |||
168 | void INFO (const char *_format, ...) | ||
169 | { | ||
170 | printf("\r[!] "); | ||
171 | va_list _arg; | ||
172 | va_start (_arg, _format); | ||
173 | vfprintf (stdout, _format, _arg); | ||
174 | va_end (_arg); | ||
175 | printf("\n\r >> "); | ||
176 | fflush(stdout); | ||
177 | } | ||
178 | |||
179 | unsigned char *hex_string_to_bin(char hex_string[]) | ||
180 | { | ||
181 | size_t i, len = strlen(hex_string); | ||
182 | unsigned char *val = calloc(sizeof(unsigned char), len); | ||
183 | char *pos = hex_string; | ||
184 | |||
185 | for (i = 0; i < len; ++i, pos += 2) | ||
186 | sscanf(pos, "%2hhx", &val[i]); | ||
187 | |||
188 | return val; | ||
189 | } | ||
190 | |||
191 | int getinput( char *_buff, size_t _limit, int *_len ) | ||
192 | { | ||
193 | if ( fgets(_buff, _limit, stdin) == NULL ) | ||
194 | return -1; | ||
195 | |||
196 | *_len = strlen(_buff) - 1; | ||
197 | |||
198 | /* Get rid of newline */ | ||
199 | _buff[*_len] = '\0'; | ||
200 | |||
201 | return 0; | ||
202 | } | ||
203 | |||
204 | char *trim_spaces ( char *buff ) | ||
205 | { | ||
206 | |||
207 | int _i = 0, _len = strlen(buff); | ||
208 | |||
209 | char *container = calloc(sizeof(char), _len); | ||
210 | int _ci = 0; | ||
211 | |||
212 | for ( ; _i < _len; _i++ ) { | ||
213 | while ( _i < _len && buff[_i] == ' ' ) | ||
214 | _i++; | ||
215 | |||
216 | if ( _i < _len ) { | ||
217 | container[_ci] = buff[_i]; | ||
218 | _ci ++; | ||
219 | } | ||
220 | } | ||
221 | |||
222 | memcpy( buff, container, _ci ); | ||
223 | buff[_ci] = '\0'; | ||
224 | free(container); | ||
225 | return buff; | ||
226 | } | ||
227 | |||
228 | #define FRADDR_TOSTR_CHUNK_LEN 8 | ||
229 | |||
230 | static void fraddr_to_str(uint8_t *id_bin, char *id_str) | ||
231 | { | ||
232 | uint i, delta = 0, pos_extra = 0, sum_extra = 0; | ||
233 | |||
234 | for (i = 0; i < TOX_FRIEND_ADDRESS_SIZE; i++) { | ||
235 | sprintf(&id_str[2 * i + delta], "%02hhX", id_bin[i]); | ||
236 | |||
237 | if ((i + 1) == TOX_CLIENT_ID_SIZE) | ||
238 | pos_extra = 2 * (i + 1) + delta; | ||
239 | |||
240 | if (i >= TOX_CLIENT_ID_SIZE) | ||
241 | sum_extra |= id_bin[i]; | ||
242 | |||
243 | if (!((i + 1) % FRADDR_TOSTR_CHUNK_LEN)) { | ||
244 | id_str[2 * (i + 1) + delta] = ' '; | ||
245 | delta++; | ||
246 | } | ||
247 | } | ||
248 | |||
249 | id_str[2 * i + delta] = 0; | ||
250 | |||
251 | if (!sum_extra) | ||
252 | id_str[pos_extra] = 0; | ||
253 | } | ||
254 | |||
255 | /********************************************* | ||
256 | ********************************************* | ||
257 | ********************************************* | ||
258 | ********************************************* | ||
259 | ********************************************* | ||
260 | ********************************************* | ||
261 | ********************************************* | ||
262 | ********************************************* | ||
263 | */ | ||
264 | |||
265 | |||
266 | /* | ||
267 | * How av stuff _should_ look like | ||
268 | */ | ||
269 | /* | ||
270 | int display_received_frame(av_session_t* _phone, vpx_image_t *image) | ||
271 | { | ||
272 | CodecState* cs = get_cs_temp(_phone->av); | ||
273 | AVPicture pict; | ||
274 | SDL_LockYUVOverlay(_phone->video_picture.bmp); | ||
275 | |||
276 | pict.data[0] = _phone->video_picture.bmp->pixels[0]; | ||
277 | pict.data[1] = _phone->video_picture.bmp->pixels[2]; | ||
278 | pict.data[2] = _phone->video_picture.bmp->pixels[1]; | ||
279 | pict.linesize[0] = _phone->video_picture.bmp->pitches[0]; | ||
280 | pict.linesize[1] = _phone->video_picture.bmp->pitches[2]; | ||
281 | pict.linesize[2] = _phone->video_picture.bmp->pitches[1]; | ||
282 | */ | ||
283 | /* Convert the image into YUV format that SDL uses *//* | ||
284 | sws_scale(_phone->sws_SDL_r_ctx, (uint8_t const * const *)r_video_frame->data, r_video_frame->linesize, 0, | ||
285 | cs->video_decoder_ctx->height, pict.data, pict.linesize ); | ||
286 | |||
287 | SDL_UnlockYUVOverlay(_phone->video_picture.bmp); | ||
288 | SDL_Rect rect; | ||
289 | rect.x = 0; | ||
290 | rect.y = 0; | ||
291 | rect.w = cs->video_decoder_ctx->width; | ||
292 | rect.h = cs->video_decoder_ctx->height; | ||
293 | SDL_DisplayYUVOverlay(_phone->video_picture.bmp, &rect); | ||
294 | return 1; | ||
295 | } | ||
296 | */ | ||
297 | #ifdef TOX_FFMPEG | ||
298 | void *encode_video_thread(void *arg) | ||
299 | { | ||
300 | INFO("Started encode video thread!"); | ||
301 | |||
302 | av_session_t *_phone = arg; | ||
303 | |||
304 | _phone->running_encvid = 1; | ||
305 | //CodecState *cs = get_cs_temp(_phone->av); | ||
306 | AVPacket pkt1, *packet = &pkt1; | ||
307 | //int p = 0; | ||
308 | //int got_packet; | ||
309 | int video_frame_finished; | ||
310 | AVFrame *s_video_frame; | ||
311 | AVFrame *webcam_frame; | ||
312 | s_video_frame = avcodec_alloc_frame(); | ||
313 | webcam_frame = avcodec_alloc_frame(); | ||
314 | //AVPacket enc_video_packet; | ||
315 | |||
316 | uint8_t *buffer; | ||
317 | int numBytes; | ||
318 | /* Determine required buffer size and allocate buffer */ | ||
319 | numBytes = avpicture_get_size(PIX_FMT_YUV420P, _phone->webcam_decoder_ctx->width, _phone->webcam_decoder_ctx->height); | ||
320 | buffer = (uint8_t *)av_calloc(numBytes * sizeof(uint8_t), 1); | ||
321 | avpicture_fill((AVPicture *)s_video_frame, buffer, PIX_FMT_YUV420P, _phone->webcam_decoder_ctx->width, | ||
322 | _phone->webcam_decoder_ctx->height); | ||
323 | _phone->sws_ctx = sws_getContext(_phone->webcam_decoder_ctx->width, _phone->webcam_decoder_ctx->height, | ||
324 | _phone->webcam_decoder_ctx->pix_fmt, _phone->webcam_decoder_ctx->width, _phone->webcam_decoder_ctx->height, | ||
325 | PIX_FMT_YUV420P, | ||
326 | SWS_BILINEAR, NULL, NULL, NULL); | ||
327 | |||
328 | |||
329 | vpx_image_t *image = | ||
330 | vpx_img_alloc(NULL, VPX_IMG_FMT_I420, _phone->webcam_decoder_ctx->width, _phone->webcam_decoder_ctx->height, 1); | ||
331 | |||
332 | //uint32_t frame_counter = 0; | ||
333 | while (_phone->running_encvid) { | ||
334 | |||
335 | if (av_read_frame(_phone->video_format_ctx, packet) < 0) { | ||
336 | printf("error reading frame\n"); | ||
337 | |||
338 | if (_phone->video_format_ctx->pb->error != 0) | ||
339 | break; | ||
340 | |||
341 | continue; | ||
342 | } | ||
343 | |||
344 | if (packet->stream_index == _phone->video_stream) { | ||
345 | if (avcodec_decode_video2(_phone->webcam_decoder_ctx, webcam_frame, &video_frame_finished, packet) < 0) { | ||
346 | printf("couldn't decode\n"); | ||
347 | continue; | ||
348 | } | ||
349 | |||
350 | av_free_packet(packet); | ||
351 | sws_scale(_phone->sws_ctx, (uint8_t const * const *)webcam_frame->data, webcam_frame->linesize, 0, | ||
352 | _phone->webcam_decoder_ctx->height, s_video_frame->data, s_video_frame->linesize); | ||
353 | /* create a new I-frame every 60 frames */ | ||
354 | //++p; | ||
355 | /* | ||
356 | if (p == 60) { | ||
357 | |||
358 | s_video_frame->pict_type = AV_PICTURE_TYPE_BI ; | ||
359 | } else if (p == 61) { | ||
360 | s_video_frame->pict_type = AV_PICTURE_TYPE_I ; | ||
361 | p = 0; | ||
362 | } else { | ||
363 | s_video_frame->pict_type = AV_PICTURE_TYPE_P ; | ||
364 | }*/ | ||
365 | |||
366 | if (video_frame_finished) { | ||
367 | memcpy(image->planes[VPX_PLANE_Y], s_video_frame->data[0], | ||
368 | s_video_frame->linesize[0] * _phone->webcam_decoder_ctx->height); | ||
369 | memcpy(image->planes[VPX_PLANE_U], s_video_frame->data[1], | ||
370 | s_video_frame->linesize[1] * _phone->webcam_decoder_ctx->height / 2); | ||
371 | memcpy(image->planes[VPX_PLANE_V], s_video_frame->data[2], | ||
372 | s_video_frame->linesize[2] * _phone->webcam_decoder_ctx->height / 2); | ||
373 | toxav_send_video (_phone->av, image); | ||
374 | //if (avcodec_encode_video2(cs->video_encoder_ctx, &enc_video_packet, s_video_frame, &got_packet) < 0) { | ||
375 | /*if (vpx_codec_encode(&cs->v_encoder, image, frame_counter, 1, 0, 0) != VPX_CODEC_OK) { | ||
376 | printf("could not encode video frame\n"); | ||
377 | continue; | ||
378 | } | ||
379 | ++frame_counter; | ||
380 | |||
381 | vpx_codec_iter_t iter = NULL; | ||
382 | vpx_codec_cx_pkt_t *pkt; | ||
383 | while( (pkt = vpx_codec_get_cx_data(&cs->v_encoder, &iter)) ) { | ||
384 | if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) | ||
385 | toxav_send_rtp_payload(_phone->av, TypeVideo, pkt->data.frame.buf, pkt->data.frame.sz); | ||
386 | }*/ | ||
387 | //if (!got_packet) { | ||
388 | // continue; | ||
389 | //} | ||
390 | |||
391 | //if (!enc_video_packet.data) fprintf(stderr, "video packet data is NULL\n"); | ||
392 | |||
393 | //toxav_send_rtp_payload(_phone->av, TypeVideo, enc_video_packet.data, enc_video_packet.size); | ||
394 | |||
395 | //av_free_packet(&enc_video_packet); | ||
396 | } | ||
397 | } else { | ||
398 | av_free_packet(packet); | ||
399 | } | ||
400 | } | ||
401 | |||
402 | vpx_img_free(image); | ||
403 | |||
404 | /* clean up codecs */ | ||
405 | //pthread_mutex_lock(&cs->ctrl_mutex); | ||
406 | av_free(buffer); | ||
407 | av_free(webcam_frame); | ||
408 | av_free(s_video_frame); | ||
409 | sws_freeContext(_phone->sws_ctx); | ||
410 | //avcodec_close(webcam_decoder_ctx); | ||
411 | //avcodec_close(cs->video_encoder_ctx); | ||
412 | //pthread_mutex_unlock(&cs->ctrl_mutex); | ||
413 | |||
414 | _phone->running_encvid = -1; | ||
415 | |||
416 | pthread_exit ( NULL ); | ||
417 | } | ||
418 | #endif | ||
419 | |||
420 | void *encode_audio_thread(void *arg) | ||
421 | { | ||
422 | INFO("Started encode audio thread!"); | ||
423 | av_session_t *_phone = arg; | ||
424 | _phone->running_encaud = 1; | ||
425 | |||
426 | int ret = 0; | ||
427 | int16_t frame[4096]; | ||
428 | int frame_size = AUDIO_FRAME_SIZE; | ||
429 | ALint sample = 0; | ||
430 | alcCaptureStart((ALCdevice *)_phone->audio_capture_device); | ||
431 | |||
432 | while (_phone->running_encaud) { | ||
433 | alcGetIntegerv((ALCdevice *)_phone->audio_capture_device, ALC_CAPTURE_SAMPLES, (ALCsizei)sizeof(ALint), &sample); | ||
434 | |||
435 | if (sample >= frame_size) { | ||
436 | alcCaptureSamples((ALCdevice *)_phone->audio_capture_device, frame, frame_size); | ||
437 | |||
438 | ret = toxav_send_audio(_phone->av, frame, frame_size); | ||
439 | |||
440 | if (ret < 0) | ||
441 | printf("Could not encode or send audio packet\n"); | ||
442 | |||
443 | } else { | ||
444 | usleep(1000); | ||
445 | } | ||
446 | } | ||
447 | |||
448 | /* clean up codecs * | ||
449 | pthread_mutex_lock(&cs->ctrl_mutex);* / | ||
450 | alcCaptureStop((ALCdevice*)_phone->audio_capture_device); | ||
451 | alcCaptureCloseDevice((ALCdevice*)_phone->audio_capture_device); | ||
452 | / *pthread_mutex_unlock(&cs->ctrl_mutex);*/ | ||
453 | _phone->running_encaud = -1; | ||
454 | pthread_exit ( NULL ); | ||
455 | } | ||
456 | |||
457 | void convert_to_rgb(vpx_image_t *img, unsigned char *out) | ||
458 | { | ||
459 | const int w = img->d_w; | ||
460 | const int w2 = w / 2; | ||
461 | const int pstride = w * 3; | ||
462 | const int h = img->d_h; | ||
463 | const int h2 = h / 2; | ||
464 | |||
465 | const int strideY = img->stride[0]; | ||
466 | const int strideU = img->stride[1]; | ||
467 | const int strideV = img->stride[2]; | ||
468 | int posy, posx; | ||
469 | |||
470 | for (posy = 0; posy < h2; posy++) { | ||
471 | unsigned char *dst = out + pstride * (posy * 2); | ||
472 | unsigned char *dst2 = out + pstride * (posy * 2 + 1); | ||
473 | const unsigned char *srcY = img->planes[0] + strideY * posy * 2; | ||
474 | const unsigned char *srcY2 = img->planes[0] + strideY * (posy * 2 + 1); | ||
475 | const unsigned char *srcU = img->planes[1] + strideU * posy; | ||
476 | const unsigned char *srcV = img->planes[2] + strideV * posy; | ||
477 | |||
478 | for (posx = 0; posx < w2; posx++) { | ||
479 | unsigned char Y, U, V; | ||
480 | short R, G, B; | ||
481 | short iR, iG, iB; | ||
482 | |||
483 | U = *(srcU++); | ||
484 | V = *(srcV++); | ||
485 | iR = (351 * (V - 128)) / 256; | ||
486 | iG = - (179 * (V - 128)) / 256 - (86 * (U - 128)) / 256; | ||
487 | iB = (444 * (U - 128)) / 256; | ||
488 | |||
489 | Y = *(srcY++); | ||
490 | R = Y + iR ; | ||
491 | G = Y + iG ; | ||
492 | B = Y + iB ; | ||
493 | R = (R < 0 ? 0 : (R > 255 ? 255 : R)); | ||
494 | G = (G < 0 ? 0 : (G > 255 ? 255 : G)); | ||
495 | B = (B < 0 ? 0 : (B > 255 ? 255 : B)); | ||
496 | *(dst++) = R; | ||
497 | *(dst++) = G; | ||
498 | *(dst++) = B; | ||
499 | |||
500 | Y = *(srcY2++); | ||
501 | R = Y + iR ; | ||
502 | G = Y + iG ; | ||
503 | B = Y + iB ; | ||
504 | R = (R < 0 ? 0 : (R > 255 ? 255 : R)); | ||
505 | G = (G < 0 ? 0 : (G > 255 ? 255 : G)); | ||
506 | B = (B < 0 ? 0 : (B > 255 ? 255 : B)); | ||
507 | *(dst2++) = R; | ||
508 | *(dst2++) = G; | ||
509 | *(dst2++) = B; | ||
510 | |||
511 | Y = *(srcY++) ; | ||
512 | R = Y + iR ; | ||
513 | G = Y + iG ; | ||
514 | B = Y + iB ; | ||
515 | R = (R < 0 ? 0 : (R > 255 ? 255 : R)); | ||
516 | G = (G < 0 ? 0 : (G > 255 ? 255 : G)); | ||
517 | B = (B < 0 ? 0 : (B > 255 ? 255 : B)); | ||
518 | *(dst++) = R; | ||
519 | *(dst++) = G; | ||
520 | *(dst++) = B; | ||
521 | |||
522 | Y = *(srcY2++); | ||
523 | R = Y + iR ; | ||
524 | G = Y + iG ; | ||
525 | B = Y + iB ; | ||
526 | R = (R < 0 ? 0 : (R > 255 ? 255 : R)); | ||
527 | G = (G < 0 ? 0 : (G > 255 ? 255 : G)); | ||
528 | B = (B < 0 ? 0 : (B > 255 ? 255 : B)); | ||
529 | *(dst2++) = R; | ||
530 | *(dst2++) = G; | ||
531 | *(dst2++) = B; | ||
532 | } | ||
533 | } | ||
534 | } | ||
535 | |||
536 | #define mask32(BYTE) (*(uint32_t *)(uint8_t [4]){ [BYTE] = 0xff }) | ||
537 | |||
538 | void *decode_video_thread(void *arg) | ||
539 | { | ||
540 | INFO("Started decode video thread!"); | ||
541 | av_session_t *_phone = arg; | ||
542 | _phone->running_decvid = 1; | ||
543 | |||
544 | //CodecState *cs = get_cs_temp(_phone->av); | ||
545 | //cs->video_stream = 0; | ||
546 | |||
547 | //int recved_size; | ||
548 | //uint8_t dest[RTP_PAYLOAD_SIZE]; | ||
549 | |||
550 | //int dec_frame_finished; | ||
551 | //AVFrame *r_video_frame; | ||
552 | //r_video_frame = avcodec_alloc_frame(); | ||
553 | //AVPacket dec_video_packet; | ||
554 | //av_new_packet (&dec_video_packet, 65536); | ||
555 | int width = 0; | ||
556 | int height = 0; | ||
557 | |||
558 | while (_phone->running_decvid) { | ||
559 | //recved_size = toxav_recv_rtp_payload(_phone->av, TypeVideo, dest); | ||
560 | //if (recved_size) { | ||
561 | vpx_image_t *image; | ||
562 | |||
563 | if (toxav_recv_video(_phone->av, &image) == 0) { | ||
564 | //memcpy(dec_video_packet.data, dest, recved_size); | ||
565 | //dec_video_packet.size = recved_size; | ||
566 | |||
567 | //avcodec_decode_video2(cs->video_decoder_ctx, r_video_frame, &dec_frame_finished, &dec_video_packet); | ||
568 | |||
569 | //if (dec_frame_finished) { | ||
570 | |||
571 | /* Check if size has changed */ | ||
572 | if (image->d_w != width || image->d_h != height) { | ||
573 | |||
574 | width = image->d_w; | ||
575 | height = image->d_h; | ||
576 | |||
577 | printf("w: %d h: %d \n", width, height); | ||
578 | |||
579 | screen = SDL_SetVideoMode(width, height, 0, 0); | ||
580 | |||
581 | //if (_phone->video_picture.bmp) | ||
582 | // SDL_FreeYUVOverlay(_phone->video_picture.bmp); | ||
583 | |||
584 | //_phone->video_picture.bmp = SDL_CreateYUVOverlay(width, height, SDL_YV12_OVERLAY, screen); | ||
585 | // _phone->sws_SDL_r_ctx = sws_getContext(width, height, cs->video_decoder_ctx->pix_fmt, width, height, PIX_FMT_YUV420P, | ||
586 | // SWS_BILINEAR, NULL, NULL, NULL); | ||
587 | } | ||
588 | |||
589 | uint8_t *rgb_image = malloc(width * height * 3); | ||
590 | convert_to_rgb(image, rgb_image); | ||
591 | SDL_Surface *img_surface = SDL_CreateRGBSurfaceFrom(rgb_image, width, height, 24, width * 3, mask32(0), mask32(1), | ||
592 | mask32(2), 0); | ||
593 | |||
594 | if (SDL_BlitSurface(img_surface, NULL, screen, NULL) == 0) | ||
595 | SDL_UpdateRect(screen, 0, 0, 0, 0); | ||
596 | |||
597 | /* | ||
598 | SDL_LockYUVOverlay(_phone->video_picture.bmp); | ||
599 | memcpy(_phone->video_picture.bmp->pixels[0], image->planes[VPX_PLANE_Y], _phone->video_picture.bmp->pitches[0] * height); | ||
600 | memcpy(_phone->video_picture.bmp->pixels[1], image->planes[VPX_PLANE_V], _phone->video_picture.bmp->pitches[1] * height / 2); | ||
601 | memcpy(_phone->video_picture.bmp->pixels[2], image->planes[VPX_PLANE_U], _phone->video_picture.bmp->pitches[2] * height / 2); | ||
602 | |||
603 | SDL_Rect rect; | ||
604 | rect.x = 0; | ||
605 | rect.y = 0; | ||
606 | rect.w = width; | ||
607 | rect.h = height; | ||
608 | SDL_DisplayYUVOverlay(_phone->video_picture.bmp, &rect);*/ | ||
609 | free(rgb_image); | ||
610 | //display_received_frame(_phone, image); | ||
611 | |||
612 | } //else { | ||
613 | |||
614 | /* TODO: request the sender to create a new i-frame immediatly */ | ||
615 | //printf("Bad video packet\n"); | ||
616 | //} | ||
617 | //} | ||
618 | |||
619 | usleep(1000); | ||
620 | } | ||
621 | |||
622 | /* clean up codecs */ | ||
623 | //av_free(r_video_frame); | ||
624 | |||
625 | //pthread_mutex_lock(&cs->ctrl_mutex); | ||
626 | //avcodec_close(cs->video_decoder_ctx); | ||
627 | //pthread_mutex_unlock(&cs->ctrl_mutex); | ||
628 | |||
629 | _phone->running_decvid = -1; | ||
630 | |||
631 | pthread_exit ( NULL ); | ||
632 | } | ||
633 | |||
634 | void *decode_audio_thread(void *arg) | ||
635 | { | ||
636 | INFO("Started decode audio thread!"); | ||
637 | av_session_t *_phone = arg; | ||
638 | _phone->running_decaud = 1; | ||
639 | |||
640 | //int recved_size; | ||
641 | //uint8_t dest [RTP_PAYLOAD_SIZE]; | ||
642 | |||
643 | int frame_size = AUDIO_FRAME_SIZE; | ||
644 | //int data_size; | ||
645 | |||
646 | ALCdevice *dev; | ||
647 | ALCcontext *ctx; | ||
648 | ALuint source, *buffers; | ||
649 | dev = alcOpenDevice(NULL); | ||
650 | ctx = alcCreateContext(dev, NULL); | ||
651 | alcMakeContextCurrent(ctx); | ||
652 | int openal_buffers = 5; | ||
653 | |||
654 | buffers = calloc(sizeof(ALuint) * openal_buffers, 1); | ||
655 | alGenBuffers(openal_buffers, buffers); | ||
656 | alGenSources((ALuint)1, &source); | ||
657 | alSourcei(source, AL_LOOPING, AL_FALSE); | ||
658 | |||
659 | ALuint buffer; | ||
660 | ALint ready; | ||
661 | |||
662 | uint16_t zeros[frame_size]; | ||
663 | memset(zeros, 0, frame_size); | ||
664 | int16_t PCM[frame_size]; | ||
665 | |||
666 | int i; | ||
667 | |||
668 | for (i = 0; i < openal_buffers; ++i) { | ||
669 | alBufferData(buffers[i], AL_FORMAT_MONO16, zeros, frame_size, 48000); | ||
670 | } | ||
671 | |||
672 | alSourceQueueBuffers(source, openal_buffers, buffers); | ||
673 | alSourcePlay(source); | ||
674 | |||
675 | if (alGetError() != AL_NO_ERROR) { | ||
676 | fprintf(stderr, "Error starting audio\n"); | ||
677 | goto ending; | ||
678 | } | ||
679 | |||
680 | int dec_frame_len = 0; | ||
681 | |||
682 | while (_phone->running_decaud) { | ||
683 | |||
684 | alGetSourcei(source, AL_BUFFERS_PROCESSED, &ready); | ||
685 | |||
686 | if (ready <= 0) | ||
687 | continue; | ||
688 | |||
689 | dec_frame_len = toxav_recv_audio(_phone->av, frame_size, PCM); | ||
690 | |||
691 | /* Play the packet */ | ||
692 | if (dec_frame_len > 0) { | ||
693 | alSourceUnqueueBuffers(source, 1, &buffer); | ||
694 | alBufferData(buffer, AL_FORMAT_MONO16, PCM, dec_frame_len * 2 * 1, 48000); | ||
695 | int error = alGetError(); | ||
696 | |||
697 | if (error != AL_NO_ERROR) { | ||
698 | fprintf(stderr, "Error setting buffer %d\n", error); | ||
699 | break; | ||
700 | } | ||
701 | |||
702 | alSourceQueueBuffers(source, 1, &buffer); | ||
703 | |||
704 | if (alGetError() != AL_NO_ERROR) { | ||
705 | fprintf(stderr, "Error: could not buffer audio\n"); | ||
706 | break; | ||
707 | } | ||
708 | |||
709 | alGetSourcei(source, AL_SOURCE_STATE, &ready); | ||
710 | |||
711 | if (ready != AL_PLAYING) alSourcePlay(source); | ||
712 | } | ||
713 | |||
714 | usleep(1000); | ||
715 | } | ||
716 | |||
717 | |||
718 | ending: | ||
719 | /* clean up codecs */ | ||
720 | //pthread_mutex_lock(&cs->ctrl_mutex); | ||
721 | /* | ||
722 | alDeleteSources(1, &source); | ||
723 | alDeleteBuffers(openal_buffers, buffers); | ||
724 | alcMakeContextCurrent(NULL); | ||
725 | alcDestroyContext(ctx); | ||
726 | alcCloseDevice(dev); | ||
727 | */ | ||
728 | //pthread_mutex_unlock(&cs->ctrl_mutex); | ||
729 | |||
730 | _phone->running_decaud = -1; | ||
731 | |||
732 | pthread_exit ( NULL ); | ||
733 | } | ||
734 | |||
735 | |||
736 | |||
737 | |||
738 | |||
739 | int phone_startmedia_loop ( ToxAv *arg ) | ||
740 | { | ||
741 | if ( !arg ) { | ||
742 | return -1; | ||
743 | } | ||
744 | |||
745 | toxav_prepare_transmission(arg); | ||
746 | |||
747 | /* | ||
748 | * Rise all threads | ||
749 | */ | ||
750 | #ifdef TOX_FFMPEG | ||
751 | |||
752 | /* Only checks for last peer */ | ||
753 | if ( toxav_get_peer_transmission_type(arg, 0) == TypeVideo && | ||
754 | 0 > event.rise(encode_video_thread, toxav_get_agent_handler(arg)) ) { | ||
755 | INFO("Error while starting encode_video_thread()"); | ||
756 | return -1; | ||
757 | } | ||
758 | |||
759 | #endif | ||
760 | |||
761 | /* Always send audio */ | ||
762 | if ( 0 > event.rise(encode_audio_thread, toxav_get_agent_handler(arg)) ) { | ||
763 | INFO("Error while starting encode_audio_thread()"); | ||
764 | return -1; | ||
765 | } | ||
766 | |||
767 | /* Only checks for last peer */ | ||
768 | if ( toxav_get_peer_transmission_type(arg, 0) == TypeVideo && | ||
769 | 0 > event.rise(decode_video_thread, toxav_get_agent_handler(arg)) ) { | ||
770 | INFO("Error while starting decode_video_thread()"); | ||
771 | return -1; | ||
772 | } | ||
773 | |||
774 | if ( 0 > event.rise(decode_audio_thread, toxav_get_agent_handler(arg)) ) { | ||
775 | INFO("Error while starting decode_audio_thread()"); | ||
776 | return -1; | ||
777 | } | ||
778 | |||
779 | |||
780 | return 0; | ||
781 | } | ||
782 | |||
783 | |||
784 | |||
785 | |||
786 | |||
787 | |||
788 | /********************************************* | ||
789 | ********************************************* | ||
790 | ********************************************* | ||
791 | ********************************************* | ||
792 | ********************************************* | ||
793 | ********************************************* | ||
794 | ********************************************* | ||
795 | ********************************************* | ||
796 | */ | ||
797 | |||
798 | |||
799 | /* Some example callbacks */ | ||
800 | |||
801 | void *callback_recv_invite ( void *_arg ) | ||
802 | { | ||
803 | assert(_arg); | ||
804 | |||
805 | switch ( toxav_get_peer_transmission_type(_arg, 0) ) { | ||
806 | case TypeAudio: | ||
807 | INFO( "Incoming audio call!"); | ||
808 | break; | ||
809 | |||
810 | case TypeVideo: | ||
811 | INFO( "Incoming video call!"); | ||
812 | break; | ||
813 | } | ||
814 | |||
815 | pthread_exit(NULL); | ||
816 | } | ||
817 | void *callback_recv_ringing ( void *_arg ) | ||
818 | { | ||
819 | INFO ( "Ringing!" ); | ||
820 | pthread_exit(NULL); | ||
821 | } | ||
822 | void *callback_recv_starting ( void *_arg ) | ||
823 | { | ||
824 | if ( 0 != phone_startmedia_loop(_arg) ) { | ||
825 | INFO("Starting call failed!"); | ||
826 | } else { | ||
827 | INFO ("Call started! ( press h to hangup )"); | ||
828 | } | ||
829 | |||
830 | pthread_exit(NULL); | ||
831 | } | ||
832 | void *callback_recv_ending ( void *_arg ) | ||
833 | { | ||
834 | av_session_t *_phone = toxav_get_agent_handler(_arg); | ||
835 | |||
836 | _phone->running_encaud = 0; | ||
837 | _phone->running_decaud = 0; | ||
838 | _phone->running_encvid = 0; | ||
839 | _phone->running_decvid = 0; | ||
840 | |||
841 | /* Wait until all threads are done */ | ||
842 | |||
843 | while ( _phone->running_encaud != -1 || | ||
844 | _phone->running_decaud != -1 || | ||
845 | _phone->running_encvid != -1 || | ||
846 | _phone->running_decvid != -1 ) | ||
847 | |||
848 | usleep(10000000); | ||
849 | |||
850 | INFO ( "Call ended!" ); | ||
851 | pthread_exit(NULL); | ||
852 | } | ||
853 | |||
854 | void *callback_recv_error ( void *_arg ) | ||
855 | { | ||
856 | /*MSISession* _session = _arg; | ||
857 | |||
858 | INFO( "Error: %s", _session->last_error_str ); */ | ||
859 | pthread_exit(NULL); | ||
860 | } | ||
861 | |||
862 | void *callback_call_started ( void *_arg ) | ||
863 | { | ||
864 | if ( 0 != phone_startmedia_loop(_arg) ) { | ||
865 | INFO("Starting call failed!"); | ||
866 | } else { | ||
867 | INFO ("Call started! ( press h to hangup )"); | ||
868 | } | ||
869 | |||
870 | pthread_exit(NULL); | ||
871 | } | ||
872 | void *callback_call_canceled ( void *_arg ) | ||
873 | { | ||
874 | INFO ( "Call canceled!" ); | ||
875 | pthread_exit(NULL); | ||
876 | } | ||
877 | void *callback_call_rejected ( void *_arg ) | ||
878 | { | ||
879 | INFO ( "Call rejected!" ); | ||
880 | pthread_exit(NULL); | ||
881 | } | ||
882 | void *callback_call_ended ( void *_arg ) | ||
883 | { | ||
884 | av_session_t *_phone = toxav_get_agent_handler(_arg); | ||
885 | |||
886 | _phone->running_encaud = 0; | ||
887 | _phone->running_decaud = 0; | ||
888 | _phone->running_encvid = 0; | ||
889 | _phone->running_decvid = 0; | ||
890 | |||
891 | /* Wait until all threads are done */ | ||
892 | |||
893 | while ( _phone->running_encaud != -1 || | ||
894 | _phone->running_decaud != -1 || | ||
895 | _phone->running_encvid != -1 || | ||
896 | _phone->running_decvid != -1 ) | ||
897 | |||
898 | usleep(10000000); | ||
899 | |||
900 | toxav_kill_transmission(_phone->av); | ||
901 | INFO ( "Call ended!" ); | ||
902 | pthread_exit(NULL); | ||
903 | } | ||
904 | |||
905 | void *callback_requ_timeout ( void *_arg ) | ||
906 | { | ||
907 | INFO( "No answer! " ); | ||
908 | pthread_exit(NULL); | ||
909 | } | ||
910 | |||
911 | av_session_t *av_init_session() | ||
912 | { | ||
913 | av_session_t *_retu = malloc(sizeof(av_session_t)); | ||
914 | |||
915 | /* Initialize our mutex */ | ||
916 | pthread_mutex_init ( &_retu->_mutex, NULL ); | ||
917 | |||
918 | _retu->_messenger = tox_new(1); | ||
919 | |||
920 | if ( !_retu->_messenger ) { | ||
921 | fprintf ( stderr, "tox_new() failed!\n" ); | ||
922 | return NULL; | ||
923 | } | ||
924 | |||
925 | _retu->_friends = NULL; | ||
926 | |||
927 | |||
928 | const ALchar *_device_list = alcGetString(NULL, ALC_CAPTURE_DEVICE_SPECIFIER); | ||
929 | int i = 0; | ||
930 | const ALchar *device_names[20]; | ||
931 | |||
932 | if ( _device_list ) { | ||
933 | INFO("\nAvailable Capture Devices are:"); | ||
934 | |||
935 | while (*_device_list ) { | ||
936 | device_names[i] = _device_list; | ||
937 | INFO("%d) %s", i, device_names[i]); | ||
938 | _device_list += strlen( _device_list ) + 1; | ||
939 | ++i; | ||
940 | } | ||
941 | } | ||
942 | |||
943 | INFO("Enter capture device number"); | ||
944 | |||
945 | char dev[2]; | ||
946 | char *left; | ||
947 | char *warned_ = fgets(dev, 2, stdin); | ||
948 | (void)warned_; | ||
949 | long selection = strtol(dev, &left, 10); | ||
950 | |||
951 | if ( *left ) { | ||
952 | printf("'%s' is not a number!", dev); | ||
953 | fflush(stdout); | ||
954 | exit(EXIT_FAILURE); | ||
955 | } else { | ||
956 | INFO("Selected: %d ( %s )", selection, device_names[selection]); | ||
957 | } | ||
958 | |||
959 | _retu->audio_capture_device = | ||
960 | (struct ALCdevice *)alcCaptureOpenDevice( | ||
961 | device_names[selection], AUDIO_SAMPLE_RATE, AL_FORMAT_MONO16, AUDIO_FRAME_SIZE * 4); | ||
962 | |||
963 | |||
964 | if (alcGetError((ALCdevice *)_retu->audio_capture_device) != AL_NO_ERROR) { | ||
965 | printf("Could not start capture device! %d\n", alcGetError((ALCdevice *)_retu->audio_capture_device)); | ||
966 | return 0; | ||
967 | } | ||
968 | |||
969 | uint16_t height = 0, width = 0; | ||
970 | #ifdef TOX_FFMPEG | ||
971 | avdevice_register_all(); | ||
972 | avcodec_register_all(); | ||
973 | av_register_all(); | ||
974 | |||
975 | _retu->video_input_format = av_find_input_format(VIDEO_DRIVER); | ||
976 | |||
977 | if (avformat_open_input(&_retu->video_format_ctx, DEFAULT_WEBCAM, _retu->video_input_format, NULL) != 0) { | ||
978 | fprintf(stderr, "Opening video_input_format failed!\n"); | ||
979 | //return -1; | ||
980 | return NULL; | ||
981 | } | ||
982 | |||
983 | avformat_find_stream_info(_retu->video_format_ctx, NULL); | ||
984 | av_dump_format(_retu->video_format_ctx, 0, DEFAULT_WEBCAM, 0); | ||
985 | |||
986 | for (i = 0; i < _retu->video_format_ctx->nb_streams; ++i) { | ||
987 | if (_retu->video_format_ctx->streams[i]->codec->codec_type == AVMEDIA_TYPE_VIDEO) { | ||
988 | _retu->video_stream = i; | ||
989 | break; | ||
990 | } | ||
991 | } | ||
992 | |||
993 | _retu->webcam_decoder_ctx = _retu->video_format_ctx->streams[_retu->video_stream]->codec; | ||
994 | _retu->webcam_decoder = avcodec_find_decoder(_retu->webcam_decoder_ctx->codec_id); | ||
995 | |||
996 | if (_retu->webcam_decoder == NULL) { | ||
997 | fprintf(stderr, "Unsupported codec!\n"); | ||
998 | //return -1; | ||
999 | return NULL; | ||
1000 | } | ||
1001 | |||
1002 | if (_retu->webcam_decoder_ctx == NULL) { | ||
1003 | fprintf(stderr, "Init webcam_decoder_ctx failed!\n"); | ||
1004 | //return -1; | ||
1005 | return NULL; | ||
1006 | } | ||
1007 | |||
1008 | if (avcodec_open2(_retu->webcam_decoder_ctx, _retu->webcam_decoder, NULL) < 0) { | ||
1009 | fprintf(stderr, "Opening webcam decoder failed!\n"); | ||
1010 | //return -1; | ||
1011 | return NULL; | ||
1012 | } | ||
1013 | |||
1014 | width = _retu->webcam_decoder_ctx->width; | ||
1015 | height = _retu->webcam_decoder_ctx->height; | ||
1016 | #endif | ||
1017 | uint8_t _byte_address[TOX_FRIEND_ADDRESS_SIZE]; | ||
1018 | tox_get_address(_retu->_messenger, _byte_address ); | ||
1019 | fraddr_to_str( _byte_address, _retu->_my_public_id ); | ||
1020 | |||
1021 | |||
1022 | _retu->av = toxav_new(_retu->_messenger, _retu, _USERAGENT, width, height); | ||
1023 | |||
1024 | /* ------------------ */ | ||
1025 | |||
1026 | toxav_register_callstate_callback(callback_call_started, OnStart); | ||
1027 | toxav_register_callstate_callback(callback_call_canceled, OnCancel); | ||
1028 | toxav_register_callstate_callback(callback_call_rejected, OnReject); | ||
1029 | toxav_register_callstate_callback(callback_call_ended, OnEnd); | ||
1030 | toxav_register_callstate_callback(callback_recv_invite, OnInvite); | ||
1031 | |||
1032 | toxav_register_callstate_callback(callback_recv_ringing, OnRinging); | ||
1033 | toxav_register_callstate_callback(callback_recv_starting, OnStarting); | ||
1034 | toxav_register_callstate_callback(callback_recv_ending, OnEnding); | ||
1035 | |||
1036 | toxav_register_callstate_callback(callback_recv_error, OnError); | ||
1037 | toxav_register_callstate_callback(callback_requ_timeout, OnRequestTimeout); | ||
1038 | |||
1039 | /* ------------------ */ | ||
1040 | |||
1041 | return _retu; | ||
1042 | } | ||
1043 | |||
1044 | int av_terminate_session(av_session_t *_phone) | ||
1045 | { | ||
1046 | toxav_hangup(_phone->av); | ||
1047 | |||
1048 | free(_phone->_friends); | ||
1049 | pthread_mutex_destroy ( &_phone->_mutex ); | ||
1050 | |||
1051 | Tox *_p = _phone->_messenger; | ||
1052 | _phone->_messenger = NULL; | ||
1053 | usleep(100000); /* Wait for tox_poll to end */ | ||
1054 | |||
1055 | tox_kill(_p); | ||
1056 | toxav_kill(_phone->av); | ||
1057 | |||
1058 | free(_phone); | ||
1059 | |||
1060 | printf("\r[i] Quit!\n"); | ||
1061 | return 0; | ||
1062 | } | ||
1063 | |||
1064 | /****** AV HELPER FUNCTIONS ******/ | ||
1065 | |||
1066 | /* Auto accept friend request */ | ||
1067 | void av_friend_requ(uint8_t *_public_key, uint8_t *_data, uint16_t _length, void *_userdata) | ||
1068 | { | ||
1069 | av_session_t *_phone = _userdata; | ||
1070 | av_allocate_friend (_phone, -1, 0); | ||
1071 | |||
1072 | INFO("Got friend request with message: %s", _data); | ||
1073 | |||
1074 | tox_add_friend_norequest(_phone->_messenger, _public_key); | ||
1075 | |||
1076 | INFO("Auto-accepted! Friend id: %d", _phone->_friends->_id ); | ||
1077 | } | ||
1078 | |||
1079 | void av_friend_active(Tox *_messenger, int _friendnumber, uint8_t *_string, uint16_t _length, void *_userdata) | ||
1080 | { | ||
1081 | av_session_t *_phone = _userdata; | ||
1082 | INFO("Friend no. %d is online", _friendnumber); | ||
1083 | |||
1084 | av_friend_t *_this_friend = av_get_friend(_phone, _friendnumber); | ||
1085 | |||
1086 | if ( !_this_friend ) { | ||
1087 | INFO("But it's not registered!"); | ||
1088 | return; | ||
1089 | } | ||
1090 | |||
1091 | (*_this_friend)._active = 1; | ||
1092 | } | ||
1093 | |||
1094 | int av_add_friend(av_session_t *_phone, char *_friend_hash) | ||
1095 | { | ||
1096 | trim_spaces(_friend_hash); | ||
1097 | |||
1098 | unsigned char *_bin_string = hex_string_to_bin(_friend_hash); | ||
1099 | int _number = tox_add_friend(_phone->_messenger, _bin_string, (uint8_t *)"Tox phone "_USERAGENT, | ||
1100 | sizeof("Tox phone "_USERAGENT)); | ||
1101 | free(_bin_string); | ||
1102 | |||
1103 | if ( _number >= 0) { | ||
1104 | INFO("Added friend as %d", _number ); | ||
1105 | av_allocate_friend(_phone, _number, 0); | ||
1106 | } else | ||
1107 | INFO("Unknown error %i", _number ); | ||
1108 | |||
1109 | return _number; | ||
1110 | } | ||
1111 | |||
1112 | int av_connect_to_dht(av_session_t *_phone, char *_dht_key, const char *_dht_addr, unsigned short _dht_port) | ||
1113 | { | ||
1114 | unsigned char *_binary_string = hex_string_to_bin(_dht_key); | ||
1115 | |||
1116 | uint16_t _port = htons(_dht_port); | ||
1117 | |||
1118 | int _if = tox_bootstrap_from_address(_phone->_messenger, _dht_addr, 1, _port, _binary_string ); | ||
1119 | |||
1120 | free(_binary_string); | ||
1121 | |||
1122 | return _if ? 0 : -1; | ||
1123 | } | ||
1124 | |||
1125 | /*********************************/ | ||
1126 | |||
1127 | void do_phone ( av_session_t *_phone ) | ||
1128 | { | ||
1129 | INFO("Welcome to tox_phone version: " _USERAGENT "\n" | ||
1130 | "Usage: \n" | ||
1131 | "f [pubkey] (add friend)\n" | ||
1132 | "c [a/v] (type) [friend] (friend id) (calls friend if online)\n" | ||
1133 | "h (if call is active hang up)\n" | ||
1134 | "a [a/v] (answer incoming call: a - audio / v - audio + video (audio is default))\n" | ||
1135 | "r (reject incoming call)\n" | ||
1136 | "q (quit)\n" | ||
1137 | "================================================================================" | ||
1138 | ); | ||
1139 | |||
1140 | while ( 1 ) { | ||
1141 | char _line [ 1500 ]; | ||
1142 | int _len; | ||
1143 | |||
1144 | if ( -1 == getinput(_line, 1500, &_len) ) { | ||
1145 | printf(" >> "); | ||
1146 | fflush(stdout); | ||
1147 | continue; | ||
1148 | } | ||
1149 | |||
1150 | if ( _len > 1 && _line[1] != ' ' && _line[1] != '\n' ) { | ||
1151 | INFO("Invalid input!"); | ||
1152 | continue; | ||
1153 | } | ||
1154 | |||
1155 | switch (_line[0]) { | ||
1156 | |||
1157 | case 'f': { | ||
1158 | char _id [128]; | ||
1159 | strncpy(_id, _line + 2, 128); | ||
1160 | |||
1161 | av_add_friend(_phone, _id); | ||
1162 | |||
1163 | } | ||
1164 | break; | ||
1165 | |||
1166 | case 'c': { | ||
1167 | ToxAvCallType _ctype; | ||
1168 | |||
1169 | if ( _len < 5 ) { | ||
1170 | INFO("Invalid input; usage: c a/v [friend]"); | ||
1171 | break; | ||
1172 | } else if ( _line[2] == 'a' || _line[2] != 'v' ) { /* default and audio */ | ||
1173 | _ctype = TypeAudio; | ||
1174 | } else { /* video */ | ||
1175 | _ctype = TypeVideo; | ||
1176 | } | ||
1177 | |||
1178 | char *_end; | ||
1179 | int _friend = strtol(_line + 4, &_end, 10); | ||
1180 | |||
1181 | if ( *_end ) { | ||
1182 | INFO("Friend num has to be numerical value"); | ||
1183 | break; | ||
1184 | } | ||
1185 | |||
1186 | if ( toxav_call(_phone->av, _friend, _ctype, 30) == ErrorAlreadyInCall ) { | ||
1187 | INFO("Already in a call"); | ||
1188 | break; | ||
1189 | } else INFO("Calling friend: %d!", _friend); | ||
1190 | |||
1191 | } | ||
1192 | break; | ||
1193 | |||
1194 | case 'h': { | ||
1195 | if ( toxav_hangup(_phone->av) == ErrorNoCall ) { | ||
1196 | INFO("No call!"); | ||
1197 | break; | ||
1198 | } else INFO("Hung up..."); | ||
1199 | |||
1200 | } | ||
1201 | break; | ||
1202 | |||
1203 | case 'a': { | ||
1204 | ToxAvError rc; | ||
1205 | |||
1206 | if ( _len > 1 && _line[2] == 'v' ) { | ||
1207 | rc = toxav_answer(_phone->av, TypeVideo); | ||
1208 | } else | ||
1209 | rc = toxav_answer(_phone->av, TypeAudio); | ||
1210 | |||
1211 | if ( rc == ErrorInvalidState ) { | ||
1212 | INFO("No call to answer!"); | ||
1213 | } | ||
1214 | |||
1215 | } | ||
1216 | break; | ||
1217 | |||
1218 | case 'r': { | ||
1219 | if ( toxav_reject(_phone->av, "User action") == ErrorInvalidState ) | ||
1220 | INFO("No state to cancel!"); | ||
1221 | else INFO("Call Rejected..."); | ||
1222 | |||
1223 | } | ||
1224 | break; | ||
1225 | |||
1226 | case 'q': { | ||
1227 | INFO("Quitting!"); | ||
1228 | return; | ||
1229 | } | ||
1230 | |||
1231 | case '\n': { | ||
1232 | } | ||
1233 | |||
1234 | default: { | ||
1235 | } break; | ||
1236 | |||
1237 | } | ||
1238 | |||
1239 | } | ||
1240 | } | ||
1241 | |||
1242 | void *tox_poll (void *_messenger_p) | ||
1243 | { | ||
1244 | Tox **_messenger = _messenger_p; | ||
1245 | |||
1246 | while ( *_messenger ) { | ||
1247 | tox_do(*_messenger); | ||
1248 | usleep(10000); | ||
1249 | } | ||
1250 | |||
1251 | pthread_exit(NULL); | ||
1252 | } | ||
1253 | |||
1254 | int av_wait_dht(av_session_t *_phone, int _wait_seconds, const char *_ip, char *_key, unsigned short _port) | ||
1255 | { | ||
1256 | if ( !_wait_seconds ) | ||
1257 | return -1; | ||
1258 | |||
1259 | int _waited = 0; | ||
1260 | |||
1261 | while ( !tox_isconnected(_phone->_messenger) ) { | ||
1262 | |||
1263 | if ( -1 == av_connect_to_dht(_phone, _key, _ip, _port) ) { | ||
1264 | INFO("Could not connect to: %s", _ip); | ||
1265 | av_terminate_session(_phone); | ||
1266 | return -1; | ||
1267 | } | ||
1268 | |||
1269 | if ( _waited >= _wait_seconds ) return 0; | ||
1270 | |||
1271 | printf("."); | ||
1272 | fflush(stdout); | ||
1273 | |||
1274 | _waited ++; | ||
1275 | usleep(1000000); | ||
1276 | } | ||
1277 | |||
1278 | int _r = _wait_seconds - _waited; | ||
1279 | return _r ? _r : 1; | ||
1280 | } | ||
1281 | /* ---------------------- */ | ||
1282 | |||
1283 | int print_help ( const char *_name ) | ||
1284 | { | ||
1285 | printf ( "Usage: %s [IP] [PORT] [KEY]\n" | ||
1286 | "\t[IP] (DHT ip)\n" | ||
1287 | "\t[PORT] (DHT port)\n" | ||
1288 | "\t[KEY] (DHT public key)\n" | ||
1289 | "P.S. Friends and key are stored in ./tox_phone.conf\n" | ||
1290 | , _name ); | ||
1291 | return 1; | ||
1292 | } | ||
1293 | |||
1294 | int main ( int argc, char *argv [] ) | ||
1295 | { | ||
1296 | if ( argc < 1 || argc < 4 ) | ||
1297 | return print_help(argv[0]); | ||
1298 | |||
1299 | char *_convertable; | ||
1300 | |||
1301 | |||
1302 | const char *_ip = argv[1]; | ||
1303 | char *_key = argv[3]; | ||
1304 | unsigned short _port = strtol(argv[2], &_convertable, 10); | ||
1305 | |||
1306 | if ( *_convertable ) { | ||
1307 | printf("Invalid port: cannot convert string to long: %s", _convertable); | ||
1308 | return 1; | ||
1309 | } | ||
1310 | |||
1311 | av_session_t *_phone = av_init_session(); | ||
1312 | |||
1313 | tox_callback_friend_request(_phone->_messenger, av_friend_requ, _phone); | ||
1314 | tox_callback_status_message(_phone->_messenger, av_friend_active, _phone); | ||
1315 | |||
1316 | |||
1317 | INFO("\r================================================================================\n" | ||
1318 | "[!] Trying dht@%s:%d" | ||
1319 | , _ip, _port); | ||
1320 | |||
1321 | /* Start tox protocol */ | ||
1322 | event.rise( tox_poll, &_phone->_messenger ); | ||
1323 | |||
1324 | /* Just clean one line */ | ||
1325 | printf("\r \r"); | ||
1326 | fflush(stdout); | ||
1327 | |||
1328 | int _r; | ||
1329 | int _wait_seconds = 5; | ||
1330 | |||
1331 | for ( _r = 0; _r == 0; _r = av_wait_dht(_phone, _wait_seconds, _ip, _key, _port) ) _wait_seconds --; | ||
1332 | |||
1333 | |||
1334 | if ( -1 == _r ) { | ||
1335 | INFO("Error while connecting to dht: %s:%d", _ip, _port); | ||
1336 | av_terminate_session(_phone); | ||
1337 | return 1; | ||
1338 | } | ||
1339 | |||
1340 | INFO("CONNECTED!\n" | ||
1341 | "================================================================================\n" | ||
1342 | "%s\n" | ||
1343 | "================================================================================" | ||
1344 | , _phone->_my_public_id ); | ||
1345 | |||
1346 | |||
1347 | do_phone (_phone); | ||
1348 | |||
1349 | av_terminate_session(_phone); | ||
1350 | |||
1351 | return 0; | ||
1352 | } | ||
diff --git a/toxav/rtp.c b/toxav/rtp.c new file mode 100644 index 00000000..dbaecbb6 --- /dev/null +++ b/toxav/rtp.c | |||
@@ -0,0 +1,934 @@ | |||
1 | /** toxrtp.c | ||
2 | * | ||
3 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
4 | * | ||
5 | * This file is part of Tox. | ||
6 | * | ||
7 | * Tox is free software: you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation, either version 3 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * Tox is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | * | ||
21 | * Report bugs/suggestions at #tox-dev @ freenode.net:6667 | ||
22 | */ | ||
23 | |||
24 | #ifdef HAVE_CONFIG_H | ||
25 | #include "config.h" | ||
26 | #endif /* HAVE_CONFIG_H */ | ||
27 | |||
28 | #include "rtp.h" | ||
29 | #include <assert.h> | ||
30 | #include <stdlib.h> | ||
31 | |||
32 | |||
33 | #define PAYLOAD_ID_VALUE_OPUS 1 | ||
34 | #define PAYLOAD_ID_VALUE_VP8 2 | ||
35 | |||
36 | #define size_32 4 | ||
37 | |||
38 | #define inline__ inline __attribute__((always_inline)) | ||
39 | |||
40 | |||
41 | #define ADD_FLAG_VERSION(_h, _v) do { ( _h->flags ) &= 0x3F; ( _h->flags ) |= ( ( ( _v ) << 6 ) & 0xC0 ); } while(0) | ||
42 | #define ADD_FLAG_PADDING(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xDF; ( _h->flags ) |= ( ( ( _v ) << 5 ) & 0x20 ); } while(0) | ||
43 | #define ADD_FLAG_EXTENSION(_h, _v) do { if ( _v > 0 ) _v = 1; ( _h->flags ) &= 0xEF;( _h->flags ) |= ( ( ( _v ) << 4 ) & 0x10 ); } while(0) | ||
44 | #define ADD_FLAG_CSRCC(_h, _v) do { ( _h->flags ) &= 0xF0; ( _h->flags ) |= ( ( _v ) & 0x0F ); } while(0) | ||
45 | #define ADD_SETTING_MARKER(_h, _v) do { if ( _v > 1 ) _v = 1; ( _h->marker_payloadt ) &= 0x7F; ( _h->marker_payloadt ) |= ( ( ( _v ) << 7 ) /*& 0x80 */ ); } while(0) | ||
46 | #define ADD_SETTING_PAYLOAD(_h, _v) do { if ( _v > 127 ) _v = 127; ( _h->marker_payloadt ) &= 0x80; ( _h->marker_payloadt ) |= ( ( _v ) /* & 0x7F */ ); } while(0) | ||
47 | |||
48 | #define GET_FLAG_VERSION(_h) (( _h->flags & 0xd0 ) >> 6) | ||
49 | #define GET_FLAG_PADDING(_h) (( _h->flags & 0x20 ) >> 5) | ||
50 | #define GET_FLAG_EXTENSION(_h) (( _h->flags & 0x10 ) >> 4) | ||
51 | #define GET_FLAG_CSRCC(_h) ( _h->flags & 0x0f ) | ||
52 | #define GET_SETTING_MARKER(_h) (( _h->marker_payloadt ) >> 7) | ||
53 | #define GET_SETTING_PAYLOAD(_h) ((_h->marker_payloadt) & 0x7f) | ||
54 | |||
55 | |||
56 | /** | ||
57 | * @brief Converts 4 bytes to uint32_t | ||
58 | * | ||
59 | * @param dest Where to convert | ||
60 | * @param bytes What bytes | ||
61 | * @return void | ||
62 | */ | ||
63 | inline__ void bytes_to_U32(uint32_t *dest, const uint8_t *bytes) | ||
64 | { | ||
65 | *dest = | ||
66 | #ifdef WORDS_BIGENDIAN | ||
67 | ( ( uint32_t ) * bytes ) | | ||
68 | ( ( uint32_t ) * ( bytes + 1 ) << 8 ) | | ||
69 | ( ( uint32_t ) * ( bytes + 2 ) << 16 ) | | ||
70 | ( ( uint32_t ) * ( bytes + 3 ) << 24 ) ; | ||
71 | #else | ||
72 | ( ( uint32_t ) * bytes << 24 ) | | ||
73 | ( ( uint32_t ) * ( bytes + 1 ) << 16 ) | | ||
74 | ( ( uint32_t ) * ( bytes + 2 ) << 8 ) | | ||
75 | ( ( uint32_t ) * ( bytes + 3 ) ) ; | ||
76 | #endif | ||
77 | } | ||
78 | |||
79 | /** | ||
80 | * @brief Converts 2 bytes to uint16_t | ||
81 | * | ||
82 | * @param dest Where to convert | ||
83 | * @param bytes What bytes | ||
84 | * @return void | ||
85 | */ | ||
86 | inline__ void bytes_to_U16(uint16_t *dest, const uint8_t *bytes) | ||
87 | { | ||
88 | *dest = | ||
89 | #ifdef WORDS_BIGENDIAN | ||
90 | ( ( uint16_t ) * bytes ) | | ||
91 | ( ( uint16_t ) * ( bytes + 1 ) << 8 ); | ||
92 | #else | ||
93 | ( ( uint16_t ) * bytes << 8 ) | | ||
94 | ( ( uint16_t ) * ( bytes + 1 ) ); | ||
95 | #endif | ||
96 | } | ||
97 | |||
98 | /** | ||
99 | * @brief Convert uint32_t to byte string of size 4 | ||
100 | * | ||
101 | * @param dest Where to convert | ||
102 | * @param value The value | ||
103 | * @return void | ||
104 | */ | ||
105 | inline__ void U32_to_bytes(uint8_t *dest, uint32_t value) | ||
106 | { | ||
107 | #ifdef WORDS_BIGENDIAN | ||
108 | *(dest) = ( value ); | ||
109 | *(dest + 1) = ( value >> 8 ); | ||
110 | *(dest + 2) = ( value >> 16 ); | ||
111 | *(dest + 3) = ( value >> 24 ); | ||
112 | #else | ||
113 | *(dest) = ( value >> 24 ); | ||
114 | *(dest + 1) = ( value >> 16 ); | ||
115 | *(dest + 2) = ( value >> 8 ); | ||
116 | *(dest + 3) = ( value ); | ||
117 | #endif | ||
118 | } | ||
119 | |||
120 | /** | ||
121 | * @brief Convert uint16_t to byte string of size 2 | ||
122 | * | ||
123 | * @param dest Where to convert | ||
124 | * @param value The value | ||
125 | * @return void | ||
126 | */ | ||
127 | inline__ void U16_to_bytes(uint8_t *dest, uint16_t value) | ||
128 | { | ||
129 | #ifdef WORDS_BIGENDIAN | ||
130 | *(dest) = ( value ); | ||
131 | *(dest + 1) = ( value >> 8 ); | ||
132 | #else | ||
133 | *(dest) = ( value >> 8 ); | ||
134 | *(dest + 1) = ( value ); | ||
135 | #endif | ||
136 | } | ||
137 | |||
138 | |||
139 | /** | ||
140 | * @brief Checks if message came in late. | ||
141 | * | ||
142 | * @param session Control session. | ||
143 | * @param msg The message. | ||
144 | * @return int | ||
145 | * @retval -1 The message came in order. | ||
146 | * @retval 0 The message came late. | ||
147 | */ | ||
148 | inline__ int check_late_message (RTPSession *session, RTPMessage *msg) | ||
149 | { | ||
150 | /* | ||
151 | * Check Sequence number. If this new msg has lesser number then the session->rsequnum | ||
152 | * it shows that the message came in late. Also check timestamp to be 100% certain. | ||
153 | * | ||
154 | */ | ||
155 | return ( msg->header->sequnum < session->rsequnum && msg->header->timestamp < session->timestamp ) ? 0 : -1; | ||
156 | } | ||
157 | |||
158 | |||
159 | /** | ||
160 | * @brief Increases nonce value by 'target' | ||
161 | * | ||
162 | * @param nonce The nonce | ||
163 | * @param target The target | ||
164 | * @return void | ||
165 | */ | ||
166 | inline__ void increase_nonce(uint8_t *nonce, uint16_t target) | ||
167 | { | ||
168 | uint16_t _nonce_counter; | ||
169 | |||
170 | uint8_t _reverse_bytes[2]; | ||
171 | _reverse_bytes[0] = nonce[crypto_box_NONCEBYTES - 1]; | ||
172 | _reverse_bytes[1] = nonce[crypto_box_NONCEBYTES - 2]; | ||
173 | |||
174 | bytes_to_U16(&_nonce_counter, _reverse_bytes ); | ||
175 | |||
176 | /* Check overflow */ | ||
177 | if (_nonce_counter > UINT16_MAX - target ) { /* 2 bytes are not long enough */ | ||
178 | uint8_t _it = 3; | ||
179 | |||
180 | while ( _it <= crypto_box_NONCEBYTES ) _it += ++nonce[crypto_box_NONCEBYTES - _it] ? crypto_box_NONCEBYTES : 1; | ||
181 | |||
182 | _nonce_counter = _nonce_counter - (UINT16_MAX - target ); /* Assign the rest of it */ | ||
183 | } else { /* Increase nonce */ | ||
184 | |||
185 | _nonce_counter += target; | ||
186 | } | ||
187 | |||
188 | /* Assign the last bytes */ | ||
189 | |||
190 | U16_to_bytes( _reverse_bytes, _nonce_counter); | ||
191 | nonce [crypto_box_NONCEBYTES - 1] = _reverse_bytes[0]; | ||
192 | nonce [crypto_box_NONCEBYTES - 2] = _reverse_bytes[1]; | ||
193 | |||
194 | } | ||
195 | |||
196 | |||
197 | /** | ||
198 | * @brief Speaks for it self. | ||
199 | * | ||
200 | */ | ||
201 | static const uint32_t payload_table[] = { | ||
202 | 8000, 8000, 8000, 8000, 8000, 8000, 16000, 8000, 8000, 8000, /* 0-9 */ | ||
203 | 44100, 44100, 0, 0, 90000, 8000, 11025, 22050, 0, 0, /* 10-19 */ | ||
204 | 0, 0, 0, 0, 0, 90000, 90000, 0, 90000, 0, /* 20-29 */ | ||
205 | 0, 90000, 90000, 90000, 90000, 0, 0, 0, 0, 0, /* 30-39 */ | ||
206 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 40-49 */ | ||
207 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 50-59 */ | ||
208 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 60-69 */ | ||
209 | PAYLOAD_ID_VALUE_OPUS, PAYLOAD_ID_VALUE_VP8, 0, 0, 0, 0, 0, 0, 0, 0,/* 70-79 */ | ||
210 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 80-89 */ | ||
211 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 90-99 */ | ||
212 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 100-109 */ | ||
213 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, /* 110-119 */ | ||
214 | 0, 0, 0, 0, 0, 0, 0, 0 /* 120-127 */ | ||
215 | }; | ||
216 | |||
217 | |||
218 | /** | ||
219 | * @brief Extracts header from payload. | ||
220 | * | ||
221 | * @param payload The payload. | ||
222 | * @param length The size of payload. | ||
223 | * @return RTPHeader* Extracted header. | ||
224 | * @retval NULL Error occurred while extracting header. | ||
225 | */ | ||
226 | RTPHeader *extract_header ( const uint8_t *payload, int length ) | ||
227 | { | ||
228 | if ( !payload || !length ) { | ||
229 | return NULL; | ||
230 | } | ||
231 | |||
232 | const uint8_t *_it = payload; | ||
233 | |||
234 | RTPHeader *_retu = calloc(1, sizeof (RTPHeader)); | ||
235 | assert(_retu); | ||
236 | |||
237 | _retu->flags = *_it; | ||
238 | ++_it; | ||
239 | |||
240 | /* This indicates if the first 2 bits are valid. | ||
241 | * Now it may happen that this is out of order but | ||
242 | * it cuts down chances of parsing some invalid value | ||
243 | */ | ||
244 | |||
245 | if ( GET_FLAG_VERSION(_retu) != RTP_VERSION ) { | ||
246 | /* Deallocate */ | ||
247 | free(_retu); | ||
248 | return NULL; | ||
249 | } | ||
250 | |||
251 | /* | ||
252 | * Added a check for the size of the header little sooner so | ||
253 | * I don't need to parse the other stuff if it's bad | ||
254 | */ | ||
255 | uint8_t _cc = GET_FLAG_CSRCC ( _retu ); | ||
256 | uint32_t _length = 12 /* Minimum header len */ + ( _cc * 4 ); | ||
257 | |||
258 | if ( length < _length ) { | ||
259 | /* Deallocate */ | ||
260 | free(_retu); | ||
261 | return NULL; | ||
262 | } | ||
263 | |||
264 | if ( _cc > 0 ) { | ||
265 | _retu->csrc = calloc (_cc, sizeof (uint32_t)); | ||
266 | assert(_retu->csrc); | ||
267 | |||
268 | } else { /* But this should not happen ever */ | ||
269 | /* Deallocate */ | ||
270 | free(_retu); | ||
271 | return NULL; | ||
272 | } | ||
273 | |||
274 | |||
275 | _retu->marker_payloadt = *_it; | ||
276 | ++_it; | ||
277 | _retu->length = _length; | ||
278 | |||
279 | |||
280 | bytes_to_U32(&_retu->timestamp, _it); | ||
281 | _it += 4; | ||
282 | bytes_to_U32(&_retu->ssrc, _it); | ||
283 | |||
284 | uint8_t _x; | ||
285 | |||
286 | for ( _x = 0; _x < _cc; _x++ ) { | ||
287 | _it += 4; | ||
288 | bytes_to_U32(&(_retu->csrc[_x]), _it); | ||
289 | } | ||
290 | |||
291 | return _retu; | ||
292 | } | ||
293 | |||
294 | /** | ||
295 | * @brief Extracts external header from payload. Must be called AFTER extract_header()! | ||
296 | * | ||
297 | * @param payload The ITERATED payload. | ||
298 | * @param length The size of payload. | ||
299 | * @return RTPExtHeader* Extracted extension header. | ||
300 | * @retval NULL Error occurred while extracting extension header. | ||
301 | */ | ||
302 | RTPExtHeader *extract_ext_header ( const uint8_t *payload, uint16_t length ) | ||
303 | { | ||
304 | const uint8_t *_it = payload; | ||
305 | |||
306 | RTPExtHeader *_retu = calloc(1, sizeof (RTPExtHeader)); | ||
307 | assert(_retu); | ||
308 | |||
309 | uint16_t _ext_length; | ||
310 | bytes_to_U16(&_ext_length, _it); | ||
311 | _it += 2; | ||
312 | |||
313 | |||
314 | if ( length < ( _ext_length * sizeof(uint32_t) ) ) { | ||
315 | free(_retu); | ||
316 | return NULL; | ||
317 | } | ||
318 | |||
319 | _retu->length = _ext_length; | ||
320 | bytes_to_U16(&_retu->type, _it); | ||
321 | _it += 2; | ||
322 | |||
323 | _retu->table = calloc(_ext_length, sizeof (uint32_t)); | ||
324 | assert(_retu->table); | ||
325 | |||
326 | uint16_t _x; | ||
327 | |||
328 | for ( _x = 0; _x < _ext_length; _x++ ) { | ||
329 | _it += 4; | ||
330 | bytes_to_U32(&(_retu->table[_x]), _it); | ||
331 | } | ||
332 | |||
333 | return _retu; | ||
334 | } | ||
335 | |||
336 | /** | ||
337 | * @brief Adds header to payload. Make sure _payload_ has enough space. | ||
338 | * | ||
339 | * @param header The header. | ||
340 | * @param payload The payload. | ||
341 | * @return uint8_t* Iterated position. | ||
342 | */ | ||
343 | uint8_t *add_header ( RTPHeader *header, uint8_t *payload ) | ||
344 | { | ||
345 | uint8_t _cc = GET_FLAG_CSRCC ( header ); | ||
346 | |||
347 | uint8_t *_it = payload; | ||
348 | |||
349 | |||
350 | /* Add sequence number first */ | ||
351 | U16_to_bytes(_it, header->sequnum); | ||
352 | _it += 2; | ||
353 | |||
354 | *_it = header->flags; | ||
355 | ++_it; | ||
356 | *_it = header->marker_payloadt; | ||
357 | ++_it; | ||
358 | |||
359 | |||
360 | U32_to_bytes( _it, header->timestamp); | ||
361 | _it += 4; | ||
362 | U32_to_bytes( _it, header->ssrc); | ||
363 | |||
364 | if ( header->csrc ) { | ||
365 | uint8_t _x; | ||
366 | |||
367 | for ( _x = 0; _x < _cc; _x++ ) { | ||
368 | _it += 4; | ||
369 | U32_to_bytes( _it, header->csrc[_x]); | ||
370 | } | ||
371 | } | ||
372 | |||
373 | return _it + 4; | ||
374 | } | ||
375 | |||
376 | /** | ||
377 | * @brief Adds extension header to payload. Make sure _payload_ has enough space. | ||
378 | * | ||
379 | * @param header The header. | ||
380 | * @param payload The payload. | ||
381 | * @return uint8_t* Iterated position. | ||
382 | */ | ||
383 | uint8_t *add_ext_header ( RTPExtHeader *header, uint8_t *payload ) | ||
384 | { | ||
385 | uint8_t *_it = payload; | ||
386 | |||
387 | U16_to_bytes(_it, header->length); | ||
388 | _it += 2; | ||
389 | U16_to_bytes(_it, header->type); | ||
390 | _it -= 2; /* Return to 0 position */ | ||
391 | |||
392 | if ( header->table ) { | ||
393 | uint16_t _x; | ||
394 | |||
395 | for ( _x = 0; _x < header->length; _x++ ) { | ||
396 | _it += 4; | ||
397 | U32_to_bytes(_it, header->table[_x]); | ||
398 | } | ||
399 | } | ||
400 | |||
401 | return _it + 4; | ||
402 | } | ||
403 | |||
404 | /** | ||
405 | * @brief Builds header from control session values. | ||
406 | * | ||
407 | * @param session Control session. | ||
408 | * @return RTPHeader* Created header. | ||
409 | */ | ||
410 | RTPHeader *build_header ( RTPSession *session ) | ||
411 | { | ||
412 | RTPHeader *_retu = calloc ( 1, sizeof (RTPHeader) ); | ||
413 | assert(_retu); | ||
414 | |||
415 | ADD_FLAG_VERSION ( _retu, session->version ); | ||
416 | ADD_FLAG_PADDING ( _retu, session->padding ); | ||
417 | ADD_FLAG_EXTENSION ( _retu, session->extension ); | ||
418 | ADD_FLAG_CSRCC ( _retu, session->cc ); | ||
419 | ADD_SETTING_MARKER ( _retu, session->marker ); | ||
420 | ADD_SETTING_PAYLOAD ( _retu, session->payload_type ); | ||
421 | |||
422 | _retu->sequnum = session->sequnum; | ||
423 | _retu->timestamp = ((uint32_t)(current_time() / 1000)); /* micro to milli */ | ||
424 | _retu->ssrc = session->ssrc; | ||
425 | |||
426 | if ( session->cc > 0 ) { | ||
427 | _retu->csrc = calloc(session->cc, sizeof (uint32_t)); | ||
428 | assert(_retu->csrc); | ||
429 | |||
430 | int i; | ||
431 | |||
432 | for ( i = 0; i < session->cc; i++ ) { | ||
433 | _retu->csrc[i] = session->csrc[i]; | ||
434 | } | ||
435 | } else { | ||
436 | _retu->csrc = NULL; | ||
437 | } | ||
438 | |||
439 | _retu->length = 12 /* Minimum header len */ + ( session->cc * size_32 ); | ||
440 | |||
441 | return _retu; | ||
442 | } | ||
443 | |||
444 | |||
445 | /** | ||
446 | * @brief Parses data into RTPMessage struct. Stores headers separately from the payload data | ||
447 | * and so the length variable is set accordingly. _sequnum_ argument is | ||
448 | * passed by the handle_packet() since it's parsed already. | ||
449 | * | ||
450 | * @param session Control session. | ||
451 | * @param sequnum Sequence number that's parsed from payload in handle_packet() | ||
452 | * @param data Payload data. | ||
453 | * @param length Payload size. | ||
454 | * @return RTPMessage* | ||
455 | * @retval NULL Error occurred. | ||
456 | */ | ||
457 | RTPMessage *msg_parse ( uint16_t sequnum, const uint8_t *data, int length ) | ||
458 | { | ||
459 | RTPMessage *_retu = calloc(1, sizeof (RTPMessage)); | ||
460 | |||
461 | _retu->header = extract_header ( data, length ); /* It allocates memory and all */ | ||
462 | |||
463 | if ( !_retu->header ) { | ||
464 | free(_retu); | ||
465 | return NULL; | ||
466 | } | ||
467 | |||
468 | _retu->header->sequnum = sequnum; | ||
469 | |||
470 | _retu->length = length - _retu->header->length; | ||
471 | |||
472 | uint16_t _from_pos = _retu->header->length - 2 /* Since sequ num is excluded */ ; | ||
473 | |||
474 | |||
475 | if ( GET_FLAG_EXTENSION ( _retu->header ) ) { | ||
476 | _retu->ext_header = extract_ext_header ( data + _from_pos, length ); | ||
477 | |||
478 | if ( _retu->ext_header ) { | ||
479 | _retu->length -= ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); | ||
480 | _from_pos += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); | ||
481 | } else { /* Error */ | ||
482 | free (_retu->ext_header); | ||
483 | free (_retu->header); | ||
484 | free (_retu); | ||
485 | return NULL; | ||
486 | } | ||
487 | } else { | ||
488 | _retu->ext_header = NULL; | ||
489 | } | ||
490 | |||
491 | if ( length - _from_pos <= MAX_RTP_SIZE ) | ||
492 | memcpy ( _retu->data, data + _from_pos, length - _from_pos ); | ||
493 | else { | ||
494 | rtp_free_msg(NULL, _retu); | ||
495 | return NULL; | ||
496 | } | ||
497 | |||
498 | _retu->next = NULL; | ||
499 | |||
500 | return _retu; | ||
501 | } | ||
502 | |||
503 | /** | ||
504 | * @brief Callback for networking core. | ||
505 | * | ||
506 | * @param object RTPSession object. | ||
507 | * @param ip_port Where the message comes from. | ||
508 | * @param data Message data. | ||
509 | * @param length Message length. | ||
510 | * @return int | ||
511 | * @retval -1 Error occurred. | ||
512 | * @retval 0 Success. | ||
513 | */ | ||
514 | int rtp_handle_packet ( void *object, IP_Port ip_port, uint8_t *data, uint32_t length ) | ||
515 | { | ||
516 | RTPSession *_session = object; | ||
517 | RTPMessage *_msg; | ||
518 | |||
519 | if ( !_session || length < 13 ) /* 12 is the minimum length for rtp + desc. byte */ | ||
520 | return -1; | ||
521 | |||
522 | uint8_t _plain[MAX_UDP_PACKET_SIZE]; | ||
523 | |||
524 | uint16_t _sequnum; | ||
525 | bytes_to_U16(&_sequnum, data + 1); | ||
526 | |||
527 | /* Clculate the right nonce */ | ||
528 | uint8_t _calculated[crypto_box_NONCEBYTES]; | ||
529 | memcpy(_calculated, _session->decrypt_nonce, crypto_box_NONCEBYTES); | ||
530 | increase_nonce ( _calculated, _sequnum ); | ||
531 | |||
532 | /* Decrypt message */ | ||
533 | int _decrypted_length = decrypt_data_symmetric( | ||
534 | (uint8_t *)_session->decrypt_key, _calculated, data + 3, length - 3, _plain ); | ||
535 | |||
536 | /* This packet is either not encrypted properly or late | ||
537 | */ | ||
538 | if ( -1 == _decrypted_length ) { | ||
539 | |||
540 | /* If this is the case, then the packet is most likely late. | ||
541 | * Try with old nonce cycle. | ||
542 | */ | ||
543 | if ( _session->rsequnum < _sequnum ) { | ||
544 | _decrypted_length = decrypt_data_symmetric( | ||
545 | (uint8_t *)_session->decrypt_key, _session->nonce_cycle, data + 3, length - 3, _plain ); | ||
546 | |||
547 | if ( !_decrypted_length ) return -1; /* This packet is not encrypted properly */ | ||
548 | |||
549 | /* Otherwise, if decryption is ok with new cycle, set new cycle | ||
550 | */ | ||
551 | } else { | ||
552 | increase_nonce ( _calculated, MAX_SEQU_NUM ); | ||
553 | _decrypted_length = decrypt_data_symmetric( | ||
554 | (uint8_t *)_session->decrypt_key, _calculated, data + 3, length - 3, _plain ); | ||
555 | |||
556 | if ( !_decrypted_length ) return -1; /* This is just an error */ | ||
557 | |||
558 | /* A new cycle setting. */ | ||
559 | memcpy(_session->nonce_cycle, _session->decrypt_nonce, crypto_box_NONCEBYTES); | ||
560 | memcpy(_session->decrypt_nonce, _calculated, crypto_box_NONCEBYTES); | ||
561 | } | ||
562 | } | ||
563 | |||
564 | _msg = msg_parse ( _sequnum, _plain, _decrypted_length ); | ||
565 | |||
566 | if ( !_msg ) return -1; | ||
567 | |||
568 | /* Hopefully this goes well | ||
569 | * NOTE: Is this even used? | ||
570 | */ | ||
571 | memcpy(&_msg->from, &ip_port, sizeof(IP_Port)); | ||
572 | |||
573 | /* Check if message came in late */ | ||
574 | if ( check_late_message(_session, _msg) < 0 ) { /* Not late */ | ||
575 | _session->rsequnum = _msg->header->sequnum; | ||
576 | _session->timestamp = _msg->header->timestamp; | ||
577 | } | ||
578 | |||
579 | pthread_mutex_lock(&_session->mutex); | ||
580 | |||
581 | if ( _session->last_msg ) { | ||
582 | _session->last_msg->next = _msg; | ||
583 | _session->last_msg = _msg; | ||
584 | } else { | ||
585 | _session->last_msg = _session->oldest_msg = _msg; | ||
586 | } | ||
587 | |||
588 | pthread_mutex_unlock(&_session->mutex); | ||
589 | |||
590 | return 0; | ||
591 | } | ||
592 | |||
593 | |||
594 | |||
595 | /** | ||
596 | * @brief Stores headers and payload data in one container ( data ) | ||
597 | * and the length is set accordingly. Returned message is used for sending _only_. | ||
598 | * | ||
599 | * @param session The control session. | ||
600 | * @param data Payload data to send ( This is what you pass ). | ||
601 | * @param length Size of the payload data. | ||
602 | * @return RTPMessage* Created message. | ||
603 | * @retval NULL Error occurred. | ||
604 | */ | ||
605 | RTPMessage *rtp_new_message ( RTPSession *session, const uint8_t *data, uint32_t length ) | ||
606 | { | ||
607 | if ( !session ) | ||
608 | return NULL; | ||
609 | |||
610 | uint8_t *_from_pos; | ||
611 | RTPMessage *_retu = calloc(1, sizeof (RTPMessage)); | ||
612 | assert(_retu); | ||
613 | |||
614 | /* Sets header values and copies the extension header in _retu */ | ||
615 | _retu->header = build_header ( session ); /* It allocates memory and all */ | ||
616 | _retu->ext_header = session->ext_header; | ||
617 | |||
618 | |||
619 | uint32_t _total_length = length + _retu->header->length; | ||
620 | |||
621 | if ( _retu->ext_header ) { | ||
622 | _total_length += ( 4 /* Minimum ext header len */ + _retu->ext_header->length * size_32 ); | ||
623 | |||
624 | _from_pos = add_header ( _retu->header, _retu->data ); | ||
625 | _from_pos = add_ext_header ( _retu->ext_header, _from_pos + 1 ); | ||
626 | } else { | ||
627 | _from_pos = add_header ( _retu->header, _retu->data ); | ||
628 | } | ||
629 | |||
630 | /* | ||
631 | * Parses the extension header into the message | ||
632 | * Of course if any | ||
633 | */ | ||
634 | |||
635 | /* Appends _data on to _retu->_data */ | ||
636 | memcpy ( _from_pos, data, length ); | ||
637 | |||
638 | _retu->length = _total_length; | ||
639 | |||
640 | _retu->next = NULL; | ||
641 | |||
642 | return _retu; | ||
643 | } | ||
644 | |||
645 | |||
646 | |||
647 | |||
648 | |||
649 | |||
650 | |||
651 | /******************************************************************************************************************** | ||
652 | ******************************************************************************************************************** | ||
653 | ******************************************************************************************************************** | ||
654 | ******************************************************************************************************************** | ||
655 | ******************************************************************************************************************** | ||
656 | * | ||
657 | * | ||
658 | * | ||
659 | * PUBLIC API FUNCTIONS IMPLEMENTATIONS | ||
660 | * | ||
661 | * | ||
662 | * | ||
663 | ******************************************************************************************************************** | ||
664 | ******************************************************************************************************************** | ||
665 | ******************************************************************************************************************** | ||
666 | ******************************************************************************************************************** | ||
667 | ********************************************************************************************************************/ | ||
668 | |||
669 | |||
670 | |||
671 | |||
672 | |||
673 | |||
674 | |||
675 | |||
676 | |||
677 | /** | ||
678 | * @brief Release all messages held by session. | ||
679 | * | ||
680 | * @param session The session. | ||
681 | * @return int | ||
682 | * @retval -1 Error occurred. | ||
683 | * @retval 0 Success. | ||
684 | */ | ||
685 | int rtp_release_session_recv ( RTPSession *session ) | ||
686 | { | ||
687 | if ( !session ) { | ||
688 | return -1; | ||
689 | } | ||
690 | |||
691 | RTPMessage *_tmp, * _it; | ||
692 | |||
693 | pthread_mutex_lock(&session->mutex); | ||
694 | |||
695 | for ( _it = session->oldest_msg; _it; _it = _tmp ) { | ||
696 | _tmp = _it->next; | ||
697 | rtp_free_msg( session, _it); | ||
698 | } | ||
699 | |||
700 | session->last_msg = session->oldest_msg = NULL; | ||
701 | |||
702 | pthread_mutex_unlock(&session->mutex); | ||
703 | |||
704 | return 0; | ||
705 | } | ||
706 | |||
707 | |||
708 | /** | ||
709 | * @brief Gets oldest message in the list. | ||
710 | * | ||
711 | * @param session Where the list is. | ||
712 | * @return RTPMessage* The message. You _must_ call rtp_msg_free() to free it. | ||
713 | * @retval NULL No messages in the list, or no list. | ||
714 | */ | ||
715 | RTPMessage *rtp_recv_msg ( RTPSession *session ) | ||
716 | { | ||
717 | if ( !session ) | ||
718 | return NULL; | ||
719 | |||
720 | RTPMessage *_retu = session->oldest_msg; | ||
721 | |||
722 | pthread_mutex_lock(&session->mutex); | ||
723 | |||
724 | if ( _retu ) | ||
725 | session->oldest_msg = _retu->next; | ||
726 | |||
727 | if ( !session->oldest_msg ) | ||
728 | session->last_msg = NULL; | ||
729 | |||
730 | pthread_mutex_unlock(&session->mutex); | ||
731 | |||
732 | return _retu; | ||
733 | } | ||
734 | |||
735 | |||
736 | /** | ||
737 | * @brief Sends data to _RTPSession::dest | ||
738 | * | ||
739 | * @param session The session. | ||
740 | * @param messenger Tox* object. | ||
741 | * @param data The payload. | ||
742 | * @param length Size of the payload. | ||
743 | * @return int | ||
744 | * @retval -1 On error. | ||
745 | * @retval 0 On success. | ||
746 | */ | ||
747 | int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *data, uint16_t length ) | ||
748 | { | ||
749 | RTPMessage *msg = rtp_new_message (session, data, length); | ||
750 | |||
751 | if ( !msg ) return -1; | ||
752 | |||
753 | uint8_t _send_data [ MAX_UDP_PACKET_SIZE ]; | ||
754 | |||
755 | _send_data[0] = session->prefix; | ||
756 | |||
757 | /* Generate the right nonce */ | ||
758 | uint8_t _calculated[crypto_box_NONCEBYTES]; | ||
759 | memcpy(_calculated, session->encrypt_nonce, crypto_box_NONCEBYTES); | ||
760 | increase_nonce ( _calculated, msg->header->sequnum ); | ||
761 | |||
762 | /* Need to skip 2 bytes that are for sequnum */ | ||
763 | int encrypted_length = encrypt_data_symmetric( /* TODO: msg->length - 2 (fix this properly)*/ | ||
764 | (uint8_t *) session->encrypt_key, _calculated, msg->data + 2, msg->length, _send_data + 3 ); | ||
765 | |||
766 | int full_length = encrypted_length + 3; | ||
767 | |||
768 | _send_data[1] = msg->data[0]; | ||
769 | _send_data[2] = msg->data[1]; | ||
770 | |||
771 | |||
772 | /*if ( full_length != sendpacket ( messenger->net, *((IP_Port*) &session->dest), _send_data, full_length) ) {*/ | ||
773 | if ( full_length != send_custom_user_packet(messenger, session->dest, _send_data, full_length) ) { | ||
774 | printf("Rtp error: %s\n", strerror(errno)); | ||
775 | return -1; | ||
776 | } | ||
777 | |||
778 | |||
779 | /* Set sequ number */ | ||
780 | if ( session->sequnum >= MAX_SEQU_NUM ) { | ||
781 | session->sequnum = 0; | ||
782 | memcpy(session->encrypt_nonce, _calculated, crypto_box_NONCEBYTES); | ||
783 | } else { | ||
784 | session->sequnum++; | ||
785 | } | ||
786 | |||
787 | rtp_free_msg ( session, msg ); | ||
788 | return 0; | ||
789 | } | ||
790 | |||
791 | |||
792 | /** | ||
793 | * @brief Speaks for it self. | ||
794 | * | ||
795 | * @param session The control session msg belongs to. You set it as NULL when freeing recved messages. | ||
796 | * Otherwise set it to session the message was created from. | ||
797 | * @param msg The message. | ||
798 | * @return void | ||
799 | */ | ||
800 | void rtp_free_msg ( RTPSession *session, RTPMessage *msg ) | ||
801 | { | ||
802 | if ( !session ) { | ||
803 | free ( msg->header->csrc ); | ||
804 | |||
805 | if ( msg->ext_header ) { | ||
806 | free ( msg->ext_header->table ); | ||
807 | free ( msg->ext_header ); | ||
808 | } | ||
809 | } else { | ||
810 | if ( session->csrc != msg->header->csrc ) | ||
811 | free ( msg->header->csrc ); | ||
812 | |||
813 | if ( msg->ext_header && session->ext_header != msg->ext_header ) { | ||
814 | free ( msg->ext_header->table ); | ||
815 | free ( msg->ext_header ); | ||
816 | } | ||
817 | } | ||
818 | |||
819 | free ( msg->header ); | ||
820 | free ( msg ); | ||
821 | } | ||
822 | |||
823 | |||
824 | /** | ||
825 | * @brief Must be called before calling any other rtp function. It's used | ||
826 | * to initialize RTP control session. | ||
827 | * | ||
828 | * @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType | ||
829 | * @param messenger Tox* object. | ||
830 | * @param friend_num Friend id. | ||
831 | * @param encrypt_key Speaks for it self. | ||
832 | * @param decrypt_key Speaks for it self. | ||
833 | * @param encrypt_nonce Speaks for it self. | ||
834 | * @param decrypt_nonce Speaks for it self. | ||
835 | * @return RTPSession* Created control session. | ||
836 | * @retval NULL Error occurred. | ||
837 | */ | ||
838 | RTPSession *rtp_init_session ( int payload_type, | ||
839 | Messenger *messenger, | ||
840 | int friend_num, | ||
841 | const uint8_t *encrypt_key, | ||
842 | const uint8_t *decrypt_key, | ||
843 | const uint8_t *encrypt_nonce, | ||
844 | const uint8_t *decrypt_nonce ) | ||
845 | { | ||
846 | RTPSession *_retu = calloc(1, sizeof(RTPSession)); | ||
847 | assert(_retu); | ||
848 | |||
849 | /*networking_registerhandler(messenger->net, payload_type, rtp_handle_packet, _retu);*/ | ||
850 | if ( -1 == custom_user_packet_registerhandler(messenger, friend_num, payload_type, rtp_handle_packet, _retu) ) { | ||
851 | fprintf(stderr, "Error setting custom register handler for rtp session\n"); | ||
852 | free(_retu); | ||
853 | return NULL; | ||
854 | } | ||
855 | |||
856 | _retu->version = RTP_VERSION; /* It's always 2 */ | ||
857 | _retu->padding = 0; /* If some additional data is needed about the packet */ | ||
858 | _retu->extension = 0; /* If extension to header is needed */ | ||
859 | _retu->cc = 1; /* Amount of contributors */ | ||
860 | _retu->csrc = NULL; /* Container */ | ||
861 | _retu->ssrc = random_int(); | ||
862 | _retu->marker = 0; | ||
863 | _retu->payload_type = payload_table[payload_type]; | ||
864 | |||
865 | _retu->dest = friend_num; | ||
866 | |||
867 | _retu->rsequnum = _retu->sequnum = 1; | ||
868 | |||
869 | _retu->ext_header = NULL; /* When needed allocate */ | ||
870 | _retu->framerate = -1; | ||
871 | _retu->resolution = -1; | ||
872 | |||
873 | _retu->encrypt_key = encrypt_key; | ||
874 | _retu->decrypt_key = decrypt_key; | ||
875 | |||
876 | /* Need to allocate new memory */ | ||
877 | _retu->encrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); | ||
878 | assert(_retu->encrypt_nonce); | ||
879 | _retu->decrypt_nonce = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); | ||
880 | assert(_retu->decrypt_nonce); | ||
881 | _retu->nonce_cycle = calloc ( crypto_box_NONCEBYTES, sizeof (uint8_t) ); | ||
882 | assert(_retu->nonce_cycle); | ||
883 | |||
884 | memcpy(_retu->encrypt_nonce, encrypt_nonce, crypto_box_NONCEBYTES); | ||
885 | memcpy(_retu->decrypt_nonce, decrypt_nonce, crypto_box_NONCEBYTES); | ||
886 | memcpy(_retu->nonce_cycle , decrypt_nonce, crypto_box_NONCEBYTES); | ||
887 | |||
888 | _retu->csrc = calloc(1, sizeof (uint32_t)); | ||
889 | assert(_retu->csrc); | ||
890 | |||
891 | _retu->csrc[0] = _retu->ssrc; /* Set my ssrc to the list receive */ | ||
892 | |||
893 | /* Also set payload type as prefix */ | ||
894 | _retu->prefix = payload_type; | ||
895 | |||
896 | _retu->oldest_msg = _retu->last_msg = NULL; | ||
897 | |||
898 | pthread_mutex_init(&_retu->mutex, NULL); | ||
899 | /* | ||
900 | * | ||
901 | */ | ||
902 | return _retu; | ||
903 | } | ||
904 | |||
905 | |||
906 | /** | ||
907 | * @brief Terminate the session. | ||
908 | * | ||
909 | * @param session The session. | ||
910 | * @param messenger The messenger who owns the session | ||
911 | * @return int | ||
912 | * @retval -1 Error occurred. | ||
913 | * @retval 0 Success. | ||
914 | */ | ||
915 | int rtp_terminate_session ( RTPSession *session, Messenger *messenger ) | ||
916 | { | ||
917 | if ( !session ) | ||
918 | return -1; | ||
919 | |||
920 | custom_user_packet_registerhandler(messenger, session->dest, session->prefix, NULL, NULL); | ||
921 | |||
922 | free ( session->ext_header ); | ||
923 | free ( session->csrc ); | ||
924 | free ( session->decrypt_nonce ); | ||
925 | free ( session->encrypt_nonce ); | ||
926 | free ( session->nonce_cycle ); | ||
927 | |||
928 | pthread_mutex_destroy(&session->mutex); | ||
929 | |||
930 | /* And finally free session */ | ||
931 | free ( session ); | ||
932 | |||
933 | return 0; | ||
934 | } \ No newline at end of file | ||
diff --git a/toxav/rtp.h b/toxav/rtp.h new file mode 100644 index 00000000..c2b68b01 --- /dev/null +++ b/toxav/rtp.h | |||
@@ -0,0 +1,219 @@ | |||
1 | /** toxrtp.h | ||
2 | * | ||
3 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
4 | * | ||
5 | * This file is part of Tox. | ||
6 | * | ||
7 | * Tox is free software: you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation, either version 3 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * Tox is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | * | ||
21 | * Report bugs/suggestions at #tox-dev @ freenode.net:6667 | ||
22 | */ | ||
23 | |||
24 | #ifndef __TOXRTP | ||
25 | #define __TOXRTP | ||
26 | |||
27 | #define RTP_VERSION 2 | ||
28 | #include <inttypes.h> | ||
29 | #include <pthread.h> | ||
30 | |||
31 | #include "../toxcore/util.h" | ||
32 | #include "../toxcore/network.h" | ||
33 | #include "../toxcore/net_crypto.h" | ||
34 | #include "../toxcore/Messenger.h" | ||
35 | |||
36 | #define MAX_SEQU_NUM 65535 | ||
37 | #define MAX_RTP_SIZE 65535 | ||
38 | |||
39 | /** | ||
40 | * @brief Standard rtp header | ||
41 | * | ||
42 | */ | ||
43 | |||
44 | typedef struct _RTPHeader { | ||
45 | uint8_t flags; /* Version(2),Padding(1), Ext(1), Cc(4) */ | ||
46 | uint8_t marker_payloadt; /* Marker(1), PlayLoad Type(7) */ | ||
47 | uint16_t sequnum; /* Sequence Number */ | ||
48 | uint32_t timestamp; /* Timestamp */ | ||
49 | uint32_t ssrc; /* SSRC */ | ||
50 | uint32_t *csrc; /* CSRC's table */ | ||
51 | uint32_t length; /* Length of the header in payload string. */ | ||
52 | |||
53 | } RTPHeader; | ||
54 | |||
55 | |||
56 | /** | ||
57 | * @brief Standard rtp extension header. | ||
58 | * | ||
59 | */ | ||
60 | typedef struct _RTPExtHeader { | ||
61 | uint16_t type; /* Extension profile */ | ||
62 | uint16_t length; /* Number of extensions */ | ||
63 | uint32_t *table; /* Extension's table */ | ||
64 | |||
65 | } RTPExtHeader; | ||
66 | |||
67 | |||
68 | /** | ||
69 | * @brief Standard rtp message. | ||
70 | * | ||
71 | */ | ||
72 | typedef struct _RTPMessage { | ||
73 | RTPHeader *header; | ||
74 | RTPExtHeader *ext_header; | ||
75 | |||
76 | uint8_t data[MAX_RTP_SIZE]; | ||
77 | uint32_t length; | ||
78 | IP_Port from; | ||
79 | |||
80 | struct _RTPMessage *next; | ||
81 | } RTPMessage; | ||
82 | |||
83 | |||
84 | /** | ||
85 | * @brief Our main session descriptor. | ||
86 | * It measures the session variables and controls | ||
87 | * the entire session. There are functions for manipulating | ||
88 | * the session so tend to use those instead of directly modifying | ||
89 | * session parameters. | ||
90 | * | ||
91 | */ | ||
92 | typedef struct _RTPSession { | ||
93 | uint8_t version; | ||
94 | uint8_t padding; | ||
95 | uint8_t extension; | ||
96 | uint8_t cc; | ||
97 | uint8_t marker; | ||
98 | uint8_t payload_type; | ||
99 | uint16_t sequnum; /* Set when sending */ | ||
100 | uint16_t rsequnum; /* Check when recving msg */ | ||
101 | uint32_t timestamp; | ||
102 | uint32_t ssrc; | ||
103 | uint32_t *csrc; | ||
104 | |||
105 | /* If some additional data must be sent via message | ||
106 | * apply it here. Only by allocating this member you will be | ||
107 | * automatically placing it within a message. | ||
108 | */ | ||
109 | RTPExtHeader *ext_header; | ||
110 | |||
111 | /* External header identifiers */ | ||
112 | int resolution; | ||
113 | int framerate; | ||
114 | |||
115 | |||
116 | /* Since these are only references of the | ||
117 | * call structure don't allocate or free | ||
118 | */ | ||
119 | |||
120 | const uint8_t *encrypt_key; | ||
121 | const uint8_t *decrypt_key; | ||
122 | uint8_t *encrypt_nonce; | ||
123 | uint8_t *decrypt_nonce; | ||
124 | |||
125 | uint8_t *nonce_cycle; | ||
126 | |||
127 | RTPMessage *oldest_msg; | ||
128 | RTPMessage *last_msg; /* tail */ | ||
129 | |||
130 | /* Msg prefix for core to know when recving */ | ||
131 | uint8_t prefix; | ||
132 | |||
133 | pthread_mutex_t mutex; | ||
134 | int dest; | ||
135 | |||
136 | } RTPSession; | ||
137 | |||
138 | |||
139 | /** | ||
140 | * @brief Release all messages held by session. | ||
141 | * | ||
142 | * @param session The session. | ||
143 | * @return int | ||
144 | * @retval -1 Error occurred. | ||
145 | * @retval 0 Success. | ||
146 | */ | ||
147 | int rtp_release_session_recv ( RTPSession *session ); | ||
148 | |||
149 | |||
150 | /** | ||
151 | * @brief Get's oldest message in the list. | ||
152 | * | ||
153 | * @param session Where the list is. | ||
154 | * @return RTPMessage* The message. You need to call rtp_msg_free() to free it. | ||
155 | * @retval NULL No messages in the list, or no list. | ||
156 | */ | ||
157 | RTPMessage *rtp_recv_msg ( RTPSession *session ); | ||
158 | |||
159 | |||
160 | /** | ||
161 | * @brief Sends msg to _RTPSession::dest | ||
162 | * | ||
163 | * @param session The session. | ||
164 | * @param msg The message | ||
165 | * @param messenger Tox* object. | ||
166 | * @return int | ||
167 | * @retval -1 On error. | ||
168 | * @retval 0 On success. | ||
169 | */ | ||
170 | int rtp_send_msg ( RTPSession *session, Messenger *messenger, const uint8_t *data, uint16_t length ); | ||
171 | |||
172 | |||
173 | /** | ||
174 | * @brief Speaks for it self. | ||
175 | * | ||
176 | * @param session The control session msg belongs to. It can be NULL. | ||
177 | * @param msg The message. | ||
178 | * @return void | ||
179 | */ | ||
180 | void rtp_free_msg ( RTPSession *session, RTPMessage *msg ); | ||
181 | |||
182 | |||
183 | /** | ||
184 | * @brief Must be called before calling any other rtp function. It's used | ||
185 | * to initialize RTP control session. | ||
186 | * | ||
187 | * @param payload_type Type of payload used to send. You can use values in toxmsi.h::MSICallType | ||
188 | * @param messenger Tox* object. | ||
189 | * @param friend_num Friend id. | ||
190 | * @param encrypt_key Speaks for it self. | ||
191 | * @param decrypt_key Speaks for it self. | ||
192 | * @param encrypt_nonce Speaks for it self. | ||
193 | * @param decrypt_nonce Speaks for it self. | ||
194 | * @return RTPSession* Created control session. | ||
195 | * @retval NULL Error occurred. | ||
196 | */ | ||
197 | RTPSession *rtp_init_session ( int payload_type, | ||
198 | Messenger *messenger, | ||
199 | int friend_num, | ||
200 | const uint8_t *encrypt_key, | ||
201 | const uint8_t *decrypt_key, | ||
202 | const uint8_t *encrypt_nonce, | ||
203 | const uint8_t *decrypt_nonce ); | ||
204 | |||
205 | |||
206 | /** | ||
207 | * @brief Terminate the session. | ||
208 | * | ||
209 | * @param session The session. | ||
210 | * @param messenger The messenger who owns the session | ||
211 | * @return int | ||
212 | * @retval -1 Error occurred. | ||
213 | * @retval 0 Success. | ||
214 | */ | ||
215 | int rtp_terminate_session ( RTPSession *session, Messenger *messenger ); | ||
216 | |||
217 | |||
218 | |||
219 | #endif /* __TOXRTP */ | ||
diff --git a/toxav/toxav.c b/toxav/toxav.c new file mode 100644 index 00000000..f704bc6b --- /dev/null +++ b/toxav/toxav.c | |||
@@ -0,0 +1,551 @@ | |||
1 | /** toxav.c | ||
2 | * | ||
3 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
4 | * | ||
5 | * This file is part of Tox. | ||
6 | * | ||
7 | * Tox is free software: you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation, either version 3 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * Tox is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | * | ||
21 | * Report bugs/suggestions at #tox-dev @ freenode.net:6667 | ||
22 | */ | ||
23 | |||
24 | #ifdef HAVE_CONFIG_H | ||
25 | #include "config.h" | ||
26 | #endif /* HAVE_CONFIG_H */ | ||
27 | |||
28 | #include "media.h" | ||
29 | #include "rtp.h" | ||
30 | #include "msi.h" | ||
31 | |||
32 | #include <stdlib.h> | ||
33 | #include <string.h> | ||
34 | #include <assert.h> | ||
35 | |||
36 | #include "toxav.h" | ||
37 | |||
38 | #define inline__ inline __attribute__((always_inline)) | ||
39 | |||
40 | static const uint8_t audio_index = 0, video_index = 1; | ||
41 | |||
42 | |||
43 | typedef enum { | ||
44 | ts_closing, | ||
45 | ts_running, | ||
46 | ts_closed | ||
47 | |||
48 | } ThreadState; | ||
49 | |||
50 | typedef struct _ToxAv { | ||
51 | Messenger *messenger; | ||
52 | |||
53 | MSISession *msi_session; /** Main msi session */ | ||
54 | |||
55 | RTPSession *rtp_sessions[2]; /* Audio is first and video is second */ | ||
56 | |||
57 | struct jitter_buffer *j_buf; | ||
58 | CodecState *cs; | ||
59 | |||
60 | void *agent_handler; | ||
61 | } ToxAv; | ||
62 | |||
63 | /** | ||
64 | * @brief Start new A/V session. There can only be one session at the time. If you register more | ||
65 | * it will result in undefined behaviour. | ||
66 | * | ||
67 | * @param messenger The messenger handle. | ||
68 | * @param useragent The agent handling A/V session (i.e. phone). | ||
69 | * @param ua_name Useragent name. | ||
70 | * @param video_width Width of video frame. | ||
71 | * @param video_height Height of video frame. | ||
72 | * @return ToxAv* | ||
73 | * @retval NULL On error. | ||
74 | */ | ||
75 | ToxAv *toxav_new( Tox *messenger, void *useragent, const char *ua_name , uint16_t video_width, uint16_t video_height) | ||
76 | { | ||
77 | ToxAv *av = calloc ( sizeof(ToxAv), 1); | ||
78 | |||
79 | if (av == NULL) | ||
80 | return NULL; | ||
81 | |||
82 | av->messenger = (Messenger *)messenger; | ||
83 | |||
84 | av->msi_session = msi_init_session(av->messenger, (const unsigned char *) ua_name ); | ||
85 | av->msi_session->agent_handler = av; | ||
86 | |||
87 | av->rtp_sessions[0] = av->rtp_sessions [1] = NULL; | ||
88 | |||
89 | /* NOTE: This should be user defined or? */ | ||
90 | av->j_buf = create_queue(20); | ||
91 | |||
92 | av->cs = codec_init_session(AUDIO_BITRATE, AUDIO_FRAME_DURATION, AUDIO_SAMPLE_RATE, AUDIO_CHANNELS, video_width, | ||
93 | video_height, VIDEO_BITRATE); | ||
94 | |||
95 | av->agent_handler = useragent; | ||
96 | |||
97 | return av; | ||
98 | } | ||
99 | |||
100 | /** | ||
101 | * @brief Remove A/V session. | ||
102 | * | ||
103 | * @param av Handler. | ||
104 | * @return void | ||
105 | */ | ||
106 | void toxav_kill ( ToxAv *av ) | ||
107 | { | ||
108 | msi_terminate_session(av->msi_session); | ||
109 | |||
110 | if ( av->rtp_sessions[audio_index] ) { | ||
111 | rtp_terminate_session(av->rtp_sessions[audio_index], av->msi_session->messenger_handle); | ||
112 | } | ||
113 | |||
114 | if ( av->rtp_sessions[video_index] ) { | ||
115 | rtp_terminate_session(av->rtp_sessions[video_index], av->msi_session->messenger_handle); | ||
116 | } | ||
117 | |||
118 | codec_terminate_session(av->cs); | ||
119 | |||
120 | free(av); | ||
121 | } | ||
122 | |||
123 | /** | ||
124 | * @brief Register callback for call state. | ||
125 | * | ||
126 | * @param callback The callback | ||
127 | * @param id One of the ToxAvCallbackID values | ||
128 | * @return void | ||
129 | */ | ||
130 | void toxav_register_callstate_callback ( ToxAVCallback callback, ToxAvCallbackID id ) | ||
131 | { | ||
132 | msi_register_callback((MSICallback)callback, (MSICallbackID) id); | ||
133 | } | ||
134 | |||
135 | /** | ||
136 | * @brief Call user. Use its friend_id. | ||
137 | * | ||
138 | * @param av Handler. | ||
139 | * @param user The user. | ||
140 | * @param call_type Call type. | ||
141 | * @param ringing_seconds Ringing timeout. | ||
142 | * @return int | ||
143 | * @retval 0 Success. | ||
144 | * @retval ToxAvError On error. | ||
145 | */ | ||
146 | int toxav_call (ToxAv *av, int user, ToxAvCallType call_type, int ringing_seconds ) | ||
147 | { | ||
148 | if ( av->msi_session->call ) { | ||
149 | return ErrorAlreadyInCall; | ||
150 | } | ||
151 | |||
152 | return msi_invite(av->msi_session, call_type, ringing_seconds * 1000, user); | ||
153 | } | ||
154 | |||
155 | /** | ||
156 | * @brief Hangup active call. | ||
157 | * | ||
158 | * @param av Handler. | ||
159 | * @return int | ||
160 | * @retval 0 Success. | ||
161 | * @retval ToxAvError On error. | ||
162 | */ | ||
163 | int toxav_hangup ( ToxAv *av ) | ||
164 | { | ||
165 | if ( !av->msi_session->call ) { | ||
166 | return ErrorNoCall; | ||
167 | } | ||
168 | |||
169 | if ( av->msi_session->call->state != call_active ) { | ||
170 | return ErrorInvalidState; | ||
171 | } | ||
172 | |||
173 | return msi_hangup(av->msi_session); | ||
174 | } | ||
175 | |||
176 | /** | ||
177 | * @brief Answer incomming call. | ||
178 | * | ||
179 | * @param av Handler. | ||
180 | * @param call_type Answer with... | ||
181 | * @return int | ||
182 | * @retval 0 Success. | ||
183 | * @retval ToxAvError On error. | ||
184 | */ | ||
185 | int toxav_answer ( ToxAv *av, ToxAvCallType call_type ) | ||
186 | { | ||
187 | if ( !av->msi_session->call ) { | ||
188 | return ErrorNoCall; | ||
189 | } | ||
190 | |||
191 | if ( av->msi_session->call->state != call_starting ) { | ||
192 | return ErrorInvalidState; | ||
193 | } | ||
194 | |||
195 | return msi_answer(av->msi_session, call_type); | ||
196 | } | ||
197 | |||
198 | /** | ||
199 | * @brief Reject incomming call. | ||
200 | * | ||
201 | * @param av Handler. | ||
202 | * @param reason Optional reason. Set NULL if none. | ||
203 | * @return int | ||
204 | * @retval 0 Success. | ||
205 | * @retval ToxAvError On error. | ||
206 | */ | ||
207 | int toxav_reject ( ToxAv *av, const char *reason ) | ||
208 | { | ||
209 | if ( !av->msi_session->call ) { | ||
210 | return ErrorNoCall; | ||
211 | } | ||
212 | |||
213 | if ( av->msi_session->call->state != call_starting ) { | ||
214 | return ErrorInvalidState; | ||
215 | } | ||
216 | |||
217 | return msi_reject(av->msi_session, (const uint8_t *) reason); | ||
218 | } | ||
219 | |||
220 | /** | ||
221 | * @brief Cancel outgoing request. | ||
222 | * | ||
223 | * @param av Handler. | ||
224 | * @param reason Optional reason. | ||
225 | * @return int | ||
226 | * @retval 0 Success. | ||
227 | * @retval ToxAvError On error. | ||
228 | */ | ||
229 | int toxav_cancel ( ToxAv *av, const char *reason ) | ||
230 | { | ||
231 | if ( !av->msi_session->call ) { | ||
232 | return ErrorNoCall; | ||
233 | } | ||
234 | |||
235 | return msi_cancel(av->msi_session, 0, (const uint8_t *)reason); | ||
236 | } | ||
237 | |||
238 | /** | ||
239 | * @brief Terminate transmission. Note that transmission will be terminated without informing remote peer. | ||
240 | * | ||
241 | * @param av Handler. | ||
242 | * @return int | ||
243 | * @retval 0 Success. | ||
244 | * @retval ToxAvError On error. | ||
245 | */ | ||
246 | int toxav_stop_call ( ToxAv *av ) | ||
247 | { | ||
248 | if ( !av->msi_session->call ) { | ||
249 | return ErrorNoCall; | ||
250 | } | ||
251 | |||
252 | return msi_stopcall(av->msi_session); | ||
253 | } | ||
254 | |||
255 | /** | ||
256 | * @brief Must be call before any RTP transmission occurs. | ||
257 | * | ||
258 | * @param av Handler. | ||
259 | * @return int | ||
260 | * @retval 0 Success. | ||
261 | * @retval ToxAvError On error. | ||
262 | */ | ||
263 | int toxav_prepare_transmission ( ToxAv *av ) | ||
264 | { | ||
265 | assert(av->msi_session); | ||
266 | |||
267 | if ( !av->msi_session || !av->msi_session->call ) { | ||
268 | return ErrorNoCall; | ||
269 | } | ||
270 | |||
271 | av->rtp_sessions[audio_index] = rtp_init_session( | ||
272 | type_audio, | ||
273 | av->messenger, | ||
274 | av->msi_session->call->peers[0], | ||
275 | av->msi_session->call->key_peer, | ||
276 | av->msi_session->call->key_local, | ||
277 | av->msi_session->call->nonce_peer, | ||
278 | av->msi_session->call->nonce_local | ||
279 | ); | ||
280 | |||
281 | |||
282 | if ( !av->rtp_sessions[audio_index] ) { | ||
283 | fprintf(stderr, "Error while starting audio RTP session!\n"); | ||
284 | return ErrorStartingAudioRtp; | ||
285 | } | ||
286 | |||
287 | av->rtp_sessions[video_index] = rtp_init_session ( | ||
288 | type_video, | ||
289 | av->messenger, | ||
290 | av->msi_session->call->peers[0], | ||
291 | av->msi_session->call->key_peer, | ||
292 | av->msi_session->call->key_local, | ||
293 | av->msi_session->call->nonce_peer, | ||
294 | av->msi_session->call->nonce_local | ||
295 | ); | ||
296 | |||
297 | |||
298 | if ( !av->rtp_sessions[video_index] ) { | ||
299 | fprintf(stderr, "Error while starting video RTP session!\n"); | ||
300 | return ErrorStartingVideoRtp; | ||
301 | } | ||
302 | |||
303 | return ErrorNone; | ||
304 | } | ||
305 | |||
306 | /** | ||
307 | * @brief Call this at the end of the transmission. | ||
308 | * | ||
309 | * @param av Handler. | ||
310 | * @return int | ||
311 | * @retval 0 Success. | ||
312 | * @retval ToxAvError On error. | ||
313 | */ | ||
314 | int toxav_kill_transmission ( ToxAv *av ) | ||
315 | { | ||
316 | /* Both sessions should be active at any time */ | ||
317 | if ( !av->rtp_sessions[0] || !av->rtp_sessions[0] ) | ||
318 | return ErrorNoTransmission; | ||
319 | |||
320 | |||
321 | if ( -1 == rtp_terminate_session(av->rtp_sessions[audio_index], av->messenger) ) { | ||
322 | fprintf(stderr, "Error while terminating audio RTP session!\n"); | ||
323 | return ErrorTerminatingAudioRtp; | ||
324 | } | ||
325 | |||
326 | if ( -1 == rtp_terminate_session(av->rtp_sessions[video_index], av->messenger) ) { | ||
327 | fprintf(stderr, "Error while terminating video RTP session!\n"); | ||
328 | return ErrorTerminatingVideoRtp; | ||
329 | } | ||
330 | |||
331 | return ErrorNone; | ||
332 | } | ||
333 | |||
334 | |||
335 | /** | ||
336 | * @brief Send RTP payload. | ||
337 | * | ||
338 | * @param av Handler. | ||
339 | * @param type Type of payload. | ||
340 | * @param payload The payload. | ||
341 | * @param length Size of it. | ||
342 | * @return int | ||
343 | * @retval 0 Success. | ||
344 | * @retval -1 Failure. | ||
345 | */ | ||
346 | inline__ int toxav_send_rtp_payload ( ToxAv *av, ToxAvCallType type, const uint8_t *payload, uint16_t length ) | ||
347 | { | ||
348 | if ( av->rtp_sessions[type - TypeAudio] ) | ||
349 | return rtp_send_msg ( av->rtp_sessions[type - TypeAudio], av->msi_session->messenger_handle, payload, length ); | ||
350 | else return -1; | ||
351 | } | ||
352 | |||
353 | /** | ||
354 | * @brief Receive RTP payload. | ||
355 | * | ||
356 | * @param av Handler. | ||
357 | * @param type Type of the payload. | ||
358 | * @param dest Storage. | ||
359 | * @return int | ||
360 | * @retval ToxAvError On Error. | ||
361 | * @retval >=0 Size of received payload. | ||
362 | */ | ||
363 | inline__ int toxav_recv_rtp_payload ( ToxAv *av, ToxAvCallType type, uint8_t *dest ) | ||
364 | { | ||
365 | if ( !dest ) return ErrorInternal; | ||
366 | |||
367 | if ( !av->rtp_sessions[type - TypeAudio] ) return ErrorNoRtpSession; | ||
368 | |||
369 | RTPMessage *message; | ||
370 | |||
371 | if ( type == TypeAudio ) { | ||
372 | |||
373 | do { | ||
374 | message = rtp_recv_msg(av->rtp_sessions[audio_index]); | ||
375 | |||
376 | if (message) { | ||
377 | /* push the packet into the queue */ | ||
378 | queue(av->j_buf, message); | ||
379 | } | ||
380 | } while (message); | ||
381 | |||
382 | int success = 0; | ||
383 | message = dequeue(av->j_buf, &success); | ||
384 | |||
385 | if ( success == 2) return ErrorAudioPacketLost; | ||
386 | } else { | ||
387 | message = rtp_recv_msg(av->rtp_sessions[video_index]); | ||
388 | } | ||
389 | |||
390 | if ( message ) { | ||
391 | memcpy(dest, message->data, message->length); | ||
392 | |||
393 | int length = message->length; | ||
394 | |||
395 | rtp_free_msg(NULL, message); | ||
396 | |||
397 | return length; | ||
398 | } | ||
399 | |||
400 | return 0; | ||
401 | } | ||
402 | |||
403 | /** | ||
404 | * @brief Receive decoded video packet. | ||
405 | * | ||
406 | * @param av Handler. | ||
407 | * @param output Storage. | ||
408 | * @return int | ||
409 | * @retval 0 Success. | ||
410 | * @retval ToxAvError On Error. | ||
411 | */ | ||
412 | inline__ int toxav_recv_video ( ToxAv *av, vpx_image_t **output) | ||
413 | { | ||
414 | if ( !output ) return ErrorInternal; | ||
415 | |||
416 | uint8_t packet [RTP_PAYLOAD_SIZE]; | ||
417 | int recved_size = 0; | ||
418 | |||
419 | do { | ||
420 | recved_size = toxav_recv_rtp_payload(av, TypeVideo, packet); | ||
421 | |||
422 | if (recved_size > 0) { | ||
423 | printf("decode: %s\n", vpx_codec_err_to_string(vpx_codec_decode(&av->cs->v_decoder, packet, recved_size, NULL, 0))); | ||
424 | } | ||
425 | } while (recved_size > 0); | ||
426 | |||
427 | vpx_codec_iter_t iter = NULL; | ||
428 | vpx_image_t *img; | ||
429 | img = vpx_codec_get_frame(&av->cs->v_decoder, &iter); | ||
430 | |||
431 | if (img == NULL) | ||
432 | return ErrorInternal; | ||
433 | |||
434 | *output = img; | ||
435 | return 0; | ||
436 | } | ||
437 | |||
438 | /** | ||
439 | * @brief Encode and send video packet. | ||
440 | * | ||
441 | * @param av Handler. | ||
442 | * @param input The packet. | ||
443 | * @return int | ||
444 | * @retval 0 Success. | ||
445 | * @retval ToxAvError On error. | ||
446 | */ | ||
447 | inline__ int toxav_send_video ( ToxAv *av, vpx_image_t *input) | ||
448 | { | ||
449 | if (vpx_codec_encode(&av->cs->v_encoder, input, av->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US) != VPX_CODEC_OK) { | ||
450 | printf("could not encode video frame\n"); | ||
451 | return ErrorInternal; | ||
452 | } | ||
453 | |||
454 | ++av->cs->frame_counter; | ||
455 | |||
456 | vpx_codec_iter_t iter = NULL; | ||
457 | const vpx_codec_cx_pkt_t *pkt; | ||
458 | int sent = 0; | ||
459 | |||
460 | while ( (pkt = vpx_codec_get_cx_data(&av->cs->v_encoder, &iter)) ) { | ||
461 | if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) { | ||
462 | if (toxav_send_rtp_payload(av, TypeVideo, pkt->data.frame.buf, pkt->data.frame.sz) != -1) | ||
463 | ++sent; | ||
464 | } | ||
465 | } | ||
466 | |||
467 | if (sent > 0) | ||
468 | return 0; | ||
469 | |||
470 | return ErrorInternal; | ||
471 | } | ||
472 | |||
473 | /** | ||
474 | * @brief Receive decoded audio frame. | ||
475 | * | ||
476 | * @param av Handler. | ||
477 | * @param frame_size ... | ||
478 | * @param dest Destination of the packet. Make sure it has enough space for | ||
479 | * RTP_PAYLOAD_SIZE bytes. | ||
480 | * @return int | ||
481 | * @retval >=0 Size of received packet. | ||
482 | * @retval ToxAvError On error. | ||
483 | */ | ||
484 | inline__ int toxav_recv_audio ( ToxAv *av, int frame_size, int16_t *dest ) | ||
485 | { | ||
486 | if ( !dest ) return ErrorInternal; | ||
487 | |||
488 | uint8_t packet [RTP_PAYLOAD_SIZE]; | ||
489 | |||
490 | int recved_size = toxav_recv_rtp_payload(av, TypeAudio, packet); | ||
491 | |||
492 | if ( recved_size == ErrorAudioPacketLost ) { | ||
493 | printf("Lost packet\n"); | ||
494 | return opus_decode(av->cs->audio_decoder, NULL, 0, dest, frame_size, 1); | ||
495 | } else if ( recved_size ) { | ||
496 | return opus_decode(av->cs->audio_decoder, packet, recved_size, dest, frame_size, 0); | ||
497 | } else { | ||
498 | return 0; /* Nothing received */ | ||
499 | } | ||
500 | } | ||
501 | |||
502 | /** | ||
503 | * @brief Encode and send audio frame. | ||
504 | * | ||
505 | * @param av Handler. | ||
506 | * @param frame The frame. | ||
507 | * @param frame_size It's size. | ||
508 | * @return int | ||
509 | * @retval 0 Success. | ||
510 | * @retval ToxAvError On error. | ||
511 | */ | ||
512 | inline__ int toxav_send_audio ( ToxAv *av, const int16_t *frame, int frame_size) | ||
513 | { | ||
514 | uint8_t temp_data[RTP_PAYLOAD_SIZE]; | ||
515 | int32_t ret = opus_encode(av->cs->audio_encoder, frame, frame_size, temp_data, sizeof(temp_data)); | ||
516 | |||
517 | if (ret <= 0) | ||
518 | return ErrorInternal; | ||
519 | |||
520 | return toxav_send_rtp_payload(av, TypeAudio, temp_data, ret); | ||
521 | } | ||
522 | |||
523 | /** | ||
524 | * @brief Get peer transmission type. It can either be audio or video. | ||
525 | * | ||
526 | * @param av Handler. | ||
527 | * @param peer The peer | ||
528 | * @return int | ||
529 | * @retval ToxAvCallType On success. | ||
530 | * @retval ToxAvError On error. | ||
531 | */ | ||
532 | int toxav_get_peer_transmission_type ( ToxAv *av, int peer ) | ||
533 | { | ||
534 | assert(av->msi_session); | ||
535 | |||
536 | if ( peer < 0 || !av->msi_session->call || av->msi_session->call->peer_count <= peer ) | ||
537 | return ErrorInternal; | ||
538 | |||
539 | return av->msi_session->call->type_peer[peer]; | ||
540 | } | ||
541 | |||
542 | /** | ||
543 | * @brief Get reference to an object that is handling av session. | ||
544 | * | ||
545 | * @param av Handler. | ||
546 | * @return void* | ||
547 | */ | ||
548 | void *toxav_get_agent_handler ( ToxAv *av ) | ||
549 | { | ||
550 | return av->agent_handler; | ||
551 | } | ||
diff --git a/toxav/toxav.h b/toxav/toxav.h new file mode 100644 index 00000000..be7a2950 --- /dev/null +++ b/toxav/toxav.h | |||
@@ -0,0 +1,300 @@ | |||
1 | /** toxav.h | ||
2 | * | ||
3 | * Copyright (C) 2013 Tox project All Rights Reserved. | ||
4 | * | ||
5 | * This file is part of Tox. | ||
6 | * | ||
7 | * Tox is free software: you can redistribute it and/or modify | ||
8 | * it under the terms of the GNU General Public License as published by | ||
9 | * the Free Software Foundation, either version 3 of the License, or | ||
10 | * (at your option) any later version. | ||
11 | * | ||
12 | * Tox is distributed in the hope that it will be useful, | ||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
15 | * GNU General Public License for more details. | ||
16 | * | ||
17 | * You should have received a copy of the GNU General Public License | ||
18 | * along with Tox. If not, see <http://www.gnu.org/licenses/>. | ||
19 | * | ||
20 | * | ||
21 | * Report bugs/suggestions at #tox-dev @ freenode.net:6667 | ||
22 | */ | ||
23 | |||
24 | |||
25 | #ifndef __TOXAV | ||
26 | #define __TOXAV | ||
27 | #include <inttypes.h> | ||
28 | |||
29 | /* vpx_image_t */ | ||
30 | #include <vpx/vpx_image.h> | ||
31 | |||
32 | typedef void *( *ToxAVCallback ) ( void *arg ); | ||
33 | typedef struct _ToxAv ToxAv; | ||
34 | |||
35 | #ifndef __TOX_DEFINED__ | ||
36 | #define __TOX_DEFINED__ | ||
37 | typedef struct Tox Tox; | ||
38 | #endif | ||
39 | |||
40 | #define RTP_PAYLOAD_SIZE 65535 | ||
41 | |||
42 | /* Default video bitrate in bytes/s */ | ||
43 | #define VIDEO_BITRATE 10*1000*100 | ||
44 | |||
45 | /* Default audio bitrate in bits/s */ | ||
46 | #define AUDIO_BITRATE 64000 | ||
47 | |||
48 | /* Number of audio channels. */ | ||
49 | #define AUDIO_CHANNELS 1 | ||
50 | |||
51 | /* Audio frame duration in miliseconds */ | ||
52 | #define AUDIO_FRAME_DURATION 20 | ||
53 | |||
54 | /* Audio sample rate recommended to be 48kHz for Opus */ | ||
55 | #define AUDIO_SAMPLE_RATE 48000 | ||
56 | |||
57 | /* The amount of samples in one audio frame */ | ||
58 | #define AUDIO_FRAME_SIZE AUDIO_SAMPLE_RATE*AUDIO_FRAME_DURATION/1000 | ||
59 | |||
60 | /* Assume 60 fps*/ | ||
61 | #define MAX_ENCODE_TIME_US ((1000 / 60) * 1000) | ||
62 | |||
63 | |||
64 | /** | ||
65 | * @brief Callbacks ids that handle the call states. | ||
66 | */ | ||
67 | typedef enum { | ||
68 | /* Requests */ | ||
69 | OnInvite, | ||
70 | OnStart, | ||
71 | OnCancel, | ||
72 | OnReject, | ||
73 | OnEnd, | ||
74 | |||
75 | /* Responses */ | ||
76 | OnRinging, | ||
77 | OnStarting, | ||
78 | OnEnding, | ||
79 | |||
80 | /* Protocol */ | ||
81 | OnError, | ||
82 | OnRequestTimeout | ||
83 | |||
84 | } ToxAvCallbackID; | ||
85 | |||
86 | |||
87 | /** | ||
88 | * @brief Call type identifier. | ||
89 | */ | ||
90 | typedef enum { | ||
91 | TypeAudio = 70, | ||
92 | TypeVideo | ||
93 | } ToxAvCallType; | ||
94 | |||
95 | |||
96 | /** | ||
97 | * @brief Error indicators. | ||
98 | * | ||
99 | */ | ||
100 | typedef enum { | ||
101 | ErrorNone = 0, | ||
102 | ErrorInternal = -1, /* Internal error */ | ||
103 | ErrorAlreadyInCall = -2, /* Already has an active call */ | ||
104 | ErrorNoCall = -3, /* Trying to perform call action while not in a call */ | ||
105 | ErrorInvalidState = -4, /* Trying to perform call action while in invalid state*/ | ||
106 | ErrorNoRtpSession = -5, /* Trying to perform rtp action on invalid session */ | ||
107 | ErrorAudioPacketLost = -6, /* Indicating packet loss */ | ||
108 | ErrorStartingAudioRtp = -7, /* Error in toxav_prepare_transmission() */ | ||
109 | ErrorStartingVideoRtp = -8 , /* Error in toxav_prepare_transmission() */ | ||
110 | ErrorNoTransmission = -9, /* Returned in toxav_kill_transmission() */ | ||
111 | ErrorTerminatingAudioRtp = -10, /* Returned in toxav_kill_transmission() */ | ||
112 | ErrorTerminatingVideoRtp = -11, /* Returned in toxav_kill_transmission() */ | ||
113 | |||
114 | } ToxAvError; | ||
115 | |||
116 | |||
117 | /** | ||
118 | * @brief Start new A/V session. There can only be one session at the time. If you register more | ||
119 | * it will result in undefined behaviour. | ||
120 | * | ||
121 | * @param messenger The messenger handle. | ||
122 | * @param useragent The agent handling A/V session (i.e. phone). | ||
123 | * @param ua_name Useragent name. | ||
124 | * @param video_width Width of video frame. | ||
125 | * @param video_height Height of video frame. | ||
126 | * @return ToxAv* | ||
127 | * @retval NULL On error. | ||
128 | */ | ||
129 | ToxAv *toxav_new(Tox *messenger, void *useragent, const char *ua_name, uint16_t video_width, uint16_t video_height); | ||
130 | |||
131 | /** | ||
132 | * @brief Remove A/V session. | ||
133 | * | ||
134 | * @param av Handler. | ||
135 | * @return void | ||
136 | */ | ||
137 | void toxav_kill(ToxAv *av); | ||
138 | |||
139 | /** | ||
140 | * @brief Register callback for call state. | ||
141 | * | ||
142 | * @param callback The callback | ||
143 | * @param id One of the ToxAvCallbackID values | ||
144 | * @return void | ||
145 | */ | ||
146 | void toxav_register_callstate_callback (ToxAVCallback callback, ToxAvCallbackID id); | ||
147 | |||
148 | /** | ||
149 | * @brief Call user. Use its friend_id. | ||
150 | * | ||
151 | * @param av Handler. | ||
152 | * @param user The user. | ||
153 | * @param call_type Call type. | ||
154 | * @param ringing_seconds Ringing timeout. | ||
155 | * @return int | ||
156 | * @retval 0 Success. | ||
157 | * @retval ToxAvError On error. | ||
158 | */ | ||
159 | int toxav_call(ToxAv *av, int user, ToxAvCallType call_type, int ringing_seconds); | ||
160 | |||
161 | /** | ||
162 | * @brief Hangup active call. | ||
163 | * | ||
164 | * @param av Handler. | ||
165 | * @return int | ||
166 | * @retval 0 Success. | ||
167 | * @retval ToxAvError On error. | ||
168 | */ | ||
169 | int toxav_hangup(ToxAv *av); | ||
170 | |||
171 | /** | ||
172 | * @brief Answer incomming call. | ||
173 | * | ||
174 | * @param av Handler. | ||
175 | * @param call_type Answer with... | ||
176 | * @return int | ||
177 | * @retval 0 Success. | ||
178 | * @retval ToxAvError On error. | ||
179 | */ | ||
180 | int toxav_answer(ToxAv *av, ToxAvCallType call_type ); | ||
181 | |||
182 | /** | ||
183 | * @brief Reject incomming call. | ||
184 | * | ||
185 | * @param av Handler. | ||
186 | * @param reason Optional reason. Set NULL if none. | ||
187 | * @return int | ||
188 | * @retval 0 Success. | ||
189 | * @retval ToxAvError On error. | ||
190 | */ | ||
191 | int toxav_reject(ToxAv *av, const char *reason); | ||
192 | |||
193 | /** | ||
194 | * @brief Cancel outgoing request. | ||
195 | * | ||
196 | * @param av Handler. | ||
197 | * @param reason Optional reason. | ||
198 | * @return int | ||
199 | * @retval 0 Success. | ||
200 | * @retval ToxAvError On error. | ||
201 | */ | ||
202 | int toxav_cancel(ToxAv *av, const char *reason); | ||
203 | |||
204 | /** | ||
205 | * @brief Terminate transmission. Note that transmission will be terminated without informing remote peer. | ||
206 | * | ||
207 | * @param av Handler. | ||
208 | * @return int | ||
209 | * @retval 0 Success. | ||
210 | * @retval ToxAvError On error. | ||
211 | */ | ||
212 | int toxav_stop_call(ToxAv *av); | ||
213 | |||
214 | /** | ||
215 | * @brief Must be call before any RTP transmission occurs. | ||
216 | * | ||
217 | * @param av Handler. | ||
218 | * @return int | ||
219 | * @retval 0 Success. | ||
220 | * @retval ToxAvError On error. | ||
221 | */ | ||
222 | int toxav_prepare_transmission(ToxAv *av); | ||
223 | |||
224 | /** | ||
225 | * @brief Call this at the end of the transmission. | ||
226 | * | ||
227 | * @param av Handler. | ||
228 | * @return int | ||
229 | * @retval 0 Success. | ||
230 | * @retval ToxAvError On error. | ||
231 | */ | ||
232 | int toxav_kill_transmission(ToxAv *av); | ||
233 | |||
234 | /** | ||
235 | * @brief Receive decoded video packet. | ||
236 | * | ||
237 | * @param av Handler. | ||
238 | * @param output Storage. | ||
239 | * @return int | ||
240 | * @retval 0 Success. | ||
241 | * @retval ToxAvError On Error. | ||
242 | */ | ||
243 | int toxav_recv_video ( ToxAv *av, vpx_image_t **output); | ||
244 | |||
245 | /** | ||
246 | * @brief Receive decoded audio frame. | ||
247 | * | ||
248 | * @param av Handler. | ||
249 | * @param frame_size ... | ||
250 | * @param dest Destination of the packet. Make sure it has enough space for | ||
251 | * RTP_PAYLOAD_SIZE bytes. | ||
252 | * @return int | ||
253 | * @retval >=0 Size of received packet. | ||
254 | * @retval ToxAvError On error. | ||
255 | */ | ||
256 | int toxav_recv_audio( ToxAv *av, int frame_size, int16_t *dest ); | ||
257 | |||
258 | /** | ||
259 | * @brief Encode and send video packet. | ||
260 | * | ||
261 | * @param av Handler. | ||
262 | * @param input The packet. | ||
263 | * @return int | ||
264 | * @retval 0 Success. | ||
265 | * @retval ToxAvError On error. | ||
266 | */ | ||
267 | int toxav_send_video ( ToxAv *av, vpx_image_t *input); | ||
268 | |||
269 | /** | ||
270 | * @brief Encode and send audio frame. | ||
271 | * | ||
272 | * @param av Handler. | ||
273 | * @param frame The frame. | ||
274 | * @param frame_size It's size. | ||
275 | * @return int | ||
276 | * @retval 0 Success. | ||
277 | * @retval ToxAvError On error. | ||
278 | */ | ||
279 | int toxav_send_audio ( ToxAv *av, const int16_t *frame, int frame_size); | ||
280 | |||
281 | /** | ||
282 | * @brief Get peer transmission type. It can either be audio or video. | ||
283 | * | ||
284 | * @param av Handler. | ||
285 | * @param peer The peer | ||
286 | * @return int | ||
287 | * @retval ToxAvCallType On success. | ||
288 | * @retval ToxAvError On error. | ||
289 | */ | ||
290 | int toxav_get_peer_transmission_type ( ToxAv *av, int peer ); | ||
291 | |||
292 | /** | ||
293 | * @brief Get reference to an object that is handling av session. | ||
294 | * | ||
295 | * @param av Handler. | ||
296 | * @return void* | ||
297 | */ | ||
298 | void *toxav_get_agent_handler ( ToxAv *av ); | ||
299 | |||
300 | #endif /* __TOXAV */ \ No newline at end of file | ||