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 #include "host/libs/audio_connector/buffers.h"
17
18 #include <android-base/logging.h>
19
20 namespace cuttlefish {
21
ShmBuffer(const virtio_snd_pcm_xfer & header,volatile uint8_t * buffer,uint32_t len,OnConsumedCb on_consumed)22 ShmBuffer::ShmBuffer(const virtio_snd_pcm_xfer& header,
23 volatile uint8_t* buffer, uint32_t len,
24 OnConsumedCb on_consumed)
25 : header_(header),
26 len_(len),
27 on_consumed_(on_consumed),
28 // Cast away the volatile qualifier: No one else will touch this buffer
29 // until SendStatus is called, at which point a memory fence will be used
30 // to ensure reads and writes are completed before the status is sent.
31 buffer_(const_cast<uint8_t*>(buffer)) {}
32
ShmBuffer(ShmBuffer && other)33 ShmBuffer::ShmBuffer(ShmBuffer&& other)
34 : header_(std::move(other.header_)),
35 len_(std::move(other.len_)),
36 on_consumed_(std::move(other.on_consumed_)),
37 status_sent_(other.status_sent_.load()),
38 buffer_(other.buffer_) {
39 // It's now this buffer's responsibility to send the status.
40 other.status_sent_ = true;
41 }
42
~ShmBuffer()43 ShmBuffer::~ShmBuffer() {
44 CHECK(status_sent_) << "Disposing of ShmBuffer before setting status";
45 }
46
stream_id() const47 uint32_t ShmBuffer::stream_id() const {
48 return header_.stream_id.as_uint32_t();
49 }
50
SendStatus(AudioStatus status,uint32_t latency_bytes,uint32_t consumed_len)51 void ShmBuffer::SendStatus(AudioStatus status, uint32_t latency_bytes,
52 uint32_t consumed_len) {
53 // Memory order is seq_cst to provide memory fence. It ensures all accesses
54 // are completed before the status is sent and the buffer is released.
55 CHECK(!status_sent_.exchange(true)) << "Status should only be sent once";
56 on_consumed_(status, latency_bytes, consumed_len);
57 }
58
59 } // namespace cuttlefish
60