saki4510t / UVCCamera

library and sample to access to UVC web camera on non-rooted Android device
3k stars 1.21k forks source link

IFrameCallback but without surface #596

Closed Ivan421399 closed 3 years ago

Ivan421399 commented 3 years ago

Hello, I need to read frames from the usb camera and send them to gstreamer code. I don't need the preview. And because it's a MJPEG HD camera if I add the surface just for the checkbox the app will consume alot of resources for nothing.

Can I get frames without the surface? Or does anyone knows how to modify the C code in order to not decode frames and casting them on the surface?

Thank you.

Ivan421399 commented 3 years ago

Ok, I've resolved the things by myself (as usual).

First and foremost I've hacked the UVCPreview.cpp file to fit my needs: all the conversions were removed and instead of sending the converted raw frame it sends mjpeg frames straight out of the cam. Also the capture frames are not processed. Another important thing is that I changed the DirectBuffer with a simple array of bytes. The reason is according to https://stackoverflow.com/questions/28791827/does-newdirectbytebuffer-create-a-copy-in-native-code the DirectByteBuffer is slower than an array. I'm not 100% sure it is faster in my case but I hope Saki will give it a try. And of course because there is now an array instead of a buffer the argument type in IFrameCallback#onFrame() was changed to byte[].

As a result I'm sending the bytes from callback to gstreamer pipeline (which also a native code in C) through appsrc element and getting silky smooth 25 fps full hd video in the end (with a proper muxer ofc).

I'm attaching the hacked .cpp file to this post if anyone needs it. Just change the extension to ".cpp" and don't forget to change the type of argument in IFrameCallback#onFrame() method to byte[] UVCPreview.txt Just a warning that you won't be able to preview the video on a widget and capture it, I've hacked those things out for additional preformance (and also because I don't need them)