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 "PlaybackService.h"
21 : #include "IMediaPipelineServerInternal.h"
22 : #include "IWebAudioPlayerServerInternal.h"
23 : #include "RialtoServerLogging.h"
24 : #include <exception>
25 : #include <future>
26 : #include <string>
27 : #include <utility>
28 : #include <vector>
29 :
30 : namespace firebolt::rialto::server::service
31 : {
32 6 : PlaybackService::PlaybackService(std::shared_ptr<IMediaPipelineServerInternalFactory> &&mediaPipelineFactory,
33 : std::shared_ptr<IMediaPipelineCapabilitiesFactory> &&mediaPipelineCapabilitiesFactory,
34 : std::shared_ptr<IWebAudioPlayerServerInternalFactory> &&webAudioPlayerFactory,
35 : std::unique_ptr<ISharedMemoryBufferFactory> &&shmBufferFactory,
36 6 : IDecryptionService &decryptionService)
37 6 : : m_shmBufferFactory{std::move(shmBufferFactory)}, m_isActive{false}, m_maxPlaybacks{0}, m_maxWebAudioPlayers{0},
38 6 : m_mediaPipelineService{std::make_unique<MediaPipelineService>(*this, std::move(mediaPipelineFactory),
39 6 : std::move(mediaPipelineCapabilitiesFactory),
40 : decryptionService)},
41 18 : m_webAudioPlayerService{std::make_unique<WebAudioPlayerService>(*this, std::move(webAudioPlayerFactory))}
42 : {
43 6 : RIALTO_SERVER_LOG_DEBUG("PlaybackService is constructed");
44 : }
45 :
46 12 : PlaybackService::~PlaybackService()
47 : {
48 6 : RIALTO_SERVER_LOG_DEBUG("PlaybackService is destructed");
49 12 : }
50 :
51 1 : bool PlaybackService::switchToActive()
52 : {
53 : try
54 : {
55 1 : RIALTO_SERVER_LOG_INFO("Switching SessionServer to Active state.");
56 1 : m_shmBuffer = m_shmBufferFactory->createSharedMemoryBuffer(m_maxPlaybacks, m_maxWebAudioPlayers);
57 1 : m_isActive = true;
58 1 : return true;
59 : }
60 0 : catch (const std::exception &e)
61 : {
62 0 : RIALTO_SERVER_LOG_ERROR("SessionServer failed to switch to active: %s", e.what());
63 0 : m_isActive = false;
64 0 : return false;
65 : }
66 : }
67 :
68 0 : void PlaybackService::switchToInactive()
69 : {
70 0 : RIALTO_SERVER_LOG_INFO("Switching SessionServer to Inactive state. Cleaning resources...");
71 0 : m_isActive = false;
72 0 : m_mediaPipelineService->clearMediaPipelines();
73 0 : m_webAudioPlayerService->clearWebAudioPlayers();
74 0 : m_shmBuffer.reset();
75 : }
76 :
77 2 : void PlaybackService::setMaxPlaybacks(int maxPlaybacks)
78 : {
79 : // Method called during initialization only (before setting any state), no need to execute it on a task thread.
80 2 : m_maxPlaybacks = maxPlaybacks;
81 : }
82 :
83 2 : void PlaybackService::setMaxWebAudioPlayers(int maxWebAudio)
84 : {
85 : // Method called during initialization only (before setting any state), no need to execute it on a task thread.
86 2 : m_maxWebAudioPlayers = maxWebAudio;
87 : }
88 :
89 1 : void PlaybackService::setClientDisplayName(const std::string &clientDisplayName) const
90 : {
91 : // Method called during initialization only (before setting any state), no need to execute it on a task thread.
92 1 : if (!clientDisplayName.empty())
93 : {
94 1 : setenv("WAYLAND_DISPLAY", clientDisplayName.c_str(), 1);
95 : }
96 : }
97 :
98 0 : void PlaybackService::setResourceManagerAppName(const std::string &appName) const
99 : {
100 : // Method called during initialization only (before setting any state), no need to execute it on a task thread.
101 0 : if (!appName.empty())
102 : {
103 0 : setenv("ESSRMGR_APPID", appName.c_str(), 1);
104 : }
105 : }
106 :
107 2 : bool PlaybackService::getSharedMemory(int32_t &fd, uint32_t &size) const
108 : {
109 2 : auto shmBuffer = m_shmBuffer;
110 :
111 2 : if (!shmBuffer)
112 : {
113 1 : return false;
114 : }
115 1 : fd = shmBuffer->getFd();
116 1 : size = shmBuffer->getSize();
117 1 : return true;
118 2 : }
119 :
120 0 : bool PlaybackService::isActive() const
121 : {
122 0 : return m_isActive;
123 : }
124 :
125 1 : int PlaybackService::getMaxPlaybacks() const
126 : {
127 1 : return m_maxPlaybacks;
128 : }
129 :
130 1 : int PlaybackService::getMaxWebAudioPlayers() const
131 : {
132 1 : return m_maxWebAudioPlayers;
133 : }
134 :
135 2 : std::shared_ptr<ISharedMemoryBuffer> PlaybackService::getShmBuffer() const
136 : {
137 2 : return m_shmBuffer;
138 : }
139 :
140 0 : IMediaPipelineService &PlaybackService::getMediaPipelineService() const
141 : {
142 0 : return *m_mediaPipelineService;
143 : }
144 :
145 0 : IWebAudioPlayerService &PlaybackService::getWebAudioPlayerService() const
146 : {
147 0 : return *m_webAudioPlayerService;
148 : }
149 :
150 1 : void PlaybackService::ping(const std::shared_ptr<IHeartbeatProcedure> &heartbeatProcedure) const
151 : {
152 1 : m_mediaPipelineService->ping(heartbeatProcedure);
153 1 : m_webAudioPlayerService->ping(heartbeatProcedure);
154 : }
155 : } // namespace firebolt::rialto::server::service
|