Christopher Grant | 764281a9 | 2018-03-10 00:48:25 | [diff] [blame] | 1 | // Copyright 2018 The Chromium Authors. All rights reserved. |
| 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 "chrome/browser/vr/sounds_manager_audio_delegate.h" |
Ken Rockot | eff2b82 | 2019-06-20 22:56:53 | [diff] [blame] | 6 | #include "content/public/browser/system_connector.h" |
Yu-Hsuan Hsu | 5caedd2 | 2019-03-11 12:38:04 | [diff] [blame] | 7 | #include "services/audio/public/cpp/sounds/sounds_manager.h" |
Christopher Grant | 764281a9 | 2018-03-10 00:48:25 | [diff] [blame] | 8 | |
| 9 | namespace vr { |
| 10 | |
| 11 | SoundsManagerAudioDelegate::SoundsManagerAudioDelegate() {} |
| 12 | |
| 13 | SoundsManagerAudioDelegate::~SoundsManagerAudioDelegate() { |
| 14 | ResetSounds(); |
| 15 | } |
| 16 | |
| 17 | void SoundsManagerAudioDelegate::ResetSounds() { |
| 18 | // Because SoundsManager cannot replace a registered sound, start fresh |
| 19 | // with a new manager if needed. |
| 20 | if (!sounds_.empty()) { |
Yu-Hsuan Hsu | 5caedd2 | 2019-03-11 12:38:04 | [diff] [blame] | 21 | audio::SoundsManager::Shutdown(); |
Christopher Grant | 764281a9 | 2018-03-10 00:48:25 | [diff] [blame] | 22 | sounds_.clear(); |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | bool SoundsManagerAudioDelegate::RegisterSound( |
| 27 | SoundId id, |
| 28 | std::unique_ptr<std::string> data) { |
| 29 | DCHECK_NE(id, kSoundNone); |
| 30 | DCHECK(sounds_.find(id) == sounds_.end()); |
| 31 | |
Ken Rockot | eff2b82 | 2019-06-20 22:56:53 | [diff] [blame] | 32 | if (sounds_.empty()) |
| 33 | audio::SoundsManager::Create(content::GetSystemConnector()->Clone()); |
Christopher Grant | 764281a9 | 2018-03-10 00:48:25 | [diff] [blame] | 34 | |
| 35 | sounds_[id] = std::move(data); |
Yu-Hsuan Hsu | 5caedd2 | 2019-03-11 12:38:04 | [diff] [blame] | 36 | return audio::SoundsManager::Get()->Initialize(id, *sounds_[id]); |
Christopher Grant | 764281a9 | 2018-03-10 00:48:25 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | void SoundsManagerAudioDelegate::PlaySound(SoundId id) { |
| 40 | if (sounds_.find(id) != sounds_.end()) |
Yu-Hsuan Hsu | 5caedd2 | 2019-03-11 12:38:04 | [diff] [blame] | 41 | audio::SoundsManager::Get()->Play(id); |
Christopher Grant | 764281a9 | 2018-03-10 00:48:25 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | } // namespace vr |