[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 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. |
| 4 | |
| 5 | // AudioOutputDispatcherImpl is an implementation of AudioOutputDispatcher. |
| 6 | // |
| 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 |
| 10 | // constructor). |
| 11 | // |
| 12 | |
| 13 | #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ |
| 14 | #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ |
| 15 | |
| 16 | #include <list> |
| 17 | #include <map> |
| 18 | |
| 19 | #include "base/basictypes.h" |
| 20 | #include "base/memory/ref_counted.h" |
| 21 | #include "base/memory/weak_ptr.h" |
[email protected] | 7e72e7b | 2013-06-28 05:40:10 | [diff] [blame] | 22 | #include "base/timer/timer.h" |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 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" |
| 27 | |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 28 | namespace media { |
| 29 | |
| 30 | class AudioOutputProxy; |
| 31 | |
| 32 | class MEDIA_EXPORT AudioOutputDispatcherImpl : public AudioOutputDispatcher { |
| 33 | public: |
| 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, |
[email protected] | 0ead785 | 2013-09-06 11:46:36 | [diff] [blame^] | 38 | const std::string& output_device_id, |
[email protected] | 2372e96 | 2013-06-03 11:09:05 | [diff] [blame] | 39 | const std::string& input_device_id, |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 40 | const base::TimeDelta& close_delay); |
| 41 | |
| 42 | // Opens a new physical stream if there are no pending streams in |
[email protected] | 911fb6c5 | 2012-09-21 06:47:31 | [diff] [blame] | 43 | // |idle_streams_|. Do not call Close() or Stop() if this method fails. |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 44 | virtual bool OpenStream() OVERRIDE; |
| 45 | |
| 46 | // If there are pending streams in |idle_streams_| then it reuses one of |
| 47 | // them, otherwise creates a new one. |
| 48 | virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback, |
| 49 | AudioOutputProxy* stream_proxy) OVERRIDE; |
| 50 | |
| 51 | // Holds the physical stream temporarily in |pausing_streams_| and then |
| 52 | // |stream| is added to the pool of pending streams (i.e. |idle_streams_|). |
| 53 | virtual void StopStream(AudioOutputProxy* stream_proxy) OVERRIDE; |
| 54 | |
| 55 | virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy, |
| 56 | double volume) OVERRIDE; |
| 57 | |
| 58 | virtual void CloseStream(AudioOutputProxy* stream_proxy) OVERRIDE; |
| 59 | |
| 60 | virtual void Shutdown() OVERRIDE; |
| 61 | |
| 62 | private: |
| 63 | typedef std::map<AudioOutputProxy*, AudioOutputStream*> AudioStreamMap; |
| 64 | friend class base::RefCountedThreadSafe<AudioOutputDispatcherImpl>; |
| 65 | virtual ~AudioOutputDispatcherImpl(); |
| 66 | |
| 67 | friend class AudioOutputProxyTest; |
| 68 | |
| 69 | // Creates a new physical output stream, opens it and pushes to |
| 70 | // |idle_streams_|. Returns false if the stream couldn't be created or |
| 71 | // opened. |
| 72 | bool CreateAndOpenStream(); |
| 73 | |
| 74 | // A task scheduled by StartStream(). Opens a new stream and puts |
| 75 | // it in |idle_streams_|. |
| 76 | void OpenTask(); |
| 77 | |
| 78 | // Before a stream is reused, it should sit idle for a bit. This task is |
| 79 | // called once that time has elapsed. |
| 80 | void StopStreamTask(); |
| 81 | |
| 82 | // Called by |close_timer_|. Closes all pending streams. |
| 83 | void ClosePendingStreams(); |
| 84 | |
| 85 | base::TimeDelta pause_delay_; |
| 86 | size_t paused_proxies_; |
| 87 | typedef std::list<AudioOutputStream*> AudioOutputStreamList; |
| 88 | AudioOutputStreamList idle_streams_; |
| 89 | AudioOutputStreamList pausing_streams_; |
| 90 | |
| 91 | // Used to post delayed tasks to ourselves that we cancel inside Shutdown(). |
| 92 | base::WeakPtrFactory<AudioOutputDispatcherImpl> weak_this_; |
| 93 | base::DelayTimer<AudioOutputDispatcherImpl> close_timer_; |
| 94 | |
| 95 | AudioStreamMap proxy_to_physical_map_; |
| 96 | |
| 97 | DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcherImpl); |
| 98 | }; |
| 99 | |
| 100 | } // namespace media |
| 101 | |
| 102 | #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ |