peerconnection_client简单分析

/******** main.cc ********/
wWinMain()
	wnd()
	PeerConnectionClient client;//用于信令处理相关
	rtc::scoped_refptr<Conductor> conductor(new rtc::RefCountedObject<Conductor>(&client, &wnd));
	while((gm = ::GetMessage(&msg, NULL, 0, 0)) != 0 && gm != -1)
		if (!wnd.PreTranslateMessage(&msg))
			::TranslateMessage(&msg);
			::DispatchMessage(&msg);
			
wnd.PreTranslateMessage(&msg)
	if (msg->message == WM_CHAR)
		if (msg->wParam == VK_TAB)
			HandleTabbing()
		else if (msg->wParam == VK_RETURN)
			OnDefaultAction();
		else if (msg->wParam == VK_ESCAPE)
			if (ui_ == STREAMING)
				callback_->DisconnectFromCurrentPeer();
			else
				callback_->DisconnectFromServer();
	
	else if (msg->hwnd == NULL && msg->message == UI_THREAD_CALLBACK)
		callback_->UIThreadCallback(static_cast<int>(msg->wParam),
                               reinterpret_cast<void*>(msg->lParam));
			switch (msg_id)
				case SEND_MESSAGE_TO_PEER:
					client_->SendToPeer(peer_id_, *msg)
						sprintfn(headers, sizeof(headers),
								"POST /message?peer_id=%i&to=%i HTTP/1.0\r\n"
								"Content-Length: %i\r\n"
								"Content-Type: text/plain\r\n"
								"\r\n", my_id_, peer_id, message.length());
						onconnect_data_ = headers;
						onconnect_data_ += message;
						ConnectControlSocket()
							control_socket_->Connect(server_address_);//触发连接事件,在回调函数中进行数据发送
				case NEW_STREAM_ADDED:
					webrtc::MediaStreamInterface* stream =
								reinterpret_cast<webrtc::MediaStreamInterface*>(data);
					webrtc::VideoTrackVector tracks = stream->GetVideoTracks();
					webrtc::VideoTrackInterface* track = tracks[0];
					main_wnd_->StartRemoteRenderer(track);
						remote_renderer_.reset(new VideoRenderer(this, remote_video)
							rendered_track_->AddRenderer(this);
				case STREAM_REMOVED:
					webrtc::MediaStreamInterface* stream =
							reinterpret_cast<webrtc::MediaStreamInterface*>(data);
					stream->Release();
					
/******** InitSocketSignals()注册的回调函数 ********/
OnConnect
	socket->Send(onconnect_data_.c_str(), onconnect_data_.length())
OnHangingGetConnect
	sprintfn(buffer, sizeof(buffer),"GET /wait?peer_id=%i HTTP/1.0\r\n\r\n", my_id_);
	int len = static_cast<int>(strlen(buffer));
	int sent = socket->Send(buffer, len);
OnRead
	ReadIntoBuffer
	ParseServerResponse
	if (ParseEntry(control_data_.substr(pos, eol - pos), 
				&name, &id, &connected) && id != my_id_)
		callback_->OnPeerConnected(id, name);
	callback_->OnSignedIn();
	hanging_get_->Connect(server_address_)		
OnHangingGetRead
	ReadIntoBuffer()
		int bytes = socket->Recv(buffer, sizeof(buffer))
		GetHeaderValue()
	ParseServerResponse()
		GetHeaderValue
	if (my_id_ == static_cast<int>(peer_id))
		if (ParseEntry(notification_data_.substr(pos), &name, &id, &connected))
			callback_->OnPeerConnected(id, name);
				main_wnd_->SwitchToPeerList(client_->peers());
					AddListBoxItem()
						LRESULT index = ::SendMessageA(listbox, LB_ADDSTRING, 0,
											reinterpret_cast<LPARAM>(str.c_str()));
						::SendMessageA(listbox, LB_SETITEMDATA, index, item_data);
					LayoutPeerListUI(true);
						::GetClientRect(wnd_, &rc);
						::MoveWindow(listbox_, 0, 0, rc.right, rc.bottom, TRUE);
						::ShowWindow(listbox_, SW_SHOWNA);
					::SetFocus(listbox_);
	else
		OnMessageFromPeer
			callback_->OnMessageFromPeer(peer_id, message);
				InitializePeerConnection()
				reader.parse(message, jmessage)//接收为offer, answer, candidate信息 
				rtc::GetStringFromJsonObject(jmessage, kSessionDescriptionTypeName, &type);
				if (!type.empty()) //接收的是offer或者answer
					webrtc::SessionDescriptionInterface* session_description(
										webrtc::CreateSessionDescription(type, sdp, &error));
					peer_connection_->SetRemoteDescription(
							DummySetSessionDescriptionObserver::Create(), session_description);
					if (session_description->type() == webrtc::SessionDescriptionInterface::kOffer)
						peer_connection_->CreateAnswer(this, NULL);//接收到offer就创建answer
				else //接收则为candidate
					rtc::scoped_ptr<webrtc::IceCandidateInterface> candidate(
							webrtc::CreateIceCandidate(sdp_mid, sdp_mlineindex, sdp, &error));
					peer_connection_->AddIceCandidate(candidate.get())//添加候选信息
	
/******** main_wnd.cc ********/
WndProc()
	OnMessage()
		switch (msg)
			case WM_PAINT:
				OnPaint();//绘制窗体
				return true;
			case WM_COMMAND:
				if (button_ == reinterpret_cast<HWND>(lp)) 
					if (BN_CLICKED == HIWORD(wp)) //单击按钮
						OnDefaultAction(); 
				else if (listbox_ == reinterpret_cast<HWND>(lp)) 
					if (LBN_DBLCLK == HIWORD(wp)) //双击列表框
						OnDefaultAction();
				return true;
				
OnDefaultAction()
	if (ui_ == CONNECT_TO_SERVER) //点击的是连接到服务器按钮
		callback_->StartLogin(server, port)//如:http://192.168.1.123:8888/sign_in?id_name
			client_->Connect(server, port, GetPeerName())
				DoConnect()
					InitSocketSignals();//设置了些回调函数
						control_socket_->SignalCloseEvent.connect(this,&PeerConnectionClient::OnClose);
						hanging_get_->SignalCloseEvent.connect(this,&PeerConnectionClient::OnClose);
						control_socket_->SignalConnectEvent.connect(this,&PeerConnectionClient::OnConnect);
						hanging_get_->SignalConnectEvent.connect(this,&PeerConnectionClient::OnHangingGetConnect);
						control_socket_->SignalReadEvent.connect(this,&PeerConnectionClient::OnRead);
						hanging_get_->SignalReadEvent.connect(this,&PeerConnectionClient::OnHangingGetRead);
					sprintfn(buffer, sizeof(buffer),"GET /sign_in?%s HTTP/1.0\r\n\r\n", client_name_.c_str());
					ConnectControlSocket();
						control_socket_->Connect(server_address_);//control_socket_为异步套接字
	else if (ui_ == LIST_PEERS) //双击的是列表框
		callback_->ConnectToPeer(peer_id);
			InitializePeerConnection()
				peer_connection_factory_  = webrtc::CreatePeerConnectionFactory();
				CreatePeerConnection(DTLS_ON)
					webrtc::PeerConnectionInterface::RTCConfiguration config;
					webrtc::PeerConnectionInterface::IceServer server;
					server.uri = GetPeerConnectionString();//stun server和turn server地址
					config.servers.push_back(server);
					webrtc::FakeConstraints constraints;
					if (dtls)  //使用srtp协议进行传输
						constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,"true");
					else   //使用rtp协议进行传输
						constraints.AddOptional(webrtc::MediaConstraintsInterface::kEnableDtlsSrtp,"false");
					peer_connection_ = peer_connection_factory_->CreatePeerConnection(
											config, &constraints, NULL, NULL, this);
				AddStreams();
					rtc::scoped_refptr<webrtc::AudioTrackInterface> audio_track(
							peer_connection_factory_->CreateAudioTrack(
							kAudioLabel, peer_connection_factory_->CreateAudioSource(NULL)));
					rtc::scoped_refptr<webrtc::VideoTrackInterface> video_track(
							peer_connection_factory_->CreateVideoTrack(
							kVideoLabel,peer_connection_factory_->CreateVideoSource(
								OpenVideoCaptureDevice(),NULL)));  //打开摄像头
					main_wnd_->StartLocalRenderer(video_track);
						local_renderer_.reset(new VideoRenderer(handle(), 1, 1, local_video));
							rendered_track_->AddRenderer(this);
					rtc::scoped_refptr<webrtc::MediaStreamInterface> stream =
							peer_connection_factory_->CreateLocalMediaStream(kStreamLabel);
					stream->AddTrack(audio_track);
					stream->AddTrack(video_track);
					peer_connection_->AddStream(stream)
					main_wnd_->SwitchToStreamingUI();
						LayoutConnectUI(false);
							CalculateWindowSizeForText()
							::GetClientRect(wnd_, &rc)
							::MoveWindow()
							::SetWindowText()
							::ShowWindow()
						LayoutPeerListUI(false);
							::GetClientRect(wnd_, &rc);
							::MoveWindow(listbox_, 0, 0, rc.right, rc.bottom, TRUE);
							::ShowWindow(listbox_, SW_SHOWNA);
			peer_connection_->CreateOffer(this, NULL);
			
