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