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 EXAMPLES_PEERCONNECTION_CLIENT_CONDUCTOR_H_
12 #define EXAMPLES_PEERCONNECTION_CLIENT_CONDUCTOR_H_
13 
14 #include <deque>
15 #include <map>
16 #include <memory>
17 #include <string>
18 #include <vector>
19 
20 #include "api/media_stream_interface.h"
21 #include "api/peer_connection_interface.h"
22 #include "examples/peerconnection/client/main_wnd.h"
23 #include "examples/peerconnection/client/peer_connection_client.h"
24 
25 namespace webrtc {
26 class VideoCaptureModule;
27 }  // namespace webrtc
28 
29 namespace cricket {
30 class VideoRenderer;
31 }  // namespace cricket
32 
33 class Conductor : public webrtc::PeerConnectionObserver,
34                   public webrtc::CreateSessionDescriptionObserver,
35                   public PeerConnectionClientObserver,
36                   public MainWndCallback {
37  public:
38   enum CallbackID {
39     MEDIA_CHANNELS_INITIALIZED = 1,
40     PEER_CONNECTION_CLOSED,
41     SEND_MESSAGE_TO_PEER,
42     NEW_TRACK_ADDED,
43     TRACK_REMOVED,
44   };
45 
46   Conductor(PeerConnectionClient* client, MainWindow* main_wnd);
47 
48   bool connection_active() const;
49 
50   void Close() override;
51 
52  protected:
53   ~Conductor();
54   bool InitializePeerConnection();
55   bool ReinitializePeerConnectionForLoopback();
56   bool CreatePeerConnection(bool dtls);
57   void DeletePeerConnection();
58   void EnsureStreamingUI();
59   void AddTracks();
60 
61   //
62   // PeerConnectionObserver implementation.
63   //
64 
OnSignalingChange(webrtc::PeerConnectionInterface::SignalingState new_state)65   void OnSignalingChange(
66       webrtc::PeerConnectionInterface::SignalingState new_state) override {}
67   void OnAddTrack(
68       rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver,
69       const std::vector<rtc::scoped_refptr<webrtc::MediaStreamInterface>>&
70           streams) override;
71   void OnRemoveTrack(
72       rtc::scoped_refptr<webrtc::RtpReceiverInterface> receiver) override;
OnDataChannel(rtc::scoped_refptr<webrtc::DataChannelInterface> channel)73   void OnDataChannel(
74       rtc::scoped_refptr<webrtc::DataChannelInterface> channel) override {}
OnRenegotiationNeeded()75   void OnRenegotiationNeeded() override {}
OnIceConnectionChange(webrtc::PeerConnectionInterface::IceConnectionState new_state)76   void OnIceConnectionChange(
77       webrtc::PeerConnectionInterface::IceConnectionState new_state) override {}
OnIceGatheringChange(webrtc::PeerConnectionInterface::IceGatheringState new_state)78   void OnIceGatheringChange(
79       webrtc::PeerConnectionInterface::IceGatheringState new_state) override {}
80   void OnIceCandidate(const webrtc::IceCandidateInterface* candidate) override;
OnIceConnectionReceivingChange(bool receiving)81   void OnIceConnectionReceivingChange(bool receiving) override {}
82 
83   //
84   // PeerConnectionClientObserver implementation.
85   //
86 
87   void OnSignedIn() override;
88 
89   void OnDisconnected() override;
90 
91   void OnPeerConnected(int id, const std::string& name) override;
92 
93   void OnPeerDisconnected(int id) override;
94 
95   void OnMessageFromPeer(int peer_id, const std::string& message) override;
96 
97   void OnMessageSent(int err) override;
98 
99   void OnServerConnectionFailure() override;
100 
101   //
102   // MainWndCallback implementation.
103   //
104 
105   void StartLogin(const std::string& server, int port) override;
106 
107   void DisconnectFromServer() override;
108 
109   void ConnectToPeer(int peer_id) override;
110 
111   void DisconnectFromCurrentPeer() override;
112 
113   void UIThreadCallback(int msg_id, void* data) override;
114 
115   // CreateSessionDescriptionObserver implementation.
116   void OnSuccess(webrtc::SessionDescriptionInterface* desc) override;
117   void OnFailure(webrtc::RTCError error) override;
118 
119  protected:
120   // Send a message to the remote peer.
121   void SendMessage(const std::string& json_object);
122 
123   int peer_id_;
124   bool loopback_;
125   rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_;
126   rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface>
127       peer_connection_factory_;
128   PeerConnectionClient* client_;
129   MainWindow* main_wnd_;
130   std::deque<std::string*> pending_messages_;
131   std::string server_;
132 };
133 
134 #endif  // EXAMPLES_PEERCONNECTION_CLIENT_CONDUCTOR_H_
135