1 /*
2 * Copyright (c) 2021-2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #define HST_LOG_TAG "StreamSourcePlugin"
17
18 #include "stream_source_plugin.h"
19 #include "plugin/common/plugin_buffer.h"
20 #include "plugin/common/plugin_source_tags.h"
21 #include "plugin/core/plugin_manager.h"
22 #include "foundation/log.h"
23
24 namespace OHOS {
25 namespace Media {
26 namespace Plugin {
27 namespace StreamSource {
StreamSourcePluginCreator(const std::string & name)28 std::shared_ptr<SourcePlugin> StreamSourcePluginCreator(const std::string& name)
29 {
30 return std::make_shared<StreamSourcePlugin>(name);
31 }
32
StreamSourceRegister(const std::shared_ptr<Register> & reg)33 const Status StreamSourceRegister(const std::shared_ptr<Register>& reg)
34 {
35 SourcePluginDef definition;
36 definition.name = "StreamSource";
37 definition.description = "Stream source";
38 definition.rank = 100; // 100: max rank
39 definition.protocol.emplace_back(ProtocolType::STREAM);
40 definition.creator = StreamSourcePluginCreator;
41 return reg->AddPlugin(definition);
42 }
43
__anon69d9e1870102null44 PLUGIN_DEFINITION(StreamSource, LicenseType::APACHE_V2, StreamSourceRegister, [] {});
45
46
StreamSourcePlugin(std::string name)47 StreamSourcePlugin::StreamSourcePlugin(std::string name)
48 : SourcePlugin(std::move(name))
49 {
50 MEDIA_LOG_D("ctor called");
51 }
52
~StreamSourcePlugin()53 StreamSourcePlugin::~StreamSourcePlugin()
54 {
55 MEDIA_LOG_D("dtor called");
56 }
57
Init()58 Status StreamSourcePlugin::Init()
59 {
60 MEDIA_LOG_D("IN");
61 return Status::OK;
62 }
63
Deinit()64 Status StreamSourcePlugin::Deinit()
65 {
66 MEDIA_LOG_D("IN");
67 return Status::OK;
68 }
69
Prepare()70 Status StreamSourcePlugin::Prepare()
71 {
72 MEDIA_LOG_D("IN");
73 return Status::OK;
74 }
75
Reset()76 Status StreamSourcePlugin::Reset()
77 {
78 MEDIA_LOG_D("IN");
79 return Status::OK;
80 }
81
Start()82 Status StreamSourcePlugin::Start()
83 {
84 MEDIA_LOG_D("IN");
85 return Status::OK;
86 }
87
Stop()88 Status StreamSourcePlugin::Stop()
89 {
90 MEDIA_LOG_D("IN");
91 return Status::OK;
92 }
93
GetParameter(Tag tag,ValueType & value)94 Status StreamSourcePlugin::GetParameter(Tag tag, ValueType& value)
95 {
96 MEDIA_LOG_D("IN");
97 return Status::OK;
98 }
99
SetParameter(Tag tag,const ValueType & value)100 Status StreamSourcePlugin::SetParameter(Tag tag, const ValueType& value)
101 {
102 MEDIA_LOG_D("IN");
103 return Status::OK;
104 }
105
SetCallback(Callback * cb)106 Status StreamSourcePlugin::SetCallback(Callback* cb)
107 {
108 MEDIA_LOG_D("IN");
109 return Status::OK;
110 }
111
SetSource(std::shared_ptr<MediaSource> source)112 Status StreamSourcePlugin::SetSource(std::shared_ptr<MediaSource> source)
113 {
114 stream_ = source->GetDataConsumer();
115 FALSE_RETURN_V(stream_ != nullptr, Status::ERROR_INVALID_PARAMETER);
116 return Status::OK;
117 }
118
WrapDataBuffer(const std::shared_ptr<DataBuffer> & dataBuffer)119 std::shared_ptr<Buffer> StreamSourcePlugin::WrapDataBuffer(const std::shared_ptr<DataBuffer>& dataBuffer)
120 {
121 FALSE_RETURN_V(dataBuffer != nullptr, nullptr);
122 std::shared_ptr<Buffer> buffer = std::make_shared<Buffer>();
123 auto deleter = [this](uint8_t* ptr) { FALSE_LOG(stream_->QueueEmptyBuffer(ptr)); };
124 std::shared_ptr<uint8_t> address = std::shared_ptr<uint8_t>(dataBuffer->GetAddress(), deleter);
125 buffer->WrapMemoryPtr(address, dataBuffer->GetCapacity(), dataBuffer->GetSize());
126 if (dataBuffer->IsEos()) {
127 buffer->flag |= BUFFER_FLAG_EOS;
128 }
129 return buffer;
130 }
131
132 // stream source is non-seekable
133 // so this read is called by MediaSourceFilter::ReadLoop
134 // ReadLoop always not provider buffer, so no need copy here, and no need to care about buffer count/length.
135 // We always process buffer count/length in DemuxerFitler's DataPacker.
Read(std::shared_ptr<Buffer> & buffer,size_t expectedLen)136 Status StreamSourcePlugin::Read(std::shared_ptr<Buffer>& buffer, size_t expectedLen)
137 {
138 std::shared_ptr<DataBuffer> dataBuffer;
139 FALSE_RETURN_V(stream_->GetDataBuffer(dataBuffer), Status::ERROR_TIMED_OUT);
140 buffer = WrapDataBuffer(dataBuffer);
141 return Status::OK;
142 }
143
GetSize(size_t & size)144 Status StreamSourcePlugin::GetSize(size_t& size)
145 {
146 MEDIA_LOG_D("IN");
147 size = 0;
148 return Status::ERROR_WRONG_STATE;
149 }
150
GetSeekable()151 Seekable StreamSourcePlugin::GetSeekable()
152 {
153 MEDIA_LOG_D("IN");
154 return seekable_;
155 }
156
SeekTo(uint64_t offset)157 Status StreamSourcePlugin::SeekTo(uint64_t offset)
158 {
159 MEDIA_LOG_D("IN");
160 return Status::ERROR_UNIMPLEMENTED;
161 }
162 } // namespace StreamSource
163 } // namespace Plugin
164 } // namespace Media
165 } // namespace OHOS
166