Move documentScan.html from extensions to apps
[chromium-blink-merge.git] / components / copresence / mediums / audio / audio_player_impl.h
blob5b6c67d22c17f31ecaa479b75b521e0e26865b37
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.
5 #ifndef COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_PLAYER_IMPL_H_
6 #define COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_PLAYER_IMPL_H_
8 #include <vector>
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 "components/copresence/mediums/audio/audio_player.h"
15 #include "media/audio/audio_io.h"
17 namespace media {
18 class AudioBus;
19 class AudioBusRefCounted;
22 namespace copresence {
24 // The AudioPlayerImpl class will play a set of samples till it is told to stop.
25 class AudioPlayerImpl final
26 : public AudioPlayer,
27 public media::AudioOutputStream::AudioSourceCallback {
28 public:
29 AudioPlayerImpl();
31 // AudioPlayer overrides:
32 void Initialize() override;
33 void Play(const scoped_refptr<media::AudioBusRefCounted>& samples) override;
34 void Stop() override;
35 void Finalize() override;
37 // Takes ownership of the stream.
38 void set_output_stream_for_testing(
39 media::AudioOutputStream* output_stream_for_testing) {
40 output_stream_for_testing_.reset(output_stream_for_testing);
43 private:
44 friend class AudioPlayerTest;
45 FRIEND_TEST_ALL_PREFIXES(AudioPlayerTest, BasicPlayAndStop);
46 FRIEND_TEST_ALL_PREFIXES(AudioPlayerTest, OutOfOrderPlayAndStopMultiple);
48 ~AudioPlayerImpl() override;
50 // Methods to do our various operations; all of these need to be run on the
51 // audio thread.
52 void InitializeOnAudioThread();
53 void PlayOnAudioThread(
54 const scoped_refptr<media::AudioBusRefCounted>& samples);
55 void StopOnAudioThread();
56 void StopAndCloseOnAudioThread();
57 void FinalizeOnAudioThread();
59 // AudioOutputStream::AudioSourceCallback overrides:
60 // Following methods could be called from *ANY* thread.
61 int OnMoreData(media::AudioBus* dest, uint32 total_bytes_delay) override;
62 void OnError(media::AudioOutputStream* stream) override;
64 // Flushes the audio loop, making sure that any queued operations are
65 // performed.
66 void FlushAudioLoopForTesting();
68 bool is_playing_;
70 // Self-deleting object.
71 media::AudioOutputStream* stream_;
73 scoped_ptr<media::AudioOutputStream> output_stream_for_testing_;
75 // All fields below here are protected by this lock.
76 base::Lock state_lock_;
78 scoped_refptr<media::AudioBusRefCounted> samples_;
80 // Index to the frame in the samples that we need to play next.
81 int frame_index_;
83 DISALLOW_COPY_AND_ASSIGN(AudioPlayerImpl);
86 } // namespace copresence
88 #endif // COMPONENTS_COPRESENCE_MEDIUMS_AUDIO_AUDIO_PLAYER_IMPL_H_