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 294 : explicit ControlClient(ControlBackend &backend) : mBackend{backend} {}
36 294 : ~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 294 : ControlBackend()
49 588 : : m_rialtoClientState{ApplicationState::UNKNOWN}, m_controlClient{std::make_shared<ControlClient>(*this)},
50 294 : m_control{IControlFactory::createFactory()->createControl()}
51 : {
52 294 : if (!m_control)
53 : {
54 1 : GST_ERROR("Unable to create control");
55 1 : return;
56 : }
57 293 : if (!m_control->registerClient(m_controlClient, m_rialtoClientState))
58 : {
59 1 : GST_ERROR("Unable to register client");
60 1 : return;
61 : }
62 : }
63 :
64 588 : ~ControlBackend() final { m_control.reset(); }
65 :
66 283 : void removeControlBackend() override { m_control.reset(); }
67 :
68 286 : bool waitForRunning() override
69 : {
70 286 : std::unique_lock<std::mutex> lock{m_mutex};
71 286 : if (ApplicationState::RUNNING == m_rialtoClientState)
72 : {
73 284 : return true;
74 : }
75 2 : m_stateCv.wait_for(lock, std::chrono::seconds{1},
76 4 : [&]() { return m_rialtoClientState == ApplicationState::RUNNING; });
77 2 : return ApplicationState::RUNNING == m_rialtoClientState;
78 286 : }
79 :
80 : private:
81 1 : void onApplicationStateChanged(ApplicationState state)
82 : {
83 1 : GST_INFO("Rialto Client application state changed to: %s",
84 : state == ApplicationState::RUNNING ? "Active" : "Inactive/Unknown");
85 1 : std::unique_lock<std::mutex> lock{m_mutex};
86 1 : m_rialtoClientState = state;
87 1 : m_stateCv.notify_one();
88 : }
89 :
90 : private:
91 : ApplicationState m_rialtoClientState;
92 : std::shared_ptr<ControlClient> m_controlClient;
93 : std::shared_ptr<IControl> m_control;
94 : std::mutex m_mutex;
95 : std::condition_variable m_stateCv;
96 : };
97 : } // namespace firebolt::rialto::client
|