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 #ifndef SURFACE_DISABLED
65 : surface_(nullptr),
66 curBuffer_(nullptr)
67 #endif
68 {}
69
~StreamSource()70 StreamSource::~StreamSource()
71 {
72 MEDIA_ERR_LOG("[%s,%d] in", __func__, __LINE__);
73 #ifndef SURFACE_DISABLED
74 if (surface_ != nullptr) {
75 delete surface_;
76 }
77 #endif
78 }
79
80 #ifndef SURFACE_DISABLED
SetSurface(Surface * surface)81 void StreamSource::SetSurface(Surface* surface)
82 {
83 surface_ = surface;
84 }
85
GetSurface(void)86 Surface* StreamSource::GetSurface(void)
87 {
88 return surface_;
89 }
90 #endif
91
GetSharedBuffer(size_t & size)92 uint8_t* StreamSource::GetSharedBuffer(size_t& size)
93 {
94 #ifndef SURFACE_DISABLED
95 if ((surface_ == nullptr) || (curBuffer_ != nullptr)) {
96 return nullptr;
97 }
98 SurfaceBuffer* surfaceBuffer = surface_->RequestBuffer();
99 if (surfaceBuffer != nullptr) {
100 curBuffer_ = surfaceBuffer;
101 size = surface_->GetSize();
102 return static_cast<uint8_t*>(surfaceBuffer->GetVirAddr());
103 } else {
104 return nullptr;
105 }
106 #else
107 return nullptr;
108 #endif
109 }
110
QueueSharedBuffer(void * buffer,size_t size)111 int StreamSource::QueueSharedBuffer(void* buffer, size_t size)
112 {
113 #ifndef SURFACE_DISABLED
114 if ((surface_ == nullptr) || (buffer == nullptr) || (curBuffer_ == nullptr)) {
115 return -1;
116 }
117
118 if(buffer != curBuffer_->GetVirAddr()) {
119 return -1;
120 }
121 curBuffer_->SetInt32(0, size);
122 if (surface_->FlushBuffer(curBuffer_) != 0) {
123 surface_->CancelBuffer(curBuffer_);
124 curBuffer_ = nullptr;
125 return -1;
126 }
127 curBuffer_ = nullptr;
128 #endif
129 return 0;
130 }
131 } // namespace Media
132 } // namespace OHOS
133