OLD | NEW |
| (Empty) |
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 #include "chrome/browser/media/media_internals.h" | |
6 | |
7 #include "base/memory/scoped_ptr.h" | |
8 #include "base/string16.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" | |
17 #include "content/public/browser/web_ui.h" | |
18 #include "media/base/media_log.h" | |
19 #include "media/base/media_log_event.h" | |
20 | |
21 using content::BrowserThread; | |
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 | |
94 MediaInternals* MediaInternals::GetInstance() { | |
95 return Singleton<MediaInternals>::get(); | |
96 } | |
97 | |
98 MediaInternals::~MediaInternals() {} | |
99 | |
100 void MediaInternals::OnDeleteAudioStream(void* host, int stream_id) { | |
101 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
102 std::string stream = base::StringPrintf("audio_streams.%p:%d", | |
103 host, stream_id); | |
104 DeleteItem(stream); | |
105 } | |
106 | |
107 void MediaInternals::OnSetAudioStreamPlaying( | |
108 void* host, int stream_id, bool playing) { | |
109 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
110 UpdateAudioStream(host, stream_id, | |
111 "playing", Value::CreateBooleanValue(playing)); | |
112 } | |
113 | |
114 void MediaInternals::OnSetAudioStreamStatus( | |
115 void* host, int stream_id, const std::string& status) { | |
116 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
117 UpdateAudioStream(host, stream_id, | |
118 "status", Value::CreateStringValue(status)); | |
119 } | |
120 | |
121 void MediaInternals::OnSetAudioStreamVolume( | |
122 void* host, int stream_id, double volume) { | |
123 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
124 UpdateAudioStream(host, stream_id, | |
125 "volume", Value::CreateDoubleValue(volume)); | |
126 } | |
127 | |
128 void MediaInternals::OnMediaEvent( | |
129 int render_process_id, const media::MediaLogEvent& event) { | |
130 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
131 | |
132 // Notify observers that |event| has occured. | |
133 DictionaryValue dict; | |
134 dict.SetInteger("renderer", render_process_id); | |
135 dict.SetInteger("player", event.id); | |
136 dict.SetString("type", media::MediaLog::EventTypeToString(event.type)); | |
137 dict.SetDouble("time", event.time.ToDoubleT()); | |
138 dict.Set("params", event.params.DeepCopy()); | |
139 SendUpdate("media.onMediaEvent", &dict); | |
140 } | |
141 | |
142 void MediaInternals::OnCaptureDevicesOpened( | |
143 int render_process_id, | |
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 } | |
151 | |
152 void MediaInternals::OnCaptureDevicesClosed( | |
153 int render_process_id, | |
154 int render_view_id, | |
155 const content::MediaStreamDevices& devices) { | |
156 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
157 media_stream_capture_indicator_->CaptureDevicesClosed(render_process_id, | |
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 } | |
188 } | |
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 } | |
199 | |
200 void MediaInternals::SendEverything() { | |
201 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
202 SendUpdate("media.onReceiveEverything", &data_); | |
203 } | |
204 | |
205 scoped_refptr<MediaCaptureDevicesDispatcher> | |
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 } | |
219 | |
220 void MediaInternals::UpdateAudioStream( | |
221 void* host, int stream_id, const std::string& property, Value* value) { | |
222 std::string stream = base::StringPrintf("audio_streams.%p:%d", | |
223 host, stream_id); | |
224 UpdateItem("media.addAudioStream", stream, property, value); | |
225 } | |
226 | |
227 void MediaInternals::DeleteItem(const std::string& item) { | |
228 data_.Remove(item, NULL); | |
229 scoped_ptr<Value> value(Value::CreateStringValue(item)); | |
230 SendUpdate("media.onItemDeleted", value.get()); | |
231 } | |
232 | |
233 void MediaInternals::UpdateItem( | |
234 const std::string& update_fn, const std::string& id, | |
235 const std::string& property, Value* value) { | |
236 DictionaryValue* item_properties; | |
237 if (!data_.GetDictionary(id, &item_properties)) { | |
238 item_properties = new DictionaryValue(); | |
239 data_.Set(id, item_properties); | |
240 item_properties->SetString("id", id); | |
241 } | |
242 item_properties->Set(property, value); | |
243 SendUpdate(update_fn, item_properties); | |
244 } | |
245 | |
246 void MediaInternals::SendUpdate(const std::string& function, Value* value) { | |
247 // Only bother serializing the update to JSON if someone is watching. | |
248 if (observers_.size()) { | |
249 std::vector<const Value*> args; | |
250 args.push_back(value); | |
251 string16 update = content::WebUI::GetJavascriptCall(function, args); | |
252 FOR_EACH_OBSERVER(MediaInternalsObserver, observers_, OnUpdate(update)); | |
253 } | |
254 } | |
OLD | NEW |