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 : {
51 22 : std::unique_lock<std::mutex> lock{m_mutex};
52 :
53 22 : if (m_currentId == std::numeric_limits<std::uint32_t>::max())
54 : {
55 0 : m_currentId = 1;
56 : }
57 :
58 22 : auto [it, inserted] = m_requestMap.emplace(m_currentId, ActiveRequestsData(mediaSourceType, maxMediaBytes));
59 22 : if (!inserted)
60 : {
61 : do
62 : {
63 0 : ++m_currentId;
64 0 : } while (m_requestMap.find(m_currentId) != m_requestMap.end());
65 :
66 0 : m_requestMap.emplace(m_currentId, ActiveRequestsData(mediaSourceType, maxMediaBytes));
67 : }
68 :
69 22 : return m_currentId++;
70 : }
71 :
72 35 : MediaSourceType ActiveRequests::getType(std::uint32_t requestId) const
73 : {
74 35 : std::unique_lock<std::mutex> lock{m_mutex};
75 35 : auto requestIter{m_requestMap.find(requestId)};
76 35 : if (requestIter != m_requestMap.end())
77 : {
78 19 : return requestIter->second.getType();
79 : }
80 16 : return MediaSourceType::UNKNOWN;
81 35 : }
82 :
83 2 : void ActiveRequests::erase(std::uint32_t requestId)
84 : {
85 2 : std::unique_lock<std::mutex> lock{m_mutex};
86 2 : m_requestMap.erase(requestId);
87 : }
88 :
89 2 : void ActiveRequests::erase(const MediaSourceType &mediaSourceType)
90 : {
91 2 : std::unique_lock<std::mutex> lock{m_mutex};
92 10 : for (auto it = m_requestMap.begin(); it != m_requestMap.end();)
93 : {
94 8 : if (it->second.getType() == mediaSourceType)
95 : {
96 6 : it = m_requestMap.erase(it);
97 : }
98 : else
99 : {
100 2 : ++it;
101 : }
102 : }
103 : }
104 :
105 2 : void ActiveRequests::clear()
106 : {
107 2 : std::unique_lock<std::mutex> lock{m_mutex};
108 2 : m_requestMap.clear();
109 : }
110 :
111 7 : AddSegmentStatus ActiveRequests::addSegment(std::uint32_t requestId,
112 : const std::unique_ptr<IMediaPipeline::MediaSegment> &segment)
113 : {
114 7 : if (nullptr == segment || nullptr == segment->getData())
115 : {
116 2 : return AddSegmentStatus::ERROR;
117 : }
118 :
119 5 : auto requestIter{m_requestMap.find(requestId)};
120 5 : if (requestIter != m_requestMap.end())
121 : {
122 4 : return requestIter->second.addSegment(segment);
123 : }
124 :
125 1 : return AddSegmentStatus::ERROR;
126 : }
127 :
128 3 : const IMediaPipeline::MediaSegmentVector &ActiveRequests::getSegments(std::uint32_t requestId) const
129 : {
130 3 : auto requestIter{m_requestMap.find(requestId)};
131 3 : if (requestIter != m_requestMap.end())
132 : {
133 2 : return requestIter->second.getSegments();
134 : }
135 :
136 2 : throw std::runtime_error("No segments for request id " + std::to_string(requestId));
137 : }
138 : } // namespace firebolt::rialto::server
|