[*] 音量监听优化

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

View File

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

View File

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

View File

@@ -11,26 +11,32 @@ import com.acgist.taoyao.signal.party.media.Room;
import com.acgist.taoyao.signal.protocol.ProtocolRoomAdapter;
/**
* 当前讲话终端信令
* 终端音量信令
*
* @author acgist
*/
@Protocol
@Description(
body = """
{
"roomId": "房间ID",
"volumes" : [
{
"volume": 音量,
"clientId": "终端ID"
},
...
]
}
""",
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() {
super("当前讲话终端信令", SIGNAL);
public MediaAudioVolumeProtocol() {
super("终端音量信令", SIGNAL);
}
@Override

View File

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