Android Question MJPEG Decoder artitecture

goldmagnet

Member
Licensed User
Longtime User
Hello

I am trying to write a MJPEG stream receiver/decoder with B4A.

I have code that connects to the cameras, logs in, and recives the stream of data chunks, I am current displaying the data in a label.

The next part "Should" be simple and something I have coded on different platforms before. BUT I can not process the data in B4A. Normally I would use a string (the decoding is all string operations) and in the socket data received event:

Sub TcpStreams_NewData (buffer() As Byte)

BufferString = BufferString & BytesToString(buffer, 0, buffer.Length, "UTF8")

But this generates a huge memory leak, thats because the strings in B4A are imputable so a new string is used each time.

Next I tried using string builder and the append method, which generates the same memory issue, and now I am at a loss as to which structure I should use. This process is going to be run at least 25 times per second (thats how fast the label updates with the data), and perhaps 100 times per second depending on how the data is "chunked". Using a massive byte array, while maintaining a pointer might work but the string conversions would be too slow.

Can anyone help me out with some advice, I am new to B4A ?

Thanks

Huw
 

jazzzzzzz

Active Member
Licensed User
Longtime User
IT WORKS !!

I now have a fully working MJPEG receiver, running at 25 FPS, coded in B4A with no custom Java library.

I have written several stream receivers in Java, but the only way to do it in B4A seems to be to have the data receiver in a service, with the parsing code in an activity/class. I don't know why this happens, but unless you do this you get lots of non specific memory errors, as the title of my post suggests it was all down to the code architecture. The memory errors only surface if your data receiver is running continuously for more than a minute or so (same code as earlier in this thread), put the same code in a service and no issues at all.

Here's it running (test app)


This means we can go ahead and release our Android CCTV client to replace the old one.

A very relived

Huw

Can you share the code?
 
Upvote 0

jazzzzzzz

Active Member
Licensed User
Longtime User
I think he's not active now...Am also eagerly waiting for the library...I found his mjpeg playback in youtube
 
Upvote 0

ciprian

Active Member
Licensed User
Longtime User
It's too bad. I'm not as good in developing, to create a library myself.
This library could be a HIT, cause there are many guys searching for a IP CAM library. Mjpeg or rtsp.
I'm looking to use some axis ip cams. Have allready tried to make it work, but only with 1 image / 5 seconds.
 
Upvote 0

jazzzzzzz

Active Member
Licensed User
Longtime User
If your camera have an rtsp video stream then you can play the stream using android videoview...just search videoview and gave your rtsp stream link as input to videoview...you dont need any library to play rtsp..
 
Upvote 0

goldmagnet

Member
Licensed User
Longtime User
Several people have contacted me on this.

There is no secret, just use the TCP Streams to connect to the device and fill a buffer with data. Then when time allows (when you are not receiving data) parse the buffer reading M JPEG headers and extract the JPEG data and display, then delete this data from the buffer. Thats it really.

I can get 25 fps no issues at all, and its all using standard B4A code, TCP streams and string parsing of the received data.

Huw
 
Upvote 0

goldmagnet

Member
Licensed User
Longtime User
Do a search in the internet for the structure, you basically find the start of Stream marker, read the headers which include a boundary marker, then use the boundary marker to chop up the buffer into jpegs and then display. I did this by having the data in a string format, and then navigated the buffer using string commands. I hope this helps.
 
Upvote 0

aaronk

Well-Known Member
Licensed User
Longtime User
Several people have contacted me on this.

There is no secret, just use the TCP Streams to connect to the device and fill a buffer with data. Then when time allows (when you are not receiving data) parse the buffer reading M JPEG headers and extract the JPEG data and display, then delete this data from the buffer. Thats it really.

I can get 25 fps no issues at all, and its all using standard B4A code, TCP streams and string parsing of the received data.

Huw
Any chance you can upload a small example so it can help me learn how to do it?
I tried and can't seem to work it out.
 
Upvote 0
Top