• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "video_pool_manager.h"
17 #include "media_dfx.h"
18 #include "media_log.h"
19 #include "media_errors.h"
20 
21 namespace {
22     constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN, "VideoPoolManager"};
23 }
24 namespace OHOS {
25 namespace Media {
~VideoPoolManager()26 VideoPoolManager::~VideoPoolManager()
27 {
28     FreeBufferQue();
29 }
30 
IsBufferQueFull()31 bool VideoPoolManager::IsBufferQueFull()
32 {
33     std::unique_lock<std::mutex> lock(mutex_);
34     if (bufferQue_.size() >= maxQueSize_) {
35         return true;
36     }
37 
38     return false;
39 }
40 
IsBufferQueEmpty()41 bool VideoPoolManager::IsBufferQueEmpty()
42 {
43     std::unique_lock<std::mutex> lock(mutex_);
44     return bufferQue_.empty();
45 }
46 
GetBufferQueSize()47 uint32_t VideoPoolManager::GetBufferQueSize()
48 {
49     std::unique_lock<std::mutex> lock(mutex_);
50     return bufferQue_.size();
51 }
52 
PushBuffer(GstBuffer * buf)53 int32_t VideoPoolManager::PushBuffer(GstBuffer *buf)
54 {
55     std::unique_lock<std::mutex> lock(mutex_);
56     CHECK_AND_RETURN_RET_LOG(bufferQue_.size() < maxQueSize_, MSERR_NO_MEMORY,
57         "The queue length has reached the maximum.");
58 
59     bufferQue_.push(buf);
60     MEDIA_LOGI("PushBuffer end, size: %{public}zu", bufferQue_.size());
61     return MSERR_OK;
62 }
63 
PopBuffer()64 GstBuffer *VideoPoolManager::PopBuffer()
65 {
66     std::unique_lock<std::mutex> lock(mutex_);
67     CHECK_AND_RETURN_RET_LOG(!bufferQue_.empty(), nullptr, "The queue is empty.");
68 
69     GstBuffer *buf = bufferQue_.front();
70     bufferQue_.pop();
71     MEDIA_LOGI("PopBuffer end, size: %{public}zu", bufferQue_.size());
72     return buf;
73 }
74 
FreeBufferQue()75 void VideoPoolManager::FreeBufferQue()
76 {
77     std::unique_lock<std::mutex> lock(mutex_);
78     MEDIA_LOGI("FreeBufferQue size: %{public}zu", bufferQue_.size());
79     while (bufferQue_.size() > 0) {
80         GstBuffer *buf = bufferQue_.front();
81         bufferQue_.pop();
82         if (buf) {
83             gst_buffer_unref(buf);
84         }
85     }
86 }
87 } // namespace Media
88 } // namespace OHOS