1 /* 2 * Copyright (c) 2021-2023 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 "av_trans_data_buffer.h" 17 #include "dtbcollabmgr_log.h" 18 19 namespace OHOS { 20 namespace DistributedCollab { 21 namespace { 22 static const std::string TAG = "DSchedCollabAVTransDataBuffer"; 23 } 24 AVTransDataBuffer(size_t capacity)25AVTransDataBuffer::AVTransDataBuffer(size_t capacity) 26 { 27 if (capacity != 0 && capacity < DSCHED_MAX_BUFFER_SIZE) { 28 data_ = new uint8_t[capacity] { 0 }; 29 if (data_ != nullptr) { 30 capacity_ = capacity; 31 rangeLength_ = capacity; 32 } 33 } 34 } 35 ~AVTransDataBuffer()36AVTransDataBuffer::~AVTransDataBuffer() 37 { 38 if (data_ != nullptr) { 39 delete[] data_; 40 data_ = nullptr; 41 } 42 } 43 Size()44size_t AVTransDataBuffer::Size() 45 { 46 return rangeLength_; 47 } 48 Offset()49size_t AVTransDataBuffer::Offset() 50 { 51 return rangeOffset_; 52 } 53 Capacity()54size_t AVTransDataBuffer::Capacity() 55 { 56 return capacity_; 57 } 58 Data()59uint8_t* AVTransDataBuffer::Data() 60 { 61 if (data_ == nullptr) { 62 return nullptr; 63 } 64 return data_ + rangeOffset_; 65 } 66 SetRange(size_t offset,size_t size)67int32_t AVTransDataBuffer::SetRange(size_t offset, size_t size) 68 { 69 if (!(offset <= capacity_) || !(offset + size <= capacity_)) { 70 return -1; 71 } 72 73 rangeOffset_ = offset; 74 rangeLength_ = size; 75 return ERR_OK; 76 } 77 } // namespace DistributedCollab 78 } // namespace OHOS 79