Revision 42756021

View differences:

libavformat/udp.c
34 34
#include "network.h"
35 35
#include "os_support.h"
36 36
#include "url.h"
37
#include <pthread.h>
38
#include <semaphore.h>
39
#include <sys/resource.h>
37 40
#include <sys/time.h>
38 41

  
39 42
#ifndef IPV6_ADD_MEMBERSHIP
......
51 54
    struct sockaddr_storage dest_addr;
52 55
    int dest_addr_len;
53 56
    int is_connected;
57

  
58
    /* Circular Buffer variables for use in UDP receive code */
59
    unsigned char *circular_buffer;
60
    int circular_buffer_head;
61
    int circular_buffer_size;
62
    int circular_buffer_tail;
63
    int circular_buffer_available;
64
    int circular_buffer_available_max;
65
    int circular_buffer_error;
66
    pthread_t circular_buffer_thread;
67
    sem_t circular_buffer_semaphore;
54 68
} UDPContext;
55 69

  
70
#define min(X,Y)                ((X)<(Y) ? (X):(Y))
71
#define max(X,Y)                ((X)>(Y) ? (X):(Y))
72

  
56 73
#define UDP_TX_BUF_SIZE 32768
57 74
#define UDP_MAX_PKT_SIZE 65536
58 75

  
......
298 315
    return s->udp_fd;
299 316
}
300 317

  
318
static void *circular_buffer_task( void *_URLContext)
319
{
320
    URLContext *h = _URLContext;
321
    UDPContext *s = h->priv_data;
322
    fd_set rfds;
323
    struct timeval tv;
324

  
325
    for(;;) {
326
        int left;
327
        int ret;
328
        int len;
329

  
330
        if (url_interrupt_cb()) {
331
            s->circular_buffer_error = EINTR;
332
            return NULL;
333
        }
334

  
335
        FD_ZERO(&rfds);
336
        FD_SET(s->udp_fd, &rfds);
337
        tv.tv_sec = 1;
338
        tv.tv_usec = 0;
339
        ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
340
        if (ret < 0) {
341
            if (ff_neterrno() == AVERROR(EINTR))
342
                continue;
343
            s->circular_buffer_error = EIO;
344
            return NULL;
345
        }
346

  
347
        if (!(ret > 0 && FD_ISSET(s->udp_fd, &rfds)))
348
            continue;
349

  
350
        /* How much do we have left to the end of the buffer */
351
        left = s->circular_buffer_size-s->circular_buffer_head;
352
        /* Whats the minimum we can read so that we dont comletely fill the buffer */
353
        sem_wait( &s->circular_buffer_semaphore);
354
        left = min( left, s->circular_buffer_size-s->circular_buffer_available);
355
        sem_post( &s->circular_buffer_semaphore );
356
        /* No Space left, error, what do we do now */
357
        if( !left) {
358
            av_log(h, AV_LOG_ERROR, "circular_buffer: OVERRUN\n");
359
            s->circular_buffer_error = EIO;
360
            return NULL;
361
        }
362
        len = recv(s->udp_fd, s->circular_buffer+s->circular_buffer_head, left, 0);
363
        if (len < 0) {
364
            if (ff_neterrno() != AVERROR(EAGAIN) && ff_neterrno() != AVERROR(EINTR)) {
365
                s->circular_buffer_error = EIO;
366
                return NULL;
367
            }
368
        }
369
        s->circular_buffer_head += len;
370
        sem_wait( &s->circular_buffer_semaphore);
371
        s->circular_buffer_available += len;
372
        s->circular_buffer_available_max = max( s->circular_buffer_available_max, s->circular_buffer_available);
373
        sem_post( &s->circular_buffer_semaphore );
374
        if( s->circular_buffer_head>=s->circular_buffer_size)
375
            s->circular_buffer_head -= s->circular_buffer_size;
376
    }
377

  
378
    return NULL;
379
}
380

  
301 381
/* put it in UDP context */
302 382
/* return non zero if error */
303 383
static int udp_open(URLContext *h, const char *uri, int flags)
......
325 405
    s->ttl = 16;
326 406
    s->buffer_size = is_output ? UDP_TX_BUF_SIZE : UDP_MAX_PKT_SIZE;
327 407

  
408
    s->circular_buffer_size = 7*188*4096;
409

  
328 410
    p = strchr(uri, '?');
