arkhipenko / esp32-cam-mjpeg-multiclient

ESP32 MJPEG Multiclient Streaming Server
BSD 3-Clause "New" or "Revised" License
228 stars 60 forks source link

Race hazard accessing camera between motion/static stream threads #21

Open fridgemagnet3 opened 7 months ago

fridgemagnet3 commented 7 months ago

There is, I think, a race hazard if you have clients using the MPJEG stream and static picture URL at the same time in that that that it is possible for both threads to be accessing the camera object at the same time. This can then result in bad things happening with regard to the memory buffer pointer returned from the camera class. I can very quickly crash the board under these conditions.

I think you need a semaphore around the camera object accesses - the attached patch seems to work for me.

Cheers,

Jon.

cam_race_hazard.diff.gz

arkhipenko commented 7 months ago

I actually did not care about static picture taking in this code at all. It is kind of a leftover from the example it was built upon. The goal of this POC was to make multi-client video streaming work on VLC, Blynk App video feed and browsers. So yeah, please go ahead and improve! Thank you.