1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef MEDIA_AUDIO_WIN_AUDIO_UNIFIED_WIN_H_
6 #define MEDIA_AUDIO_WIN_AUDIO_UNIFIED_WIN_H_
8 #include <Audioclient.h>
9 #include <MMDeviceAPI.h>
13 #include "base/compiler_specific.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/threading/platform_thread.h"
16 #include "base/threading/simple_thread.h"
17 #include "base/win/scoped_co_mem.h"
18 #include "base/win/scoped_comptr.h"
19 #include "base/win/scoped_handle.h"
20 #include "media/audio/audio_io.h"
21 #include "media/audio/audio_parameters.h"
22 #include "media/base/media_export.h"
26 class AudioManagerWin
;
28 // Implementation of AudioOutputStream for Windows using the Core Audio API
29 // where both capturing and rendering takes place on the same thread to enable
32 // The user should also ensure that audio I/O is supported by calling
33 // HasUnifiedDefaultIO().
35 // Implementation notes:
37 // - Certain conditions must be fulfilled to support audio I/O:
38 // o Both capture and render side must use the same sample rate.
39 // o Both capture and render side must use the same channel count.
40 // o Both capture and render side must use the same channel configuration.
41 // o See HasUnifiedDefaultIO() for more details.
45 // - Add support for exclusive mode.
46 // - Add multi-channel support.
47 // - Add support for non-matching sample rates.
49 class MEDIA_EXPORT WASAPIUnifiedStream
50 : public AudioOutputStream
,
51 public base::DelegateSimpleThread::Delegate
{
53 // The ctor takes all the usual parameters, plus |manager| which is the
54 // the audio manager who is creating this object.
55 WASAPIUnifiedStream(AudioManagerWin
* manager
,
56 const AudioParameters
& params
);
58 // The dtor is typically called by the AudioManager only and it is usually
59 // triggered by calling AudioOutputStream::Close().
60 virtual ~WASAPIUnifiedStream();
62 // Implementation of AudioOutputStream.
63 virtual bool Open() OVERRIDE
;
64 virtual void Start(AudioSourceCallback
* callback
) OVERRIDE
;
65 virtual void Stop() OVERRIDE
;
66 virtual void Close() OVERRIDE
;
67 virtual void SetVolume(double volume
) OVERRIDE
;
68 virtual void GetVolume(double* volume
) OVERRIDE
;
70 // Returns true if all conditions to support audio IO are fulfilled.
71 // Input and output sides of the Audio Engine must use the same native
72 // device period (requires e.g. identical sample rates) and have the same
74 static bool HasUnifiedDefaultIO();
76 bool started() const {
77 return audio_io_thread_
.get() != NULL
;
81 // DelegateSimpleThread::Delegate implementation.
82 virtual void Run() OVERRIDE
;
84 // Issues the OnError() callback to the |source_|.
85 void HandleError(HRESULT err
);
87 // Stops and joins the audio thread in case of an error.
88 void StopAndJoinThread(HRESULT err
);
90 // Converts unique endpoint ID to user-friendly device name.
91 std::string
GetDeviceName(LPCWSTR device_id
) const;
93 // Returns the number of channels the audio engine uses for its internal
94 // processing/mixing of shared-mode streams for the default endpoint device.
95 int endpoint_channel_count() { return format_
.Format
.nChannels
; }
97 // Contains the thread ID of the creating thread.
98 base::PlatformThreadId creating_thread_id_
;
100 // Our creator, the audio manager needs to be notified when we close.
101 AudioManagerWin
* manager_
;
103 // The sharing mode for the streams.
104 // Valid values are AUDCLNT_SHAREMODE_SHARED and AUDCLNT_SHAREMODE_EXCLUSIVE
105 // where AUDCLNT_SHAREMODE_SHARED is the default.
106 AUDCLNT_SHAREMODE share_mode_
;
108 // Rendering and capturing is driven by this thread (no message loop).
109 // All OnMoreIOData() callbacks will be called from this thread.
110 scoped_ptr
<base::DelegateSimpleThread
> audio_io_thread_
;
112 // Contains the desired audio format which is set up at construction.
113 // Extended PCM waveform format structure based on WAVEFORMATEXTENSIBLE.
114 // Use this for multiple channel and hi-resolution PCM data.
115 WAVEFORMATPCMEX format_
;
117 // True when successfully opened.
120 // Volume level from 0 to 1.
123 // Size in bytes of each audio frame (4 bytes for 16-bit stereo PCM).
126 // Size in audio frames of each audio packet where an audio packet
127 // is defined as the block of data which the source is expected to deliver
128 // in each OnMoreIOData() callback.
129 size_t packet_size_frames_
;
131 // Length of the audio endpoint buffer.
132 uint32 endpoint_render_buffer_size_frames_
;
133 uint32 endpoint_capture_buffer_size_frames_
;
135 // Counts the number of audio frames written to the endpoint buffer.
136 uint64 num_written_frames_
;
138 // Time stamp for last delay measurement.
139 base::TimeTicks last_delay_sample_time_
;
141 // Contains the total (sum of render and capture) delay in milliseconds.
142 double total_delay_ms_
;
144 // Pointer to the client that will deliver audio samples to be played out.
145 AudioSourceCallback
* source_
;
147 // IMMDevice interfaces which represents audio endpoint devices.
148 base::win::ScopedComPtr
<IMMDevice
> endpoint_render_device_
;
149 base::win::ScopedComPtr
<IMMDevice
> endpoint_capture_device_
;
151 // IAudioClient interfaces which enables a client to create and initialize
152 // an audio stream between an audio application and the audio engine.
153 base::win::ScopedComPtr
<IAudioClient
> audio_output_client_
;
154 base::win::ScopedComPtr
<IAudioClient
> audio_input_client_
;
156 // IAudioRenderClient interfaces enables a client to write output
157 // data to a rendering endpoint buffer.
158 base::win::ScopedComPtr
<IAudioRenderClient
> audio_render_client_
;
160 // IAudioCaptureClient interfaces enables a client to read input
161 // data from a capturing endpoint buffer.
162 base::win::ScopedComPtr
<IAudioCaptureClient
> audio_capture_client_
;
164 // The audio engine will signal this event each time a buffer has been
166 base::win::ScopedHandle capture_event_
;
168 // This event will be signaled when streaming shall stop.
169 base::win::ScopedHandle stop_streaming_event_
;
171 // Container for retrieving data from AudioSourceCallback::OnMoreIOData().
172 scoped_ptr
<AudioBus
> render_bus_
;
174 // Container for sending data to AudioSourceCallback::OnMoreIOData().
175 scoped_ptr
<AudioBus
> capture_bus_
;
177 DISALLOW_COPY_AND_ASSIGN(WASAPIUnifiedStream
);
182 #endif // MEDIA_AUDIO_WIN_AUDIO_UNIFIED_WIN_H_