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