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 is a single-threaded class that dispatches creation and |
6 // output streams. AudioOutputProxy objects use this class to allocate | 6 // deletion of audio output streams. AudioOutputProxy objects use this class to |
7 // and recycle actual audio output streams. When playback is started, | 7 // allocate 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 uses to play |
9 // uses to play the sound. When playback is stopped, the proxy returns | 9 // audio. When playback is stopped, the proxy returns the stream back to the |
10 // the stream back to the dispatcher by calling StreamStopped(). | 10 // 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 necessary, |
13 // is neccessary, each dispatcher has a pool of inactive physical | 13 // each dispatcher has a pool of inactive physical streams. A stream is closed |
14 // streams. A stream is closed only if it hasn't been used for a | 14 // only if it hasn't been used for a certain period of time (specified via the |
15 // certain period of time (specified in the constructor). | 15 // constructor). |
16 // | 16 // |
17 // AudioManagerBase creates one AudioOutputDispatcher per each | 17 // AudioManagerBase creates one AudioOutputDispatcher on the audio thread for |
18 // possible set of audio parameters, i.e. streams with different | 18 // each possible set of audio parameters. I.e streams with different parameters |
19 // parameters are managed independently. | 19 // are managed independently. The AudioOutputDispatcher instance is then |
| 20 // deleted on the audio thread when the AudioManager shuts down. |
20 | 21 |
21 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ | 22 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ |
22 #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ | 23 #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ |
23 | 24 |
24 #include <vector> | 25 #include <vector> |
25 #include <list> | 26 #include <list> |
26 | 27 |
27 #include "base/basictypes.h" | 28 #include "base/basictypes.h" |
28 #include "base/memory/ref_counted.h" | 29 #include "base/memory/ref_counted.h" |
| 30 #include "base/memory/weak_ptr.h" |
29 #include "base/timer.h" | 31 #include "base/timer.h" |
30 #include "media/audio/audio_manager.h" | 32 #include "media/audio/audio_manager.h" |
31 #include "media/audio/audio_parameters.h" | 33 #include "media/audio/audio_parameters.h" |
32 | 34 |
33 class AudioOutputStream; | 35 class AudioOutputStream; |
34 class MessageLoop; | 36 class MessageLoop; |
35 | 37 |
36 class MEDIA_EXPORT AudioOutputDispatcher | 38 class MEDIA_EXPORT AudioOutputDispatcher |
37 : public base::RefCountedThreadSafe<AudioOutputDispatcher> { | 39 : public base::RefCountedThreadSafe<AudioOutputDispatcher> { |
38 public: | 40 public: |
(...skipping 18 matching lines...) Expand all Loading... |
57 | 59 |
58 // Called by AudioOutputProxy when the stream is stopped. Holds the | 60 // Called by AudioOutputProxy when the stream is stopped. Holds the |
59 // stream temporarily in |pausing_streams_| and then |stream| is | 61 // stream temporarily in |pausing_streams_| and then |stream| is |
60 // added to the pool of pending streams (i.e. |idle_streams_|). | 62 // added to the pool of pending streams (i.e. |idle_streams_|). |
61 // Ownership of the |stream| is passed to the dispatcher. | 63 // Ownership of the |stream| is passed to the dispatcher. |
62 void StreamStopped(AudioOutputStream* stream); | 64 void StreamStopped(AudioOutputStream* stream); |
63 | 65 |
64 // Called by AudioOutputProxy when the stream is closed. | 66 // Called by AudioOutputProxy when the stream is closed. |
65 void StreamClosed(); | 67 void StreamClosed(); |
66 | 68 |
| 69 // Called on the audio thread when the AudioManager is shutting down. |
| 70 void Shutdown(); |
| 71 |
67 MessageLoop* message_loop(); | 72 MessageLoop* message_loop(); |
68 | 73 |
69 private: | 74 private: |
70 friend class AudioOutputProxyTest; | 75 friend class AudioOutputProxyTest; |
71 | 76 |
72 // Creates a new physical output stream, opens it and pushes to | 77 // Creates a new physical output stream, opens it and pushes to |
73 // |idle_streams_|. Returns false if the stream couldn't be created or | 78 // |idle_streams_|. Returns false if the stream couldn't be created or |
74 // opened. | 79 // opened. |
75 bool CreateAndOpenStream(); | 80 bool CreateAndOpenStream(); |
76 | 81 |
77 // A task scheduled by StreamStarted(). Opens a new stream and puts | 82 // A task scheduled by StreamStarted(). Opens a new stream and puts |
78 // it in |idle_streams_|. | 83 // it in |idle_streams_|. |
79 void OpenTask(); | 84 void OpenTask(); |
80 | 85 |
81 // Before a stream is reused, it should sit idle for a bit. This task is | 86 // Before a stream is reused, it should sit idle for a bit. This task is |
82 // called once that time has elapsed. | 87 // called once that time has elapsed. |
83 void StopStreamTask(); | 88 void StopStreamTask(); |
84 | 89 |
85 // Called by |close_timer_|. Closes all pending stream. | 90 // Called by |close_timer_|. Closes all pending stream. |
86 void ClosePendingStreams(); | 91 void ClosePendingStreams(); |
87 | 92 |
| 93 // A no-reference-held pointer (we don't want circular references) back to the |
| 94 // AudioManager that owns this object. |
88 AudioManager* audio_manager_; | 95 AudioManager* audio_manager_; |
89 MessageLoop* message_loop_; | 96 MessageLoop* message_loop_; |
90 AudioParameters params_; | 97 AudioParameters params_; |
91 | 98 |
92 int64 pause_delay_milliseconds_; | 99 int64 pause_delay_milliseconds_; |
93 size_t paused_proxies_; | 100 size_t paused_proxies_; |
94 std::vector<AudioOutputStream*> idle_streams_; | 101 typedef std::list<AudioOutputStream*> AudioOutputStreamList; |
95 std::list<AudioOutputStream*> pausing_streams_; | 102 AudioOutputStreamList idle_streams_; |
| 103 AudioOutputStreamList pausing_streams_; |
| 104 |
| 105 // Used to post delayed tasks to ourselves that we cancel inside Shutdown(). |
| 106 base::WeakPtrFactory<AudioOutputDispatcher> weak_this_; |
96 base::DelayTimer<AudioOutputDispatcher> close_timer_; | 107 base::DelayTimer<AudioOutputDispatcher> close_timer_; |
97 | 108 |
98 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcher); | 109 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcher); |
99 }; | 110 }; |
100 | 111 |
101 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ | 112 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_ |
OLD | NEW |