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
3 // found in the LICENSE file.
5 #include "content/browser/media/webrtc_internals.h"
7 #include "base/command_line.h"
8 #include "content/browser/media/webrtc_internals_ui_observer.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/browser/child_process_data.h"
11 #include "content/public/browser/notification_service.h"
12 #include "content/public/browser/notification_types.h"
13 #include "content/public/browser/render_process_host.h"
14 #include "content/public/browser/web_contents.h"
15 #include "content/public/browser/web_contents_view.h"
16 #include "content/public/common/content_switches.h"
18 using base::ProcessId;
24 // Makes sure that |dict| has a ListValue under path "log".
25 static base::ListValue* EnsureLogList(base::DictionaryValue* dict) {
26 base::ListValue* log = NULL;
27 if (!dict->GetList("log", &log)) {
28 log = new base::ListValue();
30 dict->Set("log", log);
37 WebRTCInternals::WebRTCInternals()
38 : aec_dump_enabled_(false) {
39 registrar_.Add(this, NOTIFICATION_RENDERER_PROCESS_TERMINATED,
40 NotificationService::AllBrowserContextsAndSources());
41 BrowserChildProcessObserver::Add(this);
42 // TODO(grunell): Shouldn't all the webrtc_internals* files be excluded from the
43 // build if WebRTC is disabled?
44 #if defined(ENABLE_WEBRTC)
45 if (CommandLine::ForCurrentProcess()->HasSwitch(
46 switches::kEnableWebRtcAecRecordings)) {
47 aec_dump_enabled_ = true;
48 aec_dump_file_path_ = CommandLine::ForCurrentProcess()->GetSwitchValuePath(
49 switches::kEnableWebRtcAecRecordings);
51 #if defined(OS_CHROMEOS)
53 base::FilePath(FILE_PATH_LITERAL("/tmp/audio.aecdump"));
54 #elif defined(OS_ANDROID)
56 base::FilePath(FILE_PATH_LITERAL("/sdcard/audio.aecdump"));
58 aec_dump_file_path_ = base::FilePath(FILE_PATH_LITERAL("audio.aecdump"));
61 #endif // defined(ENABLE_WEBRTC)
64 WebRTCInternals::~WebRTCInternals() {
65 BrowserChildProcessObserver::Remove(this);
68 WebRTCInternals* WebRTCInternals::GetInstance() {
69 return Singleton<WebRTCInternals>::get();
72 void WebRTCInternals::OnAddPeerConnection(int render_process_id,
76 const string& servers,
77 const string& constraints) {
78 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
80 base::DictionaryValue* dict = new base::DictionaryValue();
84 dict->SetInteger("rid", render_process_id);
85 dict->SetInteger("pid", static_cast<int>(pid));
86 dict->SetInteger("lid", lid);
87 dict->SetString("servers", servers);
88 dict->SetString("constraints", constraints);
89 dict->SetString("url", url);
90 peer_connection_data_.Append(dict);
92 if (observers_.might_have_observers())
93 SendUpdate("addPeerConnection", dict);
96 void WebRTCInternals::OnRemovePeerConnection(ProcessId pid, int lid) {
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
98 for (size_t i = 0; i < peer_connection_data_.GetSize(); ++i) {
99 base::DictionaryValue* dict = NULL;
100 peer_connection_data_.GetDictionary(i, &dict);
104 dict->GetInteger("pid", &this_pid);
105 dict->GetInteger("lid", &this_lid);
107 if (this_pid != static_cast<int>(pid) || this_lid != lid)
110 peer_connection_data_.Remove(i, NULL);
112 if (observers_.might_have_observers()) {
113 base::DictionaryValue id;
114 id.SetInteger("pid", static_cast<int>(pid));
115 id.SetInteger("lid", lid);
116 SendUpdate("removePeerConnection", &id);
122 void WebRTCInternals::OnUpdatePeerConnection(
123 ProcessId pid, int lid, const string& type, const string& value) {
124 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
126 for (size_t i = 0; i < peer_connection_data_.GetSize(); ++i) {
127 base::DictionaryValue* record = NULL;
128 peer_connection_data_.GetDictionary(i, &record);
130 int this_pid = 0, this_lid = 0;
131 record->GetInteger("pid", &this_pid);
132 record->GetInteger("lid", &this_lid);
134 if (this_pid != static_cast<int>(pid) || this_lid != lid)
137 // Append the update to the end of the log.
138 base::ListValue* log = EnsureLogList(record);
142 base::DictionaryValue* log_entry = new base::DictionaryValue();
146 log_entry->SetString("type", type);
147 log_entry->SetString("value", value);
148 log->Append(log_entry);
150 if (observers_.might_have_observers()) {
151 base::DictionaryValue update;
152 update.SetInteger("pid", static_cast<int>(pid));
153 update.SetInteger("lid", lid);
154 update.SetString("type", type);
155 update.SetString("value", value);
157 SendUpdate("updatePeerConnection", &update);
163 void WebRTCInternals::OnAddStats(base::ProcessId pid, int lid,
164 const base::ListValue& value) {
165 if (!observers_.might_have_observers())
168 base::DictionaryValue dict;
169 dict.SetInteger("pid", static_cast<int>(pid));
170 dict.SetInteger("lid", lid);
172 base::ListValue* list = value.DeepCopy();
176 dict.Set("reports", list);
178 SendUpdate("addStats", &dict);
181 void WebRTCInternals::OnGetUserMedia(int rid,
183 const std::string& origin,
186 const std::string& audio_constraints,
187 const std::string& video_constraints) {
188 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
190 base::DictionaryValue* dict = new base::DictionaryValue();
191 dict->SetInteger("rid", rid);
192 dict->SetInteger("pid", static_cast<int>(pid));
193 dict->SetString("origin", origin);
195 dict->SetString("audio", audio_constraints);
197 dict->SetString("video", video_constraints);
199 get_user_media_requests_.Append(dict);
201 if (observers_.might_have_observers())
202 SendUpdate("addGetUserMedia", dict);
205 void WebRTCInternals::AddObserver(WebRTCInternalsUIObserver *observer) {
206 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
207 observers_.AddObserver(observer);
210 void WebRTCInternals::RemoveObserver(WebRTCInternalsUIObserver *observer) {
211 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
212 observers_.RemoveObserver(observer);
214 // Disables the AEC recording if it is enabled and the last webrtc-internals
215 // page is going away.
216 if (aec_dump_enabled_ && !observers_.might_have_observers())
220 void WebRTCInternals::UpdateObserver(WebRTCInternalsUIObserver* observer) {
221 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
222 if (peer_connection_data_.GetSize() > 0)
223 observer->OnUpdate("updateAllPeerConnections", &peer_connection_data_);
225 for (base::ListValue::iterator it = get_user_media_requests_.begin();
226 it != get_user_media_requests_.end();
228 observer->OnUpdate("addGetUserMedia", *it);
232 void WebRTCInternals::EnableAecDump(content::WebContents* web_contents) {
233 #if defined(ENABLE_WEBRTC)
234 #if defined(OS_ANDROID)
235 EnableAecDumpOnAllRenderProcessHosts();
237 select_file_dialog_ = ui::SelectFileDialog::Create(this, NULL);
238 select_file_dialog_->SelectFile(
239 ui::SelectFileDialog::SELECT_SAVEAS_FILE,
244 FILE_PATH_LITERAL(""),
245 web_contents->GetView()->GetTopLevelNativeWindow(),
251 void WebRTCInternals::DisableAecDump() {
252 #if defined(ENABLE_WEBRTC)
253 aec_dump_enabled_ = false;
254 for (RenderProcessHost::iterator i(
255 content::RenderProcessHost::AllHostsIterator());
256 !i.IsAtEnd(); i.Advance()) {
257 i.GetCurrentValue()->DisableAecDump();
262 void WebRTCInternals::ResetForTesting() {
263 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
265 peer_connection_data_.Clear();
266 get_user_media_requests_.Clear();
267 aec_dump_enabled_ = false;
270 void WebRTCInternals::SendUpdate(const string& command, base::Value* value) {
271 DCHECK(observers_.might_have_observers());
273 FOR_EACH_OBSERVER(WebRTCInternalsUIObserver,
275 OnUpdate(command, value));
278 void WebRTCInternals::BrowserChildProcessCrashed(
279 const ChildProcessData& data) {
280 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
281 OnRendererExit(data.id);
284 void WebRTCInternals::Observe(int type,
285 const NotificationSource& source,
286 const NotificationDetails& details) {
287 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
288 DCHECK_EQ(type, NOTIFICATION_RENDERER_PROCESS_TERMINATED);
289 OnRendererExit(Source<RenderProcessHost>(source)->GetID());
292 void WebRTCInternals::FileSelected(const base::FilePath& path,
293 int /* unused_index */,
294 void* /*unused_params */) {
295 #if defined(ENABLE_WEBRTC)
296 aec_dump_file_path_ = path;
297 EnableAecDumpOnAllRenderProcessHosts();
301 void WebRTCInternals::OnRendererExit(int render_process_id) {
302 // Iterates from the end of the list to remove the PeerConnections created
303 // by the exitting renderer.
304 for (int i = peer_connection_data_.GetSize() - 1; i >= 0; --i) {
305 base::DictionaryValue* record = NULL;
306 peer_connection_data_.GetDictionary(i, &record);
309 record->GetInteger("rid", &this_rid);
311 if (this_rid == render_process_id) {
312 if (observers_.might_have_observers()) {
313 int lid = 0, pid = 0;
314 record->GetInteger("lid", &lid);
315 record->GetInteger("pid", &pid);
317 base::DictionaryValue update;
318 update.SetInteger("lid", lid);
319 update.SetInteger("pid", pid);
320 SendUpdate("removePeerConnection", &update);
322 peer_connection_data_.Remove(i, NULL);
326 bool found_any = false;
327 // Iterates from the end of the list to remove the getUserMedia requests
328 // created by the exiting renderer.
329 for (int i = get_user_media_requests_.GetSize() - 1; i >= 0; --i) {
330 base::DictionaryValue* record = NULL;
331 get_user_media_requests_.GetDictionary(i, &record);
334 record->GetInteger("rid", &this_rid);
336 if (this_rid == render_process_id) {
337 get_user_media_requests_.Remove(i, NULL);
342 if (found_any && observers_.might_have_observers()) {
343 base::DictionaryValue update;
344 update.SetInteger("rid", render_process_id);
345 SendUpdate("removeGetUserMediaForRenderer", &update);
349 #if defined(ENABLE_WEBRTC)
350 void WebRTCInternals::EnableAecDumpOnAllRenderProcessHosts() {
351 aec_dump_enabled_ = true;
352 for (RenderProcessHost::iterator i(
353 content::RenderProcessHost::AllHostsIterator());
354 !i.IsAtEnd(); i.Advance()) {
355 i.GetCurrentValue()->EnableAecDump(aec_dump_file_path_);
360 } // namespace content