webrtc对讲摄像头开发使用说明
- 摄像头开放了 9001 端口作为https接口
- 开放了 9000 端口作为http的接口
- 通过以下四个http的 post 接口调用,来完成webrtc协议播放
- 代码里的接口的基地址deviceUrl:局域网访问为https://[IP]/9001/ 或 http://[IP]/9000/
公网地址为:https://live.xstrive.com/[序列号]/webrtc/
- connect 接口
方法:POST
参数:无
返回:sessionID(在请求头的"Session-ID"里)
调用此接口会分配一个唯一的SessionID,后面的接口调用都要带上这个生成的SessionID
js调用示例:
function send_post_connect()
{
var xhr = new XMLHttpRequest();
const loc = deviceUrl;
console.log("send post connect: " + loc);
xhr.open("POST", '' + loc + 'connect', true);
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE &&
this.status === 200) {
const sessid = xhr.getResponseHeader("Session-ID");
console.log("connect: session: %s", sessid);
/* Save the session ID */
session_id = sessid;
pc.createOffer(offerOptions)
.then(function (desc) {
console.log("got local description: %s", desc.type);
pc.setLocalDescription(desc).then(() => {
const sd = pc.localDescription;
const json = JSON.stringify(sd);
retryCount = 0;
send_post_sdp(json);
},
function (error) {
console.log("setLocalDescription: %s",
error.toString());
});
})
.catch(function(error) {
console.warn("Failed to create session description: %s",
error.toString());
});
}
}
xhr.send();
}
- offersdp接口
方法:POST
参数:无
返回:sessionID(在请求头的"Session-ID"里) 和 sdp(在body中)
调用此接口,可以获取offer sdp,和一个sessionID
js调用示例:
function send_post_connect()
{
var xhr = new XMLHttpRequest();
const loc = self.location;
console.log("send post connect: " + loc);
xhr.open("POST", '' + loc + 'connect/offerer', true);
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE &&
(this.status === 200 || this.status === 201)) {
const sessid = xhr.getResponseHeader("Session-ID");
console.log("connect: new session: '%s' (%d %s)",
sessid, this.status, this.statusText);
/* Save the session ID */
session_id = sessid;
handle_offer(xhr.response);
}
}
}
xhr.send();
}
- sdp 接口
参数: 本地的sdp(offer)
返回: 远程的sdp(answer)
调用此接口把本地的sdp信息(offer)发送给摄像头,并接收摄像头返回的sdp值(answer)
js调用示例:
function send_post_sdp(descr)
{
var xhr = new XMLHttpRequest();
const loc = deviceUrl;
console.log("send post sdp: " + loc);
xhr.open("POST", '' + loc + 'sdp', true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Session-ID", session_id);
xhr.onreadystatechange = function() {
console.log("onreadystatechange:" + this.status);
if (this.readyState === XMLHttpRequest.DONE &&
this.status === 200) {
retryCount = maxRetryCount;
console.log("post sdp: 200 ok");
const descr = JSON.parse(xhr.response);
console.log("remote description: type='%s'", descr.type);
pc.setRemoteDescription(descr).then(() => {
console.log('set remote description -- success');
}, function (error) {
console.warn("setRemoteDescription: %s",
error.toString());
disconnect_call();
});
}
else if (this.readyState === XMLHttpRequest.DONE && this.status === 401){
retry_send_sdp(descr, xhr);
}
}
xhr.send(descr);
}
- candidate 接口
参数:iceServer返回的打洞信息
返回:无
调用此接口把iceServer返回的p2p连接信息发给摄像头,以方便ice框架建立p2p连接
js调用示例
function send_candidate(json)
{
var xhr = new XMLHttpRequest();
const loc = deviceUrl;
xhr.open("POST", '' + loc + 'candidate', true);
xhr.setRequestHeader("Content-Type", "application/json");
xhr.setRequestHeader("Session-ID", session_id);
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE &&
this.status !== 200) {
/* todo: print warning and close call */
}
}
xhr.send(json);
}
- disconnect 接口
参数:sessionId
返回:无
断开与服务器的连接并释放资源
js调用示例
function disconnect_call()
{
console.log("Disconnecting call");
if(localStream){
localStream.getTracks().forEach(track => track.stop());
}
if (pc) {
pc.close();
pc = null;
}
disconnectButton.disabled = true;
connectButton.disabled = false;
if (session_id) {
// send a message to the server
var xhr = new XMLHttpRequest();
xhr.open("POST", '' + deviceUrl + 'disconnect', true);
xhr.setRequestHeader("Session-ID", session_id);
xhr.send();
}
session_id = null;
}
js接口调用的示例代码可以用chrome打开网页https://[设备IP]:9001/ 右键选择查看源代码获取。
android调用的示例代码,可以联系我们获取
这份文档没解决你的问题?联系我们,技术工程师直接对接。