summaryrefslogtreecommitdiff
path: root/toxav
diff options
context:
space:
mode:
Diffstat (limited to 'toxav')
-rw-r--r--toxav/codec.c9
-rw-r--r--toxav/msi.c166
-rw-r--r--toxav/msi.h4
-rw-r--r--toxav/rtp.c12
-rw-r--r--toxav/rtp.h2
-rw-r--r--toxav/toxav.c139
6 files changed, 177 insertions, 155 deletions
diff --git a/toxav/codec.c b/toxav/codec.c
index 3e6de803..4abb7f8c 100644
--- a/toxav/codec.c
+++ b/toxav/codec.c
@@ -80,8 +80,12 @@ JitterBuffer *create_queue(int capacity)
80 80
81void terminate_queue(JitterBuffer *q) 81void terminate_queue(JitterBuffer *q)
82{ 82{
83 if (!q) return;
84
83 empty_queue(q); 85 empty_queue(q);
84 free(q->queue); 86 free(q->queue);
87
88 LOGGER_DEBUG("Terminated jitter buffer: %p", q);
85 free(q); 89 free(q);
86} 90}
87 91
@@ -314,6 +318,8 @@ CodecState *codec_init_session ( uint32_t audio_bitrate,
314 318
315void codec_terminate_session ( CodecState *cs ) 319void codec_terminate_session ( CodecState *cs )
316{ 320{
321 if (!cs) return;
322
317 if ( cs->audio_encoder ) 323 if ( cs->audio_encoder )
318 opus_encoder_destroy(cs->audio_encoder); 324 opus_encoder_destroy(cs->audio_encoder);
319 325
@@ -325,6 +331,9 @@ void codec_terminate_session ( CodecState *cs )
325 331
326 if ( cs->capabilities & v_encoding ) 332 if ( cs->capabilities & v_encoding )
327 vpx_codec_destroy(&cs->v_encoder); 333 vpx_codec_destroy(&cs->v_encoder);
334
335 LOGGER_DEBUG("Terminated codec state: %p", cs);
336 free(cs);
328} 337}
329 338
330inline float calculate_sum_sq (int16_t *n, uint16_t k) 339inline float calculate_sum_sq (int16_t *n, uint16_t k)
diff --git a/toxav/msi.c b/toxav/msi.c
index 3a0577ae..71a138d2 100644
--- a/toxav/msi.c
+++ b/toxav/msi.c
@@ -1043,12 +1043,12 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
1043 return NULL; 1043 return NULL;
1044 } 1044 }
1045 1045
1046 int32_t _call_idx = 0; 1046 int32_t call_idx = 0;
1047 1047
1048 for (; _call_idx < session->max_calls; _call_idx ++) { 1048 for (; call_idx < session->max_calls; call_idx ++) {
1049 if ( !session->calls[_call_idx] ) { 1049 if ( !session->calls[call_idx] ) {
1050 1050
1051 if (!(session->calls[_call_idx] = calloc ( sizeof ( MSICall ), 1 ))) { 1051 if (!(session->calls[call_idx] = calloc ( sizeof ( MSICall ), 1 ))) {
1052 LOGGER_WARNING("Allocation failed! Program might misbehave!"); 1052 LOGGER_WARNING("Allocation failed! Program might misbehave!");
1053 return NULL; 1053 return NULL;
1054 } 1054 }
@@ -1057,35 +1057,35 @@ MSICall *init_call ( MSISession *session, int peers, int ringing_timeout )
1057 } 1057 }
1058 } 1058 }
1059 1059
1060 if ( _call_idx == session->max_calls ) { 1060 if ( call_idx == session->max_calls ) {
1061 LOGGER_WARNING("Reached maximum amount of calls!"); 1061 LOGGER_WARNING("Reached maximum amount of calls!");
1062 return NULL; 1062 return NULL;
1063 } 1063 }
1064 1064
1065 1065
1066 MSICall *_call = session->calls[_call_idx]; 1066 MSICall *call = session->calls[call_idx];
1067 1067
1068 _call->call_idx = _call_idx; 1068 call->call_idx = call_idx;
1069 1069
1070 if ( !(_call->type_peer = calloc ( sizeof ( MSICallType ), peers )) ) { 1070 if ( !(call->type_peer = calloc ( sizeof ( MSICallType ), peers )) ) {
1071 LOGGER_WARNING("Allocation failed! Program might misbehave!"); 1071 LOGGER_WARNING("Allocation failed! Program might misbehave!");
1072 free(_call); 1072 free(call);
1073 return NULL; 1073 return NULL;
1074 } 1074 }
1075 1075
1076 _call->session = session; 1076 call->session = session;
1077 1077
1078 /*_call->_participant_count = _peers;*/ 1078 /*_call->_participant_count = _peers;*/
1079 1079
1080 _call->request_timer_id = 0; 1080 call->request_timer_id = 0;
1081 _call->ringing_timer_id = 0; 1081 call->ringing_timer_id = 0;
1082 1082
1083 _call->ringing_tout_ms = ringing_timeout; 1083 call->ringing_tout_ms = ringing_timeout;
1084 1084
1085 pthread_mutex_init ( &_call->mutex, NULL ); 1085 pthread_mutex_init ( call->mutex, NULL );
1086 1086
1087 LOGGER_DEBUG("Started new call with index: %u", _call_idx); 1087 LOGGER_DEBUG("Started new call with index: %u", call_idx);
1088 return _call; 1088 return call;
1089} 1089}
1090 1090
1091 1091
@@ -1104,7 +1104,7 @@ int terminate_call ( MSISession *session, MSICall *call )
1104 return -1; 1104 return -1;
1105 } 1105 }
1106 1106
1107 int rc = pthread_mutex_trylock(&session->mutex); /* Lock if not locked */ 1107 int rc = pthread_mutex_trylock(session->mutex); /* Lock if not locked */
1108 1108
1109 LOGGER_DEBUG("Terminated call id: %d", call->call_idx); 1109 LOGGER_DEBUG("Terminated call id: %d", call->call_idx);
1110 /* Check event loop and cancel timed events if there are any 1110 /* Check event loop and cancel timed events if there are any
@@ -1115,7 +1115,7 @@ int terminate_call ( MSISession *session, MSICall *call )
1115 timer_release ( session->timer_handler, call->ringing_timer_id ); 1115 timer_release ( session->timer_handler, call->ringing_timer_id );
1116 1116
1117 /* Get a handle */ 1117 /* Get a handle */
1118 pthread_mutex_lock ( &call->mutex ); 1118 pthread_mutex_lock ( call->mutex );
1119 1119
1120 session->calls[call->call_idx] = NULL; 1120 session->calls[call->call_idx] = NULL;
1121 1121
@@ -1123,14 +1123,14 @@ int terminate_call ( MSISession *session, MSICall *call )
1123 free ( call->peers); 1123 free ( call->peers);
1124 1124
1125 /* Release handle */ 1125 /* Release handle */
1126 pthread_mutex_unlock ( &call->mutex ); 1126 pthread_mutex_unlock ( call->mutex );
1127 1127
1128 pthread_mutex_destroy ( &call->mutex ); 1128 pthread_mutex_destroy ( call->mutex );
1129 1129
1130 free ( call ); 1130 free ( call );
1131 1131
1132 if ( rc != EBUSY ) /* Unlock if locked by this call */ 1132 if ( rc != EBUSY ) /* Unlock if locked by this call */
1133 pthread_mutex_unlock(&session->mutex); 1133 pthread_mutex_unlock(session->mutex);
1134 1134
1135 return 0; 1135 return 0;
1136} 1136}
@@ -1174,7 +1174,7 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
1174{ 1174{
1175 LOGGER_DEBUG("Session: %p Handling 'invite' on call: %s", session, call ? (char *)call->id : "making new"); 1175 LOGGER_DEBUG("Session: %p Handling 'invite' on call: %s", session, call ? (char *)call->id : "making new");
1176 1176
1177 pthread_mutex_lock(&session->mutex); 1177 pthread_mutex_lock(session->mutex);
1178 1178
1179 1179
1180 if ( call ) { 1180 if ( call ) {
@@ -1194,27 +1194,27 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
1194 call = init_call ( session, 1, 0 ); 1194 call = init_call ( session, 1, 0 );
1195 1195
1196 if ( !call ) { 1196 if ( !call ) {
1197 pthread_mutex_unlock(&session->mutex); 1197 pthread_mutex_unlock(session->mutex);
1198 LOGGER_ERROR("Starting call"); 1198 LOGGER_ERROR("Starting call");
1199 return 0; 1199 return 0;
1200 } 1200 }
1201 1201
1202 } else { 1202 } else {
1203 pthread_mutex_unlock(&session->mutex); 1203 pthread_mutex_unlock(session->mutex);
1204 return 0; /* Wait for ringing from peer */ 1204 return 0; /* Wait for ringing from peer */
1205 } 1205 }
1206 1206
1207 } else { 1207 } else {
1208 send_error ( session, call, error_busy, msg->friend_id ); /* TODO: Ugh*/ 1208 send_error ( session, call, error_busy, msg->friend_id ); /* TODO: Ugh*/
1209 terminate_call(session, call); 1209 terminate_call(session, call);
1210 pthread_mutex_unlock(&session->mutex); 1210 pthread_mutex_unlock(session->mutex);
1211 return 0; 1211 return 0;
1212 } 1212 }
1213 } else { 1213 } else {
1214 call = init_call ( session, 1, 0 ); 1214 call = init_call ( session, 1, 0 );
1215 1215
1216 if ( !call ) { 1216 if ( !call ) {
1217 pthread_mutex_unlock(&session->mutex); 1217 pthread_mutex_unlock(session->mutex);
1218 LOGGER_ERROR("Starting call"); 1218 LOGGER_ERROR("Starting call");
1219 return 0; 1219 return 0;
1220 } 1220 }
@@ -1223,7 +1223,7 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
1223 if ( !msg->callid.header_value ) { 1223 if ( !msg->callid.header_value ) {
1224 send_error ( session, call, error_no_callid, msg->friend_id ); 1224 send_error ( session, call, error_no_callid, msg->friend_id );
1225 terminate_call(session, call); 1225 terminate_call(session, call);
1226 pthread_mutex_unlock(&session->mutex); 1226 pthread_mutex_unlock(session->mutex);
1227 return 0; 1227 return 0;
1228 } 1228 }
1229 1229
@@ -1238,7 +1238,7 @@ int handle_recv_invite ( MSISession *session, MSICall *call, MSIMessage *msg )
1238 send_message ( session, call, _msg_ringing, msg->friend_id ); 1238 send_message ( session, call, _msg_ringing, msg->friend_id );
1239 free_message ( _msg_ringing ); 1239 free_message ( _msg_ringing );
1240 1240
1241 pthread_mutex_unlock(&session->mutex); 1241 pthread_mutex_unlock(session->mutex);
1242 1242
1243 invoke_callback(call->call_idx, MSI_OnInvite); 1243 invoke_callback(call->call_idx, MSI_OnInvite);
1244 1244
@@ -1253,13 +1253,13 @@ int handle_recv_start ( MSISession *session, MSICall *call, MSIMessage *msg )
1253 1253
1254 LOGGER_DEBUG("Session: %p Handling 'start' on call: %s, friend id: %d", session, call->id, msg->friend_id ); 1254 LOGGER_DEBUG("Session: %p Handling 'start' on call: %s, friend id: %d", session, call->id, msg->friend_id );
1255 1255
1256 pthread_mutex_lock(&session->mutex); 1256 pthread_mutex_lock(session->mutex);
1257 1257
1258 call->state = call_active; 1258 call->state = call_active;
1259 1259
1260 flush_peer_type ( call, msg, 0 ); 1260 flush_peer_type ( call, msg, 0 );
1261 1261
1262 pthread_mutex_unlock(&session->mutex); 1262 pthread_mutex_unlock(session->mutex);
1263 1263
1264 invoke_callback(call->call_idx, MSI_OnStart); 1264 invoke_callback(call->call_idx, MSI_OnStart);
1265 return 1; 1265 return 1;
@@ -1273,14 +1273,14 @@ int handle_recv_reject ( MSISession *session, MSICall *call, MSIMessage *msg )
1273 1273
1274 LOGGER_DEBUG("Session: %p Handling 'reject' on call: %s", session, call->id); 1274 LOGGER_DEBUG("Session: %p Handling 'reject' on call: %s", session, call->id);
1275 1275
1276 pthread_mutex_lock(&session->mutex); 1276 pthread_mutex_lock(session->mutex);
1277 1277
1278 MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) ); 1278 MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
1279 send_message ( session, call, _msg_ending, msg->friend_id ); 1279 send_message ( session, call, _msg_ending, msg->friend_id );
1280 free_message ( _msg_ending ); 1280 free_message ( _msg_ending );
1281 1281
1282 1282
1283 pthread_mutex_unlock(&session->mutex); 1283 pthread_mutex_unlock(session->mutex);
1284 1284
1285 invoke_callback(call->call_idx, MSI_OnReject); 1285 invoke_callback(call->call_idx, MSI_OnReject);
1286 1286
@@ -1296,11 +1296,11 @@ int handle_recv_cancel ( MSISession *session, MSICall *call, MSIMessage *msg )
1296 1296
1297 LOGGER_DEBUG("Session: %p Handling 'cancel' on call: %s", session, call->id ); 1297 LOGGER_DEBUG("Session: %p Handling 'cancel' on call: %s", session, call->id );
1298 1298
1299 pthread_mutex_lock(&session->mutex); 1299 pthread_mutex_lock(session->mutex);
1300 1300
1301 /* Act as end message */ 1301 /* Act as end message */
1302 1302
1303 pthread_mutex_unlock(&session->mutex); 1303 pthread_mutex_unlock(session->mutex);
1304 invoke_callback(call->call_idx, MSI_OnCancel); 1304 invoke_callback(call->call_idx, MSI_OnCancel);
1305 1305
1306 terminate_call ( session, call ); 1306 terminate_call ( session, call );
@@ -1315,13 +1315,13 @@ int handle_recv_end ( MSISession *session, MSICall *call, MSIMessage *msg )
1315 1315
1316 LOGGER_DEBUG("Session: %p Handling 'end' on call: %s", session, call->id ); 1316 LOGGER_DEBUG("Session: %p Handling 'end' on call: %s", session, call->id );
1317 1317
1318 pthread_mutex_lock(&session->mutex); 1318 pthread_mutex_lock(session->mutex);
1319 1319
1320 MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) ); 1320 MSIMessage *_msg_ending = msi_new_message ( TYPE_RESPONSE, stringify_response ( ending ) );
1321 send_message ( session, call, _msg_ending, msg->friend_id ); 1321 send_message ( session, call, _msg_ending, msg->friend_id );
1322 free_message ( _msg_ending ); 1322 free_message ( _msg_ending );
1323 1323
1324 pthread_mutex_unlock(&session->mutex); 1324 pthread_mutex_unlock(session->mutex);
1325 1325
1326 invoke_callback(call->call_idx, MSI_OnEnd); 1326 invoke_callback(call->call_idx, MSI_OnEnd);
1327 1327
@@ -1337,11 +1337,11 @@ int handle_recv_ringing ( MSISession *session, MSICall *call, MSIMessage *msg )
1337 return 0; 1337 return 0;
1338 } 1338 }
1339 1339
1340 pthread_mutex_lock(&session->mutex); 1340 pthread_mutex_lock(session->mutex);
1341 1341
1342 if ( call->ringing_timer_id ) { 1342 if ( call->ringing_timer_id ) {
1343 LOGGER_WARNING("Call already ringing"); 1343 LOGGER_WARNING("Call already ringing");
1344 pthread_mutex_unlock(&session->mutex); 1344 pthread_mutex_unlock(session->mutex);
1345 return 0; 1345 return 0;
1346 } 1346 }
1347 1347
@@ -1349,7 +1349,7 @@ int handle_recv_ringing ( MSISession *session, MSICall *call, MSIMessage *msg )
1349 1349
1350 call->ringing_timer_id = timer_alloc ( session->timer_handler, handle_timeout, call, call->ringing_tout_ms ); 1350 call->ringing_timer_id = timer_alloc ( session->timer_handler, handle_timeout, call, call->ringing_tout_ms );
1351 1351
1352 pthread_mutex_unlock(&session->mutex); 1352 pthread_mutex_unlock(session->mutex);
1353 1353
1354 invoke_callback(call->call_idx, MSI_OnRinging); 1354 invoke_callback(call->call_idx, MSI_OnRinging);
1355 return 1; 1355 return 1;
@@ -1361,7 +1361,7 @@ int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
1361 return 0; 1361 return 0;
1362 } 1362 }
1363 1363
1364 pthread_mutex_lock(&session->mutex); 1364 pthread_mutex_lock(session->mutex);
1365 1365
1366 LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id ); 1366 LOGGER_DEBUG("Session: %p Handling 'starting' on call: %s", session, call->id );
1367 1367
@@ -1375,7 +1375,7 @@ int handle_recv_starting ( MSISession *session, MSICall *call, MSIMessage *msg )
1375 1375
1376 1376
1377 timer_release ( session->timer_handler, call->ringing_timer_id ); 1377 timer_release ( session->timer_handler, call->ringing_timer_id );
1378 pthread_mutex_unlock(&session->mutex); 1378 pthread_mutex_unlock(session->mutex);
1379 1379
1380 invoke_callback(call->call_idx, MSI_OnStarting); 1380 invoke_callback(call->call_idx, MSI_OnStarting);
1381 return 1; 1381 return 1;
@@ -1387,14 +1387,14 @@ int handle_recv_ending ( MSISession *session, MSICall *call, MSIMessage *msg )
1387 return 0; 1387 return 0;
1388 } 1388 }
1389 1389
1390 pthread_mutex_lock(&session->mutex); 1390 pthread_mutex_lock(session->mutex);
1391 1391
1392 LOGGER_DEBUG("Session: %p Handling 'ending' on call: %s", session, call->id ); 1392 LOGGER_DEBUG("Session: %p Handling 'ending' on call: %s", session, call->id );
1393 1393
1394 /* Stop timer */ 1394 /* Stop timer */
1395 timer_release ( session->timer_handler, call->request_timer_id ); 1395 timer_release ( session->timer_handler, call->request_timer_id );
1396 1396
1397 pthread_mutex_unlock(&session->mutex); 1397 pthread_mutex_unlock(session->mutex);
1398 1398
1399 invoke_callback(call->call_idx, MSI_OnEnding); 1399 invoke_callback(call->call_idx, MSI_OnEnding);
1400 1400
@@ -1405,11 +1405,11 @@ int handle_recv_ending ( MSISession *session, MSICall *call, MSIMessage *msg )
1405} 1405}
1406int handle_recv_error ( MSISession *session, MSICall *call, MSIMessage *msg ) 1406int handle_recv_error ( MSISession *session, MSICall *call, MSIMessage *msg )
1407{ 1407{
1408 pthread_mutex_lock(&session->mutex); 1408 pthread_mutex_lock(session->mutex);
1409 1409
1410 if ( !call ) { 1410 if ( !call ) {
1411 LOGGER_WARNING("Handling 'error' on non-existing call!"); 1411 LOGGER_WARNING("Handling 'error' on non-existing call!");
1412 pthread_mutex_unlock(&session->mutex); 1412 pthread_mutex_unlock(session->mutex);
1413 return -1; 1413 return -1;
1414 } 1414 }
1415 1415
@@ -1422,7 +1422,7 @@ int handle_recv_error ( MSISession *session, MSICall *call, MSIMessage *msg )
1422 LOGGER_DEBUG("Error reason: %s", session->last_error_str); 1422 LOGGER_DEBUG("Error reason: %s", session->last_error_str);
1423 } 1423 }
1424 1424
1425 pthread_mutex_unlock(&session->mutex); 1425 pthread_mutex_unlock(session->mutex);
1426 1426
1427 invoke_callback(call->call_idx, MSI_OnEnding); 1427 invoke_callback(call->call_idx, MSI_OnEnding);
1428 1428
@@ -1603,38 +1603,38 @@ MSISession *msi_init_session ( Messenger *messenger, int32_t max_calls )
1603 return NULL; 1603 return NULL;
1604 } 1604 }
1605 1605
1606 MSISession *_retu = calloc ( sizeof ( MSISession ), 1 ); 1606 MSISession *retu = calloc ( sizeof ( MSISession ), 1 );
1607 1607
1608 if (_retu == NULL) { 1608 if (retu == NULL) {
1609 LOGGER_ERROR("Allocation failed! Program might misbehave!"); 1609 LOGGER_ERROR("Allocation failed! Program might misbehave!");
1610 return NULL; 1610 return NULL;
1611 } 1611 }
1612 1612
1613 _retu->messenger_handle = messenger; 1613 retu->messenger_handle = messenger;
1614 _retu->agent_handler = NULL; 1614 retu->agent_handler = NULL;
1615 _retu->timer_handler = handler; 1615 retu->timer_handler = handler;
1616 1616
1617 if (!(_retu->calls = calloc( sizeof (MSICall *), max_calls ))) { 1617 if (!(retu->calls = calloc( sizeof (MSICall *), max_calls ))) {
1618 LOGGER_ERROR("Allocation failed! Program might misbehave!"); 1618 LOGGER_ERROR("Allocation failed! Program might misbehave!");
1619 free(_retu); 1619 free(retu);
1620 return NULL; 1620 return NULL;
1621 } 1621 }
1622 1622
1623 _retu->max_calls = max_calls; 1623 retu->max_calls = max_calls;
1624 1624
1625 _retu->frequ = 10000; /* default value? */ 1625 retu->frequ = 10000; /* default value? */
1626 _retu->call_timeout = 30000; /* default value? */ 1626 retu->call_timeout = 30000; /* default value? */
1627 1627
1628 1628
1629 m_callback_msi_packet(messenger, msi_handle_packet, _retu ); 1629 m_callback_msi_packet(messenger, msi_handle_packet, retu );
1630 1630
1631 /* This is called when remote terminates session */ 1631 /* This is called when remote terminates session */
1632 m_callback_connectionstatus_internal_av(messenger, handle_remote_connection_change, _retu); 1632 m_callback_connectionstatus_internal_av(messenger, handle_remote_connection_change, retu);
1633 1633
1634 pthread_mutex_init(&_retu->mutex, NULL); 1634 pthread_mutex_init(retu->mutex, NULL);
1635 1635
1636 LOGGER_DEBUG("New msi session: %p max calls: %u", _retu, max_calls); 1636 LOGGER_DEBUG("New msi session: %p max calls: %u", retu, max_calls);
1637 return _retu; 1637 return retu;
1638} 1638}
1639 1639
1640 1640
@@ -1651,9 +1651,9 @@ int msi_terminate_session ( MSISession *session )
1651 return -1; 1651 return -1;
1652 } 1652 }
1653 1653
1654 pthread_mutex_lock(&session->mutex); 1654 pthread_mutex_lock(session->mutex);
1655 m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL); 1655 m_callback_msi_packet((struct Messenger *) session->messenger_handle, NULL, NULL);
1656 pthread_mutex_unlock(&session->mutex); 1656 pthread_mutex_unlock(session->mutex);
1657 1657
1658 int _status = 0; 1658 int _status = 0;
1659 1659
@@ -1671,7 +1671,7 @@ int msi_terminate_session ( MSISession *session )
1671 1671
1672 timer_terminate_session(session->timer_handler); 1672 timer_terminate_session(session->timer_handler);
1673 1673
1674 pthread_mutex_destroy(&session->mutex); 1674 pthread_mutex_destroy(session->mutex);
1675 1675
1676 LOGGER_DEBUG("Terminated session: %p", session); 1676 LOGGER_DEBUG("Terminated session: %p", session);
1677 free ( session ); 1677 free ( session );
@@ -1690,7 +1690,7 @@ int msi_terminate_session ( MSISession *session )
1690 */ 1690 */
1691int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type, uint32_t rngsec, uint32_t friend_id ) 1691int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type, uint32_t rngsec, uint32_t friend_id )
1692{ 1692{
1693 pthread_mutex_lock(&session->mutex); 1693 pthread_mutex_lock(session->mutex);
1694 1694
1695 LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id); 1695 LOGGER_DEBUG("Session: %p Inviting friend: %u", session, friend_id);
1696 1696
@@ -1698,7 +1698,7 @@ int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type
1698 MSICall *_call = init_call ( session, 1, rngsec ); /* Just one peer for now */ 1698 MSICall *_call = init_call ( session, 1, rngsec ); /* Just one peer for now */
1699 1699
1700 if ( !_call ) { 1700 if ( !_call ) {
1701 pthread_mutex_unlock(&session->mutex); 1701 pthread_mutex_unlock(session->mutex);
1702 LOGGER_ERROR("Cannot handle more calls"); 1702 LOGGER_ERROR("Cannot handle more calls");
1703 return -1; 1703 return -1;
1704 } 1704 }
@@ -1729,7 +1729,7 @@ int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type
1729 1729
1730 LOGGER_DEBUG("Invite sent"); 1730 LOGGER_DEBUG("Invite sent");
1731 1731
1732 pthread_mutex_unlock(&session->mutex); 1732 pthread_mutex_unlock(session->mutex);
1733 1733
1734 return 0; 1734 return 0;
1735} 1735}
@@ -1746,18 +1746,18 @@ int msi_invite ( MSISession *session, int32_t *call_index, MSICallType call_type
1746 */ 1746 */
1747int msi_hangup ( MSISession *session, int32_t call_index ) 1747int msi_hangup ( MSISession *session, int32_t call_index )
1748{ 1748{
1749 pthread_mutex_lock(&session->mutex); 1749 pthread_mutex_lock(session->mutex);
1750 LOGGER_DEBUG("Session: %p Hanging up call: %u", session, call_index); 1750 LOGGER_DEBUG("Session: %p Hanging up call: %u", session, call_index);
1751 1751
1752 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) { 1752 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
1753 LOGGER_ERROR("Invalid call index!"); 1753 LOGGER_ERROR("Invalid call index!");
1754 pthread_mutex_unlock(&session->mutex); 1754 pthread_mutex_unlock(session->mutex);
1755 return -1; 1755 return -1;
1756 } 1756 }
1757 1757
1758 if ( !session->calls[call_index] || session->calls[call_index]->state != call_active ) { 1758 if ( !session->calls[call_index] || session->calls[call_index]->state != call_active ) {
1759 LOGGER_ERROR("No call with such index or call is not active!"); 1759 LOGGER_ERROR("No call with such index or call is not active!");
1760 pthread_mutex_unlock(&session->mutex); 1760 pthread_mutex_unlock(session->mutex);
1761 return -1; 1761 return -1;
1762 } 1762 }
1763 1763
@@ -1776,7 +1776,7 @@ int msi_hangup ( MSISession *session, int32_t call_index )
1776 session->calls[call_index]->request_timer_id = 1776 session->calls[call_index]->request_timer_id =
1777 timer_alloc ( session->timer_handler, handle_timeout, session->calls[call_index], m_deftout ); 1777 timer_alloc ( session->timer_handler, handle_timeout, session->calls[call_index], m_deftout );
1778 1778
1779 pthread_mutex_unlock(&session->mutex); 1779 pthread_mutex_unlock(session->mutex);
1780 return 0; 1780 return 0;
1781} 1781}
1782 1782
@@ -1791,12 +1791,12 @@ int msi_hangup ( MSISession *session, int32_t call_index )
1791 */ 1791 */
1792int msi_answer ( MSISession *session, int32_t call_index, MSICallType call_type ) 1792int msi_answer ( MSISession *session, int32_t call_index, MSICallType call_type )
1793{ 1793{
1794 pthread_mutex_lock(&session->mutex); 1794 pthread_mutex_lock(session->mutex);
1795 LOGGER_DEBUG("Session: %p Answering call: %u", session, call_index); 1795 LOGGER_DEBUG("Session: %p Answering call: %u", session, call_index);
1796 1796
1797 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) { 1797 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
1798 LOGGER_ERROR("Invalid call index!"); 1798 LOGGER_ERROR("Invalid call index!");
1799 pthread_mutex_unlock(&session->mutex); 1799 pthread_mutex_unlock(session->mutex);
1800 return -1; 1800 return -1;
1801 } 1801 }
1802 1802
@@ -1818,7 +1818,7 @@ int msi_answer ( MSISession *session, int32_t call_index, MSICallType call_type
1818 1818
1819 session->calls[call_index]->state = call_active; 1819 session->calls[call_index]->state = call_active;
1820 1820
1821 pthread_mutex_unlock(&session->mutex); 1821 pthread_mutex_unlock(session->mutex);
1822 return 0; 1822 return 0;
1823} 1823}
1824 1824
@@ -1833,12 +1833,12 @@ int msi_answer ( MSISession *session, int32_t call_index, MSICallType call_type
1833 */ 1833 */
1834int msi_cancel ( MSISession *session, int32_t call_index, uint32_t peer, const char *reason ) 1834int msi_cancel ( MSISession *session, int32_t call_index, uint32_t peer, const char *reason )
1835{ 1835{
1836 pthread_mutex_lock(&session->mutex); 1836 pthread_mutex_lock(session->mutex);
1837 LOGGER_DEBUG("Session: %p Canceling call: %u; reason:", session, call_index, reason ? reason : "Unknown"); 1837 LOGGER_DEBUG("Session: %p Canceling call: %u; reason:", session, call_index, reason ? reason : "Unknown");
1838 1838
1839 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) { 1839 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
1840 LOGGER_ERROR("Invalid call index!"); 1840 LOGGER_ERROR("Invalid call index!");
1841 pthread_mutex_unlock(&session->mutex); 1841 pthread_mutex_unlock(session->mutex);
1842 return -1; 1842 return -1;
1843 } 1843 }
1844 1844
@@ -1852,7 +1852,7 @@ int msi_cancel ( MSISession *session, int32_t call_index, uint32_t peer, const c
1852 /*session->calls[call_index]->state = call_hanged_up; 1852 /*session->calls[call_index]->state = call_hanged_up;
1853 session->calls[call_index]->request_timer_id = timer_alloc ( handle_timeout, session->calls[call_index], m_deftout );*/ 1853 session->calls[call_index]->request_timer_id = timer_alloc ( handle_timeout, session->calls[call_index], m_deftout );*/
1854 terminate_call ( session, session->calls[call_index] ); 1854 terminate_call ( session, session->calls[call_index] );
1855 pthread_mutex_unlock(&session->mutex); 1855 pthread_mutex_unlock(session->mutex);
1856 1856
1857 return 0; 1857 return 0;
1858} 1858}
@@ -1867,12 +1867,12 @@ int msi_cancel ( MSISession *session, int32_t call_index, uint32_t peer, const c
1867 */ 1867 */
1868int msi_reject ( MSISession *session, int32_t call_index, const uint8_t *reason ) 1868int msi_reject ( MSISession *session, int32_t call_index, const uint8_t *reason )
1869{ 1869{
1870 pthread_mutex_lock(&session->mutex); 1870 pthread_mutex_lock(session->mutex);
1871 LOGGER_DEBUG("Session: %p Rejecting call: %u; reason:", session, call_index, reason ? (char *)reason : "Unknown"); 1871 LOGGER_DEBUG("Session: %p Rejecting call: %u; reason:", session, call_index, reason ? (char *)reason : "Unknown");
1872 1872
1873 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) { 1873 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
1874 LOGGER_ERROR("Invalid call index!"); 1874 LOGGER_ERROR("Invalid call index!");
1875 pthread_mutex_unlock(&session->mutex); 1875 pthread_mutex_unlock(session->mutex);
1876 return -1; 1876 return -1;
1877 } 1877 }
1878 1878
@@ -1889,7 +1889,7 @@ int msi_reject ( MSISession *session, int32_t call_index, const uint8_t *reason
1889 session->calls[call_index]->request_timer_id = 1889 session->calls[call_index]->request_timer_id =
1890 timer_alloc ( session->timer_handler, handle_timeout, session->calls[call_index], m_deftout ); 1890 timer_alloc ( session->timer_handler, handle_timeout, session->calls[call_index], m_deftout );
1891 1891
1892 pthread_mutex_unlock(&session->mutex); 1892 pthread_mutex_unlock(session->mutex);
1893 return 0; 1893 return 0;
1894} 1894}
1895 1895
@@ -1903,11 +1903,11 @@ int msi_reject ( MSISession *session, int32_t call_index, const uint8_t *reason
1903 */ 1903 */
1904int msi_stopcall ( MSISession *session, int32_t call_index ) 1904int msi_stopcall ( MSISession *session, int32_t call_index )
1905{ 1905{
1906 pthread_mutex_lock(&session->mutex); 1906 pthread_mutex_lock(session->mutex);
1907 LOGGER_DEBUG("Session: %p Stopping call index: %u", session, call_index); 1907 LOGGER_DEBUG("Session: %p Stopping call index: %u", session, call_index);
1908 1908
1909 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) { 1909 if ( call_index < 0 || call_index >= session->max_calls || !session->calls[call_index] ) {
1910 pthread_mutex_unlock(&session->mutex); 1910 pthread_mutex_unlock(session->mutex);
1911 return -1; 1911 return -1;
1912 } 1912 }
1913 1913
@@ -1915,6 +1915,6 @@ int msi_stopcall ( MSISession *session, int32_t call_index )
1915 1915
1916 terminate_call ( session, session->calls[call_index] ); 1916 terminate_call ( session, session->calls[call_index] );
1917 1917
1918 pthread_mutex_unlock(&session->mutex); 1918 pthread_mutex_unlock(session->mutex);
1919 return 0; 1919 return 0;
1920} 1920}
diff --git a/toxav/msi.h b/toxav/msi.h
index 0020df4c..428f7d9c 100644
--- a/toxav/msi.h
+++ b/toxav/msi.h
@@ -77,7 +77,7 @@ typedef struct _MSICall { /* Call info structure */
77 int ringing_timer_id; /* Timer id for ringing timeout */ 77 int ringing_timer_id; /* Timer id for ringing timeout */
78 78
79 79
80 pthread_mutex_t mutex; /* */ 80 pthread_mutex_t mutex[1]; /* */
81 uint32_t *peers; 81 uint32_t *peers;
82 uint16_t peer_count; 82 uint16_t peer_count;
83 83
@@ -104,7 +104,7 @@ typedef struct _MSISession {
104 uint32_t frequ; 104 uint32_t frequ;
105 uint32_t call_timeout; /* Time of the timeout for some action to end; 0 if infinite */ 105 uint32_t call_timeout; /* Time of the timeout for some action to end; 0 if infinite */
106 106
107 pthread_mutex_t mutex; 107 pthread_mutex_t mutex[1];
108 108
109 void *timer_handler; 109 void *timer_handler;
110} MSISession; 110} MSISession;
diff --git a/toxav/rtp.c b/toxav/rtp.c
index 9ba3b6a3..39e622bf 100644
--- a/toxav/rtp.c
+++ b/toxav/rtp.c
@@ -727,12 +727,9 @@ RTPSession *rtp_init_session ( int payload_type, Messenger *messenger, int frien
727 * @retval -1 Error occurred. 727 * @retval -1 Error occurred.
728 * @retval 0 Success. 728 * @retval 0 Success.
729 */ 729 */
730int rtp_terminate_session ( RTPSession *session, Messenger *messenger ) 730void rtp_terminate_session ( RTPSession *session, Messenger *messenger )
731{ 731{
732 if ( !session ) { 732 if ( !session ) return;
733 LOGGER_WARNING("No session!");
734 return -1;
735 }
736 733
737 custom_lossy_packet_registerhandler(messenger, session->dest, session->prefix, NULL, NULL); 734 custom_lossy_packet_registerhandler(messenger, session->dest, session->prefix, NULL, NULL);
738 735
@@ -747,8 +744,9 @@ int rtp_terminate_session ( RTPSession *session, Messenger *messenger )
747 744
748 pthread_mutex_destroy(&session->mutex); 745 pthread_mutex_destroy(&session->mutex);
749 746
747 LOGGER_DEBUG("Terminated RTP session: %p", session);
748
750 /* And finally free session */ 749 /* And finally free session */
751 free ( session ); 750 free ( session );
752 751
753 return 0;
754} \ No newline at end of file 752} \ No newline at end of file
diff --git a/toxav/rtp.h b/toxav/rtp.h
index 63c8ce3d..727839c6 100644
--- a/toxav/rtp.h
+++ b/toxav/rtp.h
@@ -195,7 +195,7 @@ RTPSession *rtp_init_session ( int payload_type, Messenger *messenger, int frien
195 * @retval -1 Error occurred. 195 * @retval -1 Error occurred.
196 * @retval 0 Success. 196 * @retval 0 Success.
197 */ 197 */
198int rtp_terminate_session ( RTPSession *session, Messenger *messenger ); 198void rtp_terminate_session ( RTPSession *session, Messenger *messenger );
199 199
200 200
201 201
diff --git a/toxav/toxav.c b/toxav/toxav.c
index 665ab254..894c12e4 100644
--- a/toxav/toxav.c
+++ b/toxav/toxav.c
@@ -68,6 +68,7 @@ typedef struct _CallSpecific {
68 void *frame_buf; /* buffer for split video payloads */ 68 void *frame_buf; /* buffer for split video payloads */
69 69
70 _Bool call_active; 70 _Bool call_active;
71 pthread_mutex_t mutex[1];
71} CallSpecific; 72} CallSpecific;
72 73
73 74
@@ -299,7 +300,7 @@ int toxav_stop_call ( ToxAv *av, int32_t call_index )
299int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettings *codec_settings, int support_video ) 300int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettings *codec_settings, int support_video )
300{ 301{
301 if ( !av->msi_session || cii(call_index, av->msi_session) || 302 if ( !av->msi_session || cii(call_index, av->msi_session) ||
302 !av->msi_session->calls[call_index] || av->calls[call_index].call_active) { 303 !av->msi_session->calls[call_index] || av->calls[call_index].call_active) {
303 LOGGER_ERROR("Error while starting RTP session: invalid call!\n"); 304 LOGGER_ERROR("Error while starting RTP session: invalid call!\n");
304 return ErrorInternal; 305 return ErrorInternal;
305 } 306 }
@@ -312,7 +313,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
312 313
313 if ( !call->crtps[audio_index] ) { 314 if ( !call->crtps[audio_index] ) {
314 LOGGER_ERROR("Error while starting audio RTP session!\n"); 315 LOGGER_ERROR("Error while starting audio RTP session!\n");
315 return ErrorStartingAudioRtp; 316 return ErrorInternal;
316 } 317 }
317 318
318 319
@@ -323,9 +324,7 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
323 324
324 if ( !call->crtps[video_index] ) { 325 if ( !call->crtps[video_index] ) {
325 LOGGER_ERROR("Error while starting video RTP session!\n"); 326 LOGGER_ERROR("Error while starting video RTP session!\n");
326 327 goto error;
327 rtp_terminate_session(call->crtps[audio_index], av->messenger);
328 return ErrorStartingVideoRtp;
329 } 328 }
330 329
331 call->frame_limit = 0; 330 call->frame_limit = 0;
@@ -335,20 +334,15 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
335 call->frame_buf = calloc(MAX_VIDEOFRAME_SIZE, 1); 334 call->frame_buf = calloc(MAX_VIDEOFRAME_SIZE, 1);
336 335
337 if (!call->frame_buf) { 336 if (!call->frame_buf) {
338 rtp_terminate_session(call->crtps[audio_index], av->messenger);
339 rtp_terminate_session(call->crtps[video_index], av->messenger);
340 LOGGER_WARNING("Frame buffer allocation failed!"); 337 LOGGER_WARNING("Frame buffer allocation failed!");
341 return ErrorInternal; 338 goto error;
342 } 339 }
343 340
344 } 341 }
345 342
346 if ( !(call->j_buf = create_queue(codec_settings->jbuf_capacity)) ) { 343 if ( !(call->j_buf = create_queue(codec_settings->jbuf_capacity)) ) {
347 rtp_terminate_session(call->crtps[audio_index], av->messenger);
348 rtp_terminate_session(call->crtps[video_index], av->messenger);
349 free(call->frame_buf);
350 LOGGER_WARNING("Jitter buffer creaton failed!"); 344 LOGGER_WARNING("Jitter buffer creaton failed!");
351 return ErrorInternal; 345 goto error;
352 } 346 }
353 347
354 if ( (call->cs = codec_init_session(codec_settings->audio_bitrate, 348 if ( (call->cs = codec_init_session(codec_settings->audio_bitrate,
@@ -359,14 +353,20 @@ int toxav_prepare_transmission ( ToxAv *av, int32_t call_index, ToxAvCodecSettin
359 codec_settings->video_width, 353 codec_settings->video_width,
360 codec_settings->video_height, 354 codec_settings->video_height,
361 codec_settings->video_bitrate) )) { 355 codec_settings->video_bitrate) )) {
356
357 if ( pthread_mutex_init(call->mutex, NULL) != 0 ) goto error;
358
362 call->call_active = 1; 359 call->call_active = 1;
360
363 return ErrorNone; 361 return ErrorNone;
364 } 362 }
365 363
364error:
366 rtp_terminate_session(call->crtps[audio_index], av->messenger); 365 rtp_terminate_session(call->crtps[audio_index], av->messenger);
367 rtp_terminate_session(call->crtps[video_index], av->messenger); 366 rtp_terminate_session(call->crtps[video_index], av->messenger);
368 free(call->frame_buf); 367 free(call->frame_buf);
369 terminate_queue(call->j_buf); 368 terminate_queue(call->j_buf);
369 codec_terminate_session(call->cs);
370 370
371 return ErrorInternal; 371 return ErrorInternal;
372} 372}
@@ -387,31 +387,18 @@ int toxav_kill_transmission ( ToxAv *av, int32_t call_index )
387 } 387 }
388 388
389 CallSpecific *call = &av->calls[call_index]; 389 CallSpecific *call = &av->calls[call_index];
390 390
391 pthread_mutex_lock(call->mutex);
392
391 call->call_active = 0; 393 call->call_active = 0;
392 394
393 if ( call->crtps[audio_index] && -1 == rtp_terminate_session(call->crtps[audio_index], av->messenger) ) { 395 rtp_terminate_session(call->crtps[audio_index], av->messenger); call->crtps[audio_index] = NULL;
394 LOGGER_ERROR("Error while terminating audio RTP session!\n"); 396 rtp_terminate_session(call->crtps[video_index], av->messenger); call->crtps[video_index] = NULL;
395 /*return ErrorTerminatingAudioRtp;*/ 397 terminate_queue(call->j_buf); call->j_buf = NULL;
396 } else call->crtps[audio_index] = NULL; 398 codec_terminate_session(call->cs); call->cs = NULL;
397 399
398 if ( call->crtps[video_index] && -1 == rtp_terminate_session(call->crtps[video_index], av->messenger) ) { 400 pthread_mutex_unlock(call->mutex);
399 LOGGER_ERROR("Error while terminating video RTP session!\n"); 401 pthread_mutex_destroy(call->mutex);
400 /*return ErrorTerminatingVideoRtp;*/
401 } else call->crtps[video_index] = NULL;
402
403 if ( call->j_buf ) {
404 terminate_queue(call->j_buf);
405 call->j_buf = NULL;
406 LOGGER_DEBUG("Terminated j queue");
407 } else LOGGER_DEBUG("No j queue");
408
409 if ( call->cs ) {
410 codec_terminate_session(call->cs);
411 call->cs = NULL;
412 LOGGER_DEBUG("Terminated codec session");
413 } else LOGGER_DEBUG("No codec session");
414
415 402
416 return ErrorNone; 403 return ErrorNone;
417} 404}
@@ -431,22 +418,22 @@ int toxav_kill_transmission ( ToxAv *av, int32_t call_index )
431inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallType type, const uint8_t *payload, 418inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallType type, const uint8_t *payload,
432 unsigned int length ) 419 unsigned int length )
433{ 420{
434#define send(data, len) rtp_send_msg(av->calls[call_index].crtps[type - TypeAudio], av->msi_session->messenger_handle, data, len) 421 CallSpecific* call = &av->calls[call_index];
435 422 if (call->crtps[type - TypeAudio]) {
436 if (av->calls[call_index].crtps[type - TypeAudio]) { 423
437 if (type == TypeAudio) { 424 if (type == TypeAudio) {
438 return send(payload, length); 425 return rtp_send_msg(call->crtps[type - TypeAudio], av->messenger, payload, length);
439 } else { 426 } else {
440 if (length == 0 || length > MAX_VIDEOFRAME_SIZE) { 427 if (length == 0 || length > MAX_VIDEOFRAME_SIZE) {
441 LOGGER_ERROR("Invalid video frame size: %u\n", length); 428 LOGGER_ERROR("Invalid video frame size: %u\n", length);
442 return -1; 429 return ErrorInternal;
443 } 430 }
444 431
445 /* number of pieces - 1*/ 432 /* number of pieces - 1*/
446 uint8_t numparts = (length - 1) / VIDEOFRAME_PIECE_SIZE; 433 uint8_t numparts = (length - 1) / VIDEOFRAME_PIECE_SIZE;
447 434
448 uint8_t load[2 + VIDEOFRAME_PIECE_SIZE]; 435 uint8_t load[2 + VIDEOFRAME_PIECE_SIZE];
449 load[0] = av->calls[call_index].frame_outid++; 436 load[0] = call->frame_outid++;
450 load[1] = 0; 437 load[1] = 0;
451 438
452 int i; 439 int i;
@@ -454,9 +441,11 @@ inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallTy
454 for (i = 0; i < numparts; i++) { 441 for (i = 0; i < numparts; i++) {
455 memcpy(load + VIDEOFRAME_HEADER_SIZE, payload, VIDEOFRAME_PIECE_SIZE); 442 memcpy(load + VIDEOFRAME_HEADER_SIZE, payload, VIDEOFRAME_PIECE_SIZE);
456 payload += VIDEOFRAME_PIECE_SIZE; 443 payload += VIDEOFRAME_PIECE_SIZE;
457 444
458 if (send(load, VIDEOFRAME_HEADER_SIZE + VIDEOFRAME_PIECE_SIZE) != 0) { 445 if (rtp_send_msg(call->crtps[type - TypeAudio], av->messenger,
459 return -1; 446 load, VIDEOFRAME_HEADER_SIZE + VIDEOFRAME_PIECE_SIZE) != 0) {
447
448 return ErrorInternal;
460 } 449 }
461 450
462 load[1]++; 451 load[1]++;
@@ -465,13 +454,12 @@ inline__ int toxav_send_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallTy
465 /* remainder = length % VIDEOFRAME_PIECE_SIZE, VIDEOFRAME_PIECE_SIZE if = 0 */ 454 /* remainder = length % VIDEOFRAME_PIECE_SIZE, VIDEOFRAME_PIECE_SIZE if = 0 */
466 length = ((length - 1) % VIDEOFRAME_PIECE_SIZE) + 1; 455 length = ((length - 1) % VIDEOFRAME_PIECE_SIZE) + 1;
467 memcpy(load + VIDEOFRAME_HEADER_SIZE, payload, length); 456 memcpy(load + VIDEOFRAME_HEADER_SIZE, payload, length);
468 return send(load, VIDEOFRAME_HEADER_SIZE + length); 457
458 return rtp_send_msg(call->crtps[type - TypeAudio], av->messenger, load, VIDEOFRAME_HEADER_SIZE + length);
469 } 459 }
470 } else { 460 } else {
471 return -1; 461 return ErrorNoRtpSession;
472 } 462 }
473
474#undef send
475} 463}
476 464
477/** 465/**
@@ -489,9 +477,9 @@ inline__ int toxav_recv_rtp_payload ( ToxAv *av, int32_t call_index, ToxAvCallTy
489 if ( !dest ) return ErrorInternal; 477 if ( !dest ) return ErrorInternal;
490 478
491 CallSpecific *call = &av->calls[call_index]; 479 CallSpecific *call = &av->calls[call_index];
492 480
493 if ( !call->crtps[type - TypeAudio] ) return ErrorNoRtpSession; 481 if ( !call->crtps[type - TypeAudio] ) return ErrorNoRtpSession;
494 482
495 RTPMessage *message; 483 RTPMessage *message;
496 484
497 if ( type == TypeAudio ) { 485 if ( type == TypeAudio ) {
@@ -545,9 +533,10 @@ inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **out
545 } 533 }
546 534
547 535
536 CallSpecific *call = &av->calls[call_index];
537 pthread_mutex_lock(call->mutex);
538
548 uint8_t packet [RTP_PAYLOAD_SIZE]; 539 uint8_t packet [RTP_PAYLOAD_SIZE];
549 CallSpecific *call = &av->calls[call_index];
550
551 int recved_size; 540 int recved_size;
552 541
553 while ((recved_size = toxav_recv_rtp_payload(av, call_index, TypeVideo, packet)) > 0) { 542 while ((recved_size = toxav_recv_rtp_payload(av, call_index, TypeVideo, packet)) > 0) {
@@ -597,7 +586,9 @@ inline__ int toxav_recv_video ( ToxAv *av, int32_t call_index, vpx_image_t **out
597 img = vpx_codec_get_frame(&call->cs->v_decoder, &iter); 586 img = vpx_codec_get_frame(&call->cs->v_decoder, &iter);
598 587
599 *output = img; 588 *output = img;
600 return 0; 589
590 pthread_mutex_unlock(call->mutex);
591 return ErrorNone;
601} 592}
602 593
603/** 594/**
@@ -616,8 +607,12 @@ inline__ int toxav_send_video ( ToxAv *av, int32_t call_index, const uint8_t *fr
616 return ErrorNoCall; 607 return ErrorNoCall;
617 } 608 }
618 609
619 610
620 return toxav_send_rtp_payload(av, call_index, TypeVideo, frame, frame_size); 611 pthread_mutex_lock(av->calls[call_index].mutex);
612 int rc = toxav_send_rtp_payload(av, call_index, TypeVideo, frame, frame_size);
613 pthread_mutex_unlock(av->calls[call_index].mutex);
614
615 return rc;
621} 616}
622 617
623/** 618/**
@@ -640,11 +635,13 @@ inline__ int toxav_prepare_video_frame(ToxAv *av, int32_t call_index, uint8_t *d
640 635
641 636
642 CallSpecific *call = &av->calls[call_index]; 637 CallSpecific *call = &av->calls[call_index];
638 pthread_mutex_lock(call->mutex);
643 639
644 int rc = vpx_codec_encode(&call->cs->v_encoder, input, call->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US); 640 int rc = vpx_codec_encode(&call->cs->v_encoder, input, call->cs->frame_counter, 1, 0, MAX_ENCODE_TIME_US);
645 641
646 if ( rc != VPX_CODEC_OK) { 642 if ( rc != VPX_CODEC_OK) {
647 LOGGER_ERROR("Could not encode video frame: %s\n", vpx_codec_err_to_string(rc)); 643 LOGGER_ERROR("Could not encode video frame: %s\n", vpx_codec_err_to_string(rc));
644 pthread_mutex_unlock(call->mutex);
648 return ErrorInternal; 645 return ErrorInternal;
649 } 646 }
650 647
@@ -656,13 +653,17 @@ inline__ int toxav_prepare_video_frame(ToxAv *av, int32_t call_index, uint8_t *d
656 653
657 while ( (pkt = vpx_codec_get_cx_data(&call->cs->v_encoder, &iter)) ) { 654 while ( (pkt = vpx_codec_get_cx_data(&call->cs->v_encoder, &iter)) ) {
658 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) { 655 if (pkt->kind == VPX_CODEC_CX_FRAME_PKT) {
659 if ( copied + pkt->data.frame.sz > dest_max ) return ErrorPacketTooLarge; 656 if ( copied + pkt->data.frame.sz > dest_max ) {
657 pthread_mutex_unlock(call->mutex);
658 return ErrorPacketTooLarge;
659 }
660 660
661 memcpy(dest + copied, pkt->data.frame.buf, pkt->data.frame.sz); 661 memcpy(dest + copied, pkt->data.frame.buf, pkt->data.frame.sz);
662 copied += pkt->data.frame.sz; 662 copied += pkt->data.frame.sz;
663 } 663 }
664 } 664 }
665 665
666 pthread_mutex_unlock(call->mutex);
666 return copied; 667 return copied;
667} 668}
668 669
@@ -689,6 +690,7 @@ inline__ int toxav_recv_audio ( ToxAv *av, int32_t call_index, int frame_size, i
689 690
690 691
691 CallSpecific *call = &av->calls[call_index]; 692 CallSpecific *call = &av->calls[call_index];
693 pthread_mutex_lock(call->mutex);
692 694
693 uint8_t packet [RTP_PAYLOAD_SIZE]; 695 uint8_t packet [RTP_PAYLOAD_SIZE];
694 696
@@ -696,7 +698,9 @@ inline__ int toxav_recv_audio ( ToxAv *av, int32_t call_index, int frame_size, i
696 698
697 if ( recved_size == ErrorAudioPacketLost ) { 699 if ( recved_size == ErrorAudioPacketLost ) {
698 int dec_size = opus_decode(call->cs->audio_decoder, NULL, 0, dest, frame_size, 1); 700 int dec_size = opus_decode(call->cs->audio_decoder, NULL, 0, dest, frame_size, 1);
699 701
702 pthread_mutex_unlock(call->mutex);
703
700 if ( dec_size < 0 ) { 704 if ( dec_size < 0 ) {
701 LOGGER_WARNING("Decoding error: %s", opus_strerror(dec_size)); 705 LOGGER_WARNING("Decoding error: %s", opus_strerror(dec_size));
702 return ErrorInternal; 706 return ErrorInternal;
@@ -704,12 +708,15 @@ inline__ int toxav_recv_audio ( ToxAv *av, int32_t call_index, int frame_size, i
704 708
705 } else if ( recved_size ) { 709 } else if ( recved_size ) {
706 int dec_size = opus_decode(call->cs->audio_decoder, packet, recved_size, dest, frame_size, 0); 710 int dec_size = opus_decode(call->cs->audio_decoder, packet, recved_size, dest, frame_size, 0);
707 711
712 pthread_mutex_unlock(call->mutex);
713
708 if ( dec_size < 0 ) { 714 if ( dec_size < 0 ) {
709 LOGGER_WARNING("Decoding error: %s", opus_strerror(dec_size)); 715 LOGGER_WARNING("Decoding error: %s", opus_strerror(dec_size));
710 return ErrorInternal; 716 return ErrorInternal;
711 } else return dec_size; 717 } else return dec_size;
712 } else { 718 } else {
719 pthread_mutex_unlock(call->mutex);
713 return 0; /* Nothing received */ 720 return 0; /* Nothing received */
714 } 721 }
715} 722}
@@ -732,8 +739,12 @@ inline__ int toxav_send_audio ( ToxAv *av, int32_t call_index, const uint8_t *fr
732 return ErrorNoCall; 739 return ErrorNoCall;
733 } 740 }
734 741
735 742
736 return toxav_send_rtp_payload(av, call_index, TypeAudio, frame, frame_size); 743 pthread_mutex_lock(av->calls[call_index].mutex);
744 int rc = toxav_send_rtp_payload(av, call_index, TypeAudio, frame, frame_size);
745 pthread_mutex_unlock(av->calls[call_index].mutex);
746
747 return rc;
737} 748}
738 749
739/** 750/**
@@ -756,9 +767,13 @@ inline__ int toxav_prepare_audio_frame ( ToxAv *av, int32_t call_index, uint8_t
756 return ErrorNoCall; 767 return ErrorNoCall;
757 } 768 }
758 769
759 770
771 pthread_mutex_lock(av->calls[call_index].mutex);
772
760 int32_t rc = opus_encode(av->calls[call_index].cs->audio_encoder, frame, frame_size, dest, dest_max); 773 int32_t rc = opus_encode(av->calls[call_index].cs->audio_encoder, frame, frame_size, dest, dest_max);
761 774
775 pthread_mutex_unlock(av->calls[call_index].mutex);
776
762 if (rc < 0) { 777 if (rc < 0) {
763 LOGGER_ERROR("Failed to encode payload: %s\n", opus_strerror(rc)); 778 LOGGER_ERROR("Failed to encode payload: %s\n", opus_strerror(rc));
764 return ErrorInternal; 779 return ErrorInternal;