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_RENDERER_MEDIA_PEERCONNECTION_TRACKER_H_
      6 #define CONTENT_RENDERER_MEDIA_PEERCONNECTION_TRACKER_H_
      7 
      8 #include <map>
      9 
     10 #include "base/compiler_specific.h"
     11 #include "content/public/renderer/render_process_observer.h"
     12 #include "third_party/WebKit/public/platform/WebMediaStream.h"
     13 #include "third_party/WebKit/public/platform/WebRTCPeerConnectionHandlerClient.h"
     14 #include "third_party/WebKit/public/platform/WebRTCSessionDescription.h"
     15 #include "third_party/libjingle/source/talk/app/webrtc/peerconnectioninterface.h"
     16 
     17 namespace blink {
     18 class WebFrame;
     19 class WebRTCICECandidate;
     20 class WebString;
     21 class WebRTCSessionDescription;
     22 class WebUserMediaRequest;
     23 }  // namespace blink
     24 
     25 namespace webrtc {
     26 class DataChannelInterface;
     27 }  // namespace webrtc
     28 
     29 namespace content {
     30 class RTCMediaConstraints;
     31 class RTCPeerConnectionHandler;
     32 
     33 // This class collects data about each peer connection,
     34 // sends it to the browser process, and handles messages
     35 // from the browser process.
     36 class CONTENT_EXPORT PeerConnectionTracker : public RenderProcessObserver {
     37  public:
     38   PeerConnectionTracker();
     39   virtual ~PeerConnectionTracker();
     40 
     41   enum Source {
     42     SOURCE_LOCAL,
     43     SOURCE_REMOTE
     44   };
     45 
     46   enum Action {
     47     ACTION_SET_LOCAL_DESCRIPTION,
     48     ACTION_SET_REMOTE_DESCRIPTION,
     49     ACTION_CREATE_OFFER,
     50     ACTION_CREATE_ANSWER
     51   };
     52 
     53   // RenderProcessObserver implementation.
     54   virtual bool OnControlMessageReceived(const IPC::Message& message) OVERRIDE;
     55 
     56   //
     57   // The following methods send an update to the browser process when a
     58   // PeerConnection update happens. The caller should call the Track* methods
     59   // after calling RegisterPeerConnection and before calling
     60   // UnregisterPeerConnection, otherwise the Track* call has no effect.
     61   //
     62 
     63   // Sends an update when a PeerConnection has been created in Javascript.
     64   // This should be called once and only once for each PeerConnection.
     65   // The |pc_handler| is the handler object associated with the PeerConnection,
     66   // the |servers| are the server configurations used to establish the
     67   // connection, the |constraints| are the media constraints used to initialize
     68   // the PeerConnection, the |frame| is the WebFrame object representing the
     69   // page in which the PeerConnection is created.
     70   void RegisterPeerConnection(
     71       RTCPeerConnectionHandler* pc_handler,
     72       const webrtc::PeerConnectionInterface::RTCConfiguration& config,
     73       const RTCMediaConstraints& constraints,
     74       const blink::WebFrame* frame);
     75 
     76   // Sends an update when a PeerConnection has been destroyed.
     77   virtual void UnregisterPeerConnection(RTCPeerConnectionHandler* pc_handler);
     78 
     79   // Sends an update when createOffer/createAnswer has been called.
     80   // The |pc_handler| is the handler object associated with the PeerConnection,
     81   // the |constraints| is the media constraints used to create the offer/answer.
     82   virtual void TrackCreateOffer(RTCPeerConnectionHandler* pc_handler,
     83                                 const RTCMediaConstraints& constraints);
     84   virtual void TrackCreateAnswer(RTCPeerConnectionHandler* pc_handler,
     85                                  const RTCMediaConstraints& constraints);
     86 
     87   // Sends an update when setLocalDescription or setRemoteDescription is called.
     88   virtual void TrackSetSessionDescription(
     89       RTCPeerConnectionHandler* pc_handler,
     90       const blink::WebRTCSessionDescription& desc, Source source);
     91 
     92   // Sends an update when Ice candidates are updated.
     93   virtual void TrackUpdateIce(
     94       RTCPeerConnectionHandler* pc_handler,
     95       const webrtc::PeerConnectionInterface::RTCConfiguration& config,
     96       const RTCMediaConstraints& options);
     97 
     98   // Sends an update when an Ice candidate is added.
     99   virtual void TrackAddIceCandidate(
    100       RTCPeerConnectionHandler* pc_handler,
    101       const blink::WebRTCICECandidate& candidate,
    102       Source source,
    103       bool succeeded);
    104 
    105   // Sends an update when a media stream is added.
    106   virtual void TrackAddStream(
    107       RTCPeerConnectionHandler* pc_handler,
    108       const blink::WebMediaStream& stream, Source source);
    109 
    110   // Sends an update when a media stream is removed.
    111   virtual void TrackRemoveStream(
    112       RTCPeerConnectionHandler* pc_handler,
    113       const blink::WebMediaStream& stream, Source source);
    114 
    115   // Sends an update when a DataChannel is created.
    116   virtual void TrackCreateDataChannel(
    117       RTCPeerConnectionHandler* pc_handler,
    118       const webrtc::DataChannelInterface* data_channel, Source source);
    119 
    120   // Sends an update when a PeerConnection has been stopped.
    121   virtual void TrackStop(RTCPeerConnectionHandler* pc_handler);
    122 
    123   // Sends an update when the signaling state of a PeerConnection has changed.
    124   virtual void TrackSignalingStateChange(
    125       RTCPeerConnectionHandler* pc_handler,
    126       blink::WebRTCPeerConnectionHandlerClient::SignalingState state);
    127 
    128   // Sends an update when the Ice connection state
    129   // of a PeerConnection has changed.
    130   virtual void TrackIceConnectionStateChange(
    131       RTCPeerConnectionHandler* pc_handler,
    132       blink::WebRTCPeerConnectionHandlerClient::ICEConnectionState state);
    133 
    134   // Sends an update when the Ice gathering state
    135   // of a PeerConnection has changed.
    136   virtual void TrackIceGatheringStateChange(
    137       RTCPeerConnectionHandler* pc_handler,
    138       blink::WebRTCPeerConnectionHandlerClient::ICEGatheringState state);
    139 
    140   // Sends an update when the SetSessionDescription or CreateOffer or
    141   // CreateAnswer callbacks are called.
    142   virtual void TrackSessionDescriptionCallback(
    143       RTCPeerConnectionHandler* pc_handler, Action action,
    144       const std::string& type, const std::string& value);
    145 
    146   // Sends an update when onRenegotiationNeeded is called.
    147   virtual void TrackOnRenegotiationNeeded(RTCPeerConnectionHandler* pc_handler);
    148 
    149   // Sends an update when a DTMFSender is created.
    150   virtual void TrackCreateDTMFSender(
    151       RTCPeerConnectionHandler* pc_handler,
    152       const blink::WebMediaStreamTrack& track);
    153 
    154   // Sends an update when getUserMedia is called.
    155   virtual void TrackGetUserMedia(
    156       const blink::WebUserMediaRequest& user_media_request);
    157 
    158  private:
    159   // Assign a local ID to a peer connection so that the browser process can
    160   // uniquely identify a peer connection in the renderer process.
    161   int GetNextLocalID();
    162 
    163   // IPC Message handler for getting all stats.
    164   void OnGetAllStats();
    165 
    166   // Called when the browser process reports a suspend event from the OS.
    167   void OnSuspend();
    168 
    169   void SendPeerConnectionUpdate(RTCPeerConnectionHandler* pc_handler,
    170                                 const std::string& callback_type,
    171                                 const std::string& value);
    172 
    173   // This map stores the local ID assigned to each RTCPeerConnectionHandler.
    174   typedef std::map<RTCPeerConnectionHandler*, int> PeerConnectionIdMap;
    175   PeerConnectionIdMap peer_connection_id_map_;
    176 
    177   // This keeps track of the next available local ID.
    178   int next_lid_;
    179 
    180   DISALLOW_COPY_AND_ASSIGN(PeerConnectionTracker);
    181 };
    182 
    183 }  // namespace content
    184 
    185 #endif  // CONTENT_RENDERER_MEDIA_PEERCONNECTION_TRACKER_H_
    186