Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / audio / audio_output_stream_sink.h
blob6ab5dc1d0526e4992544aeec927800c91e24c9cc
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 MEDIA_AUDIO_AUDIO_OUTPUT_STREAM_SINK_H_
6 #define MEDIA_AUDIO_AUDIO_OUTPUT_STREAM_SINK_H_
8 #include <string>
10 #include "base/compiler_specific.h"
11 #include "base/single_thread_task_runner.h"
12 #include "base/synchronization/lock.h"
13 #include "media/audio/audio_io.h"
14 #include "media/base/audio_renderer_sink.h"
15 #include "media/base/media_export.h"
17 namespace media {
19 // Wrapper which exposes the browser side audio interface (AudioOutputStream) as
20 // if it were a renderer side audio interface (AudioRendererSink). Note: This
21 // will not work for sandboxed renderers.
23 // TODO(dalecurtis): Delete this class once we have a proper mojo audio service;
24 // tracked by http://crbug.com/425368
25 class MEDIA_EXPORT AudioOutputStreamSink
26 : NON_EXPORTED_BASE(public AudioRendererSink),
27 public AudioOutputStream::AudioSourceCallback {
28 public:
29 AudioOutputStreamSink();
31 // AudioRendererSink implementation.
32 void Initialize(const AudioParameters& params,
33 RenderCallback* callback) override;
34 void Start() override;
35 void Stop() override;
36 void Pause() override;
37 void Play() override;
38 bool SetVolume(double volume) override;
39 OutputDevice* GetOutputDevice() override;
41 // AudioSourceCallback implementation.
42 int OnMoreData(AudioBus* dest, uint32 total_bytes_delay) override;
43 void OnError(AudioOutputStream* stream) override;
45 private:
46 ~AudioOutputStreamSink() override;
48 // Helper methods for running AudioManager methods on the audio thread.
49 void DoStart();
50 void DoStop();
51 void DoPause();
52 void DoPlay();
53 void DoSetVolume(double volume);
55 // Clears |active_render_callback_| under lock, synchronously stopping render
56 // callbacks from any thread. Must be called before Pause() and Stop()
57 // trampoline to their helper methods on the audio thread.
58 void ClearCallback();
60 // Parameters provided by Initialize(), cached for use on other threads.
61 AudioParameters params_;
63 // Since Initialize() is only called once for AudioRenderSinks, save the
64 // callback both here and under |active_render_callback_| which will be
65 // cleared during Pause() and Stop() to achieve "synchronous" stoppage.
66 RenderCallback* render_callback_;
68 // The task runner for the audio thread.
69 const scoped_refptr<base::SingleThreadTaskRunner> audio_task_runner_;
71 // The actual AudioOutputStream, must only be accessed on the audio thread.
72 AudioOutputStream* stream_;
74 // Lock and callback for forwarding OnMoreData() calls into Render() calls.
75 base::Lock callback_lock_;
76 RenderCallback* active_render_callback_;
78 DISALLOW_COPY_AND_ASSIGN(AudioOutputStreamSink);
81 } // namepace media
83 #endif // MEDIA_AUDIO_AUDIO_OUTPUT_STREAM_SINK_H_