Android Tutorial MJPEG / CCTV Server

Status
Not open for further replies.
A few days ago I've helped a developer with implementing a MJPEG client: https://www.b4x.com/android/forum/threads/73702/#content
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:

--myboundary
Content-Type:image/jpeg
Content-Length:xxx

<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.

SS-2016-12-04_15.35.36.jpg


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: https://www.b4x.com/android/forum/threads/mjpeg-cctv-server.73821/
 

Attachments

  • MJPEG_CCTV.zip
    12.6 KB · Views: 2,096
Last edited:

Mikel Huerta

Member
Licensed User
This is how the layout should look:

SS-2017-01-19_08.18.49.png


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.
 

FrankDev

Active Member
Licensed User
hi

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

With
Ip address: 51041
 

FrankDev

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
 

FrankDev

Active Member
Licensed User
hi


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
 

jchal

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
 

moster67

Expert
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...
 

maleche

Member
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!
best,
 

lemonisdead

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)
 

hatzisn

Well-Known Member
Licensed User
Hi Erel,

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

B4X:
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...
 
Status
Not open for further replies.
Top