-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
2146 lines (1850 loc) · 61.2 KB
/
player.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <AVServer.h>
#include <unistd.h>
#include <hi_adp_mpi.h>
#include <hi_common.h>
#include <hi_unf_avplay.h>
#include <hi_unf_descrambler.h>
#include <hi_unf_demux.h>
#include <hi_unf_ecs.h>
#include <hi_unf_keyled.h>
#include <hi_unf_sound.h>
#include <hi_unf_vo.h>
#include <hi_mpi_ao.h>
#include <hi_mpi_avplay.h>
#include <hi_mpi_sync.h>
#include <hi_mpi_vdec.h>
#include <hi_mpi_win.h>
#include <hi_video_codec.h>
/* Audio Includes */
#include <HA.AUDIO.MP3.decode.h>
#include <HA.AUDIO.MP2.decode.h>
#include <HA.AUDIO.AAC.decode.h>
#include <HA.AUDIO.DRA.decode.h>
#include <HA.AUDIO.PCM.decode.h>
#include <HA.AUDIO.WMA9STD.decode.h>
#include <HA.AUDIO.TRUEHDPASSTHROUGH.decode.h>
#include <HA.AUDIO.DOLBYTRUEHD.decode.h>
#include <HA.AUDIO.DTSHD.decode.h>
#include <HA.AUDIO.DOLBYPLUS.decode.h>
#include <HA.AUDIO.AC3PASSTHROUGH.decode.h>
#include <HA.AUDIO.DTSM6.decode.h>
#include <HA.AUDIO.DTSPASSTHROUGH.decode.h>
#include <HA.AUDIO.FFMPEG_DECODE.decode.h>
#define AUDIO_STREAMTYPE_AC3 0
#define AUDIO_STREAMTYPE_MPEG 1
#define AUDIO_STREAMTYPE_DTS 2
#define AUDIO_STREAMTYPE_LPCM 6
#define AUDIO_STREAMTYPE_AAC 8
#define AUDIO_STREAMTYPE_AACHE 9
#define AUDIO_STREAMTYPE_MP3 10
#define AUDIO_STREAMTYPE_AACPLUS 11
#define AUDIO_STREAMTYPE_DTSHD 16
#define AUDIO_STREAMTYPE_DDP 34
#define AUDIO_STREAMTYPE_RAW 48
#define VIDEO_STREAMTYPE_MPEG2 0
#define VIDEO_STREAMTYPE_MPEG4_H264 1
#define VIDEO_STREAMTYPE_H263 2
#define VIDEO_STREAMTYPE_VC1 3
#define VIDEO_STREAMTYPE_MPEG4_Part2 4
#define VIDEO_STREAMTYPE_VC1_SM 5
#define VIDEO_STREAMTYPE_MPEG1 6
#define VIDEO_STREAMTYPE_H265_HEVC 7
#define VIDEO_STREAMTYPE_VP8 8
#define VIDEO_STREAMTYPE_VP9 9
#define VIDEO_STREAMTYPE_XVID 10
#define VIDEO_STREAMTYPE_DIVX311 13
#define VIDEO_STREAMTYPE_DIVX4 14
#define VIDEO_STREAMTYPE_DIVX5 15
#define AUDIO_STREAM_S 0xC0
#define AUDIO_STREAM_E 0xDF
#define VIDEO_STREAM_S 0xE0
#define VIDEO_STREAM_E 0xEF
#define MAX_ADAPTER 4
#define PLAYER_DEMUX_PORT 4
static struct class_ops player_ops;
static unsigned char u8DecOpenBuf[1024];
#if defined(HAVE_AVCODEC)
/* This is for use libavcodec.so.56 of Hisilicon HiPlayer.
* Is necessary for codecs: libHA.AUDIO.FFMPEG_ADEC.decode.so
* libHA.AUDIO.FFMPEG_WMAPRO.decode.so
* libHV.VIDEO.FFMPEG_VDEC.decode.so
*/
/** extradata **/
#define EXTRADATA_SIZE 14
#ifndef AV_RB32
# define AV_RB32(x) \
((((const unsigned char*)(x))[0] << 24) | \
(((const unsigned char*)(x))[1] << 16) | \
(((const unsigned char*)(x))[2] << 8) | \
((const unsigned char*)(x))[3])
#endif
#ifndef AV_WB32
# define AV_WB32(p, d) do { \
((unsigned char*)(p))[3] = (d); \
((unsigned char*)(p))[2] = (d)>>8; \
((unsigned char*)(p))[1] = (d)>>16; \
((unsigned char*)(p))[0] = (d)>>24; \
} while(0)
#endif
/** AVCodecContext **/
#ifndef AV_RL32
# define AV_RL32(x) \
((((const unsigned char*)(x))[3] << 24) | \
(((const unsigned char*)(x))[2] << 16) | \
(((const unsigned char*)(x))[1] << 8) | \
((const unsigned char*)(x))[0])
#endif
#ifndef AV_WL32
# define AV_WL32(p, d) do { \
((unsigned char*)(p))[0] = (d); \
((unsigned char*)(p))[1] = (d)>>8; \
((unsigned char*)(p))[2] = (d)>>16; \
((unsigned char*)(p))[3] = (d)>>24; \
} while(0)
#endif
/* Set dummy extradata */
void avcodec_set_extradata(void *context, int channels, int bit_rate, int sample_rate) {
unsigned char *extradata = malloc(EXTRADATA_SIZE);
if (!context || !extradata) {
if (extradata)
free(extradata);
return;
}
/* Set dummy extradata */
AV_WB32(extradata, channels); // Channels
AV_WB32(extradata + 4, bit_rate); // BitRate
AV_WB32(extradata + 8, sample_rate); // SampleRate
/* Set Extradata in AVCodecContext */
AV_WL32(context + 96, (int)extradata);
AV_WL32(context + 100, EXTRADATA_SIZE);
}
void avcodec_set_context_info(void *context, int channels, int bit_rate, int sample_rate) {
if (!context)
return;
/* Set Extradata in AVCodecContext */
AV_WL32(context + 416, channels);
AV_WL32(context + 72, bit_rate);
AV_WL32(context + 412, sample_rate);
}
extern void avcodec_register_all(void);
extern void *avcodec_find_decoder_by_name(const char *name);
extern void *avcodec_alloc_context3(void *codec);
extern void avcodec_free_context(void **avctx);
static void *avcodec_context = NULL;
#endif
struct s_player {
bool IsCreated;
int PlayerMode; /* 0 demux, 1 memory */
int AudioPid; /* unknown pid */
int AudioType;
int AudioChannel; /* 0 stereo, 1 left, 2 right */
int AudioState; /* 0 stoped, 1 playing, 2 paused */
int VideoPid; /* unknown pid */
int VideoType;
int VideoState; /* 0 stoped, 1 playing, 2 freezed */
int VideoFormat; /* 0 4:3, 1 16:9, 2 2.21:1 */
int DisplayFormat; /* 0 Pan&Scan, 1 Letterbox, 2 Center Cut Out */
bool IsPES;
bool IsDVR;
bool IsTimeShift;
bool IsBlank;
bool IsStopThread;
bool IsSyncEnabled;
bool IsMute;
int AudioBufferState;
int VideoBufferState;
unsigned int hPlayer;
unsigned int hWindow;
unsigned int hTrack;
unsigned int hTsBuffer;
unsigned int hVdec;
unsigned int hSync;
unsigned poll_status;
unsigned event_status;
pthread_mutex_t m_apts;
pthread_mutex_t m_vpts;
pthread_mutex_t m_event;
pthread_mutex_t m_poll;
pthread_rwlock_t m_write;
struct {
bool active;
int type;
int width;
int heigth;
int aspect;
unsigned int framerate;
bool progressive;
} events[3];
struct {
char header[256];
size_t size;
size_t es_size;
long long pts;
} p2e[2];
struct fuse_pollhandle *poll_handle[2];
};
void player_showtime(void)
{
HI_UNF_KEYLED_TIME_S stTime;
time_t _tm = time(NULL);
struct tm *curtime = localtime(&_tm);
stTime.u32Hour = curtime->tm_hour;
stTime.u32Minute = curtime->tm_min;
if (HI_UNF_LED_DisplayTime(stTime) != HI_SUCCESS)
printf("[ERROR] %s: Time not writed to keyled.\n", __FUNCTION__);
}
void player_create_painel(void)
{
if (HI_UNF_KEYLED_Init() != HI_SUCCESS)
{
printf("[ERROR] %s: Failed to create painel.\n", __FUNCTION__);
return;
}
int ret = HI_UNF_KEYLED_SelectType(HI_UNF_KEYLED_TYPE_FD650);
ret |= HI_UNF_LED_Open();
ret |= HI_UNF_LED_SetFlashFreq(HI_UNF_KEYLED_LEVEL_1);
ret |= HI_UNF_LED_SetFlashPin(HI_UNF_KEYLED_LIGHT_ALL);
if (ret == HI_SUCCESS)
player_showtime();
}
void player_set_keyhandler(HI_HANDLE hPChannel, int pid)
{
int i;
for (i = 0; i < MAX_ADAPTER; i++)
{
HI_HANDLE hChannel;
if (HI_UNF_DMX_GetChannelHandle(i, (HI_U32)pid, &hChannel) == HI_SUCCESS)
{
HI_HANDLE hKey;
if (HI_UNF_DMX_GetDescramblerKeyHandle(hChannel, &hKey) == HI_SUCCESS)
{
HI_HANDLE hPKey;
if (HI_UNF_DMX_GetDescramblerKeyHandle(hPChannel, &hPKey) == HI_SUCCESS)
if (hPKey != hKey)
if (HI_UNF_DMX_DetachDescrambler(hPKey, hPChannel) != HI_SUCCESS)
printf("[ERROR] %s: Failed to detach KeyHandle from Player.\n", __FUNCTION__);
if (HI_UNF_DMX_AttachDescrambler(hKey, hPChannel) != HI_SUCCESS)
printf("[ERROR] %s: Failed to attach KeyHandle to PID %d.\n", __FUNCTION__, pid);
}
break;
}
}
}
int player_event_handler(HI_HANDLE handle, HI_UNF_AVPLAY_EVENT_E enEvent, HI_VOID *para)
{
struct s_player *player = (struct s_player *)player_ops.priv;
if (enEvent == HI_UNF_AVPLAY_EVENT_NEW_VID_FRAME)
{
int width;
int heigth;
int aspect;
int framerate;
bool progressive;
FILE *file = NULL;
HI_UNF_VIDEO_FRAME_INFO_S *vFrame = (HI_UNF_VIDEO_FRAME_INFO_S *)para;
/** VIDEO_EVENT_SIZE_CHANGED **/
width = vFrame->u32Width;
heigth = vFrame->u32Height;
if (vFrame->u32AspectWidth == 4 && vFrame->u32AspectHeight == 3)
aspect = VIDEO_FORMAT_4_3;
else if (vFrame->u32AspectWidth == 221 && vFrame->u32AspectHeight == 1)
aspect = VIDEO_FORMAT_221_1;
else /* if (vFrame->u32AspectWidth == 16 && vFrame->u32AspectHeight == 9) */
aspect = VIDEO_FORMAT_16_9;
/** VIDEO_EVENT_FRAME_RATE_CHANGED **/
if (vFrame->enFieldMode == HI_UNF_VIDEO_FIELD_TOP || vFrame->enFieldMode == HI_UNF_VIDEO_FIELD_BOTTOM)
framerate = (vFrame->stFrameRate.u32fpsInteger * 100 + vFrame->stFrameRate.u32fpsDecimal / 10) * 2;
else
framerate = vFrame->stFrameRate.u32fpsInteger * 100 + vFrame->stFrameRate.u32fpsDecimal / 10;
framerate *= 10; // Fix for Enigma2
/** VIDEO_EVENT_PROGRESSIVE_CHANGED **/
progressive = vFrame->bProgressive == HI_FALSE ? true : false;
/** Check changes. **/
/** VIDEO_EVENT_SIZE_CHANGED **/
if (!player->events[0].active ||
player->events[0].width != width ||
player->events[0].heigth != heigth)
{
pthread_mutex_lock(&player->m_event);
player->events[0].active = true;
player->events[0].width = width;
player->events[0].heigth = heigth;
player->events[0].aspect = player->VideoFormat = aspect;
player->event_status |= (1 << player->events[0].type);
pthread_mutex_unlock(&player->m_event);
}
/** VIDEO_EVENT_FRAME_RATE_CHANGED **/
if (!player->events[1].active ||
player->events[1].framerate != framerate)
{
pthread_mutex_lock(&player->m_event);
player->events[1].active = true;
player->events[1].framerate = framerate;
player->event_status |= (1 << player->events[1].type);
pthread_mutex_unlock(&player->m_event);
}
/** VIDEO_EVENT_PROGRESSIVE_CHANGED **/
if (!player->events[2].active ||
player->events[2].progressive != progressive)
{
pthread_mutex_lock(&player->m_event);
player->events[2].active = true;
player->events[2].progressive = progressive;
player->event_status |= (1 << player->events[2].type);
pthread_mutex_unlock(&player->m_event);
}
/** Fix Auto-change of 4:3 <--> 16:9 **/
file = fopen((aspect == VIDEO_FORMAT_4_3) ? "/proc/stb/video/policy" : "/proc/stb/video/policy2", "r");
if (file)
{
char mode[10];
HI_UNF_WINDOW_ATTR_S pAttr;
fgets(mode, 10, file);
fclose(file);
if (HI_UNF_VO_GetWindowAttr(player->hWindow, &pAttr) == HI_SUCCESS)
{
unsigned int h, w;
HI_UNF_VO_ASPECT_CVRS_E aspect = HI_UNF_VO_ASPECT_CVRS_IGNORE; /* Valid for bestfit/non/nonlinear/scale */
if (strEquals(mode, "letterbox", false))
aspect = HI_UNF_VO_ASPECT_CVRS_LETTERBOX;
else if (strEquals(mode, "panscan", false))
aspect = HI_UNF_VO_ASPECT_CVRS_PAN_SCAN;
pAttr.bVirtual = HI_FALSE;
pAttr.bUseCropRect = HI_FALSE;
pAttr.stOutputRect.s32X = 0;
pAttr.stOutputRect.s32Y = 0;
if (HI_UNF_DISP_GetVirtualScreen(HI_UNF_DISPLAY1, &w, &h) == HI_SUCCESS)
{
w -= 2;
h -= 4;
if (pAttr.stOutputRect.s32Width != w || pAttr.stOutputRect.s32Height != h || pAttr.stWinAspectAttr.enAspectCvrs != aspect)
{
pAttr.stOutputRect.s32Width = w;
pAttr.stOutputRect.s32Height= h;
pAttr.stWinAspectAttr.enAspectCvrs = aspect;
HI_UNF_VO_SetWindowAttr(player->hWindow, &pAttr);
}
}
}
}
}
else if (player->IsPES)
{
pthread_mutex_lock(&player->m_poll);
switch (enEvent)
{
case HI_UNF_AVPLAY_EVENT_AUD_BUF_STATE:
switch ((HI_UNF_AVPLAY_BUF_STATE_E)para)
{
case HI_UNF_AVPLAY_BUF_STATE_EMPTY:
case HI_UNF_AVPLAY_BUF_STATE_LOW:
case HI_UNF_AVPLAY_BUF_STATE_NORMAL:
case HI_UNF_AVPLAY_BUF_STATE_HIGH:
if (player->poll_status & (1 << DEV_AUDIO))
{
struct fuse_pollhandle *ph = player->poll_handle[DEV_AUDIO];
fuse_notify_poll(ph);
fuse_pollhandle_destroy(ph);
player->poll_handle[DEV_AUDIO] = NULL;
player->poll_status &= ~(1 << DEV_AUDIO);
}
break;
default:
break;
}
player->AudioBufferState = (HI_UNF_AVPLAY_BUF_STATE_E)para;
break;
case HI_UNF_AVPLAY_EVENT_VID_BUF_STATE:
switch ((HI_UNF_AVPLAY_BUF_STATE_E)para)
{
case HI_UNF_AVPLAY_BUF_STATE_EMPTY:
case HI_UNF_AVPLAY_BUF_STATE_LOW:
case HI_UNF_AVPLAY_BUF_STATE_NORMAL:
case HI_UNF_AVPLAY_BUF_STATE_HIGH:
if (player->poll_status & (1 << DEV_VIDEO))
{
struct fuse_pollhandle *ph = player->poll_handle[DEV_VIDEO];
fuse_notify_poll(ph);
fuse_pollhandle_destroy(ph);
player->poll_handle[DEV_VIDEO] = NULL;
player->poll_status &= ~(1 << DEV_VIDEO);
}
break;
default:
break;
}
player->VideoBufferState = (HI_UNF_AVPLAY_BUF_STATE_E)para;
break;
default:
break;
}
pthread_mutex_unlock(&player->m_poll);
}
return HI_SUCCESS;
}
bool player_create(void)
{
printf("[INFO] %s -> called.\n", __FUNCTION__);
DIR *lib_dir;
HI_UNF_SYNC_ATTR_S stSyncAttr;
HI_UNF_VCODEC_ATTR_S stVDecAttr;
HI_UNF_ACODEC_ATTR_S stACodecAttr;
HI_UNF_AVPLAY_ATTR_S stAvplayAttr;
HI_UNF_AVPLAY_OPEN_OPT_S OpenOpt;
HI_UNF_AUDIOTRACK_ATTR_S stTrackAttr;
struct s_player *player = calloc(1, sizeof(struct s_player));
if (!player)
return false;
if (HI_SYS_Init() != HI_SUCCESS)
{
printf("[ERROR] %s -> HI_SYS_Init failed.\n", __FUNCTION__);
return false;
}
if (HIADP_Snd_Init() != HI_SUCCESS)
{
printf("[ERROR] %s -> HIADP_Snd_Init failed.\n", __FUNCTION__);
goto SYS_DEINIT;
}
if (HIADP_Disp_Init(HI_UNF_ENC_FMT_720P_60) != HI_SUCCESS)
{
printf("[ERROR] %s -> HIADP_Disp_Init failed.\n", __FUNCTION__);
goto SND_DEINIT;
}
if (HIADP_VO_Init(HI_UNF_VO_DEV_MODE_NORMAL) != HI_SUCCESS)
{
printf("[ERROR] %s -> HIADP_VO_Init failed.\n", __FUNCTION__);
goto DISP_DEINIT;
}
if (HIADP_VO_CreatWin(HI_NULL, &player->hWindow) != HI_SUCCESS)
{
printf("[ERROR] %s -> HIADP_VO_CreatWin failed.\n", __FUNCTION__);
goto VO_DEINIT;
}
if (HI_UNF_DMX_Init() != HI_SUCCESS)
{
printf("[ERROR] %s -> HI_UNF_DMX_Init failed.\n", __FUNCTION__);
goto VO_DEINIT;
}
if (HI_UNF_DMX_AttachTSPort(PLAYER_DEMUX_PORT, HI_UNF_DMX_PORT_TSI_0) != HI_SUCCESS)
{
printf("[ERROR] %s -> Failed to Attach TS Port.\n", __FUNCTION__);
goto DMX_DEINIT;
}
/** Auto load all codecs **/
if ((lib_dir = opendir ("/usr/lib")) || (lib_dir = opendir ("/system/lib")))
{
struct dirent *file = readdir(lib_dir);
while (file)
{
if (startsWith(file->d_name, "libHA.AUDIO.") && endsWith(file->d_name, "decode.so"))
if (HI_UNF_AVPLAY_RegisterAcodecLib(file->d_name) != HI_SUCCESS)
printf("[ERROR] %s: Failed to Register Audio library %s\n", __FUNCTION__, file->d_name);
if (startsWith(file->d_name, "libHV.VIDEO.") && endsWith(file->d_name, "decode.so"))
if (HI_UNF_AVPLAY_RegisterVcodecLib(file->d_name) != HI_SUCCESS)
printf("[ERROR] %s: Failed to Register Video library %s\n", __FUNCTION__, file->d_name);
file = readdir(lib_dir);
}
closedir (lib_dir);
}
else if (HIADP_AVPlay_RegADecLib() != HI_SUCCESS)
printf("[ERROR] %s -> HIADP_AVPlay_RegADecLib failed.\n", __FUNCTION__);
if (HI_UNF_AVPLAY_Init() != HI_SUCCESS)
{
printf("[ERROR] %s -> HI_UNF_AVPLAY_Init failed.\n", __FUNCTION__);
goto DMX_DEINIT;
}
HI_UNF_AVPLAY_GetDefaultConfig(&stAvplayAttr, HI_UNF_AVPLAY_STREAM_TYPE_TS);
stAvplayAttr.u32DemuxId = PLAYER_DEMUX_PORT;
stAvplayAttr.stStreamAttr.u32AudBufSize = 4 * 1024 * 1024; // Allocate 4MB
stAvplayAttr.stStreamAttr.u32VidBufSize = 16 * 1024 * 1024; // Allocate 16MB
if (HI_UNF_AVPLAY_Create(&stAvplayAttr, &player->hPlayer) != HI_SUCCESS)
{
printf("[ERROR] %s -> HI_UNF_AVPLAY_Create failed.\n", __FUNCTION__);
goto AVPLAY_DEINIT;
}
OpenOpt.enDecType = HI_UNF_VCODEC_DEC_TYPE_NORMAL;
OpenOpt.enCapLevel = HI_UNF_VCODEC_CAP_LEVEL_4096x2160;
OpenOpt.enProtocolLevel = HI_UNF_VCODEC_PRTCL_LEVEL_H264;
if (HI_UNF_AVPLAY_ChnOpen(player->hPlayer, HI_UNF_AVPLAY_MEDIA_CHAN_VID, &OpenOpt) != HI_SUCCESS)
{
printf("[ERROR] %s -> Failed to Open Video Channel.\n", __FUNCTION__);
goto AVPLAY_DESTROY;
}
if (HI_UNF_AVPLAY_ChnOpen(player->hPlayer, HI_UNF_AVPLAY_MEDIA_CHAN_AUD, HI_NULL) != HI_SUCCESS)
{
printf("[ERROR] %s -> Failed to Open Audio Channel.\n", __FUNCTION__);
goto VCHN_CLOSE;
}
if (HI_UNF_VO_AttachWindow(player->hWindow, player->hPlayer) != HI_SUCCESS)
{
printf("[ERROR] %s -> HI_UNF_VO_AttachWindow failed.\n", __FUNCTION__);
goto ACHN_CLOSE;
}
if (HI_UNF_VO_SetWindowEnable(player->hWindow, HI_TRUE) != HI_SUCCESS)
{
printf("[ERROR] %s -> HI_UNF_VO_SetWindowEnable failed.\n", __FUNCTION__);
goto WIN_DETACH;
}
if (HI_UNF_SND_GetDefaultTrackAttr(HI_UNF_SND_TRACK_TYPE_MASTER, &stTrackAttr) != HI_SUCCESS)
{
printf("[ERROR] %s -> HI_UNF_SND_GetDefaultTrackAttr failed.\n", __FUNCTION__);
goto WIN_DETACH;
}
if (HI_UNF_SND_CreateTrack(HI_UNF_SND_0, &stTrackAttr, &player->hTrack) != HI_SUCCESS)
{
printf("[ERROR] %s -> HI_UNF_SND_CreateTrack failed.\n", __FUNCTION__);
goto WIN_DETACH;
}
if (HI_UNF_SND_Attach(player->hTrack, player->hPlayer) != HI_SUCCESS)
{
printf("[ERROR] %s -> HI_UNF_SND_Attach failed.\n", __FUNCTION__);
goto TRACK_DESTROY;
}
/* Set Audio Codec to AAC */
HI_UNF_AVPLAY_GetAttr(player->hPlayer, HI_UNF_AVPLAY_ATTR_ID_ADEC, &stACodecAttr);
stACodecAttr.enType = HA_AUDIO_ID_AAC;
HA_AAC_DecGetDefalutOpenParam(&stACodecAttr.stDecodeParam);
HI_UNF_AVPLAY_SetAttr(player->hPlayer, HI_UNF_AVPLAY_ATTR_ID_ADEC, &stACodecAttr);
/* Set Video Codec to MPEG2 */
HI_UNF_AVPLAY_GetAttr(player->hPlayer, HI_UNF_AVPLAY_ATTR_ID_VDEC, &stVDecAttr);
stVDecAttr.enType = HI_UNF_VCODEC_TYPE_MPEG2;
stVDecAttr.enMode = HI_UNF_VCODEC_MODE_NORMAL;
stVDecAttr.u32ErrCover = 100;
stVDecAttr.u32Priority = 3;
HI_UNF_AVPLAY_SetAttr(player->hPlayer, HI_UNF_AVPLAY_ATTR_ID_VDEC, &stVDecAttr);
HI_UNF_AVPLAY_GetAttr(player->hPlayer, HI_UNF_AVPLAY_ATTR_ID_SYNC, &stSyncAttr);
stSyncAttr.enSyncRef = HI_UNF_SYNC_REF_AUDIO;
if (HI_UNF_AVPLAY_SetAttr(player->hPlayer, HI_UNF_AVPLAY_ATTR_ID_SYNC, &stSyncAttr) != HI_SUCCESS)
{
printf("[ERROR] %s -> HI_UNF_AVPLAY_SetAttr failed.\n", __FUNCTION__);
goto SND_DETACH;
}
if (HI_UNF_DMX_CreateTSBuffer(HI_UNF_DMX_PORT_RAM_0, 0x1000000, &player->hTsBuffer) != HI_SUCCESS)
printf("[WARNING] %s -> Failed to create TS buffer.\n", __FUNCTION__);
if (HI_UNF_AVPLAY_RegisterEvent(player->hPlayer, HI_UNF_AVPLAY_EVENT_NEW_VID_FRAME, (HI_UNF_AVPLAY_EVENT_CB_FN)player_event_handler) != HI_SUCCESS)
printf("[WARNING] %s -> Failed to register video event callback.\n", __FUNCTION__);
if (HI_UNF_AVPLAY_RegisterEvent(player->hPlayer, HI_UNF_AVPLAY_EVENT_AUD_BUF_STATE, (HI_UNF_AVPLAY_EVENT_CB_FN)player_event_handler) != HI_SUCCESS)
printf("[WARNING] %s -> Failed to register audio buffer event callback.\n", __FUNCTION__);
if (HI_UNF_AVPLAY_RegisterEvent(player->hPlayer, HI_UNF_AVPLAY_EVENT_VID_BUF_STATE, (HI_UNF_AVPLAY_EVENT_CB_FN)player_event_handler) != HI_SUCCESS)
printf("[WARNING] %s -> Failed to register video buffer event callback.\n", __FUNCTION__);
HI_UNF_DISP_SetVirtualScreen(HI_UNF_DISPLAY1, 1280, 720);
if (HI_MPI_AVPLAY_GetSyncVdecHandle(player->hPlayer, &player->hVdec, &player->hSync) != HI_SUCCESS)
printf("[ERROR] %s: Failed to get Vdec and Sync handler from player.\n", __FUNCTION__);
player->events[0].type = VIDEO_EVENT_SIZE_CHANGED;
player->events[1].type = VIDEO_EVENT_FRAME_RATE_CHANGED;
player->events[2].type = 16; /* VIDEO_EVENT_PROGRESSIVE_CHANGED */
player->event_status = 0;
pthread_mutex_init(&player->m_apts, 0);
pthread_mutex_init(&player->m_vpts, 0);
pthread_mutex_init(&player->m_event, 0);
pthread_mutex_init(&player->m_poll, 0);
pthread_rwlock_init(&player->m_write, 0);
player->IsCreated = true;
player->PlayerMode = 0;
player->AudioPid = 0x1FFFF;
player->AudioType = HA_AUDIO_ID_AAC;
player->AudioChannel = 0;
player->AudioState = 0;
player->VideoPid = 0x1FFFF;
player->VideoType = HI_UNF_VCODEC_TYPE_MPEG2;
player->VideoState = 0;
player->VideoFormat = 1;
player->DisplayFormat = 0;
player->IsBlank = true;
player->IsSyncEnabled = true;
player->IsMute = false;
player->AudioBufferState= HI_UNF_AVPLAY_BUF_STATE_BUTT;
player->VideoBufferState= HI_UNF_AVPLAY_BUF_STATE_BUTT;
player_ops.priv = player;
player_create_painel();
#if defined(HAVE_AVCODEC)
avcodec_register_all(); // Load all codecs in libavcodec.so.56
#endif
return true;
SND_DETACH:
HI_UNF_SND_Detach(player->hTrack, player->hPlayer);
TRACK_DESTROY:
HI_UNF_SND_DestroyTrack(player->hTrack);
WIN_DETACH:
HI_UNF_VO_SetWindowEnable(player->hWindow, HI_FALSE);
HI_UNF_VO_DetachWindow(player->hWindow, player->hPlayer);
ACHN_CLOSE:
HI_UNF_AVPLAY_ChnClose(player->hPlayer, HI_UNF_AVPLAY_MEDIA_CHAN_AUD);
VCHN_CLOSE:
HI_UNF_AVPLAY_ChnClose(player->hPlayer, HI_UNF_AVPLAY_MEDIA_CHAN_VID);
AVPLAY_DESTROY:
HI_UNF_AVPLAY_Destroy(player->hPlayer);
AVPLAY_DEINIT:
HI_UNF_AVPLAY_DeInit();
DMX_DEINIT:
HI_UNF_DMX_DetachTSPort(PLAYER_DEMUX_PORT);
HI_UNF_DMX_DeInit();
VO_DEINIT:
HI_UNF_VO_DestroyWindow(player->hWindow);
HIADP_VO_DeInit();
DISP_DEINIT:
HIADP_Disp_DeInit();
SND_DEINIT:
HIADP_Snd_DeInit();
SYS_DEINIT:
HI_SYS_DeInit();
return false;
}
void player_destroy(void)
{
printf("[INFO] %s -> called.\n", __FUNCTION__);
struct s_player *player = (struct s_player *)player_ops.priv;
if (player && player->IsCreated)
{
HI_UNF_AVPLAY_STOP_OPT_S stStop;
stStop.u32TimeoutMs = 0;
stStop.enMode = HI_UNF_AVPLAY_STOP_MODE_BLACK;
player_showtime();
HI_UNF_KEYLED_DeInit();
HI_UNF_AVPLAY_Stop(player->hPlayer, HI_UNF_AVPLAY_MEDIA_CHAN_AUD, HI_NULL);
HI_UNF_AVPLAY_Stop(player->hPlayer, HI_UNF_AVPLAY_MEDIA_CHAN_VID, &stStop);
HI_UNF_AVPLAY_UnRegisterEvent(player->hPlayer, HI_UNF_AVPLAY_EVENT_NEW_VID_FRAME);
HI_UNF_AVPLAY_UnRegisterEvent(player->hPlayer, HI_UNF_AVPLAY_EVENT_AUD_BUF_STATE);
HI_UNF_AVPLAY_UnRegisterEvent(player->hPlayer, HI_UNF_AVPLAY_EVENT_VID_BUF_STATE);
HI_UNF_AVPLAY_ChnClose(player->hPlayer, HI_UNF_AVPLAY_MEDIA_CHAN_AUD);
HI_UNF_AVPLAY_ChnClose(player->hPlayer, HI_UNF_AVPLAY_MEDIA_CHAN_VID);
HI_UNF_DMX_DetachTSPort(PLAYER_DEMUX_PORT);
if (player->hTsBuffer)
HI_UNF_DMX_DestroyTSBuffer(player->hTsBuffer);
HI_UNF_SND_Detach(player->hTrack, player->hPlayer);
HI_UNF_SND_DestroyTrack(player->hTrack);
HI_UNF_VO_SetWindowEnable(player->hWindow, HI_FALSE);
HI_UNF_VO_DetachWindow(player->hWindow, player->hPlayer);
HI_UNF_AVPLAY_Destroy(player->hPlayer);
HI_UNF_AVPLAY_DeInit();
HI_UNF_DMX_DetachTSPort(PLAYER_DEMUX_PORT);
HI_UNF_DMX_DeInit();
HI_UNF_VO_DestroyWindow(player->hWindow);
HIADP_VO_DeInit();
HIADP_Disp_DeInit();
HIADP_Snd_DeInit();
HI_SYS_DeInit();
}
else
printf("[ERROR] %s -> The Player it's not created.\n", __FUNCTION__);
}
bool player_clear(int dev_type)
{
struct s_player *player = (struct s_player *)player_ops.priv;
if (!(player && player->IsCreated))
{
printf("[ERROR] %s -> The Player it's not created.\n", __FUNCTION__);
return false;
}
pthread_rwlock_rdlock(&player->m_write);
/* if (player->IsPES && (player->AudioState != 0 || player->VideoState != 0))
if (HI_UNF_AVPLAY_FlushStream(player->hPlayer, HI_NULL) != HI_SUCCESS)
printf("[ERROR] %s: Failed to Flush buffer.\n", __FUNCTION__);
*/
/** Remove poll from waiting state. **/
pthread_mutex_lock(&player->m_poll);
switch (dev_type)
{
case DEV_AUDIO:
if (player->VideoState == 0)
if (HI_UNF_AVPLAY_Reset(player->hPlayer, HI_NULL) != HI_SUCCESS)
printf("[ERROR-AUD] %s: Failed to Reset player.\n", __FUNCTION__);
player->AudioBufferState = HI_UNF_AVPLAY_BUF_STATE_EMPTY;
break;
case DEV_VIDEO:
if (HI_UNF_AVPLAY_Reset(player->hPlayer, HI_NULL) != HI_SUCCESS)
printf("[ERROR-VID] %s: Failed to Reset player.\n", __FUNCTION__);
player->VideoBufferState = HI_UNF_AVPLAY_BUF_STATE_EMPTY;
break;
}
if (player->poll_status & (1 << dev_type))
{
struct fuse_pollhandle *ph = player->poll_handle[dev_type];
fuse_notify_poll(ph);
fuse_pollhandle_destroy(ph);
player->poll_handle[dev_type] = NULL;
player->poll_status &= ~(1 << dev_type);
}
pthread_mutex_unlock(&player->m_poll);
pthread_rwlock_unlock(&player->m_write);
return true;
}
void player_set_dvr(bool status)
{
printf("[INFO] %s(%s) -> called.\n", __FUNCTION__, status ? "true" : "false");
struct s_player *player = (struct s_player *)player_ops.priv;
if (!(player && player->IsCreated))
printf("[ERROR] %s -> The Player it's not created.\n", __FUNCTION__);
else
player->IsDVR = status;
}
bool player_set_type(int dev_type, int type)
{
printf("[INFO] %s(%d, %d) -> called.\n", __FUNCTION__, dev_type, type);
struct s_player *player = (struct s_player *)player_ops.priv;
if (!(player && player->IsCreated))
{
printf("[ERROR] %s -> The Player it's not created.\n", __FUNCTION__);
return false;
}
switch (dev_type)
{
case DEV_AUDIO:
{
char s_mode[12];
HI_UNF_ACODEC_ATTR_S stAttr;
FILE *file = NULL;
HI_UNF_SND_HDMI_MODE_E h_mode = HI_UNF_SND_HDMI_MODE_LPCM;
if (HI_UNF_AVPLAY_GetAttr(player->hPlayer, HI_UNF_AVPLAY_ATTR_ID_ADEC, &stAttr) != HI_SUCCESS)
{
printf("[ERROR] %s: Failed to get Audio Attribute for Type %d.\n", __FUNCTION__, type);
return false;
}
#if defined(HAVE_AVCODEC)
if (avcodec_context)
{
avcodec_free_context(&avcodec_context);
avcodec_context = NULL;
}
#endif
switch (type)
{
case AUDIO_STREAMTYPE_AC3:
case AUDIO_STREAMTYPE_DDP:
{
file = fopen("/proc/stb/audio/ac3", "r");
#if defined(DOLBYPLUS_HACODEC_SUPPORT)
DOLBYPLUS_DECODE_OPENCONFIG_S *stConfig = (DOLBYPLUS_DECODE_OPENCONFIG_S*)u8DecOpenBuf;
HA_DOLBYPLUS_DecGetDefalutOpenConfig(stConfig);
stConfig->enDrcMode = DOLBYPLUS_DRC_RF;
stConfig->enDmxMode = DOLBYPLUS_DMX_SRND;
stAttr.enType = HA_AUDIO_ID_DOLBY_PLUS;
HA_DOLBYPLUS_DecGetDefalutOpenParam(&stAttr.stDecodeParam, stConfig);
stAttr.stDecodeParam.enDecMode = HD_DEC_MODE_SIMUL;
#elif defined(HAVE_AVCODEC)
HA_FFMPEG_DECODE_OPENCONFIG_S *stConfig = (HA_FFMPEG_DECODE_OPENCONFIG_S*)&u8DecOpenBuf[sizeof(u8DecOpenBuf)-sizeof(HA_FFMPEG_DECODE_OPENCONFIG_S)];
HA_FFMPEG_DecGetDefalutOpenConfig(stConfig);
/* If avcodec_find_decoder_by_name return NULL the codec not will work. */
avcodec_context = avcodec_alloc_context3(avcodec_find_decoder_by_name("eac3"));
stConfig->hAvCtx = avcodec_context;
stAttr.enType = HA_AUDIO_ID_FFMPEG_DECODE;
HA_FFMPEGC_DecGetDefalutOpenParam(&stAttr.stDecodeParam, stConfig);
#endif
}
break;
case AUDIO_STREAMTYPE_AAC:
case AUDIO_STREAMTYPE_AACPLUS:
case AUDIO_STREAMTYPE_AACHE:
file = fopen("/proc/stb/audio/aac", "r");
stAttr.enType = HA_AUDIO_ID_AAC;
HA_AAC_DecGetDefalutOpenParam(&stAttr.stDecodeParam);
break;
case AUDIO_STREAMTYPE_DTS:
case AUDIO_STREAMTYPE_DTSHD:
{
#if defined(HAVE_AVCODEC)
HA_FFMPEG_DECODE_OPENCONFIG_S *stConfig = (HA_FFMPEG_DECODE_OPENCONFIG_S*)&u8DecOpenBuf[sizeof(u8DecOpenBuf)-sizeof(HA_FFMPEG_DECODE_OPENCONFIG_S)];
HA_FFMPEG_DecGetDefalutOpenConfig(stConfig);
/* If avcodec_find_decoder_by_name return NULL the codec not will work. */
avcodec_context = avcodec_alloc_context3(avcodec_find_decoder_by_name("dca"));
stConfig->hAvCtx = avcodec_context;
stAttr.enType = HA_AUDIO_ID_FFMPEG_DECODE;
HA_FFMPEGC_DecGetDefalutOpenParam(&stAttr.stDecodeParam, stConfig);
#else
DTSHD_DECODE_OPENCONFIG_S *stConfig = (DTSHD_DECODE_OPENCONFIG_S*)u8DecOpenBuf;
HA_DTSHD_DecGetDefalutOpenConfig(stConfig);
stAttr.enType = HA_AUDIO_ID_DTSHD;
HA_DTSHD_DecGetDefalutOpenParam(&stAttr.stDecodeParam, stConfig);
stAttr.stDecodeParam.enDecMode = HD_DEC_MODE_SIMUL;
#endif
}
break;
case AUDIO_STREAMTYPE_RAW:
case AUDIO_STREAMTYPE_LPCM:
{
WAV_FORMAT_S *stWavFormat = (WAV_FORMAT_S*)u8DecOpenBuf; /* set pcm wav format here base on pcm file */
stWavFormat->nChannels = 1;
stWavFormat->nSamplesPerSec = 48000;
stWavFormat->wBitsPerSample = 16;
stAttr.enType = HA_AUDIO_ID_PCM;
HA_PCM_DecGetDefalutOpenParam(&stAttr.stDecodeParam, stWavFormat);
printf("[INFO] %s: Using PCM Codec with Default Config (nChannels = 1, wBitsPerSample = 16, nSamplesPerSec = 48000, isBigEndian = false)\n", __FUNCTION__);
}
break;
case AUDIO_STREAMTYPE_MP3:
case AUDIO_STREAMTYPE_MPEG:
default: /* FallBack to MP3 */
stAttr.enType = HA_AUDIO_ID_MP3;
HA_MP3_DecGetDefalutOpenParam(&stAttr.stDecodeParam);
if (type != AUDIO_STREAMTYPE_MP3 && type != AUDIO_STREAMTYPE_MPEG)
printf("[ERROR] %s: Unknown Audio Type %d.\n", __FUNCTION__, type);
break;
}
if (HI_UNF_AVPLAY_SetAttr(player->hPlayer, HI_UNF_AVPLAY_ATTR_ID_ADEC, &stAttr) != HI_SUCCESS)
{
if (file)
fclose(file);
#if defined(HAVE_AVCODEC)
if (avcodec_context)
{
avcodec_free_context(&avcodec_context);
avcodec_context = NULL;
}
#endif
printf("[ERROR] %s: Failed to set Audio Type %d.\n", __FUNCTION__, type);
return false;
}
player->AudioType = stAttr.enType;
/* Check HDMI OutPut Mode for Re-Apply this. */
if (file)
{
fgets(s_mode, 12, file);
fclose(file);
if (strEquals(s_mode, "downmix", false))
h_mode = HI_UNF_SND_HDMI_MODE_LPCM;
else if (strEquals(s_mode, "passthrough", false))
h_mode = HI_UNF_SND_HDMI_MODE_RAW;
}
if (HI_UNF_SND_SetHdmiMode(HI_UNF_SND_0, HI_UNF_SND_OUTPUTPORT_HDMI0, h_mode) != HI_SUCCESS)
printf("[ERROR] %s: Failed to set HDMI OutPut Mode.\n", __FUNCTION__);
}
break;
case DEV_VIDEO:
{
HI_UNF_VCODEC_ATTR_S stAttr;
if (HI_UNF_AVPLAY_GetAttr(player->hPlayer, HI_UNF_AVPLAY_ATTR_ID_VDEC, &stAttr) != HI_SUCCESS)
{
printf("[ERROR] %s: Failed to get Video Attribute for Type %d.\n", __FUNCTION__, type);
return false;
}
switch (type)
{
case VIDEO_STREAMTYPE_MPEG1:
case VIDEO_STREAMTYPE_MPEG2:
stAttr.enType = HI_UNF_VCODEC_TYPE_MPEG2;
break;
case VIDEO_STREAMTYPE_MPEG4_H264:
stAttr.enType = HI_UNF_VCODEC_TYPE_H264;
break;
case VIDEO_STREAMTYPE_VC1:
case VIDEO_STREAMTYPE_VC1_SM:
stAttr.enType = HI_UNF_VCODEC_TYPE_VC1;
break;
case VIDEO_STREAMTYPE_DIVX4:
case VIDEO_STREAMTYPE_DIVX5:
case VIDEO_STREAMTYPE_MPEG4_Part2:
stAttr.enType = HI_UNF_VCODEC_TYPE_MPEG4;
break;
case VIDEO_STREAMTYPE_H263:
stAttr.enType = HI_UNF_VCODEC_TYPE_H263;
break;
case VIDEO_STREAMTYPE_DIVX311:
stAttr.enType = HI_UNF_VCODEC_TYPE_DIVX3;
break;
case VIDEO_STREAMTYPE_H265_HEVC:
stAttr.enType = HI_UNF_VCODEC_TYPE_HEVC;
break;
case VIDEO_STREAMTYPE_VP8:
stAttr.enType = HI_UNF_VCODEC_TYPE_VP8;
break;
case VIDEO_STREAMTYPE_VP9:
stAttr.enType = HI_UNF_VCODEC_TYPE_VP9;
break;
default:
if (type > HI_UNF_VCODEC_TYPE_BUTT)
stAttr.enType = type - HI_UNF_VCODEC_TYPE_BUTT;
else
{
printf("[ERROR] %s: The Video Type %d is Unknown.\n", __FUNCTION__, type);
return false;
}
break;
}
stAttr.enMode = HI_UNF_VCODEC_MODE_NORMAL;
stAttr.u32ErrCover = 100;
stAttr.u32Priority = 3;
if (player->VideoType == stAttr.enType)
return true;
else if (HI_UNF_AVPLAY_SetAttr(player->hPlayer, HI_UNF_AVPLAY_ATTR_ID_VDEC, &stAttr) != HI_SUCCESS)
{
printf("[ERROR] %s: Failed to set Video Type %d.\n", __FUNCTION__, type);