Android-cuttlefish cvd tool
tuner_state.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 <chrono>
20#include <compare>
21#include <condition_variable>
22#include <cstddef>
23#include <cstdint>
24#include <map>
25#include <mutex>
26#include <optional>
27#include <string>
28#include <vector>
29
30#include "cuttlefish/host/commands/virtual_tuner_daemon/VirtualTuner.pb.h"
31
32namespace cuttlefish {
33namespace virtualtuner {
34
35inline constexpr uint32_t kMaxHdSubchannel = 7;
36inline constexpr size_t kMaxStations = 1024;
37inline constexpr size_t kMaxStationProgramBytes = 16 * 1024;
38
40 RadioBand band = RadioBand::FM;
41 uint32_t frequency_hz = 0;
42 uint32_t hd_subchannel = 0;
43 bool is_playing = false;
44 bool station_present = false;
45 std::string station_wav_path;
46};
47
49 public:
50 TunerState();
51
52 void SetTune(RadioBand band, uint32_t frequency_hz, uint32_t hd_subchannel);
53 void Stop();
54 void SetStationWavPath(RadioBand band, uint32_t frequency_hz,
55 uint32_t hd_subchannel, std::string wav_path);
56
57 std::vector<StationProgram> GetStations(std::optional<RadioBand> band) const;
58 // Blocks up to `timeout` until the station table changes from `seen_version`
59 // (always satisfied on initial call when `seen_version == 0`) or `Shutdown()`
60 // is called. Updates `seen_version` and returns matching stations on change,
61 // or returns `std::nullopt` on timeout or after `Shutdown()`.
62 std::optional<std::vector<StationProgram>> WaitForStations(
63 std::optional<RadioBand> band, std::chrono::milliseconds timeout,
64 uint64_t& seen_version) const;
65 void Shutdown();
66 bool IsShuttingDown() const;
67 std::optional<StationProgram> FindStation(RadioBand band,
68 uint32_t frequency_hz,
69 uint32_t hd_subchannel) const;
70 std::optional<StationProgram> Seek(RadioBand band,
71 uint32_t start_frequency_hz,
72 uint32_t start_hd_subchannel,
73 bool direction_up,
74 bool skip_subchannel) const;
75 std::optional<StationProgram> UpsertStation(StationProgram station);
76 bool RemoveStation(RadioBand band, uint32_t frequency_hz,
77 std::optional<uint32_t> hd_subchannel,
78 uint64_t dab_sid_ext);
79
81 TunerStatus GetStatus() const;
82 uint32_t GetFrequency() const;
83 RadioBand GetBand() const;
84 uint32_t GetSubchannel() const;
85 bool IsPlaying() const;
86
87 private:
88 struct StationKey {
89 RadioBand band = RadioBand::FM;
90 uint32_t frequency_hz = 0;
91 uint32_t hd_subchannel = 0;
92 uint64_t dab_sid_ext = 0;
93 uint32_t dab_ensemble = 0;
94
95 auto operator<=>(const StationKey&) const = default;
96 };
97
98 std::vector<StationProgram> GetStationsLocked(
99 std::optional<RadioBand> band) const;
100 std::optional<StationProgram> FindStationLocked(RadioBand band,
101 uint32_t frequency_hz,
102 uint32_t hd_subchannel) const;
103 void RecomputeSubchannelBitmaskLocked(RadioBand band, uint32_t frequency_hz);
104
105 mutable std::mutex mutex_;
106 mutable std::condition_variable stations_cv_;
107 uint64_t stations_version_ = 1;
108 bool shutting_down_ = false;
110 std::optional<StationProgram> current_station_;
111 std::map<StationKey, StationProgram> stations_;
112 std::map<StationKey, std::string> station_wav_paths_;
113};
114
115} // namespace virtualtuner
116} // namespace cuttlefish
Definition: tuner_state.h:48
std::vector< StationProgram > GetStations(std::optional< RadioBand > band) const
Definition: tuner_state.cpp:204
TunerStatus GetStatus() const
Definition: tuner_state.cpp:411
std::optional< std::vector< StationProgram > > WaitForStations(std::optional< RadioBand > band, std::chrono::milliseconds timeout, uint64_t &seen_version) const
Definition: tuner_state.cpp:210
TunerState()
Definition: tuner_state.cpp:154
std::condition_variable stations_cv_
Definition: tuner_state.h:106
TunerStateSnapshot GetSnapshot() const
Definition: tuner_state.cpp:406
std::optional< StationProgram > FindStationLocked(RadioBand band, uint32_t frequency_hz, uint32_t hd_subchannel) const
Definition: tuner_state.cpp:377
void SetTune(RadioBand band, uint32_t frequency_hz, uint32_t hd_subchannel)
Definition: tuner_state.cpp:163
uint64_t stations_version_
Definition: tuner_state.h:107
std::optional< StationProgram > current_station_
Definition: tuner_state.h:110
std::map< StationKey, StationProgram > stations_
Definition: tuner_state.h:111
std::optional< StationProgram > UpsertStation(StationProgram station)
Definition: tuner_state.cpp:297
bool IsPlaying() const
Definition: tuner_state.cpp:439
uint32_t GetSubchannel() const
Definition: tuner_state.cpp:434
TunerStateSnapshot state_
Definition: tuner_state.h:109
std::map< StationKey, std::string > station_wav_paths_
Definition: tuner_state.h:112
uint32_t GetFrequency() const
Definition: tuner_state.cpp:424
void Stop()
Definition: tuner_state.cpp:178
bool RemoveStation(RadioBand band, uint32_t frequency_hz, std::optional< uint32_t > hd_subchannel, uint64_t dab_sid_ext)
Definition: tuner_state.cpp:323
void Shutdown()
Definition: tuner_state.cpp:225
std::optional< StationProgram > Seek(RadioBand band, uint32_t start_frequency_hz, uint32_t start_hd_subchannel, bool direction_up, bool skip_subchannel) const
Definition: tuner_state.cpp:244
bool shutting_down_
Definition: tuner_state.h:108
void SetStationWavPath(RadioBand band, uint32_t frequency_hz, uint32_t hd_subchannel, std::string wav_path)
Definition: tuner_state.cpp:187
std::vector< StationProgram > GetStationsLocked(std::optional< RadioBand > band) const
Definition: tuner_state.cpp:365
bool IsShuttingDown() const
Definition: tuner_state.cpp:233
RadioBand GetBand() const
Definition: tuner_state.cpp:429
void RecomputeSubchannelBitmaskLocked(RadioBand band, uint32_t frequency_hz)
Definition: tuner_state.cpp:388
std::mutex mutex_
Definition: tuner_state.h:105
std::optional< StationProgram > FindStation(RadioBand band, uint32_t frequency_hz, uint32_t hd_subchannel) const
Definition: tuner_state.cpp:238
constexpr size_t kMaxStationProgramBytes
Definition: tuner_state.h:37
constexpr uint32_t kMaxHdSubchannel
Definition: tuner_state.h:35
constexpr size_t kMaxStations
Definition: tuner_state.h:36
Definition: alloc_driver.h:21
__u8 band
Definition: nl80211.h:0
RadioBand band
Definition: tuner_state.h:40
bool station_present
Definition: tuner_state.h:44
uint32_t hd_subchannel
Definition: tuner_state.h:42
bool is_playing
Definition: tuner_state.h:43
uint32_t frequency_hz
Definition: tuner_state.h:41
std::string station_wav_path
Definition: tuner_state.h:45
uint32_t dab_ensemble
Definition: tuner_state.h:93
uint32_t frequency_hz
Definition: tuner_state.h:90
RadioBand band
Definition: tuner_state.h:89
uint32_t hd_subchannel
Definition: tuner_state.h:91
uint64_t dab_sid_ext
Definition: tuner_state.h:92
auto operator<=>(const StationKey &) const =default