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