/*CreateSessionDescriptionObserver复写函数*/
OnSuccess()  //CreateOffer或者CreateAnswer成功
	peer_connection_->SetLocalDescription(DummySetSessionDescriptionObserver::Create(), desc);
	Json::StyledWriter writer;
	Json::Value jmessage;
	jmessage[kSessionDescriptionTypeName] = desc->type();  //kSessionDescriptionTypeName[] = "type";
	jmessage[kSessionDescriptionSdpName] = sdp;            //kSessionDescriptionSdpName[] = "sdp";
	SendMessage(writer.write(jmessage));
		main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, msg);
			::PostThreadMessage(ui_thread_id_, UI_THREAD_CALLBACK,
				static_cast<WPARAM>(msg_id), reinterpret_cast<LPARAM>(data));

/******** Conductor.cc ********/				
class Conductor
  : public webrtc::PeerConnectionObserver,
    public webrtc::CreateSessionDescriptionObserver,
    public PeerConnectionClientObserver,
    public MainWndCallback 

Conductor
	client_->RegisterObserver(this);
	main_wnd->RegisterObserver(this);
	
/*PeerConnectionObserver复写函数*/	
OnAddStream
	stream->AddRef();
	main_wnd_->QueueUIThreadCallback(NEW_STREAM_ADDED, stream);
		::PostThreadMessage(ui_thread_id_, UI_THREAD_CALLBACK,
				static_cast<WPARAM>(msg_id), reinterpret_cast<LPARAM>(data));
