Files
web-Iot/public/js/jswebrtc.min.js

2 lines
9.0 KiB
JavaScript
Raw Normal View History

var JSWebrtc = { Player: null, VideoElement: null, CreateVideoElements: function () { var elements = document.querySelectorAll(".jswebrtc"); for (var i = 0; i < elements.length; i++) { new JSWebrtc.VideoElement(elements[i]) } }, FillQuery: function (query_string, obj) { obj.user_query = {}; if (query_string.length == 0) return; if (query_string.indexOf("?") >= 0) query_string = query_string.split("?")[1]; var queries = query_string.split("&"); for (var i = 0; i < queries.length; i++) { var query = queries[i].split("="); obj[query[0]] = query[1]; obj.user_query[query[0]] = query[1] } if (obj.domain) obj.vhost = obj.domain }, ParseUrl: function (rtmp_url) { var a = document.createElement("a"); a.href = rtmp_url.replace("rtmp://", "http://").replace("webrtc://", "http://").replace("rtc://", "http://"); var vhost = a.hostname; var app = a.pathname.substr(1, a.pathname.lastIndexOf("/") - 1); var stream = a.pathname.substr(a.pathname.lastIndexOf("/") + 1); app = app.replace("...vhost...", "?vhost="); if (app.indexOf("?") >= 0) { var params = app.substr(app.indexOf("?")); app = app.substr(0, app.indexOf("?")); if (params.indexOf("vhost=") > 0) { vhost = params.substr(params.indexOf("vhost=") + "vhost=".length); if (vhost.indexOf("&") > 0) { vhost = vhost.substr(0, vhost.indexOf("&")) } } } if (a.hostname == vhost) { var re = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/; if (re.test(a.hostname)) vhost = "__defaultVhost__" } var schema = "rtmp"; if (rtmp_url.indexOf("://") > 0) schema = rtmp_url.substr(0, rtmp_url.indexOf("://")); var port = a.port; if (!port) { if (schema === "http") { port = 80 } else if (schema === "https") { port = 443 } else if (schema === "rtmp") { port = 1935 } else if (schema === "webrtc" || schema === "rtc") { port = 1985 } } var ret = { url: rtmp_url, schema: schema, server: a.hostname, port: port, vhost: vhost, app: app, stream: stream }; JSWebrtc.FillQuery(a.search, ret); return ret }, HttpPost: function (url, data) { return new Promise(function (resolve, reject) { var xhr = new XMLHttpRequest; xhr.onreadystatechange = function () { if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) { var respone = JSON.parse(xhr.responseText); xhr.onreadystatechange = new Function; xhr = null; resolve(respone) } }; xhr.open("POST", url, true); xhr.timeout = 5e3; xhr.responseType = "text"; xhr.setRequestHeader("Content-Type", "application/json"); xhr.send(data) }) } }; if (document.readyState === "complete") { JSWebrtc.CreateVideoElements() } else { document.addEventListener("DOMContentLoaded", JSWebrtc.CreateVideoElements) } JSWebrtc.VideoElement = function () { "use strict"; var VideoElement = function (element) { var url = element.dataset.url; if (!url) { throw "VideoElement has no `data-url` attribute" } var addStyles = function (element, styles) { for (var name in styles) { element.style[name] = styles[name] } }; this.container = element; addStyles(this.container, { display: "inline-block", position: "relative", minWidth: "80px", minHeight: "80px" }); this.video = document.createElement("video"); this.video.width = 960; this.video.height = 540; addStyles(this.video, { display: "block", width: "100%" }); this.container.appendChild(this.video); this.playButton = document.createElement("div"); this.playButton.innerHTML = VideoElement.PLAY_BUTTON; addStyles(this.playButton, { zIndex: 2, position: "absolute", top: "0", bottom: "0", left: "0", right: "0", maxWidth: "75px", maxHeight: "75px", margin: "auto", opacity: "0.7", cursor: "pointer" }); this.container.appendChild(this.playButton); var options = { video: this.video }; for (var option in element.dataset) { try { options[option] = JSON.parse(element.dataset[option]) } catch (err) { options[option] = element.dataset[option] } } this.player = new JSWebrtc.Player(url, options); element.playerInstance = this.player; if (options.poster && !options.autoplay) { options.decodeFirstFrame = false; this.poster = new Image; this.poster.src = options.poster; this.poster.addEventListener("load", this.posterLoaded); addStyles(this.poster, { display: "block", zIndex: 1, posi