1 /*
2  * Copyright (c) 2022-2024 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 "audio_data.h"
17 #include "daudio_errorcode.h"
18 
19 namespace OHOS {
20 namespace DistributedHardware {
AudioData(const size_t capacity)21 AudioData::AudioData(const size_t capacity)
22 {
23     if (capacity != 0 && capacity < CAPACITY_MAX_SIZE) {
24         data_ = new (std::nothrow) uint8_t[capacity] {0};
25         if (data_ != nullptr) {
26             capacity_ = capacity;
27             rangeLength_ = capacity;
28         }
29     }
30 }
31 
Capacity() const32 size_t AudioData::Capacity() const
33 {
34     return capacity_;
35 }
36 
Size() const37 size_t AudioData::Size() const
38 {
39     return rangeLength_;
40 }
41 
Data() const42 uint8_t *AudioData::Data() const
43 {
44     return data_ + rangeOffset_;
45 }
46 
SetRange(size_t offset,size_t size)47 int32_t AudioData::SetRange(size_t offset, size_t size)
48 {
49     if (!(offset <= capacity_) || !(offset + size <= capacity_)) {
50         return ERR_DH_AUDIO_BAD_VALUE;
51     }
52 
53     rangeOffset_ = offset;
54     rangeLength_ = size;
55     return DH_SUCCESS;
56 }
57 
SetInt64(const string name,int64_t value)58 void AudioData::SetInt64(const string name, int64_t value)
59 {
60     int64Map_[name] = value;
61 }
62 
FindInt32(const string & name,int32_t & value)63 bool AudioData::FindInt32(const string &name, int32_t &value)
64 {
65     if (int32Map_.count(name) != 0) {
66         value = int32Map_[name];
67         return true;
68     } else {
69         value = 0;
70         return false;
71     }
72 }
73 
FindInt64(const string & name,int64_t & value)74 bool AudioData::FindInt64(const string &name, int64_t &value)
75 {
76     if (int64Map_.count(name) != 0) {
77         value = int64Map_[name];
78         return true;
79     } else {
80         value = 0;
81         return false;
82     }
83 }
84 
FindString(const string & name,string & value)85 bool AudioData::FindString(const string &name, string &value)
86 {
87     if (stringMap_.count(name) != 0) {
88         value = stringMap_[name];
89         return true;
90     } else {
91         value = "";
92         return false;
93     }
94 }
95 
~AudioData()96 AudioData::~AudioData()
97 {
98     if (data_ != nullptr) {
99         delete[] data_;
100         data_ = nullptr;
101     }
102 
103     capacity_ = 0;
104 }
105 } // namespace DistributedHardware
106 } // namespace OHOS
107