3

我是使用 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)
    }


}
4

1 回答 1

0

首先,UnsafeMutablePointer以这种方式使用初始化器可能会产生意想不到的结果,你不应该这样做:

let data = UnsafeMutablePointer<FOSDEC_DATA>(&fosdecData)

Swift 编译器可能会为 inout 参数准备一个临时区域并传递该区域的地址,该地址在调用后立即释放。不幸的是,此代码在某些情况下可能有效,但您知道在某些情况下有效的代码可能是一个难以修复的错误。


其次,在您的 Objective-C 代码中,您声明dataFOSDEC_DATA*-- 指向FOSDEC_DATA-- 并将其初始化为NULL.

你为什么要初始化你的 Swift 版本的datawith &fosdecData?您的 Objective-C 代码中没有fosdecData

声明的等效 Swift 代码data应该是这样的:

var data: UnsafeMutablePointer<FOSDEC_DATA>? = nil

您应该声明dataUnsafeMutablePointer<FOSDEC_DATA>?-- 指向FOSDEC_DATA-- 并将其初始化为nil.

此外,它必须是var,您FosSdk_GetVideoData会将其重写为指向 的有效指针FOSDEC_DATA


第三,你需要传递UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>!给 的第二个参数FosSdk_GetVideoData,但是&data会生成一个类型的指针UnsafeMutablePointer<UnsafeMutablePointer<FOSDEC_DATA>?>

要进行这种指针类型转换,您需要使用withMemoryRebound<T, Result>(to:capacity:_:). 您需要调用withUnsafeMutablePointer<T, Result>(to:_:)以获取指向data.

let result = withUnsafeMutablePointer(to: &data) {
    pointerToFosdecDataPtr in
    //pointerToFosdecDataPtr: UnsafeMutablePointer<UnsafeMutablePointer<FOSDEC_DATA>?>
    pointerToFosdecDataPtr.withMemoryRebound(to: UnsafeMutablePointer<Int8>?.self, capacity: 1) {
        pointerToInt8Ptr in
        //pointerToInt8Ptr: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>
        //...
    }
}

因此,您的 Swift 代码等同于您的 Objective-C 代码将是这样的:

var data: UnsafeMutablePointer<FOSDEC_DATA>? = nil
var outlen: Int32 = 0

while mHandle != 0 {

    let result = withUnsafeMutablePointer(to: &data) {
        pointerToFosdecDataPtr in
        //pointerToFosdecDataPtr: UnsafeMutablePointer<UnsafeMutablePointer<FOSDEC_DATA>?>
        pointerToFosdecDataPtr.withMemoryRebound(to: UnsafeMutablePointer<Int8>?.self, capacity: 1) {
            pointerToInt8Ptr in
            //pointerToInt8Ptr: UnsafeMutablePointer<UnsafeMutablePointer<Int8>?>
            FosSdk_GetVideoData(mHandle, pointerToInt8Ptr, &outlen, FOSDECTYPE_RGB24)
            //Implicit `return` works here and the result is passed to `result`.
        }
    }
    if result == FOSCMDRET_OK && outlen > 0 {
        if let data = data, data.pointee.type == FOSMEDIATYPE_VIDEO {
            //...
            //self.imageFromAVPicture...
        } else {
        }
    }
    usleep(20*1000)
}

编辑

更新代码以反映当前工作的Objective-C 代码的整体行为。(在 Objective-C 代码中重命名为。mhandlemHandle

于 2017-07-27T00:14:01.797 回答