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, "AvCodec-AudioBufferInfo"};
23 constexpr uint8_t LOGD_FREQUENCY = 5;
24 } // namespace
25
26 namespace OHOS {
27 namespace MediaAVCodec {
AudioBufferInfo(const uint32_t bufferSize,const std::string_view & name,const uint32_t metaSize)28 AudioBufferInfo::AudioBufferInfo(const uint32_t bufferSize, const std::string_view &name, const uint32_t metaSize)
29 : isHasMeta_(false),
30 isEos_(false),
31 isFirstFrame_(false),
32 isUsing(false),
33 status_(BufferStatus::IDLE),
34 bufferSize_(bufferSize),
35 metaSize_(metaSize),
36 name_(name),
37 buffer_(nullptr),
38 metadata_(nullptr),
39 flag_(AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_NONE)
40 {
41 if (metaSize_ > 0) {
42 metadata_ =
43 std::make_shared<AVSharedMemoryBase>(metaSize_, AVSharedMemory::Flags::FLAGS_READ_ONLY, std::string(name_));
44 int32_t ret = metadata_->Init();
45 if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
46 AVCODEC_LOGE("Create metadata avsharedmemory failed, ret = %{public}d", ret);
47 }
48 isHasMeta_ = true;
49 }
50
51 buffer_ =
52 std::make_shared<AVSharedMemoryBase>(bufferSize_, AVSharedMemory::Flags::FLAGS_READ_WRITE, std::string(name_));
53 int32_t ret = buffer_->Init();
54 if (ret != AVCodecServiceErrCode::AVCS_ERR_OK) {
55 AVCODEC_LOGE("Create buffer avsharedmemory failed, ret = %{public}d", ret);
56 }
57 AVCODEC_LOGD_LIMIT(LOGD_FREQUENCY, "AudioBufferInfo constructor %{public}s buffer.", name_.data());
58 }
59
~AudioBufferInfo()60 AudioBufferInfo::~AudioBufferInfo()
61 {
62 AVCODEC_LOGD_LIMIT(LOGD_FREQUENCY, "AudioBufferInfo destructor %{public}s buffer.", name_.data());
63 isEos_ = false;
64 status_ = BufferStatus::IDLE;
65
66 if (buffer_) {
67 buffer_.reset();
68 buffer_ = nullptr;
69 }
70
71 if (metadata_) {
72 metadata_.reset();
73 metadata_ = nullptr;
74 }
75 }
76
GetBuffer() const77 std::shared_ptr<AVSharedMemoryBase> AudioBufferInfo::GetBuffer() const noexcept
78 {
79 return buffer_;
80 }
81
GetStatus() const82 BufferStatus AudioBufferInfo::GetStatus() const noexcept
83 {
84 return status_;
85 }
86
IsAvailable() const87 bool AudioBufferInfo::IsAvailable() const noexcept
88 {
89 return status_ == BufferStatus::IDLE;
90 }
91
CheckIsEos() const92 bool AudioBufferInfo::CheckIsEos() const noexcept
93 {
94 return isEos_;
95 }
96
SetBufferOwned()97 bool AudioBufferInfo::SetBufferOwned()
98 {
99 status_ = BufferStatus::OWEN_BY_CLIENT;
100 return true;
101 }
102
SetEos(bool eos)103 void AudioBufferInfo::SetEos(bool eos)
104 {
105 isEos_ = eos;
106 if (isEos_) {
107 flag_ = AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_EOS;
108 } else {
109 flag_ = AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_NONE;
110 }
111 }
112
SetBufferAttr(const AVCodecBufferInfo & attr)113 void AudioBufferInfo::SetBufferAttr(const AVCodecBufferInfo &attr)
114 {
115 info_ = attr;
116 }
117
GetBufferAttr() const118 AVCodecBufferInfo AudioBufferInfo::GetBufferAttr() const noexcept
119 {
120 return info_;
121 }
122
GetFlag() const123 AVCodecBufferFlag AudioBufferInfo::GetFlag() const noexcept
124 {
125 return flag_;
126 }
127
CheckIsFirstFrame() const128 bool AudioBufferInfo::CheckIsFirstFrame() const noexcept
129 {
130 return isFirstFrame_;
131 }
132
SetFirstFrame()133 void AudioBufferInfo::SetFirstFrame() noexcept
134 {
135 isFirstFrame_ = true;
136 }
137
CheckIsUsing() const138 bool AudioBufferInfo::CheckIsUsing() const noexcept
139 {
140 return isUsing;
141 }
142
SetUsing()143 void AudioBufferInfo::SetUsing() noexcept
144 {
145 isUsing = true;
146 }
147
GetMetadata() const148 std::shared_ptr<AVSharedMemoryBase> AudioBufferInfo::GetMetadata() const noexcept
149 {
150 return metadata_;
151 }
152
IsHasMetaData() const153 bool AudioBufferInfo::IsHasMetaData() const noexcept
154 {
155 return isHasMeta_;
156 }
157
ResetBuffer()158 bool AudioBufferInfo::ResetBuffer()
159 {
160 isEos_ = false;
161 isFirstFrame_ = false;
162 isUsing = false;
163 status_ = BufferStatus::IDLE;
164 flag_ = AVCodecBufferFlag::AVCODEC_BUFFER_FLAG_NONE;
165 if (buffer_) {
166 buffer_->ClearUsedSize();
167 } else {
168 return false;
169 }
170 return true;
171 }
172 } // namespace MediaAVCodec
173 } // namespace OHOS