Android-cuttlefish cvd tool
screen_connector_queue.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#pragma once
18
19#include <condition_variable>
20#include <deque>
21#include <memory>
22#include <mutex>
23#include <thread>
24
27
28namespace cuttlefish {
29// move-based concurrent queue
30template <typename T>
32 public:
33 static_assert(is_movable<T>::value,
34 "Items in ScreenConnectorQueue should be std::mov-able");
35
36 ScreenConnectorQueue(const int q_max_size = 2)
37 : q_mutex_(std::make_unique<std::mutex>()), q_max_size_{q_max_size} {}
42
43 bool IsEmpty() const {
44 const std::lock_guard<std::mutex> lock(*q_mutex_);
45 return buffer_.empty();
46 }
47
48 auto Size() const {
49 const std::lock_guard<std::mutex> lock(*q_mutex_);
50 return buffer_.size();
51 }
52
53 void WaitEmpty() {
54 auto is_empty = [this](void) { return buffer_.empty(); };
55 std::unique_lock<std::mutex> lock(*q_mutex_);
56 q_empty_.wait(lock, is_empty);
57 }
58
59 /*
60 * Push( std::move(src) );
61 *
62 * Note: this queue is supposed to be used only by ScreenConnector-
63 * related components such as ScreenConnectorSource
64 *
65 * The traditional assumption was that when webRTC calls
66 * OnFrameAfter, the call should be block until it could return
67 * one frame.
68 *
69 * Thus, the producers of this queue must not produce frames
70 * much faster than the consumer, WebRTC consumes.
71 * Therefore, when the small buffer is full -- which means
72 * WebRTC would not call OnNextFrame --, the producer
73 * should stop adding items to the queue.
74 *
75 */
76 void Push(T&& item) {
77 std::unique_lock<std::mutex> lock(*q_mutex_);
78 if (Full()) {
79 auto is_empty = [this](void) { return buffer_.empty(); };
80 q_empty_.wait(lock, is_empty);
81 }
82 buffer_.push_back(std::move(item));
83 }
84 void Push(T& item) = delete;
85 void Push(const T& item) = delete;
86
87 T Pop() {
88 const std::lock_guard<std::mutex> lock(*q_mutex_);
89 auto item = std::move(buffer_.front());
90 buffer_.pop_front();
91 if (buffer_.empty()) {
92 q_empty_.notify_all();
93 }
94 return item;
95 }
96
97 private:
98 bool Full() const {
99 // call this in a critical section
100 // after acquiring q_mutex_
101 return q_max_size_ == buffer_.size();
102 }
103 std::deque<T> buffer_;
104 std::unique_ptr<std::mutex> q_mutex_;
105 std::condition_variable q_empty_;
106 const int q_max_size_;
107};
108
109} // namespace cuttlefish
Definition: screen_connector_queue.h:31
auto Size() const
Definition: screen_connector_queue.h:48
void Push(T &&item)
Definition: screen_connector_queue.h:76
void WaitEmpty()
Definition: screen_connector_queue.h:53
ScreenConnectorQueue & operator=(ScreenConnectorQueue &&cq)=delete
bool Full() const
Definition: screen_connector_queue.h:98
T Pop()
Definition: screen_connector_queue.h:87
bool IsEmpty() const
Definition: screen_connector_queue.h:43
std::deque< T > buffer_
Definition: screen_connector_queue.h:103
std::condition_variable q_empty_
Definition: screen_connector_queue.h:105
const int q_max_size_
Definition: screen_connector_queue.h:106
std::unique_ptr< std::mutex > q_mutex_
Definition: screen_connector_queue.h:104
ScreenConnectorQueue(const int q_max_size=2)
Definition: screen_connector_queue.h:36
ScreenConnectorQueue & operator=(const ScreenConnectorQueue &cq)=delete
ScreenConnectorQueue(ScreenConnectorQueue &&cq)=delete
ScreenConnectorQueue(const ScreenConnectorQueue &cq)=delete
void Push(const T &item)=delete
Definition: alloc_utils.cpp:23
Definition: logging.h:464
Definition: screen_connector_common.h:27