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