Writing to ZM shared memory area

Support and queries relating to all previous versions of ZoneMinder
SyRenity
Posts: 301
Joined: Mon Jan 24, 2005 2:43 pm

Writing to ZM shared memory area

Post by SyRenity »

Hi Phil.

I remember that sometime ago you have explained on this forum, how to write to the camera shared memory space, but I couldn't locate it. Could you please point me towards this information, or better yet, give some short explanation of how to do this?
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

It depends what you want to write to the shm with really. If you're happy to do it in perl then if you look in zmtrigger.pl or zmx10.pl you will find examples if you search for shmwrite.

If you want to do it in c or c++ then obviously zm_monitor.cpp is a better example to try and follow.

If you can be more specific I can probably give you a bit more targetted advice.

Phil
SyRenity
Posts: 301
Joined: Mon Jan 24, 2005 2:43 pm

Post by SyRenity »

Hi Phil.

I'm trying to implement an additional source of video input in the ZM. After reviewing the source code, I figured that all of the image data exchange is done via shared memory. Working with shmem is somewhat a new topic for me, but I guess the zm_monitor.cpp will clear the things a bit.

Three questions, if I may:

1) How ZM differs between each monitor's regions in the shmem - via some id?
2) Are these images that I will write to the shmem have to be in some special format, or it can be a plain bitmap?
3) Will the zma and zms be able to continue the process from there, and continue the regular process of analyzing, displaying and saving to disk of the images?

Thanks.
SyRenity
Posts: 301
Joined: Mon Jan 24, 2005 2:43 pm

Post by SyRenity »

After poking some more, in the code, I'm considering now to inherit the Camera class, and implement there the abstract functions, so not to mess with the shmem at all.

Could you please tell me, what format the Camera:PostCapture(Image &image) should return? In the end of the RemoteCamera there is a DecodeJpeg function - does it means that the Monitor expects a simple bitmap array, returned from the PostCapture() function?

Thanks.
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

The images in shared memory are stored as regular bitmaps, not jpegs or any compressed form so you are correct that the monitor expects an uncompressed image. This is what it gets from normal V4L capture but remote cameras usually supply images in jpeg format which needs to be decompressed so that timestamps etc can be overlain. The PostCapture function returns an Image object (see zm_image.h).

I'd be interested to know what video source you are implementing, if you are able to discuss it.

Phil
SyRenity
Posts: 301
Joined: Mon Jan 24, 2005 2:43 pm

Post by SyRenity »

Hi Phil.

Well, I got stuck very fast in this one :).

Do you have any idea, why zmc always throws a SEGFAULT when being debugged, after the first frame was fetched?

Also, could you please tell me, how to enable the debug output? I changed the debug value in the zm.conf to a higher one, but the logs output is still on the basic level.

BTW, check your PM.
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

I don't get a segfault in gdb. Can you do a backtrace (bt) and see why/where it's happening?

To increase debug in zmc either edit zmc.cpp and change the last parameter of zmDbgInit to the level you want (if you want it compiled in), or set the ZM_DBG_LEVEL_zmc environment variable in the shell you are running zmc from. You might also want to set ZM_DBG_LOG_zmc to a log file of your choice, or ZM_DBG_PRINT to 1 to get all debug output via stdout.

Phil
SyRenity
Posts: 301
Joined: Mon Jan 24, 2005 2:43 pm

Post by SyRenity »

Thanks, I will try that - and will submit this SEGFAULT, when I'll reproduce it.
SyRenity
Posts: 301
Joined: Mon Jan 24, 2005 2:43 pm

Post by SyRenity »

Hi again.

Found something that might be interesting - when increasing the log level to include warnings, I get the following one:

06/13/05 15:25:19.692922 zmc_m1[10766].WAR-zm_config.cpp/96 [Invalid parameter 'ZM_DBG_LEVEL' in /etc/zm.conf]

Was this parameter deprecated?

ZM 1.21
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

ZM_DBG_LEVEL should not be present in zm.conf, that should only contain base database definitions. The debug stuff should just be set using setenv or export.

Phil
SyRenity
Posts: 301
Joined: Mon Jan 24, 2005 2:43 pm

Post by SyRenity »

Hi Phil.

I decided to go back to the original idea of using shared memory, as the video source works the best in asynchronous, threaded model (and the Camera() classes work via synchronous http requests).

After reviewing the zm_monitor.cpp again, I noticed that it creates a separate shared memory space for each monitor, which probably means that any daemon (zmc, zms, zma) simply creates his own monitor object, and uses it's functions, so there can be several monitor objects pointing to the same memory space (each for a diffrent daemon).

It's quite different from what I had pictured in my mind (a big pool of shared memory, where all the data exchanged between all the daemons), but much more logical (and OOP :) ).

Phil, am I correct on my understanding of the system? If yes, how then I can write to the monitor's shared memory outside of the monitor, and how I can differentiate between the monitors?

Thanks.
User avatar
krzys31337
Posts: 65
Joined: Wed Jul 06, 2005 7:43 pm
Location: Poland,Warsaw N 52°09'10" E 20°55'09"

Post by krzys31337 »

Guys,

please help, is there any example of READING from zm shared mem? I would like to read even at 1-2 fps from shm and put it onto X-display or FB device...

Any help?
User avatar
zoneminder
Site Admin
Posts: 5215
Joined: Wed Jul 09, 2003 2:07 pm
Location: Bristol, UK
Contact:

Post by zoneminder »

Several of the scripts both read and write from/to shared memory. If you look for shmread and shmwrite you should find the locations. Essentially you just need to work out the offsets to get to the images, the sizes of some of the structures are in the shared memory header already for convenience!

Phil
SyRenity
Posts: 301
Joined: Mon Jan 24, 2005 2:43 pm

Post by SyRenity »

Hi Phil.

I checked the scripts, and think I'm getting the idea. I have 2 questions, thought:

1) Is the initial memory offset for each monitor is calculated as follows: ZM shared key OR monitor ID?

2) Could you please point me, where the sizes of structures are located (or how they are accessed from the code)?

Thanks!
User avatar
krzys31337
Posts: 65
Joined: Wed Jul 06, 2005 7:43 pm
Location: Poland,Warsaw N 52°09'10" E 20°55'09"

Post by krzys31337 »

Just to be sure:

void Monitor::SingleImage( int scale)

Gives out a jpeg content with headers for www browser.
Maybe the "rightway" is to write simple app for X11/linux FrameBuffer based on zms.cpp and try not to GetImage or SingleImage and possiblly avoid jpeg decomposition.
Haven't got that far in zm*cpp files, images in shared mem exist only as coded as jpg's or are they also as some kind of "raw" format?

If it's in shm and "raw" it should be possible to do some basic loop:
1) show image(s) on screen
2) if fps >= 5 do usleep(100)
3) if load average >= 0.90 do usleep(500)

And having that kind of "browser" we will reduce system load by (at least) a factor of 4. (Overhead of httpd, jpeg, filesystems, mozilla, javascript, mpeg/m-jpeg etc...).

Phil, what would you say on that?
--
Best Regards and sorry for my poor english...
Locked