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_
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"
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
27 public media::AudioOutputStream::AudioSourceCallback
{
31 // AudioPlayer overrides:
32 void Initialize() override
;
33 void Play(const scoped_refptr
<media::AudioBusRefCounted
>& samples
) 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
);
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
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
66 void FlushAudioLoopForTesting();
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.
83 DISALLOW_COPY_AND_ASSIGN(AudioPlayerImpl
);
86 } // namespace audio_modem
88 #endif // COMPONENTS_AUDIO_MODEM_AUDIO_PLAYER_IMPL_H_