通过 Azure 通信调用库,我尝试获得网络质量。
const { CallClient, VideoStreamRenderer, LocalVideoStream } = require('@azure/communication-calling');
const { AzureCommunicationTokenCredential } = require('@azure/communication-common');
const { AzureLogger, setLogLevel } = require("@azure/logger");
我尝试了这两个代码,但我有同样的错误“功能未定义”。
编辑:我将功能的代码放入 subscribeToCall 函数中。
我忘记导入什么东西了吗?
编辑更多详细信息:
我的功能是在开始或接受时订阅呼叫。
subscribeToCall = (call) => {
try {
// Subscribe to call's 'stateChanged' event for value changes.
call.on('stateChanged', async () => {
// If ringing / Connected / Disconnected ...
});
call.on('isLocalVideoStartedChanged', () => {
console.log(`------------- isLocalVideoStarted changed: ${call.isLocalVideoStarted}`);
});
call.localVideoStreams.forEach(async (lvs) => {
localVideoStream = lvs;
await displayLocalVideoStream();
});
call.on('localVideoStreamsUpdated', e => {
e.added.forEach(async (lvs) => {
localVideoStream = lvs;
await displayLocalVideoStream();
});
e.removed.forEach(lvs => {
removeLocalVideoStream();
});
});
call.remoteParticipants.forEach(remoteParticipant => {
subscribeToRemoteParticipant(remoteParticipant);
});
// Subscribe to the call's 'remoteParticipantsUpdated' event to be
// notified when new participants are added to the call or removed from the call.
call.on('remoteParticipantsUpdated', e => {
// Subscribe to new remote participants that are added to the call.
e.added.forEach(remoteParticipant => {
subscribeToRemoteParticipant(remoteParticipant)
});
// Unsubscribe from participants that are removed from the call
e.removed.forEach(remoteParticipant => {
console.log('Remote participant removed from the call.');
});
});
// CALL FEATURE TESTS ::::::::
// First example
/*const userFacingDiagnostics = call.feature(Features.UserFacingDiagnostics);
userFacingDiagnostics.media.on("diagnosticChanged", (diagnosticInfo) => {
console.log(diagnosticInfo);
});
userFacingDiagnostics.network.on("diagnosticChanged", (diagnosticInfo) => {
console.log(diagnosticInfo);
});*/
// Second example
/*call.feature(Features.UserFacingDiagnostics).network.on('diagnosticChanged', (diagnosticInfo) => {
if (diagnosticInfo.diagnostic === 'networkReceiveQuality') {
if (diagnosticInfo.value === DiagnosticQuality.Bad) {
console.log("Network quality = BAD");
} else if (diagnosticInfo.value === DiagnosticQuality.Poor) {
console.log("Network quality = POOR");
} else if (diagnosticInfo.value === DiagnosticQuality.Good) {
console.log("Network quality = GOOD");
}
}
});*/
} catch (error) {
console.error(error);
}
}
编辑2024/04/24
第一个示例源链接:Stackoverflow - 无法加载 Features.Diagnostics
第二个示例源链接:networkReceiveQuality,UFD
错误“功能未定义”表示在您的代码范围内无法识别
Features
对象。
特征对象应该像这样使用:
const callQualityApi = call.api(Features.Diagnostics);
callQualityApi.network.on('diagnosticChanged', diagnosticChangedListener);
callQualityApi.media.on('diagnosticChanged', diagnosticChangedListener);
要为 Azure 通信服务资源启用呼叫诊断,请按照以下步骤操作:
设置诊断设置后,您可以从 Azure 门户中的任何 Azure 通信服务资源访问呼叫诊断。
下面的示例代码是 Azure 通信服务调用 Node.js 中的 SDK 来为各种呼叫和网络诊断事件设置事件处理程序和侦听器。它会检查
DiagnosticQuality.Bad
、DiagnosticQuality.Poor
和 DiagnosticQuality.Good
。
const { CallClient, VideoStreamRenderer, LocalVideoStream, Features } = require('@azure/communication-calling');
const { AzureCommunicationTokenCredential } = require('@azure/communication-common');
const { AzureLogger, setLogLevel } = require("@azure/logger");
const subscribeToCall = (call) => {
try {
call.on('stateChanged', async () => {
// If ringing / Connected / Disconnected ...
});
call.on('isLocalVideoStartedChanged', () => {
console.log(`------------- isLocalVideoStarted changed: ${call.isLocalVideoStarted}`);
});
call.localVideoStreams.forEach(async (lvs) => {
localVideoStream = lvs;
await displayLocalVideoStream();
});
call.on('localVideoStreamsUpdated', e => {
e.added.forEach(async (lvs) => {
localVideoStream = lvs;
await displayLocalVideoStream();
});
e.removed.forEach(lvs => {
removeLocalVideoStream();
});
});
call.remoteParticipants.forEach(remoteParticipant => {
subscribeToRemoteParticipant(remoteParticipant);
});
call.on('remoteParticipantsUpdated', e => {
e.added.forEach(remoteParticipant => {
subscribeToRemoteParticipant(remoteParticipant)
});
e.removed.forEach(remoteParticipant => {
console.log('Remote participant removed from the call.');
});
});
call.api(Features.Diagnostics).network.on('diagnosticChanged', (diagnosticInfo) => {
console.log(diagnosticInfo);
});
call.api(Features.UserFacingDiagnostics).network.on('diagnosticChanged', (diagnosticInfo) => {
if (diagnosticInfo.diagnostic === 'networkReceiveQuality') {
if (diagnosticInfo.value === DiagnosticQuality.Bad) {
console.log("Network quality = BAD");
} else if (diagnosticInfo.value === DiagnosticQuality.Poor) {
console.log("Network quality = POOR");
} else if (diagnosticInfo.value === DiagnosticQuality.Good) {
console.log("Network quality = GOOD");
}
}
});
} catch (error) {
console.error(error);
}
}