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