基于的Android视频监控系统实现的疑问
发布网友
发布时间:2022-04-20 22:35
我来回答
共1个回答
热心网友
时间:2023-05-23 06:55
做过一个类似的,不过得用浏览器, 当时是用的 html5的 websocket进行数据传输。这是内段 js代码
function initialize() {
console.log("Initializing; room=${roomKey}.");
card = document.getElementById("card");
localVideo = document.getElementById("localVideo");
miniVideo = document.getElementById("miniVideo");
remoteVideo = document.getElementById("remoteVideo");
resetStatus();
openChannel();
getUserMedia();
}
function getUserMedia() {
try {
navigator.webkitGetUserMedia({
'audio' : true,
'video' : true
}, onUserMediaSuccess, onUserMediaError);
console.log("Requested access to local media with new syntax.");
} catch (e) {
try {
navigator.webkitGetUserMedia("video,audio",
onUserMediaSuccess, onUserMediaError);
console
.log("Requested access to local media with old syntax.");
} catch (e) {
alert("webkitGetUserMedia() failed. Is the MediaStream flag enabled in about:flags?");
console.log("webkitGetUserMedia failed with exception: "
+ e.message);
}
}
}
function onUserMediaSuccess(stream) {
console.log("User has granted access to local media.");
var url = webkitURL.createObjectURL(stream);
localVideo.style.opacity = 1;
localVideo.src = url;
localStream = stream;
// Caller creates PeerConnection.
if (initiator)
maybeStart();
}
function maybeStart() {
if (!started && localStream && channelReady) {
setStatus("Connecting...");
console.log("Creating PeerConnection.");
createPeerConnection();
console.log("Adding local stream.");
pc.addStream(localStream);
started = true;
// Caller initiates offer to peer.
if (initiator)
doCall();
}
}
function doCall() {
console.log("Sending offer to peer.");
if (isRTCPeerConnection) {
pc.createOffer(setLocalAndSendMessage, null, mediaConstraints);
} else {
var offer = pc.createOffer(mediaConstraints);
pc.setLocalDescription(pc.SDP_OFFER, offer);
sendMessage({
type : 'offer',
sdp : offer.toSdp()
});
pc.startIce();
}
}
function setLocalAndSendMessage(sessionDescription) {
pc.setLocalDescription(sessionDescription);
sendMessage(sessionDescription);
}
function sendMessage(message) {
var msgString = JSON.stringify(message);
console.log('发出信息 : ' + msgString);
path = 'message?r=${roomKey}' + '&u=${user}';
var xhr = new XMLHttpRequest();
xhr.open('POST', path, true);
xhr.send(msgString);
}
页面加载完之后会调用initialize方法,initialize方法中调用了getUserMedia方法,这个方法是通过本地摄像头获取视频的方法,在成功获取视频之后发送连接请求,并在客户端建立连接管道,最后通过sendMessage向另外一个客户端发送连接的请求