blob: d6e58cd649fe243bc52350cbaa2f61b9e4285067 [file] [log] [blame]
Weilin Xub2a6ca62022-05-08 23:47:04 +00001/*
2 * Copyright (C) 2022 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#include "VirtualRadio.h"
18#include <broadcastradio-utils-aidl/Utils.h>
Weilin Xu90e39f52023-11-07 20:07:23 -080019#include <unordered_set>
Weilin Xub2a6ca62022-05-08 23:47:04 +000020
21namespace aidl::android::hardware::broadcastradio {
22
23using ::aidl::android::hardware::broadcastradio::utils::makeSelectorAmfm;
24using ::aidl::android::hardware::broadcastradio::utils::makeSelectorDab;
Weilin Xu39dd0f82023-09-07 17:00:57 -070025using ::aidl::android::hardware::broadcastradio::utils::makeSelectorHd;
Weilin Xub2a6ca62022-05-08 23:47:04 +000026using ::std::string;
Weilin Xu90e39f52023-11-07 20:07:23 -080027using ::std::unordered_set;
Weilin Xub2a6ca62022-05-08 23:47:04 +000028using ::std::vector;
29
30VirtualRadio::VirtualRadio(const string& name, const vector<VirtualProgram>& initialList)
31 : mName(name), mPrograms(initialList) {
32 sort(mPrograms.begin(), mPrograms.end());
33}
34
35string VirtualRadio::getName() const {
36 return mName;
37}
38
39const vector<VirtualProgram>& VirtualRadio::getProgramList() const {
40 return mPrograms;
41}
42
43bool VirtualRadio::getProgram(const ProgramSelector& selector, VirtualProgram* programOut) const {
Weilin Xu39dd0f82023-09-07 17:00:57 -070044 for (auto it = mPrograms.begin(); it != mPrograms.end(); it++) {
45 if (!utils::tunesTo(selector, it->selector)) {
46 continue;
Weilin Xub2a6ca62022-05-08 23:47:04 +000047 }
Weilin Xu39dd0f82023-09-07 17:00:57 -070048 auto firstMatchIt = it;
49 if (utils::hasAmFmFrequency(it->selector)) {
50 uint32_t channelFreq = utils::getAmFmFrequency(it->selector);
51 it++;
52 while (it != mPrograms.end() && utils::hasAmFmFrequency(it->selector) &&
53 utils::getAmFmFrequency(it->selector) == channelFreq) {
54 if (it->selector == selector) {
55 *programOut = *it;
56 return true;
57 }
58 it++;
59 }
60 }
61 *programOut = *firstMatchIt;
62 return true;
Weilin Xub2a6ca62022-05-08 23:47:04 +000063 }
64 return false;
65}
66
Weilin Xu90e39f52023-11-07 20:07:23 -080067vector<IdentifierType> VirtualRadio::getSupportedIdentifierTypes() const {
68 unordered_set<IdentifierType> supportedIdentifierTypeSet;
69 for (const auto& program : mPrograms) {
70 IdentifierType type = program.selector.primaryId.type;
71 if (supportedIdentifierTypeSet.count(type)) {
72 continue;
73 }
74 supportedIdentifierTypeSet.insert(type);
75 }
76 vector<IdentifierType> supportedIdentifierTypes(supportedIdentifierTypeSet.begin(),
77 supportedIdentifierTypeSet.end());
78 return supportedIdentifierTypes;
79}
80
Weilin Xub2a6ca62022-05-08 23:47:04 +000081// get singleton of AMFM Virtual Radio
82const VirtualRadio& VirtualRadio::getAmFmRadio() {
83 // clang-format off
84 static VirtualRadio amFmRadioMock(
85 "AM/FM radio mock",
86 {
Weilin Xu664048f2023-09-07 11:26:38 -070087 {makeSelectorAmfm(/* frequency= */ 94900u), "Wild 94.9", "Drake ft. Rihanna",
Weilin Xu0d4207d2022-12-09 00:37:44 +000088 "Too Good"},
Weilin Xu664048f2023-09-07 11:26:38 -070089 {makeSelectorAmfm(/* frequency= */ 96500u), "KOIT", "Celine Dion", "All By Myself"},
Weilin Xu664048f2023-09-07 11:26:38 -070090 {makeSelectorAmfm(/* frequency= */ 101300u), "101-3 KISS-FM", "Justin Timberlake",
Weilin Xu0d4207d2022-12-09 00:37:44 +000091 "Rock Your Body"},
Weilin Xu664048f2023-09-07 11:26:38 -070092 {makeSelectorAmfm(/* frequency= */ 103700u), "iHeart80s @ 103.7", "Michael Jackson",
Weilin Xu0d4207d2022-12-09 00:37:44 +000093 "Billie Jean"},
Weilin Xu664048f2023-09-07 11:26:38 -070094 {makeSelectorAmfm(/* frequency= */ 106100u), "106 KMEL", "Drake", "Marvins Room"},
Weilin Xu39dd0f82023-09-07 17:00:57 -070095 {makeSelectorAmfm(/* frequency= */ 560u), "Talk Radio 560 KSFO", "Artist560", "Title560"},
96 {makeSelectorAmfm(/* frequency= */ 680u), "KNBR 680", "Artist680", "Title680"},
97 {makeSelectorAmfm(/* frequency= */ 97300u), "Alice@97.3", "Drops of Jupiter", "Train"},
98 {makeSelectorAmfm(/* frequency= */ 99700u), "99.7 Now!", "The Chainsmokers", "Closer"},
99 {makeSelectorHd(/* stationId= */ 0xA0000001u, /* subChannel= */ 0u, /* frequency= */ 97700u),
100 "K-LOVE", "ArtistHd0", "TitleHd0"},
101 {makeSelectorHd(/* stationId= */ 0xA0000001u, /* subChannel= */ 1u, /* frequency= */ 97700u),
102 "Air1", "ArtistHd1", "TitleHd1"},
103 {makeSelectorHd(/* stationId= */ 0xA0000001u, /* subChannel= */ 2u, /* frequency= */ 97700u),
104 "K-LOVE Classics", "ArtistHd2", "TitleHd2"},
105 {makeSelectorHd(/* stationId= */ 0xA0000001u, /* subChannel= */ 0u, /* frequency= */ 98500u),
106 "98.5-1 South Bay's Classic Rock", "ArtistHd0", "TitleHd0"},
107 {makeSelectorHd(/* stationId= */ 0xA0000001u, /* subChannel= */ 1u, /* frequency= */ 98500u),
108 "Highway 1 - Different", "ArtistHd1", "TitleHd1"},
109 {makeSelectorHd(/* stationId= */ 0xB0000001u, /* subChannel= */ 0u, /* frequency= */ 1170u),
110 "KLOK", "ArtistHd1", "TitleHd1"},
Weilin Xub2a6ca62022-05-08 23:47:04 +0000111 });
112 // clang-format on
113 return amFmRadioMock;
114}
115
116// get singleton of DAB Virtual Radio
117const VirtualRadio& VirtualRadio::getDabRadio() {
118 // clang-format off
119 static VirtualRadio dabRadioMock(
120 "DAB radio mock",
121 {
Weilin Xu90e39f52023-11-07 20:07:23 -0800122 {makeSelectorDab(/* sidExt= */ 0x0E10000C221u, /* ensemble= */ 0xCE15u,
Weilin Xu664048f2023-09-07 11:26:38 -0700123 /* freq= */ 225648u), "BBC Radio 1", "Khalid", "Talk"},
Weilin Xu90e39f52023-11-07 20:07:23 -0800124 {makeSelectorDab(/* sidExt= */ 0x0E10000C222u, /* ensemble= */ 0xCE15u,
125 /* freq= */ 225648u), "BBC Radio 2", "Khalid", "Talk"},
126 {makeSelectorDab(/* sidExt= */ 0xE10000C224u, /* ensemble= */ 0xCE15u,
127 /* freq= */ 225648u), "BBC Radio 4", "ArtistBBC1", "TitleCountry1"},
128 {makeSelectorDab(/* sidExt= */ 0x1E10000C224u, /* ensemble= */ 0xCE15u,
129 /* freq= */ 225648u), "BBC Radio 4 LW", "ArtistBBC2", "TitleCountry2"},
130 {makeSelectorDab(/* sidExt= */ 0x0E10000C21Au, /* ensemble= */ 0xC181u,
Weilin Xu664048f2023-09-07 11:26:38 -0700131 /* freq= */ 222064u), "Classic FM", "Jean Sibelius", "Andante Festivo"},
Weilin Xu90e39f52023-11-07 20:07:23 -0800132 {makeSelectorDab(/* sidExt= */ 0x0E10000C1C0u, /* ensemble= */ 0xC181u,
133 /* freq= */ 223936u), "Absolute Radio", "Coldplay", "Clocks"},
134 {makeSelectorDab(/* sidExt= */ 0x0E10000C1C0u, /* ensemble= */ 0xC181u,
Weilin Xu664048f2023-09-07 11:26:38 -0700135 /* freq= */ 222064u), "Absolute Radio", "Coldplay", "Clocks"},
Weilin Xu90e39f52023-11-07 20:07:23 -0800136 {makeSelectorDab(/* sidExt= */ 0x0E10000CCE7u, /* ensemble= */ 0xC19Du,
137 /* freq= */ 218640u), "Absolute Radio Country", "ArtistCountry1", "TitleCountry1"},
138 {makeSelectorDab(/* sidExt= */ 0x0E10000CCE7u, /* ensemble= */ 0xC1A0u,
139 /* freq= */ 218640u), "Absolute Radio Country", "ArtistCountry2", "TitleCountry2"},
Weilin Xub2a6ca62022-05-08 23:47:04 +0000140 });
141 // clang-format on
142 return dabRadioMock;
143}
144
145} // namespace aidl::android::hardware::broadcastradio