1 /*
2 * Copyright (c) 2020 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 #include "source.h"
17 #include "media_log.h"
18
19 namespace OHOS {
20 namespace Media {
Source(const std::string & uri)21 Source::Source(const std::string &uri)
22 : uri_(uri),
23 sourceType_(SourceType::SOURCE_TYPE_URI)
24 {}
25
Source(const std::string & uri,const std::map<std::string,std::string> & header)26 Source::Source(const std::string &uri, const std::map<std::string, std::string> &header)
27 : uri_(uri),
28 sourceType_(SourceType::SOURCE_TYPE_URI),
29 header_(header)
30 {}
31
Source(const std::shared_ptr<StreamSource> & stream,const Format & formats)32 Source::Source(const std::shared_ptr<StreamSource> &stream, const Format &formats)
33 : sourceType_(SourceType::SOURCE_TYPE_STREAM),
34 stream_(stream)
35 {
36 format_.CopyFrom(formats);
37 }
38
GetSourceType() const39 SourceType Source::GetSourceType() const
40 {
41 return sourceType_;
42 }
43
GetSourceUri() const44 const std::string &Source::GetSourceUri() const
45 {
46 return uri_;
47 }
48
GetSourceHeader() const49 const std::map<std::string, std::string> &Source::GetSourceHeader() const
50 {
51 return header_;
52 }
53
GetSourceStream() const54 const std::shared_ptr<StreamSource> &Source::GetSourceStream() const
55 {
56 return stream_;
57 }
GetSourceStreamFormat() const58 const Format &Source::GetSourceStreamFormat() const
59 {
60 return format_;
61 }
62
StreamSource()63 StreamSource::StreamSource()
64 : surface_(nullptr),
65 curBuffer_(nullptr)
66 {}
67
~StreamSource()68 StreamSource::~StreamSource()
69 {
70 MEDIA_ERR_LOG("[%s,%d] in", __func__, __LINE__);
71 if (surface_ != nullptr) {
72 delete surface_;
73 }
74 }
75
SetSurface(Surface * surface)76 void StreamSource::SetSurface(Surface* surface)
77 {
78 surface_ = surface;
79 }
80
GetSurface(void)81 Surface* StreamSource::GetSurface(void)
82 {
83 return surface_;
84 }
85
86
GetSharedBuffer(size_t & size)87 uint8_t* StreamSource::GetSharedBuffer(size_t& size)
88 {
89 if ((surface_ == nullptr) || (curBuffer_ != nullptr)){
90 return nullptr;
91 }
92 SurfaceBuffer* surfaceBuffer = surface_->RequestBuffer();
93 if (surfaceBuffer != nullptr) {
94 curBuffer_ = surfaceBuffer;
95 size = surface_->GetSize();
96 return static_cast<uint8_t*>(surfaceBuffer->GetVirAddr());
97 } else {
98 return nullptr;
99 }
100 }
101
QueueSharedBuffer(void * buffer,size_t size)102 int StreamSource::QueueSharedBuffer(void* buffer, size_t size)
103 {
104 if ((surface_ == nullptr) || (buffer == nullptr) || (curBuffer_ == nullptr)){
105 return -1;
106 }
107
108 if(buffer != curBuffer_->GetVirAddr()) {
109 return -1;
110 }
111 curBuffer_->SetInt32(0, size);
112 if (surface_->FlushBuffer(curBuffer_) != 0) {
113 surface_->CancelBuffer(curBuffer_);
114 curBuffer_ = nullptr;
115 return -1;
116 }
117 curBuffer_ = nullptr;
118 return 0;
119 }
120
121 } // namespace Media
122 } // namespace OHOS
123