1 /* 2 * libjingle 3 * Copyright 2012, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef PEERCONNECTION_SAMPLES_CLIENT_CONDUCTOR_H_ 29 #define PEERCONNECTION_SAMPLES_CLIENT_CONDUCTOR_H_ 30 #pragma once 31 32 #include <deque> 33 #include <map> 34 #include <set> 35 #include <string> 36 37 #include "talk/app/webrtc/mediastreaminterface.h" 38 #include "talk/app/webrtc/peerconnectioninterface.h" 39 #include "talk/examples/peerconnection/client/main_wnd.h" 40 #include "talk/examples/peerconnection/client/peer_connection_client.h" 41 #include "webrtc/base/scoped_ptr.h" 42 43 namespace webrtc { 44 class VideoCaptureModule; 45 } // namespace webrtc 46 47 namespace cricket { 48 class VideoRenderer; 49 } // namespace cricket 50 51 class Conductor 52 : public webrtc::PeerConnectionObserver, 53 public webrtc::CreateSessionDescriptionObserver, 54 public PeerConnectionClientObserver, 55 public MainWndCallback { 56 public: 57 enum CallbackID { 58 MEDIA_CHANNELS_INITIALIZED = 1, 59 PEER_CONNECTION_CLOSED, 60 SEND_MESSAGE_TO_PEER, 61 PEER_CONNECTION_ERROR, 62 NEW_STREAM_ADDED, 63 STREAM_REMOVED, 64 }; 65 66 Conductor(PeerConnectionClient* client, MainWindow* main_wnd); 67 68 bool connection_active() const; 69 70 virtual void Close(); 71 72 protected: 73 ~Conductor(); 74 bool InitializePeerConnection(); 75 void DeletePeerConnection(); 76 void EnsureStreamingUI(); 77 void AddStreams(); 78 cricket::VideoCapturer* OpenVideoCaptureDevice(); 79 80 // 81 // PeerConnectionObserver implementation. 82 // 83 virtual void OnError(); OnStateChange(webrtc::PeerConnectionObserver::StateType state_changed)84 virtual void OnStateChange( 85 webrtc::PeerConnectionObserver::StateType state_changed) {} 86 virtual void OnAddStream(webrtc::MediaStreamInterface* stream); 87 virtual void OnRemoveStream(webrtc::MediaStreamInterface* stream); OnRenegotiationNeeded()88 virtual void OnRenegotiationNeeded() {} OnIceChange()89 virtual void OnIceChange() {} 90 virtual void OnIceCandidate(const webrtc::IceCandidateInterface* candidate); 91 92 // 93 // PeerConnectionClientObserver implementation. 94 // 95 96 virtual void OnSignedIn(); 97 98 virtual void OnDisconnected(); 99 100 virtual void OnPeerConnected(int id, const std::string& name); 101 102 virtual void OnPeerDisconnected(int id); 103 104 virtual void OnMessageFromPeer(int peer_id, const std::string& message); 105 106 virtual void OnMessageSent(int err); 107 108 virtual void OnServerConnectionFailure(); 109 110 // 111 // MainWndCallback implementation. 112 // 113 114 virtual void StartLogin(const std::string& server, int port); 115 116 virtual void DisconnectFromServer(); 117 118 virtual void ConnectToPeer(int peer_id); 119 120 virtual void DisconnectFromCurrentPeer(); 121 122 virtual void UIThreadCallback(int msg_id, void* data); 123 124 // CreateSessionDescriptionObserver implementation. 125 virtual void OnSuccess(webrtc::SessionDescriptionInterface* desc); 126 virtual void OnFailure(const std::string& error); 127 128 protected: 129 // Send a message to the remote peer. 130 void SendMessage(const std::string& json_object); 131 132 int peer_id_; 133 rtc::scoped_refptr<webrtc::PeerConnectionInterface> peer_connection_; 134 rtc::scoped_refptr<webrtc::PeerConnectionFactoryInterface> 135 peer_connection_factory_; 136 PeerConnectionClient* client_; 137 MainWindow* main_wnd_; 138 std::deque<std::string*> pending_messages_; 139 std::map<std::string, rtc::scoped_refptr<webrtc::MediaStreamInterface> > 140 active_streams_; 141 std::string server_; 142 }; 143 144 #endif // PEERCONNECTION_SAMPLES_CLIENT_CONDUCTOR_H_ 145