Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / content / browser / media / webrtc_internals.h
bloba1d7cdf0839502e7dde2126bd8009e40aded7bc4
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"
19 namespace content {
21 class PowerSaveBlocker;
22 class WebContents;
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 {
31 public:
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
42 // PeerConnection.
43 void OnAddPeerConnection(int render_process_id,
44 base::ProcessId pid,
45 int lid,
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,
58 int lid,
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,
76 base::ProcessId pid,
77 const std::string& origin,
78 bool audio,
79 bool video,
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 diagnostic audio recordings for debugging purposes.
91 void EnableAudioDebugRecordings(content::WebContents* web_contents);
92 void DisableAudioDebugRecordings();
94 bool IsAudioDebugRecordingsEnabled() const;
95 const base::FilePath& GetAudioDebugRecordingsFilePath() const;
97 void ResetForTesting();
99 private:
100 friend struct base::DefaultLazyInstanceTraits<WebRTCInternals>;
101 FRIEND_TEST_ALL_PREFIXES(WebRtcAudioDebugRecordingsBrowserTest,
102 CallWithAudioDebugRecordings);
103 FRIEND_TEST_ALL_PREFIXES(WebRtcAudioDebugRecordingsBrowserTest,
104 CallWithAudioDebugRecordingsEnabledThenDisabled);
105 FRIEND_TEST_ALL_PREFIXES(WebRtcAudioDebugRecordingsBrowserTest,
106 TwoCallsWithAudioDebugRecordings);
107 FRIEND_TEST_ALL_PREFIXES(WebRtcInternalsTest,
108 AudioDebugRecordingsFileSelectionCanceled);
110 WebRTCInternals();
111 ~WebRTCInternals() override;
113 void SendUpdate(const std::string& command, base::Value* value);
115 // RenderProcessHostObserver implementation.
116 void RenderProcessHostDestroyed(RenderProcessHost* host) override;
118 // ui::SelectFileDialog::Listener implementation.
119 void FileSelected(const base::FilePath& path,
120 int index,
121 void* unused_params) override;
122 void FileSelectionCanceled(void* params) override;
124 // Called when a renderer exits (including crashes).
125 void OnRendererExit(int render_process_id);
127 #if defined(ENABLE_WEBRTC)
128 // Enables diagnostic audio recordings on all render process hosts using
129 // |audio_debug_recordings_file_path_|.
130 void EnableAudioDebugRecordingsOnAllRenderProcessHosts();
131 #endif
133 // Called whenever an element is added to or removed from
134 // |peer_connection_data_| to impose/release a block on suspending the current
135 // application for power-saving.
136 void CreateOrReleasePowerSaveBlocker();
138 base::ObserverList<WebRTCInternalsUIObserver> observers_;
140 // |peer_connection_data_| is a list containing all the PeerConnection
141 // updates.
142 // Each item of the list represents the data for one PeerConnection, which
143 // contains these fields:
144 // "rid" -- the renderer id.
145 // "pid" -- OS process id of the renderer that creates the PeerConnection.
146 // "lid" -- local Id assigned to the PeerConnection.
147 // "url" -- url of the web page that created the PeerConnection.
148 // "servers" and "constraints" -- server configuration and media constraints
149 // used to initialize the PeerConnection respectively.
150 // "log" -- a ListValue contains all the updates for the PeerConnection. Each
151 // list item is a DictionaryValue containing "time", which is the number of
152 // milliseconds since epoch as a string, and "type" and "value", both of which
153 // are strings representing the event.
154 base::ListValue peer_connection_data_;
156 // A list of getUserMedia requests. Each item is a DictionaryValue that
157 // contains these fields:
158 // "rid" -- the renderer id.
159 // "pid" -- proceddId of the renderer.
160 // "origin" -- the security origin of the request.
161 // "audio" -- the serialized audio constraints if audio is requested.
162 // "video" -- the serialized video constraints if video is requested.
163 base::ListValue get_user_media_requests_;
165 // For managing select file dialog.
166 scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
168 // Diagnostic audio recording state.
169 bool audio_debug_recordings_;
170 base::FilePath audio_debug_recordings_file_path_;
172 // While |peer_connection_data_| is non-empty, hold an instance of
173 // PowerSaveBlocker. This prevents the application from being suspended while
174 // remoting.
175 scoped_ptr<PowerSaveBlocker> power_save_blocker_;
177 // Set of render process hosts that |this| is registered as an observer on.
178 base::hash_set<int> render_process_id_set_;
181 } // namespace content
183 #endif // CONTENT_BROWSER_MEDIA_WEBRTC_INTERNALS_H_