202 lines
4.9 KiB
Plaintext
202 lines
4.9 KiB
Plaintext
/* eslint-disable */
|
|
// @ts-nocheck
|
|
// @ts-ignore-start
|
|
|
|
import { Result } from '../interface.uts'
|
|
|
|
const resolverMap = new Map<string, (value: Result) => void>()
|
|
|
|
function avRecvHandle(
|
|
id: Optional<UnsafePointer<Int8>>,
|
|
recv_buf: Optional<UnsafeMutablePointer<UInt8>>,
|
|
recv_len: Int
|
|
) {}
|
|
|
|
function msgHandle(
|
|
id: Optional<UnsafePointer<Int8>>,
|
|
type: XP2PType,
|
|
msg: Optional<UnsafePointer<Int8>>
|
|
): Optional<UnsafePointer<Int8>> {
|
|
if (type.rawValue == 1004) {
|
|
const idString = id != null ? P2PConversionHelper.cStringToString(id) : null
|
|
if (idString != null) {
|
|
const resolver = resolverMap.get(idString!)
|
|
if (resolver != null) {
|
|
const urlResult = delegateHttpFlv(idString!)
|
|
if (urlResult != null) {
|
|
const urlString = P2PConversionHelper.cStringToString(urlResult)
|
|
if (urlString != null && urlString != '') {
|
|
resolver!({
|
|
code: 0,
|
|
data: { url: urlString },
|
|
message: '成功'
|
|
})
|
|
resolverMap.delete(idString!)
|
|
return null
|
|
}
|
|
}
|
|
resolver!({ code: -1, data: {}, message: '获取播放URL失败' })
|
|
resolverMap.delete(idString!)
|
|
}
|
|
}
|
|
}
|
|
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> {
|
|
const deviceId = `${productId}/${deviceName}`
|
|
return new Promise<Result>((resolve, reject) => {
|
|
resolverMap.set(deviceId, resolve)
|
|
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(null, msgHandle, null)
|
|
|
|
const result = startService(deviceId, productId, deviceName, config)
|
|
|
|
if (NSNumber(0) === result) {
|
|
const setP2PInfoResult = setDeviceXp2pInfo(deviceId, xp2pInfo)
|
|
|
|
if (NSNumber(0) === setP2PInfoResult) {
|
|
setTimeout(() => {
|
|
if (resolverMap.has(deviceId)) {
|
|
resolve({ code: -1, data: {}, message: '获取播放URL超时' })
|
|
resolverMap.delete(deviceId)
|
|
}
|
|
}, 20000)
|
|
} else {
|
|
resolve({ code: -1, data: {}, message: 'setDeviceXp2pInfo 调用失败' })
|
|
resolverMap.delete(deviceId)
|
|
}
|
|
} else {
|
|
resolve({ code: -1, data: {}, message: 'startService 调用失败' })
|
|
resolverMap.delete(deviceId)
|
|
}
|
|
} catch (error) {
|
|
console.log('startServiceFunction 报错', error)
|
|
resolve({
|
|
code: -1,
|
|
data: {},
|
|
message: error.toString()
|
|
})
|
|
resolverMap.delete(deviceId)
|
|
}
|
|
})
|
|
}
|
|
|
|
export const stopServiceFunction = async function (id: string): Promise<Result> {
|
|
try {
|
|
const result = await stopService(id)
|
|
console.log('stopService', 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 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 = 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> {
|
|
const buffer = XP2PDataHelper.createBytes(data as NSArray)
|
|
|
|
if (buffer == null) {
|
|
return {
|
|
code: -1,
|
|
data: {},
|
|
message: '创建数据缓冲区失败'
|
|
}
|
|
}
|
|
|
|
try {
|
|
const result = await dataSend(id, buffer, data.length)
|
|
|
|
XP2PDataHelper.deallocateBytes(buffer)
|
|
|
|
return {
|
|
code: 0,
|
|
data: {
|
|
result: result
|
|
},
|
|
message: '成功'
|
|
}
|
|
} catch (error) {
|
|
return {
|
|
code: -1,
|
|
data: {},
|
|
message: error.toString()
|
|
}
|
|
}
|
|
}
|
|
|
|
// @ts-ignore-end
|