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: |
(...skipping 14 matching lines...) Expand all Loading... |
25 // request a "refresh" query on a MirroringDestination, and this will | 25 // request a "refresh" query on a MirroringDestination, and this will |
26 // result in AudioMirroringManager starting/stopping only those Diverters | 26 // result in AudioMirroringManager starting/stopping only those Diverters |
27 // that are not correctly routed to the destination. | 27 // that are not correctly routed to the destination. |
28 // 3c. When a mirroring session is stopped, the remaining destinations will be | 28 // 3c. When a mirroring session is stopped, the remaining destinations will be |
29 // queried to determine whether diverting should continue to a different | 29 // queried to determine whether diverting should continue to a different |
30 // destination. | 30 // destination. |
31 | 31 |
32 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ | 32 #ifndef CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ |
33 #define CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ | 33 #define CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ |
34 | 34 |
| 35 #include <map> |
35 #include <set> | 36 #include <set> |
36 #include <utility> | 37 #include <utility> |
37 #include <vector> | 38 #include <vector> |
38 | 39 |
39 #include "base/callback_forward.h" | 40 #include "base/callback_forward.h" |
40 #include "base/macros.h" | 41 #include "base/macros.h" |
41 #include "base/threading/thread_checker.h" | 42 #include "base/threading/thread_checker.h" |
42 #include "content/common/content_export.h" | 43 #include "content/common/content_export.h" |
43 #include "media/audio/audio_source_diverter.h" | 44 #include "media/audio/audio_source_diverter.h" |
44 | 45 |
(...skipping 12 matching lines...) Expand all Loading... |
57 // render_frame_id> pair. | 58 // render_frame_id> pair. |
58 typedef std::pair<int, int> SourceFrameRef; | 59 typedef std::pair<int, int> SourceFrameRef; |
59 | 60 |
60 // Interface to be implemented by audio mirroring destinations. See comments | 61 // Interface to be implemented by audio mirroring destinations. See comments |
61 // for StartMirroring() and StopMirroring() below. | 62 // for StartMirroring() and StopMirroring() below. |
62 class MirroringDestination { | 63 class MirroringDestination { |
63 public: | 64 public: |
64 // Asynchronously query whether this MirroringDestination wants to consume | 65 // Asynchronously query whether this MirroringDestination wants to consume |
65 // audio sourced from each of the |candidates|. |results_callback| is run | 66 // 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 // 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 // MirroringDestination. The second parameter of |results_callback| |
68 // as the one that called QueryForMatches(). | 69 // indicates whether the MirroringDestination wants either: 1) exclusive |
69 typedef base::Callback<void(const std::set<SourceFrameRef>&)> | 70 // access to a diverted audio flow versus 2) a duplicate copy of the audio |
| 71 // flow. |results_callback| must be run on the same thread as the one that |
| 72 // called QueryForMatches(). |
| 73 typedef base::Callback<void(const std::set<SourceFrameRef>&, bool)> |
70 MatchesCallback; | 74 MatchesCallback; |
71 virtual void QueryForMatches( | 75 virtual void QueryForMatches( |
72 const std::set<SourceFrameRef>& candidates, | 76 const std::set<SourceFrameRef>& candidates, |
73 const MatchesCallback& results_callback) = 0; | 77 const MatchesCallback& results_callback) = 0; |
74 | 78 |
75 // Create a consumer of audio data in the format specified by |params|, and | 79 // Create a consumer of audio data in the format specified by |params|, and |
76 // connect it as an input to mirroring. When Close() is called on the | 80 // connect it as an input to mirroring. This is used to provide |
77 // returned AudioOutputStream, the input is disconnected and the object | 81 // MirroringDestination with exclusive access to pull the audio flow from |
78 // becomes invalid. | 82 // the source. When Close() is called on the returned AudioOutputStream, the |
| 83 // input is disconnected and the object becomes invalid. |
79 virtual media::AudioOutputStream* AddInput( | 84 virtual media::AudioOutputStream* AddInput( |
80 const media::AudioParameters& params) = 0; | 85 const media::AudioParameters& params) = 0; |
81 | 86 |
| 87 // Create a consumer of audio data in the format specified by |params|, and |
| 88 // connect it as an input to mirroring. This is used to provide |
| 89 // MirroringDestination with duplicate audio data, which is pushed from the |
| 90 // main audio flow. When Close() is called on the returned AudioPushSink, |
| 91 // the input is disconnected and the object becomes invalid. |
| 92 virtual media::AudioPushSink* AddPushInput( |
| 93 const media::AudioParameters& params) = 0; |
| 94 |
82 protected: | 95 protected: |
83 virtual ~MirroringDestination() {} | 96 virtual ~MirroringDestination() {} |
84 }; | 97 }; |
85 | 98 |
86 // Note: Use GetInstance() for non-test code. | 99 // Note: Use GetInstance() for non-test code. |
87 AudioMirroringManager(); | 100 AudioMirroringManager(); |
88 virtual ~AudioMirroringManager(); | 101 virtual ~AudioMirroringManager(); |
89 | 102 |
90 // Returns the global instance. | 103 // Returns the global instance. |
91 static AudioMirroringManager* GetInstance(); | 104 static AudioMirroringManager* GetInstance(); |
(...skipping 21 matching lines...) Expand all Loading... |
113 // The source render frame associated with the audio stream. | 126 // The source render frame associated with the audio stream. |
114 SourceFrameRef source_render_frame; | 127 SourceFrameRef source_render_frame; |
115 | 128 |
116 // The diverter for re-routing the audio stream. | 129 // The diverter for re-routing the audio stream. |
117 Diverter* diverter; | 130 Diverter* diverter; |
118 | 131 |
119 // If not NULL, the audio stream is currently being diverted to this | 132 // If not NULL, the audio stream is currently being diverted to this |
120 // destination. | 133 // destination. |
121 MirroringDestination* destination; | 134 MirroringDestination* destination; |
122 | 135 |
| 136 // The destinations to which audio stream is duplicated. AudioPushSink is |
| 137 // owned by the Diverter, but AudioMirroringManager must guarantee |
| 138 // StopDuplicating() is called to release them. |
| 139 std::map<MirroringDestination*, media::AudioPushSink*> duplications; |
| 140 |
123 StreamRoutingState(const SourceFrameRef& source_frame, | 141 StreamRoutingState(const SourceFrameRef& source_frame, |
124 Diverter* stream_diverter); | 142 Diverter* stream_diverter); |
125 StreamRoutingState(const StreamRoutingState& other); | 143 StreamRoutingState(const StreamRoutingState& other); |
126 ~StreamRoutingState(); | 144 ~StreamRoutingState(); |
127 }; | 145 }; |
128 | 146 |
129 typedef std::vector<StreamRoutingState> StreamRoutes; | 147 typedef std::vector<StreamRoutingState> StreamRoutes; |
130 typedef std::vector<MirroringDestination*> Destinations; | 148 typedef std::vector<MirroringDestination*> Destinations; |
131 | 149 |
132 // Helper to find a destination other than |old_destination| for the given | 150 // Helper to find a destination other than |old_destination| for the given |
133 // |candidates| to be diverted to. | 151 // |candidates| to be diverted to. |
134 void InitiateQueriesToFindNewDestination( | 152 void InitiateQueriesToFindNewDestination( |
135 MirroringDestination* old_destination, | 153 MirroringDestination* old_destination, |
136 const std::set<SourceFrameRef>& candidates); | 154 const std::set<SourceFrameRef>& candidates); |
137 | 155 |
138 // MirroringDestination query callback. |matches| contains all RenderFrame | 156 // MirroringDestination query callback. |matches| contains all RenderFrame |
139 // sources that will be diverted to |destination|. If |add_only| is false, | 157 // sources that will be diverted or duplicated to |destination|. |
140 // then any Diverters currently routed to |destination| but not found in | 158 // If |add_only| is false, then any audio flows currently routed to |
141 // |matches| will be stopped. | 159 // |destination| but not found in |matches| will be stopped. |
| 160 // If |is_duplicate| is true, the audio data flow will be duplicated to the |
| 161 // destination instead of diverted. |
142 void UpdateRoutesToDestination(MirroringDestination* destination, | 162 void UpdateRoutesToDestination(MirroringDestination* destination, |
143 bool add_only, | 163 bool add_only, |
144 const std::set<SourceFrameRef>& matches); | 164 const std::set<SourceFrameRef>& matches, |
| 165 bool is_duplicate); |
| 166 |
| 167 // |matches| contains all RenderFrame sources that will be diverted to |
| 168 // |destination|. If |add_only| is false, then any Diverters currently routed |
| 169 // to |destination| but not found in |matches| will be stopped. |
| 170 void UpdateRoutesToDivertDestination(MirroringDestination* destination, |
| 171 bool add_only, |
| 172 const std::set<SourceFrameRef>& matches); |
| 173 |
| 174 // |matches| contains all RenderFrame sources that will be duplicated to |
| 175 // |destination|. If |add_only| is false, then any Diverters currently |
| 176 // duplicating to |destination| but not found in |matches| will be stopped. |
| 177 void UpdateRoutesToDuplicateDestination( |
| 178 MirroringDestination* destination, |
| 179 bool add_only, |
| 180 const std::set<SourceFrameRef>& matches); |
145 | 181 |
146 // Starts diverting audio to the |new_destination|, if not NULL. Otherwise, | 182 // Starts diverting audio to the |new_destination|, if not NULL. Otherwise, |
147 // stops diverting audio. | 183 // stops diverting audio. |
148 static void ChangeRoute(StreamRoutingState* route, | 184 static void RouteDivertedFlow(StreamRoutingState* route, |
149 MirroringDestination* new_destination); | 185 MirroringDestination* new_destination); |
150 | 186 |
151 // Routing table. Contains one entry for each Diverter. | 187 // Routing table. Contains one entry for each Diverter. |
152 StreamRoutes routes_; | 188 StreamRoutes routes_; |
153 | 189 |
154 // All active mirroring sessions. | 190 // All active mirroring sessions. |
155 Destinations sessions_; | 191 Destinations sessions_; |
156 | 192 |
157 // Used to check that all AudioMirroringManager code runs on the same thread. | 193 // Used to check that all AudioMirroringManager code runs on the same thread. |
158 base::ThreadChecker thread_checker_; | 194 base::ThreadChecker thread_checker_; |
159 | 195 |
160 DISALLOW_COPY_AND_ASSIGN(AudioMirroringManager); | 196 DISALLOW_COPY_AND_ASSIGN(AudioMirroringManager); |
161 }; | 197 }; |
162 | 198 |
163 } // namespace content | 199 } // namespace content |
164 | 200 |
165 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ | 201 #endif // CONTENT_BROWSER_MEDIA_CAPTURE_AUDIO_MIRRORING_MANAGER_H_ |
OLD | NEW |