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

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 <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/ref_counted.h"
15 #include "base/memory/scoped_ptr.h"
16 #include "base/synchronization/lock.h"
14 #include "base/win/scoped_handle.h" 17 #include "base/win/scoped_handle.h"
15 #include "media/audio/audio_io.h" 18 #include "media/audio/audio_io.h"
16 #include "media/audio/audio_parameters.h" 19 #include "media/audio/audio_parameters.h"
17 20
18 class AudioManagerWin; 21 class AudioManagerWin;
19 22
20 // Implements PCM audio output support for Windows using the WaveXXX API. 23 // 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 24 // 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 25 // operating systems regardless or DirectX version installed. It is known that
23 // in some machines WaveXXX based audio is better while in others DirectSound 26 // in some machines WaveXXX based audio is better while in others DirectSound
24 // is better. 27 // is better.
25 // 28 //
26 // Important: the OnXXXX functions in AudioSourceCallback are called by more 29 // 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 30 // than one thread so it is important to have some form of synchronization if
28 // you are keeping state in it. 31 // you are keeping state in it.
29 class PCMWaveOutAudioOutputStream : public AudioOutputStream { 32 class PCMWaveOutAudioOutputStream : public AudioOutputStream {
33 friend class PlayingObject;
34
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:
52 enum State { 58 enum State {
53 PCMA_BRAND_NEW, // Initial state. 59 PCMA_BRAND_NEW, // Initial state.
54 PCMA_READY, // Device obtained and ready to play. 60 PCMA_READY, // Device obtained and ready to play.
55 PCMA_PLAYING, // Playing audio. 61 PCMA_PLAYING, // Playing audio.
56 PCMA_CLOSED // Device has been released. 62 PCMA_CLOSED // Device has been released.
57 }; 63 };
58 64
59 // Windows calls us back to feed more data to the device here. See msdn 65 // Internal reference-counted class used to speed up stopping of audio stream.
60 // documentation for 'waveOutProc' for details about the parameters. 66 // By creating that separate object and passing it as argument we can stop
67 // playing immediately, not waiting till all scheduled callbacks finish.
68 // It is reference-counted, so it would be eventualy deleted.
69 class PlayingObject :
70 public base::RefCountedThreadSafe<PlayingObject> {
71 public:
72 // Accessors.
73 base::Lock* lock() {
74 return &lock_;
75 }
76 bool playing() const {
77 return stream_ != NULL;
tommi (sloooow) - chröme 2011/11/19 17:23:27 should we lock lock_ here and in stop_playing() or
78 }
79 void stop_playing() {
80 stream_ = NULL;
81 }
82
83 PlayingObject(PCMWaveOutAudioOutputStream *stream);
tommi (sloooow) - chröme 2011/11/19 17:23:27 PCMWaveOutAudioOutputStream* stream
84 ~PlayingObject();
85 // Feed the buffer to waveOut.
86 // Called on the audio manager thread.
87 void FeedBuffer(WAVEHDR* buffer, HWAVEOUT hwo);
88
89 private:
90 // Lock used to avoid the conflict between filling audio buffers and
91 // stopping the stream.
92 base::Lock lock_;
93 // Pointer to the parent object. Not owned.
94 PCMWaveOutAudioOutputStream *stream_;
tommi (sloooow) - chröme 2011/11/19 17:23:27 PCMWaveOutAudioOutputStream* stream_;
95 };
96
97 // Returns pointer to the n-th buffer.
98 inline WAVEHDR* GetBuffer(int n) const;
99
100 // Size of one buffer in bytes, rounded up if necessary.
101 inline size_t BufferSize() const;
102
103 // Windows calls us back to free the buffer. See msdn documentation for
104 // 'waveOutProc' for details about the parameters.
61 static void CALLBACK WaveCallback(HWAVEOUT hwo, UINT msg, DWORD_PTR instance, 105 static void CALLBACK WaveCallback(HWAVEOUT hwo, UINT msg, DWORD_PTR instance,
62 DWORD_PTR param1, DWORD_PTR param2); 106 DWORD_PTR param1, DWORD_PTR param2);
63 107
64 // If windows reports an error this function handles it and passes it to 108 // If windows reports an error this function handles it and passes it to
65 // the attached AudioSourceCallback::OnError(). 109 // the attached AudioSourceCallback::OnError().
66 void HandleError(MMRESULT error); 110 void HandleError(MMRESULT error);
67 // Allocates and prepares the memory that will be used for playback. Only 111
68 // two buffers are created. 112 // Allocates and prepares the memory that will be used for playback.
69 void SetupBuffers(); 113 void SetupBuffers();
114
70 // Deallocates the memory allocated in SetupBuffers. 115 // Deallocates the memory allocated in SetupBuffers.
71 void FreeBuffers(); 116 void FreeBuffers();
72 117
73 // Reader beware. Visual C has stronger guarantees on volatile vars than 118 // Reader beware. Visual C has stronger guarantees on volatile vars than
74 // most people expect. In fact, it has release semantics on write and 119 // most people expect. In fact, it has release semantics on write and
75 // acquire semantics on reads. See the msdn documentation. 120 // acquire semantics on reads. See the msdn documentation.
76 volatile State state_; 121 volatile State state_;
77 122
78 // The audio manager that created this output stream. We notify it when 123 // The audio manager that created this output stream. We notify it when
79 // we close so it can release its own resources. 124 // we close so it can release its own resources.
(...skipping 20 matching lines...) Expand all
100 // The id assigned by the operating system to the selected wave output 145 // The id assigned by the operating system to the selected wave output
101 // hardware device. Usually this is just -1 which means 'default device'. 146 // hardware device. Usually this is just -1 which means 'default device'.
102 UINT device_id_; 147 UINT device_id_;
103 148
104 // Windows native structure to encode the format parameters. 149 // Windows native structure to encode the format parameters.
105 WAVEFORMATPCMEX format_; 150 WAVEFORMATPCMEX format_;
106 151
107 // Handle to the instance of the wave device. 152 // Handle to the instance of the wave device.
108 HWAVEOUT waveout_; 153 HWAVEOUT waveout_;
109 154
110 // Pointer to the first allocated audio buffer. This object owns it. 155 // Pointer to the allocated audio buffers, we allocate all buffers in one big
111 WAVEHDR* buffer_; 156 // chunk. This object owns them.
157 scoped_array<char> buffers_;
112 158
113 // Lock used to prevent stopping the hardware callback thread while it is 159 // Infamous aux object.
tommi (sloooow) - chröme 2011/11/19 17:23:27 hah!
114 // pending for data or feeding it to audio driver, because doing that causes 160 scoped_refptr<PlayingObject> playing_object_;
115 // the deadlock. Main thread gets that lock before stopping the playback.
116 // Callback tries to acquire that lock before entering critical code. If
117 // acquire fails then main thread is stopping the playback, callback should
118 // immediately return.
119 // Use Windows-specific lock, not Chrome one, because there is limited set of
120 // functions callback can use.
121 CRITICAL_SECTION lock_;
122 161
123 DISALLOW_COPY_AND_ASSIGN(PCMWaveOutAudioOutputStream); 162 DISALLOW_COPY_AND_ASSIGN(PCMWaveOutAudioOutputStream);
124 }; 163 };
125 164
126 #endif // MEDIA_AUDIO_WIN_WAVEOUT_OUTPUT_WIN_H_ 165 #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