OLD | NEW |
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "media/audio/linux/audio_manager_linux.h" | 5 #include "media/audio/linux/audio_manager_linux.h" |
6 | 6 |
| 7 #include "base/at_exit.h" |
7 #include "base/command_line.h" | 8 #include "base/command_line.h" |
8 #include "base/logging.h" | 9 #include "base/logging.h" |
9 #include "media/audio/fake_audio_input_stream.h" | 10 #include "media/audio/fake_audio_input_stream.h" |
10 #include "media/audio/fake_audio_output_stream.h" | 11 #include "media/audio/fake_audio_output_stream.h" |
11 #include "media/audio/linux/alsa_output.h" | 12 #include "media/audio/linux/alsa_output.h" |
12 #include "media/audio/linux/alsa_wrapper.h" | 13 #include "media/audio/linux/alsa_wrapper.h" |
13 #include "media/base/media_switches.h" | 14 #include "media/base/media_switches.h" |
14 | 15 |
| 16 |
| 17 namespace { |
| 18 |
| 19 AudioManagerLinux* g_audio_manager = NULL; |
| 20 } // namespace |
| 21 |
15 // Implementation of AudioManager. | 22 // Implementation of AudioManager. |
16 bool AudioManagerLinux::HasAudioOutputDevices() { | 23 bool AudioManagerLinux::HasAudioOutputDevices() { |
17 // TODO(ajwong): Make this actually query audio devices. | 24 // TODO(ajwong): Make this actually query audio devices. |
18 return true; | 25 return true; |
19 } | 26 } |
20 | 27 |
21 bool AudioManagerLinux::HasAudioInputDevices() { | 28 bool AudioManagerLinux::HasAudioInputDevices() { |
22 // TODO(satish): implement. | 29 // TODO(satish): implement. |
23 return false; | 30 return false; |
24 } | 31 } |
(...skipping 17 matching lines...) Expand all Loading... |
42 Format format, | 49 Format format, |
43 int channels, | 50 int channels, |
44 int sample_rate, | 51 int sample_rate, |
45 char bits_per_sample) { | 52 char bits_per_sample) { |
46 // Early return for testing hook. Do this before checking for | 53 // Early return for testing hook. Do this before checking for |
47 // |initialized_|. | 54 // |initialized_|. |
48 if (format == AudioManager::AUDIO_MOCK) { | 55 if (format == AudioManager::AUDIO_MOCK) { |
49 return FakeAudioOutputStream::MakeFakeStream(); | 56 return FakeAudioOutputStream::MakeFakeStream(); |
50 } | 57 } |
51 | 58 |
52 if (!initialized()) { | 59 if (!initialized_) { |
53 return NULL; | 60 return NULL; |
54 } | 61 } |
55 | 62 |
56 std::string device_name = AlsaPcmOutputStream::kAutoSelectDevice; | 63 std::string device_name = AlsaPcmOutputStream::kAutoSelectDevice; |
57 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAlsaDevice)) { | 64 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kAlsaDevice)) { |
58 device_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( | 65 device_name = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
59 switches::kAlsaDevice); | 66 switches::kAlsaDevice); |
60 } | 67 } |
61 AlsaPcmOutputStream* stream = | 68 AlsaPcmOutputStream* stream = |
62 new AlsaPcmOutputStream(device_name, format, channels, sample_rate, | 69 new AlsaPcmOutputStream(device_name, format, channels, sample_rate, |
63 bits_per_sample, wrapper_.get(), this, | 70 bits_per_sample, wrapper_.get(), this, |
64 GetMessageLoop()); | 71 audio_thread_.message_loop()); |
65 | 72 |
66 AutoLock l(lock_); | 73 AutoLock l(lock_); |
67 active_streams_[stream] = scoped_refptr<AlsaPcmOutputStream>(stream); | 74 active_streams_[stream] = scoped_refptr<AlsaPcmOutputStream>(stream); |
68 return stream; | 75 return stream; |
69 } | 76 } |
70 | 77 |
71 AudioManagerLinux::AudioManagerLinux() { | 78 AudioManagerLinux::AudioManagerLinux() |
| 79 : audio_thread_("AudioThread"), |
| 80 initialized_(false) { |
72 } | 81 } |
73 | 82 |
74 AudioManagerLinux::~AudioManagerLinux() { | 83 AudioManagerLinux::~AudioManagerLinux() { |
75 // Make sure we stop the thread first. If we let the default destructor to | 84 // Make sure we stop the thread first. If we let the default destructor to |
76 // destruct the members, we may destroy audio streams before stopping the | 85 // destruct the members, we may destroy audio streams before stopping the |
77 // thread, resulting an unexpected behavior. | 86 // thread, resulting an unexpected behavior. |
78 // This way we make sure activities of the audio streams are all stopped | 87 // This way we make sure activities of the audio streams are all stopped |
79 // before we destroy them. | 88 // before we destroy them. |
80 audio_thread_.Stop(); | 89 audio_thread_.Stop(); |
81 active_streams_.clear(); | 90 active_streams_.clear(); |
82 } | 91 } |
83 | 92 |
84 void AudioManagerLinux::Init() { | 93 void AudioManagerLinux::Init() { |
85 AudioManagerBase::Init(); | 94 initialized_ = audio_thread_.Start(); |
86 wrapper_.reset(new AlsaWrapper()); | 95 wrapper_.reset(new AlsaWrapper()); |
87 } | 96 } |
88 | 97 |
89 void AudioManagerLinux::MuteAll() { | 98 void AudioManagerLinux::MuteAll() { |
90 NOTIMPLEMENTED(); | 99 NOTIMPLEMENTED(); |
91 } | 100 } |
92 | 101 |
93 void AudioManagerLinux::UnMuteAll() { | 102 void AudioManagerLinux::UnMuteAll() { |
94 NOTIMPLEMENTED(); | 103 NOTIMPLEMENTED(); |
95 } | 104 } |
96 | 105 |
97 void AudioManagerLinux::ReleaseOutputStream(AlsaPcmOutputStream* stream) { | 106 void AudioManagerLinux::ReleaseOutputStream(AlsaPcmOutputStream* stream) { |
98 if (stream) { | 107 if (stream) { |
99 AutoLock l(lock_); | 108 AutoLock l(lock_); |
100 active_streams_.erase(stream); | 109 active_streams_.erase(stream); |
101 } | 110 } |
102 } | 111 } |
103 | 112 |
104 // static | 113 // TODO(ajwong): Collapse this with the windows version. |
105 AudioManager* AudioManager::CreateAudioManager() { | 114 void DestroyAudioManagerLinux(void* not_used) { |
106 return new AudioManagerLinux(); | 115 delete g_audio_manager; |
| 116 g_audio_manager = NULL; |
107 } | 117 } |
| 118 |
| 119 AudioManager* AudioManager::GetAudioManager() { |
| 120 if (!g_audio_manager) { |
| 121 g_audio_manager = new AudioManagerLinux(); |
| 122 g_audio_manager->Init(); |
| 123 base::AtExitManager::RegisterCallback(&DestroyAudioManagerLinux, NULL); |
| 124 } |
| 125 return g_audio_manager; |
| 126 } |
OLD | NEW |