WebRTC(Web Real-Time Communication)是一個(gè)開放的項(xiàng)目,旨在在網(wǎng)頁端提供實(shí)時(shí)的音頻、視頻和數(shù)據(jù)通信,不需要插件或其他附加軟件。它是由Google主導(dǎo),并在Mozilla、W3C和IETF等組織的支持下開發(fā)。WebRTC的目標(biāo)是使瀏覽器成為實(shí)時(shí)通信的強(qiáng)大平臺(tái),支持點(diǎn)對點(diǎn)(peer-to-peer)通信。
官方網(wǎng)址:https://webrtc.org/
WebRTC中文網(wǎng) https://webrtc.org.cn
WebRTC官網(wǎng) https://webrtc.org/
WebRTC范例 https://webrtc.github.io/samples/
WebRTC特點(diǎn)
實(shí)時(shí)通信: WebRTC專注于實(shí)時(shí)通信,包括音頻、視頻和其他數(shù)據(jù)。 WebRTC允許從設(shè)備中捕獲音頻和視頻流,并在對等連接中傳輸這些流。WebRTC已經(jīng)被現(xiàn)代瀏覽器(如Chrome、Firefox、Safari等)廣泛支持,使得開發(fā)者能夠在Web應(yīng)用中集成實(shí)時(shí)通信功能。
點(diǎn)對點(diǎn)通信: WebRTC支持點(diǎn)對點(diǎn)通信,即在兩個(gè)瀏覽器之間直接建立連接,而不需要通過中間服務(wù)器,不需要插件或其他附加軟件。
多媒體引擎: WebRTC包括一個(gè)多媒體引擎,用于處理音頻和視頻流,提供了豐富的API和協(xié)議。
NAT穿越和防火墻遍歷: WebRTC提供了一些機(jī)制,使得在NAT(Network Address Translation)和防火墻等網(wǎng)絡(luò)設(shè)備背后進(jìn)行通信更為容易。
WebRTC的網(wǎng)絡(luò)拓?fù)浣Y(jié)構(gòu)
Mesh 結(jié)構(gòu)
如果兩個(gè)Client端能夠順利建立P2P 的連接,則直接通過 P2P 互相交換數(shù)據(jù);如果由于網(wǎng)絡(luò)原因,無法打通,則利用 Turn Server 來中轉(zhuǎn)數(shù)據(jù)。圖中的TURN Server 是指支持 TURN 協(xié)議的服務(wù)器,它扮演著一種網(wǎng)絡(luò)中繼的角色,支持把一個(gè) Client 的數(shù)據(jù)包透明轉(zhuǎn)發(fā)到多個(gè)其他的 Client 客戶端。同理也可得出一對一的網(wǎng)絡(luò)模型。
這種完全使用 P2P 方式的網(wǎng)絡(luò)拓?fù)浣Y(jié)稱之為 Mesh 結(jié)構(gòu)。
這種結(jié)構(gòu)的優(yōu)點(diǎn)在于邏輯簡單,容易實(shí)現(xiàn),較為輕量的服務(wù)端,TURN 服務(wù)器比較簡單,一定比例的P2P 成功率可極大減輕服務(wù)端的壓力。
缺點(diǎn)在于每新增一個(gè)客戶端,所有的客戶端都需要新增一路數(shù)據(jù)上行,客戶端上行帶寬占用太大。因此,通話人數(shù)越多,效果越差。
SFU通話模型
SFU 的全稱是:Selective Forwarding Unit,它是一種通過服務(wù)器路由和轉(zhuǎn)發(fā) WebRTC 客戶端音視頻數(shù)據(jù)流的方法。
SFU 服務(wù)器將自己偽裝成了一個(gè)WebRTC 的 Peer 客戶端,WebRTC 的其他客戶端其實(shí)并不知道自己通過 P2P 連接過去的是一臺(tái)真實(shí)的客戶端還是一臺(tái)服務(wù)器,我們通常把這種連接稱之為 P2S,即Peer to Server。
SFU服務(wù)器和TURN服務(wù)器的不同在于:
TURN 服務(wù)器僅僅是提供的一種輔助的數(shù)據(jù)轉(zhuǎn)發(fā)通道,在P2P不通的時(shí)候進(jìn)行透明的數(shù)據(jù)轉(zhuǎn)發(fā)。而 SFU 是和客戶端有業(yè)務(wù)交互的,兩者是平等的關(guān)系,甚至是可以對WebRTC 客戶端的數(shù)據(jù)轉(zhuǎn)發(fā)的申請和控制。
MCU通話網(wǎng)絡(luò)模型
MCU 的全稱是:Multipoint Control Unit,又被稱為混合,是實(shí)現(xiàn)多方WebRTC交流的另一種策略。
它的主要原理是由 MCU Server 將各路客戶端上行的數(shù)據(jù)流合成為一路,再轉(zhuǎn)發(fā)給其他客戶端。這種模型相比于 SFU 降低了多人視頻通話場景下客戶端的下行帶寬壓力,但是由于合流需要轉(zhuǎn)碼操作,因此對服務(wù)器端壓力比較大,且下發(fā)給客戶端是固定的合流畫面,靈活性不好。文章來源:http://www.zghlxwxcb.cn/news/detail-820978.html
WebRTC 實(shí)現(xiàn)服務(wù)端和客戶端交互的代碼
在 Linux 環(huán)境下使用 WebRTC 實(shí)現(xiàn)服務(wù)端和客戶端交互的代碼一般較為復(fù)雜,需要使用信令服務(wù)器進(jìn)行 SDP 交換,并處理 ICE 候選等操作。下面例子是一個(gè)簡化的代碼,使用 WebSocket 作為信令服務(wù)器。文章來源地址http://www.zghlxwxcb.cn/news/detail-820978.html
創(chuàng)建信令服務(wù)器
#include <iostream>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/server.hpp>
#include <webrtc/api/peerconnectioninterface.h>
class WebSocketServer {
public:
WebSocketServer() {
server_.init_asio();
server_.set_message_handler(bind(&WebSocketServer::OnMessage, this, ::_1, ::_2));
server_.set_open_handler(bind(&WebSocketServer::OnOpen, this, ::_1));
server_.set_close_handler(bind(&WebSocketServer::OnClose, this, ::_1));
}
void Run(uint16_t port) {
server_.listen(port);
server_.start_accept();
server_.run();
}
private:
void OnMessage(websocketpp::connection_hdl hdl, websocketpp::server<websocketpp::config::asio>::message_ptr msg) {
std::string message = msg->get_payload();
// Handle incoming message (Signaling, SDP exchange, etc.)
// Example: Forward the message to the appropriate WebRTC peer connection
}
void OnOpen(websocketpp::connection_hdl hdl) {
// Handle new WebSocket connection
}
void OnClose(websocketpp::connection_hdl hdl) {
// Handle WebSocket connection close
}
websocketpp::server<websocketpp::config::asio> server_;
};
int main() {
WebSocketServer ws_server;
ws_server.Run(9000); // Run WebSocket server on port 9000
return 0;
}
客戶端
#include <iostream>
#include <websocketpp/config/asio_no_tls.hpp>
#include <websocketpp/client.hpp>
#include <webrtc/api/peerconnectioninterface.h>
class WebSocketClient {
public:
WebSocketClient() {
client_.init_asio();
client_.set_message_handler(bind(&WebSocketClient::OnMessage, this, ::_1, ::_2));
client_.set_open_handler(bind(&WebSocketClient::OnOpen, this, ::_1));
client_.set_close_handler(bind(&WebSocketClient::OnClose, this, ::_1));
}
void Connect(const std::string& uri) {
websocketpp::lib::error_code ec;
client_.get_alog().write(websocketpp::log::alevel::app, "Connecting to " + uri);
client_.connect(uri, ec);
if (ec) {
client_.get_alog().write(websocketpp::log::alevel::app, "Error connecting: " + ec.message());
}
}
void Send(const std::string& message) {
websocketpp::lib::error_code ec;
client_.send(hdl_, message, websocketpp::frame::opcode::text, ec);
if (ec) {
client_.get_alog().write(websocketpp::log::alevel::app, "Error sending message: " + ec.message());
}
}
private:
void OnMessage(websocketpp::connection_hdl hdl, websocketpp::client<websocketpp::config::asio>::message_ptr msg) {
std::string message = msg->get_payload();
// Handle incoming message (Signaling, SDP exchange, etc.)
// Example: Forward the message to the appropriate WebRTC peer connection
}
void OnOpen(websocketpp::connection_hdl hdl) {
client_.get_alog().write(websocketpp::log::alevel::app, "WebSocket connection opened");
hdl_ = hdl;
// Perform necessary setup, e.g., create WebRTC peer connection
}
void OnClose(websocketpp::connection_hdl hdl) {
client_.get_alog().write(websocketpp::log::alevel::app, "WebSocket connection closed");
// Perform necessary cleanup
}
websocketpp::client<websocketpp::config::asio> client_;
websocketpp::connection_hdl hdl_;
};
int main() {
WebSocketClient ws_client;
ws_client.Connect("ws://localhost:9000"); // Connect to WebSocket server on localhost:9000
ws_client.Send("Hello, WebSocket!"); // Send a message
// Continue with WebRTC logic, e.g., setting up peer connection, SDP exchange, media stream control, etc.
return 0;
}
到了這里,關(guān)于Web即時(shí)通信技術(shù)——WebRTC的文章就介紹完了。如果您還想了解更多內(nèi)容,請?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!