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 <cstring>
22 : #include <stdexcept>
23 :
24 : namespace firebolt::rialto::server
25 : {
26 48 : ActiveRequests::ActiveRequestsData::~ActiveRequestsData()
27 : {
28 51 : for (std::unique_ptr<IMediaPipeline::MediaSegment> &segment : m_segments)
29 : {
30 3 : delete[] segment->getData();
31 : }
32 48 : }
33 :
34 4 : AddSegmentStatus ActiveRequests::ActiveRequestsData::addSegment(const std::unique_ptr<IMediaPipeline::MediaSegment> &segment)
35 : {
36 4 : if (m_bytesWritten + segment->getDataLength() > m_maxMediaBytes)
37 1 : return AddSegmentStatus::NO_SPACE;
38 :
39 3 : std::unique_ptr<IMediaPipeline::MediaSegment> copiedSegment = segment->copy();
40 :
41 3 : uint8_t *data = new uint8_t[segment->getDataLength()];
42 3 : std::memcpy(data, segment->getData(), segment->getDataLength());
43 :
44 3 : copiedSegment->setData(segment->getDataLength(), data);
45 3 : m_segments.push_back(std::move(copiedSegment));
46 :
47 3 : m_bytesWritten += segment->getDataLength();
48 3 : return AddSegmentStatus::OK;
49 : }
50 :
51 13 : ActiveRequests::ActiveRequests() : m_currentId{0} {}
52 :
53 16 : std::uint32_t ActiveRequests::insert(const MediaSourceType &mediaSourceType, std::uint32_t maxMediaBytes)
54 : {
55 16 : std::unique_lock<std::mutex> lock{m_mutex};
56 16 : m_requestMap.insert(std::make_pair(m_currentId, ActiveRequestsData(mediaSourceType, maxMediaBytes)));
57 16 : return m_currentId++;
58 : }
59 :
60 30 : MediaSourceType ActiveRequests::getType(std::uint32_t requestId) const
61 : {
62 30 : std::unique_lock<std::mutex> lock{m_mutex};
63 30 : auto requestIter{m_requestMap.find(requestId)};
64 30 : if (requestIter != m_requestMap.end())
65 : {
66 14 : return requestIter->second.getType();
67 : }
68 16 : return MediaSourceType::UNKNOWN;
69 30 : }
70 :
71 2 : void ActiveRequests::erase(std::uint32_t requestId)
72 : {
73 2 : std::unique_lock<std::mutex> lock{m_mutex};
74 2 : m_requestMap.erase(requestId);
75 : }
76 :
77 2 : void ActiveRequests::erase(const MediaSourceType &mediaSourceType)
78 : {
79 2 : std::unique_lock<std::mutex> lock{m_mutex};
80 10 : for (auto it = m_requestMap.begin(); it != m_requestMap.end();)
81 : {
82 8 : if (it->second.getType() == mediaSourceType)
83 : {
84 6 : it = m_requestMap.erase(it);
85 : }
86 : else
87 : {
88 2 : ++it;
89 : }
90 : }
91 : }
92 :
93 2 : void ActiveRequests::clear()
94 : {
95 2 : std::unique_lock<std::mutex> lock{m_mutex};
96 2 : m_requestMap.clear();
97 : }
98 :
99 7 : AddSegmentStatus ActiveRequests::addSegment(std::uint32_t requestId,
100 : const std::unique_ptr<IMediaPipeline::MediaSegment> &segment)
101 : {
102 7 : if (nullptr == segment || nullptr == segment->getData())
103 : {
104 2 : return AddSegmentStatus::ERROR;
105 : }
106 :
107 5 : auto requestIter{m_requestMap.find(requestId)};
108 5 : if (requestIter != m_requestMap.end())
109 : {
110 4 : return requestIter->second.addSegment(segment);
111 : }
112 :
113 1 : return AddSegmentStatus::ERROR;
114 : }
115 :
116 3 : const IMediaPipeline::MediaSegmentVector &ActiveRequests::getSegments(std::uint32_t requestId) const
117 : {
118 3 : auto requestIter{m_requestMap.find(requestId)};
119 3 : if (requestIter != m_requestMap.end())
120 : {
121 2 : return requestIter->second.getSegments();
122 : }
123 :
124 2 : throw std::runtime_error("No segments for request id " + std::to_string(requestId));
125 : }
126 : } // namespace firebolt::rialto::server
|