Android-cuttlefish cvd tool
pcm_stream_server.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 <condition_variable>
21#include <cstddef>
22#include <cstdint>
23#include <memory>
24#include <mutex>
25#include <span>
26#include <thread>
27#include <vector>
28
32
33namespace cuttlefish {
34namespace virtualtuner {
35
36inline constexpr size_t kMaxConcurrentClients = 8;
37
39 public:
40 PcmStreamServer(TunerState& tuner_state, SharedFD server_fd);
42
45
47 void Stop();
48
49 bool IsRunning() const { return is_running_; }
50
51 private:
54 std::thread thread;
55 std::atomic<bool> finished = false;
56 };
57
58 void AcceptLoop();
59 void StreamClient(ClientSession& session);
60 // Sends `samples` to `client_fd`. Returns whether the client remains
61 // connected (including when interrupted by server shutdown).
62 bool SendChunk(const SharedFD& client_fd, std::span<const int16_t> samples);
64
66 std::atomic<bool> is_running_ = false;
69 std::thread accept_thread_;
70 std::mutex clients_mutex_;
71 std::vector<std::unique_ptr<ClientSession>> clients_;
72 std::condition_variable stop_cv_;
73 std::mutex stop_mutex_;
74};
75
76} // namespace virtualtuner
77} // namespace cuttlefish
Definition: shared_fd.h:125
Definition: pcm_stream_server.h:38
bool IsRunning() const
Definition: pcm_stream_server.h:49
std::vector< std::unique_ptr< ClientSession > > clients_
Definition: pcm_stream_server.h:71
std::mutex clients_mutex_
Definition: pcm_stream_server.h:70
~PcmStreamServer()
Definition: pcm_stream_server.cpp:61
PcmStreamServer(const PcmStreamServer &)=delete
SharedFD stop_fd_
Definition: pcm_stream_server.h:68
std::atomic< bool > is_running_
Definition: pcm_stream_server.h:66
void ReapFinishedClients()
Definition: pcm_stream_server.cpp:182
SharedFD server_fd_
Definition: pcm_stream_server.h:67
void StreamClient(ClientSession &session)
Definition: pcm_stream_server.cpp:238
TunerState & tuner_state_
Definition: pcm_stream_server.h:65
std::mutex stop_mutex_
Definition: pcm_stream_server.h:73
void Stop()
Definition: pcm_stream_server.cpp:85
PcmStreamServer & operator=(const PcmStreamServer &)=delete
PcmStreamServer(TunerState &tuner_state, SharedFD server_fd)
Definition: pcm_stream_server.cpp:58
Result< void > Start()
Definition: pcm_stream_server.cpp:63
std::condition_variable stop_cv_
Definition: pcm_stream_server.h:72
void AcceptLoop()
Definition: pcm_stream_server.cpp:123
std::thread accept_thread_
Definition: pcm_stream_server.h:69
bool SendChunk(const SharedFD &client_fd, std::span< const int16_t > samples)
Definition: pcm_stream_server.cpp:194
Definition: tuner_state.h:48
constexpr size_t kMaxConcurrentClients
Definition: pcm_stream_server.h:36
Definition: alloc_driver.h:21
tl::expected< T, StackTraceError< E > > Result
Definition: result_type.h:30
std::atomic< bool > finished
Definition: pcm_stream_server.h:55
std::thread thread
Definition: pcm_stream_server.h:54
SharedFD fd
Definition: pcm_stream_server.h:53