summaryrefslogtreecommitdiffstats
path: root/media/audio/audio_output_mixer.h
blob: 4ddeeef282905720857da4e3d40ee283a88429a9 (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
93
94
// 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.

// AudioOutputMixer is a class that implements browser-side audio mixer.
// AudioOutputMixer implements both AudioOutputDispatcher and
// AudioSourceCallback interfaces.

#ifndef MEDIA_AUDIO_AUDIO_OUTPUT_MIXER_H_
#define MEDIA_AUDIO_AUDIO_OUTPUT_MIXER_H_

#include <map>

#include "base/basictypes.h"
#include "base/memory/ref_counted.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/synchronization/lock.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"

namespace media {

class MEDIA_EXPORT AudioOutputMixer
    : public AudioOutputDispatcher,
      public AudioOutputStream::AudioSourceCallback {
 public:
  AudioOutputMixer(AudioManager* audio_manager,
                   const AudioParameters& params,
                   const base::TimeDelta& close_delay);

  // AudioOutputDispatcher interface.
  virtual bool OpenStream() OVERRIDE;
  virtual bool StartStream(AudioOutputStream::AudioSourceCallback* callback,
                           AudioOutputProxy* stream_proxy) OVERRIDE;
  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;

  // AudioSourceCallback interface.
  virtual uint32 OnMoreData(uint8* dest,
                            uint32 max_size,
                            AudioBuffersState buffers_state) OVERRIDE;
  virtual void OnError(AudioOutputStream* stream, int code) OVERRIDE;
  virtual void WaitTillDataReady() OVERRIDE;

 private:
  friend class base::RefCountedThreadSafe<AudioOutputMixer>;
  virtual ~AudioOutputMixer();

  // Called by |close_timer_|. Closes physical stream.
  void ClosePhysicalStream();

  // The |lock_| must be acquired whenever we modify |proxies_| in the audio
  // manager thread or accessing it in the hardware audio thread. Read in the
  // audio manager thread is safe.
  base::Lock lock_;

  // List of audio output proxies currently being played.
  // For every proxy we store aux structure containing data necessary for
  // mixing.
  struct ProxyData {
    AudioOutputStream::AudioSourceCallback* audio_source_callback;
    double volume;
    int pending_bytes;
  };
  typedef std::map<AudioOutputProxy*, ProxyData> ProxyMap;
  ProxyMap proxies_;

  // Physical stream for this mixer.
  scoped_ptr<AudioOutputStream> physical_stream_;

  // Temporary buffer used when mixing. Allocated in the constructor
  // to avoid constant allocation/deallocation in the callback.
  scoped_array<uint8> mixer_data_;

  // Used to post delayed tasks to ourselves that we cancel inside Shutdown().
  base::WeakPtrFactory<AudioOutputMixer> weak_this_;
  base::DelayTimer<AudioOutputMixer> close_timer_;

  // Size of data in all in-flight buffers.
  int pending_bytes_;

  DISALLOW_COPY_AND_ASSIGN(AudioOutputMixer);
};

}  // namespace media

#endif  // MEDIA_AUDIO_AUDIO_OUTPUT_MIXER_H_