From e27b658ef98d9ee9c29bd0726b71517a3f38f82c Mon Sep 17 00:00:00 2001 From: winlin Date: Tue, 8 Feb 2022 11:54:04 +0800 Subject: [PATCH] Refine the error for WebRTC H5 publisher. v4.0.239 --- trunk/doc/CHANGELOG.md | 1 + trunk/research/players/js/srs.sdk.js | 10 +++++++++- trunk/research/players/rtc_publisher.html | 19 +++++++++++++++++++ trunk/src/core/srs_core_version4.hpp | 2 +- 4 files changed, 30 insertions(+), 2 deletions(-) diff --git a/trunk/doc/CHANGELOG.md b/trunk/doc/CHANGELOG.md index 23b1154256..1c74e51a97 100644 --- a/trunk/doc/CHANGELOG.md +++ b/trunk/doc/CHANGELOG.md @@ -8,6 +8,7 @@ The changelog for SRS. ## SRS 4.0 Changelog +* v4.0, 2022-02-08, Refine the error for WebRTC H5 publisher. v4.0.239 * v4.0, 2022-02-04, Push docker to docker, acr and tcr. v4.0.238 * v4.0, 2022-02-03, Merge [#2888](https://github.com/ossrs/srs/pull/2888): Fix bug when the value of http header is empty. (#2888). v4.0.237 * v4.0, 2022-01-30, Refine docker console, preview by players at the same server. v4.0.236 diff --git a/trunk/research/players/js/srs.sdk.js b/trunk/research/players/js/srs.sdk.js index 5cda7947cd..2edcaa4b8b 100644 --- a/trunk/research/players/js/srs.sdk.js +++ b/trunk/research/players/js/srs.sdk.js @@ -7,6 +7,14 @@ 'use strict'; +function SrsError(name, message) { + this.name = name; + this.message = message; + this.stack = (new Error()).stack; +} +SrsError.prototype = Object.create(Error.prototype); +SrsError.prototype.constructor = SrsError; + // Depends on adapter-7.4.0.min.js from https://github.com/webrtc/adapter // Async-awat-prmise based SRS RTC Publisher. function SrsRtcPublisherAsync() { @@ -48,7 +56,7 @@ function SrsRtcPublisherAsync() { self.pc.addTransceiver("video", {direction: "sendonly"}); if (!navigator.mediaDevices && window.location.protocol === 'http:' && window.location.hostname !== 'localhost') { - throw new Error(`Please use HTTPS or localhost to publish, read https://github.com/ossrs/srs/issues/2762#issuecomment-983147576`); + throw new SrsError('HttpsRequiredError', `Please use HTTPS or localhost to publish, read https://github.com/ossrs/srs/issues/2762#issuecomment-983147576`); } var stream = await navigator.mediaDevices.getUserMedia(self.constraints); diff --git a/trunk/research/players/rtc_publisher.html b/trunk/research/players/rtc_publisher.html index 4c1fe76ddf..e7b6984b71 100644 --- a/trunk/research/players/rtc_publisher.html +++ b/trunk/research/players/rtc_publisher.html @@ -99,6 +99,25 @@ $('#sessionid').html(session.sessionid); $('#simulator-drop').attr('href', session.simulator + '?drop=1&username=' + session.sessionid); }).catch(function (reason) { + // Throw by sdk. + if (reason instanceof SrsError) { + if (reason.name === 'HttpsRequiredError') { + alert(`WebRTC推流必须是HTTPS或者localhost:${reason.name} ${reason.message}`); + } else { + alert(`${reason.name} ${reason.message}`); + } + } + // See https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia#exceptions + if (reason instanceof DOMException) { + if (reason.name === 'NotFoundError') { + alert(`找不到麦克风和摄像头设备:getUserMedia ${reason.name} ${reason.message}`); + } else if (reason.name === 'NotAllowedError') { + alert(`你禁止了网页访问摄像头和麦克风:getUserMedia ${reason.name} ${reason.message}`); + } else if (['AbortError', 'NotAllowedError', 'NotFoundError', 'NotReadableError', 'OverconstrainedError', 'SecurityError', 'TypeError'].includes(reason.name)) { + alert(`getUserMedia ${reason.name} ${reason.message}`); + } + } + sdk.close(); $('#rtc_media_player').hide(); console.error(reason); diff --git a/trunk/src/core/srs_core_version4.hpp b/trunk/src/core/srs_core_version4.hpp index 64173e8105..fc349fb927 100644 --- a/trunk/src/core/srs_core_version4.hpp +++ b/trunk/src/core/srs_core_version4.hpp @@ -9,6 +9,6 @@ #define VERSION_MAJOR 4 #define VERSION_MINOR 0 -#define VERSION_REVISION 238 +#define VERSION_REVISION 239 #endif