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 2024 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 "TextTrackAccessor.h"
21 :
22 : #include <cinttypes>
23 : #include <stdexcept>
24 : #include <string>
25 :
26 : namespace firebolt::rialto::server
27 : {
28 0 : ITextTrackAccessorFactory &ITextTrackAccessorFactory::getFactory()
29 : {
30 0 : static TextTrackAccessorFactory factory;
31 0 : return factory;
32 : }
33 :
34 0 : std::shared_ptr<ITextTrackAccessor> TextTrackAccessorFactory::getTextTrackAccessor() const
35 : try
36 : {
37 : static std::shared_ptr<TextTrackAccessor> textTrackAccessor{
38 0 : std::make_shared<TextTrackAccessor>(firebolt::rialto::wrappers::ITextTrackPluginWrapperFactory::getFactory()
39 0 : ->getTextTrackPluginWrapper(),
40 0 : firebolt::rialto::wrappers::IThunderWrapperFactory::getFactory()
41 0 : ->getThunderWrapper())};
42 :
43 0 : return textTrackAccessor;
44 : }
45 0 : catch (const std::exception &e)
46 : {
47 0 : return nullptr;
48 : }
49 :
50 29 : TextTrackAccessor::TextTrackAccessor(
51 : const std::shared_ptr<firebolt::rialto::wrappers::ITextTrackPluginWrapper> &textTrackPluginWrapper,
52 29 : const std::shared_ptr<firebolt::rialto::wrappers::IThunderWrapper> &thunderWrapper)
53 29 : : m_textTrackPluginWrapper{textTrackPluginWrapper}, m_thunderWrapper{thunderWrapper}
54 : {
55 29 : if (!createTextTrackControlInterface())
56 : {
57 4 : RIALTO_SERVER_LOG_ERROR("Failed to create TextTrack interfaces");
58 4 : throw std::runtime_error("Failed to create TextTrack interfaces");
59 : }
60 41 : }
61 :
62 : TextTrackAccessor::~TextTrackAccessor() {}
63 :
64 2 : std::optional<uint32_t> TextTrackAccessor::openSession(const std::string &displayName)
65 : {
66 2 : uint32_t sessionId = {};
67 2 : uint32_t result = m_textTrackWrapper->openSession(displayName, sessionId);
68 2 : if (m_thunderWrapper->isSuccessful(result))
69 : {
70 1 : RIALTO_SERVER_LOG_INFO("TextTrack session %u created with display '%s'", sessionId, displayName.c_str());
71 1 : return sessionId;
72 : }
73 :
74 1 : RIALTO_SERVER_LOG_ERROR("Failed to create TextTrack session with display '%s'; error '%s'", displayName.c_str(),
75 : m_thunderWrapper->errorToString(result));
76 1 : return std::nullopt;
77 : }
78 :
79 2 : bool TextTrackAccessor::closeSession(uint32_t sessionId)
80 : {
81 2 : uint32_t result = m_textTrackWrapper->closeSession(sessionId);
82 2 : if (m_thunderWrapper->isSuccessful(result))
83 : {
84 1 : RIALTO_SERVER_LOG_INFO("TextTrack session %u closed", sessionId);
85 1 : return true;
86 : }
87 :
88 1 : RIALTO_SERVER_LOG_ERROR("Failed to close TextTrack session %u; error %s", sessionId,
89 : m_thunderWrapper->errorToString(result));
90 1 : return false;
91 : }
92 :
93 2 : bool TextTrackAccessor::pause(uint32_t sessionId)
94 : {
95 2 : uint32_t result = m_textTrackWrapper->pauseSession(sessionId);
96 2 : if (m_thunderWrapper->isSuccessful(result))
97 : {
98 1 : RIALTO_SERVER_LOG_INFO("TextTrack session %u paused", sessionId);
99 1 : return true;
100 : }
101 :
102 1 : RIALTO_SERVER_LOG_ERROR("Failed to pause TextTrack session %u; error %s", sessionId,
103 : m_thunderWrapper->errorToString(result));
104 1 : return false;
105 : }
106 :
107 2 : bool TextTrackAccessor::play(uint32_t sessionId)
108 : {
109 2 : uint32_t result = m_textTrackWrapper->resumeSession(sessionId);
110 2 : if (m_thunderWrapper->isSuccessful(result))
111 : {
112 1 : RIALTO_SERVER_LOG_INFO("TextTrack session %u resumed", sessionId);
113 1 : return true;
114 : }
115 :
116 1 : RIALTO_SERVER_LOG_ERROR("Failed to resume TextTrack session %u; error %s", sessionId,
117 : m_thunderWrapper->errorToString(result));
118 1 : return false;
119 : }
120 :
121 4 : bool TextTrackAccessor::mute(uint32_t sessionId, bool mute)
122 : {
123 4 : if (mute)
124 : {
125 2 : uint32_t result = m_textTrackWrapper->muteSession(sessionId);
126 2 : if (m_thunderWrapper->isSuccessful(result))
127 : {
128 1 : RIALTO_SERVER_LOG_INFO("TextTrack session %u muted", sessionId);
129 1 : return true;
130 : }
131 :
132 1 : RIALTO_SERVER_LOG_ERROR("Failed to mute TextTrack session %u; error %s", sessionId,
133 : m_thunderWrapper->errorToString(result));
134 : }
135 : else
136 : {
137 2 : uint32_t result = m_textTrackWrapper->unmuteSession(sessionId);
138 2 : if (m_thunderWrapper->isSuccessful(result))
139 : {
140 1 : RIALTO_SERVER_LOG_INFO("TextTrack session %u unmuted", sessionId);
141 1 : return true;
142 : }
143 :
144 1 : RIALTO_SERVER_LOG_ERROR("Failed to unmute TextTrack session %u; error %s", sessionId,
145 : m_thunderWrapper->errorToString(result));
146 : }
147 :
148 2 : return false;
149 : }
150 :
151 2 : bool TextTrackAccessor::setPosition(uint32_t sessionId, uint64_t mediaTimestampMs)
152 : {
153 2 : uint32_t result = m_textTrackWrapper->sendSessionTimestamp(sessionId, mediaTimestampMs);
154 2 : if (m_thunderWrapper->isSuccessful(result))
155 : {
156 1 : RIALTO_SERVER_LOG_INFO("TextTrack session %u set position to %" PRIu64, sessionId, mediaTimestampMs);
157 1 : return true;
158 : }
159 :
160 1 : RIALTO_SERVER_LOG_ERROR("Failed to set position of TextTrack session %u to %" PRIu64 "; error %s", sessionId,
161 : mediaTimestampMs, m_thunderWrapper->errorToString(result));
162 1 : return false;
163 : }
164 :
165 4 : bool TextTrackAccessor::sendData(uint32_t sessionId, const std::string &data, DataType datatype, int32_t displayOffsetMs)
166 : {
167 4 : firebolt::rialto::wrappers::ITextTrackWrapper::DataType wrapperDataType{};
168 4 : if (datatype == DataType::WebVTT)
169 : {
170 1 : wrapperDataType = firebolt::rialto::wrappers::ITextTrackWrapper::DataType::WEBVTT;
171 : }
172 3 : else if (datatype == DataType::TTML)
173 : {
174 2 : wrapperDataType = firebolt::rialto::wrappers::ITextTrackWrapper::DataType::TTML;
175 : }
176 : else
177 : {
178 1 : RIALTO_SERVER_LOG_ERROR("Unknown data type");
179 1 : return false;
180 : }
181 :
182 3 : const uint32_t result = m_textTrackWrapper->sendSessionData(sessionId, wrapperDataType, displayOffsetMs, data);
183 3 : if (m_thunderWrapper->isSuccessful(result))
184 : {
185 2 : RIALTO_SERVER_LOG_DEBUG("Sending data to TextTrack session %u was successful; offset %d, size %zu", sessionId,
186 : displayOffsetMs, data.size());
187 2 : return true;
188 : }
189 :
190 1 : RIALTO_SERVER_LOG_ERROR("Failed to send data to TextTrack session %u; error %s", sessionId,
191 : m_thunderWrapper->errorToString(result));
192 1 : return false;
193 : }
194 :
195 29 : bool TextTrackAccessor::createTextTrackControlInterface()
196 : {
197 29 : if (!m_textTrackPluginWrapper)
198 : {
199 1 : RIALTO_SERVER_LOG_ERROR("TextTrackPlugin is null!");
200 1 : return false;
201 : }
202 :
203 28 : uint32_t openResult = m_textTrackPluginWrapper->open();
204 :
205 28 : if (m_thunderWrapper->isSuccessful(openResult))
206 : {
207 27 : if (m_textTrackPluginWrapper->isOperational())
208 : {
209 26 : m_textTrackWrapper = m_textTrackPluginWrapper->interface();
210 26 : if (m_textTrackWrapper)
211 : {
212 25 : RIALTO_SERVER_LOG_INFO("Created TextTrack interface");
213 25 : return true;
214 : }
215 : else
216 : {
217 1 : RIALTO_SERVER_LOG_ERROR("Failed to create TextTrack interface");
218 : }
219 : }
220 : else
221 : {
222 1 : RIALTO_SERVER_LOG_ERROR("TextTrack plugin is NOT operational");
223 : }
224 : }
225 : else
226 : {
227 1 : RIALTO_SERVER_LOG_ERROR("Failed to open TextTrack plugin; error '%s'",
228 : m_thunderWrapper->errorToString(openResult));
229 : }
230 :
231 3 : return false;
232 : }
233 :
234 2 : bool TextTrackAccessor::setSessionWebVTTSelection(uint32_t sessionId)
235 : {
236 2 : uint32_t result = m_textTrackWrapper->setSessionWebVTTSelection(sessionId);
237 :
238 2 : if (m_thunderWrapper->isSuccessful(result))
239 : {
240 1 : RIALTO_SERVER_LOG_DEBUG("Setting WebVTT selection for session %u was successful", sessionId);
241 1 : return true;
242 : }
243 :
244 1 : RIALTO_SERVER_LOG_ERROR("Failed to set WebVTT selection for session %u; error %s", sessionId,
245 : m_thunderWrapper->errorToString(result));
246 1 : return false;
247 : }
248 :
249 2 : bool TextTrackAccessor::setSessionTTMLSelection(uint32_t sessionId)
250 : {
251 2 : uint32_t result = m_textTrackWrapper->setSessionTTMLSelection(sessionId);
252 2 : if (m_thunderWrapper->isSuccessful(result))
253 : {
254 1 : RIALTO_SERVER_LOG_DEBUG("Setting TTML selection for session %u was successful", sessionId);
255 1 : return true;
256 : }
257 :
258 1 : RIALTO_SERVER_LOG_ERROR("Failed to set TTML selection for session %u; error %s", sessionId,
259 : m_thunderWrapper->errorToString(result));
260 1 : return false;
261 : }
262 :
263 2 : bool TextTrackAccessor::setSessionCCSelection(uint32_t sessionId, const std::string &service)
264 : {
265 2 : uint32_t result = m_textTrackWrapper->setSessionClosedCaptionsService(sessionId, service);
266 2 : if (m_thunderWrapper->isSuccessful(result))
267 : {
268 1 : RIALTO_SERVER_LOG_DEBUG("Setting CC selection service '%s' for session %u was successful", service.c_str(),
269 : sessionId);
270 1 : return true;
271 : }
272 :
273 1 : RIALTO_SERVER_LOG_ERROR("Failed to set CC selection service '%s' for session %u; error %s", service.c_str(),
274 : sessionId, m_thunderWrapper->errorToString(result));
275 1 : return false;
276 : }
277 :
278 0 : bool TextTrackAccessor::resetSession(uint32_t sessionId)
279 : {
280 0 : uint32_t result = m_textTrackWrapper->resetSession(sessionId);
281 0 : if (m_thunderWrapper->isSuccessful(result))
282 : {
283 0 : RIALTO_SERVER_LOG_MIL("Resseting session %u was successful", sessionId);
284 0 : return true;
285 : }
286 :
287 0 : RIALTO_SERVER_LOG_ERROR("Failed to reset session %u; error %s", sessionId, m_thunderWrapper->errorToString(result));
288 0 : return false;
289 : }
290 :
291 : } // namespace firebolt::rialto::server
|