Android-cuttlefish cvd tool
tuner_audio_source.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2026 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 <atomic>
20#include <cstddef>
21#include <cstdint>
22#include <string>
23#include <vector>
24
27
28namespace cuttlefish {
29
30// Feeds a dedicated virtio-snd capture stream from the virtual tuner daemon's
31// PCM socket. The socket is connected once at construction; if that fails or
32// the daemon goes away, the source emits silence from then on, so the guest
33// always sees a continuous stream at the correct rate.
34//
35// The daemon produces 48 kHz, 16-bit PCM with `channels` interleaved channels,
36// which must match the channel layout of the tuner stream in the guest audio
37// config. The guest negotiates the stream format like for any other stream; if
38// it picks a different one, the source logs a warning and emits silence.
39//
40// GetMoreAudioData() calls must not overlap, though they may come from
41// different threads over time. Reset() is safe to call from any thread.
43 public:
44 TunerAudioSource(const std::string& pcm_socket_path, uint8_t channels);
45 ~TunerAudioSource() override = default;
46
49
50 int GetMoreAudioData(void* data, int bytes_per_sample,
51 int samples_per_channel, int num_channels,
52 int sample_rate, bool& muted) override;
53
54 // Only sets a flag; the next GetMoreAudioData() call drops everything
55 // received up to that point and re-primes.
56 void Reset() override;
57
58 private:
59 void DrainSocket();
60 void TrimFifo();
61 void LogStats() const;
62
63 const int channels_;
64 const size_t frame_bytes_;
65 const size_t prime_bytes_;
66 const size_t max_fifo_bytes_;
68 std::vector<uint8_t> fifo_;
69 bool primed_ = false;
70 std::atomic<bool> reset_requested_{false};
71 // Drift indicators: overflows mean the daemon runs faster than the guest
72 // consumes, underruns mean it runs slower (or stopped sending).
73 uint64_t overflow_count_ = 0;
74 uint64_t underrun_count_ = 0;
75};
76
77} // namespace cuttlefish
Definition: shared_fd.h:125
Definition: tuner_audio_source.h:42
std::atomic< bool > reset_requested_
Definition: tuner_audio_source.h:70
int GetMoreAudioData(void *data, int bytes_per_sample, int samples_per_channel, int num_channels, int sample_rate, bool &muted) override
Definition: tuner_audio_source.cpp:71
TunerAudioSource & operator=(const TunerAudioSource &)=delete
void LogStats() const
Definition: tuner_audio_source.cpp:170
const size_t prime_bytes_
Definition: tuner_audio_source.h:65
uint64_t underrun_count_
Definition: tuner_audio_source.h:74
void TrimFifo()
Definition: tuner_audio_source.cpp:157
TunerAudioSource(const TunerAudioSource &)=delete
void Reset() override
Definition: tuner_audio_source.cpp:127
SharedFD fd_
Definition: tuner_audio_source.h:67
uint64_t overflow_count_
Definition: tuner_audio_source.h:73
std::vector< uint8_t > fifo_
Definition: tuner_audio_source.h:68
const size_t max_fifo_bytes_
Definition: tuner_audio_source.h:66
~TunerAudioSource() override=default
const int channels_
Definition: tuner_audio_source.h:63
TunerAudioSource(const std::string &pcm_socket_path, uint8_t channels)
Definition: tuner_audio_source.cpp:54
void DrainSocket()
Definition: tuner_audio_source.cpp:129
const size_t frame_bytes_
Definition: tuner_audio_source.h:64
bool primed_
Definition: tuner_audio_source.h:69
Definition: audio_source.h:26
char data[Size]
Definition: incremental_server.cpp:1
Definition: alloc_driver.h:21