329 411
    if (p) {
330 412
        if (av_find_info_tag(buf, sizeof(buf), "reuse", p)) {
331
            const char *endptr=NULL;
413
            char *endptr=NULL;
332 414
            s->reuse_socket = strtol(buf, &endptr, 10);
333 415
            /* assume if no digits were found it is a request to enable it */
334 416
            if (buf == endptr)
......
350 432
        if (av_find_info_tag(buf, sizeof(buf), "connect", p)) {
351 433
            s->is_connected = strtol(buf, NULL, 10);
352 434
        }
435
        if (av_find_info_tag(buf, sizeof(buf), "buf_size", p)) {
436
            s->circular_buffer_size = strtol(buf, NULL, 10)*188;
437
        }
353 438
    }
354 439

  
355 440
    /* fill the dest addr */
......
431 516
    }
432 517

  
433 518
    s->udp_fd = udp_fd;
519

  
520
    if (!is_output && s->circular_buffer_size) {
521
        /* start the task going */
522
        s->circular_buffer = av_malloc( s->circular_buffer_size);
523
        if (sem_init( &s->circular_buffer_semaphore, PTHREAD_PROCESS_PRIVATE, 1 )) {
524
            av_log(h, AV_LOG_ERROR, "sem_init failed\n");
525
            goto fail;
526
        }
527
        if (pthread_create(&s->circular_buffer_thread, NULL, circular_buffer_task, h)) {
528
            av_log(h, AV_LOG_ERROR, "pthread_create failed\n");
529
            goto fail;
530
        }
531
    }
532

  
434 533
    return 0;
435 534
 fail:
436 535
    if (udp_fd >= 0)
437 536
        closesocket(udp_fd);
537
        if (s->circular_buffer) {
538
            sem_destroy( &s->circular_buffer_semaphore);
539
            av_free( s->circular_buffer);
540
        }
438 541
    av_free(s);
439 542
    return AVERROR(EIO);
440 543
}
......
443 546
{
444 547
    UDPContext *s = h->priv_data;
445 548
    int ret;
549
    int avail;
550
    int left;
551
    fd_set rfds;
552
    struct timeval tv;
553

  
554
    if (s->circular_buffer_thread) {
555

  
556
        do {
557
            sem_wait( &s->circular_buffer_semaphore );
558
            avail = s->circular_buffer_available;
559
            sem_post( &s->circular_buffer_semaphore );
560
            if (avail) { // >=size) {
561

  
562
                // Maximum amount available
563
                size = min( avail, size);
564
                // Whats left till the end of the circular buffer
565
                left = s->circular_buffer_size-s->circular_buffer_tail;
566
                // How much do we need, all?
567
                left = min( left, size);
568
                // Get the first block
569
                memcpy( buf, s->circular_buffer+s->circular_buffer_tail, left);
570
                // Have we any more, this will be from the start of the buffer
571
                if (size-left)
572
                    memcpy( buf+left, s->circular_buffer, size-left);
573
                // Check for the tail wrapping around
574
                s->circular_buffer_tail += size;
575
                if( s->circular_buffer_tail>=s->circular_buffer_size)
576
                    s->circular_buffer_tail -= s->circular_buffer_size;
577
                // Update the available amount
578
                sem_wait( &s->circular_buffer_semaphore );
579
                s->circular_buffer_available -= size;
580
                sem_post( &s->circular_buffer_semaphore );
581
                return size;
582
            }
583
            else {
584
                FD_ZERO(&rfds);
585
                FD_SET(s->udp_fd, &rfds);
586
                tv.tv_sec = 1;
587
                tv.tv_usec = 0;
588
                ret = select(s->udp_fd + 1, &rfds, NULL, NULL, &tv);
589
                if (ret<0)
590
                    return ret;
591
            }
592
        } while( 1);
593
    }
446 594

  
447 595
    if (!(h->flags & AVIO_FLAG_NONBLOCK)) {
448 596
        ret = ff_network_wait_fd(s->udp_fd, 0);
......
450 598
            return ret;
451 599
    }
452 600
    ret = recv(s->udp_fd, buf, size, 0);
601

  
453 602
    return ret < 0 ? ff_neterrno() : ret;
454 603
}
455 604

  
......
481 630
    if (s->is_multicast && (h->flags & AVIO_FLAG_READ))
482 631
        udp_leave_multicast_group(s->udp_fd, (struct sockaddr *)&s->dest_addr);
483 632
    closesocket(s->udp_fd);
633
    if (s->circular_buffer) {
634
        sem_destroy( &s->circular_buffer_semaphore);
635
        av_free( s->circular_buffer);
636
        av_log( h, AV_LOG_INFO, "circular_buffer_info max:%d%%\r\n", (s->circular_buffer_available_max*100)/s->circular_buffer_size);
637
    }
484 638
    av_free(s);
485 639
    return 0;
486 640
}

Also available in: Unified diff