[email protected] | f870511 | 2012-07-02 23:24:21 | [diff] [blame] | 1 | // 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 | |||||
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 8 | #include <stddef.h> |
9 | #include <stdint.h> | ||||
10 | |||||
[email protected] | 95674a7 | 2012-10-10 02:37:47 | [diff] [blame] | 11 | #include <list> |
dcheng | 0765c49 | 2016-04-06 22:41:53 | [diff] [blame] | 12 | #include <memory> |
[email protected] | f870511 | 2012-07-02 23:24:21 | [diff] [blame] | 13 | |
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 14 | #include "base/macros.h" |
[email protected] | 95674a7 | 2012-10-10 02:37:47 | [diff] [blame] | 15 | #include "base/synchronization/lock.h" |
16 | #include "remoting/proto/audio.pb.h" | ||||
sergeyu | 77b42df8 | 2016-10-04 02:08:38 | [diff] [blame] | 17 | #include "remoting/protocol/audio_stub.h" |
[email protected] | f870511 | 2012-07-02 23:24:21 | [diff] [blame] | 18 | |
19 | namespace remoting { | ||||
20 | |||||
sergeyu | 77b42df8 | 2016-10-04 02:08:38 | [diff] [blame] | 21 | class AudioPlayer : public protocol::AudioStub { |
[email protected] | f870511 | 2012-07-02 23:24:21 | [diff] [blame] | 22 | public: |
yuweih | e276cb5d | 2016-04-14 17:04:49 | [diff] [blame] | 23 | // 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 | |||||
nicholss | eea69600 | 2016-06-20 21:03:33 | [diff] [blame] | 28 | ~AudioPlayer() override; |
[email protected] | f870511 | 2012-07-02 23:24:21 | [diff] [blame] | 29 | |
sergeyu | 77b42df8 | 2016-10-04 02:08:38 | [diff] [blame] | 30 | // protocol::AudioStub implementation. |
31 | void ProcessAudioPacket(std::unique_ptr<AudioPacket> packet, | ||||
32 | const base::Closure& done) override; | ||||
nicholss | eea69600 | 2016-06-20 21:03:33 | [diff] [blame] | 33 | |
[email protected] | f870511 | 2012-07-02 23:24:21 | [diff] [blame] | 34 | protected: |
[email protected] | 95674a7 | 2012-10-10 02:37:47 | [diff] [blame] | 35 | AudioPlayer(); |
36 | |||||
37 | // Return the recommended number of samples to include in a frame. | ||||
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 38 | virtual uint32_t GetSamplesPerFrame() = 0; |
[email protected] | 95674a7 | 2012-10-10 02:37:47 | [diff] [blame] | 39 | |
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, | ||||
avi | 5a080f01 | 2015-12-22 23:15:43 | [diff] [blame] | 46 | uint32_t buffer_size, |
[email protected] | 95674a7 | 2012-10-10 02:37:47 | [diff] [blame] | 47 | void* data); |
[email protected] | f870511 | 2012-07-02 23:24:21 | [diff] [blame] | 48 | |
yuweih | e276cb5d | 2016-04-14 17:04:49 | [diff] [blame] | 49 | // 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] | f870511 | 2012-07-02 23:24:21 | [diff] [blame] | 53 | private: |
[email protected] | 95674a7 | 2012-10-10 02:37:47 | [diff] [blame] | 54 | friend class AudioPlayerTest; |
55 | |||||
nicholss | eea69600 | 2016-06-20 21:03:33 | [diff] [blame] | 56 | typedef std::list<std::unique_ptr<AudioPacket>> AudioPacketQueue; |
[email protected] | 95674a7 | 2012-10-10 02:37:47 | [diff] [blame] | 57 | |
[email protected] | 5bf9c6e | 2012-10-11 03:30:25 | [diff] [blame] | 58 | void ResetQueue(); |
[email protected] | 95674a7 | 2012-10-10 02:37:47 | [diff] [blame] | 59 | |
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] | b5528a2 | 2012-12-29 00:53:25 | [diff] [blame] | 70 | int queued_bytes_; |
[email protected] | 95674a7 | 2012-10-10 02:37:47 | [diff] [blame] | 71 | |
72 | // The number of bytes from |queued_packets_| that have been consumed. | ||||
73 | size_t bytes_consumed_; | ||||
74 | |||||
[email protected] | f870511 | 2012-07-02 23:24:21 | [diff] [blame] | 75 | DISALLOW_COPY_AND_ASSIGN(AudioPlayer); |
76 | }; | ||||
77 | |||||
78 | } // namespace remoting | ||||
79 | |||||
80 | #endif // REMOTING_CLIENT_AUDIO_PLAYER_H_ |