Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add support multiple cameras on same port #258

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dkravetz
Copy link

Simple but working way to add support for multiple cameras; tested on a Raspberry Pi 3 with two different cameras.

You need to pass both cameras to the container via --device and use a comma separated list of devices on the environmental variable CAMERA_DEV=/dev/video0,/dev/video2

This should address the revert of #250

@dkravetz
Copy link
Author

bump, could I get a review?

@Paraphraser
Copy link

I don't have multiple cameras so I can't test this but I'll just mention something that occurred to me on reviewing this file.

I use a local Dockerfile to add some extras to the base image that comes from this repo. One of my changes is to this run file for the mjpg-streamer.

My version is:

#!/usr/bin/with-contenv sh

if [ -n "$MJPEG_STREAMER_INPUT" ]; then
  echo "Deprecation warning: the environment variable '\$MJPEG_STREAMER_INPUT' was renamed to '\$MJPG_STREAMER_INPUT'"

  MJPG_STREAMER_INPUT=$MJPEG_STREAMER_INPUT
fi

if ! expr "$MJPG_STREAMER_INPUT" : ".*\.so.*" > /dev/null; then
  MJPG_STREAMER_INPUT="input_uvc.so $MJPG_STREAMER_INPUT"
fi

# only exec the streamer if the (internal) camera device exists
if [ -e "$CAMERA_DEV" ] ; then
  exec mjpg_streamer \
    -i "/usr/local/lib/mjpg-streamer/$MJPG_STREAMER_INPUT -d $CAMERA_DEV" \
    -o "/usr/local/lib/mjpg-streamer/output_http.so -w /usr/local/share/mjpg-streamer/www -p 8080"
fi

# arriving here means camera device not linked yet - take the service down
s6-svc -d /var/run/s6/services/mjpg-streamer

My goal wasn't to support multiple cameras. My goal was to add support for a single camera that might not always be present. In essence, I have a UDEV rule which:

  1. Adds a sensible human-readable external device name for my 3D printer when it is switched on, or removes it when the printer is switched off; and

  2. Triggers a reaction script inside the container which plays follow-the-leader to:

    • define or remove a matching device name for the printer inside the container; and
    • define or remove the camera device so the camera is only on when the printer is on.

It's getting on for two years since I did this so I don't recall whether /etc/services.d/mjpg-streamer/run is always invoked or is only called if CAMERA_DEV is defined.

In my case, CAMERA_DEV is always defined to be /dev/video0. It's just that whether /dev/video actually exists inside the container or not depends on whether the printer is on or off.

But, what I did notice as I was tinkering to get it working was the absence of /dev/video0 would result in /etc/services.d/mjpg-streamer/run failing (as you would expect) then constantly being restarted, only to fail, over and over. Hence the check for the existence of $CAMERA_DEV, falling through to remove the service if it isn't.

In thinking about your changes, I'm wondering what will happen if one of the cameras in the list isn't present or isn't functioning? My guess is the same kind of infinite-loop pattern I experienced.

Now, sure, if you're using a standard devices: clause (rather than device_cgroup_rules:) then docker-compose will take care of a lot of the heavy lifting in ensuring the external device is present, then linking it to the internal device, in which case this might not matter. This feedback is really just a "what if?" response where I haven't done any actual testing to confirm the behaviour (mainly because I don't have two cameras).

@LongLiveCHIEF
Copy link
Member

Sorry! I was out for a few months during the home buying and reno process. I will take a look at this as soon as I can finish getting my testing station unpacked and a second camera installed on it. Hopefully tonight.

In the meantime, can you add documentation about this support to the README?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants