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/AttachSamples.h"
21 : #include "GenericPlayerContext.h"
22 : #include "IGstGenericPlayerPrivate.h"
23 : #include "RialtoServerLogging.h"
24 : #include "TypeConverters.h"
25 : #include <utility>
26 :
27 : namespace firebolt::rialto::server::tasks::generic
28 : {
29 5 : AttachSamples::AttachSamples(GenericPlayerContext &context,
30 : const std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> &gstWrapper,
31 5 : IGstGenericPlayerPrivate &player, const IMediaPipeline::MediaSegmentVector &mediaSegments)
32 5 : : m_context{context}, m_gstWrapper{gstWrapper}, m_player{player}
33 : {
34 5 : RIALTO_SERVER_LOG_DEBUG("Constructing AttachSamples");
35 13 : for (const auto &mediaSegment : mediaSegments)
36 : {
37 8 : GstBuffer *gstBuffer = m_player.createBuffer(*mediaSegment);
38 8 : if (mediaSegment->getType() == firebolt::rialto::MediaSourceType::VIDEO)
39 : {
40 : try
41 : {
42 : IMediaPipeline::MediaSegmentVideo &videoSegment =
43 2 : dynamic_cast<IMediaPipeline::MediaSegmentVideo &>(*mediaSegment);
44 4 : VideoData videoData = {gstBuffer, videoSegment.getWidth(), videoSegment.getHeight(),
45 2 : videoSegment.getFrameRate(), videoSegment.getCodecData()};
46 2 : m_videoData.push_back(std::move(videoData));
47 : }
48 0 : catch (const std::exception &e)
49 : {
50 : // Catching error, but continuing as best as we can
51 0 : RIALTO_SERVER_LOG_ERROR("Failed to get the video segment, reason: %s", e.what());
52 0 : m_gstWrapper->gstBufferUnref(gstBuffer);
53 : }
54 : }
55 6 : else if (mediaSegment->getType() == firebolt::rialto::MediaSourceType::AUDIO)
56 : {
57 : try
58 : {
59 : IMediaPipeline::MediaSegmentAudio &audioSegment =
60 2 : dynamic_cast<IMediaPipeline::MediaSegmentAudio &>(*mediaSegment);
61 : AudioData audioData = {gstBuffer,
62 2 : audioSegment.getSampleRate(),
63 2 : audioSegment.getNumberOfChannels(),
64 : audioSegment.getCodecData(),
65 2 : audioSegment.getClippingStart(),
66 2 : audioSegment.getClippingEnd()};
67 2 : m_audioData.push_back(std::move(audioData));
68 : }
69 0 : catch (const std::exception &e)
70 : {
71 : // Catching error, but continuing as best as we can
72 0 : RIALTO_SERVER_LOG_ERROR("Failed to get the audio segment, reason: %s", e.what());
73 0 : m_gstWrapper->gstBufferUnref(gstBuffer);
74 : }
75 : }
76 4 : else if (mediaSegment->getType() == firebolt::rialto::MediaSourceType::SUBTITLE)
77 : {
78 4 : m_subtitleData.push_back(gstBuffer);
79 : }
80 : }
81 5 : }
82 :
83 6 : AttachSamples::~AttachSamples()
84 : {
85 5 : RIALTO_SERVER_LOG_DEBUG("AttachSamples finished");
86 6 : }
87 :
88 4 : void AttachSamples::execute() const
89 : {
90 4 : RIALTO_SERVER_LOG_DEBUG("Executing AttachSamples");
91 6 : for (AudioData audioData : m_audioData)
92 : {
93 2 : m_player.updateAudioCaps(audioData.rate, audioData.channels, audioData.codecData);
94 2 : m_player.addAudioClippingToBuffer(audioData.buffer, audioData.clippingStart, audioData.clippingEnd);
95 :
96 2 : attachData(firebolt::rialto::MediaSourceType::AUDIO, audioData.buffer);
97 : }
98 6 : for (VideoData videoData : m_videoData)
99 : {
100 2 : m_player.updateVideoCaps(videoData.width, videoData.height, videoData.frameRate, videoData.codecData);
101 :
102 2 : attachData(firebolt::rialto::MediaSourceType::VIDEO, videoData.buffer);
103 : }
104 8 : for (GstBuffer *buffer : m_subtitleData)
105 : {
106 4 : attachData(firebolt::rialto::MediaSourceType::SUBTITLE, buffer);
107 : }
108 :
109 4 : if (!m_audioData.empty())
110 : {
111 1 : m_player.notifyNeedMediaData(firebolt::rialto::MediaSourceType::AUDIO);
112 : }
113 3 : else if (!m_videoData.empty())
114 : {
115 1 : m_player.notifyNeedMediaData(firebolt::rialto::MediaSourceType::VIDEO);
116 : }
117 2 : else if (!m_subtitleData.empty())
118 : {
119 2 : m_player.notifyNeedMediaData(firebolt::rialto::MediaSourceType::SUBTITLE);
120 : }
121 4 : }
122 :
123 8 : void AttachSamples::attachData(const firebolt::rialto::MediaSourceType mediaType, GstBuffer *buffer) const
124 : {
125 8 : auto elem = m_context.streamInfo.find(mediaType);
126 8 : if (elem != m_context.streamInfo.end())
127 : {
128 6 : elem->second.buffers.push_back(buffer);
129 6 : m_player.attachData(mediaType);
130 : }
131 : else
132 : {
133 2 : RIALTO_SERVER_LOG_WARN("Could not find stream info for %s", common::convertMediaSourceType(mediaType));
134 2 : m_gstWrapper->gstBufferUnref(buffer);
135 : }
136 8 : }
137 :
138 : } // namespace firebolt::rialto::server::tasks::generic
|