OnRemoveStream
	stream->AddRef();
	main_wnd_->QueueUIThreadCallback(STREAM_REMOVED,stream);
		::PostThreadMessage(ui_thread_id_, UI_THREAD_CALLBACK,
				static_cast<WPARAM>(msg_id), reinterpret_cast<LPARAM>(data));
OnIceCandidate
	jmessage[kCandidateSdpMidName] = candidate->sdp_mid();
	jmessage[kCandidateSdpMlineIndexName] = candidate->sdp_mline_index();
	jmessage[kCandidateSdpName] = sdp;
	SendMessage(writer.write(jmessage));
		main_wnd_->QueueUIThreadCallback(SEND_MESSAGE_TO_PEER, msg);
		
/*VideoRenderer复写函数*/	
SetSize
	image_.reset(new uint8_t[bmi_.bmiHeader.biSizeImage]);
RenderFrame
	const cricket::VideoFrame* frame = video_frame->GetCopyWithRotationApplied();
	SetSize(static_cast<int>(frame->GetWidth()), static_cast<int>(frame->GetHeight()));
	rame->ConvertToRgbBuffer(cricket::FOURCC_ARGB,
                              image_.get(),
                              bmi_.bmiHeader.biSizeImage,
                              bmi_.bmiHeader.biWidth *
                              bmi_.bmiHeader.biBitCount / 8);


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值