summaryrefslogtreecommitdiff
path: root/toxav
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-08-31 19:12:19 +0100
committeriphydf <iphydf@users.noreply.github.com>2016-08-31 20:04:16 +0100
commit633da98ae69866efb195e00d9a3a22ace6bada66 (patch)
tree875535f3d2257c4ea5bb97a553b2f1beab4a1590 /toxav
parent6356eb4e4fe407fa7870f2a685d0d08b5c2ec5bb (diff)
Add braces to all if statements.
Diffstat (limited to 'toxav')
-rw-r--r--toxav/audio.c39
-rw-r--r--toxav/bwcontroller.c24
-rw-r--r--toxav/group.c85
-rw-r--r--toxav/msi.c59
-rw-r--r--toxav/rtp.c45
-rw-r--r--toxav/toxav.c118
-rw-r--r--toxav/video.c28
7 files changed, 262 insertions, 136 deletions
diff --git a/toxav/audio.c b/toxav/audio.c
index 9fd8f6f1..eaa1f6d0 100644
--- a/toxav/audio.c
+++ b/toxav/audio.c
@@ -76,8 +76,9 @@ ACSession *ac_new(Logger *log, ToxAV *av, uint32_t friend_number, toxav_audio_re
76 /* Initialize encoders with default values */ 76 /* Initialize encoders with default values */
77 ac->encoder = create_audio_encoder(log, 48000, 48000, 2); 77 ac->encoder = create_audio_encoder(log, 48000, 48000, 2);
78 78
79 if (ac->encoder == NULL) 79 if (ac->encoder == NULL) {
80 goto DECODER_CLEANUP; 80 goto DECODER_CLEANUP;
81 }
81 82
82 ac->le_bit_rate = 48000; 83 ac->le_bit_rate = 48000;
83 ac->le_sample_rate = 48000; 84 ac->le_sample_rate = 48000;
@@ -110,8 +111,9 @@ BASE_CLEANUP:
110} 111}
111void ac_kill(ACSession *ac) 112void ac_kill(ACSession *ac)
112{ 113{
113 if (!ac) 114 if (!ac) {
114 return; 115 return;
116 }
115 117
116 opus_encoder_destroy(ac->encoder); 118 opus_encoder_destroy(ac->encoder);
117 opus_decoder_destroy(ac->decoder); 119 opus_decoder_destroy(ac->decoder);
@@ -124,8 +126,9 @@ void ac_kill(ACSession *ac)
124} 126}
125void ac_iterate(ACSession *ac) 127void ac_iterate(ACSession *ac)
126{ 128{
127 if (!ac) 129 if (!ac) {
128 return; 130 return;
131 }
129 132
130 /* TODO fix this and jitter buffering */ 133 /* TODO fix this and jitter buffering */
131 134
@@ -196,8 +199,9 @@ void ac_iterate(ACSession *ac)
196} 199}
197int ac_queue_message(void *acp, struct RTPMessage *msg) 200int ac_queue_message(void *acp, struct RTPMessage *msg)
198{ 201{
199 if (!acp || !msg) 202 if (!acp || !msg) {
200 return -1; 203 return -1;
204 }
201 205
202 ACSession *ac = acp; 206 ACSession *ac = acp;
203 207
@@ -231,8 +235,9 @@ int ac_reconfigure_encoder(ACSession *ac, int32_t bit_rate, int32_t sampling_rat
231 sampling_rate, channels, 235 sampling_rate, channels,
232 &ac->le_bit_rate, 236 &ac->le_bit_rate,
233 &ac->le_sample_rate, 237 &ac->le_sample_rate,
234 &ac->le_channel_count)) 238 &ac->le_channel_count)) {
235 return -1; 239 return -1;
240 }
236 241
237 return 0; 242 return 0;
238} 243}
@@ -257,7 +262,9 @@ static struct JitterBuffer *jbuf_new(uint32_t capacity)
257 262
258 struct JitterBuffer *q; 263 struct JitterBuffer *q;
259 264
260 if (!(q = calloc(sizeof(struct JitterBuffer), 1))) return NULL; 265 if (!(q = calloc(sizeof(struct JitterBuffer), 1))) {
266 return NULL;
267 }
261 268
262 if (!(q->queue = calloc(sizeof(struct RTPMessage *), size))) { 269 if (!(q->queue = calloc(sizeof(struct RTPMessage *), size))) {
263 free(q); 270 free(q);
@@ -279,7 +286,9 @@ static void jbuf_clear(struct JitterBuffer *q)
279} 286}
280static void jbuf_free(struct JitterBuffer *q) 287static void jbuf_free(struct JitterBuffer *q)
281{ 288{
282 if (!q) return; 289 if (!q) {
290 return;
291 }
283 292
284 jbuf_clear(q); 293 jbuf_clear(q);
285 free(q->queue); 294 free(q->queue);
@@ -301,13 +310,15 @@ static int jbuf_write(Logger *log, struct JitterBuffer *q, struct RTPMessage *m)
301 return 0; 310 return 0;
302 } 311 }
303 312
304 if (q->queue[num]) 313 if (q->queue[num]) {
305 return -1; 314 return -1;
315 }
306 316
307 q->queue[num] = m; 317 q->queue[num] = m;
308 318
309 if ((sequnum - q->bottom) >= (q->top - q->bottom)) 319 if ((sequnum - q->bottom) >= (q->top - q->bottom)) {
310 q->top = sequnum + 1; 320 q->top = sequnum + 1;
321 }
311 322
312 return 0; 323 return 0;
313} 324}
@@ -394,14 +405,15 @@ bool reconfigure_audio_encoder(Logger *log, OpusEncoder **e, int32_t new_br, int
394 if (*old_sr != new_sr || *old_ch != new_ch) { 405 if (*old_sr != new_sr || *old_ch != new_ch) {
395 OpusEncoder *new_encoder = create_audio_encoder(log, new_br, new_sr, new_ch); 406 OpusEncoder *new_encoder = create_audio_encoder(log, new_br, new_sr, new_ch);
396 407
397 if (new_encoder == NULL) 408 if (new_encoder == NULL) {
398 return false; 409 return false;
410 }
399 411
400 opus_encoder_destroy(*e); 412 opus_encoder_destroy(*e);
401 *e = new_encoder; 413 *e = new_encoder;
402 } else if (*old_br == new_br) 414 } else if (*old_br == new_br) {
403 return true; /* Nothing changed */ 415 return true; /* Nothing changed */
404 else { 416 } else {
405 int status = opus_encoder_ctl(*e, OPUS_SET_BITRATE(new_br)); 417 int status = opus_encoder_ctl(*e, OPUS_SET_BITRATE(new_br));
406 418
407 if (status != OPUS_OK) { 419 if (status != OPUS_OK) {
@@ -420,8 +432,9 @@ bool reconfigure_audio_encoder(Logger *log, OpusEncoder **e, int32_t new_br, int
420bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels) 432bool reconfigure_audio_decoder(ACSession *ac, int32_t sampling_rate, int8_t channels)
421{ 433{
422 if (sampling_rate != ac->ld_sample_rate || channels != ac->ld_channel_count) { 434 if (sampling_rate != ac->ld_sample_rate || channels != ac->ld_channel_count) {
423 if (current_time_monotonic() - ac->ldrts < 500) 435 if (current_time_monotonic() - ac->ldrts < 500) {
424 return false; 436 return false;
437 }
425 438
426 int status; 439 int status;
427 OpusDecoder *new_dec = opus_decoder_create(sampling_rate, channels, &status); 440 OpusDecoder *new_dec = opus_decoder_create(sampling_rate, channels, &status);
diff --git a/toxav/bwcontroller.c b/toxav/bwcontroller.c
index cd574266..4d3b7f7d 100644
--- a/toxav/bwcontroller.c
+++ b/toxav/bwcontroller.c
@@ -78,8 +78,9 @@ BWController *bwc_new(Messenger *m, uint32_t friendnumber,
78 /* Fill with zeros */ 78 /* Fill with zeros */
79 int i = 0; 79 int i = 0;
80 80
81 for (; i < BWC_AVG_PKT_COUNT; i ++) 81 for (; i < BWC_AVG_PKT_COUNT; i ++) {
82 rb_write(retu->rcvpkt.rb, retu->rcvpkt.rb_s + i); 82 rb_write(retu->rcvpkt.rb, retu->rcvpkt.rb_s + i);
83 }
83 84
84 m_callback_rtp_packet(m, friendnumber, BWC_PACKET_ID, bwc_handle_data, retu); 85 m_callback_rtp_packet(m, friendnumber, BWC_PACKET_ID, bwc_handle_data, retu);
85 86
@@ -87,8 +88,9 @@ BWController *bwc_new(Messenger *m, uint32_t friendnumber,
87} 88}
88void bwc_kill(BWController *bwc) 89void bwc_kill(BWController *bwc)
89{ 90{
90 if (!bwc) 91 if (!bwc) {
91 return; 92 return;
93 }
92 94
93 m_callback_rtp_packet(bwc->m, bwc->friend_number, BWC_PACKET_ID, NULL, NULL); 95 m_callback_rtp_packet(bwc->m, bwc->friend_number, BWC_PACKET_ID, NULL, NULL);
94 96
@@ -106,8 +108,9 @@ void bwc_feed_avg(BWController *bwc, uint32_t bytes)
106} 108}
107void bwc_add_lost(BWController *bwc, uint32_t bytes) 109void bwc_add_lost(BWController *bwc, uint32_t bytes)
108{ 110{
109 if (!bwc) 111 if (!bwc) {
110 return; 112 return;
113 }
111 114
112 if (!bytes) { 115 if (!bytes) {
113 uint32_t *t_avg[BWC_AVG_PKT_COUNT], c = 1; 116 uint32_t *t_avg[BWC_AVG_PKT_COUNT], c = 1;
@@ -119,8 +122,9 @@ void bwc_add_lost(BWController *bwc, uint32_t bytes)
119 for (; i < BWC_AVG_PKT_COUNT; i ++) { 122 for (; i < BWC_AVG_PKT_COUNT; i ++) {
120 bytes += *(t_avg[i]); 123 bytes += *(t_avg[i]);
121 124
122 if (*(t_avg[i])) 125 if (*(t_avg[i])) {
123 c++; 126 c++;
127 }
124 } 128 }
125 129
126 bytes /= c; 130 bytes /= c;
@@ -131,8 +135,9 @@ void bwc_add_lost(BWController *bwc, uint32_t bytes)
131} 135}
132void bwc_add_recv(BWController *bwc, uint32_t bytes) 136void bwc_add_recv(BWController *bwc, uint32_t bytes)
133{ 137{
134 if (!bwc || !bytes) 138 if (!bwc || !bytes) {
135 return; 139 return;
140 }
136 141
137 bwc->cycle.recv += bytes; 142 bwc->cycle.recv += bytes;
138 send_update(bwc); 143 send_update(bwc);
@@ -164,8 +169,9 @@ void send_update(BWController *bwc)
164 b_msg->lost = htonl(bwc->cycle.lost); 169 b_msg->lost = htonl(bwc->cycle.lost);
165 b_msg->recv = htonl(bwc->cycle.recv); 170 b_msg->recv = htonl(bwc->cycle.recv);
166 171
167 if (-1 == send_custom_lossy_packet(bwc->m, bwc->friend_number, p_msg, sizeof(p_msg))) 172 if (-1 == send_custom_lossy_packet(bwc->m, bwc->friend_number, p_msg, sizeof(p_msg))) {
168 LOGGER_WARNING(bwc->m->log, "BWC send failed (len: %d)! std error: %s", sizeof(p_msg), strerror(errno)); 173 LOGGER_WARNING(bwc->m->log, "BWC send failed (len: %d)! std error: %s", sizeof(p_msg), strerror(errno));
174 }
169 } 175 }
170 176
171 bwc->cycle.lsu = current_time_monotonic(); 177 bwc->cycle.lsu = current_time_monotonic();
@@ -188,17 +194,19 @@ int on_update (BWController *bwc, struct BWCMessage *msg)
188 194
189 LOGGER_DEBUG(bwc->m->log, "recved: %u lost: %u", msg->recv, msg->lost); 195 LOGGER_DEBUG(bwc->m->log, "recved: %u lost: %u", msg->recv, msg->lost);
190 196
191 if (msg->lost && bwc->mcb) 197 if (msg->lost && bwc->mcb) {
192 bwc->mcb(bwc, bwc->friend_number, 198 bwc->mcb(bwc, bwc->friend_number,
193 ((float) (msg->lost) / (msg->recv + msg->lost)), 199 ((float) (msg->lost) / (msg->recv + msg->lost)),
194 bwc->mcb_data); 200 bwc->mcb_data);
201 }
195 202
196 return 0; 203 return 0;
197} 204}
198int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object) 205int bwc_handle_data(Messenger *m, uint32_t friendnumber, const uint8_t *data, uint16_t length, void *object)
199{ 206{
200 if (length - 1 != sizeof(struct BWCMessage)) 207 if (length - 1 != sizeof(struct BWCMessage)) {
201 return -1; 208 return -1;
209 }
202 210
203 /* NOTE the data is mutable */ 211 /* NOTE the data is mutable */
204 return on_update(object, (struct BWCMessage *) (data + 1)); 212 return on_update(object, (struct BWCMessage *) (data + 1));
diff --git a/toxav/group.c b/toxav/group.c
index 1d083444..88d9bd5a 100644
--- a/toxav/group.c
+++ b/toxav/group.c
@@ -54,7 +54,9 @@ static Group_JitterBuffer *create_queue(unsigned int capacity)
54 54
55 Group_JitterBuffer *q; 55 Group_JitterBuffer *q;
56 56
57 if (!(q = calloc(sizeof(Group_JitterBuffer), 1))) return NULL; 57 if (!(q = calloc(sizeof(Group_JitterBuffer), 1))) {
58 return NULL;
59 }
58 60
59 if (!(q->queue = calloc(sizeof(Group_Audio_Packet *), size))) { 61 if (!(q->queue = calloc(sizeof(Group_Audio_Packet *), size))) {
60 free(q); 62 free(q);
@@ -78,7 +80,9 @@ static void clear_queue(Group_JitterBuffer *q)
78 80
79static void terminate_queue(Group_JitterBuffer *q) 81static void terminate_queue(Group_JitterBuffer *q)
80{ 82{
81 if (!q) return; 83 if (!q) {
84 return;
85 }
82 86
83 clear_queue(q); 87 clear_queue(q);
84 free(q->queue); 88 free(q->queue);
@@ -109,13 +113,15 @@ static int queue(Group_JitterBuffer *q, Group_Audio_Packet *pk)
109 return 0; 113 return 0;
110 } 114 }
111 115
112 if (q->queue[num]) 116 if (q->queue[num]) {
113 return -1; 117 return -1;
118 }
114 119
115 q->queue[num] = pk; 120 q->queue[num] = pk;
116 121
117 if ((sequnum - q->bottom) >= (q->top - q->bottom)) 122 if ((sequnum - q->bottom) >= (q->top - q->bottom)) {
118 q->top = sequnum + 1; 123 q->top = sequnum + 1;
124 }
119 125
120 q->last_queued_time = unix_time(); 126 q->last_queued_time = unix_time();
121 return 0; 127 return 0;
@@ -222,13 +228,15 @@ static Group_AV *new_group_av(Logger *log, Group_Chats *g_c, void (*audio_callba
222 const int16_t *, 228 const int16_t *,
223 unsigned int, uint8_t, unsigned int, void *), void *userdata) 229 unsigned int, uint8_t, unsigned int, void *), void *userdata)
224{ 230{
225 if (!g_c) 231 if (!g_c) {
226 return NULL; 232 return NULL;
233 }
227 234
228 Group_AV *group_av = calloc(1, sizeof(Group_AV)); 235 Group_AV *group_av = calloc(1, sizeof(Group_AV));
229 236
230 if (!group_av) 237 if (!group_av) {
231 return NULL; 238 return NULL;
239 }
232 240
233 group_av->log = log; 241 group_av->log = log;
234 group_av->g_c = g_c; 242 group_av->g_c = g_c;
@@ -244,8 +252,9 @@ static void group_av_peer_new(void *object, int groupnumber, int friendgroupnumb
244 Group_AV *group_av = object; 252 Group_AV *group_av = object;
245 Group_Peer_AV *peer_av = calloc(1, sizeof(Group_Peer_AV)); 253 Group_Peer_AV *peer_av = calloc(1, sizeof(Group_Peer_AV));
246 254
247 if (!peer_av) 255 if (!peer_av) {
248 return; 256 return;
257 }
249 258
250 peer_av->buffer = create_queue(GROUP_JBUF_SIZE); 259 peer_av->buffer = create_queue(GROUP_JBUF_SIZE);
251 group_peer_set_object(group_av->g_c, groupnumber, friendgroupnumber, peer_av); 260 group_peer_set_object(group_av->g_c, groupnumber, friendgroupnumber, peer_av);
@@ -255,11 +264,13 @@ static void group_av_peer_delete(void *object, int groupnumber, int friendgroupn
255{ 264{
256 Group_Peer_AV *peer_av = peer_object; 265 Group_Peer_AV *peer_av = peer_object;
257 266
258 if (!peer_av) 267 if (!peer_av) {
259 return; 268 return;
269 }
260 270
261 if (peer_av->audio_decoder) 271 if (peer_av->audio_decoder) {
262 opus_decoder_destroy(peer_av->audio_decoder); 272 opus_decoder_destroy(peer_av->audio_decoder);
273 }
263 274
264 terminate_queue(peer_av->buffer); 275 terminate_queue(peer_av->buffer);
265 free(peer_object); 276 free(peer_object);
@@ -267,20 +278,23 @@ static void group_av_peer_delete(void *object, int groupnumber, int friendgroupn
267 278
268static void group_av_groupchat_delete(void *object, int groupnumber) 279static void group_av_groupchat_delete(void *object, int groupnumber)
269{ 280{
270 if (object) 281 if (object) {
271 kill_group_av(object); 282 kill_group_av(object);
283 }
272} 284}
273 285
274static int decode_audio_packet(Group_AV *group_av, Group_Peer_AV *peer_av, int groupnumber, int friendgroupnumber) 286static int decode_audio_packet(Group_AV *group_av, Group_Peer_AV *peer_av, int groupnumber, int friendgroupnumber)
275{ 287{
276 if (!group_av || !peer_av) 288 if (!group_av || !peer_av) {
277 return -1; 289 return -1;
290 }
278 291
279 int success; 292 int success;
280 Group_Audio_Packet *pk = dequeue(peer_av->buffer, &success); 293 Group_Audio_Packet *pk = dequeue(peer_av->buffer, &success);
281 294
282 if (success == 0) 295 if (success == 0) {
283 return -1; 296 return -1;
297 }
284 298
285 int16_t *out_audio = NULL; 299 int16_t *out_audio = NULL;
286 int out_audio_samples = 0; 300 int out_audio_samples = 0;
@@ -330,16 +344,19 @@ static int decode_audio_packet(Group_AV *group_av, Group_Peer_AV *peer_av, int g
330 out_audio_samples = opus_decode(peer_av->audio_decoder, pk->data, pk->length, out_audio, num_samples, 0); 344 out_audio_samples = opus_decode(peer_av->audio_decoder, pk->data, pk->length, out_audio, num_samples, 0);
331 free(pk); 345 free(pk);
332 346
333 if (out_audio_samples <= 0) 347 if (out_audio_samples <= 0) {
334 return -1; 348 return -1;
349 }
335 350
336 peer_av->last_packet_samples = out_audio_samples; 351 peer_av->last_packet_samples = out_audio_samples;
337 } else { 352 } else {
338 if (!peer_av->audio_decoder) 353 if (!peer_av->audio_decoder) {
339 return -1; 354 return -1;
355 }
340 356
341 if (!peer_av->last_packet_samples) 357 if (!peer_av->last_packet_samples) {
342 return -1; 358 return -1;
359 }
343 360
344 out_audio = malloc(peer_av->last_packet_samples * peer_av->decoder_channels * sizeof(int16_t)); 361 out_audio = malloc(peer_av->last_packet_samples * peer_av->decoder_channels * sizeof(int16_t));
345 362
@@ -350,16 +367,18 @@ static int decode_audio_packet(Group_AV *group_av, Group_Peer_AV *peer_av, int g
350 367
351 out_audio_samples = opus_decode(peer_av->audio_decoder, NULL, 0, out_audio, peer_av->last_packet_samples, 1); 368 out_audio_samples = opus_decode(peer_av->audio_decoder, NULL, 0, out_audio, peer_av->last_packet_samples, 1);
352 369
353 if (out_audio_samples <= 0) 370 if (out_audio_samples <= 0) {
354 return -1; 371 return -1;
372 }
355 373
356 } 374 }
357 375
358 if (out_audio) { 376 if (out_audio) {
359 377
360 if (group_av->audio_data) 378 if (group_av->audio_data) {
361 group_av->audio_data(group_av->g_c->m, groupnumber, friendgroupnumber, out_audio, out_audio_samples, 379 group_av->audio_data(group_av->g_c->m, groupnumber, friendgroupnumber, out_audio, out_audio_samples,
362 peer_av->decoder_channels, sample_rate, group_av->userdata); 380 peer_av->decoder_channels, sample_rate, group_av->userdata);
381 }
363 382
364 free(out_audio); 383 free(out_audio);
365 return 0; 384 return 0;
@@ -394,7 +413,9 @@ static int handle_group_audio_packet(void *object, int groupnumber, int friendgr
394 return -1; 413 return -1;
395 } 414 }
396 415
397 while (decode_audio_packet(object, peer_av, groupnumber, friendgroupnumber) == 0); 416 while (decode_audio_packet(object, peer_av, groupnumber, friendgroupnumber) == 0) {
417 ;
418 }
398 419
399 return 0; 420 return 0;
400} 421}
@@ -408,13 +429,15 @@ static int groupchat_enable_av(Logger *log, Group_Chats *g_c, int groupnumber, v
408 int, 429 int,
409 const int16_t *, unsigned int, uint8_t, unsigned int, void *), void *userdata) 430 const int16_t *, unsigned int, uint8_t, unsigned int, void *), void *userdata)
410{ 431{
411 if (groupnumber == -1) 432 if (groupnumber == -1) {
412 return -1; 433 return -1;
434 }
413 435
414 Group_AV *group_av = new_group_av(log, g_c, audio_callback, userdata); 436 Group_AV *group_av = new_group_av(log, g_c, audio_callback, userdata);
415 437
416 if (group_av == NULL) 438 if (group_av == NULL) {
417 return -1; 439 return -1;
440 }
418 441
419 if (group_set_object(g_c, groupnumber, group_av) == -1 442 if (group_set_object(g_c, groupnumber, group_av) == -1
420 || callback_groupchat_peer_new(g_c, groupnumber, group_av_peer_new) == -1 443 || callback_groupchat_peer_new(g_c, groupnumber, group_av_peer_new) == -1
@@ -481,8 +504,9 @@ int join_av_groupchat(Logger *log, Group_Chats *g_c, int32_t friendnumber, const
481 */ 504 */
482static int send_audio_packet(Group_Chats *g_c, int groupnumber, uint8_t *packet, uint16_t length) 505static int send_audio_packet(Group_Chats *g_c, int groupnumber, uint8_t *packet, uint16_t length)
483{ 506{
484 if (!length) 507 if (!length) {
485 return -1; 508 return -1;
509 }
486 510
487 Group_AV *group_av = group_get_object(g_c, groupnumber); 511 Group_AV *group_av = group_get_object(g_c, groupnumber);
488 uint8_t data[1 + sizeof(uint16_t) + length]; 512 uint8_t data[1 + sizeof(uint16_t) + length];
@@ -492,8 +516,9 @@ static int send_audio_packet(Group_Chats *g_c, int groupnumber, uint8_t *packet,
492 memcpy(data + 1, &sequnum, sizeof(sequnum)); 516 memcpy(data + 1, &sequnum, sizeof(sequnum));
493 memcpy(data + 1 + sizeof(sequnum), packet, length); 517 memcpy(data + 1 + sizeof(sequnum), packet, length);
494 518
495 if (send_group_lossy_packet(g_c, groupnumber, data, sizeof(data)) == -1) 519 if (send_group_lossy_packet(g_c, groupnumber, data, sizeof(data)) == -1) {
496 return -1; 520 return -1;
521 }
497 522
498 ++group_av->audio_sequnum; 523 ++group_av->audio_sequnum;
499 return 0; 524 return 0;
@@ -509,14 +534,18 @@ int group_send_audio(Group_Chats *g_c, int groupnumber, const int16_t *pcm, unsi
509{ 534{
510 Group_AV *group_av = group_get_object(g_c, groupnumber); 535 Group_AV *group_av = group_get_object(g_c, groupnumber);
511 536
512 if (!group_av) 537 if (!group_av) {
513 return -1; 538 return -1;
539 }
514 540
515 if (channels != 1 && channels != 2) 541 if (channels != 1 && channels != 2) {
516 return -1; 542 return -1;
543 }
517 544
518 if (sample_rate != 8000 && sample_rate != 12000 && sample_rate != 16000 && sample_rate != 24000 && sample_rate != 48000) 545 if (sample_rate != 8000 && sample_rate != 12000 && sample_rate != 16000 && sample_rate != 24000
546 && sample_rate != 48000) {
519 return -1; 547 return -1;
548 }
520 549
521 if (!group_av->audio_encoder || group_av->audio_channels != channels || group_av->audio_sample_rate != sample_rate) { 550 if (!group_av->audio_encoder || group_av->audio_channels != channels || group_av->audio_sample_rate != sample_rate) {
522 group_av->audio_channels = channels; 551 group_av->audio_channels = channels;
@@ -528,15 +557,17 @@ int group_send_audio(Group_Chats *g_c, int groupnumber, const int16_t *pcm, unsi
528 group_av->audio_bitrate = 64000; //TODO: add way of adjusting bitrate 557 group_av->audio_bitrate = 64000; //TODO: add way of adjusting bitrate
529 } 558 }
530 559
531 if (recreate_encoder(group_av) == -1) 560 if (recreate_encoder(group_av) == -1) {
532 return -1; 561 return -1;
562 }
533 } 563 }
534 564
535 uint8_t encoded[1024]; 565 uint8_t encoded[1024];
536 int32_t size = opus_encode(group_av->audio_encoder, pcm, samples, encoded, sizeof(encoded)); 566 int32_t size = opus_encode(group_av->audio_encoder, pcm, samples, encoded, sizeof(encoded));
537 567
538 if (size <= 0) 568 if (size <= 0) {
539 return -1; 569 return -1;
570 }
540 571
541 return send_audio_packet(g_c, groupnumber, encoded, size); 572 return send_audio_packet(g_c, groupnumber, encoded, size);
542} 573}
diff --git a/toxav/msi.c b/toxav/msi.c
index b98c58de..cdc3c879 100644
--- a/toxav/msi.c
+++ b/toxav/msi.c
@@ -97,8 +97,9 @@ void handle_msi_packet (Messenger *m, uint32_t friend_number, const uint8_t *dat
97 */ 97 */
98void msi_register_callback (MSISession *session, msi_action_cb *callback, MSICallbackID id) 98void msi_register_callback (MSISession *session, msi_action_cb *callback, MSICallbackID id)
99{ 99{
100 if (!session) 100 if (!session) {
101 return; 101 return;
102 }
102 103
103 pthread_mutex_lock(session->mutex); 104 pthread_mutex_lock(session->mutex);
104 session->callbacks[id] = callback; 105 session->callbacks[id] = callback;
@@ -170,8 +171,9 @@ int msi_kill (MSISession *session)
170} 171}
171int msi_invite (MSISession *session, MSICall **call, uint32_t friend_number, uint8_t capabilities) 172int msi_invite (MSISession *session, MSICall **call, uint32_t friend_number, uint8_t capabilities)
172{ 173{
173 if (!session) 174 if (!session) {
174 return -1; 175 return -1;
176 }
175 177
176 LOGGER_DEBUG(session->messenger->log, "Session: %p Inviting friend: %u", session, friend_number); 178 LOGGER_DEBUG(session->messenger->log, "Session: %p Inviting friend: %u", session, friend_number);
177 179
@@ -211,8 +213,9 @@ int msi_invite (MSISession *session, MSICall **call, uint32_t friend_number, uin
211} 213}
212int msi_hangup (MSICall *call) 214int msi_hangup (MSICall *call)
213{ 215{
214 if (!call || !call->session) 216 if (!call || !call->session) {
215 return -1; 217 return -1;
218 }
216 219
217 MSISession *session = call->session; 220 MSISession *session = call->session;
218 221
@@ -241,8 +244,9 @@ int msi_hangup (MSICall *call)
241} 244}
242int msi_answer (MSICall *call, uint8_t capabilities) 245int msi_answer (MSICall *call, uint8_t capabilities)
243{ 246{
244 if (!call || !call->session) 247 if (!call || !call->session) {
245 return -1; 248 return -1;
249 }
246 250
247 MSISession *session = call->session; 251 MSISession *session = call->session;
248 252
@@ -278,8 +282,9 @@ int msi_answer (MSICall *call, uint8_t capabilities)
278} 282}
279int msi_change_capabilities(MSICall *call, uint8_t capabilities) 283int msi_change_capabilities(MSICall *call, uint8_t capabilities)
280{ 284{
281 if (!call || !call->session) 285 if (!call || !call->session) {
282 return -1; 286 return -1;
287 }
283 288
284 MSISession *session = call->session; 289 MSISession *session = call->session;
285 290
@@ -495,8 +500,9 @@ FAILURE:
495 * an error message will be sent to friend 500 * an error message will be sent to friend
496 */ 501 */
497 502
498 if (call->error == msi_ENone) 503 if (call->error == msi_ENone) {
499 call->error = msi_EHandle; 504 call->error = msi_EHandle;
505 }
500 506
501 return -1; 507 return -1;
502} 508}
@@ -504,8 +510,9 @@ static MSICall *get_call (MSISession *session, uint32_t friend_number)
504{ 510{
505 assert(session); 511 assert(session);
506 512
507 if (session->calls == NULL || session->calls_tail < friend_number) 513 if (session->calls == NULL || session->calls_tail < friend_number) {
508 return NULL; 514 return NULL;
515 }
509 516
510 return session->calls[friend_number]; 517 return session->calls[friend_number];
511} 518}
@@ -515,8 +522,9 @@ MSICall *new_call (MSISession *session, uint32_t friend_number)
515 522
516 MSICall *rc = calloc(sizeof(MSICall), 1); 523 MSICall *rc = calloc(sizeof(MSICall), 1);
517 524
518 if (rc == NULL) 525 if (rc == NULL) {
519 return NULL; 526 return NULL;
527 }
520 528
521 rc->session = session; 529 rc->session = session;
522 rc->friend_number = friend_number; 530 rc->friend_number = friend_number;
@@ -544,8 +552,9 @@ MSICall *new_call (MSISession *session, uint32_t friend_number)
544 /* Set fields in between to null */ 552 /* Set fields in between to null */
545 uint32_t i = session->calls_tail + 1; 553 uint32_t i = session->calls_tail + 1;
546 554
547 for (; i < friend_number; i ++) 555 for (; i < friend_number; i ++) {
548 session->calls[i] = NULL; 556 session->calls[i] = NULL;
557 }
549 558
550 rc->prev = session->calls[session->calls_tail]; 559 rc->prev = session->calls[session->calls_tail];
551 session->calls[session->calls_tail]->next = rc; 560 session->calls[session->calls_tail]->next = rc;
@@ -564,8 +573,9 @@ MSICall *new_call (MSISession *session, uint32_t friend_number)
564void kill_call (MSICall *call) 573void kill_call (MSICall *call)
565{ 574{
566 /* Assume that session mutex is locked */ 575 /* Assume that session mutex is locked */
567 if (call == NULL) 576 if (call == NULL) {
568 return; 577 return;
578 }
569 579
570 MSISession *session = call->session; 580 MSISession *session = call->session;
571 581
@@ -574,17 +584,21 @@ void kill_call (MSICall *call)
574 MSICall *prev = call->prev; 584 MSICall *prev = call->prev;
575 MSICall *next = call->next; 585 MSICall *next = call->next;
576 586
577 if (prev) 587 if (prev) {
578 prev->next = next; 588 prev->next = next;
579 else if (next) 589 } else if (next) {
580 session->calls_head = next->friend_number; 590 session->calls_head = next->friend_number;
581 else goto CLEAR_CONTAINER; 591 } else {
592 goto CLEAR_CONTAINER;
593 }
582 594
583 if (next) 595 if (next) {
584 next->prev = prev; 596 next->prev = prev;
585 else if (prev) 597 } else if (prev) {
586 session->calls_tail = prev->friend_number; 598 session->calls_tail = prev->friend_number;
587 else goto CLEAR_CONTAINER; 599 } else {
600 goto CLEAR_CONTAINER;
601 }
588 602
589 session->calls[call->friend_number] = NULL; 603 session->calls[call->friend_number] = NULL;
590 free(call); 604 free(call);
@@ -641,8 +655,9 @@ void handle_init (MSICall *call, const MSIMessage *msg)
641 call->peer_capabilities = msg->capabilities.value; 655 call->peer_capabilities = msg->capabilities.value;
642 call->state = msi_CallRequested; 656 call->state = msi_CallRequested;
643 657
644 if (invoke_callback(call, msi_OnInvite) == -1) 658 if (invoke_callback(call, msi_OnInvite) == -1) {
645 goto FAILURE; 659 goto FAILURE;
660 }
646 } 661 }
647 break; 662 break;
648 663
@@ -704,8 +719,9 @@ void handle_push (MSICall *call, const MSIMessage *msg)
704 719
705 call->peer_capabilities = msg->capabilities.value; 720 call->peer_capabilities = msg->capabilities.value;
706 721
707 if (invoke_callback(call, msi_OnCapabilities) == -1) 722 if (invoke_callback(call, msi_OnCapabilities) == -1) {
708 goto FAILURE; 723 goto FAILURE;
724 }
709 } 725 }
710 } 726 }
711 break; 727 break;
@@ -717,8 +733,9 @@ void handle_push (MSICall *call, const MSIMessage *msg)
717 call->peer_capabilities = msg->capabilities.value; 733 call->peer_capabilities = msg->capabilities.value;
718 call->state = msi_CallActive; 734 call->state = msi_CallActive;
719 735
720 if (invoke_callback(call, msi_OnStart) == -1) 736 if (invoke_callback(call, msi_OnStart) == -1) {
721 goto FAILURE; 737 goto FAILURE;
738 }
722 739
723 } 740 }
724 break; 741 break;
@@ -751,7 +768,8 @@ void handle_pop (MSICall *call, const MSIMessage *msg)
751 call->error = msg->error.value; 768 call->error = msg->error.value;
752 invoke_callback(call, msi_OnError); 769 invoke_callback(call, msi_OnError);
753 770
754 } else switch (call->state) { 771 } else {
772 switch (call->state) {
755 case msi_CallInactive: { 773 case msi_CallInactive: {
756 LOGGER_ERROR(call->session->messenger->log, "Handling what should be impossible case"); 774 LOGGER_ERROR(call->session->messenger->log, "Handling what should be impossible case");
757 abort(); 775 abort();
@@ -779,6 +797,7 @@ void handle_pop (MSICall *call, const MSIMessage *msg)
779 } 797 }
780 break; 798 break;
781 } 799 }
800 }
782 801
783 kill_call (call); 802 kill_call (call);
784} 803}
diff --git a/toxav/rtp.c b/toxav/rtp.c
index 5fe37021..a226f3ff 100644
--- a/toxav/rtp.c
+++ b/toxav/rtp.c
@@ -73,8 +73,9 @@ RTPSession *rtp_new (int payload_type, Messenger *m, uint32_t friendnumber,
73} 73}
74void rtp_kill (RTPSession *session) 74void rtp_kill (RTPSession *session)
75{ 75{
76 if (!session) 76 if (!session) {
77 return; 77 return;
78 }
78 79
79 LOGGER_DEBUG(session->m->log, "Terminated RTP session: %p", session); 80 LOGGER_DEBUG(session->m->log, "Terminated RTP session: %p", session);
80 81
@@ -83,8 +84,9 @@ void rtp_kill (RTPSession *session)
83} 84}
84int rtp_allow_receiving(RTPSession *session) 85int rtp_allow_receiving(RTPSession *session)
85{ 86{
86 if (session == NULL) 87 if (session == NULL) {
87 return -1; 88 return -1;
89 }
88 90
89 if (m_callback_rtp_packet(session->m, session->friend_number, session->payload_type, 91 if (m_callback_rtp_packet(session->m, session->friend_number, session->payload_type,
90 handle_rtp_packet, session) == -1) { 92 handle_rtp_packet, session) == -1) {
@@ -97,8 +99,9 @@ int rtp_allow_receiving(RTPSession *session)
97} 99}
98int rtp_stop_receiving(RTPSession *session) 100int rtp_stop_receiving(RTPSession *session)
99{ 101{
100 if (session == NULL) 102 if (session == NULL) {
101 return -1; 103 return -1;
104 }
102 105
103 m_callback_rtp_packet(session->m, session->friend_number, session->payload_type, NULL, NULL); 106 m_callback_rtp_packet(session->m, session->friend_number, session->payload_type, NULL, NULL);
104 107
@@ -143,8 +146,9 @@ int rtp_send_data (RTPSession *session, const uint8_t *data, uint16_t length)
143 146
144 memcpy(rdata + 1 + sizeof(struct RTPHeader), data, length); 147 memcpy(rdata + 1 + sizeof(struct RTPHeader), data, length);
145 148
146 if (-1 == send_custom_lossy_packet(session->m, session->friend_number, rdata, sizeof(rdata))) 149 if (-1 == send_custom_lossy_packet(session->m, session->friend_number, rdata, sizeof(rdata))) {
147 LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s", sizeof(rdata), strerror(errno)); 150 LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s", sizeof(rdata), strerror(errno));
151 }
148 } else { 152 } else {
149 153
150 /** 154 /**
@@ -159,9 +163,10 @@ int rtp_send_data (RTPSession *session, const uint8_t *data, uint16_t length)
159 memcpy(rdata + 1 + sizeof(struct RTPHeader), data + sent, piece); 163 memcpy(rdata + 1 + sizeof(struct RTPHeader), data + sent, piece);
160 164
161 if (-1 == send_custom_lossy_packet(session->m, session->friend_number, 165 if (-1 == send_custom_lossy_packet(session->m, session->friend_number,
162 rdata, piece + sizeof(struct RTPHeader) + 1)) 166 rdata, piece + sizeof(struct RTPHeader) + 1)) {
163 LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s", 167 LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s",
164 piece + sizeof(struct RTPHeader) + 1, strerror(errno)); 168 piece + sizeof(struct RTPHeader) + 1, strerror(errno));
169 }
165 170
166 sent += piece; 171 sent += piece;
167 header->cpart = htons(sent); 172 header->cpart = htons(sent);
@@ -174,9 +179,10 @@ int rtp_send_data (RTPSession *session, const uint8_t *data, uint16_t length)
174 memcpy(rdata + 1 + sizeof(struct RTPHeader), data + sent, piece); 179 memcpy(rdata + 1 + sizeof(struct RTPHeader), data + sent, piece);
175 180
176 if (-1 == send_custom_lossy_packet(session->m, session->friend_number, rdata, 181 if (-1 == send_custom_lossy_packet(session->m, session->friend_number, rdata,
177 piece + sizeof(struct RTPHeader) + 1)) 182 piece + sizeof(struct RTPHeader) + 1)) {
178 LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s", 183 LOGGER_WARNING(session->m->log, "RTP send failed (len: %d)! std error: %s",
179 piece + sizeof(struct RTPHeader) + 1, strerror(errno)); 184 piece + sizeof(struct RTPHeader) + 1, strerror(errno));
185 }
180 } 186 }
181 } 187 }
182 188
@@ -198,8 +204,9 @@ bool chloss (const RTPSession *session, const struct RTPHeader *header)
198 204
199 fprintf (stderr, "Lost packet\n"); 205 fprintf (stderr, "Lost packet\n");
200 206
201 while (lost --) 207 while (lost --) {
202 bwc_add_lost(session->bwc , 0); 208 bwc_add_lost(session->bwc , 0);
209 }
203 210
204 return true; 211 return true;
205 } 212 }
@@ -271,10 +278,11 @@ int handle_rtp_packet (Messenger *m, uint32_t friendnumber, const uint8_t *data,
271 278
272 /* Invoke processing of active multiparted message */ 279 /* Invoke processing of active multiparted message */
273 if (session->mp) { 280 if (session->mp) {
274 if (session->mcb) 281 if (session->mcb) {
275 session->mcb (session->cs, session->mp); 282 session->mcb (session->cs, session->mp);
276 else 283 } else {
277 free(session->mp); 284 free(session->mp);
285 }
278 286
279 session->mp = NULL; 287 session->mp = NULL;
280 } 288 }
@@ -283,8 +291,9 @@ int handle_rtp_packet (Messenger *m, uint32_t friendnumber, const uint8_t *data,
283 * process it only if handler for the session is present. 291 * process it only if handler for the session is present.
284 */ 292 */
285 293
286 if (!session->mcb) 294 if (!session->mcb) {
287 return 0; 295 return 0;
296 }
288 297
289 return session->mcb (session->cs, new_message(length, data, length)); 298 return session->mcb (session->cs, new_message(length, data, length));
290 } else { 299 } else {
@@ -323,21 +332,23 @@ int handle_rtp_packet (Messenger *m, uint32_t friendnumber, const uint8_t *data,
323 /* Received a full message; now push it for the further 332 /* Received a full message; now push it for the further
324 * processing. 333 * processing.
325 */ 334 */
326 if (session->mcb) 335 if (session->mcb) {
327 session->mcb (session->cs, session->mp); 336 session->mcb (session->cs, session->mp);
328 else 337 } else {
329 free(session->mp); 338 free(session->mp);
339 }
330 340
331 session->mp = NULL; 341 session->mp = NULL;
332 } 342 }
333 } else { 343 } else {
334 /* Second case */ 344 /* Second case */
335 345
336 if (session->mp->header.timestamp > ntohl(header->timestamp)) 346 if (session->mp->header.timestamp > ntohl(header->timestamp)) {
337 /* The received message part is from the old message; 347 /* The received message part is from the old message;
338 * discard it. 348 * discard it.
339 */ 349 */
340 return 0; 350 return 0;
351 }
341 352
342 /* Measure missing parts of the old message */ 353 /* Measure missing parts of the old message */
343 bwc_add_lost(session->bwc, 354 bwc_add_lost(session->bwc,
@@ -348,10 +359,11 @@ int handle_rtp_packet (Messenger *m, uint32_t friendnumber, const uint8_t *data,
348 MAX_CRYPTO_DATA_SIZE) * sizeof(struct RTPHeader) ); 359 MAX_CRYPTO_DATA_SIZE) * sizeof(struct RTPHeader) );
349 360
350 /* Push the previous message for processing */ 361 /* Push the previous message for processing */
351 if (session->mcb) 362 if (session->mcb) {
352 session->mcb (session->cs, session->mp); 363 session->mcb (session->cs, session->mp);
353 else 364 } else {
354 free(session->mp); 365 free(session->mp);
366 }
355 367
356 session->mp = NULL; 368 session->mp = NULL;
357 goto NEW_MULTIPARTED; 369 goto NEW_MULTIPARTED;
@@ -382,8 +394,9 @@ NEW_MULTIPARTED:
382 session->mp = new_message(ntohs(header->tlen) + sizeof(struct RTPHeader), data, length); 394 session->mp = new_message(ntohs(header->tlen) + sizeof(struct RTPHeader), data, length);
383 395
384 /* Reposition data if necessary */ 396 /* Reposition data if necessary */
385 if (ntohs(header->cpart)) 397 if (ntohs(header->cpart)) {
386 ; 398 ;
399 }
387 400
388 memmove(session->mp->data + ntohs(header->cpart), session->mp->data, session->mp->len); 401 memmove(session->mp->data + ntohs(header->cpart), session->mp->data, session->mp->len);
389 } 402 }
diff --git a/toxav/toxav.c b/toxav/toxav.c
index 4ef5d2b3..d7b367f7 100644
--- a/toxav/toxav.c
+++ b/toxav/toxav.c
@@ -179,8 +179,9 @@ ToxAV *toxav_new(Tox *tox, TOXAV_ERR_NEW *error)
179 179
180END: 180END:
181 181
182 if (error) 182 if (error) {
183 *error = rc; 183 *error = rc;
184 }
184 185
185 if (rc != TOXAV_ERR_NEW_OK) { 186 if (rc != TOXAV_ERR_NEW_OK) {
186 free(av); 187 free(av);
@@ -191,8 +192,9 @@ END:
191} 192}
192void toxav_kill(ToxAV *av) 193void toxav_kill(ToxAV *av)
193{ 194{
194 if (av == NULL) 195 if (av == NULL) {
195 return; 196 return;
197 }
196 198
197 pthread_mutex_lock(av->mutex); 199 pthread_mutex_lock(av->mutex);
198 200
@@ -249,12 +251,14 @@ void toxav_iterate(ToxAV *av)
249 vc_iterate(i->video.second); 251 vc_iterate(i->video.second);
250 252
251 if (i->msi_call->self_capabilities & msi_CapRAudio && 253 if (i->msi_call->self_capabilities & msi_CapRAudio &&
252 i->msi_call->peer_capabilities & msi_CapSAudio) 254 i->msi_call->peer_capabilities & msi_CapSAudio) {
253 rc = MIN(i->audio.second->lp_frame_duration, rc); 255 rc = MIN(i->audio.second->lp_frame_duration, rc);
256 }
254 257
255 if (i->msi_call->self_capabilities & msi_CapRVideo && 258 if (i->msi_call->self_capabilities & msi_CapRVideo &&
256 i->msi_call->peer_capabilities & msi_CapSVideo) 259 i->msi_call->peer_capabilities & msi_CapSVideo) {
257 rc = MIN(i->video.second->lcfd, (uint32_t) rc); 260 rc = MIN(i->video.second->lcfd, (uint32_t) rc);
261 }
258 262
259 uint32_t fid = i->friend_number; 263 uint32_t fid = i->friend_number;
260 264
@@ -262,8 +266,9 @@ void toxav_iterate(ToxAV *av)
262 pthread_mutex_lock(av->mutex); 266 pthread_mutex_lock(av->mutex);
263 267
264 /* In case this call is popped from container stop iteration */ 268 /* In case this call is popped from container stop iteration */
265 if (call_get(av, fid) != i) 269 if (call_get(av, fid) != i) {
266 break; 270 break;
271 }
267 } 272 }
268 } 273 }
269 274
@@ -293,8 +298,9 @@ bool toxav_call(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, uint
293 298
294 ToxAVCall *call = call_new(av, friend_number, &rc); 299 ToxAVCall *call = call_new(av, friend_number, &rc);
295 300
296 if (call == NULL) 301 if (call == NULL) {
297 goto END; 302 goto END;
303 }
298 304
299 call->audio_bit_rate = audio_bit_rate; 305 call->audio_bit_rate = audio_bit_rate;
300 call->video_bit_rate = video_bit_rate; 306 call->video_bit_rate = video_bit_rate;
@@ -315,8 +321,9 @@ bool toxav_call(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, uint
315END: 321END:
316 pthread_mutex_unlock(av->mutex); 322 pthread_mutex_unlock(av->mutex);
317 323
318 if (error) 324 if (error) {
319 *error = rc; 325 *error = rc;
326 }
320 327
321 return rc == TOXAV_ERR_CALL_OK; 328 return rc == TOXAV_ERR_CALL_OK;
322} 329}
@@ -366,14 +373,16 @@ bool toxav_answer(ToxAV *av, uint32_t friend_number, uint32_t audio_bit_rate, ui
366 call->previous_self_capabilities |= audio_bit_rate > 0 ? msi_CapSAudio : 0; 373 call->previous_self_capabilities |= audio_bit_rate > 0 ? msi_CapSAudio : 0;
367 call->previous_self_capabilities |= video_bit_rate > 0 ? msi_CapSVideo : 0; 374 call->previous_self_capabilities |= video_bit_rate > 0 ? msi_CapSVideo : 0;
368 375
369 if (msi_answer(call->msi_call, call->previous_self_capabilities) != 0) 376 if (msi_answer(call->msi_call, call->previous_self_capabilities) != 0) {
370 rc = TOXAV_ERR_ANSWER_SYNC; 377 rc = TOXAV_ERR_ANSWER_SYNC;
378 }
371 379
372END: 380END:
373 pthread_mutex_unlock(av->mutex); 381 pthread_mutex_unlock(av->mutex);
374 382
375 if (error) 383 if (error) {
376 *error = rc; 384 *error = rc;
385 }
377 386
378 return rc == TOXAV_ERR_ANSWER_OK; 387 return rc == TOXAV_ERR_ANSWER_OK;
379} 388}
@@ -528,8 +537,9 @@ bool toxav_call_control(ToxAV *av, uint32_t friend_number, TOXAV_CALL_CONTROL co
528END: 537END:
529 pthread_mutex_unlock(av->mutex); 538 pthread_mutex_unlock(av->mutex);
530 539
531 if (error) 540 if (error) {
532 *error = rc; 541 *error = rc;
542 }
533 543
534 return rc == TOXAV_ERR_CALL_CONTROL_OK; 544 return rc == TOXAV_ERR_CALL_CONTROL_OK;
535} 545}
@@ -594,8 +604,9 @@ bool toxav_bit_rate_set(ToxAV *av, uint32_t friend_number, int32_t audio_bit_rat
594 rc = TOXAV_ERR_BIT_RATE_SET_SYNC; 604 rc = TOXAV_ERR_BIT_RATE_SET_SYNC;
595 goto END; 605 goto END;
596 } 606 }
597 } else 607 } else {
598 LOGGER_DEBUG(av->m->log, "Set new audio bit rate %d", audio_bit_rate); 608 LOGGER_DEBUG(av->m->log, "Set new audio bit rate %d", audio_bit_rate);
609 }
599 610
600 call->audio_bit_rate = audio_bit_rate; 611 call->audio_bit_rate = audio_bit_rate;
601 pthread_mutex_unlock(call->mutex); 612 pthread_mutex_unlock(call->mutex);
@@ -633,8 +644,9 @@ bool toxav_bit_rate_set(ToxAV *av, uint32_t friend_number, int32_t audio_bit_rat
633 rc = TOXAV_ERR_BIT_RATE_SET_SYNC; 644 rc = TOXAV_ERR_BIT_RATE_SET_SYNC;
634 goto END; 645 goto END;
635 } 646 }
636 } else 647 } else {
637 LOGGER_DEBUG(av->m->log, "Set new video bit rate %d", video_bit_rate); 648 LOGGER_DEBUG(av->m->log, "Set new video bit rate %d", video_bit_rate);
649 }
638 650
639 call->video_bit_rate = video_bit_rate; 651 call->video_bit_rate = video_bit_rate;
640 pthread_mutex_unlock(call->mutex); 652 pthread_mutex_unlock(call->mutex);
@@ -644,8 +656,9 @@ bool toxav_bit_rate_set(ToxAV *av, uint32_t friend_number, int32_t audio_bit_rat
644 pthread_mutex_unlock(av->mutex); 656 pthread_mutex_unlock(av->mutex);
645END: 657END:
646 658
647 if (error) 659 if (error) {
648 *error = rc; 660 *error = rc;
661 }
649 662
650 return rc == TOXAV_ERR_BIT_RATE_SET_OK; 663 return rc == TOXAV_ERR_BIT_RATE_SET_OK;
651} 664}
@@ -735,8 +748,9 @@ bool toxav_audio_send_frame(ToxAV *av, uint32_t friend_number, const int16_t *pc
735 748
736END: 749END:
737 750
738 if (error) 751 if (error) {
739 *error = rc; 752 *error = rc;
753 }
740 754
741 return rc == TOXAV_ERR_SEND_FRAME_OK; 755 return rc == TOXAV_ERR_SEND_FRAME_OK;
742} 756}
@@ -834,8 +848,9 @@ bool toxav_video_send_frame(ToxAV *av, uint32_t friend_number, uint16_t width, u
834 848
835END: 849END:
836 850
837 if (error) 851 if (error) {
838 *error = rc; 852 *error = rc;
853 }
839 854
840 return rc == TOXAV_ERR_SEND_FRAME_OK; 855 return rc == TOXAV_ERR_SEND_FRAME_OK;
841} 856}
@@ -874,8 +889,9 @@ void callback_bwc(BWController *bwc, uint32_t friend_number, float loss, void *u
874 889
875 LOGGER_DEBUG(call->av->m->log, "Reported loss of %f%%", loss * 100); 890 LOGGER_DEBUG(call->av->m->log, "Reported loss of %f%%", loss * 100);
876 891
877 if (loss < .01f) 892 if (loss < .01f) {
878 return; 893 return;
894 }
879 895
880 pthread_mutex_lock(call->av->mutex); 896 pthread_mutex_lock(call->av->mutex);
881 897
@@ -885,14 +901,15 @@ void callback_bwc(BWController *bwc, uint32_t friend_number, float loss, void *u
885 return; 901 return;
886 } 902 }
887 903
888 if (call->video_bit_rate) 904 if (call->video_bit_rate) {
889 (*call->av->bcb.first) (call->av, friend_number, call->audio_bit_rate, 905 (*call->av->bcb.first) (call->av, friend_number, call->audio_bit_rate,
890 call->video_bit_rate - (call->video_bit_rate * loss), 906 call->video_bit_rate - (call->video_bit_rate * loss),
891 call->av->bcb.second); 907 call->av->bcb.second);
892 else if (call->audio_bit_rate) 908 } else if (call->audio_bit_rate) {
893 (*call->av->bcb.first) (call->av, friend_number, 909 (*call->av->bcb.first) (call->av, friend_number,
894 call->audio_bit_rate - (call->audio_bit_rate * loss), 910 call->audio_bit_rate - (call->audio_bit_rate * loss),
895 0, call->av->bcb.second); 911 0, call->av->bcb.second);
912 }
896 913
897 pthread_mutex_unlock(call->av->mutex); 914 pthread_mutex_unlock(call->av->mutex);
898} 915}
@@ -912,10 +929,10 @@ int callback_invite(void *toxav_inst, MSICall *call)
912 call->av_call = av_call; 929 call->av_call = av_call;
913 av_call->msi_call = call; 930 av_call->msi_call = call;
914 931
915 if (toxav->ccb.first) 932 if (toxav->ccb.first) {
916 toxav->ccb.first(toxav, call->friend_number, call->peer_capabilities & msi_CapSAudio, 933 toxav->ccb.first(toxav, call->friend_number, call->peer_capabilities & msi_CapSAudio,
917 call->peer_capabilities & msi_CapSVideo, toxav->ccb.second); 934 call->peer_capabilities & msi_CapSVideo, toxav->ccb.second);
918 else { 935 } else {
919 /* No handler to capture the call request, send failure */ 936 /* No handler to capture the call request, send failure */
920 pthread_mutex_unlock(toxav->mutex); 937 pthread_mutex_unlock(toxav->mutex);
921 return -1; 938 return -1;
@@ -987,15 +1004,17 @@ int callback_capabilites(void *toxav_inst, MSICall *call)
987 ToxAV *toxav = toxav_inst; 1004 ToxAV *toxav = toxav_inst;
988 pthread_mutex_lock(toxav->mutex); 1005 pthread_mutex_lock(toxav->mutex);
989 1006
990 if (call->peer_capabilities & msi_CapSAudio) 1007 if (call->peer_capabilities & msi_CapSAudio) {
991 rtp_allow_receiving(((ToxAVCall *)call->av_call)->audio.first); 1008 rtp_allow_receiving(((ToxAVCall *)call->av_call)->audio.first);
992 else 1009 } else {
993 rtp_stop_receiving(((ToxAVCall *)call->av_call)->audio.first); 1010 rtp_stop_receiving(((ToxAVCall *)call->av_call)->audio.first);
1011 }
994 1012
995 if (call->peer_capabilities & msi_CapSVideo) 1013 if (call->peer_capabilities & msi_CapSVideo) {
996 rtp_allow_receiving(((ToxAVCall *)call->av_call)->video.first); 1014 rtp_allow_receiving(((ToxAVCall *)call->av_call)->video.first);
997 else 1015 } else {
998 rtp_stop_receiving(((ToxAVCall *)call->av_call)->video.first); 1016 rtp_stop_receiving(((ToxAVCall *)call->av_call)->video.first);
1017 }
999 1018
1000 invoke_call_state_callback(toxav, call->friend_number, call->peer_capabilities); 1019 invoke_call_state_callback(toxav, call->friend_number, call->peer_capabilities);
1001 1020
@@ -1017,10 +1036,11 @@ bool video_bit_rate_invalid(uint32_t bit_rate)
1017} 1036}
1018bool invoke_call_state_callback(ToxAV *av, uint32_t friend_number, uint32_t state) 1037bool invoke_call_state_callback(ToxAV *av, uint32_t friend_number, uint32_t state)
1019{ 1038{
1020 if (av->scb.first) 1039 if (av->scb.first) {
1021 av->scb.first(av, friend_number, state, av->scb.second); 1040 av->scb.first(av, friend_number, state, av->scb.second);
1022 else 1041 } else {
1023 return false; 1042 return false;
1043 }
1024 1044
1025 return true; 1045 return true;
1026} 1046}
@@ -1083,8 +1103,9 @@ ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, TOXAV_ERR_CALL *error)
1083 /* Set fields in between to null */ 1103 /* Set fields in between to null */
1084 uint32_t i = av->calls_tail + 1; 1104 uint32_t i = av->calls_tail + 1;
1085 1105
1086 for (; i < friend_number; i ++) 1106 for (; i < friend_number; i ++) {
1087 av->calls[i] = NULL; 1107 av->calls[i] = NULL;
1108 }
1088 1109
1089 call->prev = av->calls[av->calls_tail]; 1110 call->prev = av->calls[av->calls_tail];
1090 av->calls[av->calls_tail]->next = call; 1111 av->calls[av->calls_tail]->next = call;
@@ -1101,23 +1122,26 @@ ToxAVCall *call_new(ToxAV *av, uint32_t friend_number, TOXAV_ERR_CALL *error)
1101 1122
1102END: 1123END:
1103 1124
1104 if (error) 1125 if (error) {
1105 *error = rc; 1126 *error = rc;
1127 }
1106 1128
1107 return call; 1129 return call;
1108} 1130}
1109ToxAVCall *call_get(ToxAV *av, uint32_t friend_number) 1131ToxAVCall *call_get(ToxAV *av, uint32_t friend_number)
1110{ 1132{
1111 /* Assumes mutex locked */ 1133 /* Assumes mutex locked */
1112 if (av->calls == NULL || av->calls_tail < friend_number) 1134 if (av->calls == NULL || av->calls_tail < friend_number) {
1113 return NULL; 1135 return NULL;
1136 }
1114 1137
1115 return av->calls[friend_number]; 1138 return av->calls[friend_number];
1116} 1139}
1117ToxAVCall *call_remove(ToxAVCall *call) 1140ToxAVCall *call_remove(ToxAVCall *call)
1118{ 1141{
1119 if (call == NULL) 1142 if (call == NULL) {
1120 return NULL; 1143 return NULL;
1144 }
1121 1145
1122 uint32_t friend_number = call->friend_number; 1146 uint32_t friend_number = call->friend_number;
1123 ToxAV *av = call->av; 1147 ToxAV *av = call->av;
@@ -1134,17 +1158,21 @@ ToxAVCall *call_remove(ToxAVCall *call)
1134 1158
1135 free(call); 1159 free(call);
1136 1160
1137 if (prev) 1161 if (prev) {
1138 prev->next = next; 1162 prev->next = next;
1139 else if (next) 1163 } else if (next) {
1140 av->calls_head = next->friend_number; 1164 av->calls_head = next->friend_number;
1141 else goto CLEAR; 1165 } else {
1166 goto CLEAR;
1167 }
1142 1168
1143 if (next) 1169 if (next) {
1144 next->prev = prev; 1170 next->prev = prev;
1145 else if (prev) 1171 } else if (prev) {
1146 av->calls_tail = prev->friend_number; 1172 av->calls_tail = prev->friend_number;
1147 else goto CLEAR; 1173 } else {
1174 goto CLEAR;
1175 }
1148 1176
1149 av->calls[friend_number] = NULL; 1177 av->calls[friend_number] = NULL;
1150 return next; 1178 return next;
@@ -1160,28 +1188,33 @@ bool call_prepare_transmission(ToxAVCall *call)
1160{ 1188{
1161 /* Assumes mutex locked */ 1189 /* Assumes mutex locked */
1162 1190
1163 if (call == NULL) 1191 if (call == NULL) {
1164 return false; 1192 return false;
1193 }
1165 1194
1166 ToxAV *av = call->av; 1195 ToxAV *av = call->av;
1167 1196
1168 if (!av->acb.first && !av->vcb.first) 1197 if (!av->acb.first && !av->vcb.first) {
1169 /* It makes no sense to have CSession without callbacks */ 1198 /* It makes no sense to have CSession without callbacks */
1170 return false; 1199 return false;
1200 }
1171 1201
1172 if (call->active) { 1202 if (call->active) {
1173 LOGGER_WARNING(av->m->log, "Call already active!\n"); 1203 LOGGER_WARNING(av->m->log, "Call already active!\n");
1174 return true; 1204 return true;
1175 } 1205 }
1176 1206
1177 if (create_recursive_mutex(call->mutex_audio) != 0) 1207 if (create_recursive_mutex(call->mutex_audio) != 0) {
1178 return false; 1208 return false;
1209 }
1179 1210
1180 if (create_recursive_mutex(call->mutex_video) != 0) 1211 if (create_recursive_mutex(call->mutex_video) != 0) {
1181 goto FAILURE_3; 1212 goto FAILURE_3;
1213 }
1182 1214
1183 if (create_recursive_mutex(call->mutex) != 0) 1215 if (create_recursive_mutex(call->mutex) != 0) {
1184 goto FAILURE_2; 1216 goto FAILURE_2;
1217 }
1185 1218
1186 /* Prepare bwc */ 1219 /* Prepare bwc */
1187 call->bwc = bwc_new(av->m, call->friend_number, callback_bwc, call); 1220 call->bwc = bwc_new(av->m, call->friend_number, callback_bwc, call);
@@ -1241,8 +1274,9 @@ FAILURE_3:
1241} 1274}
1242void call_kill_transmission(ToxAVCall *call) 1275void call_kill_transmission(ToxAVCall *call)
1243{ 1276{
1244 if (call == NULL || call->active == 0) 1277 if (call == NULL || call->active == 0) {
1245 return; 1278 return;
1279 }
1246 1280
1247 call->active = 0; 1281 call->active = 0;
1248 1282
diff --git a/toxav/video.c b/toxav/video.c
index 300eb377..c94579a6 100644
--- a/toxav/video.c
+++ b/toxav/video.c
@@ -51,8 +51,9 @@ VCSession *vc_new(Logger *log, ToxAV *av, uint32_t friend_number, toxav_video_re
51 return NULL; 51 return NULL;
52 } 52 }
53 53
54 if (!(vc->vbuf_raw = rb_new(VIDEO_DECODE_BUFFER_SIZE))) 54 if (!(vc->vbuf_raw = rb_new(VIDEO_DECODE_BUFFER_SIZE))) {
55 goto BASE_CLEANUP; 55 goto BASE_CLEANUP;
56 }
56 57
57 int rc = vpx_codec_dec_init(vc->decoder, VIDEO_CODEC_DECODER_INTERFACE, NULL, 0); 58 int rc = vpx_codec_dec_init(vc->decoder, VIDEO_CODEC_DECODER_INTERFACE, NULL, 0);
58 59
@@ -118,16 +119,18 @@ BASE_CLEANUP:
118} 119}
119void vc_kill(VCSession *vc) 120void vc_kill(VCSession *vc)
120{ 121{
121 if (!vc) 122 if (!vc) {
122 return; 123 return;
124 }
123 125
124 vpx_codec_destroy(vc->encoder); 126 vpx_codec_destroy(vc->encoder);
125 vpx_codec_destroy(vc->decoder); 127 vpx_codec_destroy(vc->decoder);
126 128
127 void *p; 129 void *p;
128 130
129 while (rb_read(vc->vbuf_raw, (void **)&p)) 131 while (rb_read(vc->vbuf_raw, (void **)&p)) {
130 free(p); 132 free(p);
133 }
131 134
132 rb_kill(vc->vbuf_raw); 135 rb_kill(vc->vbuf_raw);
133 136
@@ -138,8 +141,9 @@ void vc_kill(VCSession *vc)
138} 141}
139void vc_iterate(VCSession *vc) 142void vc_iterate(VCSession *vc)
140{ 143{
141 if (!vc) 144 if (!vc) {
142 return; 145 return;
146 }
143 147
144 struct RTPMessage *p; 148 struct RTPMessage *p;
145 int rc; 149 int rc;
@@ -152,18 +156,19 @@ void vc_iterate(VCSession *vc)
152 rc = vpx_codec_decode(vc->decoder, p->data, p->len, NULL, MAX_DECODE_TIME_US); 156 rc = vpx_codec_decode(vc->decoder, p->data, p->len, NULL, MAX_DECODE_TIME_US);
153 free(p); 157 free(p);
154 158
155 if (rc != VPX_CODEC_OK) 159 if (rc != VPX_CODEC_OK) {
156 LOGGER_ERROR(vc->log, "Error decoding video: %s", vpx_codec_err_to_string(rc)); 160 LOGGER_ERROR(vc->log, "Error decoding video: %s", vpx_codec_err_to_string(rc));
157 else { 161 } else {
158 vpx_codec_iter_t iter = NULL; 162 vpx_codec_iter_t iter = NULL;
159 vpx_image_t *dest = vpx_codec_get_frame(vc->decoder, &iter); 163 vpx_image_t *dest = vpx_codec_get_frame(vc->decoder, &iter);
160 164
161 /* Play decoded images */ 165 /* Play decoded images */
162 for (; dest; dest = vpx_codec_get_frame(vc->decoder, &iter)) { 166 for (; dest; dest = vpx_codec_get_frame(vc->decoder, &iter)) {
163 if (vc->vcb.first) 167 if (vc->vcb.first) {
164 vc->vcb.first(vc->av, vc->friend_number, dest->d_w, dest->d_h, 168 vc->vcb.first(vc->av, vc->friend_number, dest->d_w, dest->d_h,
165 (const uint8_t *)dest->planes[0], (const uint8_t *)dest->planes[1], (const uint8_t *)dest->planes[2], 169 (const uint8_t *)dest->planes[0], (const uint8_t *)dest->planes[1], (const uint8_t *)dest->planes[2],
166 dest->stride[0], dest->stride[1], dest->stride[2], vc->vcb.second); 170 dest->stride[0], dest->stride[1], dest->stride[2], vc->vcb.second);
171 }
167 172
168 vpx_img_free(dest); 173 vpx_img_free(dest);
169 } 174 }
@@ -179,8 +184,9 @@ int vc_queue_message(void *vcp, struct RTPMessage *msg)
179 /* This function does the reconstruction of video packets. 184 /* This function does the reconstruction of video packets.
180 * See more info about video splitting in docs 185 * See more info about video splitting in docs
181 */ 186 */
182 if (!vcp || !msg) 187 if (!vcp || !msg) {
183 return -1; 188 return -1;
189 }
184 190
185 VCSession *vc = vcp; 191 VCSession *vc = vcp;
186 192
@@ -210,14 +216,16 @@ int vc_queue_message(void *vcp, struct RTPMessage *msg)
210} 216}
211int vc_reconfigure_encoder(VCSession *vc, uint32_t bit_rate, uint16_t width, uint16_t height) 217int vc_reconfigure_encoder(VCSession *vc, uint32_t bit_rate, uint16_t width, uint16_t height)
212{ 218{
213 if (!vc) 219 if (!vc) {
214 return -1; 220 return -1;
221 }
215 222
216 vpx_codec_enc_cfg_t cfg = *vc->encoder->config.enc; 223 vpx_codec_enc_cfg_t cfg = *vc->encoder->config.enc;
217 int rc; 224 int rc;
218 225
219 if (cfg.rc_target_bitrate == bit_rate && cfg.g_w == width && cfg.g_h == height) 226 if (cfg.rc_target_bitrate == bit_rate && cfg.g_w == width && cfg.g_h == height) {
220 return 0; /* Nothing changed */ 227 return 0; /* Nothing changed */
228 }
221 229
222 if (cfg.g_w == width && cfg.g_h == height) { 230 if (cfg.g_w == width && cfg.g_h == height) {
223 /* Only bit rate changed */ 231 /* Only bit rate changed */