Commit b1b26209 authored by ali's avatar ali

feat: sdk

parent 3b84df78
......@@ -6,5 +6,6 @@
<body>
<div id="app"></div>
</body>
<!-- <script src="./utils/HWLLS_SDK_Web_2.3.0/lib/HWLLSPlayer.js"></script> -->
<script type="module" src="./main.ts"></script>
</html>
import HWLLSSDK from '@/renderer/utils/HWLLS_SDK_Web_2.3.0';
import EventEmitter from 'events';
// import { HWLLSSDK } from '@/renderer/utils/HWLLS_SDK_Web_2.3.0/export';
const { createClient, setLogLevel } = HWLLSSDK;
const { createClient, setLogLevel } = (window as any).HWLLSSDK.default;
/**
*
......@@ -97,7 +97,7 @@ export class HwWebRTC extends EventEmitter {
*/
static async isBrowserSupport() {
let check = false;
check = await HWLLSSDK.checkSystemRequirements();
check = await (window as any).HWLLSSDK.default.checkSystemRequirements();
return check;
}
......
......@@ -85,7 +85,7 @@ export class PhotoRole extends EventEmitter {
private _liveStatus: 'init' | 'ready' | 'wait' | 'closing' | 'pushing' = 'closing';
private _pollTimeout = -1;
private _answerArgs: PhotoAnswer | null = null;
answerArgs: PhotoAnswer | null = null;
readonly url: string;
readonly sessionId = guid();
......@@ -100,7 +100,7 @@ export class PhotoRole extends EventEmitter {
// type: 'photo',
// callbacks: {
// chatConnect: (ans) => {
// this._answerArgs = ans;
// this.answerArgs = ans;
// console.time('chat');
// },
// chatEnd: (ans) => {
......@@ -148,7 +148,7 @@ export class PhotoRole extends EventEmitter {
while (this._liveTaskQueue.length) {
const task = this._liveTaskQueue.shift() as LiveOptions;
console.time(task.text);
if (this._liveStatus === 'closing') await this._initLive();
if (this._liveStatus === 'closing') await this.initLive();
await this._createLive(task);
......@@ -162,31 +162,31 @@ export class PhotoRole extends EventEmitter {
}
private async _liveStatusTrace() {
if (!this._answerArgs) return;
if (!this.answerArgs) return;
if (this._liveStatus === 'pushing') {
this._answerArgs.playState = 'playing';
this.answerArgs.playState = 'playing';
this._typingOutAnswer();
return;
}
if (this._liveStatus === 'wait') {
this._answerArgs.playState = 'pause';
this.emit('asyncAnswer', this._answerArgs)
this.answerArgs.playState = 'pause';
this.emit('asyncAnswer', this.answerArgs)
}
}
private _typingRunner = false;
private async _typingOutAnswer() {
if (!this._answerArgs || this._typingRunner) return;
if (!this.answerArgs || this._typingRunner) return;
this._typingRunner = true;
// 加延迟是为了 playing 状态时,能跟声音保持相对同步
await new Promise((resolve) => setTimeout(resolve, 2000));
while (this._answerArgs._typingAnswer.length) {
this._answerArgs.asyncAnswer += this._answerArgs._typingAnswer.shift();
this.emit('asyncAnswer', this._answerArgs)
while (this.answerArgs._typingAnswer.length) {
this.answerArgs.asyncAnswer += this.answerArgs._typingAnswer.shift();
this.emit('asyncAnswer', this.answerArgs)
await new Promise((resolve) => setTimeout(resolve, 100));
}
......@@ -195,28 +195,28 @@ export class PhotoRole extends EventEmitter {
private _play(url: string) {
return new Promise<void>((resolve) => {
if (!this._rtc) return;
let isPlaying = false;
let timeout = -1;
this._rtc.once('videoStart', () => {
isPlaying = true;
clearTimeout(timeout);
resolve();
});
// if (!this._rtc) return;
// let isPlaying = false;
// let timeout = -1;
// this._rtc.once('videoStart', () => {
// isPlaying = true;
// clearTimeout(timeout);
// resolve();
// });
const keepCalling = async () => {
if (isPlaying) return;
try {
await this._rtc?.startPlay(url);
if (!isPlaying) {
timeout = setTimeout(keepCalling, 400) as unknown as number;
}
} catch (error) {
timeout = setTimeout(keepCalling, 200) as unknown as number;
}
};
keepCalling();
// const keepCalling = async () => {
// if (isPlaying) return;
// try {
// await this._rtc?.startPlay(url);
// if (!isPlaying) {
// timeout = setTimeout(keepCalling, 400) as unknown as number;
// }
// } catch (error) {
// timeout = setTimeout(keepCalling, 200) as unknown as number;
// }
// };
// keepCalling();
});
}
......@@ -331,7 +331,7 @@ export class PhotoRole extends EventEmitter {
keepCalling();
}
private async _initLive() {
async initLive() {
console.time('init');
console.time('init-_createLive');
......@@ -365,18 +365,18 @@ export class PhotoRole extends EventEmitter {
}
private _bindEvents() {
this._rtc?.on('videoStart', () => {
this.emit('videoStart');
});
this._rtc?.on('audioStart', () => {
this.emit('audioStart');
});
this._rtc?.on('audioBroken', () => {
this.emit('audioBroken');
});
this._rtc?.on('videoBroken', () => {
this.emit('videoBroken');
});
// this._rtc?.on('videoStart', () => {
// this.emit('videoStart');
// });
// this._rtc?.on('audioStart', () => {
// this.emit('audioStart');
// });
// this._rtc?.on('audioBroken', () => {
// this.emit('audioBroken');
// });
// this._rtc?.on('videoBroken', () => {
// this.emit('videoBroken');
// });
}
/**
......@@ -427,7 +427,7 @@ export class PhotoRole extends EventEmitter {
this._rtc = new HwWebRTC(this._webRTCContainer.id);
this._bindEvents();
await this._initLive();
await this.initLive();
// this._pollStatus(this.sessionId);
}
......
......@@ -11,6 +11,7 @@ import type {
import { audioAiTTS, localTTS } from '../plugins/tts'
import useStore from '@/renderer/store'
import flvjs from 'flv.js'
import { PhotoRole } from '@/renderer/plugins/live/PhotoRole';
const router = useRouter()
const route = useRoute()
......@@ -24,6 +25,7 @@ const url = route.query.url as string
const microphoneState = ref<'waitInput' | 'input' | 'loading' | 'disabled'>('waitInput')
const videoElement = ref<HTMLVideoElement | null>(null)
const can = ref<HTMLCanvasElement | null>(null)
let photoRole: PhotoRole | null = null;
let flvPlayer: flvjs.Player | null = null
onMounted(() => {
......@@ -49,6 +51,7 @@ async function init() {
draw(ctx, img)
canvasEle.width = img.naturalWidth
canvasEle.height = img.naturalHeight
photoRole = new PhotoRole(url, canvasEle);
// initPlayer(videoEle);
......@@ -71,6 +74,8 @@ async function init() {
requestAnimationFrame(updateFrame)
}
requestAnimationFrame(updateFrame)
await photoRole.initLive();
}
function draw(
......@@ -448,19 +453,6 @@ async function runAudioPlay() {
await audio.play()
}
// eslint-disable-next-line no-unused-vars
async function xfTTS(text: string) {
const tone = settings.source.find(({ sourceId }) => settings.selectSource === sourceId)
if (!tone) return
const res = await audioAiTTS({
host: settings.ttsHost,
text,
speed: 3,
speaker: tone.sourceId,
provider: tone.provider
})
console.log('----------------> tts:', res)
}
</script>
<template>
......
export * from './lib/HWLLSPlayer';
\ No newline at end of file
export * as HWLLSSDK from './lib/HWLLSPlayer';
\ No newline at end of file
......@@ -7,6 +7,7 @@
"moduleResolution": "node",
"jsx": "preserve",
"allowJs": true,
"checkJs": false,
"noImplicitAny": false,
"allowSyntheticDefaultImports": true,
"declaration": true,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment