• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 "codec/audio/pcm_buffer_queue.h"
17 
18 namespace OHOS {
19 namespace Media {
20 
PcmBufferQueue(size_t capacity)21 PcmBufferQueue::PcmBufferQueue(size_t capacity) : capacity_(capacity) {}
22 
Enqueue(const std::shared_ptr<PcmData> & data)23 void PcmBufferQueue::Enqueue(const std::shared_ptr<PcmData>& data)
24 {
25     std::unique_lock<ffrt::mutex> lock(mutex_);
26     condFull_.wait(lock, [this]() { return queue_.size() < capacity_ || cancelEnqueueFlag_ == true; });
27     if (cancelEnqueueFlag_) {
28         return;
29     }
30     queue_.push(data);
31     condEmpty_.notify_one();
32 }
33 
Dequeue()34 std::shared_ptr<PcmData> PcmBufferQueue::Dequeue()
35 {
36     std::unique_lock<ffrt::mutex> lock(mutex_);
37     condEmpty_.wait(lock, [this]() { return !queue_.empty() || cancelDequeueFlag_ == true; });
38     if (cancelDequeueFlag_) {
39         return nullptr;
40     }
41     auto data = queue_.front();
42     queue_.pop();
43     condFull_.notify_one();
44     return data;
45 }
46 
CancelEnqueue()47 void PcmBufferQueue::CancelEnqueue()
48 {
49     std::unique_lock<ffrt::mutex> lock(mutex_);
50     cancelEnqueueFlag_ = true;
51     condFull_.notify_all();
52 }
53 
CancelDequeue()54 void PcmBufferQueue::CancelDequeue()
55 {
56     std::unique_lock<ffrt::mutex> lock(mutex_);
57     cancelDequeueFlag_ = true;
58     condEmpty_.notify_all();
59 }
60 
61 } // namespace Media
62 } // namespace OHOS