summaryrefslogtreecommitdiffstats
path: root/media/audio/audio_output_dispatcher_impl.h
diff options
context:
space:
mode:
authorenal@chromium.org <enal@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-18 23:14:13 +0000
committerenal@chromium.org <enal@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-18 23:14:13 +0000
commit2d4ce826e362a773d3f08ef004237c7b5cc8a8f4 (patch)
treef98afff44a5537093a0f6947d6166c60816e9375 /media/audio/audio_output_dispatcher_impl.h
parent2103c28a4a1e27c3b6f4eb88b8db6b283c2904c7 (diff)
downloadchromium_src-2d4ce826e362a773d3f08ef004237c7b5cc8a8f4.zip
chromium_src-2d4ce826e362a773d3f08ef004237c7b5cc8a8f4.tar.gz
chromium_src-2d4ce826e362a773d3f08ef004237c7b5cc8a8f4.tar.bz2
Software audio mixer.
Code goes through old or new paths depending on the AudioParameters or command-line flag. Added unit tests. Review URL: http://codereview.chromium.org/9691001 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132891 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio/audio_output_dispatcher_impl.h')
-rw-r--r--media/audio/audio_output_dispatcher_impl.h102
1 files changed, 102 insertions, 0 deletions
diff --git a/media/audio/audio_output_dispatcher_impl.h b/media/audio/audio_output_dispatcher_impl.h
new file mode 100644
index 0000000..8ad7dda
--- /dev/null
+++ b/media/audio/audio_output_dispatcher_impl.h
@@ -0,0 +1,102 @@
+// Copyright (c) 2012 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// AudioOutputDispatcherImpl is an implementation of AudioOutputDispatcher.
+//
+// To avoid opening and closing audio devices more frequently than necessary,
+// each dispatcher has a pool of inactive physical streams. A stream is closed
+// only if it hasn't been used for a certain period of time (specified via the
+// constructor).
+//
+
+#ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_
+#define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_
+
+#include <list>
+#include <map>
+
+#include "base/basictypes.h"
+#include "base/memory/ref_counted.h"
+#include "base/memory/weak_ptr.h"
+#include "base/timer.h"
+#include "media/audio/audio_io.h"
+#include "media/audio/audio_manager.h"
+#include "media/audio/audio_output_dispatcher.h"
+#include "media/audio/audio_parameters.h"
+
+class MessageLoop;
+
+namespace media {
+
+class AudioOutputProxy;
+
+class MEDIA_EXPORT AudioOutputDispatcherImpl : public AudioOutputDispatcher {
+ public:
+ // |close_delay_ms| specifies delay after the stream is paused until
+ // the audio device is closed.
+ AudioOutputDispatcherImpl(AudioManager* audio_manager,
+ const AudioParameters& params,
+ const base::TimeDelta& close_delay);
+
+ // Opens a new physical stream if there are no pending streams in
+ // |idle_streams_|.
+ virtual bool OpenStream() OVERRIDE;
+
+ // If there are pending streams in |idle_streams_| then it reuses one of
+ // them, otherwise creates a new one.
+ virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback,
+ AudioOutputProxy* stream_proxy) OVERRIDE;
+
+ // Holds the physical stream temporarily in |pausing_streams_| and then
+ // |stream| is added to the pool of pending streams (i.e. |idle_streams_|).
+ virtual void StopStream(AudioOutputProxy* stream_proxy) OVERRIDE;
+
+ virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy,
+ double volume) OVERRIDE;
+
+ virtual void CloseStream(AudioOutputProxy* stream_proxy) OVERRIDE;
+
+ virtual void Shutdown() OVERRIDE;
+
+ private:
+ typedef std::map<AudioOutputProxy*, AudioOutputStream*> AudioStreamMap;
+ friend class base::RefCountedThreadSafe<AudioOutputDispatcherImpl>;
+ virtual ~AudioOutputDispatcherImpl();
+
+ friend class AudioOutputProxyTest;
+
+ // Creates a new physical output stream, opens it and pushes to
+ // |idle_streams_|. Returns false if the stream couldn't be created or
+ // opened.
+ bool CreateAndOpenStream();
+
+ // A task scheduled by StartStream(). Opens a new stream and puts
+ // it in |idle_streams_|.
+ void OpenTask();
+
+ // Before a stream is reused, it should sit idle for a bit. This task is
+ // called once that time has elapsed.
+ void StopStreamTask();
+
+ // Called by |close_timer_|. Closes all pending streams.
+ void ClosePendingStreams();
+
+ base::TimeDelta pause_delay_;
+ size_t paused_proxies_;
+ typedef std::list<AudioOutputStream*> AudioOutputStreamList;
+ AudioOutputStreamList idle_streams_;
+ AudioOutputStreamList pausing_streams_;
+
+ // Used to post delayed tasks to ourselves that we cancel inside Shutdown().
+ base::WeakPtrFactory<AudioOutputDispatcherImpl> weak_this_;
+ base::DelayTimer<AudioOutputDispatcherImpl> close_timer_;
+
+ AudioStreamMap proxy_to_physical_map_;
+
+ DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcherImpl);
+};
+
+} // namespace media
+
+#endif // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_IMPL_H_