![](/img/trans.png)
[英]Java web application that can stream the content of an arbitrary file to the browser (live tail)
[英]Display RTSP live stream in the web browser using java and Web Socket
我想編寫一個Java程序來解碼來自Ip攝像機的RTSP實時流,並將其通過Web套接字發送到HTML5 Web客戶端。 當我的電腦上有一個簡單的mp4文件時,我可以這樣做。 我的代碼如下所示:
爪哇
@ServerEndpoint("/echo")
public class EchoEndPoint {
@OnMessage
public byte[] echo(String message) {
File file = new File("/home/maher/devTools/video/testVideo.mp4");
byte[] data = new byte[(int) file.length()];
DataInputStream stream = null;
try {
stream = new DataInputStream(new FileInputStream(file));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
if (stream != null) {
try {
stream.readFully(data);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
stream.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return data;
}
}
的HTML
<!DOCTYPE html>
<html>
<head>
<title>Test streaming over WebSockets</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<script language="javascript" type="text/javascript">
var wsUri = "ws://localhost:7070/serverWs/echo";
function init() {
websocket = new WebSocket(wsUri);
websocket.onmessage = function (evt) {
readFile(evt.data);
};
}
function readFile(fileData) {
var video = document.getElementById('area');
video.src = window.URL.createObjectURL(fileData);
}
function createObjectURL(file) {
if (window.webkitURL) {
return window.webkitURL.createObjectURL(file);
} else if (window.URL && window.URL.createObjectURL) {
return window.URL.createObjectURL(file);
} else {
return null;
}
}
function startVideo() {
var message = "startVideo";
websocket.send(message);
}
window.addEventListener("load", init, false);
</script>
</head>
<body>
<h2 style="text-align: center;">Client WebSocket Echo</h2>
<div style="text-align: center;">
<input onclick="startVideo()" value="Start video" type="button">
</div>
<div>
<video id='area' controls="controls" autoplay></video>
</div>
</body>
</html>
但是,當涉及實時RTSP流URL時,我找不到一種方法來解碼流並通過WS發送,然后在我的網頁中顯示該流。
除非您真的真的想出於教育目的或為了滿足其他一些要求而這樣做,否則您可能會發現更容易在現有的流服務器上構建服務。
這是因為視頻流是一個非常專業的領域,並且可能需要轉換許多編解碼器,容器,流協議等以支持不同的終端設備,瀏覽器等。
此外,如果要提供良好的用戶體驗,則可能需要以多種比特率提供實時流,以便客戶端可以根據其當前的網絡狀況,屏幕大小等在它們之間進行切換。
大多數流媒體服務器將支持將視頻轉碼為不同格式,並支持多種比特率以進行自適應比特率流傳輸。
GStreamer( https://gstreamer.freedesktop.org )是一個開源的流媒體,您可能會發現它可以滿足您的需求-即使不滿足,它也是一個很好的參考實現。
您可以在此處查看有關其RTSP支持的信息: https : //gstreamer.freedesktop.org/documentation/rtp.html
我無法回復第一條評論,但如果需要的話,答案將無法滿足實時(相對於直播)的要求。 評論建議將使流延遲增加10-20 +秒。
從服務器端到WebSocket,到客戶端解決方案/選項/示例,有幾個RTSP:
https://github.com/Streamedian/ https://www.npmjs.com/package/node-rtsp-stream https://medium.com/@chpmrc/how-to-stream-rtsp-on-the-網絡使用網絡插座和帆布D821B8F7171E
Web / HTML5正在遠離Flash(及類似的插件/ ActiveX),后者通過更直接的RTP / RTSP / RTMP(基於UDP)流提供更好的延遲。 隨着時間的推移,WebRTC可能是替代解決方案。 HLS /自適應比特率等將有10-20 +秒的延遲延遲。
實時流媒體(音樂會/活動)一直在推動流媒體開發(Azure / Facebook / Wowza / AWS提供者),通常20-40秒的延遲不是問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.