• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2022 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 #ifndef OHOS_LITE
16 #include "std_stream_source_plugin.h"
17 #include "foundation/log.h"
18 #include "media_errors.h"
19 
20 namespace OHOS {
21 namespace Media {
22 namespace Plugin {
23 namespace StdStreamSource {
24 namespace {
25     constexpr uint32_t INIT_MEM_CNT = 10;
26     constexpr int32_t MEM_SIZE = 10240;
27     constexpr uint32_t MAX_MEM_CNT = 100;
28 }
StdStreamSourcePluginCreator(const std::string & name)29 std::shared_ptr<SourcePlugin> StdStreamSourcePluginCreator(const std::string& name)
30 {
31     return std::make_shared<StdStreamSourcePlugin>(name);
32 }
33 
StdStreamSourceRegister(const std::shared_ptr<Register> & reg)34 Status StdStreamSourceRegister(const std::shared_ptr<Register>& reg)
35 {
36     SourcePluginDef definition;
37     definition.name = "StdStreamSource";
38     definition.description = "standard stream source";
39     definition.rank = 100; // 100: max rank
40     definition.protocol.emplace_back(ProtocolType::STREAM);
41     definition.creator = StdStreamSourcePluginCreator;
42     return reg->AddPlugin(definition);
43 }
44 
__anonb1483f1b0202null45 PLUGIN_DEFINITION(StdStreamSource, LicenseType::APACHE_V2, StdStreamSourceRegister, [] {});
46 
StdStreamSourcePlugin(std::string name)47 StdStreamSourcePlugin::StdStreamSourcePlugin(std::string name)
48     : SourcePlugin(std::move(name))
49 {
50     MEDIA_LOG_D("ctor called");
51     pool_ = std::make_shared<AVSharedMemoryPool>("pool");
52     InitPool();
53 }
54 
~StdStreamSourcePlugin()55 StdStreamSourcePlugin::~StdStreamSourcePlugin()
56 {
57     MEDIA_LOG_D("dtor called");
58     ResetPool();
59 }
60 
Init()61 Status StdStreamSourcePlugin::Init()
62 {
63     MEDIA_LOG_D("IN");
64     return Status::OK;
65 }
66 
Deinit()67 Status StdStreamSourcePlugin::Deinit()
68 {
69     MEDIA_LOG_D("IN");
70     return Status::OK;
71 }
72 
Prepare()73 Status StdStreamSourcePlugin::Prepare()
74 {
75     MEDIA_LOG_D("IN");
76     return Status::OK;
77 }
78 
Reset()79 Status StdStreamSourcePlugin::Reset()
80 {
81     MEDIA_LOG_D("IN");
82     return Status::OK;
83 }
84 
Start()85 Status StdStreamSourcePlugin::Start()
86 {
87     MEDIA_LOG_D("IN");
88     return Status::OK;
89 }
90 
Stop()91 Status StdStreamSourcePlugin::Stop()
92 {
93     MEDIA_LOG_D("IN");
94     return Status::OK;
95 }
96 
GetParameter(Tag tag,ValueType & value)97 Status StdStreamSourcePlugin::GetParameter(Tag tag, ValueType& value)
98 {
99     MEDIA_LOG_D("IN");
100     return Status::OK;
101 }
102 
SetParameter(Tag tag,const ValueType & value)103 Status StdStreamSourcePlugin::SetParameter(Tag tag, const ValueType& value)
104 {
105     MEDIA_LOG_D("IN");
106     return Status::OK;
107 }
108 
SetCallback(Callback * cb)109 Status StdStreamSourcePlugin::SetCallback(Callback* cb)
110 {
111     MEDIA_LOG_D("IN");
112     return Status::OK;
113 }
114 
SetSource(std::shared_ptr<MediaSource> source)115 Status StdStreamSourcePlugin::SetSource(std::shared_ptr<MediaSource> source)
116 {
117     dataSrc_ = source->GetDataSrc();
118     FALSE_RETURN_V(dataSrc_ != nullptr, Status::ERROR_INVALID_PARAMETER);
119     int64_t size = 0;
120     if (dataSrc_->GetSize(size) != MSERR_OK) {
121         MEDIA_LOG_E("Get size failed");
122     }
123     size_ = size;
124     seekable_ = size == -1 ? Seekable::UNSEEKABLE : Seekable::SEEKABLE;
125     return Status::OK;
126 }
127 
WrapAVSharedMemory(const std::shared_ptr<AVSharedMemory> & avSharedMemory,int32_t realLen)128 std::shared_ptr<Buffer> StdStreamSourcePlugin::WrapAVSharedMemory(const std::shared_ptr<AVSharedMemory>& avSharedMemory,
129                                                                   int32_t realLen)
130 {
131     std::shared_ptr<Buffer> buffer = std::make_shared<Buffer>();
132     std::shared_ptr<uint8_t> address = std::shared_ptr<uint8_t>(avSharedMemory->GetBase(),
133                                                                 [avSharedMemory](uint8_t* ptr) { ptr = nullptr; });
134     buffer->WrapMemoryPtr(address, avSharedMemory->GetSize(), realLen);
135     return buffer;
136 }
137 
InitPool()138 void StdStreamSourcePlugin::InitPool()
139 {
140     AVSharedMemoryPool::InitializeOption InitOption {
141         INIT_MEM_CNT,
142         MEM_SIZE,
143         MAX_MEM_CNT,
144         AVSharedMemory::Flags::FLAGS_READ_WRITE,
145         true,
146         nullptr,
147     };
148     pool_->Init(InitOption);
149 }
150 
GetMemory()151 std::shared_ptr<AVSharedMemory> StdStreamSourcePlugin::GetMemory()
152 {
153     return pool_->AcquireMemory(MEM_SIZE); // 10240
154 }
155 
ResetPool()156 void StdStreamSourcePlugin::ResetPool()
157 {
158     pool_->Reset();
159 }
160 
Read(std::shared_ptr<Buffer> & buffer,size_t expectedLen)161 Status StdStreamSourcePlugin::Read(std::shared_ptr<Buffer>& buffer, size_t expectedLen)
162 {
163     std::shared_ptr<AVSharedMemory> memory = GetMemory();
164     FALSE_RETURN_V_MSG(memory != nullptr, Status::ERROR_NO_MEMORY, "allocate memory failed!");
165     int32_t realLen;
166     if (seekable_ == Seekable::SEEKABLE) {
167         FALSE_RETURN_V(static_cast<int64_t>(offset_) < size_, Status::END_OF_STREAM);
168         expectedLen = std::min(static_cast<size_t>(size_ - offset_), expectedLen);
169         expectedLen = std::min(static_cast<size_t>(memory->GetSize()), expectedLen);
170         realLen = dataSrc_->ReadAt(static_cast<int64_t>(offset_), expectedLen, memory);
171     } else {
172         expectedLen = std::min(static_cast<size_t>(memory->GetSize()), expectedLen);
173         realLen = dataSrc_->ReadAt(expectedLen, memory);
174     }
175     if (realLen == MediaDataSourceError::SOURCE_ERROR_IO) {
176         MEDIA_LOG_E("read data source error");
177         return Status::ERROR_UNKNOWN;
178     }
179     if (realLen == MediaDataSourceError::SOURCE_ERROR_EOF) {
180         MEDIA_LOG_I("eos reached");
181         return Status::END_OF_STREAM;
182     }
183     offset_ += realLen;
184     buffer = WrapAVSharedMemory(memory, realLen);
185     return Status::OK;
186 }
187 
GetSize(size_t & size)188 Status StdStreamSourcePlugin::GetSize(size_t& size)
189 {
190     size = size_;
191     return Status::OK;
192 }
193 
GetSeekable()194 Seekable StdStreamSourcePlugin::GetSeekable()
195 {
196     return seekable_;
197 }
198 
SeekTo(uint64_t offset)199 Status StdStreamSourcePlugin::SeekTo(uint64_t offset)
200 {
201     if (seekable_ == Seekable::UNSEEKABLE) {
202         MEDIA_LOG_E("source is unseekable!");
203         return Status::ERROR_INVALID_OPERATION;
204     }
205     if (offset >= static_cast<uint64_t>(size_)) {
206         MEDIA_LOG_E("Invalid parameter");
207         return Status::ERROR_INVALID_PARAMETER;
208     }
209     offset_ = offset;
210     MEDIA_LOG_D("seek to offset_ " PUBLIC_LOG_U64 " success", offset_);
211     return Status::OK;
212 }
213 } // namespace StdStreamSource
214 } // namespace Plugin
215 } // namespace Media
216 } // namespace OHOS
217 #endif