blob: 80781150162b2a37752958bca7a4b9b3830ffaed [file] [log] [blame]
Christopher Grant764281a92018-03-10 00:48:251// 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 Rockoteff2b822019-06-20 22:56:536#include "content/public/browser/system_connector.h"
Yu-Hsuan Hsu5caedd22019-03-11 12:38:047#include "services/audio/public/cpp/sounds/sounds_manager.h"
Christopher Grant764281a92018-03-10 00:48:258
9namespace vr {
10
11SoundsManagerAudioDelegate::SoundsManagerAudioDelegate() {}
12
13SoundsManagerAudioDelegate::~SoundsManagerAudioDelegate() {
14 ResetSounds();
15}
16
17void 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 Hsu5caedd22019-03-11 12:38:0421 audio::SoundsManager::Shutdown();
Christopher Grant764281a92018-03-10 00:48:2522 sounds_.clear();
23 }
24}
25
26bool 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 Rockoteff2b822019-06-20 22:56:5332 if (sounds_.empty())
33 audio::SoundsManager::Create(content::GetSystemConnector()->Clone());
Christopher Grant764281a92018-03-10 00:48:2534
35 sounds_[id] = std::move(data);
Yu-Hsuan Hsu5caedd22019-03-11 12:38:0436 return audio::SoundsManager::Get()->Initialize(id, *sounds_[id]);
Christopher Grant764281a92018-03-10 00:48:2537}
38
39void SoundsManagerAudioDelegate::PlaySound(SoundId id) {
40 if (sounds_.find(id) != sounds_.end())
Yu-Hsuan Hsu5caedd22019-03-11 12:38:0441 audio::SoundsManager::Get()->Play(id);
Christopher Grant764281a92018-03-10 00:48:2542}
43
44} // namespace vr