summaryrefslogtreecommitdiffstats
path: root/media/audio/audio_output_dispatcher.h
diff options
context:
space:
mode:
authorthestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-19 02:48:09 +0000
committerthestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2012-04-19 02:48:09 +0000
commite8e3519eef539f73ed55eb345e21ab409c1d53d4 (patch)
tree72e46c4450b4cdab41c08c172ff58a9f1a395929 /media/audio/audio_output_dispatcher.h
parent62405028eba3eab760007a3e736426927f3f3435 (diff)
downloadchromium_src-e8e3519eef539f73ed55eb345e21ab409c1d53d4.zip
chromium_src-e8e3519eef539f73ed55eb345e21ab409c1d53d4.tar.gz
chromium_src-e8e3519eef539f73ed55eb345e21ab409c1d53d4.tar.bz2
Revert 132891 - 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 TBR=enal@chromium.org Review URL: https://chromiumcodereview.appspot.com/10127002 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@132929 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'media/audio/audio_output_dispatcher.h')
-rw-r--r--media/audio/audio_output_dispatcher.h105
1 files changed, 69 insertions, 36 deletions
diff --git a/media/audio/audio_output_dispatcher.h b/media/audio/audio_output_dispatcher.h
index 5c96873..79474a4 100644
--- a/media/audio/audio_output_dispatcher.h
+++ b/media/audio/audio_output_dispatcher.h
@@ -2,26 +2,33 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
-// AudioOutputDispatcher is a single-threaded base class that dispatches
-// creation and deletion of audio output streams. AudioOutputProxy objects use
-// this class to allocate and recycle actual audio output streams. When playback
-// is started, the proxy calls StartStream() to get an output stream that it
-// uses to play audio. When playback is stopped, the proxy returns the stream
-// back to the dispatcher by calling StopStream().
+// AudioOutputDispatcher is a single-threaded class that dispatches creation and
+// deletion of audio output streams. AudioOutputProxy objects use this class to
+// allocate and recycle actual audio output streams. When playback is started,
+// the proxy calls StreamStarted() to get an output stream that it uses to play
+// audio. When playback is stopped, the proxy returns the stream back to the
+// dispatcher by calling StreamStopped().
//
-// AudioManagerBase creates one specialization of AudioOutputDispatcher on the
-// audio thread for each possible set of audio parameters. I.e streams with
-// different parameters are managed independently. The AudioOutputDispatcher
-// instance is then deleted on the audio thread when the AudioManager shuts
-// down.
+// 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).
+//
+// AudioManagerBase creates one AudioOutputDispatcher on the audio thread for
+// each possible set of audio parameters. I.e streams with different parameters
+// are managed independently. The AudioOutputDispatcher instance is then
+// deleted on the audio thread when the AudioManager shuts down.
#ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_
#define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_
+#include <vector>
+#include <list>
+
#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_parameters.h"
@@ -29,43 +36,60 @@ class MessageLoop;
namespace media {
-class AudioOutputProxy;
+class AudioOutputStream;
class MEDIA_EXPORT AudioOutputDispatcher
: public base::RefCountedThreadSafe<AudioOutputDispatcher> {
public:
+ // |close_delay_ms| specifies delay after the stream is paused until
+ // the audio device is closed.
AudioOutputDispatcher(AudioManager* audio_manager,
- const AudioParameters& params);
+ const AudioParameters& params,
+ base::TimeDelta close_delay);
+ ~AudioOutputDispatcher();
- // Called by AudioOutputProxy to open the stream.
+ // Called by AudioOutputProxy when the stream is closed. Opens a new
+ // physical stream if there are no pending streams in |idle_streams_|.
// Returns false, if it fails to open it.
- virtual bool OpenStream() = 0;
-
- // Called by AudioOutputProxy when the stream is started.
- // Uses |callback| to get source data and report errors, if any.
- // Does *not* take ownership of this callback.
- // Returns true if started successfully, false otherwise.
- virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback,
- AudioOutputProxy* stream_proxy) = 0;
+ bool StreamOpened();
- // Called by AudioOutputProxy when the stream is stopped.
- // Ownership of the |stream_proxy| is passed to the dispatcher.
- virtual void StopStream(AudioOutputProxy* stream_proxy) = 0;
+ // Called by AudioOutputProxy when the stream is started. If there
+ // are pending streams in |idle_streams_| then it returns one of them,
+ // otherwise creates a new one. Returns a physical stream that must
+ // be used, or NULL if it fails to open audio device. Ownership of
+ // the result is passed to the caller.
+ AudioOutputStream* StreamStarted();
-
- // Called by AudioOutputProxy when the volume is set.
- virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy,
- double volume) = 0;
+ // Called by AudioOutputProxy when the stream is stopped. Holds the
+ // stream temporarily in |pausing_streams_| and then |stream| is
+ // added to the pool of pending streams (i.e. |idle_streams_|).
+ // Ownership of the |stream| is passed to the dispatcher.
+ void StreamStopped(AudioOutputStream* stream);
// Called by AudioOutputProxy when the stream is closed.
- virtual void CloseStream(AudioOutputProxy* stream_proxy) = 0;
+ void StreamClosed();
// Called on the audio thread when the AudioManager is shutting down.
- virtual void Shutdown() = 0;
+ void Shutdown();
+
+ private:
+ friend class AudioOutputProxyTest;
- protected:
- friend class base::RefCountedThreadSafe<AudioOutputDispatcher>;
- virtual ~AudioOutputDispatcher();
+ // 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 StreamStarted(). 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 stream.
+ void ClosePendingStreams();
// A no-reference-held pointer (we don't want circular references) back to the
// AudioManager that owns this object.
@@ -73,7 +97,16 @@ class MEDIA_EXPORT AudioOutputDispatcher
MessageLoop* message_loop_;
AudioParameters params_;
- private:
+ 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<AudioOutputDispatcher> weak_this_;
+ base::DelayTimer<AudioOutputDispatcher> close_timer_;
+
DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcher);
};