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> |
16 #include <map> | 17 #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" |
21 #include "base/timer/timer.h" | 22 #include "base/timer/timer.h" |
22 #include "media/audio/audio_io.h" | 23 #include "media/audio/audio_io.h" |
23 #include "media/audio/audio_manager.h" | 24 #include "media/audio/audio_manager.h" |
24 #include "media/audio/audio_output_dispatcher.h" | 25 #include "media/audio/audio_output_dispatcher.h" |
25 #include "media/audio/audio_parameters.h" | 26 #include "media/audio/audio_parameters.h" |
26 | 27 |
27 namespace media { | 28 namespace media { |
28 | 29 |
29 class AudioOutputProxy; | 30 class AudioOutputProxy; |
30 | 31 |
31 class MEDIA_EXPORT AudioOutputDispatcherImpl : public AudioOutputDispatcher { | 32 class MEDIA_EXPORT AudioOutputDispatcherImpl : public AudioOutputDispatcher { |
32 public: | 33 public: |
33 // |close_delay| specifies delay after the stream is idle until the audio | 34 // |close_delay_ms| specifies delay after the stream is paused until |
34 // device is closed. | 35 // the audio device is closed. |
35 AudioOutputDispatcherImpl(AudioManager* audio_manager, | 36 AudioOutputDispatcherImpl(AudioManager* audio_manager, |
36 const AudioParameters& params, | 37 const AudioParameters& params, |
37 const std::string& output_device_id, | 38 const std::string& output_device_id, |
38 const std::string& input_device_id, | 39 const std::string& input_device_id, |
39 const base::TimeDelta& close_delay); | 40 const base::TimeDelta& close_delay); |
40 | 41 |
41 // Opens a new physical stream if there are no pending streams in | 42 // Opens a new physical stream if there are no pending streams in |
42 // |idle_streams_|. Do not call Close() or Stop() if this method fails. | 43 // |idle_streams_|. Do not call Close() or Stop() if this method fails. |
43 virtual bool OpenStream() OVERRIDE; | 44 virtual bool OpenStream() OVERRIDE; |
44 | 45 |
45 // If there are pending streams in |idle_streams_| then it reuses one of | 46 // If there are pending streams in |idle_streams_| then it reuses one of |
46 // them, otherwise creates a new one. | 47 // them, otherwise creates a new one. |
47 virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback, | 48 virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback, |
48 AudioOutputProxy* stream_proxy) OVERRIDE; | 49 AudioOutputProxy* stream_proxy) OVERRIDE; |
49 | 50 |
50 // Holds the physical stream temporarily in |pausing_streams_| and then | 51 // Holds the physical stream temporarily in |pausing_streams_| and then |
51 // |stream| is added to the pool of pending streams (i.e. |idle_streams_|). | 52 // |stream| is added to the pool of pending streams (i.e. |idle_streams_|). |
52 virtual void StopStream(AudioOutputProxy* stream_proxy) OVERRIDE; | 53 virtual void StopStream(AudioOutputProxy* stream_proxy) OVERRIDE; |
53 | 54 |
54 virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy, | 55 virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy, |
55 double volume) OVERRIDE; | 56 double volume) OVERRIDE; |
56 | 57 |
57 virtual void CloseStream(AudioOutputProxy* stream_proxy) OVERRIDE; | 58 virtual void CloseStream(AudioOutputProxy* stream_proxy) OVERRIDE; |
58 | 59 |
59 virtual void Shutdown() OVERRIDE; | 60 virtual void Shutdown() OVERRIDE; |
60 | 61 |
61 private: | 62 private: |
| 63 typedef std::map<AudioOutputProxy*, AudioOutputStream*> AudioStreamMap; |
62 friend class base::RefCountedThreadSafe<AudioOutputDispatcherImpl>; | 64 friend class base::RefCountedThreadSafe<AudioOutputDispatcherImpl>; |
63 virtual ~AudioOutputDispatcherImpl(); | 65 virtual ~AudioOutputDispatcherImpl(); |
64 | 66 |
| 67 friend class AudioOutputProxyTest; |
| 68 |
65 // Creates a new physical output stream, opens it and pushes to | 69 // Creates a new physical output stream, opens it and pushes to |
66 // |idle_streams_|. Returns false if the stream couldn't be created or | 70 // |idle_streams_|. Returns false if the stream couldn't be created or |
67 // opened. | 71 // opened. |
68 bool CreateAndOpenStream(); | 72 bool CreateAndOpenStream(); |
69 | 73 |
70 // Closes all |idle_streams_|. | 74 // A task scheduled by StartStream(). Opens a new stream and puts |
71 void CloseIdleStreams(); | 75 // it in |idle_streams_|. |
| 76 void OpenTask(); |
72 | 77 |
73 size_t idle_proxies_; | 78 // Before a stream is reused, it should sit idle for a bit. This task is |
74 std::vector<AudioOutputStream*> idle_streams_; | 79 // called once that time has elapsed. |
| 80 void StopStreamTask(); |
75 | 81 |
76 // When streams are stopped they're added to |idle_streams_|, if no stream is | 82 // Called by |close_timer_|. Closes all pending streams. |
77 // reused before |close_delay_| elapses |close_timer_| will run | 83 void ClosePendingStreams(); |
78 // CloseIdleStreams(). | 84 |
| 85 base::TimeDelta pause_delay_; |
| 86 size_t paused_proxies_; |
| 87 typedef std::list<AudioOutputStream*> AudioOutputStreamList; |
| 88 AudioOutputStreamList idle_streams_; |
| 89 AudioOutputStreamList pausing_streams_; |
| 90 |
| 91 // Used to post delayed tasks to ourselves that we cancel inside Shutdown(). |
| 92 base::WeakPtrFactory<AudioOutputDispatcherImpl> weak_this_; |
79 base::DelayTimer<AudioOutputDispatcherImpl> close_timer_; | 93 base::DelayTimer<AudioOutputDispatcherImpl> close_timer_; |
80 | 94 |
81 typedef std::map<AudioOutputProxy*, AudioOutputStream*> AudioStreamMap; | |
82 AudioStreamMap proxy_to_physical_map_; | 95 AudioStreamMap proxy_to_physical_map_; |
83 | 96 |
84 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcherImpl); | 97 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcherImpl); |
85 }; | 98 }; |
86 | 99 |
87 } // namespace media | 100 } // namespace media |
88 | 101 |
89 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ | 102 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ |
OLD | NEW |