blob: b19e6e5a173b653bf2708c81dd70d81db96c9bde [file] [log] [blame]
[email protected]f8705112012-07-02 23:24:211// Copyright (c) 2012 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef REMOTING_CLIENT_AUDIO_PLAYER_H_
6#define REMOTING_CLIENT_AUDIO_PLAYER_H_
7
avi5a080f012015-12-22 23:15:438#include <stddef.h>
9#include <stdint.h>
10
[email protected]95674a72012-10-10 02:37:4711#include <list>
dcheng0765c492016-04-06 22:41:5312#include <memory>
[email protected]f8705112012-07-02 23:24:2113
avi5a080f012015-12-22 23:15:4314#include "base/macros.h"
[email protected]95674a72012-10-10 02:37:4715#include "base/synchronization/lock.h"
16#include "remoting/proto/audio.pb.h"
sergeyu77b42df82016-10-04 02:08:3817#include "remoting/protocol/audio_stub.h"
[email protected]f8705112012-07-02 23:24:2118
19namespace remoting {
20
sergeyu77b42df82016-10-04 02:08:3821class AudioPlayer : public protocol::AudioStub {
[email protected]f8705112012-07-02 23:24:2122 public:
yuweihe276cb5d2016-04-14 17:04:4923 // The number of channels in the audio stream (only supporting stereo audio
24 // for now).
25 static const int kChannels = 2;
26 static const int kSampleSizeBytes = 2;
27
nicholsseea696002016-06-20 21:03:3328 ~AudioPlayer() override;
[email protected]f8705112012-07-02 23:24:2129
sergeyu77b42df82016-10-04 02:08:3830 // protocol::AudioStub implementation.
31 void ProcessAudioPacket(std::unique_ptr<AudioPacket> packet,
32 const base::Closure& done) override;
nicholsseea696002016-06-20 21:03:3333
[email protected]f8705112012-07-02 23:24:2134 protected:
[email protected]95674a72012-10-10 02:37:4735 AudioPlayer();
36
37 // Return the recommended number of samples to include in a frame.
avi5a080f012015-12-22 23:15:4338 virtual uint32_t GetSamplesPerFrame() = 0;
[email protected]95674a72012-10-10 02:37:4739
40 // Resets the audio player and starts playback.
41 // Returns true on success.
42 virtual bool ResetAudioPlayer(AudioPacket::SamplingRate sampling_rate) = 0;
43
44 // Function called by the browser when it needs more audio samples.
45 static void AudioPlayerCallback(void* samples,
avi5a080f012015-12-22 23:15:4346 uint32_t buffer_size,
[email protected]95674a72012-10-10 02:37:4747 void* data);
[email protected]f8705112012-07-02 23:24:2148
yuweihe276cb5d2016-04-14 17:04:4949 // Function called by the subclass when it needs more audio samples to fill
50 // its buffer. Will fill the buffer with 0's if no sample is available.
51 void FillWithSamples(void* samples, uint32_t buffer_size);
52
[email protected]f8705112012-07-02 23:24:2153 private:
[email protected]95674a72012-10-10 02:37:4754 friend class AudioPlayerTest;
55
nicholsseea696002016-06-20 21:03:3356 typedef std::list<std::unique_ptr<AudioPacket>> AudioPacketQueue;
[email protected]95674a72012-10-10 02:37:4757
[email protected]5bf9c6e2012-10-11 03:30:2558 void ResetQueue();
[email protected]95674a72012-10-10 02:37:4759
60 AudioPacket::SamplingRate sampling_rate_;
61
62 bool start_failed_;
63
64 // Protects |queued_packets_|, |queued_samples_ and |bytes_consumed_|. This is
65 // necessary to prevent races, because Pepper will call the callback on a
66 // separate thread.
67 base::Lock lock_;
68
69 AudioPacketQueue queued_packets_;
[email protected]b5528a22012-12-29 00:53:2570 int queued_bytes_;
[email protected]95674a72012-10-10 02:37:4771
72 // The number of bytes from |queued_packets_| that have been consumed.
73 size_t bytes_consumed_;
74
[email protected]f8705112012-07-02 23:24:2175 DISALLOW_COPY_AND_ASSIGN(AudioPlayer);
76};
77
78} // namespace remoting
79
80#endif // REMOTING_CLIENT_AUDIO_PLAYER_H_