38317ef8094fbe59d6200f16115d59fcda5e51fe
[platform/framework/web/crosswalk.git] / src / content / browser / renderer_host / media / audio_renderer_host.h
1 // Copyright (c) 2012 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 // AudioRendererHost serves audio related requests from AudioRenderer which
6 // lives inside the render process and provide access to audio hardware.
7 //
8 // This class is owned by BrowserRenderProcessHost, and instantiated on UI
9 // thread, but all other operations and method calls happen on IO thread, so we
10 // need to be extra careful about the lifetime of this object. AudioManager is a
11 // singleton and created in IO thread, audio output streams are also created in
12 // the IO thread, so we need to destroy them also in IO thread. After this class
13 // is created, a task of OnInitialized() is posted on IO thread in which
14 // singleton of AudioManager is created.
15 //
16 // Here's an example of a typical IPC dialog for audio:
17 //
18 //   Renderer                     AudioRendererHost
19 //      |                               |
20 //      |         CreateStream >        |
21 //      |     < NotifyStreamCreated     |
22 //      |                               |
23 //      |          PlayStream >         |
24 //      |  < NotifyStreamStateChanged   | kAudioStreamPlaying
25 //      |                               |
26 //      |         PauseStream >         |
27 //      |  < NotifyStreamStateChanged   | kAudioStreamPaused
28 //      |                               |
29 //      |          PlayStream >         |
30 //      |  < NotifyStreamStateChanged   | kAudioStreamPlaying
31 //      |             ...               |
32 //      |         CloseStream >         |
33 //      v                               v
34
35 // A SyncSocket pair is used to signal buffer readiness between processes.
36
37 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
38 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
39
40 #include <map>
41
42 #include "base/atomic_ref_count.h"
43 #include "base/gtest_prod_util.h"
44 #include "base/memory/ref_counted.h"
45 #include "base/memory/scoped_ptr.h"
46 #include "base/process/process.h"
47 #include "base/sequenced_task_runner_helpers.h"
48 #include "content/common/content_export.h"
49 #include "content/public/browser/browser_message_filter.h"
50 #include "content/public/browser/browser_thread.h"
51 #include "content/public/browser/render_view_host.h"
52 #include "media/audio/audio_io.h"
53 #include "media/audio/audio_logging.h"
54 #include "media/audio/audio_output_controller.h"
55 #include "media/audio/simple_sources.h"
56
57 namespace media {
58 class AudioManager;
59 class AudioParameters;
60 }
61
62 namespace content {
63
64 class AudioMirroringManager;
65 class MediaInternals;
66 class MediaStreamManager;
67 class ResourceContext;
68
69 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter {
70  public:
71   // Called from UI thread from the owner of this object.
72   AudioRendererHost(int render_process_id,
73                     media::AudioManager* audio_manager,
74                     AudioMirroringManager* mirroring_manager,
75                     MediaInternals* media_internals,
76                     MediaStreamManager* media_stream_manager);
77
78   // Calls |callback| with the list of AudioOutputControllers for this object.
79   void GetOutputControllers(
80       int render_view_id,
81       const RenderViewHost::GetAudioOutputControllersCallback& callback) const;
82
83   // BrowserMessageFilter implementation.
84   virtual void OnChannelClosing() OVERRIDE;
85   virtual void OnDestruct() const OVERRIDE;
86   virtual bool OnMessageReceived(const IPC::Message& message) OVERRIDE;
87
88   // Returns true if any streams managed by this host are actively playing.  Can
89   // be called from any thread.
90   bool HasActiveAudio();
91
92  private:
93   friend class AudioRendererHostTest;
94   friend class BrowserThread;
95   friend class base::DeleteHelper<AudioRendererHost>;
96   friend class MockAudioRendererHost;
97   friend class TestAudioRendererHost;
98   FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream);
99   FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation);
100
101   class AudioEntry;
102   typedef std::map<int, AudioEntry*> AudioEntryMap;
103
104   virtual ~AudioRendererHost();
105
106   // Methods called on IO thread ----------------------------------------------
107
108   // Audio related IPC message handlers.
109
110   // Creates an audio output stream with the specified format whose data is
111   // produced by an entity in the render view referenced by |render_view_id|.
112   // |session_id| is used for unified IO to find out which input device to be
113   // opened for the stream. For clients that do not use unified IO,
114   // |session_id| will be ignored.
115   // Upon success/failure, the peer is notified via the NotifyStreamCreated
116   // message.
117   void OnCreateStream(int stream_id,
118                       int render_view_id,
119                       int render_frame_id,
120                       int session_id,
121                       const media::AudioParameters& params);
122
123   // Play the audio stream referenced by |stream_id|.
124   void OnPlayStream(int stream_id);
125
126   // Pause the audio stream referenced by |stream_id|.
127   void OnPauseStream(int stream_id);
128
129   // Close the audio stream referenced by |stream_id|.
130   void OnCloseStream(int stream_id);
131
132   // Set the volume of the audio stream referenced by |stream_id|.
133   void OnSetVolume(int stream_id, double volume);
134
135   // Complete the process of creating an audio stream. This will set up the
136   // shared memory or shared socket in low latency mode and send the
137   // NotifyStreamCreated message to the peer.
138   void DoCompleteCreation(int stream_id);
139
140   // Send playing/paused status to the renderer.
141   void DoNotifyStreamStateChanged(int stream_id, bool is_playing);
142
143   RenderViewHost::AudioOutputControllerList DoGetOutputControllers(
144       int render_view_id) const;
145
146   // Send an error message to the renderer.
147   void SendErrorMessage(int stream_id);
148
149   // Delete an audio entry, notifying observers first.  This is called by
150   // AudioOutputController after it has closed.
151   void DeleteEntry(scoped_ptr<AudioEntry> entry);
152
153   // Send an error message to the renderer, then close the stream.
154   void ReportErrorAndClose(int stream_id);
155
156   // A helper method to look up a AudioEntry identified by |stream_id|.
157   // Returns NULL if not found.
158   AudioEntry* LookupById(int stream_id);
159
160   // ID of the RenderProcessHost that owns this instance.
161   const int render_process_id_;
162
163   media::AudioManager* const audio_manager_;
164   AudioMirroringManager* const mirroring_manager_;
165   scoped_ptr<media::AudioLog> audio_log_;
166
167   // Used to access to AudioInputDeviceManager.
168   MediaStreamManager* media_stream_manager_;
169
170   // A map of stream IDs to audio sources.
171   AudioEntryMap audio_entries_;
172
173   // The number of streams in the playing state.
174   base::AtomicRefCount num_playing_streams_;
175
176   DISALLOW_COPY_AND_ASSIGN(AudioRendererHost);
177 };
178
179 }  // namespace content
180
181 #endif  // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_