1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #include "DecryptJob.h"
7 #include "mozilla/Atomics.h"
8 
9 namespace mozilla {
10 
11 static Atomic<uint32_t> sDecryptJobInstanceCount(0u);
12 
DecryptJob(MediaRawData * aSample)13 DecryptJob::DecryptJob(MediaRawData* aSample)
14     : mId(++sDecryptJobInstanceCount), mSample(aSample) {}
15 
Ensure()16 RefPtr<DecryptPromise> DecryptJob::Ensure() {
17   return mPromise.Ensure(__func__);
18 }
19 
PostResult(DecryptStatus aResult)20 void DecryptJob::PostResult(DecryptStatus aResult) {
21   nsTArray<uint8_t> empty;
22   PostResult(aResult, empty);
23 }
24 
PostResult(DecryptStatus aResult,Span<const uint8_t> aDecryptedData)25 void DecryptJob::PostResult(DecryptStatus aResult,
26                             Span<const uint8_t> aDecryptedData) {
27   if (aDecryptedData.Length() != mSample->Size()) {
28     NS_WARNING("CDM returned incorrect number of decrypted bytes");
29   }
30   if (aResult == eme::Ok) {
31     UniquePtr<MediaRawDataWriter> writer(mSample->CreateWriter());
32     PodCopy(writer->Data(), aDecryptedData.Elements(),
33             std::min<size_t>(aDecryptedData.Length(), mSample->Size()));
34   } else if (aResult == eme::NoKeyErr) {
35     NS_WARNING("CDM returned NoKeyErr");
36     // We still have the encrypted sample, so we can re-enqueue it to be
37     // decrypted again once the key is usable again.
38   } else {
39     nsAutoCString str("CDM returned decode failure DecryptStatus=");
40     str.AppendInt(aResult);
41     NS_WARNING(str.get());
42   }
43   mPromise.Resolve(DecryptResult(aResult, mSample), __func__);
44 }
45 
46 }  // namespace mozilla
47