[+] 优化逻辑

This commit is contained in:
acgist
2023-03-05 12:28:28 +08:00
parent c49c3b19d8
commit 4ddf66f0d8
4 changed files with 19 additions and 36 deletions

View File

@@ -96,8 +96,10 @@
<!-- 终端 --> <!-- 终端 -->
<div class="clients"> <div class="clients">
<LocalClient ref="local" :client="taoyao" :taoyao="taoyao"></LocalClient> <!-- 本地终端 -->
<RemoteClient :ref="'remote-' + kv[0]" v-for="(kv, index) in remoteClients" :key="index" :client="kv[1]" :taoyao="taoyao"></RemoteClient> <LocalClient ref="local-client" v-if="this.taoyao" :client="taoyao" :taoyao="taoyao"></LocalClient>
<!-- 远程终端 -->
<RemoteClient :ref="'remote-client-' + kv[0]" v-for="(kv, index) in remoteClients" :key="index" :client="kv[1]" :taoyao="taoyao"></RemoteClient>
</div> </div>
</div> </div>
</template> </template>
@@ -140,7 +142,7 @@ export default {
async connectSignal() { async connectSignal() {
const me = this; const me = this;
me.taoyao = new Taoyao({ ...this.config }); me.taoyao = new Taoyao({ ...this.config });
await me.taoyao.connectSignal(me.callback, me.callbackMedia); await me.taoyao.connectSignal(me.callback);
me.signalVisible = false; me.signalVisible = false;
me.remoteClients = me.taoyao.remoteClients; me.remoteClients = me.taoyao.remoteClients;
// 全局绑定 // 全局绑定
@@ -197,26 +199,6 @@ export default {
} }
return false; return false;
}, },
/**
* 媒体回调
*
* @param {*} type 类型
* @param {*} track 媒体Track
* @param {*} consumer 消费者
*/
callbackMedia(type, track, consumer) {
const me = this;
return new Promise((resolve, reject) => {
if(type === 'local') {
me.$refs.local.media(track);
} else if(type === 'remote') {
me.$refs['remote-' + consumer.sourceId][0].media(track, consumer);
} else {
// 其他
}
resolve();
});
},
}, },
components: { components: {
LocalClient, LocalClient,

View File

@@ -93,6 +93,7 @@ export default {
mounted() { mounted() {
this.audio = this.$refs.audio; this.audio = this.$refs.audio;
this.video = this.$refs.video; this.video = this.$refs.video;
this.client.proxy = this;
}, },
props: { props: {
"client": { "client": {

View File

@@ -58,6 +58,7 @@ export default {
mounted() { mounted() {
this.audio = this.$refs.audio; this.audio = this.$refs.audio;
this.video = this.$refs.video; this.video = this.$refs.video;
this.client.proxy = this;
}, },
props: { props: {
"client": { "client": {

View File

@@ -234,6 +234,8 @@ class RemoteClient {
clientId; clientId;
// 音量 // 音量
volume = 0; volume = 0;
// 代理对象
proxy;
constructor({ constructor({
name, name,
@@ -263,8 +265,6 @@ class Taoyao extends RemoteClient {
roomId; roomId;
// 回调事件 // 回调事件
callback; callback;
// 媒体回调
callbackMedia;
// 请求回调 // 请求回调
callbackMapping = new Map(); callbackMapping = new Map();
// 音频媒体配置 // 音频媒体配置
@@ -348,15 +348,13 @@ class Taoyao extends RemoteClient {
/** /**
* 连接信令 * 连接信令
* *
* @param {*} callback 信令回调 * @param {*} callback 回调事件
* @param {*} callbackMedia 媒体回调
* *
* @returns * @returns
*/ */
async connectSignal(callback, callbackMedia) { async connectSignal(callback) {
const self = this; const self = this;
self.callback = callback; self.callback = callback;
self.callbackMedia = callbackMedia;
self.signalChannel = signalChannel; self.signalChannel = signalChannel;
signalChannel.taoyao = self; signalChannel.taoyao = self;
return self.signalChannel.connect( return self.signalChannel.connect(
@@ -636,9 +634,12 @@ class Taoyao extends RemoteClient {
// ); // );
self.push(message); self.push(message);
console.log("消费者", consumer); console.log("消费者", consumer);
const remoteClient = self.remoteClients.get(consumer.sourceId);
self.callbackMedia("remote", consumer.track, consumer); if(remoteClient) {
remoteClient.proxy.media(consumer.track, consumer);
} else {
console.warn("远程终端无效:", consumer);
}
// If audio-only mode is enabled, pause it. // If audio-only mode is enabled, pause it.
if (consumer.kind === "video" && !self.videoProduce) { if (consumer.kind === "video" && !self.videoProduce) {
// this.pauseConsumer(consumer); // this.pauseConsumer(consumer);
@@ -1108,7 +1109,7 @@ class Taoyao extends RemoteClient {
console.debug("enableWebcam() | calling getUserMedia()"); console.debug("enableWebcam() | calling getUserMedia()");
// TODO参数 // TODO参数
const stream = await navigator.mediaDevices.getUserMedia({ const stream = await navigator.mediaDevices.getUserMedia({
video: true, video: self.videoConfig,
}); });
track = stream.getVideoTracks()[0]; track = stream.getVideoTracks()[0];
// TODO验证修改API videoTrack.applyCapabilities // TODO验证修改API videoTrack.applyCapabilities
@@ -1130,9 +1131,7 @@ class Taoyao extends RemoteClient {
} else { } else {
// TODO异常 // TODO异常
} }
self.proxy.media(track);
self.callbackMedia("local", track);
let codec; let codec;
let encodings; let encodings;
const codecOptions = { const codecOptions = {