• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <cinttypes>
18 #include <functional>
19 
20 #include "host/libs/audio_connector/shm_layout.h"
21 
22 namespace cuttlefish {
23 
24 enum class Status : uint32_t {
25   Ok = 0x8000,
26   BadMessage,
27   NotSupported,
28   IOError,
29 };
30 
31 using 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 class ShmBuffer {
39  public:
ShmBuffer(const virtio_snd_pcm_xfer & header,uint32_t len,OnConsumedCb on_consumed)40   ShmBuffer(const virtio_snd_pcm_xfer& header, uint32_t len,
41             OnConsumedCb on_consumed)
42       : header_(header), len_(len), on_consumed_(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;
len()50   uint32_t len() const { return len_; }
51 
52   void SendStatus(AudioStatus status, uint32_t latency_bytes,
53                   uint32_t consumed_len);
54 
55  private:
56   const virtio_snd_pcm_xfer header_;
57   const uint32_t len_;
58   OnConsumedCb on_consumed_;
59   bool status_sent_ = false;
60 };
61 
62 class TxBuffer : public ShmBuffer {
63  public:
TxBuffer(const virtio_snd_pcm_xfer & header,const volatile uint8_t * buffer,uint32_t len,OnConsumedCb on_consumed)64   TxBuffer(const virtio_snd_pcm_xfer& header, const volatile uint8_t* buffer,
65            uint32_t len, OnConsumedCb on_consumed)
66       : ShmBuffer(header, len, on_consumed), buffer_(buffer) {}
67   TxBuffer(const TxBuffer& other) = delete;
68   TxBuffer(TxBuffer&& other) = default;
69   TxBuffer& operator=(const TxBuffer& other) = delete;
70 
get()71   const volatile uint8_t* get() const { return buffer_; }
72 
73  private:
74   const volatile uint8_t* const buffer_;
75 };
76 
77 class RxBuffer : public ShmBuffer {
78  public:
RxBuffer(const virtio_snd_pcm_xfer & header,volatile uint8_t * buffer,uint32_t len,OnConsumedCb on_consumed)79   RxBuffer(const virtio_snd_pcm_xfer& header, volatile uint8_t* buffer,
80            uint32_t len, OnConsumedCb on_consumed)
81       : ShmBuffer(header, len, on_consumed), buffer_(buffer) {}
82   RxBuffer(const RxBuffer& other) = delete;
83   RxBuffer(RxBuffer&& other) = default;
84   RxBuffer& operator=(const RxBuffer& other) = delete;
85 
get()86   volatile uint8_t* get() const { return buffer_; }
87 
88  private:
89   volatile uint8_t* const buffer_;
90 };
91 
92 }  // namespace cuttlefish
93