audio_renderer_host.h revision 2a99a7e74a7f215066514fe81d2bfa6639d9eddd
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.h"
46#include "base/sequenced_task_runner_helpers.h"
47#include "base/shared_memory.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 "media/audio/audio_io.h"
52#include "media/audio/audio_output_controller.h"
53#include "media/audio/simple_sources.h"
54
55namespace media {
56class AudioManager;
57class AudioParameters;
58}
59
60namespace content {
61
62class AudioMirroringManager;
63class MediaInternals;
64class ResourceContext;
65
66class CONTENT_EXPORT AudioRendererHost
67    : public BrowserMessageFilter,
68      public media::AudioOutputController::EventHandler {
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
76  // BrowserMessageFilter implementation.
77  virtual void OnChannelClosing() OVERRIDE;
78  virtual void OnDestruct() const OVERRIDE;
79  virtual bool OnMessageReceived(const IPC::Message& message,
80                                 bool* message_was_ok) OVERRIDE;
81
82  // AudioOutputController::EventHandler implementations.
83  virtual void OnCreated(media::AudioOutputController* controller) OVERRIDE;
84  virtual void OnPlaying(media::AudioOutputController* controller) OVERRIDE;
85  virtual void OnPaused(media::AudioOutputController* controller) OVERRIDE;
86  virtual void OnError(media::AudioOutputController* controller) OVERRIDE;
87  virtual void OnDeviceChange(media::AudioOutputController* controller,
88                              int new_buffer_size,
89                              int new_sample_rate) OVERRIDE;
90
91 private:
92  friend class AudioRendererHostTest;
93  friend class BrowserThread;
94  friend class base::DeleteHelper<AudioRendererHost>;
95  friend class MockAudioRendererHost;
96  FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream);
97  FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation);
98
99  struct AudioEntry;
100  typedef std::map<int, AudioEntry*> AudioEntryMap;
101
102  virtual ~AudioRendererHost();
103
104  // Methods called on IO thread ----------------------------------------------
105
106  // Audio related IPC message handlers.
107  // Creates an audio output stream with the specified format. If this call is
108  // successful this object would keep an internal entry of the stream for the
109  // required properties.
110  void OnCreateStream(int stream_id,
111                      const media::AudioParameters& params);
112
113  // Track that the data for the audio stream referenced by |stream_id| is
114  // produced by an entity in the render view referenced by |render_view_id|.
115  void OnAssociateStreamWithProducer(int stream_id, int render_view_id);
116
117  // Play the audio stream referenced by |stream_id|.
118  void OnPlayStream(int stream_id);
119
120  // Pause the audio stream referenced by |stream_id|.
121  void OnPauseStream(int stream_id);
122
123  // Discard all audio data in stream referenced by |stream_id|.
124  void OnFlushStream(int stream_id);
125
126  // Close the audio stream referenced by |stream_id|.
127  void OnCloseStream(int stream_id);
128
129  // Set the volume of the audio stream referenced by |stream_id|.
130  void OnSetVolume(int stream_id, double volume);
131
132  // Complete the process of creating an audio stream. This will set up the
133  // shared memory or shared socket in low latency mode.
134  void DoCompleteCreation(media::AudioOutputController* controller);
135
136  // Send a state change message to the renderer.
137  void DoSendPlayingMessage(media::AudioOutputController* controller);
138  void DoSendPausedMessage(media::AudioOutputController* controller);
139  void DoSendDeviceChangeMessage(media::AudioOutputController* controller,
140                                 int new_buffer_size, int new_sample_rate);
141
142  // Handle error coming from audio stream.
143  void DoHandleError(media::AudioOutputController* controller);
144
145  // Send an error message to the renderer.
146  void SendErrorMessage(int stream_id);
147
148  // Delete all audio entry and all audio streams
149  void DeleteEntries();
150
151  // Closes the stream. The stream is then deleted in DeleteEntry() after it
152  // is closed.
153  void CloseAndDeleteStream(AudioEntry* entry);
154
155  // Delete an audio entry and close the related audio stream.
156  void DeleteEntry(AudioEntry* entry);
157
158  // Delete audio entry and close the related audio stream due to an error,
159  // and error message is send to the renderer.
160  void DeleteEntryOnError(AudioEntry* entry);
161
162  // A helper method to look up a AudioEntry identified by |stream_id|.
163  // Returns NULL if not found.
164  AudioEntry* LookupById(int stream_id);
165
166  // Search for a AudioEntry having the reference to |controller|.
167  // This method is used to look up an AudioEntry after a controller
168  // event is received.
169  AudioEntry* LookupByController(media::AudioOutputController* controller);
170
171  media::AudioOutputController* LookupControllerByIdForTesting(int stream_id);
172
173  // ID of the RenderProcessHost that owns this instance.
174  const int render_process_id_;
175
176  media::AudioManager* const audio_manager_;
177  AudioMirroringManager* const mirroring_manager_;
178  MediaInternals* const media_internals_;
179
180  // A map of stream IDs to audio sources.
181  AudioEntryMap audio_entries_;
182
183  DISALLOW_COPY_AND_ASSIGN(AudioRendererHost);
184};
185
186}  // namespace content
187
188#endif  // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
189