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