Avi Drissman | 4e1b7bc3 | 2022-09-15 14:03:50 | [diff] [blame] | 1 | // Copyright 2019 The Chromium Authors |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "content/public/browser/audio_service.h" |
| 6 | |
David Sanders | 6f0eeb0 | 2025-01-30 06:48:50 | [diff] [blame] | 7 | #include "base/auto_reset.h" |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 8 | #include "base/command_line.h" |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 9 | #include "base/metrics/field_trial_params.h" |
Douglas Wong | 6961d23 | 2022-10-07 17:19:01 | [diff] [blame] | 10 | #include "base/strings/strcat.h" |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 11 | #include "base/strings/string_number_conversions.h" |
Patrick Monette | 643cdf6 | 2021-10-15 19:13:42 | [diff] [blame] | 12 | #include "base/task/deferred_sequenced_task_runner.h" |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 13 | #include "base/threading/sequence_local_storage_slot.h" |
| 14 | #include "base/time/time.h" |
| 15 | #include "build/build_config.h" |
| 16 | #include "content/browser/browser_main_loop.h" |
| 17 | #include "content/public/browser/browser_task_traits.h" |
| 18 | #include "content/public/browser/browser_thread.h" |
| 19 | #include "content/public/browser/content_browser_client.h" |
| 20 | #include "content/public/browser/service_process_host.h" |
| 21 | #include "content/public/common/content_client.h" |
| 22 | #include "content/public/common/content_features.h" |
| 23 | #include "content/public/common/content_switches.h" |
| 24 | #include "media/audio/audio_manager.h" |
| 25 | #include "media/base/media_switches.h" |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 26 | #include "media/media_buildflags.h" |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 27 | #include "mojo/public/cpp/bindings/remote.h" |
| 28 | #include "services/audio/public/cpp/audio_system_to_service_adapter.h" |
Alex Gough | ec569389 | 2021-10-12 00:55:44 | [diff] [blame] | 29 | #include "services/audio/public/mojom/audio_service.mojom.h" |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 30 | #include "services/audio/service.h" |
| 31 | #include "services/audio/service_factory.h" |
| 32 | |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 33 | #if BUILDFLAG(ENABLE_PASSTHROUGH_AUDIO_CODECS) |
Roy Funderburk | 32f66f6b | 2022-10-25 02:46:28 | [diff] [blame] | 34 | #include "ui/display/util/edid_parser.h" |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 35 | |
| 36 | #if BUILDFLAG(IS_LINUX) |
| 37 | #include "ui/display/display_util.h" |
| 38 | #endif // BUILDFLAG(IS_LINUX) |
| 39 | |
| 40 | #if BUILDFLAG(IS_WIN) |
Roy Funderburk | 32f66f6b | 2022-10-25 02:46:28 | [diff] [blame] | 41 | #include "ui/display/win/audio_edid_scan.h" |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 42 | #endif // BUILDFLAG(IS_WIN) |
| 43 | #endif // BUILDFLAG(ENABLE_PASSTHROUGH_AUDIO_CODECS) |
Douglas Wong | 6961d23 | 2022-10-07 17:19:01 | [diff] [blame] | 44 | |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 45 | namespace content { |
| 46 | |
| 47 | namespace { |
| 48 | |
ahmedmoussa | f16c8c9 | 2023-12-12 09:33:40 | [diff] [blame] | 49 | audio::mojom::AudioService* g_service_override = nullptr; |
| 50 | |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 51 | bool IsAudioServiceOutOfProcess() { |
Lei Li | d9d7df4 | 2020-03-27 04:33:23 | [diff] [blame] | 52 | return !base::CommandLine::ForCurrentProcess()->HasSwitch( |
| 53 | switches::kSingleProcess) && |
| 54 | base::FeatureList::IsEnabled(features::kAudioServiceOutOfProcess) && |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 55 | !GetContentClient()->browser()->OverridesAudioManager(); |
| 56 | } |
| 57 | |
| 58 | void BindSystemInfoFromAnySequence( |
| 59 | mojo::PendingReceiver<audio::mojom::SystemInfo> receiver) { |
| 60 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 61 | GetUIThreadTaskRunner({})->PostTask( |
| 62 | FROM_HERE, |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 63 | base::BindOnce(&BindSystemInfoFromAnySequence, std::move(receiver))); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | GetAudioService().BindSystemInfo(std::move(receiver)); |
| 68 | } |
| 69 | |
| 70 | void BindStreamFactoryFromAnySequence( |
Katie Dektar | e87ac2e | 2021-03-03 01:05:58 | [diff] [blame] | 71 | mojo::PendingReceiver<media::mojom::AudioStreamFactory> receiver) { |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 72 | if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
Gabriel Charette | e7cdc5cd | 2020-05-27 23:35:05 | [diff] [blame] | 73 | GetUIThreadTaskRunner({})->PostTask( |
| 74 | FROM_HERE, |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 75 | base::BindOnce(&BindStreamFactoryFromAnySequence, std::move(receiver))); |
| 76 | return; |
| 77 | } |
| 78 | |
| 79 | GetAudioService().BindStreamFactory(std::move(receiver)); |
| 80 | } |
| 81 | |
| 82 | void LaunchAudioServiceInProcess( |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 83 | mojo::PendingReceiver<audio::mojom::AudioService> receiver) { |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 84 | // NOTE: If BrowserMainLoop is uninitialized, we have no AudioManager. In |
| 85 | // this case we discard the receiver. The remote will always discard |
| 86 | // messages. This is to work around unit testing environments where no |
| 87 | // BrowserMainLoop is initialized. |
| 88 | if (!BrowserMainLoop::GetInstance()) |
| 89 | return; |
| 90 | |
Georg Neis | 35ff854b | 2024-12-17 02:02:08 | [diff] [blame] | 91 | #if BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(USE_CRAS) |
Simon Hangl | 90a481c1 | 2023-02-22 13:45:46 | [diff] [blame] | 92 | if (GetContentClient()->browser()->EnforceSystemAudioEchoCancellation()) { |
| 93 | base::CommandLine::ForCurrentProcess()->AppendSwitch( |
| 94 | switches::kSystemAecEnabled); |
| 95 | } |
Georg Neis | 35ff854b | 2024-12-17 02:02:08 | [diff] [blame] | 96 | #endif // BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(USE_CRAS) |
Simon Hangl | 90a481c1 | 2023-02-22 13:45:46 | [diff] [blame] | 97 | |
Alison Gale | 53c77f6 | 2024-04-22 15:16:27 | [diff] [blame] | 98 | // TODO(crbug.com/40580951): Remove |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 99 | // BrowserMainLoop::GetAudioManager(). |
| 100 | audio::Service::GetInProcessTaskRunner()->PostTask( |
| 101 | FROM_HERE, |
| 102 | base::BindOnce( |
| 103 | [](media::AudioManager* audio_manager, |
| 104 | mojo::PendingReceiver<audio::mojom::AudioService> receiver) { |
Avi Drissman | ded7717 | 2021-07-02 18:23:00 | [diff] [blame] | 105 | static base::SequenceLocalStorageSlot< |
| 106 | std::unique_ptr<audio::Service>> |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 107 | service; |
Avi Drissman | ded7717 | 2021-07-02 18:23:00 | [diff] [blame] | 108 | service.GetOrCreateValue() = audio::CreateEmbeddedService( |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 109 | audio_manager, std::move(receiver)); |
| 110 | }, |
| 111 | BrowserMainLoop::GetAudioManager(), std::move(receiver))); |
| 112 | } |
| 113 | |
| 114 | void LaunchAudioServiceOutOfProcess( |
Douglas Wong | 6961d23 | 2022-10-07 17:19:01 | [diff] [blame] | 115 | mojo::PendingReceiver<audio::mojom::AudioService> receiver, |
| 116 | uint32_t codec_bitmask) { |
| 117 | std::vector<std::string> switches; |
| 118 | #if BUILDFLAG(IS_MAC) |
| 119 | // On Mac, the audio service requires a CFRunLoop provided by a |
| 120 | // UI MessageLoop type, to run AVFoundation and CoreAudio code. |
| 121 | // See https://crbug.com/834581. |
| 122 | switches.push_back(switches::kMessageLoopTypeUi); |
| 123 | #elif BUILDFLAG(IS_WIN) |
| 124 | if (GetContentClient()->browser()->ShouldEnableAudioProcessHighPriority()) |
| 125 | switches.push_back(switches::kAudioProcessHighPriority); |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 126 | #endif // BUILDFLAG(IS_WIN) |
| 127 | #if BUILDFLAG(ENABLE_PASSTHROUGH_AUDIO_CODECS) |
Douglas Wong | 6961d23 | 2022-10-07 17:19:01 | [diff] [blame] | 128 | switches.push_back(base::StrCat({switches::kAudioCodecsFromEDID, "=", |
| 129 | base::NumberToString(codec_bitmask)})); |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 130 | #endif // BUILDFLAG(ENABLE_PASSTHROUGH_AUDIO_CODECS) |
Georg Neis | 35ff854b | 2024-12-17 02:02:08 | [diff] [blame] | 131 | #if BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(USE_CRAS) |
Simon Hangl | 90a481c1 | 2023-02-22 13:45:46 | [diff] [blame] | 132 | if (GetContentClient()->browser()->EnforceSystemAudioEchoCancellation()) { |
| 133 | switches.push_back(switches::kSystemAecEnabled); |
| 134 | } |
Georg Neis | 81f9d7f4 | 2024-08-06 02:43:45 | [diff] [blame] | 135 | #endif // BUILDFLAG(IS_CHROMEOS) && BUILDFLAG(USE_CRAS) |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 136 | ServiceProcessHost::Launch( |
| 137 | std::move(receiver), |
| 138 | ServiceProcessHost::Options() |
| 139 | .WithDisplayName("Audio Service") |
Douglas Wong | 6961d23 | 2022-10-07 17:19:01 | [diff] [blame] | 140 | .WithExtraCommandLineSwitches(std::move(switches)) |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 141 | .Pass()); |
| 142 | } |
| 143 | |
Douglas Wong | 6961d23 | 2022-10-07 17:19:01 | [diff] [blame] | 144 | void LaunchAudioService( |
| 145 | mojo::PendingReceiver<audio::mojom::AudioService> receiver, |
| 146 | uint32_t codec_bitmask) { |
| 147 | // The static storage slot in GetAudioService() prevents LaunchAudioService |
| 148 | // from being called more than once. |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 149 | if (IsAudioServiceOutOfProcess()) { |
Douglas Wong | 6961d23 | 2022-10-07 17:19:01 | [diff] [blame] | 150 | LaunchAudioServiceOutOfProcess(std::move(receiver), codec_bitmask); |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 151 | } else { |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 152 | LaunchAudioServiceInProcess(std::move(receiver)); |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 153 | } |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 154 | } |
| 155 | |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 156 | #if BUILDFLAG(ENABLE_PASSTHROUGH_AUDIO_CODECS) |
Roy Funderburk | 32f66f6b | 2022-10-25 02:46:28 | [diff] [blame] | 157 | // Convert the EDID supported audio bitstream formats into media codec bitmasks. |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 158 | uint32_t ConvertEdidBitstreams(uint32_t formats) { |
Roy Funderburk | 32f66f6b | 2022-10-25 02:46:28 | [diff] [blame] | 159 | uint32_t codec_bitmask = 0; |
Roy Funderburk | 32f66f6b | 2022-10-25 02:46:28 | [diff] [blame] | 160 | if (formats & display::EdidParser::kAudioBitstreamPcmLinear) |
| 161 | codec_bitmask |= media::AudioParameters::AUDIO_PCM_LINEAR; |
| 162 | if (formats & display::EdidParser::kAudioBitstreamDts) |
| 163 | codec_bitmask |= media::AudioParameters::AUDIO_BITSTREAM_DTS; |
| 164 | if (formats & display::EdidParser::kAudioBitstreamDtsHd) |
| 165 | codec_bitmask |= media::AudioParameters::AUDIO_BITSTREAM_DTS_HD; |
| 166 | return codec_bitmask; |
| 167 | } |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 168 | |
| 169 | #if BUILDFLAG(IS_WIN) |
| 170 | // Convert the EDID supported audio bitstream formats into media codec bitmasks. |
| 171 | uint32_t ScanEdidBitstreams() { |
| 172 | return ConvertEdidBitstreams(display::win::ScanEdidBitstreams()); |
| 173 | } |
| 174 | #endif // BUILDFLAG(IS_WIN) |
| 175 | #endif // BUILDFLAG(ENABLE_PASSTHROUGH_AUDIO_CODECS) |
Roy Funderburk | 32f66f6b | 2022-10-25 02:46:28 | [diff] [blame] | 176 | |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 177 | } // namespace |
| 178 | |
| 179 | audio::mojom::AudioService& GetAudioService() { |
| 180 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
ahmedmoussa | f16c8c9 | 2023-12-12 09:33:40 | [diff] [blame] | 181 | if (g_service_override) { |
| 182 | return *g_service_override; |
| 183 | } |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 184 | |
| 185 | // NOTE: We use sequence-local storage slot not because we support access from |
| 186 | // any sequence, but to limit the lifetime of this Remote to the lifetime of |
| 187 | // UI-thread sequence. This is to support re-creation after task environment |
| 188 | // shutdown and reinitialization e.g. between unit tests. |
Avi Drissman | ded7717 | 2021-07-02 18:23:00 | [diff] [blame] | 189 | static base::SequenceLocalStorageSlot< |
| 190 | mojo::Remote<audio::mojom::AudioService>> |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 191 | remote_slot; |
Avi Drissman | ded7717 | 2021-07-02 18:23:00 | [diff] [blame] | 192 | auto& remote = remote_slot.GetOrCreateValue(); |
Douglas Wong | 6961d23 | 2022-10-07 17:19:01 | [diff] [blame] | 193 | if (!remote) { |
| 194 | auto receiver = remote.BindNewPipeAndPassReceiver(); |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 195 | #if BUILDFLAG(ENABLE_PASSTHROUGH_AUDIO_CODECS) && BUILDFLAG(IS_WIN) |
Douglas Wong | 6961d23 | 2022-10-07 17:19:01 | [diff] [blame] | 196 | // The EDID scan is done in a COM STA thread and the result |
| 197 | // passed to the audio service launcher. |
| 198 | base::ThreadPool::CreateCOMSTATaskRunner( |
| 199 | {base::MayBlock(), base::TaskPriority::USER_BLOCKING, |
| 200 | base::TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN}) |
| 201 | ->PostTaskAndReplyWithResult( |
Roy Funderburk | 32f66f6b | 2022-10-25 02:46:28 | [diff] [blame] | 202 | FROM_HERE, base::BindOnce(&ScanEdidBitstreams), |
Douglas Wong | 6961d23 | 2022-10-07 17:19:01 | [diff] [blame] | 203 | base::BindOnce(&LaunchAudioService, std::move(receiver))); |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 204 | #elif BUILDFLAG(ENABLE_PASSTHROUGH_AUDIO_CODECS) && BUILDFLAG(IS_LINUX) |
| 205 | LaunchAudioService( |
| 206 | std::move(receiver), |
| 207 | ConvertEdidBitstreams(display::DisplayUtil::GetAudioFormats())); |
Douglas Wong | 6961d23 | 2022-10-07 17:19:01 | [diff] [blame] | 208 | #else |
| 209 | LaunchAudioService(std::move(receiver), 0); |
Roy Funderburk | b7cbf5a9 | 2022-11-10 19:10:11 | [diff] [blame] | 210 | #endif // BUILDFLAG(ENABLE_PASSTHROUGH_AUDIO_CODECS) && BUILDFLAG(IS_WIN) |
Douglas Wong | 6961d23 | 2022-10-07 17:19:01 | [diff] [blame] | 211 | remote.reset_on_disconnect(); |
| 212 | } |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 213 | return *remote.get(); |
| 214 | } |
| 215 | |
ahmedmoussa | f16c8c9 | 2023-12-12 09:33:40 | [diff] [blame] | 216 | base::AutoReset<audio::mojom::AudioService*> |
| 217 | OverrideAudioServiceForTesting( // IN-TEST |
| 218 | audio::mojom::AudioService* service) { |
| 219 | DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 220 | return {&g_service_override, service}; |
| 221 | } |
| 222 | |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 223 | std::unique_ptr<media::AudioSystem> CreateAudioSystemForAudioService() { |
Peter Kasting | e5a38ed | 2021-10-02 03:06:35 | [diff] [blame] | 224 | constexpr auto kServiceDisconnectTimeout = base::Seconds(1); |
Ken Rockot | 7296440 | 2019-12-06 10:40:51 | [diff] [blame] | 225 | return std::make_unique<audio::AudioSystemToServiceAdapter>( |
| 226 | base::BindRepeating(&BindSystemInfoFromAnySequence), |
| 227 | kServiceDisconnectTimeout); |
| 228 | } |
| 229 | |
| 230 | AudioServiceStreamFactoryBinder GetAudioServiceStreamFactoryBinder() { |
| 231 | return base::BindRepeating(&BindStreamFactoryFromAnySequence); |
| 232 | } |
| 233 | |
| 234 | } // namespace content |