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 2025 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 "GstInitialiser.h"
21 : #include "GstLogForwarding.h"
22 : #include "IGlibWrapper.h"
23 : #include "RialtoServerLogging.h"
24 :
25 : namespace firebolt::rialto::server
26 : {
27 4 : GstInitialiser::~GstInitialiser()
28 : {
29 4 : if (m_thread.joinable())
30 : {
31 4 : m_thread.join();
32 : }
33 :
34 : #ifdef FREE_MEM_BEFORE_EXIT
35 : if (m_gstWrapper)
36 : {
37 : m_gstWrapper->gstDeinit();
38 : m_gstWrapper.reset();
39 : }
40 : #endif
41 0 : }
42 :
43 13 : IGstInitialiser &IGstInitialiser::instance()
44 : {
45 13 : static GstInitialiser initialiser;
46 13 : return initialiser;
47 : }
48 :
49 5 : void GstInitialiser::initialise(int *argc, char ***argv)
50 : {
51 5 : if (m_thread.joinable())
52 : {
53 1 : RIALTO_SERVER_LOG_WARN("Gstreamer is already initialised, no need to do it twice...");
54 1 : return;
55 : }
56 :
57 4 : m_thread = std::thread(
58 8 : [=]()
59 : {
60 : std::shared_ptr<firebolt::rialto::wrappers::IGstWrapperFactory> factory =
61 4 : firebolt::rialto::wrappers::IGstWrapperFactory::getFactory();
62 4 : m_gstWrapper = factory->getGstWrapper();
63 :
64 4 : if (!m_gstWrapper)
65 : {
66 0 : RIALTO_SERVER_LOG_ERROR("Failed to create the gst wrapper");
67 0 : return;
68 : }
69 :
70 4 : m_gstWrapper->gstInit(argc, argv);
71 :
72 4 : auto glibWrapper = firebolt::rialto::wrappers::IGlibWrapperFactory::getFactory()->getGlibWrapper();
73 4 : if (!glibWrapper)
74 : {
75 0 : RIALTO_SERVER_LOG_ERROR("Failed to create the glib wrapper");
76 0 : return;
77 : }
78 4 : glibWrapper->gThreadPoolSetMaxUnusedThreads(2);
79 4 : glibWrapper->gThreadPoolSetMaxIdleTime(5 * 1000); // 5 seconds
80 :
81 : // remove rialto sinks from the registry
82 : GstPlugin *rialtoPlugin =
83 4 : m_gstWrapper->gstRegistryFindPlugin(m_gstWrapper->gstRegistryGet(), "rialtosinks");
84 4 : if (rialtoPlugin)
85 : {
86 2 : m_gstWrapper->gstRegistryRemovePlugin(m_gstWrapper->gstRegistryGet(), rialtoPlugin);
87 2 : m_gstWrapper->gstObjectUnref(rialtoPlugin);
88 : }
89 :
90 4 : enableGstLogForwarding();
91 :
92 4 : std::unique_lock lock{m_mutex};
93 4 : m_isInitialised = true;
94 4 : m_cv.notify_all();
95 8 : });
96 : }
97 :
98 8 : void GstInitialiser::waitForInitialisation() const
99 : {
100 8 : std::unique_lock lock{m_mutex};
101 20 : m_cv.wait(lock, [this]() { return m_isInitialised; });
102 8 : }
103 : }; // namespace firebolt::rialto::server
|