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 : #ifndef FIREBOLT_RIALTO_SERVER_GST_DISPATCHER_THREAD_H_
21 : #define FIREBOLT_RIALTO_SERVER_GST_DISPATCHER_THREAD_H_
22 :
23 : #include "IGlibWrapper.h"
24 : #include "IGstDispatcherThread.h"
25 : #include <atomic>
26 : #include <gst/gst.h>
27 : #include <memory>
28 : #include <thread>
29 :
30 : namespace firebolt::rialto::server
31 : {
32 : class GstDispatcherThreadFactory : public IGstDispatcherThreadFactory
33 : {
34 : public:
35 4 : ~GstDispatcherThreadFactory() override = default;
36 : std::unique_ptr<IGstDispatcherThread>
37 : createGstDispatcherThread(IGstDispatcherThreadClient &client, GstElement *pipeline,
38 : const std::shared_ptr<IFlushOnPrerollController> &flushOnPrerollController,
39 : const std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> &gstWrapper,
40 : const std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> &glibWrapper) const override;
41 : };
42 :
43 : class GstDispatcherThread : public IGstDispatcherThread
44 : {
45 : public:
46 : GstDispatcherThread(IGstDispatcherThreadClient &client, GstElement *pipeline,
47 : const std::shared_ptr<IFlushOnPrerollController> &flushOnPrerollController,
48 : const std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> &gstWrapper,
49 : const std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> &glibWrapper);
50 : ~GstDispatcherThread() override;
51 :
52 : private:
53 : /**
54 : * @brief Runs the GMainLoop that drives the bus watch. Runs on m_gstBusDispatcherThread.
55 : */
56 : void gstBusEventHandler();
57 :
58 : /**
59 : * @brief For handling gst bus messages, invoked by the bus watch on m_gstBusDispatcherThread.
60 : *
61 : * @param[in] message : The message popped from the bus.
62 : */
63 : void handleMessage(GstMessage *message);
64 :
65 : /**
66 : * @brief GSourceFunc-compatible trampoline registered as the bus watch's callback.
67 : */
68 : static gboolean onBusMessage(GstBus *bus, GstMessage *message, gpointer userData);
69 :
70 : /**
71 : * @brief Idle-source callback that quits the main loop. Scheduling the quit this way, rather than
72 : * calling gMainLoopQuit directly from the destructor, guarantees it only runs once the loop
73 : * has actually started (idle sources dispatch strictly from within a running loop), so a
74 : * destructor that races loop startup can never have its quit request lost.
75 : */
76 : static gboolean onQuitRequested(gpointer userData);
77 :
78 : private:
79 : /**
80 : * @brief The listening client.
81 : */
82 : IGstDispatcherThreadClient &m_client;
83 :
84 : /**
85 : * @brief The flush on preroll controller.
86 : */
87 : std::shared_ptr<IFlushOnPrerollController> m_flushOnPrerollController;
88 :
89 : /**
90 : * @brief The gstreamer wrapper object.
91 : */
92 : std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> m_gstWrapper;
93 :
94 : /**
95 : * @brief The glib wrapper object.
96 : */
97 : std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> m_glibWrapper;
98 :
99 : /**
100 : * @brief Flag used to check, if the dispatcher should keep running.
101 : */
102 : std::atomic<bool> m_isGstreamerDispatcherActive;
103 :
104 : /**
105 : * @brief The pipeline being watched, used to identify pipeline-scoped messages.
106 : */
107 : GstElement *m_pipeline;
108 :
109 : /**
110 : * @brief Thread for handling gst bus callbacks
111 : */
112 : std::thread m_gstBusDispatcherThread;
113 :
114 : /**
115 : * @brief Private main context the bus watch is attached to. Only touched on the constructing thread
116 : * and m_gstBusDispatcherThread; null if construction failed to obtain a bus.
117 : */
118 : GMainContext *m_mainContext{nullptr};
119 :
120 : /**
121 : * @brief The main loop that drives the bus watch.
122 : */
123 : GMainLoop *m_mainLoop{nullptr};
124 : };
125 : } // namespace firebolt::rialto::server
126 :
127 : #endif // FIREBOLT_RIALTO_SERVER_GST_DISPATCHER_THREAD_H_
|