Line data Source code
1 : /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 : /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 : /* This Source Code Form is subject to the terms of the Mozilla Public
4 : * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 : * You can obtain one at http://mozilla.org/MPL/2.0/. */
6 :
7 : #include "SamplesWaitingForKey.h"
8 : #include "MediaData.h"
9 : #include "MediaEventSource.h"
10 : #include "mozilla/CDMCaps.h"
11 : #include "mozilla/CDMProxy.h"
12 : #include "mozilla/TaskQueue.h"
13 :
14 : namespace mozilla {
15 :
16 0 : SamplesWaitingForKey::SamplesWaitingForKey(
17 : CDMProxy* aProxy, TrackInfo::TrackType aType,
18 0 : MediaEventProducer<TrackInfo::TrackType>* aOnWaitingForKey)
19 : : mMutex("SamplesWaitingForKey")
20 : , mProxy(aProxy)
21 : , mType(aType)
22 0 : , mOnWaitingForKeyEvent(aOnWaitingForKey)
23 : {
24 0 : }
25 :
26 0 : SamplesWaitingForKey::~SamplesWaitingForKey()
27 : {
28 0 : Flush();
29 0 : }
30 :
31 : RefPtr<SamplesWaitingForKey::WaitForKeyPromise>
32 0 : SamplesWaitingForKey::WaitIfKeyNotUsable(MediaRawData* aSample)
33 : {
34 0 : if (!aSample || !aSample->mCrypto.mValid || !mProxy) {
35 0 : return WaitForKeyPromise::CreateAndResolve(aSample, __func__);
36 : }
37 0 : CDMCaps::AutoLock caps(mProxy->Capabilites());
38 0 : const auto& keyid = aSample->mCrypto.mKeyId;
39 0 : if (caps.IsKeyUsable(keyid)) {
40 0 : return WaitForKeyPromise::CreateAndResolve(aSample, __func__);
41 : }
42 0 : SampleEntry entry;
43 0 : entry.mSample = aSample;
44 0 : RefPtr<WaitForKeyPromise> p = entry.mPromise.Ensure(__func__);
45 : {
46 0 : MutexAutoLock lock(mMutex);
47 0 : mSamples.AppendElement(Move(entry));
48 : }
49 0 : if (mOnWaitingForKeyEvent) {
50 0 : mOnWaitingForKeyEvent->Notify(mType);
51 : }
52 0 : caps.NotifyWhenKeyIdUsable(aSample->mCrypto.mKeyId, this);
53 0 : return p;
54 : }
55 :
56 : void
57 0 : SamplesWaitingForKey::NotifyUsable(const CencKeyId& aKeyId)
58 : {
59 0 : MutexAutoLock lock(mMutex);
60 0 : size_t i = 0;
61 0 : while (i < mSamples.Length()) {
62 0 : auto& entry = mSamples[i];
63 0 : if (aKeyId == entry.mSample->mCrypto.mKeyId) {
64 0 : entry.mPromise.Resolve(entry.mSample, __func__);
65 0 : mSamples.RemoveElementAt(i);
66 : } else {
67 0 : i++;
68 : }
69 : }
70 0 : }
71 :
72 : void
73 0 : SamplesWaitingForKey::Flush()
74 : {
75 0 : MutexAutoLock lock(mMutex);
76 0 : for (auto& sample : mSamples) {
77 0 : sample.mPromise.Reject(true, __func__);
78 : }
79 0 : mSamples.Clear();
80 0 : }
81 :
82 : } // namespace mozilla
|