Home | History | Annotate | Download | only in media
      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.
      4 
      5 #ifndef CONTENT_BROWSER_MEDIA_WEBRTC_INTERNALS_H_
      6 #define CONTENT_BROWSER_MEDIA_WEBRTC_INTERNALS_H_
      7 
      8 #include "base/gtest_prod_util.h"
      9 #include "base/lazy_instance.h"
     10 #include "base/memory/scoped_ptr.h"
     11 #include "base/observer_list.h"
     12 #include "base/process/process.h"
     13 #include "base/values.h"
     14 #include "content/common/content_export.h"
     15 #include "content/public/browser/notification_observer.h"
     16 #include "content/public/browser/notification_registrar.h"
     17 #include "ui/shell_dialogs/select_file_dialog.h"
     18 
     19 namespace content {
     20 
     21 class PowerSaveBlocker;
     22 class WebContents;
     23 class WebRTCInternalsUIObserver;
     24 
     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 NotificationObserver,
     30                                        public ui::SelectFileDialog::Listener {
     31  public:
     32   static WebRTCInternals* GetInstance();
     33 
     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);
     49 
     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);
     53 
     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);
     61 
     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);
     66 
     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);
     82 
     83   // Methods for adding or removing WebRTCInternalsUIObserver.
     84   void AddObserver(WebRTCInternalsUIObserver *observer);
     85   void RemoveObserver(WebRTCInternalsUIObserver *observer);
     86 
     87   // Sends all update data to |observer|.
     88   void UpdateObserver(WebRTCInternalsUIObserver* observer);
     89 
     90   // Enables or disables AEC dump (diagnostic echo canceller recording).
     91   void EnableAecDump(content::WebContents* web_contents);
     92   void DisableAecDump();
     93 
     94   bool aec_dump_enabled() {
     95     return aec_dump_enabled_;
     96   }
     97 
     98   base::FilePath aec_dump_file_path() {
     99     return aec_dump_file_path_;
    100   }
    101 
    102   void ResetForTesting();
    103 
    104  private:
    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);
    112 
    113   WebRTCInternals();
    114   virtual ~WebRTCInternals();
    115 
    116   void SendUpdate(const std::string& command, base::Value* value);
    117 
    118   // NotificationObserver implementation.
    119   virtual void Observe(int type,
    120                        const NotificationSource& source,
    121                        const NotificationDetails& details) OVERRIDE;
    122 
    123   // ui::SelectFileDialog::Listener implementation.
    124   virtual void FileSelected(const base::FilePath& path,
    125                             int index,
    126                             void* unused_params) OVERRIDE;
    127   virtual void FileSelectionCanceled(void* params) OVERRIDE;
    128 
    129   // Called when a renderer exits (including crashes).
    130   void OnRendererExit(int render_process_id);
    131 
    132 #if defined(ENABLE_WEBRTC)
    133   // Enables AEC dump on all render process hosts using |aec_dump_file_path_|.
    134   void EnableAecDumpOnAllRenderProcessHosts();
    135 #endif
    136 
    137   // Called whenever an element is added to or removed from
    138   // |peer_connection_data_| to impose/release a block on suspending the current
    139   // application for power-saving.
    140   void CreateOrReleasePowerSaveBlocker();
    141 
    142   ObserverList<WebRTCInternalsUIObserver> observers_;
    143 
    144   // |peer_connection_data_| is a list containing all the PeerConnection
    145   // updates.
    146   // Each item of the list represents the data for one PeerConnection, which
    147   // contains these fields:
    148   // "rid" -- the renderer id.
    149   // "pid" -- OS process id of the renderer that creates the PeerConnection.
    150   // "lid" -- local Id assigned to the PeerConnection.
    151   // "url" -- url of the web page that created the PeerConnection.
    152   // "servers" and "constraints" -- server configuration and media constraints
    153   // used to initialize the PeerConnection respectively.
    154   // "log" -- a ListValue contains all the updates for the PeerConnection. Each
    155   // list item is a DictionaryValue containing "time", which is the number of
    156   // milliseconds since epoch as a string, and "type" and "value", both of which
    157   // are strings representing the event.
    158   base::ListValue peer_connection_data_;
    159 
    160   // A list of getUserMedia requests. Each item is a DictionaryValue that
    161   // contains these fields:
    162   // "rid" -- the renderer id.
    163   // "pid" -- proceddId of the renderer.
    164   // "origin" -- the security origin of the request.
    165   // "audio" -- the serialized audio constraints if audio is requested.
    166   // "video" -- the serialized video constraints if video is requested.
    167   base::ListValue get_user_media_requests_;
    168 
    169   NotificationRegistrar registrar_;
    170 
    171   // For managing select file dialog.
    172   scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
    173 
    174   // AEC dump (diagnostic echo canceller recording) state.
    175   bool aec_dump_enabled_;
    176   base::FilePath aec_dump_file_path_;
    177 
    178   // While |peer_connection_data_| is non-empty, hold an instance of
    179   // PowerSaveBlocker.  This prevents the application from being suspended while
    180   // remoting.
    181   scoped_ptr<PowerSaveBlocker> power_save_blocker_;
    182 };
    183 
    184 }  // namespace content
    185 
    186 #endif  // CONTENT_BROWSER_MEDIA_WEBRTC_INTERNALS_H_
    187