OLD | NEW |
(Empty) | |
| 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" |
| 22 #include "base/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" |
| 27 |
| 28 class AudioOutputProxy; |
| 29 class MessageLoop; |
| 30 |
| 31 class MEDIA_EXPORT AudioOutputDispatcherImpl |
| 32 : 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, |
| 38 const base::TimeDelta& close_delay); |
| 39 |
| 40 // Opens a new physical stream if there are no pending streams in |
| 41 // |idle_streams_|. |
| 42 virtual bool OpenStream() OVERRIDE; |
| 43 |
| 44 // If there are pending streams in |idle_streams_| then it reuses one of |
| 45 // them, otherwise creates a new one. |
| 46 virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback, |
| 47 AudioOutputProxy* stream_proxy) OVERRIDE; |
| 48 |
| 49 // Holds the physical stream temporarily in |pausing_streams_| and then |
| 50 // |stream| is added to the pool of pending streams (i.e. |idle_streams_|). |
| 51 virtual void StopStream(AudioOutputProxy* stream_proxy) OVERRIDE; |
| 52 |
| 53 virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy, |
| 54 double volume) OVERRIDE; |
| 55 |
| 56 virtual void CloseStream(AudioOutputProxy* stream_proxy) OVERRIDE; |
| 57 |
| 58 virtual void Shutdown() OVERRIDE; |
| 59 |
| 60 private: |
| 61 typedef std::map<AudioOutputProxy*, AudioOutputStream*> AudioStreamMap; |
| 62 friend class base::RefCountedThreadSafe<AudioOutputDispatcherImpl>; |
| 63 virtual ~AudioOutputDispatcherImpl(); |
| 64 |
| 65 friend class AudioOutputProxyTest; |
| 66 |
| 67 // Creates a new physical output stream, opens it and pushes to |
| 68 // |idle_streams_|. Returns false if the stream couldn't be created or |
| 69 // opened. |
| 70 bool CreateAndOpenStream(); |
| 71 |
| 72 // A task scheduled by StartStream(). Opens a new stream and puts |
| 73 // it in |idle_streams_|. |
| 74 void OpenTask(); |
| 75 |
| 76 // Before a stream is reused, it should sit idle for a bit. This task is |
| 77 // called once that time has elapsed. |
| 78 void StopStreamTask(); |
| 79 |
| 80 // Called by |close_timer_|. Closes all pending streams. |
| 81 void ClosePendingStreams(); |
| 82 |
| 83 base::TimeDelta pause_delay_; |
| 84 size_t paused_proxies_; |
| 85 typedef std::list<AudioOutputStream*> AudioOutputStreamList; |
| 86 AudioOutputStreamList idle_streams_; |
| 87 AudioOutputStreamList pausing_streams_; |
| 88 |
| 89 // Used to post delayed tasks to ourselves that we cancel inside Shutdown(). |
| 90 base::WeakPtrFactory<AudioOutputDispatcherImpl> weak_this_; |
| 91 base::DelayTimer<AudioOutputDispatcherImpl> close_timer_; |
| 92 |
| 93 AudioStreamMap proxy_to_physical_map_; |
| 94 |
| 95 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcherImpl); |
| 96 }; |
| 97 |
| 98 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ |
OLD | NEW |