1 /*
2 * Copyright (C) 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 "audio_buffer_info.h"
17 #include "avcodec_errors.h"
18 #include "avcodec_log.h"
19 #include "securec.h"
20
21 namespace {
22 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_AUDIO, "AvCodec-AudioBufferInfo"};
23 constexpr uint8_t LOGD_FREQUENCY = 5;
24 } // namespace
25
26 namespace OHOS {
27 namespace MediaAVCodec {
28 using namespace OHOS::Media;
AudioBufferInfo(const uint32_t bufferSize,const std::string_view & name,const uint32_t metaSize)29 AudioBufferInfo::AudioBufferInfo(const uint32_t bufferSize, const std::string_view &name, const uint32_t metaSize)
30 : isHasMeta_(false),
31 isEos_(false),
32 isFirstFrame_(false),
33 isUsing(false),
34 status_(BufferStatus::IDLE),
35 bufferSize_(bufferSize),
36 metaSize_(metaSize),
37 name_(name),
38 buffer_(nullptr),
39 metadata_(nullptr),
40 flag_(AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_NONE)
41 {
42 if (metaSize_ > 0) {
43 metadata_ =
44 std::make_shared<AVSharedMemoryBase>(metaSize_, AVSharedMemory::Flags::FLAGS_READ_ONLY, std::string(name_));
45 int32_t ret = metadata_->Init();
46 if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
47 AVCODEC_LOGE("Create metadata avsharedmemory failed, ret = %{public}d", ret);
48 }
49 isHasMeta_ = true;
50 }
51
52 buffer_ =
53 std::make_shared<AVSharedMemoryBase>(bufferSize_, AVSharedMemory::Flags::FLAGS_READ_WRITE, std::string(name_));
54 int32_t ret = buffer_->Init();
55 if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
56 AVCODEC_LOGE("Create buffer avsharedmemory failed, ret = %{public}d", ret);
57 }
58 AVCODEC_LOGD_LIMIT(LOGD_FREQUENCY, "AudioBufferInfo constructor %{public}s buffer.", name_.data());
59 }
60
~AudioBufferInfo()61 AudioBufferInfo::~AudioBufferInfo()
62 {
63 AVCODEC_LOGD_LIMIT(LOGD_FREQUENCY, "AudioBufferInfo destructor %{public}s buffer.", name_.data());
64 isEos_ = false;
65 status_ = BufferStatus::IDLE;
66
67 if (buffer_) {
68 buffer_.reset();
69 buffer_ = nullptr;
70 }
71
72 if (metadata_) {
73 metadata_.reset();
74 metadata_ = nullptr;
75 }
76 }
77
GetBuffer() const78 std::shared_ptr<AVSharedMemoryBase> AudioBufferInfo::GetBuffer() const noexcept
79 {
80 return buffer_;
81 }
82
GetStatus() const83 BufferStatus AudioBufferInfo::GetStatus() const noexcept
84 {
85 return status_;
86 }
87
IsAvailable() const88 bool AudioBufferInfo::IsAvailable() const noexcept
89 {
90 return status_ == BufferStatus::IDLE;
91 }
92
CheckIsEos() const93 bool AudioBufferInfo::CheckIsEos() const noexcept
94 {
95 return isEos_;
96 }
97
SetBufferOwned()98 bool AudioBufferInfo::SetBufferOwned()
99 {
100 status_ = BufferStatus::OWEN_BY_CLIENT;
101 return true;
102 }
103
SetEos(bool eos)104 void AudioBufferInfo::SetEos(bool eos)
105 {
106 isEos_ = eos;
107 if (isEos_) {
108 flag_ = AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS;
109 } else {
110 flag_ = AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_NONE;
111 }
112 }
113
SetBufferAttr(const AVCodecBufferInfo & attr)114 void AudioBufferInfo::SetBufferAttr(const AVCodecBufferInfo &attr)
115 {
116 info_ = attr;
117 }
118
GetBufferAttr() const119 AVCodecBufferInfo AudioBufferInfo::GetBufferAttr() const noexcept
120 {
121 return info_;
122 }
123
GetFlag() const124 AVCodecBufferFlag AudioBufferInfo::GetFlag() const noexcept
125 {
126 return flag_;
127 }
128
CheckIsFirstFrame() const129 bool AudioBufferInfo::CheckIsFirstFrame() const noexcept
130 {
131 return isFirstFrame_;
132 }
133
SetFirstFrame()134 void AudioBufferInfo::SetFirstFrame() noexcept
135 {
136 isFirstFrame_ = true;
137 }
138
CheckIsUsing() const139 bool AudioBufferInfo::CheckIsUsing() const noexcept
140 {
141 return isUsing;
142 }
143
SetUsing()144 void AudioBufferInfo::SetUsing() noexcept
145 {
146 isUsing = true;
147 }
148
GetMetadata() const149 std::shared_ptr<AVSharedMemoryBase> AudioBufferInfo::GetMetadata() const noexcept
150 {
151 return metadata_;
152 }
153
IsHasMetaData() const154 bool AudioBufferInfo::IsHasMetaData() const noexcept
155 {
156 return isHasMeta_;
157 }
158
GetBufferSize() const159 uint32_t AudioBufferInfo::GetBufferSize() const noexcept
160 {
161 return bufferSize_;
162 }
163
ResetBuffer()164 bool AudioBufferInfo::ResetBuffer()
165 {
166 isEos_ = false;
167 isFirstFrame_ = false;
168 isUsing = false;
169 status_ = BufferStatus::IDLE;
170 flag_ = AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_NONE;
171 if (buffer_) {
172 buffer_->ClearUsedSize();
173 } else {
174 return false;
175 }
176 return true;
177 }
178 } // namespace MediaAVCodec
179 } // namespace OHOS