fanpeng 601bfb1e16 1
2025-07-10 17:46:36 +08:00

189 lines
4.4 KiB
Plaintext

/* eslint-disable */
// @ts-nocheck
// @ts-ignore-start
import { UTSAndroid } from 'io.dcloud.uts'
import { XP2PAppConfig, XP2P, XP2PCallback } from 'com.tencent.xnet'
import { Result } from '../interface.uts'
class XP2PCallbackImpl extends XP2PCallback {
private eventNotifyCallback?: (id: string | null, msg: string | null, event: Int) => void
constructor(eventNotifyCallback?: (id: string | null, msg: string | null, event: Int) => void) {
super()
this.eventNotifyCallback = eventNotifyCallback
}
override fail(msg: string | null, errorCode: Int): void {
// console.log('XP2P callback fail:', msg, errorCode)
}
override commandRequest(id: string | null, msg: string | null): void {
// console.log('XP2P callback commandRequest:', id, msg)
}
override xp2pEventNotify(id: string | null, msg: string | null, event: Int): void {
// console.log('XP2P callback xp2pEventNotify:', id, msg, event)
this.eventNotifyCallback?.invoke(id, msg, event)
}
override avDataRecvHandle(id: string | null, data: ByteArray | null, len: Int): void {
// console.log('XP2P callback avDataRecvHandle:', id, len)
}
override avDataCloseHandle(id: string | null, msg: string | null, errorCode: Int): void {
// console.log('XP2P callback avDataCloseHandle:', id, msg, errorCode)
}
override onDeviceMsgArrived(id: string | null, data: ByteArray | null, len: Int): string {
// console.log('XP2P callback onDeviceMsgArrived:', id, len)
return ''
}
}
async function getLiveUrlAsync(
id: string,
resolve: (result: Result) => void,
reject: (result: Result) => void
): Promise<void> {
try {
const liveUrl = await XP2P.delegateHttpFlv(id)
resolve({
code: 0,
data: {
url: liveUrl
},
message: '成功'
})
} catch (error) {
reject({
code: -1,
data: {},
message: error.toString()
})
}
}
export const startServiceFunction = async function (
appKey: string,
appSecret: string,
productId: string,
deviceName: string,
xp2pInfo: string
): Promise<Result> {
try {
const context = UTSAndroid.getAppContext()
let xp2pConfig: XP2PAppConfig = new XP2PAppConfig()
xp2pConfig.appKey = appKey
xp2pConfig.appSecret = appSecret
await XP2P.startService(context, productId, deviceName, xp2pInfo, xp2pConfig)
return new Promise<Result>((resolve, reject) => {
const callback: XP2PCallback = new XP2PCallbackImpl(
(id: string | null, msg: string | null, event: Int): void => {
if (event == 1004) {
getLiveUrlAsync(id!, resolve, reject)
}
}
)
XP2P.setCallback(callback)
})
} catch (error) {
return {
code: -1,
data: {},
message: error.toString()
}
}
}
export const stopServiceFunction = async function (id: string): Promise<Result> {
try {
const result = await XP2P.stopService(id)
console.log('停止服务', result)
return {
code: 0,
data: {},
message: '成功'
}
} catch (error) {
return {
code: -1,
data: {},
message: error.toString()
}
}
}
export const runSendServiceFunction = async function (
id: string,
cmd: string,
crypto: boolean
): Promise<Result> {
try {
const result = await XP2P.runSendService(id, cmd, crypto)
console.log('发送服务', result)
return {
code: 0,
data: {},
message: '成功'
}
} catch (error) {
return {
code: -1,
data: {},
message: error.toString()
}
}
}
export const stopSendServiceFunction = async function (id: string): Promise<Result> {
try {
const result = await XP2P.stopSendService(id, null)
console.log('停止发送服务', result)
return {
code: 0,
data: {},
message: '成功'
}
} catch (error) {
return {
code: -1,
data: {},
message: error.toString()
}
}
}
export const dataSendFunction = async function (id: string, data: Array<number>): Promise<Result> {
try {
let byteArray = new ByteArray(data.length.toInt())
for (let i = 0; i < data.length; i++) {
byteArray.set(i.toInt(), data[i].toByte())
}
const result = await XP2P.dataSend(id, byteArray, data.length.toInt())
return {
code: 0,
data: {
result: result
},
message: '成功'
}
} catch (error) {
return {
code: -1,
data: {},
message: error.toString()
}
}
}
// @ts-ignore-end