-
Notifications
You must be signed in to change notification settings - Fork 248
broadcastradio: Add Audio Generator and PCM Stream Server for Virtual Tuner #3175
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
abinba
wants to merge
1
commit into
google:main
Choose a base branch
from
abinba:feat/virtual-tuner-audio
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
base/cvd/cuttlefish/host/commands/virtual_tuner_daemon/audio_generator.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| * Copyright (C) 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "cuttlefish/host/commands/virtual_tuner_daemon/audio_generator.h" | ||
|
|
||
| #include <algorithm> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <random> | ||
| #include <span> | ||
|
|
||
| #include "cuttlefish/host/commands/virtual_tuner_daemon/tuner_state.h" | ||
|
|
||
| namespace cuttlefish { | ||
| namespace virtualtuner { | ||
| namespace { | ||
|
|
||
| constexpr int16_t kNoiseAmplitude = 4000; | ||
|
|
||
| } // namespace | ||
|
|
||
| AudioGenerator::AudioGenerator() : rng_(std::random_device()()) {} | ||
|
|
||
| void AudioGenerator::GenerateChunk(std::span<int16_t> samples, | ||
| const TunerStateSnapshot& snapshot) { | ||
| if (!snapshot.is_playing) { | ||
| std::fill(samples.begin(), samples.end(), 0); | ||
| return; | ||
| } | ||
|
|
||
| std::uniform_int_distribution<int> noise(-kNoiseAmplitude, kNoiseAmplitude); | ||
| for (size_t frame = 0; frame + kChannels <= samples.size(); | ||
| frame += kChannels) { | ||
| const auto sample = static_cast<int16_t>(noise(rng_)); | ||
| for (size_t channel = 0; channel < kChannels; ++channel) { | ||
| samples[frame + channel] = sample; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| } // namespace virtualtuner | ||
| } // namespace cuttlefish |
49 changes: 49 additions & 0 deletions
49
base/cvd/cuttlefish/host/commands/virtual_tuner_daemon/audio_generator.h
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| /* | ||
| * Copyright (C) 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <random> | ||
| #include <span> | ||
|
|
||
| #include "cuttlefish/host/commands/virtual_tuner_daemon/tuner_state.h" | ||
|
|
||
| namespace cuttlefish { | ||
| namespace virtualtuner { | ||
|
|
||
| inline constexpr size_t kSampleRate = 48000; | ||
| inline constexpr size_t kChannels = 2; | ||
| inline constexpr size_t kBytesPerSample = sizeof(int16_t); | ||
| inline constexpr size_t kFrameSizeBytes = kChannels * kBytesPerSample; | ||
|
|
||
| inline constexpr size_t kChunkFrames = 4096; | ||
| inline constexpr size_t kChunkSizeBytes = kChunkFrames * kFrameSizeBytes; | ||
|
|
||
| class AudioGenerator { | ||
| public: | ||
| AudioGenerator(); | ||
|
|
||
| void GenerateChunk(std::span<int16_t> samples, | ||
| const TunerStateSnapshot& snapshot); | ||
|
|
||
| private: | ||
| std::minstd_rand rng_; | ||
| }; | ||
|
|
||
| } // namespace virtualtuner | ||
| } // namespace cuttlefish |
196 changes: 196 additions & 0 deletions
196
base/cvd/cuttlefish/host/commands/virtual_tuner_daemon/pcm_stream_server.cpp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| /* | ||
| * Copyright (C) 2026 The Android Open Source Project | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| #include "cuttlefish/host/commands/virtual_tuner_daemon/pcm_stream_server.h" | ||
|
|
||
| #include <atomic> | ||
| #include <cerrno> | ||
| #include <chrono> | ||
| #include <cstddef> | ||
| #include <cstdint> | ||
| #include <functional> | ||
| #include <memory> | ||
| #include <mutex> | ||
| #include <ratio> | ||
| #include <thread> | ||
| #include <utility> | ||
| #include <vector> | ||
|
|
||
| #include "absl/log/log.h" | ||
|
|
||
| #include "cuttlefish/common/libs/fs/fd.h" | ||
| #include "cuttlefish/common/libs/fs/shared_buf.h" | ||
| #include "cuttlefish/common/libs/fs/shared_fd.h" | ||
| #include "cuttlefish/host/commands/virtual_tuner_daemon/audio_generator.h" | ||
| #include "cuttlefish/host/commands/virtual_tuner_daemon/tuner_state.h" | ||
| #include "cuttlefish/result/result.h" | ||
|
|
||
| namespace cuttlefish { | ||
| namespace virtualtuner { | ||
| namespace { | ||
|
|
||
| using ChunkDuration = | ||
| std::chrono::duration<int64_t, std::ratio<kChunkFrames, kSampleRate>>; | ||
|
|
||
| constexpr auto kMaxSchedulingLag = std::chrono::milliseconds(500); | ||
| constexpr int kPrebufferChunks = 2; | ||
|
|
||
| } // namespace | ||
|
|
||
| PcmStreamServer::PcmStreamServer(TunerState& tuner_state, SharedFD server_fd) | ||
| : tuner_state_(tuner_state), server_fd_(std::move(server_fd)) {} | ||
|
|
||
| PcmStreamServer::~PcmStreamServer() { Stop(); } | ||
|
|
||
| Result<void> PcmStreamServer::Start() { | ||
| if (is_running_) { | ||
| return {}; | ||
| } | ||
|
|
||
| CF_EXPECT(server_fd_->IsOpen(), | ||
| "Failed to start PCM streaming server: server_fd is not open"); | ||
|
|
||
| LOG(INFO) << "PCM streaming server running with inherited server fd."; | ||
| is_running_ = true; | ||
| accept_thread_ = std::thread(&PcmStreamServer::AcceptLoop, this); | ||
| return {}; | ||
| } | ||
|
|
||
| void PcmStreamServer::Stop() { | ||
| if (!is_running_.exchange(false)) { | ||
| return; | ||
| } | ||
|
|
||
| if (server_fd_->IsOpen()) { | ||
| server_fd_->Shutdown(SHUT_RDWR); | ||
| server_fd_->Close(); | ||
| } | ||
| stop_cv_.notify_all(); | ||
| if (accept_thread_.joinable()) { | ||
| accept_thread_.join(); | ||
| } | ||
|
|
||
| std::vector<std::unique_ptr<ClientSession>> sessions; | ||
| { | ||
| std::lock_guard<std::mutex> lock(clients_mutex_); | ||
| sessions.swap(clients_); | ||
| } | ||
| for (const auto& session : sessions) { | ||
| if (session->fd->IsOpen()) { | ||
| session->fd->Shutdown(SHUT_RDWR); | ||
| session->fd->Close(); | ||
| } | ||
| } | ||
| for (auto& session : sessions) { | ||
| if (session->thread.joinable()) { | ||
| session->thread.join(); | ||
| } | ||
| } | ||
|
|
||
| LOG(INFO) << "PCM streaming server stopped."; | ||
| } | ||
|
|
||
| void PcmStreamServer::AcceptLoop() { | ||
| while (is_running_) { | ||
| SharedFD client_fd = Fd::Accept(*server_fd_).value_or(Fd()); | ||
| if (!is_running_) { | ||
| break; | ||
| } | ||
| if (!client_fd->IsOpen()) { | ||
| LOG(ERROR) << "Accept failed on PCM server socket: " | ||
| << client_fd->StrError(); | ||
| continue; | ||
|
abinba marked this conversation as resolved.
|
||
| } | ||
|
|
||
| std::lock_guard<std::mutex> lock(clients_mutex_); | ||
| ReapFinishedClients(); | ||
| if (clients_.size() >= kMaxConcurrentClients) { | ||
| LOG(WARNING) << "Refusing PCM client: already serving " << clients_.size() | ||
| << " clients."; | ||
| continue; | ||
| } | ||
|
|
||
| LOG(INFO) << "PCM streaming server accepted a client."; | ||
| auto session = std::make_unique<ClientSession>(); | ||
| session->fd = client_fd; | ||
| ClientSession* session_ptr = session.get(); | ||
| clients_.push_back(std::move(session)); | ||
| session_ptr->thread = std::thread(&PcmStreamServer::StreamClient, this, | ||
| std::ref(*session_ptr)); | ||
| } | ||
| } | ||
|
|
||
| void PcmStreamServer::ReapFinishedClients() { | ||
| std::erase_if(clients_, [](const std::unique_ptr<ClientSession>& session) { | ||
| if (!session->finished.load(std::memory_order_acquire)) { | ||
| return false; | ||
| } | ||
| if (session->thread.joinable()) { | ||
| session->thread.join(); | ||
| } | ||
| return true; | ||
| }); | ||
| } | ||
|
|
||
| void PcmStreamServer::StreamClient(ClientSession& session) { | ||
| const SharedFD& client_fd = session.fd; | ||
| AudioGenerator generator; | ||
| std::vector<int16_t> buffer(kChunkFrames * kChannels); | ||
|
|
||
| const auto write_next_chunk = [&]() -> bool { | ||
| generator.GenerateChunk(buffer, tuner_state_.GetSnapshot()); | ||
| return SendAll(client_fd, buffer.data(), buffer.size() * sizeof(int16_t)); | ||
| }; | ||
|
|
||
| bool connected = true; | ||
| for (int i = 0; i < kPrebufferChunks && connected && is_running_; ++i) { | ||
| connected = write_next_chunk(); | ||
| } | ||
|
|
||
| auto anchor = std::chrono::steady_clock::now(); | ||
| int64_t chunks_since_anchor = 0; | ||
|
|
||
| while (connected && is_running_) { | ||
| ++chunks_since_anchor; | ||
| const auto deadline = anchor + ChunkDuration(chunks_since_anchor); | ||
| { | ||
| std::unique_lock<std::mutex> lock(stop_mutex_); | ||
| if (stop_cv_.wait_until(lock, deadline, | ||
| [&]() -> bool { return !is_running_; })) { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| const auto now = std::chrono::steady_clock::now(); | ||
| if (now > deadline + kMaxSchedulingLag) { | ||
| LOG(WARNING) << "PCM stream fell behind schedule by " | ||
| << std::chrono::duration_cast<std::chrono::milliseconds>( | ||
| now - deadline) | ||
| .count() | ||
| << " ms; re-anchoring."; | ||
| anchor = now; | ||
| chunks_since_anchor = 0; | ||
| } | ||
|
|
||
| connected = write_next_chunk(); | ||
| } | ||
|
|
||
| LOG(INFO) << "PCM client disconnected."; | ||
| session.finished.store(true, std::memory_order_release); | ||
| } | ||
|
|
||
| } // namespace virtualtuner | ||
| } // namespace cuttlefish | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.