LCOV - code coverage report
Current view: top level - source - ActiveSessions.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 26 26
Test Date: 2025-07-07 09:31:15 Functions: 100.0 % 5 5

            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 "ActiveSessions.h"
      21              : #include "OpenCDMSessionPrivate.h"
      22              : #include <algorithm>
      23              : 
      24           22 : ActiveSessions &ActiveSessions::instance()
      25              : {
      26           22 :     static ActiveSessions activeSessions;
      27           22 :     return activeSessions;
      28              : }
      29              : 
      30            4 : OpenCDMSession *ActiveSessions::create(const std::shared_ptr<ICdmBackend> &cdm,
      31              :                                        const std::shared_ptr<IMessageDispatcher> &messageDispatcher,
      32              :                                        const LicenseType &sessionType, OpenCDMSessionCallbacks *callbacks, void *context,
      33              :                                        const std::string &initDataType, const std::vector<uint8_t> &initData)
      34              : {
      35            4 :     std::unique_lock<std::mutex> lock{m_mutex};
      36              :     OpenCDMSession *newSession =
      37            4 :         new OpenCDMSessionPrivate(cdm, messageDispatcher, sessionType, callbacks, context, initDataType, initData);
      38            4 :     m_activeSessions.insert(std::make_pair(newSession, 1));
      39            4 :     return newSession;
      40              : }
      41              : 
      42            8 : OpenCDMSession *ActiveSessions::get(const std::vector<uint8_t> &keyId)
      43              : {
      44            8 :     std::unique_lock<std::mutex> lock{m_mutex};
      45            8 :     auto sessionIter{std::find_if(m_activeSessions.begin(), m_activeSessions.end(), [&](const auto &iter)
      46            4 :                                   { return iter.first->status(keyId) != KeyStatus::InternalError; })};
      47            8 :     if (sessionIter != m_activeSessions.end())
      48              :     {
      49            3 :         ++sessionIter->second;
      50            3 :         return sessionIter->first;
      51              :     }
      52            5 :     return nullptr;
      53            8 : }
      54              : 
      55           10 : void ActiveSessions::remove(OpenCDMSession *session)
      56              : {
      57           10 :     std::unique_lock<std::mutex> lock{m_mutex};
      58           10 :     auto sessionIter{m_activeSessions.find(session)};
      59           10 :     if (sessionIter != m_activeSessions.end())
      60              :     {
      61            7 :         --sessionIter->second;
      62            7 :         if (0 == sessionIter->second)
      63              :         {
      64            4 :             delete sessionIter->first;
      65            4 :             m_activeSessions.erase(sessionIter);
      66              :         }
      67              :     }
      68           10 : }
        

Generated by: LCOV version 2.0-1