Skip to content

Commit 3b269a0

Browse files
committed
feat(Connect): add a mediaExt optional field
1 parent a83b6a1 commit 3b269a0

File tree

1 file changed

+64
-6
lines changed

1 file changed

+64
-6
lines changed

src/Connect.ts

+64-6
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
* This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
44
* See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
55
*/
6-
import { NetAddress } from './NetAddress';
76
import * as Util from './Util';
7+
import { NetAddress } from './NetAddress';
88

99
/**
1010
* Parameters of connections
@@ -27,6 +27,11 @@ export type Params = {
2727
* iceServer to use while connecting to a WebRTC stream
2828
*/
2929
iceServer?: RTCIceServer; // Authentication value
30+
/**
31+
* Optional media extension (mp4, flv, ts, rts), usefull for protocol like WebRTS which supports different container type.
32+
* When not set, it's also an output parameter to indicate what is the media type selected
33+
*/
34+
mediaExt?: string;
3035
/**
3136
* Optional query to add into the generated url of connection
3237
*/
@@ -48,6 +53,61 @@ export enum Type {
4853
* Some connection utility functions
4954
*/
5055

56+
/**
57+
* Defines the {@link Params.mediaExt} based on the type of parameters and its endpoint.
58+
* This method always assigns a value to params.mediaExt, defaulting to an empty string if indeterminable,
59+
* allowing detection of whether the function has been applied to the parameters.
60+
* @param type The type of parameters to define.
61+
* @param params The parameters for which the media extension is to be defined
62+
*/
63+
export function defineMediaExt(type: Type, params: Params) {
64+
// Fix mediaExt
65+
/// remove all the possible '.' prefix
66+
if (params.mediaExt) {
67+
let i = 0;
68+
while (params.mediaExt.charAt(i) === '.') {
69+
++i;
70+
}
71+
params.mediaExt = params.mediaExt.substring(i);
72+
}
73+
/// Set mediaExt out parameter if not set!
74+
switch (type) {
75+
case Type.HESP:
76+
params.mediaExt = 'mp4';
77+
break;
78+
case Type.WEBRTC:
79+
params.mediaExt = 'rtp';
80+
break;
81+
case Type.WRTS: {
82+
try {
83+
const url = new URL(params.endPoint);
84+
const ext = Util.getExtension(Util.getFile(url.pathname));
85+
// set extension just if not json, json means a manifest file endPoint
86+
if (ext && ext !== 'json') {
87+
params.mediaExt = ext;
88+
}
89+
} catch (_) {
90+
// not an URL, it's only a host => keep mediaExt unchanged to build the URL
91+
}
92+
if (!params.mediaExt) {
93+
// set to its default rts value => always set for WRTS!
94+
params.mediaExt = 'rts';
95+
}
96+
break;
97+
}
98+
case Type.META:
99+
params.mediaExt = 'js';
100+
break;
101+
case Type.DATA:
102+
params.mediaExt = 'json';
103+
break;
104+
default:
105+
params.mediaExt = ''; // set always a value to know that the parameters have been fixed
106+
console.warn('Unknown params type ' + type);
107+
break;
108+
}
109+
}
110+
51111
/**
52112
* Build an URL from {@link Type | type} and {@link Params | params}
53113
* @param type Type of the connection wanted
@@ -56,11 +116,9 @@ export enum Type {
56116
* @returns The URL of connection
57117
*/
58118
export function buildURL(type: Type, params: Params, protocol: string = 'wss'): URL {
59-
const url = new URL(NetAddress.fixProtocol(protocol, params.endPoint));
119+
defineMediaExt(type, params);
60120

61-
// Remove possible extension of streamName put sometimes to decide format when multiple choices are possible like with WRTS
62-
const ext = Util.parseExtension(params.streamName);
63-
params.streamName = params.streamName.substring(0, params.streamName.length - ext.length);
121+
const url = new URL(NetAddress.fixProtocol(protocol, params.endPoint));
64122

65123
if (url.pathname.length <= 1) {
66124
// build ceeblue path!
@@ -72,7 +130,7 @@ export function buildURL(type: Type, params: Params, protocol: string = 'wss'):
72130
url.pathname = '/webrtc/' + params.streamName;
73131
break;
74132
case Type.WRTS:
75-
url.pathname = '/wrts/' + params.streamName + ext;
133+
url.pathname = '/wrts/' + params.streamName;
76134
break;
77135
case Type.META:
78136
url.pathname = '/json_' + params.streamName + '.js';

0 commit comments

Comments
 (0)