OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 #include "chrome/browser/media/media_internals.h" | 5 #include "content/browser/media/media_internals.h" |
6 | 6 |
7 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
8 #include "base/string16.h" | 8 #include "base/string16.h" |
9 #include "base/stringprintf.h" | 9 #include "base/stringprintf.h" |
10 #include "chrome/browser/media/media_capture_devices_dispatcher.h" | |
11 #include "chrome/browser/media/media_internals_observer.h" | |
12 #include "chrome/browser/media/media_stream_capture_indicator.h" | |
13 #include "chrome/browser/prefs/scoped_user_pref_update.h" | |
14 #include "chrome/browser/profiles/profile.h" | |
15 #include "chrome/common/pref_names.h" | |
16 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
17 #include "content/public/browser/web_ui.h" | 11 #include "content/public/browser/web_ui.h" |
18 #include "media/base/media_log.h" | 12 #include "media/base/media_log.h" |
19 #include "media/base/media_log_event.h" | 13 #include "media/base/media_log_event.h" |
20 | 14 |
21 using content::BrowserThread; | 15 namespace content { |
22 | |
23 namespace media { | |
24 | |
25 namespace { | |
26 | |
27 const content::MediaStreamDevice* FindDefaultDeviceWithId( | |
28 const content::MediaStreamDevices& devices, | |
29 const std::string& device_id) { | |
30 if (devices.empty()) | |
31 return NULL; | |
32 | |
33 content::MediaStreamDevices::const_iterator iter = devices.begin(); | |
34 for (; iter != devices.end(); ++iter) { | |
35 if (iter->id == device_id) { | |
36 return &(*iter); | |
37 } | |
38 } | |
39 | |
40 return &(*devices.begin()); | |
41 }; | |
42 | |
43 } // namespace | |
44 | |
45 void GetDefaultDevicesForProfile(Profile* profile, | |
46 bool audio, | |
47 bool video, | |
48 content::MediaStreamDevices* devices) { | |
49 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
50 DCHECK(audio || video); | |
51 | |
52 PrefService* prefs = profile->GetPrefs(); | |
53 std::string default_device; | |
54 if (audio) { | |
55 default_device = prefs->GetString(prefs::kDefaultAudioCaptureDevice); | |
56 GetRequestedDevice(default_device, true, false, devices); | |
57 } | |
58 | |
59 if (video) { | |
60 default_device = prefs->GetString(prefs::kDefaultVideoCaptureDevice); | |
61 GetRequestedDevice(default_device, false, true, devices); | |
62 } | |
63 } | |
64 | |
65 void GetRequestedDevice(const std::string& requested_device_id, | |
66 bool audio, | |
67 bool video, | |
68 content::MediaStreamDevices* devices) { | |
69 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
70 DCHECK(audio || video); | |
71 | |
72 MediaCaptureDevicesDispatcher* dispatcher = | |
73 MediaInternals::GetInstance()->GetMediaCaptureDevicesDispatcher(); | |
74 if (audio) { | |
75 const content::MediaStreamDevices& audio_devices = | |
76 dispatcher->GetAudioCaptureDevices(); | |
77 const content::MediaStreamDevice* const device = | |
78 media::FindDefaultDeviceWithId(audio_devices, requested_device_id); | |
79 if (device) | |
80 devices->push_back(*device); | |
81 } | |
82 if (video) { | |
83 const content::MediaStreamDevices& video_devices = | |
84 dispatcher->GetVideoCaptureDevices(); | |
85 const content::MediaStreamDevice* const device = | |
86 media::FindDefaultDeviceWithId(video_devices, requested_device_id); | |
87 if (device) | |
88 devices->push_back(*device); | |
89 } | |
90 } | |
91 | |
92 } // namespace media | |
93 | 16 |
94 MediaInternals* MediaInternals::GetInstance() { | 17 MediaInternals* MediaInternals::GetInstance() { |
95 return Singleton<MediaInternals>::get(); | 18 return Singleton<MediaInternals>::get(); |
96 } | 19 } |
97 | 20 |
98 MediaInternals::~MediaInternals() {} | 21 MediaInternals::~MediaInternals() {} |
99 | 22 |
100 void MediaInternals::OnDeleteAudioStream(void* host, int stream_id) { | 23 void MediaInternals::OnDeleteAudioStream(void* host, int stream_id) { |
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 24 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
102 std::string stream = base::StringPrintf("audio_streams.%p:%d", | 25 std::string stream = base::StringPrintf("audio_streams.%p:%d", |
(...skipping 29 matching lines...) Expand all Loading... |
132 // Notify observers that |event| has occured. | 55 // Notify observers that |event| has occured. |
133 DictionaryValue dict; | 56 DictionaryValue dict; |
134 dict.SetInteger("renderer", render_process_id); | 57 dict.SetInteger("renderer", render_process_id); |
135 dict.SetInteger("player", event.id); | 58 dict.SetInteger("player", event.id); |
136 dict.SetString("type", media::MediaLog::EventTypeToString(event.type)); | 59 dict.SetString("type", media::MediaLog::EventTypeToString(event.type)); |
137 dict.SetDouble("time", event.time.ToDoubleT()); | 60 dict.SetDouble("time", event.time.ToDoubleT()); |
138 dict.Set("params", event.params.DeepCopy()); | 61 dict.Set("params", event.params.DeepCopy()); |
139 SendUpdate("media.onMediaEvent", &dict); | 62 SendUpdate("media.onMediaEvent", &dict); |
140 } | 63 } |
141 | 64 |
142 void MediaInternals::OnCaptureDevicesOpened( | 65 void MediaInternals::AddUpdateCallback(const UpdateCallback& callback) { |
143 int render_process_id, | 66 update_callbacks_.push_back(callback); |
144 int render_view_id, | |
145 const content::MediaStreamDevices& devices) { | |
146 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
147 media_stream_capture_indicator_->CaptureDevicesOpened(render_process_id, | |
148 render_view_id, | |
149 devices); | |
150 } | 67 } |
151 | 68 |
152 void MediaInternals::OnCaptureDevicesClosed( | 69 void MediaInternals::RemoveUpdateCallback(const UpdateCallback& callback) { |
153 int render_process_id, | 70 for (size_t i = 0; i < update_callbacks_.size(); ++i) { |
154 int render_view_id, | 71 if (update_callbacks_[i].Equals(callback)) { |
155 const content::MediaStreamDevices& devices) { | 72 update_callbacks_.erase(update_callbacks_.begin() + i); |
156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 73 return; |
157 media_stream_capture_indicator_->CaptureDevicesClosed(render_process_id, | 74 } |
158 render_view_id, | |
159 devices); | |
160 } | |
161 | |
162 void MediaInternals::OnAudioCaptureDevicesChanged( | |
163 const content::MediaStreamDevices& devices) { | |
164 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
165 media_devices_dispatcher_->AudioCaptureDevicesChanged(devices); | |
166 } | |
167 | |
168 void MediaInternals::OnVideoCaptureDevicesChanged( | |
169 const content::MediaStreamDevices& devices) { | |
170 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
171 media_devices_dispatcher_->VideoCaptureDevicesChanged(devices); | |
172 } | |
173 | |
174 void MediaInternals::OnMediaRequestStateChanged( | |
175 int render_process_id, | |
176 int render_view_id, | |
177 const content::MediaStreamDevice& device, | |
178 content::MediaRequestState state) { | |
179 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
180 | |
181 if (observers_.size()) { | |
182 FOR_EACH_OBSERVER( | |
183 MediaInternalsObserver, observers_, OnRequestUpdate(render_process_id, | |
184 render_view_id, | |
185 device, | |
186 state)); | |
187 } | 75 } |
188 } | 76 NOTREACHED(); |
189 | |
190 void MediaInternals::AddObserver(MediaInternalsObserver* observer) { | |
191 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
192 observers_.AddObserver(observer); | |
193 } | |
194 | |
195 void MediaInternals::RemoveObserver(MediaInternalsObserver* observer) { | |
196 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
197 observers_.RemoveObserver(observer); | |
198 } | 77 } |
199 | 78 |
200 void MediaInternals::SendEverything() { | 79 void MediaInternals::SendEverything() { |
201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 80 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
202 SendUpdate("media.onReceiveEverything", &data_); | 81 SendUpdate("media.onReceiveEverything", &data_); |
203 } | 82 } |
204 | 83 |
205 scoped_refptr<MediaCaptureDevicesDispatcher> | 84 MediaInternals::MediaInternals() { |
206 MediaInternals::GetMediaCaptureDevicesDispatcher() { | |
207 return media_devices_dispatcher_; | |
208 } | |
209 | |
210 scoped_refptr<MediaStreamCaptureIndicator> | |
211 MediaInternals::GetMediaStreamCaptureIndicator() { | |
212 return media_stream_capture_indicator_.get(); | |
213 } | |
214 | |
215 MediaInternals::MediaInternals() | |
216 : media_stream_capture_indicator_(new MediaStreamCaptureIndicator()), | |
217 media_devices_dispatcher_(new MediaCaptureDevicesDispatcher()) { | |
218 } | 85 } |
219 | 86 |
220 void MediaInternals::UpdateAudioStream( | 87 void MediaInternals::UpdateAudioStream( |
221 void* host, int stream_id, const std::string& property, Value* value) { | 88 void* host, int stream_id, const std::string& property, Value* value) { |
222 std::string stream = base::StringPrintf("audio_streams.%p:%d", | 89 std::string stream = base::StringPrintf("audio_streams.%p:%d", |
223 host, stream_id); | 90 host, stream_id); |
224 UpdateItem("media.addAudioStream", stream, property, value); | 91 UpdateItem("media.addAudioStream", stream, property, value); |
225 } | 92 } |
226 | 93 |
227 void MediaInternals::DeleteItem(const std::string& item) { | 94 void MediaInternals::DeleteItem(const std::string& item) { |
(...skipping 10 matching lines...) Expand all Loading... |
238 item_properties = new DictionaryValue(); | 105 item_properties = new DictionaryValue(); |
239 data_.Set(id, item_properties); | 106 data_.Set(id, item_properties); |
240 item_properties->SetString("id", id); | 107 item_properties->SetString("id", id); |
241 } | 108 } |
242 item_properties->Set(property, value); | 109 item_properties->Set(property, value); |
243 SendUpdate(update_fn, item_properties); | 110 SendUpdate(update_fn, item_properties); |
244 } | 111 } |
245 | 112 |
246 void MediaInternals::SendUpdate(const std::string& function, Value* value) { | 113 void MediaInternals::SendUpdate(const std::string& function, Value* value) { |
247 // Only bother serializing the update to JSON if someone is watching. | 114 // Only bother serializing the update to JSON if someone is watching. |
248 if (observers_.size()) { | 115 if (update_callbacks_.empty()) |
249 std::vector<const Value*> args; | 116 return; |
250 args.push_back(value); | 117 |
251 string16 update = content::WebUI::GetJavascriptCall(function, args); | 118 std::vector<const Value*> args; |
252 FOR_EACH_OBSERVER(MediaInternalsObserver, observers_, OnUpdate(update)); | 119 args.push_back(value); |
253 } | 120 string16 update = WebUI::GetJavascriptCall(function, args); |
| 121 for (size_t i = 0; i < update_callbacks_.size(); i++) |
| 122 update_callbacks_[i].Run(update); |
254 } | 123 } |
| 124 |
| 125 } // namespace content |
OLD | NEW |