How to display a PBFrame in iOS - iphone

I am currently working on an app which uses a live stream from an IP camera(PnP IP/Network camera manufactured by V Star). When connected using the Api provided I get the frame o/p as PBFrames. I am having trouble displaying them. anybody who have been through it please help. Thanks in advance
void *thread_ReceiveVideo(void *arg)
{
NSLog(#"[thread_ReceiveVideo] Starting...");
int avIndex = *(int *)arg;
char *buf = malloc(VIDEO_BUF_SIZE);
unsigned int frmNo;
int ret;
FRAMEINFO_t frameInfo;
while (1)
{
ret = avRecvFrameData(avIndex, buf, VIDEO_BUF_SIZE, (char *)&frameInfo, sizeof(FRAMEINFO_t), &frmNo);
if(ret == AV_ER_DATA_NOREADY)
{
usleep(30000);
continue;
}
else if(ret == AV_ER_LOSED_THIS_FRAME)
{
NSLog(#"Lost video frame NO[%d]", frmNo);
continue;
}
else if(ret == AV_ER_INCOMPLETE_FRAME)
{
NSLog(#"Incomplete video frame NO[%d]", frmNo);
NSLog(#" the biffer is : %lu",sizeof(buf));
continue;
}
else if(ret == AV_ER_SESSION_CLOSE_BY_REMOTE)
{
NSLog(#"[thread_ReceiveVideo] AV_ER_SESSION_CLOSE_BY_REMOTE");
break;
}
else if(ret == AV_ER_REMOTE_TIMEOUT_DISCONNECT)
{
NSLog(#"[thread_ReceiveVideo] AV_ER_REMOTE_TIMEOUT_DISCONNECT");
break;
}
else if(ret == IOTC_ER_INVALID_SID)
{
NSLog(#"[thread_ReceiveVideo] Session cant be used anymore");
break;
}
NSLog(#"FRAM INFO FLAG :%hhu ",frameInfo.flags);
if(frameInfo.flags == IPC_FRAME_FLAG_PBFRAME)
{
// got an PBFrame, draw it.
NSLog(#"got a video iframe to display");
// NSString *iFramestring = [NSString stringWithUTF8String:buf];
printf("correclty recieved frame is %s",buf);
//
}
}
free(buf);
NSLog(#"[thread_ReceiveVideo] thread exit");
return 0;
}

Related

Get incoming number from a Linphone call

I am trying to detect the number that is calling me during a Linphone call. I have tried
case LinphoneCallConnected:
NSLog("callStateChanged: LinphoneCallConnected")
NSLog("CALL ID: \(linphone_call_log_get_call_id(linphone_call_get_call_log(linphone_core_get_current_call(lc)))!)")
but that is null. Is there another way?
in my app I take linphoneCore and linphoneCall and after call linphone_call_get_remote_address. Now you have linphoneAddress from which you can extract username linphone_address_get_username.
Full code here:
- (NSString *)userNameFromCurrentCall {
LinphoneCore *lc = [LinphoneManager getLc];
LinphoneCall *currentcall = linphone_core_get_current_call(lc);
if (currentcall != NULL) {
LinphoneAddress const * addr = linphone_call_get_remote_address(currentcall);
if (addr != NULL) {
return [NSString stringWithUTF8String:linphone_address_get_username(addr)];
}
}
return nil;
}
SString *)userNameFromCurrentCall {
LinphoneCore *lc = [LinphoneManager getLc];
LinphoneCall *currentcall = linphone_core_get_current_call(lc);
if (currentcall != NULL) {
LinphoneAddress const * addr = linphone_call_get_remote_address(currentcall);
if (addr != NULL) {
return [NSString stringWithUTF8String:linphone_address_get_username(addr)];
}
}
return nil;

how to get EmbeddedFiles from FileAttachment Annotation in pdf file

I am working on PDF annotation app i added PDF file annotation in iPhone it working fine it annotation also visible any reader but facing one issue How to get EmbeddedFiles from File Attachment Annotation in PDF file whose is created annotation from desktop how can do for this issue
i am using this code for get contents and location of annotation from File Attachment Annotation it working fine
CGPDFDictionaryRef pageDictionary = CGPDFPageGetDictionary(pPage);
// NSLog(#"%#",(NSDictionary*)pageDictionary);
CGPDFArrayRef outputArray;
if(!CGPDFDictionaryGetArray(pageDictionary, "Annots", &outputArray)) {
[pdfAnnots release];
return nil;
}
CGPDFArrayRef rectArray;
if(!CGPDFDictionaryGetArray(annotDict, "Rect", &rectArray)) {
break;
}
int arrayCount = CGPDFArrayGetCount( rectArray );
CGPDFReal coords[4];
for( int k = 0; k < arrayCount; ++k ) {
CGPDFObjectRef rectObj;
if(!CGPDFArrayGetObject(rectArray, k, &rectObj)) {
break;
}
CGPDFReal coord;
if(!CGPDFObjectGetValue(rectObj, kCGPDFObjectTypeReal, &coord)) {
break;
}
coords[k] = coord;
}
CGRect rect = CGRectMake(coords[0],coords[1],coords[2],coords[3]);
NSLog(#"%#",NSStringFromCGRect(rect));
CGPDFDictionaryRef aDict;
if(CGPDFDictionaryGetDictionary(annotDict, "AP", &aDict))
{
CGPDFStreamRef textStringRef33;
if(CGPDFDictionaryGetStream(aDict, "N", &textStringRef33)) {
CGPDFDataFormat *format = NULL;
CFDataRef contdata = CGPDFStreamCopyData( textStringRef33, format );
NSData *data=(NSData*)contdata;
}
please help me
Thank you in advance
PDF embeds is not the same as annotations. Embedded files stored in document section instead of page section.
Annonation can contains embedded file, but mechanism of it's extraction is different.
First, you should see internal structure of annotation object. You can use function like this:
static void op_Applier(const char *key, CGPDFObjectRef value, void *info) {
if (*(int *)info > 6) {
return;
}
const char *valuestr = NULL;
CGPDFObjectType type = CGPDFObjectGetType(value);
int i = 0;
for (i = 0; i < *(int *)info; i++) {
printf("\t");
}
printf("%s", key);
switch (type) {
case kCGPDFObjectTypeBoolean: {
bool b;
CGPDFObjectGetValue(value, type, &b);
printf(" (boolean) : %s\n", (b)?"true":"false");
}
break;
case kCGPDFObjectTypeInteger: {
int64_t i;
CGPDFObjectGetValue(value, type, &i);
printf(" (integer) : %lld\n", i);
}
break;
case kCGPDFObjectTypeReal: {
CGPDFReal f;
CGPDFObjectGetValue(value, type, &f);
printf(" (real) : %f\n", f);
}
break;
case kCGPDFObjectTypeName:{
CGPDFObjectGetValue(value, type, (void *)&valuestr);
printf(" (name) : %s\n", valuestr);
}
break;
case kCGPDFObjectTypeString: {
CGPDFStringRef str;
CGPDFObjectGetValue(value, type, &str);
printf(" (string) : %s\n", CGPDFStringGetBytePtr(str));
}
break;
case kCGPDFObjectTypeArray: {
printf(" (array)\n");
CGPDFArrayRef arr;
CGPDFObjectGetValue(value, type, &arr);
int t = *(int *)info + 1;
int j;
size_t size = CGPDFArrayGetCount(arr);
for (j = 0; j < size; j ++) {
CGPDFObjectRef obj;
CGPDFArrayGetObject(arr, j, &obj);
char buf[4] = {0};
sprintf(buf, "%d", j);
op_Applier(buf, obj, &t);
}
}
break;
case kCGPDFObjectTypeDictionary: {
printf(" (dictionary)\n");
CGPDFDictionaryRef dict;
CGPDFObjectGetValue(value, type, &dict);
int t = *(int *)info + 1;
CGPDFDictionaryApplyFunction(dict, &op_Applier, &t);
}
break;
case kCGPDFObjectTypeStream:
printf(" (stream)\n");
break;
default:
printf(" (NULL)\n");
break;
}
}
int t = 0; op_Applier("MyDictionary", yourAnnotationDictionary, &t);
Second, you should find path to your file (it will be stream object), and move to this object. This can be done with CGPDFDictionary methods.
Third, get NSData from stream, and save it
CGPDFDataFormat format;
CFDataRef data = CGPDFStreamCopyData(stream, &format);
[((__bridge NSData *)data) writeToURL:resultUrl atomically:NO];
CFRelease(data);
P.S. PostScript syntax of PDF annotation can be different. Different libraries use different syntax, and you should implement all possibilities to make app, that works on almost all PDF files. Sample can be found in this file in vfr Reader

Get Wifi Address Problem

I try to get ip address using asyncsocket framework. When do it via ethernet cable the following method works good.
But when try to get ip address using wifi access point it returns nil.
Here is a method:
- (NSData *)wifiAddress
{
// On iPhone, WiFi is always "en0"
NSData *result = nil;
struct ifaddrs *addrs;
const struct ifaddrs *cursor;
if ((getifaddrs(&addrs) == 0))
{
cursor = addrs;
while (cursor != NULL)
{
NSLog(#"cursor->ifa_name = %s", cursor->ifa_name);
if (strcmp(cursor->ifa_name, "en0") == 0)
{
if (cursor->ifa_addr->sa_family == AF_INET)
{
struct sockaddr_in *addr = (struct sockaddr_in *)cursor->ifa_addr;
NSLog(#"cursor->ifa_addr = %s", inet_ntoa(addr->sin_addr));
result = [NSData dataWithBytes:addr length:sizeof(struct sockaddr_in)];
cursor = NULL;
}
else
{
cursor = cursor->ifa_next;
}
}
else
{
cursor = cursor->ifa_next;
}
}
freeifaddrs(addrs);
}
return result;
}
The issue we had was that an exact match on en0 would not always return the wifi address. We have something similar to the following. Hope this helps.
NSString* wifiIp = [NetUtils getLocalAddress:#"en"];
+ (NSString *) getLocalAddress:(NSString*) interface
{
NSString *address = nil;
struct ifaddrs *interfaces = NULL;
struct ifaddrs *temp_addr = NULL;
int success = 0;
success = getifaddrs(&interfaces);
if (success == 0)
{
temp_addr = interfaces;
while(temp_addr != NULL)
{
if(temp_addr->ifa_addr->sa_family == AF_INET)
{
NSRange range = [[NSString stringWithUTF8String:temp_addr->ifa_name] rangeOfString : interface];
if(range.location != NSNotFound)
{
address = [NSString stringWithUTF8String:inet_ntoa(((struct sockaddr_in *)temp_addr->ifa_addr)->sin_addr)];
}
}
temp_addr = temp_addr->ifa_next;
}
}
freeifaddrs(interfaces);
return address;
}

Async CFStream networking with runloop

I am trying to implement async tcp networking with runloop.
currently I manage to connect, but when I try to send something I get that -1 bytes have been written - but CFWriteStreamCopyError returns null.
code sample below, first function connects, second send a simple message.
any help will be appreciated, including random bug spotting (I am new to objective-c and to iphone development in general).
struct header
{
uint32_t length;
uint32_t type;
} header;
- (void) connect
{
NSLog(#"Attempting to (re)connect to %#:%d", m_host, m_port);
while(TRUE)
{
CFHostRef host = CFHostCreateWithName(kCFAllocatorDefault, (CFStringRef)m_host);
if (!host)
{
NSLog(#"Error resolving host %#", m_host);
[NSThread sleepForTimeInterval:5.0];
continue;
}
CFStreamCreatePairWithSocketToCFHost(kCFAllocatorDefault, host , m_port, &m_in, &m_out);
CFRelease(host);
if (!m_in)
{
NSLog(#"Error");
}
CFStreamClientContext context = {0, self,nil,nil,nil};
if (CFReadStreamSetClient(m_in, kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered, networkReadEvent, &context))
{
CFReadStreamScheduleWithRunLoop(m_in, CFRunLoopGetCurrent(),kCFRunLoopCommonModes);
}
if (CFWriteStreamSetClient(m_out, kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered, networkWriteEvent, &context))
{
CFWriteStreamScheduleWithRunLoop(m_out, CFRunLoopGetCurrent(),kCFRunLoopCommonModes);
}
BOOL success = CFReadStreamOpen(m_in);
CFErrorRef error = CFReadStreamCopyError(m_in);
if (!success || (error && CFErrorGetCode(error) != 0))
{
NSLog(#"Connect error %s : %d", CFErrorGetDomain(error), CFErrorGetCode(error));
[NSThread sleepForTimeInterval:5.0];
}
else
{
NSLog(#"Connected");
break;
}
}
[self startSession];
}
- (void) startSession
{
struct header hh;
hh.type = RTR_CREATE_SESSION;
hh.length = 0;
CFIndex res = CFWriteStreamWrite(self.m_out, (const UInt8*)&hh, sizeof(hh));
NSLog(#"Written %d", res);
CFErrorRef error = CFWriteStreamCopyError(self.m_out);
if (error)
{
NSLog(#"Read error %s : %d", CFErrorGetDomain(error), CFErrorGetCode(error));
CFRelease(error);
}
}
figured it out, I forgot to open the write stream as well:
CFWriteStreamOpen(m_out);

Trying to find USB device on iphone with IOKit.framework

I'm working on a project were I need the USB port to communicate with an external device. I have been looking for examples on the net (Apple and /developer/IOKit/usb exemple) and trying some others, but I can't even find the device.
In my code, I'm blocking at the place where the function looks for a next iterator (pointer in fact) with the function getNextIterator; but it never returns a good value, so the code is blocking. By the way, I am using toolchain and added IOKit.framework in my project. All I want right now is to communicate or do like a ping to someone on the USB bus! I'm blocking in FindDevice... I can't manage to enter in the while loop because the variable usbDevice is always = to 0... I have tested my code in a small mac program and it works...
Here is my code :
IOReturn ConfigureDevice(IOUSBDeviceInterface **dev) {
UInt8 numConfig;
IOReturn result;
IOUSBConfigurationDescriptorPtr configDesc;
//Get the number of configurations
result = (*dev)->GetNumberOfConfigurations(dev, &numConfig);
if (!numConfig) {
return -1;
}
// Get the configuration descriptor
result = (*dev)->GetConfigurationDescriptorPtr(dev, 0, &configDesc);
if (result) {
NSLog(#"Couldn't get configuration descriptior for index %d (err=%08x)\n", 0, result);
return -1;
}
#ifdef OSX_DEBUG
NSLog(#"Number of Configurations: %d\n", numConfig);
#endif
// Configure the device
result = (*dev)->SetConfiguration(dev, configDesc->bConfigurationValue);
if (result)
{
NSLog(#"Unable to set configuration to value %d (err=%08x)\n", 0, result);
return -1;
}
return kIOReturnSuccess;
}
IOReturn FindInterfaces(IOUSBDeviceInterface **dev, IOUSBInterfaceInterface ***itf) {
IOReturn kr;
IOUSBFindInterfaceRequest request;
io_iterator_t iterator;
io_service_t usbInterface;
IOUSBInterfaceInterface **intf = NULL;
IOCFPlugInInterface **plugInInterface = NULL;
HRESULT res;
SInt32 score;
UInt8 intfClass;
UInt8 intfSubClass;
UInt8 intfNumEndpoints;
int pipeRef;
CFRunLoopSourceRef runLoopSource;
NSLog(#"Debut FindInterfaces \n");
request.bInterfaceClass = kIOUSBFindInterfaceDontCare;
request.bInterfaceSubClass = kIOUSBFindInterfaceDontCare;
request.bInterfaceProtocol = kIOUSBFindInterfaceDontCare;
request.bAlternateSetting = kIOUSBFindInterfaceDontCare;
kr = (*dev)->CreateInterfaceIterator(dev, &request, &iterator);
usbInterface = IOIteratorNext(iterator);
IOObjectRelease(iterator);
NSLog(#"Interface found.\n");
kr = IOCreatePlugInInterfaceForService(usbInterface, kIOUSBInterfaceUserClientTypeID, kIOCFPlugInInterfaceID, &plugInInterface, &score);
kr = IOObjectRelease(usbInterface); // done with the usbInterface object now that I have the plugin
if ((kIOReturnSuccess != kr) || !plugInInterface)
{
NSLog(#"unable to create a plugin (%08x)\n", kr);
return -1;
}
// I have the interface plugin. I need the interface interface
res = (*plugInInterface)->QueryInterface(plugInInterface, CFUUIDGetUUIDBytes(kIOUSBInterfaceInterfaceID), (LPVOID*) &intf);
(*plugInInterface)->Release(plugInInterface); // done with this
if (res || !intf)
{
NSLog(#"couldn't create an IOUSBInterfaceInterface (%08x)\n", (int) res);
return -1;
}
// Now open the interface. This will cause the pipes to be instantiated that are
// associated with the endpoints defined in the interface descriptor.
kr = (*intf)->USBInterfaceOpen(intf);
if (kIOReturnSuccess != kr)
{
NSLog(#"unable to open interface (%08x)\n", kr);
(void) (*intf)->Release(intf);
return -1;
}
kr = (*intf)->CreateInterfaceAsyncEventSource(intf, &runLoopSource);
if (kIOReturnSuccess != kr)
{
NSLog(#"unable to create async event source (%08x)\n", kr);
(void) (*intf)->USBInterfaceClose(intf);
(void) (*intf)->Release(intf);
return -1;
}
CFRunLoopAddSource(CFRunLoopGetCurrent(), runLoopSource, kCFRunLoopDefaultMode);
if (!intf)
{
NSLog(#"Interface is NULL!\n");
} else
{
*itf = intf;
}
NSLog(#"End of FindInterface \n \n");
return kr;
}
unsigned int FindDevice(void *refCon, io_iterator_t iterator) {
kern_return_t kr;
io_service_t usbDevice;
IOCFPlugInInterface **plugInInterface = NULL;
HRESULT result;
SInt32 score;
UInt16 vendor;
UInt16 product;
UInt16 release;
unsigned int count = 0;
NSLog(#"Searching Device....\n");
while (usbDevice = IOIteratorNext(iterator))
{
// create intermediate plug-in
NSLog(#"Found a device!\n");
kr = IOCreatePlugInInterfaceForService(usbDevice,
kIOUSBDeviceUserClientTypeID,
kIOCFPlugInInterfaceID,
&plugInInterface, &score);
kr = IOObjectRelease(usbDevice);
if ((kIOReturnSuccess != kr) || !plugInInterface) {
NSLog(#"Unable to create a plug-in (%08x)\n", kr);
continue;
}
// Now create the device interface
result = (*plugInInterface)->QueryInterface(plugInInterface,
CFUUIDGetUUIDBytes(kIOUSBDeviceInterfaceID),
(LPVOID)&dev);
// Don't need intermediate Plug-In Interface
(*plugInInterface)->Release(plugInInterface);
if (result || !dev) {
NSLog(#"Couldn't create a device interface (%08x)\n",
(int)result);
continue;
}
// check these values for confirmation
kr = (*dev)->GetDeviceVendor(dev, &vendor);
kr = (*dev)->GetDeviceProduct(dev, &product);
//kr = (*dev)->GetDeviceReleaseNumber(dev, &release);
//if ((vendor != LegoUSBVendorID) || (product != LegoUSBProductID) || (release != LegoUSBRelease)) {
if ((vendor != LegoUSBVendorID) || (product != LegoUSBProductID))
{
NSLog(#"Found unwanted device (vendor = %d != %d, product = %d != %d, release = %d)\n",
vendor, kUSBVendorID, product, LegoUSBProductID, release);
(void) (*dev)->Release(dev);
continue;
}
// Open the device to change its state
kr = (*dev)->USBDeviceOpen(dev);
if (kr == kIOReturnSuccess) {
count++;
} else {
NSLog(#"Unable to open device: %08x\n", kr);
(void) (*dev)->Release(dev);
continue;
}
// Configure device
kr = ConfigureDevice(dev);
if (kr != kIOReturnSuccess) {
NSLog(#"Unable to configure device: %08x\n", kr);
(void) (*dev)->USBDeviceClose(dev);
(void) (*dev)->Release(dev);
continue;
}
break;
}
return count;
}
// USB rcx Init
IOUSBInterfaceInterface** osx_usb_rcx_init (void)
{
CFMutableDictionaryRef matchingDict;
kern_return_t result;
IOUSBInterfaceInterface **intf = NULL;
unsigned int device_count = 0;
// Create master handler
result = IOMasterPort(MACH_PORT_NULL, &gMasterPort);
if (result || !gMasterPort)
{
NSLog(#"ERR: Couldn't create master I/O Kit port(%08x)\n", result);
return NULL;
}
else {
NSLog(#"Created Master Port.\n");
NSLog(#"Master port 0x:08X \n \n", gMasterPort);
}
// Set up the matching dictionary for class IOUSBDevice and its subclasses
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
if (!matchingDict) {
NSLog(#"Couldn't create a USB matching dictionary \n");
mach_port_deallocate(mach_task_self(), gMasterPort);
return NULL;
}
else {
NSLog(#"USB matching dictionary : %08X \n", matchingDict);
}
CFDictionarySetValue(matchingDict, CFSTR(kUSBVendorID),
CFNumberCreate(kCFAllocatorDefault, kCFNumberShortType, &LegoUSBVendorID));
CFDictionarySetValue(matchingDict, CFSTR(kUSBProductID),
CFNumberCreate(kCFAllocatorDefault, kCFNumberShortType, &LegoUSBProductID));
result = IOServiceGetMatchingServices(gMasterPort, matchingDict, &gRawAddedIter);
matchingDict = 0; // this was consumed by the above call
// Iterate over matching devices to access already present devices
NSLog(#"RawAddedIter : 0x:%08X \n", &gRawAddedIter);
device_count = FindDevice(NULL, gRawAddedIter);
if (device_count == 1)
{
result = FindInterfaces(dev, &intf);
if (kIOReturnSuccess != result)
{
NSLog(#"unable to find interfaces on device: %08x\n", result);
(*dev)->USBDeviceClose(dev);
(*dev)->Release(dev);
return NULL;
}
// osx_usb_rcx_wakeup(intf);
return intf;
}
else if (device_count > 1)
{
NSLog(#"too many matching devices (%d) !\n", device_count);
}
else
{
NSLog(#"no matching devices found\n");
}
return NULL;
}
int main(int argc, char *argv[])
{
int returnCode;
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSLog(#"Debut du programme \n \n");
osx_usb_rcx_init();
NSLog(#"Fin du programme \n \n");
return 0;
// returnCode = UIApplicationMain(argc, argv, #"Untitled1App", #"Untitled1App");
// [pool release];
// return returnCode;
}
IOKit is not available for iPhone applications. If you need to connect with external devices from the iPhone you need to sign up for the MFi Program which will provide you with the needed API's and documentation.
besides the appstore rules i dont think u can even touch iokit on iOS without violating the sdk's agreement.