| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 // AudioOutputDispatcherImpl is an implementation of AudioOutputDispatcher. | 5 // AudioOutputDispatcherImpl is an implementation of AudioOutputDispatcher. |
| 6 // | 6 // |
| 7 // To avoid opening and closing audio devices more frequently than necessary, | 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 | 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 | 9 // only if it hasn't been used for a certain period of time (specified via the |
| 10 // constructor). | 10 // constructor). |
| 11 // | 11 // |
| 12 | 12 |
| 13 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ | 13 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ |
| 14 #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ | 14 #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ |
| 15 | 15 |
| 16 #include <list> | |
| 17 #include <map> | 16 #include <map> |
| 17 #include <vector> |
| 18 | 18 |
| 19 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
| 20 #include "base/memory/ref_counted.h" | 20 #include "base/memory/ref_counted.h" |
| 21 #include "base/memory/weak_ptr.h" | |
| 22 #include "base/timer/timer.h" | 21 #include "base/timer/timer.h" |
| 23 #include "media/audio/audio_io.h" | 22 #include "media/audio/audio_io.h" |
| 24 #include "media/audio/audio_manager.h" | 23 #include "media/audio/audio_manager.h" |
| 25 #include "media/audio/audio_output_dispatcher.h" | 24 #include "media/audio/audio_output_dispatcher.h" |
| 26 #include "media/audio/audio_parameters.h" | 25 #include "media/audio/audio_parameters.h" |
| 27 | 26 |
| 28 namespace media { | 27 namespace media { |
| 29 | 28 |
| 30 class AudioOutputProxy; | 29 class AudioOutputProxy; |
| 31 | 30 |
| 32 class MEDIA_EXPORT AudioOutputDispatcherImpl : public AudioOutputDispatcher { | 31 class MEDIA_EXPORT AudioOutputDispatcherImpl : public AudioOutputDispatcher { |
| 33 public: | 32 public: |
| 34 // |close_delay_ms| specifies delay after the stream is paused until | 33 // |close_delay| specifies delay after the stream is idle until the audio |
| 35 // the audio device is closed. | 34 // device is closed. |
| 36 AudioOutputDispatcherImpl(AudioManager* audio_manager, | 35 AudioOutputDispatcherImpl(AudioManager* audio_manager, |
| 37 const AudioParameters& params, | 36 const AudioParameters& params, |
| 38 const std::string& output_device_id, | 37 const std::string& output_device_id, |
| 39 const std::string& input_device_id, | 38 const std::string& input_device_id, |
| 40 const base::TimeDelta& close_delay); | 39 const base::TimeDelta& close_delay); |
| 41 | 40 |
| 42 // Opens a new physical stream if there are no pending streams in | 41 // Opens a new physical stream if there are no pending streams in |
| 43 // |idle_streams_|. Do not call Close() or Stop() if this method fails. | 42 // |idle_streams_|. Do not call Close() or Stop() if this method fails. |
| 44 virtual bool OpenStream() OVERRIDE; | 43 virtual bool OpenStream() OVERRIDE; |
| 45 | 44 |
| 46 // If there are pending streams in |idle_streams_| then it reuses one of | 45 // If there are pending streams in |idle_streams_| then it reuses one of |
| 47 // them, otherwise creates a new one. | 46 // them, otherwise creates a new one. |
| 48 virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback, | 47 virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback, |
| 49 AudioOutputProxy* stream_proxy) OVERRIDE; | 48 AudioOutputProxy* stream_proxy) OVERRIDE; |
| 50 | 49 |
| 51 // Holds the physical stream temporarily in |pausing_streams_| and then | 50 // Stops the stream assigned to the specified proxy and moves it into |
| 52 // |stream| is added to the pool of pending streams (i.e. |idle_streams_|). | 51 // |idle_streams_| for reuse by other proxies. |
| 53 virtual void StopStream(AudioOutputProxy* stream_proxy) OVERRIDE; | 52 virtual void StopStream(AudioOutputProxy* stream_proxy) OVERRIDE; |
| 54 | 53 |
| 55 virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy, | 54 virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy, |
| 56 double volume) OVERRIDE; | 55 double volume) OVERRIDE; |
| 57 | 56 |
| 57 // Closes |idle_streams_| until the number of |idle_streams_| is equal to the |
| 58 // |idle_proxies_| count. If there are no |idle_proxies_| a single stream is |
| 59 // kept alive until |close_timer_| fires. |
| 58 virtual void CloseStream(AudioOutputProxy* stream_proxy) OVERRIDE; | 60 virtual void CloseStream(AudioOutputProxy* stream_proxy) OVERRIDE; |
| 59 | 61 |
| 60 virtual void Shutdown() OVERRIDE; | 62 virtual void Shutdown() OVERRIDE; |
| 61 | 63 |
| 62 virtual void CloseStreamsForWedgeFix() OVERRIDE; | 64 virtual void CloseStreamsForWedgeFix() OVERRIDE; |
| 63 virtual void RestartStreamsForWedgeFix() OVERRIDE; | 65 virtual void RestartStreamsForWedgeFix() OVERRIDE; |
| 64 | 66 |
| 65 private: | 67 private: |
| 66 typedef std::map<AudioOutputProxy*, AudioOutputStream*> AudioStreamMap; | |
| 67 friend class base::RefCountedThreadSafe<AudioOutputDispatcherImpl>; | 68 friend class base::RefCountedThreadSafe<AudioOutputDispatcherImpl>; |
| 68 virtual ~AudioOutputDispatcherImpl(); | 69 virtual ~AudioOutputDispatcherImpl(); |
| 69 | 70 |
| 70 friend class AudioOutputProxyTest; | |
| 71 | |
| 72 // Creates a new physical output stream, opens it and pushes to | 71 // Creates a new physical output stream, opens it and pushes to |
| 73 // |idle_streams_|. Returns false if the stream couldn't be created or | 72 // |idle_streams_|. Returns false if the stream couldn't be created or |
| 74 // opened. | 73 // opened. |
| 75 bool CreateAndOpenStream(); | 74 bool CreateAndOpenStream(); |
| 76 | 75 |
| 77 // Before a stream is reused, it should sit idle for a bit. This task is | 76 // Closes all |idle_streams_|. |
| 78 // called once that time has elapsed. | 77 void CloseAllIdleStreams(); |
| 79 void StopStreamTask(); | 78 // Similar to CloseAllIdleStreams(), but keeps |keep_alive| streams alive. |
| 79 void CloseIdleStreams(size_t keep_alive); |
| 80 | 80 |
| 81 // Called by |close_timer_|. Closes all pending streams. | 81 size_t idle_proxies_; |
| 82 void ClosePendingStreams(); | 82 std::vector<AudioOutputStream*> idle_streams_; |
| 83 | 83 |
| 84 base::TimeDelta pause_delay_; | 84 // When streams are stopped they're added to |idle_streams_|, if no stream is |
| 85 size_t paused_proxies_; | 85 // reused before |close_delay_| elapses |close_timer_| will run |
| 86 typedef std::list<AudioOutputStream*> AudioOutputStreamList; | 86 // CloseIdleStreams(). |
| 87 AudioOutputStreamList idle_streams_; | |
| 88 AudioOutputStreamList pausing_streams_; | |
| 89 | |
| 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_; | 87 base::DelayTimer<AudioOutputDispatcherImpl> close_timer_; |
| 93 | 88 |
| 89 typedef std::map<AudioOutputProxy*, AudioOutputStream*> AudioStreamMap; |
| 94 AudioStreamMap proxy_to_physical_map_; | 90 AudioStreamMap proxy_to_physical_map_; |
| 95 | 91 |
| 96 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcherImpl); | 92 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcherImpl); |
| 97 }; | 93 }; |
| 98 | 94 |
| 99 } // namespace media | 95 } // namespace media |
| 100 | 96 |
| 101 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ | 97 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ |
| OLD | NEW |