openhab / openhab-addons

Add-ons for openHAB
https://www.openhab.org/
Eclipse Public License 2.0
1.86k stars 3.56k forks source link

[ipcamera] `image` never updated when using ffmpeg channel in combination with `pollImage` #14686

Open J-N-K opened 1 year ago

J-N-K commented 1 year ago

STR:

One of the reasons is that updateImageChannel is set to false when pollImage is set to ON. But this alone does not fix it, at some point it just turns off again.

case CHANNEL_POLL_IMAGE:
                    if (OnOffType.ON.equals(command)) {
                        if (snapshotUri.isEmpty()) {
                            ffmpegSnapshotGeneration = true;
                            setupFfmpegFormat(FFmpegFormat.SNAPSHOT);
                            updateImageChannel = false;
                        } else {
                            updateImageChannel = true;
                            updateSnapshot();// Allows this to change Image FPS on demand
                        }
                    } else {
                        Ffmpeg localSnaps = ffmpegSnapshot;
                        if (localSnaps != null) {
                            localSnaps.stopConverting();
                            ffmpegSnapshotGeneration = false;
                        }
                        updateImageChannel = false;
                    }
                    return;
Skinah commented 1 year ago

From the readme:

pollImage | Switch | This control can be used to manually start and stop using your CPU to create snapshots from a RTSP source. If you have a snapshot URL setup in the binding, only then can this control can be used to update the Image channel.

I agree it is not 100% clear, and also a separate control should be created instead of sharing the switch if people want to do this.

1: A switch to start and stop the CPU load and network traffic of using a RTSP stream to create snapshots. 2: A switch to start and stop the image channel getting updated.

There was a memory leak of some kind occurring when an image item was updated a lot. Have not tested this in a very long time, probably before OH3 so it may be fine to update the image item once a second on a fast system, but it can flood the event bus with the raw image data and I have seen it choke a system up. Data gets sent to the event bus faster than it can get processed and logged etc...

J-N-K commented 1 year ago
UID: ipcamera:generic:95ee9d211a
label: RTSP/HTTP IP Camera
thingTypeUID: ipcamera:generic
configuration:
  mjpegOptions: -q:v 5 -r 2 -vf scale=640:-2 -update 1
  ipAddress: 192.168.0.185
  mjpegUrl: ffmpeg
  updateImageWhen: "1"
  gifPreroll: 0
  ffmpegLocation: /opt/homebrew/bin/ffmpeg
  ipWhitelist: DISABLE
  mp4OutOptions: -c:v copy -c:a copy
  pollTime: 30000
  password: <....>
  port: 80
  snapshotUrl: ffmpeg
  snapshotOptions: -an -vsync vfr -q:v 2 -update 1
  ffmpegInput: rtsp://192.168.0.185:554/stream1
  gifOutOptions: -r 2 -filter_complex
    scale=-2:360:flags=lanczos,setpts=0.5*PTS,split[o1][o2];[o1]palettegen[p];[o2]fifo[o3];[o3][p]paletteuse
  hlsOutOptions: -strict -2 -f lavfi -i aevalsrc=0 -acodec aac -vcodec copy
    -hls_flags delete_segments -hls_time 2 -hls_list_size 4
  username: <....>

There is a snapshotUrl configured, but that get's erased because it is ffmpeg and then it is not working at all.

Skinah commented 1 year ago

ffmpeg is not a URL that counts, it is a keyword that overrides normally for when a camera is ONVIF and the snapshot url is auto found by ONVIF methods. It allows me to test these features by entering in that keyword to force it to ignore the cameras auto found URL.

J-N-K commented 1 year ago

That's very unexpected: I configure something according to the documentation and it only works sometimes. Why does it make a difference if the image is provided by ffmpeg or the camera itself?

Skinah commented 1 year ago

I agree, see my very first reply here.

Skinah commented 5 months ago

@J-N-K are you still wanting this feature? I have cleaned up the other issues for the binding, so looking at this can be next if you're still wanting this. I looked and I could not find the reply from @kaikreuzer where he stated that it was not a good use case to update an image channel once a second with what can be a 2 megabyte or more image. From my testing I agree with this.

  1. It floods the event bus with huge amounts of data.
  2. It floods the openhab cloud with huge amounts of data, and I reported this in an issue report many years back. This could be used back then (perhaps still can) as a form of DoS attack. The cloud did not like it for obvious reasons.
  3. I was also seeing memory leaks when doing this and I was able to prove it was not my code. It appeared to be on the core side and may have just been more data coming in, then what the event bus can handle.

If someone is willing to assist in dealing with these issues, then I am happy to look into it further otherwise I was hoping to remove the image channel. I did remove it once and a user had a good use case for it so it went back in. I may even have limited it to only 1 update every 8 seconds for the two reasons above.

I agree with your thoughts completely that this is not good and needs to be addressed. I will need guidance on if there is a way to do this that is kind to the event bus and cloud.