When no data was available both the buffer thread as well as the main thread would block in select(), when data becomes available both should move forward and as data is read in the buffer thread the main thread would block in select() later the read data was put in the fifo but the main thread still would be blocked in select() until either the timeout or another packet would come in. This is solved in this commit by using a mutex and a condition variable
Unified · Split
| @@ -69,6 +69,8 @@ typedef struct { · 68 unmodified lines | |||
| 69 | int circular_buffer_error; | 69 | int circular_buffer_error; |
| 70 | #if HAVE_PTHREADS | 70 | #if HAVE_PTHREADS |
| 71 | pthread_t circular_buffer_thread; | 71 | pthread_t circular_buffer_thread; |
| 72 | pthread_mutex_t mutex; | ||
| 73 | pthread_cond_t cond; | ||
| 72 | #endif | 74 | #endif |
| 73 | uint8_t tmp[UDP_MAX_PKT_SIZE+4]; | 75 | uint8_t tmp[UDP_MAX_PKT_SIZE+4]; |
| 74 | int remaining_in_dg; | 76 | int remaining_in_dg; |
| @@ -317,6 +319,7 @@ static int udp_get_file_handle(URLContext *h) · 242 unmodified lines | |||
| 317 | return s->udp_fd; | 319 | return s->udp_fd; |
| 318 | } | 320 | } |
| 319 | 321 | ||
| 322 | #if HAVE_PTHREADS | ||
| 320 | static void *circular_buffer_task( void *_URLContext) | 323 | static void *circular_buffer_task( void *_URLContext) |
| 321 | { | 324 | { |
| 322 | URLContext *h = _URLContext; | 325 | URLContext *h = _URLContext; |
| @@ -331,7 +334,7 @@ static void *circular_buffer_task( void *_URLContext) · 8 unmodified lines | |||
| 331 | 334 | ||
| 332 | if (ff_check_interrupt(&h->interrupt_callback)) { | 335 | if (ff_check_interrupt(&h->interrupt_callback)) { |
| 333 | s->circular_buffer_error = EINTR; | 336 | s->circular_buffer_error = EINTR; |
| 334 | return NULL; | 337 | goto end; |
| 335 | } | 338 | } |
| 336 | 339 | ||
| 337 | FD_ZERO(&rfds); | 340 | FD_ZERO(&rfds); |
| @@ -343,7 +346,7 @@ static void *circular_buffer_task( void *_URLContext) · 5 unmodified lines | |||
| 343 | if (ff_neterrno() == AVERROR(EINTR)) | 346 | if (ff_neterrno() == AVERROR(EINTR)) |
| 344 | continue; | 347 | continue; |
| 345 | s->circular_buffer_error = EIO; | 348 | s->circular_buffer_error = EIO; |
| 346 | return NULL; | 349 | goto end; |
| 347 | } | 350 | } |
| 348 | 351 | ||
| 349 | if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds))) | 352 | if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds))) |
| @@ -357,23 +360,31 @@ static void *circular_buffer_task( void *_URLContext) · 7 unmodified lines | |||
| 357 | if(left < UDP_MAX_PKT_SIZE + 4) { | 360 | if(left < UDP_MAX_PKT_SIZE + 4) { |
| 358 | av_log(h, AV_LOG_ERROR, "circular_buffer: OVERRUN\n"); | 361 | av_log(h, AV_LOG_ERROR, "circular_buffer: OVERRUN\n"); |
| 359 | s->circular_buffer_error = EIO; | 362 | s->circular_buffer_error = EIO; |
| 360 | return NULL; | 363 | goto end; |
| 361 | } | 364 | } |
| 362 | left = FFMIN(left, s->fifo->end - s->fifo->wptr); | 365 | left = FFMIN(left, s->fifo->end - s->fifo->wptr); |
| 363 | len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0); | 366 | len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0); |
| 364 | if (len < 0) { | 367 | if (len < 0) { |
| 365 | if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) { | 368 | if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) { |
| 366 | s->circular_buffer_error = EIO; | 369 | s->circular_buffer_error = EIO; |
| 367 | return NULL; | 370 | goto end; |
| 368 | } | 371 | } |
| 369 | continue; | 372 | continue; |
| 370 | } | 373 | } |
| 371 | AV_WL32(s->tmp, len); | 374 | AV_WL32(s->tmp, len); |
| 375 | pthread_mutex_lock(&s->mutex); | ||
| 372 | av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL); | 376 | av_fifo_generic_write(s->fifo, s->tmp, len+4, NULL); |
| 377 | pthread_cond_signal(&s->cond); | ||
| 378 | pthread_mutex_unlock(&s->mutex); | ||
| 373 | } | 379 | } |
| 374 | 380 | ||
| 381 | end: | ||
| 382 | pthread_mutex_lock(&s->mutex); | ||
| 383 | pthread_cond_signal(&s->cond); | ||
| 384 | pthread_mutex_unlock(&s->mutex); | ||
| 375 | return NULL; | 385 | return NULL; |
| 376 | } | 386 | } |
| 387 | #endif | ||
| 377 | 388 | ||
| 378 | /* put it in UDP context */ | 389 | /* put it in UDP context */ |
| 379 | /* return non zero if error */ | 390 | /* return non zero if error */ |
| @@ -516,6 +527,8 @@ static int udp_open(URLContext *h, const char *uri, int flags) · 136 unmodified lines | |||
| 516 | if (!is_output && s->circular_buffer_size) { | 527 | if (!is_output && s->circular_buffer_size) { |
| 517 | /* start the task going */ | 528 | /* start the task going */ |
| 518 | s->fifo = av_fifo_alloc(s->circular_buffer_size); | 529 | s->fifo = av_fifo_alloc(s->circular_buffer_size); |
| 530 | pthread_mutex_init(&s->mutex, NULL); | ||
| 531 | pthread_cond_init(&s->cond, NULL); | ||
| 519 | if (pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h)) { | 532 | if (pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h)) { |
| 520 | av_log(h, AV_LOG_ERROR, "pthread_create failed\n"); | 533 | av_log(h, AV_LOG_ERROR, "pthread_create failed\n"); |
| 521 | goto fail; | 534 | goto fail; |
| @@ -536,15 +549,15 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) · 14 unmodified lines | |||
| 536 | UDPContext *s = h->priv_data; | 549 | UDPContext *s = h->priv_data; |
| 537 | int ret; | 550 | int ret; |
| 538 | int avail; | 551 | int avail; |
| 539 | fd_set rfds; | ||
| 540 | struct timeval tv; | ||
| 541 | 552 | ||
| 553 | #if HAVE_PTHREADS | ||
| 542 | if (s->fifo) { | 554 | if (s->fifo) { |
| 543 | 555 | pthread_mutex_lock(&s->mutex); | |
| 544 | do { | 556 | do { |
| 545 | avail = av_fifo_size(s->fifo); | 557 | avail = av_fifo_size(s->fifo); |
| 546 | if (avail) { // >=size) { | 558 | if (avail) { // >=size) { |
| 547 | uint8_t tmp[4]; | 559 | uint8_t tmp[4]; |
| 560 | pthread_mutex_unlock(&s->mutex); | ||
| 548 | 561 | ||
| 549 | av_fifo_generic_read(s->fifo, tmp, 4, NULL); | 562 | av_fifo_generic_read(s->fifo, tmp, 4, NULL); |
| 550 | avail= AV_RL32(tmp); | 563 | avail= AV_RL32(tmp); |
| @@ -557,19 +570,15 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) · 6 unmodified lines | |||
| 557 | av_fifo_drain(s->fifo, AV_RL32(tmp) - avail); | 570 | av_fifo_drain(s->fifo, AV_RL32(tmp) - avail); |
| 558 | return avail; | 571 | return avail; |
| 559 | } else if(s->circular_buffer_error){ | 572 | } else if(s->circular_buffer_error){ |
| 573 | pthread_mutex_unlock(&s->mutex); | ||
| 560 | return s->circular_buffer_error; | 574 | return s->circular_buffer_error; |
| 561 | } | 575 | } |
| 562 | else { | 576 | else { |
| 563 | FD_ZERO(&rfds); | 577 | pthread_cond_wait(&s->cond, &s->mutex); |
| 564 | FD_SET(s->udp_fd, &rfds); | ||
| 565 | tv.tv_sec = 1; | ||
| 566 | tv.tv_usec = 0; | ||
| 567 | ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv); | ||
| 568 | if (ret<0) | ||
| 569 | return ret; | ||
| 570 | } | 578 | } |
| 571 | } while( 1); | 579 | } while( 1); |
| 572 | } | 580 | } |
| 581 | #endif | ||
| 573 | 582 | ||
| 574 | if (!(h->flags & AVIO_FLAG_NONBLOCK)) { | 583 | if (!(h->flags & AVIO_FLAG_NONBLOCK)) { |
| 575 | ret = ff_network_wait_fd(s->udp_fd, 0); | 584 | ret = ff_network_wait_fd(s->udp_fd, 0); |
| @@ -610,6 +619,10 @@ static int udp_close(URLContext *h) · 34 unmodified lines | |||
| 610 | udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr); | 619 | udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr); |
| 611 | closesocket(s->udp_fd); | 620 | closesocket(s->udp_fd); |
| 612 | av_fifo_free(s->fifo); | 621 | av_fifo_free(s->fifo); |
| 622 | #if HAVE_PTHREADS | ||
| 623 | pthread_mutex_destroy(&s->mutex); | ||
| 624 | pthread_cond_destroy(&s->cond); | ||
| 625 | #endif | ||
| 613 | return 0; | 626 | return 0; |
| 614 | } | 627 | } |
| 615 | 628 | ||