0

I have a piece of code written in C++ which is cross-platform and uses OpenCV.

My C++ code does some processing on an image input.

I want to use iOS's AVFoundation to decode a video and send the data to C++ for processing.

I can find many tutorials that allow me to run C++ code in my App, but what I need is to call an Objective-C function from C++ that will request the next frame of the video.

Please Help.

Oh and I know that OpenCV has a VideoReader class - but I need to have my own implementation.

aaa
  • 601
  • 6
  • 13
  • 1
    Why don't you iterate over the frames of the video in Objective-C and just call the appropriate C++ function on them rather than requesting the next frame from C++? – Mo Abdul-Hameed Dec 01 '17 at 19:16
  • Thanks for the tip, sadly that's not how the code works. The code works by a user making some action, then a C++ function is called and performs some long process (which is cross-platform) then in the middle of a process it needs to decode a video and get process each frame's data. So your suggestion requires me to copy a part of my C++ code to Objective-C, therefore losing it's cross-platform nature. – aaa Dec 01 '17 at 19:31
  • 1
    I understand now. Did you see [this](https://stackoverflow.com/a/8851898/4763963) and [this](https://stackoverflow.com/questions/1061005/calling-objective-c-method-from-c-method)? – Mo Abdul-Hameed Dec 01 '17 at 19:36
  • Have you considered to port the relevant C++ parts to Objective-C++? Method calls should then be easy. If this is not possible: ObjC method calls are simple calls to the ObjcC runtime which is plain C [Documentation](https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtHowMessagingWorks.html). – mschmidt Dec 01 '17 at 23:24

2 Answers2

1

The easiest way is to just change myFile.cpp to myFile.mm. Then the compiler will interpret this as Objective-C++ and you can import AVFoundation into the .mm file and use Objective-C.

dave234
  • 4,793
  • 1
  • 14
  • 29
  • So far this is how I similar to how I solved it, I'll try and post a full answer later :) – aaa Dec 07 '17 at 15:51
0

ObjectiveC message calls are translated by the compiler into calls to the runtime library - for message calls refer to objc_msgSend. You can call this runtime-function by hand from your C++ code.

Objective-C Runtime Programming Guide / Messaging

A further - and possibly easier - way would be to port the C++ parts to Objective-C++.

mschmidt
  • 2,740
  • 4
  • 17
  • 31