| 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 // AudioOutputDispatcher is a single-threaded class that dispatches creation and | 5 // AudioOutputDispatcher is a single-threaded base class that dispatches |
| 6 // deletion of audio output streams. AudioOutputProxy objects use this class to | 6 // creation and deletion of audio output streams. AudioOutputProxy objects use |
| 7 // allocate and recycle actual audio output streams. When playback is started, | 7 // this class to allocate and recycle actual audio output streams. When playback |
| 8 // the proxy calls StreamStarted() to get an output stream that it uses to play | 8 // is started, the proxy calls StartStream() to get an output stream that it |
| 9 // audio. When playback is stopped, the proxy returns the stream back to the | 9 // uses to play audio. When playback is stopped, the proxy returns the stream |
| 10 // dispatcher by calling StreamStopped(). | 10 // back to the dispatcher by calling StopStream(). |
| 11 // | 11 // |
| 12 // To avoid opening and closing audio devices more frequently than necessary, | 12 // AudioManagerBase creates one specialization of AudioOutputDispatcher on the |
| 13 // each dispatcher has a pool of inactive physical streams. A stream is closed | 13 // audio thread for each possible set of audio parameters. I.e streams with |
| 14 // only if it hasn't been used for a certain period of time (specified via the | 14 // different parameters are managed independently. The AudioOutputDispatcher |
| 15 // constructor). | 15 // instance is then deleted on the audio thread when the AudioManager shuts |
| 16 // | 16 // down. |
| 17 // AudioManagerBase creates one AudioOutputDispatcher on the audio thread for | |
| 18 // each possible set of audio parameters. I.e streams with different parameters | |
| 19 // are managed independently. The AudioOutputDispatcher instance is then | |
| 20 // deleted on the audio thread when the AudioManager shuts down. | |
| 21 | 17 |
| 22 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ | 18 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ |
| 23 #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ | 19 #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ |
| 24 | 20 |
| 25 #include <vector> | |
| 26 #include <list> | |
| 27 | |
| 28 #include "base/basictypes.h" | 21 #include "base/basictypes.h" |
| 29 #include "base/memory/ref_counted.h" | 22 #include "base/memory/ref_counted.h" |
| 30 #include "base/memory/weak_ptr.h" | |
| 31 #include "base/timer.h" | 23 #include "base/timer.h" |
| 24 #include "media/audio/audio_io.h" |
| 32 #include "media/audio/audio_manager.h" | 25 #include "media/audio/audio_manager.h" |
| 33 #include "media/audio/audio_parameters.h" | 26 #include "media/audio/audio_parameters.h" |
| 34 | 27 |
| 35 class MessageLoop; | 28 class MessageLoop; |
| 36 | 29 |
| 37 namespace media { | 30 namespace media { |
| 38 | 31 |
| 39 class AudioOutputStream; | 32 class AudioOutputProxy; |
| 40 | 33 |
| 41 class MEDIA_EXPORT AudioOutputDispatcher | 34 class MEDIA_EXPORT AudioOutputDispatcher |
| 42 : public base::RefCountedThreadSafe<AudioOutputDispatcher> { | 35 : public base::RefCountedThreadSafe<AudioOutputDispatcher> { |
| 43 public: | 36 public: |
| 44 // |close_delay_ms| specifies delay after the stream is paused until | |
| 45 // the audio device is closed. | |
| 46 AudioOutputDispatcher(AudioManager* audio_manager, | 37 AudioOutputDispatcher(AudioManager* audio_manager, |
| 47 const AudioParameters& params, | 38 const AudioParameters& params); |
| 48 base::TimeDelta close_delay); | |
| 49 ~AudioOutputDispatcher(); | |
| 50 | 39 |
| 51 // Called by AudioOutputProxy when the stream is closed. Opens a new | 40 // Called by AudioOutputProxy to open the stream. |
| 52 // physical stream if there are no pending streams in |idle_streams_|. | |
| 53 // Returns false, if it fails to open it. | 41 // Returns false, if it fails to open it. |
| 54 bool StreamOpened(); | 42 virtual bool OpenStream() = 0; |
| 55 | 43 |
| 56 // Called by AudioOutputProxy when the stream is started. If there | 44 // Called by AudioOutputProxy when the stream is started. |
| 57 // are pending streams in |idle_streams_| then it returns one of them, | 45 // Uses |callback| to get source data and report errors, if any. |
| 58 // otherwise creates a new one. Returns a physical stream that must | 46 // Does *not* take ownership of this callback. |
| 59 // be used, or NULL if it fails to open audio device. Ownership of | 47 // Returns true if started successfully, false otherwise. |
| 60 // the result is passed to the caller. | 48 virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback, |
| 61 AudioOutputStream* StreamStarted(); | 49 AudioOutputProxy* stream_proxy) = 0; |
| 62 | 50 |
| 63 // Called by AudioOutputProxy when the stream is stopped. Holds the | 51 // Called by AudioOutputProxy when the stream is stopped. |
| 64 // stream temporarily in |pausing_streams_| and then |stream| is | 52 // Ownership of the |stream_proxy| is passed to the dispatcher. |
| 65 // added to the pool of pending streams (i.e. |idle_streams_|). | 53 virtual void StopStream(AudioOutputProxy* stream_proxy) = 0; |
| 66 // Ownership of the |stream| is passed to the dispatcher. | 54 |
| 67 void StreamStopped(AudioOutputStream* stream); | 55 |
| 56 // Called by AudioOutputProxy when the volume is set. |
| 57 virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy, |
| 58 double volume) = 0; |
| 68 | 59 |
| 69 // Called by AudioOutputProxy when the stream is closed. | 60 // Called by AudioOutputProxy when the stream is closed. |
| 70 void StreamClosed(); | 61 virtual void CloseStream(AudioOutputProxy* stream_proxy) = 0; |
| 71 | 62 |
| 72 // Called on the audio thread when the AudioManager is shutting down. | 63 // Called on the audio thread when the AudioManager is shutting down. |
| 73 void Shutdown(); | 64 virtual void Shutdown() = 0; |
| 74 | 65 |
| 75 private: | 66 protected: |
| 76 friend class AudioOutputProxyTest; | 67 friend class base::RefCountedThreadSafe<AudioOutputDispatcher>; |
| 77 | 68 virtual ~AudioOutputDispatcher(); |
| 78 // Creates a new physical output stream, opens it and pushes to | |
| 79 // |idle_streams_|. Returns false if the stream couldn't be created or | |
| 80 // opened. | |
| 81 bool CreateAndOpenStream(); | |
| 82 | |
| 83 // A task scheduled by StreamStarted(). Opens a new stream and puts | |
| 84 // it in |idle_streams_|. | |
| 85 void OpenTask(); | |
| 86 | |
| 87 // Before a stream is reused, it should sit idle for a bit. This task is | |
| 88 // called once that time has elapsed. | |
| 89 void StopStreamTask(); | |
| 90 | |
| 91 // Called by |close_timer_|. Closes all pending stream. | |
| 92 void ClosePendingStreams(); | |
| 93 | 69 |
| 94 // A no-reference-held pointer (we don't want circular references) back to the | 70 // A no-reference-held pointer (we don't want circular references) back to the |
| 95 // AudioManager that owns this object. | 71 // AudioManager that owns this object. |
| 96 AudioManager* audio_manager_; | 72 AudioManager* audio_manager_; |
| 97 MessageLoop* message_loop_; | 73 MessageLoop* message_loop_; |
| 98 AudioParameters params_; | 74 AudioParameters params_; |
| 99 | 75 |
| 100 base::TimeDelta pause_delay_; | 76 private: |
| 101 size_t paused_proxies_; | |
| 102 typedef std::list<AudioOutputStream*> AudioOutputStreamList; | |
| 103 AudioOutputStreamList idle_streams_; | |
| 104 AudioOutputStreamList pausing_streams_; | |
| 105 | |
| 106 // Used to post delayed tasks to ourselves that we cancel inside Shutdown(). | |
| 107 base::WeakPtrFactory<AudioOutputDispatcher> weak_this_; | |
| 108 base::DelayTimer<AudioOutputDispatcher> close_timer_; | |
| 109 | |
| 110 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcher); | 77 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcher); |
| 111 }; | 78 }; |
| 112 | 79 |
| 113 } // namespace media | 80 } // namespace media |
| 114 | 81 |
| 115 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ | 82 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ |
| OLD | NEW |