summaryrefslogtreecommitdiff
path: root/chromium/media/audio/audio_output_dispatcher.h
blob: 30266ed6a9a509be6d834d583010afdcb841039d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 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.

// 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().
//
// 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.

#ifndef MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_
#define MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_

#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/timer/timer.h"
#include "media/audio/audio_io.h"
#include "media/audio/audio_manager.h"
#include "media/audio/audio_parameters.h"

namespace base {
class MessageLoop;
}

namespace media {

class AudioOutputProxy;

class MEDIA_EXPORT AudioOutputDispatcher
    : public base::RefCountedThreadSafe<AudioOutputDispatcher> {
 public:
  AudioOutputDispatcher(AudioManager* audio_manager,
                        const AudioParameters& params,
                        const std::string& output_device_id,
                        const std::string& input_device_id);

  // Called by AudioOutputProxy to open the stream.
  // 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;

  // 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 volume is set.
  virtual void StreamVolumeSet(AudioOutputProxy* stream_proxy,
                               double volume) = 0;

  // Called by AudioOutputProxy when the stream is closed.
  virtual void CloseStream(AudioOutputProxy* stream_proxy) = 0;

  // Called on the audio thread when the AudioManager is shutting down.
  virtual void Shutdown() = 0;

  // Accessor to the input device id used by unified IO.
  const std::string& input_device_id() const { return input_device_id_; }

 protected:
  friend class base::RefCountedThreadSafe<AudioOutputDispatcher>;
  friend class AudioOutputProxyTest;

  virtual ~AudioOutputDispatcher();

  // A no-reference-held pointer (we don't want circular references) back to the
  // AudioManager that owns this object.
  AudioManager* audio_manager_;
  base::MessageLoop* message_loop_;
  AudioParameters params_;
  const std::string output_device_id_;
  const std::string input_device_id_;

 private:
  DISALLOW_COPY_AND_ASSIGN(AudioOutputDispatcher);
};

}  // namespace media

#endif  // MEDIA_AUDIO_AUDIO_OUTPUT_DISPATCHER_H_