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 "SessionServerManager.h"
21 : #include "IApplicationManagementServer.h"
22 : #include "IIpcFactory.h"
23 : #include "ISessionManagementServer.h"
24 : #include "RialtoServerLogging.h"
25 :
26 : #include <algorithm>
27 : #include <stdexcept>
28 : #include <utility>
29 :
30 : namespace
31 : {
32 5 : inline bool isNumber(const std::string &text)
33 : {
34 10 : return std::find_if(text.begin(), text.end(), [](const auto &letter) { return !std::isdigit(letter); }) == text.end();
35 : }
36 : } // namespace
37 :
38 : namespace firebolt::rialto::server::service
39 : {
40 28 : SessionServerManager::SessionServerManager(const ipc::IIpcFactory &ipcFactory, IPlaybackService &playbackService,
41 : ICdmService &cdmService, IControlService &controlService,
42 28 : std::unique_ptr<IHeartbeatProcedureFactory> &&heartbeatProcedureFactory)
43 28 : : m_playbackService{playbackService}, m_cdmService{cdmService}, m_controlService{controlService},
44 28 : m_heartbeatProcedureFactory{std::move(heartbeatProcedureFactory)},
45 28 : m_applicationManagementServer{ipcFactory.createApplicationManagementServer(*this)},
46 28 : m_sessionManagementServer{ipcFactory.createSessionManagementServer(playbackService, cdmService, controlService)},
47 56 : m_isServiceRunning{true}, m_currentState{common::SessionServerState::UNINITIALIZED}
48 : {
49 28 : RIALTO_SERVER_LOG_INFO("Starting Rialto Server Service");
50 : }
51 :
52 56 : SessionServerManager::~SessionServerManager()
53 : {
54 28 : RIALTO_SERVER_LOG_INFO("Stopping Rialto Server Service");
55 :
56 : // The following reset() will ensure that the thread ApplicationManagementServer::m_ipcServerThread
57 : // isn't currently calling any methods within this class (while it is being destructed)
58 : // Particularly, the mentioned thread is responsible for calling the method
59 : // SessionServerManager::teardownService() which then triggers a call to this destuctor
60 : // after it calls stopService()
61 28 : m_applicationManagementServer.reset();
62 :
63 28 : stopService();
64 56 : }
65 :
66 7 : bool SessionServerManager::initialize(int argc, char *argv[])
67 : try
68 : {
69 7 : if (argc != 2)
70 : {
71 2 : RIALTO_SERVER_LOG_ERROR("Wrong number of arguments. Rialto Server Service will close now.");
72 2 : return false;
73 : }
74 10 : std::string socketStr{argv[1]};
75 5 : if (!isNumber(socketStr))
76 : {
77 1 : RIALTO_SERVER_LOG_ERROR("Rialto App Management socket is not a number.");
78 1 : return false;
79 : }
80 4 : if (!m_applicationManagementServer->initialize(std::stoi(socketStr)))
81 : {
82 1 : RIALTO_SERVER_LOG_ERROR("Initialization of Application Management server failed.");
83 1 : return false;
84 : }
85 2 : m_applicationManagementServer->start();
86 2 : return m_applicationManagementServer->sendStateChangedEvent(common::SessionServerState::UNINITIALIZED);
87 5 : }
88 1 : catch (const std::exception &e)
89 : {
90 1 : RIALTO_SERVER_LOG_ERROR("Exception caught during service initialization: %s", e.what());
91 1 : return false;
92 : }
93 :
94 1 : void SessionServerManager::startService()
95 : {
96 1 : std::unique_lock<std::mutex> lock{m_serviceMutex};
97 3 : m_serviceCv.wait(lock, [this]() { return !m_isServiceRunning; });
98 1 : }
99 :
100 32 : void SessionServerManager::stopService()
101 : {
102 32 : std::unique_lock<std::mutex> lock{m_serviceMutex};
103 32 : if (m_isServiceRunning)
104 : {
105 28 : m_isServiceRunning = false;
106 28 : m_serviceCv.notify_one();
107 : }
108 32 : }
109 :
110 3 : bool SessionServerManager::configureIpc(const std::string &socketName, unsigned int socketPermissions,
111 : const std::string &socketOwner, const std::string &socketGroup)
112 : {
113 3 : if (!m_sessionManagementServer->initialize(socketName, socketPermissions, socketOwner, socketGroup))
114 : {
115 1 : RIALTO_SERVER_LOG_ERROR("configureIpc failed - SessionManagementServer failed to initialize");
116 1 : return false;
117 : }
118 2 : return true;
119 : }
120 :
121 2 : bool SessionServerManager::configureIpc(int32_t socketFd)
122 : {
123 2 : if (!m_sessionManagementServer->initialize(socketFd))
124 : {
125 1 : RIALTO_SERVER_LOG_ERROR("configureIpc failed - SessionManagementServer failed to initialize");
126 1 : return false;
127 : }
128 1 : return true;
129 : }
130 :
131 3 : bool SessionServerManager::configureServices(const common::SessionServerState &state,
132 : const common::MaxResourceCapabilitites &maxResource,
133 : const std::string &clientDisplayName, const std::string &appName)
134 : {
135 3 : firebolt::rialto::logging::setApplicationName(appName.c_str());
136 3 : m_sessionManagementServer->start();
137 3 : m_playbackService.setMaxPlaybacks(maxResource.maxPlaybacks);
138 3 : m_playbackService.setMaxWebAudioPlayers(maxResource.maxWebAudioPlayers);
139 3 : m_playbackService.setClientDisplayName(clientDisplayName);
140 3 : m_playbackService.setResourceManagerAppName(appName);
141 3 : return setState(state);
142 : }
143 :
144 22 : bool SessionServerManager::setState(const common::SessionServerState &state)
145 : {
146 22 : switch (state)
147 : {
148 7 : case common::SessionServerState::ACTIVE:
149 : {
150 7 : return switchToActive();
151 : }
152 8 : case common::SessionServerState::INACTIVE:
153 : {
154 8 : return switchToInactive();
155 : }
156 5 : case common::SessionServerState::NOT_RUNNING:
157 : case common::SessionServerState::SUSPENDED:
158 : {
159 5 : return teardownService(state);
160 : }
161 2 : default:
162 : {
163 2 : RIALTO_SERVER_LOG_ERROR("SetState failed - unsupported state");
164 2 : m_applicationManagementServer->sendStateChangedEvent(common::SessionServerState::ERROR);
165 : }
166 : }
167 2 : return false;
168 : }
169 :
170 1 : void SessionServerManager::setLogLevels(RIALTO_DEBUG_LEVEL defaultLogLevels, RIALTO_DEBUG_LEVEL clientLogLevels,
171 : RIALTO_DEBUG_LEVEL sessionServerLogLevels, RIALTO_DEBUG_LEVEL ipcLogLevels,
172 : RIALTO_DEBUG_LEVEL serverManagerLogLevels, RIALTO_DEBUG_LEVEL commonLogLevels)
173 : {
174 1 : firebolt::rialto::logging::setLogLevels(RIALTO_COMPONENT_DEFAULT, defaultLogLevels);
175 1 : firebolt::rialto::logging::setLogLevels(RIALTO_COMPONENT_CLIENT, clientLogLevels);
176 1 : firebolt::rialto::logging::setLogLevels(RIALTO_COMPONENT_SERVER, sessionServerLogLevels);
177 1 : firebolt::rialto::logging::setLogLevels(RIALTO_COMPONENT_IPC, ipcLogLevels);
178 1 : firebolt::rialto::logging::setLogLevels(RIALTO_COMPONENT_SERVER_MANAGER, serverManagerLogLevels);
179 1 : firebolt::rialto::logging::setLogLevels(RIALTO_COMPONENT_COMMON, commonLogLevels);
180 1 : m_sessionManagementServer->setLogLevels(defaultLogLevels, clientLogLevels, ipcLogLevels, commonLogLevels);
181 : }
182 :
183 2 : bool SessionServerManager::ping(std::int32_t id, const std::shared_ptr<IAckSender> &ackSender)
184 : {
185 2 : auto heartbeatProcedure{m_heartbeatProcedureFactory->createHeartbeatProcedure(ackSender, id)};
186 :
187 : // Check all rialto server internal threads
188 2 : m_cdmService.ping(heartbeatProcedure);
189 2 : m_playbackService.ping(heartbeatProcedure);
190 :
191 : // Check all Rialto Clients
192 4 : return m_controlService.ping(heartbeatProcedure);
193 2 : }
194 :
195 0 : void SessionServerManager::setPreloadedCapabilities(const std::optional<common::AudioDecoderCapabilities> &audioCaps,
196 : const std::optional<common::VideoDecoderCapabilities> &videoCaps)
197 : {
198 0 : RIALTO_SERVER_LOG_DEBUG("setPreloadedCapabilities called with audio: %s, video: %s",
199 : audioCaps.has_value() ? "yes" : "no", videoCaps.has_value() ? "yes" : "no");
200 :
201 : // Forward preloaded capabilities to MediaPipelineService
202 : // (Capabilities are stored in MediaCapabilities, not in this service layer)
203 0 : m_playbackService.getMediaPipelineService().setPreloadedCapabilities(audioCaps, videoCaps);
204 : }
205 :
206 7 : bool SessionServerManager::switchToActive()
207 : {
208 7 : const auto currentState{m_currentState.load()};
209 7 : if (currentState == common::SessionServerState::ACTIVE)
210 : {
211 1 : RIALTO_SERVER_LOG_DEBUG("Session server already in Active state.");
212 1 : return true;
213 : }
214 6 : if (!m_playbackService.switchToActive())
215 : {
216 1 : RIALTO_SERVER_LOG_ERROR("Player service failed to switch to active state");
217 1 : return false;
218 : }
219 5 : if (!m_cdmService.switchToActive())
220 : {
221 1 : RIALTO_SERVER_LOG_ERROR("Cdm service failed to switch to active state");
222 1 : m_playbackService.switchToInactive();
223 1 : return false;
224 : }
225 4 : if (m_applicationManagementServer->sendStateChangedEvent(common::SessionServerState::ACTIVE))
226 : {
227 3 : m_controlService.setApplicationState(ApplicationState::RUNNING);
228 3 : m_sessionManagementServer->notifyApplicationStateChanged(ApplicationState::RUNNING);
229 3 : m_currentState.store(common::SessionServerState::ACTIVE);
230 3 : RIALTO_SERVER_LOG_MIL("RialtoServer state is ACTIVE now");
231 3 : return true;
232 : }
233 1 : m_playbackService.switchToInactive();
234 1 : m_cdmService.switchToInactive();
235 1 : return false;
236 : }
237 :
238 8 : bool SessionServerManager::switchToInactive()
239 : {
240 8 : const auto currentState{m_currentState.load()};
241 8 : if (currentState == common::SessionServerState::INACTIVE)
242 : {
243 1 : RIALTO_SERVER_LOG_DEBUG("Session server already in Inactive state.");
244 1 : return true;
245 : }
246 7 : m_playbackService.switchToInactive();
247 7 : m_cdmService.switchToInactive();
248 : // Record INACTIVE memory snapshot immediately after resource teardown,
249 : // before the manager ACK — ensures we capture it even if the socket breaks.
250 7 : m_sessionManagementServer->notifyApplicationStateChanged(ApplicationState::INACTIVE);
251 7 : if (m_applicationManagementServer->sendStateChangedEvent(common::SessionServerState::INACTIVE))
252 : {
253 4 : m_controlService.setApplicationState(ApplicationState::INACTIVE);
254 4 : m_currentState.store(common::SessionServerState::INACTIVE);
255 4 : RIALTO_SERVER_LOG_MIL("RialtoServer state is INACTIVE now");
256 4 : return true;
257 : }
258 3 : if (currentState == common::SessionServerState::ACTIVE)
259 : {
260 1 : if (!m_playbackService.switchToActive())
261 : {
262 1 : RIALTO_SERVER_LOG_WARN("Player service failed to switch to active state");
263 : }
264 1 : if (!m_cdmService.switchToActive())
265 : {
266 1 : RIALTO_SERVER_LOG_WARN("Cdm service failed to switch to active state");
267 : }
268 : }
269 3 : return false;
270 : }
271 :
272 5 : bool SessionServerManager::teardownService(const common::SessionServerState &state)
273 : {
274 5 : RIALTO_SERVER_LOG_MIL("RialtoServer tear down requested");
275 9 : if (m_currentState.load() == common::SessionServerState::NOT_RUNNING ||
276 4 : m_currentState.load() == common::SessionServerState::SUSPENDED)
277 : {
278 1 : RIALTO_SERVER_LOG_DEBUG("Session server already in NotRunning or Suspended state.");
279 1 : return true;
280 : }
281 : // Free resources before sending notification to ServerManager
282 4 : m_playbackService.switchToInactive();
283 4 : m_cdmService.switchToInactive();
284 4 : m_controlService.setApplicationState(ApplicationState::UNKNOWN);
285 :
286 4 : bool result{true};
287 4 : if (m_applicationManagementServer->sendStateChangedEvent(state))
288 : {
289 3 : m_currentState.store(state);
290 : }
291 : else
292 : {
293 1 : result = false;
294 : }
295 :
296 : // stopService() needs to be the last command of this method.
297 : // It triggers destruction of SessionServerManager and therefore
298 : // some member variables may not be valid after this command
299 4 : stopService(); // This HAS TO BE LAST (see above)
300 4 : return result;
301 : }
302 : } // namespace firebolt::rialto::server::service
|