...
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToOneParticipantView = function () { ... return { ... dispose: function () { for (const player of videoPlayers.values()) { player.dispose(); } videoPlayers.clear(); for (const element of audioElements.values()) { element.remove(); } audioElements.clear(); }, ... } } |
11. Создание объекта отображения участника в комнате с несколькими участниками
createOneToManyParticipantView() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
const participantDiv = createContainer(null);
const audioDisplay = createContainer(participantDiv);
const participantNicknameDisplay = createInfoDisplay(participantDiv, "Name: ")
const audioElements = new Map();
const player = createVideoPlayer(participantDiv);
return {
rootDiv: participantDiv,
currentTrack: null,
dispose: function () {
...
},
addVideoTrack: function (track) {
...
},
removeVideoTrack: function (track) {
...
},
addVideoSource: function (remoteVideoTrack, track, onResize, muteHandler) {
...
},
removeVideoSource: function (track) {
...
},
showVideoTrack: function (track) {
...
},
addAudioTrack: function (track, audioTrack, show) {
...
},
removeAudioTrack: function (track) {
...
},
setNickname: function (userId, nickname) {
...
},
updateQuality: function (track, qualityName, available) {
...
},
addQuality: function (track, qualityName, available, onQualityPick) {
...
},
clearQualityState: function (track) {
...
},
pickQuality: function (track, qualityName) {
...
}
}
} |
11.1. Добавление видео дорожки
addVideoTrack() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
addVideoTrack: function (track, requestVideoTrack) {
player.addVideoTrack(track, async () => {
return requestVideoTrack();
});
},
...
}
} |
11.2. Удаление видео дорожки
removeVideoTrack() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
removeVideoTrack: function (track) {
player.removeVideoTrack(track);
},
...
}
} |
11.3. Добавление источника к видео элементу
addVideoSource() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
addVideoSource: function (remoteVideoTrack, track, onResize, muteHandler) {
this.currentTrack = track;
player.setVideoSource(remoteVideoTrack, onResize, muteHandler);
},
...
}
} |
11.4. Удаление источника из видео элемента
removeVideoSource() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
removeVideoSource: function (track) {
if (this.currentTrack && this.currentTrack.mid === track.mid) {
player.removeVideoSource();
}
},
...
}
} |
11.5. Показать видео дорожку
showVideoTrack() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
showVideoTrack: function (track) {
player.showVideoTrack(track);
},
...
}
} |
11.6. Добавление аудио дорожки
addAudioTrack() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
addAudioTrack: function (track, audioTrack, show) {
const stream = new MediaStream();
stream.addTrack(audioTrack);
const audioElement = document.createElement("audio");
if (!show) {
hideItem(audioElement);
}
audioElement.controls = "controls";
audioElement.muted = true;
audioElement.autoplay = true;
audioElement.onloadedmetadata = function (e) {
audioElement.play().then(function () {
if (Browser().isSafariWebRTC() && Browser().isiOS()) {
console.warn("Audio track should be manually unmuted in iOS Safari");
} else {
audioElement.muted = false;
}
});
};
audioElements.set(track.mid, audioElement);
audioDisplay.appendChild(audioElement);
audioElement.srcObject = stream;
},
...
}
} |
11.7. Удаление аудио дорожки
removeAudioTrack() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
removeAudioTrack: function (track) {
const audioElement = audioElements.get(track.mid);
if (audioElement) {
audioElement.remove();
audioElements.delete(track.mid);
}
},
...
}
} |
11.8. Отображение имени участника
setNickname() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
setNickname: function (userId, nickname) {
const additionalUserId = userId ? "#" + getShortUserId(userId) : "";
participantNicknameDisplay.innerText = "Name: " + nickname + additionalUserId;
},
...
}
} |
11.9. Обновление информации о качестве
updateQuality() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
updateQuality: function (track, qualityName, available) {
player.updateQuality(qualityName, available);
},
...
}
} |
11.10. Добавление информации о качестве
addQuality() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
addQuality: function (track, qualityName, available, onQualityPick) {
player.addQuality(qualityName, available, onQualityPick);
},
...
}
} |
11.11. Выбор качества
pickQuality() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
pickQuality: function (track, qualityName) {
player.pickQuality(qualityName);
}
...
}
} |
11.12. Очистка отображаемого списка качеств
clearQualityState() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
clearQualityState: function (track) {
player.clearQualityState();
},
...
}
} |
11.13. Завершение отображения
dispose() code
Code Block | ||||
---|---|---|---|---|
| ||||
const createOneToManyParticipantView = function () {
...
return {
...
dispose: function () {
player.dispose();
for (const element of audioElements.values()) {
element.remove();
}
audioElements.clear();
},
...
}
} |