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", sessionId);
186 2 : return true;
187 : }
188 :
189 1 : RIALTO_SERVER_LOG_ERROR("Failed to send data to TextTrack session %u; error %s", sessionId,
190 : m_thunderWrapper->errorToString(result));
191 1 : return false;
192 : }
193 :
194 29 : bool TextTrackAccessor::createTextTrackControlInterface()
195 : {
196 29 : if (!m_textTrackPluginWrapper)
197 : {
198 1 : RIALTO_SERVER_LOG_ERROR("TextTrackPlugin is null!");
199 1 : return false;
200 : }
201 :
202 28 : uint32_t openResult = m_textTrackPluginWrapper->open();
203 :
204 28 : if (m_thunderWrapper->isSuccessful(openResult))
205 : {
206 27 : if (m_textTrackPluginWrapper->isOperational())
207 : {
208 26 : m_textTrackWrapper = m_textTrackPluginWrapper->interface();
209 26 : if (m_textTrackWrapper)
210 : {
211 25 : RIALTO_SERVER_LOG_INFO("Created TextTrack interface");
212 25 : return true;
213 : }
214 : else
215 : {
216 1 : RIALTO_SERVER_LOG_ERROR("Failed to create TextTrack interface");
217 : }
218 : }
219 : else
220 : {
221 1 : RIALTO_SERVER_LOG_ERROR("TextTrack plugin is NOT operational");
222 : }
223 : }
224 : else
225 : {
226 1 : RIALTO_SERVER_LOG_ERROR("Failed to open TextTrack plugin; error '%s'",
227 : m_thunderWrapper->errorToString(openResult));
228 : }
229 :
230 3 : return false;
231 : }
232 :
233 2 : bool TextTrackAccessor::setSessionWebVTTSelection(uint32_t sessionId)
234 : {
235 2 : uint32_t result = m_textTrackWrapper->setSessionWebVTTSelection(sessionId);
236 :
237 2 : if (m_thunderWrapper->isSuccessful(result))
238 : {
239 1 : RIALTO_SERVER_LOG_DEBUG("Setting WebVTT selection for session %u was successful", sessionId);
240 1 : return true;
241 : }
242 :
243 1 : RIALTO_SERVER_LOG_ERROR("Failed to set WebVTT selection for session %u; error %s", sessionId,
244 : m_thunderWrapper->errorToString(result));
245 1 : return false;
246 : }
247 :
248 2 : bool TextTrackAccessor::setSessionTTMLSelection(uint32_t sessionId)
249 : {
250 2 : uint32_t result = m_textTrackWrapper->setSessionTTMLSelection(sessionId);
251 2 : if (m_thunderWrapper->isSuccessful(result))
252 : {
253 1 : RIALTO_SERVER_LOG_DEBUG("Setting TTML selection for session %u was successful", sessionId);
254 1 : return true;
255 : }
256 :
257 1 : RIALTO_SERVER_LOG_ERROR("Failed to set TTML selection for session %u; error %s", sessionId,
258 : m_thunderWrapper->errorToString(result));
259 1 : return false;
260 : }
261 :
262 2 : bool TextTrackAccessor::setSessionCCSelection(uint32_t sessionId, const std::string &service)
263 : {
264 2 : uint32_t result = m_textTrackWrapper->setSessionClosedCaptionsService(sessionId, service);
265 2 : if (m_thunderWrapper->isSuccessful(result))
266 : {
267 1 : RIALTO_SERVER_LOG_DEBUG("Setting CC selection service '%s' for session %u was successful", service.c_str(),
268 : sessionId);
269 1 : return true;
270 : }
271 :
272 1 : RIALTO_SERVER_LOG_ERROR("Failed to set CC selection service '%s' for session %u; error %s", service.c_str(),
273 : sessionId, m_thunderWrapper->errorToString(result));
274 1 : return false;
275 : }
276 :
277 0 : bool TextTrackAccessor::resetSession(uint32_t sessionId)
278 : {
279 0 : uint32_t result = m_textTrackWrapper->resetSession(sessionId);
280 0 : if (m_thunderWrapper->isSuccessful(result))
281 : {
282 0 : RIALTO_SERVER_LOG_MIL("Resseting session %u was successful", sessionId);
283 0 : return true;
284 : }
285 :
286 0 : RIALTO_SERVER_LOG_ERROR("Failed to reset session %u; error %s", sessionId, m_thunderWrapper->errorToString(result));
287 0 : return false;
288 : }
289 :
290 : } // namespace firebolt::rialto::server
|