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 : #include "tasks/generic/HandleBusMessage.h"
21 : #include "GenericPlayerContext.h"
22 : #include "IGstGenericPlayerClient.h"
23 : #include "IGstWrapper.h"
24 : #include "RialtoServerLogging.h"
25 :
26 : namespace firebolt::rialto::server::tasks::generic
27 : {
28 28 : HandleBusMessage::HandleBusMessage(GenericPlayerContext &context, IGstGenericPlayerPrivate &player,
29 : IGstGenericPlayerClient *client,
30 : std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> gstWrapper,
31 : std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> glibWrapper,
32 28 : GstMessage *message)
33 28 : : m_context{context}, m_player{player}, m_gstPlayerClient{client}, m_gstWrapper{gstWrapper},
34 28 : m_glibWrapper{glibWrapper}, m_message{message}
35 : {
36 28 : RIALTO_SERVER_LOG_DEBUG("Constructing HandleBusMessage");
37 : }
38 :
39 29 : HandleBusMessage::~HandleBusMessage()
40 : {
41 28 : RIALTO_SERVER_LOG_DEBUG("HandleBusMessage finished");
42 29 : }
43 :
44 27 : void HandleBusMessage::execute() const
45 : {
46 27 : RIALTO_SERVER_LOG_DEBUG("Executing HandleBusMessage");
47 27 : switch (GST_MESSAGE_TYPE(m_message))
48 : {
49 8 : case GST_MESSAGE_STATE_CHANGED:
50 : {
51 8 : if (m_context.pipeline && GST_MESSAGE_SRC(m_message) == GST_OBJECT(m_context.pipeline))
52 : {
53 : GstState oldState, newState, pending;
54 6 : m_gstWrapper->gstMessageParseStateChanged(m_message, &oldState, &newState, &pending);
55 6 : RIALTO_SERVER_LOG_INFO("State changed (old: %s, new: %s, pending: %s)",
56 : m_gstWrapper->gstElementStateGetName(oldState),
57 : m_gstWrapper->gstElementStateGetName(newState),
58 : m_gstWrapper->gstElementStateGetName(pending));
59 :
60 12 : std::string filename = std::string(m_gstWrapper->gstElementStateGetName(oldState)) + "-" +
61 18 : std::string(m_gstWrapper->gstElementStateGetName(newState));
62 6 : m_gstWrapper->gstDebugBinToDotFileWithTs(GST_BIN(m_context.pipeline), GST_DEBUG_GRAPH_SHOW_ALL,
63 : filename.c_str());
64 6 : if (!m_gstPlayerClient)
65 : {
66 1 : break;
67 : }
68 5 : switch (newState)
69 : {
70 1 : case GST_STATE_NULL:
71 : {
72 1 : m_gstPlayerClient->notifyPlaybackState(PlaybackState::STOPPED);
73 1 : break;
74 : }
75 2 : case GST_STATE_PAUSED:
76 : {
77 2 : if (pending != GST_STATE_PAUSED)
78 : {
79 : // newState==GST_STATE_PAUSED, pending==GST_STATE_PAUSED state transition is received as a result of
80 : // waiting for preroll after seek.
81 : // Subsequent newState==GST_STATE_PAUSED, pending!=GST_STATE_PAUSED transition will
82 : // indicate that the pipeline is prerolled and it reached GST_STATE_PAUSED state after seek.
83 1 : m_gstPlayerClient->notifyPlaybackState(PlaybackState::PAUSED);
84 : }
85 2 : break;
86 : }
87 2 : case GST_STATE_PLAYING:
88 : {
89 2 : if (m_context.pendingPlaybackRate != kNoPendingPlaybackRate)
90 : {
91 1 : m_player.setPendingPlaybackRate();
92 : }
93 2 : m_player.startPositionReportingAndCheckAudioUnderflowTimer();
94 :
95 2 : m_context.isPlaying = true;
96 2 : m_gstPlayerClient->notifyPlaybackState(PlaybackState::PLAYING);
97 2 : break;
98 : }
99 0 : case GST_STATE_VOID_PENDING:
100 : case GST_STATE_READY:
101 : {
102 0 : break;
103 : }
104 : }
105 6 : }
106 7 : break;
107 : }
108 4 : case GST_MESSAGE_EOS:
109 : {
110 4 : if (m_context.pipeline && GST_MESSAGE_SRC(m_message) == GST_OBJECT(m_context.pipeline))
111 : {
112 2 : RIALTO_SERVER_LOG_MIL("End of stream reached.");
113 2 : if (!m_context.eosNotified && m_gstPlayerClient)
114 : {
115 1 : m_gstPlayerClient->notifyPlaybackState(PlaybackState::END_OF_STREAM);
116 1 : m_context.eosNotified = true;
117 : }
118 : }
119 4 : break;
120 : }
121 4 : case GST_MESSAGE_QOS:
122 : {
123 : GstFormat format;
124 4 : gboolean isLive = FALSE;
125 4 : guint64 runningTime = 0;
126 4 : guint64 streamTime = 0;
127 4 : guint64 timestamp = 0;
128 4 : guint64 duration = 0;
129 4 : guint64 dropped = 0;
130 4 : guint64 processed = 0;
131 :
132 4 : m_gstWrapper->gstMessageParseQos(m_message, &isLive, &runningTime, &streamTime, ×tamp, &duration);
133 4 : m_gstWrapper->gstMessageParseQosStats(m_message, &format, &processed, &dropped);
134 :
135 4 : if (GST_FORMAT_BUFFERS == format || GST_FORMAT_DEFAULT == format)
136 : {
137 3 : RIALTO_SERVER_LOG_INFO("QOS message: runningTime %" G_GUINT64_FORMAT ", streamTime %" G_GUINT64_FORMAT
138 : ", timestamp %" G_GUINT64_FORMAT ", duration %" G_GUINT64_FORMAT
139 : ", format %u, processed %" G_GUINT64_FORMAT ", dropped %" G_GUINT64_FORMAT,
140 : runningTime, streamTime, timestamp, duration, format, processed, dropped);
141 :
142 3 : if (m_gstPlayerClient)
143 : {
144 3 : firebolt::rialto::QosInfo qosInfo = {processed, dropped};
145 : const gchar *klass;
146 3 : klass = m_gstWrapper->gstElementClassGetMetadata(GST_ELEMENT_GET_CLASS(GST_MESSAGE_SRC(m_message)),
147 : GST_ELEMENT_METADATA_KLASS);
148 :
149 3 : if (g_strrstr(klass, "Video"))
150 : {
151 1 : m_gstPlayerClient->notifyQos(firebolt::rialto::MediaSourceType::VIDEO, qosInfo);
152 : }
153 2 : else if (g_strrstr(klass, "Audio"))
154 : {
155 1 : m_gstPlayerClient->notifyQos(firebolt::rialto::MediaSourceType::AUDIO, qosInfo);
156 : }
157 : else
158 : {
159 1 : RIALTO_SERVER_LOG_WARN("Unknown source type for class '%s', ignoring QOS Message", klass);
160 : }
161 : }
162 3 : }
163 : else
164 : {
165 1 : RIALTO_SERVER_LOG_WARN("Received a QOS_MESSAGE with unhandled format %s",
166 : m_gstWrapper->gstFormatGetName(format));
167 : }
168 4 : break;
169 : }
170 6 : case GST_MESSAGE_ERROR:
171 : {
172 6 : GError *err = nullptr;
173 6 : gchar *debug = nullptr;
174 6 : m_gstWrapper->gstMessageParseError(m_message, &err, &debug);
175 :
176 6 : if ((err->domain == GST_STREAM_ERROR) && (allSourcesEos()))
177 : {
178 2 : RIALTO_SERVER_LOG_WARN("Got stream error from %s. But all streams are ended, so reporting EOS. Error code "
179 : "%d: %s "
180 : "(%s).",
181 : GST_OBJECT_NAME(GST_MESSAGE_SRC(m_message)), err->code, err->message, debug);
182 2 : if (!m_context.eosNotified && m_gstPlayerClient)
183 : {
184 1 : m_gstPlayerClient->notifyPlaybackState(PlaybackState::END_OF_STREAM);
185 1 : m_context.eosNotified = true;
186 : }
187 : }
188 : else
189 : {
190 4 : RIALTO_SERVER_LOG_ERROR("Error from %s - %d: %s (%s)", GST_OBJECT_NAME(GST_MESSAGE_SRC(m_message)),
191 : err->code, err->message, debug);
192 4 : m_gstPlayerClient->notifyPlaybackState(PlaybackState::FAILURE);
193 : }
194 :
195 6 : m_glibWrapper->gFree(debug);
196 6 : m_glibWrapper->gErrorFree(err);
197 6 : break;
198 : }
199 4 : case GST_MESSAGE_WARNING:
200 : {
201 4 : PlaybackError rialtoError = PlaybackError::UNKNOWN;
202 4 : GError *err = nullptr;
203 4 : gchar *debug = nullptr;
204 4 : m_gstWrapper->gstMessageParseWarning(m_message, &err, &debug);
205 :
206 4 : if ((err->domain == GST_STREAM_ERROR) && (err->code == GST_STREAM_ERROR_DECRYPT))
207 : {
208 3 : RIALTO_SERVER_LOG_WARN("Decrypt error %s - %d: %s (%s)", GST_OBJECT_NAME(GST_MESSAGE_SRC(m_message)),
209 : err->code, err->message, debug);
210 3 : rialtoError = PlaybackError::DECRYPTION;
211 : }
212 : else
213 : {
214 1 : RIALTO_SERVER_LOG_WARN("Unknown warning, ignoring %s - %d: %s (%s)",
215 : GST_OBJECT_NAME(GST_MESSAGE_SRC(m_message)), err->code, err->message, debug);
216 : }
217 :
218 4 : if ((PlaybackError::UNKNOWN != rialtoError) && (m_gstPlayerClient))
219 : {
220 3 : const gchar *kName = GST_ELEMENT_NAME(GST_ELEMENT(GST_MESSAGE_SRC(m_message)));
221 3 : if (g_strrstr(kName, "video"))
222 : {
223 1 : m_gstPlayerClient->notifyPlaybackError(firebolt::rialto::MediaSourceType::VIDEO,
224 : PlaybackError::DECRYPTION);
225 : }
226 2 : else if (g_strrstr(kName, "audio"))
227 : {
228 1 : m_gstPlayerClient->notifyPlaybackError(firebolt::rialto::MediaSourceType::AUDIO,
229 : PlaybackError::DECRYPTION);
230 : }
231 : else
232 : {
233 1 : RIALTO_SERVER_LOG_WARN("Unknown source type for element '%s', not propagating error", kName);
234 : }
235 : }
236 :
237 4 : m_glibWrapper->gFree(debug);
238 4 : m_glibWrapper->gErrorFree(err);
239 4 : break;
240 : }
241 1 : default:
242 1 : break;
243 : }
244 :
245 27 : m_gstWrapper->gstMessageUnref(m_message);
246 : }
247 :
248 4 : bool HandleBusMessage::allSourcesEos() const
249 : {
250 8 : for (const auto &streamInfo : m_context.streamInfo)
251 : {
252 6 : const auto eosInfoIt = m_context.endOfStreamInfo.find(streamInfo.first);
253 6 : if (eosInfoIt == m_context.endOfStreamInfo.end() || eosInfoIt->second != EosState::SET)
254 : {
255 2 : return false;
256 : }
257 : }
258 2 : return true;
259 : }
260 : } // namespace firebolt::rialto::server::tasks::generic
|