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 "FlushOnPrerollController.h"
21 : #include "RialtoServerLogging.h"
22 : #include "TypeConverters.h"
23 : #include <chrono>
24 : #include <gst/gst.h>
25 :
26 : namespace firebolt::rialto::server
27 : {
28 : namespace
29 : {
30 : // Upper bound on how long a flush may wait for a previous async flush to settle.
31 : // Must stay well below the test's 10 s state-change timeout. To be tuned on device.
32 : constexpr std::chrono::milliseconds kMaxFlushWaitTime{1000};
33 : } // namespace
34 :
35 15 : void FlushOnPrerollController::waitIfRequired(const MediaSourceType &type)
36 : {
37 15 : std::unique_lock lock{m_mutex};
38 15 : RIALTO_SERVER_LOG_DEBUG("FlushOnPrerollController: Waiting if required for %s source entry",
39 : common::convertMediaSourceType(type));
40 15 : const bool settled = m_conditionVariable.wait_for(lock, kMaxFlushWaitTime,
41 16 : [this, &type]()
42 : // coverity[MISSING_LOCK:FALSE]
43 : {
44 22 : return !m_isPrerolled || m_flushingSources.find(type) ==
45 22 : m_flushingSources.end();
46 : });
47 15 : if (!settled)
48 : {
49 : // The previous async flush never settled (target state was not reached in time). Drop the stale
50 : // flag so this flush is not blocked again, and let the flush proceed instead of hanging the task thread.
51 1 : RIALTO_SERVER_LOG_WARN("FlushOnPrerollController: timed out waiting for %s source, proceeding with flush",
52 : common::convertMediaSourceType(type));
53 1 : m_flushingSources.erase(type);
54 : }
55 15 : RIALTO_SERVER_LOG_DEBUG("FlushOnPrerollController: Waiting if required for %s source exit",
56 : common::convertMediaSourceType(type));
57 : }
58 :
59 13 : void FlushOnPrerollController::setFlushing(const MediaSourceType &type)
60 : {
61 13 : RIALTO_SERVER_LOG_DEBUG("FlushOnPrerollController: Set flushing for: %s", common::convertMediaSourceType(type));
62 13 : std::unique_lock lock{m_mutex};
63 13 : m_flushingSources.insert(type);
64 : }
65 :
66 1 : void FlushOnPrerollController::setPrerolling()
67 : {
68 1 : RIALTO_SERVER_LOG_DEBUG("FlushOnPrerollController: Set prerolling");
69 1 : std::unique_lock lock{m_mutex};
70 1 : m_isPrerolled = false;
71 1 : m_conditionVariable.notify_all();
72 : }
73 :
74 7 : void FlushOnPrerollController::stateReached(const GstState &newPipelineState)
75 : {
76 7 : RIALTO_SERVER_LOG_DEBUG("FlushOnPrerollController: State reached %s", gst_element_state_get_name(newPipelineState));
77 7 : std::unique_lock lock{m_mutex};
78 7 : m_isPrerolled = true;
79 7 : if (m_targetState.has_value() && newPipelineState == m_targetState.value())
80 : {
81 2 : RIALTO_SERVER_LOG_DEBUG("FlushOnPrerollController: Clear state after state reached %s",
82 : gst_element_state_get_name(newPipelineState));
83 2 : m_flushingSources.clear();
84 : }
85 7 : m_conditionVariable.notify_all();
86 : }
87 :
88 10 : void FlushOnPrerollController::setTargetState(const GstState &state)
89 : {
90 10 : std::unique_lock lock{m_mutex};
91 10 : m_targetState = state;
92 10 : RIALTO_SERVER_LOG_DEBUG("FlushOnPrerollController: Set target state %s", gst_element_state_get_name(state));
93 : }
94 :
95 1 : void FlushOnPrerollController::reset()
96 : {
97 1 : RIALTO_SERVER_LOG_DEBUG("Reset FlushOnPrerollController");
98 1 : std::unique_lock lock{m_mutex};
99 1 : m_isPrerolled = false;
100 1 : m_flushingSources.clear();
101 1 : m_targetState = std::nullopt;
102 1 : m_conditionVariable.notify_all();
103 : }
104 : } // namespace firebolt::rialto::server
|