blob: ab290c5d2d101830a65ebdcd6e19eba9e72b464a [file] [log] [blame]
[email protected]08c1c132012-04-19 17:44:101// 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]08c1c132012-04-19 17:44:1016#include <map>
[email protected]8e7813c2013-12-10 04:34:1817#include <vector>
[email protected]08c1c132012-04-19 17:44:1018
19#include "base/basictypes.h"
20#include "base/memory/ref_counted.h"
[email protected]7e72e7b2013-06-28 05:40:1021#include "base/timer/timer.h"
[email protected]08c1c132012-04-19 17:44:1022#include "media/audio/audio_io.h"
[email protected]6f8d96552013-12-13 21:10:1323#include "media/audio/audio_logging.h"
[email protected]08c1c132012-04-19 17:44:1024#include "media/audio/audio_manager.h"
25#include "media/audio/audio_output_dispatcher.h"
26#include "media/audio/audio_parameters.h"
27
[email protected]08c1c132012-04-19 17:44:1028namespace media {
29
30class AudioOutputProxy;
31
32class MEDIA_EXPORT AudioOutputDispatcherImpl : public AudioOutputDispatcher {
33 public:
[email protected]8e7813c2013-12-10 04:34:1834 // |close_delay| specifies delay after the stream is idle until the audio
35 // device is closed.
[email protected]08c1c132012-04-19 17:44:1036 AudioOutputDispatcherImpl(AudioManager* audio_manager,
37 const AudioParameters& params,
[email protected]0ead7852013-09-06 11:46:3638 const std::string& output_device_id,
[email protected]08c1c132012-04-19 17:44:1039 const base::TimeDelta& close_delay);
40
41 // Opens a new physical stream if there are no pending streams in
[email protected]911fb6c52012-09-21 06:47:3142 // |idle_streams_|. Do not call Close() or Stop() if this method fails.
dchengc24565478f2014-10-21 12:23:2743 bool OpenStream() override;
[email protected]08c1c132012-04-19 17:44:1044
45 // If there are pending streams in |idle_streams_| then it reuses one of
46 // them, otherwise creates a new one.
dchengc24565478f2014-10-21 12:23:2747 bool StartStream(AudioOutputStream::AudioSourceCallback* callback,
48 AudioOutputProxy* stream_proxy) override;
[email protected]08c1c132012-04-19 17:44:1049
[email protected]8e7813c2013-12-10 04:34:1850 // Stops the stream assigned to the specified proxy and moves it into
51 // |idle_streams_| for reuse by other proxies.
dchengc24565478f2014-10-21 12:23:2752 void StopStream(AudioOutputProxy* stream_proxy) override;
[email protected]08c1c132012-04-19 17:44:1053
dchengc24565478f2014-10-21 12:23:2754 void StreamVolumeSet(AudioOutputProxy* stream_proxy, double volume) override;
[email protected]08c1c132012-04-19 17:44:1055
[email protected]8e7813c2013-12-10 04:34:1856 // 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.
dchengc24565478f2014-10-21 12:23:2759 void CloseStream(AudioOutputProxy* stream_proxy) override;
[email protected]08c1c132012-04-19 17:44:1060
dchengc24565478f2014-10-21 12:23:2761 void Shutdown() override;
[email protected]08c1c132012-04-19 17:44:1062
dalecurtisc85184d2015-02-05 23:19:4563 // Returns true if there are any open AudioOutputProxy objects.
64 bool HasOutputProxies() const;
65
[email protected]08c1c132012-04-19 17:44:1066 private:
[email protected]08c1c132012-04-19 17:44:1067 friend class base::RefCountedThreadSafe<AudioOutputDispatcherImpl>;
dchengc24565478f2014-10-21 12:23:2768 ~AudioOutputDispatcherImpl() override;
[email protected]08c1c132012-04-19 17:44:1069
[email protected]08c1c132012-04-19 17:44:1070 // Creates a new physical output stream, opens it and pushes to
71 // |idle_streams_|. Returns false if the stream couldn't be created or
72 // opened.
73 bool CreateAndOpenStream();
74
[email protected]8e7813c2013-12-10 04:34:1875 // Closes all |idle_streams_|.
76 void CloseAllIdleStreams();
77 // Similar to CloseAllIdleStreams(), but keeps |keep_alive| streams alive.
78 void CloseIdleStreams(size_t keep_alive);
[email protected]08c1c132012-04-19 17:44:1079
[email protected]8e7813c2013-12-10 04:34:1880 size_t idle_proxies_;
81 std::vector<AudioOutputStream*> idle_streams_;
[email protected]58bb3a22013-10-23 23:05:2782
[email protected]8e7813c2013-12-10 04:34:1883 // When streams are stopped they're added to |idle_streams_|, if no stream is
84 // reused before |close_delay_| elapses |close_timer_| will run
85 // CloseIdleStreams().
danakj8c3eb802015-09-24 07:53:0086 base::DelayTimer close_timer_;
[email protected]08c1c132012-04-19 17:44:1087
[email protected]8e7813c2013-12-10 04:34:1888 typedef std::map<AudioOutputProxy*, AudioOutputStream*> AudioStreamMap;
[email protected]08c1c132012-04-19 17:44:1089 AudioStreamMap proxy_to_physical_map_;
90
[email protected]6f8d96552013-12-13 21:10:1391 scoped_ptr<AudioLog> audio_log_;
92 typedef std::map<AudioOutputStream*, int> AudioStreamIDMap;
93 AudioStreamIDMap audio_stream_ids_;
94 int audio_stream_id_;
95
[email protected]08c1c132012-04-19 17:44:1096 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcherImpl);
97};
98
99} // namespace media
100
101#endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_