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,61 @@ 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
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
+
51
111
/**
52
112
* Build an URL from {@link Type | type} and {@link Params | params}
53
113
* @param type Type of the connection wanted
@@ -56,11 +116,9 @@ export enum Type {
56
116
* @returns The URL of connection
57
117
*/
58
118
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 ) ;
60
120
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 ) ) ;
64
122
65
123
if ( url . pathname . length <= 1 ) {
66
124
// build ceeblue path!
@@ -72,7 +130,7 @@ export function buildURL(type: Type, params: Params, protocol: string = 'wss'):
72
130
url . pathname = '/webrtc/' + params . streamName ;
73
131
break ;
74
132
case Type . WRTS :
75
- url . pathname = '/wrts/' + params . streamName + ext ;
133
+ url . pathname = '/wrts/' + params . streamName ;
76
134
break ;
77
135
case Type . META :
78
136
url . pathname = '/json_' + params . streamName + '.js' ;
0 commit comments