audio_renderer_host.h revision f2477e01787aa58f445919b809d89e252beef54f
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/gtest_prod_util.h"
43#include "base/memory/ref_counted.h"
44#include "base/memory/scoped_ptr.h"
45#include "base/process/process.h"
46#include "base/sequenced_task_runner_helpers.h"
47#include "content/common/content_export.h"
48#include "content/public/browser/browser_message_filter.h"
49#include "content/public/browser/browser_thread.h"
50#include "content/public/browser/render_view_host.h"
51#include "media/audio/audio_io.h"
52#include "media/audio/audio_logging.h"
53#include "media/audio/audio_output_controller.h"
54#include "media/audio/simple_sources.h"
55
56namespace media {
57class AudioManager;
58class AudioParameters;
59}
60
61namespace content {
62
63class AudioMirroringManager;
64class MediaInternals;
65class MediaStreamManager;
66class ResourceContext;
67
68class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter {
69 public:
70  // Called from UI thread from the owner of this object.
71  AudioRendererHost(int render_process_id,
72                    media::AudioManager* audio_manager,
73                    AudioMirroringManager* mirroring_manager,
74                    MediaInternals* media_internals,
75                    MediaStreamManager* media_stream_manager);
76
77  // Calls |callback| with the list of AudioOutputControllers for this object.
78  void GetOutputControllers(
79      int render_view_id,
80      const RenderViewHost::GetAudioOutputControllersCallback& callback) const;
81
82  // BrowserMessageFilter implementation.
83  virtual void OnChannelClosing() OVERRIDE;
84  virtual void OnDestruct() const OVERRIDE;
85  virtual bool OnMessageReceived(const IPC::Message& message,
86                                 bool* message_was_ok) OVERRIDE;
87
88 private:
89  friend class AudioRendererHostTest;
90  friend class BrowserThread;
91  friend class base::DeleteHelper<AudioRendererHost>;
92  friend class MockAudioRendererHost;
93  friend class TestAudioRendererHost;
94  FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream);
95  FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation);
96
97  class AudioEntry;
98  typedef std::map<int, AudioEntry*> AudioEntryMap;
99
100  virtual ~AudioRendererHost();
101
102  // Methods called on IO thread ----------------------------------------------
103
104  // Audio related IPC message handlers.
105
106  // Creates an audio output stream with the specified format whose data is
107  // produced by an entity in the render view referenced by |render_view_id|.
108  // |session_id| is used for unified IO to find out which input device to be
109  // opened for the stream. For clients that do not use unified IO,
110  // |session_id| will be ignored.
111  // Upon success/failure, the peer is notified via the NotifyStreamCreated
112  // message.
113  void OnCreateStream(int stream_id,
114                      int render_view_id,
115                      int session_id,
116                      const media::AudioParameters& params);
117
118  // Play the audio stream referenced by |stream_id|.
119  void OnPlayStream(int stream_id);
120
121  // Pause the audio stream referenced by |stream_id|.
122  void OnPauseStream(int stream_id);
123
124  // Close the audio stream referenced by |stream_id|.
125  void OnCloseStream(int stream_id);
126
127  // Set the volume of the audio stream referenced by |stream_id|.
128  void OnSetVolume(int stream_id, double volume);
129
130  // Complete the process of creating an audio stream. This will set up the
131  // shared memory or shared socket in low latency mode and send the
132  // NotifyStreamCreated message to the peer.
133  void DoCompleteCreation(int stream_id);
134
135  RenderViewHost::AudioOutputControllerList DoGetOutputControllers(
136      int render_view_id) const;
137
138  // Propagate measured power level of the audio signal to MediaObserver.
139  void DoNotifyAudioPowerLevel(int stream_id, float power_dbfs, bool clipped);
140
141  // Send an error message to the renderer.
142  void SendErrorMessage(int stream_id);
143
144  // Delete an audio entry, notifying observers first.  This is called by
145  // AudioOutputController after it has closed.
146  void DeleteEntry(scoped_ptr<AudioEntry> entry);
147
148  // Send an error message to the renderer, then close the stream.
149  void ReportErrorAndClose(int stream_id);
150
151  // A helper method to look up a AudioEntry identified by |stream_id|.
152  // Returns NULL if not found.
153  AudioEntry* LookupById(int stream_id);
154
155  // ID of the RenderProcessHost that owns this instance.
156  const int render_process_id_;
157
158  media::AudioManager* const audio_manager_;
159  AudioMirroringManager* const mirroring_manager_;
160  scoped_ptr<media::AudioLog> audio_log_;
161
162  // Used to access to AudioInputDeviceManager.
163  MediaStreamManager* media_stream_manager_;
164
165  // A map of stream IDs to audio sources.
166  AudioEntryMap audio_entries_;
167
168  DISALLOW_COPY_AND_ASSIGN(AudioRendererHost);
169};
170
171}  // namespace content
172
173#endif  // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
174