55#include < cstring>
66#include < memory>
77#include < vector>
8+ #include < mutex>
9+ #include < queue>
10+ #include < condition_variable>
811#include " mp4demux.hpp"
912#include " rialto-gst-pipeline.h"
1013
@@ -20,6 +23,21 @@ static int32_t sourceIdVideo;
2023
2124using namespace firebolt ::rialto;
2225
26+ uint32_t WaitForNeedDataRequest (int32_t sourceId, int timeoutMs = 5000 )
27+ {
28+ std::unique_lock<std::mutex> lock (g_needDataMutex);
29+ if (!g_needDataCv.wait_for (lock, std::chrono::milliseconds (timeoutMs), [sourceId]{
30+ return !g_needDataQueue.empty () && g_needDataQueue.front ().sourceId == sourceId;
31+ }))
32+ {
33+ return UINT32_MAX;
34+ }
35+
36+ uint32_t requestId = g_needDataQueue.front ().requestId ;
37+ g_needDataQueue.pop ();
38+ return requestId;
39+ }
40+
2341void LoadAndDemuxSegment (Mp4Demux &mp4Demux, const char *path)
2442{
2543 char fullpath[512 ];
@@ -86,15 +104,13 @@ void ConfigureAudio()
86104 // const char *codec_ptr = trackAudio.codec_data.c_str();
87105 // codecData.data = std::vector<uint8_t>( codec_ptr, &codec_ptr[trackAudio.codec_data.size()] );
88106
89- std::unique_ptr<IMediaPipeline::MediaSourceAudio> sourceAudio =
90- std::make_unique<IMediaPipeline::MediaSourceAudio>(
91- mimeType,
92- hasDrm,
93- alignment,
94- streamFormat,
95- trackAudio.audio .rate ,
96- trackAudio.audio .channels ,
97- std::make_shared<CodecData>(codecData) );
107+ std::unique_ptr<IMediaPipeline::MediaSourceAudio> sourceAudio = std::make_unique<IMediaPipeline::MediaSourceAudio>(
108+ mimeType,
109+ hasDrm,
110+ audioConfig,
111+ SegmentAlignment::UNDEFINED,
112+ streamFormat,
113+ nullptr /* codecData*/ );
98114
99115 gstMediaPipeline->attachSource ( std::move (sourceAudio), sourceIdAudio );
100116}
@@ -146,12 +162,13 @@ void ConfigureComplete()
146162 gstMediaPipeline->allSourcesAttached ();
147163}
148164
149- void InjectAudio ()
165+ void InjectAudio (int32_t needDataId )
150166{
151167 LoadAndDemuxSegment (trackAudio, " audio/chunk-stream0-00001.m4s" );
152168 std::cout << " loading rialtotest /tmp/data/bipbop-gen/audio/chunk-stream0-00001.m4s" << std::endl;
153169
154- size_t segmentCount = trackAudio.getNbSegments (); // Corrected function name
170+ // size_t segmentCount = trackAudio.getNbSegments();
171+ size_t segmentCount = trackAudio.count ();
155172 printf (" adding %zu audio frames\n " , segmentCount);
156173
157174 for (size_t i = 0 ; i < segmentCount; ++i)
@@ -170,17 +187,18 @@ void InjectAudio()
170187 std::memcpy (data, trackAudio.getPtr (i), len);
171188 audioSegment->setData ((uint32_t )len, data);
172189
173- AddSegmentStatus status = gstMediaPipeline->addSegment (0 , audioSegment);
190+ AddSegmentStatus status = gstMediaPipeline->addSegment (needDataId , audioSegment);
174191 assert (status == AddSegmentStatus::OK);
175192 }
176193}
177194
178- void InjectVideo ()
195+ void InjectVideo (int32_t needDataId )
179196{
180197 LoadAndDemuxSegment (trackVideo, " video/chunk-stream0-00001.m4s" );
181198 std::cout << " loading rialtotest /tmp/data/bipbop-gen/video/chunk-stream0-00001.m4s" << std::endl;
182199
183- size_t segmentCount = trackVideo.getNbSegments (); // Corrected function name
200+ // size_t segmentCount = trackVideo.getNbSegments();
201+ size_t segmentCount = trackVideo.count ();
184202 printf (" adding %zu video frames\n " , segmentCount);
185203
186204 for (size_t i = 0 ; i < segmentCount; ++i)
@@ -201,7 +219,7 @@ void InjectVideo()
201219 std::memcpy (data, trackVideo.getPtr (i), len);
202220 videoSegment->setData ((uint32_t )len, data);
203221
204- AddSegmentStatus status = gstMediaPipeline->addSegment (0 , videoSegment);
222+ AddSegmentStatus status = gstMediaPipeline->addSegment (needDataId , videoSegment);
205223 assert (status == AddSegmentStatus::OK);
206224 }
207225}
@@ -222,23 +240,33 @@ int my_main(int argc, char **argv)
222240 return -1 ;
223241 }
224242
225- if (!gstMediaPipeline->setVideoWindow (0 , 0 , 1920 , 1080 ))
226- {
227- fprintf (stderr, " Warning: Failed to set video window. Video may not appear.\n " );
228- }
243+ // if (!gstMediaPipeline->setVideoWindow(0, 0, 1920, 1080))
244+ // {
245+ // fprintf(stderr, "Warning: Failed to set video window. Video may not appear.\n");
246+ // }
229247
230- gstMediaPipeline->play ();
248+ // MUST happen before any attachSource() to create a Rialto Gstreamer player
249+ gstMediaPipeline->load (MediaType::MSE, " video/fmp4" , " test://local" ); // Dummy values
231250
232251 ConfigureAudio ();
233252 ConfigureVideo ();
234253 ConfigureComplete ();
235254
236- InjectAudio ();
237- InjectVideo ();
255+ gstMediaPipeline->play ();
256+
257+ uint32_t audioReqId = WaitForNeedDataRequest (sourceIdAudio);
258+ uint32_t videoReqId = WaitForNeedDataRequest (sourceIdVideo);
259+
260+ if (audioReqId != UINT32_MAX)
261+ InjectAudio (audioReqId);
262+
263+ if (videoReqId != UINT32_MAX)
264+ InjectVideo (videoReqId);
265+
238266
239- std::this_thread::sleep_for (std::chrono::seconds (5 ));
267+ // std::this_thread::sleep_for(std::chrono::seconds(5));
240268
241- gstMediaPipeline->stop ();
269+ // gstMediaPipeline->stop();
242270
243271 return 0 ;
244272}
0 commit comments