Ignore:
Timestamp:
11/20/17 17:42:27 (6 years ago)
Author:
obi
Message:

update libeplayer3 v36

File:
1 edited

Legend:

Unmodified
Added
Removed
  • titan/libeplayer3/main/exteplayer.c

    r40322 r41219  
    3232#include <sys/resource.h>
    3333#include <sys/mman.h>
     34#include <sys/socket.h>
     35#include <sys/un.h>
     36#include <errno.h>
     37
     38#include <pthread.h>
    3439
    3540#include "common.h"
     41#include "misc.h"
    3642
    3743#define DUMP_BOOL(x) 0 == x ? "false"  : "true"
     
    4854extern void            rtmp_proto_impl_set(const int32_t val);
    4955extern void        flv2mpeg4_converter_set(const int32_t val);
     56extern void        sel_program_id_set(const int32_t val);
    5057
    5158extern void pcm_resampling_set(int32_t val);
     
    6168static Context_t *g_player = NULL;
    6269
     70static void TerminateAllSockets(void)
     71{
     72    int i;
     73    for(i=0; i<1024; ++i)
     74    {
     75        if( 0 == shutdown(i, SHUT_RDWR) )
     76        {
     77            /* yes, I know that this is not good practice and I know what this could cause
     78             * but in this use case it can be accepted.
     79             * We must close socket because without closing it recv will return 0 (after shutdown)
     80             * 0 is not correctly handled by external libraries
     81             */
     82            close(i);
     83        }
     84    }
     85}
     86
     87static int g_pfd[2] = {-1, -1}; /* Used to wake terminate thread */
     88static int isPlaybackStarted = 0;
     89static pthread_mutex_t playbackStartMtx;
     90
     91static void *TermThreadFun(void *arg)
     92{
     93    const char *socket_path = "/tmp/iptvplayer_extplayer_term_fd";
     94    struct sockaddr_un addr;
     95    int fd = -1;
     96    int cl = -1;
     97    int nfds = 1;
     98    fd_set readfds;
     99   
     100    unlink(socket_path);
     101    if ( (fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1)
     102    {
     103        perror("TermThreadFun socket error");
     104        goto finish;
     105    }
     106
     107    memset(&addr, 0, sizeof(addr));
     108    addr.sun_family = AF_UNIX;
     109    strncpy(addr.sun_path, socket_path, sizeof(addr.sun_path)-1);
     110
     111    if (bind(fd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
     112    {
     113        perror("TermThreadFun bind error");
     114        goto finish;
     115    }
     116
     117    if (listen(fd, 1) == -1)
     118    {
     119        perror("TermThreadFun listen error");
     120        goto finish;
     121    }
     122
     123    FD_ZERO(&readfds);
     124    FD_SET(g_pfd[0], &readfds);
     125    FD_SET(fd, &readfds);
     126   
     127    nfds = fd > g_pfd[0] ? fd + 1 : g_pfd[0] + 1;
     128   
     129    while (select(nfds, &readfds, NULL, NULL, NULL) == -1
     130           && errno == EINTR)
     131    {
     132        /* Restart if interrupted by signal */
     133        continue;
     134    }
     135   
     136    if (FD_ISSET(fd, &readfds))
     137    {
     138        /*
     139        if ( (cl = accept(fd, NULL, NULL)) == -1)
     140        {
     141            perror("TermThreadFun accept error");
     142            goto finish;
     143        }
     144        */
     145       
     146        pthread_mutex_lock(&playbackStartMtx);
     147        PlaybackDieNow(1);
     148        if (isPlaybackStarted)
     149            TerminateAllSockets();
     150        else
     151            kill(getpid(), SIGINT);
     152        pthread_mutex_unlock(&playbackStartMtx);
     153    }
     154
     155finish:
     156    close(cl);
     157    close(fd);
     158    pthread_exit(NULL);
     159   
     160}
     161
    63162static void map_inter_file_path(char *filename)
    64163{
     
    84183static int kbhit(void)
    85184{
    86         struct timeval tv;
    87         fd_set read_fd;
    88 
    89         tv.tv_sec=1;
    90         tv.tv_usec=0;
    91 
    92         FD_ZERO(&read_fd);
    93         FD_SET(0,&read_fd);
    94 
    95         if(-1 == select(1, &read_fd, NULL, NULL, &tv))
    96         {
    97             return 0;
    98         }
    99 
    100         if(FD_ISSET(0,&read_fd))
    101         {
    102             return 1;
    103         }
    104 
     185    struct timeval tv;
     186    fd_set read_fd;
     187
     188    tv.tv_sec=1;
     189    tv.tv_usec=0;
     190
     191    FD_ZERO(&read_fd);
     192    FD_SET(0,&read_fd);
     193
     194    if(-1 == select(1, &read_fd, NULL, NULL, &tv))
     195    {
    105196        return 0;
     197    }
     198
     199    if(FD_ISSET(0,&read_fd))
     200    {
     201        return 1;
     202    }
     203
     204    return 0;
    106205}
    107206
     
    277376    int aopt = 0, bopt = 0;
    278377    char *copt = 0, *dopt = 0;
    279     while ( (c = getopt(argc, argv, "we3dlsrimva:n:x:u:c:h:o:p:t:9:0:1:4:f:")) != -1)
     378    while ( (c = getopt(argc, argv, "we3dlsrimva:n:x:u:c:h:o:p:P:t:9:0:1:4:f:")) != -1)
    280379    {
    281380        switch (c)
     
    328427            SetNice(atoi(optarg));
    329428            break;
     429        case 'P':
     430            sel_program_id_set(atoi(optarg));
     431            break;
    330432        case 't':
    331433            *pAudioTrackIdx = atoi(optarg);
     
    412514int main(int argc, char* argv[])
    413515{
     516    pthread_t termThread;
     517    int isTermThreadStarted = 0;
    414518    char file[IPTV_MAX_FILE_PATH];
    415519    memset(file, '\0', sizeof(file));
     
    425529    int commandRetVal = -1;
    426530    /* inform client that we can handle additional commands */
    427     fprintf(stderr, "{\"EPLAYER3_EXTENDED\":{\"version\":%d}}\n", 34);
     531    fprintf(stderr, "{\"EPLAYER3_EXTENDED\":{\"version\":%d}}\n", 36);
    428532
    429533    if (0 != ParseParams(argc, argv, file, audioFile, &audioTrackIdx, &subtitleTrackIdx))
     
    446550        printf("[-o 0|1] set progressive download\n");
    447551        printf("[-p value] nice value\n");
     552        printf("[-P value] select Program ID from multi-service stream\n");
    448553        printf("[-t id] audio track ID switched on at start\n");
    449554        printf("[-9 id] subtitle track ID switched on at start\n");
     
    465570        exit(1);
    466571    }
     572   
     573    pthread_mutex_init(&playbackStartMtx, NULL);
     574    do
     575    {
     576        int flags = 0;
     577       
     578        if (pipe(g_pfd) == -1)
     579            break;
     580       
     581        /* Make read and write ends of pipe nonblocking */
     582        if ((flags = fcntl(g_pfd[0], F_GETFL)) == -1)
     583            break;
     584       
     585        /* Make read end nonblocking */
     586        flags |= O_NONBLOCK;
     587        if (fcntl(g_pfd[0], F_SETFL, flags) == -1)
     588            break;
     589       
     590        if ((flags = fcntl(g_pfd[1], F_GETFL)) == -1)
     591            break;
     592       
     593        /* Make write end nonblocking */
     594        flags |= O_NONBLOCK;
     595        if (fcntl(g_pfd[1], F_SETFL, flags) == -1)
     596            break;
     597       
     598        if(0 == pthread_create(&termThread, NULL, TermThreadFun, NULL))
     599            isTermThreadStarted = 1;
     600    } while(0);
    467601
    468602    g_player->playback    = &PlaybackHandler;
     
    505639   
    506640    {
     641        pthread_mutex_lock(&playbackStartMtx);
     642        isPlaybackStarted = 1;
     643        pthread_mutex_unlock(&playbackStartMtx);
     644       
    507645        commandRetVal = g_player->output->Command(g_player, OUTPUT_OPEN, NULL);
    508646        fprintf(stderr, "{\"OUTPUT_OPEN\":{\"sts\":%d}}\n", commandRetVal);
     
    757895        free(g_player);
    758896    }
     897   
     898    if (isTermThreadStarted && 1 == write(g_pfd[1], "x", 1))
     899    {
     900        pthread_join(termThread, NULL);
     901    }
     902   
     903    pthread_mutex_destroy(&playbackStartMtx);
     904   
     905    close(g_pfd[0]);
     906    close(g_pfd[1]);
    759907
    760908    //printOutputCapabilities();
Note: See TracChangeset for help on using the changeset viewer.