[*] 音量监听优化

This commit is contained in:
acgist
2023-03-05 13:25:28 +08:00
parent 7f4378626b
commit 38b7db4dfe
5 changed files with 73 additions and 42 deletions

View File

@@ -265,39 +265,40 @@ class Room {
this.handleAudioLevelObserver(); this.handleAudioLevelObserver();
this.handleActiveSpeakerObserver(); this.handleActiveSpeakerObserver();
} }
/** /**
* 音量监控 * 音量监控
*/ */
handleAudioLevelObserver() { handleAudioLevelObserver() {
const self = this; const me = this;
self.audioLevelObserver.on("volumes", (volumes) => { // 静音
me.audioLevelObserver.on("silence", () => {
signalChannel.push(
protocol.buildMessage("media::audio::volume", {
roomId: me.roomId,
})
);
});
// 音量
me.audioLevelObserver.on("volumes", (volumes) => {
const volumeArray = [];
for (const value of volumes) { for (const value of volumes) {
const { producer, volume } = value; const { producer, volume } = value;
signalChannel.push( volumeArray.push({ volume: volume, clientId: producer.clientId });
protocol.buildMessage("media::audio::active::speaker", {
volume: volume,
roomId: self.roomId,
clientId: producer.clientId,
})
);
} }
});
self.audioLevelObserver.on("silence", () => {
signalChannel.push( signalChannel.push(
protocol.buildMessage("media::audio::active::speaker", { protocol.buildMessage("media::audio::volume", {
roomId: self.roomId, roomId: me.roomId,
volumes: volumeArray
}) })
); );
}); });
} }
/** /**
* 采样监控 * 采样监控
*/ */
handleActiveSpeakerObserver() { handleActiveSpeakerObserver() {
const self = this; const me = this;
self.activeSpeakerObserver.on("dominantspeaker", (dominantSpeaker) => { me.activeSpeakerObserver.on("dominantspeaker", (dominantSpeaker) => {
console.debug( console.debug(
"dominantspeaker", "dominantspeaker",
dominantSpeaker.producer.id, dominantSpeaker.producer.id,
@@ -305,7 +306,6 @@ class Room {
); );
}); });
} }
/** /**
* 使用情况 * 使用情况
*/ */

View File

@@ -245,6 +245,15 @@ class RemoteClient {
this.clientId = clientId; this.clientId = clientId;
} }
/**
* 设置音量
*
* @param {*} volume 音量
*/
setVolume(volume) {
this.volume = ((volume + 127) / 127 * 100) + "%";
}
} }
/** /**
@@ -482,8 +491,8 @@ class Taoyao extends RemoteClient {
case "client::shutdown": case "client::shutdown":
me.defaultClientShutdown(message); me.defaultClientShutdown(message);
break; break;
case "media::audio::active::speaker": case "media::audio::volume":
me.defaultMediaAudioActiveSpeaker(message); me.defaultMediaAudioVolume(message);
break; break;
case "room::client::list": case "room::client::list":
me.defaultRoomClientList(message); me.defaultRoomClientList(message);
@@ -546,24 +555,31 @@ class Taoyao extends RemoteClient {
window.close(); window.close();
} }
/** /**
* 当前讲话终端信令 * 终端音量信令
* *
* @param {*} message 消息 * @param {*} message 消息
*/ */
defaultMediaAudioActiveSpeaker(message) { defaultMediaAudioVolume(message) {
const me = this; const me = this;
const { volume, clientId } = message.body; const { roomId, volumes } = message.body;
if(!clientId) { // 静音
if(!volumes || !volumes.length) {
me.volume = 0; me.volume = 0;
me.remoteClients.forEach(v => v.volume = 0); me.remoteClients.forEach(v => v.volume = 0);
} if(me.clientId === clientId) { return;
me.volume = ((volume + 127) / 127 * 100) + "%";
} else {
const remoteClient = me.remoteClients.get(clientId);
if(remoteClient) {
remoteClient.volume = ((volume + 127) / 127 * 100) + "%";
}
} }
// 声音
volumes.forEach(v => {
const { volume, clientId } = v;
if(me.clientId === clientId) {
me.setVolume(volume);
} else {
const remoteClient = me.remoteClients.get(clientId);
if(remoteClient) {
remoteClient.setVolume(volume);
}
}
});
} }
/** /**
* 消费媒体信令 * 消费媒体信令
@@ -633,12 +649,12 @@ class Taoyao extends RemoteClient {
// ) // )
// ); // );
self.push(message); self.push(message);
console.log("消费者", consumer); console.debug("远程媒体:", consumer);
const remoteClient = self.remoteClients.get(consumer.sourceId); const remoteClient = self.remoteClients.get(consumer.sourceId);
if(remoteClient) { if(remoteClient && remoteClient.proxy && remoteClient.proxy.media) {
remoteClient.proxy.media(consumer.track, consumer); remoteClient.proxy.media(consumer.track, consumer);
} else { } else {
console.warn("远程终端无效:", consumer); console.warn("远程终端没有实现服务代理:", remoteClient);
} }
// 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) {
@@ -1131,7 +1147,11 @@ class Taoyao extends RemoteClient {
} else { } else {
// TODO异常 // TODO异常
} }
self.proxy.media(track); if(self.proxy && self.proxy.media) {
self.proxy.media(track);
} else {
console.warn("终端没有实现服务代理:", self);
}
let codec; let codec;
let encodings; let encodings;
const codecOptions = { const codecOptions = {

View File

@@ -131,6 +131,10 @@ public interface Constant {
* WebRTC * WebRTC
*/ */
String WEBRTC = "webrtc"; String WEBRTC = "webrtc";
/**
* 音量
*/
String VOLUMES = "volumes";
/** /**
* 日期时间 * 日期时间
*/ */

