3
3
* This file is part of https://github.com/CeeblueTV/web-utils which is released under GNU Affero General Public License.
4
4
* See file LICENSE or go to https://spdx.org/licenses/AGPL-3.0-or-later.html for full license details.
5
5
*/
6
- import { NetAddress } from './NetAddress' ;
7
6
import * as Util from './Util' ;
7
+ import { NetAddress } from './NetAddress' ;
8
8
9
9
/**
10
10
* Parameters of connections
@@ -27,6 +27,11 @@ export type Params = {
27
27
* iceServer to use while connecting to a WebRTC stream
28
28
*/
29
29
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 ;
30
35
/**
31
36
* Optional query to add into the generated url of connection
32
37
*/
@@ -48,6 +53,56 @@ export enum Type {
48
53
* Some connection utility functions
49
54
*/
50
55
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 in removing the possible '.' prefix
65
+ if ( params . mediaExt ) {
66
+ params . mediaExt = Util . trimStart ( params . mediaExt , '.' ) ;
67
+ }
68
+ // Compute appropriate mediaExt out parameter
69
+ switch ( type ) {
70
+ case Type . HESP :
71
+ params . mediaExt = 'mp4' ;
72
+ break ;
73
+ case Type . WEBRTC :
74
+ params . mediaExt = 'rtp' ;
75
+ break ;
76
+ case Type . WRTS : {
77
+ try {
78
+ const url = new URL ( params . endPoint ) ;
79
+ const ext = Util . getExtension ( Util . getFile ( url . pathname ) ) ;
80
+ // set extension just if not json, json means a manifest file endPoint
81
+ if ( ext && ext !== 'json' ) {
82
+ params . mediaExt = ext ;
83
+ }
84
+ } catch ( _ ) {
85
+ // not an URL, it's only a host => keep mediaExt unchanged to build the URL
86
+ }
87
+ if ( ! params . mediaExt ) {
88
+ // set to its default rts value => always set for WRTS!
89
+ params . mediaExt = 'rts' ;
90
+ }
91
+ break ;
92
+ }
93
+ case Type . META :
94
+ params . mediaExt = 'js' ;
95
+ break ;
96
+ case Type . DATA :
97
+ params . mediaExt = 'json' ;
98
+ break ;
99
+ default :
100
+ params . mediaExt = '' ; // set always a value to know that the parameters have been fixed
101
+ console . warn ( 'Unknown params type ' + type ) ;
102
+ break ;
103
+ }
104
+ }
105
+
51
106
/**
52
107
* Build an URL from {@link Type | type} and {@link Params | params}
53
108
* @param type Type of the connection wanted
@@ -56,11 +111,9 @@ export enum Type {
56
111
* @returns The URL of connection
57
112
*/
58
113
export function buildURL ( type : Type , params : Params , protocol : string = 'wss' ) : URL {
59
- const url = new URL ( NetAddress . fixProtocol ( protocol , params . endPoint ) ) ;
114
+ defineMediaExt ( type , params ) ;
60
115
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 ) ;
116
+ const url = new URL ( NetAddress . fixProtocol ( protocol , params . endPoint ) ) ;
64
117
65
118
if ( url . pathname . length <= 1 ) {
66
119
// build ceeblue path!
@@ -72,7 +125,7 @@ export function buildURL(type: Type, params: Params, protocol: string = 'wss'):
72
125
url . pathname = '/webrtc/' + params . streamName ;
73
126
break ;
74
127
case Type . WRTS :
75
- url . pathname = '/wrts/' + params . streamName + ext ;
128
+ url . pathname = '/wrts/' + params . streamName ;
76
129
break ;
77
130
case Type . META :
78
131
url . pathname = '/json_' + params . streamName + '.js' ;
0 commit comments