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
Source(const std::shared_ptr<DataConsumer> & dataConsumer)39 Source::Source(const std::shared_ptr<DataConsumer> &dataConsumer)
40 : sourceType_(SourceType::SOURCE_TYPE_STREAM),
41 dataConsumer_(dataConsumer)
42 {
43 }
44
GetSourceType() const45 SourceType Source::GetSourceType() const
46 {
47 return sourceType_;
48 }
49
GetSourceUri() const50 const std::string &Source::GetSourceUri() const
51 {
52 return uri_;
53 }
54
GetSourceHeader() const55 const std::map<std::string, std::string> &Source::GetSourceHeader() const
56 {
57 return header_;
58 }
59
GetSourceStream() const60 const std::shared_ptr<StreamSource> &Source::GetSourceStream() const
61 {
62 return stream_;
63 }
GetSourceStreamFormat() const64 const Format &Source::GetSourceStreamFormat() const
65 {
66 return format_;
67 }
68
GetDataConsumer() const69 const std::shared_ptr<DataConsumer> &Source::GetDataConsumer() const
70 {
71 return dataConsumer_;
72 }
73
StreamSource()74 StreamSource::StreamSource()
75 #ifndef SURFACE_DISABLED
76 : surface_(nullptr),
77 curBuffer_(nullptr)
78 #endif
79 {}
80
~StreamSource()81 StreamSource::~StreamSource()
82 {
83 MEDIA_DEBUG_LOG("[%s,%d] in", __func__, __LINE__);
84 #ifndef SURFACE_DISABLED
85 if (surface_ != nullptr) {
86 delete surface_;
87 }
88 #endif
89 }
90
91 #ifndef SURFACE_DISABLED
SetSurface(Surface * surface)92 void StreamSource::SetSurface(Surface* surface)
93 {
94 surface_ = surface;
95 }
96
GetSurface(void)97 Surface* StreamSource::GetSurface(void)
98 {
99 return surface_;
100 }
101 #endif
102
GetSharedBuffer(size_t & size)103 uint8_t* StreamSource::GetSharedBuffer(size_t& size)
104 {
105 #ifndef SURFACE_DISABLED
106 if ((surface_ == nullptr) || (curBuffer_ != nullptr)) {
107 return nullptr;
108 }
109 SurfaceBuffer* surfaceBuffer = surface_->RequestBuffer();
110 if (surfaceBuffer != nullptr) {
111 curBuffer_ = surfaceBuffer;
112 size = surface_->GetSize();
113 return static_cast<uint8_t*>(surfaceBuffer->GetVirAddr());
114 } else {
115 return nullptr;
116 }
117 #else
118 return nullptr;
119 #endif
120 }
121
QueueSharedBuffer(void * buffer,size_t size)122 int StreamSource::QueueSharedBuffer(void* buffer, size_t size)
123 {
124 #ifndef SURFACE_DISABLED
125 if ((surface_ == nullptr) || (buffer == nullptr) || (curBuffer_ == nullptr)) {
126 return -1;
127 }
128
129 if(buffer != curBuffer_->GetVirAddr()) {
130 return -1;
131 }
132 curBuffer_->SetInt32(0, size);
133 if (surface_->FlushBuffer(curBuffer_) != 0) {
134 surface_->CancelBuffer(curBuffer_);
135 curBuffer_ = nullptr;
136 return -1;
137 }
138 curBuffer_ = nullptr;
139 #endif
140 return 0;
141 }
142 } // namespace Media
143 } // namespace OHOS
144