View File

@@ -11,7 +11,7 @@ import com.acgist.taoyao.signal.party.media.Room;
import com.acgist.taoyao.signal.protocol.ProtocolRoomAdapter; import com.acgist.taoyao.signal.protocol.ProtocolRoomAdapter;
/** /**
* 当前讲话终端信令 * 终端音量信令
* *
* @author acgist * @author acgist
*/ */
@@ -19,18 +19,24 @@ import com.acgist.taoyao.signal.protocol.ProtocolRoomAdapter;
@Description( @Description(
body = """ body = """
{ {
"volume": 音量, "roomId": "房间ID",
"clientId": "终端ID" "volumes" : [
{
"volume": 音量,
"clientId": "终端ID"
},
...
]
} }
""", """,
flow = "媒体服务->信令服务->终端" flow = "媒体服务->信令服务->终端"
) )
public class MediaAudioActiveSpeakerProtocol extends ProtocolRoomAdapter { public class MediaAudioVolumeProtocol extends ProtocolRoomAdapter {
public static final String SIGNAL = "media::audio::active::speaker"; public static final String SIGNAL = "media::audio::volume";
public MediaAudioActiveSpeakerProtocol() { public MediaAudioVolumeProtocol() {
super("当前讲话终端信令", SIGNAL); super("终端音量信令", SIGNAL);
} }
@Override @Override

View File

@@ -94,6 +94,7 @@ public class MediaConsumeProtocol extends ProtocolRoomAdapter implements Applica
* @param producer * @param producer
*/ */
private void consume(Room room, ClientWrapper consumeClientWrapper, Producer producer) { private void consume(Room room, ClientWrapper consumeClientWrapper, Producer producer) {
// TODO掉线删除
if(producer.getProduceClient().consume(producer)) { if(producer.getProduceClient().consume(producer)) {
log.debug("已经消费:{}", consumeClientWrapper.getClientId()); log.debug("已经消费:{}", consumeClientWrapper.getClientId());
return; return;