[FFmpeg-devel] [PATCH] libavformat/UDP Protocol : seg fault on closing

Laurent BRULET lbrulet at gmail.com
Fri Jan 13 19:15:46 CET 2012


Hi ,

The following patch fixes ticket #915 (
https://ffmpeg.org/trac/ffmpeg/ticket/915)

--
Laurent

---
diff --git a/libavformat/udp.c b/libavformat/udp.c
index cdcd136..c823b69 100644
--- a/libavformat/udp.c
+++ b/libavformat/udp.c
@@ -71,6 +71,7 @@ typedef struct {
     pthread_t circular_buffer_thread;
     pthread_mutex_t mutex;
     pthread_cond_t cond;
+    int exit_thread;
 #endif
     uint8_t tmp[UDP_MAX_PKT_SIZE+4];
     int remaining_in_dg;
@@ -327,7 +328,7 @@ static void *circular_buffer_task( void *_URLContext)
     fd_set rfds;
     struct timeval tv;
 
-    for(;;) {
+    while(!s->exit_thread) {
         int left;
         int ret;
         int len;
@@ -529,6 +530,7 @@ static int udp_open(URLContext *h, const char *uri,
int flags)
         s->fifo = av_fifo_alloc(s->circular_buffer_size);
         pthread_mutex_init(&s->mutex, NULL);
         pthread_cond_init(&s->cond, NULL);
+        s->exit_thread = 0;
         if (pthread_create(&s->circular_buffer_thread, NULL,
circular_buffer_task, h)) {
             av_log(h, AV_LOG_ERROR, "pthread_create failed\n");
             goto fail;
@@ -617,12 +619,18 @@ static int udp_write(URLContext *h, const uint8_t
*buf, int size)
 static int udp_close(URLContext *h)
 {
     UDPContext *s = h->priv_data;
+    int ret;
 
     if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
         udp_leave_multicast_group(s->udp_fd, (struct sockaddr
*)&s->dest_addr);
     closesocket(s->udp_fd);
     av_fifo_free(s->fifo);
 #if HAVE_PTHREADS
+    s->exit_thread = 1;
+    ret = pthread_join(s->circular_buffer_thread, NULL);
+    if (ret != 0)
+        av_log(h, AV_LOG_ERROR, "pthread_join(): %s\n", strerror(ret));
+
     pthread_mutex_destroy(&s->mutex);
     pthread_cond_destroy(&s->cond);
 #endif



More information about the ffmpeg-devel mailing list