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 | 69 | int circular_buffer_error; |
| 70 | 70 | #if HAVE_PTHREADS |
| 71 | 71 | pthread_t circular_buffer_thread; |
| 72 | + pthread_mutex_t mutex; | |
| 73 | + pthread_cond_t cond; | |
| 72 | 74 | #endif |
| 73 | 75 | uint8_t tmp[UDP_MAX_PKT_SIZE+4]; |
| 74 | 76 | int remaining_in_dg; |
| @@ -317,6 +319,7 @@ static int udp_get_file_handle(URLContext *h) · 242 unmodified lines | ||
| 317 | 319 | return s->udp_fd; |
| 318 | 320 | } |
| 319 | 321 | |
| 322 | +#if HAVE_PTHREADS | |
| 320 | 323 | static void *circular_buffer_task( void *_URLContext) |
| 321 | 324 | { |
| 322 | 325 | URLContext *h = _URLContext; |
| @@ -331,7 +334,7 @@ static void *circular_buffer_task( void *_URLContext) · 8 unmodified lines | ||
| 331 | 334 | |
| 332 | 335 | if (ff_check_interrupt(&h->interrupt_callback)) { |
| 333 | 336 | s->circular_buffer_error = EINTR; |
| 334 | − return NULL; | |
| 337 | + goto end; | |
| 335 | 338 | } |
| 336 | 339 | |
| 337 | 340 | FD_ZERO(&rfds); |
| @@ -343,7 +346,7 @@ static void *circular_buffer_task( void *_URLContext) · 5 unmodified lines | ||
| 343 | 346 | if (ff_neterrno() == AVERROR(EINTR)) |
| 344 | 347 | continue; |
| 345 | 348 | s->circular_buffer_error = EIO; |
| 346 | − return NULL; | |
| 349 | + goto end; | |
| 347 | 350 | } |
| 348 | 351 | |
| 349 | 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 | 360 | if(left < UDP_MAX_PKT_SIZE + 4) { |
| 358 | 361 | av_log(h, AV_LOG_ERROR, "circular_buffer: OVERRUN\n"); |
| 359 | 362 | s->circular_buffer_error = EIO; |
| 360 | − return NULL; | |
| 363 | + goto end; | |
| 361 | 364 | } |
| 362 | 365 | left = FFMIN(left, s->fifo->end - s->fifo->wptr); |
| 363 | 366 | len = recv(s->udp_fd, s->tmp+4, sizeof(s->tmp)-4, 0); |
| 364 | 367 | if (len < 0) { |
| 365 | 368 | if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) { |
| 366 | 369 | s->circular_buffer_error = EIO; |
| 367 | − return NULL; | |
| 370 | + goto end; | |
| 368 | 371 | } |
| 369 | 372 | continue; |
| 370 | 373 | } |
| 371 | 374 | AV_WL32(s->tmp, len); |
| 375 | + pthread_mutex_lock(&s->mutex); | |
| 372 | 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 | 385 | return NULL; |
| 376 | 386 | } |
| 387 | +#endif | |
| 377 | 388 | |
| 378 | 389 | /* put it in UDP context */ |
| 379 | 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 | 527 | if (!is_output && s->circular_buffer_size) { |
| 517 | 528 | /* start the task going */ |
| 518 | 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 | 532 | if (pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h)) { |
| 520 | 533 | av_log(h, AV_LOG_ERROR, "pthread_create failed\n"); |
| 521 | 534 | goto fail; |
| @@ -536,15 +549,15 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) · 14 unmodified lines | ||
| 536 | 549 | UDPContext *s = h->priv_data; |
| 537 | 550 | int ret; |
| 538 | 551 | int avail; |
| 539 | − fd_set rfds; | |
| 540 | − struct timeval tv; | |
| 541 | 552 | |
| 553 | +#if HAVE_PTHREADS | |
| 542 | 554 | if (s->fifo) { |
| 543 | − | |
| 555 | + pthread_mutex_lock(&s->mutex); | |
| 544 | 556 | do { |
| 545 | 557 | avail = av_fifo_size(s->fifo); |
| 546 | 558 | if (avail) { // >=size) { |
| 547 | 559 | uint8_t tmp[4]; |
| 560 | + pthread_mutex_unlock(&s->mutex); | |
| 548 | 561 | |
| 549 | 562 | av_fifo_generic_read(s->fifo, tmp, 4, NULL); |
| 550 | 563 | avail= AV_RL32(tmp); |
| @@ -557,19 +570,15 @@ static int udp_read(URLContext *h, uint8_t *buf, int size) · 6 unmodified lines | ||
| 557 | 570 | av_fifo_drain(s->fifo, AV_RL32(tmp) - avail); |
| 558 | 571 | return avail; |
| 559 | 572 | } else if(s->circular_buffer_error){ |
| 573 | + pthread_mutex_unlock(&s->mutex); | |
| 560 | 574 | return s->circular_buffer_error; |
| 561 | 575 | } |
| 562 | 576 | else { |
| 563 | − FD_ZERO(&rfds); | |
| 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; | |
| 577 | + pthread_cond_wait(&s->cond, &s->mutex); | |
| 570 | 578 | } |
| 571 | 579 | } while( 1); |
| 572 | 580 | } |
| 581 | +#endif | |
| 573 | 582 | |
| 574 | 583 | if (!(h->flags & AVIO_FLAG_NONBLOCK)) { |
| 575 | 584 | ret = ff_network_wait_fd(s->udp_fd, 0); |
| @@ -610,6 +619,10 @@ static int udp_close(URLContext *h) · 34 unmodified lines | ||
| 610 | 619 | udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr); |
| 611 | 620 | closesocket(s->udp_fd); |
| 612 | 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 | 626 | return 0; |
| 614 | 627 | } |
| 615 | 628 | |