Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #ifndef FIREBOLT_RIALTO_MEDIA_PIPELINE_H_
21 : #define FIREBOLT_RIALTO_MEDIA_PIPELINE_H_
22 :
23 : #include "AttachedSources.h"
24 : #include "IClientController.h"
25 : #include "IControlClient.h"
26 : #include "IMediaFrameWriter.h"
27 : #include "IMediaPipeline.h"
28 : #include "IMediaPipelineIpc.h"
29 : #include <atomic>
30 : #include <condition_variable>
31 : #include <map>
32 : #include <memory>
33 : #include <mutex>
34 : #include <string>
35 : #include <vector>
36 :
37 : namespace firebolt::rialto
38 : {
39 : /**
40 : * @brief IMediaPipeline factory class definition.
41 : */
42 : class MediaPipelineFactory : public IMediaPipelineFactory
43 : {
44 : public:
45 3 : MediaPipelineFactory() = default;
46 3 : ~MediaPipelineFactory() override = default;
47 :
48 : std::unique_ptr<IMediaPipeline> createMediaPipeline(std::weak_ptr<IMediaPipelineClient> client,
49 : const VideoRequirements &videoRequirements) const override;
50 :
51 : /**
52 : * @brief IMediaPipeline factory method with factory parameters for mock injection.
53 : *
54 : * @param[in] client : The Rialto media player client.
55 : * @param[in] videoRequirements : The video decoder requirements for the MediaPipeline session.
56 : * @param[in] mediaPipelineIpcFactory : This was added for the test environment where a mock object needs to be passed in.
57 : * @param[in] clientController : This was added for the test environment where a mock object needs to be passed in.
58 : *
59 : * @retval the new backend instance or null on error.
60 : */
61 : std::unique_ptr<IMediaPipeline>
62 : createMediaPipeline(std::weak_ptr<IMediaPipelineClient> client, const VideoRequirements &videoRequirements,
63 : std::weak_ptr<client::IMediaPipelineIpcFactory> mediaPipelineIpcFactory,
64 : std::weak_ptr<client::IClientController> clientController) const;
65 : };
66 :
67 : }; // namespace firebolt::rialto
68 :
69 : namespace firebolt::rialto::client
70 : {
71 : class IMediaPipelineAndIControlClient : public IMediaPipeline, public IControlClient
72 : {
73 : };
74 :
75 : /**
76 : * @brief The definition of the MediaPipeline.
77 : */
78 : class MediaPipeline : public IMediaPipelineAndIControlClient, public IMediaPipelineIpcClient
79 : {
80 : public:
81 : /**
82 : * @brief The possible states of the MediaPipeline.
83 : */
84 : enum class State
85 : {
86 : IDLE, /**< The MediaPipeline is idle. */
87 : BUFFERING, /**< The MediaPipeline is buffering data. */
88 : PLAYING, /**< The MediaPipeline is playing. */
89 : SEEKING, /**< The MediaPipeline is seeking position. */
90 : FAILURE, /**< The MediaPipeline is failed. */
91 : END_OF_STREAM /**< The MediaPipeline is at the end of stream. */
92 : };
93 :
94 : /**
95 : * @brief The constructor.
96 : *
97 : * @param[in] client : The Rialto media player client.
98 : * @param[in] videoRequirements : The video decoder requirements for the MediaPipeline session.
99 : * @param[in] mediaPipelineIpcFactory : The media player ipc factory.
100 : * @param[in] mediaFrameWriterFactory : The media frame writer factory.
101 : * @param[in] clientController : The shared memory manager.
102 : */
103 : MediaPipeline(std::weak_ptr<IMediaPipelineClient> client, const VideoRequirements &videoRequirements,
104 : const std::shared_ptr<IMediaPipelineIpcFactory> &mediaPipelineIpcFactory,
105 : const std::shared_ptr<common::IMediaFrameWriterFactory> &mediaFrameWriterFactory,
106 : IClientController &clientController);
107 :
108 : /**
109 : * @brief Virtual destructor.
110 : */
111 : virtual ~MediaPipeline();
112 :
113 : bool load(MediaType type, const std::string &mimeType, const std::string &url, bool isLive) override;
114 :
115 : bool attachSource(const std::unique_ptr<IMediaPipeline::MediaSource> &source) override;
116 :
117 : bool removeSource(int32_t id) override;
118 :
119 : bool allSourcesAttached() override;
120 :
121 : bool play(bool &async) override;
122 :
123 : bool pause() override;
124 :
125 : bool stop() override;
126 :
127 : bool setPlaybackRate(double rate) override;
128 :
129 : bool setPosition(int64_t position) override;
130 :
131 : bool getPosition(int64_t &position) override;
132 :
133 : bool setImmediateOutput(int32_t sourceId, bool immediateOutput) override;
134 :
135 : bool setReportDecodeErrors(int32_t sourceId, bool reportDecodeErrors) override;
136 :
137 : bool setMetricSubscription(const std::vector<MetricType> &types) override;
138 :
139 : bool getQueuedFrames(int32_t sourceId, uint32_t &queuedFrames) override;
140 :
141 : bool getImmediateOutput(int32_t sourceId, bool &immediateOutput) override;
142 :
143 : bool getStats(int32_t sourceId, uint64_t &renderedFrames, uint64_t &droppedFrames) override;
144 :
145 : bool getFrameCounters(int32_t sourceId, uint64_t &renderedFrames, uint64_t &droppedFrames,
146 : uint64_t &corruptedFrames, uint64_t ×tamp) override;
147 :
148 : bool setVideoWindow(uint32_t x, uint32_t y, uint32_t width, uint32_t height) override;
149 :
150 : bool haveData(MediaSourceStatus status, uint32_t needDataRequestId) override;
151 :
152 : AddSegmentStatus addSegment(uint32_t needDataRequestId, const std::unique_ptr<MediaSegment> &mediaSegment) override;
153 :
154 : std::weak_ptr<IMediaPipelineClient> getClient() override;
155 :
156 : void notifyPlaybackState(PlaybackState state) override;
157 :
158 : void notifyPosition(int64_t position) override;
159 :
160 : void notifyNetworkState(NetworkState state) override;
161 :
162 : void notifyNeedMediaData(int32_t sourceId, size_t frameCount, uint32_t requestId,
163 : const std::shared_ptr<MediaPlayerShmInfo> &shmInfo) override;
164 :
165 : void notifyQos(int32_t sourceId, const QosInfo &qosInfo) override;
166 :
167 : void notifyMetric(const MetricEvent &metric) override;
168 :
169 : void notifyBufferUnderflow(int32_t sourceId) override;
170 :
171 : void notifyFirstFrameReceived(int32_t sourceId) override;
172 :
173 : void notifyPlaybackError(int32_t sourceId, PlaybackError error) override;
174 :
175 : void notifyVideoData(bool hasData) override;
176 :
177 : void notifyAudioData(bool hasData) override;
178 :
179 : void notifySourceFlushed(int32_t sourceId) override;
180 :
181 : void notifyPlaybackInfo(const PlaybackInfo &playbackInfo) override;
182 :
183 : bool renderFrame() override;
184 :
185 : bool setVolume(double targetVolume, uint32_t volumeDuration, EaseType easeType) override;
186 :
187 : bool getVolume(double ¤tVolume) override;
188 :
189 : bool setMute(int32_t sourceId, bool mute) override;
190 :
191 : bool getMute(int32_t sourceId, bool &mute) override;
192 :
193 : bool setTextTrackIdentifier(const std::string &textTrackIdentifier) override;
194 :
195 : bool getTextTrackIdentifier(std::string &textTrackIdentifier) override;
196 :
197 : bool setLowLatency(bool lowLatency) override;
198 :
199 : bool setSync(bool sync) override;
200 :
201 : bool getSync(bool &sync) override;
202 :
203 : bool setSyncOff(bool syncOff) override;
204 :
205 : bool setStreamSyncMode(int32_t sourceId, int32_t streamSyncMode) override;
206 :
207 : bool getStreamSyncMode(int32_t &streamSyncMode) override;
208 :
209 : bool getDuration(int64_t &duration) override;
210 :
211 : bool flush(int32_t sourceId, bool resetTime, bool &async) override;
212 :
213 : bool setSourcePosition(int32_t sourceId, int64_t position, bool resetTime, double appliedRate,
214 : uint64_t stopPosition) override;
215 :
216 : bool setSubtitleOffset(int32_t sourceId, int64_t position) override;
217 :
218 : bool processAudioGap(int64_t position, uint32_t duration, int64_t discontinuityGap, bool audioAac) override;
219 :
220 : bool setBufferingLimit(uint32_t limitBufferingMs) override;
221 :
222 : bool getBufferingLimit(uint32_t &limitBufferingMs) override;
223 :
224 : bool setUseBuffering(bool useBuffering) override;
225 :
226 : bool getUseBuffering(bool &useBuffering) override;
227 :
228 : bool switchSource(const std::unique_ptr<MediaSource> &source) override;
229 :
230 : void notifyApplicationState(ApplicationState state) override;
231 :
232 : protected:
233 : /**
234 : * @brief The need data request data.
235 : */
236 : struct NeedDataRequest
237 : {
238 : int32_t sourceId; /**< The source id. */
239 : std::shared_ptr<MediaPlayerShmInfo> shmInfo; /**< The shared memory information. */
240 : std::unique_ptr<common::IMediaFrameWriter> frameWriter; /**< The frame writer used to add segments. */
241 : };
242 :
243 : /**
244 : * @brief The media player client.
245 : */
246 : std::weak_ptr<IMediaPipelineClient> m_mediaPipelineClient;
247 :
248 : /**
249 : * @brief The media player ipc object.
250 : */
251 : std::unique_ptr<IMediaPipelineIpc> m_mediaPipelineIpc;
252 :
253 : /**
254 : * @brief The rialto shared memory manager object.
255 : */
256 : IClientController &m_clientController;
257 :
258 : /**
259 : * @brief The Need data request map.
260 : * Key: requestId
261 : * Value: NeedDataRequest
262 : */
263 : std::map<uint32_t, std::shared_ptr<NeedDataRequest>> m_needDataRequestMap;
264 :
265 : /**
266 : * @brief The current application state. Protected by m_needDataRequestMapMutex
267 : */
268 : ApplicationState m_currentAppState;
269 :
270 : /**
271 : * @brief The need data request map mutex.
272 : */
273 : std::mutex m_needDataRequestMapMutex;
274 :
275 : /**
276 : * @brief The media frame writer factory.
277 : */
278 : std::shared_ptr<common::IMediaFrameWriterFactory> m_mediaFrameWriterFactory;
279 :
280 : /**
281 : * @brief The shared memory mutex. Guards m_shmHandle.
282 : */
283 : std::mutex m_shmMutex;
284 :
285 : /**
286 : * @brief Cached shared memory handle, refreshed by the subscribeSharedMemoryHandle() observer.
287 : */
288 : std::shared_ptr<ISharedMemoryHandle> m_shmHandle;
289 :
290 : /**
291 : * @brief Token for the shared memory handle observer subscription.
292 : */
293 : int m_shmHandleObserverId{-1};
294 :
295 : /**
296 : * @brief The current state of the MediaPipeline.
297 : */
298 : std::atomic<State> m_currentState;
299 :
300 : /**
301 : * @brief The flush request mutex.
302 : */
303 : std::mutex m_flushMutex;
304 :
305 : /**
306 : * @brief The attach source mutex.
307 : */
308 : std::mutex m_attachSourceMutex;
309 :
310 : /**
311 : * @brief The attach source condition variable.
312 : */
313 : std::condition_variable m_attachSourceCond;
314 :
315 : /**
316 : * @brief Whether attachSource is currently in progress.
317 : */
318 : bool m_attachingSource;
319 :
320 : /**
321 : * @brief The container with attached source id <-> MediaSourceType mapping
322 : */
323 : AttachedSources m_attachedSources;
324 :
325 : /**
326 : * @brief Sets the new internal MediaPipeline state based on the NetworkState.
327 : *
328 : * @param[in] state : The new NeworkState.
329 : */
330 : void updateState(NetworkState state);
331 :
332 : /**
333 : * @brief Sets the new internal MediaPipeline state based on the PlaybackState.
334 : *
335 : * @param[in] state : The new PlaybackState.
336 : */
337 : void updateState(PlaybackState state);
338 :
339 : /**
340 : * @brief Handles a have data request.
341 : *
342 : * @param[in] status : The status
343 : * @param[in] needDataRequestId : Need data request id
344 : *
345 : * @retval true on success.
346 : */
347 : bool handleHaveData(MediaSourceStatus status, uint32_t needDataRequestId);
348 :
349 : /**
350 : * @brief Handles a set position request.
351 : *
352 : * @param[in] position : The playback position in nanoseconds.
353 : *
354 : * @retval true on success.
355 : */
356 : bool handleSetPosition(int64_t position);
357 :
358 : /**
359 : * @brief Discards the need data request with id.
360 : *
361 : * @param[in] needDataRequestId : Need data request id
362 : */
363 : void discardNeedDataRequest(uint32_t needDataRequestId);
364 : };
365 :
366 : }; // namespace firebolt::rialto::client
367 :
368 : #endif // FIREBOLT_RIALTO_MEDIA_PIPELINE_H_
|