mirror of
https://github.com/aler9/rtsp-simple-server
synced 2025-09-27 03:56:15 +08:00
webrtc: add authentication parameters to publisher.js and reader.js (#5026)
add user, pass and token configuration parameters.
This commit is contained in:
@@ -13,6 +13,9 @@
|
|||||||
* @typedef Conf
|
* @typedef Conf
|
||||||
* @type {object}
|
* @type {object}
|
||||||
* @property {string} url - absolute URL of the WHIP endpoint.
|
* @property {string} url - absolute URL of the WHIP endpoint.
|
||||||
|
* @property {string} user - username.
|
||||||
|
* @property {string} pass - password.
|
||||||
|
* @property {string} token - token.
|
||||||
* @property {MediaStream} stream - stream that contains outgoing tracks.
|
* @property {MediaStream} stream - stream that contains outgoing tracks.
|
||||||
* @property {string} videoCodec - outgoing video codec.
|
* @property {string} videoCodec - outgoing video codec.
|
||||||
* @property {number} videoBitrate - outgoing video bitrate.
|
* @property {number} videoBitrate - outgoing video bitrate.
|
||||||
@@ -276,9 +279,23 @@ class MediaMTXWebRTCPublisher {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#authHeader() {
|
||||||
|
if (this.conf.user !== undefined && this.conf.user !== '') {
|
||||||
|
const credentials = btoa(`${this.conf.user}:${this.conf.pass}`);
|
||||||
|
return {'Authorization': `Basic ${credentials}`};
|
||||||
|
}
|
||||||
|
if (this.conf.token !== undefined && this.conf.token !== '') {
|
||||||
|
return {'Authorization': `Bearer ${this.conf.token}`};
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
#requestICEServers() {
|
#requestICEServers() {
|
||||||
return fetch(this.conf.url, {
|
return fetch(this.conf.url, {
|
||||||
method: 'OPTIONS',
|
method: 'OPTIONS',
|
||||||
|
headers: {
|
||||||
|
...this.#authHeader(),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.then((res) => MediaMTXWebRTCPublisher.#linkToIceServers(res.headers.get('Link')));
|
.then((res) => MediaMTXWebRTCPublisher.#linkToIceServers(res.headers.get('Link')));
|
||||||
}
|
}
|
||||||
@@ -325,6 +342,7 @@ class MediaMTXWebRTCPublisher {
|
|||||||
return fetch(this.conf.url, {
|
return fetch(this.conf.url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {
|
headers: {
|
||||||
|
...this.#authHeader(),
|
||||||
'Content-Type': 'application/sdp',
|
'Content-Type': 'application/sdp',
|
||||||
},
|
},
|
||||||
body: offer,
|
body: offer,
|
||||||
|
@@ -14,6 +14,9 @@
|
|||||||
* @typedef Conf
|
* @typedef Conf
|
||||||
* @type {object}
|
* @type {object}
|
||||||
* @property {string} url - absolute URL of the WHEP endpoint.
|
* @property {string} url - absolute URL of the WHEP endpoint.
|
||||||
|
* @property {string} user - username.
|
||||||
|
* @property {string} pass - password.
|
||||||
|
* @property {string} token - token.
|
||||||
* @property {OnError} onError - called when there's an error.
|
* @property {OnError} onError - called when there's an error.
|
||||||
* @property {OnTrack} onTrack - called when there's a track available.
|
* @property {OnTrack} onTrack - called when there's a track available.
|
||||||
*/
|
*/
|
||||||
@@ -403,9 +406,23 @@ class MediaMTXWebRTCReader {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#authHeader() {
|
||||||
|
if (this.conf.user !== undefined && this.conf.user !== '') {
|
||||||
|
const credentials = btoa(`${this.conf.user}:${this.conf.pass}`);
|
||||||
|
return {'Authorization': `Basic ${credentials}`};
|
||||||
|
}
|
||||||
|
if (this.conf.token !== undefined && this.conf.token !== '') {
|
||||||
|
return {'Authorization': `Bearer ${this.conf.token}`};
|
||||||
|
}
|
||||||
|
return {};
|
||||||
|
}
|
||||||
|
|
||||||
#requestICEServers() {
|
#requestICEServers() {
|
||||||
return fetch(this.conf.url, {
|
return fetch(this.conf.url, {
|
||||||
method: 'OPTIONS',
|
method: 'OPTIONS',
|
||||||
|
headers: {
|
||||||
|
...this.#authHeader(),
|
||||||
|
},
|
||||||
})
|
})
|
||||||
.then((res) => MediaMTXWebRTCReader.#linkToIceServers(res.headers.get('Link')));
|
.then((res) => MediaMTXWebRTCReader.#linkToIceServers(res.headers.get('Link')));
|
||||||
}
|
}
|
||||||
@@ -446,7 +463,10 @@ class MediaMTXWebRTCReader {
|
|||||||
|
|
||||||
return fetch(this.conf.url, {
|
return fetch(this.conf.url, {
|
||||||
method: 'POST',
|
method: 'POST',
|
||||||
headers: {'Content-Type': 'application/sdp'},
|
headers: {
|
||||||
|
...this.#authHeader(),
|
||||||
|
'Content-Type': 'application/sdp',
|
||||||
|
},
|
||||||
body: offer,
|
body: offer,
|
||||||
})
|
})
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
|
Reference in New Issue
Block a user