Android Tutorial MJPEG / CCTV Server

Not open for further replies.
A few days ago I've helped a developer with implementing a MJPEG client:
It was a good experience as while implementing it I've become familiar with the MJPEG protocol.
MJPEG is quite simple. Each frames look like:


<image data>

Note that the end of lines are made of Chr(13) & Chr(10) and not the standard Chr(10) which is the value of CRLF in B4A, B4i and B4J (this is the natural end of line character on those platforms).

This example turns the device into a CCTV camera. You can connect to it with any browser (including mobile browsers) and see the video stream. You can also access it over the internet if the router is configured to allow it.

The nice thing about this example is that it supports multiple clients.


Each connection is managed by a different instance of the "client" class. This makes it simple to support multiple and concurrent clients. AsyncStreams is responsible for the concurrency.

Note that the image sent as-is. The orientation might be wrong.

The frame rate is currently set to 6 frames per second. You can change it by modifying IntervalMS. Set it to 50 (1000 / 50 = 20 fps) and it will look much better.

Test it by running the app on the device and then enter the device ip address in the browser followed by the port (51042).

B4i example is available here:


    12.6 KB · Views: 2,096
Last edited:

Mikel Huerta

Licensed User
This is how the layout should look:


Delete the layout file and create a new one.

Awesome !
Thanks You a lot for teach me howto.
It runs very well, great job.

btw, this is not a true video stream right ? is a high speed static pictures refreshing ?
I down the interval time to 5 and runs nearly to realtime.


Active Member
Licensed User

Can you see the stream with the webview?
I have tested it but no display synonymous no error message

Ip address: 51041


Active Member
Licensed User
no .. cctv is running on a Smartphone
webview on a Amazon fire TV.
on my PC, I can see the stream of Firefox or chrome.
all devices are on the same wireless network


Active Member
Licensed User

I think the Fire TV has no standard browser.
Is MJPEG decoding a thing of the operating system? I thought this might be dependent on the functionality of the webview.

Thanks for the information


Active Member
Licensed User
i have tested the app and it works as far you do a portforword in the router, does anybody has an i dea how this will work over 3g? or 4 g? i canot figure out how can i do a portforward in the mobile phone


Licensed User
It should work with a VPN though - however to enforce the user to set up a VPN is not perhaps the best idea...


Licensed User
Anyway to use this method to send GPS lat/lon from one client (cell phone) to a server cell phone to pass information? I want to make an app that consolidates our Emergency Response and Search and Rescue team's positions and emergency recourses locations.
Basically, each Emergency Response member's Lat/Lon is transmitted via cellular to a 'master' cell phone which plots everyone's position every 5 minutes.
This would really help out!


Well-Known Member
Licensed User
Can I use in version 5.5?, purchased since long time ago and not update yet
If I recall it correctly, it was working with #5.5 but I would suggest you to update to #7.8 because they are lot of improvements and some libraries do require it now (just an advise)


Well-Known Member
Licensed User
Hi Erel,

I tried your example but it won't compile. I get this error:

B4A Version: 7.80
Parsing code.    (0.01s)
Compiling code.    Error
Object reference not set to an instance of an object.

My android.jar is set to android 27.

I don't see any red curled lines in the modules nor any error messages in the logs tab.

Any suggestions?

Thanks in advance...
Not open for further replies.