Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(284)

Side by Side Diff: media/audio/win/waveout_output_win.h

Issue 8591028: Change the way we are sending audio data to driver when using WaveOut API. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <map>
tommi (sloooow) - chröme 2011/11/18 09:53:21 os includes first
enal1 2011/11/19 00:59:19 Done.
10
9 #include <windows.h> 11 #include <windows.h>
10 #include <mmsystem.h> 12 #include <mmsystem.h>
11 #include <mmreg.h> 13 #include <mmreg.h>
12 14
13 #include "base/basictypes.h" 15 #include "base/basictypes.h"
16 #include "base/logging.h"
17 #include "base/memory/scoped_ptr.h"
18 #include "base/synchronization/lock.h"
14 #include "base/win/scoped_handle.h" 19 #include "base/win/scoped_handle.h"
15 #include "media/audio/audio_io.h" 20 #include "media/audio/audio_io.h"
16 #include "media/audio/audio_parameters.h" 21 #include "media/audio/audio_parameters.h"
17 22
18 class AudioManagerWin; 23 class AudioManagerWin;
19 24
20 // Implements PCM audio output support for Windows using the WaveXXX API. 25 // 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 26 // 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 27 // operating systems regardless or DirectX version installed. It is known that
23 // in some machines WaveXXX based audio is better while in others DirectSound 28 // in some machines WaveXXX based audio is better while in others DirectSound
24 // is better. 29 // is better.
25 // 30 //
26 // Important: the OnXXXX functions in AudioSourceCallback are called by more 31 // 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 32 // than one thread so it is important to have some form of synchronization if
28 // you are keeping state in it. 33 // you are keeping state in it.
29 class PCMWaveOutAudioOutputStream : public AudioOutputStream { 34 class PCMWaveOutAudioOutputStream : public AudioOutputStream {
30 public: 35 public:
31 // The ctor takes all the usual parameters, plus |manager| which is the 36 // The ctor takes all the usual parameters, plus |manager| which is the
32 // the audio manager who is creating this object and |device_id| which 37 // the audio manager who is creating this object and on which message loop
33 // is provided by the operating system. 38 // "feeder" callbacks are running, and |device_id| which is provided by the
39 // operating system.
34 PCMWaveOutAudioOutputStream(AudioManagerWin* manager, 40 PCMWaveOutAudioOutputStream(AudioManagerWin* manager,
35 const AudioParameters& params, 41 const AudioParameters& params,
36 int num_buffers, 42 int num_buffers,
37 UINT device_id); 43 UINT device_id);
38 virtual ~PCMWaveOutAudioOutputStream(); 44 virtual ~PCMWaveOutAudioOutputStream();
39 45
40 // Implementation of AudioOutputStream. 46 // Implementation of AudioOutputStream.
41 virtual bool Open(); 47 virtual bool Open();
42 virtual void Close(); 48 virtual void Close();
43 virtual void Start(AudioSourceCallback* callback); 49 virtual void Start(AudioSourceCallback* callback);
44 virtual void Stop(); 50 virtual void Stop();
45 virtual void SetVolume(double volume); 51 virtual void SetVolume(double volume);
46 virtual void GetVolume(double* volume); 52 virtual void GetVolume(double* volume);
47 53
48 // Sends a buffer to the audio driver for playback. 54 // Sends a buffer to the audio driver for playback.
49 void QueueNextPacket(WAVEHDR* buffer); 55 void QueueNextPacket(WAVEHDR* buffer);
50 56
51 private: 57 private:
58 // Returns pointer to the n-th buffer.
59 WAVEHDR* GetBuffer(int n) const {
tommi (sloooow) - chröme 2011/11/18 09:53:21 nice. you could move this to the cc file to avoid
enal1 2011/11/19 00:59:19 Done.
60 DCHECK_GE(n, 0);
61 DCHECK_LT(n, num_buffers_);
62 return reinterpret_cast<WAVEHDR*>(buffers_ + n * CbBuffer());
63 }
64
52 enum State { 65 enum State {
53 PCMA_BRAND_NEW, // Initial state. 66 PCMA_BRAND_NEW, // Initial state.
54 PCMA_READY, // Device obtained and ready to play. 67 PCMA_READY, // Device obtained and ready to play.
55 PCMA_PLAYING, // Playing audio. 68 PCMA_PLAYING, // Playing audio.
69 PCMA_STOPPING, // Stopping the audio.
56 PCMA_CLOSED // Device has been released. 70 PCMA_CLOSED // Device has been released.
57 }; 71 };
58 72
59 // Windows calls us back to feed more data to the device here. See msdn 73 // Size of one buffer in bytes, rounded up to the nearest 16 bytes.
60 // documentation for 'waveOutProc' for details about the parameters. 74 DWORD CbBuffer() const {
tommi (sloooow) - chröme 2011/11/18 09:53:21 CbBuffer -> BufferSize Also, return size_t?
enal1 2011/11/19 00:59:19 Done.
75 return (sizeof(WAVEHDR) + buffer_size_ + 15u) & static_cast<DWORD>(-16);
tommi (sloooow) - chröme 2011/11/18 09:53:21 instead of static_cast<DWORD>(-16), can you just u
enal1 2011/11/19 00:59:19 In general, 0xFFFFFFF0 is bad -- it can become a p
76 }
77
78 // Windows calls us back to free the buffer. See msdn documentation for
79 // 'waveOutProc' for details about the parameters.
61 static void CALLBACK WaveCallback(HWAVEOUT hwo, UINT msg, DWORD_PTR instance, 80 static void CALLBACK WaveCallback(HWAVEOUT hwo, UINT msg, DWORD_PTR instance,
62 DWORD_PTR param1, DWORD_PTR param2); 81 DWORD_PTR param1, DWORD_PTR param2);
63 82
83 // Feed the buffer to waveOut.
84 // Called on the audio manager thread.
85 static void FeedBuffer(PCMWaveOutAudioOutputStream* stream,
86 WAVEHDR* buffer,
87 HWAVEOUT hwo,
88 DWORD ordinal);
89
64 // If windows reports an error this function handles it and passes it to 90 // If windows reports an error this function handles it and passes it to
65 // the attached AudioSourceCallback::OnError(). 91 // the attached AudioSourceCallback::OnError().
66 void HandleError(MMRESULT error); 92 void HandleError(MMRESULT error);
67 // Allocates and prepares the memory that will be used for playback. Only 93 // Allocates and prepares the memory that will be used for playback.
tommi (sloooow) - chröme 2011/11/18 09:53:21 add an empty line above this one. same throughout
enal1 2011/11/19 00:59:19 Done.
68 // two buffers are created.
69 void SetupBuffers(); 94 void SetupBuffers();
70 // Deallocates the memory allocated in SetupBuffers. 95 // Deallocates the memory allocated in SetupBuffers.
71 void FreeBuffers(); 96 void FreeBuffers();
97 // Globals-related functions.
tommi (sloooow) - chröme 2011/11/18 09:53:21 This comment isn't very useful. Can you document
enal1 2011/11/19 00:59:19 Done.
98 static void InitializeGlobalsIfNecessary();
99 static void CleanupGlobals(void* not_used);
100
101 // Static lock guarding access to globals.
102 // We are not doing any expensive operations under this lock --
103 // adding/searching/removing object to/in/from map of all live objects, and
104 // trying to acquire object lock, so hopefully we can live with one global
105 // lock for all audio streams. If contention ever becomes the problem we
106 // can use hash of stream address to use one of several global locks.
107 static base::Lock g_lock_;
tommi (sloooow) - chröme 2011/11/18 09:53:21 We should avoid global objects that require constr
enal1 2011/11/19 00:59:19 Done.
108
109 // When playing, every instance of our audio stream has associated ordinal.
110 // Callback that feeds the audio thread verifies that stream is still alive
111 // by looking stream address and its ordinal in the map of all streams.
tommi (sloooow) - chröme 2011/11/18 09:53:21 by looking stream address -> by looking at the str
enal1 2011/11/19 00:59:19 Done.
112 // Stream is not touched before check is complete. This way we don't
tommi (sloooow) - chröme 2011/11/18 09:53:21 "Stream is not touched before check is complete"
enal1 2011/11/19 00:59:19 Done.
113 // have to wait for all scheduled callbacks to finish when stopping audio
114 // stream. Ordinal is needed because audio stream can be deleted and new one
115 // with same address created.
116 // Ordinal overflow would be a problem only if some scheduled feeder callback
117 // would not run before 4.2*10**9 play() calls are executed, impossible for
118 // now...
119 static volatile DWORD g_ordinal_;
120
121 // Map of all live streams with their ordinals.
122 static scoped_ptr<std::map<PCMWaveOutAudioOutputStream*, DWORD> >
123 g_streams_map_;
72 124
73 // Reader beware. Visual C has stronger guarantees on volatile vars than 125 // Reader beware. Visual C has stronger guarantees on volatile vars than
74 // most people expect. In fact, it has release semantics on write and 126 // most people expect. In fact, it has release semantics on write and
75 // acquire semantics on reads. See the msdn documentation. 127 // acquire semantics on reads. See the msdn documentation.
76 volatile State state_; 128 volatile State state_;
77 129
78 // The audio manager that created this output stream. We notify it when 130 // The audio manager that created this output stream. We notify it when
79 // we close so it can release its own resources. 131 // we close so it can release its own resources.
80 AudioManagerWin* manager_; 132 AudioManagerWin* manager_;
81 133
(...skipping 18 matching lines...) Expand all
100 // The id assigned by the operating system to the selected wave output 152 // The id assigned by the operating system to the selected wave output
101 // hardware device. Usually this is just -1 which means 'default device'. 153 // hardware device. Usually this is just -1 which means 'default device'.
102 UINT device_id_; 154 UINT device_id_;
103 155
104 // Windows native structure to encode the format parameters. 156 // Windows native structure to encode the format parameters.
105 WAVEFORMATPCMEX format_; 157 WAVEFORMATPCMEX format_;
106 158
107 // Handle to the instance of the wave device. 159 // Handle to the instance of the wave device.
108 HWAVEOUT waveout_; 160 HWAVEOUT waveout_;
109 161
110 // Pointer to the first allocated audio buffer. This object owns it. 162 // Pointer to the allocated audio buffers, we allocate all buffers in one big
111 WAVEHDR* buffer_; 163 // chunk. Stream owns them.
tommi (sloooow) - chröme 2011/11/18 09:53:21 I prefer "This object owns it" to "Stream owns the
164 char* buffers_;
112 165
113 // Lock used to prevent stopping the hardware callback thread while it is 166 // Lock used to avoid the conflict between filling audio buffers and
114 // pending for data or feeding it to audio driver, because doing that causes 167 // stopping the stream.
115 // the deadlock. Main thread gets that lock before stopping the playback. 168 base::Lock lock_;
116 // Callback tries to acquire that lock before entering critical code. If 169
117 // acquire fails then main thread is stopping the playback, callback should 170 // Ordinal of current stream when playing.
118 // immediately return. 171 DWORD ordinal_;
119 // Use Windows-specific lock, not Chrome one, because there is limited set of
120 // functions callback can use.
121 CRITICAL_SECTION lock_;
122 172
123 DISALLOW_COPY_AND_ASSIGN(PCMWaveOutAudioOutputStream); 173 DISALLOW_COPY_AND_ASSIGN(PCMWaveOutAudioOutputStream);
124 }; 174 };
125 175
126 #endif // MEDIA_AUDIO_WIN_WAVEOUT_OUTPUT_WIN_H_ 176 #endif // MEDIA_AUDIO_WIN_WAVEOUT_OUTPUT_WIN_H_
OLDNEW
« no previous file with comments | « no previous file | media/audio/win/waveout_output_win.cc » ('j') | media/audio/win/waveout_output_win.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698