Android-cuttlefish cvd tool
buffers.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#pragma once
16
17#include <atomic>
18#include <functional>
19
21
22namespace cuttlefish {
23
24enum class Status : uint32_t {
25 Ok = 0x8000,
28 IOError,
29};
30
31using OnConsumedCb = std::function<void(AudioStatus, uint32_t /*latency*/,
32 uint32_t /*consumed length*/)>;
33
34// Wraps and provides access to audio buffers sent by the client.
35// Objects of this class can only be moved, not copied. Destroying a buffer
36// without sending the status to the client is a bug so the program aborts in
37// those cases.
38// This class is NOT thread safe despite its use of atomic variables.
39class ShmBuffer {
40 public:
41 ShmBuffer(const virtio_snd_pcm_xfer& header, volatile uint8_t* buffer,
42 uint32_t len, OnConsumedCb on_consumed);
43 ShmBuffer(const ShmBuffer& other) = delete;
44 ShmBuffer(ShmBuffer&& other);
45 ShmBuffer& operator=(const ShmBuffer& other) = delete;
46
47 ~ShmBuffer();
48
49 uint32_t stream_id() const;
50 uint32_t len() const { return len_; }
51
52 void SendStatus(AudioStatus status, uint32_t latency_bytes,
53 uint32_t consumed_len);
54
55 const uint8_t* get() const { return buffer_; }
56
57 private:
59 const uint32_t len_;
61 std::atomic<bool> status_sent_ = false;
62
63 protected:
64 uint8_t* buffer_;
65};
66
68// Only RxBuffer can be written to
69class RxBuffer : public ShmBuffer {
70 public:
71 RxBuffer(const virtio_snd_pcm_xfer& header, volatile uint8_t* buffer,
72 uint32_t len, OnConsumedCb on_consumed)
73 : ShmBuffer(header, buffer, len, on_consumed) {}
74
75 uint8_t* get() { return buffer_; }
76};
77
78} // namespace cuttlefish
Definition: buffers.h:69
uint8_t * get()
Definition: buffers.h:75
RxBuffer(const virtio_snd_pcm_xfer &header, volatile uint8_t *buffer, uint32_t len, OnConsumedCb on_consumed)
Definition: buffers.h:71
Definition: buffers.h:39
ShmBuffer & operator=(const ShmBuffer &other)=delete
uint32_t len() const
Definition: buffers.h:50
ShmBuffer(const virtio_snd_pcm_xfer &header, volatile uint8_t *buffer, uint32_t len, OnConsumedCb on_consumed)
Definition: buffers.cpp:22
~ShmBuffer()
Definition: buffers.cpp:43
const virtio_snd_pcm_xfer header_
Definition: buffers.h:58
const uint8_t * get() const
Definition: buffers.h:55
OnConsumedCb on_consumed_
Definition: buffers.h:60
std::atomic< bool > status_sent_
Definition: buffers.h:61
void SendStatus(AudioStatus status, uint32_t latency_bytes, uint32_t consumed_len)
Definition: buffers.cpp:51
ShmBuffer(const ShmBuffer &other)=delete
uint8_t * buffer_
Definition: buffers.h:64
const uint32_t len_
Definition: buffers.h:59
uint32_t stream_id() const
Definition: buffers.cpp:47
int status()
Definition: health.cpp:42
ResponseHeader header
Definition: incremental_server.cpp:0
Definition: alloc_utils.cpp:23
std::function< void(AudioStatus, uint32_t, uint32_t)> OnConsumedCb
Definition: buffers.h:32
Status
Definition: buffers.h:24
AudioStatus
Definition: shm_layout.h:40
Definition: shm_layout.h:211