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/ReadShmDataAndAttachSamples.h"
21 : #include "GenericPlayerContext.h"
22 : #include "IDataReader.h"
23 : #include "IGstGenericPlayerPrivate.h"
24 : #include "IMediaPipeline.h"
25 : #include "RialtoServerLogging.h"
26 : #include "TypeConverters.h"
27 :
28 : namespace firebolt::rialto::server::tasks::generic
29 : {
30 6 : ReadShmDataAndAttachSamples::ReadShmDataAndAttachSamples(
31 : GenericPlayerContext &context, const std::shared_ptr<firebolt::rialto::wrappers::IGstWrapper> &gstWrapper,
32 6 : IGstGenericPlayerPrivate &player, const std::shared_ptr<IDataReader> &dataReader)
33 6 : : m_context{context}, m_gstWrapper{gstWrapper}, m_player{player}, m_dataReader{dataReader}
34 : {
35 6 : RIALTO_SERVER_LOG_DEBUG("Constructing ReadShmDataAndAttachSamples");
36 : }
37 :
38 7 : ReadShmDataAndAttachSamples::~ReadShmDataAndAttachSamples()
39 : {
40 6 : RIALTO_SERVER_LOG_DEBUG("ReadShmDataAndAttachSamples finished");
41 7 : }
42 :
43 5 : void ReadShmDataAndAttachSamples::execute() const
44 : {
45 5 : RIALTO_SERVER_LOG_DEBUG("Executing ReadShmDataAndAttachSamples");
46 : // Read media segments from shared memory
47 5 : IMediaPipeline::MediaSegmentVector mediaSegments = m_dataReader->readData();
48 :
49 15 : for (const auto &mediaSegment : mediaSegments)
50 : {
51 10 : if (mediaSegment->getType() == firebolt::rialto::MediaSourceType::UNKNOWN)
52 : {
53 2 : RIALTO_SERVER_LOG_WARN("Unknown media segment type");
54 2 : continue;
55 : }
56 :
57 8 : GstBuffer *gstBuffer = m_player.createBuffer(*mediaSegment);
58 8 : if (mediaSegment->getType() == firebolt::rialto::MediaSourceType::VIDEO)
59 : {
60 : try
61 : {
62 : IMediaPipeline::MediaSegmentVideo &videoSegment =
63 2 : dynamic_cast<IMediaPipeline::MediaSegmentVideo &>(*mediaSegment);
64 2 : m_player.updateVideoCaps(videoSegment.getWidth(), videoSegment.getHeight(), videoSegment.getFrameRate(),
65 : videoSegment.getCodecData());
66 : }
67 0 : catch (const std::exception &e)
68 : {
69 0 : RIALTO_SERVER_LOG_ERROR("Failed to get the video segment, reason: %s", e.what());
70 : }
71 : }
72 6 : else if (mediaSegment->getType() == firebolt::rialto::MediaSourceType::AUDIO)
73 : {
74 : try
75 : {
76 : IMediaPipeline::MediaSegmentAudio &audioSegment =
77 2 : dynamic_cast<IMediaPipeline::MediaSegmentAudio &>(*mediaSegment);
78 2 : m_player.updateAudioCaps(audioSegment.getSampleRate(), audioSegment.getNumberOfChannels(),
79 : audioSegment.getCodecData());
80 2 : m_player.addAudioClippingToBuffer(gstBuffer, audioSegment.getClippingStart(),
81 : audioSegment.getClippingEnd());
82 : }
83 0 : catch (const std::exception &e)
84 : {
85 0 : RIALTO_SERVER_LOG_ERROR("Failed to get the audio segment, reason: %s", e.what());
86 : }
87 : }
88 : // no special action for SUBTITLE needed, just attach the buffer
89 :
90 8 : attachData(mediaSegment->getType(), gstBuffer);
91 : }
92 : // All segments in vector have the same type
93 5 : if (!mediaSegments.empty())
94 : {
95 5 : m_player.notifyNeedMediaData(mediaSegments.front()->getType());
96 : }
97 : }
98 :
99 8 : void ReadShmDataAndAttachSamples::attachData(const firebolt::rialto::MediaSourceType mediaType, GstBuffer *buffer) const
100 : {
101 8 : auto elem = m_context.streamInfo.find(mediaType);
102 8 : if (elem != m_context.streamInfo.end())
103 : {
104 6 : elem->second.buffers.push_back(buffer);
105 6 : m_player.attachData(mediaType);
106 : }
107 : else
108 : {
109 2 : RIALTO_SERVER_LOG_WARN("Could not find stream info for %s", common::convertMediaSourceType(mediaType));
110 2 : m_gstWrapper->gstBufferUnref(buffer);
111 : }
112 8 : }
113 : } // namespace firebolt::rialto::server::tasks::generic
|