LCOV - code coverage report
Current view: top level - media/server/main/source - ActiveRequests.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 93.8 % 65 61
Test Date: 2026-07-07 06:27:16 Functions: 100.0 % 10 10

            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 "ActiveRequests.h"
      21              : #include "RialtoServerLogging.h"
      22              : #include <cstring>
      23              : #include <limits>
      24              : #include <stdexcept>
      25              : 
      26              : namespace firebolt::rialto::server
      27              : {
      28              : ActiveRequests::ActiveRequestsData::~ActiveRequestsData() {}
      29              : 
      30            4 : AddSegmentStatus ActiveRequests::ActiveRequestsData::addSegment(const std::unique_ptr<IMediaPipeline::MediaSegment> &segment)
      31              : {
      32            4 :     if (m_bytesWritten + segment->getDataLength() > m_maxMediaBytes)
      33            1 :         return AddSegmentStatus::NO_SPACE;
      34              : 
      35            3 :     std::unique_ptr<IMediaPipeline::MediaSegment> copiedSegment = segment->copy();
      36              : 
      37            3 :     m_segmentBuffers.emplace_back(segment->getDataLength());
      38            3 :     std::memcpy(m_segmentBuffers.back().data(), segment->getData(), segment->getDataLength());
      39              : 
      40            3 :     copiedSegment->setData(m_segmentBuffers.back().size(), m_segmentBuffers.back().data());
      41            3 :     m_segments.push_back(std::move(copiedSegment));
      42              : 
      43            3 :     m_bytesWritten += segment->getDataLength();
      44            3 :     return AddSegmentStatus::OK;
      45              : }
      46              : 
      47           15 : ActiveRequests::ActiveRequests() : m_currentId{0} {}
      48              : 
      49           22 : std::uint32_t ActiveRequests::insert(const MediaSourceType &mediaSourceType, std::uint32_t maxMediaBytes,
      50              :                                      std::uint32_t maxFrames)
      51              : {
      52           22 :     std::unique_lock<std::mutex> lock{m_mutex};
      53              : 
      54           22 :     if (m_currentId == std::numeric_limits<std::uint32_t>::max())
      55              :     {
      56            0 :         m_currentId = 1;
      57              :     }
      58              : 
      59           22 :     auto [it, inserted] =
      60           22 :         m_requestMap.emplace(m_currentId, ActiveRequestsData(mediaSourceType, maxMediaBytes, maxFrames));
      61           22 :     if (!inserted)
      62              :     {
      63              :         do
      64              :         {
      65            0 :             ++m_currentId;
      66            0 :         } while (m_requestMap.find(m_currentId) != m_requestMap.end());
      67              : 
      68            0 :         m_requestMap.emplace(m_currentId, ActiveRequestsData(mediaSourceType, maxMediaBytes, maxFrames));
      69              :     }
      70              : 
      71           22 :     return m_currentId++;
      72              : }
      73              : 
      74           35 : MediaSourceType ActiveRequests::getType(std::uint32_t requestId) const
      75              : {
      76           35 :     std::unique_lock<std::mutex> lock{m_mutex};
      77           35 :     auto requestIter{m_requestMap.find(requestId)};
      78           35 :     if (requestIter != m_requestMap.end())
      79              :     {
      80           19 :         return requestIter->second.getType();
      81              :     }
      82           16 :     return MediaSourceType::UNKNOWN;
      83           35 : }
      84              : 
      85            5 : std::uint32_t ActiveRequests::getMaxFrames(std::uint32_t requestId) const
      86              : {
      87            5 :     constexpr std::uint32_t kDefaultMaxFrames{24};
      88            5 :     std::unique_lock<std::mutex> lock{m_mutex};
      89            5 :     auto requestIter{m_requestMap.find(requestId)};
      90            5 :     if (requestIter != m_requestMap.end())
      91              :     {
      92            2 :         return requestIter->second.getMaxFrames();
      93              :     }
      94            3 :     return kDefaultMaxFrames;
      95            5 : }
      96              : 
      97            2 : void ActiveRequests::erase(std::uint32_t requestId)
      98              : {
      99            2 :     std::unique_lock<std::mutex> lock{m_mutex};
     100            2 :     m_requestMap.erase(requestId);
     101              : }
     102              : 
     103            2 : void ActiveRequests::erase(const MediaSourceType &mediaSourceType)
     104              : {
     105            2 :     std::unique_lock<std::mutex> lock{m_mutex};
     106           10 :     for (auto it = m_requestMap.begin(); it != m_requestMap.end();)
     107              :     {
     108            8 :         if (it->second.getType() == mediaSourceType)
     109              :         {
     110            6 :             it = m_requestMap.erase(it);
     111              :         }
     112              :         else
     113              :         {
     114            2 :             ++it;
     115              :         }
     116              :     }
     117              : }
     118              : 
     119            2 : void ActiveRequests::clear()
     120              : {
     121            2 :     std::unique_lock<std::mutex> lock{m_mutex};
     122            2 :     m_requestMap.clear();
     123              : }
     124              : 
     125            7 : AddSegmentStatus ActiveRequests::addSegment(std::uint32_t requestId,
     126              :                                             const std::unique_ptr<IMediaPipeline::MediaSegment> &segment)
     127              : {
     128            7 :     if (nullptr == segment || nullptr == segment->getData())
     129              :     {
     130            2 :         return AddSegmentStatus::ERROR;
     131              :     }
     132              : 
     133            5 :     std::unique_lock<std::mutex> lock{m_mutex};
     134            5 :     auto requestIter{m_requestMap.find(requestId)};
     135            5 :     if (requestIter != m_requestMap.end())
     136              :     {
     137            4 :         return requestIter->second.addSegment(segment);
     138              :     }
     139              : 
     140            1 :     return AddSegmentStatus::ERROR;
     141            5 : }
     142              : 
     143            3 : const IMediaPipeline::MediaSegmentVector &ActiveRequests::getSegments(std::uint32_t requestId) const
     144              : {
     145            3 :     std::unique_lock<std::mutex> lock{m_mutex};
     146            3 :     auto requestIter{m_requestMap.find(requestId)};
     147            3 :     if (requestIter != m_requestMap.end())
     148              :     {
     149            2 :         return requestIter->second.getSegments();
     150              :     }
     151              : 
     152            2 :     throw std::runtime_error("No segments for request id " + std::to_string(requestId));
     153            3 : }
     154              : } // namespace firebolt::rialto::server
        

Generated by: LCOV version 2.0-1