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 // AudioOutputDispatcherImpl is an implementation of AudioOutputDispatcher.
7 // To avoid opening and closing audio devices more frequently than necessary,
8 // each dispatcher has a pool of inactive physical streams. A stream is closed
9 // only if it hasn't been used for a certain period of time (specified via the
13 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_
14 #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_
19 #include "base/basictypes.h"
20 #include "base/memory/ref_counted.h"
21 #include "base/memory/weak_ptr.h"
22 #include "base/timer/timer.h"
23 #include "media/audio/audio_io.h"
24 #include "media/audio/audio_manager.h"
25 #include "media/audio/audio_output_dispatcher.h"
26 #include "media/audio/audio_parameters.h"
30 class AudioOutputProxy
;
32 class MEDIA_EXPORT AudioOutputDispatcherImpl
: public AudioOutputDispatcher
{
34 // |close_delay_ms| specifies delay after the stream is paused until
35 // the audio device is closed.
36 AudioOutputDispatcherImpl(AudioManager
* audio_manager
,
37 const AudioParameters
& params
,
38 const std::string
& input_device_id
,
39 const base::TimeDelta
& close_delay
);
41 // Opens a new physical stream if there are no pending streams in
42 // |idle_streams_|. Do not call Close() or Stop() if this method fails.
43 virtual bool OpenStream() OVERRIDE
;
45 // If there are pending streams in |idle_streams_| then it reuses one of
46 // them, otherwise creates a new one.
47 virtual bool StartStream(AudioOutputStream::AudioSourceCallback
* callback
,
48 AudioOutputProxy
* stream_proxy
) OVERRIDE
;
50 // Holds the physical stream temporarily in |pausing_streams_| and then
51 // |stream| is added to the pool of pending streams (i.e. |idle_streams_|).
52 virtual void StopStream(AudioOutputProxy
* stream_proxy
) OVERRIDE
;
54 virtual void StreamVolumeSet(AudioOutputProxy
* stream_proxy
,
55 double volume
) OVERRIDE
;
57 virtual void CloseStream(AudioOutputProxy
* stream_proxy
) OVERRIDE
;
59 virtual void Shutdown() OVERRIDE
;
62 typedef std::map
<AudioOutputProxy
*, AudioOutputStream
*> AudioStreamMap
;
63 friend class base::RefCountedThreadSafe
<AudioOutputDispatcherImpl
>;
64 virtual ~AudioOutputDispatcherImpl();
66 friend class AudioOutputProxyTest
;
68 // Creates a new physical output stream, opens it and pushes to
69 // |idle_streams_|. Returns false if the stream couldn't be created or
71 bool CreateAndOpenStream();
73 // A task scheduled by StartStream(). Opens a new stream and puts
74 // it in |idle_streams_|.
77 // Before a stream is reused, it should sit idle for a bit. This task is
78 // called once that time has elapsed.
79 void StopStreamTask();
81 // Called by |close_timer_|. Closes all pending streams.
82 void ClosePendingStreams();
84 base::TimeDelta pause_delay_
;
85 size_t paused_proxies_
;
86 typedef std::list
<AudioOutputStream
*> AudioOutputStreamList
;
87 AudioOutputStreamList idle_streams_
;
88 AudioOutputStreamList pausing_streams_
;
90 // Used to post delayed tasks to ourselves that we cancel inside Shutdown().
91 base::WeakPtrFactory
<AudioOutputDispatcherImpl
> weak_this_
;
92 base::DelayTimer
<AudioOutputDispatcherImpl
> close_timer_
;
94 AudioStreamMap proxy_to_physical_map_
;
96 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcherImpl
);
101 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_