RDK Documentation (Open Sourced RDK Components)
mediasourcepipeline.h
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2017 RDK Management
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 MEDIASOURCEPIPELINE_H_
21 #define MEDIASOURCEPIPELINE_H_
22 
23 #include <gst/app/gstappsrc.h>
24 #include <gst/gst.h>
25 
26 #include <cstdio>
27 #include <fstream>
28 #include <sstream>
29 #include <string>
30 #include <vector>
31 
32 #include <rtRemote.h>
33 #include <rtError.h>
34 
35 /**
36  * @addtogroup MSE Player
37  * @{
38  */
39 
40 enum ReadStatus { kDone = 0, kFrameRead, kPerformSeek };
41 enum PipelineType { kAudioVideo = 0, kAudioOnly, kVideoOnly };
42 
43 enum AVType { kAudio = 0, kVideo };
44 
45 struct AVFrame {
46  guint8* data_;
47  int32_t size_;
48  int64_t timestamp_us_;
49 };
50 
51 class MediaSourcePipeline : public rtObject {
52  public:
53  rtDeclareObject(MediaSourcePipeline, rtObject);
54  rtMethodNoArgAndNoReturn("suspend", suspend);
55  rtMethodNoArgAndNoReturn("resume", resume);
56 
57  explicit MediaSourcePipeline(std::string frame_files_path);
58  virtual ~MediaSourcePipeline();
59  virtual bool Start();
60  virtual void HandleKeyboardInput(unsigned int key);
61  rtError suspend();
62  rtError resume();
63 
64  // functions called by glib static functions
65 /**
66  * @brief This API handles different message types.
67  *
68  * Types can be errors, warnings, End of stream, state changed.
69  *
70  * @param[in] message Indicates the message type.
71  *
72  * @return Returns true on success, appropriate error code otherwise.
73  */
74  gboolean HandleMessage(GstMessage* message);
75 
76 /**
77  * @brief This signal callback is called when appsrc needs data.
78  *
79  * @param[in] p_src App source.
80  */
81  void StartFeedingAppSource(GstAppSrc* p_src);
82 
83 /**
84  * @brief This signal callback is called when appsrc have enough-data.
85  *
86  * @param[in] p_src App source.
87  */
88  void StopFeedingAppSource(GstAppSrc* p_src);
89 
90 /**
91  * @brief This API is to seek the app's position and set the position.
92  *
93  * @param[in] p_src App source.
94  * @param[in] position Position to set.
95  */
96  void SetNewAppSourceReadPosition(GstAppSrc* p_src, guint64 position);
97 
98 /**
99  * @brief This API links auto pad added elements in audio pipeline or video pipeline depending upon the element audio/video.
100  *
101  * @param[in] element Source element
102  * @param[in] pad Capabilities
103  *
104  * @return Returns RT_OK on success, appropriate error code otherwise.
105  */
106  void OnAutoPadAddedMediaSource(GstElement* element, GstPad* pad);
107 
108 /**
109  * @brief This API checks for the dynamic addition of real audio sink.
110  *
111  * @param[in] element Source element
112  *
113  * @return Returns RT_OK on success, appropriate error code otherwise.
114  */
115  void OnAutoElementAddedMediaSource(GstElement* element);
116 
117 /**
118  * @brief This API reads data from both the audio/video timestamp file, raw frames file and push the details to appsrc.
119  *
120  * if it fails reading,assume end of file is reached and need to peform a seek.
121  *
122  * @return Returns TRUE on success, appropriate error code otherwise.
123  */
124  gboolean ReadVideoFrame();
125 
126 /**
127  * @brief This API reads the audio frame details and push the details to appsrc.
128  *
129  * @return Returns TRUE on success, appropriate error code otherwise.
130  */
131  gboolean ReadAudioFrame();
132 
133 /**
134  * @brief This API queries the playback position.
135  *
136  * Resets the file counter back to before beginning if it is complete.
137  *
138  * @return Returns RT_OK on success, appropriate error code otherwise.
139  */
140  gboolean StatusPoll();
141 
142 /**
143  * @brief This API is used by the mse source to perform its own seek before
144  * starting the read data again.
145  *
146  * @return Returns False.
147  */
148  gboolean ChunkDemuxerSeek();
149 
150 /**
151  * @brief This signal callback is called when "source-setup" is emitted.
152  */
153  void sourceChanged();
154 
155  private:
156  bool Build();
157  void Init();
158  void Destroy();
159  void StopAllTimeouts();
160  void CloseAllFiles();
161  void PerformSeek();
162  ReadStatus GetNextFrame(AVFrame* frame, AVType type);
163  bool PushFrameToAppSrc(const AVFrame& frame, AVType type);
164  bool ShouldBeReading(AVType av);
165  void SetShouldBeReading(bool is_reading, AVType av);
166  void CalculateCurrentEndTime();
167  bool ShouldPerformSeek();
168  int64_t GetCurrentStartTimeMicroseconds() const;
169  bool IsPlaybackOver();
170  void AddPlaybackPositionToHistory(int64_t position);
171  bool IsPlaybackStalled();
172  bool HasPlaybackAdvanced();
173  void ResetPlaybackHistory();
174  void DoPause();
175  void finishPipelineLinkingAndStartPlaybackIfNeeded();
176 
177  std::string frame_files_path_;
178  int32_t current_file_counter_;
179  FILE* current_video_file_;
180  FILE* current_video_timestamp_file_;
181  FILE* current_audio_file_;
182  FILE* current_audio_timestamp_file_;
183  bool seeking_;
184  GstElement* pipeline_;
185  GstAppSrc* appsrc_source_video_;
186  GstAppSrc* appsrc_source_audio_;
187  GstElement* video_sink_;
188  GstElement* audio_sink_;
189  std::vector<GstElement*> ms_video_pipeline_;
190  std::vector<GstElement*> ms_audio_pipeline_;
191  bool should_be_reading_[2];
192  float playback_position_secs_;
193  float current_end_time_secs_;
194  guint video_frame_timeout_handle_;
195  guint audio_frame_timeout_handle_;
196  guint status_timeout_handle_;
197  int32_t current_playback_history_cnt_;
198  std::vector<int64_t> playback_position_history_;
199  bool playback_started_;
200  bool is_playing_;
201  PipelineType pipeline_type_;
202 
203  GstElement* source_;
204  GstCaps *appsrc_caps_video_;
205  GstCaps *appsrc_caps_audio_;
206  bool pause_before_seek_;
207  bool is_active_;
208  int64_t seek_offset_;
209 
210 };
211 
212 #endif // MEDIASOURCEPIPELINE_H_
213 
214 /**
215  * @}
216  */
217 
MediaSourcePipeline::sourceChanged
void sourceChanged()
This signal callback is called when "source-setup" is emitted.
Definition: mediasourcepipeline.cpp:128
MediaSourcePipeline::SetNewAppSourceReadPosition
void SetNewAppSourceReadPosition(GstAppSrc *p_src, guint64 position)
This API is to seek the app's position and set the position.
Definition: mediasourcepipeline.cpp:454
MediaSourcePipeline::OnAutoPadAddedMediaSource
void OnAutoPadAddedMediaSource(GstElement *element, GstPad *pad)
This API links auto pad added elements in audio pipeline or video pipeline depending upon the element...
Definition: mediasourcepipeline.cpp:457
MediaSourcePipeline::ChunkDemuxerSeek
gboolean ChunkDemuxerSeek()
This API is used by the mse source to perform its own seek before starting the read data again.
Definition: mediasourcepipeline.cpp:778
MediaSourcePipeline::StatusPoll
gboolean StatusPoll()
This API queries the playback position.
Definition: mediasourcepipeline.cpp:306
MediaSourcePipeline::HandleMessage
gboolean HandleMessage(GstMessage *message)
This API handles different message types.
Definition: mediasourcepipeline.cpp:136
MediaSourcePipeline::OnAutoElementAddedMediaSource
void OnAutoElementAddedMediaSource(GstElement *element)
This API checks for the dynamic addition of real audio sink.
Definition: mediasourcepipeline.cpp:496
MediaSourcePipeline::StopFeedingAppSource
void StopFeedingAppSource(GstAppSrc *p_src)
This signal callback is called when appsrc have enough-data.
Definition: mediasourcepipeline.cpp:438
AVFrame
Definition: mediasourcepipeline.h:45
MediaSourcePipeline::ReadVideoFrame
gboolean ReadVideoFrame()
This API reads data from both the audio/video timestamp file, raw frames file and push the details to...
Definition: mediasourcepipeline.cpp:353
MediaSourcePipeline::StartFeedingAppSource
void StartFeedingAppSource(GstAppSrc *p_src)
This signal callback is called when appsrc needs data.
Definition: mediasourcepipeline.cpp:412
MediaSourcePipeline::ReadAudioFrame
gboolean ReadAudioFrame()
This API reads the audio frame details and push the details to appsrc.
Definition: mediasourcepipeline.cpp:382
MediaSourcePipeline
Definition: mediasourcepipeline.h:51