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 2024 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 "tasks/generic/Flush.h"
21 : #include "RialtoServerLogging.h"
22 : #include "TypeConverters.h"
23 : #include "tasks/generic/NeedData.h"
24 :
25 : namespace firebolt::rialto::server::tasks::generic
26 : {
27 9 : Flush::Flush(GenericPlayerContext &context, IGstGenericPlayerPrivate &player, IGstGenericPlayerClient *client,
28 : const std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> &gstWrapper, const MediaSourceType &type,
29 9 : bool resetTime, bool isAsync)
30 18 : : m_context{context}, m_player{player}, m_gstPlayerClient{client}, m_gstWrapper{gstWrapper}, m_type{type},
31 9 : m_resetTime{resetTime}, m_isAsync{isAsync}
32 : {
33 9 : RIALTO_SERVER_LOG_DEBUG("Constructing Flush");
34 : }
35 :
36 10 : Flush::~Flush()
37 : {
38 9 : RIALTO_SERVER_LOG_DEBUG("Flush finished");
39 10 : }
40 :
41 8 : void Flush::execute() const
42 : {
43 8 : RIALTO_SERVER_LOG_DEBUG("Executing Flush for %s source", common::convertMediaSourceType(m_type));
44 :
45 : // Get source first
46 8 : GstElement *source{nullptr};
47 8 : auto sourceElem = m_context.streamInfo.find(m_type);
48 8 : if (sourceElem != m_context.streamInfo.end())
49 : {
50 7 : source = sourceElem->second.appSrc;
51 : }
52 8 : if (!source)
53 : {
54 1 : RIALTO_SERVER_LOG_WARN("failed to flush %s - source is NULL", common::convertMediaSourceType(m_type));
55 2 : return;
56 : }
57 :
58 7 : if (m_type == MediaSourceType::UNKNOWN)
59 : {
60 1 : RIALTO_SERVER_LOG_WARN("Flush failed: Media source type not supported.");
61 1 : return;
62 : }
63 :
64 6 : StreamInfo &streamInfo = sourceElem->second;
65 6 : streamInfo.isDataNeeded = false;
66 6 : streamInfo.isNeedDataPending = false;
67 :
68 12 : for (auto &buffer : streamInfo.buffers)
69 : {
70 6 : m_gstWrapper->gstBufferUnref(buffer);
71 : }
72 6 : streamInfo.buffers.clear();
73 6 : m_context.initialPositions.erase(sourceElem->second.appSrc);
74 :
75 6 : if (m_type == MediaSourceType::AUDIO)
76 : {
77 4 : m_player.clearAudioFirstFrameFallbackProbe();
78 4 : m_context.firstAudioFrameReceived = false;
79 : }
80 :
81 6 : m_gstPlayerClient->invalidateActiveRequests(m_type);
82 :
83 6 : if (GST_STATE(m_context.pipeline) >= GST_STATE_PAUSED)
84 : {
85 5 : m_context.flushOnPrerollController->waitIfRequired(m_type);
86 :
87 5 : RIALTO_SERVER_LOG_MIL("Sending flush event for %s source.", common::convertMediaSourceType(m_type));
88 : // Flush source
89 5 : GstEvent *flushStart = m_gstWrapper->gstEventNewFlushStart();
90 5 : if (!m_gstWrapper->gstElementSendEvent(source, flushStart))
91 : {
92 1 : RIALTO_SERVER_LOG_WARN("failed to send flush-start event for %s", common::convertMediaSourceType(m_type));
93 : }
94 :
95 5 : GstEvent *flushStop = m_gstWrapper->gstEventNewFlushStop(m_resetTime);
96 5 : if (!m_gstWrapper->gstElementSendEvent(source, flushStop))
97 : {
98 1 : RIALTO_SERVER_LOG_WARN("failed to send flush-stop event for %s", common::convertMediaSourceType(m_type));
99 : }
100 :
101 5 : if (m_isAsync)
102 : {
103 5 : m_context.flushOnPrerollController->setFlushing(m_type);
104 : }
105 : }
106 : else
107 : {
108 1 : RIALTO_SERVER_LOG_DEBUG("Skip sending flush event for %s - pipeline below paused",
109 : common::convertMediaSourceType(m_type));
110 : }
111 :
112 : // Reset Eos info
113 6 : m_context.endOfStreamInfo.erase(m_type);
114 6 : m_context.eosNotified = false;
115 :
116 6 : if (m_resetTime)
117 : {
118 0 : m_context.streamPosition.store(-1);
119 : }
120 :
121 : // Notify client, that flush has been finished
122 6 : m_gstPlayerClient->notifySourceFlushed(m_type);
123 :
124 : // Notify GstGenericPlayer, that flush has been finished
125 6 : m_player.setSourceFlushed(m_type);
126 :
127 6 : if (m_context.setupSourceFinished)
128 : {
129 : // Trigger NeedData for source
130 2 : NeedData task{m_context, m_player, m_gstPlayerClient, GST_APP_SRC(source)};
131 2 : task.execute();
132 : }
133 :
134 6 : RIALTO_SERVER_LOG_MIL("%s source flushed.", common::convertMediaSourceType(m_type));
135 : }
136 : } // namespace firebolt::rialto::server::tasks::generic
|