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 "TextTrackSession.h"
21 : #include "ITextTrackAccessor.h"
22 : #include "RialtoServerLogging.h"
23 : #include <stdexcept>
24 :
25 : namespace firebolt::rialto::server
26 : {
27 0 : ITextTrackSessionFactory &ITextTrackSessionFactory::getFactory()
28 : {
29 0 : static TextTrackSessionFactory factory;
30 0 : return factory;
31 : }
32 :
33 0 : std::unique_ptr<ITextTrackSession> TextTrackSessionFactory::createTextTrackSession(const std::string &display) const
34 : {
35 0 : return std::make_unique<TextTrackSession>(display, ITextTrackAccessorFactory::getFactory());
36 : }
37 :
38 11 : TextTrackSession::TextTrackSession(const std::string &displayName,
39 11 : const ITextTrackAccessorFactory &textTrackAccessorFactory)
40 : {
41 11 : m_textTrackAccessor = textTrackAccessorFactory.getTextTrackAccessor();
42 11 : if (!m_textTrackAccessor)
43 : {
44 1 : RIALTO_SERVER_LOG_ERROR("Failed to get TextTrackAccessor");
45 1 : throw std::runtime_error("Failed to get TextTrackAccessor");
46 : }
47 :
48 10 : std::optional<uint32_t> sessionId = m_textTrackAccessor->openSession(displayName);
49 10 : if (!sessionId)
50 : {
51 1 : RIALTO_SERVER_LOG_ERROR("Failed to create TextTrack session");
52 1 : throw std::runtime_error("Failed to create TextTrack session");
53 : }
54 :
55 9 : m_sessionId = sessionId.value();
56 15 : }
57 :
58 18 : TextTrackSession::~TextTrackSession()
59 : {
60 9 : m_textTrackAccessor->closeSession(m_sessionId);
61 18 : }
62 :
63 1 : bool TextTrackSession::resetSession(bool isMuted)
64 : {
65 : // There is no direct way to clear TextTrack's data. The only option is to reset the session, but that also resets
66 : // the data type and mute values
67 1 : if (!m_textTrackAccessor->resetSession(m_sessionId))
68 : {
69 0 : return false;
70 : }
71 :
72 1 : bool wasDataTypeSelected = false;
73 1 : if (m_dataType == ITextTrackAccessor::DataType::WebVTT)
74 : {
75 0 : wasDataTypeSelected = setSessionWebVTTSelection();
76 : }
77 1 : else if (m_dataType == ITextTrackAccessor::DataType::TTML)
78 : {
79 1 : wasDataTypeSelected = setSessionTTMLSelection();
80 : }
81 0 : else if (m_dataType == ITextTrackAccessor::DataType::CC)
82 : {
83 0 : if (m_ccService.has_value())
84 : {
85 0 : wasDataTypeSelected = setSessionCCSelection(m_ccService.value());
86 : }
87 : else
88 : {
89 0 : RIALTO_SERVER_LOG_ERROR("CC service not set");
90 0 : return false;
91 : }
92 : }
93 :
94 1 : if (!wasDataTypeSelected)
95 : {
96 0 : return false;
97 : }
98 :
99 : // changing the data type resets the mute value in TextTrack to its default (false), so we need to set mute
100 : // after selecting the data type
101 1 : return mute(isMuted);
102 : }
103 :
104 1 : bool TextTrackSession::pause()
105 : {
106 1 : return m_textTrackAccessor->pause(m_sessionId);
107 : }
108 :
109 1 : bool TextTrackSession::play()
110 : {
111 1 : return m_textTrackAccessor->play(m_sessionId);
112 : }
113 :
114 2 : bool TextTrackSession::mute(bool mute)
115 : {
116 2 : return m_textTrackAccessor->mute(m_sessionId, mute);
117 : }
118 :
119 1 : bool TextTrackSession::setPosition(uint64_t mediaTimestampMs)
120 : {
121 1 : return m_textTrackAccessor->setPosition(m_sessionId, mediaTimestampMs);
122 : }
123 :
124 2 : bool TextTrackSession::sendData(const std::string &data, int32_t displayOffsetMs)
125 : {
126 2 : return m_textTrackAccessor->sendData(m_sessionId, data, m_dataType, displayOffsetMs);
127 : }
128 :
129 1 : bool TextTrackSession::setSessionWebVTTSelection()
130 : {
131 1 : m_dataType = ITextTrackAccessor::DataType::WebVTT;
132 1 : m_ccService = std::optional<std::string>();
133 1 : return m_textTrackAccessor->setSessionWebVTTSelection(m_sessionId);
134 : }
135 :
136 3 : bool TextTrackSession::setSessionTTMLSelection()
137 : {
138 3 : m_dataType = ITextTrackAccessor::DataType::TTML;
139 3 : m_ccService = std::optional<std::string>();
140 3 : return m_textTrackAccessor->setSessionTTMLSelection(m_sessionId);
141 : }
142 :
143 1 : bool TextTrackSession::setSessionCCSelection(const std::string &service)
144 : {
145 1 : m_dataType = ITextTrackAccessor::DataType::CC;
146 1 : m_ccService = service;
147 1 : return m_textTrackAccessor->setSessionCCSelection(m_sessionId, service);
148 : }
149 : } // namespace firebolt::rialto::server
|