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_MAC_AUDIO_OUTPUT_MAC_H_
6 #define MEDIA_AUDIO_MAC_AUDIO_OUTPUT_MAC_H_
8 #include <AudioToolbox/AudioFormat.h>
9 #include <AudioToolbox/AudioQueue.h>
10 #include <AudioUnit/AudioUnit.h>
11 #include <CoreAudio/CoreAudio.h>
13 #include "base/compiler_specific.h"
14 #include "base/synchronization/lock.h"
15 #include "base/synchronization/waitable_event.h"
16 #include "media/audio/audio_io.h"
17 #include "media/audio/audio_parameters.h"
21 class AudioManagerMac
;
24 // Implementation of AudioOuputStream for Mac OS X using the audio queue service
25 // present in OS 10.5 and later. Audioqueue is the successor to the SoundManager
26 // services but it is supported in 64 bits.
27 class PCMQueueOutAudioOutputStream
: public AudioOutputStream
{
29 // The ctor takes all the usual parameters, plus |manager| which is the
30 // the audio manager who is creating this object.
31 PCMQueueOutAudioOutputStream(AudioManagerMac
* manager
,
32 const AudioParameters
& params
);
33 // The dtor is typically called by the AudioManager only and it is usually
34 // triggered by calling AudioOutputStream::Close().
35 virtual ~PCMQueueOutAudioOutputStream();
37 // Implementation of AudioOutputStream.
38 virtual bool Open() OVERRIDE
;
39 virtual void Close() OVERRIDE
;
40 virtual void Start(AudioSourceCallback
* callback
) OVERRIDE
;
41 virtual void Stop() OVERRIDE
;
42 virtual void SetVolume(double volume
) OVERRIDE
;
43 virtual void GetVolume(double* volume
) OVERRIDE
;
46 // The audio is double buffered.
47 static const uint32 kNumBuffers
= 2;
48 static const int kEmptyChannel
= -1;
50 // Reorder PCM from source layout to device layout found in Core Audio.
51 template<class Format
>
52 void SwizzleLayout(Format
* b
, uint32 filled
);
53 // Check and move channels if surround sound layout needs adjusted.
54 bool CheckForAdjustedLayout(Channels input_channel
, Channels output_channel
);
56 // The OS calls back here when an audio buffer has been processed.
57 static void RenderCallback(void* p_this
, AudioQueueRef queue
,
58 AudioQueueBufferRef buffer
);
59 // Called when an error occurs.
60 void HandleError(OSStatus err
);
62 // Atomic operations for setting/getting the source callback.
63 void SetSource(AudioSourceCallback
* source
);
64 AudioSourceCallback
* GetSource();
66 // Structure that holds the stream format details such as bitrate.
67 AudioStreamBasicDescription format_
;
68 // Handle to the OS audio queue object.
69 AudioQueueRef audio_queue_
;
70 // Array of pointers to the OS managed audio buffers.
71 AudioQueueBufferRef buffer_
[kNumBuffers
];
72 // Mutex for the |source_| to implment atomic set and get.
73 // It is important to NOT wait on any other locks while this is held.
74 base::Lock source_lock_
;
75 // Pointer to the object that will provide the audio samples.
76 AudioSourceCallback
* source_
;
77 // Our creator, the audio manager needs to be notified when we close.
78 AudioManagerMac
* manager_
;
79 // Packet size in bytes.
81 // Number of bytes for making a silence buffer.
83 // Volume level from 0 to 1.
85 // Number of bytes yet to be played in audio buffer.
86 uint32 pending_bytes_
;
87 // Number of channels in the source audio.
88 int num_source_channels_
;
89 // Source's channel layout for surround sound channels.
90 ChannelLayout source_layout_
;
91 // Device's channel layout.
92 int core_channel_orderings_
[CHANNELS_MAX
];
93 // An array for remapping source to device channel layouts during a swizzle.
94 int channel_remap_
[CHANNELS_MAX
];
95 // Number of channels in device layout.
96 int num_core_channels_
;
97 // A flag to determine if swizzle is needed from source to device layouts.
99 // A flag to determine if downmix is needed from source to device layouts.
100 bool should_down_mix_
;
102 // Event used for synchronization when stopping the stream.
103 // Callback sets it after stream is stopped.
104 base::WaitableEvent stopped_event_
;
105 // When stopping we keep track of number of buffers in flight and
106 // signal "stop completed" from the last buffer's callback.
107 int num_buffers_left_
;
109 // Container for retrieving data from AudioSourceCallback::OnMoreData().
110 scoped_ptr
<AudioBus
> audio_bus_
;
112 // Channel mixer and temporary bus for the final mixed channel data.
113 scoped_ptr
<ChannelMixer
> channel_mixer_
;
114 scoped_ptr
<AudioBus
> mixed_audio_bus_
;
116 DISALLOW_COPY_AND_ASSIGN(PCMQueueOutAudioOutputStream
);
121 #endif // MEDIA_AUDIO_MAC_AUDIO_OUTPUT_MAC_H_