Repository: bluenviron/mediamtx

Issue:
Incompatibility with libcamera 0.1.0 and Raspberry Pi 5
<!--
To increase the chance of your issue getting fixed, open an issue FOR EACH problem. Please do not report multiple problems in a single issue, otherwise they'll probably never get ALL fixed.

Please include all sections of this template into your issue, or it will be automatically closed.
-->

## Which version are you using?

v1.2.0

## Which operating system are you using?

<!-- fill checkboxes with a x. Example: [x] Linux -->

- [ ] Linux amd64 standard
- [ ] Linux amd64 Docker
- [ ] Linux arm64 standard
- [ ] Linux arm64 Docker
- [ ] Linux arm7 standard
- [ ] Linux arm7 Docker
- [x] Linux arm6 standard
- [ ] Linux arm6 Docker
- [ ] Windows amd64 standard
- [ ] Windows amd64 Docker (WSL backend)
- [ ] macOS amd64 standard
- [ ] macOS amd64 Docker
- [ ] Other (please describe)

## Describe the issue

Description
 mediamtx fails to work after `apt-get upgrade`

Libcamera: v0.1.0+52-a858d20b

## Describe how to replicate the issue

<!--
the maintainers must be able to REPLICATE your issue to solve it - therefore, describe in a very detailed way how to replicate it.
-->

sudo apt-get update && sudo apt-get upgrade-y

## Did you attach the server logs?

<!--
Server logs are sometimes useful to identify the issue.
If you think this is the case, set the parameter 'logLevel' to 'debug' and attach the server logs.
-->

~~~
/dev/shm/rtspss-embeddedexe-1698423452247192666: error while loading shared libraries: libcamera.so.0.0: cannot open shared object file: No such file or directory
~~~

yes 

## Did you attach a network dump?

<!--
If the bug arises when using MediaMTX with an external hardware or software, the most helpful content you can provide is a dump of the data exchanged between the server and the target (network dump), that can be generated in this way:
1) Download wireshark (https://www.wireshark.org/)
2) Start capturing on the interface used for exchanging RTSP (if the server and the target software are both installed on your pc, the interface is probably "loopback", otherwise it's the one of your network card)
3) Start the server and replicate the issue
4) Stop capturing, save the result in .pcap format
5) Attach
-->

no


Discussion:
@aler9: Hello, the server is compatible with only a version of libcamera at the time, and the current compatible version is 0.0.5.

It seems like Raspberry Pi maintainers are starting to unroll 0.1.0 on some OS-hardware combinations, but not on everyone.

Anyway, the majority of OSs currently run 0.0.5, so we can't perform the upgrade right now, but i'll keep this issue open in order to monitor the situation.@neutralvibes: I reported a bump in version a while ago that you thankfully were able to resolve quickly. I cannot remember how I previously got the version. Just in case I have somehow provided an incorrect version this time I thought I should provide info on how it was obtained.

~~~bash
libcamera-hello --version
~~~

Reports
~~~bash
libcamera-apps build: 7e4d3d71867f 22-08-2023 (10:57:38)
libcamera build: v0.1.0+52-a858d20b
~~~
Thanks for your help. @aler9: We could quickly solve this issue too, but the problem is that i just checked the latest libcamera version on my Raspberry Pi, and it's still 0.0.5, so a full switch to 0.1.0 would cause problems to most users.@neutralvibes: I will use docker in meantime.@Topper69: Hi,
Raspbian OS has pushed libcamera 0.1.0+99-4a23664b version which brake the compatibility.

Extract of error logs : 
```Nov 07 21:23:08 baby mediamtx[6996]: [0:04:27.916109998] [6996]  INFO Camera camera_manager.cpp:284 libcamera v0.1.0+99-4a23664b
Nov 07 21:23:08 baby mediamtx[6996]: [0:04:27.972403420] [6998]  WARN RPiSdn sdn.cpp:39 Using legacy SDN tuning - please consider moving SDN inside rpi.denoise
Nov 07 21:23:08 baby mediamtx[6996]: [0:04:27.978540965] [6998]  INFO RPI vc4.cpp:444 Registered camera /base/soc/i2c0mux/i2c@1/ov5647@36 to Unicam device /dev/media0 and ISP device /dev/media1
Nov 07 21:23:08 baby mediamtx[6996]: [0:04:27.980189092] [6996] ERROR RPI pipeline_base.cpp:166 Invalid sensor configuration request
Nov 07 21:23:08 baby mediamtx[6996]: [0:04:27.981955922] [6998] ERROR Camera camera.cpp:585 Removing camera while still in use
Nov 07 21:23:08 baby mediamtx[459]: 2023/11/07 21:23:08 ERR [path cam] [RPI Camera source] camera_create(): StreamConfiguration.validate() failed```@aler9: Unfortunately libcamera version is still 0.0.5 on Raspberry Bullseye. If you need a server version that is compatible with libcamera 0.1.0, compile it from source by following instructions in the README, or use the Dockerized version, which is independent from libcamera.@Topper69: I have fix the issue with the following code in camera.cpp line 192:

```
conf->transform = Transform::Identity;
if (params->h_flip) {
    conf->transform = Transform::HFlip; 
}
if (params->v_flip) {
    conf->transform = Transform::VFlip;
}
if  (params->v_flip && params->h_flip) {
     conf->transform = Transform::Rot180Transpose;
}
```@neutralvibes: Bookworm is the latest Raspberry distro. That may explain the issue.@kanehekili: In 1.2.1 I had to create library links in bookworm to get it running:

    /lib/aarch64-linux-gnu # :
     ln -s libcamera.so.0.1 libcamera.so.0.0
     ln -s libcamera-base.so.0.1 libcamera-base.so.0.0
