1 /*
2 * Copyright (c) 2021-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 "avbuffer_queue_consumer_impl.h"
17 #include "avbuffer_queue_impl.h"
18 #include "avbuffer_queue_producer_impl.h"
19 #include "common/log.h"
20 #include "meta/media_types.h"
21
22 namespace {
23 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_DOMAIN_FOUNDATION, "AVBufferQueue" };
24 }
25
26 namespace OHOS {
27 namespace Media {
28
Create( uint32_t size, MemoryType type, const std::string& name, bool disableAlloc)29 std::shared_ptr<AVBufferQueue> AVBufferQueue::Create(
30 uint32_t size, MemoryType type, const std::string& name, bool disableAlloc)
31 {
32 MEDIA_LOG_D("AVBufferQueue::Create size = %u, type = %u, name = %s",
33 size, static_cast<uint32_t>(type), name.c_str());
34 return std::make_shared<AVBufferQueueImpl>(size, type, name, disableAlloc);
35 }
36
GetLocalProducer()37 std::shared_ptr<AVBufferQueueProducer> AVBufferQueueImpl::GetLocalProducer()
38 {
39 std::lock_guard<std::mutex> lockGuard(producerCreatorMutex_);
40 std::shared_ptr<AVBufferQueueProducerImpl> producer = nullptr;
41 if (localProducer_.expired()) {
42 auto shared_this = shared_from_this();
43 FALSE_RETURN_V(shared_this != nullptr, nullptr);
44 producer = std::make_shared<AVBufferQueueProducerImpl>(shared_this);
45 localProducer_ = producer;
46 }
47
48 return localProducer_.lock();
49 }
50
GetLocalConsumer()51 std::shared_ptr<AVBufferQueueConsumer> AVBufferQueueImpl::GetLocalConsumer()
52 {
53 std::lock_guard<std::mutex> lockGuard(consumerCreatorMutex_);
54 std::shared_ptr<AVBufferQueueConsumerImpl> consumer = nullptr;
55 if (localConsumer_.expired()) {
56 auto shared_this = shared_from_this();
57 FALSE_RETURN_V(shared_this != nullptr, nullptr);
58 consumer = std::make_shared<AVBufferQueueConsumerImpl>(shared_this);
59 localConsumer_ = consumer;
60 }
61 return localConsumer_.lock();
62 }
63
GetProducer()64 sptr<AVBufferQueueProducer> AVBufferQueueImpl::GetProducer()
65 {
66 std::lock_guard<std::mutex> lockGuard(producerCreatorMutex_);
67 sptr<AVBufferQueueProducerImpl> producer = nullptr;
68 if (producer_ == nullptr || producer_->GetSptrRefCount() <= 0) {
69 auto shared_this = shared_from_this();
70 FALSE_RETURN_V(shared_this != nullptr, nullptr);
71 producer = new AVBufferQueueProducerImpl(shared_this);
72 producer_ = producer;
73 }
74
75 return producer_.promote();
76 }
77
GetConsumer()78 sptr<AVBufferQueueConsumer> AVBufferQueueImpl::GetConsumer()
79 {
80 std::lock_guard<std::mutex> lockGuard(consumerCreatorMutex_);
81 sptr<AVBufferQueueConsumerImpl> consumer = nullptr;
82 if (consumer_ == nullptr || consumer_->GetSptrRefCount() <= 0) {
83 auto shared_this = shared_from_this();
84 FALSE_RETURN_V(shared_this != nullptr, nullptr);
85 consumer = new AVBufferQueueConsumerImpl(shared_this);
86 consumer_ = consumer;
87 }
88
89 return consumer_.promote();
90 }
91
AVBufferQueueImpl(const std::string &name)92 AVBufferQueueImpl::AVBufferQueueImpl(const std::string &name)
93 : AVBufferQueue(), name_(name), size_(0), memoryType_(MemoryType::UNKNOWN_MEMORY), disableAlloc_(false) {}
94
AVBufferQueueImpl(uint32_t size, MemoryType type, const std::string &name, bool disableAlloc)95 AVBufferQueueImpl::AVBufferQueueImpl(uint32_t size, MemoryType type, const std::string &name, bool disableAlloc)
96 : AVBufferQueue(), name_(name), size_(size), memoryType_(type), disableAlloc_(disableAlloc)
97 {
98 if (size_ > AVBUFFER_QUEUE_MAX_QUEUE_SIZE) {
99 size_ = AVBUFFER_QUEUE_MAX_QUEUE_SIZE;
100 }
101 }
102
GetQueueSize()103 uint32_t AVBufferQueueImpl::GetQueueSize()
104 {
105 return size_;
106 }
107
SetQueueSize(uint32_t size)108 Status AVBufferQueueImpl::SetQueueSize(uint32_t size)
109 {
110 FALSE_RETURN_V(size >= 0 && size <= AVBUFFER_QUEUE_MAX_QUEUE_SIZE && size != size_,
111 Status::ERROR_INVALID_BUFFER_SIZE);
112
113 if (size > size_) {
114 size_ = size;
115 if (!disableAlloc_) {
116 requestCondition.notify_all();
117 }
118 } else {
119 std::lock_guard<std::mutex> lockGuard(queueMutex_);
120 DeleteBuffers(size_ - size);
121 size_ = size;
122 }
123
124 return Status::OK;
125 }
126
IsBufferInQueue(const std::shared_ptr<AVBuffer>& buffer)127 bool AVBufferQueueImpl::IsBufferInQueue(const std::shared_ptr<AVBuffer>& buffer)
128 {
129 FALSE_RETURN_V(buffer != nullptr, false);
130 auto uniqueId = buffer->GetUniqueId();
131 return cachedBufferMap_.find(uniqueId) != cachedBufferMap_.end();
132 }
133
GetCachedBufferCount() const134 uint32_t AVBufferQueueImpl::GetCachedBufferCount() const
135 {
136 // 确保cachedBufferMap_.size()不会超过MAX_UINT32
137 return static_cast<uint32_t>(cachedBufferMap_.size());
138 }
139
PopFromFreeBufferList(std::shared_ptr<AVBuffer>& buffer, const AVBufferConfig& config)140 Status AVBufferQueueImpl::PopFromFreeBufferList(std::shared_ptr<AVBuffer>& buffer, const AVBufferConfig& config)
141 {
142 for (auto it = freeBufferList_.begin(); it != freeBufferList_.end(); it++) {
143 if (config <= cachedBufferMap_[*it].config) {
144 buffer = cachedBufferMap_[*it].buffer;
145 freeBufferList_.erase(it);
146 return Status::OK;
147 }
148 }
149
150 if (freeBufferList_.empty()) {
151 buffer = nullptr;
152 // 没有可以重用的freeBuffer
153 return Status::ERROR_NO_FREE_BUFFER;
154 }
155
156 buffer = cachedBufferMap_[freeBufferList_.front()].buffer;
157 freeBufferList_.pop_front();
158
159 return Status::OK;
160 }
161
PopFromDirtyBufferList(std::shared_ptr<AVBuffer>& buffer)162 Status AVBufferQueueImpl::PopFromDirtyBufferList(std::shared_ptr<AVBuffer>& buffer)
163 {
164 FALSE_RETURN_V(!dirtyBufferList_.empty(), Status::ERROR_NO_DIRTY_BUFFER);
165
166 buffer = cachedBufferMap_[dirtyBufferList_.front()].buffer;
167 dirtyBufferList_.pop_front();
168 return Status::OK;
169 }
170
AllocBuffer(std::shared_ptr<AVBuffer>& buffer, const AVBufferConfig& config)171 Status AVBufferQueueImpl::AllocBuffer(std::shared_ptr<AVBuffer>& buffer, const AVBufferConfig& config)
172 {
173 auto bufferImpl = AVBuffer::CreateAVBuffer(config);
174 FALSE_RETURN_V(bufferImpl != nullptr, Status::ERROR_CREATE_BUFFER);
175
176 auto uniqueId = bufferImpl->GetUniqueId();
177 AVBufferElement ele = {
178 .config = bufferImpl->GetConfig(),
179 .state = AVBUFFER_STATE_RELEASED,
180 .isDeleting = false,
181 .buffer = bufferImpl,
182 };
183 cachedBufferMap_[uniqueId] = ele;
184 buffer = bufferImpl;
185
186 return Status::OK;
187 }
188
RequestReuseBuffer(std::shared_ptr<AVBuffer>& buffer, const AVBufferConfig& config)189 Status AVBufferQueueImpl::RequestReuseBuffer(std::shared_ptr<AVBuffer>& buffer, const AVBufferConfig& config)
190 {
191 FALSE_RETURN_V(buffer != nullptr, Status::ERROR_NULL_POINT_BUFFER);
192
193 auto uniqueId = buffer->GetUniqueId();
194 FALSE_RETURN_V(cachedBufferMap_.find(uniqueId) != cachedBufferMap_.end(), Status::ERROR_CREATE_BUFFER);
195
196 if (config <= cachedBufferMap_[uniqueId].config) {
197 // 不需要重新分配,直接更新buffer大小
198 cachedBufferMap_[uniqueId].config.size = config.size;
199 } else {
200 // 重新分配
201 DeleteCachedBufferById(uniqueId);
202 NOK_RETURN(AllocBuffer(buffer, config));
203 }
204
205 // 注意这里的uniqueId可能因为重新分配buffer而更新,所以需要再次获取
206 cachedBufferMap_[buffer->GetUniqueId()].state = AVBUFFER_STATE_REQUESTED;
207 return Status::OK;
208 }
209
DeleteBuffers(uint32_t count)210 void AVBufferQueueImpl::DeleteBuffers(uint32_t count)
211 {
212 FALSE_RETURN(count > 0);
213
214 while (!freeBufferList_.empty()) {
215 DeleteCachedBufferById(freeBufferList_.front());
216 freeBufferList_.pop_front();
217 count--;
218 if (count <= 0) {
219 return;
220 }
221 }
222
223 while (!dirtyBufferList_.empty()) {
224 DeleteCachedBufferById(dirtyBufferList_.front());
225 dirtyBufferList_.pop_front();
226 count--;
227 if (count <= 0) {
228 return;
229 }
230 }
231
232 for (auto&& ele : cachedBufferMap_) {
233 ele.second.isDeleting = true;
234 // we don't have to do anything
235 count--;
236 if (count <= 0) {
237 break;
238 }
239 }
240 }
241
DeleteCachedBufferById(uint64_t uniqueId)242 void AVBufferQueueImpl::DeleteCachedBufferById(uint64_t uniqueId)
243 {
244 auto it = cachedBufferMap_.find(uniqueId);
245 if (it != cachedBufferMap_.end()) {
246 cachedBufferMap_.erase(it);
247 }
248 }
249
CheckConfig(const AVBufferConfig& config)250 Status AVBufferQueueImpl::CheckConfig(const AVBufferConfig& config)
251 {
252 if (config.memoryType == MemoryType::UNKNOWN_MEMORY) {
253 MEDIA_LOG_D("config.memoryType != MemoryType::UNKNOWN_MEMORY");
254 return Status::ERROR_UNEXPECTED_MEMORY_TYPE;
255 }
256 // memoryType_初始化之后将无法改变。
257 if (memoryType_ != MemoryType::UNKNOWN_MEMORY && config.memoryType != memoryType_) {
258 MEDIA_LOG_D("memoryType_ != MemoryType::UNKNOWN_MEMORY && config.memoryType != memoryType_");
259 return Status::ERROR_UNEXPECTED_MEMORY_TYPE;
260 }
261 memoryType_ = config.memoryType;
262 return Status::OK;
263 }
264
wait_for(std::unique_lock<std::mutex>& lock, int32_t timeoutMs)265 bool AVBufferQueueImpl::wait_for(std::unique_lock<std::mutex>& lock, int32_t timeoutMs)
266 {
267 MEDIA_LOG_D("wait for free buffer, timeout = %d", timeoutMs);
268 if (timeoutMs > 0) {
269 return requestCondition.wait_for(
270 lock, std::chrono::milliseconds(timeoutMs), [this]() {
271 return !freeBufferList_.empty() || (GetCachedBufferCount() < GetQueueSize());
272 });
273 } else if (timeoutMs < 0) {
274 requestCondition.wait(lock);
275 }
276 return true;
277 }
278
RequestBuffer( std::shared_ptr<AVBuffer>& buffer, const AVBufferConfig& config, int32_t timeoutMs)279 Status AVBufferQueueImpl::RequestBuffer(
280 std::shared_ptr<AVBuffer>& buffer, const AVBufferConfig& config, int32_t timeoutMs)
281 {
282 auto configCopy = config;
283 if (config.memoryType == MemoryType::UNKNOWN_MEMORY) {
284 MEDIA_LOG_D("AVBufferQueueImpl::RequestBuffer config.memoryType unknown, "
285 "memoryType_ = %u", static_cast<uint32_t>(memoryType_));
286 configCopy.memoryType = memoryType_;
287 }
288
289 // check param
290 std::unique_lock<std::mutex> lock(queueMutex_);
291 auto res = CheckConfig(configCopy);
292 FALSE_RETURN_V_MSG(res == Status::OK,
293 res, "CheckConfig not OK, code %{public}d", static_cast<int32_t>(res));
294 // dequeue from free list
295 auto ret = PopFromFreeBufferList(buffer, configCopy);
296 if (ret == Status::OK) {
297 return RequestReuseBuffer(buffer, configCopy);
298 }
299
300 // check queue size
301 if (GetCachedBufferCount() >= GetQueueSize()) {
302 if (!wait_for(lock, timeoutMs)) {
303 MEDIA_LOG_D("FALSE_RETURN_V wait_for(lock, timeoutMs)");
304 return Status::ERROR_WAIT_TIMEOUT;
305 }
306
307 // 被条件唤醒后,再次尝试从freeBufferList中取buffer
308 ret = PopFromFreeBufferList(buffer, configCopy);
309 if (ret == Status::OK) {
310 return RequestReuseBuffer(buffer, configCopy);
311 }
312 if (GetCachedBufferCount() >= GetQueueSize()) return Status::ERROR_NO_FREE_BUFFER;
313 }
314
315 NOK_RETURN(AllocBuffer(buffer, configCopy));
316 cachedBufferMap_[buffer->GetUniqueId()].state = AVBUFFER_STATE_REQUESTED;
317
318 return Status::OK;
319 }
320
InsertFreeBufferInOrder(uint64_t uniqueId)321 void AVBufferQueueImpl::InsertFreeBufferInOrder(uint64_t uniqueId)
322 {
323 for (auto it = freeBufferList_.begin(); it != freeBufferList_.end(); it++) {
324 if ((*it != uniqueId) &&
325 (cachedBufferMap_[*it].config.capacity >= cachedBufferMap_[uniqueId].config.capacity)) {
326 freeBufferList_.insert(it, uniqueId);
327 return;
328 }
329 }
330 freeBufferList_.emplace_back(uniqueId);
331 }
332
CancelBuffer(uint64_t uniqueId)333 Status AVBufferQueueImpl::CancelBuffer(uint64_t uniqueId)
334 {
335 FALSE_RETURN_V(cachedBufferMap_.find(uniqueId) != cachedBufferMap_.end(), Status::ERROR_INVALID_BUFFER_ID);
336
337 FALSE_RETURN_V(cachedBufferMap_[uniqueId].state == AVBUFFER_STATE_REQUESTED ||
338 cachedBufferMap_[uniqueId].state == AVBUFFER_STATE_PUSHED,
339 Status::ERROR_INVALID_BUFFER_STATE);
340
341 InsertFreeBufferInOrder(uniqueId);
342
343 cachedBufferMap_[uniqueId].state = AVBUFFER_STATE_RELEASED;
344
345 requestCondition.notify_all();
346
347 MEDIA_LOG_D("cancel buffer id = %llu", uniqueId);
348
349 return Status::OK;
350 }
351
PushBuffer(uint64_t uniqueId, bool available)352 Status AVBufferQueueImpl::PushBuffer(uint64_t uniqueId, bool available)
353 {
354 std::shared_ptr<AVBuffer> buffer = nullptr;
355 {
356 std::lock_guard<std::mutex> lockGuard(queueMutex_);
357 FALSE_RETURN_V(cachedBufferMap_.find(uniqueId) != cachedBufferMap_.end(),
358 Status::ERROR_INVALID_BUFFER_ID);
359
360 auto& ele = cachedBufferMap_[uniqueId];
361 if (ele.isDeleting) {
362 DeleteCachedBufferById(uniqueId);
363 MEDIA_LOG_D("delete push buffer uniqueId(%llu)", uniqueId);
364 return Status::OK;
365 }
366
367 if (available) {
368 FALSE_RETURN_V(ele.buffer->GetConfig().size >= 0, Status::ERROR_INVALID_BUFFER_SIZE);
369 }
370
371 FALSE_RETURN_V(ele.state == AVBUFFER_STATE_REQUESTED || ele.state == AVBUFFER_STATE_ATTACHED,
372 Status::ERROR_INVALID_BUFFER_STATE);
373
374 ele.state = AVBUFFER_STATE_PUSHED;
375 buffer = cachedBufferMap_[uniqueId].buffer;
376 }
377
378 if (available) {
379 std::lock_guard<std::mutex> lockGuard(brokerListenerMutex_);
380 if (!brokerListeners_.empty() && brokerListeners_.back() != nullptr) {
381 brokerListeners_.back()->OnBufferFilled(buffer);
382 return Status::OK;
383 }
384 }
385
386 return ReturnBuffer(uniqueId, available);
387 }
388
PushBuffer(const std::shared_ptr<AVBuffer>& buffer, bool available)389 Status AVBufferQueueImpl::PushBuffer(const std::shared_ptr<AVBuffer>& buffer, bool available)
390 {
391 FALSE_RETURN_V(buffer != nullptr, Status::ERROR_NULL_POINT_BUFFER);
392
393 return PushBuffer(buffer->GetUniqueId(), available);
394 }
395
ReturnBuffer(uint64_t uniqueId, bool available)396 Status __attribute__((no_sanitize("cfi"))) AVBufferQueueImpl::ReturnBuffer(uint64_t uniqueId, bool available)
397 {
398 {
399 std::lock_guard<std::mutex> lockGuard(queueMutex_);
400 FALSE_RETURN_V(cachedBufferMap_.find(uniqueId) != cachedBufferMap_.end(),
401 Status::ERROR_INVALID_BUFFER_ID);
402
403 if (cachedBufferMap_[uniqueId].isDeleting) {
404 DeleteCachedBufferById(uniqueId);
405 MEDIA_LOG_D("delete return buffer uniqueId(%llu)", uniqueId);
406 return Status::OK;
407 }
408
409 FALSE_RETURN_V(cachedBufferMap_[uniqueId].state == AVBUFFER_STATE_PUSHED,
410 Status::ERROR_INVALID_BUFFER_STATE);
411
412 if (!available) {
413 NOK_RETURN(CancelBuffer(uniqueId));
414 } else {
415 auto& config = cachedBufferMap_[uniqueId].buffer->GetConfig();
416 bool isEosBuffer = cachedBufferMap_[uniqueId].buffer->flag_ & (uint32_t)(Plugins::AVBufferFlag::EOS);
417 if (!isEosBuffer) {
418 FALSE_RETURN_V(config.size > 0, Status::ERROR_INVALID_BUFFER_SIZE);
419 }
420 cachedBufferMap_[uniqueId].config = config;
421 cachedBufferMap_[uniqueId].state = AVBUFFER_STATE_RETURNED;
422 dirtyBufferList_.push_back(uniqueId);
423 }
424 }
425
426 if (!available) {
427 std::lock_guard<std::mutex> lockGuard(producerListenerMutex_);
428 if (producerListener_ != nullptr) {
429 producerListener_->OnBufferAvailable();
430 }
431 return Status::OK;
432 }
433
434 std::lock_guard<std::mutex> lockGuard(consumerListenerMutex_);
435 FALSE_RETURN_V(consumerListener_ != nullptr, Status::ERROR_NO_CONSUMER_LISTENER);
436 consumerListener_->OnBufferAvailable();
437
438 return Status::OK;
439 }
440
ReturnBuffer(const std::shared_ptr<AVBuffer>& buffer, bool available)441 Status AVBufferQueueImpl::ReturnBuffer(const std::shared_ptr<AVBuffer>& buffer, bool available)
442 {
443 FALSE_RETURN_V(buffer != nullptr, Status::ERROR_NULL_POINT_BUFFER);
444
445 return ReturnBuffer(buffer->GetUniqueId(), available);
446 }
447
SetQueueSizeAndAttachBuffer(uint32_t size, std::shared_ptr<AVBuffer>& buffer, bool isFilled)448 Status AVBufferQueueImpl::SetQueueSizeAndAttachBuffer(uint32_t size,
449 std::shared_ptr<AVBuffer>& buffer, bool isFilled)
450 {
451 FALSE_RETURN_V(buffer != nullptr, Status::ERROR_NULL_POINT_BUFFER);
452 auto config = buffer->GetConfig();
453 auto uniqueId = buffer->GetUniqueId();
454 {
455 std::lock_guard<std::mutex> lockGuard(queueMutex_);
456 if (size >= 0 && size <= AVBUFFER_QUEUE_MAX_QUEUE_SIZE && size != size_) {
457 SetQueueSizeBeforeAttachBufferLocked(size);
458 }
459 FALSE_RETURN_V(cachedBufferMap_.find(uniqueId) == cachedBufferMap_.end(),
460 Status::ERROR_INVALID_BUFFER_ID);
461 NOK_RETURN(CheckConfig(config));
462 Status result = AttachAvailableBufferLocked(buffer);
463 FALSE_RETURN_V(result == Status::OK, result);
464 }
465 if (isFilled) {
466 return PushBufferOnFilled(uniqueId, isFilled);
467 }
468 return ReleaseBuffer(uniqueId);
469 }
470
AttachAvailableBufferLocked(std::shared_ptr<AVBuffer>& buffer)471 Status AVBufferQueueImpl::AttachAvailableBufferLocked(std::shared_ptr<AVBuffer>& buffer)
472 {
473 auto config = buffer->GetConfig();
474 auto uniqueId = buffer->GetUniqueId();
475 AVBufferElement ele = {
476 .config = config,
477 .state = AVBUFFER_STATE_ATTACHED,
478 .isDeleting = false,
479 .buffer = buffer
480 };
481
482 auto cachedCount = GetCachedBufferCount();
483 auto queueSize = GetQueueSize();
484 if (cachedCount >= queueSize) {
485 auto validCount = static_cast<uint32_t>(dirtyBufferList_.size() + freeBufferList_.size());
486 auto toBeDeleteCount = cachedCount - queueSize;
487 // 这里表示有可以删除的buffer,或者
488 if (validCount > toBeDeleteCount) {
489 // 在什么场景下需要在此处删除buffer?
490 DeleteBuffers(toBeDeleteCount + 1); // 多删除一个,用于attach当前buffer
491 cachedBufferMap_[uniqueId] = ele;
492 MEDIA_LOG_D("uniqueId(%llu) attached with delete", uniqueId);
493 } else {
494 MEDIA_LOG_E("attach failed, out of range");
495 return Status::ERROR_OUT_OF_RANGE;
496 }
497 } else {
498 cachedBufferMap_[uniqueId] = ele;
499 MEDIA_LOG_D("uniqueId(%llu) attached without delete", uniqueId);
500 }
501 return Status::OK;
502 }
503
PushBufferOnFilled(uint64_t uniqueId, bool isFilled)504 Status AVBufferQueueImpl::PushBufferOnFilled(uint64_t uniqueId, bool isFilled)
505 {
506 auto ret = PushBuffer(uniqueId, isFilled);
507 if (ret != Status::OK) {
508 // PushBuffer失败,强制Detach
509 DetachBuffer(uniqueId, true);
510 }
511 return ret;
512 }
513
SetQueueSizeBeforeAttachBufferLocked(uint32_t size)514 void AVBufferQueueImpl::SetQueueSizeBeforeAttachBufferLocked(uint32_t size)
515 {
516 if (size > size_) {
517 size_ = size;
518 if (!disableAlloc_) {
519 requestCondition.notify_all();
520 }
521 } else {
522 DeleteBuffers(size_ - size);
523 size_ = size;
524 }
525 }
526
AttachBuffer(std::shared_ptr<AVBuffer>& buffer, bool isFilled)527 Status AVBufferQueueImpl::AttachBuffer(std::shared_ptr<AVBuffer>& buffer, bool isFilled)
528 {
529 FALSE_RETURN_V(buffer != nullptr, Status::ERROR_NULL_POINT_BUFFER);
530
531 auto config = buffer->GetConfig();
532 auto uniqueId = buffer->GetUniqueId();
533 {
534 std::lock_guard<std::mutex> lockGuard(queueMutex_);
535 FALSE_RETURN_V(cachedBufferMap_.find(uniqueId) == cachedBufferMap_.end(),
536 Status::ERROR_INVALID_BUFFER_ID);
537
538 NOK_RETURN(CheckConfig(config));
539
540 Status result = AttachAvailableBufferLocked(buffer);
541 FALSE_RETURN_V(result == Status::OK, result);
542 }
543
544 if (isFilled) {
545 return PushBufferOnFilled(uniqueId, isFilled);
546 }
547
548 return ReleaseBuffer(uniqueId);
549 }
550
DetachBuffer(uint64_t uniqueId, bool force)551 Status AVBufferQueueImpl::DetachBuffer(uint64_t uniqueId, bool force)
552 {
553 FALSE_RETURN_V(cachedBufferMap_.find(uniqueId) != cachedBufferMap_.end(), Status::ERROR_INVALID_BUFFER_ID);
554
555 const auto& ele = cachedBufferMap_[uniqueId];
556
557 if (!force) {
558 // 只有生产者或消费者在获取到buffer后才能detach
559 if (ele.state == AVBUFFER_STATE_REQUESTED) {
560 MEDIA_LOG_D("detach buffer(%llu) on state requested", uniqueId);
561 } else if (ele.state == AVBUFFER_STATE_ACQUIRED) {
562 MEDIA_LOG_D("detach buffer(%llu) on state acquired", uniqueId);
563 } else {
564 MEDIA_LOG_W("detach buffer(%llu) on state %d forbidden", uniqueId, ele.state);
565 return Status::ERROR_INVALID_BUFFER_STATE;
566 }
567 }
568
569 cachedBufferMap_.erase(uniqueId);
570
571 return Status::OK;
572 }
573
DetachBuffer(uint64_t uniqueId)574 Status AVBufferQueueImpl::DetachBuffer(uint64_t uniqueId)
575 {
576 std::lock_guard<std::mutex> lockGuard(queueMutex_);
577 return DetachBuffer(uniqueId, false);
578 }
579
DetachBuffer(const std::shared_ptr<AVBuffer>& buffer)580 Status AVBufferQueueImpl::DetachBuffer(const std::shared_ptr<AVBuffer>& buffer)
581 {
582 FALSE_RETURN_V(buffer != nullptr, Status::ERROR_NULL_POINT_BUFFER);
583
584 return DetachBuffer(buffer->GetUniqueId());
585 }
586
AcquireBuffer(std::shared_ptr<AVBuffer>& buffer)587 Status AVBufferQueueImpl::AcquireBuffer(std::shared_ptr<AVBuffer>& buffer)
588 {
589 std::lock_guard<std::mutex> lockGuard(queueMutex_);
590 auto ret = PopFromDirtyBufferList(buffer);
591 if (ret != Status::OK) {
592 MEDIA_LOG_D("acquire buffer failed");
593 return ret;
594 }
595
596 cachedBufferMap_[buffer->GetUniqueId()].state = AVBUFFER_STATE_ACQUIRED;
597
598 return Status::OK;
599 }
600
ReleaseBuffer(uint64_t uniqueId)601 Status AVBufferQueueImpl::ReleaseBuffer(uint64_t uniqueId)
602 {
603 {
604 std::lock_guard<std::mutex> lockGuard(queueMutex_);
605 FALSE_RETURN_V(cachedBufferMap_.find(uniqueId) != cachedBufferMap_.end(), Status::ERROR_INVALID_BUFFER_ID);
606
607 FALSE_RETURN_V(cachedBufferMap_[uniqueId].state == AVBUFFER_STATE_ACQUIRED ||
608 cachedBufferMap_[uniqueId].state == AVBUFFER_STATE_ATTACHED, Status::ERROR_INVALID_BUFFER_STATE);
609
610 cachedBufferMap_[uniqueId].state = AVBUFFER_STATE_RELEASED;
611 if (cachedBufferMap_[uniqueId].isDeleting) {
612 DeleteCachedBufferById(uniqueId);
613 return Status::OK;
614 }
615
616 InsertFreeBufferInOrder(uniqueId);
617
618 requestCondition.notify_all();
619 }
620
621 // 注意:此时通知生产者有buffer可用,但实际有可能已经被request wait的生产者获取
622 std::lock_guard<std::mutex> lockGuard(producerListenerMutex_);
623 if (producerListener_ != nullptr) {
624 producerListener_->OnBufferAvailable();
625 }
626
627 return Status::OK;
628 }
629
ReleaseBuffer(const std::shared_ptr<AVBuffer>& buffer)630 Status AVBufferQueueImpl::ReleaseBuffer(const std::shared_ptr<AVBuffer>& buffer)
631 {
632 FALSE_RETURN_V(buffer != nullptr, Status::ERROR_NULL_POINT_BUFFER);
633
634 return ReleaseBuffer(buffer->GetUniqueId());
635 }
636
Clear()637 Status AVBufferQueueImpl::Clear()
638 {
639 MEDIA_LOG_D("AVBufferQueueImpl Clear");
640 std::lock_guard<std::mutex> lockGuard(queueMutex_);
641 dirtyBufferList_.clear();
642 for (auto it = cachedBufferMap_.begin(); it != cachedBufferMap_.end(); it++) {
643 if (it->second.state == AVBUFFER_STATE_PUSHED || it->second.state == AVBUFFER_STATE_RETURNED) {
644 it->second.state = AVBUFFER_STATE_RELEASED;
645 InsertFreeBufferInOrder(it->first);
646 }
647 }
648 requestCondition.notify_all();
649 return Status::OK;
650 }
651
SetBrokerListener(sptr<IBrokerListener>& listener)652 Status AVBufferQueueImpl::SetBrokerListener(sptr<IBrokerListener>& listener)
653 {
654 std::lock_guard<std::mutex> lockGuard(brokerListenerMutex_);
655 brokerListeners_.push_back(listener);
656 return Status::OK;
657 }
658
RemoveBrokerListener(sptr<IBrokerListener>& listener)659 Status AVBufferQueueImpl::RemoveBrokerListener(sptr<IBrokerListener>& listener)
660 {
661 std::lock_guard<std::mutex> lockGuard(brokerListenerMutex_);
662 if (!brokerListeners_.empty() && listener == brokerListeners_.back()) {
663 brokerListeners_.pop_back();
664 MEDIA_LOG_I("RemoveBrokerListener success, size: %{public}d", brokerListeners_.size());
665 } else {
666 MEDIA_LOG_E("removed item is not the back one.");
667 }
668 return Status::OK;
669 }
670
SetProducerListener(sptr<IProducerListener>& listener)671 Status AVBufferQueueImpl::SetProducerListener(sptr<IProducerListener>& listener)
672 {
673 std::lock_guard<std::mutex> lockGuard(producerListenerMutex_);
674 producerListener_ = listener;
675
676 return Status::OK;
677 }
678
SetConsumerListener(sptr<IConsumerListener>& listener)679 Status AVBufferQueueImpl::SetConsumerListener(sptr<IConsumerListener>& listener)
680 {
681 std::lock_guard<std::mutex> lockGuard(consumerListenerMutex_);
682 consumerListener_ = listener;
683
684 return Status::OK;
685 }
686
687 } // namespace Media
688 } // namespace OHOS
689