[*] 日常优化

This commit is contained in:
acgist
2023-10-11 08:00:08 +08:00
parent f86faee977
commit 69be5ada9b
3 changed files with 71 additions and 70 deletions

View File

@@ -606,34 +606,35 @@ class Taoyao {
/** /**
* 服务端录像信令 * 服务端录像信令
* *
* @param {*} message 消息 * @param {*} message 信令消息
* @param {*} body 消息主体 * @param {*} body 消息主体
*/ */
async controlServerRecord(message, body) { async controlServerRecord(message, body) {
const me = this; const {
const { enabled, roomId } = body; roomId,
const room = me.rooms.get(roomId); enabled,
} = body;
const room = this.rooms.get(roomId);
if(!room) { if(!room) {
// 直接关闭房间时,房间关闭可能早于结束录像。 // 直接关闭房间时,房间关闭可能早于结束录像。
console.info("服务端录像房间无效", roomId); console.debug("服务端录像房间无效", roomId);
return; return;
} }
if(enabled) { if(enabled) {
await me.controlServerRecordStart(message, body, room); await this.controlServerRecordStart(message, body, room);
} else { } else {
await me.controlServerRecordStop(message, body, room); await this.controlServerRecordStop(message, body, room);
} }
} }
/** /**
* 开始服务端录像 * 开始服务端录像
* *
* @param {*} message 消息 * @param {*} message 信令消息
* @param {*} body 消息主体 * @param {*} body 消息主体
* @param {*} room 房间 * @param {*} room 房间
*/ */
async controlServerRecordStart(message, body, room) { async controlServerRecordStart(message, body, room) {
const me = this;
const { const {
host, host,
roomId, roomId,
@@ -665,7 +666,7 @@ class Taoyao {
const audioTransport = await room.mediasoupRouter.createPlainTransport(plainTransportOptions); const audioTransport = await room.mediasoupRouter.createPlainTransport(plainTransportOptions);
audioTransportId = audioTransport.id; audioTransportId = audioTransport.id;
room.transports.set(audioTransportId, audioTransport); room.transports.set(audioTransportId, audioTransport);
me.transportEvent("plain", roomId, audioTransport); this.transportEvent("plain", roomId, audioTransport);
audioTransport.clientId = clientId; audioTransport.clientId = clientId;
await audioTransport.connect({ await audioTransport.connect({
ip : host, ip : host,
@@ -683,16 +684,16 @@ class Taoyao {
audioConsumer.streamId = audioStreamId; audioConsumer.streamId = audioStreamId;
room.consumers.set(audioConsumerId, audioConsumer); room.consumers.set(audioConsumerId, audioConsumer);
audioConsumer.observer.on("close", () => { audioConsumer.observer.on("close", () => {
console.info("关闭服务端录像音频消费者", audioConsumerId); console.debug("关闭服务端录像音频消费者", audioConsumerId);
room.consumers.delete(audioConsumerId); room.consumers.delete(audioConsumerId);
}); });
console.debug("创建服务器录像音频消费者", audioTransportId, audioConsumerId, audioTransport.tuple, audioRtpParameters); console.debug("创建服务器录像音频消费者", audioTransportId, audioConsumerId, audioTransport.tuple, audioRtpParameters.codecs);
} }
if(videoProducerId) { if(videoProducerId) {
const videoTransport = await room.mediasoupRouter.createPlainTransport(plainTransportOptions); const videoTransport = await room.mediasoupRouter.createPlainTransport(plainTransportOptions);
videoTransportId = videoTransport.id; videoTransportId = videoTransport.id;
room.transports.set(videoTransportId, videoTransport); room.transports.set(videoTransportId, videoTransport);
me.transportEvent("plain", roomId, videoTransport); this.transportEvent("plain", roomId, videoTransport);
videoTransport.clientId = clientId; videoTransport.clientId = clientId;
await videoTransport.connect({ await videoTransport.connect({
ip : host, ip : host,
@@ -710,18 +711,22 @@ class Taoyao {
videoConsumer.streamId = videoStreamId; videoConsumer.streamId = videoStreamId;
room.consumers.set(videoConsumerId, videoConsumer); room.consumers.set(videoConsumerId, videoConsumer);
videoConsumer.observer.on("close", () => { videoConsumer.observer.on("close", () => {
console.info("关闭服务器录像视频消费者", videoConsumerId); console.debug("关闭服务器录像视频消费者", videoConsumerId);
room.consumers.delete(videoConsumerId); room.consumers.delete(videoConsumerId);
}); });
console.debug("创建服务器录像视频消费者", videoTransportId, videoConsumerId, videoTransport.tuple, videoRtpParameters); console.debug("创建服务器录像视频消费者", videoTransportId, videoConsumerId, videoTransport.tuple, videoRtpParameters.codecs);
} }
if(audioConsumer) { if(audioConsumer) {
await audioConsumer.resume(); await audioConsumer.resume();
} }
if(videoConsumer) { if(videoConsumer) {
await videoConsumer.resume(); await videoConsumer.resume();
}
try {
// 请求录像关键帧 // 请求录像关键帧
me.requestKeyFrameForRecord(0, filepath, videoConsumer); this.requestKeyFrameForRecord(0, filepath, videoConsumer);
} catch (error) {
console.error("请求录像关键帧异常", error);
} }
message.body = { message.body = {
roomId : roomId, roomId : roomId,
@@ -732,7 +737,7 @@ class Taoyao {
audioRtpParameters: audioRtpParameters, audioRtpParameters: audioRtpParameters,
videoRtpParameters: videoRtpParameters, videoRtpParameters: videoRtpParameters,
}; };
me.push(message); this.push(message);
} }
/** /**
@@ -748,35 +753,34 @@ class Taoyao {
requestKeyFrameMaxIndex, requestKeyFrameMaxIndex,
requestKeyFrameFileSize requestKeyFrameFileSize
} = config.record; } = config.record;
if(++index > requestKeyFrameMaxIndex) { if(index >= requestKeyFrameMaxIndex) {
console.warn("请求录像关键帧次数超限", filepath, index); console.warn("请求录像关键帧次数超限", filepath, index);
return; return;
} }
if(videoConsumer.closed) { if(videoConsumer.closed) {
console.warn("请求录像关键帧视频关闭", filepath); console.warn("请求录像关键帧视频关闭", filepath, index);
return; return;
} }
// 判断文件大小验证是否已经开始录像:创建文件 -> 视频信息 -> 视频数据 -> 封装视频 // 判断文件大小验证是否已经开始录像:创建文件 -> 视频信息 -> 视频数据 -> 封装视频
if(fs.existsSync(filepath) && fs.statSync(filepath).size >= requestKeyFrameFileSize) { if(fs.existsSync(filepath) && fs.statSync(filepath).size >= requestKeyFrameFileSize) {
console.info("请求录像关键帧已经开始录像", filepath); console.info("请求录像关键帧已经开始录像", filepath, index);
return; return;
} }
console.debug("请求录像关键帧", filepath, index); console.debug("请求录像关键帧", filepath, index);
videoConsumer.requestKeyFrame(); videoConsumer.requestKeyFrame();
setTimeout(() => { setTimeout(() => {
this.requestKeyFrameForRecord(index, filepath, videoConsumer); this.requestKeyFrameForRecord(++index, filepath, videoConsumer);
}, 1000); }, 1000);
} }
/** /**
* 结束服务端录像 * 结束服务端录像
* *
* @param {*} message 消息 * @param {*} message 信令消息
* @param {*} body 消息主体 * @param {*} body 消息主体
* @param {*} room 房间 * @param {*} room 房间
*/ */
async controlServerRecordStop(message, body, room) { async controlServerRecordStop(message, body, room) {
const me = this;
const { const {
audioStreamId, videoStreamId, audioStreamId, videoStreamId,
audioConsumerId, videoConsumerId, audioConsumerId, videoConsumerId,
@@ -785,21 +789,21 @@ class Taoyao {
console.info("结束服务端录像", audioStreamId, videoStreamId); console.info("结束服务端录像", audioStreamId, videoStreamId);
const audioConsumer = room.consumers.get(audioConsumerId); const audioConsumer = room.consumers.get(audioConsumerId);
if(audioConsumer) { if(audioConsumer) {
audioConsumer.close(); await audioConsumer.close();
} }
const videoConsumer = room.consumers.get(videoConsumerId); const videoConsumer = room.consumers.get(videoConsumerId);
if(videoConsumer) { if(videoConsumer) {
videoConsumer.close(); await videoConsumer.close();
} }
const audioTransport = room.transports.get(audioTransportId); const audioTransport = room.transports.get(audioTransportId);
if(audioTransport) { if(audioTransport) {
audioTransport.close(); await audioTransport.close();
} }
const videoTransport = room.transports.get(videoTransportId); const videoTransport = room.transports.get(videoTransportId);
if(videoTransport) { if(videoTransport) {
videoTransport.close(); await videoTransport.close();
} }
me.push(message); this.push(message);
} }
/** /**

View File

@@ -1460,12 +1460,11 @@ class Taoyao extends RemoteClient {
* @param {*} clientId 终端ID * @param {*} clientId 终端ID
* @param {*} enabled 录制状态 * @param {*} enabled 录制状态
*/ */
controlServerRecord(clientId, enabled) { async controlServerRecord(clientId, enabled) {
const me = this; return await this.request(protocol.buildMessage("control::server::record", {
me.request(protocol.buildMessage("control::server::record", { enabled,
to : clientId, to : clientId,
roomId : me.roomId, roomId: this.roomId,
enabled: enabled
})); }));
} }

View File

@@ -2,7 +2,6 @@ package com.acgist.taoyao.signal.protocol.control;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async; import org.springframework.scheduling.annotation.Async;
@@ -106,7 +105,6 @@ public class ControlServerRecordProtocol extends ProtocolControlAdapter implemen
* *
* @param room 房间 * @param room 房间
* @param clientWrapper 终端 * @param clientWrapper 终端
* @param mediaClient 媒体终端
* *
* @return 文件地址 * @return 文件地址
*/ */
@@ -114,17 +112,17 @@ public class ControlServerRecordProtocol extends ProtocolControlAdapter implemen
if(clientWrapper == null) { if(clientWrapper == null) {
throw MessageCodeException.of("终端没有进入房间"); throw MessageCodeException.of("终端没有进入房间");
} }
final Recorder recorder;
synchronized (clientWrapper) { synchronized (clientWrapper) {
final Recorder recorder = clientWrapper.getRecorder(); final Recorder oldRecorder = clientWrapper.getRecorder();
if(recorder != null) { if(oldRecorder != null) {
return recorder.getFilepath(); return oldRecorder.getFilepath();
} }
}
final String name = UUID.randomUUID().toString();
// 打开录像线程 // 打开录像线程
final Recorder recorder = new Recorder(name, room, clientWrapper, this.ffmpegProperties); recorder = new Recorder(this.idService.buildUuid(), room, clientWrapper, this.ffmpegProperties);
recorder.start(); recorder.start();
clientWrapper.setRecorder(recorder); clientWrapper.setRecorder(recorder);
}
// 打开媒体录像 // 打开媒体录像
final Message message = this.build(); final Message message = this.build();
final Map<String, Object> body = new HashMap<>(); final Map<String, Object> body = new HashMap<>();
@@ -144,7 +142,7 @@ public class ControlServerRecordProtocol extends ProtocolControlAdapter implemen
body.put(Constant.AUDIO_STREAM_ID, recorder.getAudioStreamId()); body.put(Constant.AUDIO_STREAM_ID, recorder.getAudioStreamId());
body.put(Constant.AUDIO_PRODUCER_ID, producer.getProducerId()); body.put(Constant.AUDIO_PRODUCER_ID, producer.getProducerId());
} else if(producer.getKind() == Kind.VIDEO) { } else if(producer.getKind() == Kind.VIDEO) {
recorder.setAudioStreamId(Constant.STREAM_ID_CONSUMER.apply(producer.getStreamId(), clientWrapper.getClientId())); recorder.setVideoStreamId(Constant.STREAM_ID_CONSUMER.apply(producer.getStreamId(), clientWrapper.getClientId()));
body.put(Constant.VIDEO_STREAM_ID, recorder.getVideoStreamId()); body.put(Constant.VIDEO_STREAM_ID, recorder.getVideoStreamId());
body.put(Constant.VIDEO_PRODUCER_ID, producer.getProducerId()); body.put(Constant.VIDEO_PRODUCER_ID, producer.getProducerId());
} else { } else {
@@ -177,10 +175,10 @@ public class ControlServerRecordProtocol extends ProtocolControlAdapter implemen
if(recorder == null) { if(recorder == null) {
return null; return null;
} }
}
// 关闭录像线程 // 关闭录像线程
recorder.stop(); recorder.stop();
clientWrapper.setRecorder(null); clientWrapper.setRecorder(null);
}
// 关闭媒体录像 // 关闭媒体录像
final Message message = this.build(); final Message message = this.build();
final Map<String, Object> body = new HashMap<>(); final Map<String, Object> body = new HashMap<>();