[Sync] Componentize UIModelWorker.
[chromium-blink-merge.git] / components / audio_modem / audio_player_impl.h
blob145dab6164521098b8b984ec6c0d2685c0883c70
1 // Copyright 2015 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_AUDIO_MODEM_AUDIO_PLAYER_IMPL_H_
6 #define COMPONENTS_AUDIO_MODEM_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/audio_modem/audio_player.h"
15 #include "media/audio/audio_io.h"
17 namespace media {
18 class AudioBus;
19 class AudioBusRefCounted;
22 namespace audio_modem {
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 audio_modem
88 #endif // COMPONENTS_AUDIO_MODEM_AUDIO_PLAYER_IMPL_H_