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 3 : mediaKeys = std::make_unique<client::MediaKeys>(keySystem, mediaKeysIpcFactoryLocked
67 5 : ? mediaKeysIpcFactoryLocked
68 3 : : 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 25 : MediaKeys::MediaKeys(const std::string &keySystem, const std::shared_ptr<IMediaKeysIpcFactory> &mediaKeysIpcFactory)
82 25 : : m_keySystem{keySystem}
83 : {
84 25 : RIALTO_CLIENT_LOG_DEBUG("entry:");
85 25 : m_mediaKeysIpc = mediaKeysIpcFactory->createMediaKeysIpc(keySystem);
86 25 : if (!m_mediaKeysIpc)
87 : {
88 2 : throw std::runtime_error("Media keys ipc could not be created");
89 : }
90 29 : }
91 :
92 46 : MediaKeys::~MediaKeys()
93 : {
94 23 : RIALTO_CLIENT_LOG_DEBUG("entry:");
95 :
96 23 : m_mediaKeysIpc.reset();
97 46 : }
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 : bool isLDL, int32_t &keySessionId)
119 : {
120 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
121 :
122 2 : auto result{m_mediaKeysIpc->createKeySession(sessionType, client, isLDL, 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 : {
133 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
134 :
135 1 : return m_mediaKeysIpc->generateRequest(keySessionId, initDataType, initData);
136 : }
137 :
138 1 : MediaKeyErrorStatus MediaKeys::loadSession(int32_t keySessionId)
139 : {
140 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
141 :
142 1 : return m_mediaKeysIpc->loadSession(keySessionId);
143 : }
144 :
145 1 : MediaKeyErrorStatus MediaKeys::updateSession(int32_t keySessionId, const std::vector<uint8_t> &responseData)
146 : {
147 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
148 :
149 1 : return m_mediaKeysIpc->updateSession(keySessionId, responseData);
150 : }
151 :
152 1 : MediaKeyErrorStatus MediaKeys::setDrmHeader(int32_t keySessionId, const std::vector<uint8_t> &requestData)
153 : {
154 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
155 :
156 1 : return m_mediaKeysIpc->setDrmHeader(keySessionId, requestData);
157 : }
158 :
159 2 : MediaKeyErrorStatus MediaKeys::closeKeySession(int32_t keySessionId)
160 : {
161 2 : RIALTO_CLIENT_LOG_DEBUG("entry:");
162 2 : if (isNetflixPlayready(m_keySystem))
163 : {
164 1 : KeyIdMap::instance().erase(keySessionId);
165 : }
166 2 : return m_mediaKeysIpc->closeKeySession(keySessionId);
167 : }
168 :
169 1 : MediaKeyErrorStatus MediaKeys::removeKeySession(int32_t keySessionId)
170 : {
171 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
172 :
173 1 : return m_mediaKeysIpc->removeKeySession(keySessionId);
174 : }
175 :
176 1 : MediaKeyErrorStatus MediaKeys::deleteDrmStore()
177 : {
178 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
179 :
180 1 : return m_mediaKeysIpc->deleteDrmStore();
181 : }
182 :
183 1 : MediaKeyErrorStatus MediaKeys::deleteKeyStore()
184 : {
185 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
186 :
187 1 : return m_mediaKeysIpc->deleteKeyStore();
188 : }
189 :
190 1 : MediaKeyErrorStatus MediaKeys::getDrmStoreHash(std::vector<unsigned char> &drmStoreHash)
191 : {
192 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
193 :
194 1 : return m_mediaKeysIpc->getDrmStoreHash(drmStoreHash);
195 : }
196 :
197 1 : MediaKeyErrorStatus MediaKeys::getKeyStoreHash(std::vector<unsigned char> &keyStoreHash)
198 : {
199 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
200 :
201 1 : return m_mediaKeysIpc->getKeyStoreHash(keyStoreHash);
202 : }
203 :
204 1 : MediaKeyErrorStatus MediaKeys::getLdlSessionsLimit(uint32_t &ldlLimit)
205 : {
206 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
207 :
208 1 : return m_mediaKeysIpc->getLdlSessionsLimit(ldlLimit);
209 : }
210 :
211 1 : MediaKeyErrorStatus MediaKeys::getLastDrmError(int32_t keySessionId, uint32_t &errorCode)
212 : {
213 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
214 :
215 1 : return m_mediaKeysIpc->getLastDrmError(keySessionId, errorCode);
216 : }
217 :
218 1 : MediaKeyErrorStatus MediaKeys::getDrmTime(uint64_t &drmTime)
219 : {
220 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
221 :
222 1 : return m_mediaKeysIpc->getDrmTime(drmTime);
223 : }
224 :
225 1 : MediaKeyErrorStatus MediaKeys::getCdmKeySessionId(int32_t keySessionId, std::string &cdmKeySessionId)
226 : {
227 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
228 :
229 1 : return m_mediaKeysIpc->getCdmKeySessionId(keySessionId, cdmKeySessionId);
230 : }
231 :
232 1 : MediaKeyErrorStatus MediaKeys::releaseKeySession(int32_t keySessionId)
233 : {
234 1 : RIALTO_CLIENT_LOG_DEBUG("entry:");
235 1 : return m_mediaKeysIpc->releaseKeySession(keySessionId);
236 : }
237 : }; // namespace firebolt::rialto::client
|