-
Notifications
You must be signed in to change notification settings - Fork 2k
Explain the new software camera synchronisation feature #4019
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
Changes from 3 commits
753f450
bb1c632
8c05c55
9088335
cf3a28f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,3 +12,8 @@ Raspberry Pi produces several official camera modules, including: | |||||
For more information about camera hardware, see the xref:../accessories/camera.adoc#about-the-camera-modules[camera hardware documentation]. | ||||||
|
||||||
First, xref:../accessories/camera.adoc#install-a-raspberry-pi-camera[install your camera module]. Then, follow the guides in this section to put your camera module to use. | ||||||
|
||||||
[WARNING] | ||||||
==== | ||||||
This guide no longer covers the _legacy camera stack_ which was available in Bullseye and earlier Raspberry Pi OS releases. The legacy camera stack, using applications like `raspivid`, `raspistill` and the original `Picamera` (_not_ `Picamera2`) Python library, has been deprecated for many years, and is now unsupported. If you are using the legacy camera stack, it will only have support for the Camera Module 1, Camera Module 2 and the High Quality Camera, and will never support any newer camera modules. Nothing in this document is applicable to the legacy camera stack. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
==== |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,4 +8,61 @@ | |
|
||
To list all the cameras available on your platform, use the xref:camera_software.adoc#list-cameras[`list-cameras`] option. To choose which camera to use, pass the camera index to the xref:camera_software.adoc#camera[`camera`] option. | ||
|
||
NOTE: `libcamera` does not yet provide stereoscopic camera support. When running two cameras simultaneously, they must be run in separate processes. This means there is no way to synchronise sensor framing or 3A operation between them. As a workaround, you could synchronise the cameras through an external sync signal for the HQ (IMX477) camera, and switch the 3A to manual mode if necessary. | ||
NOTE: `libcamera` does not yet provide stereoscopic camera support. When running two cameras simultaneously, they must be run in separate processes, meaning there is no way to synchronise 3A operation between them. As a workaround, you could synchronise the cameras through an external sync signal for the HQ (IMX477) camera or use the software camera synchronisation support that is described below, switching the 3A to manual mode if necessary. | ||
|
||
==== Software Camera Synchronisation | ||
|
||
Raspberry Pi's _libcamera_ implementation has the ability to synchronise the frames of different cameras using only software. This will cause one camera to adjust it's frame timing so as to coincide as closely as possible with the frames of another camera. No soldering or hardware connections are required, and it will work with all Raspberry Pi's camera modules, and even third party ones so long as their drivers implement frame duration control correctly. | ||
davidplowman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
**How it works** | ||
|
||
The scheme works by designating one camera to be the _server_. The server will broadcast timing messages onto the network at regular intervals, such as once a second. Meanwhile other cameras, known as _clients_, can listen to these messages whereupon they may lengthen or shorten frame times slightly so as to pull them into sync with the server. This process is continual, though after the first adjustment, subsequent adjustments are normally small. | ||
|
||
The client cameras may be attached to the same Raspberry Pi device as the server, or they may be attached to different Raspberry Pis on the same network. The camera model on the clients may match the server, or they may be different. | ||
|
||
Clients and servers need to be set running at the same nominal framerate (such as 30fps). Note that there is no back-channel from the clients back to the server. It is solely the clients' responsibility to be up and running in time to match the server, and the server is completely unaware whether clients have synchronised successfully, or indeed whether there are any clients at all. | ||
|
||
In normal operation, running the same make of camera on the same Raspberry Pi, we would expect the frame start times of the camera images to match within "several tens of microseconds". When the camera models are different this could be significantly larger as the cameras will probably not be able to match framerates exactly and will therefore be continually drifting apart (and brought back together with every timing message). | ||
|
||
When cameras are on different devices, the system clocks should be synchronised using NTP (normally the case by default for Raspberry Pi OS), or if this is insufficiently precise, another protocol like PTP might be used. Any discrepancy between system clocks will feed directly into extra error in frame start times - even though the advertised timestamps on the frames will not tell you. | ||
|
||
**The Server** | ||
|
||
The server, as previously explained, broadcasts timing messages onto the network, by default every second. The server will run for a fixed number of frames, by default 100, after which it will inform the camera application on the device that the "sychronisation point" has been reached. At this moment, the application will start using the frames, so in the case of `rpicam-vid`, they will start being encoded and recorded. Recall that the behaviour and even existence of clients has no bearing on this. | ||
|
||
If required, there can be several servers on the same network so long as they are broadcasting timing messages to different network addresses. Clients, of course, will have to be configured to listen for the correct address. | ||
|
||
**Clients** | ||
|
||
Clients listen out for server timing messages and, when they receive one, will shorten or lengthen a camera frame duration by the required amount so that subsequent frames will start, as far as possible, at the same moment as the server's. | ||
|
||
The clients learn the correct "synchronisation point" from the server's messages, and just like the server, will signal the camera application at the same moment that it should start using the frames. So in the case of `rpicam-vid`, this is once again the moment at which frames will start being recorded. | ||
|
||
Normally it makes sense to start clients _before_ the server, as the clients will simply wait (the "syncrhonisation point" has not been reached) until a server is seen broadcasting onto the network. This obviously avoids timing problems where a server might reach its "synchronisation point" even before all the clients have been started! | ||
davidplowman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
**Usage in `rpicam-vid`** | ||
|
||
We can use software camera synchronisation with `rpicam-vid` to record videos that are sychronised frame-by-frame. We're going to assume we have two cameras attached, and we're going to use camera 0 as the server, and camera 1 as the client. `rpicam-vid` defaults to a fixed 30 frames per second, which will be fine for us. | ||
|
||
First we should start the client: | ||
[source,console] | ||
---- | ||
$ rpicam-vid -n -t 20s --camera 1 --codec libav -o client.mp4 --sync client | ||
---- | ||
|
||
Note the `--sync client` parameter. This will record for 20 seconds in total but note that this _includes_ the time to start the server and achieve synchronisation. So while the start of the recordings, and all the frames, will be synchronised, the end of the recordings is not. | ||
|
||
To start the server: | ||
[source,console] | ||
---- | ||
$ rpicam-vid -n -t 20s --camera 0 --codec libav -o server.mp4 --sync server | ||
---- | ||
|
||
This will run for 20 seconds but with the default settings (100 frames at 30fps) will give clients just over 3 seconds to get synchronised before anything is recorded. So the final video file will contain slightly under 17 seconds of video. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "default settings" -> "default synchronisation settings" ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep. |
||
|
||
The server's broadcast address and port, the frequency of the timing messages and the number of frames to wait for clients to synchronise, can all be changed in the camera tuning file. Clients only pay attention to the broadcast address here which should match the server's; the other information will be ignored. Please refer to the https://datasheets.raspberrypi.com/camera/raspberry-pi-camera-guide.pdf[Raspberry Pi Camera tuning guide] for more information. | ||
davidplowman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
In practical operation there are a few final points to be aware of: | ||
|
||
* The fixed framerate needs to be below the maximum framerate at which the camera can operate (in the camera mode that is being used). This is because the synchronisation algorithm may need to _shorten_ camera frames so that clients can catch up with the server, and this will fail if it is already running as fast as it can. | ||
* Whilst cameras frames should be correctly synchronised, at higher framerates, or depending on system load, it is possible for frames, either on the clients or server, to be dropped. In these cases the frame timestamps will help an application to work out what has happened, though it's usually easier simply to try and avoid frame drops - perhaps by lowering the framerate, increasing the number of buffers being allocated to the camera queues, or reducing system load (see the xref:camera_software.adoc#buffer-count[`--buffer-count` option].) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should "cameras frames" be "camera's frames" or "camera frames"? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Indeed, "camera frames", I think!
davidplowman marked this conversation as resolved.
Show resolved
Hide resolved
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "usually easier simply to try" -> "usually easier to try" ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, will re-word a bit. Maybe "usually simpler to try". |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -132,3 +132,10 @@ Records exactly the specified number of frames. Any non-zero value overrides xre | |||||
|
||||||
Records exactly the specified framerate. Accepts a nonzero integer. | ||||||
|
||||||
==== `low-latency` | ||||||
|
||||||
On a Pi 5, the `--low-latency` option will reduce the encoding latency, which may be beneficial for real-time streaming applications, in return for (slightly) less good coding efficiency (for example, B frames and arithmethic coding will no longer be used). | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
==== `sync` | ||||||
|
||||||
Run the camera in software synchronisation mode, where multiple cameras synchronise frames to the same moment in time. The `sync` mode can be set to either `client` or `server`. For more information, please refer to the detailed explanation of xref:camera_software.adoc#software-camera-synchronisation[how software synchronisation works]. |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -11,20 +11,32 @@ For example, the following command writes a ten-second video to a file named `te | |||||||||||||||||||||||||
$ rpicam-vid -t 10s -o test.h264 | ||||||||||||||||||||||||||
---- | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
You can play the resulting file with VLC and other video players: | ||||||||||||||||||||||||||
You can play the resulting file with ffplay and other video players: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
[source,console] | ||||||||||||||||||||||||||
---- | ||||||||||||||||||||||||||
$ vlc test.h264 | ||||||||||||||||||||||||||
$ ffplay test.h264 | ||||||||||||||||||||||||||
---- | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
[WARNING] | ||||||||||||||||||||||||||
==== | ||||||||||||||||||||||||||
Older versions of vlc used to play H.264 files correctly, but recent versions do not - displaying only a few, or possibly garbled, frames. You should either use a different media player, or save your files in a more widely supported container format - such as MP4 (see below). | ||||||||||||||||||||||||||
davidplowman marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
==== | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
On Raspberry Pi 5, you can output to the MP4 container format directly by specifying the `mp4` file extension for your output file: | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
[source,console] | ||||||||||||||||||||||||||
---- | ||||||||||||||||||||||||||
$ rpicam-vid -t 10s -o test.mp4 | ||||||||||||||||||||||||||
---- | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
On Raspberry Pi 4, or earlier devices, you can save MP4 files using: | ||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
[source,console] | ||||||||||||||||||||||||||
---- | ||||||||||||||||||||||||||
$ rpicam-vid -t 10s --codec libav -o test.mp4 | ||||||||||||||||||||||||||
---- | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
==== Encoders | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
`rpicam-vid` supports motion JPEG as well as both uncompressed and unformatted YUV420: | ||||||||||||||||||||||||||
|
@@ -76,3 +88,11 @@ To enable the `libav` backend, pass `libav` to the xref:camera_software.adoc#cod | |||||||||||||||||||||||||
---- | ||||||||||||||||||||||||||
$ rpicam-vid --codec libav --libav-format avi --libav-audio --output example.avi | ||||||||||||||||||||||||||
---- | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
==== Low latency video with the Pi 5 | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
Pi 5 uses software video encoders. These generally output frames with a longer latency than the old hardware encoders, and this can sometimes be an issue for real-time streaming applications. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
In this case, please add the option `--low-latency` to the `rpicam-vid` command. This will alter certain encoder options to output the encoded frame more quickly. | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
The downside is that coding efficiency is (slightly) less good, and that the processor's multiple cores may be used (slightly) less efficiently. The maximum framerate that can be encoded may be slightly reduced (though it will still easily achieve 1080p30). | ||||||||||||||||||||||||||
Comment on lines
+92
to
+98
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.