• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 Amlogic, Inc. All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
12  * more details.
13  *
14  */
15 
16 /**
17 * @file kplayer.c
18 * @brief
19 * @author Xu Hui <hui.xu@amlogic.com>
20 * @version 1.0.0
21 * @date 2011-02-28
22 */
23 /*!< Copyright (C) 2007-2011, Amlogic Inc.
24 * All right reserved
25 *
26 */
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <stdio.h>
30 #include <unistd.h>
31 #include <signal.h>
32 #include <string.h>
33 #include <ctype.h>
34 #include <errno.h>
35 
36 #include "player.h"
37 //#include "player_type.h"
38 //#include "streamsource.h"
39 typedef enum {
40     //EMU_STEP_NONE = 0,
41     EMU_STEP_PAUSE = 2,
42     EMU_STEP_RESUME = 3,
43     EMU_STEP_START = 4,
44     EMU_STEP_FF = 5,
45     EMU_STEP_RR = 6,
46     EMU_STEP_SEEK = 7,
47     //EMU_STEP_MUTE= 8,
48     //EMU_STEP_SETVOL=9,
49     //EMU_STEP_GETVOL = 10,
50     //EMU_STEP_SETTONE= 11,
51     EMU_STEP_SETLOOP = 14,
52     EMU_STEP_STOP = 16,
53     //EMU_STEP_SPECTRUM = 17,
54     //EMU_STEP_SETSUBT = 19,
55     EMU_STEP_MENU = 20,
56     EMU_STEP_EXIT = 21,
57     //EMU_STEP_ATRACK = 22,
58     EMU_STEP_GETAVMEDIAINFO = 25,
59     //EMU_STEP_LISTALLMEDIAID = 27,
60 } EMU_STEP;
61 #define SCREEN_SPLITER            "***************************************************************************\r\n"
62 
amsysfs_set_sysfs_int(const char * path,int val)63 static int amsysfs_set_sysfs_int(const char *path, int val)
64 {
65     int fd;
66     int bytes;
67     char  bcmd[16];
68     fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
69     if (fd >= 0) {
70         sprintf(bcmd, "%d", val);
71         bytes = write(fd, bcmd, strlen(bcmd));
72         close(fd);
73         return 0;
74     } else {
75         printf("unable to open file %s,err: %s\n", path, strerror(errno));
76     }
77     return -1;
78 }
79 
amsysfs_set_sysfs_str(const char * path,const char * val)80 static int amsysfs_set_sysfs_str(const char *path, const char *val)
81 {
82     int fd;
83     int bytes;
84     fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
85     if (fd >= 0) {
86         bytes = write(fd, val, strlen(val));
87         close(fd);
88         return 0;
89     } else {
90         printf("unable to open file %s,err: %s\n", path, strerror(errno));
91     }
92     return -1;
93 }
94 
amsysfs_get_sysfs_str(const char * path,char * valstr,int size)95 static int  amsysfs_get_sysfs_str(const char *path, char *valstr, int size)
96 {
97     int fd;
98     fd = open(path, O_RDONLY);
99     if (fd >= 0) {
100         memset(valstr, 0, size);
101         read(fd, valstr, size - 1);
102         valstr[strlen(valstr)] = '\0';
103         close(fd);
104     } else {
105         printf("unable to open file %s,err: %s\n", path, strerror(errno));
106         sprintf(valstr, "%s", "fail");
107         return -1;
108     };
109     //LOGI("get_sysfs_str=%s\n", valstr);
110     return 0;
111 }
112 
113 
114 static int axis[8] = {0};
115 
update_player_info(int pid,player_info_t * info)116 int update_player_info(int pid, player_info_t * info)
117 {
118     printf("pid:%d,status:%x,current pos:%d,total:%d,errcode:%x\n", pid, info->status, info->current_time, info->full_time, ~(info->error_no));
119     return 0;
120 }
_media_info_dump(media_info_t * minfo)121 int _media_info_dump(media_info_t* minfo)
122 {
123     int i = 0;
124     printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
125     printf("======||file size:%lld\n", minfo->stream_info.file_size);
126     printf("======||file type:%d\n", minfo->stream_info.type);
127     printf("======||has internal subtitle?:%s\n", minfo->stream_info.has_sub > 0 ? "YES!" : "NO!");
128     printf("======||internal subtile counts:%d\n", minfo->stream_info.total_sub_num);
129     printf("======||has video track?:%s\n", minfo->stream_info.has_video > 0 ? "YES!" : "NO!");
130     printf("======||has audio track?:%s\n", minfo->stream_info.has_audio > 0 ? "YES!" : "NO!");
131     printf("======||duration:%d\n", minfo->stream_info.duration);
132     if (minfo->stream_info.has_video && minfo->stream_info.total_video_num > 0) {
133         printf("======||video counts:%d\n", minfo->stream_info.total_video_num);
134         printf("======||video width:%d\n", minfo->video_info[0]->width);
135         printf("======||video height:%d\n", minfo->video_info[0]->height);
136         printf("======||video bitrate:%d\n", minfo->video_info[0]->bit_rate);
137         printf("======||video format:%d\n", minfo->video_info[0]->format);
138 
139     }
140     if (minfo->stream_info.has_audio && minfo->stream_info.total_audio_num > 0) {
141         printf("======||audio counts:%d\n", minfo->stream_info.total_audio_num);
142 
143         if (NULL != minfo->audio_info[0]->audio_tag) {
144             printf("======||track title:%s", minfo->audio_info[0]->audio_tag->title);
145             printf("\n======||track album:%s", minfo->audio_info[0]->audio_tag->album);
146             printf("\n======||track author:%s\n", minfo->audio_info[0]->audio_tag->author);
147             printf("\n======||track year:%s\n", minfo->audio_info[0]->audio_tag->year);
148             printf("\n======||track comment:%s\n", minfo->audio_info[0]->audio_tag->comment);
149             printf("\n======||track genre:%s\n", minfo->audio_info[0]->audio_tag->genre);
150             printf("\n======||track copyright:%s\n", minfo->audio_info[0]->audio_tag->copyright);
151             printf("\n======||track track:%d\n", minfo->audio_info[0]->audio_tag->track);
152         }
153 
154 
155 
156         for (i = 0; i < minfo->stream_info.total_audio_num; i++) {
157             printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
158             printf("======||%d 'st audio track codec type:%d\n", i, minfo->audio_info[i]->aformat);
159             printf("======||%d 'st audio track audio_channel:%d\n", i, minfo->audio_info[i]->channel);
160             printf("======||%d 'st audio track bit_rate:%d\n", i, minfo->audio_info[i]->bit_rate);
161             printf("======||%d 'st audio track audio_samplerate:%d\n", i, minfo->audio_info[i]->sample_rate);
162             printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
163 
164         }
165 
166     }
167     if (minfo->stream_info.has_sub && minfo->stream_info.total_sub_num > 0) {
168         for (i = 0; i < minfo->stream_info.total_sub_num; i++) {
169             if (0 == minfo->sub_info[i]->internal_external) {
170                 printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
171                 printf("======||%d 'st internal subtitle pid:%d\n", i, minfo->sub_info[i]->id);
172                 printf("======||%d 'st internal subtitle language:%s\n", i, minfo->sub_info[i]->sub_language);
173                 printf("======||%d 'st internal subtitle width:%d\n", i, minfo->sub_info[i]->width);
174                 printf("======||%d 'st internal subtitle height:%d\n", i, minfo->sub_info[i]->height);
175                 printf("======||%d 'st internal subtitle resolution:%d\n", i, minfo->sub_info[i]->resolution);
176                 printf("======||%d 'st internal subtitle subtitle size:%lld\n", i, minfo->sub_info[i]->subtitle_size);
177                 printf("^^^^^^^^^^^^^^^^^^^^^^^^^^^^^\n");
178             }
179         }
180     }
181     printf("@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@\n");
182     return 0;
183 }
184 
get_axis(const char * para,int para_num,int * result)185 static int get_axis(const char *para, int para_num, int *result)
186 {
187     char *endp;
188     const char *startp = para;
189     int *out = result;
190     int len = 0, count = 0;
191 
192     if (!startp) {
193         return 0;
194     }
195 
196     len = strlen(startp);
197 
198     do {
199         //filter space out
200         while (startp && (isspace(*startp) || !isgraph(*startp)) && len) {
201             startp++;
202             len--;
203         }
204 
205         if (len == 0) {
206             break;
207         }
208 
209         *out++ = strtol(startp, &endp, 0);
210 
211         len -= endp - startp;
212         startp = endp;
213         count++;
214 
215     } while ((endp) && (count < para_num) && (len > 0));
216 
217     return count;
218 }
219 
__set_display_axis(int recovery)220 static int __set_display_axis(int recovery)
221 {
222     int fd;
223     char *path = "/sys/class/display/axis";
224     char str[128];
225     int count;
226     fd = open(path, O_CREAT | O_RDWR | O_TRUNC, 0644);
227     if (fd >= 0) {
228         if (!recovery) {
229             read(fd, str, 128);
230             printf("read axis %s, length %d\n", str, strlen(str));
231             count = get_axis(str, 8, axis);
232         }
233         if (recovery) {
234             sprintf(str, "%d %d %d %d %d %d %d %d",
235                     axis[0], axis[1], axis[2], axis[3], axis[4], axis[5], axis[6], axis[7]);
236         } else {
237             sprintf(str, "2048 %d %d %d %d %d %d %d",
238                     axis[1], axis[2], axis[3], axis[4], axis[5], axis[6], axis[7]);
239         }
240         write(fd, str, strlen(str));
241         close(fd);
242         return 0;
243     }
244 
245     return -1;
246 }
247 
set_osd_blank(int blank)248 static int set_osd_blank(int blank)
249 {
250     char *drm_osd_enable = "/sys/class/meson_crtc/enable_osd";
251     char *path1 = "/sys/class/graphics/fb0/blank";
252     char *path2 = "/sys/class/graphics/fb1/blank";
253     char *wl_path0 = "/sys/kernel/debug/dri/0/vpu/blank";
254     char *wl_path1 = "/sys/kernel/debug/dri/64/vpu/blank";
255 
256     char *fb_stat0 = "/sys/class/graphics/fb0/osd_status";
257     char *fb_stat1 = "/sys/class/graphics/fb1/osd_status";
258 
259     static int fb0_enable = -1;
260     static int fb1_enable = -1;
261     static int use_wayland = 0;
262     char osd_status[128]="";
263 
264     if (amsysfs_get_sysfs_str(drm_osd_enable, osd_status, sizeof(osd_status)) == 0) {
265         amsysfs_set_sysfs_str(drm_osd_enable, blank ? "0" : "1");
266         return 0;
267     }
268 
269     if (fb0_enable == -1)
270     {
271         osd_status[0]='\0';
272         if ( !use_wayland && (amsysfs_get_sysfs_str(fb_stat0, osd_status, 128) == 0))
273         {
274             if (strstr(osd_status, "enable: 1"))
275                 fb0_enable = 1;
276             else
277                 fb0_enable = 0;
278         } else if (amsysfs_get_sysfs_str(wl_path0, osd_status, 128) == 0)
279         {
280             use_wayland = 1;
281             if (strstr(osd_status, "blank_enable: 0"))
282                 fb0_enable = 1;
283             else
284                 fb0_enable = 0;
285         }
286         else
287             fb0_enable = 1; //In case the osd_status node not exist
288 
289     }
290     if (fb1_enable == -1)
291     {
292         osd_status[0]='\0';
293         if (!use_wayland && (amsysfs_get_sysfs_str(fb_stat1, osd_status, 32) == 0))
294         {
295             if (strstr(osd_status, "enable: 1"))
296                 fb1_enable = 1;
297             else
298                 fb1_enable = 0;
299         } else if (amsysfs_get_sysfs_str(wl_path1, osd_status, 128) == 0)
300         {
301             use_wayland = 1;
302             if (strstr(osd_status, "blank_enable: 0"))
303                 fb0_enable = 1;
304             else
305                 fb0_enable = 0;
306         }
307         else
308             fb1_enable = 1; //In case the osd_status node not exist
309     }
310 
311     if (fb0_enable)
312         amsysfs_set_sysfs_str(use_wayland ? wl_path0 : path1, blank ? "1" : "0");
313 
314     if (fb1_enable)
315         amsysfs_set_sysfs_str(use_wayland ? wl_path1 : path2, blank ? "1" : "0");
316     return 0;
317 }
318 
set_vfm_path(void)319 static void set_vfm_path(void)
320 {
321     char *vfm_path = "/sys/class/vfm/map";
322     char *vfm_rm = "rm default";
323     char *vfm_add = "add default decoder ppmgr deinterlace amvideo";
324 
325     amsysfs_set_sysfs_str(vfm_path, vfm_rm);
326     amsysfs_set_sysfs_str(vfm_path, vfm_add);
327 }
328 
signal_handler(int signum)329 static void signal_handler(int signum)
330 {
331     printf("Get signum=%x\n", signum);
332    // player_progress_exit();
333    // __set_display_axis(1);
334     set_black_policy(1);
335     set_osd_blank(0);
336     signal(signum, SIG_DFL);
337     raise(signum);
338 }
339 
set_video_display(int val)340 static void set_video_display(int val)
341 {
342     char *disable_path = "/sys/class/video/disable_video";
343 
344     amsysfs_set_sysfs_int(disable_path, val);
345 }
346 
347 #define TMP_COMMAND_MAX 512
348 
main(int argc,char * argv[])349 int main(int argc, char *argv[])
350 {
351     play_control_t *pCtrl = NULL;
352     int pid;
353     int pos = 0;
354     int speed = 0;
355     int tmpneedexit = 0;
356     int ret = -1;
357     int flag = 1;
358     media_info_t minfo;
359     char tmpcommand[TMP_COMMAND_MAX];
360     EMU_STEP tmpstep = EMU_STEP_MENU;
361 
362     pCtrl = (play_control_t*)malloc(sizeof(play_control_t));
363     memset(pCtrl, 0, sizeof(play_control_t));
364     memset(&minfo, 0, sizeof(media_info_t));
365     if (argc < 2) {
366         printf("usage:player file\n");
367         return -1;
368     }
369 
370     /*set default path for vfm.*/
371     set_vfm_path();
372     /*enable video layer*/
373     set_video_display(2);
374 
375     player_init();
376     //__set_display_axis(0);        //move osd out of screen to set video layer out
377 
378     player_register_update_callback(&pCtrl->callback_fn, &update_player_info, 1000);
379     printf("player callback register....\n");
380 
381     pCtrl->file_name = strdup(argv[1]);
382 
383     //pCtrl->nosound = 1;   // if disable audio...,must call this api
384     pCtrl->video_index = -1;// MUST
385     pCtrl->audio_index = -1;// MUST
386     pCtrl->sub_index = -1;/// MUST
387     pCtrl->hassub = 1;  // enable subtitle
388 
389     //just open a buffer,just for p2p,http,etc...
390     pCtrl->auto_buffing_enable = 1;
391     pCtrl->buffing_min = 0.001;
392     pCtrl->buffing_middle = 0.02;
393     pCtrl->buffing_max = 0.9;
394 
395     pCtrl->t_pos = -1;  // start position, if live streaming, need set to -1
396     pCtrl->need_start = 0; // if 0,you can omit player_start_play API.just play video/audio immediately. if 1,need call "player_start_play" API;
397 
398     pid = player_start(pCtrl, 0);
399     if (pid < 0) {
400         printf("player start failed!error=%d\n", pid);
401         return -1;
402     }
403     signal(SIGSEGV, signal_handler);
404     signal(SIGINT, signal_handler);
405     //SYS_disable_osd0();
406     set_osd_blank(1);
407 
408     while ((!tmpneedexit) && (!PLAYER_THREAD_IS_STOPPED(player_get_state(pid)))) {
409         switch (tmpstep) {
410         case EMU_STEP_PAUSE:
411             player_pause(pid);
412             tmpstep = EMU_STEP_MENU;
413             break;
414         case EMU_STEP_RESUME:
415             player_resume(pid);
416             tmpstep = EMU_STEP_MENU;
417             break;
418         case EMU_STEP_SEEK:
419             pos = 0;
420             printf("please input seek pos:");
421             scanf("%d", &pos);
422 
423             printf("will  seek position: %d\n", pos);
424             player_timesearch(pid, pos);
425             tmpstep = EMU_STEP_MENU;
426             break;
427 
428             break;
429         case EMU_STEP_STOP:
430             player_stop(pid);
431             tmpstep = EMU_STEP_MENU;
432             break;
433         case EMU_STEP_FF:
434             printf("please input fastforward speed:\n");
435             speed = 1;
436             player_forward(pid, speed);
437             tmpstep = EMU_STEP_MENU;
438             break;
439         case EMU_STEP_RR:
440             printf("please input fastrewind speed:");
441             speed = 1;
442             player_backward(pid, speed);
443             tmpstep = EMU_STEP_MENU;
444             break;
445         case EMU_STEP_SETLOOP:
446             player_loop(pid);
447             tmpstep = EMU_STEP_MENU;
448             break;
449         case EMU_STEP_EXIT:
450             player_exit(pid);
451             tmpneedexit = 1;
452             break;
453         case EMU_STEP_START:
454             player_start_play(pid);
455             //SYS_set_tsync_enable(0);///< f no sound,can set to be 0
456             tmpstep = EMU_STEP_MENU;
457             break;
458         case EMU_STEP_GETAVMEDIAINFO:
459             if (pid >= 0) {
460                 if (player_get_state(pid) > PLAYER_INITOK) {
461                     ret = player_get_media_info(pid, &minfo);
462                     if (ret == 0) {
463                         _media_info_dump(&minfo);
464                     }
465                 }
466             }
467             tmpstep = EMU_STEP_MENU;
468             break;
469         case EMU_STEP_MENU:
470             do {
471                 if (flag) {
472                     printf(SCREEN_SPLITER);
473                     printf("       	     player benchmark tool for android            v2.0\n");
474                     printf(SCREEN_SPLITER);
475                     printf("* Please choose one option                                 *\r\n");
476                     printf("* 0   show main menu                                       *\r\n");
477                     printf("* a   start play                                           *\r\n");
478                     printf("* s   get media info                                       *\r\n");
479                     printf("* 1   Pause play                                           *\r\n");
480                     printf("* 2   Resume play                                          *\r\n");
481                     printf("* 3   Stop play                                            *\r\n");
482                     printf("* 4   Fastforward                                          *\r\n");
483                     printf("* 5   Fastrewind                                       	   *\r\n");
484                     printf("* 6   Seek                                             	   *\r\n");
485                     printf("* 7   Set repeat                                           *\r\n");
486                     printf("* 8   Quit tools                                           *\r\n");
487                     printf(SCREEN_SPLITER);
488                     printf("please input you choice:");
489                 }
490                 flag = 1;
491                 memset(tmpcommand, 0, TMP_COMMAND_MAX);
492 
493                 scanf("%s", tmpcommand);
494 
495                 if (strcmp(tmpcommand, "0") == 0) {
496                     sleep(1);
497                     tmpstep = EMU_STEP_MENU;
498                 } else if (strcmp(tmpcommand, "1") == 0) {
499                     tmpstep = EMU_STEP_PAUSE;
500                 } else if (strcmp(tmpcommand, "2") == 0) {
501                     tmpstep = EMU_STEP_RESUME;
502                 } else if (strcmp(tmpcommand, "3") == 0) {
503                     tmpstep = EMU_STEP_STOP;
504                 } else if (strcmp(tmpcommand, "4") == 0) {
505                     tmpstep = EMU_STEP_FF;
506                 } else if (strcmp(tmpcommand, "5") == 0) {
507                     tmpstep = EMU_STEP_RR;
508                 } else if (strcmp(tmpcommand, "6") == 0) {
509                     tmpstep = EMU_STEP_SEEK;
510                 } else if (strcmp(tmpcommand, "7") == 0) {
511                     tmpstep = EMU_STEP_SETLOOP;
512                 } else if (strcmp(tmpcommand, "8") == 0) {
513                     tmpstep = EMU_STEP_EXIT;
514                 } else if (strcmp(tmpcommand, "a") == 0) {
515                     tmpstep = EMU_STEP_START;
516                 } else if (strcmp(tmpcommand, "s") == 0) {
517                     tmpstep = EMU_STEP_GETAVMEDIAINFO;
518                 } else {
519                     flag = 0;
520                 }
521             } while (0);
522 
523             break;
524         }
525         usleep(100 * 1000);
526         signal(SIGCHLD, SIG_IGN);
527         signal(SIGTSTP, SIG_IGN);
528         signal(SIGTTOU, SIG_IGN);
529         signal(SIGTTIN, SIG_IGN);
530         signal(SIGHUP, signal_handler);
531         signal(SIGTERM, signal_handler);
532         signal(SIGSEGV, signal_handler);
533         signal(SIGQUIT, signal_handler);
534     }
535     __set_display_axis(1);    //recover osd
536     set_osd_blank(0);
537     free(pCtrl->file_name);
538     free(pCtrl);
539     printf("...........player exit,~,byeybe...........\n");
540     return 0;
541 }
542 
543