172 lines
4.3 KiB
Plaintext
172 lines
4.3 KiB
Plaintext
/* eslint-disable */
|
|
// @ts-nocheck
|
|
// @ts-ignore-start
|
|
|
|
import { Result } from '../interface.uts'
|
|
|
|
function avRecvHandle(
|
|
id: Optional<UnsafePointer<Int8>>,
|
|
recv_buf: Optional<UnsafeMutablePointer<UInt8>>,
|
|
recv_len: Int
|
|
) {
|
|
if (id != null) {
|
|
const idString = P2PConversionHelper.cStringToString(id)
|
|
// console.log(`avRecvHandle: ${idString}`)
|
|
}
|
|
}
|
|
|
|
function msgHandle(
|
|
id: Optional<UnsafePointer<Int8>>,
|
|
type: XP2PType,
|
|
msg: Optional<UnsafePointer<Int8>>
|
|
): Optional<UnsafePointer<Int8>> {
|
|
const idString = id != null ? P2PConversionHelper.cStringToString(id) : ''
|
|
const msgString = msg != null ? P2PConversionHelper.cStringToString(msg) : ''
|
|
if (type == XP2PTypeLog) {
|
|
console.log(`${msgString}`)
|
|
}
|
|
return null
|
|
}
|
|
|
|
function deviceDataHandle(
|
|
id: Optional<UnsafePointer<Int8>>,
|
|
recv_buf: Optional<UnsafeMutablePointer<UInt8>>,
|
|
recv_len: Int
|
|
): Optional<UnsafeMutablePointer<Int8>> {
|
|
if (id != null) {
|
|
const idString = P2PConversionHelper.cStringToString(id)
|
|
// console.log(`deviceDataHandle: ${idString}, len: ${recv_len}`)
|
|
}
|
|
return null
|
|
}
|
|
|
|
export const startServiceFunction = async function (
|
|
appKey: string,
|
|
appSecret: string,
|
|
productId: string,
|
|
deviceName: string,
|
|
xp2pInfo: string
|
|
): Promise<Result> {
|
|
return new Promise<Result>((resolve, reject) => {
|
|
try {
|
|
const config = app_config_t()
|
|
config.server = UnsafePointer(strdup('')!)
|
|
config.ip = UnsafePointer(strdup('')!)
|
|
config.port = 20002
|
|
config.type = XP2P_PROTOCOL_AUTO
|
|
config.cross = true
|
|
|
|
// 设置回调
|
|
setUserCallbackToXp2p(avRecvHandle, msgHandle, deviceDataHandle)
|
|
|
|
const result = startService(`${productId}/${deviceName}`, productId, deviceName, config)
|
|
|
|
if (NSNumber(0) === result) {
|
|
const setP2PInfoResult = setDeviceXp2pInfo(`${productId}/${deviceName}`, xp2pInfo)
|
|
|
|
if (NSNumber(0) === setP2PInfoResult) {
|
|
let attempts = 0
|
|
const maxAttempts = 20
|
|
const interval: Int = 100
|
|
|
|
function pollForUrl() {
|
|
if (attempts >= maxAttempts) {
|
|
resolve({ code: -1, data: {}, message: '获取播放URL超时' })
|
|
return
|
|
}
|
|
attempts++
|
|
|
|
const urlResult = delegateHttpFlv(`${productId}/${deviceName}`)
|
|
|
|
if (urlResult != null) {
|
|
const urlString = P2PConversionHelper.cStringToString(urlResult)
|
|
|
|
if (urlString != null && urlString != '') {
|
|
resolve({
|
|
code: 0,
|
|
data: { url: urlString },
|
|
message: '成功'
|
|
})
|
|
return
|
|
}
|
|
}
|
|
setTimeout(pollForUrl, interval)
|
|
}
|
|
pollForUrl()
|
|
} else {
|
|
resolve({ code: -1, data: {}, message: 'setDeviceXp2pInfo 调用失败' })
|
|
}
|
|
} else {
|
|
resolve({ code: -1, data: {}, message: 'startService 调用失败' })
|
|
}
|
|
} catch (error) {
|
|
console.log('startServiceFunction 报错', error)
|
|
resolve({
|
|
code: -1,
|
|
data: {},
|
|
message: error.toString()
|
|
})
|
|
}
|
|
})
|
|
}
|
|
|
|
export const stopServiceFunction = async function (id: string): Promise<Result> {
|
|
try {
|
|
await stopService(id)
|
|
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 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 nullPointer = P2PPointerHelper.createNullPointer()
|
|
// const result = await stopSendService(id, nullPointer)
|
|
// console.log('停止发送服务', result)
|
|
return {
|
|
code: 0,
|
|
data: {},
|
|
message: '成功'
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
code: -1,
|
|
data: {},
|
|
message: error.toString()
|
|
}
|
|
}
|
|
}
|
|
|
|
// @ts-ignore-end
|