Add some instrumentation for jank in URLRequest::Start.
[chromium-blink-merge.git] / media / audio / audio_output_dispatcher_impl.h
blobd27178458fe81eaa666ec3f5b401cd020118ae6c
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.
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).
13 #ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_
14 #define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_
16 #include <map>
17 #include <vector>
19 #include "base/basictypes.h"
20 #include "base/memory/ref_counted.h"
21 #include "base/timer/timer.h"
22 #include "media/audio/audio_io.h"
23 #include "media/audio/audio_logging.h"
24 #include "media/audio/audio_manager.h"
25 #include "media/audio/audio_output_dispatcher.h"
26 #include "media/audio/audio_parameters.h"
28 namespace media {
30 class AudioOutputProxy;
32 class MEDIA_EXPORT AudioOutputDispatcherImpl : public AudioOutputDispatcher {
33 public:
34 // |close_delay| specifies delay after the stream is idle until the audio
35 // device is closed.
36 AudioOutputDispatcherImpl(AudioManager* audio_manager,
37 const AudioParameters& params,
38 const std::string& output_device_id,
39 const base::TimeDelta& close_delay);
41 // 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 bool OpenStream() override;
45 // If there are pending streams in |idle_streams_| then it reuses one of
46 // them, otherwise creates a new one.
47 bool StartStream(AudioOutputStream::AudioSourceCallback* callback,
48 AudioOutputProxy* stream_proxy) override;
50 // Stops the stream assigned to the specified proxy and moves it into
51 // |idle_streams_| for reuse by other proxies.
52 void StopStream(AudioOutputProxy* stream_proxy) override;
54 void StreamVolumeSet(AudioOutputProxy* stream_proxy, double volume) override;
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.
59 void CloseStream(AudioOutputProxy* stream_proxy) override;
61 void Shutdown() override;
63 // Returns true if there are any open AudioOutputProxy objects.
64 bool HasOutputProxies() const;
66 private:
67 friend class base::RefCountedThreadSafe<AudioOutputDispatcherImpl>;
68 ~AudioOutputDispatcherImpl() override;
70 // 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();
75 // Closes all |idle_streams_|.
76 void CloseAllIdleStreams();
77 // Similar to CloseAllIdleStreams(), but keeps |keep_alive| streams alive.
78 void CloseIdleStreams(size_t keep_alive);
80 size_t idle_proxies_;
81 std::vector<AudioOutputStream*> idle_streams_;
83 // 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().
86 base::DelayTimer<AudioOutputDispatcherImpl> close_timer_;
88 typedef std::map<AudioOutputProxy*, AudioOutputStream*> AudioStreamMap;
89 AudioStreamMap proxy_to_physical_map_;
91 scoped_ptr<AudioLog> audio_log_;
92 typedef std::map<AudioOutputStream*, int> AudioStreamIDMap;
93 AudioStreamIDMap audio_stream_ids_;
94 int audio_stream_id_;
96 DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcherImpl);
99 } // namespace media
101 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_