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

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

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

Powered by Google App Engine
This is Rietveld 408576698