1 /*
2  *  Copyright 2012 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #ifndef WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_CONDUCTOR_H_
12 #define WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_CONDUCTOR_H_
13 #pragma once
14 
15 #include <deque>
16 #include <map>
17 #include <set>
18 #include <string>
19 
20 #include "talk/app/webrtc/mediastreaminterface.h"
21 #include "talk/app/webrtc/peerconnectioninterface.h"
22 #include "webrtc/examples/peerconnection/client/main_wnd.h"
23 #include "webrtc/examples/peerconnection/client/peer_connection_client.h"
24 #include "webrtc/base/scoped_ptr.h"
25 
26 namespace webrtc {
27 class VideoCaptureModule;
28 }  // namespace webrtc
29 
30 namespace cricket {
31 class VideoRenderer;
32 }  // namespace cricket
33 
34 class Conductor
35   : public webrtc::PeerConnectionObserver,
36     public webrtc::CreateSessionDescriptionObserver,
37     public PeerConnectionClientObserver,
38     public MainWndCallback {
39  public:
40   enum CallbackID {
41     MEDIA_CHANNELS_INITIALIZED = 1,
42     PEER_CONNECTION_CLOSED,
43     SEND_MESSAGE_TO_PEER,
44     NEW_STREAM_ADDED,
45     STREAM_REMOVED,
46   };
47 
48   Conductor(PeerConnectionClient* client, MainWindow* main_wnd);
49 
50   bool connection_active() const;
51 
52   virtual void Close();
53 
54  protected:
55   ~Conductor();
56   bool InitializePeerConnection();
57   bool ReinitializePeerConnectionForLoopback();
58   bool CreatePeerConnection(bool dtls);
59   void DeletePeerConnection();
60   void EnsureStreamingUI();
61   void AddStreams();
62   cricket::VideoCapturer* OpenVideoCaptureDevice();
63 
64   //
65   // PeerConnectionObserver implementation.
66   //
OnStateChange(webrtc::PeerConnectionObserver::StateType state_changed)67   virtual void OnStateChange(
68       webrtc::PeerConnectionObserver::StateType state_changed) {}
69   virtual void OnAddStream(webrtc::MediaStreamInterface* stream);
70   virtual void OnRemoveStream(webrtc::MediaStreamInterface* stream);
OnDataChannel(webrtc::DataChannelInterface * channel)71   virtual void OnDataChannel(webrtc::DataChannelInterface* channel) {}
OnRenegotiationNeeded()72   virtual void OnRenegotiationNeeded() {}
OnIceChange()73   virtual void OnIceChange() {}
74   virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate);
75 
76   //
77   // PeerConnectionClientObserver implementation.
78   //
79 
80   virtual void OnSignedIn();
81 
82   virtual void OnDisconnected();
83 
84   virtual void OnPeerConnected(int id, const std::string& name);
85 
86   virtual void OnPeerDisconnected(int id);
87 
88   virtual void OnMessageFromPeer(int peer_id, const std::string& message);
89 
90   virtual void OnMessageSent(int err);
91 
92   virtual void OnServerConnectionFailure();
93 
94   //
95   // MainWndCallback implementation.
96   //
97 
98   virtual void StartLogin(const std::string& server, int port);
99 
100   virtual void DisconnectFromServer();
101 
102   virtual void ConnectToPeer(int peer_id);
103 
104   virtual void DisconnectFromCurrentPeer();
105 
106   virtual void UIThreadCallback(int msg_id, void* data);
107 
108   // CreateSessionDescriptionObserver implementation.
109   virtual void OnSuccess(webrtc::SessionDescriptionInterface* desc);
110   virtual void OnFailure(const std::string& error);
111 
112  protected:
113   // Send a message to the remote peer.
114   void SendMessage(const std::string& json_object);
115 
116   int peer_id_;
117   bool loopback_;
118   rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
119   rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
120       peer_connection_factory_;
121   PeerConnectionClient* client_;
122   MainWindow* main_wnd_;
123   std::deque<std::string*> pending_messages_;
124   std::map<std::string, rtc::scoped_refptr<webrtc::MediaStreamInterface> >
125       active_streams_;
126   std::string server_;
127 };
128 
129 #endif  // WEBRTC_EXAMPLES_PEERCONNECTION_CLIENT_CONDUCTOR_H_
130