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 <algorithm>
21 : #include <exception>
22 : #include <memory>
23 : #include <string>
24 : #include <utility>
25 : #include <vector>
26 :
27 : #include "CdmService.h"
28 : #include "RialtoServerLogging.h"
29 :
30 : namespace firebolt::rialto::server::service
31 : {
32 97 : CdmService::CdmService(std::shared_ptr<IMediaKeysServerInternalFactory> &&mediaKeysFactory,
33 97 : std::shared_ptr<IMediaKeysCapabilitiesFactory> &&mediaKeysCapabilitiesFactory)
34 97 : : m_mediaKeysFactory{std::move(mediaKeysFactory)},
35 194 : m_mediaKeysCapabilitiesFactory{std::move(mediaKeysCapabilitiesFactory)}, m_isActive{false}
36 : {
37 97 : RIALTO_SERVER_LOG_DEBUG("CdmService is constructed");
38 : }
39 :
40 97 : CdmService::~CdmService()
41 : {
42 97 : RIALTO_SERVER_LOG_DEBUG("CdmService is destructed");
43 : }
44 :
45 92 : bool CdmService::switchToActive()
46 : {
47 92 : RIALTO_SERVER_LOG_INFO("Switching SessionServer to Active state.");
48 92 : m_isActive = true;
49 92 : return true;
50 : }
51 :
52 3 : void CdmService::switchToInactive()
53 : {
54 3 : RIALTO_SERVER_LOG_INFO("Switching SessionServer to Inactive state. Cleaning resources...");
55 3 : m_isActive = false;
56 :
57 : {
58 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
59 3 : m_mediaKeys.clear();
60 3 : m_mediaKeysClients.clear();
61 3 : m_sessionInfo.clear();
62 : }
63 : }
64 :
65 63 : bool CdmService::createMediaKeys(int mediaKeysHandle, std::string keySystem)
66 : {
67 63 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to create new media keys handle: %d", mediaKeysHandle);
68 63 : if (!m_isActive)
69 : {
70 2 : RIALTO_SERVER_LOG_ERROR("Skip to create media keys handle: %d - Session Server in Inactive state",
71 : mediaKeysHandle);
72 2 : return false;
73 : }
74 :
75 : {
76 61 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
77 61 : if (m_mediaKeys.find(mediaKeysHandle) != m_mediaKeys.end())
78 : {
79 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d already exists", mediaKeysHandle);
80 1 : return false;
81 : }
82 60 : m_mediaKeys.emplace(std::make_pair(mediaKeysHandle, m_mediaKeysFactory->createMediaKeysServerInternal(keySystem)));
83 60 : if (!m_mediaKeys.at(mediaKeysHandle))
84 : {
85 1 : RIALTO_SERVER_LOG_ERROR("Could not create MediaKeys for media keys handle: %d", mediaKeysHandle);
86 1 : m_mediaKeys.erase(mediaKeysHandle);
87 1 : return false;
88 : }
89 61 : }
90 :
91 59 : RIALTO_SERVER_LOG_INFO("New media keys handle: %d created", mediaKeysHandle);
92 59 : return true;
93 : }
94 :
95 56 : bool CdmService::destroyMediaKeys(int mediaKeysHandle)
96 : {
97 56 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to destroy media keys handle: %d", mediaKeysHandle);
98 :
99 : {
100 56 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
101 56 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
102 56 : if (mediaKeysIter == m_mediaKeys.end())
103 : {
104 2 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
105 2 : return false;
106 : }
107 68 : for (auto it = m_sessionInfo.begin(); it != m_sessionInfo.end();)
108 : {
109 14 : if (it->second.mediaKeysHandle == mediaKeysHandle)
110 : {
111 14 : it = m_sessionInfo.erase(it);
112 : }
113 : else
114 : {
115 0 : ++it;
116 : }
117 : }
118 54 : m_mediaKeys.erase(mediaKeysIter);
119 56 : }
120 :
121 54 : RIALTO_SERVER_LOG_INFO("Media keys handle: %d destroyed", mediaKeysHandle);
122 54 : return true;
123 : }
124 :
125 22 : MediaKeyErrorStatus CdmService::createKeySession(int mediaKeysHandle, KeySessionType sessionType,
126 : const std::shared_ptr<IMediaKeysClient> &client, int32_t &keySessionId)
127 : {
128 22 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to create key session: %d", mediaKeysHandle);
129 :
130 22 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
131 22 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
132 22 : if (mediaKeysIter == m_mediaKeys.end())
133 : {
134 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
135 1 : return MediaKeyErrorStatus::FAIL;
136 : }
137 :
138 21 : MediaKeyErrorStatus status = mediaKeysIter->second->createKeySession(sessionType, client, keySessionId);
139 21 : if (MediaKeyErrorStatus::OK == status)
140 : {
141 20 : if (m_mediaKeysClients.find(keySessionId) != m_mediaKeysClients.end())
142 : {
143 1 : RIALTO_SERVER_LOG_ERROR("Media keys client for key session: %d already exists", keySessionId);
144 1 : static_cast<void>(removeKeySessionInternal(mediaKeysHandle, keySessionId));
145 1 : return MediaKeyErrorStatus::FAIL;
146 : }
147 19 : m_sessionInfo.emplace(std::make_pair(keySessionId, MediaKeySessionInfo{mediaKeysHandle}));
148 19 : m_mediaKeysClients.emplace(std::make_pair(keySessionId, client));
149 : }
150 :
151 20 : return status;
152 22 : }
153 :
154 4 : MediaKeyErrorStatus CdmService::generateRequest(int mediaKeysHandle, int32_t keySessionId, InitDataType initDataType,
155 : const std::vector<uint8_t> &initData,
156 : const LimitedDurationLicense &ldlState)
157 : {
158 4 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to generate request: %d", mediaKeysHandle);
159 :
160 4 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
161 4 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
162 4 : if (mediaKeysIter == m_mediaKeys.end())
163 : {
164 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
165 1 : return MediaKeyErrorStatus::FAIL;
166 : }
167 3 : if (LimitedDurationLicense::NOT_SPECIFIED != ldlState && m_sessionInfo.find(keySessionId) != m_sessionInfo.end())
168 : {
169 1 : m_sessionInfo[keySessionId].isExtendedInterfaceUsed = true;
170 : }
171 3 : return mediaKeysIter->second->generateRequest(keySessionId, initDataType, initData, ldlState);
172 4 : }
173 :
174 3 : MediaKeyErrorStatus CdmService::loadSession(int mediaKeysHandle, int32_t keySessionId)
175 : {
176 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to load session: %d", mediaKeysHandle);
177 :
178 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
179 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
180 3 : if (mediaKeysIter == m_mediaKeys.end())
181 : {
182 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
183 1 : return MediaKeyErrorStatus::FAIL;
184 : }
185 2 : return mediaKeysIter->second->loadSession(keySessionId);
186 3 : }
187 :
188 3 : MediaKeyErrorStatus CdmService::updateSession(int mediaKeysHandle, int32_t keySessionId,
189 : const std::vector<uint8_t> &responseData)
190 : {
191 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to update session: %d", mediaKeysHandle);
192 :
193 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
194 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
195 3 : if (mediaKeysIter == m_mediaKeys.end())
196 : {
197 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
198 1 : return MediaKeyErrorStatus::FAIL;
199 : }
200 2 : return mediaKeysIter->second->updateSession(keySessionId, responseData);
201 3 : }
202 :
203 5 : MediaKeyErrorStatus CdmService::closeKeySession(int mediaKeysHandle, int32_t keySessionId)
204 : {
205 5 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to close key session: %d", mediaKeysHandle);
206 :
207 5 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
208 5 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
209 5 : if (mediaKeysHandleIter == m_sessionInfo.end())
210 : {
211 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
212 1 : return MediaKeyErrorStatus::FAIL;
213 : }
214 4 : if (mediaKeysHandleIter->second.refCounter > 0)
215 : {
216 2 : RIALTO_SERVER_LOG_INFO("Deferring closing of mksId %d", keySessionId);
217 2 : mediaKeysHandleIter->second.shouldBeClosed = true;
218 2 : return MediaKeyErrorStatus::OK;
219 : }
220 2 : return m_mediaKeys[mediaKeysHandleIter->second.mediaKeysHandle]->closeKeySession(keySessionId);
221 5 : }
222 :
223 3 : MediaKeyErrorStatus CdmService::removeKeySession(int mediaKeysHandle, int32_t keySessionId)
224 : {
225 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to remove key session: %d", mediaKeysHandle);
226 :
227 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
228 6 : return removeKeySessionInternal(mediaKeysHandle, keySessionId);
229 3 : }
230 :
231 4 : MediaKeyErrorStatus CdmService::removeKeySessionInternal(int mediaKeysHandle, int32_t keySessionId)
232 : {
233 4 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
234 4 : if (mediaKeysIter == m_mediaKeys.end())
235 : {
236 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
237 1 : return MediaKeyErrorStatus::FAIL;
238 : }
239 :
240 3 : MediaKeyErrorStatus status = mediaKeysIter->second->removeKeySession(keySessionId);
241 3 : if (MediaKeyErrorStatus::OK == status)
242 : {
243 2 : auto mediaKeysClientsIter = m_mediaKeysClients.find(keySessionId);
244 2 : if (mediaKeysClientsIter != m_mediaKeysClients.end())
245 : {
246 1 : m_mediaKeysClients.erase(mediaKeysClientsIter);
247 : }
248 : }
249 :
250 3 : return status;
251 : }
252 :
253 3 : MediaKeyErrorStatus CdmService::getCdmKeySessionId(int mediaKeysHandle, int32_t keySessionId, std::string &cdmKeySessionId)
254 : {
255 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get cdm key session id: %d", mediaKeysHandle);
256 :
257 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
258 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
259 3 : if (mediaKeysIter == m_mediaKeys.end())
260 : {
261 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
262 1 : return MediaKeyErrorStatus::FAIL;
263 : }
264 :
265 2 : MediaKeyErrorStatus status = mediaKeysIter->second->getCdmKeySessionId(keySessionId, cdmKeySessionId);
266 2 : return status;
267 3 : }
268 :
269 3 : bool CdmService::containsKey(int mediaKeysHandle, int32_t keySessionId, const std::vector<uint8_t> &keyId)
270 : {
271 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to check if key is present: %d", mediaKeysHandle);
272 :
273 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
274 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
275 3 : if (mediaKeysIter == m_mediaKeys.end())
276 : {
277 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
278 1 : return false;
279 : }
280 :
281 2 : return mediaKeysIter->second->containsKey(keySessionId, keyId);
282 3 : }
283 :
284 3 : MediaKeyErrorStatus CdmService::setDrmHeader(int mediaKeysHandle, int32_t keySessionId,
285 : const std::vector<uint8_t> &requestData)
286 : {
287 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to set drm header: %d", mediaKeysHandle);
288 :
289 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
290 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
291 3 : if (mediaKeysIter == m_mediaKeys.end())
292 : {
293 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
294 1 : return MediaKeyErrorStatus::FAIL;
295 : }
296 2 : if (m_sessionInfo.find(keySessionId) != m_sessionInfo.end())
297 : {
298 1 : m_sessionInfo[keySessionId].isExtendedInterfaceUsed = true;
299 : }
300 2 : return mediaKeysIter->second->setDrmHeader(keySessionId, requestData);
301 3 : }
302 :
303 3 : MediaKeyErrorStatus CdmService::deleteDrmStore(int mediaKeysHandle)
304 : {
305 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to delete drm store: %d", mediaKeysHandle);
306 :
307 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
308 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
309 3 : if (mediaKeysIter == m_mediaKeys.end())
310 : {
311 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
312 1 : return MediaKeyErrorStatus::FAIL;
313 : }
314 2 : return mediaKeysIter->second->deleteDrmStore();
315 3 : }
316 :
317 3 : MediaKeyErrorStatus CdmService::deleteKeyStore(int mediaKeysHandle)
318 : {
319 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to delete key store: %d", mediaKeysHandle);
320 :
321 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
322 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
323 3 : if (mediaKeysIter == m_mediaKeys.end())
324 : {
325 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
326 1 : return MediaKeyErrorStatus::FAIL;
327 : }
328 :
329 2 : return mediaKeysIter->second->deleteKeyStore();
330 3 : }
331 :
332 3 : MediaKeyErrorStatus CdmService::getDrmStoreHash(int mediaKeysHandle, std::vector<unsigned char> &drmStoreHash)
333 : {
334 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get drm store hash: %d", mediaKeysHandle);
335 :
336 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
337 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
338 3 : if (mediaKeysIter == m_mediaKeys.end())
339 : {
340 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
341 1 : return MediaKeyErrorStatus::FAIL;
342 : }
343 2 : return mediaKeysIter->second->getDrmStoreHash(drmStoreHash);
344 3 : }
345 :
346 3 : MediaKeyErrorStatus CdmService::getKeyStoreHash(int mediaKeysHandle, std::vector<unsigned char> &keyStoreHash)
347 : {
348 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get key store hash: %d", mediaKeysHandle);
349 :
350 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
351 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
352 3 : if (mediaKeysIter == m_mediaKeys.end())
353 : {
354 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
355 1 : return MediaKeyErrorStatus::FAIL;
356 : }
357 2 : return mediaKeysIter->second->getKeyStoreHash(keyStoreHash);
358 3 : }
359 :
360 3 : MediaKeyErrorStatus CdmService::getLdlSessionsLimit(int mediaKeysHandle, uint32_t &ldlLimit)
361 : {
362 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get ldl sessions limit: %d", mediaKeysHandle);
363 :
364 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
365 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
366 3 : if (mediaKeysIter == m_mediaKeys.end())
367 : {
368 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
369 1 : return MediaKeyErrorStatus::FAIL;
370 : }
371 2 : return mediaKeysIter->second->getLdlSessionsLimit(ldlLimit);
372 3 : }
373 :
374 3 : MediaKeyErrorStatus CdmService::getLastDrmError(int mediaKeysHandle, int32_t keySessionId, uint32_t &errorCode)
375 : {
376 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get last drm error: %d", mediaKeysHandle);
377 :
378 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
379 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
380 3 : if (mediaKeysIter == m_mediaKeys.end())
381 : {
382 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
383 1 : return MediaKeyErrorStatus::FAIL;
384 : }
385 2 : return mediaKeysIter->second->getLastDrmError(keySessionId, errorCode);
386 3 : }
387 :
388 3 : MediaKeyErrorStatus CdmService::getDrmTime(int mediaKeysHandle, uint64_t &drmTime)
389 : {
390 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get drm time: %d", mediaKeysHandle);
391 :
392 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
393 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
394 3 : if (mediaKeysIter == m_mediaKeys.end())
395 : {
396 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
397 1 : return MediaKeyErrorStatus::FAIL;
398 : }
399 2 : return mediaKeysIter->second->getDrmTime(drmTime);
400 3 : }
401 :
402 5 : MediaKeyErrorStatus CdmService::releaseKeySession(int mediaKeysHandle, int32_t keySessionId)
403 : {
404 5 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to release key session: %d", mediaKeysHandle);
405 :
406 5 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
407 5 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
408 5 : if (mediaKeysHandleIter == m_sessionInfo.end())
409 : {
410 2 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
411 2 : return MediaKeyErrorStatus::FAIL;
412 : }
413 3 : if (mediaKeysHandleIter->second.refCounter > 0)
414 : {
415 1 : RIALTO_SERVER_LOG_INFO("Deferring releasing of key session %d", keySessionId);
416 1 : mediaKeysHandleIter->second.shouldBeReleased = true;
417 1 : return MediaKeyErrorStatus::OK;
418 : }
419 2 : const auto result = m_mediaKeys[mediaKeysHandleIter->second.mediaKeysHandle]->releaseKeySession(keySessionId);
420 2 : m_sessionInfo.erase(keySessionId);
421 2 : return result;
422 5 : }
423 :
424 3 : std::vector<std::string> CdmService::getSupportedKeySystems()
425 : {
426 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to getSupportedKeySystems");
427 :
428 3 : if (!m_isActive)
429 : {
430 1 : RIALTO_SERVER_LOG_ERROR("Skip to get supported key systems: Session Server in Inactive state");
431 1 : return {};
432 : }
433 :
434 2 : auto mediaKeysCapabilities = m_mediaKeysCapabilitiesFactory->getMediaKeysCapabilities();
435 2 : if (!mediaKeysCapabilities)
436 : {
437 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the mediaKeysCapabilities object");
438 1 : return {};
439 : }
440 1 : return mediaKeysCapabilities->getSupportedKeySystems();
441 2 : }
442 :
443 3 : bool CdmService::supportsKeySystem(const std::string &keySystem)
444 : {
445 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to supportsKeySystem");
446 :
447 3 : if (!m_isActive)
448 : {
449 1 : RIALTO_SERVER_LOG_ERROR("Skip to get supported key systems: Session Server in Inactive state");
450 1 : return false;
451 : }
452 :
453 2 : auto mediaKeysCapabilities = m_mediaKeysCapabilitiesFactory->getMediaKeysCapabilities();
454 2 : if (!mediaKeysCapabilities)
455 : {
456 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the mediaKeysCapabilities object");
457 1 : return false;
458 : }
459 1 : return mediaKeysCapabilities->supportsKeySystem(keySystem);
460 2 : }
461 :
462 4 : bool CdmService::getSupportedKeySystemVersion(const std::string &keySystem, std::string &version)
463 : {
464 4 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to getSupportedKeySystemVersion");
465 :
466 4 : if (!m_isActive)
467 : {
468 1 : RIALTO_SERVER_LOG_ERROR("Skip to get supported key systems: Session Server in Inactive state");
469 1 : return false;
470 : }
471 :
472 3 : auto mediaKeysCapabilities = m_mediaKeysCapabilitiesFactory->getMediaKeysCapabilities();
473 3 : if (!mediaKeysCapabilities)
474 : {
475 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the mediaKeysCapabilities object");
476 1 : return false;
477 : }
478 2 : return mediaKeysCapabilities->getSupportedKeySystemVersion(keySystem, version);
479 3 : }
480 :
481 3 : bool CdmService::isServerCertificateSupported(const std::string &keySystem)
482 : {
483 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to isServerCertificateSupported");
484 :
485 3 : if (!m_isActive)
486 : {
487 1 : RIALTO_SERVER_LOG_ERROR("Skip to check if server cert is supported: Session Server in Inactive state");
488 1 : return false;
489 : }
490 :
491 2 : auto mediaKeysCapabilities = m_mediaKeysCapabilitiesFactory->getMediaKeysCapabilities();
492 2 : if (!mediaKeysCapabilities)
493 : {
494 1 : RIALTO_SERVER_LOG_ERROR("Failed to create the mediaKeysCapabilities object");
495 1 : return false;
496 : }
497 1 : return mediaKeysCapabilities->isServerCertificateSupported(keySystem);
498 2 : }
499 :
500 4 : MediaKeyErrorStatus CdmService::decrypt(int32_t keySessionId, GstBuffer *encrypted, GstCaps *caps)
501 : {
502 4 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to decrypt, key session id: %d", keySessionId);
503 :
504 4 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
505 4 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
506 4 : if (mediaKeysHandleIter == m_sessionInfo.end())
507 : {
508 2 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
509 2 : return MediaKeyErrorStatus::FAIL;
510 : }
511 2 : return m_mediaKeys[mediaKeysHandleIter->second.mediaKeysHandle]->decrypt(keySessionId, encrypted, caps);
512 4 : }
513 :
514 7 : bool CdmService::isExtendedInterfaceUsed(int32_t keySessionId)
515 : {
516 7 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to check if extended interface is used, key session id: %d",
517 : keySessionId);
518 :
519 7 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
520 7 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
521 7 : if (mediaKeysHandleIter == m_sessionInfo.end())
522 : {
523 3 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
524 3 : return false;
525 : }
526 4 : return mediaKeysHandleIter->second.isExtendedInterfaceUsed;
527 7 : }
528 :
529 4 : MediaKeyErrorStatus CdmService::selectKeyId(int32_t keySessionId, const std::vector<uint8_t> &keyId)
530 : {
531 4 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to select key id, key session id: %d", keySessionId);
532 :
533 4 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
534 4 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
535 4 : if (mediaKeysHandleIter == m_sessionInfo.end())
536 : {
537 2 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
538 2 : return MediaKeyErrorStatus::FAIL;
539 : }
540 2 : if (m_sessionInfo.find(keySessionId) != m_sessionInfo.end())
541 : {
542 2 : m_sessionInfo[keySessionId].isExtendedInterfaceUsed = true;
543 : }
544 2 : return m_mediaKeys[mediaKeysHandleIter->second.mediaKeysHandle]->selectKeyId(keySessionId, keyId);
545 4 : }
546 :
547 5 : void CdmService::incrementSessionIdUsageCounter(int32_t keySessionId)
548 : {
549 5 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
550 5 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
551 5 : if (mediaKeysHandleIter == m_sessionInfo.end())
552 : {
553 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
554 1 : return;
555 : }
556 4 : ++mediaKeysHandleIter->second.refCounter;
557 5 : }
558 :
559 5 : void CdmService::decrementSessionIdUsageCounter(int32_t keySessionId)
560 : {
561 5 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
562 5 : auto mediaKeysHandleIter{m_sessionInfo.find(keySessionId)};
563 5 : if (mediaKeysHandleIter == m_sessionInfo.end())
564 : {
565 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle for mksId: %d does not exists", keySessionId);
566 1 : return;
567 : }
568 4 : if (mediaKeysHandleIter->second.refCounter > 0)
569 : {
570 3 : --mediaKeysHandleIter->second.refCounter;
571 : }
572 4 : if (mediaKeysHandleIter->second.refCounter == 0)
573 : {
574 4 : if (mediaKeysHandleIter->second.shouldBeClosed)
575 : {
576 2 : RIALTO_SERVER_LOG_INFO("Deferred closing of mksId %d", keySessionId);
577 2 : if (MediaKeyErrorStatus::OK !=
578 2 : m_mediaKeys[mediaKeysHandleIter->second.mediaKeysHandle]->closeKeySession(keySessionId))
579 : {
580 1 : RIALTO_SERVER_LOG_ERROR("Failed to close the key session %d", keySessionId);
581 : }
582 : }
583 4 : if (mediaKeysHandleIter->second.shouldBeReleased)
584 : {
585 1 : RIALTO_SERVER_LOG_INFO("Deferred releasing of mksId %d", keySessionId);
586 1 : m_mediaKeys[mediaKeysHandleIter->second.mediaKeysHandle]->releaseKeySession(keySessionId);
587 1 : m_sessionInfo.erase(keySessionId);
588 : }
589 : }
590 5 : }
591 :
592 1 : void CdmService::ping(const std::shared_ptr<IHeartbeatProcedure> &heartbeatProcedure)
593 : {
594 1 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
595 2 : for (const auto &mediaKeyPair : m_mediaKeys)
596 : {
597 1 : auto &mediaKeys = mediaKeyPair.second;
598 1 : mediaKeys->ping(heartbeatProcedure->createHandler());
599 : }
600 : }
601 :
602 3 : MediaKeyErrorStatus CdmService::getMetricSystemData(int mediaKeysHandle, std::vector<uint8_t> &buffer)
603 : {
604 3 : RIALTO_SERVER_LOG_DEBUG("CdmService requested to get metric system data: %d", mediaKeysHandle);
605 :
606 3 : std::lock_guard<std::mutex> lock{m_mediaKeysMutex};
607 3 : auto mediaKeysIter = m_mediaKeys.find(mediaKeysHandle);
608 3 : if (mediaKeysIter == m_mediaKeys.end())
609 : {
610 1 : RIALTO_SERVER_LOG_ERROR("Media keys handle: %d does not exists", mediaKeysHandle);
611 1 : return MediaKeyErrorStatus::FAIL;
612 : }
613 2 : return mediaKeysIter->second->getMetricSystemData(buffer);
614 3 : }
615 : } // namespace firebolt::rialto::server::service
|