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 339 : explicit ControlClient(ControlBackend &backend, std::weak_ptr<IControlClient> client)
36 339 : : mBackend{backend}, mClient{std::move(client)}
37 : {
38 : }
39 339 : ~ControlClient() override = default;
40 6 : void notifyApplicationState(ApplicationState state) override
41 : {
42 6 : GST_INFO("ApplicationStateChanged received by rialto sink");
43 6 : mBackend.onApplicationStateChanged(state);
44 :
45 6 : if (auto client = mClient.lock())
46 : {
47 4 : client->notifyApplicationState(state);
48 6 : }
49 : }
50 :
51 : private:
52 : ControlBackend &mBackend;
53 : std::weak_ptr<IControlClient> mClient;
54 : };
55 :
56 : public:
57 339 : explicit ControlBackend(std::weak_ptr<IControlClient> controlClient = {})
58 678 : : m_rialtoClientState{ApplicationState::UNKNOWN},
59 339 : m_controlClient{std::make_shared<ControlClient>(*this, std::move(controlClient))},
60 678 : m_control{IControlFactory::createFactory()->createControl()}
61 : {
62 339 : if (!m_control)
63 : {
64 1 : GST_ERROR("Unable to create control");
65 1 : return;
66 : }
67 338 : if (!m_control->registerClient(m_controlClient, m_rialtoClientState))
68 : {
69 1 : GST_ERROR("Unable to register client");
70 1 : return;
71 : }
72 : }
73 :
74 678 : ~ControlBackend() final { m_control.reset(); }
75 :
76 328 : void removeControlBackend() override { m_control.reset(); }
77 :
78 333 : bool waitForRunning() override
79 : {
80 333 : std::unique_lock<std::mutex> lock{m_mutex};
81 333 : if (ApplicationState::RUNNING == m_rialtoClientState)
82 : {
83 330 : return true;
84 : }
85 3 : m_stateCv.wait_for(lock, std::chrono::seconds{1},
86 6 : [&]() { return m_rialtoClientState == ApplicationState::RUNNING; });
87 3 : return ApplicationState::RUNNING == m_rialtoClientState;
88 333 : }
89 :
90 : private:
91 6 : void onApplicationStateChanged(ApplicationState state)
92 : {
93 6 : GST_INFO("Rialto Client application state changed to: %s",
94 : state == ApplicationState::RUNNING ? "Active" : "Inactive/Unknown");
95 : {
96 6 : std::unique_lock<std::mutex> lock{m_mutex};
97 6 : m_rialtoClientState = state;
98 6 : m_stateCv.notify_one();
99 : }
100 : }
101 :
102 : private:
103 : ApplicationState m_rialtoClientState;
104 : std::shared_ptr<ControlClient> m_controlClient;
105 : std::shared_ptr<IControl> m_control;
106 : std::mutex m_mutex;
107 : std::condition_variable m_stateCv;
108 : };
109 : } // namespace firebolt::rialto::client
|