Line data Source code
1 : /*
2 : * Copyright (C) 2022 Sky UK
3 : *
4 : * This library is free software; you can redistribute it and/or
5 : * modify it under the terms of the GNU Lesser General Public
6 : * License as published by the Free Software Foundation;
7 : * version 2.1 of the License.
8 : *
9 : * This library is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 : * Lesser General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU Lesser General Public
15 : * License along with this library; if not, write to the Free Software
16 : * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
17 : */
18 :
19 : #pragma once
20 :
21 : #include <condition_variable>
22 : #include <gst/gst.h>
23 : #include <mutex>
24 :
25 : #include "ControlBackendInterface.h"
26 : #include "IControl.h"
27 :
28 : namespace firebolt::rialto::client
29 : {
30 : class ControlBackend final : public ControlBackendInterface
31 : {
32 : class ControlClient : public IControlClient
33 : {
34 : public:
35 298 : explicit ControlClient(ControlBackend &backend) : mBackend{backend} {}
36 298 : ~ControlClient() override = default;
37 1 : void notifyApplicationState(ApplicationState state) override
38 : {
39 1 : GST_INFO("ApplicationStateChanged received by rialto sink");
40 1 : mBackend.onApplicationStateChanged(state);
41 : }
42 :
43 : private:
44 : ControlBackend &mBackend;
45 : };
46 :
47 : public:
48 298 : ControlBackend() : m_rialtoClientState{ApplicationState::UNKNOWN}
49 : {
50 298 : m_controlClient = std::make_shared<ControlClient>(*this);
51 298 : if (m_controlClient)
52 : {
53 298 : m_control = IControlFactory::createFactory()->createControl();
54 298 : if (!m_control)
55 : {
56 1 : GST_ERROR("Unable to create control");
57 1 : return;
58 : }
59 297 : if (!m_control->registerClient(m_controlClient, m_rialtoClientState))
60 : {
61 1 : GST_ERROR("Unable to register client");
62 1 : return;
63 : }
64 : }
65 : else
66 : {
67 0 : GST_ERROR("Unable to create controlClient");
68 : }
69 : }
70 :
71 596 : ~ControlBackend() final { m_control.reset(); }
72 :
73 166 : void removeControlBackend() override { m_control.reset(); }
74 :
75 169 : bool waitForRunning() override
76 : {
77 169 : std::unique_lock<std::mutex> lock{m_mutex};
78 169 : if (ApplicationState::RUNNING == m_rialtoClientState)
79 : {
80 167 : return true;
81 : }
82 2 : m_stateCv.wait_for(lock, std::chrono::seconds{1},
83 4 : [&]() { return m_rialtoClientState == ApplicationState::RUNNING; });
84 2 : return ApplicationState::RUNNING == m_rialtoClientState;
85 169 : }
86 :
87 : private:
88 1 : void onApplicationStateChanged(ApplicationState state)
89 : {
90 1 : GST_INFO("Rialto Client application state changed to: %s",
91 : state == ApplicationState::RUNNING ? "Active" : "Inactive/Unknown");
92 1 : std::unique_lock<std::mutex> lock{m_mutex};
93 1 : m_rialtoClientState = state;
94 1 : m_stateCv.notify_one();
95 : }
96 :
97 : private:
98 : ApplicationState m_rialtoClientState;
99 : std::shared_ptr<ControlClient> m_controlClient;
100 : std::shared_ptr<IControl> m_control;
101 : std::mutex m_mutex;
102 : std::condition_variable m_stateCv;
103 : };
104 : } // namespace firebolt::rialto::client
|