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