b746500597bfad368367db39df5d2a67edf7ed70
[platform/framework/web/crosswalk.git] / src / content / browser / media / capture / web_contents_audio_input_stream.h
1 // Copyright (c) 2013 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 // An AudioInputStream which provides a loop-back of all audio output generated
6 // by the entire RenderFrame tree associated with a WebContents instance.  The
7 // single stream of data is produced by format-converting and mixing all audio
8 // output streams.  As the RenderFrameHost tree mutates (e.g., due to page
9 // navigations, or crashes/reloads), the stream will continue without
10 // interruption.  In other words, WebContentsAudioInputStream provides tab-level
11 // audio mirroring.
12
13 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_AUDIO_INPUT_STREAM_H_
14 #define CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_AUDIO_INPUT_STREAM_H_
15
16 #include <string>
17
18 #include "base/memory/ref_counted.h"
19 #include "content/common/content_export.h"
20 #include "media/audio/audio_io.h"
21
22 namespace base {
23 class SingleThreadTaskRunner;
24 }
25
26 namespace media {
27 class AudioParameters;
28 class VirtualAudioInputStream;
29 }
30
31 namespace content {
32
33 class AudioMirroringManager;
34 class WebContentsTracker;
35
36 class CONTENT_EXPORT WebContentsAudioInputStream
37     : NON_EXPORTED_BASE(public media::AudioInputStream) {
38  public:
39   // media::AudioInputStream implementation
40   virtual bool Open() OVERRIDE;
41   virtual void Start(AudioInputCallback* callback) OVERRIDE;
42   virtual void Stop() OVERRIDE;
43   virtual void Close() OVERRIDE;
44   virtual double GetMaxVolume() OVERRIDE;
45   virtual void SetVolume(double volume) OVERRIDE;
46   virtual double GetVolume() OVERRIDE;
47   virtual void SetAutomaticGainControl(bool enabled) OVERRIDE;
48   virtual bool GetAutomaticGainControl() OVERRIDE;
49
50   // Create a new audio mirroring session, or return NULL on error.  |device_id|
51   // should be in the format accepted by
52   // WebContentsCaptureUtil::ExtractTabCaptureTarget().  The caller must
53   // guarantee Close() is called on the returned object so that it may
54   // self-destruct.
55   // |worker_task_runner| is the task runner on which AudioInputCallback methods
56   // are called and may or may not be the single thread that invokes the
57   // AudioInputStream methods.
58   static WebContentsAudioInputStream* Create(
59       const std::string& device_id,
60       const media::AudioParameters& params,
61       const scoped_refptr<base::SingleThreadTaskRunner>& worker_task_runner,
62       AudioMirroringManager* audio_mirroring_manager);
63
64  private:
65   friend class WebContentsAudioInputStreamTest;
66
67   // Maintain most state and functionality in an internal ref-counted
68   // implementation class.  This object must outlive a call to Close(), until
69   // the shutdown tasks running on other threads complete: The
70   // AudioMirroringManager on the IO thread, the WebContentsTracker on the UI
71   // thread, and the VirtualAudioOuputStreams on the audio thread.
72   class Impl;
73
74   WebContentsAudioInputStream(
75       int render_process_id, int main_render_frame_id,
76       AudioMirroringManager* mirroring_manager,
77       const scoped_refptr<WebContentsTracker>& tracker,
78       media::VirtualAudioInputStream* mixer_stream);
79
80   virtual ~WebContentsAudioInputStream();
81
82   scoped_refptr<Impl> impl_;
83
84   DISALLOW_COPY_AND_ASSIGN(WebContentsAudioInputStream);
85 };
86
87 }  // namespace content
88
89 #endif  // CONTENT_BROWSER_MEDIA_CAPTURE_WEB_CONTENTS_AUDIO_INPUT_STREAM_H_