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 "foundation/log.h"
20 
21 namespace OHOS {
22 namespace Media {
23 namespace Plugin {
24 namespace StreamSource {
StreamSourcePluginCreator(const std::string & name)25 std::shared_ptr<SourcePlugin> StreamSourcePluginCreator(const std::string& name)
26 {
27     return std::make_shared<StreamSourcePlugin>(name);
28 }
29 
StreamSourceRegister(const std::shared_ptr<Register> & reg)30 const Status StreamSourceRegister(const std::shared_ptr<Register>& reg)
31 {
32     SourcePluginDef definition;
33     definition.name = "StreamSource";
34     definition.description = "Stream source";
35     definition.rank = 100; // 100: max rank
36     definition.protocol.emplace_back(ProtocolType::STREAM);
37     definition.creator = StreamSourcePluginCreator;
38     return reg->AddPlugin(definition);
39 }
40 
__anoned29b5000102null41 PLUGIN_DEFINITION(StreamSource, LicenseType::APACHE_V2, StreamSourceRegister, [] {});
42 
43 
StreamSourcePlugin(std::string name)44 StreamSourcePlugin::StreamSourcePlugin(std::string name)
45     : SourcePlugin(std::move(name))
46 {
47     MEDIA_LOG_D("ctor called");
48 }
49 
~StreamSourcePlugin()50 StreamSourcePlugin::~StreamSourcePlugin()
51 {
52     MEDIA_LOG_D("dtor called");
53 }
54 
Init()55 Status StreamSourcePlugin::Init()
56 {
57     MEDIA_LOG_D("IN");
58     return Status::OK;
59 }
60 
Deinit()61 Status StreamSourcePlugin::Deinit()
62 {
63     MEDIA_LOG_D("IN");
64     return Status::OK;
65 }
66 
Prepare()67 Status StreamSourcePlugin::Prepare()
68 {
69     MEDIA_LOG_D("IN");
70     return Status::OK;
71 }
72 
Reset()73 Status StreamSourcePlugin::Reset()
74 {
75     MEDIA_LOG_D("IN");
76     return Status::OK;
77 }
78 
Start()79 Status StreamSourcePlugin::Start()
80 {
81     MEDIA_LOG_D("IN");
82     return Status::OK;
83 }
84 
Stop()85 Status StreamSourcePlugin::Stop()
86 {
87     MEDIA_LOG_D("IN");
88     return Status::OK;
89 }
90 
GetParameter(Tag tag,ValueType & value)91 Status StreamSourcePlugin::GetParameter(Tag tag, ValueType& value)
92 {
93     MEDIA_LOG_D("IN");
94     return Status::OK;
95 }
96 
SetParameter(Tag tag,const ValueType & value)97 Status StreamSourcePlugin::SetParameter(Tag tag, const ValueType& value)
98 {
99     MEDIA_LOG_D("IN");
100     return Status::OK;
101 }
102 
SetCallback(Callback * cb)103 Status StreamSourcePlugin::SetCallback(Callback* cb)
104 {
105     MEDIA_LOG_D("IN");
106     return Status::OK;
107 }
108 
SetSource(std::shared_ptr<MediaSource> source)109 Status StreamSourcePlugin::SetSource(std::shared_ptr<MediaSource> source)
110 {
111     stream_ = source->GetDataConsumer();
112     FALSE_RETURN_V(stream_ != nullptr, Status::ERROR_INVALID_PARAMETER);
113     return Status::OK;
114 }
115 
WrapDataBuffer(const std::shared_ptr<DataBuffer> & dataBuffer)116 std::shared_ptr<Buffer> StreamSourcePlugin::WrapDataBuffer(const std::shared_ptr<DataBuffer>& dataBuffer)
117 {
118     FALSE_RETURN_V(dataBuffer != nullptr, nullptr);
119     std::shared_ptr<Buffer> buffer = std::make_shared<Buffer>();
120     auto deleter = [this](uint8_t* ptr) {  FALSE_LOG(stream_->QueueEmptyBuffer(ptr));  };
121     std::shared_ptr<uint8_t> address = std::shared_ptr<uint8_t>(dataBuffer->GetAddress(), deleter);
122     buffer->WrapMemoryPtr(address, dataBuffer->GetCapacity(), dataBuffer->GetSize());
123     if (dataBuffer->IsEos()) {
124         buffer->flag |= BUFFER_FLAG_EOS;
125     }
126     return buffer;
127 }
128 
129 // stream source is non-seekable
130 // so this read is called by MediaSourceFilter::ReadLoop
131 // ReadLoop always not provider buffer, so no need copy here, and no need to care about buffer count/length.
132 // We always process buffer count/length in DemuxerFitler's DataPacker.
Read(std::shared_ptr<Buffer> & buffer,size_t expectedLen)133 Status StreamSourcePlugin::Read(std::shared_ptr<Buffer>& buffer, size_t expectedLen)
134 {
135     std::shared_ptr<DataBuffer> dataBuffer;
136     FALSE_RETURN_V(stream_->GetDataBuffer(dataBuffer), Status::ERROR_TIMED_OUT);
137     buffer = WrapDataBuffer(dataBuffer);
138     return Status::OK;
139 }
140 
GetSize(uint64_t & size)141 Status StreamSourcePlugin::GetSize(uint64_t& size)
142 {
143     MEDIA_LOG_D("IN");
144     size = 0;
145     return Status::ERROR_WRONG_STATE;
146 }
147 
GetSeekable()148 Seekable StreamSourcePlugin::GetSeekable()
149 {
150     MEDIA_LOG_D("IN");
151     return seekable_;
152 }
153 
SeekTo(uint64_t offset)154 Status StreamSourcePlugin::SeekTo(uint64_t offset)
155 {
156     MEDIA_LOG_D("IN");
157     return Status::ERROR_UNIMPLEMENTED;
158 }
159 } // namespace StreamSource
160 } // namespace Plugin
161 } // namespace Media
162 } // namespace OHOS
163