@lolorc: new libcamera changes related to transform pushed to bookworm ;-)
```
~/mediamtx/internal/protocols/rpicamera/exe $ make
g++ -Ofast -Werror -Wall -Wextra -Wno-unused-parameter -Wno-unused-result -std=c++17 $(pkg-config --cflags libcamera) -c camera.cpp -o camera.o
camera.cpp:37:18: error: ‘Transform’ has not been declared in ‘libcamera’
   37 | using libcamera::Transform;
      |                  ^~~~~~~~~
camera.cpp: In function ‘bool camera_create(const parameters_t*, camera_frame_cb, camera_t**)’:
camera.cpp:192:11: error: ‘class libcamera::CameraConfiguration’ has no member named ‘transform’
  192 |     conf->transform = Transform::Identity;
      |           ^~~~~~~~~
camera.cpp:192:23: error: ‘Transform’ has not been declared
  192 |     conf->transform = Transform::Identity;
      |                       ^~~~~~~~~
camera.cpp:194:15: error: ‘class libcamera::CameraConfiguration’ has no member named ‘transform’
  194 |         conf->transform = Transform::HFlip * conf->transform;
      |               ^~~~~~~~~
camera.cpp:194:27: error: ‘Transform’ has not been declared
  194 |         conf->transform = Transform::HFlip * conf->transform;
      |                           ^~~~~~~~~
camera.cpp:194:52: error: ‘class libcamera::CameraConfiguration’ has no member named ‘transform’
  194 |         conf->transform = Transform::HFlip * conf->transform;
      |                                                    ^~~~~~~~~
camera.cpp:197:15: error: ‘class libcamera::CameraConfiguration’ has no member named ‘transform’
  197 |         conf->transform = Transform::VFlip * conf->transform;
      |               ^~~~~~~~~
camera.cpp:197:27: error: ‘Transform’ has not been declared
  197 |         conf->transform = Transform::VFlip * conf->transform;
      |                           ^~~~~~~~~
camera.cpp:197:52: error: ‘class libcamera::CameraConfiguration’ has no member named ‘transform’
  197 |         conf->transform = Transform::VFlip * conf->transform;
      |                                                    ^~~~~~~~~
make: *** [Makefile:46: camera.o] Error 1
```

@suosuo6: raspberry  pi5 bookworm camera v3 docker
 docker run --rm -it --network=host --privileged --tmpfs /dev/shm:exec -v /run/udev:/run/udev:ro -e MTX_PATHS_CAM_SOURCE=rpiCamera bluenviron/mediamtx:latest-rpi
