Line data Source code
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 "WidevineUtils.h"
7 : #include "WidevineDecryptor.h"
8 : #include <mozilla/SizePrintfMacros.h>
9 :
10 : #include "gmp-api/gmp-errors.h"
11 : #include <stdarg.h>
12 : #include <stdio.h>
13 : #include <inttypes.h>
14 :
15 : namespace mozilla {
16 :
17 : namespace detail {
18 0 : LogModule* GetCDMLog()
19 : {
20 : static LazyLogModule sLog("CDM");
21 0 : return sLog;
22 : }
23 : } // namespace detail
24 :
25 : GMPErr
26 0 : ToGMPErr(cdm::Status aStatus)
27 : {
28 0 : switch (aStatus) {
29 0 : case cdm::kSuccess: return GMPNoErr;
30 0 : case cdm::kNeedMoreData: return GMPGenericErr;
31 0 : case cdm::kNoKey: return GMPNoKeyErr;
32 0 : case cdm::kSessionError: return GMPGenericErr;
33 0 : case cdm::kDecryptError: return GMPCryptoErr;
34 0 : case cdm::kDecodeError: return GMPDecodeErr;
35 0 : case cdm::kDeferredInitialization: return GMPGenericErr;
36 0 : default: return GMPGenericErr;
37 : }
38 : }
39 :
40 0 : void InitInputBuffer(const GMPEncryptedBufferMetadata* aCrypto,
41 : int64_t aTimestamp,
42 : const uint8_t* aData,
43 : size_t aDataSize,
44 : cdm::InputBuffer &aInputBuffer,
45 : nsTArray<cdm::SubsampleEntry> &aSubsamples)
46 : {
47 0 : if (aCrypto) {
48 0 : aInputBuffer.key_id = aCrypto->KeyId();
49 0 : aInputBuffer.key_id_size = aCrypto->KeyIdSize();
50 0 : aInputBuffer.iv = aCrypto->IV();
51 0 : aInputBuffer.iv_size = aCrypto->IVSize();
52 0 : aInputBuffer.num_subsamples = aCrypto->NumSubsamples();
53 0 : aSubsamples.SetCapacity(aInputBuffer.num_subsamples);
54 0 : const uint16_t* clear = aCrypto->ClearBytes();
55 0 : const uint32_t* cipher = aCrypto->CipherBytes();
56 0 : for (size_t i = 0; i < aCrypto->NumSubsamples(); i++) {
57 0 : aSubsamples.AppendElement(cdm::SubsampleEntry(clear[i], cipher[i]));
58 : }
59 : }
60 0 : aInputBuffer.data = aData;
61 0 : aInputBuffer.data_size = aDataSize;
62 0 : aInputBuffer.subsamples = aSubsamples.Elements();
63 0 : aInputBuffer.timestamp = aTimestamp;
64 0 : }
65 :
66 0 : CDMWrapper::CDMWrapper(cdm::ContentDecryptionModule_8* aCDM,
67 0 : WidevineDecryptor* aDecryptor)
68 : : mCDM(aCDM)
69 0 : , mDecryptor(aDecryptor)
70 : {
71 0 : MOZ_ASSERT(mCDM);
72 0 : }
73 :
74 0 : CDMWrapper::~CDMWrapper()
75 : {
76 0 : CDM_LOG("CDMWrapper destroying CDM=%p", mCDM);
77 0 : mCDM->Destroy();
78 0 : mCDM = nullptr;
79 0 : }
80 :
81 0 : WidevineBuffer::WidevineBuffer(size_t aSize)
82 : {
83 0 : CDM_LOG("WidevineBuffer(size=%" PRIuSIZE ") created", aSize);
84 0 : mBuffer.SetLength(aSize);
85 0 : }
86 :
87 0 : WidevineBuffer::~WidevineBuffer()
88 : {
89 0 : CDM_LOG("WidevineBuffer(size=%" PRIu32 ") destroyed", Size());
90 0 : }
91 :
92 : void
93 0 : WidevineBuffer::Destroy()
94 : {
95 0 : delete this;
96 0 : }
97 :
98 : uint32_t
99 0 : WidevineBuffer::Capacity() const
100 : {
101 0 : return mBuffer.Length();
102 : }
103 :
104 : uint8_t*
105 0 : WidevineBuffer::Data()
106 : {
107 0 : return mBuffer.Elements();
108 : }
109 :
110 : void
111 0 : WidevineBuffer::SetSize(uint32_t aSize)
112 : {
113 0 : mBuffer.SetLength(aSize);
114 0 : }
115 :
116 : uint32_t
117 0 : WidevineBuffer::Size() const
118 : {
119 0 : return mBuffer.Length();
120 : }
121 :
122 : nsTArray<uint8_t>
123 0 : WidevineBuffer::ExtractBuffer() {
124 0 : nsTArray<uint8_t> out;
125 0 : out.SwapElements(mBuffer);
126 0 : return out;
127 : }
128 :
129 0 : WidevineDecryptedBlock::WidevineDecryptedBlock()
130 : : mBuffer(nullptr)
131 0 : , mTimestamp(0)
132 : {
133 0 : }
134 :
135 0 : WidevineDecryptedBlock::~WidevineDecryptedBlock()
136 : {
137 0 : if (mBuffer) {
138 0 : mBuffer->Destroy();
139 0 : mBuffer = nullptr;
140 : }
141 0 : }
142 :
143 : void
144 0 : WidevineDecryptedBlock::SetDecryptedBuffer(cdm::Buffer* aBuffer)
145 : {
146 0 : mBuffer = aBuffer;
147 0 : }
148 :
149 : cdm::Buffer*
150 0 : WidevineDecryptedBlock::DecryptedBuffer()
151 : {
152 0 : return mBuffer;
153 : }
154 :
155 : void
156 0 : WidevineDecryptedBlock::SetTimestamp(int64_t aTimestamp)
157 : {
158 0 : mTimestamp = aTimestamp;
159 0 : }
160 :
161 : int64_t
162 0 : WidevineDecryptedBlock::Timestamp() const
163 : {
164 0 : return mTimestamp;
165 : }
166 :
167 : } // namespace mozilla
|