| OLD | NEW |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // AudioMirroringManager is a singleton object that maintains a set of active | 5 // AudioMirroringManager is a singleton object that maintains a set of active |
| 6 // audio mirroring destinations and auto-connects/disconnects audio streams | 6 // audio mirroring destinations and auto-connects/disconnects audio streams |
| 7 // to/from those destinations. It is meant to be used exclusively on the IO | 7 // to/from those destinations. It is meant to be used exclusively on the IO |
| 8 // thread. | 8 // thread. |
| 9 // | 9 // |
| 10 // How it works: | 10 // How it works: |
| 11 // | 11 // |
| 12 // 1. AudioRendererHost gets a CreateStream message from the render process | 12 // 1. AudioRendererHost gets a CreateStream message from the render process |
| 13 // and, among other things, creates an AudioOutputController to control the | 13 // and, among other things, creates an AudioOutputController to control the |
| 14 // audio data flow between the render and browser processes. | 14 // audio data flow between the render and browser processes. More |
| 15 // 2. At some point, AudioRendererHost receives an "associate with render | 15 // importantly, it registers the AudioOutputController with |
| 16 // view" message. Among other actions, it registers the | 16 // AudioMirroringManager (as a Diverter). |
| 17 // AudioOutputController with AudioMirroringManager (as a Diverter). | 17 // 2. A user request to mirror all the audio for a WebContents is made. A |
| 18 // 3. A user request to mirror all the audio for a single RenderView is made. | 18 // MirroringDestination is created, and StartMirroring() is called to begin |
| 19 // A MirroringDestination is created, and StartMirroring() is called to | 19 // the mirroring session. The MirroringDestination is queried to determine |
| 20 // begin the mirroring session. This causes AudioMirroringManager to | 20 // which of all the known Diverters will re-route their audio to it. |
| 21 // instruct any matching Diverters to divert their audio data to the | 21 // 3a. During a mirroring session, AudioMirroringManager may encounter new |
| 22 // MirroringDestination. | 22 // Diverters, and will query all the MirroringDestinations to determine |
| 23 // | 23 // which is a match, if any. |
| 24 // #2 and #3 above may occur in any order, as it is the job of | 24 // 3b. During a mirroring session, a call to StartMirroring() can be made to |
| 25 // AudioMirroringManager to realize when the players can be "matched up." | 25 // request a "refresh" query on a MirroringDestination, and this will |
| 26 // result in AudioMirroringManager starting/stopping only those Diverters |
| 27 // that are not correctly routed to the destination. |
| 28 // 3c. When a mirroring session is stopped, the remaining destinations will be |
| 29 // queried to determine whether diverting should continue to a different |
| 30 // destination. |
| 26 | 31 |
| 27 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ | 32 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ |
| 28 #define CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ | 33 #define CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ |
| 29 | 34 |
| 30 #include <map> | 35 #include <set> |
| 31 #include <utility> | 36 #include <utility> |
| 37 #include <vector> |
| 32 | 38 |
| 33 #include "base/basictypes.h" | 39 #include "base/basictypes.h" |
| 40 #include "base/callback_forward.h" |
| 34 #include "base/threading/thread_checker.h" | 41 #include "base/threading/thread_checker.h" |
| 35 #include "content/common/content_export.h" | 42 #include "content/common/content_export.h" |
| 36 #include "media/audio/audio_source_diverter.h" | 43 #include "media/audio/audio_source_diverter.h" |
| 37 | 44 |
| 38 namespace media { | 45 namespace media { |
| 39 class AudioOutputStream; | 46 class AudioOutputStream; |
| 40 } | 47 } |
| 41 | 48 |
| 42 namespace content { | 49 namespace content { |
| 43 | 50 |
| 44 class CONTENT_EXPORT AudioMirroringManager { | 51 class CONTENT_EXPORT AudioMirroringManager { |
| 45 public: | 52 public: |
| 46 // Interface for diverting audio data to an alternative AudioOutputStream. | 53 // Interface for diverting audio data to an alternative AudioOutputStream. |
| 47 typedef media::AudioSourceDiverter Diverter; | 54 typedef media::AudioSourceDiverter Diverter; |
| 48 | 55 |
| 56 // A SourceFrameRef is a RenderFrameHost identified by a <render_process_id, |
| 57 // render_frame_id> pair. |
| 58 typedef std::pair<int, int> SourceFrameRef; |
| 59 |
| 49 // Interface to be implemented by audio mirroring destinations. See comments | 60 // Interface to be implemented by audio mirroring destinations. See comments |
| 50 // for StartMirroring() and StopMirroring() below. | 61 // for StartMirroring() and StopMirroring() below. |
| 51 class MirroringDestination { | 62 class MirroringDestination { |
| 52 public: | 63 public: |
| 64 // Asynchronously query whether this MirroringDestination wants to consume |
| 65 // audio sourced from each of the |candidates|. |results_callback| is run |
| 66 // to indicate which of them (or none) should have audio routed to this |
| 67 // MirroringDestination. |results_callback| must be run on the same thread |
| 68 // as the one that called QueryForMatches(). |
| 69 typedef base::Callback<void(const std::set<SourceFrameRef>&)> |
| 70 MatchesCallback; |
| 71 virtual void QueryForMatches( |
| 72 const std::set<SourceFrameRef>& candidates, |
| 73 const MatchesCallback& results_callback) = 0; |
| 74 |
| 53 // Create a consumer of audio data in the format specified by |params|, and | 75 // Create a consumer of audio data in the format specified by |params|, and |
| 54 // connect it as an input to mirroring. When Close() is called on the | 76 // connect it as an input to mirroring. When Close() is called on the |
| 55 // returned AudioOutputStream, the input is disconnected and the object | 77 // returned AudioOutputStream, the input is disconnected and the object |
| 56 // becomes invalid. | 78 // becomes invalid. |
| 57 virtual media::AudioOutputStream* AddInput( | 79 virtual media::AudioOutputStream* AddInput( |
| 58 const media::AudioParameters& params) = 0; | 80 const media::AudioParameters& params) = 0; |
| 59 | 81 |
| 60 protected: | 82 protected: |
| 61 virtual ~MirroringDestination() {} | 83 virtual ~MirroringDestination() {} |
| 62 }; | 84 }; |
| 63 | 85 |
| 64 // Note: Use GetInstance() for non-test code. | 86 // Note: Use GetInstance() for non-test code. |
| 65 AudioMirroringManager(); | 87 AudioMirroringManager(); |
| 66 virtual ~AudioMirroringManager(); | 88 virtual ~AudioMirroringManager(); |
| 67 | 89 |
| 68 // Returns the global instance. | 90 // Returns the global instance. |
| 69 static AudioMirroringManager* GetInstance(); | 91 static AudioMirroringManager* GetInstance(); |
| 70 | 92 |
| 71 // Add/Remove a diverter for an audio stream with a known RenderView target | 93 // Add/Remove a diverter for an audio stream with a known RenderFrame source |
| 72 // (represented by |render_process_id| + |render_view_id|). Multiple | 94 // (represented by |render_process_id| + |render_frame_id|). Multiple |
| 73 // diverters may be added for the same target. |diverter| must live until | 95 // diverters may be added for the same source frame, but never the same |
| 74 // after RemoveDiverter() is called. | 96 // diverter. |diverter| must live until after RemoveDiverter() is called. |
| 75 // | 97 virtual void AddDiverter(int render_process_id, int render_frame_id, |
| 76 // Re-entrancy warning: These methods should not be called by a Diverter | |
| 77 // during a Start/StopDiverting() invocation. | |
| 78 virtual void AddDiverter(int render_process_id, int render_view_id, | |
| 79 Diverter* diverter); | 98 Diverter* diverter); |
| 80 virtual void RemoveDiverter(int render_process_id, int render_view_id, | 99 virtual void RemoveDiverter(Diverter* diverter); |
| 81 Diverter* diverter); | |
| 82 | 100 |
| 83 // Start/stop mirroring all audio output streams associated with a RenderView | 101 // (Re-)Start/Stop mirroring to the given |destination|. |destination| must |
| 84 // target (represented by |render_process_id| + |render_view_id|) to | 102 // live until after StopMirroring() is called. A client may request a |
| 85 // |destination|. |destination| must live until after StopMirroring() is | 103 // re-start by calling StartMirroring() again; and this will cause |
| 86 // called. | 104 // AudioMirroringManager to query |destination| and only re-route those |
| 87 virtual void StartMirroring(int render_process_id, int render_view_id, | 105 // diverters that are missing/new to the returned set of matches. |
| 88 MirroringDestination* destination); | 106 virtual void StartMirroring(MirroringDestination* destination); |
| 89 virtual void StopMirroring(int render_process_id, int render_view_id, | 107 virtual void StopMirroring(MirroringDestination* destination); |
| 90 MirroringDestination* destination); | |
| 91 | 108 |
| 92 private: | 109 private: |
| 93 // A mirroring target is a RenderView identified by a | 110 friend class AudioMirroringManagerTest; |
| 94 // <render_process_id, render_view_id> pair. | |
| 95 typedef std::pair<int, int> Target; | |
| 96 | 111 |
| 97 // Note: Objects in these maps are not owned. | 112 struct StreamRoutingState { |
| 98 typedef std::multimap<Target, Diverter*> DiverterMap; | 113 // The source render frame associated with the audio stream. |
| 99 typedef std::map<Target, MirroringDestination*> SessionMap; | 114 SourceFrameRef source_render_frame; |
| 100 | 115 |
| 101 // Currently-active divertable audio streams. | 116 // The diverter for re-routing the audio stream. |
| 102 DiverterMap diverters_; | 117 Diverter* diverter; |
| 103 | 118 |
| 104 // Currently-active mirroring sessions. | 119 // If not NULL, the audio stream is currently being diverted to this |
| 105 SessionMap sessions_; | 120 // destination. |
| 121 MirroringDestination* destination; |
| 122 |
| 123 StreamRoutingState(const SourceFrameRef& source_frame, |
| 124 Diverter* stream_diverter); |
| 125 ~StreamRoutingState(); |
| 126 }; |
| 127 |
| 128 typedef std::vector<StreamRoutingState> StreamRoutes; |
| 129 typedef std::vector<MirroringDestination*> Destinations; |
| 130 |
| 131 // Helper to find a destination other than |old_destination| for the given |
| 132 // |candidates| to be diverted to. |
| 133 void InitiateQueriesToFindNewDestination( |
| 134 MirroringDestination* old_destination, |
| 135 const std::set<SourceFrameRef>& candidates); |
| 136 |
| 137 // MirroringDestination query callback. |matches| contains all RenderFrame |
| 138 // sources that will be diverted to |destination|. If |add_only| is false, |
| 139 // then any Diverters currently routed to |destination| but not found in |
| 140 // |matches| will be stopped. |
| 141 void UpdateRoutesToDestination(MirroringDestination* destination, |
| 142 bool add_only, |
| 143 const std::set<SourceFrameRef>& matches); |
| 144 |
| 145 // Starts diverting audio to the |new_destination|, if not NULL. Otherwise, |
| 146 // stops diverting audio. |
| 147 static void ChangeRoute(StreamRoutingState* route, |
| 148 MirroringDestination* new_destination); |
| 149 |
| 150 // Routing table. Contains one entry for each Diverter. |
| 151 StreamRoutes routes_; |
| 152 |
| 153 // All active mirroring sessions. |
| 154 Destinations sessions_; |
| 106 | 155 |
| 107 // Used to check that all AudioMirroringManager code runs on the same thread. | 156 // Used to check that all AudioMirroringManager code runs on the same thread. |
| 108 base::ThreadChecker thread_checker_; | 157 base::ThreadChecker thread_checker_; |
| 109 | 158 |
| 110 DISALLOW_COPY_AND_ASSIGN(AudioMirroringManager); | 159 DISALLOW_COPY_AND_ASSIGN(AudioMirroringManager); |
| 111 }; | 160 }; |
| 112 | 161 |
| 113 } // namespace content | 162 } // namespace content |
| 114 | 163 |
| 115 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ | 164 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ |
| OLD | NEW |