| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 dispatches creation and deletion of audio | 5 // AudioOutputDispatcher dispatches creation and deletion of audio |
| 6 // output streams. AudioOutputProxy objects use this class to allocate | 6 // output streams. AudioOutputProxy objects use this class to allocate |
| 7 // and recycle actual audio output streams. When playback is started, | 7 // and recycle actual audio output streams. When playback is started, |
| 8 // the proxy calls StreamStarted() to get an output stream that it | 8 // the proxy calls StreamStarted() to get an output stream that it |
| 9 // uses to play the sound. When playback is stopped, the proxy returns | 9 // uses to play the sound. When playback is stopped, the proxy returns |
| 10 // the stream back to the dispatcher by calling StreamStopped(). | 10 // the stream back to the dispatcher by calling StreamStopped(). |
| 11 // | 11 // |
| 12 // To avoid opening and closing audio devices more frequently than it | 12 // To avoid opening and closing audio devices more frequently than it |
| 13 // is neccessary, each dispatcher has a pool of inactive physical | 13 // is neccessary, each dispatcher has a pool of inactive physical |
| 14 // streams. A stream is closed only if it hasn't been used for a | 14 // streams. A stream is closed only if it hasn't been used for a |
| 15 // certain period of time (specified in the constructor). | 15 // certain period of time (specified in the constructor). |
| 16 // | 16 // |
| 17 // AudioManagerBase creates one AudioOutputDispatcher per each | 17 // AudioManagerBase creates one AudioOutputDispatcher per each |
| 18 // possible set of audio parameters, i.e. streams with different | 18 // possible set of audio parameters, i.e. streams with different |
| 19 // parameters are managed independently. | 19 // parameters are managed independently. |
| 20 | 20 |
| 21 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ | 21 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ |
| 22 #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ | 22 #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ |
| 23 | 23 |
| 24 #include <vector> | 24 #include <vector> |
| 25 #include <list> |
| 25 | 26 |
| 26 #include "base/basictypes.h" | 27 #include "base/basictypes.h" |
| 27 #include "base/memory/ref_counted.h" | 28 #include "base/memory/ref_counted.h" |
| 28 #include "base/timer.h" | 29 #include "base/timer.h" |
| 29 #include "media/audio/audio_manager.h" | 30 #include "media/audio/audio_manager.h" |
| 30 #include "media/audio/audio_parameters.h" | 31 #include "media/audio/audio_parameters.h" |
| 31 | 32 |
| 32 class AudioOutputStream; | 33 class AudioOutputStream; |
| 33 class MessageLoop; | 34 class MessageLoop; |
| 34 | 35 |
| 35 class AudioOutputDispatcher | 36 class AudioOutputDispatcher |
| 36 : public base::RefCountedThreadSafe<AudioOutputDispatcher> { | 37 : public base::RefCountedThreadSafe<AudioOutputDispatcher> { |
| 37 public: | 38 public: |
| 38 // |close_delay_ms| specifies delay after the stream is paused until | 39 // |close_delay_ms| specifies delay after the stream is paused until |
| 39 // the audio device is closed. | 40 // the audio device is closed. |
| 40 AudioOutputDispatcher(AudioManager* audio_manager, | 41 AudioOutputDispatcher(AudioManager* audio_manager, |
| 41 const AudioParameters& params, | 42 const AudioParameters& params, |
| 42 int close_delay_ms); | 43 int close_delay_ms); |
| 43 ~AudioOutputDispatcher(); | 44 ~AudioOutputDispatcher(); |
| 44 | 45 |
| 45 // Called by AudioOutputProxy when the stream is closed. Opens a new | 46 // Called by AudioOutputProxy when the stream is closed. Opens a new |
| 46 // physical stream if there are no pending streams in |streams_|. | 47 // physical stream if there are no pending streams in |idle_streams_|. |
| 47 // Returns false, if it fails to open it. | 48 // Returns false, if it fails to open it. |
| 48 bool StreamOpened(); | 49 bool StreamOpened(); |
| 49 | 50 |
| 50 // Called by AudioOutputProxy when the stream is started. If there | 51 // Called by AudioOutputProxy when the stream is started. If there |
| 51 // are pending streams in |streams_| then it returns one of them, | 52 // are pending streams in |idle_streams_| then it returns one of them, |
| 52 // otherwise creates a new one. Returns a physical stream that must | 53 // otherwise creates a new one. Returns a physical stream that must |
| 53 // be used, or NULL if it fails to open audio device. Ownership of | 54 // be used, or NULL if it fails to open audio device. Ownership of |
| 54 // the result is passed to the caller. | 55 // the result is passed to the caller. |
| 55 AudioOutputStream* StreamStarted(); | 56 AudioOutputStream* StreamStarted(); |
| 56 | 57 |
| 57 // Called by AudioOutputProxy when the stream is stopped. Returns | 58 // Called by AudioOutputProxy when the stream is stopped. Holds the |
| 58 // |stream| to the pool of pending streams (i.e. |streams_|). | 59 // stream temporarily in |pausing_streams_| and then |stream| is |
| 60 // added to the pool of pending streams (i.e. |idle_streams_|). |
| 59 // Ownership of the |stream| is passed to the dispatcher. | 61 // Ownership of the |stream| is passed to the dispatcher. |
| 60 void StreamStopped(AudioOutputStream* stream); | 62 void StreamStopped(AudioOutputStream* stream); |
| 61 | 63 |
| 62 // Called by AudioOutputProxy when the stream is closed. | 64 // Called by AudioOutputProxy when the stream is closed. |
| 63 void StreamClosed(); | 65 void StreamClosed(); |
| 64 | 66 |
| 65 MessageLoop* message_loop(); | 67 MessageLoop* message_loop(); |
| 66 | 68 |
| 67 private: | 69 private: |
| 68 friend class AudioOutputProxyTest; | 70 friend class AudioOutputProxyTest; |
| 69 | 71 |
| 70 // Creates a new physical output stream, opens it and pushes to | 72 // Creates a new physical output stream, opens it and pushes to |
| 71 // |streams_|. Returns false if the stream couldn't be created or | 73 // |idle_streams_|. Returns false if the stream couldn't be created or |
| 72 // opened. | 74 // opened. |
| 73 bool CreateAndOpenStream(); | 75 bool CreateAndOpenStream(); |
| 74 | 76 |
| 75 // A task scheduled by StreamStarted(). Opens a new stream and puts | 77 // A task scheduled by StreamStarted(). Opens a new stream and puts |
| 76 // it in |streams_|. | 78 // it in |idle_streams_|. |
| 77 void OpenTask(); | 79 void OpenTask(); |
| 78 | 80 |
| 81 // Before a stream is reused, it should sit idle for a bit. This task is |
| 82 // called once that time has elapsed. |
| 83 void StopStreamTask(); |
| 84 |
| 79 // Called by |close_timer_|. Closes all pending stream. | 85 // Called by |close_timer_|. Closes all pending stream. |
| 80 void ClosePendingStreams(); | 86 void ClosePendingStreams(); |
| 81 | 87 |
| 82 AudioManager* audio_manager_; | 88 AudioManager* audio_manager_; |
| 83 MessageLoop* message_loop_; | 89 MessageLoop* message_loop_; |
| 84 AudioParameters params_; | 90 AudioParameters params_; |
| 85 | 91 |
| 92 int64 pause_delay_milliseconds_; |
| 86 size_t paused_proxies_; | 93 size_t paused_proxies_; |
| 87 std::vector<AudioOutputStream*> streams_; | 94 std::vector<AudioOutputStream*> idle_streams_; |
| 95 std::list<AudioOutputStream*> pausing_streams_; |
| 88 base::DelayTimer<AudioOutputDispatcher> close_timer_; | 96 base::DelayTimer<AudioOutputDispatcher> close_timer_; |
| 89 | 97 |
| 90 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcher); | 98 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcher); |
| 91 }; | 99 }; |
| 92 | 100 |
| 93 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ | 101 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ |
| OLD | NEW |