Save errno for logging before potentially overwriting it.
[chromium-blink-merge.git] / content / browser / renderer_host / media / audio_renderer_host.h
blobb249ef418ceece4bcbffd7a1cf8bae03c73a1fe6
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.
16 // Here's an example of a typical IPC dialog for audio:
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
35 // A SyncSocket pair is used to signal buffer readiness between processes.
37 #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
38 #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
40 #include <map>
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"
55 namespace media {
56 class AudioManager;
57 class AudioParameters;
60 namespace content {
62 class AudioMirroringManager;
63 class MediaInternals;
64 class MediaStreamManager;
65 class ResourceContext;
67 class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter {
68 public:
69 // Called from UI thread from the owner of this object.
70 AudioRendererHost(int render_process_id,
71 media::AudioManager* audio_manager,
72 AudioMirroringManager* mirroring_manager,
73 MediaInternals* media_internals,
74 MediaStreamManager* media_stream_manager);
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;
82 private:
83 friend class AudioRendererHostTest;
84 friend class BrowserThread;
85 friend class base::DeleteHelper<AudioRendererHost>;
86 friend class MockAudioRendererHost;
87 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream);
88 FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation);
90 class AudioEntry;
91 typedef std::map<int, AudioEntry*> AudioEntryMap;
93 virtual ~AudioRendererHost();
95 // Methods called on IO thread ----------------------------------------------
97 // Audio related IPC message handlers.
99 // Creates an audio output stream with the specified format whose data is
100 // produced by an entity in the render view referenced by |render_view_id|.
101 // |session_id| is used for unified IO to find out which input device to be
102 // opened for the stream. For clients that do not use unified IO,
103 // |session_id| will be ignored.
104 // Upon success/failure, the peer is notified via the NotifyStreamCreated
105 // message.
106 void OnCreateStream(int stream_id,
107 int render_view_id,
108 int session_id,
109 const media::AudioParameters& params);
111 // Play the audio stream referenced by |stream_id|.
112 void OnPlayStream(int stream_id);
114 // Pause the audio stream referenced by |stream_id|.
115 void OnPauseStream(int stream_id);
117 // Close the audio stream referenced by |stream_id|.
118 void OnCloseStream(int stream_id);
120 // Set the volume of the audio stream referenced by |stream_id|.
121 void OnSetVolume(int stream_id, double volume);
123 // Complete the process of creating an audio stream. This will set up the
124 // shared memory or shared socket in low latency mode and send the
125 // NotifyStreamCreated message to the peer.
126 void DoCompleteCreation(AudioEntry* entry);
128 // Propagate audible signal to MediaObserver.
129 void DoNotifyAudibleState(AudioEntry* entry, bool is_audible);
131 // Send an error message to the renderer.
132 void SendErrorMessage(int stream_id);
134 // Delete an audio entry, notifying observers first. This is called by
135 // AudioOutputController after it has closed.
136 void DeleteEntry(scoped_ptr<AudioEntry> entry);
138 // Send an error message to the renderer, then close the stream.
139 void ReportErrorAndClose(int stream_id);
141 // A helper method to look up a AudioEntry identified by |stream_id|.
142 // Returns NULL if not found.
143 AudioEntry* LookupById(int stream_id);
145 // ID of the RenderProcessHost that owns this instance.
146 const int render_process_id_;
148 media::AudioManager* const audio_manager_;
149 AudioMirroringManager* const mirroring_manager_;
150 MediaInternals* const media_internals_;
152 // Used to access to AudioInputDeviceManager.
153 MediaStreamManager* media_stream_manager_;
155 // A map of stream IDs to audio sources.
156 AudioEntryMap audio_entries_;
158 DISALLOW_COPY_AND_ASSIGN(AudioRendererHost);
161 } // namespace content
163 #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_