OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #ifndef MEDIA_AUDIO_WIN_WAVEOUT_OUTPUT_WIN_H_ | 5 #ifndef MEDIA_AUDIO_WIN_WAVEOUT_OUTPUT_WIN_H_ |
6 #define MEDIA_AUDIO_WIN_WAVEOUT_OUTPUT_WIN_H_ | 6 #define MEDIA_AUDIO_WIN_WAVEOUT_OUTPUT_WIN_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <windows.h> | 9 #include <windows.h> |
10 #include <mmsystem.h> | 10 #include <mmsystem.h> |
11 #include <mmreg.h> | 11 #include <mmreg.h> |
12 | 12 |
13 #include "base/basictypes.h" | 13 #include "base/basictypes.h" |
| 14 #include "base/memory/scoped_ptr.h" |
| 15 #include "base/synchronization/lock.h" |
14 #include "base/win/scoped_handle.h" | 16 #include "base/win/scoped_handle.h" |
15 #include "media/audio/audio_io.h" | 17 #include "media/audio/audio_io.h" |
16 #include "media/audio/audio_parameters.h" | 18 #include "media/audio/audio_parameters.h" |
17 | 19 |
18 class AudioManagerWin; | 20 class AudioManagerWin; |
19 | 21 |
20 // Implements PCM audio output support for Windows using the WaveXXX API. | 22 // Implements PCM audio output support for Windows using the WaveXXX API. |
21 // While not as nice as the DirectSound-based API, it should work in all target | 23 // While not as nice as the DirectSound-based API, it should work in all target |
22 // operating systems regardless or DirectX version installed. It is known that | 24 // operating systems regardless or DirectX version installed. It is known that |
23 // in some machines WaveXXX based audio is better while in others DirectSound | 25 // in some machines WaveXXX based audio is better while in others DirectSound |
24 // is better. | 26 // is better. |
25 // | 27 // |
26 // Important: the OnXXXX functions in AudioSourceCallback are called by more | 28 // Important: the OnXXXX functions in AudioSourceCallback are called by more |
27 // than one thread so it is important to have some form of synchronization if | 29 // than one thread so it is important to have some form of synchronization if |
28 // you are keeping state in it. | 30 // you are keeping state in it. |
29 class PCMWaveOutAudioOutputStream : public AudioOutputStream { | 31 class PCMWaveOutAudioOutputStream : public AudioOutputStream { |
30 public: | 32 public: |
31 // The ctor takes all the usual parameters, plus |manager| which is the | 33 // The ctor takes all the usual parameters, plus |manager| which is the the |
32 // the audio manager who is creating this object and |device_id| which | 34 // audio manager who is creating this object and |device_id| which is provided |
33 // is provided by the operating system. | 35 // by the operating system. |
34 PCMWaveOutAudioOutputStream(AudioManagerWin* manager, | 36 PCMWaveOutAudioOutputStream(AudioManagerWin* manager, |
35 const AudioParameters& params, | 37 const AudioParameters& params, |
36 int num_buffers, | 38 int num_buffers, |
37 UINT device_id); | 39 UINT device_id); |
38 virtual ~PCMWaveOutAudioOutputStream(); | 40 virtual ~PCMWaveOutAudioOutputStream(); |
39 | 41 |
40 // Implementation of AudioOutputStream. | 42 // Implementation of AudioOutputStream. |
41 virtual bool Open(); | 43 virtual bool Open(); |
42 virtual void Close(); | 44 virtual void Close(); |
43 virtual void Start(AudioSourceCallback* callback); | 45 virtual void Start(AudioSourceCallback* callback); |
44 virtual void Stop(); | 46 virtual void Stop(); |
45 virtual void SetVolume(double volume); | 47 virtual void SetVolume(double volume); |
46 virtual void GetVolume(double* volume); | 48 virtual void GetVolume(double* volume); |
47 | 49 |
48 // Sends a buffer to the audio driver for playback. | 50 // Sends a buffer to the audio driver for playback. |
49 void QueueNextPacket(WAVEHDR* buffer); | 51 void QueueNextPacket(WAVEHDR* buffer); |
50 | 52 |
51 private: | 53 private: |
52 enum State { | 54 enum State { |
53 PCMA_BRAND_NEW, // Initial state. | 55 PCMA_BRAND_NEW, // Initial state. |
54 PCMA_READY, // Device obtained and ready to play. | 56 PCMA_READY, // Device obtained and ready to play. |
55 PCMA_PLAYING, // Playing audio. | 57 PCMA_PLAYING, // Playing audio. |
| 58 PCMA_STOPPING, // Audio is stopping, do not "feed" data to Windows. |
56 PCMA_CLOSED // Device has been released. | 59 PCMA_CLOSED // Device has been released. |
57 }; | 60 }; |
58 | 61 |
59 // Windows calls us back to feed more data to the device here. See msdn | 62 // Returns pointer to the n-th buffer. |
60 // documentation for 'waveOutProc' for details about the parameters. | 63 inline WAVEHDR* GetBuffer(int n) const; |
61 static void CALLBACK WaveCallback(HWAVEOUT hwo, UINT msg, DWORD_PTR instance, | 64 |
62 DWORD_PTR param1, DWORD_PTR param2); | 65 // Size of one buffer in bytes, rounded up if necessary. |
| 66 inline size_t BufferSize() const; |
| 67 |
| 68 // Windows calls us back asking for more data when buffer_event_ signalled. |
| 69 // See MSDN for help on RegisterWaitForSingleObject() and waveOutOpen(). |
| 70 static void NTAPI BufferCallback(PVOID lpParameter, BOOLEAN timer_fired); |
63 | 71 |
64 // If windows reports an error this function handles it and passes it to | 72 // If windows reports an error this function handles it and passes it to |
65 // the attached AudioSourceCallback::OnError(). | 73 // the attached AudioSourceCallback::OnError(). |
66 void HandleError(MMRESULT error); | 74 void HandleError(MMRESULT error); |
67 // Allocates and prepares the memory that will be used for playback. Only | 75 |
68 // two buffers are created. | 76 // Allocates and prepares the memory that will be used for playback. |
69 void SetupBuffers(); | 77 void SetupBuffers(); |
| 78 |
70 // Deallocates the memory allocated in SetupBuffers. | 79 // Deallocates the memory allocated in SetupBuffers. |
71 void FreeBuffers(); | 80 void FreeBuffers(); |
72 | 81 |
73 // Reader beware. Visual C has stronger guarantees on volatile vars than | 82 // Reader beware. Visual C has stronger guarantees on volatile vars than |
74 // most people expect. In fact, it has release semantics on write and | 83 // most people expect. In fact, it has release semantics on write and |
75 // acquire semantics on reads. See the msdn documentation. | 84 // acquire semantics on reads. See the msdn documentation. |
76 volatile State state_; | 85 volatile State state_; |
77 | 86 |
78 // The audio manager that created this output stream. We notify it when | 87 // The audio manager that created this output stream. We notify it when |
79 // we close so it can release its own resources. | 88 // we close so it can release its own resources. |
(...skipping 20 matching lines...) Expand all Loading... |
100 // The id assigned by the operating system to the selected wave output | 109 // The id assigned by the operating system to the selected wave output |
101 // hardware device. Usually this is just -1 which means 'default device'. | 110 // hardware device. Usually this is just -1 which means 'default device'. |
102 UINT device_id_; | 111 UINT device_id_; |
103 | 112 |
104 // Windows native structure to encode the format parameters. | 113 // Windows native structure to encode the format parameters. |
105 WAVEFORMATPCMEX format_; | 114 WAVEFORMATPCMEX format_; |
106 | 115 |
107 // Handle to the instance of the wave device. | 116 // Handle to the instance of the wave device. |
108 HWAVEOUT waveout_; | 117 HWAVEOUT waveout_; |
109 | 118 |
110 // Pointer to the first allocated audio buffer. This object owns it. | 119 // Handle to the buffer event. |
111 WAVEHDR* buffer_; | 120 base::win::ScopedHandle buffer_event_; |
112 | 121 |
113 // Lock used to prevent stopping the hardware callback thread while it is | 122 // Handle returned by RegisterWaitForSingleObject(). |
114 // pending for data or feeding it to audio driver, because doing that causes | 123 base::win::ScopedHandle waiting_handle_; |
115 // the deadlock. Main thread gets that lock before stopping the playback. | 124 |
116 // Callback tries to acquire that lock before entering critical code. If | 125 // Pointer to the allocated audio buffers, we allocate all buffers in one big |
117 // acquire fails then main thread is stopping the playback, callback should | 126 // chunk. This object owns them. |
118 // immediately return. | 127 scoped_array<char> buffers_; |
119 // Use Windows-specific lock, not Chrome one, because there is limited set of | 128 |
120 // functions callback can use. | 129 // Lock used to avoid the conflict when callbacks are called simultaneously. |
121 CRITICAL_SECTION lock_; | 130 base::Lock lock_; |
122 | 131 |
123 DISALLOW_COPY_AND_ASSIGN(PCMWaveOutAudioOutputStream); | 132 DISALLOW_COPY_AND_ASSIGN(PCMWaveOutAudioOutputStream); |
124 }; | 133 }; |
125 | 134 |
126 #endif // MEDIA_AUDIO_WIN_WAVEOUT_OUTPUT_WIN_H_ | 135 #endif // MEDIA_AUDIO_WIN_WAVEOUT_OUTPUT_WIN_H_ |
OLD | NEW |