forked from rdkcmf/rdk-aamp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aampgstplayer.cpp
3792 lines (3491 loc) · 128 KB
/
aampgstplayer.cpp
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
/*
* If not stated otherwise in this file or this component's license file the
* following copyright and licenses apply:
*
* Copyright 2018 RDK Management
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @file aampgstplayer.cpp
* @brief Gstreamer based player impl for AAMP
*/
#include "aampgstplayer.h"
#include "AampUtils.h"
#include <gst/gst.h>
#include <gst/app/gstappsrc.h>
#if defined(REALTEKCE)
#include <gst/app/gstappsink.h>
#endif
#include <string.h>
#include <assert.h>
#include <stdlib.h>
#include <stdio.h> // for sprintf
#include "priv_aamp.h"
#include <pthread.h>
#include <atomic>
#ifdef __APPLE__
#include "gst/video/videooverlay.h"
#import "cocoa_window.h"
#endif
#ifdef AAMP_MPD_DRM
#include "aampoutputprotection.h"
#endif
/**
* @enum GstPlayFlags
* @brief Enum of configuration flags used by playbin
*/
typedef enum {
GST_PLAY_FLAG_VIDEO = (1 << 0), // 0x001
GST_PLAY_FLAG_AUDIO = (1 << 1), // 0x002
GST_PLAY_FLAG_TEXT = (1 << 2), // 0x004
GST_PLAY_FLAG_VIS = (1 << 3), // 0x008
GST_PLAY_FLAG_SOFT_VOLUME = (1 << 4), // 0x010
GST_PLAY_FLAG_NATIVE_AUDIO = (1 << 5), // 0x020
GST_PLAY_FLAG_NATIVE_VIDEO = (1 << 6), // 0x040
GST_PLAY_FLAG_DOWNLOAD = (1 << 7), // 0x080
GST_PLAY_FLAG_BUFFERING = (1 << 8), // 0x100
GST_PLAY_FLAG_DEINTERLACE = (1 << 9), // 0x200
GST_PLAY_FLAG_SOFT_COLORBALANCE = (1 << 10) // 0x400
} GstPlayFlags;
//#define SUPPORT_MULTI_AUDIO
#define GST_ELEMENT_GET_STATE_RETRY_CNT_MAX 5
/*Playersinkbin events*/
#define GSTPLAYERSINKBIN_EVENT_HAVE_VIDEO 0x01
#define GSTPLAYERSINKBIN_EVENT_HAVE_AUDIO 0x02
#define GSTPLAYERSINKBIN_EVENT_FIRST_VIDEO_FRAME 0x03
#define GSTPLAYERSINKBIN_EVENT_FIRST_AUDIO_FRAME 0x04
#define GSTPLAYERSINKBIN_EVENT_ERROR_VIDEO_UNDERFLOW 0x06
#define GSTPLAYERSINKBIN_EVENT_ERROR_AUDIO_UNDERFLOW 0x07
#define GSTPLAYERSINKBIN_EVENT_ERROR_VIDEO_PTS 0x08
#define GSTPLAYERSINKBIN_EVENT_ERROR_AUDIO_PTS 0x09
#ifdef INTELCE
#define INPUT_GAIN_DB_MUTE (gdouble)-145
#define INPUT_GAIN_DB_UNMUTE (gdouble)0
#define DEFAULT_VIDEO_RECTANGLE "0,0,0,0"
#else
#define DEFAULT_VIDEO_RECTANGLE "0,0,1280,720"
#endif
#define DEFAULT_BUFFERING_TO_MS 10 // TimeOut interval to check buffer fullness
#define DEFAULT_BUFFERING_QUEUED_BYTES_MIN (128 * 1024) // prebuffer in bytes
#define DEFAULT_BUFFERING_QUEUED_FRAMES_MIN (5) // if the video decoder has this many queued frames start.. even at 60fps, close to 100ms...
#define DEFAULT_BUFFERING_MAX_MS (1000) // max buffering time
#define DEFAULT_BUFFERING_MAX_CNT (DEFAULT_BUFFERING_MAX_MS/DEFAULT_BUFFERING_TO_MS) // max buffering timeout count
#define AAMP_MIN_PTS_UPDATE_INTERVAL 4000
#define AAMP_DELAY_BETWEEN_PTS_CHECK_FOR_EOS_ON_UNDERFLOW 500
#define BUFFERING_TIMEOUT_PRIORITY -70
/**
* @struct media_stream
* @brief Holds stream(A/V) specific variables.
*/
struct media_stream
{
GstElement *sinkbin;
GstElement *source;
StreamOutputFormat format;
gboolean using_playersinkbin;
bool flush;
bool resetPosition;
bool bufferUnderrun;
bool eosReached;
bool sourceConfigured;
};
/**
* @struct AAMPGstPlayerPriv
* @brief Holds private variables of AAMPGstPlayer
*/
struct AAMPGstPlayerPriv
{
media_stream stream[AAMP_TRACK_COUNT];
GstElement *pipeline; //GstPipeline used for playback.
GstBus *bus; //Bus for receiving GstEvents from pipeline.
int current_rate;
guint64 total_bytes;
gint n_audio; //Number of audio tracks.
gint current_audio; //Offset of current audio track.
guint firstProgressCallbackIdleTaskId; //ID of idle handler created for notifying first progress event.
std::atomic<bool> firstProgressCallbackIdleTaskPending; //Set if any first progress callback is pending.
guint periodicProgressCallbackIdleTaskId; //ID of timed handler created for notifying progress events.
guint bufferingTimeoutTimerId; //ID of timer handler created for buffering timeout.
guint id3MetadataCallbackIdleTaskId; //ID of handler created to send ID3 metadata events
std::atomic<bool> id3MetadataCallbackTaskPending; //Set if an id3 metadata callback is pending
GstElement *video_dec; //Video decoder used by pipeline.
GstElement *audio_dec; //Audio decoder used by pipeline.
GstElement *video_sink; //Video sink used by pipeline.
GstElement *audio_sink; //Audio sink used by pipeline.
#ifdef INTELCE_USE_VIDRENDSINK
GstElement *video_pproc; //Video element used by pipeline.(only for Intel).
#endif
int rate; //Current playback rate.
VideoZoomMode zoom; //Video-zoom setting.
bool videoMuted; //Video mute status.
bool audioMuted; //Audio mute status.
double audioVolume; //Audio volume.
guint eosCallbackIdleTaskId; //ID of idle handler created for notifying EOS event.
std::atomic<bool> eosCallbackIdleTaskPending; //Set if any eos callback is pending.
bool firstFrameReceived; //Flag that denotes if first frame was notified.
char videoRectangle[32]; //Video-rectangle co-ordinates in format x,y,w,h.
bool pendingPlayState; //Flag that denotes if set pipeline to PLAYING state is pending.
bool decoderHandleNotified; //Flag that denotes if decoder handle was notified.
guint firstFrameCallbackIdleTaskId; //ID of idle handler created for notifying first frame event.
GstEvent *protectionEvent[AAMP_TRACK_COUNT]; //GstEvent holding the pssi data to be sent downstream.
std::atomic<bool> firstFrameCallbackIdleTaskPending; //Set if any first frame callback is pending.
bool using_westerossink; //true if westros sink is used as video sink
guint busWatchId;
std::atomic<bool> eosSignalled; /** Indicates if EOS has signaled */
gboolean buffering_enabled; // enable buffering based on multiqueue
gboolean buffering_in_progress; // buffering is in progress
guint buffering_timeout_cnt; // make sure buffering_timout doesn't get stuck
GstState buffering_target_state; // the target state after buffering
#ifdef INTELCE
bool keepLastFrame; //Keep last frame over next pipeline delete/ create cycle
#endif
gint64 lastKnownPTS; //To store the PTS of last displayed video
long long ptsUpdatedTimeMS; //Timestamp when PTS was last updated
guint ptsCheckForEosOnUnderflowIdleTaskId; //ID of task to ensure video PTS is not moving before notifying EOS on underflow.
int numberOfVideoBuffersSent; //Number of video buffers sent to pipeline
gint64 segmentStart; // segment start value; required when qtdemux is enabled and restamping is disabled
GstQuery *positionQuery; // pointer that holds a position query object
GstQuery *durationQuery; // pointer that holds a duration query object
bool paused; // if pipeline is deliberately put in PAUSED state due to user interaction
GstState pipelineState; // current state of pipeline
guint firstVideoFrameDisplayedCallbackIdleTaskId; //ID of idle handler created for notifying state changed to Playing
std::atomic<bool> firstVideoFrameDisplayedCallbackIdleTaskPending; //Set if any state changed to Playing callback is pending.
#if defined(REALTEKCE)
bool firstTuneWithWesterosSinkOff; // DELIA-33640: track if first tune was done for Realtekce build
gboolean audioSinkAsyncEnabled; // XIONE-1279: track if AudioSink Async Mode is enabled for Realtekce build
#endif
int32_t lastId3DataLen; // last sent ID3 data length
uint8_t *lastId3Data; // ptr with last sent ID3 data
};
/**
* @class Id3CallbackData
* @brief Holds id3 metadata callback specific variables.
*/
class Id3CallbackData
{
public:
Id3CallbackData(class AAMPGstPlayer *instance, const uint8_t* ptr, uint32_t len) : _this(instance), data()
{
data = std::vector<uint8_t>(ptr, ptr + len);
}
Id3CallbackData() = delete;
Id3CallbackData(const Id3CallbackData&) = delete;
Id3CallbackData& operator=(const Id3CallbackData&) = delete;
class AAMPGstPlayer* _this; // AAMPGstPlayer instance
std::vector<uint8_t> data; //id3 metadata
};
static const char* GstPluginNamePR = "aampplayreadydecryptor";
static const char* GstPluginNameWV = "aampwidevinedecryptor";
static const char* GstPluginNameCK = "aampclearkeydecryptor";
/**
* @brief Called from the mainloop when a message is available on the bus
* @param[in] bus the GstBus that sent the message
* @param[in] msg the GstMessage
* @param[in] _this pointer to AAMPGstPlayer instance
* @retval FALSE if the event source should be removed.
*/
static gboolean bus_message(GstBus * bus, GstMessage * msg, AAMPGstPlayer * _this);
/**
* @brief Invoked synchronously when a message is available on the bus
* @param[in] bus the GstBus that sent the message
* @param[in] msg the GstMessage
* @param[in] _this pointer to AAMPGstPlayer instance
* @retval FALSE if the event source should be removed.
*/
static GstBusSyncReply bus_sync_handler(GstBus * bus, GstMessage * msg, AAMPGstPlayer * _this);
/**
* @brief g_timeout callback to wait for buffering to change
* pipeline from paused->playing
*/
static gboolean buffering_timeout (gpointer data);
/**
* @brief check if elemement is instance (BCOM-3563)
*/
static void type_check_instance( const char * str, GstElement * elem);
#define PLUGINS_TO_LOWER_RANK_MAX 2
const char *plugins_to_lower_rank[PLUGINS_TO_LOWER_RANK_MAX] = {
"aacparse",
"ac3parse",
};
/**
* @brief AAMPGstPlayer Constructor
* @param[in] aamp pointer to PrivateInstanceAAMP object associated with player
*/
AAMPGstPlayer::AAMPGstPlayer(PrivateInstanceAAMP *aamp
) : aamp(NULL) , privateContext(NULL), mBufferingLock(), mProtectionLock()
{
privateContext = (AAMPGstPlayerPriv *)malloc(sizeof(*privateContext));
if(privateContext)
{
memset(privateContext, 0, sizeof(*privateContext));
privateContext->audioVolume = 1.0;
privateContext->pipelineState = GST_STATE_NULL;
this->aamp = aamp;
pthread_mutex_init(&mBufferingLock, NULL);
pthread_mutex_init(&mProtectionLock, NULL);
CreatePipeline();
privateContext->rate = AAMP_NORMAL_PLAY_RATE;
strcpy(privateContext->videoRectangle, DEFAULT_VIDEO_RECTANGLE);
}
else
{
AAMPLOG_WARN("%s:%d : privateContext is null", __FUNCTION__, __LINE__); //CID:85372 - Null Returns
}
}
/**
* @brief AAMPGstPlayer Destructor
*/
AAMPGstPlayer::~AAMPGstPlayer()
{
DestroyPipeline();
free(privateContext);
pthread_mutex_destroy(&mBufferingLock);
pthread_mutex_destroy(&mProtectionLock);
}
/**
* @brief Analyze stream info from the GstPipeline
* @param[in] _this pointer to AAMPGstPlayer instance
*/
static void analyze_streams(AAMPGstPlayer *_this)
{
#ifdef SUPPORT_MULTI_AUDIO
GstElement *sinkbin = _this->privateContext->stream[eMEDIATYPE_VIDEO].sinkbin;
g_object_get(sinkbin, "n-audio", &_this->privateContext->n_audio, NULL);
g_print("audio:\n");
for (gint i = 0; i < _this->privateContext->n_audio; i++)
{
GstTagList *tags = NULL;
g_signal_emit_by_name(sinkbin, "get-audio-tags", i, &tags);
if (tags)
{
gchar *str;
guint rate;
g_print("audio stream %d:\n", i);
if (gst_tag_list_get_string(tags, GST_TAG_AUDIO_CODEC, &str)) {
g_print(" codec: %s\n", str);
g_free(str);
}
if (gst_tag_list_get_string(tags, GST_TAG_LANGUAGE_CODE, &str)) {
g_print(" language: %s\n", str);
g_free(str);
}
if (gst_tag_list_get_uint(tags, GST_TAG_BITRATE, &rate)) {
g_print(" bitrate: %d\n", rate);
}
gst_tag_list_free(tags);
}
}
g_object_get(sinkbin, "current-audio", &_this->privateContext->current_audio, NULL);
#endif
}
/**
* @brief Callback for appsrc "need-data" signal
* @param[in] source pointer to appsrc instance triggering "need-data" signal
* @param[in] size size of data required
* @param[in] _this pointer to AAMPGstPlayer instance associated with the playback
*/
static void need_data(GstElement *source, guint size, AAMPGstPlayer * _this)
{
if (source == _this->privateContext->stream[eMEDIATYPE_SUBTITLE].source)
{
_this->aamp->ResumeTrackDownloads(eMEDIATYPE_SUBTITLE); // signal fragment downloader thread
}
else if (source == _this->privateContext->stream[eMEDIATYPE_AUDIO].source)
{
_this->aamp->ResumeTrackDownloads(eMEDIATYPE_AUDIO); // signal fragment downloader thread
}
else
{
_this->aamp->ResumeTrackDownloads(eMEDIATYPE_VIDEO); // signal fragment downloader thread
}
}
/**
* @brief Callback for appsrc "enough-data" signal
* @param[in] source pointer to appsrc instance triggering "enough-data" signal
* @param[in] _this pointer to AAMPGstPlayer instance associated with the playback
*/
static void enough_data(GstElement *source, AAMPGstPlayer * _this)
{
if (source == _this->privateContext->stream[eMEDIATYPE_SUBTITLE].source)
{
_this->aamp->StopTrackDownloads(eMEDIATYPE_SUBTITLE); // signal fragment downloader thread
}
else if (source == _this->privateContext->stream[eMEDIATYPE_AUDIO].source)
{
_this->aamp->StopTrackDownloads(eMEDIATYPE_AUDIO); // signal fragment downloader thread
}
else
{
_this->aamp->StopTrackDownloads(eMEDIATYPE_VIDEO); // signal fragment downloader thread
}
}
/**
* @brief Callback for appsrc "seek-data" signal
* @param[in] src pointer to appsrc instance triggering "seek-data" signal
* @param[in] offset seek position offset
* @param[in] _this pointer to AAMPGstPlayer instance associated with the playback
*/
static gboolean appsrc_seek(GstAppSrc *src, guint64 offset, AAMPGstPlayer * _this)
{
#ifdef TRACE
logprintf("appsrc %p seek-signal - offset %" G_GUINT64_FORMAT, src, offset);
#endif
return TRUE;
}
/**
* @brief Parse format to generate GstCaps
* @param[in] format stream format to generate caps
* @retval GstCaps for the input format
*/
static GstCaps* GetGstCaps(StreamOutputFormat format)
{
GstCaps * caps = NULL;
switch (format)
{
case FORMAT_MPEGTS:
caps = gst_caps_new_simple ("video/mpegts",
"systemstream", G_TYPE_BOOLEAN, TRUE,
"packetsize", G_TYPE_INT, 188, NULL);
break;
case FORMAT_ISO_BMFF:
caps = gst_caps_new_simple("video/quicktime", NULL, NULL);
break;
case FORMAT_AUDIO_ES_AAC:
caps = gst_caps_new_simple ("audio/mpeg",
"mpegversion", G_TYPE_INT, 2,
"stream-format", G_TYPE_STRING, "adts", NULL);
break;
case FORMAT_AUDIO_ES_AC3:
caps = gst_caps_new_simple ("audio/x-ac3", NULL, NULL);
break;
case FORMAT_AUDIO_ES_ATMOS:
// Todo :: a) Test with all platforms if atmos works
// b) Test to see if x-eac3 config is enough for atmos stream.
// if x-eac3 is enough then both switch cases can be combined
caps = gst_caps_new_simple ("audio/x-eac3", NULL, NULL);
break;
case FORMAT_AUDIO_ES_EC3:
caps = gst_caps_new_simple ("audio/x-eac3", NULL, NULL);
break;
case FORMAT_VIDEO_ES_H264:
#ifdef INTELCE
caps = gst_caps_new_simple ("video/x-h264",
"stream-format", G_TYPE_STRING, "avc",
"width", G_TYPE_INT, 1920,
"height", G_TYPE_INT, 1080,
NULL);
#elif (defined(RPI) || defined(__APPLE__))
caps = gst_caps_new_simple ("video/x-h264",
"alignment", G_TYPE_STRING, "au",
"stream-format", G_TYPE_STRING, "avc",
NULL);
#else
caps = gst_caps_new_simple ("video/x-h264", NULL, NULL);
#endif
break;
case FORMAT_VIDEO_ES_HEVC:
caps = gst_caps_new_simple ("video/x-h265", NULL, NULL);
break;
case FORMAT_VIDEO_ES_MPEG2:
caps = gst_caps_new_simple ("video/mpeg",
"mpegversion", G_TYPE_INT, 2,
"systemstream", G_TYPE_BOOLEAN, FALSE, NULL);
break; //CID:81305 - Using break statement
case FORMAT_UNKNOWN:
AAMPLOG_WARN("%s:%d Unknown format %d", __FUNCTION__, __LINE__, format);
break;
case FORMAT_INVALID:
default:
AAMPLOG_WARN("%s:%d Unsupported format %d", __FUNCTION__, __LINE__, format);
break;
}
return caps;
}
/**
* @brief Initialize properties/callback of appsrc
* @param[in] _this pointer to AAMPGstPlayer instance associated with the playback
* @param[in] source pointer to appsrc instance to be initialized
* @param[in] mediaType stream type
*/
static void InitializeSource(AAMPGstPlayer *_this, GObject *source, MediaType mediaType = eMEDIATYPE_VIDEO)
{
media_stream *stream = &_this->privateContext->stream[mediaType];
GstCaps * caps = NULL;
g_signal_connect(source, "need-data", G_CALLBACK(need_data), _this);
g_signal_connect(source, "enough-data", G_CALLBACK(enough_data), _this);
g_signal_connect(source, "seek-data", G_CALLBACK(appsrc_seek), _this);
gst_app_src_set_stream_type(GST_APP_SRC(source), GST_APP_STREAM_TYPE_SEEKABLE);
if (eMEDIATYPE_VIDEO == mediaType )
{
#ifdef CONTENT_4K_SUPPORTED
g_object_set(source, "max-bytes", 4194304 * 3, NULL); // 4096k * 3
#else
g_object_set(source, "max-bytes", (guint64)4194304, NULL); // 4096k
#endif
}
else if (eMEDIATYPE_AUDIO == mediaType)
{
#ifdef CONTENT_4K_SUPPORTED
g_object_set(source, "max-bytes", 512000 * 3, NULL); // 512k * 3 for audio
#else
g_object_set(source, "max-bytes", (guint64)512000, NULL); // 512k for audio
#endif
}
g_object_set(source, "min-percent", 50, NULL);
g_object_set(source, "format", GST_FORMAT_TIME, NULL);
caps = GetGstCaps(stream->format);
if (caps != NULL)
{
gst_app_src_set_caps(GST_APP_SRC(source), caps);
gst_caps_unref(caps);
}
else
{
g_object_set(source, "typefind", TRUE, NULL);
}
stream->sourceConfigured = true;
}
/**
* @brief Callback when source is added by playbin
* @param[in] object a GstObject
* @param[in] orig the object that originated the signal
* @param[in] pspec the property that changed
* @param[in] _this pointer to AAMPGstPlayer instance associated with the playback
*/
static void found_source(GObject * object, GObject * orig, GParamSpec * pspec, AAMPGstPlayer * _this )
{
MediaType mediaType;
media_stream *stream;
if (object == G_OBJECT(_this->privateContext->stream[eMEDIATYPE_VIDEO].sinkbin))
{
logprintf("Found source for video");
mediaType = eMEDIATYPE_VIDEO;
}
else if (object == G_OBJECT(_this->privateContext->stream[eMEDIATYPE_AUDIO].sinkbin))
{
logprintf("Found source for audio");
mediaType = eMEDIATYPE_AUDIO;
}
else
{
logprintf("Found source for subtitle");
mediaType = eMEDIATYPE_SUBTITLE;
}
stream = &_this->privateContext->stream[mediaType];
g_object_get(orig, pspec->name, &stream->source, NULL);
InitializeSource(_this, G_OBJECT(stream->source), mediaType);
}
static void httpsoup_source_setup (GstElement * element, GstElement * source, gpointer data)
{
AAMPGstPlayer * _this = (AAMPGstPlayer *)data;
if (!strcmp(GST_ELEMENT_NAME(source), "source"))
{
const char *proxy = _this->aamp->GetNetworkProxy();
if(proxy)
{
g_object_set(source, "proxy", proxy, NULL);
logprintf("%s() : httpsoup -> Set network proxy '%s'", __FUNCTION__, proxy);
}
}
}
/**
* @brief Idle callback to notify first frame rendered event
* @param[in] user_data pointer to AAMPGstPlayer instance
* @retval G_SOURCE_REMOVE, if the source should be removed
*/
static gboolean IdleCallbackOnFirstFrame(gpointer user_data)
{
AAMPGstPlayer *_this = (AAMPGstPlayer *)user_data;
if (_this)
{
_this->aamp->NotifyFirstFrameReceived();
_this->privateContext->firstFrameCallbackIdleTaskPending = false;
_this->privateContext->firstFrameCallbackIdleTaskId = 0;
}
return G_SOURCE_REMOVE;
}
/**
* @brief Idle callback to notify end-of-stream event
* @param[in] user_data pointer to AAMPGstPlayer instance
* @retval G_SOURCE_REMOVE, if the source should be removed
*/
static gboolean IdleCallbackOnEOS(gpointer user_data)
{
AAMPGstPlayer *_this = (AAMPGstPlayer *)user_data;
if (_this)
{
_this->privateContext->eosCallbackIdleTaskPending = false;
logprintf("%s:%d eosCallbackIdleTaskId %d", __FUNCTION__, __LINE__, _this->privateContext->eosCallbackIdleTaskId);
_this->aamp->NotifyEOSReached();
_this->privateContext->eosCallbackIdleTaskId = 0;
}
return G_SOURCE_REMOVE;
}
/**
* @brief Idle callback to notify ID3 metadata event
* @param[in] user_data pointer to Id3CallbackData object containing AAMPGstPlayer instance
* @retval G_SOURCE_REMOVE, if the source should be removed
*/
static gboolean IdleCallbackOnId3Metadata(gpointer user_data)
{
Id3CallbackData *id3 = (Id3CallbackData*)user_data;
id3->_this->aamp->SendId3MetadataEvent(id3->data);
id3->_this->privateContext->id3MetadataCallbackTaskPending = false;
id3->_this->privateContext->id3MetadataCallbackIdleTaskId = 0;
delete id3;
return G_SOURCE_REMOVE;
}
/**
* @brief Timer's callback to notify playback progress event
* @param[in] user_data pointer to AAMPGstPlayer instance
* @retval G_SOURCE_REMOVE, if the source should be removed
*/
static gboolean ProgressCallbackOnTimeout(gpointer user_data)
{
AAMPGstPlayer *_this = (AAMPGstPlayer *)user_data;
if (_this)
{
_this->aamp->ReportProgress();
traceprintf("%s:%d current %d, stored %d ", __FUNCTION__, __LINE__, g_source_get_id(g_main_current_source()), _this->privateContext->periodicProgressCallbackIdleTaskId);
}
return G_SOURCE_CONTINUE;
}
/**
* @brief Idle callback to start progress notifier timer
* @param[in] user_data pointer to AAMPGstPlayer instance
* @retval G_SOURCE_REMOVE, if the source should be removed
*/
static gboolean IdleCallback(gpointer user_data)
{
AAMPGstPlayer *_this = (AAMPGstPlayer *)user_data;
if (_this)
{
_this->aamp->ReportProgress();
_this->privateContext->firstProgressCallbackIdleTaskPending = false;
_this->privateContext->firstProgressCallbackIdleTaskId = 0;
if (0 == _this->privateContext->periodicProgressCallbackIdleTaskId)
{
_this->privateContext->periodicProgressCallbackIdleTaskId = g_timeout_add(_this->aamp->mReportProgressInterval, ProgressCallbackOnTimeout, user_data);
AAMPLOG_WARN("%s:%d current %d, periodicProgressCallbackIdleTaskId %d", __FUNCTION__, __LINE__, g_source_get_id(g_main_current_source()), _this->privateContext->periodicProgressCallbackIdleTaskId);
}
else
{
AAMPLOG_INFO("%s:%d Progress callback already available: periodicProgressCallbackIdleTaskId %d", __FUNCTION__, __LINE__, _this->privateContext->periodicProgressCallbackIdleTaskId);
}
}
return G_SOURCE_REMOVE;
}
/**
* @brief Idle callback to notify first video frame was displayed
* @param[in] user_data pointer to AAMPGstPlayer instance
* @retval G_SOURCE_REMOVE, if the source should be removed
*/
static gboolean IdleCallbackFirstVideoFrameDisplayed(gpointer user_data)
{
AAMPGstPlayer *_this = (AAMPGstPlayer *)user_data;
if (_this)
{
_this->aamp->NotifyFirstVideoFrameDisplayed();
_this->privateContext->firstVideoFrameDisplayedCallbackIdleTaskPending = false;
_this->privateContext->firstVideoFrameDisplayedCallbackIdleTaskId = 0;
}
return G_SOURCE_REMOVE;
}
/**
* @brief Notify first Audio and Video frame through an idle function to make the playersinkbin halding same as normal(playbin) playback.
* @param[in] type media type of the frame which is decoded, either audio or video.
*/
void AAMPGstPlayer::NotifyFirstFrame(MediaType type)
{
if(!privateContext->firstFrameReceived)
{
privateContext->firstFrameReceived = true;
aamp->LogFirstFrame();
aamp->LogTuneComplete();
aamp->NotifyFirstBufferProcessed();
}
if (eMEDIATYPE_VIDEO == type)
{
// DELIA-42262: No additional checks added here, since the NotifyFirstFrame will be invoked only once
// in westerossink disabled case until BCOM fixes it. Also aware of NotifyFirstBufferProcessed called
// twice in this function, since it updates timestamp for calculating time elapsed, its trivial
aamp->NotifyFirstBufferProcessed();
if (!privateContext->decoderHandleNotified)
{
privateContext->decoderHandleNotified = true;
privateContext->firstFrameCallbackIdleTaskPending = true;
privateContext->firstFrameCallbackIdleTaskId = g_idle_add(IdleCallbackOnFirstFrame, this);
if (!privateContext->firstFrameCallbackIdleTaskPending)
{
logprintf("%s:%d firstFrameCallbackIdleTask already finished, reset id", __FUNCTION__, __LINE__);
privateContext->firstFrameCallbackIdleTaskId = 0;
}
}
if (privateContext->firstProgressCallbackIdleTaskId == 0)
{
privateContext->firstProgressCallbackIdleTaskPending = true;
privateContext->firstProgressCallbackIdleTaskId = g_idle_add(IdleCallback, this);
if (!privateContext->firstProgressCallbackIdleTaskPending)
{
logprintf("%s:%d firstProgressCallbackIdleTask already finished, reset id", __FUNCTION__, __LINE__);
privateContext->firstProgressCallbackIdleTaskId = 0;
}
}
if ( (!privateContext->firstVideoFrameDisplayedCallbackIdleTaskPending)
&& (aamp->IsFirstVideoFrameDisplayedRequired()) )
{
privateContext->firstVideoFrameDisplayedCallbackIdleTaskPending = true;
privateContext->firstVideoFrameDisplayedCallbackIdleTaskId =
g_idle_add(IdleCallbackFirstVideoFrameDisplayed, this);
}
}
}
/**
* @brief Callback invoked after first video frame decoded
* @param[in] object pointer to element raising the callback
* @param[in] arg0 number of arguments
* @param[in] arg1 array of arguments
* @param[in] _this pointer to AAMPGstPlayer instance
*/
static void AAMPGstPlayer_OnFirstVideoFrameCallback(GstElement* object, guint arg0, gpointer arg1,
AAMPGstPlayer * _this)
{
logprintf("AAMPGstPlayer_OnFirstVideoFrameCallback. got First Video Frame");
_this->NotifyFirstFrame(eMEDIATYPE_VIDEO);
}
/**
* @brief Callback invoked after first audio buffer decoded
* @param[in] object pointer to element raising the callback
* @param[in] arg0 number of arguments
* @param[in] arg1 array of arguments
* @param[in] _this pointer to AAMPGstPlayer instance
*/
static void AAMPGstPlayer_OnAudioFirstFrameBrcmAudDecoder(GstElement* object, guint arg0, gpointer arg1,
AAMPGstPlayer * _this)
{
logprintf("AAMPGstPlayer_OnAudioFirstFrameBrcmAudDecoder. got First Audio Frame");
_this->NotifyFirstFrame(eMEDIATYPE_AUDIO);
}
/**
* @brief Check if gstreamer element is video decoder
* @param[in] name Name of the element
* @param[in] _this pointer to AAMPGstPlayer instance
* @retval TRUE if element name is that of the decoder
*/
bool AAMPGstPlayer_isVideoDecoder(const char* name, AAMPGstPlayer * _this)
{
return _this->privateContext->using_westerossink?
aamp_StartsWith(name, "westerossink"):
(aamp_StartsWith(name, "brcmvideodecoder") ||aamp_StartsWith(name, "omxwmvdec") || aamp_StartsWith(name, "omxh26") ||
aamp_StartsWith(name, "omxav1dec") || aamp_StartsWith(name, "omxvp") || aamp_StartsWith(name, "omxmpeg"));
}
/**
* @brief Check if gstreamer element is video sink
* @param[in] name Name of the element
* @param[in] _this pointer to AAMPGstPlayer instance
* @retval TRUE if element name is that of video sink
*/
bool AAMPGstPlayer_isVideoSink(const char* name, AAMPGstPlayer * _this)
{
return (!_this->privateContext->using_westerossink && aamp_StartsWith(name, "brcmvideosink") == true) || // brcmvideosink0, brcmvideosink1, ...
( _this->privateContext->using_westerossink && aamp_StartsWith(name, "westerossink") == true);
}
/**
* @brief Check if gstreamer element is audio decoder
* @param[in] name Name of the element
* @param[in] _this pointer to AAMPGstPlayer instance
* @retval TRUE if element name is that of audio or video decoder
*/
bool AAMPGstPlayer_isVideoOrAudioDecoder(const char* name, AAMPGstPlayer * _this)
{
// The idea is to identify video or audio decoder plugin created at runtime by playbin and register to its first-frame/pts-error callbacks
// This support is available in BCOM plugins in RDK builds and hence checking only for such plugin instances here
// While using playersinkbin, these callbacks are supported via "event-callback" signal and hence not requried to do explicitly
// For platforms that doesnt support callback, we use GST_STATE_PLAYING state change of playbin to notify first frame to app
return (!_this->privateContext->stream[eMEDIATYPE_VIDEO].using_playersinkbin &&
(!_this->privateContext->using_westerossink && aamp_StartsWith(name, "brcmvideodecoder") == true) ||
(_this->privateContext->using_westerossink && aamp_StartsWith(name, "westerossink") == true) ||
(aamp_StartsWith(name, "brcmaudiodecoder") == true));
}
/**
* @brief Notifies EOS if video decoder pts is stalled
* @param[in] user_data pointer to AAMPGstPlayer instance
* @retval G_SOURCE_REMOVE, if the source should be removed
*/
static gboolean VideoDecoderPtsCheckerForEOS(gpointer user_data)
{
AAMPGstPlayer *_this = (AAMPGstPlayer *) user_data;
AAMPGstPlayerPriv *privateContext = _this->privateContext;
#ifndef INTELCE
gint64 currentPTS = 0;
if (privateContext->video_dec)
{
g_object_get(privateContext->video_dec, "video-pts", ¤tPTS, NULL);
}
if (currentPTS == privateContext->lastKnownPTS)
{
logprintf("%s:%d : PTS not changed", __FUNCTION__, __LINE__);
_this->NotifyEOS();
}
else
{
logprintf("%s:%d : Video PTS still moving lastKnownPTS %" G_GUINT64_FORMAT " currentPTS %" G_GUINT64_FORMAT " ##", __FUNCTION__, __LINE__, privateContext->lastKnownPTS, currentPTS);
}
#endif
privateContext->ptsCheckForEosOnUnderflowIdleTaskId = 0;
return G_SOURCE_REMOVE;
}
/**
* @brief Callback invoked when facing an underflow
* @param[in] object pointer to element raising the callback
* @param[in] arg0 number of arguments
* @param[in] arg1 array of arguments
* @param[in] _this pointer to AAMPGstPlayer instance
*/
static void AAMPGstPlayer_OnGstBufferUnderflowCb(GstElement* object, guint arg0, gpointer arg1,
AAMPGstPlayer * _this)
{
//TODO - Handle underflow
MediaType type = eMEDIATYPE_DEFAULT; //CID:89173 - Resolve Uninit
AAMPGstPlayerPriv *privateContext = _this->privateContext;
logprintf("## %s() : Got Underflow message from %s ##", __FUNCTION__, GST_ELEMENT_NAME(object));
if (AAMPGstPlayer_isVideoDecoder(GST_ELEMENT_NAME(object), _this))
{
type = eMEDIATYPE_VIDEO;
}
else if (aamp_StartsWith(GST_ELEMENT_NAME(object), "brcmaudiodecoder") == true)
{
type = eMEDIATYPE_AUDIO;
}
_this->privateContext->stream[type].bufferUnderrun = true;
if (_this->privateContext->stream[type].eosReached)
{
if (_this->privateContext->rate > 0)
{
if (privateContext->video_dec)
{
if (!privateContext->ptsCheckForEosOnUnderflowIdleTaskId)
{
g_object_get(privateContext->video_dec, "video-pts", &privateContext->lastKnownPTS, NULL);
privateContext->ptsUpdatedTimeMS = NOW_STEADY_TS_MS;
privateContext->ptsCheckForEosOnUnderflowIdleTaskId = g_timeout_add(AAMP_DELAY_BETWEEN_PTS_CHECK_FOR_EOS_ON_UNDERFLOW, VideoDecoderPtsCheckerForEOS, _this);
}
else
{
logprintf("%s:%d : ptsCheckForEosOnUnderflowIdleTask ID %d already running, ignore underflow", __FUNCTION__, __LINE__, (int)privateContext->ptsCheckForEosOnUnderflowIdleTaskId);
}
}
else
{
logprintf("%s:%d : video_dec not available", __FUNCTION__, __LINE__);
_this->NotifyEOS();
}
}
else
{
_this->aamp->ScheduleRetune(eGST_ERROR_UNDERFLOW, type);
}
}
else
{
_this->aamp->ScheduleRetune(eGST_ERROR_UNDERFLOW, type);
}
}
/**
* @brief Callback invoked a PTS error is encountered
* @param[in] object pointer to element raising the callback
* @param[in] arg0 number of arguments
* @param[in] arg1 array of arguments
* @param[in] _this pointer to AAMPGstPlayer instance
*/
static void AAMPGstPlayer_OnGstPtsErrorCb(GstElement* object, guint arg0, gpointer arg1,
AAMPGstPlayer * _this)
{
logprintf("## %s() : Got PTS error message from %s ##", __FUNCTION__, GST_ELEMENT_NAME(object));
if (AAMPGstPlayer_isVideoDecoder(GST_ELEMENT_NAME(object), _this))
{
_this->aamp->ScheduleRetune(eGST_ERROR_PTS, eMEDIATYPE_VIDEO);
}
else if (aamp_StartsWith(GST_ELEMENT_NAME(object), "brcmaudiodecoder") == true)
{
_this->aamp->ScheduleRetune(eGST_ERROR_PTS, eMEDIATYPE_AUDIO);
}
}
static gboolean buffering_timeout (gpointer data)
{
AAMPGstPlayer * _this = (AAMPGstPlayer *) data;
if (_this && _this->privateContext)
{
AAMPGstPlayerPriv * privateContext = _this->privateContext;
if (_this->privateContext->buffering_in_progress)
{
guint bytes = 0, frames = DEFAULT_BUFFERING_QUEUED_FRAMES_MIN+1; // if queue_depth property, or video_dec, doesn't exist move to next state.
if (_this->privateContext->video_dec)
{
g_object_get(_this->privateContext->video_dec,"buffered_bytes",&bytes,NULL);
g_object_get(_this->privateContext->video_dec,"queued_frames",&frames,NULL);
}
/* DELIA-34654: Disable re-tune on buffering timeout for DASH as unlike HLS,
DRM key acquisition can end after injection, and buffering is not expected
to be completed by the 1 second timeout
*/
if (G_UNLIKELY(( _this->aamp->getStreamType() < 20) && (privateContext->buffering_timeout_cnt == 0 ) && gpGlobalConfig->reTuneOnBufferingTimeout && (privateContext->numberOfVideoBuffersSent > 0)))
{
logprintf("%s:%d Schedule retune. numberOfVideoBuffersSent %d bytes %u frames %u", __FUNCTION__, __LINE__, privateContext->numberOfVideoBuffersSent, bytes, frames);
privateContext->buffering_in_progress = false;
_this->DumpDiagnostics();
_this->aamp->ScheduleRetune(eGST_ERROR_VIDEO_BUFFERING, eMEDIATYPE_VIDEO);
}
else if (bytes > DEFAULT_BUFFERING_QUEUED_BYTES_MIN || frames > DEFAULT_BUFFERING_QUEUED_FRAMES_MIN || privateContext->buffering_timeout_cnt-- == 0)
{
logprintf("%s: Set pipeline state to %s - buffering_timeout_cnt %u bytes %u frames %u", __FUNCTION__, gst_element_state_get_name(_this->privateContext->buffering_target_state), (_this->privateContext->buffering_timeout_cnt+1), bytes, frames);
gst_element_set_state (_this->privateContext->pipeline, _this->privateContext->buffering_target_state);
_this->privateContext->buffering_in_progress = false;
}
}
if (!_this->privateContext->buffering_in_progress)
{
//reset timer id after buffering operation is completed
_this->privateContext->bufferingTimeoutTimerId = 0;
}
return _this->privateContext->buffering_in_progress;
}
else
{
logprintf("%s:%d in buffering_timeout got invalid or NULL handle ! _this = %p _this->privateContext = %p ", __FUNCTION__, __LINE__,
_this, (_this? _this->privateContext: NULL) );
return false;
}
}
/**
* @brief Called from the mainloop when a message is available on the bus
* @param[in] bus the GstBus that sent the message
* @param[in] msg the GstMessage
* @param[in] _this pointer to AAMPGstPlayer instance
* @retval FALSE if the event source should be removed.
*/
static gboolean bus_message(GstBus * bus, GstMessage * msg, AAMPGstPlayer * _this)
{
GError *error;
gchar *dbg_info;
bool isPlaybinStateChangeEvent;
switch (GST_MESSAGE_TYPE(msg))
{ // see https://developer.gnome.org/gstreamer/stable/gstreamer-GstMessage.html#GstMessage
case GST_MESSAGE_ERROR:
gst_message_parse_error(msg, &error, &dbg_info);
g_printerr("GST_MESSAGE_ERROR %s: %s\n", GST_OBJECT_NAME(msg->src), error->message);
char errorDesc[MAX_ERROR_DESCRIPTION_LENGTH];
memset(errorDesc, '\0', MAX_ERROR_DESCRIPTION_LENGTH);
strncpy(errorDesc, "GstPipeline Error:", 18);
strncat(errorDesc, error->message, MAX_ERROR_DESCRIPTION_LENGTH - 18 - 1);
if (strstr(error->message, "video decode error") != NULL)
{
_this->aamp->SendErrorEvent(AAMP_TUNE_GST_PIPELINE_ERROR, errorDesc, false);
}
else if(strstr(error->message, "HDCP Compliance Check Failure") != NULL)
{
// Trying to play a 4K content on a non-4K TV .Report error to XRE with no retune
_this->aamp->SendErrorEvent(AAMP_TUNE_HDCP_COMPLIANCE_ERROR, errorDesc, false);
}
else if (strstr(error->message, "Internal data stream error") && _this->aamp->mUseRetuneForGSTInternalError)
{
// This can be executed only for Peacock when it hits Internal data stream error.
AAMPLOG_WARN("%s:%d Schedule retune for GstPipeline Error", __FUNCTION__, __LINE__);
_this->aamp->ScheduleRetune(eGST_ERROR_GST_PIPELINE_INTERNAL, eMEDIATYPE_VIDEO);
}
else
{
_this->aamp->SendErrorEvent(AAMP_TUNE_GST_PIPELINE_ERROR, errorDesc);
}
g_printerr("Debug Info: %s\n", (dbg_info) ? dbg_info : "none");
g_clear_error(&error);
g_free(dbg_info);
break;
case GST_MESSAGE_WARNING:
gst_message_parse_warning(msg, &error, &dbg_info);
g_printerr("GST_MESSAGE_WARNING %s: %s\n", GST_OBJECT_NAME(msg->src), error->message);
if (gpGlobalConfig->decoderUnavailableStrict && strstr(error->message, "No decoder available") != NULL)
{
char warnDesc[MAX_ERROR_DESCRIPTION_LENGTH];
snprintf( warnDesc, MAX_ERROR_DESCRIPTION_LENGTH, "GstPipeline Error:%s", error->message );
// decoding failures due to unsupported codecs are received as warnings, i.e.