[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 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 | |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 16 | #include <map> |
[email protected] | 8e7813c | 2013-12-10 04:34:18 | [diff] [blame] | 17 | #include <vector> |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 18 | |
| 19 | #include "base/basictypes.h" |
| 20 | #include "base/memory/ref_counted.h" |
[email protected] | 7e72e7b | 2013-06-28 05:40:10 | [diff] [blame] | 21 | #include "base/timer/timer.h" |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 22 | #include "media/audio/audio_io.h" |
[email protected] | 6f8d9655 | 2013-12-13 21:10:13 | [diff] [blame] | 23 | #include "media/audio/audio_logging.h" |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 24 | #include "media/audio/audio_manager.h" |
| 25 | #include "media/audio/audio_output_dispatcher.h" |
| 26 | #include "media/audio/audio_parameters.h" |
| 27 | |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 28 | namespace media { |
| 29 | |
| 30 | class AudioOutputProxy; |
| 31 | |
| 32 | class MEDIA_EXPORT AudioOutputDispatcherImpl : public AudioOutputDispatcher { |
| 33 | public: |
[email protected] | 8e7813c | 2013-12-10 04:34:18 | [diff] [blame] | 34 | // |close_delay| specifies delay after the stream is idle until the audio |
| 35 | // device is closed. |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 36 | AudioOutputDispatcherImpl(AudioManager* audio_manager, |
| 37 | const AudioParameters& params, |
[email protected] | 0ead785 | 2013-09-06 11:46:36 | [diff] [blame] | 38 | const std::string& output_device_id, |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 39 | const base::TimeDelta& close_delay); |
| 40 | |
| 41 | // Opens a new physical stream if there are no pending streams in |
[email protected] | 911fb6c5 | 2012-09-21 06:47:31 | [diff] [blame] | 42 | // |idle_streams_|. Do not call Close() or Stop() if this method fails. |
dcheng | c24565478f | 2014-10-21 12:23:27 | [diff] [blame] | 43 | bool OpenStream() override; |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 44 | |
| 45 | // If there are pending streams in |idle_streams_| then it reuses one of |
| 46 | // them, otherwise creates a new one. |
dcheng | c24565478f | 2014-10-21 12:23:27 | [diff] [blame] | 47 | bool StartStream(AudioOutputStream::AudioSourceCallback* callback, |
| 48 | AudioOutputProxy* stream_proxy) override; |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 49 | |
[email protected] | 8e7813c | 2013-12-10 04:34:18 | [diff] [blame] | 50 | // Stops the stream assigned to the specified proxy and moves it into |
| 51 | // |idle_streams_| for reuse by other proxies. |
dcheng | c24565478f | 2014-10-21 12:23:27 | [diff] [blame] | 52 | void StopStream(AudioOutputProxy* stream_proxy) override; |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 53 | |
dcheng | c24565478f | 2014-10-21 12:23:27 | [diff] [blame] | 54 | void StreamVolumeSet(AudioOutputProxy* stream_proxy, double volume) override; |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 55 | |
[email protected] | 8e7813c | 2013-12-10 04:34:18 | [diff] [blame] | 56 | // Closes |idle_streams_| until the number of |idle_streams_| is equal to the |
| 57 | // |idle_proxies_| count. If there are no |idle_proxies_| a single stream is |
| 58 | // kept alive until |close_timer_| fires. |
dcheng | c24565478f | 2014-10-21 12:23:27 | [diff] [blame] | 59 | void CloseStream(AudioOutputProxy* stream_proxy) override; |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 60 | |
dcheng | c24565478f | 2014-10-21 12:23:27 | [diff] [blame] | 61 | void Shutdown() override; |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 62 | |
dalecurtis | c85184d | 2015-02-05 23:19:45 | [diff] [blame] | 63 | // Returns true if there are any open AudioOutputProxy objects. |
| 64 | bool HasOutputProxies() const; |
| 65 | |
dalecurtis | 784c059 | 2015-10-28 00:50:15 | [diff] [blame] | 66 | // Closes all |idle_streams_|. |
| 67 | void CloseAllIdleStreams(); |
| 68 | |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 69 | private: |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 70 | friend class base::RefCountedThreadSafe<AudioOutputDispatcherImpl>; |
dcheng | c24565478f | 2014-10-21 12:23:27 | [diff] [blame] | 71 | ~AudioOutputDispatcherImpl() override; |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 72 | |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 73 | // Creates a new physical output stream, opens it and pushes to |
| 74 | // |idle_streams_|. Returns false if the stream couldn't be created or |
| 75 | // opened. |
| 76 | bool CreateAndOpenStream(); |
| 77 | |
[email protected] | 8e7813c | 2013-12-10 04:34:18 | [diff] [blame] | 78 | // Similar to CloseAllIdleStreams(), but keeps |keep_alive| streams alive. |
| 79 | void CloseIdleStreams(size_t keep_alive); |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 80 | |
[email protected] | 8e7813c | 2013-12-10 04:34:18 | [diff] [blame] | 81 | size_t idle_proxies_; |
| 82 | std::vector<AudioOutputStream*> idle_streams_; |
[email protected] | 58bb3a2 | 2013-10-23 23:05:27 | [diff] [blame] | 83 | |
[email protected] | 8e7813c | 2013-12-10 04:34:18 | [diff] [blame] | 84 | // When streams are stopped they're added to |idle_streams_|, if no stream is |
| 85 | // reused before |close_delay_| elapses |close_timer_| will run |
| 86 | // CloseIdleStreams(). |
danakj | 8c3eb80 | 2015-09-24 07:53:00 | [diff] [blame] | 87 | base::DelayTimer close_timer_; |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 88 | |
[email protected] | 8e7813c | 2013-12-10 04:34:18 | [diff] [blame] | 89 | typedef std::map<AudioOutputProxy*, AudioOutputStream*> AudioStreamMap; |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 90 | AudioStreamMap proxy_to_physical_map_; |
| 91 | |
[email protected] | 6f8d9655 | 2013-12-13 21:10:13 | [diff] [blame] | 92 | scoped_ptr<AudioLog> audio_log_; |
| 93 | typedef std::map<AudioOutputStream*, int> AudioStreamIDMap; |
| 94 | AudioStreamIDMap audio_stream_ids_; |
| 95 | int audio_stream_id_; |
| 96 | |
[email protected] | 08c1c13 | 2012-04-19 17:44:10 | [diff] [blame] | 97 | DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcherImpl); |
| 98 | }; |
| 99 | |
| 100 | } // namespace media |
| 101 | |
| 102 | #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_ |