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