Changeset 41219 for titan/libeplayer3/main/exteplayer.c
- Timestamp:
- 11/20/17 17:42:27 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
titan/libeplayer3/main/exteplayer.c
r40322 r41219 32 32 #include <sys/resource.h> 33 33 #include <sys/mman.h> 34 #include <sys/socket.h> 35 #include <sys/un.h> 36 #include <errno.h> 37 38 #include <pthread.h> 34 39 35 40 #include "common.h" 41 #include "misc.h" 36 42 37 43 #define DUMP_BOOL(x) 0 == x ? "false" : "true" … … 48 54 extern void rtmp_proto_impl_set(const int32_t val); 49 55 extern void flv2mpeg4_converter_set(const int32_t val); 56 extern void sel_program_id_set(const int32_t val); 50 57 51 58 extern void pcm_resampling_set(int32_t val); … … 61 68 static Context_t *g_player = NULL; 62 69 70 static 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 87 static int g_pfd[2] = {-1, -1}; /* Used to wake terminate thread */ 88 static int isPlaybackStarted = 0; 89 static pthread_mutex_t playbackStartMtx; 90 91 static 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 155 finish: 156 close(cl); 157 close(fd); 158 pthread_exit(NULL); 159 160 } 161 63 162 static void map_inter_file_path(char *filename) 64 163 { … … 84 183 static int kbhit(void) 85 184 { 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 { 105 196 return 0; 197 } 198 199 if(FD_ISSET(0,&read_fd)) 200 { 201 return 1; 202 } 203 204 return 0; 106 205 } 107 206 … … 277 376 int aopt = 0, bopt = 0; 278 377 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) 280 379 { 281 380 switch (c) … … 328 427 SetNice(atoi(optarg)); 329 428 break; 429 case 'P': 430 sel_program_id_set(atoi(optarg)); 431 break; 330 432 case 't': 331 433 *pAudioTrackIdx = atoi(optarg); … … 412 514 int main(int argc, char* argv[]) 413 515 { 516 pthread_t termThread; 517 int isTermThreadStarted = 0; 414 518 char file[IPTV_MAX_FILE_PATH]; 415 519 memset(file, '\0', sizeof(file)); … … 425 529 int commandRetVal = -1; 426 530 /* inform client that we can handle additional commands */ 427 fprintf(stderr, "{\"EPLAYER3_EXTENDED\":{\"version\":%d}}\n", 3 4);531 fprintf(stderr, "{\"EPLAYER3_EXTENDED\":{\"version\":%d}}\n", 36); 428 532 429 533 if (0 != ParseParams(argc, argv, file, audioFile, &audioTrackIdx, &subtitleTrackIdx)) … … 446 550 printf("[-o 0|1] set progressive download\n"); 447 551 printf("[-p value] nice value\n"); 552 printf("[-P value] select Program ID from multi-service stream\n"); 448 553 printf("[-t id] audio track ID switched on at start\n"); 449 554 printf("[-9 id] subtitle track ID switched on at start\n"); … … 465 570 exit(1); 466 571 } 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); 467 601 468 602 g_player->playback = &PlaybackHandler; … … 505 639 506 640 { 641 pthread_mutex_lock(&playbackStartMtx); 642 isPlaybackStarted = 1; 643 pthread_mutex_unlock(&playbackStartMtx); 644 507 645 commandRetVal = g_player->output->Command(g_player, OUTPUT_OPEN, NULL); 508 646 fprintf(stderr, "{\"OUTPUT_OPEN\":{\"sts\":%d}}\n", commandRetVal); … … 757 895 free(g_player); 758 896 } 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]); 759 907 760 908 //printOutputCapabilities();
Note: See TracChangeset
for help on using the changeset viewer.