@@ -78,35 +78,18 @@ async function createRemoteVideoStream(id) {
78
78
}
79
79
80
80
// Helper function to attach media stream to the video element
81
- function attachMediaStream ( video , stream , id ) {
81
+ async function attachMediaStream ( video , stream , id ) {
82
82
try {
83
+ const overlay = document . getElementById ( id + '-video-overlay' ) ;
83
84
// Use Safari-friendly attachment logic
84
85
video . srcObject = stream ;
85
- video . addEventListener ( "loadedmetadata" , async ( ) => {
86
- try {
87
- await video . play ( ) ; // Ensure video playback starts
88
- console . log ( "Remote video is playing for:" , id ) ;
89
- video . muted = false ; // Unmute after video starts
90
- } catch ( playError ) {
91
- console . error ( "Error playing the remote video:" , playError ) ;
92
-
93
- // Optional: Add a fallback for user interaction
94
- const overlay = document . getElementById ( id + '-video-overlay' ) ;
95
- overlay . innerHTML += "<p>Click to play</p>" ;
96
- overlay . style . cursor = "pointer" ;
97
-
98
- overlay . addEventListener ( "click" , async ( ) => {
99
- try {
100
- overlay . style . display = "none" ; // Hide the overlay
101
- await video . play ( ) ;
102
- console . log ( "Remote video resumed after user interaction:" , id ) ;
103
- video . muted = false ;
104
- } catch ( interactionError ) {
105
- console . error ( "Error playing the video after user interaction:" , interactionError ) ;
106
- }
107
- } ) ;
108
- }
109
- } ) ;
86
+ try {
87
+ await video . play ( ) ; // Ensure video playback starts
88
+ console . log ( "Remote video is playing for:" , id ) ;
89
+ video . muted = false ; // Unmute after video starts
90
+ } catch ( playError ) {
91
+ console . error ( "Error playing the remote video:" , playError ) ;
92
+ }
110
93
updateContainerClass ( ) ;
111
94
} catch ( error ) {
112
95
console . error ( "Error attaching media stream:" , error ) ;
@@ -116,6 +99,9 @@ function attachMediaStream(video, stream, id) {
116
99
function updateContainerClass ( ) {
117
100
const videoContainer = document . getElementById ( 'video-container' ) ;
118
101
const childrenCount = videoContainer . children . length ;
102
+ videoContainer . classList . remove ( 'one' ) ;
103
+ videoContainer . classList . remove ( 'two' ) ;
104
+
119
105
if ( childrenCount > 0 ) {
120
106
videoContainer . classList . remove ( 'hidden' ) ;
121
107
if ( window . innerWidth > 800 ) {
@@ -125,13 +111,26 @@ function updateContainerClass() {
125
111
} else {
126
112
videoContainer . classList . add ( 'hidden' ) ;
127
113
}
128
- if ( childrenCount === 4 ) {
129
- videoContainer . classList . add ( 'four' ) ;
114
+ if ( childrenCount === 1 ) {
115
+ videoContainer . classList . add ( 'one' ) ;
116
+ return ;
117
+ }
118
+ if ( childrenCount === 2 ) {
119
+ if ( window . innerWidth > 800 ) {
120
+ videoContainer . classList . add ( 'two' ) ;
121
+ } else {
122
+ videoContainer . classList . add ( 'one' ) ;
123
+ }
124
+ }
125
+ if ( childrenCount === 4 || childrenCount === 3 ) {
126
+ videoContainer . classList . add ( 'two' ) ;
130
127
return ;
131
128
}
132
129
}
133
130
134
131
132
+ window . addEventListener ( "resize" , updateContainerClass ) ;
133
+
135
134
function removeRemoteVideoStream ( id ) {
136
135
const containerDiv = document . getElementById ( id + '-container' ) ;
137
136
if ( containerDiv ) {
0 commit comments