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| must be | |
66 // run on the current thread's task runner to indicate which of them (or | |
DaleCurtis
2014/09/05 22:41:41
current is unclear in this context. current as in
miu
2014/09/08 22:50:54
Done. Clarified comment.
| |
67 // none) should have audio routed to this MirroringDestination. | |
68 typedef base::Callback<void(const std::set<SourceFrameRef>&)> | |
69 MatchesCallback; | |
70 virtual void QueryForMatches( | |
71 const std::set<SourceFrameRef>& candidates, | |
72 const MatchesCallback& results_callback) = 0; | |
73 | |
53 // Create a consumer of audio data in the format specified by |params|, and | 74 // 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 | 75 // connect it as an input to mirroring. When Close() is called on the |
55 // returned AudioOutputStream, the input is disconnected and the object | 76 // returned AudioOutputStream, the input is disconnected and the object |
56 // becomes invalid. | 77 // becomes invalid. |
57 virtual media::AudioOutputStream* AddInput( | 78 virtual media::AudioOutputStream* AddInput( |
58 const media::AudioParameters& params) = 0; | 79 const media::AudioParameters& params) = 0; |
59 | 80 |
60 protected: | 81 protected: |
61 virtual ~MirroringDestination() {} | 82 virtual ~MirroringDestination() {} |
62 }; | 83 }; |
63 | 84 |
64 // Note: Use GetInstance() for non-test code. | 85 // Note: Use GetInstance() for non-test code. |
65 AudioMirroringManager(); | 86 AudioMirroringManager(); |
66 virtual ~AudioMirroringManager(); | 87 virtual ~AudioMirroringManager(); |
67 | 88 |
68 // Returns the global instance. | 89 // Returns the global instance. |
69 static AudioMirroringManager* GetInstance(); | 90 static AudioMirroringManager* GetInstance(); |
70 | 91 |
71 // Add/Remove a diverter for an audio stream with a known RenderView target | 92 // Add/Remove a diverter for an audio stream with a known RenderFrame source |
72 // (represented by |render_process_id| + |render_view_id|). Multiple | 93 // (represented by |render_process_id| + |render_frame_id|). Multiple |
73 // diverters may be added for the same target. |diverter| must live until | 94 // diverters may be added for the same source frame, but never the same |
74 // after RemoveDiverter() is called. | 95 // diverter. |diverter| must live until after RemoveDiverter() is called. |
75 // | 96 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); | 97 Diverter* diverter); |
80 virtual void RemoveDiverter(int render_process_id, int render_view_id, | 98 virtual void RemoveDiverter(Diverter* diverter); |
81 Diverter* diverter); | |
82 | 99 |
83 // Start/stop mirroring all audio output streams associated with a RenderView | 100 // (Re-)Start/Stop mirroring to the given |destination|. |destination| must |
84 // target (represented by |render_process_id| + |render_view_id|) to | 101 // live until after StopMirroring() is called. A client may request a |
85 // |destination|. |destination| must live until after StopMirroring() is | 102 // re-start by calling StartMirroring() again; and this will cause |
86 // called. | 103 // AudioMirroringManager to query |destination| and only re-route those |
87 virtual void StartMirroring(int render_process_id, int render_view_id, | 104 // diverters that are missing/new to the returned set of matches. |
88 MirroringDestination* destination); | 105 virtual void StartMirroring(MirroringDestination* destination); |
89 virtual void StopMirroring(int render_process_id, int render_view_id, | 106 virtual void StopMirroring(MirroringDestination* destination); |
90 MirroringDestination* destination); | |
91 | 107 |
92 private: | 108 private: |
93 // A mirroring target is a RenderView identified by a | 109 friend class AudioMirroringManagerTest; |
94 // <render_process_id, render_view_id> pair. | |
95 typedef std::pair<int, int> Target; | |
96 | 110 |
97 // Note: Objects in these maps are not owned. | 111 struct StreamRoutingState { |
98 typedef std::multimap<Target, Diverter*> DiverterMap; | 112 // The source render frame associated with the audio stream. |
99 typedef std::map<Target, MirroringDestination*> SessionMap; | 113 SourceFrameRef source_render_frame; |
100 | 114 |
101 // Currently-active divertable audio streams. | 115 // The diverter for re-routing the audio stream. |
102 DiverterMap diverters_; | 116 Diverter* diverter; |
103 | 117 |
104 // Currently-active mirroring sessions. | 118 // If not NULL, the audio stream is currently being diverted to this |
105 SessionMap sessions_; | 119 // destination. |
120 MirroringDestination* destination; | |
121 | |
122 StreamRoutingState(const SourceFrameRef& source_frame, | |
123 Diverter* stream_diverter); | |
124 ~StreamRoutingState(); | |
125 }; | |
126 | |
127 typedef std::vector<StreamRoutingState> StreamRoutes; | |
128 typedef std::vector<MirroringDestination*> Destinations; | |
129 | |
130 // Helper to find a destination other than |old_destination| for the given | |
131 // |candidates| to be diverted to. | |
132 void AttemptNewMatches(MirroringDestination* old_destination, | |
DaleCurtis
2014/09/05 22:41:41
Name could use some work. AttemptNewMatches is kin
miu
2014/09/08 22:50:54
Done.
| |
133 const std::set<SourceFrameRef>& candidates); | |
134 | |
135 // MirroringDestination query callback. |matches| contains all RenderFrame | |
136 // sources that will be diverted to |destination|. If |add_only| is false, | |
137 // then any Diverters currently routed to |destination| but not found in | |
138 // |matches| will be stopped. | |
139 void UpdateRoutesToDestination(MirroringDestination* destination, | |
140 bool add_only, | |
141 const std::set<SourceFrameRef>& matches); | |
142 | |
143 // Starts diverting audio to the |new_destination|, if not NULL. Otherwise, | |
144 // stops diverting audio. | |
145 static void ChangeRoute(StreamRoutingState* route, | |
146 MirroringDestination* new_destination); | |
147 | |
148 // Routing table. Contains one entry for each Diverter. | |
149 StreamRoutes routes_; | |
150 | |
151 // All active mirroring sessions. | |
152 Destinations sessions_; | |
106 | 153 |
107 // Used to check that all AudioMirroringManager code runs on the same thread. | 154 // Used to check that all AudioMirroringManager code runs on the same thread. |
108 base::ThreadChecker thread_checker_; | 155 base::ThreadChecker thread_checker_; |
109 | 156 |
110 DISALLOW_COPY_AND_ASSIGN(AudioMirroringManager); | 157 DISALLOW_COPY_AND_ASSIGN(AudioMirroringManager); |
111 }; | 158 }; |
112 | 159 |
113 } // namespace content | 160 } // namespace content |
114 | 161 |
115 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ | 162 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ |
OLD | NEW |