Skip to content

Commit 35571fe

Browse files
authored
Merge pull request sailfishos#11 from sailfishos/jb51577
[gmp] Reset m_draining when drain complete and on reset. JB#51577
2 parents b4a960c + c1458e0 commit 35571fe

File tree

1 file changed

+32
-10
lines changed

1 file changed

+32
-10
lines changed

gmp-droid.cpp

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,17 @@ class DroidVideoDecoder : public GMPVideoDecoder
6666
err = g_platform_api->createmutex (&m_stop_lock);
6767
if (GMP_FAILED (err))
6868
Error (err);
69+
err = g_platform_api->createmutex (&m_drain_lock);
70+
if (GMP_FAILED (err))
71+
Error (err);
6972
}
7073

7174
virtual ~DroidVideoDecoder ()
7275
{
7376
// Destroy the Mutex
7477
m_codec_lock->Destroy ();
7578
m_stop_lock->Destroy ();
79+
m_drain_lock->Destroy ();
7680
}
7781

7882
// GMPVideoDecoder methods
@@ -240,32 +244,43 @@ class DroidVideoDecoder : public GMPVideoDecoder
240244
void SubmitBufferThread (DroidMediaCodecData cdata,
241245
DroidMediaBufferCallbacks cb)
242246
{
243-
247+
m_drain_lock->Acquire ();
244248
if (m_draining || (!m_codec && !CreateCodec ())) {
249+
LOG (ERROR, "Buffer submitted while draining");
245250
cb.unref (cb.data);
251+
m_drain_lock->Release ();
246252
return;
247253
}
254+
m_drain_lock->Release ();
255+
256+
// This blocks when the input Source is full
248257
droid_media_codec_queue (m_codec, &cdata, &cb);
249258

259+
m_drain_lock->Acquire ();
250260
if (!m_draining && m_callback && g_platform_api) {
251261
g_platform_api->runonmainthread (WrapTask (m_callback,
252262
&GMPVideoDecoderCallback::InputDataExhausted));
253263
}
264+
m_drain_lock->Release ();
254265
}
255266

256267
virtual void Reset ()
257268
{
258269
m_stop_lock->Acquire ();
259270
if (m_resetting) {
260-
m_stop_lock->Release ();
261-
return;
271+
m_stop_lock->Release ();
272+
return;
262273
}
263274
m_resetting = true;
264275
m_stop_lock->Release ();
265276

266277
if (m_codec) {
267278
ResetCodec ();
268279
}
280+
m_drain_lock->Acquire ();
281+
m_draining = false;
282+
m_drain_lock->Release ();
283+
m_resetting = false;
269284
m_callback->ResetComplete ();
270285
}
271286

@@ -276,11 +291,14 @@ class DroidVideoDecoder : public GMPVideoDecoder
276291
}
277292

278293
//TODO: This never happens because the codec never really drains, except for EOS
294+
m_drain_lock->Acquire ();
279295
if (!m_codec || m_dur.size () == 0) {
280296
m_callback->DrainComplete ();
297+
m_draining = false;
281298
} else {
282-
m_draining = true;
299+
m_draining = true;
283300
}
301+
m_drain_lock->Release ();
284302
}
285303

286304
virtual void DecodingComplete ()
@@ -316,9 +334,10 @@ class DroidVideoDecoder : public GMPVideoDecoder
316334
cb.data_available = DataAvailable;
317335
droid_media_codec_set_data_callbacks (m_codec, &cb, this);
318336
}
319-
320337
// Reset state
338+
m_drain_lock->Acquire ();
321339
m_draining = false;
340+
m_drain_lock->Release ();
322341

323342
if (!droid_media_codec_start (m_codec)) {
324343
droid_media_codec_destroy (m_codec);
@@ -382,7 +401,6 @@ class DroidVideoDecoder : public GMPVideoDecoder
382401
m_dur.clear ();
383402
RequestNewConverter ();
384403
m_codec_lock->Release ();
385-
m_resetting = false;
386404
}
387405

388406
void ProcessFrameLock (DroidMediaCodecData * decoded)
@@ -460,12 +478,15 @@ class DroidVideoDecoder : public GMPVideoDecoder
460478
// Send the new frame back to Gecko
461479
m_callback->Decoded (frame);
462480
LOG (DEBUG, "ProcessFrame: Returning frame ts: " << ts << " dur: " << dur);
481+
m_drain_lock->Acquire ();
463482
if (m_dur.size () == 0 && m_draining) {
464-
// TODO: we never get the buffers down to 0 with the current SimpleDecodingSource, but EOS will do it
465-
m_callback->DrainComplete ();
483+
// TODO: we never get the buffers down to 0 with the current SimpleDecodingSource, but EOS will do it
484+
m_callback->DrainComplete ();
485+
m_draining = false;
466486
} else {
467487
LOG (DEBUG, "Buffers still out " << m_dur.size ());
468488
}
489+
m_drain_lock->Release ();
469490
}
470491

471492
virtual void EOS ()
@@ -494,6 +515,8 @@ class DroidVideoDecoder : public GMPVideoDecoder
494515
// Stop lock prevents a deadlock when droid_media_codec_loop can't quit during
495516
// shutdown because it's waiting to get a frame processed on the main thread.
496517
GMPMutex *m_stop_lock = nullptr;
518+
// Drain lock protects the m_draining flag
519+
GMPMutex *m_drain_lock = nullptr;
497520
GMPThread *m_submit_thread = nullptr;
498521
DroidMediaCodecDecoderMetaData m_metadata;
499522
DroidMediaCodec *m_codec = nullptr;
@@ -558,8 +581,7 @@ GMPErr GMPInit (GMPPlatformAPI * platformAPI)
558581
GMPErr GMPGetAPI (const char *apiName, void *hostAPI, void **pluginApi)
559582
{
560583
if (!strcmp (apiName, "decode-video")) {
561-
*pluginApi =
562-
new DroidVideoDecoder (static_cast <GMPVideoHost *>(hostAPI));
584+
*pluginApi = new DroidVideoDecoder (static_cast <GMPVideoHost *>(hostAPI));
563585
return GMPNoErr;
564586
}
565587
return GMPGenericErr;

0 commit comments

Comments
 (0)