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/SetVolume.h"
21 : #include "IRdkGstreamerUtilsWrapper.h"
22 : #include "RialtoServerLogging.h"
23 : #include <cmath>
24 : #include <gst/gst.h>
25 : #include <stdexcept>
26 :
27 : namespace firebolt::rialto::server::tasks::generic
28 : {
29 9 : SetVolume::SetVolume(GenericPlayerContext &context, IGstGenericPlayerPrivate &player,
30 : std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> gstWrapper,
31 : std::shared_ptr<firebolt::rialto::wrappers::IGlibWrapper> glibWrapper,
32 : std::shared_ptr<firebolt::rialto::wrappers::IRdkGstreamerUtilsWrapper> rdkGstreamerUtilsWrapper,
33 9 : double targetVolume, uint32_t volumeDuration, firebolt::rialto::EaseType easeType)
34 9 : : m_context{context}, m_player{player}, m_gstWrapper{gstWrapper}, m_glibWrapper{glibWrapper},
35 9 : m_rdkGstreamerUtilsWrapper{rdkGstreamerUtilsWrapper}, m_targetVolume{targetVolume},
36 9 : m_volumeDuration{volumeDuration}, m_easeType{easeType}
37 : {
38 9 : RIALTO_SERVER_LOG_DEBUG("Constructing SetVolume");
39 : }
40 :
41 10 : SetVolume::~SetVolume()
42 : {
43 9 : RIALTO_SERVER_LOG_DEBUG("SetVolume finished");
44 10 : }
45 :
46 3 : firebolt::rialto::wrappers::rgu_Ease convertEaseTypeToRguEase(EaseType easeType)
47 : {
48 3 : switch (easeType)
49 : {
50 1 : case EaseType::EASE_LINEAR:
51 1 : return firebolt::rialto::wrappers::rgu_Ease::EaseLinear;
52 1 : case EaseType::EASE_IN_CUBIC:
53 1 : return firebolt::rialto::wrappers::rgu_Ease::EaseInCubic;
54 1 : case EaseType::EASE_OUT_CUBIC:
55 1 : return firebolt::rialto::wrappers::rgu_Ease::EaseOutCubic;
56 0 : default:
57 0 : RIALTO_SERVER_LOG_ERROR("Unknown EaseType, defaulting to EaseLinear");
58 0 : return firebolt::rialto::wrappers::rgu_Ease::EaseLinear;
59 : }
60 : }
61 :
62 8 : void SetVolume::execute() const
63 : {
64 8 : RIALTO_SERVER_LOG_DEBUG("Executing SetVolume");
65 :
66 8 : if (!m_context.pipeline)
67 : {
68 1 : RIALTO_SERVER_LOG_ERROR("Setting volume failed. Pipeline is NULL");
69 1 : return;
70 : }
71 7 : bool isImmediateVolumeChange = (m_volumeDuration == 0);
72 7 : GstElement *audioSink = m_player.getSink(firebolt::rialto::MediaSourceType::AUDIO);
73 :
74 7 : if (isImmediateVolumeChange)
75 : {
76 1 : RIALTO_SERVER_LOG_DEBUG("Immediate volume change, setting volume directly");
77 2 : m_gstWrapper->gstStreamVolumeSetVolume(GST_STREAM_VOLUME(m_context.pipeline), GST_STREAM_VOLUME_FORMAT_LINEAR,
78 1 : m_targetVolume);
79 : }
80 6 : else if (audioSink && m_glibWrapper->gObjectClassFindProperty(G_OBJECT_GET_CLASS(audioSink), "audio-fade"))
81 : {
82 : gchar fadeStr[32];
83 3 : uint32_t scaledTarget = trunc(100 * m_targetVolume);
84 3 : std::string easeString = "L";
85 :
86 3 : switch (m_easeType)
87 : {
88 1 : default:
89 : case firebolt::rialto::EaseType::EASE_LINEAR:
90 1 : easeString = "L";
91 1 : break;
92 1 : case firebolt::rialto::EaseType::EASE_IN_CUBIC:
93 1 : easeString = "I";
94 1 : break;
95 1 : case firebolt::rialto::EaseType::EASE_OUT_CUBIC:
96 1 : easeString = "O";
97 1 : break;
98 : }
99 :
100 3 : snprintf(reinterpret_cast<gchar *>(fadeStr), sizeof(fadeStr), "%u,%u,%s", scaledTarget, m_volumeDuration,
101 : easeString.c_str());
102 3 : RIALTO_SERVER_LOG_DEBUG("Fade String: %s", fadeStr);
103 :
104 3 : m_glibWrapper->gObjectSet(audioSink, "audio-fade", fadeStr, nullptr);
105 3 : m_context.audioFadeEnabled = true;
106 : }
107 3 : else if (m_rdkGstreamerUtilsWrapper->isSocAudioFadeSupported())
108 : {
109 3 : RIALTO_SERVER_LOG_DEBUG("SOC audio fading is supported, applying SOC audio fade");
110 3 : auto rguEaseType = convertEaseTypeToRguEase(m_easeType);
111 3 : m_rdkGstreamerUtilsWrapper->doAudioEasingonSoc(m_targetVolume, m_volumeDuration, rguEaseType);
112 3 : m_context.audioFadeEnabled = true;
113 : }
114 : else
115 : {
116 0 : RIALTO_SERVER_LOG_WARN("No audio-fade property found in audio sink or SOC");
117 : }
118 :
119 7 : if (audioSink)
120 3 : m_gstWrapper->gstObjectUnref(GST_OBJECT(audioSink));
121 : }
122 : } // namespace firebolt::rialto::server::tasks::generic
|