2023/11/28 13:39:58 INF MediaMTX v1.3.1
2023/11/28 13:39:58 INF configuration loaded from /mediamtx.yml
2023/11/28 13:39:58 INF [path cam] [RPI Camera source] started
2023/11/28 13:39:58 INF [RTSP] listener opened on :8554 (TCP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
2023/11/28 13:39:58 INF [RTMP] listener opened on :1935
2023/11/28 13:39:58 INF [HLS] listener opened on :8888
2023/11/28 13:39:58 INF [WebRTC] listener opened on :8889 (HTTP), :8189 (ICE/UDP)
2023/11/28 13:39:58 INF [SRT] listener opened on :8890 (UDP)
[0:32:34.424405642] [13]  INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
2023/11/28 13:39:59 ERR [path cam] [RPI Camera source] camera_create(): selected camera is not available
[0:32:39.569273539] [19]  INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
2023/11/28 13:40:04 ERR [path cam] [RPI Camera source] camera_create(): selected camera is not available@masipcat: After applying the @Topper69 patch to a Raspberry Pi 5 (Bookworm), the `create_camera` function works as expected. However, since the Raspberry Pi 5 does not have a hardware video encoder, the `encoder_create` function fails. This is the log:
```
2023/12/06 19:39:56 INF MediaMTX v0.0.0
2023/12/06 19:39:56 INF configuration loaded from /home/jordi/mediamtx.yml
2023/12/06 19:39:56 INF [path cam] [RPI Camera source] started
2023/12/06 19:39:56 INF [RTSP] listener opened on :8554 (TCP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
2023/12/06 19:39:56 INF [RTMP] listener opened on :1935
2023/12/06 19:39:56 INF [HLS] listener opened on :8888
2023/12/06 19:39:56 INF [SRT] listener opened on :8890 (UDP)
[1:30:45.141358142] [7124]  INFO Camera camera_manager.cpp:284 libcamera v0.1.0+118-563cd78e
[1:30:45.165242066] [7128]  INFO RPI pisp.cpp:653 libpisp version v1.0.2 fa44a258644a 22-11-2023 (21:59:22)
[1:30:45.341024932] [7128]  INFO RPI pisp.cpp:1112 Registered camera /base/axi/pcie@120000/rp1/i2c@88000/imx708@1a to CFE device /dev/media1 and ISP device /dev/media0 using PiSP variant BCM2712_C0
[1:30:45.341676454] [7124]  INFO Camera camera.cpp:1183 configuring streams: (0) 1920x1080-YUV420 (1) 1536x864-BGGR16_PISP_COMP1
[1:30:45.341833770] [7128]  INFO RPI pisp.cpp:1396 Sensor: /base/axi/pcie@120000/rp1/i2c@88000/imx708@1a - Selected sensor format: 1536x864-SBGGR10_1X10 - Selected CFE format: 1536x864-PC1B
2023/12/06 19:39:57 ERR [path cam] [RPI Camera source] encoder_create(): unable to open device
```

@Topper69: > After applying the @Topper69 patch to a Raspberry Pi 5 (Bookworm), the `create_camera` function works as expected. However, since the Raspberry Pi 5 does not have a hardware video encoder, the `encoder_create` function fails. This is the log:
> 
> ```
> 2023/12/06 19:39:56 INF MediaMTX v0.0.0
> 2023/12/06 19:39:56 INF configuration loaded from /home/jordi/mediamtx.yml
> 2023/12/06 19:39:56 INF [path cam] [RPI Camera source] started
> 2023/12/06 19:39:56 INF [RTSP] listener opened on :8554 (TCP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
> 2023/12/06 19:39:56 INF [RTMP] listener opened on :1935
> 2023/12/06 19:39:56 INF [HLS] listener opened on :8888
> 2023/12/06 19:39:56 INF [SRT] listener opened on :8890 (UDP)
> [1:30:45.141358142] [7124]  INFO Camera camera_manager.cpp:284 libcamera v0.1.0+118-563cd78e
> [1:30:45.165242066] [7128]  INFO RPI pisp.cpp:653 libpisp version v1.0.2 fa44a258644a 22-11-2023 (21:59:22)
> [1:30:45.341024932] [7128]  INFO RPI pisp.cpp:1112 Registered camera /base/axi/pcie@120000/rp1/i2c@88000/imx708@1a to CFE device /dev/media1 and ISP device /dev/media0 using PiSP variant BCM2712_C0
> [1:30:45.341676454] [7124]  INFO Camera camera.cpp:1183 configuring streams: (0) 1920x1080-YUV420 (1) 1536x864-BGGR16_PISP_COMP1
> [1:30:45.341833770] [7128]  INFO RPI pisp.cpp:1396 Sensor: /base/axi/pcie@120000/rp1/i2c@88000/imx708@1a - Selected sensor format: 1536x864-SBGGR10_1X10 - Selected CFE format: 1536x864-PC1B
> 2023/12/06 19:39:57 ERR [path cam] [RPI Camera source] encoder_create(): unable to open device
> ```

Have you tried to get the rasp5 camera  videostream from gstreamer (to push the rtmp stream to mediamtx ) instead of the native raspberry pi camera support ?@masipcat: Now I was trying something like this:

```yaml
paths:
  # example:
  cam:
    runOnInit: bash -c 'libcamera-vid -t 0 --codec yuv420 --width 1280 --height 720 --inline --listen -o - | ffmpeg -f rawvideo -pix_fmt yuv420p -s:v 1280x720 -i /dev/stdin -c:v libx264 -preset ultrafast -tune zerolatency -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH'
    runOnInitRestart: yes
```

It works, but I observe 2-3 seconds delay and frequent freezes... Do you think switching to GStreamer would improve performance?

EDIT:

I tried using gstreamer and it's worse than using ffmpeg. Maybe I'm configuring something wrong?

```yaml
    runOnInit: gst-launch-1.0 -v libcamerasrc ! "video/x-raw,width=1280,height=720,framerate=2/1" ! videoconvert ! x264enc speed-preset=ultrafast tune=zerolatency byte-stream=true bitrate=3000 threads=2 ! rtspclientsink location=rtsp://localhost:$RTSP_PORT/$MTX_PATH
```@Topper69: > Now I was trying something like this:
> 
> ```yaml
> paths:
>   # example:
>   cam:
>     runOnInit: bash -c 'libcamera-vid -t 0 --codec yuv420 --width 1280 --height 720 --inline --listen -o - | ffmpeg -f rawvideo -pix_fmt yuv420p -s:v 1280x720 -i /dev/stdin -c:v libx264 -preset ultrafast -tune zerolatency -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH'
>     runOnInitRestart: yes
> ```
> 
> It works, but I observe 2-3 seconds delay and frequent freezes... Do you think switching to GStreamer would improve performance?
> 
> EDIT:
> 
> I tried using gstreamer and it's worse than using ffmpeg. Maybe I'm configuring something wrong?
> 
> ```yaml
>     runOnInit: gst-launch-1.0 -v libcamerasrc ! "video/x-raw,width=1280,height=720,framerate=2/1" ! videoconvert ! x264enc speed-preset=ultrafast tune=zerolatency byte-stream=true bitrate=3000 threads=2 ! rtspclientsink location=rtsp://localhost:$RTSP_PORT/$MTX_PATH
> ```

Worse for the delay ?@masipcat: > Worse for the delay ?

Yes, for the delay and freezes@Topper69: > > Worse for the delay ?
> 
> Yes, for the delay and freezes

Strange.

try to tweak your gstreamer command. Take some inspiration from this one :

https://github.com/bluenviron/mediamtx/discussions/1425#discussion-4795897
@aler9: >  the Raspberry Pi 5 does not have a hardware video encoder

Thanks for reporting this news since it changes a lot of things. Performing a linear bump of libcamera is not enough anymore and a little reasoning is needed before improving the server in order to deal with the issue.

There are two separate problems, a technical one and a legal one.

The technical problem can be solved by embedding a software encoder into the server and using it as failback. Raspberry Pi employees wrote that they recommend performing software encoding on the new Pi, even though it consumes at least an entire core. Let's do like they say, even though it sounds particularly inadequate in the era of 8k videos.

Unfortunately embedding an H264 software encoder has legal implications, since H264 is covered by patents. Hardware encoders don't have this problem since the legal responsibility is on the hardware manufacturers, and that is why, for instance, Chrome supports H265 decoding if and only if there's an available hardware decoder, and the same applies to MediaMTX in case of H264 encoding.

My idea is to change the video codec of the RPI camera from H264 to AV1, which is royalty free, and embed an AV1 software encoder into the server. AV1 is supported by most browsers and by a relevant fraction of mobile devices, although the compatibility level is not the same as the one of H264. I can't imagine any other way.@CVasilakis: > My idea is to change the video codec of the RPI camera from H264 to AV1

I would suggest to maintain the H264 option for backward compatibility with older devices and add option for AV1 for the newer devices.@Tdougherty350: > > the Raspberry Pi 5 does not have a hardware video encoder
> 
> Thanks for reporting this news since it changes a lot of things. Performing a linear bump of libcamera is not enough anymore and a little reasoning is needed before improving the server in order to deal with the issue.
> 
> There are two separate problems, a technical one and a legal one.
> 
> The technical problem can be solved by embedding a software encoder into the server and using it as failback. Raspberry Pi employees wrote that they recommend performing software encoding on the new Pi, even though it consumes at least an entire core. Let's do like they say, even though it sounds particularly inadequate in the era of 8k videos.
> 
> Unfortunately embedding an H264 software encoder has legal implications, since H264 is covered by patents. Hardware encoders don't have this problem since the legal responsibility is on the hardware manufacturers, and that is why, for instance, Chrome supports H265 decoding if and only if there's an available hardware decoder, and the same applies to MediaMTX in case of H264 encoding.
> 
> My idea is to change the video codec of the RPI camera from H264 to AV1, which is royalty free, and embed an AV1 software encoder into the server. AV1 is supported by most browsers and by a relevant fraction of mobile devices, although the compatibility level is not the same as the one of H264. I can't imagine any other way.

Hello, Just my two cents. With the Lite version of Bookworm, the video encoders are not included. You either have to use the full desktop version of rasbian bookworm, or compile rpicam-aps to get the h264 encoders. Reference: https://forums.raspberrypi.com/viewtopic.php?t=361268@IgorWolkov: @masipcat, I tried your solution 

```
paths:
  # example:
  cam:
    runOnInit: bash -c 'libcamera-vid -t 0 --codec yuv420 --width 1280 --height 720 --inline --listen -o - | ffmpeg -f rawvideo -pix_fmt yuv420p -s:v 1280x720 -i /dev/stdin -c:v libx264 -preset ultrafast -tune zerolatency -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH'
    runOnInitRestart: yes
```    
and it works for my Raspberry Pi 5 (Raspberry Pi OS (64-bit)). It takes up to 5 seconds to start the stream, then it streams with a subsecond delay to my laptop over wifi. It even works for two cameras.

My script with tiny improvements:
 ```
paths:
  cam1:
    runOnInit: bash -c 'rpicam-vid -t 0 --camera 0 --nopreview --codec yuv420 --width 1280 --height 720 --inline --listen -o - | ffmpeg -f rawvideo -pix_fmt yuv420p -s:v 1280x720 -i /dev/stdin -c:v libx264 -preset ultrafast -tune zerolatency -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH'
    runOnInitRestart: yes
  cam2:
    runOnInit: bash -c 'rpicam-vid -t 0 --camera 1 --nopreview --codec yuv420 --width 1280 --height 720 --inline --listen -o - | ffmpeg -f rawvideo -pix_fmt yuv420p -s:v 1280x720 -i /dev/stdin -c:v libx264 -preset ultrafast -tune zerolatency -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH'
    runOnInitRestart: yes
```@JohnMcLear: If I run ``apt-get install libcamera0`` on Pi5 it somehow kills my GUI...  I can still ``ssh`` into the pi.

```
pi@raspberrypi:~/Downloads $ ./mediamtx 
2024/01/27 16:14:23 INF MediaMTX v1.5.0
2024/01/27 16:14:23 INF configuration loaded from /home/pi/Downloads/mediamtx.yml
2024/01/27 16:14:23 INF [path cam] [RPI Camera source] started
2024/01/27 16:14:23 INF [RTSP] listener opened on :8554 (TCP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
2024/01/27 16:14:23 INF [RTMP] listener opened on :1935
2024/01/27 16:14:23 INF [HLS] listener opened on :8888
2024/01/27 16:14:23 INF [WebRTC] listener opened on :8889 (HTTP), :8189 (ICE/UDP)
2024/01/27 16:14:23 INF [SRT] listener opened on :8890 (UDP)
[0:04:25.785510356] [6691]  INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
2024/01/27 16:14:23 ERR [path cam] [RPI Camera source] camera_create(): selected camera is not available
```

Before I had ``libcamera-hello --list-cameras`` but obviously this has now gone

```
pi@raspberrypi:~/Downloads $ dmesg  | grep video
[    3.196991] videodev: Linux video capture interface: v2.00
[    3.294898] rpivid 1000800000.codec: Device registered as /dev/video19
[    3.308074] pispbe 1000880000.pisp_be: input device node registered as /dev/video20
[    3.318090] pispbe 1000880000.pisp_be: tdn_input device node registered as /dev/video21
[    3.329244] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-csi2_ch0] node id 0 successfully as /dev/video0
[    3.329463] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-embedded] node id 1 successfully as /dev/video1
[    3.329494] pispbe 1000880000.pisp_be: stitch_input device node registered as /dev/video22
[    3.329574] pispbe 1000880000.pisp_be: hog_output device node registered as /dev/video23
[    3.329633] pispbe 1000880000.pisp_be: output0 device node registered as /dev/video24
[    3.329690] pispbe 1000880000.pisp_be: output1 device node registered as /dev/video25
[    3.329701] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-csi2_ch2] node id 2 successfully as /dev/video2
[    3.329754] pispbe 1000880000.pisp_be: tdn_output device node registered as /dev/video26
[    3.329887] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-csi2_ch3] node id 3 successfully as /dev/video3
[    3.330112] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-fe_image0] node id 4 successfully as /dev/video4
[    3.330153] pispbe 1000880000.pisp_be: stitch_output device node registered as /dev/video27
[    3.330226] pispbe 1000880000.pisp_be: config device node registered as /dev/video28
[    3.330361] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-fe_image1] node id 5 successfully as /dev/video5
[    3.330508] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-fe_stats] node id 6 successfully as /dev/video6
[    3.330621] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-fe_config] node id 7 successfully as /dev/video7
[    3.330624] pispbe 1000880000.pisp_be: input device node registered as /dev/video29
[    3.330688] pispbe 1000880000.pisp_be: tdn_input device node registered as /dev/video30
[    3.331160] pispbe 1000880000.pisp_be: stitch_input device node registered as /dev/video31
[    3.331227] pispbe 1000880000.pisp_be: hog_output device node registered as /dev/video32
[    3.331290] pispbe 1000880000.pisp_be: output0 device node registered as /dev/video33
[    3.331348] pispbe 1000880000.pisp_be: output1 device node registered as /dev/video34
[    3.331405] pispbe 1000880000.pisp_be: tdn_output device node registered as /dev/video35
[    3.331461] pispbe 1000880000.pisp_be: stitch_output device node registered as /dev/video36
[    3.331520] pispbe 1000880000.pisp_be: config device node registered as /dev/video37
```

``mediamtx.yml`` includes
```
paths:
  cam:
    source: rpiCamera
```
as per docs

I have no ``rpicam-vid`` command available

```
pi@raspberrypi:~/Downloads $ cat /etc/os-release 
PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
NAME="Debian GNU/Linux"
VERSION_ID="12"
VERSION="12 (bookworm)"
VERSION_CODENAME=bookworm
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"
pi@raspberrypi:~/Downloads $ 
```@kungeplay: > If I run `apt-get install libcamera0` on Pi5 it somehow kills my GUI... I can still `ssh` into the pi.
> 
> ```
> pi@raspberrypi:~/Downloads $ ./mediamtx 
> 2024/01/27 16:14:23 INF MediaMTX v1.5.0
> 2024/01/27 16:14:23 INF configuration loaded from /home/pi/Downloads/mediamtx.yml
> 2024/01/27 16:14:23 INF [path cam] [RPI Camera source] started
> 2024/01/27 16:14:23 INF [RTSP] listener opened on :8554 (TCP), :8000 (UDP/RTP), :8001 (UDP/RTCP)
> 2024/01/27 16:14:23 INF [RTMP] listener opened on :1935
> 2024/01/27 16:14:23 INF [HLS] listener opened on :8888
> 2024/01/27 16:14:23 INF [WebRTC] listener opened on :8889 (HTTP), :8189 (ICE/UDP)
> 2024/01/27 16:14:23 INF [SRT] listener opened on :8890 (UDP)
> [0:04:25.785510356] [6691]  INFO Camera camera_manager.cpp:297 libcamera v0.0.5+83-bde9b04f
> 2024/01/27 16:14:23 ERR [path cam] [RPI Camera source] camera_create(): selected camera is not available
> ```
> 
> Before I had `libcamera-hello --list-cameras` but obviously this has now gone
> 
> ```
> pi@raspberrypi:~/Downloads $ dmesg  | grep video
> [    3.196991] videodev: Linux video capture interface: v2.00
> [    3.294898] rpivid 1000800000.codec: Device registered as /dev/video19
> [    3.308074] pispbe 1000880000.pisp_be: input device node registered as /dev/video20
> [    3.318090] pispbe 1000880000.pisp_be: tdn_input device node registered as /dev/video21
> [    3.329244] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-csi2_ch0] node id 0 successfully as /dev/video0
> [    3.329463] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-embedded] node id 1 successfully as /dev/video1
> [    3.329494] pispbe 1000880000.pisp_be: stitch_input device node registered as /dev/video22
> [    3.329574] pispbe 1000880000.pisp_be: hog_output device node registered as /dev/video23
> [    3.329633] pispbe 1000880000.pisp_be: output0 device node registered as /dev/video24
> [    3.329690] pispbe 1000880000.pisp_be: output1 device node registered as /dev/video25
> [    3.329701] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-csi2_ch2] node id 2 successfully as /dev/video2
> [    3.329754] pispbe 1000880000.pisp_be: tdn_output device node registered as /dev/video26
> [    3.329887] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-csi2_ch3] node id 3 successfully as /dev/video3
> [    3.330112] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-fe_image0] node id 4 successfully as /dev/video4
> [    3.330153] pispbe 1000880000.pisp_be: stitch_output device node registered as /dev/video27
> [    3.330226] pispbe 1000880000.pisp_be: config device node registered as /dev/video28
> [    3.330361] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-fe_image1] node id 5 successfully as /dev/video5
> [    3.330508] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-fe_stats] node id 6 successfully as /dev/video6
> [    3.330621] rp1-cfe 1f00128000.csi: Registered [rp1-cfe-fe_config] node id 7 successfully as /dev/video7
> [    3.330624] pispbe 1000880000.pisp_be: input device node registered as /dev/video29
> [    3.330688] pispbe 1000880000.pisp_be: tdn_input device node registered as /dev/video30
> [    3.331160] pispbe 1000880000.pisp_be: stitch_input device node registered as /dev/video31
> [    3.331227] pispbe 1000880000.pisp_be: hog_output device node registered as /dev/video32
> [    3.331290] pispbe 1000880000.pisp_be: output0 device node registered as /dev/video33
> [    3.331348] pispbe 1000880000.pisp_be: output1 device node registered as /dev/video34
> [    3.331405] pispbe 1000880000.pisp_be: tdn_output device node registered as /dev/video35
> [    3.331461] pispbe 1000880000.pisp_be: stitch_output device node registered as /dev/video36
> [    3.331520] pispbe 1000880000.pisp_be: config device node registered as /dev/video37
> ```
> 
> `mediamtx.yml` includes
> 
> ```
> paths:
>   cam:
>     source: rpiCamera
> ```
> 
> as per docs
> 
> I have no `rpicam-vid` command available
> 
> ```
> pi@raspberrypi:~/Downloads $ cat /etc/os-release 
> PRETTY_NAME="Debian GNU/Linux 12 (bookworm)"
> NAME="Debian GNU/Linux"
> VERSION_ID="12"
> VERSION="12 (bookworm)"
> VERSION_CODENAME=bookworm
> ID=debian
> HOME_URL="https://www.debian.org/"
> SUPPORT_URL="https://www.debian.org/support"
> BUG_REPORT_URL="https://bugs.debian.org/"
> pi@raspberrypi:~/Downloads $ 
> ```

I met this problem too.@xXAzazelXx: i get the same issue on pi5 with docker image, ERR [path cam] [RPI Camera source] camera_create(): selected camera is not available@Ataman: The current incompatibility issue with libcamera0.1.0 (and 0.2.0 for that matter) also causes problems on other systems if you're trying to use Picamera2 on the current Raspberry Pi OS (Bookworm). 
Currently, installing libcamera0 removes libcamera0.2 which python3-libcamera and picamera2 depends on. As it stands, we can only use MediaMTX **or** the Picamera2 python module on the same system.

```
The following packages will be REMOVED:
  libcamera-ipa libcamera0.2 python3-libcamera
The following NEW packages will be installed:
  libcamera0
```

I was testing a dual setup with MediaMTX for streaming and Picamera2 for taking still images with a Raspberry Pi Zero 2 running Bookworm but I'm putting this on hold for now.@yoyojacky: mine is Raspberry pi OS bootworm 64bit, application is openCV and I've tried to do :
```bash
sudo modprobe bcm2835-v4l2 
```
although I can see /dev/video0 
but exeucte the python code to grap image from camera 0 - /dev/video0, I have nothing on the screen. 
my camera is CSI camera and i can detect it via 
```bash
libcamera-hello --list-cameras 
```
it shows: 
0: imx708 [4608x2592 10-bit RGGB]xxxxxx
![bf17198a82f626a2bfab215c56c5a934](https://github.com/bluenviron/mediamtx/assets/6893075/5e324792-fbfa-4c66-b74c-811a4b8d197d)
and try to use picamera with no luck.
```bash 
import cv2
from picamera.array import PiRGBArray
from picamera import PiCamera
import time

# Initialize the camera
camera = PiCamera()

# Set camera resolution (optional)
camera.resolution = (640, 480)

# Set camera framerate (optional)
camera.framerate = 32

# Initialize the video stream
rawCapture = PiRGBArray(camera, size=(640, 480))

# Allow the camera to warm up
time.sleep(0.1)

# Capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
    # Grab the raw NumPy array representing the image
    image = frame.array

    # Perform image processing with OpenCV (e.g., display the image)
    cv2.imshow("Frame", image)
    
    # Clear the stream in preparation for the next frame
    rawCapture.truncate(0)
    
    # Check for key press to exit the loop
    key = cv2.waitKey(1) & 0xFF
    if key == ord("q"):
        break

# Release the camera resources
cv2.destroyAllWindows()
```
![909b35f3290b52d1447c7fa1ebe2190f](https://github.com/bluenviron/mediamtx/assets/6893075/f3b350b0-4ace-40dc-aa3c-4d6e80167ca3)
![9d6effa1d8424b17a9c02eede62c7930](https://github.com/bluenviron/mediamtx/assets/6893075/44b95be6-b2e5-4fe8-9017-a24914a0b19a)
How can I use CSI camera on Raspberry Pi 5 with OpenCV ? 
@kawogi: > new libcamera changes related to transform pushed to bookworm ;-)
> 
> ```
> ~/mediamtx/internal/protocols/rpicamera/exe $ make
> g++ -Ofast -Werror -Wall -Wextra -Wno-unused-parameter -Wno-unused-result -std=c++17 $(pkg-config --cflags libcamera) -c camera.cpp -o camera.o
> camera.cpp:37:18: error: ‘Transform’ has not been declared in ‘libcamera’
>    37 | using libcamera::Transform;
>       |                  ^~~~~~~~~
> ```

I was able to compile against libcamera 0.2.0 (part of current RasbperryPi OS, Bookworm) by removing these lines.
Would it be possible to provide a compile-time switch to be able to compile against the current version rather than manually patching the code?

It there an estimate on how long mediamtx will be dependent on `0.0.5` or `0.1.0` before migrating to `0.2.0`?@krokosik: Libcamera 0.3.0 has been pushed to bookworm and removed transform altogether. In order to compile from source, you need to use the new orientation enumeration. Replace the lines by @Topper69 with:

```cpp
conf->orientation = Orientation::Rotate0;
if (params->h_flip) {
      conf->orientation = Orientation::Rotate0Mirror;
}
if (params->v_flip) {
      conf->orientation = Orientation::Rotate180Mirror;
}
if  (params->v_flip && params->h_flip) {
      conf->orientation = Orientation::Rotate180;
}
```
And replace `using libcamera::Transform;` with `using libcamera::Orientation;`@krokosik: Regarding the encoder, would it be possible to implement it in the same way as `rpicam-apps` did?
https://github.com/raspberrypi/rpicam-apps/commit/213b166b7e48b3fca0a4aa172baaa773f1dd280f@xXAzazelXx: omg , this worked for me https://gektor650.medium.com/comparing-video-stream-latencies-raspberry-pi-5-camera-v3-a8d5dad2f67b@aler9: MediaMTX now supports serving the Raspberry Pi Camera feed on both Raspberry Pi OS Bookworm and Bullseye (because dependencies have been embedded inside the server) and on the Raspberry Pi 5 (because a H264 software encoder was added as failback).@github-actions: This issue is mentioned in release v1.9.0 🚀
Check out the entire changelog by [clicking here](https://github.com/bluenviron/mediamtx/releases/tag/v1.9.0)@github-actions: This issue is being locked automatically because it has been closed for more than 6 months.
Please open a new issue in case you encounter a similar problem.

Solution patch:
diff --git a/apidocs/openapi.yaml b/apidocs/openapi.yaml
index 4e0ce1b5..94fd864a 100644
--- a/apidocs/openapi.yaml
+++ b/apidocs/openapi.yaml
@@ -402,14 +402,6 @@ components:
           type: string
         rpiCameraFPS:
           type: number
-        rpiCameraIDRPeriod:
-          type: integer
-        rpiCameraBitrate:
-          type: integer
-        rpiCameraProfile:
-          type: string
-        rpiCameraLevel:
-          type: string
         rpiCameraAfMode:
           type: string
         rpiCameraAfRange:
@@ -426,6 +418,16 @@ components:
           type: boolean
         rpiCameraTextOverlay:
           type: string
+        rpiCameraCodec:
+          type: string
+        rpiCameraIDRPeriod:
+          type: integer
+        rpiCameraBitrate:
+          type: integer
+        rpiCameraProfile:
+          type: string
+        rpiCameraLevel:
+          type: string
 
         # Hooks
         runOnInit:
diff --git a/internal/conf/path.go b/internal/conf/path.go
index 242d1309..2cff904e 100644
--- a/internal/conf/path.go
+++ b/internal/conf/path.go
@@ -152,10 +152,6 @@ type Path struct {
 	RPICameraTuningFile        string    `json:"rpiCameraTuningFile"`
 	RPICameraMode              string    `json:"rpiCameraMode"`
 	RPICameraFPS               float64   `json:"rpiCameraFPS"`
-	RPICameraIDRPeriod         int       `json:"rpiCameraIDRPeriod"`
-	RPICameraBitrate           int       `json:"rpiCameraBitrate"`
-	RPICameraProfile           string    `json:"rpiCameraProfile"`
-	RPICameraLevel             string    `json:"rpiCameraLevel"`
 	RPICameraAfMode            string    `json:"rpiCameraAfMode"`
 	RPICameraAfRange           string    `json:"rpiCameraAfRange"`
 	RPICameraAfSpeed           string    `json:"rpiCameraAfSpeed"`
@@ -164,6 +160,11 @@ type Path struct {
 	RPICameraFlickerPeriod     int       `json:"rpiCameraFlickerPeriod"`
 	RPICameraTextOverlayEnable bool      `json:"rpiCameraTextOverlayEnable"`
 	RPICameraTextOverlay       string    `json:"rpiCameraTextOverlay"`
+	RPICameraCodec             string    `json:"rpiCameraCodec"`
+	RPICameraIDRPeriod         int       `json:"rpiCameraIDRPeriod"`
+	RPICameraBitrate           int       `json:"rpiCameraBitrate"`
+	RPICameraProfile           string    `json:"rpiCameraProfile"`
+	RPICameraLevel             string    `json:"rpiCameraLevel"`
 
 	// Hooks
 	RunOnInit                  string         `json:"runOnInit"`
@@ -211,14 +212,15 @@ func (pconf *Path) setDefaults() {
 	pconf.RPICameraDenoise = "off"
 	pconf.RPICameraMetering = "centre"
 	pconf.RPICameraFPS = 30
-	pconf.RPICameraIDRPeriod = 60
-	pconf.RPICameraBitrate = 1000000
-	pconf.RPICameraProfile = "main"
-	pconf.RPICameraLevel = "4.1"
 	pconf.RPICameraAfMode = "continuous"
 	pconf.RPICameraAfRange = "normal"
 	pconf.RPICameraAfSpeed = "normal"
 	pconf.RPICameraTextOverlay = "%Y-%m-%d %H:%M:%S - MediaMTX"
+	pconf.RPICameraCodec = "auto"
+	pconf.RPICameraIDRPeriod = 60
+	pconf.RPICameraBitrate = 1000000
+	pconf.RPICameraProfile = "main"
+	pconf.RPICameraLevel = "4.1"
 
 	// Hooks
 	pconf.RunOnDemandStartTimeout = 10 * StringDuration(time.Second)
@@ -550,6 +552,11 @@ func (pconf *Path) validate(
 	default:
 		return fmt.Errorf("invalid 'rpiCameraAfSpeed' value")
 	}
+	switch pconf.RPICameraCodec {
+	case "auto", "hardwareH264", "softwareH264":
+	default:
+		return fmt.Errorf("invalid 'rpiCameraCodec' value")
+	}
 
 	// Hooks
 
diff --git a/internal/staticsources/rpicamera/mtxrpicamdownloader/VERSION b/internal/staticsources/rpicamera/mtxrpicamdownloader/VERSION
index 1defe531..a4b6ac3d 100644
--- a/internal/staticsources/rpicamera/mtxrpicamdownloader/VERSION
+++ b/internal/staticsources/rpicamera/mtxrpicamdownloader/VERSION
@@ -1 +1 @@
-v2.1.0
+v2.2.0
diff --git a/internal/staticsources/rpicamera/params.go b/internal/staticsources/rpicamera/params.go
index b5460d3e..ec864110 100644
--- a/internal/staticsources/rpicamera/params.go
+++ b/internal/staticsources/rpicamera/params.go
@@ -25,10 +25,6 @@ type params struct {
 	TuningFile        string
 	Mode              string
 	FPS               float64
-	IDRPeriod         int
-	Bitrate           int
-	Profile           string
-	Level             string
 	AfMode            string
 	AfRange           string
 	AfSpeed           string
@@ -37,4 +33,9 @@ type params struct {
 	FlickerPeriod     int
 	TextOverlayEnable bool
 	TextOverlay       string
+	Codec             string
+	IDRPeriod         int
+	Bitrate           int
+	Profile           string
+	Level             string
 }
diff --git a/internal/staticsources/rpicamera/source.go b/internal/staticsources/rpicamera/source.go
index 6f2c72b6..75e70288 100644
--- a/internal/staticsources/rpicamera/source.go
+++ b/internal/staticsources/rpicamera/source.go
@@ -50,10 +50,6 @@ func paramsFromConf(logLevel conf.LogLevel, cnf *conf.Path) params {
 		TuningFile:        cnf.RPICameraTuningFile,
 		Mode:              cnf.RPICameraMode,
 		FPS:               cnf.RPICameraFPS,
-		IDRPeriod:         cnf.RPICameraIDRPeriod,
-		Bitrate:           cnf.RPICameraBitrate,
-		Profile:           cnf.RPICameraProfile,
-		Level:             cnf.RPICameraLevel,
 		AfMode:            cnf.RPICameraAfMode,
 		AfRange:           cnf.RPICameraAfRange,
 		AfSpeed:           cnf.RPICameraAfSpeed,
@@ -62,6 +58,11 @@ func paramsFromConf(logLevel conf.LogLevel, cnf *conf.Path) params {
 		FlickerPeriod:     cnf.RPICameraFlickerPeriod,
 		TextOverlayEnable: cnf.RPICameraTextOverlayEnable,
 		TextOverlay:       cnf.RPICameraTextOverlay,
+		Codec:             cnf.RPICameraCodec,
+		IDRPeriod:         cnf.RPICameraIDRPeriod,
+		Bitrate:           cnf.RPICameraBitrate,
+		Profile:           cnf.RPICameraProfile,
+		Level:             cnf.RPICameraLevel,
 	}
 }
 
diff --git a/mediamtx.yml b/mediamtx.yml
index 9bc18f12..ba713a9d 100644
--- a/mediamtx.yml
+++ b/mediamtx.yml
@@ -508,62 +508,54 @@ pathDefaults:
 
   # ID of the camera
   rpiCameraCamID: 0
-  # width of frames
+  # Width of frames
   rpiCameraWidth: 1920
-  # height of frames
+  # Height of frames
   rpiCameraHeight: 1080
-  # flip horizontally
+  # Flip horizontally
   rpiCameraHFlip: false
-  # flip vertically
+  # Flip vertically
   rpiCameraVFlip: false
-  # brightness [-1, 1]
+  # Brightness [-1, 1]
   rpiCameraBrightness: 0
-  # contrast [0, 16]
+  # Contrast [0, 16]
   rpiCameraContrast: 1
-  # saturation [0, 16]
+  # Saturation [0, 16]
   rpiCameraSaturation: 1
-  # sharpness [0, 16]
+  # Sharpness [0, 16]
   rpiCameraSharpness: 1
-  # exposure mode.
+  # Exposure mode.
   # values: normal, short, long, custom
   rpiCameraExposure: normal
-  # auto-white-balance mode.
+  # Auto-white-balance mode.
   # values: auto, incandescent, tungsten, fluorescent, indoor, daylight, cloudy, custom
   rpiCameraAWB: auto
-  # auto-white-balance fixed gains. This can be used in place of rpiCameraAWB.
+  # Auto-white-balance fixed gains. This can be used in place of rpiCameraAWB.
   # format: [red,blue]
   rpiCameraAWBGains: [0, 0]
-  # denoise operating mode.
+  # Denoise operating mode.
   # values: off, cdn_off, cdn_fast, cdn_hq
   rpiCameraDenoise: "off"
-  # fixed shutter speed, in microseconds.
+  # Fixed shutter speed, in microseconds.
   rpiCameraShutter: 0
-  # metering mode of the AEC/AGC algorithm.
+  # Metering mode of the AEC/AGC algorithm.
   # values: centre, spot, matrix, custom
   rpiCameraMetering: centre
-  # fixed gain
+  # Fixed gain
   rpiCameraGain: 0
   # EV compensation of the image [-10, 10]
   rpiCameraEV: 0
   # Region of interest, in format x,y,width,height
   rpiCameraROI:
-  # whether to enable HDR on Raspberry Camera 3.
+  # Whether to enable HDR on Raspberry Camera 3.
   rpiCameraHDR: false
-  # tuning file
+  # Tuning file
   rpiCameraTuningFile:
-  # sensor mode, in format [width]:[height]:[bit-depth]:[packing]
+  # Sensor mode, in format [width]:[height]:[bit-depth]:[packing]
   # bit-depth and packing are optional.
   rpiCameraMode:
   # frames per second
   rpiCameraFPS: 30
-  # period between IDR frames
-  rpiCameraIDRPeriod: 60
-  # bitrate
-  rpiCameraBitrate: 1000000
-  # H264 profile
-  rpiCameraProfile: main
-  # H264 level
-  rpiCameraLevel: '4.1'
   # Autofocus mode
   # values: auto, manual, continuous
   rpiCameraAfMode: continuous
@@ -584,11 +576,21 @@ pathDefaults:
   rpiCameraAfWindow:
   # Manual flicker correction period, in microseconds.
   rpiCameraFlickerPeriod: 0
-  # enables printing text on each frame.
+  # Enables printing text on each frame.
   rpiCameraTextOverlayEnable: false
-  # text that is printed on each frame.
+  # Text that is printed on each frame.
   # format is the one of the strftime() function.
   rpiCameraTextOverlay: '%Y-%m-%d %H:%M:%S - MediaMTX'
+  # Codec. Available values: auto, hardwareH264, softwareH264
+  rpiCameraCodec: auto
+  # Period between IDR frames
+  rpiCameraIDRPeriod: 60
+  # Bitrate
+  rpiCameraBitrate: 1000000
+  # H264 profile
+  rpiCameraProfile: main
+  # H264 level
+  rpiCameraLevel: '4.1'
 
   ###############################################
   # Default path settings -> Hooks



Test patch:
diff --git a/internal/conf/conf_test.go b/internal/conf/conf_test.go
index f0d1529a..88982317 100644
--- a/internal/conf/conf_test.go
+++ b/internal/conf/conf_test.go
@@ -69,14 +69,15 @@ func TestConfFromFile(t *testing.T) {
 			RPICameraDenoise:           "off",
 			RPICameraMetering:          "centre",
 			RPICameraFPS:               30,
-			RPICameraIDRPeriod:         60,
-			RPICameraBitrate:           1000000,
-			RPICameraProfile:           "main",
-			RPICameraLevel:             "4.1",
 			RPICameraAfMode:            "continuous",
 			RPICameraAfRange:           "normal",
 			RPICameraAfSpeed:           "normal",
 			RPICameraTextOverlay:       "%Y-%m-%d %H:%M:%S - MediaMTX",
+			RPICameraCodec:             "auto",
+			RPICameraIDRPeriod:         60,
+			RPICameraBitrate:           1000000,
+			RPICameraProfile:           "main",
+			RPICameraLevel:             "4.1",
 			RunOnDemandStartTimeout:    5 * StringDuration(time.Second),
 			RunOnDemandCloseAfter:      10 * StringDuration(time.Second),
 		}, pa)