OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_PLAYER_H_ | 5 #ifndef COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_PLAYER_H_ |
6 #define COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_PLAYER_H_ | 6 #define COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_PLAYER_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/gtest_prod_util.h" | |
11 #include "base/macros.h" | 10 #include "base/macros.h" |
12 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
13 #include "base/synchronization/lock.h" | |
14 #include "media/audio/audio_io.h" | |
15 | 12 |
16 namespace media { | 13 namespace media { |
17 class AudioBus; | |
18 class AudioBusRefCounted; | 14 class AudioBusRefCounted; |
19 } | 15 } |
20 | 16 |
21 namespace copresence { | 17 namespace copresence { |
22 | 18 |
23 // The AudioPlayer class will play a set of samples till it is told to stop. | 19 // The AudioPlayerImpl class will play a set of samples till it is told to stop. |
24 class AudioPlayer : public media::AudioOutputStream::AudioSourceCallback { | 20 class AudioPlayer { |
25 public: | 21 public: |
26 AudioPlayer(); | |
27 | |
28 // Initializes the object. Do not use this object before calling this method. | 22 // Initializes the object. Do not use this object before calling this method. |
29 virtual void Initialize(); | 23 virtual void Initialize() = 0; |
30 | 24 |
31 // Play the given samples. These samples will keep on being played in a loop | 25 // Play the given samples. These samples will keep on being played in a loop |
32 // till we explicitly tell the player to stop playing. | 26 // till we explicitly tell the player to stop playing. |
33 virtual void Play(const scoped_refptr<media::AudioBusRefCounted>& samples); | 27 virtual void Play( |
| 28 const scoped_refptr<media::AudioBusRefCounted>& samples) = 0; |
34 | 29 |
35 // Stop playing. | 30 // Stop playing. |
36 virtual void Stop(); | 31 virtual void Stop() = 0; |
37 | 32 |
38 // Cleans up and deletes this object. Do not use object after this call. | 33 // Cleans up and deletes this object. Do not use object after this call. |
39 virtual void Finalize(); | 34 virtual void Finalize() = 0; |
40 | 35 |
41 bool IsPlaying(); | 36 virtual bool IsPlaying() = 0; |
42 | |
43 // Takes ownership of the stream. | |
44 void set_output_stream_for_testing( | |
45 media::AudioOutputStream* output_stream_for_testing) { | |
46 output_stream_for_testing_.reset(output_stream_for_testing); | |
47 } | |
48 | 37 |
49 protected: | 38 protected: |
50 ~AudioPlayer() override; | 39 virtual ~AudioPlayer() {} |
51 void set_is_playing(bool is_playing) { is_playing_ = is_playing; } | |
52 | |
53 private: | |
54 friend class AudioPlayerTest; | |
55 FRIEND_TEST_ALL_PREFIXES(AudioPlayerTest, BasicPlayAndStop); | |
56 FRIEND_TEST_ALL_PREFIXES(AudioPlayerTest, OutOfOrderPlayAndStopMultiple); | |
57 | |
58 // Methods to do our various operations; all of these need to be run on the | |
59 // audio thread. | |
60 void InitializeOnAudioThread(); | |
61 void PlayOnAudioThread( | |
62 const scoped_refptr<media::AudioBusRefCounted>& samples); | |
63 void StopOnAudioThread(); | |
64 void StopAndCloseOnAudioThread(); | |
65 void FinalizeOnAudioThread(); | |
66 | |
67 // AudioOutputStream::AudioSourceCallback overrides: | |
68 // Following methods could be called from *ANY* thread. | |
69 int OnMoreData(media::AudioBus* dest, uint32 total_bytes_delay) override; | |
70 void OnError(media::AudioOutputStream* stream) override; | |
71 | |
72 // Flushes the audio loop, making sure that any queued operations are | |
73 // performed. | |
74 void FlushAudioLoopForTesting(); | |
75 | |
76 bool is_playing_; | |
77 | |
78 // Self-deleting object. | |
79 media::AudioOutputStream* stream_; | |
80 | |
81 scoped_ptr<media::AudioOutputStream> output_stream_for_testing_; | |
82 | |
83 // All fields below here are protected by this lock. | |
84 base::Lock state_lock_; | |
85 | |
86 scoped_refptr<media::AudioBusRefCounted> samples_; | |
87 | |
88 // Index to the frame in the samples that we need to play next. | |
89 int frame_index_; | |
90 | |
91 DISALLOW_COPY_AND_ASSIGN(AudioPlayer); | |
92 }; | 40 }; |
93 | 41 |
94 } // namespace copresence | 42 } // namespace copresence |
95 | 43 |
96 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_PLAYER_H_ | 44 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_PLAYER_H_ |
OLD | NEW |