-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathGstGenericPlayer.h
More file actions
493 lines (440 loc) · 19.6 KB
/
GstGenericPlayer.h
File metadata and controls
493 lines (440 loc) · 19.6 KB
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
/*
* If not stated otherwise in this file or this component's LICENSE file the
* following copyright and licenses apply:
*
* Copyright 2022 Sky UK
*
* 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.
*/
#ifndef FIREBOLT_RIALTO_SERVER_GST_GENERIC_PLAYER_H_
#define FIREBOLT_RIALTO_SERVER_GST_GENERIC_PLAYER_H_
#include "GenericPlayerContext.h"
#include "IFlushWatcher.h"
#include "IGlibWrapper.h"
#include "IGstDispatcherThread.h"
#include "IGstDispatcherThreadClient.h"
#include "IGstGenericPlayer.h"
#include "IGstGenericPlayerPrivate.h"
#include "IGstInitialiser.h"
#include "IGstProtectionMetadataHelperFactory.h"
#include "IGstSrc.h"
#include "IGstWrapper.h"
#include "ITimer.h"
#include "IWorkerThread.h"
#include "tasks/IGenericPlayerTaskFactory.h"
#include "tasks/IPlayerTask.h"
#include <IMediaPipeline.h>
#include <atomic>
#include <memory>
#include <string>
#include <utility>
#include <vector>
namespace firebolt::rialto::server
{
constexpr uint32_t kMinPrimaryVideoWidth{1920};
constexpr uint32_t kMinPrimaryVideoHeight{1080};
/**
* @brief IGstGenericPlayer factory class definition.
*/
class GstGenericPlayerFactory : public IGstGenericPlayerFactory
{
public:
/**
* @brief Weak pointer to the singleton factory object.
*/
static std::weak_ptr<IGstGenericPlayerFactory> m_factory;
std::unique_ptr<IGstGenericPlayer>
createGstGenericPlayer(IGstGenericPlayerClient *client, IDecryptionService &decryptionService, MediaType type,
const VideoRequirements &videoRequirements,
const std::shared_ptr<firebolt::rialto::wrappers::IRdkGstreamerUtilsWrapperFactory>
&rdkGstreamerUtilsWrapperFactory) override;
};
/**
* @brief The definition of the GstGenericPlayer.
*/
class GstGenericPlayer : public IGstGenericPlayer, public IGstGenericPlayerPrivate, public IGstDispatcherThreadClient
{
public:
/**
* @brief The constructor.
*
* @param[in] client : The gstreamer player client.
* @param[in] decryptionService : The decryption service
* @param[in] type : The media type the gstreamer player shall support.
* @param[in] videoRequirements : The video requirements for the playback.
* @param[in] gstWrapper : The gstreamer wrapper.
* @param[in] glibWrapper : The glib wrapper.
* @param[in] gstInitialiser : The gst initialiser
* @param[in] flushWatcher : The flush watcher
* @param[in] gstSrcFactory : The gstreamer rialto src factory.
* @param[in] timerFactory : The Timer factory
* @param[in] taskFactory : The task factory
* @param[in] workerThreadFactory : The worker thread factory
* @param[in] gstDispatcherThreadFactory : The gst dispatcher thread factory
*/
GstGenericPlayer(IGstGenericPlayerClient *client, IDecryptionService &decryptionService, MediaType type,
const VideoRequirements &videoRequirements,
const std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> &gstWrapper,
const std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> &glibWrapper,
const std::shared_ptr<firebolt::rialto::wrappers::IRdkGstreamerUtilsWrapper> &rdkGstreamerUtilsWrapper,
const IGstInitialiser &gstInitialiser, std::unique_ptr<IFlushWatcher> &&flushWatcher,
const std::shared_ptr<IGstSrcFactory> &gstSrcFactory,
std::shared_ptr<common::ITimerFactory> timerFactory,
std::unique_ptr<IGenericPlayerTaskFactory> taskFactory,
std::unique_ptr<IWorkerThreadFactory> workerThreadFactory,
std::unique_ptr<IGstDispatcherThreadFactory> gstDispatcherThreadFactory,
std::shared_ptr<IGstProtectionMetadataHelperFactory> gstProtectionMetadataFactory);
/**
* @brief Virtual destructor.
*/
virtual ~GstGenericPlayer();
void attachSource(const std::unique_ptr<IMediaPipeline::MediaSource> &mediaSource) override;
void allSourcesAttached() override;
void play(bool &async) override;
void pause() override;
void stop() override;
void attachSamples(const IMediaPipeline::MediaSegmentVector &mediaSegments) override;
void attachSamples(const std::shared_ptr<IDataReader> &dataReader) override;
void setPosition(std::int64_t position) override;
void setVideoGeometry(int x, int y, int width, int height) override;
void setEos(const firebolt::rialto::MediaSourceType &type) override;
void setPlaybackRate(double rate) override;
bool getPosition(std::int64_t &position) override;
bool setImmediateOutput(const MediaSourceType &mediaSourceType, bool immediateOutput) override;
bool getImmediateOutput(const MediaSourceType &mediaSourceType, bool &immediateOutput) override;
bool getStats(const MediaSourceType &mediaSourceType, uint64_t &renderedFrames, uint64_t &droppedFrames) override;
void setVolume(double targetVolume, uint32_t volumeDuration, firebolt::rialto::EaseType easeType) override;
bool getVolume(double &volume) override;
void setMute(const MediaSourceType &mediaSourceType, bool mute) override;
bool getMute(const MediaSourceType &mediaSourceType, bool &mute) override;
void setTextTrackIdentifier(const std::string &textTrackIdentifier) override;
bool getTextTrackIdentifier(std::string &textTrackIdentifier) override;
bool setLowLatency(bool lowLatency) override;
bool setSync(bool sync) override;
bool getSync(bool &sync) override;
bool setSyncOff(bool syncOff) override;
bool setStreamSyncMode(const MediaSourceType &mediaSourceType, int32_t streamSyncMode) override;
bool getStreamSyncMode(int32_t &streamSyncMode) override;
void ping(std::unique_ptr<IHeartbeatHandler> &&heartbeatHandler) override;
void flush(const MediaSourceType &mediaSourceType, bool resetTime, bool &async) override;
void setSourcePosition(const MediaSourceType &mediaSourceType, int64_t position, bool resetTime, double appliedRate,
uint64_t stopPosition) override;
void setSubtitleOffset(int64_t position) override;
void processAudioGap(int64_t position, uint32_t duration, int64_t discontinuityGap, bool audioAac) override;
void setBufferingLimit(uint32_t limitBufferingMs) override;
bool getBufferingLimit(uint32_t &limitBufferingMs) override;
void setUseBuffering(bool useBuffering) override;
bool getUseBuffering(bool &useBuffering) override;
void switchSource(const std::unique_ptr<IMediaPipeline::MediaSource> &mediaSource) override;
private:
void scheduleNeedMediaData(GstAppSrc *src) override;
void scheduleEnoughData(GstAppSrc *src) override;
void scheduleAudioUnderflow() override;
void scheduleVideoUnderflow() override;
void scheduleAllSourcesAttached() override;
bool setVideoSinkRectangle() override;
bool setImmediateOutput() override;
bool setShowVideoWindow() override;
bool setLowLatency() override;
bool setSync() override;
bool setSyncOff() override;
bool setStreamSyncMode(const MediaSourceType &type) override;
bool setRenderFrame() override;
bool setBufferingLimit() override;
bool setUseBuffering() override;
void notifyNeedMediaData(const MediaSourceType mediaSource) override;
GstBuffer *createBuffer(const IMediaPipeline::MediaSegment &mediaSegment) const override;
void attachData(const firebolt::rialto::MediaSourceType mediaType) override;
void updateAudioCaps(int32_t rate, int32_t channels, const std::shared_ptr<CodecData> &codecData) override;
void updateVideoCaps(int32_t width, int32_t height, Fraction frameRate,
const std::shared_ptr<CodecData> &codecData) override;
void addAudioClippingToBuffer(GstBuffer *buffer, uint64_t clippingStart, uint64_t clippingEnd) const override;
GstStateChangeReturn changePipelineState(GstState newState) override;
int64_t getPosition(GstElement *element) override;
void startPositionReportingAndCheckAudioUnderflowTimer() override;
void stopPositionReportingAndCheckAudioUnderflowTimer() override;
void startSubtitleClockResyncTimer() override;
void stopSubtitleClockResyncTimer() override;
void stopWorkerThread() override;
void cancelUnderflow(firebolt::rialto::MediaSourceType mediaSource) override;
void setPendingPlaybackRate() override;
void renderFrame() override;
void handleBusMessage(GstMessage *message) override;
void updatePlaybackGroup(GstElement *typefind, const GstCaps *caps) override;
void addAutoVideoSinkChild(GObject *object) override;
void addAutoAudioSinkChild(GObject *object) override;
void removeAutoVideoSinkChild(GObject *object) override;
void removeAutoAudioSinkChild(GObject *object) override;
void pushSampleIfRequired(GstElement *source, const std::string &typeStr) override;
bool reattachSource(const std::unique_ptr<IMediaPipeline::MediaSource> &source) override;
bool hasSourceType(const MediaSourceType &mediaSourceType) const override;
GstElement *getSink(const MediaSourceType &mediaSourceType) const override;
void setSourceFlushed(const MediaSourceType &mediaSourceType) override;
bool isAsync(const MediaSourceType &mediaSourceType) const;
void notifyPlaybackInfo() override;
private:
/**
* @brief Initialises the player pipeline for MSE playback.
*/
void initMsePipeline();
/**
* @brief Gets the flag from gstreamer.
*
* @param[in] nick : The name of the flag in gstreamer.
*
* @retval Value of the flag or 0 on error.
*/
unsigned getGstPlayFlag(const char *nick);
/**
* @brief Callback on source-setup. Called by the Gstreamer thread
*
* @param[in] pipeline : The pipeline the signal was fired from.
* @param[in] source : The source to setup.
* @param[in] self : Reference to the calling object.
*/
static void setupSource(GstElement *pipeline, GstElement *source, GstGenericPlayer *self);
/**
* @brief Callback on element-setup. Called by the Gstreamer thread
*
* @param[in] pipeline : The pipeline the signal was fired from.
* @param[in] element : an element that was added to the playbin hierarchy
* @param[in] self : Reference to the calling object.
*/
static void setupElement(GstElement *pipeline, GstElement *element, GstGenericPlayer *self);
/**
* @brief Callback on element-setup. Called by the Gstreamer thread
*
* @param[in] pipeline : The pipeline the signal was fired from.
* @param[in] bin : the GstBin the element was added to
* @param[in] element : an element that was added to the playbin hierarchy
* @param[in] self : Reference to the calling object.
*/
static void deepElementAdded(GstBin *pipeline, GstBin *bin, GstElement *element, GstGenericPlayer *self);
/**
* @brief Creates a Westeros sink and sets the res-usage flag for a secondary video.
*
* @retval true on success.
*/
bool setWesterossinkSecondaryVideo();
/**
* @brief Creates an "erm" gstreamer context in the pipeline
*
* @retval true on success.
*/
bool setErmContext();
/**
* @brief Terminates the player pipeline.
*/
void termPipeline();
/**
* @brief Shutdown and destroys the worker thread.
*/
void resetWorkerThread();
/**
* @brief Whether native audio should be enabled on the current platform.
*/
bool shouldEnableNativeAudio();
/**
* @brief Sets codec_data in GstCaps if available
*
* @retval True if caps were changed
*/
bool setCodecData(GstCaps *caps, const std::shared_ptr<CodecData> &codecData) const;
/**
* @brief Gets the video sink element child sink if present.
* Only gets children for GstAutoVideoSink's.
*
* @param[in] sink : Sink element to check.
*
* @retval Underlying child video sink or 'sink' if there are no children.
*/
GstElement *getSinkChildIfAutoVideoSink(GstElement *sink) const;
/**
* @brief Gets the audio sink element child sink if present.
* Only gets children for GstAutoAudioSink's.
*
* @param[in] sink : Sink element to check.
*
* @retval Underlying child audio sink or 'sink' if there are no children.
*/
GstElement *getSinkChildIfAutoAudioSink(GstElement *sink) const;
/**
* @brief Gets the decoder element for source type.
*
* @param[in] mediaSourceType : the source type to obtain the decoder for
*
* @retval The decoder, NULL if not found
*/
GstElement *getDecoder(const MediaSourceType &mediaSourceType);
/**
* @brief Gets the parser element for source type.
*
* @param[in] mediaSourceType : the source type to obtain the parser for
*
* @retval The parser, NULL if not found
*/
GstElement *getParser(const MediaSourceType &mediaSourceType);
/**
* @brief Constructs new Audio Attributes structure based on MediaSource
* Called by worker thread only!
*
* @param[in] source : the media source, which contains params needed by Audio Attributes struct
*
* @retval The Audio Attributes structure on success, std::nullopt on failure
*/
std::optional<firebolt::rialto::wrappers::AudioAttributesPrivate>
createAudioAttributes(const std::unique_ptr<IMediaPipeline::MediaSource> &source) const;
/**
* @brief Configures audio caps based on audio attributes.
* Called by worker thread only!
*
* @param[in] pAttrib : The audio attributes.
* @param[out] audioaac : Set to true if AAC, false otherwise.
* @param[in] svpenabled : Whether SVP is enabled.
* @param[in,out] appsrcCaps : The caps to configure.
*/
void configAudioCap(firebolt::rialto::wrappers::AudioAttributesPrivate *pAttrib, bool *audioaac, bool svpenabled,
GstCaps **appsrcCaps);
/**
* @brief Halts audio playback by setting playsink to READY and decodebin to PAUSED.
* Called by worker thread only!
*/
void haltAudioPlayback();
/**
* @brief Resumes audio playback by syncing playsink and decodebin with parent.
* Called by worker thread only!
*/
void resumeAudioPlayback();
/**
* @brief First-time codec switch from AC3 to AAC when no decoder exists yet.
* Called by worker thread only!
*
* @param[in] newAudioCaps : The new audio caps to apply.
*/
void firstTimeSwitchFromAC3toAAC(GstCaps *newAudioCaps);
/**
* @brief Switches the audio codec by unlinking old parser/decoder and linking new ones.
* Called by worker thread only!
*
* @param[in] isAudioAAC : Whether the new codec is AAC.
* @param[in] newAudioCaps : The new audio caps to apply.
*
* @retval true if codec was switched, false if same codec.
*/
bool switchAudioCodec(bool isAudioAAC, GstCaps *newAudioCaps);
/**
* @brief Top-level audio track codec channel switch, ported from rdk_gstreamer_utils_soc.
* Called by worker thread only!
*/
bool performAudioTrackCodecChannelSwitch(const void *pSampleAttr,
firebolt::rialto::wrappers::AudioAttributesPrivate *pAudioAttr,
uint32_t *pStatus, unsigned int *pui32Delay,
long long *pAudioChangeTargetPts, // NOLINT(runtime/int)
const long long *pcurrentDispPts, // NOLINT(runtime/int)
unsigned int *audioChangeStage, GstCaps **appsrcCaps, bool *audioaac,
bool svpenabled, GstElement *aSrc, bool *ret);
/**
* @brief Sets text track position before pushing data
*
* @param[in] source : the subtitle media source
*/
void setTextTrackPositionIfRequired(GstElement *source);
/**
* @brief GstAppSrc does not replace segment, if it's the same as previous one.
* It causes problems with position reporing in amlogic devices, so we need to push
* two segments with different reset time value.
*
* @param[in] source : the media source
*/
void pushAdditionalSegmentIfRequired(GstElement *source);
/**
* @brief Sets the audio and video flags on the pipeline based on the input.
*
* @param[in] enableAudio : Whether to enable audio flags.
*/
void setPlaybinFlags(bool enableAudio = true);
private:
/**
* @brief The player context.
*/
GenericPlayerContext m_context;
/**
* @brief The gstreamer player client.
*/
IGstGenericPlayerClient *m_gstPlayerClient = nullptr;
/**
* @brief The gstreamer wrapper object.
*/
std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> m_gstWrapper;
/**
* @brief The glib wrapper object.
*/
std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> m_glibWrapper;
/**
* @brief The rdk gstreamer utils wrapper object
*/
std::shared_ptr<firebolt::rialto::wrappers::IRdkGstreamerUtilsWrapper> m_rdkGstreamerUtilsWrapper;
/**
* @brief Thread for handling player tasks.
*/
std::unique_ptr<IWorkerThread> m_workerThread;
/**
* @brief Thread for handling gst bus callbacks
*/
std::unique_ptr<IGstDispatcherThread> m_gstDispatcherThread;
/**
* @brief Factory creating timers
*
*/
std::shared_ptr<common::ITimerFactory> m_timerFactory;
/**
* @brief Timer to trigger FinishSourceSetup
*/
std::unique_ptr<firebolt::rialto::common::ITimer> m_finishSourceSetupTimer{nullptr};
/**
* @brief Timer checking audio underflow
*
* Variable can be used only in worker thread
*/
std::unique_ptr<firebolt::rialto::common::ITimer> m_positionReportingAndCheckAudioUnderflowTimer{nullptr};
/**
* @brief Timer reporting playback information
*
* Variable can be used only in worker thread
*/
std::unique_ptr<firebolt::rialto::common::ITimer> m_playbackInfoTimer{nullptr};
/**
* @brief Timer to resync subtitle clock with AV clock
*
* Variable can be used only in worker thread
*/
std::unique_ptr<firebolt::rialto::common::ITimer> m_subtitleClockResyncTimer{nullptr};
/**
* @brief The GstGenericPlayer task factory
*/
std::unique_ptr<IGenericPlayerTaskFactory> m_taskFactory;
/**
* @brief The protection metadata wrapper
*/
std::unique_ptr<IGstProtectionMetadataHelper> m_protectionMetadataWrapper;
/**
* @brief The object used to check flushing state for all sources
*/
std::unique_ptr<IFlushWatcher> m_flushWatcher;
/**
* @brief The ongoing state change operations counter
*/
std::atomic<uint32_t> m_ongoingStateChangesNumber{0};
};
} // namespace firebolt::rialto::server
#endif // FIREBOLT_RIALTO_SERVER_GST_GENERIC_PLAYER_H_