8

Starting the Raspi Camera is rather trivial:

raspivid -t 10000 -o test.h264

Problem:

There is no way to stop this. This seems really stupid to me as, to the best of my knowledge, there is no camera available for retail which requires the user to specify how many seconds to run for, and then runs for that many seconds before stopping.

Background:

This presents a huge problem in surveillance. I am creating a program where home surveillance is switched on when the user leaves, and then off again when they return. If motion is detected just before the user returns, the camera starts recording for a predetermined period of time (ie. 10 mins), but since the raspivid command is blocking, I get a situation where the user is physically in the house, but the program is in the surveillance state. To get around this, I launch a thread to launch the raspivid command, so now the states are correctly updated based on when the user comes and goes. However, now other states don't work. For example, I can't switch to the TV state as I can not record HD footage and watch it at the same time. Similarly, I can't switch to the Party state (constant video recording of parties) as the camera is already recording.

Potential Solution:

Using code I found on the web (but can't find the link to any more), I found a hack whereby I kill the thread which launched the raspivid command. Unfortunately this is considered blasphemy by SO (see here and here), which I agree with not only because it might corrupt the .h264 files and lead to memory leaks, but also because it is a nightmare for me to code (I have to have locks and semaphores everywhere as I am launching other threads for motion detection as well).

Question:

Is there any way to stop a recording raspi camera? If not, why not?

7

Inspecting the source code to Raspivid reveals that there are two ways to interrupt the capture.

The first method is to send any SIGNAL to the capture process. The signal is processed by the following code in Raspivid:

    /**
     * Handler for sigint signals
     *
     * @param signal_number ID of incoming signal.
     *
     */
    static void signal_handler(int signal_number)
    {
        // Going to abort on all signals
        vcos_log_error("Aborting program\n");

        // TODO : Need to close any open stuff...how?

       exit(255);
    }

This results in the capture process ending execution in 100ms on average.

As noted in the comments to this answer this can be accomplished in Python by using the following code:

    popen = subprocess.Popen ( cmd )
    .
    .
    .
    popen.send_signal ( signal.SIGINT )

Another method would be using user data callback, that has field:

    callback_data.abort = 0;

and you may guess what happens if you set this field to anything except zero =)

If you need more detailed explanation, I'd like to invite you to check the source code.

  • Thanks. However, I am not sure what either of those are. Is that C? I should have mentioned my code is Python. I managed to find a [very painful] way around this by using popen = subprocess.Popen ( cmd ) and popen.send_signal ( signal.SIGINT ) – puk Oct 16 '13 at 4:51
  • well, sending signals is the first approach I wrote about, sorry it was painful =) – lenik Oct 16 '13 at 6:15
  • 1
    usually STOP and CONT signals work ok for this – John La Rooy Oct 16 '13 at 11:03
  • FYI, code has been added to raspivid to allow the use of signals to control the recording of the video. You can now use signals to pause and then resume recording. It is unclear if there's a way to signal exiting the program and closing the output file gracefully. See github.com/raspberrypi/userland/pull/104 for more details on the new options being added to raspivid. – HeatfanJohn Oct 19 '13 at 16:03
  • I made it in node js sending a SIGTERM signal to exit it gracefully. – Goofyahead Jan 13 '14 at 15:02
2

Try using the -k (keypress) option: Cycle between capture and pause on ENTER.

raspivid -t 10000 -o test.h264 -k

After executing this command press 'x' and 'enter' to stop videoing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.