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 #ifndef CONTENT_BROWSER_MEDIA_WEBRTC_INTERNALS_H_
6 #define CONTENT_BROWSER_MEDIA_WEBRTC_INTERNALS_H_
8 #include "base/containers/hash_tables.h"
9 #include "base/gtest_prod_util.h"
10 #include "base/lazy_instance.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/observer_list.h"
13 #include "base/process/process.h"
14 #include "base/values.h"
15 #include "content/common/content_export.h"
16 #include "content/public/browser/render_process_host_observer.h"
17 #include "ui/shell_dialogs/select_file_dialog.h"
21 class PowerSaveBlocker
;
23 class WebRTCInternalsUIObserver
;
25 // This is a singleton class running in the browser UI thread.
26 // It collects peer connection infomation from the renderers,
27 // forwards the data to WebRTCInternalsUIObserver and
28 // sends data collecting commands to the renderers.
29 class CONTENT_EXPORT WebRTCInternals
: public RenderProcessHostObserver
,
30 public ui::SelectFileDialog::Listener
{
32 static WebRTCInternals
* GetInstance();
34 // This method is called when a PeerConnection is created.
35 // |render_process_id| is the id of the render process (not OS pid), which is
36 // needed because we might not be able to get the OS process id when the
37 // render process terminates and we want to clean up.
38 // |pid| is the renderer process id, |lid| is the renderer local id used to
39 // identify a PeerConnection, |url| is the url of the tab owning the
40 // PeerConnection, |rtc_configuration| is the serialized RTCConfiguration,
41 // |constraints| is the media constraints used to initialize the
43 void OnAddPeerConnection(int render_process_id
,
46 const std::string
& url
,
47 const std::string
& rtc_configuration
,
48 const std::string
& constraints
);
50 // This method is called when PeerConnection is destroyed.
51 // |pid| is the renderer process id, |lid| is the renderer local id.
52 void OnRemovePeerConnection(base::ProcessId pid
, int lid
);
54 // This method is called when a PeerConnection is updated.
55 // |pid| is the renderer process id, |lid| is the renderer local id,
56 // |type| is the update type, |value| is the detail of the update.
57 void OnUpdatePeerConnection(base::ProcessId pid
,
59 const std::string
& type
,
60 const std::string
& value
);
62 // This method is called when results from PeerConnectionInterface::GetStats
63 // are available. |pid| is the renderer process id, |lid| is the renderer
64 // local id, |value| is the list of stats reports.
65 void OnAddStats(base::ProcessId pid
, int lid
, const base::ListValue
& value
);
67 // This method is called when getUserMedia is called. |render_process_id| is
68 // the id of the render process (not OS pid), which is needed because we might
69 // not be able to get the OS process id when the render process terminates and
70 // we want to clean up. |pid| is the renderer OS process id, |origin| is the
71 // security origin of the getUserMedia call, |audio| is true if audio stream
72 // is requested, |video| is true if the video stream is requested,
73 // |audio_constraints| is the constraints for the audio, |video_constraints|
74 // is the constraints for the video.
75 void OnGetUserMedia(int render_process_id
,
77 const std::string
& origin
,
80 const std::string
& audio_constraints
,
81 const std::string
& video_constraints
);
83 // Methods for adding or removing WebRTCInternalsUIObserver.
84 void AddObserver(WebRTCInternalsUIObserver
*observer
);
85 void RemoveObserver(WebRTCInternalsUIObserver
*observer
);
87 // Sends all update data to |observer|.
88 void UpdateObserver(WebRTCInternalsUIObserver
* observer
);
90 // Enables or disables AEC dump (diagnostic echo canceller recording).
91 void EnableAecDump(content::WebContents
* web_contents
);
92 void DisableAecDump();
94 bool aec_dump_enabled() {
95 return aec_dump_enabled_
;
98 base::FilePath
aec_dump_file_path() {
99 return aec_dump_file_path_
;
102 void ResetForTesting();
105 friend struct base::DefaultLazyInstanceTraits
<WebRTCInternals
>;
106 FRIEND_TEST_ALL_PREFIXES(WebRtcAecDumpBrowserTest
, CallWithAecDump
);
107 FRIEND_TEST_ALL_PREFIXES(WebRtcAecDumpBrowserTest
,
108 CallWithAecDumpEnabledThenDisabled
);
109 FRIEND_TEST_ALL_PREFIXES(WebRtcAecDumpBrowserTest
, TwoCallsWithAecDump
);
110 FRIEND_TEST_ALL_PREFIXES(WebRtcInternalsTest
,
111 AecRecordingFileSelectionCanceled
);
114 ~WebRTCInternals() override
;
116 void SendUpdate(const std::string
& command
, base::Value
* value
);
118 // RenderProcessHostObserver implementation.
119 void RenderProcessHostDestroyed(RenderProcessHost
* host
) override
;
121 // ui::SelectFileDialog::Listener implementation.
122 void FileSelected(const base::FilePath
& path
,
124 void* unused_params
) override
;
125 void FileSelectionCanceled(void* params
) override
;
127 // Called when a renderer exits (including crashes).
128 void OnRendererExit(int render_process_id
);
130 #if defined(ENABLE_WEBRTC)
131 // Enables AEC dump on all render process hosts using |aec_dump_file_path_|.
132 void EnableAecDumpOnAllRenderProcessHosts();
135 // Called whenever an element is added to or removed from
136 // |peer_connection_data_| to impose/release a block on suspending the current
137 // application for power-saving.
138 void CreateOrReleasePowerSaveBlocker();
140 base::ObserverList
<WebRTCInternalsUIObserver
> observers_
;
142 // |peer_connection_data_| is a list containing all the PeerConnection
144 // Each item of the list represents the data for one PeerConnection, which
145 // contains these fields:
146 // "rid" -- the renderer id.
147 // "pid" -- OS process id of the renderer that creates the PeerConnection.
148 // "lid" -- local Id assigned to the PeerConnection.
149 // "url" -- url of the web page that created the PeerConnection.
150 // "servers" and "constraints" -- server configuration and media constraints
151 // used to initialize the PeerConnection respectively.
152 // "log" -- a ListValue contains all the updates for the PeerConnection. Each
153 // list item is a DictionaryValue containing "time", which is the number of
154 // milliseconds since epoch as a string, and "type" and "value", both of which
155 // are strings representing the event.
156 base::ListValue peer_connection_data_
;
158 // A list of getUserMedia requests. Each item is a DictionaryValue that
159 // contains these fields:
160 // "rid" -- the renderer id.
161 // "pid" -- proceddId of the renderer.
162 // "origin" -- the security origin of the request.
163 // "audio" -- the serialized audio constraints if audio is requested.
164 // "video" -- the serialized video constraints if video is requested.
165 base::ListValue get_user_media_requests_
;
167 // For managing select file dialog.
168 scoped_refptr
<ui::SelectFileDialog
> select_file_dialog_
;
170 // AEC dump (diagnostic echo canceller recording) state.
171 bool aec_dump_enabled_
;
172 base::FilePath aec_dump_file_path_
;
174 // While |peer_connection_data_| is non-empty, hold an instance of
175 // PowerSaveBlocker. This prevents the application from being suspended while
177 scoped_ptr
<PowerSaveBlocker
> power_save_blocker_
;
179 // Set of render process hosts that |this| is registered as an observer on.
180 base::hash_set
<int> render_process_id_set_
;
183 } // namespace content
185 #endif // CONTENT_BROWSER_MEDIA_WEBRTC_INTERNALS_H_