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 std::vector<webrtc::PeerConnectionInterface::IceServer>& servers, 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 std::vector<webrtc::PeerConnectionInterface::IceServer>& servers, 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, Source source); 102 103 // Sends an update when a media stream is added. 104 virtual void TrackAddStream( 105 RTCPeerConnectionHandler* pc_handler, 106 const blink::WebMediaStream& stream, Source source); 107 108 // Sends an update when a media stream is removed. 109 virtual void TrackRemoveStream( 110 RTCPeerConnectionHandler* pc_handler, 111 const blink::WebMediaStream& stream, Source source); 112 113 // Sends an update when a DataChannel is created. 114 virtual void TrackCreateDataChannel( 115 RTCPeerConnectionHandler* pc_handler, 116 const webrtc::DataChannelInterface* data_channel, Source source); 117 118 // Sends an update when a PeerConnection has been stopped. 119 virtual void TrackStop(RTCPeerConnectionHandler* pc_handler); 120 121 // Sends an update when the signaling state of a PeerConnection has changed. 122 virtual void TrackSignalingStateChange( 123 RTCPeerConnectionHandler* pc_handler, 124 blink::WebRTCPeerConnectionHandlerClient::SignalingState state); 125 126 // Sends an update when the Ice connection state 127 // of a PeerConnection has changed. 128 virtual void TrackIceConnectionStateChange( 129 RTCPeerConnectionHandler* pc_handler, 130 blink::WebRTCPeerConnectionHandlerClient::ICEConnectionState state); 131 132 // Sends an update when the Ice gathering state 133 // of a PeerConnection has changed. 134 virtual void TrackIceGatheringStateChange( 135 RTCPeerConnectionHandler* pc_handler, 136 blink::WebRTCPeerConnectionHandlerClient::ICEGatheringState state); 137 138 // Sends an update when the SetSessionDescription or CreateOffer or 139 // CreateAnswer callbacks are called. 140 virtual void TrackSessionDescriptionCallback( 141 RTCPeerConnectionHandler* pc_handler, Action action, 142 const std::string& type, const std::string& value); 143 144 // Sends an update when onRenegotiationNeeded is called. 145 virtual void TrackOnRenegotiationNeeded(RTCPeerConnectionHandler* pc_handler); 146 147 // Sends an update when a DTMFSender is created. 148 virtual void TrackCreateDTMFSender( 149 RTCPeerConnectionHandler* pc_handler, 150 const blink::WebMediaStreamTrack& track); 151 152 // Sends an update when getUserMedia is called. 153 virtual void TrackGetUserMedia( 154 const blink::WebUserMediaRequest& user_media_request); 155 156 private: 157 // Assign a local ID to a peer connection so that the browser process can 158 // uniquely identify a peer connection in the renderer process. 159 int GetNextLocalID(); 160 161 // IPC Message handler for getting all stats. 162 void OnGetAllStats(); 163 164 void SendPeerConnectionUpdate(RTCPeerConnectionHandler* pc_handler, 165 const std::string& callback_type, 166 const std::string& value); 167 168 // This map stores the local ID assigned to each RTCPeerConnectionHandler. 169 typedef std::map<RTCPeerConnectionHandler*, int> PeerConnectionIdMap; 170 PeerConnectionIdMap peer_connection_id_map_; 171 172 // This keeps track of the next available local ID. 173 int next_lid_; 174 175 DISALLOW_COPY_AND_ASSIGN(PeerConnectionTracker); 176 }; 177 178 } // namespace content 179 180 #endif // CONTENT_RENDERER_MEDIA_PEERCONNECTION_TRACKER_H_ 181