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