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/SetPlaybackRate.h"
21 : #include "IGlibWrapper.h"
22 : #include "IGstWrapper.h"
23 : #include "RialtoServerLogging.h"
24 : #include <gst/base/gstbasesink.h>
25 :
26 : namespace
27 : {
28 : const char kCustomInstantRateChangeEventName[] = "custom-instant-rate-change";
29 : } // namespace
30 :
31 : namespace firebolt::rialto::server::tasks::generic
32 : {
33 10 : SetPlaybackRate::SetPlaybackRate(GenericPlayerContext &context,
34 : const std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> &gstWrapper,
35 : const std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> &glibWrapper,
36 10 : double rate)
37 10 : : m_context{context}, m_gstWrapper{gstWrapper}, m_glibWrapper{glibWrapper}, m_rate{rate}
38 : {
39 10 : RIALTO_SERVER_LOG_DEBUG("Constructing SetPlaybackRate");
40 : }
41 :
42 11 : SetPlaybackRate::~SetPlaybackRate()
43 : {
44 10 : RIALTO_SERVER_LOG_DEBUG("SetPlaybackRate finished");
45 11 : }
46 :
47 9 : void SetPlaybackRate::execute() const
48 : {
49 9 : RIALTO_SERVER_LOG_DEBUG("Executing SetPlaybackRate");
50 9 : if (m_context.playbackRate == m_rate)
51 : {
52 1 : RIALTO_SERVER_LOG_DEBUG("No need to change playback rate - it is already %lf", m_rate);
53 3 : return;
54 : }
55 :
56 8 : if (!m_context.pipeline)
57 : {
58 1 : RIALTO_SERVER_LOG_INFO("Postponing set playback rate to %lf. Pipeline is NULL", m_rate);
59 1 : m_context.pendingPlaybackRate = m_rate;
60 1 : return;
61 : }
62 :
63 7 : if (GST_STATE(m_context.pipeline) < GST_STATE_PLAYING)
64 : {
65 1 : RIALTO_SERVER_LOG_INFO("Postponing set playback rate to %lf. Pipeline state is below PLAYING", m_rate);
66 1 : m_context.pendingPlaybackRate = m_rate;
67 1 : return;
68 : }
69 6 : m_context.pendingPlaybackRate = kNoPendingPlaybackRate;
70 :
71 6 : GstElement *audioSink{nullptr};
72 6 : gboolean success{FALSE};
73 6 : m_glibWrapper->gObjectGet(m_context.pipeline, "audio-sink", &audioSink, nullptr);
74 6 : if (audioSink && m_glibWrapper->gStrHasPrefix(GST_ELEMENT_NAME(audioSink), "amlhalasink"))
75 : {
76 2 : GstSegment *segment{m_gstWrapper->gstSegmentNew()};
77 2 : m_gstWrapper->gstSegmentInit(segment, GST_FORMAT_TIME);
78 2 : segment->rate = m_rate;
79 2 : segment->start = GST_CLOCK_TIME_NONE;
80 2 : segment->position = GST_CLOCK_TIME_NONE;
81 2 : success = m_gstWrapper->gstPadSendEvent(GST_BASE_SINK_PAD(audioSink), m_gstWrapper->gstEventNewSegment(segment));
82 2 : RIALTO_SERVER_LOG_DEBUG("Sent new segment, success = %s", success ? "true" : "false");
83 2 : m_gstWrapper->gstSegmentFree(segment);
84 : }
85 : else
86 : {
87 : GstStructure *structure{
88 4 : m_gstWrapper->gstStructureNew(kCustomInstantRateChangeEventName, "rate", G_TYPE_DOUBLE, m_rate, NULL)};
89 8 : success = m_gstWrapper->gstElementSendEvent(m_context.pipeline,
90 4 : m_gstWrapper->gstEventNewCustom(GST_EVENT_CUSTOM_DOWNSTREAM_OOB,
91 : structure));
92 4 : RIALTO_SERVER_LOG_DEBUG("Sent new event, success = %s", success ? "true" : "false");
93 : }
94 :
95 6 : if (success)
96 : {
97 3 : RIALTO_SERVER_LOG_MIL("Playback rate set to: %lf", m_rate);
98 3 : m_context.playbackRate = m_rate;
99 : }
100 :
101 6 : if (audioSink)
102 : {
103 4 : m_glibWrapper->gObjectUnref(audioSink);
104 : }
105 : }
106 : } // namespace firebolt::rialto::server::tasks::generic
|