Line data Source code
1 : /*
2 : * If not stated otherwise in this file or this component's LICENSE file the
3 : * following copyright and licenses apply:
4 : *
5 : * Copyright 2022 Sky UK
6 : *
7 : * Licensed under the Apache License, Version 2.0 (the "License");
8 : * you may not use this file except in compliance with the License.
9 : * You may obtain a copy of the License at
10 : *
11 : * http://www.apache.org/licenses/LICENSE-2.0
12 : *
13 : * Unless required by applicable law or agreed to in writing, software
14 : * distributed under the License is distributed on an "AS IS" BASIS,
15 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 : * See the License for the specific language governing permissions and
17 : * limitations under the License.
18 : */
19 :
20 : #include <stdexcept>
21 :
22 : #include "KeyIdMap.h"
23 : #include "MediaKeys.h"
24 : #include "RialtoClientLogging.h"
25 :
26 : namespace
27 : {
28 4 : bool isNetflixPlayready(const std::string &keySystem)
29 : {
30 4 : return keySystem.find("netflix") != std::string::npos;
31 : }
32 : } // namespace
33 :
34 : namespace firebolt::rialto
35 : {
36 2 : std::shared_ptr<IMediaKeysFactory> IMediaKeysFactory::createFactory()
37 : {
38 2 : std::shared_ptr<IMediaKeysFactory> factory;
39 :
40 : try
41 : {
42 2 : factory = std::make_shared<MediaKeysFactory>();
43 : }
44 0 : catch (const std::exception &e)
45 : {
46 0 : RIALTO_CLIENT_LOG_ERROR("Failed to create the media keys factory, reason: %s", e.what());
47 : }
48 :
49 2 : return factory;
50 : }
51 :
52 0 : std::unique_ptr<IMediaKeys> MediaKeysFactory::createMediaKeys(const std::string &keySystem) const
53 : {
54 0 : return createMediaKeys(keySystem, {});
55 : }
56 :
57 : std::unique_ptr<IMediaKeys>
58 2 : MediaKeysFactory::createMediaKeys(const std::string &keySystem,
59 : std::weak_ptr<firebolt::rialto::client::IMediaKeysIpcFactory> mediaKeysIpcFactory) const
60 : {
61 2 : std::unique_ptr<IMediaKeys> mediaKeys;
62 : try
63 : {
64 : std::shared_ptr<firebolt::rialto::client::IMediaKeysIpcFactory> mediaKeysIpcFactoryLocked =
65 2 : mediaKeysIpcFactory.lock();
66 6 : mediaKeys = std::make_unique<client::MediaKeys>(keySystem, mediaKeysIpcFactoryLocked
67 5 : ? mediaKeysIpcFactoryLocked
68 1 : : client::IMediaKeysIpcFactory::createFactory());
69 2 : }
70 1 : catch (const std::exception &e)
71 : {
72 1 : RIALTO_CLIENT_LOG_ERROR("Failed to create the media keys, reason: %s", e.what());
73 : }
74 :
75 2 : return mediaKeys;
76 : }
77 : }; // namespace firebolt::rialto
78 :
79 : namespace firebolt::rialto::client
80 : {
81 26 : MediaKeys::MediaKeys(const std::string &keySystem, const std::shared_ptr<IMediaKeysIpcFactory> &mediaKeysIpcFactory)
82 26 : : m_keySystem{keySystem}
83 : {
84 26 : RIALTO_CLIENT_LOG_DEBUG("entry:");
85 26 : m_mediaKeysIpc = mediaKeysIpcFactory->createMediaKeysIpc(keySystem);
86 26 : if (!m_mediaKeysIpc)
87 : {
88 2 : throw std::runtime_error("Media keys ipc could not be created");
89 : }
90 30 : }
91 :
92 48 : MediaKeys::~MediaKeys()
93 : {
94 24 : RIALTO_CLIENT_LOG_DEBUG("entry:");
95 :
96 24 : m_mediaKeysIpc.reset();
97 48 : }
98 :
99 2 : MediaKeyErrorStatus MediaKeys::selectKeyId(int32_t keySessionId, const std::vector<uint8_t> &keyId)
100 : {
101 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
102 :
103 2 : if (KeyIdMap::instance().updateKey(keySessionId, keyId))
104 : {
105 1 : return MediaKeyErrorStatus::OK;
106 : }
107 1 : return MediaKeyErrorStatus::FAIL;
108 : }
109 :
110 1 : bool MediaKeys::containsKey(int32_t keySessionId, const std::vector<uint8_t> &keyId)
111 : {
112 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
113 :
114 1 : return m_mediaKeysIpc->containsKey(keySessionId, keyId);
115 : }
116 :
117 2 : MediaKeyErrorStatus MediaKeys::createKeySession(KeySessionType sessionType, std::weak_ptr<IMediaKeysClient> client,
118 : int32_t &keySessionId)
119 : {
120 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
121 :
122 2 : auto result{m_mediaKeysIpc->createKeySession(sessionType, client, keySessionId)};
123 2 : if (isNetflixPlayready(m_keySystem) && MediaKeyErrorStatus::OK == result)
124 : {
125 1 : KeyIdMap::instance().addSession(keySessionId);
126 : }
127 2 : return result;
128 : }
129 :
130 1 : MediaKeyErrorStatus MediaKeys::generateRequest(int32_t keySessionId, InitDataType initDataType,
131 : const std::vector<uint8_t> &initData,
132 : const LimitedDurationLicense &ldlState)
133 : {
134 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
135 :
136 1 : return m_mediaKeysIpc->generateRequest(keySessionId, initDataType, initData, ldlState);
137 : }
138 :
139 1 : MediaKeyErrorStatus MediaKeys::loadSession(int32_t keySessionId)
140 : {
141 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
142 :
143 1 : return m_mediaKeysIpc->loadSession(keySessionId);
144 : }
145 :
146 1 : MediaKeyErrorStatus MediaKeys::updateSession(int32_t keySessionId, const std::vector<uint8_t> &responseData)
147 : {
148 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
149 :
150 1 : return m_mediaKeysIpc->updateSession(keySessionId, responseData);
151 : }
152 :
153 1 : MediaKeyErrorStatus MediaKeys::setDrmHeader(int32_t keySessionId, const std::vector<uint8_t> &requestData)
154 : {
155 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
156 :
157 1 : return m_mediaKeysIpc->setDrmHeader(keySessionId, requestData);
158 : }
159 :
160 2 : MediaKeyErrorStatus MediaKeys::closeKeySession(int32_t keySessionId)
161 : {
162 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
163 2 : if (isNetflixPlayready(m_keySystem))
164 : {
165 1 : KeyIdMap::instance().erase(keySessionId);
166 : }
167 2 : return m_mediaKeysIpc->closeKeySession(keySessionId);
168 : }
169 :
170 1 : MediaKeyErrorStatus MediaKeys::removeKeySession(int32_t keySessionId)
171 : {
172 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
173 :
174 1 : return m_mediaKeysIpc->removeKeySession(keySessionId);
175 : }
176 :
177 1 : MediaKeyErrorStatus MediaKeys::deleteDrmStore()
178 : {
179 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
180 :
181 1 : return m_mediaKeysIpc->deleteDrmStore();
182 : }
183 :
184 1 : MediaKeyErrorStatus MediaKeys::deleteKeyStore()
185 : {
186 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
187 :
188 1 : return m_mediaKeysIpc->deleteKeyStore();
189 : }
190 :
191 1 : MediaKeyErrorStatus MediaKeys::getDrmStoreHash(std::vector<unsigned char> &drmStoreHash)
192 : {
193 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
194 :
195 1 : return m_mediaKeysIpc->getDrmStoreHash(drmStoreHash);
196 : }
197 :
198 1 : MediaKeyErrorStatus MediaKeys::getKeyStoreHash(std::vector<unsigned char> &keyStoreHash)
199 : {
200 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
201 :
202 1 : return m_mediaKeysIpc->getKeyStoreHash(keyStoreHash);
203 : }
204 :
205 1 : MediaKeyErrorStatus MediaKeys::getLdlSessionsLimit(uint32_t &ldlLimit)
206 : {
207 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
208 :
209 1 : return m_mediaKeysIpc->getLdlSessionsLimit(ldlLimit);
210 : }
211 :
212 1 : MediaKeyErrorStatus MediaKeys::getLastDrmError(int32_t keySessionId, uint32_t &errorCode)
213 : {
214 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
215 :
216 1 : return m_mediaKeysIpc->getLastDrmError(keySessionId, errorCode);
217 : }
218 :
219 1 : MediaKeyErrorStatus MediaKeys::getDrmTime(uint64_t &drmTime)
220 : {
221 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
222 :
223 1 : return m_mediaKeysIpc->getDrmTime(drmTime);
224 : }
225 :
226 1 : MediaKeyErrorStatus MediaKeys::getCdmKeySessionId(int32_t keySessionId, std::string &cdmKeySessionId)
227 : {
228 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
229 :
230 1 : return m_mediaKeysIpc->getCdmKeySessionId(keySessionId, cdmKeySessionId);
231 : }
232 :
233 1 : MediaKeyErrorStatus MediaKeys::releaseKeySession(int32_t keySessionId)
234 : {
235 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
236 1 : return m_mediaKeysIpc->releaseKeySession(keySessionId);
237 : }
238 :
239 1 : MediaKeyErrorStatus MediaKeys::getMetricSystemData(std::vector<uint8_t> &buffer)
240 : {
241 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
242 :
243 1 : return m_mediaKeysIpc->getMetricSystemData(buffer);
244 : }
245 : }; // namespace firebolt::rialto::client
|