• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 #include "buffer_source_stream.h"
17 
18 #include <string>
19 #include "image_log.h"
20 #ifndef _WIN32
21 #include "securec.h"
22 #else
23 #include "memory.h"
24 #endif
25 #include "buffer_packer_stream.h"
26 
27 namespace OHOS {
28 namespace Media {
29 using namespace OHOS::HiviewDFX;
30 using namespace std;
31 using namespace ImagePlugin;
32 
BufferSourceStream(uint8_t * data,uint32_t size,uint32_t offset)33 BufferSourceStream::BufferSourceStream(uint8_t *data, uint32_t size, uint32_t offset)
34     : inputBuffer_(data), dataSize_(size), dataOffset_(offset)
35 {}
36 
~BufferSourceStream()37 BufferSourceStream::~BufferSourceStream()
38 {
39     if (inputBuffer_ != nullptr) {
40         free(inputBuffer_);
41         inputBuffer_ = nullptr;
42     }
43 }
44 
CreateSourceStream(const uint8_t * data,uint32_t size)45 std::unique_ptr<BufferSourceStream> BufferSourceStream::CreateSourceStream(const uint8_t *data, uint32_t size)
46 {
47     if ((data == nullptr) || (size == 0)) {
48         IMAGE_LOGE("[BufferSourceStream]input the parameter exception.");
49         return nullptr;
50     }
51     uint8_t *dataCopy = static_cast<uint8_t *>(malloc(size));
52     if (dataCopy == nullptr) {
53         IMAGE_LOGE("[BufferSourceStream]malloc the input data buffer fail.");
54         return nullptr;
55     }
56     errno_t ret = memcpy_s(dataCopy, size, data, size);
57     if (ret != EOK) {
58         free(dataCopy);
59         dataCopy = nullptr;
60         IMAGE_LOGE("[BufferSourceStream]copy the input data fail, ret:%{public}d.", ret);
61         return nullptr;
62     }
63     return (unique_ptr<BufferSourceStream>(new BufferSourceStream(dataCopy, size, 0)));
64 }
65 
Read(uint32_t desiredSize,DataStreamBuffer & outData)66 bool BufferSourceStream::Read(uint32_t desiredSize, DataStreamBuffer &outData)
67 {
68     if (!Peek(desiredSize, outData)) {
69         IMAGE_LOGE("[BufferSourceStream]read fail.");
70         return false;
71     }
72     dataOffset_ += outData.dataSize;
73     return true;
74 }
75 
Peek(uint32_t desiredSize,DataStreamBuffer & outData)76 bool BufferSourceStream::Peek(uint32_t desiredSize, DataStreamBuffer &outData)
77 {
78     if (desiredSize == 0) {
79         IMAGE_LOGE("[BufferSourceStream]input the parameter exception.");
80         return false;
81     }
82     if (dataSize_ == dataOffset_) {
83         IMAGE_LOGE("[BufferSourceStream]buffer read finish, offset:%{public}zu ,dataSize%{public}zu.", dataOffset_,
84                    dataSize_);
85         return false;
86     }
87     outData.bufferSize = dataSize_ - dataOffset_;
88     if (desiredSize > dataSize_ - dataOffset_) {
89         desiredSize = dataSize_ - dataOffset_;
90     }
91     outData.dataSize = desiredSize;
92     outData.inputStreamBuffer = inputBuffer_ + dataOffset_;
93     IMAGE_LOGD("[BufferSourceStream]Peek end. desiredSize:%{public}d, offset:%{public}zu ,dataSize%{public}zu.",
94                desiredSize, dataOffset_, dataSize_);
95     return true;
96 }
97 
Read(uint32_t desiredSize,uint8_t * outBuffer,uint32_t bufferSize,uint32_t & readSize)98 bool BufferSourceStream::Read(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize)
99 {
100     if (!Peek(desiredSize, outBuffer, bufferSize, readSize)) {
101         IMAGE_LOGE("[BufferSourceStream]read fail.");
102         return false;
103     }
104     dataOffset_ += readSize;
105     return true;
106 }
107 
Peek(uint32_t desiredSize,uint8_t * outBuffer,uint32_t bufferSize,uint32_t & readSize)108 bool BufferSourceStream::Peek(uint32_t desiredSize, uint8_t *outBuffer, uint32_t bufferSize, uint32_t &readSize)
109 {
110     if (desiredSize == 0 || outBuffer == nullptr || desiredSize > bufferSize) {
111         IMAGE_LOGE("[BufferSourceStream]input the parameter exception, desiredSize:%{public}u, bufferSize:%{public}u.",
112                    desiredSize, bufferSize);
113         return false;
114     }
115     if (dataSize_ == dataOffset_) {
116         IMAGE_LOGE("[BufferSourceStream]buffer read finish, offset:%{public}zu ,dataSize%{public}zu.", dataOffset_,
117                    dataSize_);
118         return false;
119     }
120     if (desiredSize > dataSize_ - dataOffset_) {
121         desiredSize = dataSize_ - dataOffset_;
122     }
123     errno_t ret = memcpy_s(outBuffer, bufferSize, inputBuffer_ + dataOffset_, desiredSize);
124     if (ret != EOK) {
125         IMAGE_LOGE("[BufferSourceStream]copy data fail, ret:%{public}d, bufferSize:%{public}u, offset:%{public}zu, \
126                    desiredSize:%{public}u.",
127                    ret, bufferSize, dataOffset_, desiredSize);
128         return false;
129     }
130     readSize = desiredSize;
131     return true;
132 }
133 
Tell()134 uint32_t BufferSourceStream::Tell()
135 {
136     return dataOffset_;
137 }
138 
Seek(uint32_t position)139 bool BufferSourceStream::Seek(uint32_t position)
140 {
141     if (position > dataSize_) {
142         IMAGE_LOGE("[BufferSourceStream]Seek the position greater than the Data Size,position:%{public}u.", position);
143         return false;
144     }
145     dataOffset_ = position;
146     return true;
147 }
148 
GetStreamSize()149 size_t BufferSourceStream::GetStreamSize()
150 {
151     return dataSize_;
152 }
153 
GetDataPtr()154 uint8_t *BufferSourceStream::GetDataPtr()
155 {
156     return inputBuffer_;
157 }
158 
GetStreamType()159 uint32_t BufferSourceStream::GetStreamType()
160 {
161     return ImagePlugin::BUFFER_SOURCE_TYPE;
162 }
163 
ToOutputDataStream()164 OutputDataStream* BufferSourceStream::ToOutputDataStream()
165 {
166     return new (std::nothrow) BufferPackerStream(inputBuffer_, dataSize_);
167 }
168 }  // namespace Media
169 }  // namespace OHOS
170