@@ -48,38 +48,59 @@ const runSocketIOSample = function() {
48
48
var subscribeForward = getParameterByName ( 'forward' ) === 'true' ?true :false ;
49
49
var isSelf = getParameterByName ( 'self' ) === 'false' ?false :true ;
50
50
conference = new Owt . Conference . ConferenceClient ( ) ;
51
- function renderVideo ( stream ) {
52
- let subscirptionForward = null ;
53
- function subscribeDifferentResolutionForward ( forward , resolution ) {
54
- subscirptionForward && subscirptionForward . stop ( ) ;
55
- subscirptionForward = null ;
51
+ function createResolutionButtons ( stream , subscribeResolutionCallback ) {
52
+ let $p = $ ( `#${ stream . id } resolutions` ) ;
53
+ if ( $p . length === 0 ) {
54
+ $p = $ ( `<div id=${ stream . id } resolutions> </div>` ) ;
55
+ $p . appendTo ( $ ( 'body' ) ) ;
56
+ }
57
+ // Resolutions from settings.
58
+ for ( const videoSetting of stream . settings . video ) {
59
+ const resolution = videoSetting . resolution ;
60
+ if ( resolution ) {
61
+ const button = $ ( '<button/>' , {
62
+ text : resolution . width + 'x' +
63
+ resolution . height ,
64
+ click : ( ) => {
65
+ subscribeResolutionCallback ( stream , resolution ) ;
66
+ }
67
+ } ) ;
68
+ button . prependTo ( $p ) ;
69
+ }
70
+ }
71
+ // Resolutions from extraCapabilities.
72
+ for ( const resolution of stream . extraCapabilities . video . resolutions ) {
73
+ const button = $ ( '<button/>' , {
74
+ text : resolution . width + 'x' +
75
+ resolution . height ,
76
+ click : ( ) => {
77
+ subscribeResolutionCallback ( stream , resolution ) ;
78
+ }
79
+ } ) ;
80
+ button . prependTo ( $p ) ;
81
+ } ;
82
+ return $p ;
83
+ }
84
+ function subscribeAndRenderVideo ( stream ) {
85
+ let subscirptionLocal = null ;
86
+ function subscribeDifferentResolution ( stream , resolution ) {
87
+ subscirptionLocal && subscirptionLocal . stop ( ) ;
88
+ subscirptionLocal = null ;
56
89
const videoOptions = { } ;
57
90
videoOptions . resolution = resolution ;
58
91
conference . subscribe ( stream , {
59
92
audio : true ,
60
93
video : videoOptions
61
94
} ) . then ( (
62
95
subscription ) => {
63
- subscirptionForward = subscription ;
96
+ subscirptionLocal = subscription ;
64
97
$ ( `#${ stream . id } ` ) . get ( 0 ) . srcObject = stream . mediaStream ;
65
98
} ) ;
66
99
}
67
- let $p = $ ( `<div id=${ stream . id } resolutions> </div>` )
68
- // TODO: Add resolutions from settings.
69
- for ( const resolution of stream . extraCapabilities . video . resolutions ) {
70
- const button = $ ( '<button/>' , {
71
- text : resolution . width + 'x' +
72
- resolution . height ,
73
- click : ( ) => {
74
- subscribeDifferentResolutionForward ( stream , resolution ) ;
75
- }
76
- } ) ;
77
- button . appendTo ( $p ) ;
78
- } ;
79
- $p . appendTo ( $ ( 'body' ) ) ;
100
+ let $p = createResolutionButtons ( stream , subscribeDifferentResolution ) ;
80
101
conference . subscribe ( stream )
81
102
. then ( ( subscription ) => {
82
- subscirptionForward = subscription ;
103
+ subscirptionLocal = subscription ;
83
104
let $video = $ ( `<video controls autoplay id=${ stream . id } style="display:block" >this browser does not supported video tag</video>` ) ;
84
105
$video . get ( 0 ) . srcObject = stream . mediaStream ;
85
106
$p . append ( $video ) ;
@@ -89,29 +110,20 @@ const runSocketIOSample = function() {
89
110
removeUi ( stream . id ) ;
90
111
$ ( `#${ stream . id } resolutions` ) . remove ( ) ;
91
112
} ) ;
113
+ stream . addEventListener ( 'updated' , ( ) => {
114
+ // Update resolution buttons
115
+ $p . children ( 'button' ) . remove ( ) ;
116
+ createResolutionButtons ( stream , subscribeDifferentResolution ) ;
117
+ } ) ;
92
118
}
93
119
function removeUi ( id ) {
94
120
$ ( `#${ id } ` ) . remove ( ) ;
95
121
}
96
- function subscribeDifferentResolution ( stream , resolution ) {
97
- subscriptionForMixedStream . stop ( ) ;
98
- subscriptionForMixedStream = null ;
99
- const videoOptions = { } ;
100
- videoOptions . resolution = resolution ;
101
- conference . subscribe ( stream , {
102
- audio : true ,
103
- video : videoOptions
104
- } ) . then ( (
105
- subscription ) => {
106
- subscriptionForMixedStream = subscription ;
107
- $ ( `#${ stream . id } ` ) . get ( 0 ) . srcObject = stream . mediaStream ;
108
- } ) ;
109
- }
110
122
111
123
conference . addEventListener ( 'streamadded' , ( event ) => {
112
124
console . log ( 'A new stream is added ' , event . stream . id ) ;
113
125
isSelf = isSelf ?isSelf :event . stream . id != publicationGlobal . id ;
114
- subscribeForward && isSelf && renderVideo ( event . stream ) ;
126
+ subscribeForward && isSelf && subscribeAndRenderVideo ( event . stream ) ;
115
127
mixStream ( myRoom , event . stream . id , 'common' ) ;
116
128
event . stream . addEventListener ( 'ended' , ( ) => {
117
129
console . log ( event . stream . id + ' is ended.' ) ;
@@ -120,10 +132,7 @@ const runSocketIOSample = function() {
120
132
121
133
122
134
window . onload = function ( ) {
123
- var myResolution = getParameterByName ( 'resolution' ) || {
124
- width : 1280 ,
125
- height : 720
126
- } ;
135
+ var simulcast = getParameterByName ( 'simulcast' ) || false ;
127
136
var shareScreen = getParameterByName ( 'screen' ) || false ;
128
137
myRoom = getParameterByName ( 'room' ) ;
129
138
var isHttps = ( location . protocol === 'https:' ) ;
@@ -138,21 +147,34 @@ const runSocketIOSample = function() {
138
147
startStreamingIn ( myRoom , mediaUrl ) ;
139
148
}
140
149
if ( isPublish !== 'false' ) {
141
- const audioConstraintsForMic = new Owt . Base . AudioTrackConstraints ( Owt . Base . AudioSourceInfo . MIC ) ;
142
- const videoConstraintsForCamera = new Owt . Base . VideoTrackConstraints ( Owt . Base . VideoSourceInfo . CAMERA ) ;
150
+ // audioConstraintsForMic
151
+ let audioConstraints = new Owt . Base . AudioTrackConstraints ( Owt . Base . AudioSourceInfo . MIC ) ;
152
+ // videoConstraintsForCamera
153
+ let videoConstraints = new Owt . Base . VideoTrackConstraints ( Owt . Base . VideoSourceInfo . CAMERA ) ;
154
+ if ( shareScreen ) {
155
+ // audioConstraintsForScreen
156
+ audioConstraints = new Owt . Base . AudioTrackConstraints ( Owt . Base . AudioSourceInfo . SCREENCAST ) ;
157
+ // videoConstraintsForScreen
158
+ videoConstraints = new Owt . Base . VideoTrackConstraints ( Owt . Base . VideoSourceInfo . SCREENCAST ) ;
159
+ }
160
+
143
161
let mediaStream ;
144
162
Owt . Base . MediaStreamFactory . createMediaStream ( new Owt . Base . StreamConstraints (
145
- audioConstraintsForMic , videoConstraintsForCamera ) ) . then ( stream => {
163
+ audioConstraints , videoConstraints ) ) . then ( stream => {
164
+ let publishOption ;
165
+ if ( simulcast ) {
166
+ publishOption = { video :[
167
+ { rid : 'q' , active : true /*, scaleResolutionDownBy: 4.0*/ } ,
168
+ { rid : 'h' , active : true /*, scaleResolutionDownBy: 2.0*/ } ,
169
+ { rid : 'f' , active : true }
170
+ ] } ;
171
+ }
146
172
mediaStream = stream ;
147
173
localStream = new Owt . Base . LocalStream (
148
174
mediaStream , new Owt . Base . StreamSourceInfo (
149
175
'mic' , 'camera' ) ) ;
150
176
$ ( '.local video' ) . get ( 0 ) . srcObject = stream ;
151
- conference . publish ( localStream , { video :[
152
- { rid : 'q' , active : true , scaleResolutionDownBy : 4.0 } ,
153
- { rid : 'h' , active : true , scaleResolutionDownBy : 2.0 } ,
154
- { rid : 'f' , active : true }
155
- ] } ) . then ( publication => {
177
+ conference . publish ( localStream , publishOption ) . then ( publication => {
156
178
publicationGlobal = publication ;
157
179
mixStream ( myRoom , publication . id , 'common' )
158
180
publication . addEventListener ( 'error' , ( err ) => {
@@ -169,32 +191,10 @@ const runSocketIOSample = function() {
169
191
if ( ! subscribeForward ) {
170
192
if ( stream . source . audio === 'mixed' || stream . source . video ===
171
193
'mixed' ) {
172
- conference . subscribe ( stream , {
173
- audio : { codecs :[ { name :'opus' } ] } ,
174
- video : true
175
- } ) . then ( ( subscription ) => {
176
- subscriptionForMixedStream = subscription ;
177
- let $video = $ ( `<video controls autoplay id=${ stream . id } style='display:block'>this browser does not supported video tag</video>` ) ;
178
- $video . get ( 0 ) . srcObject = stream . mediaStream ;
179
- $ ( 'body' ) . append ( $video ) ;
180
- subscription . addEventListener ( 'error' , ( err ) => {
181
- console . log ( 'Subscription error: ' + err . error . message ) ;
182
- } )
183
- } ) ;
184
- // TODO: Add resolutions from settings.
185
- for ( const resolution of stream . extraCapabilities . video . resolutions ) {
186
- const button = $ ( '<button/>' , {
187
- text : resolution . width + 'x' +
188
- resolution . height ,
189
- click : ( ) => {
190
- subscribeDifferentResolution ( stream , resolution ) ;
191
- }
192
- } ) ;
193
- button . appendTo ( $ ( 'body' ) ) ;
194
- } ;
194
+ subscribeAndRenderVideo ( stream ) ;
195
195
}
196
- } else if ( stream . source . audio !== 'mixed' ) {
197
- subscribeForward && renderVideo ( stream ) ;
196
+ } else if ( stream . source . audio !== 'mixed' ) {
197
+ subscribeAndRenderVideo ( stream ) ;
198
198
}
199
199
}
200
200
console . log ( 'Streams in conference:' , streams . length ) ;
0 commit comments