@@ -2544,7 +2544,9 @@ static void janus_sip_hangup_media_internal(janus_plugin_session *handle) {
2544
2544
session -> media .simulcast_ssrc = 0 ;
2545
2545
/* Do cleanup if media thread has not been created */
2546
2546
if (!session -> media .ready && !session -> relayer_thread ) {
2547
+ janus_mutex_lock (& session -> mutex );
2547
2548
janus_sip_media_cleanup (session );
2549
+ janus_mutex_unlock (& session -> mutex );
2548
2550
}
2549
2551
/* Get rid of the recorders, if available */
2550
2552
janus_mutex_lock (& session -> rec_mutex );
@@ -3396,13 +3398,16 @@ static void *janus_sip_handler(void *data) {
3396
3398
JANUS_LOG (LOG_VERB , "Going to negotiate video...\n" );
3397
3399
session -> media .has_video = TRUE; /* FIXME Maybe we need a better way to signal this */
3398
3400
}
3401
+ janus_mutex_lock (& session -> mutex );
3399
3402
if (janus_sip_allocate_local_ports (session , FALSE) < 0 ) {
3403
+ janus_mutex_unlock (& session -> mutex );
3400
3404
JANUS_LOG (LOG_ERR , "Could not allocate RTP/RTCP ports\n" );
3401
3405
janus_sdp_destroy (parsed_sdp );
3402
3406
error_code = JANUS_SIP_ERROR_IO_ERROR ;
3403
3407
g_snprintf (error_cause , 512 , "Could not allocate RTP/RTCP ports" );
3404
3408
goto error ;
3405
3409
}
3410
+ janus_mutex_unlock (& session -> mutex );
3406
3411
char * sdp = janus_sip_sdp_manipulate (session , parsed_sdp , FALSE);
3407
3412
if (sdp == NULL ) {
3408
3413
JANUS_LOG (LOG_ERR , "Error manipulating SDP\n" );
@@ -3694,13 +3699,16 @@ static void *janus_sip_handler(void *data) {
3694
3699
JANUS_LOG (LOG_VERB , "Going to negotiate video...\n" );
3695
3700
session -> media .has_video = TRUE; /* FIXME Maybe we need a better way to signal this */
3696
3701
}
3702
+ janus_mutex_lock (& session -> mutex );
3697
3703
if (janus_sip_allocate_local_ports (session , FALSE) < 0 ) {
3704
+ janus_mutex_unlock (& session -> mutex );
3698
3705
JANUS_LOG (LOG_ERR , "Could not allocate RTP/RTCP ports\n" );
3699
3706
janus_sdp_destroy (parsed_sdp );
3700
3707
error_code = JANUS_SIP_ERROR_IO_ERROR ;
3701
3708
g_snprintf (error_cause , 512 , "Could not allocate RTP/RTCP ports" );
3702
3709
goto error ;
3703
3710
}
3711
+ janus_mutex_unlock (& session -> mutex );
3704
3712
char * sdp = janus_sip_sdp_manipulate (session , parsed_sdp , TRUE);
3705
3713
if (sdp == NULL ) {
3706
3714
JANUS_LOG (LOG_ERR , "Could not allocate RTP/RTCP ports\n" );
@@ -3883,13 +3891,16 @@ static void *janus_sip_handler(void *data) {
3883
3891
session -> media .has_srtp_local_video = session -> media .has_srtp_remote_video ;
3884
3892
}
3885
3893
if (audio_added || video_added ) {
3894
+ janus_mutex_lock (& session -> mutex );
3886
3895
if (janus_sip_allocate_local_ports (session , TRUE) < 0 ) {
3896
+ janus_mutex_unlock (& session -> mutex );
3887
3897
JANUS_LOG (LOG_ERR , "Could not allocate RTP/RTCP ports\n" );
3888
3898
janus_sdp_destroy (parsed_sdp );
3889
3899
error_code = JANUS_SIP_ERROR_IO_ERROR ;
3890
3900
g_snprintf (error_cause , 512 , "Could not allocate RTP/RTCP ports" );
3891
3901
goto error ;
3892
3902
}
3903
+ janus_mutex_unlock (& session -> mutex );
3893
3904
if (!offer )
3894
3905
session -> media .updated = TRUE;
3895
3906
}
@@ -6252,8 +6263,10 @@ static int janus_sip_allocate_local_ports(janus_sip_session *session, gboolean u
6252
6263
session -> media .local_video_rtcp_port = rtcp_port ;
6253
6264
}
6254
6265
}
6255
- /* We need this to quickly interrupt the poll when it's time to update a session or wrap up */
6256
- pipe (session -> media .pipefd );
6266
+ if (!update ) {
6267
+ /* We need this to quickly interrupt the poll when it's time to update a session or wrap up */
6268
+ pipe (session -> media .pipefd );
6269
+ }
6257
6270
return 0 ;
6258
6271
}
6259
6272
@@ -6353,7 +6366,7 @@ static void *janus_sip_relay_thread(void *data) {
6353
6366
JANUS_LOG (LOG_VERB , "Starting relay thread (%s <--> %s)\n" , session -> account .username , session -> callee );
6354
6367
6355
6368
if (!session -> callee ) {
6356
- JANUS_LOG (LOG_VERB , "[SIP-%s] Leaving thread, no callee...\n" , session -> account .username );
6369
+ JANUS_LOG (LOG_WARN , "[SIP-%s] Leaving thread, no callee...\n" , session -> account .username );
6357
6370
janus_refcount_decrease (& session -> ref );
6358
6371
g_thread_unref (g_thread_self ());
6359
6372
return NULL ;
@@ -6366,6 +6379,14 @@ static void *janus_sip_relay_thread(void *data) {
6366
6379
int pipe_fd = session -> media .pipefd [0 ];
6367
6380
char buffer [1500 ];
6368
6381
memset (buffer , 0 , 1500 );
6382
+ if (pipe_fd == -1 ) {
6383
+ /* If the pipe file descriptor doesn't exist, it means we're done already,
6384
+ * and/or we may never be notified about sessions being closed, so give up */
6385
+ JANUS_LOG (LOG_WARN , "[SIP-%s] Leaving thread, no pipe file descriptor...\n" , session -> account .username );
6386
+ janus_refcount_decrease (& session -> ref );
6387
+ g_thread_unref (g_thread_self ());
6388
+ return NULL ;
6389
+ }
6369
6390
/* Loop */
6370
6391
int num = 0 ;
6371
6392
gboolean goon = TRUE;
@@ -6462,12 +6483,16 @@ static void *janus_sip_relay_thread(void *data) {
6462
6483
fds [num ].revents = 0 ;
6463
6484
num ++ ;
6464
6485
}
6465
- if ( pipe_fd != -1 ) {
6466
- fds [ num ]. fd = pipe_fd ;
6467
- fds [ num ]. events = POLLIN ;
6468
- fds [ num ]. revents = 0 ;
6469
- num ++ ;
6486
+ /* Finally, let's add the pipe */
6487
+ pipe_fd = session -> media . pipefd [ 0 ] ;
6488
+ if ( pipe_fd == -1 ) {
6489
+ /* Pipe was closed? Means the call is over */
6490
+ break ;
6470
6491
}
6492
+ fds [num ].fd = pipe_fd ;
6493
+ fds [num ].events = POLLIN ;
6494
+ fds [num ].revents = 0 ;
6495
+ num ++ ;
6471
6496
/* Wait for some data */
6472
6497
resfd = poll (fds , num , 1000 );
6473
6498
if (resfd < 0 ) {
@@ -6504,14 +6529,18 @@ static void *janus_sip_relay_thread(void *data) {
6504
6529
if (fds [i ].fd == session -> media .audio_rtcp_fd ) {
6505
6530
JANUS_LOG (LOG_WARN , "[SIP-%s] Got a '%s' on the audio RTCP socket, closing it\n" ,
6506
6531
session -> account .username , g_strerror (error ));
6532
+ janus_mutex_lock (& session -> mutex );
6507
6533
close (session -> media .audio_rtcp_fd );
6508
6534
session -> media .audio_rtcp_fd = -1 ;
6535
+ janus_mutex_unlock (& session -> mutex );
6509
6536
continue ;
6510
6537
} else if (fds [i ].fd == session -> media .video_rtcp_fd ) {
6511
6538
JANUS_LOG (LOG_WARN , "[SIP-%s] Got a '%s' on the video RTCP socket, closing it\n" ,
6512
6539
session -> account .username , g_strerror (error ));
6540
+ janus_mutex_lock (& session -> mutex );
6513
6541
close (session -> media .video_rtcp_fd );
6514
6542
session -> media .video_rtcp_fd = -1 ;
6543
+ janus_mutex_unlock (& session -> mutex );
6515
6544
continue ;
6516
6545
}
6517
6546
}
@@ -6687,7 +6716,9 @@ static void *janus_sip_relay_thread(void *data) {
6687
6716
}
6688
6717
}
6689
6718
/* Cleanup the media session */
6719
+ janus_mutex_lock (& session -> mutex );
6690
6720
janus_sip_media_cleanup (session );
6721
+ janus_mutex_unlock (& session -> mutex );
6691
6722
/* Done */
6692
6723
JANUS_LOG (LOG_VERB , "Leaving SIP relay thread\n" );
6693
6724
session -> relayer_thread = NULL ;
0 commit comments