Weilin Xu | b2a6ca6 | 2022-05-08 23:47:04 +0000 | [diff] [blame] | 1 | /* |
| 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 Xu | 90e39f5 | 2023-11-07 20:07:23 -0800 | [diff] [blame] | 19 | #include <unordered_set> |
Weilin Xu | b2a6ca6 | 2022-05-08 23:47:04 +0000 | [diff] [blame] | 20 | |
| 21 | namespace aidl::android::hardware::broadcastradio { |
| 22 | |
| 23 | using ::aidl::android::hardware::broadcastradio::utils::makeSelectorAmfm; |
| 24 | using ::aidl::android::hardware::broadcastradio::utils::makeSelectorDab; |
Weilin Xu | 39dd0f8 | 2023-09-07 17:00:57 -0700 | [diff] [blame] | 25 | using ::aidl::android::hardware::broadcastradio::utils::makeSelectorHd; |
Weilin Xu | b2a6ca6 | 2022-05-08 23:47:04 +0000 | [diff] [blame] | 26 | using ::std::string; |
Weilin Xu | 90e39f5 | 2023-11-07 20:07:23 -0800 | [diff] [blame] | 27 | using ::std::unordered_set; |
Weilin Xu | b2a6ca6 | 2022-05-08 23:47:04 +0000 | [diff] [blame] | 28 | using ::std::vector; |
| 29 | |
| 30 | VirtualRadio::VirtualRadio(const string& name, const vector<VirtualProgram>& initialList) |
| 31 | : mName(name), mPrograms(initialList) { |
| 32 | sort(mPrograms.begin(), mPrograms.end()); |
| 33 | } |
| 34 | |
| 35 | string VirtualRadio::getName() const { |
| 36 | return mName; |
| 37 | } |
| 38 | |
| 39 | const vector<VirtualProgram>& VirtualRadio::getProgramList() const { |
| 40 | return mPrograms; |
| 41 | } |
| 42 | |
| 43 | bool VirtualRadio::getProgram(const ProgramSelector& selector, VirtualProgram* programOut) const { |
Weilin Xu | 39dd0f8 | 2023-09-07 17:00:57 -0700 | [diff] [blame] | 44 | for (auto it = mPrograms.begin(); it != mPrograms.end(); it++) { |
| 45 | if (!utils::tunesTo(selector, it->selector)) { |
| 46 | continue; |
Weilin Xu | b2a6ca6 | 2022-05-08 23:47:04 +0000 | [diff] [blame] | 47 | } |
Weilin Xu | 39dd0f8 | 2023-09-07 17:00:57 -0700 | [diff] [blame] | 48 | 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 Xu | b2a6ca6 | 2022-05-08 23:47:04 +0000 | [diff] [blame] | 63 | } |
| 64 | return false; |
| 65 | } |
| 66 | |
Weilin Xu | 90e39f5 | 2023-11-07 20:07:23 -0800 | [diff] [blame] | 67 | vector<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 Xu | b2a6ca6 | 2022-05-08 23:47:04 +0000 | [diff] [blame] | 81 | // get singleton of AMFM Virtual Radio |
| 82 | const VirtualRadio& VirtualRadio::getAmFmRadio() { |
| 83 | // clang-format off |
| 84 | static VirtualRadio amFmRadioMock( |
| 85 | "AM/FM radio mock", |
| 86 | { |
Weilin Xu | 664048f | 2023-09-07 11:26:38 -0700 | [diff] [blame] | 87 | {makeSelectorAmfm(/* frequency= */ 94900u), "Wild 94.9", "Drake ft. Rihanna", |
Weilin Xu | 0d4207d | 2022-12-09 00:37:44 +0000 | [diff] [blame] | 88 | "Too Good"}, |
Weilin Xu | 664048f | 2023-09-07 11:26:38 -0700 | [diff] [blame] | 89 | {makeSelectorAmfm(/* frequency= */ 96500u), "KOIT", "Celine Dion", "All By Myself"}, |
Weilin Xu | 664048f | 2023-09-07 11:26:38 -0700 | [diff] [blame] | 90 | {makeSelectorAmfm(/* frequency= */ 101300u), "101-3 KISS-FM", "Justin Timberlake", |
Weilin Xu | 0d4207d | 2022-12-09 00:37:44 +0000 | [diff] [blame] | 91 | "Rock Your Body"}, |
Weilin Xu | 664048f | 2023-09-07 11:26:38 -0700 | [diff] [blame] | 92 | {makeSelectorAmfm(/* frequency= */ 103700u), "iHeart80s @ 103.7", "Michael Jackson", |
Weilin Xu | 0d4207d | 2022-12-09 00:37:44 +0000 | [diff] [blame] | 93 | "Billie Jean"}, |
Weilin Xu | 664048f | 2023-09-07 11:26:38 -0700 | [diff] [blame] | 94 | {makeSelectorAmfm(/* frequency= */ 106100u), "106 KMEL", "Drake", "Marvins Room"}, |
Weilin Xu | 39dd0f8 | 2023-09-07 17:00:57 -0700 | [diff] [blame] | 95 | {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 Xu | b2a6ca6 | 2022-05-08 23:47:04 +0000 | [diff] [blame] | 111 | }); |
| 112 | // clang-format on |
| 113 | return amFmRadioMock; |
| 114 | } |
| 115 | |
| 116 | // get singleton of DAB Virtual Radio |
| 117 | const VirtualRadio& VirtualRadio::getDabRadio() { |
| 118 | // clang-format off |
| 119 | static VirtualRadio dabRadioMock( |
| 120 | "DAB radio mock", |
| 121 | { |
Weilin Xu | 90e39f5 | 2023-11-07 20:07:23 -0800 | [diff] [blame] | 122 | {makeSelectorDab(/* sidExt= */ 0x0E10000C221u, /* ensemble= */ 0xCE15u, |
Weilin Xu | 664048f | 2023-09-07 11:26:38 -0700 | [diff] [blame] | 123 | /* freq= */ 225648u), "BBC Radio 1", "Khalid", "Talk"}, |
Weilin Xu | 90e39f5 | 2023-11-07 20:07:23 -0800 | [diff] [blame] | 124 | {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 Xu | 664048f | 2023-09-07 11:26:38 -0700 | [diff] [blame] | 131 | /* freq= */ 222064u), "Classic FM", "Jean Sibelius", "Andante Festivo"}, |
Weilin Xu | 90e39f5 | 2023-11-07 20:07:23 -0800 | [diff] [blame] | 132 | {makeSelectorDab(/* sidExt= */ 0x0E10000C1C0u, /* ensemble= */ 0xC181u, |
| 133 | /* freq= */ 223936u), "Absolute Radio", "Coldplay", "Clocks"}, |
| 134 | {makeSelectorDab(/* sidExt= */ 0x0E10000C1C0u, /* ensemble= */ 0xC181u, |
Weilin Xu | 664048f | 2023-09-07 11:26:38 -0700 | [diff] [blame] | 135 | /* freq= */ 222064u), "Absolute Radio", "Coldplay", "Clocks"}, |
Weilin Xu | 90e39f5 | 2023-11-07 20:07:23 -0800 | [diff] [blame] | 136 | {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 Xu | b2a6ca6 | 2022-05-08 23:47:04 +0000 | [diff] [blame] | 140 | }); |
| 141 | // clang-format on |
| 142 | return dabRadioMock; |
| 143 | } |
| 144 | |
| 145 | } // namespace aidl::android::hardware::broadcastradio |