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