我是使用 swift 的 ios 开发人员。我正在使用 sdk 与 ip camera 通信,从中检索数据,并将其显示给用户。我有一个随 sdk 一起提供的项目,它是用 obj c 编写的,并且可以正常工作。问题是我无法从函数将参数传递给预期的参数类型。SDK 非常旧,我使用静态库和桥接头集成它,我能够成功连接到相机。我认为 sdk 是用 c 或 c++ 编写的,我基本上在参数转换方面遇到了问题。
fos.h
typedef struct
{
int channel;
long long time;
unsigned int index;
FOSMEDIATYPE type; //Is video or audio
FOSDECFMT fmt; //The format of video or audio.
short isKey;
short multiAudioPage;
int frameTag;
union{
FOSVIDEO_INFO video; //Media type is video.
FOSAUDIO_INFO audio; //Media type is audio.
} media;
unsigned long long pts; //Pts.
unsigned int len; //Size of data.
char data[0]; //Just data.
}ATTRIBUTE_PACKED FOSDEC_DATA; //Save the video or audio data,include video or audio information.
.
目标 C 函数,当前工作
- (IBAction)login:(id)sender {
//[self._imageView add]
NSString* str = [self._textField text];
const char* url = [str UTF8String];
char ip[128];
int port;
char usr[64];
char pwd[64];
int count = sscanf(url, "%[^:]:%d/%[^:]:%s", ip, &port, usr, pwd);
if (count != 4) {
return;
}
mHandle = FosSdk_Create(ip, "", usr, pwd, port, port, FOSIPC_H264, FOSCNTYPE_IP);
//mHandle = FosSdk_Create(ip, "FASDFDSAFDASFASD", usr, pwd, port, port, FOSIPC_H264, FOSCNTYPE_P2P);
if (mHandle == FOSHANDLE_INVALID) {
return;
}
[self._Login setEnabled:NO];
[self._Logout setEnabled:YES];
[NSThread detachNewThreadSelector:@selector(RunInThread:) toTarget:self withObject:^(void *param){
int usrPrivilege = 0;
FOSCMD_RESULT ret = FosSdk_Login(mHandle, &usrPrivilege, 500);
if (FOSCMDRET_OK != ret) {
return;
}
ret = FosSdk_OpenVideo(mHandle, FOSSTREAM_SUB, 500);
if (FOSCMDRET_OK != ret) {
return;
}
//char* framebuf = (char*)malloc(512*1024);
FOSDEC_DATA* data = NULL;
int outlen = 0;
while (mHandle) {
// FosSdk_RetainHandle(mHandle, &usrPrivilege);
if ( FOSCMDRET_OK == FosSdk_GetVideoData(mHandle, (char**)&data, &outlen, FOSDECTYPE_RGB24) && outlen>0)
{
if (data->type == FOSMEDIATYPE_VIDEO) {
//int a=0;
//a++;
[self imageFromAVPicture:data->data width:data->media.video.picWidth height:data->media.video.picHeight];
}
else{
}
}
usleep(20*1000);
}
}];
}
.
fos.h 中的方法签名
FOSSDK FOSCMD_RESULT FOSAPI FosSdk_GetVideoData(FOSHANDLE handle, char **data, int *outLen, FOSDECFMT videoFmt);
.
从objective-c调用它时
FosSdk_GetVideoData(<#unsigned int handle#>, <#char **data#>, <#int *outLen#>, <#FOSDECFMT videoFmt#>)
}];
.
从swift调用它时
FosSdk_GetVideoData(handle: UInt32,
data: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>!,
outLen: UnsafeMutablePointer<Int32>!,
videoFmt: FOSDECFMT)
.
错误是数据参数,确切地说:
Cannot convert value of type 'UnsafeMutablePointer<FOSDEC_DATA>' to expected argument type 'UnsafeMutablePointer<Int8>?'
.
我尝试将数据声明更改为此,但它给了我这个错误
let data = UnsafeMutablePointer<Int8>(&fosdecData)
Ambiguous use of 'init'
.
这个指针的东西有点复杂,但我能够将我的设备与相机配对,使用的方法也使用了 unsafeMutablePointers。但是,我无法弄清楚如何将这个 empy 数据对象传递给函数。
SDK 已过时且文档不完整,这无济于事。无论如何,我需要让它工作。希望能得到一些帮助,谢谢码农!.
编辑,感谢 OOPer 。这是swift中的更新版本,实际上会产生意外错误。从函数返回的结果现在是 0,所以它是正确的。但是,尝试访问原始 FosdedData 结构时,我有一个Command failed 由于 signal: Segmentation fault: 11。我怎么能解决这个问题?对不起,很长的问题,我想准确。
var mhandle : UInt32 = FOS_HANDLE_INIT.rawValue
override func viewDidLoad() {
super.viewDidLoad()
//These functions basically do camera pairing, and write result on mhandle, which is equal to 0 , if functions work, otherwise 1
initializeFOS()
startEzLinkFOS()
sdkCreateFOS()
loginFOS()
openVideoFOS()
var data: UnsafeMutablePointer<FOSDEC_DATA>? = nil
var outlen : Int32 = 0
while self.mhandle > 0 {
let resultOOPer = withUnsafeMutablePointer(to: &data) { pointerToFosdecData in
pointerToFosdecData.withMemoryRebound(to: UnsafeMutablePointer<Int8>?.self, capacity: 1, { (pointerToInt8Pointer) in
FosSdk_GetVideoData(self.mhandle, pointerToInt8Pointer, &outlen, FOSDECTYPE_RGB24)
})
}
if resultOOPer == FOSCMDRET_OK && outlen > 0{
if let unwrData = data {
//if unwrData.pointee.type == FOSMEDIATYPE_VIDEO{
//Produced Output when i try access pointee, which is actually the fosdecData struct i need:
//Command failed due to signal: Segmentation fault: 11
//}
}
}
usleep(20*1000)
}
}