summaryrefslogtreecommitdiffstats
path: root/remoting/client/audio_player.h
blob: 94de1b4297e25d9ef2bc092ca3819f63f7580e76 (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
// 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.

#ifndef REMOTING_CLIENT_AUDIO_PLAYER_H_
#define REMOTING_CLIENT_AUDIO_PLAYER_H_

#include <stddef.h>
#include <stdint.h>

#include <list>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/synchronization/lock.h"
#include "remoting/proto/audio.pb.h"

namespace remoting {

class AudioPlayer {
 public:
  virtual ~AudioPlayer();

  void ProcessAudioPacket(scoped_ptr<AudioPacket> packet);

 protected:
  AudioPlayer();

  // Return the recommended number of samples to include in a frame.
  virtual uint32_t GetSamplesPerFrame() = 0;

  // Resets the audio player and starts playback.
  // Returns true on success.
  virtual bool ResetAudioPlayer(AudioPacket::SamplingRate sampling_rate) = 0;

  // Function called by the browser when it needs more audio samples.
  static void AudioPlayerCallback(void* samples,
                                  uint32_t buffer_size,
                                  void* data);

 private:
  friend class AudioPlayerTest;

  typedef std::list<AudioPacket*> AudioPacketQueue;

  void ResetQueue();
  void FillWithSamples(void* samples, uint32_t buffer_size);

  AudioPacket::SamplingRate sampling_rate_;

  bool start_failed_;

  // Protects |queued_packets_|, |queued_samples_ and |bytes_consumed_|. This is
  // necessary to prevent races, because Pepper will call the  callback on a
  // separate thread.
  base::Lock lock_;

  AudioPacketQueue queued_packets_;
  int queued_bytes_;

  // The number of bytes from |queued_packets_| that have been consumed.
  size_t bytes_consumed_;

  DISALLOW_COPY_AND_ASSIGN(AudioPlayer);
};

}  // namespace remoting

#endif  // REMOTING_CLIENT_AUDIO_PLAYER_H_