PageRenderTime 47ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/content/browser/renderer_host/media/audio_renderer_host.h

https://gitlab.com/jonnialva90/iridium-browser
C Header | 315 lines | 142 code | 57 blank | 116 comment | 0 complexity | 8a134188090b0b4bc4fdaf3c1821c351 MD5 | raw file
  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 RenderProcessHostImpl, 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. // | RequestDeviceAuthorization > |
  21. // | < NotifyDeviceAuthorized |
  22. // | |
  23. // | CreateStream > |
  24. // | < NotifyStreamCreated |
  25. // | |
  26. // | PlayStream > |
  27. // | < NotifyStreamStateChanged | kAudioStreamPlaying
  28. // | |
  29. // | PauseStream > |
  30. // | < NotifyStreamStateChanged | kAudioStreamPaused
  31. // | |
  32. // | PlayStream > |
  33. // | < NotifyStreamStateChanged | kAudioStreamPlaying
  34. // | ... |
  35. // | CloseStream > |
  36. // v v
  37. // A SyncSocket pair is used to signal buffer readiness between processes.
  38. #ifndef CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
  39. #define CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_
  40. #include <map>
  41. #include <string>
  42. #include <utility>
  43. #include "base/atomic_ref_count.h"
  44. #include "base/gtest_prod_util.h"
  45. #include "base/memory/ref_counted.h"
  46. #include "base/memory/scoped_ptr.h"
  47. #include "base/process/process.h"
  48. #include "base/sequenced_task_runner_helpers.h"
  49. #include "content/browser/renderer_host/media/audio_output_device_enumerator.h"
  50. #include "content/common/content_export.h"
  51. #include "content/public/browser/browser_message_filter.h"
  52. #include "content/public/browser/browser_thread.h"
  53. #include "content/public/browser/render_process_host.h"
  54. #include "content/public/browser/resource_context.h"
  55. #include "media/audio/audio_io.h"
  56. #include "media/audio/audio_logging.h"
  57. #include "media/audio/audio_output_controller.h"
  58. #include "media/audio/simple_sources.h"
  59. #include "url/origin.h"
  60. namespace media {
  61. class AudioManager;
  62. class AudioParameters;
  63. }
  64. namespace content {
  65. class AudioMirroringManager;
  66. class MediaInternals;
  67. class MediaStreamManager;
  68. class MediaStreamUIProxy;
  69. class ResourceContext;
  70. class CONTENT_EXPORT AudioRendererHost : public BrowserMessageFilter {
  71. public:
  72. // Called from UI thread from the owner of this object.
  73. AudioRendererHost(int render_process_id,
  74. media::AudioManager* audio_manager,
  75. AudioMirroringManager* mirroring_manager,
  76. MediaInternals* media_internals,
  77. MediaStreamManager* media_stream_manager,
  78. const ResourceContext::SaltCallback& salt_callback);
  79. // Calls |callback| with the list of AudioOutputControllers for this object.
  80. void GetOutputControllers(
  81. const RenderProcessHost::GetAudioOutputControllersCallback&
  82. callback) const;
  83. // BrowserMessageFilter implementation.
  84. void OnChannelClosing() override;
  85. void OnDestruct() const override;
  86. bool OnMessageReceived(const IPC::Message& message) override;
  87. // Returns true if any streams managed by this host are actively playing. Can
  88. // be called from any thread.
  89. bool HasActiveAudio();
  90. // Returns true if any streams managed by the RenderFrame identified by
  91. // |render_frame_id| are actively playing. Can be called from any thread.
  92. bool RenderFrameHasActiveAudio(int render_frame_id) const;
  93. private:
  94. friend class AudioRendererHostTest;
  95. friend class BrowserThread;
  96. friend class base::DeleteHelper<AudioRendererHost>;
  97. friend class MockAudioRendererHost;
  98. friend class TestAudioRendererHost;
  99. FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, CreateMockStream);
  100. FRIEND_TEST_ALL_PREFIXES(AudioRendererHostTest, MockStreamDataConversation);
  101. class AudioEntry;
  102. typedef std::map<int, AudioEntry*> AudioEntryMap;
  103. // Internal callback type for access requests to output devices.
  104. // |have_access| is true only if there is permission to access the device.
  105. typedef base::Callback<void(bool have_access)> OutputDeviceAccessCB;
  106. // Internal callback type for information requests about an output device.
  107. // |success| indicates the operation was successful. If true, |device_info|
  108. // contains data about the device.
  109. typedef base::Callback<void(bool success,
  110. const AudioOutputDeviceInfo& device_info)>
  111. OutputDeviceInfoCB;
  112. ~AudioRendererHost() override;
  113. // Methods called on IO thread ----------------------------------------------
  114. // Audio related IPC message handlers.
  115. // Request permission to use an output device for use by a stream produced
  116. // in the RenderFrame referenced by |render_frame_id|.
  117. // |session_id| is used for unified IO to find out which input device to be
  118. // opened for the stream. For clients that do not use unified IO,
  119. // |session_id| will be ignored and the given |device_id| and
  120. // |security_origin| will be used to select the output device.
  121. // Upon completion of the process, the peer is notified with the device output
  122. // parameters via the NotifyDeviceAuthorized message.
  123. void OnRequestDeviceAuthorization(int stream_id,
  124. int render_frame_id,
  125. int session_id,
  126. const std::string& device_id,
  127. const url::Origin& gurl_security_origin);
  128. // Creates an audio output stream with the specified format.
  129. // Upon success/failure, the peer is notified via the NotifyStreamCreated
  130. // message.
  131. void OnCreateStream(int stream_id,
  132. int render_frame_id,
  133. const media::AudioParameters& params);
  134. // Play the audio stream referenced by |stream_id|.
  135. void OnPlayStream(int stream_id);
  136. // Pause the audio stream referenced by |stream_id|.
  137. void OnPauseStream(int stream_id);
  138. // Close the audio stream referenced by |stream_id|.
  139. void OnCloseStream(int stream_id);
  140. // Set the volume of the audio stream referenced by |stream_id|.
  141. void OnSetVolume(int stream_id, double volume);
  142. // Set the output device of the audio stream referenced by |stream_id|.
  143. void OnSwitchOutputDevice(int stream_id,
  144. int render_frame_id,
  145. const std::string& device_id,
  146. const url::Origin& gurl_security_origin);
  147. // Helper methods.
  148. // Proceed with device authorization after checking permissions.
  149. void OnDeviceAuthorized(int stream_id,
  150. const std::string& device_id,
  151. const GURL& security_origin,
  152. bool have_access);
  153. // Proceed with device authorization after translating device ID.
  154. void OnDeviceIDTranslated(int stream_id,
  155. bool device_found,
  156. const AudioOutputDeviceInfo& device_info);
  157. // Start the actual creation of an audio stream, after the device
  158. // authorization process is complete.
  159. void DoCreateStream(int stream_id,
  160. int render_frame_id,
  161. const media::AudioParameters& params,
  162. const std::string& device_unique_id);
  163. // Complete the process of creating an audio stream. This will set up the
  164. // shared memory or shared socket in low latency mode and send the
  165. // NotifyStreamCreated message to the peer.
  166. void DoCompleteCreation(int stream_id);
  167. // Send playing/paused status to the renderer.
  168. void DoNotifyStreamStateChanged(int stream_id, bool is_playing);
  169. // Proceed with output device switching after checking permissions.
  170. void OnSwitchDeviceAuthorized(int stream_id,
  171. const std::string& device_id,
  172. const GURL& security_origin,
  173. bool have_access);
  174. // Proceed with output device switching after getting the name of the current
  175. // device from the audio controller.
  176. void OnSwitchDeviceCurrentName(const std::string& device_id,
  177. const GURL& security_origin,
  178. int stream_id,
  179. const std::string& current_device_unique_id);
  180. // Proceed with output device switching after translating the ID of the new
  181. // device and checking that output parameters are compatible with the current
  182. // device.
  183. void OnSwitchDeviceIDTranslatedAndParamsChecked(
  184. int stream_id,
  185. bool success,
  186. const AudioOutputDeviceInfo& device_info);
  187. // Finish handling the output device switch request, after the device has
  188. // been switched.
  189. void OnDeviceSwitched(int stream_id);
  190. RenderProcessHost::AudioOutputControllerList DoGetOutputControllers() const;
  191. // Send an error message to the renderer.
  192. void SendErrorMessage(int stream_id);
  193. // Delete an audio entry, notifying observers first. This is called by
  194. // AudioOutputController after it has closed.
  195. void DeleteEntry(scoped_ptr<AudioEntry> entry);
  196. // Send an error message to the renderer, then close the stream.
  197. void ReportErrorAndClose(int stream_id);
  198. // A helper method to look up a AudioEntry identified by |stream_id|.
  199. // Returns NULL if not found.
  200. AudioEntry* LookupById(int stream_id);
  201. // A helper method to update the number of playing streams and alert the
  202. // ResourceScheduler when the renderer starts or stops playing an audiostream.
  203. void UpdateNumPlayingStreams(AudioEntry* entry, bool is_playing);
  204. // Check if the renderer process has access to the requested output device.
  205. void CheckOutputDeviceAccess(int render_frame_id,
  206. const std::string& device_id,
  207. const GURL& gurl_security_origin,
  208. const OutputDeviceAccessCB& callback);
  209. // Invoke |callback| after permission to use a device has been checked.
  210. void AccessChecked(scoped_ptr<MediaStreamUIProxy> ui_proxy,
  211. const OutputDeviceAccessCB& callback,
  212. bool have_access);
  213. // Translate the hashed |device_id| to a unique device ID.
  214. void TranslateDeviceID(const std::string& device_id,
  215. const GURL& gurl_security_origin,
  216. const OutputDeviceInfoCB& callback,
  217. const AudioOutputDeviceEnumeration& device_infos);
  218. // Translate the hashed |device_id| to a unique device ID and compare
  219. // output parameters for both devices.
  220. // |callback| is invoked with the following arguments:
  221. // bool: true if |device_id| was found and its parameters match the
  222. // parmeters of the current device.
  223. // AudioOutputDeviceInfo: info for |device_id| if it was found, or dummy
  224. // info with empty unique ID and name otherwise.
  225. // TODO(guidou): Remove this method once all clients select the output device
  226. // in the initial creation/authorization process instead of by issuing
  227. // switch output device IPCs . http://crbug.com/531468
  228. void TranslateDeviceIDAndCheckParams(
  229. const std::string& device_id,
  230. const GURL& gurl_security_origin,
  231. const std::string& current_device_unique_id,
  232. const OutputDeviceInfoCB& callback,
  233. const AudioOutputDeviceEnumeration& device_infos);
  234. // Helper method to check if the authorization procedure for stream
  235. // |stream_id| has started.
  236. bool IsAuthorizationStarted(int stream_id);
  237. // ID of the RenderProcessHost that owns this instance.
  238. const int render_process_id_;
  239. media::AudioManager* const audio_manager_;
  240. AudioMirroringManager* const mirroring_manager_;
  241. scoped_ptr<media::AudioLog> audio_log_;
  242. // Used to access to AudioInputDeviceManager.
  243. MediaStreamManager* media_stream_manager_;
  244. // A map of stream IDs to audio sources.
  245. AudioEntryMap audio_entries_;
  246. // The number of streams in the playing state.
  247. base::AtomicRefCount num_playing_streams_;
  248. // Salt required to translate renderer device IDs to raw device unique IDs
  249. ResourceContext::SaltCallback salt_callback_;
  250. // Map of device authorizations for streams that are not yet created
  251. // The key is the stream ID, and the value is a pair. The pair's first element
  252. // is a bool that is true if the authorization process completes successfully.
  253. // The second element contains the unique ID of the authorized device.
  254. std::map<int, std::pair<bool, std::string>> authorizations_;
  255. DISALLOW_COPY_AND_ASSIGN(AudioRendererHost);
  256. };
  257. } // namespace content
  258. #endif // CONTENT_BROWSER_RENDERER_HOST_MEDIA_AUDIO_RENDERER_HOST_H_