Repository: AlexxIT/go2rtc Branch: master Commit: dc1685e9cf7a Files: 530 Total size: 1.7 MB Directory structure: gitextract_ui7an4hw/ ├── .github/ │ └── workflows/ │ ├── build.yml │ ├── gh-pages.yml │ └── test.yml ├── .gitignore ├── LICENSE ├── README.md ├── docker/ │ ├── Dockerfile │ ├── README.md │ ├── hardware.Dockerfile │ └── rockchip.Dockerfile ├── examples/ │ ├── go2rtc_hass/ │ │ └── main.go │ ├── go2rtc_mjpeg/ │ │ └── main.go │ ├── go2rtc_rtsp/ │ │ └── main.go │ ├── homekit_info/ │ │ └── main.go │ ├── mdns/ │ │ └── main.go │ ├── mod_pinggy/ │ │ ├── go.mod │ │ ├── go.sum │ │ └── main.go │ ├── onvif_client/ │ │ ├── README.md │ │ └── main.go │ ├── rtsp_client/ │ │ └── main.go │ └── tutk_decoder/ │ ├── README.md │ └── main.go ├── go.mod ├── go.sum ├── internal/ │ ├── README.md │ ├── alsa/ │ │ ├── README.md │ │ ├── alsa.go │ │ └── alsa_linux.go │ ├── api/ │ │ ├── README.md │ │ ├── api.go │ │ ├── config.go │ │ ├── static.go │ │ └── ws/ │ │ ├── README.md │ │ └── ws.go │ ├── app/ │ │ ├── README.md │ │ ├── app.go │ │ ├── config.go │ │ ├── log.go │ │ └── storage.go │ ├── bubble/ │ │ ├── README.md │ │ └── bubble.go │ ├── debug/ │ │ ├── README.md │ │ ├── debug.go │ │ └── stack.go │ ├── doorbird/ │ │ ├── README.md │ │ └── doorbird.go │ ├── dvrip/ │ │ ├── README.md │ │ └── dvrip.go │ ├── echo/ │ │ ├── README.md │ │ └── echo.go │ ├── eseecloud/ │ │ ├── README.md │ │ └── eseecloud.go │ ├── exec/ │ │ ├── README.md │ │ └── exec.go │ ├── expr/ │ │ ├── README.md │ │ └── expr.go │ ├── ffmpeg/ │ │ ├── README.md │ │ ├── api.go │ │ ├── device/ │ │ │ ├── README.md │ │ │ ├── device_bsd.go │ │ │ ├── device_darwin.go │ │ │ ├── device_unix.go │ │ │ ├── device_windows.go │ │ │ └── devices.go │ │ ├── ffmpeg.go │ │ ├── ffmpeg_test.go │ │ ├── hardware/ │ │ │ ├── README.md │ │ │ ├── hardware.go │ │ │ ├── hardware_bsd.go │ │ │ ├── hardware_darwin.go │ │ │ ├── hardware_unix.go │ │ │ └── hardware_windows.go │ │ ├── jpeg.go │ │ ├── jpeg_test.go │ │ ├── producer.go │ │ ├── version.go │ │ └── virtual/ │ │ ├── virtual.go │ │ └── virtual_test.go │ ├── flussonic/ │ │ ├── README.md │ │ └── flussonic.go │ ├── gopro/ │ │ ├── README.md │ │ └── gopro.go │ ├── hass/ │ │ ├── README.md │ │ ├── api.go │ │ └── hass.go │ ├── hls/ │ │ ├── README.md │ │ ├── hls.go │ │ ├── session.go │ │ └── ws.go │ ├── homekit/ │ │ ├── README.md │ │ ├── api.go │ │ ├── homekit.go │ │ └── server.go │ ├── http/ │ │ ├── README.md │ │ └── http.go │ ├── isapi/ │ │ ├── README.md │ │ └── init.go │ ├── ivideon/ │ │ ├── README.md │ │ └── ivideon.go │ ├── kasa/ │ │ ├── README.md │ │ └── kasa.go │ ├── mjpeg/ │ │ ├── README.md │ │ └── mjpeg.go │ ├── mp4/ │ │ ├── README.md │ │ ├── mp4.go │ │ └── ws.go │ ├── mpeg/ │ │ ├── README.md │ │ └── mpeg.go │ ├── multitrans/ │ │ ├── README.md │ │ └── multitrans.go │ ├── nest/ │ │ ├── README.md │ │ └── init.go │ ├── ngrok/ │ │ ├── README.md │ │ └── ngrok.go │ ├── onvif/ │ │ ├── README.md │ │ └── onvif.go │ ├── pinggy/ │ │ ├── README.md │ │ └── pinggy.go │ ├── ring/ │ │ ├── README.md │ │ └── ring.go │ ├── roborock/ │ │ ├── README.md │ │ └── roborock.go │ ├── rtmp/ │ │ ├── README.md │ │ └── rtmp.go │ ├── rtsp/ │ │ ├── README.md │ │ └── rtsp.go │ ├── srtp/ │ │ ├── README.md │ │ └── srtp.go │ ├── streams/ │ │ ├── README.md │ │ ├── add_consumer.go │ │ ├── api.go │ │ ├── api_test.go │ │ ├── dot.go │ │ ├── handlers.go │ │ ├── helpers.go │ │ ├── play.go │ │ ├── preload.go │ │ ├── producer.go │ │ ├── publish.go │ │ ├── stream.go │ │ ├── stream_test.go │ │ └── streams.go │ ├── tapo/ │ │ ├── README.md │ │ └── tapo.go │ ├── tuya/ │ │ ├── README.md │ │ └── tuya.go │ ├── v4l2/ │ │ ├── README.md │ │ ├── v4l2.go │ │ └── v4l2_linux.go │ ├── webrtc/ │ │ ├── README.md │ │ ├── candidates.go │ │ ├── client.go │ │ ├── client_creality.go │ │ ├── kinesis.go │ │ ├── milestone.go │ │ ├── openipc.go │ │ ├── server.go │ │ ├── switchbot.go │ │ ├── webrtc.go │ │ └── webrtc_test.go │ ├── webtorrent/ │ │ ├── README.md │ │ ├── init.go │ │ └── tracker.go │ ├── wyoming/ │ │ ├── README.md │ │ └── wyoming.go │ ├── wyze/ │ │ ├── README.md │ │ └── wyze.go │ ├── xiaomi/ │ │ ├── README.md │ │ └── xiaomi.go │ └── yandex/ │ ├── README.md │ ├── goloom.go │ └── yandex.go ├── main.go ├── package.json ├── pkg/ │ ├── README.md │ ├── aac/ │ │ ├── README.md │ │ ├── aac.go │ │ ├── aac_test.go │ │ ├── adts.go │ │ ├── consumer.go │ │ ├── producer.go │ │ ├── rtp.go │ │ └── rtp_test.go │ ├── alsa/ │ │ ├── README.md │ │ ├── capture_linux.go │ │ ├── device/ │ │ │ ├── asound_32bit.go │ │ │ ├── asound_64bit.go │ │ │ ├── asound_arch.c │ │ │ ├── asound_mipsle.go │ │ │ ├── device_linux.go │ │ │ └── ioctl_linux.go │ │ ├── open_linux.go │ │ └── playback_linux.go │ ├── ascii/ │ │ ├── README.md │ │ └── ascii.go │ ├── bits/ │ │ ├── reader.go │ │ └── writer.go │ ├── bubble/ │ │ ├── client.go │ │ └── producer.go │ ├── core/ │ │ ├── README.md │ │ ├── codec.go │ │ ├── connection.go │ │ ├── core.go │ │ ├── core_test.go │ │ ├── helpers.go │ │ ├── listener.go │ │ ├── media.go │ │ ├── media_test.go │ │ ├── node.go │ │ ├── readbuffer.go │ │ ├── readbuffer_test.go │ │ ├── slices.go │ │ ├── track.go │ │ ├── track_test.go │ │ ├── waiter.go │ │ ├── worker.go │ │ └── writebuffer.go │ ├── creds/ │ │ ├── README.md │ │ ├── creds.go │ │ ├── secrets.go │ │ └── secrets_test.go │ ├── debug/ │ │ ├── conn.go │ │ └── debug.go │ ├── doorbird/ │ │ └── backchannel.go │ ├── dvrip/ │ │ ├── backchannel.go │ │ ├── client.go │ │ ├── dvrip.go │ │ └── producer.go │ ├── eseecloud/ │ │ └── eseecloud.go │ ├── expr/ │ │ ├── expr.go │ │ └── expr_test.go │ ├── ffmpeg/ │ │ ├── README.md │ │ └── ffmpeg.go │ ├── flussonic/ │ │ └── flussonic.go │ ├── flv/ │ │ ├── amf/ │ │ │ ├── amf.go │ │ │ └── amf_test.go │ │ ├── consumer.go │ │ ├── flv_test.go │ │ ├── muxer.go │ │ └── producer.go │ ├── gopro/ │ │ ├── discovery.go │ │ └── producer.go │ ├── h264/ │ │ ├── README.md │ │ ├── annexb/ │ │ │ ├── annexb.go │ │ │ └── annexb_test.go │ │ ├── avc.go │ │ ├── avcc.go │ │ ├── h264.go │ │ ├── h264_test.go │ │ ├── mpeg4.go │ │ ├── payloader.go │ │ ├── rtp.go │ │ └── sps.go │ ├── h265/ │ │ ├── README.md │ │ ├── avc.go │ │ ├── avcc.go │ │ ├── h265_test.go │ │ ├── helper.go │ │ ├── mpeg4.go │ │ ├── payloader.go │ │ ├── rtp.go │ │ └── sps.go │ ├── hap/ │ │ ├── README.md │ │ ├── accessory.go │ │ ├── camera/ │ │ │ ├── README.md │ │ │ ├── accessory.go │ │ │ ├── accessory_test.go │ │ │ ├── ch114_supported_video.go │ │ │ ├── ch115_supported_audio.go │ │ │ ├── ch116_supported_rtp.go │ │ │ ├── ch117_selected_stream.go │ │ │ ├── ch118_setup_endpoints.go │ │ │ ├── ch120_streaming_status.go │ │ │ ├── ch130_data_stream_transport.go │ │ │ ├── ch131_data_stream.go │ │ │ ├── ch205.go │ │ │ ├── ch206.go │ │ │ ├── ch207.go │ │ │ ├── ch209.go │ │ │ └── stream.go │ │ ├── chacha20poly1305/ │ │ │ └── chacha20poly1305.go │ │ ├── character.go │ │ ├── client.go │ │ ├── client_http.go │ │ ├── client_pairing.go │ │ ├── conn.go │ │ ├── curve25519/ │ │ │ └── curve25519.go │ │ ├── ed25519/ │ │ │ └── ed25519.go │ │ ├── hds/ │ │ │ ├── hds.go │ │ │ └── hds_test.go │ │ ├── helpers.go │ │ ├── hkdf/ │ │ │ └── hkdf.go │ │ ├── server.go │ │ ├── setup/ │ │ │ ├── setup.go │ │ │ └── setup_test.go │ │ └── tlv8/ │ │ ├── tlv8.go │ │ └── tlv8_test.go │ ├── hass/ │ │ ├── api.go │ │ └── client.go │ ├── hls/ │ │ ├── producer.go │ │ └── reader.go │ ├── homekit/ │ │ ├── consumer.go │ │ ├── helpers.go │ │ ├── log/ │ │ │ └── debug.go │ │ ├── producer.go │ │ ├── proxy.go │ │ └── server.go │ ├── image/ │ │ └── producer.go │ ├── ioctl/ │ │ ├── README.md │ │ ├── ioctl.go │ │ ├── ioctl_be.go │ │ ├── ioctl_le.go │ │ ├── ioctl_linux.go │ │ └── ioctl_test.go │ ├── isapi/ │ │ ├── backchannel.go │ │ └── client.go │ ├── iso/ │ │ ├── atoms.go │ │ ├── codecs.go │ │ ├── iso.go │ │ └── reader.go │ ├── ivideon/ │ │ └── ivideon.go │ ├── kasa/ │ │ └── producer.go │ ├── magic/ │ │ ├── bitstream/ │ │ │ └── producer.go │ │ ├── keyframe.go │ │ ├── mjpeg/ │ │ │ └── producer.go │ │ └── producer.go │ ├── mdns/ │ │ ├── README.md │ │ ├── client.go │ │ ├── mdns_test.go │ │ ├── server.go │ │ ├── syscall.go │ │ ├── syscall_bsd.go │ │ └── syscall_windows.go │ ├── mjpeg/ │ │ ├── README.md │ │ ├── consumer.go │ │ ├── helpers.go │ │ ├── jpeg.go │ │ ├── mjpeg_test.go │ │ ├── rfc2435.go │ │ ├── rtp.go │ │ └── writer.go │ ├── mp4/ │ │ ├── README.md │ │ ├── consumer.go │ │ ├── demuxer.go │ │ ├── helpers.go │ │ ├── keyframe.go │ │ ├── mime.go │ │ └── muxer.go │ ├── mpegts/ │ │ ├── README.md │ │ ├── checksum.go │ │ ├── consumer.go │ │ ├── demuxer.go │ │ ├── muxer.go │ │ ├── opus.go │ │ └── producer.go │ ├── mpjpeg/ │ │ ├── multipart.go │ │ └── producer.go │ ├── mqtt/ │ │ ├── client.go │ │ └── message.go │ ├── multitrans/ │ │ └── client.go │ ├── nest/ │ │ ├── api.go │ │ └── client.go │ ├── ngrok/ │ │ └── ngrok.go │ ├── onvif/ │ │ ├── README.md │ │ ├── client.go │ │ ├── envelope.go │ │ ├── helpers.go │ │ ├── onvif_test.go │ │ └── server.go │ ├── opus/ │ │ ├── README.md │ │ ├── homekit.go │ │ └── opus.go │ ├── pcm/ │ │ ├── backchannel.go │ │ ├── flac.go │ │ ├── handlers.go │ │ ├── pcm.go │ │ ├── pcm_test.go │ │ ├── pcma.go │ │ ├── pcmu.go │ │ ├── producer.go │ │ ├── producer_sync.go │ │ ├── s16le/ │ │ │ └── s16le.go │ │ └── v1/ │ │ ├── pcm.go │ │ └── pcm_test.go │ ├── pinggy/ │ │ └── pinggy.go │ ├── probe/ │ │ └── consumer.go │ ├── ring/ │ │ ├── api.go │ │ ├── client.go │ │ ├── snapshot.go │ │ └── ws.go │ ├── roborock/ │ │ ├── api.go │ │ ├── client.go │ │ ├── iot/ │ │ │ ├── client.go │ │ │ └── crypto.go │ │ └── producer.go │ ├── rtmp/ │ │ ├── README.md │ │ ├── client.go │ │ ├── conn.go │ │ ├── flv.go │ │ └── server.go │ ├── rtsp/ │ │ ├── README.md │ │ ├── client.go │ │ ├── client_test.go │ │ ├── conn.go │ │ ├── consumer.go │ │ ├── helpers.go │ │ ├── producer.go │ │ ├── rtsp_test.go │ │ └── server.go │ ├── shell/ │ │ ├── command.go │ │ ├── procattr.go │ │ ├── procattr_linux.go │ │ ├── shell.go │ │ └── shell_test.go │ ├── srtp/ │ │ ├── server.go │ │ └── session.go │ ├── tapo/ │ │ ├── backchannel.go │ │ ├── client.go │ │ └── producer.go │ ├── tcp/ │ │ ├── auth.go │ │ ├── dial.go │ │ ├── request.go │ │ ├── textproto.go │ │ ├── textproto_test.go │ │ └── websocket/ │ │ ├── client.go │ │ └── dial.go │ ├── tutk/ │ │ ├── codec.go │ │ ├── conn.go │ │ ├── crypto.go │ │ ├── crypto_test.go │ │ ├── dtls/ │ │ │ ├── auth.go │ │ │ ├── cipher.go │ │ │ ├── conn_dtls.go │ │ │ └── dtls.go │ │ ├── frame.go │ │ ├── helpers.go │ │ ├── session0.go │ │ ├── session16.go │ │ └── session25.go │ ├── tuya/ │ │ ├── README.md │ │ ├── client.go │ │ ├── cloud_api.go │ │ ├── helper.go │ │ ├── interface.go │ │ ├── mqtt.go │ │ └── smart_api.go │ ├── v4l2/ │ │ ├── device/ │ │ │ ├── README.md │ │ │ ├── device.go │ │ │ ├── formats.go │ │ │ ├── videodev2_386.go │ │ │ ├── videodev2_arch.c │ │ │ ├── videodev2_arm.go │ │ │ ├── videodev2_mipsle.go │ │ │ └── videodev2_x64.go │ │ └── producer.go │ ├── wav/ │ │ ├── backchannel.go │ │ ├── producer.go │ │ └── wav.go │ ├── webrtc/ │ │ ├── README.md │ │ ├── api.go │ │ ├── client.go │ │ ├── client_test.go │ │ ├── conn.go │ │ ├── consumer.go │ │ ├── helpers.go │ │ ├── producer.go │ │ ├── server.go │ │ ├── track.go │ │ └── webrtc_test.go │ ├── webtorrent/ │ │ ├── client.go │ │ ├── crypto.go │ │ └── server.go │ ├── wyoming/ │ │ ├── README.md │ │ ├── api.go │ │ ├── backchannel.go │ │ ├── expr.go │ │ ├── mic.go │ │ ├── producer.go │ │ ├── satellite.go │ │ ├── snd.go │ │ ├── wakeword.go │ │ └── wyoming.go │ ├── wyze/ │ │ ├── backchannel.go │ │ ├── client.go │ │ ├── cloud.go │ │ └── producer.go │ ├── xiaomi/ │ │ ├── cloud.go │ │ ├── crypto/ │ │ │ └── crypto.go │ │ ├── legacy/ │ │ │ ├── client.go │ │ │ └── producer.go │ │ ├── miss/ │ │ │ ├── backchannel.go │ │ │ ├── client.go │ │ │ ├── cs2/ │ │ │ │ └── conn.go │ │ │ └── producer.go │ │ └── producer.go │ ├── xnet/ │ │ ├── net.go │ │ └── tls/ │ │ └── tls.go │ ├── y4m/ │ │ ├── README.md │ │ ├── consumer.go │ │ ├── producer.go │ │ └── y4m.go │ ├── yaml/ │ │ ├── yaml.go │ │ └── yaml_test.go │ └── yandex/ │ └── session.go ├── scripts/ │ ├── README.md │ ├── build.cmd │ └── build.sh ├── website/ │ ├── .vitepress/ │ │ └── config.js │ ├── README.md │ ├── api/ │ │ ├── index.html │ │ └── openapi.yaml │ ├── manifest.json │ └── webtorrent/ │ └── index.html └── www/ ├── README.md ├── add.html ├── config.html ├── hls.html ├── index.html ├── links.html ├── log.html ├── main.js ├── net.html ├── schema.json ├── static.go ├── stream.html ├── video-rtc.js ├── video-stream.js ├── webrtc-sync.html └── webrtc.html ================================================ FILE CONTENTS ================================================ ================================================ FILE: .github/workflows/build.yml ================================================ name: Build and Push on: workflow_dispatch: push: branches: - 'master' tags: - 'v*' jobs: build-binaries: name: Build binaries runs-on: ubuntu-latest env: { CGO_ENABLED: 0 } steps: - name: Checkout uses: actions/checkout@v4 - name: Setup Go uses: actions/setup-go@v5 with: { go-version: '1.25' } - name: Build go2rtc_win64 env: { GOOS: windows, GOARCH: amd64 } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_win64 uses: actions/upload-artifact@v4 with: { name: go2rtc_win64, path: go2rtc.exe } - name: Build go2rtc_win32 env: { GOOS: windows, GOARCH: 386 } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_win32 uses: actions/upload-artifact@v4 with: { name: go2rtc_win32, path: go2rtc.exe } - name: Build go2rtc_win_arm64 env: { GOOS: windows, GOARCH: arm64 } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_win_arm64 uses: actions/upload-artifact@v4 with: { name: go2rtc_win_arm64, path: go2rtc.exe } - name: Build go2rtc_linux_amd64 env: { GOOS: linux, GOARCH: amd64 } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_linux_amd64 uses: actions/upload-artifact@v4 with: { name: go2rtc_linux_amd64, path: go2rtc } - name: Build go2rtc_linux_i386 env: { GOOS: linux, GOARCH: 386 } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_linux_i386 uses: actions/upload-artifact@v4 with: { name: go2rtc_linux_i386, path: go2rtc } - name: Build go2rtc_linux_arm64 env: { GOOS: linux, GOARCH: arm64 } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_linux_arm64 uses: actions/upload-artifact@v4 with: { name: go2rtc_linux_arm64, path: go2rtc } - name: Build go2rtc_linux_arm env: { GOOS: linux, GOARCH: arm, GOARM: 7 } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_linux_arm uses: actions/upload-artifact@v4 with: { name: go2rtc_linux_arm, path: go2rtc } - name: Build go2rtc_linux_armv6 env: { GOOS: linux, GOARCH: arm, GOARM: 6 } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_linux_armv6 uses: actions/upload-artifact@v4 with: { name: go2rtc_linux_armv6, path: go2rtc } - name: Build go2rtc_linux_mipsel env: { GOOS: linux, GOARCH: mipsle } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_linux_mipsel uses: actions/upload-artifact@v4 with: { name: go2rtc_linux_mipsel, path: go2rtc } - name: Build go2rtc_mac_amd64 env: { GOOS: darwin, GOARCH: amd64 } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_mac_amd64 uses: actions/upload-artifact@v4 with: { name: go2rtc_mac_amd64, path: go2rtc } - name: Build go2rtc_mac_arm64 env: { GOOS: darwin, GOARCH: arm64 } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_mac_arm64 uses: actions/upload-artifact@v4 with: { name: go2rtc_mac_arm64, path: go2rtc } - name: Build go2rtc_freebsd_amd64 env: { GOOS: freebsd, GOARCH: amd64 } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_freebsd_amd64 uses: actions/upload-artifact@v4 with: { name: go2rtc_freebsd_amd64, path: go2rtc } - name: Build go2rtc_freebsd_arm64 env: { GOOS: freebsd, GOARCH: arm64 } run: go build -ldflags "-s -w" -trimpath - name: Upload go2rtc_freebsd_arm64 uses: actions/upload-artifact@v4 with: { name: go2rtc_freebsd_arm64, path: go2rtc } docker-master: name: Build docker master runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - name: Docker meta id: meta uses: docker/metadata-action@v5 with: images: | name=${{ github.repository }},enable=${{ github.event.repository.fork == false }} ghcr.io/${{ github.repository }} tags: | type=ref,event=branch type=semver,pattern={{version}},enable=false type=match,pattern=v(.*),group=1 - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Login to DockerHub if: github.event_name == 'push' && github.event.repository.fork == false uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Login to GitHub Container Registry if: github.event_name == 'push' uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build and push uses: docker/build-push-action@v5 with: context: . file: docker/Dockerfile platforms: | linux/amd64 linux/386 linux/arm/v6 linux/arm/v7 linux/arm64/v8 push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max docker-hardware: name: Build docker hardware runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - name: Docker meta id: meta-hw uses: docker/metadata-action@v5 with: images: | name=${{ github.repository }},enable=${{ github.event.repository.fork == false }} ghcr.io/${{ github.repository }} flavor: | suffix=-hardware,onlatest=true latest=auto tags: | type=ref,event=branch type=semver,pattern={{version}},enable=false type=match,pattern=v(.*),group=1 - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Login to DockerHub if: github.event_name == 'push' && github.event.repository.fork == false uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Login to GitHub Container Registry if: github.event_name == 'push' uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build and push uses: docker/build-push-action@v5 with: context: . file: docker/hardware.Dockerfile platforms: linux/amd64 push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta-hw.outputs.tags }} labels: ${{ steps.meta-hw.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max docker-rockchip: name: Build docker rockchip runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - name: Docker meta id: meta-rk uses: docker/metadata-action@v5 with: images: | name=${{ github.repository }},enable=${{ github.event.repository.fork == false }} ghcr.io/${{ github.repository }} flavor: | suffix=-rockchip,onlatest=true latest=auto tags: | type=ref,event=branch type=semver,pattern={{version}},enable=false type=match,pattern=v(.*),group=1 - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Login to DockerHub if: github.event_name == 'push' && github.event.repository.fork == false uses: docker/login-action@v3 with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - name: Login to GitHub Container Registry if: github.event_name == 'push' uses: docker/login-action@v3 with: registry: ghcr.io username: ${{ github.actor }} password: ${{ secrets.GITHUB_TOKEN }} - name: Build and push uses: docker/build-push-action@v5 with: context: . file: docker/rockchip.Dockerfile platforms: linux/arm64 push: ${{ github.event_name != 'pull_request' }} tags: ${{ steps.meta-rk.outputs.tags }} labels: ${{ steps.meta-rk.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max ================================================ FILE: .github/workflows/gh-pages.yml ================================================ # Simple workflow for deploying static content to GitHub Pages name: Deploy static content to Pages on: # Allows you to run this workflow manually from the Actions tab workflow_dispatch: # Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages permissions: contents: read pages: write id-token: write # Allow one concurrent deployment concurrency: group: "pages" cancel-in-progress: true jobs: build: runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v5 - name: Setup Node uses: actions/setup-node@v6 with: node-version: 24 package-manager-cache: false - name: Install dependencies run: npm install --no-package-lock - name: Build docs run: npm run docs:build - name: Copy docs into website run: rsync -a --exclude '.vitepress/' --exclude 'README.md' website/ website/.vitepress/dist/ - name: Setup Pages uses: actions/configure-pages@v5 - name: Upload artifact uses: actions/upload-pages-artifact@v3 with: path: website/.vitepress/dist # Single deploy job since we're just deploying deploy: environment: name: github-pages url: ${{ steps.deployment.outputs.page_url }} runs-on: ubuntu-latest needs: build steps: - name: Deploy to GitHub Pages id: deployment uses: actions/deploy-pages@v4 ================================================ FILE: .github/workflows/test.yml ================================================ name: Test Build and Run on: # push: # branches: # - '*' # pull_request: # merge_group: workflow_dispatch: jobs: build-test: strategy: matrix: os: [windows-latest, ubuntu-latest, macos-latest] arch: [amd64, arm64] runs-on: ${{ matrix.os }} continue-on-error: true env: GOARCH: ${{ matrix.arch }} steps: - name: Checkout uses: actions/checkout@v4 - name: Setup Go uses: actions/setup-go@v5 with: go-version: '1.24' - name: Build Go binary run: go build -ldflags "-s -w" -trimpath -o ./go2rtc - name: Test Go binary on linux if: matrix.os == 'ubuntu-latest' run: | if [ "${{ matrix.arch }}" = "amd64" ]; then ./go2rtc -version else sudo apt-get update && sudo apt-get install -y qemu-user-static sudo cp /usr/bin/qemu-aarch64-static . sudo chown $USER:$USER ./qemu-aarch64-static qemu-aarch64-static ./go2rtc -version fi - name: Test Go binary on macos if: matrix.os == 'macos-latest' run: | if [ "${{ matrix.arch }}" = "amd64" ]; then ./go2rtc -version else echo "ARM64 architecture is not yet supported on macOS" fi - name: Test Go binary on windows if: matrix.os == 'windows-latest' run: | if ("${{ matrix.arch }}" -eq "amd64") { .\go2rtc* -version } else { Write-Host "ARM64 architecture is not yet supported on Windows" } docker-test: strategy: matrix: platform: - amd64 - "386" - arm/v7 - arm64/v8 continue-on-error: true runs-on: ubuntu-latest steps: - name: Checkout uses: actions/checkout@v4 - name: Set up QEMU uses: docker/setup-qemu-action@v3 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 - name: Build and push uses: docker/build-push-action@v5 with: context: . file: docker/Dockerfile platforms: linux/${{ matrix.platform }} push: false load: true tags: go2rtc-${{ matrix.platform }} - name: test run run: | docker run --platform=linux/${{ matrix.platform }} --rm go2rtc-${{ matrix.platform }} go2rtc -version - name: Build and push Hardware if: matrix.platform == 'amd64' uses: docker/build-push-action@v5 with: context: . file: docker/hardware.Dockerfile platforms: linux/amd64 push: false load: true tags: go2rtc-${{ matrix.platform }}-hardware - name: test run if: matrix.platform == 'amd64' run: | docker run --platform=linux/${{ matrix.platform }} --rm go2rtc-${{ matrix.platform }}-hardware go2rtc -version ================================================ FILE: .gitignore ================================================ .idea/ .tmp/ go2rtc.yaml go2rtc.json go2rtc_freebsd* go2rtc_linux* go2rtc_mac* go2rtc_win* /go2rtc /go2rtc.exe 0_test.go .DS_Store website/.vitepress/cache website/.vitepress/dist node_modules package-lock.json CLAUDE.md ================================================ FILE: LICENSE ================================================ MIT License Copyright (c) 2022 Alexey Khit Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. ================================================ FILE: README.md ================================================

go2rtc - GitHub

go2rtc - GitHub Stars go2rtc - Docker Pulls go2rtc - GitHub Downloads

go2rtc - Trendshift

Ultimate camera streaming application with support for dozens formats and protocols. - zero-dependency [small app](#go2rtc-binary) for all OS (Windows, macOS, Linux, FreeBSD) - zero-delay for many [supported protocols](#codecs-madness) (lowest possible streaming latency) - [streaming input](#streaming-input) from dozens formats and protocols - [streaming output](#streaming-output) in all popular formats - [streaming ingest](#streaming-ingest) in a number of popular formats - [publish](#publish-stream) any source to popular streaming services (YouTube, Telegram) - on-the-fly transcoding only if necessary via [FFmpeg](internal/ffmpeg/README.md) - [two-way audio](#two-way-audio) support for many formats - [streaming audio](#stream-to-camera) to all cameras with [two-way audio](#two-way-audio) support - mixing tracks from different sources to single stream - [auto-match](www/README.md#javascript-api) client-supported streaming formats and codecs - [streaming stats](#streaming-stats) for all active connections - can be [integrated to any project](#projects-using-go2rtc) or be used as [standalone app](#go2rtc-binary) #### Inspired by - series of streaming projects from [@deepch](https://github.com/deepch) - [webrtc](https://github.com/pion/webrtc) go library and whole [@pion](https://github.com/pion) team - [rtsp-simple-server](https://github.com/aler9/rtsp-simple-server) idea from [@aler9](https://github.com/aler9) - [GStreamer](https://gstreamer.freedesktop.org/) framework pipeline idea - [MediaSoup](https://mediasoup.org/) framework routing idea - HomeKit Accessory Protocol from [@brutella](https://github.com/brutella/hap) - creator of the project's logo [@v_novoseltsev](https://www.instagram.com/v_novoseltsev)
Table of Contents - [Installation](#installation) - [go2rtc: Binary](#go2rtc-binary) - [go2rtc: Docker](#go2rtc-docker) - [go2rtc: Home Assistant add-on](#go2rtc-home-assistant-add-on) - [go2rtc: Home Assistant Integration](#go2rtc-home-assistant-integration) - [go2rtc: Master version](#go2rtc-master-version) - [Configuration](#configuration) - [Features](#features) - [Streaming input](#streaming-input) - [Streaming output](#streaming-output) - [Streaming ingest](#streaming-ingest) - [Two-way audio](#two-way-audio) - [Stream to camera](#stream-to-camera) - [Publish stream](#publish-stream) - [Preload stream](#preload-stream) - [Streaming stats](#streaming-stats) - [Codecs](#codecs) - [Codecs filters](#codecs-filters) - [Codecs madness](#codecs-madness) - [Built-in transcoding](#built-in-transcoding) - [Codecs negotiation](#codecs-negotiation) - [Security](#security) - [Projects using go2rtc](#projects-using-go2rtc) - [Camera experience](#camera-experience) - [Tips](#tips)
## Installation 1. Download [binary](#go2rtc-binary) or use [Docker](#go2rtc-docker) or Home Assistant [add-on](#go2rtc-home-assistant-add-on) or [integration](#go2rtc-home-assistant-integration) 2. Open web interface: `http://localhost:1984/` 3. Add [streams](#streaming-input) to [config](#configuration) **Developers:** integrate [HTTP API](internal/api/README.md) into your smart home platform. ### go2rtc: Binary Download binary for your OS from [latest release](https://github.com/AlexxIT/go2rtc/releases/): | name | description | |-----------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------| | [go2rtc_win64.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_win64.zip) | Windows 10+ 64-bit | | [go2rtc_win32.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_win32.zip) | Windows 10+ 32-bit | | [go2rtc_win_arm64.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_win_arm64.zip) | Windows ARM 64-bit | | [go2rtc_linux_amd64](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_linux_amd64) | Linux 64-bit | | [go2rtc_linux_i386](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_linux_i386) | Linux 32-bit | | [go2rtc_linux_arm64](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_linux_arm64) | Linux ARM 64-bit (ex. Raspberry 64-bit OS) | | [go2rtc_linux_arm](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_linux_arm) | Linux ARM 32-bit (ex. Raspberry 32-bit OS) | | [go2rtc_linux_armv6](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_linux_armv6) | Linux ARMv6 (for old Raspberry 1 and Zero) | | [go2rtc_linux_mipsel](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_linux_mipsel) | Linux MIPS (ex. [Xiaomi Gateway 3](https://github.com/AlexxIT/XiaomiGateway3), [Wyze cameras](https://github.com/gtxaspec/wz_mini_hacks)) | | [go2rtc_mac_amd64.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_mac_amd64.zip) | macOS 11+ Intel 64-bit | | [go2rtc_mac_arm64.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_mac_arm64.zip) | macOS ARM 64-bit | | [go2rtc_freebsd_amd64.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_freebsd_amd64.zip) | FreeBSD 64-bit | | [go2rtc_freebsd_arm64.zip](https://github.com/AlexxIT/go2rtc/releases/latest/download/go2rtc_freebsd_arm64.zip) | FreeBSD ARM 64-bit | Don't forget to fix the rights `chmod +x go2rtc_xxx_xxx` on Linux and Mac. PS. The application is compiled with the latest versions of the Go language for maximum speed and security. Therefore, the [minimum OS versions](https://go.dev/wiki/MinimumRequirements) depend on the Go language. ### go2rtc: Docker The Docker containers [`alexxit/go2rtc`](https://hub.docker.com/r/alexxit/go2rtc) and [`ghcr.io/alexxit/go2rtc`](https://github.com/AlexxIT/go2rtc/pkgs/container/go2rtc) support multiple architectures including `386`, `amd64`, `arm/v6`, `arm/v7` and `arm64`. These containers offer the same functionality as the Home Assistant [add-on](#go2rtc-home-assistant-add-on) but are designed to operate independently of Home Assistant. It comes preinstalled with [FFmpeg](internal/ffmpeg/README.md) and [Python](internal/echo/README.md). ### go2rtc: Home Assistant add-on [![Open your Home Assistant instance and show the add add-on repository dialog with a specific repository URL pre-filled.](https://my.home-assistant.io/badges/supervisor_add_addon_repository.svg)](https://my.home-assistant.io/redirect/supervisor_add_addon_repository/?repository_url=https%3A%2F%2Fgithub.com%2FAlexxIT%2Fhassio-addons) 1. Settings > Add-ons > Plus > Repositories > Add ``` https://github.com/AlexxIT/hassio-addons ``` 2. go2rtc > Install > Start ### go2rtc: Home Assistant Integration [WebRTC Camera](https://github.com/AlexxIT/WebRTC) custom component can be used on any Home Assistant [installation](https://www.home-assistant.io/installation/), including [HassWP](https://github.com/AlexxIT/HassWP) on Windows. It can automatically download and use the latest version of go2rtc. Or it can connect to an existing version of go2rtc. Addon installation in this case is optional. ### go2rtc: Master version Latest, but maybe unstable version: - Binary: [latest master build](https://nightly.link/AlexxIT/go2rtc/workflows/build/master) - Docker: `alexxit/go2rtc:master` or `alexxit/go2rtc:master-hardware` versions - Home Assistant add-on: `go2rtc master` or `go2rtc master hardware` versions ## Configuration This is the `go2rtc.yaml` file in [YAML-format](https://en.wikipedia.org/wiki/YAML). The configuration can be changed in the [WebUI](www/README.md) at `http://localhost:1984`. The editor provides syntax highlighting and checking. ![go2rtc webui config](website/images/webui-config.png) The simplest config looks like this: ```yaml streams: hall-camera: rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0 ``` - by default go2rtc will search `go2rtc.yaml` in the current work directory - `api` server will start on default **1984 port** (TCP) - `rtsp` server will start on default **8554 port** (TCP) - `webrtc` will use port **8555** (TCP/UDP) for connections More information can be [found here](internal/app/README.md). ## Features A summary table of all modules and features can be found [here](internal/README.md). **Core modules** - [`app`](internal/app/README.md) - Reading [configs](internal/app/README.md) and setting up [logs](internal/app/README.md#log). - [`api`](internal/api/README.md) - Handle [HTTP](internal/api/README.md) and [WebSocket](internal/api/ws/README.md) API. - [`streams`](internal/streams/README.md) - Handle a list of streams. ### Streaming input #### public protocols - [`mpjpeg`](internal/mjpeg/README.md#mjpeg-client) - The legacy but still used [MJPEG](https://en.wikipedia.org/wiki/Motion_JPEG) protocol for real-time media transmission. - [`onvif`](internal/onvif/README.md#onvif-client) - A popular [ONVIF](https://en.wikipedia.org/wiki/ONVIF) protocol for receiving media in RTSP format. - [`rtmp`](internal/rtmp/README.md#rtmp-client) - The legacy but still used [RTMP](https://en.wikipedia.org/wiki/Real-Time_Messaging_Protocol) protocol for real-time media transmission. - [`rtsp`](internal/rtsp/README.md#rtsp-client) - The most common [RTSP](https://en.wikipedia.org/wiki/Real-Time_Streaming_Protocol) protocol for real-time media transmission. - [`webrtc`](internal/webrtc/README.md#webrtc-client) - [WebRTC](https://en.wikipedia.org/wiki/WebRTC) web-compatible protocol for real-time media transmission. - [`yuv4mpegpipe`](internal/http/README.md#tcp) - Raw [YUV](https://en.wikipedia.org/wiki/Y%E2%80%B2UV) frame stream with [YUV4MPEG](https://manned.org/yuv4mpeg) header. #### private protocols - [`bubble`](internal/bubble/README.md) - Some NVR from [dvr163.com](http://help.dvr163.com/) and [eseecloud.com](http://www.eseecloud.com/). - [`doorbird`](internal/doorbird/README.md) - [Doorbird](https://www.doorbird.com/) devices with two-way audio. - [`dvrip`](internal/dvrip/README.md) - DVR-IP NVR, NetSurveillance, Sofia protocol (XMeye SDK). - [`eseecloud`](internal/eseecloud/README.md) - Some NVR from [dvr163.com](http://help.dvr163.com/) and [eseecloud.com](http://www.eseecloud.com/). - [`gopro`](internal/gopro/README.md) - [GoPro](https://gopro.com/) cameras, connected via USB or Wi-Fi. - [`hass`](internal/hass/README.md) - Import cameras from [Home Assistant](https://www.home-assistant.io/) config files. - [`homekit`](internal/homekit/README.md) - Cameras with [Apple HomeKit](https://www.apple.com/home-app/accessories/) protocol. - [`isapi`](internal/isapi/README.md) - Two-way audio for [Hikvision ISAPI](https://tpp.hikvision.com/download/ISAPI_OTAP) protocol. - [`kasa`](internal/kasa/README.md) - [TP-Link Kasa](https://www.kasasmart.com/) cameras. - [`multitrans`](internal/multitrans/README.md) - Two-way audio for Chinese version of [TP-Link](https://www.tp-link.com.cn/) cameras. - [`nest`](internal/nest/README.md) - [Google Nest](https://developers.google.com/nest/device-access/supported-devices) cameras through user-unfriendly and paid APIs. - [`ring`](internal/ring/README.md) - Ring cameras with two-way audio support. - [`roborock`](internal/roborock/README.md) - [Roborock](https://roborock.com/) vacuums with cameras with two-way audio support. - [`tapo`](internal/tapo/README.md) - [TP-Link Tapo](https://www.tapo.com/) cameras with two-way audio support. - [`vigi`](internal/tapo/README.md#tp-link-vigi) - TP-Link Vigi cameras. - [`tuya`](internal/tuya/README.md) - [Tuya](https://www.tuya.com/) ecosystem cameras with two-way audio support. - [`webtorrent`](internal/webtorrent/README.md) - Stream from another go2rtc via [WebTorrent](https://en.wikipedia.org/wiki/WebTorrent) protocol. - [`wyze`](internal/wyze/README.md) - [Wyze](https://wyze.com/) cameras using native P2P protocol - [`xiaomi`](internal/xiaomi/README.md) - [Xiaomi Mi Home](https://home.mi.com/) ecosystem cameras with two-way audio support. #### devices - [`alsa`](internal/alsa/README.md) - A [framework](https://en.wikipedia.org/wiki/Advanced_Linux_Sound_Architecture) for receiving audio from devices on Linux OS. - [`v4l2`](internal/v4l2/README.md) - A [framework](https://en.wikipedia.org/wiki/Video4Linux) for receiving video from devices on Linux OS. #### files - [`adts`](internal/http/README.md#tcp) - Audio stream in [AAC](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) codec with Audio Data Transport Stream headers. - [`flv`](internal/http/README.md#tcp) - The legacy but still used [Flash Video](https://en.wikipedia.org/wiki/Flash_Video) format. - [`h264`](internal/http/README.md#tcp) - AVC/H.264 bitstream. - [`hevc`](internal/http/README.md#tcp) - HEVC/H.265 bitstream. - [`hls`](internal/http/README.md) - A popular [HTTP Live Streaming](https://en.wikipedia.org/wiki/HTTP_Live_Streaming) format. - [`mjpeg`](internal/http/README.md#tcp) - A continuous sequence of JPEG frames (without HTTP headers). - [`mpegts`](internal/http/README.md#tcp) - The legacy [MPEG transport stream](https://en.wikipedia.org/wiki/MPEG_transport_stream) format. - [`wav`](internal/http/README.md#tcp) - Audio stream in [Waveform Audio File](https://en.wikipedia.org/wiki/WAV) format. #### scripts - [`echo`](internal/echo/README.md) - If the source has a dynamic link, you can use a bash or python script to get it. - [`exec`](internal/exec/README.md) - You can run an external application (`ffmpeg`, `gstreamer`, `rpicam`, etc.) and receive a media stream from it. - [`expr`](internal/expr/README.md) - If the source has a dynamic link, you can use [Expr](https://github.com/expr-lang/expr) language to get it. - [`ffmpeg`](internal/ffmpeg/README.md) - Use [FFmpeg](https://ffmpeg.org/) as a stream source. Hardware-accelerated transcoding and streaming from USB devices are supported. #### webrtc - [`creality`](internal/webrtc/README.md#creality) - [Creality](https://www.creality.com/) 3D printer cameras. - [`kinesis`](internal/webrtc/README.md#kinesis) - [Amazon Kinesis](https://aws.amazon.com/kinesis/video-streams/) video streams. - [`openipc`](internal/webrtc/README.md#openipc) - Cameras on open-source [OpenIPC](https://openipc.org/) firmware. - [`switchbot`](internal/webrtc/README.md#switchbot) - [SwitchBot](https://us.switch-bot.com/) cameras. - [`whep`](internal/webrtc/README.md#whep) - [WebRTC/WHEP](https://datatracker.ietf.org/doc/draft-murillo-whep/) is replaced by [WebRTC/WISH](https://datatracker.ietf.org/doc/charter-ietf-wish/02/) standard for WebRTC video/audio viewers. - [`wyze`](internal/webrtc/README.md#wyze) - Legacy method to connect to [Wyze](https://www.wyze.com/) cameras via [docker-wyze-bridge](https://github.com/mrlt8/docker-wyze-bridge). ### Streaming output - [`adts`](internal/mpeg/README.md) - Output stream in ADTS format with [AAC](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) audio. - [`ascii`](internal/mjpeg/README.md#ascii) - Just for fun stream as [ASCII to Terminal](https://www.youtube.com/watch?v=sHj_3h_sX7M). - [`flv`](internal/rtmp/README.md) - Output stream in [Flash Video](https://en.wikipedia.org/wiki/Flash_Video) format. - [`hls`](internal/hls/README.md) - Output stream in [HTTP Live Streaming](https://en.wikipedia.org/wiki/HTTP_Live_Streaming) format. - [`homekit`](internal/homekit/README.md#homekit-server) - Output stream to [Apple Home](https://www.apple.com/home-app/) using [HomeKit](https://en.wikipedia.org/wiki/Apple_Home) protocol. - [`jpeg`](internal/mjpeg/README.md#jpeg) - Output snapshots in [JPEG](https://en.wikipedia.org/wiki/JPEG) format. - [`mpjpeg`](internal/mjpeg/README.md#mpjpeg) - Output a stream in [MJPEG](https://en.wikipedia.org/wiki/Motion_JPEG) format. - [`mp4`](internal/mp4/README.md) - Output as [MP4 stream](https://en.wikipedia.org/wiki/Progressive_download) or [Media Source Extensions](https://developer.mozilla.org/en-US/docs/Web/API/Media_Source_Extensions_API) (MSE) compatible format. - [`mpegts`](internal/mpeg/README.md) - Output stream in [MPEG transport stream](https://en.wikipedia.org/wiki/MPEG_transport_stream) format. - [`onvif`](internal/onvif/README.md#onvif-server) - Output stream using [ONVIF](https://en.wikipedia.org/wiki/ONVIF) protocol. - [`rtmp`](internal/rtmp/README.md#rtmp-server) - Output stream using [Real-Time Messaging](https://en.wikipedia.org/wiki/Real-Time_Messaging_Protocol) protocol. - [`rtsp`](internal/rtsp/README.md#rtsp-server) - Output stream using [Real-Time Streaming](https://en.wikipedia.org/wiki/Real-Time_Streaming_Protocol) protocol. - [`webrtc`](internal/webrtc/README.md#webrtc-server) - Output stream using [Web Real-Time Communication](https://developer.mozilla.org/en-US/docs/Web/API/WebRTC_API) API. - [`webtorrent`](internal/webtorrent/README.md#webtorrent-server) - Output stream using [WebTorrent](https://en.wikipedia.org/wiki/WebTorrent) protocol. - [`yuv4mpegpipe`](internal/mjpeg/README.md#yuv4mpegpipe) - Output in raw [YUV](https://en.wikipedia.org/wiki/Y%E2%80%B2UV) frame stream with [YUV4MPEG](https://manned.org/yuv4mpeg) header. ### Streaming ingest Supported for: [`flv`](internal/rtmp/README.md#flv-server), [`mjpeg`](internal/mjpeg/README.md#streaming-ingest), [`mpegts`](internal/mpeg/README.md#streaming-ingest), [`rtmp`](internal/rtmp/README.md#rtmp-server), [`rtsp`](internal/rtsp/README.md#streaming-ingest), [`webrtc`](internal/webrtc/README.md#streaming-ingest). This is a feature when go2rtc expects to receive an incoming stream from an external application. The stream transmission is started and stopped by an external application. - You can push data only to an existing stream (create a stream with empty source in config). - You can push multiple incoming sources to the same stream. - You can push data to a non-empty stream, so it will have additional codecs inside. ### Two-way audio Supported for: [`doorbird`](internal/doorbird/README.md), [`dvrip`](internal/dvrip/README.md), [`exec`](internal/exec/README.md), [`isapi`](internal/isapi/README.md), [`multitrans`](internal/multitrans/README.md), [`ring`](internal/ring/README.md), [`roborock`](internal/roborock/README.md), [`rtsp`](internal/rtsp/README.md#two-way-audio), [`tapo`](internal/tapo/README.md), [`tuya`](internal/tuya/README.md), [`webrtc`](internal/webrtc/README.md), [`wyze`](internal/wyze/README.md), [`xiaomi`](internal/xiaomi/README.md). Two-way audio can be used in browser with [WebRTC](internal/webrtc/README.md) technology. The browser will give access to the microphone only for HTTPS sites ([read more](https://stackoverflow.com/questions/52759992/how-to-access-camera-and-microphone-in-chrome-without-https)). ### Stream to camera You can play audio files or live streams on any camera with [two-way audio](#two-way-audio) support. [read more](internal/streams/README.md#stream-to-camera) ### Publish stream You can publish any stream to streaming services (YouTube, Telegram, etc.) via RTMP/RTMPS. [read more](internal/streams/README.md#publish-stream) ### Preload stream You can preload any stream on go2rtc start. This is useful for cameras that take a long time to start up. [read more](internal/streams/README.md#preload-stream) ### Streaming stats [WebUI](www/README.md) provides detailed information about all active connections, including IP-addresses, formats, protocols, number of packets and bytes transferred. Via the [HTTP API](internal/api/README.md) in [`json`](https://en.wikipedia.org/wiki/JSON) or [`dot`](https://en.wikipedia.org/wiki/DOT_(graph_description_language)) format on an interactive connection map. ![go2rtc webui net](website/images/webui-net.png) ## Codecs If you have questions about why video or audio is not displayed, you need to read the following sections. | Name | FFmpeg | RTSP | Aliases | |------------------------------|----------|---------------|-------------| | Advanced Audio Coding | `aac` | MPEG4-GENERIC | | | Advanced Video Coding | `h264` | H264 | AVC, H.264 | | G.711 PCM (A-law) | `alaw` | PCMA | G711A | | G.711 PCM (µ-law) | `mulaw` | PCMU | G711u | | High Efficiency Video Coding | `hevc` | H265 | HEVC, H.265 | | Motion JPEG | `mpjpeg` | JPEG | | | MPEG-1 Audio Layer III | `mp3` | MPA | | | Opus Codec | `opus` | OPUS | | | PCM signed 16-bit big-endian | `s16be` | L16 | | ### Codecs filters go2rtc can automatically detect which codecs your device supports for [WebRTC](internal/webrtc/README.md) and [MSE](internal/mp4/README.md) technologies. But it cannot be done for [RTSP](internal/rtsp/README.md), [HTTP progressive streaming](internal/mp4/README.md), [HLS](internal/hls/README.md) technologies. You can manually add a codec filter when you create a link to a stream. The filters work the same for all three technologies. Filters do not create a new codec, they only select the suitable codec from existing sources. You can add new codecs to the stream using the [FFmpeg transcoding](internal/ffmpeg/README.md). Without filters: - RTSP will provide only the first video and only the first audio (any codec) - MP4 will include only compatible codecs (H264, H265, AAC) - HLS will output in the legacy TS format (H264 without audio) Some examples: - `rtsp://192.168.1.123:8554/camera1?mp4` - useful for recording as MP4 files (e.g. Home Assistant or Frigate) - `rtsp://192.168.1.123:8554/camera1?video=h264,h265&audio=aac` - full version of the filter above - `rtsp://192.168.1.123:8554/camera1?video=h264&audio=aac&audio=opus` - H264 video codec and two separate audio tracks - `rtsp://192.168.1.123:8554/camera1?video&audio=all` - any video codec and all audio codecs as separate tracks - `http://192.168.1.123:1984/api/stream.m3u8?src=camera1&mp4` - HLS stream with MP4 compatible codecs (HLS/fMP4) - `http://192.168.1.123:1984/api/stream.m3u8?src=camera1&mp4=flac` - HLS stream with PCMA/PCMU/PCM audio support (HLS/fMP4), won't work on old devices - `http://192.168.1.123:1984/api/stream.mp4?src=camera1&mp4=flac` - MP4 file with PCMA/PCMU/PCM audio support, won't work on old devices (ex. iOS 12) - `http://192.168.1.123:1984/api/stream.mp4?src=camera1&mp4=all` - MP4 file with non-standard audio codecs, won't work on some players ### Codecs madness `AVC/H.264` video can be played almost anywhere. But `HEVC/H.265` has many limitations in supporting different devices and browsers. | Device | WebRTC | MSE | HTTP* | HLS | |--------------------------------------------------------------------|-----------------------------------------|-----------------------------------------|----------------------------------------------|-----------------------------| | *latency* | best | medium | bad | bad | | Desktop Chrome 136+
Desktop Edge
Android Chrome 136+ | H264, H265*
PCMU, PCMA
OPUS | H264, H265*
AAC, FLAC*
OPUS | H264, H265*
AAC, FLAC*
OPUS, MP3 | no | | Desktop Firefox | H264
PCMU, PCMA
OPUS | H264
AAC, FLAC*
OPUS | H264
AAC, FLAC*
OPUS | no | | Desktop Safari 14+
iPad Safari 14+
iPhone Safari 17.1+ | H264, H265*
PCMU, PCMA
OPUS | H264, H265
AAC, FLAC* | **no!** | H264, H265
AAC, FLAC* | | iPhone Safari 14+ | H264, H265*
PCMU, PCMA
OPUS | **no!** | **no!** | H264, H265
AAC, FLAC* | | macOS [Hass App][1] | no | no | no | H264, H265
AAC, FLAC* | [1]: https://apps.apple.com/app/home-assistant/id1099568401 - `HTTP*` - HTTP Progressive Streaming, not related to [progressive download](https://en.wikipedia.org/wiki/Progressive_download), because the file has no size and no end - `WebRTC H265` - supported in [Chrome 136+](https://developer.chrome.com/release-notes/136), supported in [Safari 18+](https://developer.apple.com/documentation/safari-release-notes/safari-18-release-notes) - `MSE iPhone` - supported in [iOS 17.1+](https://webkit.org/blog/14735/webkit-features-in-safari-17-1/) **Audio** - go2rtc supports [automatic repackaging](#built-in-transcoding) of `PCMA/PCMU/PCM` codecs into `FLAC` for MSE/MP4/HLS so they'll work almost anywhere - **WebRTC** audio codecs: `PCMU/8000`, `PCMA/8000`, `OPUS/48000/2` - `OPUS` and `MP3` inside **MP4** are part of the standard, but some players do not support them anyway (especially Apple) **Apple devices** - all Apple devices don't support HTTP progressive streaming - old iPhone firmwares don't support MSE technology because it competes with the HTTP Live Streaming (HLS) technology, invented by Apple - HLS is the worst technology for **live** streaming, it still exists only because of iPhones ### Built-in transcoding There are no plans to embed complex transcoding algorithms inside go2rtc. [FFmpeg source](internal/ffmpeg/README.md) does a great job with this. Including [hardware acceleration](https://github.com/AlexxIT/go2rtc/wiki/Hardware-acceleration) support. But go2rtc has some simple algorithms. They are turned on automatically; you do not need to set them up additionally. **PCM for MSE/MP4/HLS** Go2rtc can pack `PCMA`, `PCMU` and `PCM` codecs into an MP4 container so that they work in all browsers and all built-in players on modern devices. Including Apple QuickTime: ```text PCMA/PCMU => PCM => FLAC => MSE/MP4/HLS ``` **Resample PCMA/PCMU for WebRTC** By default WebRTC supports only `PCMA/8000` and `PCMU/8000`. But go2rtc can automatically resample PCMA and PCMU codecs with a different sample rate. Also, go2rtc can transcode `PCM` codec to `PCMA/8000`, so WebRTC can play it: ```text PCM/xxx => PCMA/8000 => WebRTC PCMA/xxx => PCMA/8000 => WebRTC PCMU/xxx => PCMU/8000 => WebRTC ``` **Important** - FLAC codec not supported in an RTSP stream. If you are using Frigate or Home Assistant for recording MP4 files with PCMA/PCMU/PCM audio, you should set up transcoding to the AAC codec. - PCMA and PCMU are VERY low-quality codecs. They support only 256! different sounds. Use them only when you have no other options. ### Codecs negotiation For example, you want to watch an RTSP stream from a [Dahua IPC-K42](https://www.dahuasecurity.com/fr/products/All-Products/Network-Cameras/Wireless-Series/Wi-Fi-Series/4MP/IPC-K42) camera in your Chrome browser. - this camera supports two-way audio standard **ONVIF Profile T** - this camera supports codecs **H264, H265** for sending video, and you select `H264` in camera settings - this camera supports codecs **AAC, PCMU, PCMA** for sending audio (from mic), and you select `AAC/16000` in camera settings - this camera supports codecs **AAC, PCMU, PCMA** for receiving audio (to speaker), you don't need to select them - your browser supports codecs **H264, VP8, VP9, AV1** for receiving video, you don't need to select them - your browser supports codecs **OPUS, PCMU, PCMA** for sending and receiving audio, you don't need to select them - you can't get the camera audio directly because its audio codecs don't match your browser's codecs - so you decide to use transcoding via FFmpeg and add this setting to the config YAML file - you have chosen `OPUS/48000/2` codec, because it is higher quality than the `PCMU/8000` or `PCMA/8000` Now you have a stream with two sources - **RTSP and FFmpeg**: ```yaml streams: dahua: - rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif - ffmpeg:rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0#audio=opus ``` **go2rtc** automatically matches codecs for your browser across all of your stream sources. This is called **multi-source two-way codec negotiation**, and it's one of the main features of this app. **PS.** You can select `PCMU` or `PCMA` codec in camera settings and not use transcoding at all. Or you can select `AAC` codec for main stream and `PCMU` codec for second stream and add both RTSP to YAML config, this also will work fine. ## Security > [!IMPORTANT] > If an attacker gains access to the API, you are in danger. Through the API, an attacker can use insecure sources such as echo and exec. And get full access to your server. For maximum (paranoid) security, go2rtc has special settings: ```yaml app: # use only allowed modules modules: [api, rtsp, webrtc, exec, ffmpeg, mjpeg] api: # use only allowed API paths allow_paths: [/api, /api/streams, /api/webrtc, /api/frame.jpeg] # enable auth for localhost (used together with username and password) local_auth: true exec: # use only allowed exec paths allow_paths: [ffmpeg] ``` By default, `go2rtc` starts the Web interface on port `1984` and RTSP on port `8554`, as well as uses port `8555` for WebRTC connections. The three ports are accessible from your local network. So anyone on your local network can watch video from your cameras without authorization. The same rule applies to the Home Assistant add-on. This is not a problem if you trust your local network as much as I do. But you can change this behaviour with a `go2rtc.yaml` config: ```yaml api: listen: "127.0.0.1:1984" # localhost rtsp: listen: "127.0.0.1:8554" # localhost webrtc: listen: ":8555" # external TCP/UDP port ``` - local access to RTSP is not a problem for [FFmpeg](internal/ffmpeg/README.md) integration, because it runs locally on your server - local access to API is not a problem for the [Home Assistant add-on](#go2rtc-home-assistant-add-on), because Home Assistant runs locally on the same server, and the add-on web UI is protected with Home Assistant authorization ([Ingress feature](https://www.home-assistant.io/blog/2019/04/15/hassio-ingress/)) - external access to WebRTC TCP port is not a problem, because it is used only for transmitting encrypted media data - anyway you need to open this port to your local network and to the Internet for WebRTC to work If you need web interface protection without the Home Assistant add-on, you need to use a reverse proxy, like [Nginx](https://nginx.org/), [Caddy](https://caddyserver.com/), etc. PS. Additionally, WebRTC will try to use the 8555 UDP port to transmit encrypted media. It works without problems on the local network, and sometimes also works for external access, even if you haven't opened this port on your router ([read more](https://en.wikipedia.org/wiki/UDP_hole_punching)). But for stable external WebRTC access, you need to open the 8555 port on your router for both TCP and UDP. ## Projects using go2rtc - [Home Assistant](https://www.home-assistant.io/) [2024.11+](https://www.home-assistant.io/integrations/go2rtc/) - top open-source smart home project - [Frigate](https://frigate.video/) [0.12+](https://docs.frigate.video/guides/configuring_go2rtc/) - open-source NVR built around real-time AI object detection - [Advanced Camera Card](https://github.com/dermotduffy/advanced-camera-card) - custom card for Home Assistant - [OpenIPC](https://github.com/OpenIPC/firmware/tree/master/general/package/go2rtc) - alternative IP camera firmware from an open community - [wz_mini_hacks](https://github.com/gtxaspec/wz_mini_hacks) - custom firmware for Wyze cameras - [EufyP2PStream](https://github.com/oischinger/eufyp2pstream) - a small project that provides a video/audio stream from Eufy cameras that don't directly support RTSP - [ioBroker.euSec](https://github.com/bropat/ioBroker.eusec) - [ioBroker](https://www.iobroker.net/) adapter for controlling Eufy security devices - [MMM-go2rtc](https://github.com/Anonym-tsk/MMM-go2rtc) - MagicMirror² module - [ring-mqtt](https://github.com/tsightler/ring-mqtt) - Ring-to-MQTT bridge - [lightNVR](https://github.com/opensensor/lightNVR) **Distributions** - [Alpine Linux](https://pkgs.alpinelinux.org/packages?name=go2rtc) - [Arch User Repository](https://linux-packages.com/aur/package/go2rtc) - [Gentoo](https://github.com/inode64/inode64-overlay/tree/main/media-video/go2rtc) - [NixOS](https://search.nixos.org/packages?query=go2rtc) - [Proxmox Helper Scripts](https://github.com/community-scripts/ProxmoxVE/) - [QNAP](https://www.myqnap.org/product/go2rtc/) - [Synology NAS](https://synocommunity.com/package/go2rtc) - [Unraid](https://unraid.net/community/apps?q=go2rtc) ## Camera experience - [Dahua](https://www.dahuasecurity.com/) - reference implementation streaming protocols, a lot of settings, high stream quality, multiple streaming clients - [EZVIZ](https://www.ezviz.com/) - awful RTSP protocol implementation, many bugs in SDP - [Hikvision](https://www.hikvision.com/) - a lot of proprietary streaming technologies - [Reolink](https://reolink.com/) - some models have an awful, unusable RTSP implementation and not the best RTMP alternative (I recommend that you contact Reolink support for new firmware), few settings - [Sonoff](https://sonoff.tech/) - very low stream quality, no settings, not the best protocol implementation - [TP-Link](https://www.tp-link.com/) - few streaming clients, packet loss? - Cheap noname cameras, Wyze Cams, Xiaomi cameras with hacks (usually have `/live/ch00_1` in RTSP URL) - awful but usable RTSP protocol implementation, low stream quality, few settings, packet loss? ## Tips **Using apps for low RTSP delay** - `ffplay -fflags nobuffer -flags low_delay "rtsp://192.168.1.123:8554/camera1"` - VLC > Preferences > Input / Codecs > Default Caching Level: Lowest Latency **Snapshots to Telegram** [read more](https://github.com/AlexxIT/go2rtc/wiki/Snapshot-to-Telegram) ================================================ FILE: docker/Dockerfile ================================================ # syntax=docker/dockerfile:labs # 0. Prepare images ARG PYTHON_VERSION="3.13" ARG GO_VERSION="1.25" # 1. Build go2rtc binary FROM --platform=$BUILDPLATFORM golang:${GO_VERSION}-alpine AS build ARG TARGETPLATFORM ARG TARGETOS ARG TARGETARCH ENV GOOS=${TARGETOS} ENV GOARCH=${TARGETARCH} WORKDIR /build RUN apk add git # Cache dependencies COPY go.mod go.sum ./ RUN --mount=type=cache,target=/root/.cache/go-build go mod download COPY . . RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath # 2. Final image FROM python:${PYTHON_VERSION}-alpine AS base # Install ffmpeg, tini (for signal handling), # and other common tools for the echo source. # alsa-plugins-pulse for ALSA support (+0MB) # font-droid for FFmpeg drawtext filter (+2MB) RUN apk add --no-cache tini ffmpeg ffplay bash curl jq alsa-plugins-pulse font-droid # Hardware Acceleration for Intel CPU (+50MB) ARG TARGETARCH RUN if [ "${TARGETARCH}" = "amd64" ]; then apk add --no-cache libva-intel-driver intel-media-driver; fi # Hardware: AMD and NVidia VAAPI (not sure about this) # RUN libva-glx mesa-va-gallium # Hardware: AMD and NVidia VDPAU (not sure about this) # RUN libva-vdpau-driver mesa-vdpau-gallium (+150MB total) COPY --from=build /build/go2rtc /usr/local/bin/ EXPOSE 1984 8554 8555 8555/udp ENTRYPOINT ["/sbin/tini", "--"] VOLUME /config WORKDIR /config CMD ["go2rtc", "-config", "/config/go2rtc.yaml"] ================================================ FILE: docker/README.md ================================================ # Docker Images are built automatically via [GitHub actions](https://github.com/AlexxIT/go2rtc/actions) and published on [Docker Hub](https://hub.docker.com/r/alexxit/go2rtc) and [GitHub](https://github.com/AlexxIT/go2rtc/pkgs/container/go2rtc). ## Versions - `alexxit/go2rtc:latest` - latest release based on `alpine` (`amd64`, `386`, `arm/v6`, `arm/v7`, `arm64`) with support for hardware transcoding for Intel iGPU and Raspberry - `alexxit/go2rtc:latest-hardware` - latest release based on `debian 13` (`amd64`) with support for hardware transcoding for Intel iGPU, AMD GPU and NVidia GPU - `alexxit/go2rtc:latest-rockchip` - latest release based on `debian 12` (`arm64`) with support for hardware transcoding for Rockchip RK35xx - `alexxit/go2rtc:master` - latest unstable version based on `alpine` - `alexxit/go2rtc:master-hardware` - latest unstable version based on `debian 13` (`amd64`) - `alexxit/go2rtc:master-rockchip` - latest unstable version based on `debian 12` (`arm64`) ## Docker compose ```yaml services: go2rtc: image: alexxit/go2rtc network_mode: host # important for WebRTC, HomeKit, UDP cameras privileged: true # only for FFmpeg hardware transcoding restart: unless-stopped # autorestart on fail or config change from WebUI environment: - TZ=Atlantic/Bermuda # timezone in logs volumes: - "~/go2rtc:/config" # folder for go2rtc.yaml file (edit from WebUI) ``` ## Basic Deployment ```bash docker run -d \ --name go2rtc \ --network host \ --privileged \ --restart unless-stopped \ -e TZ=Atlantic/Bermuda \ -v ~/go2rtc:/config \ alexxit/go2rtc ``` ## Deployment with GPU Acceleration ```bash docker run -d \ --name go2rtc \ --network host \ --privileged \ --restart unless-stopped \ -e TZ=Atlantic/Bermuda \ --gpus all \ -v ~/go2rtc:/config \ alexxit/go2rtc:latest-hardware ``` ================================================ FILE: docker/hardware.Dockerfile ================================================ # syntax=docker/dockerfile:labs # 0. Prepare images # only debian 13 (trixie) has latest ffmpeg # https://packages.debian.org/trixie/ffmpeg ARG DEBIAN_VERSION="trixie-slim" ARG GO_VERSION="1.25-bookworm" # 1. Build go2rtc binary FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build ARG TARGETPLATFORM ARG TARGETOS ARG TARGETARCH ENV GOOS=${TARGETOS} ENV GOARCH=${TARGETARCH} WORKDIR /build # Cache dependencies COPY go.mod go.sum ./ RUN --mount=type=cache,target=/root/.cache/go-build go mod download COPY . . RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath # 2. Final image FROM debian:${DEBIAN_VERSION} # Prepare apt for buildkit cache RUN rm -f /etc/apt/apt.conf.d/docker-clean \ && echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache # Install ffmpeg, tini (for signal handling), # and other common tools for the echo source. # non-free for Intel QSV support (not used by go2rtc, just for tests) # mesa-va-drivers for AMD APU # libasound2-plugins for ALSA support RUN --mount=type=cache,target=/var/cache/apt,sharing=locked --mount=type=cache,target=/var/lib/apt,sharing=locked \ echo 'deb http://deb.debian.org/debian trixie non-free' > /etc/apt/sources.list.d/debian-non-free.list && \ apt-get -y update && apt-get -y install ffmpeg tini \ python3 curl jq \ intel-media-va-driver-non-free \ mesa-va-drivers \ libasound2-plugins && \ apt-get clean && rm -rf /var/lib/apt/lists/* COPY --from=build /build/go2rtc /usr/local/bin/ EXPOSE 1984 8554 8555 8555/udp ENTRYPOINT ["/usr/bin/tini", "--"] VOLUME /config WORKDIR /config # https://github.com/NVIDIA/nvidia-docker/wiki/Installation-(Native-GPU-Support) ENV NVIDIA_VISIBLE_DEVICES all ENV NVIDIA_DRIVER_CAPABILITIES compute,video,utility CMD ["go2rtc", "-config", "/config/go2rtc.yaml"] ================================================ FILE: docker/rockchip.Dockerfile ================================================ # syntax=docker/dockerfile:labs # 0. Prepare images ARG PYTHON_VERSION="3.13-slim-bookworm" ARG GO_VERSION="1.25-bookworm" # 1. Build go2rtc binary FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build ARG TARGETPLATFORM ARG TARGETOS ARG TARGETARCH ENV GOOS=${TARGETOS} ENV GOARCH=${TARGETARCH} WORKDIR /build # Cache dependencies COPY go.mod go.sum ./ RUN --mount=type=cache,target=/root/.cache/go-build go mod download COPY . . RUN --mount=type=cache,target=/root/.cache/go-build CGO_ENABLED=0 go build -ldflags "-s -w" -trimpath # 2. Final image FROM python:${PYTHON_VERSION} # Prepare apt for buildkit cache RUN rm -f /etc/apt/apt.conf.d/docker-clean \ && echo 'Binary::apt::APT::Keep-Downloaded-Packages "true";' >/etc/apt/apt.conf.d/keep-cache # Install ffmpeg, tini (for signal handling), # and other common tools for the echo source. # libasound2-plugins for ALSA support RUN --mount=type=cache,target=/var/cache/apt,sharing=locked --mount=type=cache,target=/var/lib/apt,sharing=locked \ apt-get -y update && apt-get -y install tini \ curl jq \ libasound2-plugins && \ apt-get clean && rm -rf /var/lib/apt/lists/* COPY --from=build /build/go2rtc /usr/local/bin/ ADD --chmod=755 https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/download/6.1-8-no_extra_dump/ffmpeg /usr/local/bin EXPOSE 1984 8554 8555 8555/udp ENTRYPOINT ["/usr/bin/tini", "--"] VOLUME /config WORKDIR /config CMD ["go2rtc", "-config", "/config/go2rtc.yaml"] ================================================ FILE: examples/go2rtc_hass/main.go ================================================ package main import ( "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/hass" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/shell" ) func main() { app.Init() streams.Init() api.Init() hass.Init() shell.RunUntilSignal() } ================================================ FILE: examples/go2rtc_mjpeg/main.go ================================================ package main import ( "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api/ws" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/ffmpeg" "github.com/AlexxIT/go2rtc/internal/mjpeg" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/internal/v4l2" "github.com/AlexxIT/go2rtc/pkg/shell" ) func main() { app.Init() streams.Init() api.Init() ws.Init() ffmpeg.Init() mjpeg.Init() v4l2.Init() shell.RunUntilSignal() } ================================================ FILE: examples/go2rtc_rtsp/main.go ================================================ package main import ( "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/rtsp" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/shell" ) func main() { app.Init() streams.Init() rtsp.Init() shell.RunUntilSignal() } ================================================ FILE: examples/homekit_info/main.go ================================================ package main import ( "encoding/json" "os" "github.com/AlexxIT/go2rtc/pkg/hap" ) var servs = map[string]string{ "3E": "Accessory Information", "7E": "Security System", "85": "Motion Sensor", "96": "Battery", "A2": "Protocol Information", "110": "Camera RTP Stream Management", "112": "Microphone", "113": "Speaker", "121": "Doorbell", "129": "Data Stream Transport Management", "204": "Camera Recording Management", "21A": "Camera Operating Mode", "22A": "Wi-Fi Transport", "239": "Accessory Runtime Information", } var chars = map[string]string{ "14": "Identify", "20": "Manufacturer", "21": "Model", "23": "Name", "30": "Serial Number", "52": "Firmware Revision", "53": "Hardware Revision", "220": "Product Data", "A6": "Accessory Flags", "22": "Motion Detected", "75": "Status Active", "11A": "Mute", "119": "Volume", "B0": "Active", "209": "Selected Camera Recording Configuration", "207": "Supported Audio Recording Configuration", "205": "Supported Camera Recording Configuration", "206": "Supported Video Recording Configuration", "226": "Recording Audio Active", "223": "Event Snapshots Active", "225": "Periodic Snapshots Active", "21B": "HomeKit Camera Active", "21C": "Third Party Camera Active", "21D": "Camera Operating Mode Indicator", "11B": "Night Vision", //"129": "Supported Data Stream Transport Configuration", "37": "Version", "131": "Setup Data Stream Transport", "130": "Supported Data Stream Transport Configuration", "120": "Streaming Status", "115": "Supported Audio Stream Configuration", "116": "Supported RTP Configuration", "114": "Supported Video Stream Configuration", "117": "Selected RTP Stream Configuration", "118": "Setup Endpoints", "22B": "Current Transport", "22C": "Wi-Fi Capabilities", "22D": "Wi-Fi Configuration Control", "23C": "Ping", "68": "Battery Level", "79": "Status Low Battery", "8F": "Charging State", "73": "Programmable Switch Event", "232": "Operating State Response", "66": "Security System Current State", "67": "Security System Target State", } func main() { src := os.Args[1] dst := os.Args[2] f, err := os.Open(src) if err != nil { panic(err) } var v hap.JSONAccessories if err = json.NewDecoder(f).Decode(&v); err != nil { panic(err) } for _, acc := range v.Value { for _, srv := range acc.Services { if srv.Desc == "" { srv.Desc = servs[srv.Type] } for _, chr := range srv.Characters { if chr.Desc == "" { chr.Desc = chars[chr.Type] } } } } f, err = os.Create(dst) if err != nil { panic(err) } enc := json.NewEncoder(f) enc.SetIndent("", " ") if err = enc.Encode(v); err != nil { panic(err) } } ================================================ FILE: examples/mdns/main.go ================================================ package main import ( "log" "os" "github.com/AlexxIT/go2rtc/pkg/mdns" ) func main() { var service = mdns.ServiceHAP if len(os.Args) >= 2 { service = os.Args[1] } onentry := func(entry *mdns.ServiceEntry) bool { log.Printf("name=%s, addr=%s, info=%s\n", entry.Name, entry.Addr(), entry.Info) return false } var err error if len(os.Args) >= 3 { host := os.Args[2] log.Printf("run discovery service=%s host=%s\n", service, host) err = mdns.QueryOrDiscovery(host, service, onentry) } else { log.Printf("run discovery service=%s\n", service) err = mdns.Discovery(service, onentry) } if err != nil { log.Println(err) } } ================================================ FILE: examples/mod_pinggy/go.mod ================================================ module pinggy go 1.25 require ( github.com/Pinggy-io/pinggy-go/pinggy v0.6.9 // indirect golang.org/x/crypto v0.8.0 // indirect golang.org/x/sys v0.7.0 // indirect ) ================================================ FILE: examples/mod_pinggy/go.sum ================================================ github.com/Pinggy-io/pinggy-go/pinggy v0.6.9 h1:lzZ00JK6BUGQXnpkJZ+cVj8kIkXsmiVBUci9uEkSwEY= github.com/Pinggy-io/pinggy-go/pinggy v0.6.9/go.mod h1:V1Sxb+4zyr36o9atZiqtT4XhsKtW1RSb2GvsbTbTJYw= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc= golang.org/x/crypto v0.8.0 h1:pd9TJtTueMTVQXzk8E2XESSMQDj/U7OUu0PqJqPXQjQ= golang.org/x/crypto v0.8.0/go.mod h1:mRqEX+O9/h5TFCrQhkgjo2yKi0yYA+9ecGkdQoHrywE= golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4= golang.org/x/mod v0.8.0/go.mod h1:iBbtSCu2XBx23ZKBPSOrRkjjQPZFPuis4dIYUhu/chs= golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg= golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c= golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs= golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.1.0/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.7.0 h1:3jlCCIQZPdOYu1h8BkNvLz8Kgwtae2cagcG/VamtZRU= golang.org/x/sys v0.7.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8= golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k= golang.org/x/term v0.7.0/go.mod h1:P32HKFT3hSsZrRxla30E9HqToFYAQPCMs/zFMBUFqPY= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ= golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8= golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8= golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo= golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc= golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= ================================================ FILE: examples/mod_pinggy/main.go ================================================ package main import ( "log" "os" "github.com/Pinggy-io/pinggy-go/pinggy" ) func main() { tunType := os.Args[1] address := os.Args[2] log.SetFlags(log.Llongfile | log.LstdFlags) config := pinggy.Config{ Type: pinggy.TunnelType(tunType), TcpForwardingAddr: address, //SshOverSsl: true, //Stdout: os.Stderr, //Stderr: os.Stderr, } if tunType == "http" { hman := pinggy.CreateHeaderManipulationAndAuthConfig() //hman.SetReverseProxy(address) //hman.SetPassPreflight(true) //hman.SetNoReverseProxy() config.HeaderManipulationAndAuth = hman } pl, err := pinggy.ConnectWithConfig(config) if err != nil { log.Panicln(err) } log.Println("Addrs: ", pl.RemoteUrls()) //err = pl.InitiateWebDebug("localhost:3424") //log.Println(err) pl.StartForwarding() } ================================================ FILE: examples/onvif_client/README.md ================================================ ## ONVIF Client ```shell go run examples/onvif_client/main.go http://admin:password@192.168.10.90 GetAudioEncoderConfigurations ``` ================================================ FILE: examples/onvif_client/main.go ================================================ package main import ( "log" "net/url" "os" "github.com/AlexxIT/go2rtc/pkg/onvif" ) func main() { var rawURL = os.Args[1] var operation = os.Args[2] var token string if len(os.Args) > 3 { token = os.Args[3] } client, err := onvif.NewClient(rawURL) if err != nil { log.Panic(err) } var b []byte switch operation { case onvif.ServiceGetServiceCapabilities: b, err = client.MediaRequest(operation) case onvif.DeviceGetCapabilities, onvif.DeviceGetDeviceInformation, onvif.DeviceGetDiscoveryMode, onvif.DeviceGetDNS, onvif.DeviceGetHostname, onvif.DeviceGetNetworkDefaultGateway, onvif.DeviceGetNetworkInterfaces, onvif.DeviceGetNetworkProtocols, onvif.DeviceGetNTP, onvif.DeviceGetScopes, onvif.DeviceGetServices, onvif.DeviceGetSystemDateAndTime, onvif.DeviceSystemReboot: b, err = client.DeviceRequest(operation) case onvif.MediaGetProfiles, onvif.MediaGetVideoEncoderConfigurations, onvif.MediaGetVideoSources, onvif.MediaGetVideoSourceConfigurations, onvif.MediaGetAudioEncoderConfigurations, onvif.MediaGetAudioSources, onvif.MediaGetAudioSourceConfigurations: b, err = client.MediaRequest(operation) case onvif.MediaGetProfile: b, err = client.GetProfile(token) case onvif.MediaGetVideoSourceConfiguration: b, err = client.GetVideoSourceConfiguration(token) case onvif.MediaGetStreamUri: b, err = client.GetStreamUri(token) case onvif.MediaGetSnapshotUri: b, err = client.GetSnapshotUri(token) default: log.Printf("unknown action\n") } if err != nil { log.Printf("%s\n", err) } u, err := url.Parse(rawURL) if err != nil { log.Fatal(err) } if err = os.WriteFile(u.Hostname()+"_"+operation+".xml", b, 0644); err != nil { log.Printf("%s\n", err) } } ================================================ FILE: examples/rtsp_client/main.go ================================================ package main import ( "log" "os" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/rtsp" "github.com/AlexxIT/go2rtc/pkg/shell" ) func main() { client := rtsp.NewClient(os.Args[1]) if err := client.Dial(); err != nil { log.Panic(err) } client.Medias = []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ {Name: core.CodecPCMU, ClockRate: 8000}, }, ID: "streamid=0", }, } if err := client.Announce(); err != nil { log.Panic(err) } if _, err := client.SetupMedia(client.Medias[0]); err != nil { log.Panic(err) } if err := client.Record(); err != nil { log.Panic(err) } shell.RunUntilSignal() } ================================================ FILE: examples/tutk_decoder/README.md ================================================ # tutk_decoder 1. Wireshark > Select any packet > Follow > UDP Stream 2. Wireshark > File > Export Packet Dissections > As JSON > Displayed, Values 3. `tutk_decoder wireshark.json decoded.txt` ================================================ FILE: examples/tutk_decoder/main.go ================================================ package main import ( "encoding/hex" "encoding/json" "fmt" "log" "os" "strings" "github.com/AlexxIT/go2rtc/pkg/tutk" ) func main() { if len(os.Args) != 3 { fmt.Println("Usage: tutk_decoder wireshark.json decoded.txt") return } src, err := os.Open(os.Args[1]) if err != nil { log.Fatal(err) } defer src.Close() dst, err := os.Create(os.Args[2]) if err != nil { log.Fatal(err) } defer dst.Close() var items []item if err = json.NewDecoder(src).Decode(&items); err != nil { log.Fatal(err) } var b []byte for _, v := range items { if v.Source.Layers.Data.DataData == "" { continue } s := strings.ReplaceAll(v.Source.Layers.Data.DataData, ":", "") b, err = hex.DecodeString(s) if err != nil { log.Fatal(err) } tutk.ReverseTransCodePartial(b, b) ts := v.Source.Layers.Frame.FrameTimeRelative _, _ = fmt.Fprintf(dst, "%8s: %s -> %s [%4d] %x\n", ts[:len(ts)-6], v.Source.Layers.Ip.IpSrc, v.Source.Layers.Ip.IpDst, len(b), b) } } type item struct { Source struct { Layers struct { Frame struct { FrameTimeRelative string `json:"frame.time_relative"` FrameNumber string `json:"frame.number"` } `json:"frame"` Ip struct { IpSrc string `json:"ip.src"` IpDst string `json:"ip.dst"` } `json:"ip"` Udp struct { UdpSrcport string `json:"udp.srcport"` UdpDstport string `json:"udp.dstport"` } `json:"udp"` Data struct { DataData string `json:"data.data"` DataLen string `json:"data.len"` } `json:"data"` } `json:"layers"` } `json:"_source"` } ================================================ FILE: go.mod ================================================ module github.com/AlexxIT/go2rtc go 1.24.0 require ( github.com/asticode/go-astits v1.14.0 github.com/eclipse/paho.mqtt.golang v1.5.1 github.com/expr-lang/expr v1.17.7 github.com/google/uuid v1.6.0 github.com/gorilla/websocket v1.5.3 github.com/mattn/go-isatty v0.0.20 github.com/miekg/dns v1.1.70 github.com/pion/dtls/v3 v3.0.10 github.com/pion/ice/v4 v4.2.0 github.com/pion/interceptor v0.1.43 github.com/pion/rtcp v1.2.16 github.com/pion/rtp v1.10.0 github.com/pion/sdp/v3 v3.0.17 github.com/pion/srtp/v3 v3.0.10 github.com/pion/stun/v3 v3.1.1 github.com/pion/webrtc/v4 v4.2.3 github.com/rs/zerolog v1.34.0 github.com/sigurn/crc16 v0.0.0-20240131213347-83fcde1e29d1 github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f github.com/stretchr/testify v1.11.1 github.com/tadglines/go-pkgs v0.0.0-20210623144937-b983b20f54f9 golang.org/x/crypto v0.47.0 golang.org/x/net v0.49.0 gopkg.in/yaml.v3 v3.0.1 ) require ( github.com/asticode/go-astikit v0.57.1 // indirect github.com/davecgh/go-spew v1.1.1 // indirect github.com/kr/pretty v0.3.1 // indirect github.com/mattn/go-colorable v0.1.14 // indirect github.com/pion/datachannel v1.6.0 // indirect github.com/pion/logging v0.2.4 // indirect github.com/pion/mdns/v2 v2.1.0 // indirect github.com/pion/randutil v0.1.0 // indirect github.com/pion/sctp v1.9.2 // indirect github.com/pion/transport/v3 v3.1.1 // indirect github.com/pion/transport/v4 v4.0.1 // indirect github.com/pion/turn/v4 v4.1.4 // indirect github.com/pmezard/go-difflib v1.0.0 // indirect github.com/wlynxg/anet v0.0.5 // indirect golang.org/x/mod v0.32.0 // indirect golang.org/x/sync v0.19.0 // indirect golang.org/x/sys v0.40.0 // indirect golang.org/x/time v0.14.0 // indirect golang.org/x/tools v0.41.0 // indirect ) ================================================ FILE: go.sum ================================================ github.com/asticode/go-astikit v0.30.0/go.mod h1:h4ly7idim1tNhaVkdVBeXQZEE3L0xblP7fCWbgwipF0= github.com/asticode/go-astikit v0.57.1 h1:fEykwH98Nny08kcRbk4uer+S8h0rKveCIpG9F6NVLuA= github.com/asticode/go-astikit v0.57.1/go.mod h1:fV43j20UZYfXzP9oBn33udkvCvDvCDhzjVqoLFuuYZE= github.com/asticode/go-astits v1.14.0 h1:zkgnZzipx2XX5mWycqsSBeEyDH58+i4HtyF4j2ROb00= github.com/asticode/go-astits v1.14.0/go.mod h1:QSHmknZ51pf6KJdHKZHJTLlMegIrhega3LPWz3ND/iI= github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/eclipse/paho.mqtt.golang v1.5.1 h1:/VSOv3oDLlpqR2Epjn1Q7b2bSTplJIeV2ISgCl2W7nE= github.com/eclipse/paho.mqtt.golang v1.5.1/go.mod h1:1/yJCneuyOoCOzKSsOTUc0AJfpsItBGWvYpBLimhArU= github.com/expr-lang/expr v1.17.6 h1:1h6i8ONk9cexhDmowO/A64VPxHScu7qfSl2k8OlINec= github.com/expr-lang/expr v1.17.6/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4= github.com/expr-lang/expr v1.17.7 h1:Q0xY/e/2aCIp8g9s/LGvMDCC5PxYlvHgDZRQ4y16JX8= github.com/expr-lang/expr v1.17.7/go.mod h1:8/vRC7+7HBzESEqt5kKpYXxrxkr31SaO8r40VO/1IT4= github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= github.com/gorilla/websocket v1.5.3 h1:saDtZ6Pbx/0u+bgYQ3q96pZgCzfhKXGPqt7kZ72aNNg= github.com/gorilla/websocket v1.5.3/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHPsaIE= github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.19/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/miekg/dns v1.1.69 h1:Kb7Y/1Jo+SG+a2GtfoFUfDkG//csdRPwRLkCsxDG9Sc= github.com/miekg/dns v1.1.69/go.mod h1:7OyjD9nEba5OkqQ/hB4fy3PIoxafSZJtducccIelz3g= github.com/miekg/dns v1.1.70 h1:DZ4u2AV35VJxdD9Fo9fIWm119BsQL5cZU1cQ9s0LkqA= github.com/miekg/dns v1.1.70/go.mod h1:+EuEPhdHOsfk6Wk5TT2CzssZdqkmFhf8r+aVyDEToIs= github.com/pion/datachannel v1.5.10 h1:ly0Q26K1i6ZkGf42W7D4hQYR90pZwzFOjTq5AuCKk4o= github.com/pion/datachannel v1.5.10/go.mod h1:p/jJfC9arb29W7WrxyKbepTU20CFgyx5oLo8Rs4Py/M= github.com/pion/datachannel v1.6.0 h1:XecBlj+cvsxhAMZWFfFcPyUaDZtd7IJvrXqlXD/53i0= github.com/pion/datachannel v1.6.0/go.mod h1:ur+wzYF8mWdC+Mkis5Thosk+u/VOL287apDNEbFpsIk= github.com/pion/dtls/v3 v3.0.9 h1:4AijfFRm8mAjd1gfdlB1wzJF3fjjR/VPIpJgkEtvYmM= github.com/pion/dtls/v3 v3.0.9/go.mod h1:abApPjgadS/ra1wvUzHLc3o2HvoxppAh+NZkyApL4Os= github.com/pion/dtls/v3 v3.0.10 h1:k9ekkq1kaZoxnNEbyLKI8DI37j/Nbk1HWmMuywpQJgg= github.com/pion/dtls/v3 v3.0.10/go.mod h1:YEmmBYIoBsY3jmG56dsziTv/Lca9y4Om83370CXfqJ8= github.com/pion/ice/v4 v4.1.0 h1:YlxIii2bTPWyC08/4hdmtYq4srbrY0T9xcTsTjldGqU= github.com/pion/ice/v4 v4.1.0/go.mod h1:5gPbzYxqenvn05k7zKPIZFuSAufolygiy6P1U9HzvZ4= github.com/pion/ice/v4 v4.2.0 h1:jJC8S+CvXCCvIQUgx+oNZnoUpt6zwc34FhjWwCU4nlw= github.com/pion/ice/v4 v4.2.0/go.mod h1:EgjBGxDgmd8xB0OkYEVFlzQuEI7kWSCFu+mULqaisy4= github.com/pion/interceptor v0.1.42 h1:0/4tvNtruXflBxLfApMVoMubUMik57VZ+94U0J7cmkQ= github.com/pion/interceptor v0.1.42/go.mod h1:g6XYTChs9XyolIQFhRHOOUS+bGVGLRfgTCUzH29EfVU= github.com/pion/interceptor v0.1.43 h1:6hmRfnmjogSs300xfkR0JxYFZ9k5blTEvCD7wxEDuNQ= github.com/pion/interceptor v0.1.43/go.mod h1:BSiC1qKIJt1XVr3l3xQ2GEmCFStk9tx8fwtCZxxgR7M= github.com/pion/logging v0.2.4 h1:tTew+7cmQ+Mc1pTBLKH2puKsOvhm32dROumOZ655zB8= github.com/pion/logging v0.2.4/go.mod h1:DffhXTKYdNZU+KtJ5pyQDjvOAh/GsNSyv1lbkFbe3so= github.com/pion/mdns/v2 v2.1.0 h1:3IJ9+Xio6tWYjhN6WwuY142P/1jA0D5ERaIqawg/fOY= github.com/pion/mdns/v2 v2.1.0/go.mod h1:pcez23GdynwcfRU1977qKU0mDxSeucttSHbCSfFOd9A= github.com/pion/randutil v0.1.0 h1:CFG1UdESneORglEsnimhUjf33Rwjubwj6xfiOXBa3mA= github.com/pion/randutil v0.1.0/go.mod h1:XcJrSMMbbMRhASFVOlj/5hQial/Y8oH/HVo7TBZq+j8= github.com/pion/rtcp v1.2.16 h1:fk1B1dNW4hsI78XUCljZJlC4kZOPk67mNRuQ0fcEkSo= github.com/pion/rtcp v1.2.16/go.mod h1:/as7VKfYbs5NIb4h6muQ35kQF/J0ZVNz2Z3xKoCBYOo= github.com/pion/rtp v1.8.26 h1:VB+ESQFQhBXFytD+Gk8cxB6dXeVf2WQzg4aORvAvAAc= github.com/pion/rtp v1.8.26/go.mod h1:rF5nS1GqbR7H/TCpKwylzeq6yDM+MM6k+On5EgeThEM= github.com/pion/rtp v1.10.0 h1:XN/xca4ho6ZEcijpdF2VGFbwuHUfiIMf3ew8eAAE43w= github.com/pion/rtp v1.10.0/go.mod h1:rF5nS1GqbR7H/TCpKwylzeq6yDM+MM6k+On5EgeThEM= github.com/pion/sctp v1.8.41 h1:20R4OHAno4Vky3/iE4xccInAScAa83X6nWUfyc65MIs= github.com/pion/sctp v1.8.41/go.mod h1:2wO6HBycUH7iCssuGyc2e9+0giXVW0pyCv3ZuL8LiyY= github.com/pion/sctp v1.9.2 h1:HxsOzEV9pWoeggv7T5kewVkstFNcGvhMPx0GvUOUQXo= github.com/pion/sctp v1.9.2/go.mod h1:OTOlsQ5EDQ6mQ0z4MUGXt2CgQmKyafBEXhUVqLRB6G8= github.com/pion/sdp/v3 v3.0.16 h1:0dKzYO6gTAvuLaAKQkC02eCPjMIi4NuAr/ibAwrGDCo= github.com/pion/sdp/v3 v3.0.16/go.mod h1:9tyKzznud3qiweZcD86kS0ff1pGYB3VX+Bcsmkx6IXo= github.com/pion/sdp/v3 v3.0.17 h1:9SfLAW/fF1XC8yRqQ3iWGzxkySxup4k4V7yN8Fs8nuo= github.com/pion/sdp/v3 v3.0.17/go.mod h1:9tyKzznud3qiweZcD86kS0ff1pGYB3VX+Bcsmkx6IXo= github.com/pion/srtp/v3 v3.0.9 h1:lRGF4G61xxj+m/YluB3ZnBpiALSri2lTzba0kGZMrQY= github.com/pion/srtp/v3 v3.0.9/go.mod h1:E+AuWd7Ug2Fp5u38MKnhduvpVkveXJX6J4Lq4rxUYt8= github.com/pion/srtp/v3 v3.0.10 h1:tFirkpBb3XccP5VEXLi50GqXhv5SKPxqrdlhDCJlZrQ= github.com/pion/srtp/v3 v3.0.10/go.mod h1:3mOTIB0cq9qlbn59V4ozvv9ClW/BSEbRp4cY0VtaR7M= github.com/pion/stun/v3 v3.0.2 h1:BJuGEN2oLrJisiNEJtUTJC4BGbzbfp37LizfqswblFU= github.com/pion/stun/v3 v3.0.2/go.mod h1:JFJKfIWvt178MCF5H/YIgZ4VX3LYE77vca4b9HP60SA= github.com/pion/stun/v3 v3.1.1 h1:CkQxveJ4xGQjulGSROXbXq94TAWu8gIX2dT+ePhUkqw= github.com/pion/stun/v3 v3.1.1/go.mod h1:qC1DfmcCTQjl9PBaMa5wSn3x9IPmKxSdcCsxBcDBndM= github.com/pion/transport/v3 v3.1.1 h1:Tr684+fnnKlhPceU+ICdrw6KKkTms+5qHMgw6bIkYOM= github.com/pion/transport/v3 v3.1.1/go.mod h1:+c2eewC5WJQHiAA46fkMMzoYZSuGzA/7E2FPrOYHctQ= github.com/pion/transport/v4 v4.0.1 h1:sdROELU6BZ63Ab7FrOLn13M6YdJLY20wldXW2Cu2k8o= github.com/pion/transport/v4 v4.0.1/go.mod h1:nEuEA4AD5lPdcIegQDpVLgNoDGreqM/YqmEx3ovP4jM= github.com/pion/turn/v4 v4.1.3 h1:jVNW0iR05AS94ysEtvzsrk3gKs9Zqxf6HmnsLfRvlzA= github.com/pion/turn/v4 v4.1.3/go.mod h1:TD/eiBUf5f5LwXbCJa35T7dPtTpCHRJ9oJWmyPLVT3A= github.com/pion/turn/v4 v4.1.4 h1:EU11yMXKIsK43FhcUnjLlrhE4nboHZq+TXBIi3QpcxQ= github.com/pion/turn/v4 v4.1.4/go.mod h1:ES1DXVFKnOhuDkqn9hn5VJlSWmZPaRJLyBXoOeO/BmQ= github.com/pion/webrtc/v4 v4.1.8 h1:ynkjfiURDQ1+8EcJsoa60yumHAmyeYjz08AaOuor+sk= github.com/pion/webrtc/v4 v4.1.8/go.mod h1:KVaARG2RN0lZx0jc7AWTe38JpPv+1/KicOZ9jN52J/s= github.com/pion/webrtc/v4 v4.2.3 h1:RtdWDnkenNQGxUrZqWa5gSkTm5ncsLg5d+zu0M4cXt4= github.com/pion/webrtc/v4 v4.2.3/go.mod h1:7vsyFzRzaKP5IELUnj8zLcglPyIT6wWwqTppBZ1k6Kc= github.com/pkg/diff v0.0.0-20210226163009-20ebb0f2a09e/go.mod h1:pJLUxLENpZxwdsKMEsNbx1VGcRFpLqf3715MtcvvzbA= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/profile v1.4.0/go.mod h1:NWz/XGvpEW1FyYQ7fCx4dqYBLlfTcE+A9FLAkNKqjFE= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8= github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs= github.com/rs/xid v1.6.0/go.mod h1:7XoLgs4eV+QndskICGsho+ADou8ySMSjJKDIan90Nz0= github.com/rs/zerolog v1.34.0 h1:k43nTLIwcTVQAncfCw4KZ2VY6ukYoZaBPNOE8txlOeY= github.com/rs/zerolog v1.34.0/go.mod h1:bJsvje4Z08ROH4Nhs5iH600c3IkWhwp44iRc54W6wYQ= github.com/sigurn/crc16 v0.0.0-20240131213347-83fcde1e29d1 h1:NVK+OqnavpyFmUiKfUMHrpvbCi2VFoWTrcpI7aDaJ2I= github.com/sigurn/crc16 v0.0.0-20240131213347-83fcde1e29d1/go.mod h1:9/etS5gpQq9BJsJMWg1wpLbfuSnkm8dPF6FdW2JXVhA= github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f h1:1R9KdKjCNSd7F8iGTxIpoID9prlYH8nuNYKt0XvweHA= github.com/sigurn/crc8 v0.0.0-20220107193325-2243fe600f9f/go.mod h1:vQhwQ4meQEDfahT5kd61wLAF5AAeh5ZPLVI4JJ/tYo8= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= github.com/stretchr/testify v1.11.1 h1:7s2iGBzp5EwR7/aIZr8ao5+dra3wiQyKjjFuvgVKu7U= github.com/stretchr/testify v1.11.1/go.mod h1:wZwfW3scLgRK+23gO65QZefKpKQRnfz6sD981Nm4B6U= github.com/tadglines/go-pkgs v0.0.0-20210623144937-b983b20f54f9 h1:aeN+ghOV0b2VCmKKO3gqnDQ8mLbpABZgRR2FVYx4ouI= github.com/tadglines/go-pkgs v0.0.0-20210623144937-b983b20f54f9/go.mod h1:roo6cZ/uqpwKMuvPG0YmzI5+AmUiMWfjCBZpGXqbTxE= github.com/wlynxg/anet v0.0.5 h1:J3VJGi1gvo0JwZ/P1/Yc/8p63SoW98B5dHkYDmpgvvU= github.com/wlynxg/anet v0.0.5/go.mod h1:eay5PRQr7fIVAMbTbchTnO9gG65Hg/uYGdc7mguHxoA= golang.org/x/crypto v0.46.0 h1:cKRW/pmt1pKAfetfu+RCEvjvZkA9RimPbh7bhFjGVBU= golang.org/x/crypto v0.46.0/go.mod h1:Evb/oLKmMraqjZ2iQTwDwvCtJkczlDuTmdJXoZVzqU0= golang.org/x/crypto v0.47.0 h1:V6e3FRj+n4dbpw86FJ8Fv7XVOql7TEwpHapKoMJ/GO8= golang.org/x/crypto v0.47.0/go.mod h1:ff3Y9VzzKbwSSEzWqJsJVBnWmRwRSHt/6Op5n9bQc4A= golang.org/x/mod v0.31.0 h1:HaW9xtz0+kOcWKwli0ZXy79Ix+UW/vOfmWI5QVd2tgI= golang.org/x/mod v0.31.0/go.mod h1:43JraMp9cGx1Rx3AqioxrbrhNsLl2l/iNAvuBkrezpg= golang.org/x/mod v0.32.0 h1:9F4d3PHLljb6x//jOyokMv3eX+YDeepZSEo3mFJy93c= golang.org/x/mod v0.32.0/go.mod h1:SgipZ/3h2Ci89DlEtEXWUk/HteuRin+HHhN+WbNhguU= golang.org/x/net v0.48.0 h1:zyQRTTrjc33Lhh0fBgT/H3oZq9WuvRR5gPC70xpDiQU= golang.org/x/net v0.48.0/go.mod h1:+ndRgGjkh8FGtu1w1FGbEC31if4VrNVMuKTgcAAnQRY= golang.org/x/net v0.49.0 h1:eeHFmOGUTtaaPSGNmjBKpbng9MulQsJURQUAfUwY++o= golang.org/x/net v0.49.0/go.mod h1:/ysNB2EvaqvesRkuLAyjI1ycPZlQHM3q01F02UY/MV8= golang.org/x/sync v0.19.0 h1:vV+1eWNmZ5geRlYjzm2adRgW2/mcpevXNg50YZtPCE4= golang.org/x/sync v0.19.0/go.mod h1:9KTHXmSnoGruLpwFjVSX0lNNA75CykiMECbovNTZqGI= golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.12.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.39.0 h1:CvCKL8MeisomCi6qNZ+wbb0DN9E5AATixKsvNtMoMFk= golang.org/x/sys v0.39.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/sys v0.40.0 h1:DBZZqJ2Rkml6QMQsZywtnjnnGvHza6BTfYFWY9kjEWQ= golang.org/x/sys v0.40.0/go.mod h1:OgkHotnGiDImocRcuBABYBEXf8A9a87e/uXjp9XT3ks= golang.org/x/term v0.38.0 h1:PQ5pkm/rLO6HnxFR7N2lJHOZX6Kez5Y1gDSJla6jo7Q= golang.org/x/term v0.38.0/go.mod h1:bSEAKrOT1W+VSu9TSCMtoGEOUcKxOKgl3LE5QEF/xVg= golang.org/x/term v0.39.0 h1:RclSuaJf32jOqZz74CkPA9qFuVTX7vhLlpfj/IGWlqY= golang.org/x/time v0.14.0 h1:MRx4UaLrDotUKUdCIqzPC48t1Y9hANFKIRpNx+Te8PI= golang.org/x/time v0.14.0/go.mod h1:eL/Oa2bBBK0TkX57Fyni+NgnyQQN4LitPmob2Hjnqw4= golang.org/x/tools v0.40.0 h1:yLkxfA+Qnul4cs9QA3KnlFu0lVmd8JJfoq+E41uSutA= golang.org/x/tools v0.40.0/go.mod h1:Ik/tzLRlbscWpqqMRjyWYDisX8bG13FrdXp3o4Sr9lc= golang.org/x/tools v0.41.0 h1:a9b8iMweWG+S0OBnlU36rzLp20z1Rp10w+IY2czHTQc= golang.org/x/tools v0.41.0/go.mod h1:XSY6eDqxVNiYgezAVqqCeihT4j1U2CCsqvH3WhQpnlg= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo= gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= ================================================ FILE: internal/README.md ================================================ # Modules go2rtc tries to name formats, protocols and codecs the same way they are named in FFmpeg. Some formats and protocols go2rtc supports exclusively. They have no equivalent in FFmpeg. - The [`echo`], [`expr`], [`hass`] and [`onvif`] modules receive a link to a stream. They don't know the protocol in advance. - The [`exec`] and [`ffmpeg`] modules support many formats. They are identical to the [`http`] module. - The [`api`], [`app`], [`debug`], [`ngrok`], [`pinggy`], [`srtp`], [`streams`] are supporting modules. **Modules** implement communication APIs: authorization, encryption, command set, structure of media packets. **Formats** describe the structure of the data being transmitted. **Protocols** implement transport for data transmission. | module | formats | protocols | input | output | ingest | two-way | |----------------|-----------------|------------------|-------|--------|--------|---------| | [`alsa`] | `pcm` | `ioctl` | yes | | | | | [`bubble`] | - | `http` | yes | | | | | [`doorbird`] | `mulaw` | `http` | yes | | | yes | | [`dvrip`] | - | `tcp` | yes | | | yes | | [`echo`] | * | * | yes | | | | | [`eseecloud`] | `rtp` | `http` | yes | | | | | [`exec`] | * | `pipe`, `rtsp` | yes | | | yes | | [`expr`] | * | * | yes | | | | | [`ffmpeg`] | * | `pipe`, `rtsp` | yes | | | | | [`flussonic`] | `mp4` | `ws` | yes | | | | | [`gopro`] | `mpegts` | `udp` | yes | | | | | [`hass`] | * | * | yes | | | | | [`hls`] | `mpegts`, `mp4` | `http` | | yes | | | | [`homekit`] | `srtp` | `hap` | yes | yes | | no | | [`http`] | `adts` | `http`, `tcp` | yes | | | | | [`http`] | `flv` | `http`, `tcp` | yes | | | | | [`http`] | `h264` | `http`, `tcp` | yes | | | | | [`http`] | `hevc` | `http`, `tcp` | yes | | | | | [`http`] | `hls` | `http`, `tcp` | yes | | | | | [`http`] | `mjpeg` | `http`, `tcp` | yes | | | | | [`http`] | `mpjpeg` | `http` | yes | | | | | [`http`] | `mpegts` | `http`, `tcp` | yes | | | | | [`http`] | `wav` | `http`, `tcp` | yes | | | | | [`http`] | `yuv4mpegpipe` | `http`, `tcp` | yes | | | | | [`isapi`] | `alaw`, `mulaw` | `http` | | | | yes | | [`ivideon`] | `mp4` | `ws` | yes | | | | | [`kasa`] | `h264`, `mulaw` | `http` | yes | | | | | [`mjpeg`] | `ascii` | `http` | | yes | | | | [`mjpeg`] | `jpeg` | `http` | | yes | | | | [`mjpeg`] | `mpjpeg` | `http` | | yes | yes | | | [`mjpeg`] | `yuv4mpegpipe` | `http` | | yes | | | | [`mp4`] | `mp4` | `http`, `ws` | | yes | | | | [`mpeg`] | `adts` | `http` | | yes | | | | [`mpeg`] | `mpegts` | `http` | | yes | yes | | | [`multitrans`] | `rtp` | `tcp` | | | | yes | | [`nest`] | `srtp` | `rtsp`, `webrtc` | yes | | | no | | [`onvif`] | `rtp` | * | yes | yes | | | | [`ring`] | `srtp` | `webrtc` | yes | | | yes | | [`roborock`] | `srtp` | `webrtc` | yes | | | yes | | [`rtmp`] | `flv` | `rtmp` | yes | yes | yes | | | [`rtmp`] | `flv` | `http` | | yes | yes | | | [`rtsp`] | `rtsp` | `rtsp` | yes | yes | yes | yes | | [`tapo`] | `mpegts` | `http` | yes | | | yes | | [`tuya`] | `srtp` | `webrtc` | yes | | | yes | | [`v4l2`] | `rawvideo` | `ioctl` | yes | | | | | [`webrtc`] | `srtp` | `webrtc` | yes | yes | yes | yes | | [`webtorrent`] | `srtp` | `webrtc` | yes | yes | | | | [`wyoming`] | `pcm` | `tcp` | | yes | | | | [`wyze`] | - | `tutk` | yes | | | yes | | [`xiaomi`] | - | `cs2`, `tutk` | yes | | | yes | | [`yandex`] | `srtp` | `webrtc` | yes | | | | [`alsa`]: alsa/README.md [`api`]: api/README.md [`app`]: app/README.md [`bubble`]: bubble/README.md [`debug`]: debug/README.md [`doorbird`]: doorbird/README.md [`dvrip`]: dvrip/README.md [`echo`]: echo/README.md [`eseecloud`]: eseecloud/README.md [`exec`]: exec/README.md [`expr`]: expr/README.md [`ffmpeg`]: ffmpeg/README.md [`flussonic`]: flussonic/README.md [`gopro`]: gopro/README.md [`hass`]: hass/README.md [`hls`]: hls/README.md [`homekit`]: homekit/README.md [`http`]: http/README.md [`isapi`]: isapi/README.md [`ivideon`]: ivideon/README.md [`kasa`]: kasa/README.md [`mjpeg`]: mjpeg/README.md [`mp4`]: mp4/README.md [`mpeg`]: mpeg/README.md [`multitrans`]: multitrans/README.md [`nest`]: nest/README.md [`ngrok`]: ngrok/README.md [`onvif`]: onvif/README.md [`pinggy`]: pinggy/README.md [`ring`]: ring/README.md [`roborock`]: roborock/README.md [`rtmp`]: rtmp/README.md [`rtsp`]: rtsp/README.md [`srtp`]: srtp/README.md [`streams`]: streams/README.md [`tapo`]: tapo/README.md [`tuya`]: tuya/README.md [`v4l2`]: v4l2/README.md [`webrtc`]: webrtc/README.md [`webtorrent`]: webtorrent/README.md [`wyoming`]: wyze/README.md [`wyze`]: wyze/README.md [`xiaomi`]: xiaomi/README.md [`yandex`]: yandex/README.md ================================================ FILE: internal/alsa/README.md ================================================ # ALSA [`new in v1.9.10`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.10) > [!WARNING] > This source is under development and does not always work well. [Advanced Linux Sound Architecture](https://en.wikipedia.org/wiki/Advanced_Linux_Sound_Architecture) - a framework for receiving audio from devices on Linux OS. Easy to add via **WebUI > add > ALSA**. Alternatively, you can use FFmpeg source. ================================================ FILE: internal/alsa/alsa.go ================================================ //go:build !(linux && (386 || amd64 || arm || arm64 || mipsle)) package alsa func Init() { // not supported } ================================================ FILE: internal/alsa/alsa_linux.go ================================================ //go:build linux && (386 || amd64 || arm || arm64 || mipsle) package alsa import ( "fmt" "net/http" "os" "strconv" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/alsa" "github.com/AlexxIT/go2rtc/pkg/alsa/device" ) func Init() { streams.HandleFunc("alsa", alsa.Open) api.HandleFunc("api/alsa", apiAlsa) } func apiAlsa(w http.ResponseWriter, r *http.Request) { files, err := os.ReadDir("/dev/snd/") if err != nil { return } var sources []*api.Source for _, file := range files { if !strings.HasPrefix(file.Name(), "pcm") { continue } path := "/dev/snd/" + file.Name() dev, err := device.Open(path) if err != nil { continue } info, err := dev.Info() if err == nil { formats := formatsToString(dev.ListFormats()) r1, r2 := dev.RangeRates() c1, c2 := dev.RangeChannels() source := &api.Source{ Name: info.ID, Info: fmt.Sprintf("Formats: %s, Rates: %d-%d, Channels: %d-%d", formats, r1, r2, c1, c2), URL: "alsa:device?audio=" + path, } if !strings.Contains(source.Name, info.Name) { source.Name += ", " + info.Name } sources = append(sources, source) } _ = dev.Close() } api.ResponseSources(w, sources) } func formatsToString(formats []byte) string { var s string for i, format := range formats { if i > 0 { s += " " } switch format { case 2: s += "s16le" case 10: s += "s32le" default: s += strconv.Itoa(int(format)) } } return s } ================================================ FILE: internal/api/README.md ================================================ # HTTP API The HTTP API is the main part for interacting with the application. Default address: `http://localhost:1984/`. The HTTP API is described in [OpenAPI](../../website/api/openapi.yaml) format. It can be explored in [interactive viewer](https://go2rtc.org/api/). WebSocket API described [here](ws/README.md). The project's static HTML and JS files are located in the [www](../../www/README.md) folder. An external developer can use them as a basis for integrating go2rtc into their project or for developing a custom web interface for go2rtc. The contents of `www` folder are built into go2rtc when building, but you can use configuration to specify an external folder as the source of static files. ## Configuration **Important!** go2rtc passes requests from localhost and Unix sockets without HTTP authorization, even if you have it configured. It is your responsibility to set up secure external access to the API. If not properly configured, an attacker can gain access to your cameras and even your server. - you can disable HTTP API with `listen: ""` and use, for example, only RTSP client/server protocol - you can enable HTTP API only on localhost with `listen: "127.0.0.1:1984"` setting - you can change the API `base_path` and host go2rtc on your main app webserver suburl - all files from `static_dir` hosted on root path: `/` - you can use raw TLS cert/key content or path to files ```yaml api: listen: ":1984" # default ":1984", HTTP API port ("" - disabled) username: "admin" # default "", Basic auth for WebUI password: "pass" # default "", Basic auth for WebUI local_auth: true # default false, Enable auth check for localhost requests base_path: "/rtc" # default "", API prefix for serving on suburl (/api => /rtc/api) static_dir: "www" # default "", folder for static files (custom web interface) origin: "*" # default "", allow CORS requests (only * supported) tls_listen: ":443" # default "", enable HTTPS server tls_cert: | # default "", PEM-encoded fullchain certificate for HTTPS -----BEGIN CERTIFICATE----- ... -----END CERTIFICATE----- tls_key: | # default "", PEM-encoded private key for HTTPS -----BEGIN PRIVATE KEY----- ... -----END PRIVATE KEY----- unix_listen: "/tmp/go2rtc.sock" # default "", unix socket listener for API ``` **PS:** - MJPEG over WebSocket plays better than native MJPEG because Chrome [bug](https://bugs.chromium.org/p/chromium/issues/detail?id=527446) - MP4 over WebSocket was created only for Apple iOS because it doesn't support file streaming ================================================ FILE: internal/api/api.go ================================================ package api import ( "crypto/tls" "encoding/json" "fmt" "net" "net/http" "os" "slices" "strconv" "strings" "sync" "syscall" "time" "github.com/AlexxIT/go2rtc/internal/app" "github.com/rs/zerolog" ) func Init() { var cfg struct { Mod struct { Listen string `yaml:"listen"` Username string `yaml:"username"` Password string `yaml:"password"` LocalAuth bool `yaml:"local_auth"` BasePath string `yaml:"base_path"` StaticDir string `yaml:"static_dir"` Origin string `yaml:"origin"` TLSListen string `yaml:"tls_listen"` TLSCert string `yaml:"tls_cert"` TLSKey string `yaml:"tls_key"` UnixListen string `yaml:"unix_listen"` AllowPaths []string `yaml:"allow_paths"` } `yaml:"api"` } // default config cfg.Mod.Listen = ":1984" // load config from YAML app.LoadConfig(&cfg) if cfg.Mod.Listen == "" && cfg.Mod.UnixListen == "" && cfg.Mod.TLSListen == "" { return } allowPaths = cfg.Mod.AllowPaths basePath = cfg.Mod.BasePath log = app.GetLogger("api") initStatic(cfg.Mod.StaticDir) HandleFunc("api", apiHandler) HandleFunc("api/config", configHandler) HandleFunc("api/exit", exitHandler) HandleFunc("api/restart", restartHandler) HandleFunc("api/log", logHandler) Handler = http.DefaultServeMux // 4th if cfg.Mod.Origin == "*" { Handler = middlewareCORS(Handler) // 3rd } if cfg.Mod.Username != "" { Handler = middlewareAuth(cfg.Mod.Username, cfg.Mod.Password, cfg.Mod.LocalAuth, Handler) // 2nd } if log.Trace().Enabled() { Handler = middlewareLog(Handler) // 1st } if cfg.Mod.Listen != "" { _, port, _ := net.SplitHostPort(cfg.Mod.Listen) Port, _ = strconv.Atoi(port) go listen("tcp", cfg.Mod.Listen) } if cfg.Mod.UnixListen != "" { _ = syscall.Unlink(cfg.Mod.UnixListen) go listen("unix", cfg.Mod.UnixListen) } // Initialize the HTTPS server if cfg.Mod.TLSListen != "" && cfg.Mod.TLSCert != "" && cfg.Mod.TLSKey != "" { go tlsListen("tcp", cfg.Mod.TLSListen, cfg.Mod.TLSCert, cfg.Mod.TLSKey) } } func listen(network, address string) { ln, err := net.Listen(network, address) if err != nil { log.Error().Err(err).Msg("[api] listen") return } log.Info().Str("addr", address).Msg("[api] listen") server := http.Server{ Handler: Handler, ReadHeaderTimeout: 5 * time.Second, // Example: Set to 5 seconds } if err = server.Serve(ln); err != nil { log.Fatal().Err(err).Msg("[api] serve") } } func tlsListen(network, address, certFile, keyFile string) { var cert tls.Certificate var err error if strings.IndexByte(certFile, '\n') < 0 && strings.IndexByte(keyFile, '\n') < 0 { // check if file path cert, err = tls.LoadX509KeyPair(certFile, keyFile) } else { // if text file content cert, err = tls.X509KeyPair([]byte(certFile), []byte(keyFile)) } if err != nil { log.Error().Err(err).Caller().Send() return } ln, err := net.Listen(network, address) if err != nil { log.Error().Err(err).Msg("[api] tls listen") return } log.Info().Str("addr", address).Msg("[api] tls listen") server := &http.Server{ Handler: Handler, TLSConfig: &tls.Config{Certificates: []tls.Certificate{cert}}, ReadHeaderTimeout: 5 * time.Second, } if err = server.ServeTLS(ln, "", ""); err != nil { log.Fatal().Err(err).Msg("[api] tls serve") } } var Port int const ( MimeJSON = "application/json" MimeText = "text/plain" ) var Handler http.Handler // HandleFunc handle pattern with relative path: // - "api/streams" => "{basepath}/api/streams" // - "/streams" => "/streams" func HandleFunc(pattern string, handler http.HandlerFunc) { if len(pattern) == 0 || pattern[0] != '/' { pattern = basePath + "/" + pattern } if allowPaths != nil && !slices.Contains(allowPaths, pattern) { log.Trace().Str("path", pattern).Msg("[api] ignore path not in allow_paths") return } log.Trace().Str("path", pattern).Msg("[api] register path") http.HandleFunc(pattern, handler) } // ResponseJSON important always add Content-Type // so go won't need to call http.DetectContentType func ResponseJSON(w http.ResponseWriter, v any) { w.Header().Set("Content-Type", MimeJSON) _ = json.NewEncoder(w).Encode(v) } func ResponsePrettyJSON(w http.ResponseWriter, v any) { w.Header().Set("Content-Type", MimeJSON) enc := json.NewEncoder(w) enc.SetIndent("", " ") _ = enc.Encode(v) } func Response(w http.ResponseWriter, body any, contentType string) { w.Header().Set("Content-Type", contentType) switch v := body.(type) { case []byte: _, _ = w.Write(v) case string: _, _ = w.Write([]byte(v)) default: _, _ = fmt.Fprint(w, body) } } const StreamNotFound = "stream not found" var allowPaths []string var basePath string var log zerolog.Logger func middlewareLog(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { log.Trace().Msgf("[api] %s %s %s", r.Method, r.URL, r.RemoteAddr) next.ServeHTTP(w, r) }) } func isLoopback(remoteAddr string) bool { return strings.HasPrefix(remoteAddr, "127.") || strings.HasPrefix(remoteAddr, "[::1]") || remoteAddr == "@" } func middlewareAuth(username, password string, localAuth bool, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if localAuth || !isLoopback(r.RemoteAddr) { user, pass, ok := r.BasicAuth() if !ok || user != username || pass != password { w.Header().Set("Www-Authenticate", `Basic realm="go2rtc"`) http.Error(w, "Unauthorized", http.StatusUnauthorized) return } } next.ServeHTTP(w, r) }) } func middlewareCORS(next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS") w.Header().Set("Access-Control-Allow-Headers", "Authorization, Content-Type") next.ServeHTTP(w, r) }) } var mu sync.Mutex func apiHandler(w http.ResponseWriter, r *http.Request) { mu.Lock() app.Info["host"] = r.Host mu.Unlock() ResponseJSON(w, app.Info) } func exitHandler(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, "", http.StatusBadRequest) return } s := r.URL.Query().Get("code") code, err := strconv.Atoi(s) // https://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02 if err != nil || code < 0 || code > 125 { http.Error(w, "Code must be in the range [0, 125]", http.StatusBadRequest) return } os.Exit(code) } func restartHandler(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, "", http.StatusBadRequest) return } path, err := os.Executable() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } log.Debug().Msgf("[api] restart %s", path) go syscall.Exec(path, os.Args, os.Environ()) } func logHandler(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": // Send current state of the log file immediately w.Header().Set("Content-Type", "application/jsonlines") _, _ = app.MemoryLog.WriteTo(w) case "DELETE": app.MemoryLog.Reset() Response(w, "OK", "text/plain") default: http.Error(w, "Method not allowed", http.StatusBadRequest) } } type Source struct { ID string `json:"id,omitempty"` Name string `json:"name,omitempty"` Info string `json:"info,omitempty"` URL string `json:"url,omitempty"` Location string `json:"location,omitempty"` } func ResponseSources(w http.ResponseWriter, sources []*Source) { if len(sources) == 0 { http.Error(w, "no sources", http.StatusNotFound) return } var response = struct { Sources []*Source `json:"sources"` }{ Sources: sources, } ResponseJSON(w, response) } func Error(w http.ResponseWriter, err error) { log.Error().Err(err).Caller(1).Send() http.Error(w, err.Error(), http.StatusInsufficientStorage) } ================================================ FILE: internal/api/config.go ================================================ package api import ( "io" "net/http" "os" "github.com/AlexxIT/go2rtc/internal/app" "gopkg.in/yaml.v3" ) func configHandler(w http.ResponseWriter, r *http.Request) { if app.ConfigPath == "" { http.Error(w, "", http.StatusGone) return } switch r.Method { case "GET": data, err := os.ReadFile(app.ConfigPath) if err != nil { http.Error(w, "", http.StatusNotFound) return } // https://www.ietf.org/archive/id/draft-ietf-httpapi-yaml-mediatypes-00.html Response(w, data, "application/yaml") case "POST", "PATCH": data, err := io.ReadAll(r.Body) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } if r.Method == "PATCH" { // no need to validate after merge data, err = mergeYAML(app.ConfigPath, data) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } } else { // validate config if err = yaml.Unmarshal(data, map[string]any{}); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } } if err = os.WriteFile(app.ConfigPath, data, 0644); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } } } func mergeYAML(file1 string, yaml2 []byte) ([]byte, error) { // Read the contents of the first YAML file data1, err := os.ReadFile(file1) if err != nil { return nil, err } // Unmarshal the first YAML file into a map var config1 map[string]any if err = yaml.Unmarshal(data1, &config1); err != nil { return nil, err } // Unmarshal the second YAML document into a map var config2 map[string]any if err = yaml.Unmarshal(yaml2, &config2); err != nil { return nil, err } // Merge the two maps config1 = merge(config1, config2) // Marshal the merged map into YAML return yaml.Marshal(&config1) } func merge(dst, src map[string]any) map[string]any { for k, v := range src { if vv, ok := dst[k]; ok { switch vv := vv.(type) { case map[string]any: v := v.(map[string]any) dst[k] = merge(vv, v) case []any: v := v.([]any) dst[k] = v default: dst[k] = v } } else { dst[k] = v } } return dst } ================================================ FILE: internal/api/static.go ================================================ package api import ( "net/http" "github.com/AlexxIT/go2rtc/www" ) func initStatic(staticDir string) { var root http.FileSystem if staticDir != "" { log.Info().Str("dir", staticDir).Msg("[api] serve static") root = http.Dir(staticDir) } else { root = http.FS(www.Static) } base := len(basePath) fileServer := http.FileServer(root) HandleFunc("", func(w http.ResponseWriter, r *http.Request) { if base > 0 { r.URL.Path = r.URL.Path[base:] } fileServer.ServeHTTP(w, r) }) } ================================================ FILE: internal/api/ws/README.md ================================================ # WebSocket Endpoint: `/api/ws` Query parameters: - `src` (required) - Stream name ### WebRTC Request SDP: ```json {"type":"webrtc/offer","value":"v=0\r\n..."} ``` Response SDP: ```json {"type":"webrtc/answer","value":"v=0\r\n..."} ``` Request/response candidate: - empty value also allowed and optional ```json {"type":"webrtc/candidate","value":"candidate:3277516026 1 udp 2130706431 192.168.1.123 54321 typ host"} ``` ### MSE Request: - codecs list optional ```json {"type":"mse","value":"avc1.640029,avc1.64002A,avc1.640033,hvc1.1.6.L153.B0,mp4a.40.2,mp4a.40.5,flac,opus"} ``` Response: ```json {"type":"mse","value":"video/mp4; codecs=\"avc1.64001F,mp4a.40.2\""} ``` ### HLS Request: ```json {"type":"hls","value":"avc1.640029,avc1.64002A,avc1.640033,hvc1.1.6.L153.B0,mp4a.40.2,mp4a.40.5,flac"} ``` Response: - you MUST rewrite full HTTP path to `http://192.168.1.123:1984/api/hls/playlist.m3u8` ```json {"type":"hls","value":"#EXTM3U\n#EXT-X-STREAM-INF:BANDWIDTH=1000000,CODECS=\"avc1.64001F,mp4a.40.2\"\nhls/playlist.m3u8?id=DvmHdd9w"} ``` ### MJPEG Request/response: ```json {"type":"mjpeg"} ``` ================================================ FILE: internal/api/ws/ws.go ================================================ package ws import ( "encoding/json" "io" "net/http" "net/url" "strings" "sync" "time" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/pkg/creds" "github.com/gorilla/websocket" "github.com/rs/zerolog" ) func Init() { var cfg struct { Mod struct { Origin string `yaml:"origin"` } `yaml:"api"` } app.LoadConfig(&cfg) log = app.GetLogger("api") initWS(cfg.Mod.Origin) api.HandleFunc("api/ws", apiWS) } var log zerolog.Logger // Message - struct for data exchange in Web API type Message struct { Type string `json:"type"` Value any `json:"value,omitempty"` } func (m *Message) String() (value string) { if s, ok := m.Value.(string); ok { return s } return } func (m *Message) Unmarshal(v any) error { b, err := json.Marshal(m.Value) if err != nil { return err } return json.Unmarshal(b, v) } type WSHandler func(tr *Transport, msg *Message) error func HandleFunc(msgType string, handler WSHandler) { wsHandlers[msgType] = handler } var wsHandlers = make(map[string]WSHandler) func initWS(origin string) { wsUp = &websocket.Upgrader{ ReadBufferSize: 4096, // for SDP WriteBufferSize: 512 * 1024, // 512K } switch origin { case "": // same origin + ignore port wsUp.CheckOrigin = func(r *http.Request) bool { origin := r.Header["Origin"] if len(origin) == 0 { return true } o, err := url.Parse(origin[0]) if err != nil { return false } if o.Host == r.Host { return true } log.Trace().Msgf("[api] ws origin=%s, host=%s", o.Host, r.Host) // https://github.com/AlexxIT/go2rtc/issues/118 if i := strings.IndexByte(o.Host, ':'); i > 0 { return o.Host[:i] == r.Host } return false } case "*": // any origin wsUp.CheckOrigin = func(r *http.Request) bool { return true } } } func apiWS(w http.ResponseWriter, r *http.Request) { ws, err := wsUp.Upgrade(w, r, nil) if err != nil { origin := r.Header.Get("Origin") log.Error().Err(err).Caller().Msgf("host=%s origin=%s", r.Host, origin) return } tr := &Transport{Request: r} tr.OnWrite(func(msg any) error { _ = ws.SetWriteDeadline(time.Now().Add(time.Second * 5)) if data, ok := msg.([]byte); ok { return ws.WriteMessage(websocket.BinaryMessage, data) } else { return ws.WriteJSON(msg) } }) for { msg := new(Message) if err = ws.ReadJSON(msg); err != nil { if !websocket.IsCloseError(err, websocket.CloseNoStatusReceived) { log.Trace().Err(err).Caller().Send() } _ = ws.Close() break } log.Trace().Str("type", msg.Type).Msg("[api] ws msg") if handler := wsHandlers[msg.Type]; handler != nil { go func() { if err = handler(tr, msg); err != nil { errMsg := creds.SecretString(err.Error()) tr.Write(&Message{Type: "error", Value: msg.Type + ": " + errMsg}) } }() } } tr.Close() } var wsUp *websocket.Upgrader type Transport struct { Request *http.Request ctx map[any]any closed bool mx sync.Mutex wrmx sync.Mutex onChange func() onWrite func(msg any) error onClose []func() } func (t *Transport) OnWrite(f func(msg any) error) { t.mx.Lock() if t.onChange != nil { t.onChange() } t.onWrite = f t.mx.Unlock() } func (t *Transport) Write(msg any) { t.wrmx.Lock() _ = t.onWrite(msg) t.wrmx.Unlock() } func (t *Transport) Close() { t.mx.Lock() for _, f := range t.onClose { f() } t.closed = true t.mx.Unlock() } func (t *Transport) OnChange(f func()) { t.mx.Lock() t.onChange = f t.mx.Unlock() } func (t *Transport) OnClose(f func()) { t.mx.Lock() if t.closed { f() } else { t.onClose = append(t.onClose, f) } t.mx.Unlock() } // WithContext - run function with Context variable func (t *Transport) WithContext(f func(ctx map[any]any)) { t.mx.Lock() if t.ctx == nil { t.ctx = map[any]any{} } f(t.ctx) t.mx.Unlock() } func (t *Transport) Writer() io.Writer { return &writer{t: t} } type writer struct { t *Transport } func (w *writer) Write(p []byte) (n int, err error) { w.t.wrmx.Lock() if err = w.t.onWrite(p); err == nil { n = len(p) } w.t.wrmx.Unlock() return } ================================================ FILE: internal/app/README.md ================================================ # App The application module is responsible for reading configuration files, running other modules and setting up [logs](#log). The configuration can be edited through the application's WebUI with code highlighting, syntax and specification checking. - By default, go2rtc will search for the `go2rtc.yaml` config file in the current working directory - go2rtc supports multiple config files: - `go2rtc -c config1.yaml -c config2.yaml -c config3.yaml` - go2rtc supports inline config in multiple formats from the command line: - **YAML**: `go2rtc -c '{log: {format: text}}'` - **JSON**: `go2rtc -c '{"log":{"format":"text"}}'` - **key=value**: `go2rtc -c log.format=text` - Each subsequent config will overwrite the previous one (but only for defined params) ``` go2rtc -config "{log: {format: text}}" -config /config/go2rtc.yaml -config "{rtsp: {listen: ''}}" -config /usr/local/go2rtc/go2rtc.yaml ``` or a simpler version ``` go2rtc -c log.format=text -c /config/go2rtc.yaml -c rtsp.listen='' -c /usr/local/go2rtc/go2rtc.yaml ``` ## Environment variables There is support for loading external variables into the config. First, they will be loaded from [credential files](https://systemd.io/CREDENTIALS). If `CREDENTIALS_DIRECTORY` is not set, then the key will be loaded from an environment variable. If no environment variable is set, then the string will be left as-is. ```yaml streams: camera1: rtsp://rtsp:${CAMERA_PASSWORD}@192.168.1.123/av_stream/ch0 rtsp: username: ${RTSP_USER:admin} # "admin" if "RTSP_USER" not set password: ${RTSP_PASS:secret} # "secret" if "RTSP_PASS" not set ``` ## JSON Schema Editors like [GoLand](https://www.jetbrains.com/go/) and [VS Code](https://code.visualstudio.com/) support autocomplete and syntax validation. ```yaml # yaml-language-server: $schema=https://raw.githubusercontent.com/AlexxIT/go2rtc/master/www/schema.json ``` or from a running go2rtc: ```yaml # yaml-language-server: $schema=http://localhost:1984/schema.json ``` ## Defaults - Default values may change in updates - FFmpeg module has many presets, they are not listed here because they may also change in updates ```yaml api: listen: ":1984" # default public port for WebUI and HTTP API ffmpeg: bin: "ffmpeg" # default binary path for FFmpeg log: level: "info" # default log level output: "stdout" time: "UNIXMS" rtsp: listen: ":8554" # default public port for RTSP server default_query: "video&audio" srtp: listen: ":8443" # default public port for SRTP server (used for HomeKit) webrtc: listen: ":8555" # default public port for WebRTC server (TCP and UDP) ice_servers: - urls: [ "stun:stun.cloudflare.com:3478", "stun:stun.l.google.com:19302" ] ``` ## Log You can set different log levels for different modules. ```yaml log: format: "" # empty (default, autodetect color support), color, json, text level: "info" # disabled, trace, debug, info (default), warn, error output: "stdout" # empty (only to memory), stderr, stdout (default) time: "UNIXMS" # empty (disable timestamp), UNIXMS (default), UNIXMICRO, UNIXNANO api: trace # module name: log level ``` Modules: `api`, `streams`, `rtsp`, `webrtc`, `mp4`, `hls`, `mjpeg`, `hass`, `homekit`, `onvif`, `rtmp`, `webtorrent`, `wyoming`, `echo`, `exec`, `expr`, `ffmpeg`, `wyze`, `xiaomi`. ================================================ FILE: internal/app/app.go ================================================ package app import ( "flag" "fmt" "os" "os/exec" "runtime" "runtime/debug" ) var ( Version string Modules []string UserAgent string ConfigPath string Info = make(map[string]any) ) const usage = `Usage of go2rtc: -c, --config Path to config file or config string as YAML or JSON, support multiple -d, --daemon Run in background -v, --version Print version and exit ` func Init() { var config flagConfig var daemon bool var version bool flag.Var(&config, "config", "") flag.Var(&config, "c", "") flag.BoolVar(&daemon, "daemon", false, "") flag.BoolVar(&daemon, "d", false, "") flag.BoolVar(&version, "version", false, "") flag.BoolVar(&version, "v", false, "") flag.Usage = func() { fmt.Print(usage) } flag.Parse() revision, vcsTime := readRevisionTime() if version { fmt.Printf("go2rtc version %s (%s) %s/%s\n", Version, revision, runtime.GOOS, runtime.GOARCH) os.Exit(0) } if daemon && os.Getppid() != 1 { if runtime.GOOS == "windows" { fmt.Println("Daemon mode is not supported on Windows") os.Exit(1) } // Re-run the program in background and exit cmd := exec.Command(os.Args[0], os.Args[1:]...) if err := cmd.Start(); err != nil { fmt.Println("Failed to start daemon:", err) os.Exit(1) } fmt.Println("Running in daemon mode with PID:", cmd.Process.Pid) os.Exit(0) } UserAgent = "go2rtc/" + Version Info["version"] = Version Info["revision"] = revision initConfig(config) initLogger() platform := fmt.Sprintf("%s/%s", runtime.GOOS, runtime.GOARCH) Logger.Info().Str("version", Version).Str("platform", platform).Str("revision", revision).Msg("go2rtc") Logger.Debug().Str("version", runtime.Version()).Str("vcs.time", vcsTime).Msg("build") if ConfigPath != "" { Logger.Info().Str("path", ConfigPath).Msg("config") } var cfg struct { Mod struct { Modules []string `yaml:"modules"` } `yaml:"app"` } LoadConfig(&cfg) Modules = cfg.Mod.Modules } func readRevisionTime() (revision, vcsTime string) { if info, ok := debug.ReadBuildInfo(); ok { for _, setting := range info.Settings { switch setting.Key { case "vcs.revision": if len(setting.Value) > 7 { revision = setting.Value[:7] } else { revision = setting.Value } case "vcs.time": vcsTime = setting.Value case "vcs.modified": if setting.Value == "true" { revision += ".dirty" } } } // Check version from -buildvcs info // Format for tagged version : v1.9.13 // Format for modified code: v1.9.14-0.20251215184105-753d6617ab58+dirty if info.Main.Version != "v"+Version { // Format: 1.9.13+dev.753d661[.dirty] // Compatible with "awesomeversion" and "packaging.version" from python. // Version will be larger than the previous release, but smaller than the next release. Version += "+dev." + revision } } return } ================================================ FILE: internal/app/config.go ================================================ package app import ( "errors" "os" "path/filepath" "strings" "sync" "github.com/AlexxIT/go2rtc/pkg/creds" "github.com/AlexxIT/go2rtc/pkg/yaml" ) func LoadConfig(v any) { for _, data := range configs { if err := yaml.Unmarshal(data, v); err != nil { Logger.Warn().Err(err).Send() } } } var configMu sync.Mutex func PatchConfig(path []string, value any) error { if ConfigPath == "" { return errors.New("config file disabled") } configMu.Lock() defer configMu.Unlock() // empty config is OK b, _ := os.ReadFile(ConfigPath) b, err := yaml.Patch(b, path, value) if err != nil { return err } return os.WriteFile(ConfigPath, b, 0644) } type flagConfig []string func (c *flagConfig) String() string { return strings.Join(*c, " ") } func (c *flagConfig) Set(value string) error { *c = append(*c, value) return nil } var configs [][]byte func initConfig(confs flagConfig) { if confs == nil { confs = []string{"go2rtc.yaml"} } for _, conf := range confs { if len(conf) == 0 { continue } if conf[0] == '{' { // config as raw YAML or JSON configs = append(configs, []byte(conf)) } else if data := parseConfString(conf); data != nil { configs = append(configs, data) } else { // config as file if ConfigPath == "" { ConfigPath = conf initStorage() } if data, _ = os.ReadFile(conf); data == nil { continue } loadEnv(data) data = creds.ReplaceVars(data) configs = append(configs, data) } } if ConfigPath != "" { if !filepath.IsAbs(ConfigPath) { if cwd, err := os.Getwd(); err == nil { ConfigPath = filepath.Join(cwd, ConfigPath) } } Info["config_path"] = ConfigPath } } func parseConfString(s string) []byte { i := strings.IndexByte(s, '=') if i < 0 { return nil } items := strings.Split(s[:i], ".") if len(items) < 2 { return nil } // `log.level=trace` => `{log: {level: trace}}` var pre string var suf = s[i+1:] for _, item := range items { pre += "{" + item + ": " suf += "}" } return []byte(pre + suf) } ================================================ FILE: internal/app/log.go ================================================ package app import ( "io" "os" "strings" "sync" "github.com/AlexxIT/go2rtc/pkg/creds" "github.com/mattn/go-isatty" "github.com/rs/zerolog" ) var MemoryLog = newBuffer() func GetLogger(module string) zerolog.Logger { Logger.Trace().Str("module", module).Msgf("[log] init") if s, ok := modules[module]; ok { lvl, err := zerolog.ParseLevel(s) if err == nil { return Logger.Level(lvl) } Logger.Warn().Err(err).Caller().Send() } return Logger } // initLogger support: // - output: empty (only to memory), stderr, stdout // - format: empty (autodetect color support), color, json, text // - time: empty (disable timestamp), UNIXMS, UNIXMICRO, UNIXNANO // - level: disabled, trace, debug, info, warn, error... func initLogger() { var cfg struct { Mod map[string]string `yaml:"log"` } cfg.Mod = modules // defaults LoadConfig(&cfg) var writer io.Writer switch output, path, _ := strings.Cut(modules["output"], ":"); output { case "stderr": writer = os.Stderr case "stdout": writer = os.Stdout case "file": if path == "" { path = "go2rtc.log" } // if fail - only MemoryLog will be available writer, _ = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644) } timeFormat := modules["time"] if writer != nil { if format := modules["format"]; format != "json" { console := &zerolog.ConsoleWriter{Out: writer} switch format { case "text": console.NoColor = true case "color": console.NoColor = false // useless, but anyway default: // autodetection if output support color // go-isatty - dependency for go-colorable - dependency for ConsoleWriter console.NoColor = !isatty.IsTerminal(writer.(*os.File).Fd()) } if timeFormat != "" { console.TimeFormat = "15:04:05.000" } else { console.PartsOrder = []string{ zerolog.LevelFieldName, zerolog.CallerFieldName, zerolog.MessageFieldName, } } writer = console } writer = zerolog.MultiLevelWriter(writer, MemoryLog) } else { writer = MemoryLog } writer = creds.SecretWriter(writer) lvl, _ := zerolog.ParseLevel(modules["level"]) Logger = zerolog.New(writer).Level(lvl) if timeFormat != "" { zerolog.TimeFieldFormat = timeFormat Logger = Logger.With().Timestamp().Logger() } } var Logger zerolog.Logger // modules log levels var modules = map[string]string{ "format": "", // useless, but anyway "level": "info", "output": "stdout", // TODO: change to stderr someday "time": zerolog.TimeFormatUnixMs, } const ( chunkCount = 16 chunkSize = 1 << 16 ) type circularBuffer struct { chunks [][]byte r, w int mu sync.Mutex } func newBuffer() *circularBuffer { b := &circularBuffer{chunks: make([][]byte, 0, chunkCount)} // create first chunk b.chunks = append(b.chunks, make([]byte, 0, chunkSize)) return b } func (b *circularBuffer) Write(p []byte) (n int, err error) { n = len(p) b.mu.Lock() // check if chunk has size if len(b.chunks[b.w])+n > chunkSize { // increase write chunk index if b.w++; b.w == chunkCount { b.w = 0 } // check overflow if b.r == b.w { // increase read chunk index if b.r++; b.r == chunkCount { b.r = 0 } } // check if current chunk exists if b.w == len(b.chunks) { // allocate new chunk b.chunks = append(b.chunks, make([]byte, 0, chunkSize)) } else { // reset len of current chunk b.chunks[b.w] = b.chunks[b.w][:0] } } b.chunks[b.w] = append(b.chunks[b.w], p...) b.mu.Unlock() return } func (b *circularBuffer) WriteTo(w io.Writer) (n int64, err error) { buf := make([]byte, 0, chunkCount*chunkSize) // use temp buffer inside mutex because w.Write can take some time b.mu.Lock() for i := b.r; ; { buf = append(buf, b.chunks[i]...) if i == b.w { break } if i++; i == chunkCount { i = 0 } } b.mu.Unlock() nn, err := w.Write(buf) return int64(nn), err } func (b *circularBuffer) Reset() { b.mu.Lock() b.chunks[0] = b.chunks[0][:0] b.r = 0 b.w = 0 b.mu.Unlock() } ================================================ FILE: internal/app/storage.go ================================================ package app import ( "sync" "github.com/AlexxIT/go2rtc/pkg/creds" "github.com/AlexxIT/go2rtc/pkg/yaml" ) func initStorage() { storage = &envStorage{data: make(map[string]string)} creds.SetStorage(storage) } func loadEnv(data []byte) { var cfg struct { Env map[string]string `yaml:"env"` } if err := yaml.Unmarshal(data, &cfg); err != nil { return } storage.mu.Lock() for name, value := range cfg.Env { storage.data[name] = value creds.AddSecret(value) } storage.mu.Unlock() } var storage *envStorage type envStorage struct { data map[string]string mu sync.Mutex } func (s *envStorage) SetValue(name, value string) error { if err := PatchConfig([]string{"env", name}, value); err != nil { return err } s.mu.Lock() s.data[name] = value s.mu.Unlock() return nil } func (s *envStorage) GetValue(name string) (value string, ok bool) { s.mu.Lock() value, ok = s.data[name] s.mu.Unlock() return } ================================================ FILE: internal/bubble/README.md ================================================ # Bubble [`new in v1.6.1`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.6.1) Private format in some cameras from [dvr163.com](http://help.dvr163.com/) and [eseecloud.com](http://www.eseecloud.com/). ## Configuration - you can skip `username`, `password`, `port`, `ch` and `stream` if they are default - set up separate streams for different channels and streams ```yaml streams: camera1: bubble://username:password@192.168.1.123:34567/bubble/live?ch=0&stream=0 ``` ================================================ FILE: internal/bubble/bubble.go ================================================ package bubble import ( "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/bubble" "github.com/AlexxIT/go2rtc/pkg/core" ) func Init() { streams.HandleFunc("bubble", func(source string) (core.Producer, error) { return bubble.Dial(source) }) } ================================================ FILE: internal/debug/README.md ================================================ # Debug This module provides `GET /api/stack`, with which you can find hanging goroutines ================================================ FILE: internal/debug/debug.go ================================================ package debug import ( "github.com/AlexxIT/go2rtc/internal/api" ) func Init() { api.HandleFunc("api/stack", stackHandler) } ================================================ FILE: internal/debug/stack.go ================================================ package debug import ( "bytes" "fmt" "net/http" "runtime" "github.com/AlexxIT/go2rtc/internal/api" ) var stackSkip = [][]byte{ // main.go []byte("main.main()"), []byte("created by os/signal.Notify"), // api/stack.go []byte("github.com/AlexxIT/go2rtc/internal/api.stackHandler"), // api/api.go []byte("created by github.com/AlexxIT/go2rtc/internal/api.Init"), []byte("created by net/http.(*connReader).startBackgroundRead"), []byte("created by net/http.(*Server).Serve"), // TODO: why two? []byte("created by github.com/AlexxIT/go2rtc/internal/rtsp.Init"), []byte("created by github.com/AlexxIT/go2rtc/internal/srtp.Init"), // homekit []byte("created by github.com/AlexxIT/go2rtc/internal/homekit.Init"), // webrtc/api.go []byte("created by github.com/pion/ice/v4.NewTCPMuxDefault"), []byte("created by github.com/pion/ice/v4.NewUDPMuxDefault"), } func stackHandler(w http.ResponseWriter, r *http.Request) { sep := []byte("\n\n") buf := make([]byte, 65535) i := 0 n := runtime.Stack(buf, true) skipped := 0 for _, item := range bytes.Split(buf[:n], sep) { for _, skip := range stackSkip { if bytes.Contains(item, skip) { item = nil skipped++ break } } if item != nil { i += copy(buf[i:], item) i += copy(buf[i:], sep) } } i += copy(buf[i:], fmt.Sprintf( "Total: %d, Skipped: %d", runtime.NumGoroutine(), skipped), ) api.Response(w, buf[:i], api.MimeText) } ================================================ FILE: internal/doorbird/README.md ================================================ # Doorbird [`new in v1.9.8`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.8) This source type supports [Doorbird](https://www.doorbird.com/) devices including MJPEG stream, audio stream as well as two-way audio. It is recommended to create a separate user within your doorbird setup for go2rtc. Minimum permissions for the user are: - Watch always - API operator ## Configuration ```yaml streams: doorbird1: - rtsp://admin:password@192.168.1.123:8557/mpeg/720p/media.amp # RTSP stream - doorbird://admin:password@192.168.1.123?media=video # MJPEG stream - doorbird://admin:password@192.168.1.123?media=audio # audio stream - doorbird://admin:password@192.168.1.123 # two-way audio ``` ================================================ FILE: internal/doorbird/doorbird.go ================================================ package doorbird import ( "net/url" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/doorbird" ) func Init() { streams.RedirectFunc("doorbird", func(rawURL string) (string, error) { u, err := url.Parse(rawURL) if err != nil { return "", err } // https://www.doorbird.com/downloads/api_lan.pdf switch u.Query().Get("media") { case "video": u.Path = "/bha-api/video.cgi" case "audio": u.Path = "/bha-api/audio-receive.cgi" default: return "", nil } u.Scheme = "http" return u.String(), nil }) streams.HandleFunc("doorbird", func(source string) (core.Producer, error) { return doorbird.Dial(source) }) } ================================================ FILE: internal/dvrip/README.md ================================================ # DVR-IP [`new in v1.2.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.2.0) Private format from DVR-IP NVR, NetSurveillance, Sofia protocol (NETsurveillance ActiveX plugin XMeye SDK). ## Configuration - you can skip `username`, `password`, `port`, `channel` and `subtype` if they are default - set up separate streams for different channels - use `subtype=0` for Main stream, and `subtype=1` for Extra1 stream - only the TCP protocol is supported ```yaml streams: only_stream: dvrip://username:password@192.168.1.123:34567?channel=0&subtype=0 only_tts: dvrip://username:password@192.168.1.123:34567?backchannel=1 two_way_audio: - dvrip://username:password@192.168.1.123:34567?channel=0&subtype=0 - dvrip://username:password@192.168.1.123:34567?backchannel=1 ``` ================================================ FILE: internal/dvrip/dvrip.go ================================================ package dvrip import ( "encoding/hex" "encoding/json" "fmt" "net" "net/http" "time" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/dvrip" ) func Init() { streams.HandleFunc("dvrip", dvrip.Dial) // DVRIP client autodiscovery api.HandleFunc("api/dvrip", apiDvrip) } const Port = 34569 // UDP port number for dvrip discovery func apiDvrip(w http.ResponseWriter, r *http.Request) { items, err := discover() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } api.ResponseSources(w, items) } func discover() ([]*api.Source, error) { addr := &net.UDPAddr{ Port: Port, IP: net.IP{239, 255, 255, 250}, } conn, err := net.ListenUDP("udp4", addr) if err != nil { return nil, err } defer conn.Close() go sendBroadcasts(conn) var items []*api.Source for _, info := range getResponses(conn) { if info.HostIP == "" || info.HostName == "" { continue } host, err := hexToDecimalBytes(info.HostIP) if err != nil { continue } items = append(items, &api.Source{ Name: info.HostName, URL: "dvrip://user:pass@" + host + "?channel=0&subtype=0", }) } return items, nil } func sendBroadcasts(conn *net.UDPConn) { // broadcasting the same multiple times because the devies some times don't answer data, err := hex.DecodeString("ff00000000000000000000000000fa0500000000") if err != nil { return } addr := &net.UDPAddr{ Port: Port, IP: net.IP{255, 255, 255, 255}, } for i := 0; i < 3; i++ { time.Sleep(100 * time.Millisecond) _, _ = conn.WriteToUDP(data, addr) } } type Message struct { NetCommon NetCommon `json:"NetWork.NetCommon"` Ret int `json:"Ret"` SessionID string `json:"SessionID"` } type NetCommon struct { BuildDate string `json:"BuildDate"` ChannelNum int `json:"ChannelNum"` DeviceType int `json:"DeviceType"` GateWay string `json:"GateWay"` HostIP string `json:"HostIP"` HostName string `json:"HostName"` HttpPort int `json:"HttpPort"` MAC string `json:"MAC"` MonMode string `json:"MonMode"` NetConnectState int `json:"NetConnectState"` OtherFunction string `json:"OtherFunction"` SN string `json:"SN"` SSLPort int `json:"SSLPort"` Submask string `json:"Submask"` TCPMaxConn int `json:"TCPMaxConn"` TCPPort int `json:"TCPPort"` UDPPort int `json:"UDPPort"` UseHSDownLoad bool `json:"UseHSDownLoad"` Version string `json:"Version"` } func getResponses(conn *net.UDPConn) (infos []*NetCommon) { if err := conn.SetReadDeadline(time.Now().Add(time.Second * 2)); err != nil { return } var ips []net.IP // processed IPs b := make([]byte, 4096) loop: for { n, addr, err := conn.ReadFromUDP(b) if err != nil { break } for _, ip := range ips { if ip.Equal(addr.IP) { continue loop } } if n <= 20+1 { continue } var msg Message if err = json.Unmarshal(b[20:n-1], &msg); err != nil { continue } infos = append(infos, &msg.NetCommon) ips = append(ips, addr.IP) } return } func hexToDecimalBytes(hexIP string) (string, error) { b, err := hex.DecodeString(hexIP[2:]) // remove the '0x' prefix if err != nil { return "", err } return fmt.Sprintf("%d.%d.%d.%d", b[3], b[2], b[1], b[0]), nil } ================================================ FILE: internal/echo/README.md ================================================ # Echo Some sources may have a dynamic link. And you will need to get it using a Bash or Python script. Your script should echo a link to the source. RTSP, FFmpeg or any of the supported sources. **Docker** and **Home Assistant add-on** users have preinstalled `python3`, `curl`, `jq`. ## Configuration ```yaml streams: apple_hls: echo:python3 hls.py https://developer.apple.com/streaming/examples/basic-stream-osx-ios5.html ``` ## Install python libraries **Docker** and **Hass Add-on** users have preinstalled `python3` without any additional libraries, like [requests](https://requests.readthedocs.io/) or others. If you need some additional libraries - you need to install them to folder with your script: 1. Install [SSH & Web Terminal](https://github.com/hassio-addons/addon-ssh) 2. Goto Add-on Web UI 3. Install library: `pip install requests -t /config/echo` 4. Add your script to `/config/echo/myscript.py` 5. Use your script as source `echo:python3 /config/echo/myscript.py` ## Example: Apple HLS ```yaml streams: apple_hls: echo:python3 hls.py https://developer.apple.com/streaming/examples/basic-stream-osx-ios5.html ``` **hls.py** ```python import re import sys from urllib.parse import urljoin from urllib.request import urlopen html = urlopen(sys.argv[1]).read().decode("utf-8") url = re.search(r"https.+?m3u8", html)[0] html = urlopen(url).read().decode("utf-8") m = re.search(r"^[a-z0-1/_]+\.m3u8$", html, flags=re.MULTILINE) url = urljoin(url, m[0]) # ffmpeg:https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_16x9/gear1/prog_index.m3u8#video=copy print("ffmpeg:" + url + "#video=copy") ``` ================================================ FILE: internal/echo/echo.go ================================================ package echo import ( "bytes" "errors" "os/exec" "slices" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/shell" ) func Init() { var cfg struct { Mod struct { AllowPaths []string `yaml:"allow_paths"` } `yaml:"echo"` } app.LoadConfig(&cfg) allowPaths := cfg.Mod.AllowPaths log := app.GetLogger("echo") streams.RedirectFunc("echo", func(url string) (string, error) { args := shell.QuoteSplit(url[5:]) if allowPaths != nil && !slices.Contains(allowPaths, args[0]) { return "", errors.New("echo: bin not in allow_paths: " + args[0]) } b, err := exec.Command(args[0], args[1:]...).Output() if err != nil { return "", err } b = bytes.TrimSpace(b) log.Debug().Str("url", url).Msgf("[echo] %s", b) return string(b), nil }) streams.MarkInsecure("echo") } ================================================ FILE: internal/eseecloud/README.md ================================================ # EseeCloud [`new in v1.9.10`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.10) This source is for cameras with a link like this `http://admin:@192.168.1.123:80/livestream/12`. Related [issue](https://github.com/AlexxIT/go2rtc/issues/1690). ## Configuration ```yaml streams: camera1: eseecloud://user:pass@192.168.1.123:80/livestream/12 ``` ================================================ FILE: internal/eseecloud/eseecloud.go ================================================ package eseecloud import ( "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/eseecloud" ) func Init() { streams.HandleFunc("eseecloud", eseecloud.Dial) } ================================================ FILE: internal/exec/README.md ================================================ # Exec Exec source can run any external application and expect data from it. Two transports are supported - **pipe** ([`new in v1.5.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.5.0)) and **RTSP**. If you want to use **RTSP** transport, the command must contain the `{output}` argument in any place. On launch, it will be replaced by the local address of the RTSP server. **pipe** reads data from app stdout in different formats: **MJPEG**, **H.264/H.265 bitstream**, **MPEG-TS**. Also pipe can write data to app stdin in two formats: **PCMA** and **PCM/48000**. The source can be used with: - [FFmpeg](https://ffmpeg.org/) - go2rtc ffmpeg source is just a shortcut to exec source - [FFplay](https://ffmpeg.org/ffplay.html) - play audio on your server - [GStreamer](https://gstreamer.freedesktop.org/) - [Raspberry Pi Cameras](https://www.raspberrypi.com/documentation/computers/camera_software.html) - any of your own software ## Configuration Pipe commands support parameters (format: `exec:{command}#{param1}#{param2}`): - `killsignal` - signal which will be sent to stop the process (numeric form) - `killtimeout` - time in seconds for forced termination with sigkill - `backchannel` - enable backchannel for two-way audio - `starttimeout` - time in seconds for waiting first byte from RTSP ```yaml streams: stream: exec:ffmpeg -re -i /media/BigBuckBunny.mp4 -c copy -rtsp_transport tcp -f rtsp {output} picam_h264: exec:libcamera-vid -t 0 --inline -o - picam_mjpeg: exec:libcamera-vid -t 0 --codec mjpeg -o - pi5cam_h264: exec:libcamera-vid -t 0 --libav-format h264 -o - canon: exec:gphoto2 --capture-movie --stdout#killsignal=2#killtimeout=5 play_pcma: exec:ffplay -fflags nobuffer -f alaw -ar 8000 -i -#backchannel=1 play_pcm48k: exec:ffplay -fflags nobuffer -f s16be -ar 48000 -i -#backchannel=1 ``` ## Backchannel - You can check audio card names in the **Go2rtc > WebUI > Add** - You can specify multiple backchannel lines with different codecs ```yaml sources: two_way_audio_win: - exec:ffmpeg -hide_banner -f dshow -i "audio=Microphone (High Definition Audio Device)" -c pcm_s16le -ar 16000 -ac 1 -f wav - - exec:ffplay -nodisp -probesize 32 -f s16le -ar 16000 -#backchannel=1#audio=s16le/16000 - exec:ffplay -nodisp -probesize 32 -f alaw -ar 8000 -#backchannel=1#audio=alaw/8000 ``` ================================================ FILE: internal/exec/exec.go ================================================ package exec import ( "bufio" "crypto/md5" "encoding/hex" "errors" "fmt" "io" "net/url" "os" "slices" "strings" "sync" "syscall" "time" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/rtsp" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/magic" "github.com/AlexxIT/go2rtc/pkg/pcm" pkg "github.com/AlexxIT/go2rtc/pkg/rtsp" "github.com/AlexxIT/go2rtc/pkg/shell" "github.com/rs/zerolog" ) func Init() { var cfg struct { Mod struct { AllowPaths []string `yaml:"allow_paths"` } `yaml:"exec"` } app.LoadConfig(&cfg) allowPaths = cfg.Mod.AllowPaths rtsp.HandleFunc(func(conn *pkg.Conn) bool { waitersMu.Lock() waiter := waiters[conn.URL.Path] waitersMu.Unlock() if waiter == nil { return false } // unblocking write to channel select { case waiter <- conn: return true default: return false } }) streams.HandleFunc("exec", execHandle) streams.MarkInsecure("exec") log = app.GetLogger("exec") } var allowPaths []string func execHandle(rawURL string) (prod core.Producer, err error) { rawURL, rawQuery, _ := strings.Cut(rawURL, "#") query := streams.ParseQuery(rawQuery) var path string // RTSP flow should have `{output}` inside URL // pipe flow may have `#{params}` inside URL if i := strings.Index(rawURL, "{output}"); i > 0 { if rtsp.Port == "" { return nil, errors.New("exec: rtsp module disabled") } sum := md5.Sum([]byte(rawURL)) path = "/" + hex.EncodeToString(sum[:]) rawURL = rawURL[:i] + "rtsp://127.0.0.1:" + rtsp.Port + path + rawURL[i+8:] } cmd := shell.NewCommand(rawURL[5:]) // remove `exec:` cmd.Stderr = &logWriter{ buf: make([]byte, 512), debug: log.Debug().Enabled(), } if allowPaths != nil && !slices.Contains(allowPaths, cmd.Args[0]) { _ = cmd.Close() return nil, errors.New("exec: bin not in allow_paths: " + cmd.Args[0]) } if s := query.Get("killsignal"); s != "" { sig := syscall.Signal(core.Atoi(s)) cmd.Cancel = func() error { log.Debug().Msgf("[exec] kill with signal=%d", sig) return cmd.Process.Signal(sig) } } if s := query.Get("killtimeout"); s != "" { cmd.WaitDelay = time.Duration(core.Atoi(s)) * time.Second } if query.Get("backchannel") == "1" { return pcm.NewBackchannel(cmd, query.Get("audio")) } var timeout time.Duration if s := query.Get("starttimeout"); s != "" { timeout = time.Duration(core.Atoi(s)) * time.Second } else { timeout = 30 * time.Second } if path == "" { prod, err = handlePipe(rawURL, cmd) } else { prod, err = handleRTSP(rawURL, cmd, path, timeout) } if err != nil { _ = cmd.Close() } return } func handlePipe(source string, cmd *shell.Command) (core.Producer, error) { stdout, err := cmd.StdoutPipe() if err != nil { return nil, err } rd := struct { io.Reader io.Closer }{ // add buffer for pipe reader to reduce syscall bufio.NewReaderSize(stdout, core.BufferSize), // stop cmd on close pipe call cmd, } log.Debug().Strs("args", cmd.Args).Msg("[exec] run pipe") ts := time.Now() if err = cmd.Start(); err != nil { return nil, err } prod, err := magic.Open(rd) if err != nil { return nil, fmt.Errorf("exec/pipe: %w\n%s", err, cmd.Stderr) } if info, ok := prod.(core.Info); ok { info.SetProtocol("pipe") setRemoteInfo(info, source, cmd.Args) } log.Debug().Stringer("launch", time.Since(ts)).Msg("[exec] run pipe") return prod, nil } func handleRTSP(source string, cmd *shell.Command, path string, timeout time.Duration) (core.Producer, error) { if log.Trace().Enabled() { cmd.Stdout = os.Stdout } waiter := make(chan *pkg.Conn, 1) waitersMu.Lock() waiters[path] = waiter waitersMu.Unlock() defer func() { waitersMu.Lock() delete(waiters, path) waitersMu.Unlock() }() log.Debug().Strs("args", cmd.Args).Msg("[exec] run rtsp") ts := time.Now() if err := cmd.Start(); err != nil { log.Error().Err(err).Str("source", source).Msg("[exec]") return nil, err } timer := time.NewTimer(timeout) defer timer.Stop() select { case <-timer.C: // haven't received data from app in timeout log.Error().Str("source", source).Msg("[exec] timeout") return nil, errors.New("exec: timeout") case <-cmd.Done(): // app fail before we receive any data return nil, fmt.Errorf("exec/rtsp\n%s", cmd.Stderr) case prod := <-waiter: // app started successfully log.Debug().Stringer("launch", time.Since(ts)).Msg("[exec] run rtsp") setRemoteInfo(prod, source, cmd.Args) prod.OnClose = cmd.Close return prod, nil } } // internal var ( log zerolog.Logger waiters = make(map[string]chan *pkg.Conn) waitersMu sync.Mutex ) type logWriter struct { buf []byte debug bool n int } func (l *logWriter) String() string { if l.n == len(l.buf) { return string(l.buf) + "..." } return string(l.buf[:l.n]) } func (l *logWriter) Write(p []byte) (n int, err error) { if l.n < cap(l.buf) { l.n += copy(l.buf[l.n:], p) } n = len(p) if l.debug { if p = trimSpace(p); p != nil { log.Debug().Msgf("[exec] %s", p) } } return } func trimSpace(b []byte) []byte { start := 0 stop := len(b) for ; start < stop; start++ { if b[start] >= ' ' { break // trim all ASCII before 0x20 } } for ; ; stop-- { if stop == start { return nil // skip empty output } if b[stop-1] > ' ' { break // trim all ASCII before 0x21 } } return b[start:stop] } func setRemoteInfo(info core.Info, source string, args []string) { info.SetSource(source) if i := core.Index(args, "-i"); i > 0 && i < len(args)-1 { rawURL := args[i+1] if u, err := url.Parse(rawURL); err == nil && u.Host != "" { info.SetRemoteAddr(u.Host) info.SetURL(rawURL) } } } ================================================ FILE: internal/expr/README.md ================================================ # Expr [`new in v1.8.2`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.8.2) [Expr](https://github.com/antonmedv/expr) - expression language and expression evaluation for Go. - [language definition](https://expr.medv.io/docs/Language-Definition) - takes best from JS, Python, Jinja2 syntax - your expression should return a link of any supported source - expression supports multiple operation, but: - all operations must be separated by a semicolon - all operations, except the last one, must declare a new variable (`let s = "abc";`) - the last operation should return a string - go2rtc supports additional functions: - `fetch` - JS-like HTTP requests - `match` - JS-like RegExp queries ## Fetch examples Multiple fetch requests are executed within a single session. They share the same cookie. **HTTP GET** ```js var r = fetch('https://example.org/products.json'); ``` **HTTP POST JSON** ```js var r = fetch('https://example.org/post', { method: 'POST', // Content-Type: application/json will be set automatically json: {username: 'example'} }); ``` **HTTP POST Form** ```js var r = fetch('https://example.org/post', { method: 'POST', // Content-Type: application/x-www-form-urlencoded will be set automatically data: {username: 'example', password: 'password'} }); ``` ## Script examples **Two way audio for Dahua VTO** ```yaml streams: dahua_vto: | expr: let host = 'admin:password@192.168.1.123'; var r = fetch('http://' + host + '/cgi-bin/configManager.cgi?action=setConfig&Encode[0].MainFormat[0].Audio.Compression=G.711A&Encode[0].MainFormat[0].Audio.Frequency=8000'); 'rtsp://' + host + '/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif' ``` **dom.ru** You can get credentials from https://github.com/ad/domru ```yaml streams: dom_ru: | expr: let camera = '***'; let token = '***'; let operator = '***'; fetch('https://myhome.proptech.ru/rest/v1/forpost/cameras/' + camera + '/video', { headers: { 'Authorization': 'Bearer ' + token, 'User-Agent': 'Google sdkgphone64x8664 | Android 14 | erth | 8.26.0 (82600010) | 0 | 0 | 0', 'Operator': operator } }).json().data.URL ``` **dom.ufanet.ru** ```yaml streams: ufanet_ru: | expr: let username = '***'; let password = '***'; let cameraid = '***'; let r1 = fetch('https://ucams.ufanet.ru/api/internal/login/', { method: 'POST', data: {username: username, password: password} }); let r2 = fetch('https://ucams.ufanet.ru/api/v0/cameras/this/?lang=ru', { method: 'POST', json: {'fields': ['token_l', 'server'], 'token_l_ttl': 3600, 'numbers': [cameraid]}, }).json().results[0]; 'rtsp://' + r2.server.domain + '/' + r2.number + '?token=' + r2.token_l ``` **Parse HLS files from Apple** Same example in two languages - python and expr. ```yaml streams: example_python: | echo:python -c 'from urllib.request import urlopen; import re # url1 = "https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_16x9/bipbop_16x9_variant.m3u8" html1 = urlopen("https://developer.apple.com/streaming/examples/basic-stream-osx-ios5.html").read().decode("utf-8") url1 = re.search(r"https.+?m3u8", html1)[0] # url2 = "gear1/prog_index.m3u8" html2 = urlopen(url1).read().decode("utf-8") url2 = re.search(r"^[a-z0-1/_]+\.m3u8$", html2, flags=re.MULTILINE)[0] # url3 = "https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_16x9/gear1/prog_index.m3u8" url3 = url1[:url1.rindex("/")+1] + url2 print("ffmpeg:" + url3 + "#video=copy")' example_expr: | expr: let html1 = fetch("https://developer.apple.com/streaming/examples/basic-stream-osx-ios5.html").text; let url1 = match(html1, "https.+?m3u8")[0]; let html2 = fetch(url1).text; let url2 = match(html2, "^[a-z0-1/_]+\\.m3u8$", "m")[0]; let url3 = url1[:lastIndexOf(url1, "/")+1] + url2; "ffmpeg:" + url3 + "#video=copy" ``` ## Comparison | expr | python | js | |------------------------------|----------------------------|--------------------------------| | let x = 1; | x = 1 | let x = 1 | | {a: 1, b: 2} | {"a": 1, "b": 2} | {a: 1, b: 2} | | let r = fetch(url, {method}) | r = request(method, url) | r = await fetch(url, {method}) | | r.ok | r.ok | r.ok | | r.status | r.status_code | r.status | | r.text | r.text | await r.text() | | r.json() | r.json() | await r.json() | | r.headers | r.headers | r.headers | | let m = match(text, "abc") | m = re.search("abc", text) | let m = text.match(/abc/) | ================================================ FILE: internal/expr/expr.go ================================================ package expr import ( "errors" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/expr" ) func Init() { log := app.GetLogger("expr") streams.RedirectFunc("expr", func(url string) (string, error) { v, err := expr.Eval(url[5:], nil) if err != nil { return "", err } log.Debug().Msgf("[expr] url=%s", url) if url = v.(string); url == "" { return "", errors.New("expr: result is empty") } return url, nil }) streams.MarkInsecure("expr") } ================================================ FILE: internal/ffmpeg/README.md ================================================ # FFmpeg You can get any stream, file or device via FFmpeg and push it to go2rtc. The app will automatically start FFmpeg with the proper arguments when someone starts watching the stream. - FFmpeg preinstalled for **Docker** and **Home Assistant add-on** users - **Home Assistant add-on** users can target files from [/media](https://www.home-assistant.io/more-info/local-media/setup-media/) folder ## Configuration Format: `ffmpeg:{input}#{param1}#{param2}#{param3}`. Examples: ```yaml streams: # [FILE] all tracks will be copied without transcoding codecs file1: ffmpeg:/media/BigBuckBunny.mp4 # [FILE] video will be transcoded to H264, audio will be skipped file2: ffmpeg:/media/BigBuckBunny.mp4#video=h264 # [FILE] video will be copied, audio will be transcoded to PCMU file3: ffmpeg:/media/BigBuckBunny.mp4#video=copy#audio=pcmu # [HLS] video will be copied, audio will be skipped hls: ffmpeg:https://devstreaming-cdn.apple.com/videos/streaming/examples/bipbop_16x9/gear5/prog_index.m3u8#video=copy # [MJPEG] video will be transcoded to H264 mjpeg: ffmpeg:http://185.97.122.128/cgi-bin/faststream.jpg#video=h264 # [RTSP] video with rotation, should be transcoded, so select H264 rotate: ffmpeg:rtsp://12345678@192.168.1.123/av_stream/ch0#video=h264#rotate=90 ``` All transcoding formats have [built-in templates](ffmpeg.go): `h264`, `h265`, `opus`, `pcmu`, `pcmu/16000`, `pcmu/48000`, `pcma`, `pcma/16000`, `pcma/48000`, `aac`, `aac/16000`. But you can override them via YAML config. You can also add your own formats to the config and use them with source params. ```yaml ffmpeg: bin: ffmpeg # path to ffmpeg binary global: "-hide_banner" timeout: 5 # default timeout in seconds for rtsp inputs h264: "-codec:v libx264 -g:v 30 -preset:v superfast -tune:v zerolatency -profile:v main -level:v 4.1" mycodec: "-any args that supported by ffmpeg..." myinput: "-fflags nobuffer -flags low_delay -timeout {timeout} -i {input}" myraw: "-ss 00:00:20" ``` - You can use go2rtc stream name as ffmpeg input (ex. `ffmpeg:camera1#video=h264`) - You can use `video` and `audio` params multiple times (ex. `#video=copy#audio=copy#audio=pcmu`) - You can use `rotate` param with `90`, `180`, `270` or `-90` values, important with transcoding (ex. `#video=h264#rotate=90`) - You can use `width` and/or `height` params, important with transcoding (ex. `#video=h264#width=1280`) - You can use `drawtext` to add a timestamp (ex. `drawtext=x=2:y=2:fontsize=12:fontcolor=white:box=1:boxcolor=black`) - This will greatly increase the CPU of the server, even with hardware acceleration - You can use `timeout` param to set RTSP input timeout in seconds (ex. `#timeout=10`) - You can use `raw` param for any additional FFmpeg arguments (ex. `#raw=-vf transpose=1`) - You can use `input` param to override default input template (ex. `#input=rtsp/udp` will change RTSP transport from TCP to UDP+TCP) - You can use raw input value (ex. `#input=-timeout {timeout} -i {input}`) - You can add your own input templates Read more about [hardware acceleration](hardware/README.md). **PS.** It is recommended to check the available hardware in the WebUI add page. ================================================ FILE: internal/ffmpeg/api.go ================================================ package ffmpeg import ( "net/http" "strings" "github.com/AlexxIT/go2rtc/internal/streams" ) func apiFFmpeg(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { http.Error(w, "", http.StatusMethodNotAllowed) return } query := r.URL.Query() dst := query.Get("dst") stream := streams.Get(dst) if stream == nil { http.Error(w, "", http.StatusNotFound) return } var src string if s := query.Get("file"); s != "" { if streams.Validate(s) == nil { src = "ffmpeg:" + s + "#audio=auto#input=file" } } else if s = query.Get("live"); s != "" { if streams.Validate(s) == nil { src = "ffmpeg:" + s + "#audio=auto" } } else if s = query.Get("text"); s != "" { if strings.IndexAny(s, `'"&%$`) < 0 { src = "ffmpeg:tts?text=" + s if s = query.Get("voice"); s != "" { src += "&voice=" + s } src += "#audio=auto" } } if src == "" { http.Error(w, "", http.StatusBadRequest) return } if err := stream.Play(src); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } ================================================ FILE: internal/ffmpeg/device/README.md ================================================ # FFmpeg Device You can get video from any USB camera or Webcam as RTSP or WebRTC stream. This is part of FFmpeg integration. - check available devices in web interface - `video_size` and `framerate` must be supported by your camera! - for Linux supported only video for now - for macOS you can stream FaceTime camera or whole desktop! - for macOS important to set right framerate ## Configuration Format: `ffmpeg:device?{input-params}#{param1}#{param2}#{param3}` ```yaml streams: linux_usbcam: ffmpeg:device?video=0&video_size=1280x720#video=h264 windows_webcam: ffmpeg:device?video=0#video=h264 macos_facetime: ffmpeg:device?video=0&audio=1&video_size=1280x720&framerate=30#video=h264#audio=pcma ``` **PS.** It is recommended to check the available devices in the WebUI add page. ================================================ FILE: internal/ffmpeg/device/device_bsd.go ================================================ //go:build freebsd || netbsd || openbsd || dragonfly package device import ( "net/url" "os" "os/exec" "regexp" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/pkg/core" ) func queryToInput(query url.Values) string { if video := query.Get("video"); video != "" { // https://ffmpeg.org/ffmpeg-devices.html#video4linux2_002c-v4l2 input := "-f v4l2" for key, value := range query { switch key { case "resolution": input += " -video_size " + value[0] case "video_size", "pixel_format", "input_format", "framerate", "use_libv4l2": input += " -" + key + " " + value[0] } } return input + " -i " + indexToItem(videos, video) } if audio := query.Get("audio"); audio != "" { input := "-f oss" for key, value := range query { switch key { case "channels", "sample_rate": input += " -" + key + " " + value[0] } } return input + " -i " + indexToItem(audios, audio) } return "" } func initDevices() { files, err := os.ReadDir("/dev") if err != nil { return } for _, file := range files { if !strings.HasPrefix(file.Name(), core.KindVideo) { continue } name := "/dev/" + file.Name() cmd := exec.Command( Bin, "-hide_banner", "-f", "v4l2", "-list_formats", "all", "-i", name, ) b, _ := cmd.CombinedOutput() // [video4linux2,v4l2 @ 0x860b92280] Raw : yuyv422 : YUYV 4:2:2 : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360 752x416 800x448 800x600 864x480 960x544 960x720 1024x576 1184x656 1280x720 1280x960 // [video4linux2,v4l2 @ 0x860b92280] Compressed: mjpeg : Motion-JPEG : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360 752x416 800x448 800x600 864x480 960x544 960x720 1024x576 1184x656 1280x720 1280x960 re := regexp.MustCompile("(Raw *|Compressed): +(.+?) : +(.+?) : (.+)") m := re.FindAllStringSubmatch(string(b), -1) for _, i := range m { size, _, _ := strings.Cut(i[4], " ") stream := &api.Source{ Name: i[3], Info: i[4], URL: "ffmpeg:device?video=" + name + "&input_format=" + i[2] + "&video_size=" + size, } if i[1] != "Compressed" { stream.URL += "#video=h264#hardware" } videos = append(videos, name) streams = append(streams, stream) } } err = exec.Command(Bin, "-f", "oss", "-i", "/dev/dsp", "-t", "1", "-f", "null", "-").Run() if err == nil { stream := &api.Source{ Name: "OSS default", Info: " ", URL: "ffmpeg:device?audio=default&channels=1&sample_rate=16000&#audio=opus", } audios = append(audios, "default") streams = append(streams, stream) } } ================================================ FILE: internal/ffmpeg/device/device_darwin.go ================================================ //go:build darwin || ios package device import ( "net/url" "os/exec" "regexp" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/pkg/core" ) func queryToInput(query url.Values) string { video := query.Get("video") audio := query.Get("audio") if video == "" && audio == "" { return "" } // https://ffmpeg.org/ffmpeg-devices.html#avfoundation input := "-f avfoundation" if video != "" { video = indexToItem(videos, video) for key, value := range query { switch key { case "resolution": input += " -video_size " + value[0] case "pixel_format", "framerate", "video_size", "capture_cursor", "capture_mouse_clicks", "capture_raw_data": input += " -" + key + " " + value[0] } } } if audio != "" { audio = indexToItem(audios, audio) } return input + ` -i "` + video + `:` + audio + `"` } func initDevices() { // [AVFoundation indev @ 0x147f04510] AVFoundation video devices: // [AVFoundation indev @ 0x147f04510] [0] FaceTime HD Camera // [AVFoundation indev @ 0x147f04510] [1] Capture screen 0 // [AVFoundation indev @ 0x147f04510] AVFoundation audio devices: // [AVFoundation indev @ 0x147f04510] [0] MacBook Pro Microphone cmd := exec.Command( Bin, "-hide_banner", "-list_devices", "true", "-f", "avfoundation", "-i", "", ) b, _ := cmd.CombinedOutput() re := regexp.MustCompile(`\[\d+] (.+)`) var kind string for _, line := range strings.Split(string(b), "\n") { switch { case strings.HasSuffix(line, "video devices:"): kind = core.KindVideo continue case strings.HasSuffix(line, "audio devices:"): kind = core.KindAudio continue } m := re.FindStringSubmatch(line) if m == nil { continue } name := m[1] switch kind { case core.KindVideo: videos = append(videos, name) case core.KindAudio: audios = append(audios, name) } streams = append(streams, &api.Source{ Name: name, URL: "ffmpeg:device?" + kind + "=" + name, }) } } ================================================ FILE: internal/ffmpeg/device/device_unix.go ================================================ //go:build unix && !darwin && !freebsd && !netbsd && !openbsd && !dragonfly package device import ( "net/url" "os" "os/exec" "regexp" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/pkg/core" ) func queryToInput(query url.Values) string { if video := query.Get("video"); video != "" { // https://ffmpeg.org/ffmpeg-devices.html#video4linux2_002c-v4l2 input := "-f v4l2" for key, value := range query { switch key { case "resolution": input += " -video_size " + value[0] case "video_size", "pixel_format", "input_format", "framerate", "use_libv4l2": input += " -" + key + " " + value[0] } } return input + " -i " + indexToItem(videos, video) } if audio := query.Get("audio"); audio != "" { // https://trac.ffmpeg.org/wiki/Capture/ALSA input := "-f alsa" for key, value := range query { switch key { case "channels", "sample_rate": input += " -" + key + " " + value[0] } } return input + " -i " + indexToItem(audios, audio) } return "" } func initDevices() { files, err := os.ReadDir("/dev") if err != nil { return } for _, file := range files { if !strings.HasPrefix(file.Name(), core.KindVideo) { continue } name := "/dev/" + file.Name() cmd := exec.Command( Bin, "-hide_banner", "-f", "v4l2", "-list_formats", "all", "-i", name, ) b, _ := cmd.CombinedOutput() // [video4linux2,v4l2 @ 0x204e1c0] Compressed: mjpeg : Motion-JPEG : 640x360 1280x720 1920x1080 // [video4linux2,v4l2 @ 0x204e1c0] Raw : yuyv422 : YUYV 4:2:2 : 640x360 1280x720 1920x1080 // [video4linux2,v4l2 @ 0x204e1c0] Compressed: h264 : H.264 : 640x360 1280x720 1920x1080 re := regexp.MustCompile("(Raw *|Compressed): +(.+?) : +(.+?) : (.+)") m := re.FindAllStringSubmatch(string(b), -1) for _, i := range m { size, _, _ := strings.Cut(i[4], " ") stream := &api.Source{ Name: i[3], Info: i[4], URL: "ffmpeg:device?video=" + name + "&input_format=" + i[2] + "&video_size=" + size, } if i[1] != "Compressed" { stream.URL += "#video=h264#hardware" } videos = append(videos, name) streams = append(streams, stream) } } err = exec.Command(Bin, "-f", "alsa", "-i", "default", "-t", "1", "-f", "null", "-").Run() if err == nil { stream := &api.Source{ Name: "ALSA default", Info: " ", URL: "ffmpeg:device?audio=default&channels=1&sample_rate=16000&#audio=opus", } audios = append(audios, "default") streams = append(streams, stream) } } ================================================ FILE: internal/ffmpeg/device/device_windows.go ================================================ //go:build windows package device import ( "net/url" "os/exec" "regexp" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/pkg/core" ) func queryToInput(query url.Values) string { video := query.Get("video") audio := query.Get("audio") if video == "" && audio == "" { return "" } // https://ffmpeg.org/ffmpeg-devices.html#dshow input := "-f dshow" if video != "" { video = indexToItem(videos, video) for key, value := range query { switch key { case "resolution": input += " -video_size " + value[0] case "video_size", "framerate", "pixel_format": input += " -" + key + " " + value[0] } } } if audio != "" { audio = indexToItem(audios, audio) for key, value := range query { switch key { case "sample_rate", "sample_size", "channels", "audio_buffer_size": input += " -" + key + " " + value[0] } } } if video != "" { input += ` -i "video=` + video if audio != "" { input += `:audio=` + audio } input += `"` } else { input += ` -i "audio=` + audio + `"` } return input } func initDevices() { cmd := exec.Command( Bin, "-hide_banner", "-list_devices", "true", "-f", "dshow", "-i", "", ) b, _ := cmd.CombinedOutput() re := regexp.MustCompile(`"([^"]+)" \((video|audio)\)`) for _, m := range re.FindAllStringSubmatch(string(b), -1) { name := m[1] kind := m[2] stream := &api.Source{ Name: name, URL: "ffmpeg:device?" + kind + "=" + name, } switch kind { case core.KindVideo: videos = append(videos, name) stream.URL += "#video=h264#hardware" case core.KindAudio: audios = append(audios, name) stream.URL += "&channels=1&sample_rate=16000&audio_buffer_size=10" } streams = append(streams, stream) } } ================================================ FILE: internal/ffmpeg/device/devices.go ================================================ package device import ( "net/http" "net/url" "strconv" "sync" "github.com/AlexxIT/go2rtc/internal/api" ) func Init(bin string) { Bin = bin api.HandleFunc("api/ffmpeg/devices", apiDevices) } func GetInput(src string) string { query, err := url.ParseQuery(src) if err != nil { return "" } runonce.Do(initDevices) return queryToInput(query) } var Bin string var videos, audios []string var streams []*api.Source var runonce sync.Once func apiDevices(w http.ResponseWriter, r *http.Request) { runonce.Do(initDevices) api.ResponseSources(w, streams) } func indexToItem(items []string, index string) string { if i, err := strconv.Atoi(index); err == nil && i < len(items) { return items[i] } return index } ================================================ FILE: internal/ffmpeg/ffmpeg.go ================================================ package ffmpeg import ( "net/url" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/ffmpeg/device" "github.com/AlexxIT/go2rtc/internal/ffmpeg/hardware" "github.com/AlexxIT/go2rtc/internal/ffmpeg/virtual" "github.com/AlexxIT/go2rtc/internal/rtsp" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/ffmpeg" "github.com/rs/zerolog" ) func Init() { var cfg struct { Mod map[string]string `yaml:"ffmpeg"` Log struct { Level string `yaml:"ffmpeg"` } `yaml:"log"` } cfg.Mod = defaults // will be overriden from yaml cfg.Log.Level = "error" app.LoadConfig(&cfg) log = app.GetLogger("ffmpeg") // zerolog levels: trace debug info warn error fatal panic disabled // FFmpeg levels: trace debug verbose info warning error fatal panic quiet if cfg.Log.Level == "warn" { cfg.Log.Level = "warning" } defaults["global"] += " -v " + cfg.Log.Level streams.RedirectFunc("ffmpeg", func(url string) (string, error) { if _, err := Version(); err != nil { return "", err } args := parseArgs(url[7:]) if core.Contains(args.Codecs, "auto") { return "", nil // force call streams.HandleFunc("ffmpeg") } return "exec:" + args.String(), nil }) streams.HandleFunc("ffmpeg", NewProducer) api.HandleFunc("api/ffmpeg", apiFFmpeg) device.Init(defaults["bin"]) hardware.Init(defaults["bin"]) } var defaults = map[string]string{ "bin": "ffmpeg", "global": "-hide_banner", "timeout": "5", // inputs "file": "-re -i {input}", "http": "-fflags nobuffer -flags low_delay -i {input}", "rtsp": "-fflags nobuffer -flags low_delay -timeout {timeout} -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i {input}", "rtsp/udp": "-fflags nobuffer -flags low_delay -timeout {timeout} -user_agent go2rtc/ffmpeg -i {input}", // output "output": "-user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}", "output/mjpeg": "-f mjpeg -", "output/raw": "-f yuv4mpegpipe -", "output/aac": "-f adts -", "output/wav": "-f wav -", // `-preset superfast` - we can't use ultrafast because it doesn't support `-profile main -level 4.1` // `-tune zerolatency` - for minimal latency // `-profile high -level 4.1` - most used streaming profile // `-pix_fmt:v yuv420p` - important for Telegram "h264": "-c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p", "h265": "-c:v libx265 -g 50 -profile:v main -x265-params level=5.1:high-tier=0 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p", "mjpeg": "-c:v mjpeg", //"mjpeg": "-c:v mjpeg -force_duplicated_matrix:v 1 -huffman:v 0 -pix_fmt:v yuvj420p", "raw": "-c:v rawvideo", "raw/gray8": "-c:v rawvideo -pix_fmt:v gray8", "raw/yuv420p": "-c:v rawvideo -pix_fmt:v yuv420p", "raw/yuv422p": "-c:v rawvideo -pix_fmt:v yuv422p", "raw/yuv444p": "-c:v rawvideo -pix_fmt:v yuv444p", // https://ffmpeg.org/ffmpeg-codecs.html#libopus-1 // https://github.com/pion/webrtc/issues/1514 // https://ffmpeg.org/ffmpeg-resampler.html // `-async 1` or `-min_comp 0` - force resampling for static timestamp inc, important for WebRTC audio quality "opus": "-c:a libopus -application:a lowdelay -min_comp 0", "opus/16000": "-c:a libopus -application:a lowdelay -min_comp 0 -ar:a 16000 -ac:a 1", "pcmu": "-c:a pcm_mulaw -ar:a 8000 -ac:a 1", "pcmu/8000": "-c:a pcm_mulaw -ar:a 8000 -ac:a 1", "pcmu/16000": "-c:a pcm_mulaw -ar:a 16000 -ac:a 1", "pcmu/48000": "-c:a pcm_mulaw -ar:a 48000 -ac:a 1", "pcma": "-c:a pcm_alaw -ar:a 8000 -ac:a 1", "pcma/8000": "-c:a pcm_alaw -ar:a 8000 -ac:a 1", "pcma/16000": "-c:a pcm_alaw -ar:a 16000 -ac:a 1", "pcma/48000": "-c:a pcm_alaw -ar:a 48000 -ac:a 1", "aac": "-c:a aac", // keep sample rate and channels "aac/16000": "-c:a aac -ar:a 16000 -ac:a 1", "mp3": "-c:a libmp3lame -q:a 8", "pcm": "-c:a pcm_s16be -ar:a 8000 -ac:a 1", "pcm/8000": "-c:a pcm_s16be -ar:a 8000 -ac:a 1", "pcm/16000": "-c:a pcm_s16be -ar:a 16000 -ac:a 1", "pcm/48000": "-c:a pcm_s16be -ar:a 48000 -ac:a 1", "pcml": "-c:a pcm_s16le -ar:a 8000 -ac:a 1", "pcml/8000": "-c:a pcm_s16le -ar:a 8000 -ac:a 1", "pcml/16000": "-c:a pcm_s16le -ar:a 16000 -ac:a 1", "pcml/44100": "-c:a pcm_s16le -ar:a 44100 -ac:a 1", // hardware Intel and AMD on Linux // better not to set `-async_depth:v 1` like for QSV, because framedrops // `-bf 0` - disable B-frames is very important "h264/vaapi": "-c:v h264_vaapi -g 50 -bf 0 -profile:v high -level:v 4.1 -sei:v 0", "h265/vaapi": "-c:v hevc_vaapi -g 50 -bf 0 -profile:v main -level:v 5.1 -sei:v 0", "mjpeg/vaapi": "-c:v mjpeg_vaapi", // hardware Raspberry "h264/v4l2m2m": "-c:v h264_v4l2m2m -g 50 -bf 0", "h265/v4l2m2m": "-c:v hevc_v4l2m2m -g 50 -bf 0", // hardware Rockchip // important to use custom ffmpeg https://github.com/AlexxIT/go2rtc/issues/768 // hevc - doesn't have a profile setting "h264/rkmpp": "-c:v h264_rkmpp -g 50 -bf 0 -profile:v high -level:v 4.1", "h265/rkmpp": "-c:v hevc_rkmpp -g 50 -bf 0 -profile:v main -level:v 5.1", "mjpeg/rkmpp": "-c:v mjpeg_rkmpp", // hardware NVidia on Linux and Windows // preset=p2 - faster, tune=ll - low latency "h264/cuda": "-c:v h264_nvenc -g 50 -bf 0 -profile:v high -level:v auto -preset:v p2 -tune:v ll", "h265/cuda": "-c:v hevc_nvenc -g 50 -bf 0 -profile:v main -level:v auto", // hardware Intel on Windows "h264/dxva2": "-c:v h264_qsv -g 50 -bf 0 -profile:v high -level:v 4.1 -async_depth:v 1", "h265/dxva2": "-c:v hevc_qsv -g 50 -bf 0 -profile:v main -level:v 5.1 -async_depth:v 1", "mjpeg/dxva2": "-c:v mjpeg_qsv", // hardware macOS "h264/videotoolbox": "-c:v h264_videotoolbox -g 50 -bf 0 -profile:v high -level:v 4.1", "h265/videotoolbox": "-c:v hevc_videotoolbox -g 50 -bf 0 -profile:v main -level:v 5.1", } var log zerolog.Logger // configTemplate - return template from config (defaults) if exist or return raw template func configTemplate(template string) string { if s := defaults[template]; s != "" { return s } return template } // inputTemplate - select input template from YAML config by template name // if query has input param - select another template by this name // if there is no another template - use input param as template func inputTemplate(name, s string, query url.Values) string { var template string if input := query.Get("input"); input != "" { template = configTemplate(input) } else { template = defaults[name] } if strings.Contains(template, "{timeout}") { timeout := query.Get("timeout") if timeout == "" { timeout = defaults["timeout"] } template = strings.Replace(template, "{timeout}", timeout+"000000", 1) } return strings.Replace(template, "{input}", s, 1) } func parseArgs(s string) *ffmpeg.Args { // init FFmpeg arguments args := &ffmpeg.Args{ Bin: defaults["bin"], Global: defaults["global"], Output: defaults["output"], Version: verAV, } var source = s var query url.Values if i := strings.IndexByte(s, '#'); i >= 0 { query = streams.ParseQuery(s[i+1:]) args.Video = len(query["video"]) args.Audio = len(query["audio"]) s = s[:i] } // Parse input: // 1. Input as xxxx:// link (http or rtsp or any other) // 2. Input as stream name // 3. Input as FFmpeg device (local USB camera) if i := strings.Index(s, "://"); i > 0 { switch s[:i] { case "http", "https", "rtmp": args.Input = inputTemplate("http", s, query) case "rtsp", "rtsps": // https://ffmpeg.org/ffmpeg-protocols.html#rtsp // skip unnecessary input tracks switch { case (args.Video > 0 && args.Audio > 0) || (args.Video == 0 && args.Audio == 0): args.Input = "-allowed_media_types video+audio " case args.Video > 0: args.Input = "-allowed_media_types video " case args.Audio > 0: args.Input = "-allowed_media_types audio " } args.Input += inputTemplate("rtsp", s, query) default: args.Input = "-i " + s } } else if streams.Get(s) != nil { s = "rtsp://127.0.0.1:" + rtsp.Port + "/" + s switch { case args.Video > 0 && args.Audio == 0: s += "?video" case args.Audio > 0 && args.Video == 0: s += "?audio" default: s += "?video&audio" } s += "&source=ffmpeg:" + url.QueryEscape(source) for _, v := range query["query"] { s += "&" + v } args.Input = inputTemplate("rtsp", s, query) } else if i = strings.Index(s, "?"); i > 0 { switch s[:i] { case "device": args.Input = device.GetInput(s[i+1:]) case "virtual": args.Input = virtual.GetInput(s[i+1:]) case "tts": args.Input = virtual.GetInputTTS(s[i+1:]) } } else { args.Input = inputTemplate("file", s, query) } if query["async"] != nil { args.Input = "-use_wallclock_as_timestamps 1 -async 1 " + args.Input } // Parse query params: // 1. `width`/`height` params // 2. `rotate` param // 3. `video` params (support multiple) // 4. `audio` params (support multiple) // 5. `hardware` param if query != nil { // 1. Process raw params for FFmpeg for _, raw := range query["raw"] { // support templates https://github.com/AlexxIT/go2rtc/issues/487 raw = configTemplate(raw) args.AddCodec(raw) } // 2. Process video filters (resize and rotation) if query["width"] != nil || query["height"] != nil { filter := "scale=" if query["width"] != nil { filter += query["width"][0] } else { filter += "-1" } filter += ":" if query["height"] != nil { filter += query["height"][0] } else { filter += "-1" } args.AddFilter(filter) } if query["rotate"] != nil { var filter string switch query["rotate"][0] { case "90": filter = "transpose=1" // 90 degrees clockwise case "180": filter = "transpose=1,transpose=1" case "-90", "270": filter = "transpose=2" // 90 degrees counterclockwise } if filter != "" { args.AddFilter(filter) } } for _, drawtext := range query["drawtext"] { // support templates https://github.com/AlexxIT/go2rtc/issues/487 drawtext = configTemplate(drawtext) // support default timestamp format if !strings.Contains(drawtext, "text=") { drawtext += `:text='%{localtime\:%Y-%m-%d %X}'` } args.AddFilter("drawtext=" + drawtext) } // 3. Process video codecs if args.Video > 0 { for _, video := range query["video"] { if video != "copy" { if codec := defaults[video]; codec != "" { args.AddCodec(codec) } else { args.AddCodec(video) } } else { args.AddCodec("-c:v copy") } } } if query["bitrate"] != nil { // https://trac.ffmpeg.org/wiki/Limiting%20the%20output%20bitrate b := query["bitrate"][0] args.AddCodec("-b:v " + b + " -maxrate " + b + " -bufsize " + b) } // 4. Process audio codecs if args.Audio > 0 { for _, audio := range query["audio"] { if audio != "copy" { if codec := defaults[audio]; codec != "" { args.AddCodec(codec) } else { args.AddCodec(audio) } } else { args.AddCodec("-c:a copy") } } } if query["hardware"] != nil { hardware.MakeHardware(args, query["hardware"][0], defaults) } } switch { case args.Video == 0 && args.Audio == 0: args.AddCodec("-c copy") case args.Video == 0: args.AddCodec("-vn") case args.Audio == 0: args.AddCodec("-an") } // change otput from RTSP to some other pipe format switch { case args.Video == 0 && args.Audio == 0: // no transcoding from mjpeg input (ffmpeg device with support output as raw MJPEG) if strings.Contains(args.Input, " mjpeg ") { args.Output = defaults["output/mjpeg"] } case args.Video == 1 && args.Audio == 0: switch core.Before(query.Get("video"), "/") { case "mjpeg": args.Output = defaults["output/mjpeg"] case "raw": args.Output = defaults["output/raw"] } case args.Video == 0 && args.Audio == 1: switch core.Before(query.Get("audio"), "/") { case "aac": args.Output = defaults["output/aac"] case "pcma", "pcmu", "pcml": args.Output = defaults["output/wav"] } } return args } ================================================ FILE: internal/ffmpeg/ffmpeg_test.go ================================================ package ffmpeg import ( "testing" "github.com/AlexxIT/go2rtc/pkg/ffmpeg" "github.com/stretchr/testify/require" ) func TestParseArgsFile(t *testing.T) { tests := []struct { name string source string expect string }{ { name: "[FILE] all tracks will be copied without transcoding codecs", source: "/media/bbb.mp4", expect: `ffmpeg -hide_banner -re -i /media/bbb.mp4 -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[FILE] video will be transcoded to H264, audio will be skipped", source: "/media/bbb.mp4#video=h264", expect: `ffmpeg -hide_banner -re -i /media/bbb.mp4 -c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[FILE] video will be copied, audio will be transcoded to pcmu", source: "/media/bbb.mp4#video=copy#audio=pcmu", expect: `ffmpeg -hide_banner -re -i /media/bbb.mp4 -c:v copy -c:a pcm_mulaw -ar:a 8000 -ac:a 1 -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[FILE] video will be transcoded to H265 and rotate 270º, audio will be skipped", source: "/media/bbb.mp4#video=h265#rotate=-90", expect: `ffmpeg -hide_banner -re -i /media/bbb.mp4 -c:v libx265 -g 50 -profile:v main -level:v 5.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -vf "transpose=2" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[FILE] video will be output for MJPEG to pipe, audio will be skipped", source: "/media/bbb.mp4#video=mjpeg", expect: `ffmpeg -hide_banner -re -i /media/bbb.mp4 -c:v mjpeg -an -f mjpeg -`, }, { name: "https://github.com/AlexxIT/go2rtc/issues/509", source: "ffmpeg:test.mp4#raw=-ss 00:00:20", expect: `ffmpeg -hide_banner -re -i ffmpeg:test.mp4 -ss 00:00:20 -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { args := parseArgs(test.source) require.Equal(t, test.expect, args.String()) }) } } func TestParseArgsDevice(t *testing.T) { tests := []struct { name string source string expect string }{ { name: "[DEVICE] video will be output for MJPEG to pipe, with size 1920x1080", source: "device?video=0&video_size=1920x1080", expect: `ffmpeg -hide_banner -f dshow -video_size 1920x1080 -i "video=0" -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[DEVICE] video will be transcoded to H265 with framerate 20, audio will be skipped", source: "device?video=0&framerate=20#video=h265", expect: `ffmpeg -hide_banner -f dshow -framerate 20 -i "video=0" -c:v libx265 -g 50 -profile:v main -level:v 5.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[DEVICE] video/audio", source: "device?video=FaceTime HD Camera&audio=Microphone (High Definition Audio Device)", expect: `ffmpeg -hide_banner -f dshow -i "video=FaceTime HD Camera:audio=Microphone (High Definition Audio Device)" -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { args := parseArgs(test.source) require.Equal(t, test.expect, args.String()) }) } } func TestParseArgsIpCam(t *testing.T) { tests := []struct { name string source string expect string }{ { name: "[HTTP] video will be copied", source: "http://example.com", expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[HTTP-MJPEG] video will be transcoded to H264", source: "http://example.com#video=h264", expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http://example.com -c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[HLS] video will be copied, audio will be skipped", source: "https://example.com#video=copy", expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i https://example.com -c:v copy -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[RTSP] video will be copied without transcoding codecs", source: "rtsp://example.com", expect: `ffmpeg -hide_banner -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[RTSP] video with resize to 1280x720, should be transcoded, so select H265", source: "rtsp://example.com#video=h265#width=1280#height=720", expect: `ffmpeg -hide_banner -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v libx265 -g 50 -profile:v main -level:v 5.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -vf "scale=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[RTSP] video will be copied, changing RTSP transport from TCP to UDP+TCP", source: "rtsp://example.com#input=rtsp/udp", expect: `ffmpeg -hide_banner -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -i rtsp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[RTMP] video will be copied, changing RTSP transport from TCP to UDP+TCP", source: "rtmp://example.com#input=rtsp/udp", expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -i rtmp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[RTSP] custom timeout", source: "rtsp://example.com#timeout=10", expect: `ffmpeg -hide_banner -allowed_media_types video+audio -fflags nobuffer -flags low_delay -timeout 10000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { args := parseArgs(test.source) require.Equal(t, test.expect, args.String()) }) } } func TestParseArgsAudio(t *testing.T) { tests := []struct { name string source string expect string }{ { name: "[AUDIO] audio will be transcoded to AAC, video will be skipped", source: "rtsp://example.com#audio=aac", expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a aac -vn -f adts -`, }, { name: "[AUDIO] audio will be transcoded to AAC/16000, video will be skipped", source: "rtsp://example.com#audio=aac/16000", expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a aac -ar:a 16000 -ac:a 1 -vn -f adts -`, }, { name: "[AUDIO] audio will be transcoded to OPUS, video will be skipped", source: "rtsp://example.com#audio=opus", expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a libopus -application:a lowdelay -min_comp 0 -vn -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[AUDIO] audio will be transcoded to PCMU, video will be skipped", source: "rtsp://example.com#audio=pcmu", expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_mulaw -ar:a 8000 -ac:a 1 -vn -f wav -`, }, { name: "[AUDIO] audio will be transcoded to PCMU/16000, video will be skipped", source: "rtsp://example.com#audio=pcmu/16000", expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_mulaw -ar:a 16000 -ac:a 1 -vn -f wav -`, }, { name: "[AUDIO] audio will be transcoded to PCMU/48000, video will be skipped", source: "rtsp://example.com#audio=pcmu/48000", expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_mulaw -ar:a 48000 -ac:a 1 -vn -f wav -`, }, { name: "[AUDIO] audio will be transcoded to PCMA, video will be skipped", source: "rtsp://example.com#audio=pcma", expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_alaw -ar:a 8000 -ac:a 1 -vn -f wav -`, }, { name: "[AUDIO] audio will be transcoded to PCMA/16000, video will be skipped", source: "rtsp://example.com#audio=pcma/16000", expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_alaw -ar:a 16000 -ac:a 1 -vn -f wav -`, }, { name: "[AUDIO] audio will be transcoded to PCMA/48000, video will be skipped", source: "rtsp://example.com#audio=pcma/48000", expect: `ffmpeg -hide_banner -allowed_media_types audio -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:a pcm_alaw -ar:a 48000 -ac:a 1 -vn -f wav -`, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { args := parseArgs(test.source) require.Equal(t, test.expect, args.String()) }) } } func TestParseArgsHwVaapi(t *testing.T) { tests := []struct { name string source string expect string }{ { name: "[HTTP-MJPEG] video will be transcoded to H264", source: "http:///example.com#video=h264#hardware=vaapi", expect: `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -fflags nobuffer -flags low_delay -i http:///example.com -c:v h264_vaapi -g 50 -bf 0 -profile:v high -level:v 4.1 -sei:v 0 -an -vf "format=vaapi|nv12,hwupload,scale_vaapi=out_color_matrix=bt709:out_range=tv:format=nv12" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[RTSP] video with rotation, should be transcoded, so select H264", source: "rtsp://example.com#video=h264#rotate=180#hardware=vaapi", expect: `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v h264_vaapi -g 50 -bf 0 -profile:v high -level:v 4.1 -sei:v 0 -an -vf "format=vaapi|nv12,hwupload,transpose_vaapi=4,scale_vaapi=out_color_matrix=bt709:out_range=tv:format=nv12" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[RTSP] video with resize to 1280x720, should be transcoded, so select H265", source: "rtsp://example.com#video=h265#width=1280#height=720#hardware=vaapi", expect: `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v hevc_vaapi -g 50 -bf 0 -profile:v main -level:v 5.1 -sei:v 0 -an -vf "format=vaapi|nv12,hwupload,scale_vaapi=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[FILE] video will be output for MJPEG to pipe, audio will be skipped", source: "/media/bbb.mp4#video=mjpeg#hardware=vaapi", expect: `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -re -i /media/bbb.mp4 -c:v mjpeg_vaapi -an -vf "format=vaapi|nv12,hwupload" -f mjpeg -`, }, { name: "[DEVICE] MJPEG video with size 1920x1080 will be transcoded to H265", source: "device?video=0&video_size=1920x1080#video=h265#hardware=vaapi", expect: `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -f dshow -video_size 1920x1080 -i "video=0" -c:v hevc_vaapi -g 50 -bf 0 -profile:v main -level:v 5.1 -sei:v 0 -an -vf "format=vaapi|nv12,hwupload" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { args := parseArgs(test.source) require.Equal(t, test.expect, args.String()) }) } } func _TestParseArgsHwV4l2m2m(t *testing.T) { // [HTTP-MJPEG] video will be transcoded to H264 args := parseArgs("http:///example.com#video=h264#hardware=v4l2m2m") require.Equal(t, `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http:///example.com -c:v h264_v4l2m2m -g 50 -bf 0 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) // [RTSP] video with rotation, should be transcoded, so select H264 args = parseArgs("rtsp://example.com#video=h264#rotate=180#hardware=v4l2m2m") require.Equal(t, `ffmpeg -hide_banner -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v h264_v4l2m2m -g 50 -bf 0 -an -vf "transpose=1,transpose=1" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) // [RTSP] video with resize to 1280x720, should be transcoded, so select H265 args = parseArgs("rtsp://example.com#video=h265#width=1280#height=720#hardware=v4l2m2m") require.Equal(t, `ffmpeg -hide_banner -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v hevc_v4l2m2m -g 50 -bf 0 -an -vf "scale=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) // [DEVICE] MJPEG video with size 1920x1080 will be transcoded to H265 args = parseArgs("device?video=0&video_size=1920x1080#video=h265#hardware=v4l2m2m") require.Equal(t, `ffmpeg -hide_banner -f dshow -video_size 1920x1080 -i video="0" -c:v hevc_v4l2m2m -g 50 -bf 0 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) } func TestParseArgsHwRKMPP(t *testing.T) { tests := []struct { name string source string expect string }{ { name: "[FILE] transcoding to H264", source: "bbb.mp4#video=h264#hardware=rkmpp", expect: `ffmpeg -hide_banner -hwaccel rkmpp -hwaccel_output_format drm_prime -afbc rga -re -i bbb.mp4 -c:v h264_rkmpp -g 50 -bf 0 -profile:v high -level:v 4.1 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[FILE] transcoding with rotation", source: "bbb.mp4#video=h264#rotate=180#hardware=rkmpp", expect: `ffmpeg -hide_banner -hwaccel rkmpp -hwaccel_output_format drm_prime -afbc rga -re -i bbb.mp4 -c:v h264_rkmpp -g 50 -bf 0 -profile:v high -level:v 4.1 -an -vf "format=drm_prime|nv12,hwupload,vpp_rkrga=transpose=4" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { name: "[FILE] transcoding with scaling", source: "bbb.mp4#video=h264#height=320#hardware=rkmpp", expect: `ffmpeg -hide_banner -hwaccel rkmpp -hwaccel_output_format drm_prime -afbc rga -re -i bbb.mp4 -c:v h264_rkmpp -g 50 -bf 0 -profile:v high -level:v 4.1 -an -vf "format=drm_prime|nv12,hwupload,scale_rkrga=-1:320:force_original_aspect_ratio=0" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { args := parseArgs(test.source) require.Equal(t, test.expect, args.String()) }) } } func _TestParseArgsHwCuda(t *testing.T) { // [HTTP-MJPEG] video will be transcoded to H264 args := parseArgs("http:///example.com#video=h264#hardware=cuda") require.Equal(t, `ffmpeg -hide_banner -hwaccel cuda -hwaccel_output_format cuda -fflags nobuffer -flags low_delay -i http:///example.com -c:v h264_nvenc -g 50 -bf 0 -profile:v high -level:v auto -preset:v p2 -tune:v ll -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) // [RTSP] video with rotation, should be transcoded, so select H264 args = parseArgs("rtsp://example.com#video=h264#rotate=180#hardware=cuda") require.Equal(t, `ffmpeg -hide_banner -hwaccel cuda -hwaccel_output_format nv12 -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v h264_nvenc -g 50 -bf 0 -profile:v high -level:v auto -preset:v p2 -tune:v ll -an -vf "transpose=1,transpose=1,hwupload" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) // [RTSP] video with resize to 1280x720, should be transcoded, so select H265 args = parseArgs("rtsp://example.com#video=h265#width=1280#height=720#hardware=cuda") require.Equal(t, `ffmpeg -hide_banner -hwaccel cuda -hwaccel_output_format cuda -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v hevc_nvenc -g 50 -bf 0 -profile:v high -level:v auto -an -vf "scale_cuda=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) // [DEVICE] MJPEG video with size 1920x1080 will be transcoded to H265 args = parseArgs("device?video=0&video_size=1920x1080#video=h265#hardware=cuda") require.Equal(t, `ffmpeg -hide_banner -hwaccel cuda -hwaccel_output_format cuda -f dshow -video_size 1920x1080 -i video="0" -c:v hevc_nvenc -g 50 -bf 0 -profile:v high -level:v auto -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) } func _TestParseArgsHwDxva2(t *testing.T) { // [HTTP-MJPEG] video will be transcoded to H264 args := parseArgs("http:///example.com#video=h264#hardware=dxva2") require.Equal(t, `ffmpeg -hide_banner -hwaccel dxva2 -hwaccel_output_format dxva2_vld -fflags nobuffer -flags low_delay -i http:///example.com -c:v h264_qsv -g 50 -bf 0 -profile:v high -level:v 4.1 -async_depth:v 1 -an -vf "hwmap=derive_device=qsv,format=qsv" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) // [RTSP] video with rotation, should be transcoded, so select H264 args = parseArgs("rtsp://example.com#video=h264#rotate=180#hardware=dxva2") require.Equal(t, `ffmpeg -hide_banner -hwaccel dxva2 -hwaccel_output_format dxva2_vld -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v h264_qsv -g 50 -bf 0 -profile:v high -level:v 4.1 -async_depth:v 1 -an -vf "hwmap=derive_device=qsv,format=qsv,transpose=1,transpose=1" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) // [RTSP] video with resize to 1280x720, should be transcoded, so select H265 args = parseArgs("rtsp://example.com#video=h265#width=1280#height=720#hardware=dxva2") require.Equal(t, `ffmpeg -hide_banner -hwaccel dxva2 -hwaccel_output_format dxva2_vld -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v hevc_qsv -g 50 -bf 0 -profile:v high -level:v 5.1 -async_depth:v 1 -an -vf "hwmap=derive_device=qsv,format=qsv,scale_qsv=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) // [FILE] video will be output for MJPEG to pipe, audio will be skipped args = parseArgs("/media/bbb.mp4#video=mjpeg#hardware=dxva2") require.Equal(t, `ffmpeg -hide_banner -hwaccel dxva2 -hwaccel_output_format dxva2_vld -re -i /media/bbb.mp4 -c:v mjpeg_qsv -profile:v high -level:v 5.1 -an -vf "hwmap=derive_device=qsv,format=qsv" -f mjpeg -`, args.String()) // [DEVICE] MJPEG video with size 1920x1080 will be transcoded to H265 args = parseArgs("device?video=0&video_size=1920x1080#video=h265#hardware=dxva2") require.Equal(t, `ffmpeg -hide_banner -hwaccel dxva2 -hwaccel_output_format dxva2_vld -f dshow -video_size 1920x1080 -i video="0" -c:v hevc_qsv -g 50 -bf 0 -profile:v high -level:v 5.1 -async_depth:v 1 -an -vf "hwmap=derive_device=qsv,format=qsv" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) } func _TestParseArgsHwVideotoolbox(t *testing.T) { // [HTTP-MJPEG] video will be transcoded to H264 args := parseArgs("http:///example.com#video=h264#hardware=videotoolbox") require.Equal(t, `ffmpeg -hide_banner -hwaccel videotoolbox -hwaccel_output_format videotoolbox_vld -fflags nobuffer -flags low_delay -i http:///example.com -c:v h264_videotoolbox -g 50 -bf 0 -profile:v high -level:v 4.1 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) // [RTSP] video with rotation, should be transcoded, so select H264 args = parseArgs("rtsp://example.com#video=h264#rotate=180#hardware=videotoolbox") require.Equal(t, `ffmpeg -hide_banner -hwaccel videotoolbox -hwaccel_output_format videotoolbox_vld -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v h264_videotoolbox -g 50 -bf 0 -profile:v high -level:v 4.1 -an -vf "transpose=1,transpose=1" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) // [RTSP] video with resize to 1280x720, should be transcoded, so select H265 args = parseArgs("rtsp://example.com#video=h265#width=1280#height=720#hardware=videotoolbox") require.Equal(t, `ffmpeg -hide_banner -hwaccel videotoolbox -hwaccel_output_format videotoolbox_vld -allowed_media_types video -fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i rtsp://example.com -c:v hevc_videotoolbox -g 50 -bf 0 -profile:v high -level:v 5.1 -an -vf "scale=1280:720" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) // [DEVICE] MJPEG video with size 1920x1080 will be transcoded to H265 args = parseArgs("device?video=0&video_size=1920x1080#video=h265#hardware=videotoolbox") require.Equal(t, `ffmpeg -hide_banner -hwaccel videotoolbox -hwaccel_output_format videotoolbox_vld -f dshow -video_size 1920x1080 -i video="0" -c:v hevc_videotoolbox -g 50 -bf 0 -profile:v high -level:v 5.1 -an -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) } func TestDeckLink(t *testing.T) { args := parseArgs(`DeckLink SDI (2)#video=h264#hardware=vaapi#input=-format_code Hp29 -f decklink -i "{input}"`) require.Equal(t, `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -format_code Hp29 -f decklink -i "DeckLink SDI (2)" -c:v h264_vaapi -g 50 -bf 0 -profile:v high -level:v 4.1 -sei:v 0 -an -vf "format=vaapi|nv12,hwupload,scale_vaapi=out_color_matrix=bt709:out_range=tv:format=nv12" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, args.String()) } func TestDrawText(t *testing.T) { tests := []struct { name string source string expect string }{ { source: "http:///example.com#video=h264#drawtext=fontsize=12", expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http:///example.com -c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -vf "drawtext=fontsize=12:text='%{localtime\:%Y-%m-%d %X}'" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { source: "http:///example.com#video=h264#width=640#drawtext=fontsize=12", expect: `ffmpeg -hide_banner -fflags nobuffer -flags low_delay -i http:///example.com -c:v libx264 -g 50 -profile:v high -level:v 4.1 -preset:v superfast -tune:v zerolatency -pix_fmt:v yuv420p -an -vf "scale=640:-1,drawtext=fontsize=12:text='%{localtime\:%Y-%m-%d %X}'" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, { source: "http:///example.com#video=h264#width=640#drawtext=fontsize=12#hardware=vaapi", expect: `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format nv12 -hwaccel_flags allow_profile_mismatch -fflags nobuffer -flags low_delay -i http:///example.com -c:v h264_vaapi -g 50 -bf 0 -profile:v high -level:v 4.1 -sei:v 0 -an -vf "scale=640:-1,drawtext=fontsize=12:text='%{localtime\:%Y-%m-%d %X}',hwupload" -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { args := parseArgs(test.source) require.Equal(t, test.expect, args.String()) }) } } func TestVersion(t *testing.T) { verAV = ffmpeg.Version61 tests := []struct { name string source string expect string }{ { source: "/media/bbb.mp4", expect: `ffmpeg -hide_banner -readrate_initial_burst 0.001 -re -i /media/bbb.mp4 -c copy -user_agent ffmpeg/go2rtc -rtsp_transport tcp -f rtsp {output}`, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { args := parseArgs(test.source) require.Equal(t, test.expect, args.String()) }) } } ================================================ FILE: internal/ffmpeg/hardware/README.md ================================================ # Hardware You **DON'T** need hardware acceleration if: - you're not using the [FFmpeg source](../README.md) - you're using only `#video=copy` for the FFmpeg source - you're using only `#audio=...` (any audio) transcoding for the FFmpeg source You **NEED** hardware acceleration if you're using `#video=h264`, `#video=h265`, `#video=mjpeg` (video) transcoding. ## Important - Acceleration is disabled by default because it can be unstable (this may change in the future) - go2rtc can automatically detect supported hardware acceleration if enabled - go2rtc will enable hardware decoding only if hardware encoding is supported - go2rtc will use the same GPU for decoder and encoder - Intel and AMD will switch to a software decoder if the input codec isn't supported by the hardware decoder - NVIDIA will fail if the input codec isn't supported by the hardware decoder - Raspberry Pi always uses a software decoder ```yaml streams: # auto select hardware encoder camera1_hw: ffmpeg:rtsp://rtsp:12345678@192.168.1.123/av_stream/ch0#video=h264#hardware # manual select hardware encoder (vaapi, cuda, v4l2m2m, dxva2, videotoolbox) camera1_vaapi: ffmpeg:rtsp://rtsp:12345678@192.168.1.123/av_stream/ch0#video=h264#hardware=vaapi ``` ## Docker and Hass Addon There are two versions of the Docker container and Hass Add-on: - Latest (Alpine) supports hardware acceleration for Intel iGPU (CPU with graphics) and Raspberry Pi. - Hardware (Debian 12) supports Intel iGPU, AMD GPU, NVIDIA GPU. ## Intel iGPU **Supported on:** Windows binary, Linux binary, Docker, Hass Addon. If you have an Intel Sandy Bridge (2011) CPU with graphics, you already have hardware decoding/encoding support for `AVC/H.264`. If you have an Intel Skylake (2015) CPU with graphics, you already have hardware decoding/encoding support for `AVC/H.264`, `HEVC/H.265` and `MJPEG`. Read more [here](https://en.wikipedia.org/wiki/Intel_Quick_Sync_Video#Hardware_decoding_and_encoding) and [here](https://en.wikipedia.org/wiki/Intel_Graphics_Technology#Capabilities_(GPU_video_acceleration)). Linux and Docker: - It may be important to have a recent OS and Linux kernel. For example, on my **Debian 10 (kernel 4.19)** it did not work, but after updating to **Debian 11 (kernel 5.10)** everything was fine. - If you run into trouble, check that you have the `/dev/dri/` folder on your host. Docker users should add the `--privileged` option to the container for access to the hardware. **PS.** Supported via [VAAPI](https://trac.ffmpeg.org/wiki/Hardware/VAAPI) engine on Linux and [DXVA2+QSV](https://trac.ffmpeg.org/wiki/Hardware/QuickSync) engine on Windows. ## AMD GPU *I don't have the hardware to test this!!!* **Supported on:** Linux binary, Docker, Hass Addon. Docker users should install: `alexxit/go2rtc:master-hardware`. Docker users should add the `--privileged` option to the container for access to the hardware. Hass Addon users should install **go2rtc master hardware** version. **PS.** Supported via [VAAPI](https://trac.ffmpeg.org/wiki/Hardware/VAAPI) engine. ## NVIDIA GPU **Supported on:** Windows binary, Linux binary, Docker. Docker users should install: `alexxit/go2rtc:master-hardware`. Read more [here](https://docs.frigate.video/configuration/hardware_acceleration) and [here](https://jellyfin.org/docs/general/administration/hardware-acceleration/#nvidia-hardware-acceleration-on-docker-linux). **PS.** Supported via [CUDA](https://trac.ffmpeg.org/wiki/HWAccelIntro#CUDANVENCNVDEC) engine. ## Raspberry Pi 3 **Supported on:** Linux binary, Docker, Hass Addon. I don't recommend using transcoding on the Raspberry Pi 3. It's extremely slow, even with hardware acceleration. Also, it may fail when transcoding a 2K+ stream. ## Raspberry Pi 4 *I don't have the hardware to test this!!!* **Supported on:** Linux binary, Docker, Hass Addon. **PS.** Supported via [v4l2m2m](https://lalitm.com/hw-encoding-raspi/) engine. ## macOS In my tests, transcoding is faster on the M1 CPU than on the M1 GPU. Transcoding time on the M1 CPU is better than any Intel iGPU and comparable to an NVIDIA RTX 2070. **PS.** Supported via [videotoolbox](https://trac.ffmpeg.org/wiki/HWAccelIntro#VideoToolbox) engine. ## Rockchip - It's important to use a custom FFmpeg build with Rockchip support from [@nyanmisaka](https://github.com/nyanmisaka/ffmpeg-rockchip) - Static binaries from [@MarcA711](https://github.com/MarcA711/Rockchip-FFmpeg-Builds/releases/) - It's important to have Linux kernel 5.10 or 6.1 **Tested** - [Orange Pi 3B](https://www.armbian.com/orangepi3b/) with Armbian 6.1, supports transcoding H.264, H.265, MJPEG ================================================ FILE: internal/ffmpeg/hardware/hardware.go ================================================ package hardware import ( "net/http" "os/exec" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/pkg/ffmpeg" ) const ( EngineSoftware = "software" EngineVAAPI = "vaapi" // Intel iGPU and AMD GPU EngineV4L2M2M = "v4l2m2m" // Raspberry Pi 3 and 4 EngineCUDA = "cuda" // NVidia on Windows and Linux EngineDXVA2 = "dxva2" // Intel on Windows EngineVideoToolbox = "videotoolbox" // macOS EngineRKMPP = "rkmpp" // Rockchip ) func Init(bin string) { api.HandleFunc("api/ffmpeg/hardware", func(w http.ResponseWriter, r *http.Request) { api.ResponseSources(w, ProbeAll(bin)) }) } // MakeHardware converts software FFmpeg args to hardware args // empty engine for autoselect func MakeHardware(args *ffmpeg.Args, engine string, defaults map[string]string) { for i, codec := range args.Codecs { if len(codec) < 10 { continue // skip short line (-c:v mjpeg...) } // get current codec name name := cut(codec, ' ', 1) switch name { case "libx264": name = "h264" case "libx265": name = "h265" case "mjpeg": default: continue // skip unsupported codec } // temporary disable probe for H265 if engine == "" && name != "h265" { if engine = cache[name]; engine == "" { engine = ProbeHardware(args.Bin, name) cache[name] = engine } } switch engine { case EngineVAAPI: args.Codecs[i] = defaults[name+"/"+engine] if !args.HasFilters("drawtext=") { args.Input = "-hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch " + args.Input if name == "h264" { fixPixelFormat(args) } for i, filter := range args.Filters { if strings.HasPrefix(filter, "scale=") { args.Filters[i] = "scale_vaapi=" + filter[6:] } if strings.HasPrefix(filter, "transpose=") { if filter == "transpose=1,transpose=1" { // 180 degrees half-turn args.Filters[i] = "transpose_vaapi=4" // reversal } else { args.Filters[i] = "transpose_vaapi=" + filter[10:] } } } // fix if input doesn't support hwaccel, do nothing when support // insert as first filter before hardware scale and transpose args.InsertFilter("format=vaapi|nv12,hwupload") } else { // enable software pixel for drawtext, scale and transpose args.Input = "-hwaccel vaapi -hwaccel_output_format nv12 -hwaccel_flags allow_profile_mismatch " + args.Input args.AddFilter("hwupload") } case EngineCUDA: args.Codecs[i] = defaults[name+"/"+engine] // CUDA doesn't support hardware transpose // https://github.com/AlexxIT/go2rtc/issues/389 if !args.HasFilters("drawtext=", "transpose=") { args.Input = "-hwaccel cuda -hwaccel_output_format cuda " + args.Input for i, filter := range args.Filters { if strings.HasPrefix(filter, "scale=") { args.Filters[i] = "scale_cuda=" + filter[6:] } } } else { args.Input = "-hwaccel cuda -hwaccel_output_format nv12 " + args.Input args.AddFilter("hwupload") } case EngineDXVA2: args.Input = "-hwaccel dxva2 -hwaccel_output_format dxva2_vld " + args.Input args.Codecs[i] = defaults[name+"/"+engine] for i, filter := range args.Filters { if strings.HasPrefix(filter, "scale=") { args.Filters[i] = "scale_qsv=" + filter[6:] } } args.InsertFilter("hwmap=derive_device=qsv,format=qsv") case EngineVideoToolbox: args.Input = "-hwaccel videotoolbox -hwaccel_output_format videotoolbox_vld " + args.Input args.Codecs[i] = defaults[name+"/"+engine] case EngineV4L2M2M: args.Codecs[i] = defaults[name+"/"+engine] case EngineRKMPP: args.Codecs[i] = defaults[name+"/"+engine] if !args.HasFilters("drawtext=") { args.Input = "-hwaccel rkmpp -hwaccel_output_format drm_prime -afbc rga " + args.Input for i, filter := range args.Filters { if strings.HasPrefix(filter, "scale=") { args.Filters[i] = "scale_rkrga=" + filter[6:] + ":force_original_aspect_ratio=0" } if strings.HasPrefix(filter, "transpose=") { if filter == "transpose=1,transpose=1" { // 180 degrees half-turn args.Filters[i] = "vpp_rkrga=transpose=4" // reversal } else { args.Filters[i] = "vpp_rkrga=transpose=" + filter[10:] } } } if len(args.Filters) > 0 { // fix if input doesn't support hwaccel, do nothing when support // insert as first filter before hardware scale and transpose args.InsertFilter("format=drm_prime|nv12,hwupload") } } else { // enable software pixel for drawtext, scale and transpose args.Input = "-hwaccel rkmpp -hwaccel_output_format nv12 -afbc rga " + args.Input args.AddFilter("hwupload") } } } } var cache = map[string]string{} func run(bin string, args string) bool { err := exec.Command(bin, strings.Split(args, " ")...).Run() return err == nil } func runToString(bin string, args string) string { if run(bin, args) { return "OK" } else { return "ERROR" } } func cut(s string, sep byte, pos int) string { for n := 0; n < pos; n++ { if i := strings.IndexByte(s, sep); i > 0 { s = s[i+1:] } else { return "" } } if i := strings.IndexByte(s, sep); i > 0 { return s[:i] } return s } // fixPixelFormat: // - good h264 pixel: yuv420p(tv, bt709) == yuv420p (mpeg/limited/tv) // - bad h264 pixel: yuvj420p(pc, bt709) == yuvj420p (jpeg/full/pc) // - bad jpeg pixel: yuvj422p(pc, bt470bg) func fixPixelFormat(args *ffmpeg.Args) { // in my tests this filters has same CPU/GPU load: // - "hwupload" // - "hwupload,scale_vaapi=out_color_matrix=bt709:out_range=tv" // - "hwupload,scale_vaapi=out_color_matrix=bt709:out_range=tv:format=nv12" const fixPixFmt = "out_color_matrix=bt709:out_range=tv:format=nv12" for i, filter := range args.Filters { if strings.HasPrefix(filter, "scale=") { args.Filters[i] = filter + ":" + fixPixFmt return } } args.Filters = append(args.Filters, "scale="+fixPixFmt) } ================================================ FILE: internal/ffmpeg/hardware/hardware_bsd.go ================================================ //go:build freebsd || netbsd || openbsd || dragonfly package hardware import ( "runtime" "github.com/AlexxIT/go2rtc/internal/api" ) const ( ProbeV4L2M2MH264 = "-f lavfi -i testsrc2 -t 1 -c h264_v4l2m2m -f null -" ProbeV4L2M2MH265 = "-f lavfi -i testsrc2 -t 1 -c hevc_v4l2m2m -f null -" ProbeRKMPPH264 = "-f lavfi -i testsrc2 -t 1 -c h264_rkmpp_encoder -f null -" ProbeRKMPPH265 = "-f lavfi -i testsrc2 -t 1 -c hevc_rkmpp_encoder -f null -" ) func ProbeAll(bin string) []*api.Source { return []*api.Source{ { Name: runToString(bin, ProbeV4L2M2MH264), URL: "ffmpeg:...#video=h264#hardware=" + EngineV4L2M2M, }, { Name: runToString(bin, ProbeV4L2M2MH265), URL: "ffmpeg:...#video=h265#hardware=" + EngineV4L2M2M, }, { Name: runToString(bin, ProbeRKMPPH264), URL: "ffmpeg:...#video=h264#hardware=" + EngineRKMPP, }, { Name: runToString(bin, ProbeRKMPPH265), URL: "ffmpeg:...#video=h265#hardware=" + EngineRKMPP, }, } } func ProbeHardware(bin, name string) string { if runtime.GOARCH == "arm64" || runtime.GOARCH == "arm" { switch name { case "h264": if run(bin, ProbeV4L2M2MH264) { return EngineV4L2M2M } if run(bin, ProbeRKMPPH264) { return EngineRKMPP } case "h265": if run(bin, ProbeV4L2M2MH265) { return EngineV4L2M2M } if run(bin, ProbeRKMPPH265) { return EngineRKMPP } } return EngineSoftware } return EngineSoftware } ================================================ FILE: internal/ffmpeg/hardware/hardware_darwin.go ================================================ //go:build darwin || ios package hardware import ( "github.com/AlexxIT/go2rtc/internal/api" ) const ProbeVideoToolboxH264 = "-f lavfi -i testsrc2=size=svga -t 1 -c h264_videotoolbox -f null -" const ProbeVideoToolboxH265 = "-f lavfi -i testsrc2=size=svga -t 1 -c hevc_videotoolbox -f null -" func ProbeAll(bin string) []*api.Source { return []*api.Source{ { Name: runToString(bin, ProbeVideoToolboxH264), URL: "ffmpeg:...#video=h264#hardware=" + EngineVideoToolbox, }, { Name: runToString(bin, ProbeVideoToolboxH265), URL: "ffmpeg:...#video=h265#hardware=" + EngineVideoToolbox, }, } } func ProbeHardware(bin, name string) string { switch name { case "h264": if run(bin, ProbeVideoToolboxH264) { return EngineVideoToolbox } case "h265": if run(bin, ProbeVideoToolboxH265) { return EngineVideoToolbox } } return EngineSoftware } ================================================ FILE: internal/ffmpeg/hardware/hardware_unix.go ================================================ //go:build unix && !darwin && !freebsd && !netbsd && !openbsd && !dragonfly package hardware import ( "runtime" "github.com/AlexxIT/go2rtc/internal/api" ) const ( ProbeV4L2M2MH264 = "-f lavfi -i testsrc2 -t 1 -c h264_v4l2m2m -f null -" ProbeV4L2M2MH265 = "-f lavfi -i testsrc2 -t 1 -c hevc_v4l2m2m -f null -" ProbeRKMPPH264 = "-f lavfi -i testsrc2 -t 1 -c h264_rkmpp -f null -" ProbeRKMPPH265 = "-f lavfi -i testsrc2 -t 1 -c hevc_rkmpp -f null -" ProbeRKMPPJPEG = "-f lavfi -i testsrc2 -t 1 -c mjpeg_rkmpp -f null -" ProbeVAAPIH264 = "-init_hw_device vaapi -f lavfi -i testsrc2 -t 1 -vf format=nv12,hwupload -c h264_vaapi -f null -" ProbeVAAPIH265 = "-init_hw_device vaapi -f lavfi -i testsrc2 -t 1 -vf format=nv12,hwupload -c hevc_vaapi -f null -" ProbeVAAPIJPEG = "-init_hw_device vaapi -f lavfi -i testsrc2 -t 1 -vf format=nv12,hwupload -c mjpeg_vaapi -f null -" ProbeCUDAH264 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c h264_nvenc -f null -" ProbeCUDAH265 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c hevc_nvenc -f null -" ) func ProbeAll(bin string) []*api.Source { if runtime.GOARCH == "arm64" || runtime.GOARCH == "arm" { return []*api.Source{ { Name: runToString(bin, ProbeV4L2M2MH264), URL: "ffmpeg:...#video=h264#hardware=" + EngineV4L2M2M, }, { Name: runToString(bin, ProbeV4L2M2MH265), URL: "ffmpeg:...#video=h265#hardware=" + EngineV4L2M2M, }, { Name: runToString(bin, ProbeRKMPPH264), URL: "ffmpeg:...#video=h264#hardware=" + EngineRKMPP, }, { Name: runToString(bin, ProbeRKMPPH265), URL: "ffmpeg:...#video=h265#hardware=" + EngineRKMPP, }, { Name: runToString(bin, ProbeRKMPPJPEG), URL: "ffmpeg:...#video=mjpeg#hardware=" + EngineRKMPP, }, } } return []*api.Source{ { Name: runToString(bin, ProbeVAAPIH264), URL: "ffmpeg:...#video=h264#hardware=" + EngineVAAPI, }, { Name: runToString(bin, ProbeVAAPIH265), URL: "ffmpeg:...#video=h265#hardware=" + EngineVAAPI, }, { Name: runToString(bin, ProbeVAAPIJPEG), URL: "ffmpeg:...#video=mjpeg#hardware=" + EngineVAAPI, }, { Name: runToString(bin, ProbeCUDAH264), URL: "ffmpeg:...#video=h264#hardware=" + EngineCUDA, }, { Name: runToString(bin, ProbeCUDAH265), URL: "ffmpeg:...#video=h265#hardware=" + EngineCUDA, }, } } func ProbeHardware(bin, name string) string { if runtime.GOARCH == "arm64" || runtime.GOARCH == "arm" { switch name { case "h264": if run(bin, ProbeV4L2M2MH264) { return EngineV4L2M2M } if run(bin, ProbeRKMPPH264) { return EngineRKMPP } case "h265": if run(bin, ProbeV4L2M2MH265) { return EngineV4L2M2M } if run(bin, ProbeRKMPPH265) { return EngineRKMPP } case "mjpeg": if run(bin, ProbeRKMPPJPEG) { return EngineRKMPP } } return EngineSoftware } switch name { case "h264": if run(bin, ProbeCUDAH264) { return EngineCUDA } if run(bin, ProbeVAAPIH264) { return EngineVAAPI } case "h265": if run(bin, ProbeCUDAH265) { return EngineCUDA } if run(bin, ProbeVAAPIH265) { return EngineVAAPI } case "mjpeg": if run(bin, ProbeVAAPIJPEG) { return EngineVAAPI } } return EngineSoftware } ================================================ FILE: internal/ffmpeg/hardware/hardware_windows.go ================================================ //go:build windows package hardware import "github.com/AlexxIT/go2rtc/internal/api" const ProbeDXVA2H264 = "-init_hw_device dxva2 -f lavfi -i testsrc2 -t 1 -c h264_qsv -f null -" const ProbeDXVA2H265 = "-init_hw_device dxva2 -f lavfi -i testsrc2 -t 1 -c hevc_qsv -f null -" const ProbeDXVA2JPEG = "-init_hw_device dxva2 -f lavfi -i testsrc2 -t 1 -c mjpeg_qsv -f null -" const ProbeCUDAH264 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c h264_nvenc -f null -" const ProbeCUDAH265 = "-init_hw_device cuda -f lavfi -i testsrc2 -t 1 -c hevc_nvenc -f null -" func ProbeAll(bin string) []*api.Source { return []*api.Source{ { Name: runToString(bin, ProbeDXVA2H264), URL: "ffmpeg:...#video=h264#hardware=" + EngineDXVA2, }, { Name: runToString(bin, ProbeDXVA2H265), URL: "ffmpeg:...#video=h265#hardware=" + EngineDXVA2, }, { Name: runToString(bin, ProbeDXVA2JPEG), URL: "ffmpeg:...#video=mjpeg#hardware=" + EngineDXVA2, }, { Name: runToString(bin, ProbeCUDAH264), URL: "ffmpeg:...#video=h264#hardware=" + EngineCUDA, }, { Name: runToString(bin, ProbeCUDAH265), URL: "ffmpeg:...#video=h265#hardware=" + EngineCUDA, }, } } func ProbeHardware(bin, name string) string { switch name { case "h264": if run(bin, ProbeCUDAH264) { return EngineCUDA } if run(bin, ProbeDXVA2H264) { return EngineDXVA2 } case "h265": if run(bin, ProbeCUDAH265) { return EngineCUDA } if run(bin, ProbeDXVA2H265) { return EngineDXVA2 } case "mjpeg": if run(bin, ProbeDXVA2JPEG) { return EngineDXVA2 } } return EngineSoftware } ================================================ FILE: internal/ffmpeg/jpeg.go ================================================ package ffmpeg import ( "bytes" "fmt" "net/url" "os/exec" "github.com/AlexxIT/go2rtc/internal/ffmpeg/hardware" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/ffmpeg" "github.com/AlexxIT/go2rtc/pkg/shell" ) func JPEGWithQuery(b []byte, query url.Values) ([]byte, error) { args := parseQuery(query) return transcode(b, args.String()) } func JPEGWithScale(b []byte, width, height int) ([]byte, error) { args := defaultArgs() args.AddFilter(fmt.Sprintf("scale=%d:%d", width, height)) return transcode(b, args.String()) } func transcode(b []byte, args string) ([]byte, error) { cmdArgs := shell.QuoteSplit(args) cmd := exec.Command(cmdArgs[0], cmdArgs[1:]...) cmd.Stdin = bytes.NewBuffer(b) return cmd.Output() } func defaultArgs() *ffmpeg.Args { return &ffmpeg.Args{ Bin: defaults["bin"], Global: defaults["global"], Input: "-i -", Codecs: []string{defaults["mjpeg"]}, Output: defaults["output/mjpeg"], } } func parseQuery(query url.Values) *ffmpeg.Args { args := defaultArgs() var width = -1 var height = -1 var r, hw string for k, v := range query { switch k { case "width", "w": width = core.Atoi(v[0]) case "height", "h": height = core.Atoi(v[0]) case "rotate": r = v[0] case "hardware", "hw": hw = v[0] } } if width > 0 || height > 0 { args.AddFilter(fmt.Sprintf("scale=%d:%d", width, height)) } if r != "" { switch r { case "90": args.AddFilter("transpose=1") // 90 degrees clockwise case "180": args.AddFilter("transpose=1,transpose=1") case "-90", "270": args.AddFilter("transpose=2") // 90 degrees counterclockwise } } if hw != "" { hardware.MakeHardware(args, hw, defaults) } return args } ================================================ FILE: internal/ffmpeg/jpeg_test.go ================================================ package ffmpeg import ( "net/url" "testing" "github.com/stretchr/testify/require" ) func TestParseQuery(t *testing.T) { args := parseQuery(nil) require.Equal(t, `ffmpeg -hide_banner -i - -c:v mjpeg -f mjpeg -`, args.String()) query, err := url.ParseQuery("h=480") require.Nil(t, err) args = parseQuery(query) require.Equal(t, `ffmpeg -hide_banner -i - -c:v mjpeg -vf "scale=-1:480" -f mjpeg -`, args.String()) query, err = url.ParseQuery("hw=vaapi") require.Nil(t, err) args = parseQuery(query) require.Equal(t, `ffmpeg -hide_banner -hwaccel vaapi -hwaccel_output_format vaapi -hwaccel_flags allow_profile_mismatch -i - -c:v mjpeg_vaapi -vf "format=vaapi|nv12,hwupload" -f mjpeg -`, args.String()) } ================================================ FILE: internal/ffmpeg/producer.go ================================================ package ffmpeg import ( "encoding/json" "errors" "net/url" "strconv" "strings" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" ) type Producer struct { core.Connection url string query url.Values ffmpeg core.Producer } // NewProducer - FFmpeg producer with auto selection video/audio codec based on client capabilities func NewProducer(url string) (core.Producer, error) { p := &Producer{} i := strings.IndexByte(url, '#') p.url, p.query = url[:i], streams.ParseQuery(url[i+1:]) // ffmpeg.NewProducer support only one audio if len(p.query["video"]) != 0 || len(p.query["audio"]) != 1 { return nil, errors.New("ffmpeg: unsupported params: " + url[i:]) } p.ID = core.NewID() p.FormatName = "ffmpeg" p.Medias = []*core.Media{ { // we can support only audio, because don't know FmtpLine for H264 and PayloadType for MJPEG Kind: core.KindAudio, Direction: core.DirectionRecvonly, // codecs in order from best to worst Codecs: []*core.Codec{ // OPUS will always marked as OPUS/48000/2 {Name: core.CodecOpus, ClockRate: 48000, Channels: 2}, {Name: core.CodecPCML, ClockRate: 16000}, {Name: core.CodecPCM, ClockRate: 16000}, {Name: core.CodecPCMA, ClockRate: 16000}, {Name: core.CodecPCMU, ClockRate: 16000}, {Name: core.CodecPCML, ClockRate: 8000}, {Name: core.CodecPCM, ClockRate: 8000}, {Name: core.CodecPCMA, ClockRate: 8000}, {Name: core.CodecPCMU, ClockRate: 8000}, // AAC has unknown problems on Dahua two way {Name: core.CodecAAC, ClockRate: 16000, FmtpLine: aac.FMTP + "1408"}, }, }, } return p, nil } func (p *Producer) Start() error { var err error if p.ffmpeg, err = streams.GetProducer(p.newURL()); err != nil { return err } for i, media := range p.ffmpeg.GetMedias() { track, err := p.ffmpeg.GetTrack(media, media.Codecs[0]) if err != nil { return err } p.Receivers[i].Replace(track) } return p.ffmpeg.Start() } func (p *Producer) Stop() error { if p.ffmpeg == nil { return nil } return p.ffmpeg.Stop() } func (p *Producer) MarshalJSON() ([]byte, error) { if p.ffmpeg == nil { return json.Marshal(p.Connection) } return json.Marshal(p.ffmpeg) } func (p *Producer) newURL() string { s := p.url // rewrite codecs in url from auto to known presets from defaults for _, receiver := range p.Receivers { codec := receiver.Codec switch codec.Name { case core.CodecOpus: s += "#audio=opus/16000" case core.CodecAAC: s += "#audio=aac/16000" case core.CodecPCML: s += "#audio=pcml/" + strconv.Itoa(int(codec.ClockRate)) case core.CodecPCM: s += "#audio=pcm/" + strconv.Itoa(int(codec.ClockRate)) case core.CodecPCMA: s += "#audio=pcma/" + strconv.Itoa(int(codec.ClockRate)) case core.CodecPCMU: s += "#audio=pcmu/" + strconv.Itoa(int(codec.ClockRate)) } } // add other params for key, values := range p.query { if key != "audio" { for _, value := range values { s += "#" + key + "=" + value } } } return s } ================================================ FILE: internal/ffmpeg/version.go ================================================ package ffmpeg import ( "errors" "os/exec" "sync" "github.com/AlexxIT/go2rtc/pkg/ffmpeg" ) var verMu sync.Mutex var verErr error var verFF string var verAV string func Version() (string, error) { verMu.Lock() defer verMu.Unlock() if verFF != "" { return verFF, verErr } cmd := exec.Command(defaults["bin"], "-version") b, err := cmd.Output() if err != nil { verFF = "-" verErr = err return verFF, verErr } verFF, verAV = ffmpeg.ParseVersion(b) if verFF == "" { verFF = "?" } // better to compare libavformat, because nightly/master builds if verAV != "" && verAV < ffmpeg.Version50 { verErr = errors.New("ffmpeg: unsupported version: " + verFF) } log.Debug().Str("version", verFF).Str("libavformat", verAV).Msgf("[ffmpeg] bin") return verFF, verErr } ================================================ FILE: internal/ffmpeg/virtual/virtual.go ================================================ package virtual import ( "net/url" ) func GetInput(src string) string { query, err := url.ParseQuery(src) if err != nil { return "" } input := "-re" for _, video := range query["video"] { // https://ffmpeg.org/ffmpeg-filters.html sep := "=" // first separator if video == "" { video = "testsrc=decimals=2" // default video sep = ":" } input += " -f lavfi -i " + video // set defaults (using Add instead of Set) query.Add("size", "1920x1080") for key, values := range query { value := values[0] // https://ffmpeg.org/ffmpeg-utils.html#video-size-syntax switch key { case "color", "rate", "duration", "sar", "decimals": case "size": switch value { case "720": value = "1280x720" // crf=1 -> 12 Mbps case "1080": value = "1920x1080" // crf=1 -> 25 Mbps case "2K": value = "2560x1440" // crf=1 -> 43 Mbps case "4K": value = "3840x2160" // crf=1 -> 103 Mbps case "8K": value = "7680x4230" // https://reolink.com/blog/8k-resolution/ } default: continue } input += sep + key + "=" + value sep = ":" // next separator } if s := query.Get("format"); s != "" { input += ",format=" + s } } return input } func GetInputTTS(src string) string { query, err := url.ParseQuery(src) if err != nil { return "" } input := `-re -f lavfi -i "flite=text='` + query.Get("text") + `'` // ffmpeg -f lavfi -i flite=list_voices=1 // awb, kal, kal16, rms, slt if voice := query.Get("voice"); voice != "" { input += ":voice" + voice } return input + `"` } ================================================ FILE: internal/ffmpeg/virtual/virtual_test.go ================================================ package virtual import ( "testing" "github.com/stretchr/testify/require" ) func TestGetInput(t *testing.T) { s := GetInput("video") require.Equal(t, "-re -f lavfi -i testsrc=decimals=2:size=1920x1080", s) s = GetInput("video=testsrc2&size=4K") require.Equal(t, "-re -f lavfi -i testsrc2=size=3840x2160", s) } func TestGetInputTTS(t *testing.T) { s := GetInputTTS("text=hello world&voice=slt") require.Equal(t, `-re -f lavfi -i "flite=text='hello world':voiceslt"`, s) } ================================================ FILE: internal/flussonic/README.md ================================================ # Flussonic [`new in v1.9.10`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.10) Support streams from [Flussonic](https://flussonic.com/) server. Related [issue](https://github.com/AlexxIT/go2rtc/issues/1678). ================================================ FILE: internal/flussonic/flussonic.go ================================================ package flussonic import ( "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/flussonic" ) func Init() { streams.HandleFunc("flussonic", flussonic.Dial) } ================================================ FILE: internal/gopro/README.md ================================================ # GoPro [`new in v1.8.3`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.8.3) Support streaming from [GoPro](https://gopro.com/) cameras, connected via USB or Wi-Fi to Linux, Mac, Windows. Supported models: HERO9, HERO10, HERO11, HERO12. Supported OS: Linux, Mac, Windows, [HassOS](https://www.home-assistant.io/installation/) Other camera models have different APIs. I will try to add them in future versions. ## Configuration - USB-connected cameras create a new network interface in the system - Linux users do not need to install anything - Windows users should install the [network driver](https://community.gopro.com/s/article/GoPro-Webcam) - if the camera is detected but the stream does not start, you need to disable the firewall 1. Discover camera address: WebUI > Add > GoPro 2. Add camera to config ```yaml streams: hero12: gopro://172.20.100.51 ``` ## Useful links - https://gopro.github.io/OpenGoPro/ ================================================ FILE: internal/gopro/gopro.go ================================================ package gopro import ( "net/http" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/gopro" ) func Init() { streams.HandleFunc("gopro", func(source string) (core.Producer, error) { return gopro.Dial(source) }) api.HandleFunc("api/gopro", apiGoPro) } func apiGoPro(w http.ResponseWriter, r *http.Request) { var items []*api.Source for _, host := range gopro.Discovery() { items = append(items, &api.Source{Name: host, URL: "gopro://" + host}) } api.ResponseSources(w, items) } ================================================ FILE: internal/hass/README.md ================================================ # Hass Support import camera links from [Home Assistant](https://www.home-assistant.io/) config files: - [Generic Camera](https://www.home-assistant.io/integrations/generic/), setup via GUI - [HomeKit Camera](https://www.home-assistant.io/integrations/homekit_controller/) - [ONVIF](https://www.home-assistant.io/integrations/onvif/) - [Roborock](https://github.com/humbertogontijo/homeassistant-roborock) vacuums with camera ## Configuration ```yaml hass: config: "/config" # skip this setting if you are a Home Assistant add-on user streams: generic_camera: hass:Camera1 # Settings > Integrations > Integration Name aqara_g3: hass:Camera-Hub-G3-AB12 ``` ### WebRTC Cameras [`new in v1.6.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.6.0) Any cameras in WebRTC format are supported. But at the moment Home Assistant only supports some [Nest](https://www.home-assistant.io/integrations/nest/) cameras in this format. **Important.** The Nest API only allows you to get a link to a stream for 5 minutes. Do not use this with Frigate! If the stream expires, Frigate will consume all available RAM on your machine within seconds. It's recommended to use [Nest source](../nest/README.md) - it supports extending the stream. ```yaml streams: # link to Home Assistant Supervised hass-webrtc1: hass://supervisor?entity_id=camera.nest_doorbell # link to external Home Assistant with Long-Lived Access Tokens hass-webrtc2: hass://192.168.1.123:8123?entity_id=camera.nest_doorbell&token=eyXYZ... ``` ### RTSP Cameras By default, the Home Assistant API does not allow you to get a dynamic RTSP link to a camera stream. [This method](https://github.com/felipecrs/hass-expose-camera-stream-source#importing-cameras-from-home-assistant-to-go2rtc-or-frigate) can work around it. ================================================ FILE: internal/hass/api.go ================================================ package hass import ( "encoding/base64" "encoding/json" "net" "net/http" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/internal/webrtc" ) func apiOK(w http.ResponseWriter, r *http.Request) { api.Response(w, `{"status":1,"payload":{}}`, api.MimeJSON) } func apiStream(w http.ResponseWriter, r *http.Request) { switch { // /stream/{id}/add case strings.HasSuffix(r.RequestURI, "/add"): var v addJSON if err := json.NewDecoder(r.Body).Decode(&v); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } // we can get three types of links: // 1. link to go2rtc stream: rtsp://...:8554/{stream_name} // 2. static link to Hass camera // 3. dynamic link to Hass camera if _, err := streams.Patch(v.Name, v.Channels.First.Url); err == nil { apiOK(w, r) } else { http.Error(w, err.Error(), http.StatusBadRequest) } // /stream/{id}/channel/0/webrtc default: i := strings.IndexByte(r.RequestURI[8:], '/') if i <= 0 { http.Error(w, "", http.StatusBadRequest) return } name := r.RequestURI[8 : 8+i] stream := streams.Get(name) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } if err := r.ParseForm(); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } s := r.FormValue("data") offer, err := base64.StdEncoding.DecodeString(s) if err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } s, err = webrtc.ExchangeSDP(stream, string(offer), "hass/webrtc", r.UserAgent()) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } s = base64.StdEncoding.EncodeToString([]byte(s)) _, _ = w.Write([]byte(s)) } } func HassioAddr() string { ints, _ := net.Interfaces() for _, i := range ints { if i.Name != "hassio" { continue } addrs, _ := i.Addrs() for _, addr := range addrs { if addr, ok := addr.(*net.IPNet); ok { return addr.IP.String() } } } return "" } type addJSON struct { Name string `json:"name"` Channels struct { First struct { //Name string `json:"name"` Url string `json:"url"` } `json:"0"` } `json:"channels"` } ================================================ FILE: internal/hass/hass.go ================================================ package hass import ( "bytes" "encoding/json" "fmt" "net/http" "os" "path" "strings" "sync" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/roborock" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/hass" "github.com/rs/zerolog" ) func Init() { var conf struct { API struct { Listen string `yaml:"listen"` } `yaml:"api"` Mod struct { Config string `yaml:"config"` } `yaml:"hass"` } app.LoadConfig(&conf) log = app.GetLogger("hass") // support API for https://www.home-assistant.io/integrations/rtsp_to_webrtc/ api.HandleFunc("/static", apiOK) api.HandleFunc("/streams", apiOK) api.HandleFunc("/stream/", apiStream) streams.RedirectFunc("hass", func(rawURL string) (string, error) { rawURL, rawQuery, _ := strings.Cut(rawURL, "#") if location := entities[rawURL[5:]]; location != "" { if rawQuery != "" { return location + "#" + rawQuery, nil } return location, nil } return "", nil }) streams.HandleFunc("hass", func(source string) (core.Producer, error) { // support hass://supervisor?entity_id=camera.driveway_doorbell return hass.NewClient(source) }) // load static entries from Hass config if err := importConfig(conf.Mod.Config); err != nil { log.Trace().Msgf("[hass] can't import config: %s", err) api.HandleFunc("api/hass", func(w http.ResponseWriter, _ *http.Request) { http.Error(w, "no hass config", http.StatusNotFound) }) return } api.HandleFunc("api/hass", func(w http.ResponseWriter, _ *http.Request) { once.Do(func() { // load WebRTC entities from Hass API, works only for add-on version if token := hass.SupervisorToken(); token != "" { if err := importWebRTC(token); err != nil { log.Warn().Err(err).Caller().Send() } } }) var items []*api.Source for name, url := range entities { items = append(items, &api.Source{ Name: name, URL: "hass:" + name, Location: url, }) } api.ResponseSources(w, items) }) // for Addon listen on hassio interface, so WebUI feature will work if conf.API.Listen == "127.0.0.1:1984" { if addr := HassioAddr(); addr != "" { addr += ":1984" go func() { log.Info().Str("addr", addr).Msg("[hass] listen") if err := http.ListenAndServe(addr, api.Handler); err != nil { log.Error().Err(err).Caller().Send() } }() } } } func importConfig(config string) error { // support load cameras from Hass config file filename := path.Join(config, ".storage/core.config_entries") b, err := os.ReadFile(filename) if err != nil { return err } var storage struct { Data struct { Entries []struct { Title string `json:"title"` Domain string `json:"domain"` Data json.RawMessage `json:"data"` Options json.RawMessage `json:"options"` } `json:"entries"` } `json:"data"` } if err = json.Unmarshal(b, &storage); err != nil { return err } for _, entrie := range storage.Data.Entries { switch entrie.Domain { case "generic": var options struct { StreamSource string `json:"stream_source"` } if err = json.Unmarshal(entrie.Options, &options); err != nil { continue } entities[entrie.Title] = options.StreamSource case "homekit_controller": if !bytes.Contains(entrie.Data, []byte("iOSPairingId")) { continue } var data struct { ClientID string `json:"iOSPairingId"` ClientPrivate string `json:"iOSDeviceLTSK"` ClientPublic string `json:"iOSDeviceLTPK"` DeviceID string `json:"AccessoryPairingID"` DevicePublic string `json:"AccessoryLTPK"` DeviceHost string `json:"AccessoryIP"` DevicePort uint16 `json:"AccessoryPort"` } if err = json.Unmarshal(entrie.Data, &data); err != nil { continue } entities[entrie.Title] = fmt.Sprintf( "homekit://%s:%d?client_id=%s&client_private=%s%s&device_id=%s&device_public=%s", data.DeviceHost, data.DevicePort, data.ClientID, data.ClientPrivate, data.ClientPublic, data.DeviceID, data.DevicePublic, ) case "roborock": _ = json.Unmarshal(entrie.Data, &roborock.Auth) case "onvif": var data struct { Host string `json:"host" json:"host"` Port uint16 `json:"port" json:"port"` Username string `json:"username" json:"username"` Password string `json:"password" json:"password"` } if err = json.Unmarshal(entrie.Data, &data); err != nil { continue } if data.Username != "" && data.Password != "" { entities[entrie.Title] = fmt.Sprintf( "onvif://%s:%s@%s:%d", data.Username, data.Password, data.Host, data.Port, ) } else { entities[entrie.Title] = fmt.Sprintf("onvif://%s:%d", data.Host, data.Port) } default: continue } log.Debug().Str("url", "hass:"+entrie.Title).Msg("[hass] load config") //streams.Get("hass:" + entrie.Title) } return nil } func importWebRTC(token string) error { hassAPI, err := hass.NewAPI("ws://supervisor/core/websocket", token) if err != nil { return err } webrtcEntities, err := hassAPI.GetWebRTCEntities() if err != nil { return err } if len(webrtcEntities) == 0 { log.Debug().Msg("[hass] webrtc cameras not found") } for name, entityID := range webrtcEntities { entities[name] = "hass://supervisor?entity_id=" + entityID log.Debug().Msgf("[hass] load webrtc name=%s entity_id=%s", name, entityID) } return nil } var entities = map[string]string{} var log zerolog.Logger var once sync.Once ================================================ FILE: internal/hls/README.md ================================================ # HLS [`new in v1.1.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.1.0) [HLS](https://en.wikipedia.org/wiki/HTTP_Live_Streaming) is the worst technology for real-time streaming. It can only be useful on devices that do not support more modern technology, like [WebRTC](../webrtc/README.md), [MP4](../mp4/README.md). The go2rtc implementation differs from the standards and may not work with all players. API examples: - HLS/TS stream: `http://192.168.1.123:1984/api/stream.m3u8?src=camera1` (H264) - HLS/fMP4 stream: `http://192.168.1.123:1984/api/stream.m3u8?src=camera1&mp4` (H264, H265, AAC) Read more about [codecs filters](../../README.md#codecs-filters). ## Useful links - https://walterebert.com/playground/video/hls/ ================================================ FILE: internal/hls/hls.go ================================================ package hls import ( "net/http" "sync" "time" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api/ws" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/mp4" "github.com/AlexxIT/go2rtc/pkg/mpegts" "github.com/rs/zerolog" ) func Init() { log = app.GetLogger("hls") api.HandleFunc("api/stream.m3u8", handlerStream) api.HandleFunc("api/hls/playlist.m3u8", handlerPlaylist) // HLS (TS) api.HandleFunc("api/hls/segment.ts", handlerSegmentTS) // HLS (fMP4) api.HandleFunc("api/hls/init.mp4", handlerInit) api.HandleFunc("api/hls/segment.m4s", handlerSegmentMP4) ws.HandleFunc("hls", handlerWSHLS) } var log zerolog.Logger const keepalive = 5 * time.Second // once I saw 404 on MP4 segment, so better to use mutex var sessions = map[string]*Session{} var sessionsMu sync.RWMutex func handlerStream(w http.ResponseWriter, r *http.Request) { // CORS important for Chromecast w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Content-Type", "application/vnd.apple.mpegurl") if r.Method == "OPTIONS" { w.Header().Set("Access-Control-Allow-Methods", "GET") return } src := r.URL.Query().Get("src") stream := streams.Get(src) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } var cons core.Consumer // use fMP4 with codecs filter and TS without medias := mp4.ParseQuery(r.URL.Query()) if medias != nil { c := mp4.NewConsumer(medias) c.FormatName = "hls/fmp4" c.WithRequest(r) cons = c } else { c := mpegts.NewConsumer() c.FormatName = "hls/mpegts" c.WithRequest(r) cons = c } if err := stream.AddConsumer(cons); err != nil { log.Error().Err(err).Caller().Send() return } session := NewSession(cons) session.alive = time.AfterFunc(keepalive, func() { sessionsMu.Lock() delete(sessions, session.id) sessionsMu.Unlock() stream.RemoveConsumer(cons) }) sessionsMu.Lock() sessions[session.id] = session sessionsMu.Unlock() go session.Run() if _, err := w.Write(session.Main()); err != nil { log.Error().Err(err).Caller().Send() } } func handlerPlaylist(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Content-Type", "application/vnd.apple.mpegurl") if r.Method == "OPTIONS" { w.Header().Set("Access-Control-Allow-Methods", "GET") return } sid := r.URL.Query().Get("id") sessionsMu.RLock() session := sessions[sid] sessionsMu.RUnlock() if session == nil { http.NotFound(w, r) return } if _, err := w.Write(session.Playlist()); err != nil { log.Error().Err(err).Caller().Send() } } func handlerSegmentTS(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Set("Content-Type", "video/mp2t") if r.Method == "OPTIONS" { w.Header().Set("Access-Control-Allow-Methods", "GET") return } sid := r.URL.Query().Get("id") sessionsMu.RLock() session := sessions[sid] sessionsMu.RUnlock() if session == nil { http.NotFound(w, r) return } session.alive.Reset(keepalive) data := session.Segment() if data == nil { log.Warn().Msgf("[hls] can't get segment %s", r.URL.RawQuery) http.NotFound(w, r) return } if _, err := w.Write(data); err != nil { log.Error().Err(err).Caller().Send() } } func handlerInit(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Add("Content-Type", "video/mp4") if r.Method == "OPTIONS" { w.Header().Set("Access-Control-Allow-Methods", "GET") return } sid := r.URL.Query().Get("id") sessionsMu.RLock() session := sessions[sid] sessionsMu.RUnlock() if session == nil { http.NotFound(w, r) return } data := session.Init() if data == nil { log.Warn().Msgf("[hls] can't get init %s", r.URL.RawQuery) http.NotFound(w, r) return } if _, err := w.Write(data); err != nil { log.Error().Err(err).Caller().Send() } } func handlerSegmentMP4(w http.ResponseWriter, r *http.Request) { w.Header().Set("Access-Control-Allow-Origin", "*") w.Header().Add("Content-Type", "video/iso.segment") if r.Method == "OPTIONS" { w.Header().Set("Access-Control-Allow-Methods", "GET") return } query := r.URL.Query() sid := query.Get("id") sessionsMu.RLock() session := sessions[sid] sessionsMu.RUnlock() if session == nil { http.NotFound(w, r) return } session.alive.Reset(keepalive) data := session.Segment() if data == nil { log.Warn().Msgf("[hls] can't get segment %s", r.URL.RawQuery) http.NotFound(w, r) return } if _, err := w.Write(data); err != nil { log.Error().Err(err).Caller().Send() } } ================================================ FILE: internal/hls/session.go ================================================ package hls import ( "fmt" "io" "strings" "sync" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/mp4" ) type Session struct { cons core.Consumer id string template string init []byte buffer []byte seq int alive *time.Timer mu sync.Mutex } func NewSession(cons core.Consumer) *Session { s := &Session{ id: core.RandString(8, 62), cons: cons, } // two segments important for Chromecast if _, ok := cons.(*mp4.Consumer); ok { s.template = `#EXTM3U #EXT-X-VERSION:6 #EXT-X-TARGETDURATION:1 #EXT-X-MEDIA-SEQUENCE:%d #EXT-X-MAP:URI="init.mp4?id=` + s.id + `" #EXTINF:0.500, segment.m4s?id=` + s.id + `&n=%d #EXTINF:0.500, segment.m4s?id=` + s.id + `&n=%d` } else { s.template = `#EXTM3U #EXT-X-VERSION:3 #EXT-X-TARGETDURATION:1 #EXT-X-MEDIA-SEQUENCE:%d #EXTINF:0.500, segment.ts?id=` + s.id + `&n=%d #EXTINF:0.500, segment.ts?id=` + s.id + `&n=%d` } return s } func (s *Session) Write(p []byte) (n int, err error) { s.mu.Lock() if s.init == nil { s.init = p } else { s.buffer = append(s.buffer, p...) } s.mu.Unlock() return len(p), nil } func (s *Session) Run() { _, _ = s.cons.(io.WriterTo).WriteTo(s) } func (s *Session) Main() []byte { type withCodecs interface { Codecs() []*core.Codec } codecs := mp4.MimeCodecs(s.cons.(withCodecs).Codecs()) codecs = strings.Replace(codecs, mp4.MimeFlac, "fLaC", 1) // bandwidth important for Safari, codecs useful for smooth playback return []byte(`#EXTM3U #EXT-X-STREAM-INF:BANDWIDTH=192000,CODECS="` + codecs + `" hls/playlist.m3u8?id=` + s.id) } func (s *Session) Playlist() []byte { return []byte(fmt.Sprintf(s.template, s.seq, s.seq, s.seq+1)) } func (s *Session) Init() (init []byte) { for i := 0; i < 60 && init == nil; i++ { if i > 0 { time.Sleep(50 * time.Millisecond) } s.mu.Lock() // return init only when have some buffer if len(s.buffer) > 0 { init = s.init } s.mu.Unlock() } return } func (s *Session) Segment() (segment []byte) { for i := 0; i < 60 && segment == nil; i++ { if i > 0 { time.Sleep(50 * time.Millisecond) } s.mu.Lock() if len(s.buffer) > 0 { segment = s.buffer if _, ok := s.cons.(*mp4.Consumer); ok { s.buffer = nil } else { // for TS important to start new segment with init s.buffer = s.init } s.seq++ } s.mu.Unlock() } return } ================================================ FILE: internal/hls/ws.go ================================================ package hls import ( "errors" "time" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api/ws" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/mp4" ) func handlerWSHLS(tr *ws.Transport, msg *ws.Message) error { stream, _ := streams.GetOrPatch(tr.Request.URL.Query()) if stream == nil { return errors.New(api.StreamNotFound) } codecs := msg.String() medias := mp4.ParseCodecs(codecs, true) cons := mp4.NewConsumer(medias) cons.FormatName = "hls/fmp4" cons.WithRequest(tr.Request) log.Trace().Msgf("[hls] new ws consumer codecs=%s", codecs) if err := stream.AddConsumer(cons); err != nil { log.Error().Err(err).Caller().Send() return err } session := NewSession(cons) session.alive = time.AfterFunc(keepalive, func() { sessionsMu.Lock() delete(sessions, session.id) sessionsMu.Unlock() stream.RemoveConsumer(cons) }) sessionsMu.Lock() sessions[session.id] = session sessionsMu.Unlock() go session.Run() main := session.Main() tr.Write(&ws.Message{Type: "hls", Value: string(main)}) return nil } ================================================ FILE: internal/homekit/README.md ================================================ # Apple HomeKit This module supports both client and server for the [Apple HomeKit](https://www.apple.com/home-app/accessories/) protocol. ## HomeKit Client **Important:** - You can use HomeKit Cameras **without Apple devices** (iPhone, iPad, etc.), it's just a yet another protocol - HomeKit device can be paired with only one ecosystem. So, if you have paired it to an iPhone (Apple Home), you can't pair it with Home Assistant or go2rtc. Or if you have paired it to go2rtc, you can't pair it with an iPhone - HomeKit device should be on the same network with working [mDNS](https://en.wikipedia.org/wiki/Multicast_DNS) between the device and go2rtc go2rtc supports importing paired HomeKit devices from [Home Assistant](../hass/README.md). So you can use HomeKit camera with Home Assistant and go2rtc simultaneously. If you are using Home Assistant, I recommend pairing devices with it; it will give you more options. You can pair device with go2rtc on the HomeKit page. If you can't see your devices, reload the page. Also, try rebooting your HomeKit device (power off). If you still can't see it, you have a problem with mDNS. If you see a device but it does not have a pairing button, it is paired to some ecosystem (Apple Home, Home Assistant, HomeBridge, etc.). You need to delete the device from that ecosystem, and it will be available for pairing. If you cannot unpair the device, you will have to reset it. **Important:** - HomeKit audio uses very non-standard **AAC-ELD** codec with very non-standard params and specification violations - Audio can't be played in `VLC` and probably any other player - Audio should be transcoded for use with MSE, WebRTC, etc. ### Client Configuration Recommended settings for using HomeKit Camera with WebRTC, MSE, MP4, RTSP: ```yaml streams: aqara_g3: - hass:Camera-Hub-G3-AB12 - ffmpeg:aqara_g3#audio=aac#audio=opus ``` RTSP link with "normal" audio for any player: `rtsp://192.168.1.123:8554/aqara_g3?video&audio=aac` **This source is in active development!** Tested only with [Aqara Camera Hub G3](https://www.aqara.com/eu/product/camera-hub-g3) (both EU and CN versions). ## HomeKit Server [`new in v1.7.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.7.0) HomeKit module can work in two modes: - export any H264 camera to Apple HomeKit - transparent proxy any Apple HomeKit camera (Aqara, Eve, Eufy, etc.) back to Apple HomeKit, so you will have all camera features in Apple Home and also will have RTSP/WebRTC/MP4/etc. from your HomeKit camera **Important** - HomeKit cameras support only H264 video and OPUS audio ### Server Configuration **Minimal config** ```yaml streams: dahua1: rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0 homekit: dahua1: # same stream ID from streams list, default PIN - 19550224 ``` **Full config** ```yaml streams: dahua1: - rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0 - ffmpeg:dahua1#video=h264#hardware # if your camera doesn't support H264, important for HomeKit - ffmpeg:dahua1#audio=opus # only OPUS audio supported by HomeKit homekit: dahua1: # same stream ID from streams list pin: 12345678 # custom PIN, default: 19550224 name: Dahua camera # custom camera name, default: generated from stream ID device_id: dahua1 # custom ID, default: generated from stream ID device_private: dahua1 # custom key, default: generated from stream ID ``` **Proxy HomeKit camera** - Video stream from HomeKit camera to Apple device (iPhone, Apple TV) will be transmitted directly - Video stream from HomeKit camera to RTSP/WebRTC/MP4/etc. will be transmitted via go2rtc ```yaml streams: aqara1: - homekit://... - ffmpeg:aqara1#audio=aac#audio=opus # optional audio transcoding homekit: aqara1: # same stream ID from streams list ``` ================================================ FILE: internal/homekit/api.go ================================================ package homekit import ( "errors" "fmt" "io" "net/http" "net/url" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/hap" "github.com/AlexxIT/go2rtc/pkg/mdns" ) func apiDiscovery(w http.ResponseWriter, r *http.Request) { sources, err := discovery() if err != nil { api.Error(w, err) return } urls := findHomeKitURLs() for id, u := range urls { deviceID := u.Query().Get("device_id") for _, source := range sources { if strings.Contains(source.URL, deviceID) { source.Location = id break } } } for _, source := range sources { if source.Location == "" { source.Location = " " } } api.ResponseSources(w, sources) } func apiHomekit(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } switch r.Method { case "GET": if id := r.Form.Get("id"); id != "" { if srv := servers[id]; srv != nil { api.ResponsePrettyJSON(w, srv) } else { http.Error(w, "server not found", http.StatusNotFound) } } else { api.ResponsePrettyJSON(w, servers) } case "POST": id := r.Form.Get("id") rawURL := r.Form.Get("src") + "&pin=" + r.Form.Get("pin") if err := apiPair(id, rawURL); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } case "DELETE": id := r.Form.Get("id") if err := apiUnpair(id); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } } func apiHomekitAccessories(w http.ResponseWriter, r *http.Request) { id := r.URL.Query().Get("id") stream := streams.Get(id) if stream == nil { http.Error(w, "", http.StatusNotFound) return } rawURL := findHomeKitURL(stream.Sources()) if rawURL == "" { http.Error(w, "", http.StatusBadRequest) return } client, err := hap.Dial(rawURL) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } defer client.Close() res, err := client.Get(hap.PathAccessories) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Set("Content-Type", api.MimeJSON) _, _ = io.Copy(w, res.Body) } func discovery() ([]*api.Source, error) { var sources []*api.Source // 1. Get streams from Discovery err := mdns.Discovery(mdns.ServiceHAP, func(entry *mdns.ServiceEntry) bool { log.Trace().Msgf("[homekit] mdns=%s", entry) category := entry.Info[hap.TXTCategory] if entry.Complete() && (category == hap.CategoryCamera || category == hap.CategoryDoorbell) { source := &api.Source{ Name: entry.Name, Info: entry.Info[hap.TXTModel], URL: fmt.Sprintf( "homekit://%s:%d?device_id=%s&feature=%s&status=%s", entry.IP, entry.Port, entry.Info[hap.TXTDeviceID], entry.Info[hap.TXTFeatureFlags], entry.Info[hap.TXTStatusFlags], ), } sources = append(sources, source) } return false }) if err != nil { return nil, err } return sources, nil } func apiPair(id, url string) error { conn, err := hap.Pair(url) if err != nil { return err } streams.New(id, conn.URL()) return app.PatchConfig([]string{"streams", id}, conn.URL()) } func apiUnpair(id string) error { stream := streams.Get(id) if stream == nil { return errors.New(api.StreamNotFound) } rawURL := findHomeKitURL(stream.Sources()) if rawURL == "" { return errors.New("not homekit source") } if err := hap.Unpair(rawURL); err != nil { return err } streams.Delete(id) return app.PatchConfig([]string{"streams", id}, nil) } func findHomeKitURLs() map[string]*url.URL { urls := map[string]*url.URL{} for name, sources := range streams.GetAllSources() { if rawURL := findHomeKitURL(sources); rawURL != "" { if u, err := url.Parse(rawURL); err == nil { urls[name] = u } } } return urls } ================================================ FILE: internal/homekit/homekit.go ================================================ package homekit import ( "errors" "net/http" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/srtp" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/hap" "github.com/AlexxIT/go2rtc/pkg/hap/camera" "github.com/AlexxIT/go2rtc/pkg/homekit" "github.com/AlexxIT/go2rtc/pkg/mdns" "github.com/rs/zerolog" ) func Init() { var cfg struct { Mod map[string]struct { Pin string `yaml:"pin"` Name string `yaml:"name"` DeviceID string `yaml:"device_id"` DevicePrivate string `yaml:"device_private"` CategoryID string `yaml:"category_id"` Pairings []string `yaml:"pairings"` } `yaml:"homekit"` } app.LoadConfig(&cfg) log = app.GetLogger("homekit") streams.HandleFunc("homekit", streamHandler) api.HandleFunc("api/homekit", apiHomekit) api.HandleFunc("api/homekit/accessories", apiHomekitAccessories) api.HandleFunc("api/discovery/homekit", apiDiscovery) if cfg.Mod == nil { return } hosts = map[string]*server{} servers = map[string]*server{} var entries []*mdns.ServiceEntry for id, conf := range cfg.Mod { stream := streams.Get(id) if stream == nil { log.Warn().Msgf("[homekit] missing stream: %s", id) continue } if conf.Pin == "" { conf.Pin = "19550224" // default PIN } pin, err := hap.SanitizePin(conf.Pin) if err != nil { log.Error().Err(err).Caller().Send() continue } deviceID := calcDeviceID(conf.DeviceID, id) // random MAC-address name := calcName(conf.Name, deviceID) setupID := calcSetupID(id) srv := &server{ stream: id, pairings: conf.Pairings, setupID: setupID, } srv.hap = &hap.Server{ Pin: pin, DeviceID: deviceID, DevicePrivate: calcDevicePrivate(conf.DevicePrivate, id), GetClientPublic: srv.GetPair, } srv.mdns = &mdns.ServiceEntry{ Name: name, Port: uint16(api.Port), Info: map[string]string{ hap.TXTConfigNumber: "1", hap.TXTFeatureFlags: "0", hap.TXTDeviceID: deviceID, hap.TXTModel: app.UserAgent, hap.TXTProtoVersion: "1.1", hap.TXTStateNumber: "1", hap.TXTStatusFlags: hap.StatusNotPaired, hap.TXTCategory: calcCategoryID(conf.CategoryID), hap.TXTSetupHash: hap.SetupHash(setupID, deviceID), }, } entries = append(entries, srv.mdns) srv.UpdateStatus() if url := findHomeKitURL(stream.Sources()); url != "" { // 1. Act as transparent proxy for HomeKit camera srv.proxyURL = url } else { // 2. Act as basic HomeKit camera srv.accessory = camera.NewAccessory("AlexxIT", "go2rtc", name, "-", app.Version) } host := srv.mdns.Host(mdns.ServiceHAP) hosts[host] = srv servers[id] = srv log.Trace().Msgf("[homekit] new server: %s", srv.mdns) } api.HandleFunc(hap.PathPairSetup, hapHandler) api.HandleFunc(hap.PathPairVerify, hapHandler) go func() { if err := mdns.Serve(mdns.ServiceHAP, entries); err != nil { log.Error().Err(err).Caller().Send() } }() } var log zerolog.Logger var hosts map[string]*server var servers map[string]*server func streamHandler(rawURL string) (core.Producer, error) { if srtp.Server == nil { return nil, errors.New("homekit: can't work without SRTP server") } rawURL, rawQuery, _ := strings.Cut(rawURL, "#") client, err := homekit.Dial(rawURL, srtp.Server) if client != nil && rawQuery != "" { query := streams.ParseQuery(rawQuery) client.MaxWidth = core.Atoi(query.Get("maxwidth")) client.MaxHeight = core.Atoi(query.Get("maxheight")) client.Bitrate = parseBitrate(query.Get("bitrate")) } return client, err } func resolve(host string) *server { if len(hosts) == 1 { for _, srv := range hosts { return srv } } if srv, ok := hosts[host]; ok { return srv } return nil } func hapHandler(w http.ResponseWriter, r *http.Request) { // Can support multiple HomeKit cameras on single port ONLY for Apple devices. // Doesn't support Home Assistant and any other open source projects // because they don't send the host header in requests. srv := resolve(r.Host) if srv == nil { log.Error().Msg("[homekit] unknown host: " + r.Host) return } srv.Handle(w, r) } func findHomeKitURL(sources []string) string { if len(sources) == 0 { return "" } url := sources[0] if strings.HasPrefix(url, "homekit") { return url } if strings.HasPrefix(url, "hass") { location, _ := streams.Location(url) if strings.HasPrefix(location, "homekit") { return location } } return "" } func parseBitrate(s string) int { n := len(s) if n == 0 { return 0 } var k int switch n--; s[n] { case 'K': k = 1024 s = s[:n] case 'M': k = 1024 * 1024 s = s[:n] default: k = 1 } return k * core.Atoi(s) } ================================================ FILE: internal/homekit/server.go ================================================ package homekit import ( "crypto/ed25519" "crypto/sha512" "encoding/hex" "encoding/json" "errors" "fmt" "io" "net" "net/http" "net/url" "slices" "strings" "sync" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/ffmpeg" srtp2 "github.com/AlexxIT/go2rtc/internal/srtp" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/hap" "github.com/AlexxIT/go2rtc/pkg/hap/camera" "github.com/AlexxIT/go2rtc/pkg/hap/hds" "github.com/AlexxIT/go2rtc/pkg/hap/tlv8" "github.com/AlexxIT/go2rtc/pkg/homekit" "github.com/AlexxIT/go2rtc/pkg/magic" "github.com/AlexxIT/go2rtc/pkg/mdns" ) type server struct { hap *hap.Server // server for HAP connection and encryption mdns *mdns.ServiceEntry pairings []string // pairings list conns []any mu sync.Mutex accessory *hap.Accessory // HAP accessory consumer *homekit.Consumer proxyURL string setupID string stream string // stream name from YAML } func (s *server) MarshalJSON() ([]byte, error) { v := struct { Name string `json:"name"` DeviceID string `json:"device_id"` Paired int `json:"paired,omitempty"` CategoryID string `json:"category_id,omitempty"` SetupCode string `json:"setup_code,omitempty"` SetupID string `json:"setup_id,omitempty"` Conns []any `json:"connections,omitempty"` }{ Name: s.mdns.Name, DeviceID: s.mdns.Info[hap.TXTDeviceID], CategoryID: s.mdns.Info[hap.TXTCategory], Paired: len(s.pairings), Conns: s.conns, } if v.Paired == 0 { v.SetupCode = s.hap.Pin v.SetupID = s.setupID } return json.Marshal(v) } func (s *server) Handle(w http.ResponseWriter, r *http.Request) { conn, rw, err := w.(http.Hijacker).Hijack() if err != nil { return } defer conn.Close() // Fix reading from Body after Hijack. r.Body = io.NopCloser(rw) switch r.RequestURI { case hap.PathPairSetup: id, key, err := s.hap.PairSetup(r, rw) if err != nil { log.Error().Err(err).Caller().Send() return } s.AddPair(id, key, hap.PermissionAdmin) case hap.PathPairVerify: id, key, err := s.hap.PairVerify(r, rw) if err != nil { log.Debug().Err(err).Caller().Send() return } log.Debug().Str("stream", s.stream).Str("client_id", id).Msgf("[homekit] %s: new conn", conn.RemoteAddr()) controller, err := hap.NewConn(conn, rw, key, false) if err != nil { log.Error().Err(err).Caller().Send() return } s.AddConn(controller) defer s.DelConn(controller) var handler homekit.HandlerFunc switch { case s.accessory != nil: handler = homekit.ServerHandler(s) case s.proxyURL != "": client, err := hap.Dial(s.proxyURL) if err != nil { log.Error().Err(err).Caller().Send() return } handler = homekit.ProxyHandler(s, client.Conn) } // If your iPhone goes to sleep, it will be an EOF error. if err = handler(controller); err != nil && !errors.Is(err, io.EOF) { log.Error().Err(err).Caller().Send() return } } } type logger struct { v any } func (l logger) String() string { switch v := l.v.(type) { case *hap.Conn: return "hap " + v.RemoteAddr().String() case *hds.Conn: return "hds " + v.RemoteAddr().String() case *homekit.Consumer: return "rtp " + v.RemoteAddr } return "unknown" } func (s *server) AddConn(v any) { log.Trace().Str("stream", s.stream).Msgf("[homekit] add conn %s", logger{v}) s.mu.Lock() s.conns = append(s.conns, v) s.mu.Unlock() } func (s *server) DelConn(v any) { log.Trace().Str("stream", s.stream).Msgf("[homekit] del conn %s", logger{v}) s.mu.Lock() if i := slices.Index(s.conns, v); i >= 0 { s.conns = slices.Delete(s.conns, i, i+1) } s.mu.Unlock() } func (s *server) UpdateStatus() { // true status is important, or device may be offline in Apple Home if len(s.pairings) == 0 { s.mdns.Info[hap.TXTStatusFlags] = hap.StatusNotPaired } else { s.mdns.Info[hap.TXTStatusFlags] = hap.StatusPaired } } func (s *server) pairIndex(id string) int { id = "client_id=" + id for i, pairing := range s.pairings { if strings.HasPrefix(pairing, id) { return i } } return -1 } func (s *server) GetPair(id string) []byte { s.mu.Lock() defer s.mu.Unlock() if i := s.pairIndex(id); i >= 0 { query, _ := url.ParseQuery(s.pairings[i]) b, _ := hex.DecodeString(query.Get("client_public")) return b } return nil } func (s *server) AddPair(id string, public []byte, permissions byte) { log.Debug().Str("stream", s.stream).Msgf("[homekit] add pair id=%s public=%x perm=%d", id, public, permissions) s.mu.Lock() if s.pairIndex(id) < 0 { s.pairings = append(s.pairings, fmt.Sprintf( "client_id=%s&client_public=%x&permissions=%d", id, public, permissions, )) s.UpdateStatus() s.PatchConfig() } s.mu.Unlock() } func (s *server) DelPair(id string) { log.Debug().Str("stream", s.stream).Msgf("[homekit] del pair id=%s", id) s.mu.Lock() if i := s.pairIndex(id); i >= 0 { s.pairings = append(s.pairings[:i], s.pairings[i+1:]...) s.UpdateStatus() s.PatchConfig() } s.mu.Unlock() } func (s *server) PatchConfig() { if err := app.PatchConfig([]string{"homekit", s.stream, "pairings"}, s.pairings); err != nil { log.Error().Err(err).Msgf( "[homekit] can't save %s pairings=%v", s.stream, s.pairings, ) } } func (s *server) GetAccessories(_ net.Conn) []*hap.Accessory { return []*hap.Accessory{s.accessory} } func (s *server) GetCharacteristic(conn net.Conn, aid uint8, iid uint64) any { log.Trace().Str("stream", s.stream).Msgf("[homekit] get char aid=%d iid=0x%x", aid, iid) char := s.accessory.GetCharacterByID(iid) if char == nil { log.Warn().Msgf("[homekit] get unknown characteristic: %d", iid) return nil } switch char.Type { case camera.TypeSetupEndpoints: consumer := s.consumer if consumer == nil { return nil } answer := consumer.GetAnswer() v, err := tlv8.MarshalBase64(answer) if err != nil { return nil } return v } return char.Value } func (s *server) SetCharacteristic(conn net.Conn, aid uint8, iid uint64, value any) { log.Trace().Str("stream", s.stream).Msgf("[homekit] set char aid=%d iid=0x%x value=%v", aid, iid, value) char := s.accessory.GetCharacterByID(iid) if char == nil { log.Warn().Msgf("[homekit] set unknown characteristic: %d", iid) return } switch char.Type { case camera.TypeSetupEndpoints: var offer camera.SetupEndpointsRequest if err := tlv8.UnmarshalBase64(value, &offer); err != nil { return } consumer := homekit.NewConsumer(conn, srtp2.Server) consumer.SetOffer(&offer) s.consumer = consumer case camera.TypeSelectedStreamConfiguration: var conf camera.SelectedStreamConfiguration if err := tlv8.UnmarshalBase64(value, &conf); err != nil { return } log.Trace().Str("stream", s.stream).Msgf("[homekit] stream id=%x cmd=%d", conf.Control.SessionID, conf.Control.Command) switch conf.Control.Command { case camera.SessionCommandEnd: for _, consumer := range s.conns { if consumer, ok := consumer.(*homekit.Consumer); ok { if consumer.SessionID() == conf.Control.SessionID { _ = consumer.Stop() return } } } case camera.SessionCommandStart: consumer := s.consumer if consumer == nil { return } if !consumer.SetConfig(&conf) { log.Warn().Msgf("[homekit] wrong config") return } s.AddConn(consumer) stream := streams.Get(s.stream) if err := stream.AddConsumer(consumer); err != nil { return } go func() { _, _ = consumer.WriteTo(nil) stream.RemoveConsumer(consumer) s.DelConn(consumer) }() } } } func (s *server) GetImage(conn net.Conn, width, height int) []byte { log.Trace().Str("stream", s.stream).Msgf("[homekit] get image width=%d height=%d", width, height) stream := streams.Get(s.stream) cons := magic.NewKeyframe() if err := stream.AddConsumer(cons); err != nil { return nil } once := &core.OnceBuffer{} // init and first frame _, _ = cons.WriteTo(once) b := once.Buffer() stream.RemoveConsumer(cons) switch cons.CodecName() { case core.CodecH264, core.CodecH265: var err error if b, err = ffmpeg.JPEGWithScale(b, width, height); err != nil { return nil } } return b } func calcName(name, seed string) string { if name != "" { return name } b := sha512.Sum512([]byte(seed)) return fmt.Sprintf("go2rtc-%02X%02X", b[0], b[2]) } func calcDeviceID(deviceID, seed string) string { if deviceID != "" { if len(deviceID) >= 17 { // 1. Returd device_id as is (ex. AA:BB:CC:DD:EE:FF) return deviceID } // 2. Use device_id as seed if not zero seed = deviceID } b := sha512.Sum512([]byte(seed)) return fmt.Sprintf("%02X:%02X:%02X:%02X:%02X:%02X", b[32], b[34], b[36], b[38], b[40], b[42]) } func calcDevicePrivate(private, seed string) []byte { if private != "" { // 1. Decode private from HEX string if b, _ := hex.DecodeString(private); len(b) == ed25519.PrivateKeySize { // 2. Return if OK return b } // 3. Use private as seed if not zero seed = private } b := sha512.Sum512([]byte(seed)) return ed25519.NewKeyFromSeed(b[:ed25519.SeedSize]) } func calcSetupID(seed string) string { b := sha512.Sum512([]byte(seed)) return fmt.Sprintf("%02X%02X", b[44], b[46]) } func calcCategoryID(categoryID string) string { switch categoryID { case "bridge": return hap.CategoryBridge case "doorbell": return hap.CategoryDoorbell } if core.Atoi(categoryID) > 0 { return categoryID } return hap.CategoryCamera } ================================================ FILE: internal/http/README.md ================================================ # HTTP This source supports receiving a stream via an HTTP link. It can determine the source format from the`Content-Type` HTTP header: - **HTTP-JPEG** (`image/jpeg`) - camera snapshot link, can be converted by go2rtc to MJPEG stream - **HTTP-MJPEG** (`multipart/x-mixed-replace`) - A continuous sequence of JPEG frames (with HTTP headers). - **HLS** (`application/vnd.apple.mpegurl`) - A popular [HTTP Live Streaming](https://en.wikipedia.org/wiki/HTTP_Live_Streaming) (HLS) format, which is not designed for real-time media transmission. > [!WARNING] > The HLS format is not designed for real time and is supported quite poorly. It is recommended to use it via ffmpeg source with buffering enabled (disabled by default). ## TCP Source also supports HTTP and TCP streams with autodetection for different formats: - `adts` - Audio stream in [AAC](https://en.wikipedia.org/wiki/Advanced_Audio_Coding) codec with Audio Data Transport Stream (ADTS) headers. - `flv` - The legacy but still used [Flash Video](https://en.wikipedia.org/wiki/Flash_Video) format. - `h264` - AVC/H.264 bitstream. - `hevc` - HEVC/H.265 bitstream. - `mjpeg` - A continuous sequence of JPEG frames (without HTTP headers). - `mpegts` - The legacy [MPEG transport stream](https://en.wikipedia.org/wiki/MPEG_transport_stream) format. - `wav` - Audio stream in [WAV](https://en.wikipedia.org/wiki/WAV) format. - `yuv4mpegpipe` - Raw YUV frame stream with YUV4MPEG header. ## Configuration ```yaml streams: # [HTTP-FLV] stream in video/x-flv format http_flv: http://192.168.1.123:20880/api/camera/stream/780900131155/657617 # [JPEG] snapshots from Dahua camera, will be converted to MJPEG stream dahua_snap: http://admin:password@192.168.1.123/cgi-bin/snapshot.cgi?channel=1 # [MJPEG] stream will be proxied without modification http_mjpeg: https://mjpeg.sanford.io/count.mjpeg # [MJPEG or H.264/H.265 bitstream or MPEG-TS] tcp_magic: tcp://192.168.1.123:12345 # Add custom header custom_header: "https://mjpeg.sanford.io/count.mjpeg#header=Authorization: Bearer XXX" ``` **PS.** Dahua camera has a bug: if you select MJPEG codec for RTSP second stream, snapshot won't work. ================================================ FILE: internal/http/http.go ================================================ package http import ( "errors" "net" "net/http" "net/url" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/hls" "github.com/AlexxIT/go2rtc/pkg/image" "github.com/AlexxIT/go2rtc/pkg/magic" "github.com/AlexxIT/go2rtc/pkg/mpjpeg" "github.com/AlexxIT/go2rtc/pkg/pcm" "github.com/AlexxIT/go2rtc/pkg/tcp" ) func Init() { streams.HandleFunc("http", handleHTTP) streams.HandleFunc("https", handleHTTP) streams.HandleFunc("httpx", handleHTTP) streams.HandleFunc("tcp", handleTCP) api.HandleFunc("api/stream", apiStream) } func handleHTTP(rawURL string) (core.Producer, error) { rawURL, rawQuery, _ := strings.Cut(rawURL, "#") // first we get the Content-Type to define supported producer req, err := http.NewRequest("GET", rawURL, nil) if err != nil { return nil, err } if rawQuery != "" { query := streams.ParseQuery(rawQuery) for _, header := range query["header"] { key, value, _ := strings.Cut(header, ":") req.Header.Add(key, strings.TrimSpace(value)) } } prod, err := do(req) if err != nil { return nil, err } if info, ok := prod.(core.Info); ok { info.SetProtocol("http") info.SetRemoteAddr(req.URL.Host) // TODO: rewrite to net.Conn info.SetURL(rawURL) } return prod, nil } func do(req *http.Request) (core.Producer, error) { res, err := tcp.Do(req) if err != nil { return nil, err } if res.StatusCode != http.StatusOK { return nil, errors.New(res.Status) } // 1. Guess format from content type ct := res.Header.Get("Content-Type") if i := strings.IndexByte(ct, ';'); i > 0 { ct = ct[:i] } var ext string if i := strings.LastIndexByte(req.URL.Path, '.'); i > 0 { ext = req.URL.Path[i+1:] } switch { case ct == "application/vnd.apple.mpegurl" || ext == "m3u8": return hls.OpenURL(req.URL, res.Body) case ct == "image/jpeg": return image.Open(res) case ct == "multipart/x-mixed-replace": return mpjpeg.Open(res.Body) //https://www.iana.org/assignments/media-types/audio/basic case ct == "audio/basic": return pcm.Open(res.Body) } return magic.Open(res.Body) } func handleTCP(rawURL string) (core.Producer, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } conn, err := net.DialTimeout("tcp", u.Host, core.ConnDialTimeout) if err != nil { return nil, err } return magic.Open(conn) } func apiStream(w http.ResponseWriter, r *http.Request) { dst := r.URL.Query().Get("dst") stream := streams.Get(dst) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } client, err := magic.Open(r.Body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } stream.AddProducer(client) defer stream.RemoveProducer(client) if err = client.Start(); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } } ================================================ FILE: internal/isapi/README.md ================================================ # Hikvision ISAPI [`new in v1.3.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.3.0) This source type supports only backchannel audio for the [Hikvision ISAPI](https://tpp.hikvision.com/download/ISAPI_OTAP) protocol. So it should be used as a second source in addition to the RTSP protocol. ## Configuration ```yaml streams: hikvision1: - rtsp://admin:password@192.168.1.123:554/Streaming/Channels/101 - isapi://admin:password@192.168.1.123:80/ ``` ================================================ FILE: internal/isapi/init.go ================================================ package isapi import ( "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/isapi" ) func Init() { streams.HandleFunc("isapi", func(source string) (core.Producer, error) { return isapi.Dial(source) }) } ================================================ FILE: internal/ivideon/README.md ================================================ # Ivideon Support public cameras from the service [Ivideon](https://tv.ivideon.com/). ## Configuration ```yaml streams: quailcam: ivideon:100-tu5dkUPct39cTp9oNEN2B6/0 ``` ================================================ FILE: internal/ivideon/ivideon.go ================================================ package ivideon import ( "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/ivideon" ) func Init() { streams.HandleFunc("ivideon", ivideon.Dial) } ================================================ FILE: internal/kasa/README.md ================================================ # TP-Link Kasa [`new in v1.7.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.7.0) [TP-Link Kasa](https://www.kasasmart.com/) non-standard protocol [more info](https://medium.com/@hu3vjeen/reverse-engineering-tp-link-kc100-bac4641bf1cd). - `username` - urlsafe email, `alex@gmail.com` -> `alex%40gmail.com` - `password` - base64password, `secret1` -> `c2VjcmV0MQ==` ```yaml streams: kc401: kasa://username:password@192.168.1.123:19443/https/stream/mixed ``` Tested: KD110, KC200, KC401, KC420WS, EC71. ================================================ FILE: internal/kasa/kasa.go ================================================ package kasa import ( "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/kasa" ) func Init() { streams.HandleFunc("kasa", func(source string) (core.Producer, error) { return kasa.Dial(source) }) } ================================================ FILE: internal/mjpeg/README.md ================================================ # Motion JPEG - This module can provide and receive streams in MJPEG format. - This module is also responsible for receiving snapshots in JPEG format. - This module also supports streaming to the server console (terminal) in the **animated ASCII art** format. ## MJPEG Client **Important.** For a stream in MJPEG format, your source MUST contain the MJPEG codec. If your stream has the MJPEG codec, you can receive an **MJPEG stream** or **JPEG snapshots** via the API. You can receive an MJPEG stream in several ways: - some cameras support MJPEG codec inside [RTSP stream](../rtsp/README.md) (ex. second stream for Dahua cameras) - some cameras have an HTTP link with [MJPEG stream](../http/README.md) - some cameras have an HTTP link with snapshots - go2rtc can convert them to [MJPEG stream](../http/README.md) - you can convert an H264/H265 stream from your camera via [FFmpeg integration](../ffmpeg/README.md) With this example, your stream will have both H264 and MJPEG codecs: ```yaml streams: camera1: - rtsp://rtsp:12345678@192.168.1.123/av_stream/ch0 - ffmpeg:camera1#video=mjpeg ``` ## MJPEG Server ### mpjpeg Output a stream in [MJPEG](https://en.wikipedia.org/wiki/Motion_JPEG) format. In [FFmpeg](https://ffmpeg.org/), this format is called `mpjpeg` because it contains HTTP headers. ``` ffplay http://192.168.1.123:1984/api/stream.mjpeg?src=camera1 ``` ### jpeg Receiving a JPEG snapshot. ``` curl http://192.168.1.123:1984/api/frame.jpeg?src=camera1 ``` - You can use `width`/`w` and/or `height`/`h` parameters. - You can use `rotate` param with `90`, `180`, `270` or `-90` values. - You can use `hardware`/`hw` param [read more](https://github.com/AlexxIT/go2rtc/wiki/Hardware-acceleration). - You can use `cache` param (`1m`, `10s`, etc.) to get a cached snapshot. - The snapshot is cached only when requested with the `cache` parameter. - A cached snapshot will be used if its time is not older than the time specified in the `cache` parameter. - The `cache` parameter does not check the image dimensions from the cache and those specified in the query. ### ascii Stream as ASCII to Terminal. This format is just for fun. You can boast to your friends that you can stream cameras even to the server console without a GUI. [![](https://img.youtube.com/vi/sHj_3h_sX7M/mqdefault.jpg)](https://www.youtube.com/watch?v=sHj_3h_sX7M) > The demo video features a combination of several settings for this format with added audio. Of course, the format doesn't support audio out of the box. **Tips** - this feature works only with MJPEG codec (use transcoding) - choose a low frame rate (FPS) - choose the width and height to fit in your terminal - different terminals support different numbers of colors (8, 256, rgb) - URL-encode the `text` parameter - you can stream any camera or file from disk **go2rtc.yaml** - transcoding to MJPEG, terminal size - 210x59 (16/9), fps - 10 ```yaml streams: gamazda: ffmpeg:gamazda.mp4#video=mjpeg#hardware#width=210#height=59#raw=-r 10 ``` **API params** - `color` - foreground color, values: empty, `8`, `256`, `rgb`, [SGR](https://en.wikipedia.org/wiki/ANSI_escape_code) - example: `30` (black), `37` (white), `38;5;226` (yellow) - `back` - background color, values: empty, `8`, `256`, `rgb`, [SGR](https://en.wikipedia.org/wiki/ANSI_escape_code) - example: `40` (black), `47` (white), `48;5;226` (yellow) - `text` - character set, values: empty, one character, `block`, list of chars (in order of brightness) - example: `%20` (space), `block` (keyword for block elements), `ox` (two chars) **Examples** ```bash % curl "http://192.168.1.123:1984/api/stream.ascii?src=gamazda" % curl "http://192.168.1.123:1984/api/stream.ascii?src=gamazda&color=256" % curl "http://192.168.1.123:1984/api/stream.ascii?src=gamazda&back=256&text=%20" % curl "http://192.168.1.123:1984/api/stream.ascii?src=gamazda&back=8&text=%20%20" % curl "http://192.168.1.123:1984/api/stream.ascii?src=gamazda&text=helloworld" ``` ### yuv4mpegpipe Raw [YUV](https://en.wikipedia.org/wiki/Y%E2%80%B2UV) frame stream with [YUV4MPEG](https://manned.org/yuv4mpeg) header. ``` ffplay http://192.168.1.123:1984/api/stream.y4m?src=camera1 ``` ## Streaming ingest ```shell ffmpeg -re -i BigBuckBunny.mp4 -c mjpeg -f mpjpeg http://localhost:1984/api/stream.mjpeg?dst=camera1 ``` ================================================ FILE: internal/mjpeg/mjpeg.go ================================================ package mjpeg import ( "errors" "io" "net/http" "strconv" "strings" "sync" "time" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api/ws" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/ffmpeg" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/ascii" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/magic" "github.com/AlexxIT/go2rtc/pkg/mjpeg" "github.com/AlexxIT/go2rtc/pkg/mpjpeg" "github.com/AlexxIT/go2rtc/pkg/y4m" "github.com/rs/zerolog" ) func Init() { api.HandleFunc("api/frame.jpeg", handlerKeyframe) api.HandleFunc("api/stream.mjpeg", handlerStream) api.HandleFunc("api/stream.ascii", handlerStream) api.HandleFunc("api/stream.y4m", apiStreamY4M) ws.HandleFunc("mjpeg", handlerWS) log = app.GetLogger("mjpeg") } var log zerolog.Logger func handlerKeyframe(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() stream, _ := streams.GetOrPatch(query) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } var b []byte if s := query.Get("cache"); s != "" { if timeout, err := time.ParseDuration(s); err == nil { src := query.Get("src") cacheMu.Lock() entry, found := cache[src] cacheMu.Unlock() if found && time.Since(entry.timestamp) < timeout { writeJPEGResponse(w, entry.payload) return } defer func() { if b == nil { return } entry = cacheEntry{payload: b, timestamp: time.Now()} cacheMu.Lock() if cache == nil { cache = map[string]cacheEntry{src: entry} } else { cache[src] = entry } cacheMu.Unlock() }() } } cons := magic.NewKeyframe() cons.WithRequest(r) if err := stream.AddConsumer(cons); err != nil { log.Error().Err(err).Caller().Send() return } once := &core.OnceBuffer{} // init and first frame _, _ = cons.WriteTo(once) b = once.Buffer() stream.RemoveConsumer(cons) switch cons.CodecName() { case core.CodecH264, core.CodecH265: ts := time.Now() var err error if b, err = ffmpeg.JPEGWithQuery(b, query); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } log.Debug().Msgf("[mjpeg] transcoding time=%s", time.Since(ts)) case core.CodecJPEG: b = mjpeg.FixJPEG(b) } writeJPEGResponse(w, b) } var cache map[string]cacheEntry var cacheMu sync.Mutex // cacheEntry represents a cached keyframe with its timestamp type cacheEntry struct { payload []byte timestamp time.Time } func writeJPEGResponse(w http.ResponseWriter, b []byte) { h := w.Header() h.Set("Content-Type", "image/jpeg") h.Set("Content-Length", strconv.Itoa(len(b))) h.Set("Cache-Control", "no-cache") h.Set("Connection", "close") h.Set("Pragma", "no-cache") if _, err := w.Write(b); err != nil { log.Error().Err(err).Caller().Send() } } func handlerStream(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { outputMjpeg(w, r) } else { inputMjpeg(w, r) } } func outputMjpeg(w http.ResponseWriter, r *http.Request) { src := r.URL.Query().Get("src") stream := streams.Get(src) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } cons := mjpeg.NewConsumer() cons.WithRequest(r) if err := stream.AddConsumer(cons); err != nil { log.Error().Err(err).Msg("[api.mjpeg] add consumer") return } h := w.Header() h.Set("Cache-Control", "no-cache") h.Set("Connection", "close") h.Set("Pragma", "no-cache") if strings.HasSuffix(r.URL.Path, "mjpeg") { wr := mjpeg.NewWriter(w) _, _ = cons.WriteTo(wr) } else { cons.FormatName = "ascii" query := r.URL.Query() wr := ascii.NewWriter(w, query.Get("color"), query.Get("back"), query.Get("text")) _, _ = cons.WriteTo(wr) } stream.RemoveConsumer(cons) } func inputMjpeg(w http.ResponseWriter, r *http.Request) { dst := r.URL.Query().Get("dst") stream := streams.Get(dst) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } prod, _ := mpjpeg.Open(r.Body) prod.WithRequest(r) stream.AddProducer(prod) if err := prod.Start(); err != nil && err != io.EOF { log.Warn().Err(err).Caller().Send() } stream.RemoveProducer(prod) } func handlerWS(tr *ws.Transport, _ *ws.Message) error { stream, _ := streams.GetOrPatch(tr.Request.URL.Query()) if stream == nil { return errors.New(api.StreamNotFound) } cons := mjpeg.NewConsumer() cons.WithRequest(tr.Request) if err := stream.AddConsumer(cons); err != nil { log.Debug().Err(err).Msg("[mjpeg] add consumer") return err } tr.Write(&ws.Message{Type: "mjpeg"}) go cons.WriteTo(tr.Writer()) tr.OnClose(func() { stream.RemoveConsumer(cons) }) return nil } func apiStreamY4M(w http.ResponseWriter, r *http.Request) { src := r.URL.Query().Get("src") stream := streams.Get(src) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } cons := y4m.NewConsumer() cons.WithRequest(r) if err := stream.AddConsumer(cons); err != nil { log.Error().Err(err).Caller().Send() return } _, _ = cons.WriteTo(w) stream.RemoveConsumer(cons) } ================================================ FILE: internal/mp4/README.md ================================================ # MP4 This module provides several features: 1. MSE stream (fMP4 over WebSocket) 2. Camera snapshots in MP4 format (single frame), can be sent to [Telegram](#snapshot-to-telegram) 3. HTTP progressive streaming (MP4 file stream) - bad format for streaming because of high start delay. This format doesn't work in all Safari browsers, but go2rtc will automatically redirect it to HLS/fMP4 in this case. ## API examples - MP4 snapshot: `http://192.168.1.123:1984/api/frame.mp4?src=camera1` (H264, H265) - MP4 stream: `http://192.168.1.123:1984/api/stream.mp4?src=camera1` (H264, H265, AAC) - MP4 file: `http://192.168.1.123:1984/api/stream.mp4?src=camera1` (H264, H265*, AAC, OPUS, MP3, PCMA, PCMU, PCM) - You can use `mp4`, `mp4=flac` and `mp4=all` param for codec filters - You can use `duration` param in seconds (ex. `duration=15`) - You can use `filename` param (ex. `filename=record.mp4`) - You can use `rotate` param with `90`, `180` or `270` values - You can use `scale` param with positive integer values (ex. `scale=4:3`) Read more about [codecs filters](../../README.md#codecs-filters). **PS.** Rotate and scale params don't use transcoding and change video using metadata. ## Snapshot to Telegram This examples for Home Assistant [Telegram Bot](https://www.home-assistant.io/integrations/telegram_bot/) integration. - change `url` to your go2rtc web API (`http://localhost:1984/` for most users) - change `target` to your Telegram chat ID (support list) - change `src=camera1` to your stream name from go2rtc config **Important.** Snapshot will be near instant for most cameras and many sources, except `ffmpeg` source. Because it takes a long time for ffmpeg to start streaming with video, even when you use `#video=copy`. Also the delay can be with cameras that do not start the stream with a keyframe. ### Snapshot from H264 or H265 camera ```yaml service: telegram_bot.send_video data: url: http://localhost:1984/api/frame.mp4?src=camera1 target: 123456789 ``` ### Record from H264 or H265 camera Record from service call to the future. Doesn't support loopback. - `mp4=flac` - adds support PCM audio family - `filename=record.mp4` - set name for downloaded file ```yaml service: telegram_bot.send_video data: url: http://localhost:1984/api/stream.mp4?src=camera1&mp4=flac&duration=5&filename=record.mp4 # duration in seconds target: 123456789 ``` ### Snapshot from JPEG or MJPEG camera This example works via the [mjpeg](../mjpeg/README.md) module. ```yaml service: telegram_bot.send_photo data: url: http://localhost:1984/api/frame.jpeg?src=camera1 target: 123456789 ``` ================================================ FILE: internal/mp4/mp4.go ================================================ package mp4 import ( "context" "net/http" "strconv" "strings" "time" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api/ws" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/mp4" "github.com/rs/zerolog" ) func Init() { log = app.GetLogger("mp4") ws.HandleFunc("mse", handlerWSMSE) ws.HandleFunc("mp4", handlerWSMP4) api.HandleFunc("api/frame.mp4", handlerKeyframe) api.HandleFunc("api/stream.mp4", handlerMP4) } var log zerolog.Logger func handlerKeyframe(w http.ResponseWriter, r *http.Request) { // Chrome 105 does two requests: without Range and with `Range: bytes=0-` ua := r.UserAgent() if strings.Contains(ua, " Chrome/") { if r.Header.Values("Range") == nil { w.Header().Set("Content-Type", "video/mp4") w.WriteHeader(http.StatusOK) return } } query := r.URL.Query() src := query.Get("src") stream := streams.Get(src) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } cons := mp4.NewKeyframe(nil) if err := stream.AddConsumer(cons); err != nil { log.Error().Err(err).Caller().Send() http.Error(w, err.Error(), http.StatusInternalServerError) return } once := &core.OnceBuffer{} // init and first frame _, _ = cons.WriteTo(once) stream.RemoveConsumer(cons) // Apple Safari won't show frame without length header := w.Header() header.Set("Content-Length", strconv.Itoa(once.Len())) header.Set("Content-Type", mp4.ContentType(cons.Codecs())) if filename := query.Get("filename"); filename != "" { header.Set("Content-Disposition", `attachment; filename="`+filename+`"`) } if _, err := once.WriteTo(w); err != nil { log.Error().Err(err).Caller().Send() } } func handlerMP4(w http.ResponseWriter, r *http.Request) { log.Trace().Msgf("[mp4] %s %+v", r.Method, r.Header) query := r.URL.Query() ua := r.UserAgent() if strings.Contains(ua, " Safari/") && !strings.Contains(ua, " Chrome/") && !query.Has("duration") { // auto redirect to HLS/fMP4 format, because Safari not support MP4 stream url := "stream.m3u8?" + r.URL.RawQuery if !query.Has("mp4") { url += "&mp4" } http.Redirect(w, r, url, http.StatusMovedPermanently) return } stream, _ := streams.GetOrPatch(query) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } medias := mp4.ParseQuery(r.URL.Query()) cons := mp4.NewConsumer(medias) cons.FormatName = "mp4" cons.Protocol = "http" cons.WithRequest(r) if err := stream.AddConsumer(cons); err != nil { log.Error().Err(err).Caller().Send() http.Error(w, err.Error(), http.StatusInternalServerError) return } if rotate := query.Get("rotate"); rotate != "" { cons.Rotate = core.Atoi(rotate) } if scale := query.Get("scale"); scale != "" { if sx, sy, ok := strings.Cut(scale, ":"); ok { cons.ScaleX = core.Atoi(sx) cons.ScaleY = core.Atoi(sy) } } header := w.Header() header.Set("Content-Type", mp4.ContentType(cons.Codecs())) if filename := query.Get("filename"); filename != "" { header.Set("Content-Disposition", `attachment; filename="`+filename+`"`) } ctx := r.Context() // handle when the client drops the connection if i := core.Atoi(query.Get("duration")); i > 0 { timeout := time.Second * time.Duration(i) var cancel context.CancelFunc ctx, cancel = context.WithTimeout(ctx, timeout) defer cancel() } go func() { <-ctx.Done() _ = cons.Stop() stream.RemoveConsumer(cons) }() _, _ = cons.WriteTo(w) } ================================================ FILE: internal/mp4/ws.go ================================================ package mp4 import ( "errors" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api/ws" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/mp4" ) func handlerWSMSE(tr *ws.Transport, msg *ws.Message) error { stream, _ := streams.GetOrPatch(tr.Request.URL.Query()) if stream == nil { return errors.New(api.StreamNotFound) } var medias []*core.Media if codecs := msg.String(); codecs != "" { log.Trace().Str("codecs", codecs).Msgf("[mp4] new WS/MSE consumer") medias = mp4.ParseCodecs(codecs, true) } cons := mp4.NewConsumer(medias) cons.FormatName = "mse/fmp4" cons.WithRequest(tr.Request) if err := stream.AddConsumer(cons); err != nil { log.Debug().Err(err).Msg("[mp4] add consumer") return err } tr.Write(&ws.Message{Type: "mse", Value: mp4.ContentType(cons.Codecs())}) go cons.WriteTo(tr.Writer()) tr.OnClose(func() { stream.RemoveConsumer(cons) }) return nil } func handlerWSMP4(tr *ws.Transport, msg *ws.Message) error { stream, _ := streams.GetOrPatch(tr.Request.URL.Query()) if stream == nil { return errors.New(api.StreamNotFound) } var medias []*core.Media if codecs := msg.String(); codecs != "" { log.Trace().Str("codecs", codecs).Msgf("[mp4] new WS/MP4 consumer") medias = mp4.ParseCodecs(codecs, false) } cons := mp4.NewKeyframe(medias) cons.WithRequest(tr.Request) if err := stream.AddConsumer(cons); err != nil { log.Error().Err(err).Caller().Send() return err } tr.Write(&ws.Message{Type: "mse", Value: mp4.ContentType(cons.Codecs())}) go cons.WriteTo(tr.Writer()) tr.OnClose(func() { stream.RemoveConsumer(cons) }) return nil } ================================================ FILE: internal/mpeg/README.md ================================================ # MPEG This module provides an [HTTP API](../api/README.md) for: - Streaming output in `mpegts` format. - Streaming output in `adts` format. - Streaming ingest in `mpegts` format. ## MPEG-TS Server ```shell ffplay http://localhost:1984/api/stream.ts?src=camera1 ``` ## ADTS Server ```shell ffplay http://localhost:1984/api/stream.aac?src=camera1 ``` ## Streaming ingest ```shell ffmpeg -re -i BigBuckBunny.mp4 -c copy -f mpegts http://localhost:1984/api/stream.ts?dst=camera1 ``` ================================================ FILE: internal/mpeg/mpeg.go ================================================ package mpeg import ( "net/http" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/mpegts" ) func Init() { api.HandleFunc("api/stream.ts", apiHandle) api.HandleFunc("api/stream.aac", apiStreamAAC) } func apiHandle(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { outputMpegTS(w, r) } else { inputMpegTS(w, r) } } func outputMpegTS(w http.ResponseWriter, r *http.Request) { src := r.URL.Query().Get("src") stream := streams.Get(src) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } cons := mpegts.NewConsumer() cons.WithRequest(r) if err := stream.AddConsumer(cons); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Add("Content-Type", "video/mp2t") _, _ = cons.WriteTo(w) stream.RemoveConsumer(cons) } func inputMpegTS(w http.ResponseWriter, r *http.Request) { dst := r.URL.Query().Get("dst") stream := streams.Get(dst) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } client, err := mpegts.Open(r.Body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } stream.AddProducer(client) defer stream.RemoveProducer(client) if err = client.Start(); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } } func apiStreamAAC(w http.ResponseWriter, r *http.Request) { src := r.URL.Query().Get("src") stream := streams.Get(src) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } cons := aac.NewConsumer() cons.WithRequest(r) if err := stream.AddConsumer(cons); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } w.Header().Add("Content-Type", "audio/aac") _, _ = cons.WriteTo(w) stream.RemoveConsumer(cons) } ================================================ FILE: internal/multitrans/README.md ================================================ # TP-Link MULTITRANS [`new in v1.9.14`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.14) by [@forrestsocool](https://github.com/forrestsocool) Two-way audio support for Chinese version of [TP-Link](https://www.tp-link.com.cn/) cameras. ## Configuration ```yaml streams: tplink_cam: # video uses standard RTSP - rtsp://admin:admin@192.168.1.202:554/stream1 # two-way audio uses MULTITRANS schema - multitrans://admin:admin@192.168.1.202:554 ``` ## Useful links - https://www.tp-link.com.cn/list_2549.html - https://github.com/AlexxIT/go2rtc/issues/1724 - https://github.com/bingooo/hass-tplink-ipc/ ================================================ FILE: internal/multitrans/multitrans.go ================================================ package multitrans import ( "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/multitrans" ) func Init() { streams.HandleFunc("multitrans", multitrans.Dial) } ================================================ FILE: internal/nest/README.md ================================================ # Google Nest [`new in v1.6.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.6.0) For simplicity, it is recommended to connect the Nest/WebRTC camera to the [Home Assistant](../hass/README.md). But if you can somehow get the below parameters, Nest/WebRTC source will work without Home Assistant. ```yaml streams: nest-doorbell: nest:?client_id=***&client_secret=***&refresh_token=***&project_id=***&device_id=*** ``` ================================================ FILE: internal/nest/init.go ================================================ package nest import ( "net/http" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/nest" ) func Init() { streams.HandleFunc("nest", func(source string) (core.Producer, error) { return nest.Dial(source) }) api.HandleFunc("api/nest", apiNest) } func apiNest(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() cliendID := query.Get("client_id") cliendSecret := query.Get("client_secret") refreshToken := query.Get("refresh_token") projectID := query.Get("project_id") nestAPI, err := nest.NewAPI(cliendID, cliendSecret, refreshToken) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } devices, err := nestAPI.GetDevices(projectID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var items []*api.Source for _, device := range devices { query.Set("device_id", device.DeviceID) query.Set("protocols", strings.Join(device.Protocols, ",")) items = append(items, &api.Source{ Name: device.Name, URL: "nest:?" + query.Encode(), }) } api.ResponseSources(w, items) } ================================================ FILE: internal/ngrok/README.md ================================================ # ngrok With the ngrok integration, you can get external access to your streams when your Internet connection is behind a private IP address. - you may need external access for two different things: - WebRTC streams (tunnel the WebRTC TCP port, e.g. 8555) - go2rtc web interface (tunnel the API HTTP port, e.g. 1984) - ngrok supports authorization for your web interface - ngrok automatically adds HTTPS to your web interface The ngrok free subscription has the following limitations: - You can reserve a free domain for serving the web interface, but the TCP address you get will always be random and will change with each restart of the ngrok agent (not a problem for WebRTC streams) - You can forward multiple ports from a single agent, but you can only run one ngrok agent on the free plan go2rtc will automatically get your external TCP address (if you enable it in the ngrok config) and use it for WebRTC connections (if you enable it in the WebRTC config). You need to manually download the [ngrok agent](https://ngrok.com/download) for your OS and register with the [ngrok service](https://ngrok.com/signup). **Tunnel for only WebRTC Stream** You need to add your [ngrok authtoken](https://dashboard.ngrok.com/get-started/your-authtoken) and WebRTC TCP port to YAML: ```yaml ngrok: command: ngrok tcp 8555 --authtoken eW91IHNoYWxsIG5vdCBwYXNzCnlvdSBzaGFsbCBub3QgcGFzcw ``` **Tunnel for WebRTC and Web interface** You need to create `ngrok.yaml` config file and add it to the go2rtc config: ```yaml ngrok: command: ngrok start --all --config ngrok.yaml ``` ngrok config example: ```yaml version: "2" authtoken: eW91IHNoYWxsIG5vdCBwYXNzCnlvdSBzaGFsbCBub3QgcGFzcw tunnels: api: addr: 1984 # use the same port as in the go2rtc config proto: http basic_auth: - admin:password # you can set login/pass for your web interface webrtc: addr: 8555 # use the same port as in the go2rtc config proto: tcp ``` See the [ngrok agent documentation](https://ngrok.com/docs/agent/config/) for more details on the ngrok configuration file. ================================================ FILE: internal/ngrok/ngrok.go ================================================ package ngrok import ( "fmt" "net" "strings" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/webrtc" "github.com/AlexxIT/go2rtc/pkg/ngrok" "github.com/rs/zerolog" ) func Init() { var cfg struct { Mod struct { Cmd string `yaml:"command"` } `yaml:"ngrok"` } app.LoadConfig(&cfg) if cfg.Mod.Cmd == "" { return } log = app.GetLogger("ngrok") ngr, err := ngrok.NewNgrok(cfg.Mod.Cmd) if err != nil { log.Error().Err(err).Msg("[ngrok] start") } ngr.Listen(func(msg any) { if msg := msg.(*ngrok.Message); msg != nil { if strings.HasPrefix(msg.Line, "ERROR:") { log.Warn().Msg("[ngrok] " + msg.Line) } else { log.Debug().Msg("[ngrok] " + msg.Line) } // Addr: "//localhost:8555", URL: "tcp://1.tcp.eu.ngrok.io:12345" if strings.HasPrefix(msg.Addr, "//localhost:") && strings.HasPrefix(msg.URL, "tcp://") { // don't know if really necessary use IP address, err := ConvertHostToIP(msg.URL[6:]) if err != nil { log.Warn().Err(err).Msg("[ngrok] add candidate") return } log.Info().Str("addr", address).Msg("[ngrok] add external candidate for WebRTC") webrtc.AddCandidate("tcp", address) } } }) go func() { if err = ngr.Serve(); err != nil { log.Error().Err(err).Msg("[ngrok] run") } }() } var log zerolog.Logger func ConvertHostToIP(address string) (string, error) { host, port, err := net.SplitHostPort(address) if err != nil { return "", err } ip, err := net.LookupIP(host) if err != nil { return "", err } if len(ip) == 0 { return "", fmt.Errorf("can't resolve: %s", host) } return ip[0].String() + ":" + port, nil } ================================================ FILE: internal/onvif/README.md ================================================ # ONVIF ## ONVIF Client [`new in v1.5.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.5.0) The source is not very useful if you already know RTSP and snapshot links for your camera. But it can be useful if you don't. **WebUI > Add** webpage supports ONVIF autodiscovery. Your server must be on the same subnet as the camera. If you use Docker, you must use "network host". ```yaml streams: dahua1: onvif://admin:password@192.168.1.123 reolink1: onvif://admin:password@192.168.1.123:8000 tapo1: onvif://admin:password@192.168.1.123:2020 ``` ## ONVIF Server A regular camera has a single video source (`GetVideoSources`) and two profiles (`GetProfiles`). Go2rtc has one video source and one profile per stream. ## Tested clients Go2rtc works as ONVIF server: - Happytime onvif client (windows) - Home Assistant ONVIF integration (linux) - Onvier (android) - ONVIF Device Manager (windows) PS. Supports only TCP transport for RTSP protocol. UDP and HTTP transports - unsupported yet. ## Tested cameras Go2rtc works as ONVIF client: - Dahua IPC-K42 - OpenIPC - Reolink RLC-520A - TP-Link Tapo TC60 ================================================ FILE: internal/onvif/onvif.go ================================================ package onvif import ( "io" "net" "net/http" "net/url" "os" "strconv" "strings" "time" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/rtsp" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/onvif" "github.com/rs/zerolog" ) func Init() { log = app.GetLogger("onvif") streams.HandleFunc("onvif", streamOnvif) // ONVIF server on all suburls api.HandleFunc("/onvif/", onvifDeviceService) // ONVIF client autodiscovery api.HandleFunc("api/onvif", apiOnvif) } var log zerolog.Logger func streamOnvif(rawURL string) (core.Producer, error) { client, err := onvif.NewClient(rawURL) if err != nil { return nil, err } uri, err := client.GetURI() if err != nil { return nil, err } // Append hash-based arguments to the retrieved URI if i := strings.IndexByte(rawURL, '#'); i > 0 { uri += rawURL[i:] } log.Debug().Msgf("[onvif] new uri=%s", uri) if err = streams.Validate(uri); err != nil { return nil, err } return streams.GetProducer(uri) } func onvifDeviceService(w http.ResponseWriter, r *http.Request) { b, err := io.ReadAll(r.Body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } operation := onvif.GetRequestAction(b) if operation == "" { http.Error(w, "malformed request body", http.StatusBadRequest) return } log.Trace().Msgf("[onvif] server request %s %s:\n%s", r.Method, r.RequestURI, b) switch operation { case onvif.ServiceGetServiceCapabilities, // important for Hass onvif.DeviceGetNetworkInterfaces, // important for Hass onvif.DeviceGetSystemDateAndTime, // important for Hass onvif.DeviceSetSystemDateAndTime, // return just OK onvif.DeviceGetDiscoveryMode, onvif.DeviceGetDNS, onvif.DeviceGetHostname, onvif.DeviceGetNetworkDefaultGateway, onvif.DeviceGetNetworkProtocols, onvif.DeviceGetNTP, onvif.DeviceGetScopes, onvif.MediaGetVideoEncoderConfiguration, onvif.MediaGetVideoEncoderConfigurations, onvif.MediaGetAudioEncoderConfigurations, onvif.MediaGetVideoEncoderConfigurationOptions, onvif.MediaGetAudioSources, onvif.MediaGetAudioSourceConfigurations: b = onvif.StaticResponse(operation) case onvif.DeviceGetCapabilities: // important for Hass: Media section b = onvif.GetCapabilitiesResponse(r.Host) case onvif.DeviceGetServices: b = onvif.GetServicesResponse(r.Host) case onvif.DeviceGetDeviceInformation: // important for Hass: SerialNumber (unique server ID) b = onvif.GetDeviceInformationResponse("", "go2rtc", app.Version, r.Host) case onvif.DeviceSystemReboot: b = onvif.StaticResponse(operation) time.AfterFunc(time.Second, func() { os.Exit(0) }) case onvif.MediaGetVideoSources: b = onvif.GetVideoSourcesResponse(streams.GetAllNames()) case onvif.MediaGetProfiles: // important for Hass: H264 codec, width, height b = onvif.GetProfilesResponse(streams.GetAllNames()) case onvif.MediaGetProfile: token := onvif.FindTagValue(b, "ProfileToken") b = onvif.GetProfileResponse(token) case onvif.MediaGetVideoSourceConfigurations: // important for Happytime Onvif Client b = onvif.GetVideoSourceConfigurationsResponse(streams.GetAllNames()) case onvif.MediaGetVideoSourceConfiguration: token := onvif.FindTagValue(b, "ConfigurationToken") b = onvif.GetVideoSourceConfigurationResponse(token) case onvif.MediaGetStreamUri: host, _, err := net.SplitHostPort(r.Host) if err != nil { host = r.Host // in case of Host without port } uri := "rtsp://" + host + ":" + rtsp.Port + "/" + onvif.FindTagValue(b, "ProfileToken") b = onvif.GetStreamUriResponse(uri) case onvif.MediaGetSnapshotUri: uri := "http://" + r.Host + "/api/frame.jpeg?src=" + onvif.FindTagValue(b, "ProfileToken") b = onvif.GetSnapshotUriResponse(uri) default: http.Error(w, "unsupported operation", http.StatusBadRequest) log.Warn().Msgf("[onvif] unsupported operation: %s", operation) log.Debug().Msgf("[onvif] unsupported request:\n%s", b) return } log.Trace().Msgf("[onvif] server response:\n%s", b) w.Header().Set("Content-Type", "application/soap+xml; charset=utf-8") if _, err = w.Write(b); err != nil { log.Error().Err(err).Caller().Send() } } func apiOnvif(w http.ResponseWriter, r *http.Request) { src := r.URL.Query().Get("src") var items []*api.Source if src == "" { devices, err := onvif.DiscoveryStreamingDevices() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } for _, device := range devices { u, err := url.Parse(device.URL) if err != nil { log.Warn().Str("url", device.URL).Msg("[onvif] broken") continue } if u.Scheme != "http" { log.Warn().Str("url", device.URL).Msg("[onvif] unsupported") continue } u.Scheme = "onvif" u.User = url.UserPassword("user", "pass") if u.Path == onvif.PathDevice { u.Path = "" } items = append(items, &api.Source{ Name: u.Host, URL: u.String(), Info: device.Name + " " + device.Hardware, }) } } else { client, err := onvif.NewClient(src) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if l := log.Trace(); l.Enabled() { b, _ := client.MediaRequest(onvif.MediaGetProfiles) l.Msgf("[onvif] src=%s profiles:\n%s", src, b) } name, err := client.GetName() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } tokens, err := client.GetProfilesTokens() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } for i, token := range tokens { items = append(items, &api.Source{ Name: name + " stream" + strconv.Itoa(i), URL: src + "?subtype=" + token, }) } if len(tokens) > 0 && client.HasSnapshots() { items = append(items, &api.Source{ Name: name + " snapshot", URL: src + "?subtype=" + tokens[0] + "&snapshot", }) } } api.ResponseSources(w, items) } ================================================ FILE: internal/pinggy/README.md ================================================ # Pinggy [Pinggy](https://pinggy.io/) - nice service for public tunnels to your local services. **Features:** - A free account does not require registration. - It does not require downloading third-party binaries and works over the SSH protocol. - Works with HTTP, TCP and UDP protocols. - Creates HTTPS for your HTTP services. > [!IMPORTANT] > A free account creates a tunnel with a random address that only works for an hour. It is suitable for testing purposes ONLY. > [!CAUTION] > Public access to go2rtc without authorization puts your entire home network at risk. Use with caution. **Why:** - It's easy to set up HTTPS for testing two-way audio. - It's easy to check whether external access via WebRTC technology will work. - It's easy to share direct access to your RTSP or HTTP camera with the go2rtc developer. If such access is necessary to debug your problem. ## Configuration You will find public links in the go2rtc log after startup. **Tunnel to go2rtc WebUI.** ```yaml pinggy: tunnel: http://localhost:1984 ``` **Tunnel to RTSP camera.** For example, you have camera: `rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0` ```yaml pinggy: tunnel: tcp://192.168.10.91:554 ``` In go2rtc logs you will get similar output: ``` 16:17:43.167 INF [pinggy] proxy url=tcp://abcde-123-123-123-123.a.free.pinggy.link:12345 ``` Now you have a working stream: ``` rtsp://admin:password@abcde-123-123-123-123.a.free.pinggy.link:12345/cam/realmonitor?channel=1&subtype=0 ``` ================================================ FILE: internal/pinggy/pinggy.go ================================================ package pinggy import ( "net/url" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/pkg/pinggy" "github.com/rs/zerolog" ) func Init() { var cfg struct { Mod struct { Tunnel string `yaml:"tunnel"` } `yaml:"pinggy"` } app.LoadConfig(&cfg) if cfg.Mod.Tunnel == "" { return } log = app.GetLogger("pinggy") u, err := url.Parse(cfg.Mod.Tunnel) if err != nil { log.Error().Err(err).Send() return } go proxy(u.Scheme, u.Host) } var log zerolog.Logger func proxy(proto, address string) { client, err := pinggy.NewClient(proto) if err != nil { log.Error().Err(err).Send() return } defer client.Close() urls, err := client.GetURLs() if err != nil { log.Error().Err(err).Send() return } for _, s := range urls { log.Info().Str("url", s).Msgf("[pinggy] proxy") } err = client.Proxy(address) if err != nil { log.Error().Err(err).Send() return } } ================================================ FILE: internal/ring/README.md ================================================ # Ring [`new in v1.9.13`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.13) by [@seydx](https://github.com/seydx) This source type supports Ring cameras with [two-way audio](../../README.md#two-way-audio) support. ## Configuration If you have a `refresh_token` and `device_id`, you can use them in the `go2rtc.yaml` config file. Otherwise, you can use the go2rtc web interface and add your Ring account (WebUI > Add > Ring). Once added, it will list all your Ring cameras. ```yaml streams: ring: ring:?device_id=XXX&refresh_token=XXX ring_snapshot: ring:?device_id=XXX&refresh_token=XXX&snapshot ``` ================================================ FILE: internal/ring/ring.go ================================================ package ring import ( "net/http" "net/url" "fmt" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/ring" ) func Init() { streams.HandleFunc("ring", func(source string) (core.Producer, error) { return ring.Dial(source) }) api.HandleFunc("api/ring", apiRing) } func apiRing(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() var ringAPI *ring.RingApi // Check auth method if email := query.Get("email"); email != "" { // Email/Password Flow password := query.Get("password") code := query.Get("code") var err error ringAPI, err = ring.NewRestClient(ring.EmailAuth{ Email: email, Password: password, }, nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } // Try authentication (this will trigger 2FA if needed) if _, err = ringAPI.GetAuth(code); err != nil { if ringAPI.Using2FA { // Return 2FA prompt api.ResponseJSON(w, map[string]interface{}{ "needs_2fa": true, "prompt": ringAPI.PromptFor2FA, }) return } http.Error(w, err.Error(), http.StatusInternalServerError) return } } else if refreshToken := query.Get("refresh_token"); refreshToken != "" { // Refresh Token Flow if refreshToken == "" { http.Error(w, "either email/password or refresh_token is required", http.StatusBadRequest) return } var err error ringAPI, err = ring.NewRestClient(ring.RefreshTokenAuth{ RefreshToken: refreshToken, }, nil) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } } else { http.Error(w, "either email/password or refresh token is required", http.StatusBadRequest) return } devices, err := ringAPI.FetchRingDevices() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } cleanQuery := url.Values{} cleanQuery.Set("refresh_token", ringAPI.RefreshToken) var items []*api.Source for _, camera := range devices.AllCameras { cleanQuery.Set("camera_id", fmt.Sprint(camera.ID)) cleanQuery.Set("device_id", camera.DeviceID) // Stream source items = append(items, &api.Source{ Name: camera.Description, URL: "ring:?" + cleanQuery.Encode(), }) // Snapshot source items = append(items, &api.Source{ Name: camera.Description + " Snapshot", URL: "ring:?" + cleanQuery.Encode() + "&snapshot", }) } api.ResponseSources(w, items) } ================================================ FILE: internal/roborock/README.md ================================================ # Roborock [`new in v1.3.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.3.0) This source type supports Roborock vacuums with cameras. Known working models: - **Roborock S6 MaxV** - only video (the vacuum has no microphone) - **Roborock S7 MaxV** - video and two-way audio - **Roborock Qrevo MaxV** - video and two-way audio ## Configuration This source supports loading Roborock credentials from the Home Assistant [custom integration](https://github.com/humbertogontijo/homeassistant-roborock) or the [core integration](https://www.home-assistant.io/integrations/roborock). Otherwise, you need to log in to your Roborock account (MiHome account is not supported). Go to go2rtc WebUI > Add webpage. Copy the `roborock://...` source for your vacuum and paste it into your `go2rtc.yaml` config. If you have a pattern PIN for your vacuum, add it as a numeric PIN (lines: 123, 456, 789) to the end of the `roborock` link. ================================================ FILE: internal/roborock/roborock.go ================================================ package roborock import ( "fmt" "net/http" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/roborock" ) func Init() { streams.HandleFunc("roborock", func(source string) (core.Producer, error) { return roborock.Dial(source) }) api.HandleFunc("api/roborock", apiHandle) } var Auth struct { UserData *roborock.UserInfo `json:"user_data"` BaseURL string `json:"base_url"` } func apiHandle(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": if Auth.UserData == nil { http.Error(w, "no auth", http.StatusNotFound) return } case "POST": if err := r.ParseMultipartForm(1024); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } username := r.Form.Get("username") password := r.Form.Get("password") if username == "" || password == "" { http.Error(w, "empty username or password", http.StatusBadRequest) return } base, err := roborock.GetBaseURL(username) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } ui, err := roborock.Login(base, username, password) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } Auth.BaseURL = base Auth.UserData = ui default: http.Error(w, "", http.StatusMethodNotAllowed) return } homeID, err := roborock.GetHomeID(Auth.BaseURL, Auth.UserData.Token) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } devices, err := roborock.GetDevices(Auth.UserData, homeID) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var items []*api.Source for _, device := range devices { source := fmt.Sprintf( "roborock://%s?u=%s&s=%s&k=%s&did=%s&key=%s&pin=", Auth.UserData.IoT.URL.MQTT[6:], Auth.UserData.IoT.User, Auth.UserData.IoT.Pass, Auth.UserData.IoT.Domain, device.DID, device.Key, ) items = append(items, &api.Source{Name: device.Name, URL: source}) } api.ResponseSources(w, items) } ================================================ FILE: internal/rtmp/README.md ================================================ # Real-Time Messaging Protocol This module provides the following features for the RTMP protocol: - Streaming input - [RTMP client](#rtmp-client) - Streaming output and ingest in `rtmp` format - [RTMP server](#rtmp-server) - Streaming output and ingest in `flv` format - [FLV server](#flv-server) ## RTMP Client You can get a stream from an RTMP server, for example [Nginx with nginx-rtmp-module](https://github.com/arut/nginx-rtmp-module). ### Client Configuration ```yaml streams: rtmp_stream: rtmp://192.168.1.123/live/camera1 ``` ## RTMP Server [`new in v1.8.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.8.0) Streaming output stream in `rtmp` format: ```shell ffplay rtmp://localhost:1935/camera1 ``` Streaming ingest stream in `rtmp` format: ```shell ffmpeg -re -i BigBuckBunny.mp4 -c copy -f flv rtmp://localhost:1935/camera1 ``` ### Server Configuration By default, the RTMP server is disabled. ```yaml rtmp: listen: ":1935" # by default - disabled! ``` ## FLV Server Streaming output in `flv` format. ```shell ffplay http://localhost:1984/stream.flv?src=camera1 ``` Streaming ingest in `flv` format. ```shell ffmpeg -re -i BigBuckBunny.mp4 -c copy -f flv http://localhost:1984/api/stream.flv?dst=camera1 ``` ## Tested clients | From | To | Comment | |--------|---------------------------------|---------| | go2rtc | Reolink RLC-520A fw. v3.1.0.801 | OK | **go2rtc.yaml** ```yaml streams: rtmp-reolink1: rtmp://192.168.10.92/bcs/channel0_main.bcs?channel=0&stream=0&user=admin&password=password rtmp-reolink2: rtmp://192.168.10.92/bcs/channel0_sub.bcs?channel=0&stream=1&user=admin&password=password rtmp-reolink3: rtmp://192.168.10.92/bcs/channel0_ext.bcs?channel=0&stream=1&user=admin&password=password ``` ## Tested server | From | To | Comment | |------------------------|--------|---------------------| | OBS 31.0.2 | go2rtc | OK | | OpenIPC 2.5.03.02-lite | go2rtc | OK | | FFmpeg 6.1 | go2rtc | OK | | GoPro Black 12 | go2rtc | OK, 1080p, 5000kbps | **go2rtc.yaml** ```yaml rtmp: listen: :1935 streams: tmp: ``` **OBS** Settings > Stream: - Service: Custom - Server: rtmp://192.168.10.101/tmp - Stream Key: `` - Use auth: `` **OpenIPC** WebUI > Majestic > Settings > Outgoing - Enable - Address: rtmp://192.168.10.101/tmp - Save - Restart **FFmpeg** ```shell ffmpeg -re -i bbb.mp4 -c copy -f flv rtmp://192.168.10.101/tmp ``` **GoPro** GoPro Quik > Camera > Translation > Other ================================================ FILE: internal/rtmp/rtmp.go ================================================ package rtmp import ( "errors" "io" "net" "net/http" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/flv" "github.com/AlexxIT/go2rtc/pkg/rtmp" "github.com/rs/zerolog" ) func Init() { var conf struct { Mod struct { Listen string `yaml:"listen" json:"listen"` } `yaml:"rtmp"` } app.LoadConfig(&conf) log = app.GetLogger("rtmp") streams.HandleFunc("rtmp", streamsHandle) streams.HandleFunc("rtmps", streamsHandle) streams.HandleFunc("rtmpx", streamsHandle) api.HandleFunc("api/stream.flv", apiHandle) streams.HandleConsumerFunc("rtmp", streamsConsumerHandle) streams.HandleConsumerFunc("rtmps", streamsConsumerHandle) streams.HandleConsumerFunc("rtmpx", streamsConsumerHandle) address := conf.Mod.Listen if address == "" { return } ln, err := net.Listen("tcp", address) if err != nil { log.Error().Err(err).Caller().Send() return } log.Info().Str("addr", address).Msg("[rtmp] listen") go func() { for { conn, err := ln.Accept() if err != nil { return } go func() { if err = tcpHandle(conn); err != nil { log.Error().Err(err).Caller().Send() } }() } }() } func tcpHandle(netConn net.Conn) error { rtmpConn, err := rtmp.NewServer(netConn) if err != nil { return err } if err = rtmpConn.ReadCommands(); err != nil { return err } switch rtmpConn.Intent { case rtmp.CommandPlay: stream := streams.Get(rtmpConn.App) if stream == nil { return errors.New("stream not found: " + rtmpConn.App) } cons := flv.NewConsumer() if err = stream.AddConsumer(cons); err != nil { return err } defer stream.RemoveConsumer(cons) if err = rtmpConn.WriteStart(); err != nil { return err } _, _ = cons.WriteTo(rtmpConn) return nil case rtmp.CommandPublish: stream := streams.Get(rtmpConn.App) if stream == nil { return errors.New("stream not found: " + rtmpConn.App) } if err = rtmpConn.WriteStart(); err != nil { return err } prod, err := rtmpConn.Producer() if err != nil { return err } stream.AddProducer(prod) defer stream.RemoveProducer(prod) _ = prod.Start() return nil } return errors.New("rtmp: unknown command: " + rtmpConn.Intent) } var log zerolog.Logger func streamsHandle(url string) (core.Producer, error) { return rtmp.DialPlay(url) } func streamsConsumerHandle(url string) (core.Consumer, func(), error) { cons := flv.NewConsumer() run := func() { wr, err := rtmp.DialPublish(url, cons) if err != nil { return } _, err = cons.WriteTo(wr) } return cons, run, nil } func apiHandle(w http.ResponseWriter, r *http.Request) { if r.Method != "POST" { outputFLV(w, r) } else { inputFLV(w, r) } } func outputFLV(w http.ResponseWriter, r *http.Request) { src := r.URL.Query().Get("src") stream := streams.Get(src) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } cons := flv.NewConsumer() cons.WithRequest(r) if err := stream.AddConsumer(cons); err != nil { log.Error().Err(err).Caller().Send() return } h := w.Header() h.Set("Content-Type", "video/x-flv") _, _ = cons.WriteTo(w) stream.RemoveConsumer(cons) } func inputFLV(w http.ResponseWriter, r *http.Request) { dst := r.URL.Query().Get("dst") stream := streams.Get(dst) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } client, err := flv.Open(r.Body) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } stream.AddProducer(client) if err = client.Start(); err != nil && err != io.EOF { log.Warn().Err(err).Caller().Send() } stream.RemoveProducer(client) } ================================================ FILE: internal/rtsp/README.md ================================================ # Real Time Streaming Protocol This module provides the following features for the RTSP protocol: - Streaming input - [RTSP client](#rtsp-client) - Streaming output - [RTSP server](#rtsp-server) - [Streaming ingest](#streaming-ingest) - [Two-way audio](#two-way-audio) ## RTSP Client ### Configuration ```yaml streams: sonoff_camera: rtsp://rtsp:12345678@192.168.1.123/av_stream/ch0 dahua_camera: - rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif - rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=1#backchannel=0 amcrest_doorbell: - rtsp://username:password@192.168.1.123:554/cam/realmonitor?channel=1&subtype=0#backchannel=0 unifi_camera: rtspx://192.168.1.123:7441/fD6ouM72bWoFijxK glichy_camera: ffmpeg:rtsp://username:password@192.168.1.123/live/ch00_1 ``` ### Recommendations - **Amcrest Doorbell** users may want to disable two-way audio, because with an active stream, you won't have a working call button. You need to add `#backchannel=0` to the end of your RTSP link in YAML config file - **Dahua Doorbell** users may want to change [audio codec](https://github.com/AlexxIT/go2rtc/issues/49#issuecomment-2127107379) for proper two-way audio. Make sure not to request backchannel multiple times by adding `#backchannel=0` to other stream sources of the same doorbell. The `unicast=true&proto=Onvif` is preferred for two-way audio as this makes the doorbell accept multiple codecs for the incoming audio - **Reolink** users may want NOT to use RTSP protocol at all, some camera models have a very awful, unusable stream implementation - **Ubiquiti UniFi** users may want to disable HTTPS verification. Use `rtspx://` prefix instead of `rtsps://`. And don't use `?enableSrtp` [suffix](https://github.com/AlexxIT/go2rtc/issues/81) - **TP-Link Tapo** users may skip login and password, because go2rtc supports login [without them](https://drmnsamoliu.github.io/video.html) - If your camera has two RTSP links, you can add both as sources. This is useful when streams have different codecs, for example AAC audio with main stream and PCMU/PCMA audio with second stream - If the stream from your camera is glitchy, try using [ffmpeg source](../ffmpeg/README.md). It will not add CPU load if you don't use transcoding - If the stream from your camera is very glitchy, try to use transcoding with [ffmpeg source](../ffmpeg/README.md) ### Other options Format: `rtsp...#{param1}#{param2}#{param3}` - Add custom timeout `#timeout=30` (in seconds) - Ignore audio - `#media=video` or ignore video - `#media=audio` - Ignore two-way audio API `#backchannel=0` - important for some glitchy cameras - Use WebSocket transport `#transport=ws...` ### RTSP over WebSocket ```yaml streams: # WebSocket with authorization, RTSP - without axis-rtsp-ws: rtsp://192.168.1.123:4567/axis-media/media.amp?overview=0&camera=1&resolution=1280x720&videoframeskipmode=empty&Axis-Orig-Sw=true#transport=ws://user:pass@192.168.1.123:4567/rtsp-over-websocket # WebSocket without authorization, RTSP - with dahua-rtsp-ws: rtsp://user:pass@192.168.1.123/cam/realmonitor?channel=1&subtype=1&proto=Private3#transport=ws://192.168.1.123/rtspoverwebsocket ``` ## RTSP Server You can get any stream as RTSP-stream: `rtsp://192.168.1.123:8554/{stream_name}` You can enable external password protection for your RTSP streams. Password protection is always disabled for localhost calls (ex. FFmpeg or Home Assistant on the same server). ### Configuration ```yaml rtsp: listen: ":8554" # RTSP Server TCP port, default - 8554 username: "admin" # optional, default - disabled password: "pass" # optional, default - disabled default_query: "video&audio" # optional, default codecs filters ``` By default go2rtc provide RTSP-stream with only one first video and only one first audio. You can change it with the `default_query` setting: - `default_query: "mp4"` - MP4 compatible codecs (H264, H265, AAC) - `default_query: "video=all&audio=all"` - all tracks from all source (not all players can handle this) - `default_query: "video=h264,h265"` - only one video track (H264 or H265) - `default_query: "video&audio=all"` - only one first any video and all audio as separate tracks Read more about [codecs filters](../../README.md#codecs-filters). ## Streaming ingest ```shell ffmpeg -re -i BigBuckBunny.mp4 -c copy -rtsp_transport tcp -f rtsp rtsp://localhost:8554/camera1 ``` ## Two-way audio Before purchasing, it is difficult to understand whether the camera supports two-way audio via the RTSP protocol or not. This isn't usually mentioned in a camera's description. You can only find out by reading reviews from real buyers. A camera is considered to support two-way audio if it supports the ONVIF Profile T protocol. But in reality, this isn't always the case. And the ONVIF protocol has no connection with the camera's RTSP implementation. In go2rtc you can find out if the camera supports two-way audio via WebUI > stream probe. ================================================ FILE: internal/rtsp/rtsp.go ================================================ package rtsp import ( "errors" "io" "net" "net/url" "strings" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/rtsp" "github.com/AlexxIT/go2rtc/pkg/tcp" "github.com/rs/zerolog" ) func Init() { var conf struct { Mod struct { Listen string `yaml:"listen" json:"listen"` Username string `yaml:"username" json:"-"` Password string `yaml:"password" json:"-"` DefaultQuery string `yaml:"default_query" json:"default_query"` PacketSize uint16 `yaml:"pkt_size" json:"pkt_size,omitempty"` } `yaml:"rtsp"` } // default config conf.Mod.Listen = ":8554" conf.Mod.DefaultQuery = "video&audio" app.LoadConfig(&conf) app.Info["rtsp"] = conf.Mod log = app.GetLogger("rtsp") // RTSP client support streams.HandleFunc("rtsp", rtspHandler) streams.HandleFunc("rtsps", rtspHandler) streams.HandleFunc("rtspx", rtspHandler) // RTSP server support address := conf.Mod.Listen if address == "" { return } ln, err := net.Listen("tcp", address) if err != nil { log.Error().Err(err).Msg("[rtsp] listen") return } _, Port, _ = net.SplitHostPort(address) log.Info().Str("addr", address).Msg("[rtsp] listen") if query, err := url.ParseQuery(conf.Mod.DefaultQuery); err == nil { defaultMedias = ParseQuery(query) } go func() { for { conn, err := ln.Accept() if err != nil { return } c := rtsp.NewServer(conn) c.PacketSize = conf.Mod.PacketSize // skip check auth for localhost if conf.Mod.Username != "" && !conn.RemoteAddr().(*net.TCPAddr).IP.IsLoopback() { c.Auth(conf.Mod.Username, conf.Mod.Password) } go tcpHandler(c) } }() } type Handler func(conn *rtsp.Conn) bool func HandleFunc(handler Handler) { handlers = append(handlers, handler) } var Port string // internal var log zerolog.Logger var handlers []Handler var defaultMedias []*core.Media func rtspHandler(rawURL string) (core.Producer, error) { rawURL, rawQuery, _ := strings.Cut(rawURL, "#") conn := rtsp.NewClient(rawURL) conn.Backchannel = true conn.UserAgent = app.UserAgent if rawQuery != "" { query := streams.ParseQuery(rawQuery) conn.Backchannel = query.Get("backchannel") == "1" conn.Media = query.Get("media") conn.Timeout = core.Atoi(query.Get("timeout")) conn.Transport = query.Get("transport") } if log.Trace().Enabled() { conn.Listen(func(msg any) { switch msg := msg.(type) { case *tcp.Request: log.Trace().Msgf("[rtsp] client request:\n%s", msg) case *tcp.Response: log.Trace().Msgf("[rtsp] client response:\n%s", msg) case string: log.Trace().Msgf("[rtsp] client msg: %s", msg) } }) } if err := conn.Dial(); err != nil { return nil, err } if err := conn.Describe(); err != nil { if !conn.Backchannel { return nil, err } log.Trace().Msgf("[rtsp] describe (backchannel=%t) err: %v", conn.Backchannel, err) // second try without backchannel, we need to reconnect conn.Backchannel = false if err = conn.Dial(); err != nil { return nil, err } if err = conn.Describe(); err != nil { return nil, err } } return conn, nil } func tcpHandler(conn *rtsp.Conn) { var name string var closer func() trace := log.Trace().Enabled() level := zerolog.WarnLevel conn.Listen(func(msg any) { if trace { switch msg := msg.(type) { case *tcp.Request: log.Trace().Msgf("[rtsp] server request:\n%s", msg) case *tcp.Response: log.Trace().Msgf("[rtsp] server response:\n%s", msg) } } switch msg { case rtsp.MethodDescribe: if len(conn.URL.Path) == 0 { log.Warn().Msg("[rtsp] server empty URL on DESCRIBE") return } name = conn.URL.Path[1:] stream := streams.Get(name) if stream == nil { return } log.Debug().Str("stream", name).Msg("[rtsp] new consumer") conn.SessionName = app.UserAgent query := conn.URL.Query() conn.Medias = ParseQuery(query) if conn.Medias == nil { for _, media := range defaultMedias { conn.Medias = append(conn.Medias, media.Clone()) } } if query.Get("backchannel") == "1" { conn.Medias = append(conn.Medias, &core.Media{ Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ {Name: core.CodecOpus, ClockRate: 48000, Channels: 2}, {Name: core.CodecPCM, ClockRate: 16000}, {Name: core.CodecPCMA, ClockRate: 16000}, {Name: core.CodecPCMU, ClockRate: 16000}, {Name: core.CodecPCM, ClockRate: 8000}, {Name: core.CodecPCMA, ClockRate: 8000}, {Name: core.CodecPCMU, ClockRate: 8000}, {Name: core.CodecAAC, ClockRate: 8000}, {Name: core.CodecAAC, ClockRate: 16000}, }, }) } if s := query.Get("pkt_size"); s != "" { conn.PacketSize = uint16(core.Atoi(s)) } // param name like ffmpeg style https://ffmpeg.org/ffmpeg-protocols.html if s := query.Get("log_level"); s != "" { if lvl, err := zerolog.ParseLevel(s); err == nil { level = lvl } } // will help to protect looping requests to same source conn.Connection.Source = query.Get("source") if err := stream.AddConsumer(conn); err != nil { log.WithLevel(level).Err(err).Str("stream", name).Msg("[rtsp]") return } closer = func() { stream.RemoveConsumer(conn) } case rtsp.MethodAnnounce: if len(conn.URL.Path) == 0 { log.Warn().Msg("[rtsp] server empty URL on ANNOUNCE") return } name = conn.URL.Path[1:] stream := streams.Get(name) if stream == nil { return } query := conn.URL.Query() if s := query.Get("timeout"); s != "" { conn.Timeout = core.Atoi(s) } log.Debug().Str("stream", name).Msg("[rtsp] new producer") stream.AddProducer(conn) closer = func() { stream.RemoveProducer(conn) } } }) if err := conn.Accept(); err != nil { if errors.Is(err, rtsp.FailedAuth) { log.Warn().Str("remote_addr", conn.Connection.RemoteAddr).Msg("[rtsp] failed authentication") } else if err != io.EOF { log.WithLevel(level).Err(err).Caller().Send() } if closer != nil { closer() } _ = conn.Close() return } for _, handler := range handlers { if handler(conn) { return } } if closer != nil { if err := conn.Handle(); err != nil { log.Debug().Err(err).Msg("[rtsp] handle") } closer() log.Debug().Str("stream", name).Msg("[rtsp] disconnect") } _ = conn.Close() } func ParseQuery(query map[string][]string) []*core.Media { if v := query["mp4"]; v != nil { return []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecH264}, {Name: core.CodecH265}, }, }, { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecAAC}, }, }, } } return core.ParseQuery(query) } ================================================ FILE: internal/srtp/README.md ================================================ # SRTP This is a support module for the [HomeKit](../homekit/README.md) module. > [!NOTE] > This module can be removed and its functionality transferred to the homekit module. ## Configuration ```yaml srtp: listen: :8443 # enabled by default ``` ================================================ FILE: internal/srtp/srtp.go ================================================ package srtp import ( "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/pkg/srtp" ) func Init() { var cfg struct { Mod struct { Listen string `yaml:"listen"` } `yaml:"srtp"` } // default config cfg.Mod.Listen = ":8443" // load config from YAML app.LoadConfig(&cfg) if cfg.Mod.Listen == "" { return } // create SRTP server (endpoint) for receiving video from HomeKit cameras Server = srtp.NewServer(cfg.Mod.Listen) } var Server *srtp.Server ================================================ FILE: internal/streams/README.md ================================================ # Streams This core module is responsible for managing the stream list. ## Stream to camera [`new in v1.3.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.3.0) go2rtc supports playing audio files (ex. music or [TTS](https://www.home-assistant.io/integrations/#text-to-speech)) and live streams (ex. radio) on cameras with [two-way audio](../../README.md#two-way-audio) support. API example: ```text POST http://localhost:1984/api/streams?dst=camera1&src=ffmpeg:http://example.com/song.mp3#audio=pcma#input=file ``` - you can stream: local files, web files, live streams or any format, supported by FFmpeg - you should use [ffmpeg source](../ffmpeg/README.md) for transcoding audio to codec, that your camera supports - you can check camera codecs on the go2rtc WebUI info page when the stream is active - some cameras support only low quality `PCMA/8000` codec (ex. [Tapo](../tapo/README.md)) - it is recommended to choose higher quality formats if your camera supports them (ex. `PCMA/48000` for some Dahua cameras) - if you play files over `http` link, you need to add `#input=file` params for transcoding, so the file will be transcoded and played in real time - if you play live streams, you should skip `#input` param, because it is already in real time - you can stop active playback by calling the API with the empty `src` parameter - you will see one active producer and one active consumer in go2rtc WebUI info page during streaming ## Publish stream [`new in v1.8.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.8.0) You can publish any stream to streaming services (YouTube, Telegram, etc.) via RTMP/RTMPS. Important: - Supported codecs: H264 for video and AAC for audio - AAC audio is required for YouTube; videos without audio will not work - You don't need to enable [RTMP module](../rtmp/README.md) listening for this task You can use the API: ```text POST http://localhost:1984/api/streams?src=camera1&dst=rtmps://... ``` Or config file: ```yaml publish: # publish stream "video_audio_transcode" to Telegram video_audio_transcode: - rtmps://xxx-x.rtmp.t.me/s/xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxx # publish stream "audio_transcode" to Telegram and YouTube audio_transcode: - rtmps://xxx-x.rtmp.t.me/s/xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxx - rtmp://xxx.rtmp.youtube.com/live2/xxxx-xxxx-xxxx-xxxx-xxxx streams: video_audio_transcode: - ffmpeg:rtsp://user:pass@192.168.1.123/stream1#video=h264#hardware#audio=aac audio_transcode: - ffmpeg:rtsp://user:pass@192.168.1.123/stream1#video=copy#audio=aac ``` - **Telegram Desktop App** > Any public or private channel or group (where you admin) > Live stream > Start with... > Start streaming. - **YouTube** > Create > Go live > Stream latency: Ultra low-latency > Copy: Stream URL + Stream key. ## Preload stream [`new in v1.9.11`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.11) You can preload any stream on go2rtc start. This is useful for cameras that take a long time to start up. ```yaml preload: camera1: # default: video&audio = ANY camera2: "video" # preload only video track camera3: "video=h264&audio=opus" # preload H264 video and OPUS audio streams: camera1: - rtsp://192.168.1.100/stream camera2: - rtsp://192.168.1.101/stream camera3: - rtsp://192.168.1.102/h265stream - ffmpeg:camera3#video=h264#audio=opus#hardware ``` ## Examples ```yaml streams: # known RTSP sources rtsp-dahua1: rtsp://admin:password@192.168.10.90/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif rtsp-dahua2: rtsp://admin:password@192.168.10.90/cam/realmonitor?channel=1&subtype=1 rtsp-tplink1: rtsp://admin:password@192.168.10.91/stream1 rtsp-tplink2: rtsp://admin:password@192.168.10.91/stream2 rtsp-reolink1: rtsp://admin:password@192.168.10.92/h264Preview_01_main rtsp-reolink2: rtsp://admin:password@192.168.10.92/h264Preview_01_sub rtsp-sonoff1: rtsp://admin:password@192.168.10.93/av_stream/ch0 rtsp-sonoff2: rtsp://admin:password@192.168.10.93/av_stream/ch1 # known RTMP sources rtmp-reolink1: rtmp://192.168.10.92/bcs/channel0_main.bcs?channel=0&stream=0&user=admin&password=password rtmp-reolink2: rtmp://192.168.10.92/bcs/channel0_sub.bcs?channel=0&stream=1&user=admin&password=password rtmp-reolink3: rtmp://192.168.10.92/bcs/channel0_ext.bcs?channel=0&stream=1&user=admin&password=password # known HTTP sources http-reolink1: http://192.168.10.92/flv?port=1935&app=bcs&stream=channel0_main.bcs&user=admin&password=password http-reolink2: http://192.168.10.92/flv?port=1935&app=bcs&stream=channel0_sub.bcs&user=admin&password=password http-reolink3: http://192.168.10.92/flv?port=1935&app=bcs&stream=channel0_ext.bcs&user=admin&password=password # known ONVIF sources onvif-dahua1: onvif://admin:password@192.168.10.90?subtype=MediaProfile00000 onvif-dahua2: onvif://admin:password@192.168.10.90?subtype=MediaProfile00001 onvif-dahua3: onvif://admin:password@192.168.10.90?subtype=MediaProfile00000&snapshot onvif-tplink1: onvif://admin:password@192.168.10.91:2020?subtype=profile_1 onvif-tplink2: onvif://admin:password@192.168.10.91:2020?subtype=profile_2 onvif-reolink1: onvif://admin:password@192.168.10.92:8000?subtype=000 onvif-reolink2: onvif://admin:password@192.168.10.92:8000?subtype=001 onvif-reolink3: onvif://admin:password@192.168.10.92:8000?subtype=000&snapshot onvif-openipc1: onvif://admin:password@192.168.10.95:80?subtype=PROFILE_000 onvif-openipc2: onvif://admin:password@192.168.10.95:80?subtype=PROFILE_001 # some EXEC examples exec-h264-pipe: exec:ffmpeg -re -i bbb.mp4 -c copy -f h264 - exec-flv-pipe: exec:ffmpeg -re -i bbb.mp4 -c copy -f flv - exec-mpegts-pipe: exec:ffmpeg -re -i bbb.mp4 -c copy -f mpegts - exec-adts-pipe: exec:ffmpeg -re -i bbb.mp4 -c copy -f adts - exec-mjpeg-pipe: exec:ffmpeg -re -i bbb.mp4 -c mjpeg -f mjpeg - exec-hevc-pipe: exec:ffmpeg -re -i bbb.mp4 -c libx265 -preset superfast -tune zerolatency -f hevc - exec-wav-pipe: exec:ffmpeg -re -i bbb.mp4 -c pcm_alaw -ar 8000 -ac 1 -f wav - exec-y4m-pipe: exec:ffmpeg -re -i bbb.mp4 -c rawvideo -f yuv4mpegpipe - exec-pcma-pipe: exec:ffmpeg -re -i numb.mp3 -c:a pcm_alaw -ar:a 8000 -ac:a 1 -f wav - exec-pcmu-pipe: exec:ffmpeg -re -i numb.mp3 -c:a pcm_mulaw -ar:a 8000 -ac:a 1 -f wav - exec-s16le-pipe: exec:ffmpeg -re -i numb.mp3 -c:a pcm_s16le -ar:a 16000 -ac:a 1 -f wav - # some FFmpeg examples ffmpeg-video-h264: ffmpeg:virtual?video#video=h264 ffmpeg-video-4K: ffmpeg:virtual?video&size=4K#video=h264 ffmpeg-video-10s: ffmpeg:virtual?video&duration=10#video=h264 ffmpeg-video-src2: ffmpeg:virtual?video=testsrc2&size=2K#video=h264 ``` ================================================ FILE: internal/streams/add_consumer.go ================================================ package streams import ( "errors" "strings" "github.com/AlexxIT/go2rtc/pkg/core" ) func (s *Stream) AddConsumer(cons core.Consumer) (err error) { // support for multiple simultaneous pending from different consumers consN := s.pending.Add(1) - 1 var prodErrors = make([]error, len(s.producers)) var prodMedias []*core.Media var prodStarts []*Producer // Step 1. Get consumer medias consMedias := cons.GetMedias() for _, consMedia := range consMedias { log.Trace().Msgf("[streams] check cons=%d media=%s", consN, consMedia) producers: for prodN, prod := range s.producers { // check for loop request, ex. `camera1: ffmpeg:camera1` if info, ok := cons.(core.Info); ok && prod.url == info.GetSource() { log.Trace().Msgf("[streams] skip cons=%d prod=%d", consN, prodN) continue } if prodErrors[prodN] != nil { log.Trace().Msgf("[streams] skip cons=%d prod=%d", consN, prodN) continue } if err = prod.Dial(); err != nil { log.Trace().Err(err).Msgf("[streams] dial cons=%d prod=%d", consN, prodN) prodErrors[prodN] = err continue } // Step 2. Get producer medias (not tracks yet) for _, prodMedia := range prod.GetMedias() { log.Trace().Msgf("[streams] check cons=%d prod=%d media=%s", consN, prodN, prodMedia) prodMedias = append(prodMedias, prodMedia) // Step 3. Match consumer/producer codecs list prodCodec, consCodec := prodMedia.MatchMedia(consMedia) if prodCodec == nil { continue } var track *core.Receiver switch prodMedia.Direction { case core.DirectionRecvonly: log.Trace().Msgf("[streams] match cons=%d <= prod=%d", consN, prodN) // Step 4. Get recvonly track from producer if track, err = prod.GetTrack(prodMedia, prodCodec); err != nil { log.Info().Err(err).Msg("[streams] can't get track") prodErrors[prodN] = err continue } // Step 5. Add track to consumer if err = cons.AddTrack(consMedia, consCodec, track); err != nil { log.Info().Err(err).Msg("[streams] can't add track") continue } case core.DirectionSendonly: log.Trace().Msgf("[streams] match cons=%d => prod=%d", consN, prodN) // Step 4. Get recvonly track from consumer (backchannel) if track, err = cons.(core.Producer).GetTrack(consMedia, consCodec); err != nil { log.Info().Err(err).Msg("[streams] can't get track") continue } // Step 5. Add track to producer if err = prod.AddTrack(prodMedia, prodCodec, track); err != nil { log.Info().Err(err).Msg("[streams] can't add track") prodErrors[prodN] = err continue } } prodStarts = append(prodStarts, prod) if !consMedia.MatchAll() { break producers } } } } // stop producers if they don't have readers if s.pending.Add(-1) == 0 { s.stopProducers() } if len(prodStarts) == 0 { return formatError(consMedias, prodMedias, prodErrors) } s.mu.Lock() s.consumers = append(s.consumers, cons) s.mu.Unlock() // there may be duplicates, but that's not a problem for _, prod := range prodStarts { prod.start() } return nil } func formatError(consMedias, prodMedias []*core.Media, prodErrors []error) error { // 1. Return errors if any not nil var text string for _, err := range prodErrors { if err != nil { text = appendString(text, err.Error()) } } if len(text) != 0 { return errors.New("streams: " + text) } // 2. Return "codecs not matched" if prodMedias != nil { var prod, cons string for _, media := range prodMedias { if media.Direction == core.DirectionRecvonly { for _, codec := range media.Codecs { prod = appendString(prod, media.Kind+":"+codec.PrintName()) } } } for _, media := range consMedias { if media.Direction == core.DirectionSendonly { for _, codec := range media.Codecs { cons = appendString(cons, media.Kind+":"+codec.PrintName()) } } } return errors.New("streams: codecs not matched: " + prod + " => " + cons) } // 3. Return unknown error return errors.New("streams: unknown error") } func appendString(s, elem string) string { if strings.Contains(s, elem) { return s } if len(s) == 0 { return elem } return s + ", " + elem } ================================================ FILE: internal/streams/api.go ================================================ package streams import ( "net/http" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/creds" "github.com/AlexxIT/go2rtc/pkg/probe" ) func apiStreams(w http.ResponseWriter, r *http.Request) { w = creds.SecretResponse(w) query := r.URL.Query() src := query.Get("src") // without source - return all streams list if src == "" && r.Method != "POST" { api.ResponseJSON(w, streams) return } // Not sure about all this API. Should be rewrited... switch r.Method { case "GET": stream := Get(src) if stream == nil { http.Error(w, "", http.StatusNotFound) return } cons := probe.Create("probe", query) if len(cons.Medias) != 0 { cons.WithRequest(r) if err := stream.AddConsumer(cons); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } api.ResponsePrettyJSON(w, stream) stream.RemoveConsumer(cons) } else { api.ResponsePrettyJSON(w, streams[src]) } case "PUT": name := query.Get("name") if name == "" { name = src } if _, err := New(name, query["src"]...); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } if err := app.PatchConfig([]string{"streams", name}, query["src"]); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } case "PATCH": name := query.Get("name") if name == "" { http.Error(w, "", http.StatusBadRequest) return } // support {input} templates: https://github.com/AlexxIT/go2rtc#module-hass if _, err := Patch(name, src); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } case "POST": // with dst - redirect source to dst if dst := query.Get("dst"); dst != "" { if stream := Get(dst); stream != nil { if err := Validate(src); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } else if err = stream.Play(src); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } else { api.ResponseJSON(w, stream) } } else if stream = Get(src); stream != nil { if err := Validate(dst); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } else if err = stream.Publish(dst); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } else { http.Error(w, "", http.StatusNotFound) } } else { http.Error(w, "", http.StatusBadRequest) } case "DELETE": delete(streams, src) if err := app.PatchConfig([]string{"streams", src}, nil); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) } } } func apiStreamsDOT(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() dot := make([]byte, 0, 1024) dot = append(dot, "digraph {\n"...) if query.Has("src") { for _, name := range query["src"] { if stream := streams[name]; stream != nil { dot = AppendDOT(dot, stream) } } } else { for _, stream := range streams { dot = AppendDOT(dot, stream) } } dot = append(dot, '}') dot = []byte(creds.SecretString(string(dot))) api.Response(w, dot, "text/vnd.graphviz") } func apiPreload(w http.ResponseWriter, r *http.Request) { // GET - return all preloads if r.Method == "GET" { api.ResponseJSON(w, GetPreloads()) return } query := r.URL.Query() src := query.Get("src") switch r.Method { case "PUT": // it's safe to delete from map while iterating for k := range query { switch k { case core.KindVideo, core.KindAudio, "microphone": default: delete(query, k) } } rawQuery := query.Encode() if err := AddPreload(src, rawQuery); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := app.PatchConfig([]string{"preload", src}, rawQuery); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } case "DELETE": if err := DelPreload(src); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if err := app.PatchConfig([]string{"preload", src}, nil); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } default: http.Error(w, "", http.StatusMethodNotAllowed) } } func apiSchemes(w http.ResponseWriter, r *http.Request) { api.ResponseJSON(w, SupportedSchemes()) } ================================================ FILE: internal/streams/api_test.go ================================================ package streams import ( "encoding/json" "net/http" "net/http/httptest" "testing" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/stretchr/testify/require" ) func TestApiSchemes(t *testing.T) { // Setup: Register some test handlers and redirects HandleFunc("rtsp", func(url string) (core.Producer, error) { return nil, nil }) HandleFunc("rtmp", func(url string) (core.Producer, error) { return nil, nil }) RedirectFunc("http", func(url string) (string, error) { return "", nil }) t.Run("GET request returns schemes", func(t *testing.T) { req := httptest.NewRequest("GET", "/api/schemes", nil) w := httptest.NewRecorder() apiSchemes(w, req) require.Equal(t, http.StatusOK, w.Code) require.Equal(t, "application/json", w.Header().Get("Content-Type")) var schemes []string err := json.Unmarshal(w.Body.Bytes(), &schemes) require.NoError(t, err) require.NotEmpty(t, schemes) // Check that our test schemes are in the response require.Contains(t, schemes, "rtsp") require.Contains(t, schemes, "rtmp") require.Contains(t, schemes, "http") }) } func TestApiSchemesNoDuplicates(t *testing.T) { // Setup: Register a scheme in both handlers and redirects HandleFunc("duplicate", func(url string) (core.Producer, error) { return nil, nil }) RedirectFunc("duplicate", func(url string) (string, error) { return "", nil }) req := httptest.NewRequest("GET", "/api/schemes", nil) w := httptest.NewRecorder() apiSchemes(w, req) require.Equal(t, http.StatusOK, w.Code) var schemes []string err := json.Unmarshal(w.Body.Bytes(), &schemes) require.NoError(t, err) // Count occurrences of "duplicate" count := 0 for _, scheme := range schemes { if scheme == "duplicate" { count++ } } // Should only appear once require.Equal(t, 1, count, "scheme 'duplicate' should appear exactly once") } ================================================ FILE: internal/streams/dot.go ================================================ package streams import ( "encoding/json" "fmt" "strings" ) func AppendDOT(dot []byte, stream *Stream) []byte { for _, prod := range stream.producers { if prod.conn == nil { continue } c, err := marshalConn(prod.conn) if err != nil { continue } dot = c.appendDOT(dot, "producer") } for _, cons := range stream.consumers { c, err := marshalConn(cons) if err != nil { continue } dot = c.appendDOT(dot, "consumer") } return dot } func marshalConn(v any) (*conn, error) { b, err := json.Marshal(v) if err != nil { return nil, err } var c conn if err = json.Unmarshal(b, &c); err != nil { return nil, err } return &c, nil } const bytesK = "KMGTP" func humanBytes(i int) string { if i < 1000 { return fmt.Sprintf("%d B", i) } f := float64(i) / 1000 var n uint8 for f >= 1000 && n < 5 { f /= 1000 n++ } return fmt.Sprintf("%.2f %cB", f, bytesK[n]) } type node struct { ID uint32 `json:"id"` Codec map[string]any `json:"codec"` Parent uint32 `json:"parent"` Childs []uint32 `json:"childs"` Bytes int `json:"bytes"` //Packets uint32 `json:"packets"` //Drops uint32 `json:"drops"` } var codecKeys = []string{"codec_name", "sample_rate", "channels", "profile", "level"} func (n *node) name() string { if name, ok := n.Codec["codec_name"].(string); ok { return name } return "unknown" } func (n *node) codec() []byte { b := make([]byte, 0, 128) for _, k := range codecKeys { if v := n.Codec[k]; v != nil { b = fmt.Appendf(b, "%s=%v\n", k, v) } } if l := len(b); l > 0 { return b[:l-1] } return b } func (n *node) appendDOT(dot []byte, group string) []byte { dot = fmt.Appendf(dot, "%d [group=%s, label=%q, title=%q];\n", n.ID, group, n.name(), n.codec()) //for _, sink := range n.Childs { // dot = fmt.Appendf(dot, "%d -> %d;\n", n.ID, sink) //} return dot } type conn struct { ID uint32 `json:"id"` FormatName string `json:"format_name"` Protocol string `json:"protocol"` RemoteAddr string `json:"remote_addr"` Source string `json:"source"` URL string `json:"url"` UserAgent string `json:"user_agent"` Receivers []node `json:"receivers"` Senders []node `json:"senders"` BytesRecv int `json:"bytes_recv"` BytesSend int `json:"bytes_send"` } func (c *conn) appendDOT(dot []byte, group string) []byte { host := c.host() dot = fmt.Appendf(dot, "%s [group=host];\n", host) dot = fmt.Appendf(dot, "%d [group=%s, label=%q, title=%q];\n", c.ID, group, c.FormatName, c.label()) if group == "producer" { dot = fmt.Appendf(dot, "%s -> %d [label=%q];\n", host, c.ID, humanBytes(c.BytesRecv)) } else { dot = fmt.Appendf(dot, "%d -> %s [label=%q];\n", c.ID, host, humanBytes(c.BytesSend)) } for _, recv := range c.Receivers { dot = fmt.Appendf(dot, "%d -> %d [label=%q];\n", c.ID, recv.ID, humanBytes(recv.Bytes)) dot = recv.appendDOT(dot, "node") } for _, send := range c.Senders { dot = fmt.Appendf(dot, "%d -> %d [label=%q];\n", send.Parent, c.ID, humanBytes(send.Bytes)) //dot = fmt.Appendf(dot, "%d -> %d [label=%q];\n", send.ID, c.ID, humanBytes(send.Bytes)) //dot = send.appendDOT(dot, "node") } return dot } func (c *conn) host() (s string) { if c.Protocol == "pipe" { return "127.0.0.1" } if s = c.RemoteAddr; s == "" { return "unknown" } if i := strings.Index(s, "forwarded"); i > 0 { s = s[i+10:] } if s[0] == '[' { if i := strings.Index(s, "]"); i > 0 { return s[1:i] } } if i := strings.IndexAny(s, " ,:"); i > 0 { return s[:i] } return } func (c *conn) label() string { var sb strings.Builder sb.WriteString("format_name=" + c.FormatName) if c.Protocol != "" { sb.WriteString("\nprotocol=" + c.Protocol) } if c.Source != "" { sb.WriteString("\nsource=" + c.Source) } if c.URL != "" { sb.WriteString("\nurl=" + c.URL) } if c.UserAgent != "" { sb.WriteString("\nuser_agent=" + c.UserAgent) } // escape quotes https://github.com/AlexxIT/go2rtc/issues/1603 return strings.ReplaceAll(sb.String(), `"`, `'`) } ================================================ FILE: internal/streams/handlers.go ================================================ package streams import ( "errors" "regexp" "strings" "github.com/AlexxIT/go2rtc/pkg/core" ) type Handler func(source string) (core.Producer, error) var handlers = map[string]Handler{} func HandleFunc(scheme string, handler Handler) { handlers[scheme] = handler } func SupportedSchemes() []string { uniqueKeys := make(map[string]struct{}, len(handlers)+len(redirects)) for scheme := range handlers { uniqueKeys[scheme] = struct{}{} } for scheme := range redirects { uniqueKeys[scheme] = struct{}{} } resultKeys := make([]string, 0, len(uniqueKeys)) for key := range uniqueKeys { resultKeys = append(resultKeys, key) } return resultKeys } func HasProducer(url string) bool { if i := strings.IndexByte(url, ':'); i > 0 { scheme := url[:i] if _, ok := handlers[scheme]; ok { return true } if _, ok := redirects[scheme]; ok { return true } } return false } func GetProducer(url string) (core.Producer, error) { if i := strings.IndexByte(url, ':'); i > 0 { scheme := url[:i] if redirect, ok := redirects[scheme]; ok { location, err := redirect(url) if err != nil { return nil, err } if location != "" { return GetProducer(location) } } if handler, ok := handlers[scheme]; ok { return handler(url) } } return nil, errors.New("streams: unsupported scheme: " + url) } // Redirect can return: location URL or error or empty URL and error type Redirect func(url string) (string, error) var redirects = map[string]Redirect{} func RedirectFunc(scheme string, redirect Redirect) { redirects[scheme] = redirect } func Location(url string) (string, error) { if i := strings.IndexByte(url, ':'); i > 0 { scheme := url[:i] if redirect, ok := redirects[scheme]; ok { return redirect(url) } } return "", nil } // TODO: rework type ConsumerHandler func(url string) (core.Consumer, func(), error) var consumerHandlers = map[string]ConsumerHandler{} func HandleConsumerFunc(scheme string, handler ConsumerHandler) { consumerHandlers[scheme] = handler } func GetConsumer(url string) (core.Consumer, func(), error) { if i := strings.IndexByte(url, ':'); i > 0 { scheme := url[:i] if handler, ok := consumerHandlers[scheme]; ok { return handler(url) } } return nil, nil, errors.New("streams: unsupported scheme: " + url) } var insecure = map[string]bool{} func MarkInsecure(scheme string) { insecure[scheme] = true } var sanitize = regexp.MustCompile(`\s`) func Validate(source string) error { // TODO: Review the entire logic of insecure sources if i := strings.IndexByte(source, ':'); i > 0 { if insecure[source[:i]] { return errors.New("streams: source from insecure producer") } } if sanitize.MatchString(source) { return errors.New("streams: source with spaces may be insecure") } return nil } ================================================ FILE: internal/streams/helpers.go ================================================ package streams import ( "net/url" "strings" ) func ParseQuery(s string) url.Values { if len(s) == 0 { return nil } params := url.Values{} for _, key := range strings.Split(s, "#") { var value string i := strings.IndexByte(key, '=') if i > 0 { key, value = key[:i], key[i+1:] } params[key] = append(params[key], value) } return params } ================================================ FILE: internal/streams/play.go ================================================ package streams import ( "errors" "time" "github.com/AlexxIT/go2rtc/pkg/core" ) func (s *Stream) Play(urlOrProd any) error { s.mu.Lock() for _, producer := range s.producers { if producer.state == stateInternal && producer.conn != nil { _ = producer.conn.Stop() } } s.mu.Unlock() var source string var src core.Producer switch urlOrProd.(type) { case string: if source = urlOrProd.(string); source == "" { return nil } case core.Producer: src = urlOrProd.(core.Producer) } for _, producer := range s.producers { if producer.conn == nil { continue } cons, ok := producer.conn.(core.Consumer) if !ok { continue } if src == nil { var err error if src, err = GetProducer(source); err != nil { return err } } if !matchMedia(src, cons) { continue } s.AddInternalProducer(src) go func() { _ = src.Start() s.RemoveProducer(src) }() return nil } for _, producer := range s.producers { // start new client dst, err := GetProducer(producer.url) if err != nil { continue } // check if client support consumer interface cons, ok := dst.(core.Consumer) if !ok { _ = dst.Stop() continue } // start new producer if src == nil { if src, err = GetProducer(source); err != nil { return err } } if !matchMedia(src, cons) { _ = dst.Stop() continue } s.AddInternalProducer(src) s.AddInternalConsumer(cons) go func() { _ = dst.Start() _ = src.Stop() s.RemoveInternalConsumer(cons) }() go func() { _ = src.Start() // little timeout before stop dst, so the buffer can be transferred time.Sleep(time.Second) _ = dst.Stop() s.RemoveProducer(src) }() return nil } return errors.New("can't find consumer") } func (s *Stream) AddInternalProducer(conn core.Producer) { producer := &Producer{conn: conn, state: stateInternal, url: "internal"} s.mu.Lock() s.producers = append(s.producers, producer) s.mu.Unlock() } func (s *Stream) AddInternalConsumer(conn core.Consumer) { s.mu.Lock() s.consumers = append(s.consumers, conn) s.mu.Unlock() } func (s *Stream) RemoveInternalConsumer(conn core.Consumer) { s.mu.Lock() for i, consumer := range s.consumers { if consumer == conn { s.consumers = append(s.consumers[:i], s.consumers[i+1:]...) break } } s.mu.Unlock() } func matchMedia(prod core.Producer, cons core.Consumer) bool { for _, consMedia := range cons.GetMedias() { for _, prodMedia := range prod.GetMedias() { if prodMedia.Direction != core.DirectionRecvonly { continue } prodCodec, consCodec := prodMedia.MatchMedia(consMedia) if prodCodec == nil { continue } track, err := prod.GetTrack(prodMedia, prodCodec) if err != nil { log.Warn().Err(err).Msg("[streams] can't get track") continue } if err = cons.AddTrack(consMedia, consCodec, track); err != nil { log.Warn().Err(err).Msg("[streams] can't add track") continue } return true } } return false } ================================================ FILE: internal/streams/preload.go ================================================ package streams import ( "fmt" "maps" "net/url" "sync" "github.com/AlexxIT/go2rtc/pkg/probe" ) type Preload struct { stream *Stream // Don't include the stream in JSON to avoid leaking secrets. Cons *probe.Probe `json:"consumer"` Query string `json:"query"` } var preloads = map[string]*Preload{} var preloadsMu sync.Mutex func AddPreload(name, rawQuery string) error { if rawQuery == "" { rawQuery = "video&audio" } query, err := url.ParseQuery(rawQuery) if err != nil { return err } preloadsMu.Lock() defer preloadsMu.Unlock() if p := preloads[name]; p != nil { p.stream.RemoveConsumer(p.Cons) } stream := Get(name) if stream == nil { return fmt.Errorf("streams: stream not found: %s", name) } cons := probe.Create("preload", query) if err = stream.AddConsumer(cons); err != nil { return err } preloads[name] = &Preload{stream: stream, Cons: cons, Query: rawQuery} return nil } func DelPreload(name string) error { preloadsMu.Lock() defer preloadsMu.Unlock() if p := preloads[name]; p != nil { p.stream.RemoveConsumer(p.Cons) delete(preloads, name) return nil } return fmt.Errorf("streams: preload not found: %s", name) } func GetPreloads() map[string]*Preload { preloadsMu.Lock() defer preloadsMu.Unlock() return maps.Clone(preloads) } ================================================ FILE: internal/streams/producer.go ================================================ package streams import ( "encoding/json" "errors" "strings" "sync" "time" "github.com/AlexxIT/go2rtc/pkg/core" ) type state byte const ( stateNone state = iota stateMedias stateTracks stateStart stateExternal stateInternal ) type Producer struct { core.Listener url string template string conn core.Producer receivers []*core.Receiver senders []*core.Receiver state state mu sync.Mutex workerID int } const SourceTemplate = "{input}" func NewProducer(source string) *Producer { if strings.Contains(source, SourceTemplate) { return &Producer{template: source} } return &Producer{url: source} } func (p *Producer) SetSource(s string) { if p.template == "" { p.url = s } else { p.url = strings.Replace(p.template, SourceTemplate, s, 1) } } func (p *Producer) Dial() error { p.mu.Lock() defer p.mu.Unlock() if p.state == stateNone { conn, err := GetProducer(p.url) if err != nil { return err } p.conn = conn p.state = stateMedias } return nil } func (p *Producer) GetMedias() []*core.Media { p.mu.Lock() defer p.mu.Unlock() if p.conn == nil { return nil } return p.conn.GetMedias() } func (p *Producer) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { p.mu.Lock() defer p.mu.Unlock() if p.state == stateNone { return nil, errors.New("get track from none state") } for _, track := range p.receivers { if track.Codec == codec { return track, nil } } track, err := p.conn.GetTrack(media, codec) if err != nil { return nil, err } p.receivers = append(p.receivers, track) if p.state == stateMedias { p.state = stateTracks } return track, nil } func (p *Producer) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { p.mu.Lock() defer p.mu.Unlock() if p.state == stateNone { return errors.New("add track from none state") } if err := p.conn.(core.Consumer).AddTrack(media, codec, track); err != nil { return err } p.senders = append(p.senders, track) if p.state == stateMedias { p.state = stateTracks } return nil } func (p *Producer) MarshalJSON() ([]byte, error) { if conn := p.conn; conn != nil { return json.Marshal(conn) } info := map[string]string{"url": p.url} return json.Marshal(info) } // internals func (p *Producer) start() { p.mu.Lock() defer p.mu.Unlock() if p.state != stateTracks { return } log.Debug().Msgf("[streams] start producer url=%s", p.url) p.state = stateStart p.workerID++ go p.worker(p.conn, p.workerID) } func (p *Producer) worker(conn core.Producer, workerID int) { if err := conn.Start(); err != nil { p.mu.Lock() closed := p.workerID != workerID p.mu.Unlock() if closed { return } log.Warn().Err(err).Str("url", p.url).Caller().Send() } p.reconnect(workerID, 0) } func (p *Producer) reconnect(workerID, retry int) { p.mu.Lock() defer p.mu.Unlock() if p.workerID != workerID { log.Trace().Msgf("[streams] stop reconnect url=%s", p.url) return } log.Debug().Msgf("[streams] retry=%d to url=%s", retry, p.url) conn, err := GetProducer(p.url) if err != nil { log.Debug().Msgf("[streams] producer=%s", err) timeout := time.Minute if retry < 5 { timeout = time.Second } else if retry < 10 { timeout = time.Second * 5 } else if retry < 20 { timeout = time.Second * 10 } time.AfterFunc(timeout, func() { p.reconnect(workerID, retry+1) }) return } for _, media := range conn.GetMedias() { switch media.Direction { case core.DirectionRecvonly: for i, receiver := range p.receivers { codec := media.MatchCodec(receiver.Codec) if codec == nil { continue } track, err := conn.GetTrack(media, codec) if err != nil { continue } receiver.Replace(track) p.receivers[i] = track break } case core.DirectionSendonly: for _, sender := range p.senders { codec := media.MatchCodec(sender.Codec) if codec == nil { continue } _ = conn.(core.Consumer).AddTrack(media, codec, sender) } } } // stop previous connection after moving tracks (fix ghost exec/ffmpeg) _ = p.conn.Stop() // swap connections p.conn = conn go p.worker(conn, workerID) } func (p *Producer) stop() { p.mu.Lock() defer p.mu.Unlock() switch p.state { case stateExternal: log.Trace().Msgf("[streams] skip stop external producer") return case stateNone: log.Trace().Msgf("[streams] skip stop none producer") return case stateStart: p.workerID++ } log.Debug().Msgf("[streams] stop producer url=%s", p.url) if p.conn != nil { _ = p.conn.Stop() p.conn = nil } p.state = stateNone p.receivers = nil p.senders = nil } ================================================ FILE: internal/streams/publish.go ================================================ package streams import "time" func (s *Stream) Publish(url string) error { cons, run, err := GetConsumer(url) if err != nil { return err } if err = s.AddConsumer(cons); err != nil { return err } go func() { run() s.RemoveConsumer(cons) // TODO: more smart retry time.Sleep(5 * time.Second) _ = s.Publish(url) }() return nil } func Publish(stream *Stream, destination any) { switch v := destination.(type) { case string: if err := stream.Publish(v); err != nil { log.Error().Err(err).Caller().Send() } case []any: for _, v := range v { Publish(stream, v) } } } ================================================ FILE: internal/streams/stream.go ================================================ package streams import ( "encoding/json" "sync" "sync/atomic" "github.com/AlexxIT/go2rtc/pkg/core" ) type Stream struct { producers []*Producer consumers []core.Consumer mu sync.Mutex pending atomic.Int32 } func NewStream(source any) *Stream { switch source := source.(type) { case string: return &Stream{ producers: []*Producer{NewProducer(source)}, } case []string: s := new(Stream) for _, str := range source { s.producers = append(s.producers, NewProducer(str)) } return s case []any: s := new(Stream) for _, src := range source { str, ok := src.(string) if !ok { log.Error().Msgf("[stream] NewStream: Expected string, got %v", src) continue } s.producers = append(s.producers, NewProducer(str)) } return s case map[string]any: return NewStream(source["url"]) case nil: return new(Stream) default: panic(core.Caller()) } } func (s *Stream) Sources() []string { sources := make([]string, 0, len(s.producers)) for _, prod := range s.producers { sources = append(sources, prod.url) } return sources } func (s *Stream) SetSource(source string) { for _, prod := range s.producers { prod.SetSource(source) } } func (s *Stream) RemoveConsumer(cons core.Consumer) { _ = cons.Stop() s.mu.Lock() for i, consumer := range s.consumers { if consumer == cons { s.consumers = append(s.consumers[:i], s.consumers[i+1:]...) break } } s.mu.Unlock() s.stopProducers() } func (s *Stream) AddProducer(prod core.Producer) { producer := &Producer{conn: prod, state: stateExternal, url: "external"} s.mu.Lock() s.producers = append(s.producers, producer) s.mu.Unlock() } func (s *Stream) RemoveProducer(prod core.Producer) { s.mu.Lock() for i, producer := range s.producers { if producer.conn == prod { s.producers = append(s.producers[:i], s.producers[i+1:]...) break } } s.mu.Unlock() } func (s *Stream) stopProducers() { if s.pending.Load() > 0 { log.Trace().Msg("[streams] skip stop pending producer") return } s.mu.Lock() producers: for _, producer := range s.producers { for _, track := range producer.receivers { if len(track.Senders()) > 0 { continue producers } } for _, track := range producer.senders { if len(track.Senders()) > 0 { continue producers } } producer.stop() } s.mu.Unlock() } func (s *Stream) MarshalJSON() ([]byte, error) { var info = struct { Producers []*Producer `json:"producers"` Consumers []core.Consumer `json:"consumers"` }{ Producers: s.producers, Consumers: s.consumers, } return json.Marshal(info) } ================================================ FILE: internal/streams/stream_test.go ================================================ package streams import ( "net/url" "testing" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/stretchr/testify/require" ) func TestRecursion(t *testing.T) { // create stream with some source stream1, err := New("from_yaml", "does_not_matter") require.NoError(t, err) require.Len(t, streams, 1) // ask another unnamed stream that links go2rtc query, err := url.ParseQuery("src=rtsp://localhost:8554/from_yaml?video") require.NoError(t, err) stream2, err := GetOrPatch(query) require.NoError(t, err) // check stream is same require.Equal(t, stream1, stream2) // check stream urls is same require.Equal(t, stream1.producers[0].url, stream2.producers[0].url) require.Len(t, streams, 2) } func TestTempate(t *testing.T) { HandleFunc("rtsp", func(url string) (core.Producer, error) { return nil, nil }) // bypass HasProducer // config from yaml stream1, err := New("camera.from_hass", "ffmpeg:{input}#video=copy") require.NoError(t, err) // request from hass stream2, err := Patch("camera.from_hass", "rtsp://example.com") require.NoError(t, err) require.Equal(t, stream1, stream2) require.Equal(t, "ffmpeg:rtsp://example.com#video=copy", stream1.producers[0].url) } ================================================ FILE: internal/streams/streams.go ================================================ package streams import ( "errors" "net/url" "sync" "time" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/rs/zerolog" ) func Init() { var cfg struct { Streams map[string]any `yaml:"streams"` Publish map[string]any `yaml:"publish"` Preload map[string]string `yaml:"preload"` } app.LoadConfig(&cfg) log = app.GetLogger("streams") for name, item := range cfg.Streams { streams[name] = NewStream(item) } api.HandleFunc("api/streams", apiStreams) api.HandleFunc("api/streams.dot", apiStreamsDOT) api.HandleFunc("api/preload", apiPreload) api.HandleFunc("api/schemes", apiSchemes) if cfg.Publish == nil && cfg.Preload == nil { return } time.AfterFunc(time.Second, func() { // range for nil map is OK for name, dst := range cfg.Publish { if stream := Get(name); stream != nil { Publish(stream, dst) } } for name, rawQuery := range cfg.Preload { if err := AddPreload(name, rawQuery); err != nil { log.Error().Err(err).Caller().Send() } } }) } func New(name string, sources ...string) (*Stream, error) { for _, source := range sources { if !HasProducer(source) { return nil, errors.New("streams: source not supported") } if err := Validate(source); err != nil { return nil, err } } stream := NewStream(sources) streamsMu.Lock() streams[name] = stream streamsMu.Unlock() return stream, nil } func Patch(name string, source string) (*Stream, error) { streamsMu.Lock() defer streamsMu.Unlock() // check if source links to some stream name from go2rtc if u, err := url.Parse(source); err == nil && u.Scheme == "rtsp" && len(u.Path) > 1 { rtspName := u.Path[1:] if stream, ok := streams[rtspName]; ok { if streams[name] != stream { // link (alias) streams[name] to streams[rtspName] streams[name] = stream } return stream, nil } } if stream, ok := streams[source]; ok { if name != source { // link (alias) streams[name] to streams[source] streams[name] = stream } return stream, nil } // check if src has supported scheme if !HasProducer(source) { return nil, errors.New("streams: source not supported") } if err := Validate(source); err != nil { return nil, err } // check an existing stream with this name if stream, ok := streams[name]; ok { stream.SetSource(source) return stream, nil } // create new stream with this name stream := NewStream(source) streams[name] = stream return stream, nil } func GetOrPatch(query url.Values) (*Stream, error) { // check if src param exists source := query.Get("src") if source == "" { return nil, errors.New("streams: source empty") } // check if src is stream name if stream := Get(source); stream != nil { return stream, nil } // check if name param provided if name := query.Get("name"); name != "" { return Patch(name, source) } // return new stream with src as name return Patch(source, source) } var log zerolog.Logger // streams map var streams = map[string]*Stream{} var streamsMu sync.Mutex func Get(name string) *Stream { streamsMu.Lock() defer streamsMu.Unlock() return streams[name] } func Delete(name string) { streamsMu.Lock() defer streamsMu.Unlock() delete(streams, name) } func GetAllNames() []string { streamsMu.Lock() names := make([]string, 0, len(streams)) for name := range streams { names = append(names, name) } streamsMu.Unlock() return names } func GetAllSources() map[string][]string { streamsMu.Lock() sources := make(map[string][]string, len(streams)) for name, stream := range streams { sources[name] = stream.Sources() } streamsMu.Unlock() return sources } ================================================ FILE: internal/tapo/README.md ================================================ # TP-Link Tapo [`new in v1.2.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.2.0) [TP-Link Tapo](https://www.tapo.com/) proprietary camera protocol with **two-way audio** support. - stream quality is the same as [RTSP protocol](https://www.tapo.com/en/faq/34/) - use the **cloud password**, this is not the RTSP password! you do not need to add a login! - you can also use **UPPERCASE** MD5 hash from your cloud password with `admin` username - some new camera firmwares require SHA256 instead of MD5 ## Configuration ```yaml streams: # cloud password without username camera1: tapo://cloud-password@192.168.1.123 # admin username and UPPERCASE MD5 cloud-password hash camera2: tapo://admin:UPPERCASE-MD5@192.168.1.123 # admin username and UPPERCASE SHA256 cloud-password hash camera3: tapo://admin:UPPERCASE-SHA256@192.168.1.123 # VGA stream (the so called substream, the lower resolution one) camera4: tapo://cloud-password@192.168.1.123?subtype=1 # HD stream (default) camera5: tapo://cloud-password@192.168.1.123?subtype=0 ``` ```bash echo -n "cloud password" | md5 | awk '{print toupper($0)}' echo -n "cloud password" | shasum -a 256 | awk '{print toupper($0)}' ``` ## TP-Link Kasa [`new in v1.7.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.7.0) > [!NOTE] > This source should be moved to separate module. Because it's source code not related to Tapo. [TP-Link Kasa](https://www.kasasmart.com/) non-standard protocol [more info](https://medium.com/@hu3vjeen/reverse-engineering-tp-link-kc100-bac4641bf1cd). - `username` - urlsafe email, `alex@gmail.com` -> `alex%40gmail.com` - `password` - base64password, `secret1` -> `c2VjcmV0MQ==` ```yaml streams: kc401: kasa://username:password@192.168.1.123:19443/https/stream/mixed ``` Tested: KD110, KC200, KC401, KC420WS, EC71. ## TP-Link Vigi [`new in v1.9.8`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.8) [TP-Link VIGI](https://www.vigi.com/) cameras. These are cameras from a different sub-brand, but the format is very similar to Tapo. Only the authorization is different. Read more [here](https://github.com/AlexxIT/go2rtc/issues/1470). ```yaml streams: camera1: vigi://admin:{password}@192.168.1.123 ``` ================================================ FILE: internal/tapo/tapo.go ================================================ package tapo import ( "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/kasa" "github.com/AlexxIT/go2rtc/pkg/tapo" ) func Init() { streams.HandleFunc("kasa", func(source string) (core.Producer, error) { return kasa.Dial(source) }) streams.HandleFunc("tapo", func(source string) (core.Producer, error) { return tapo.Dial(source) }) streams.HandleFunc("vigi", func(source string) (core.Producer, error) { return tapo.Dial(source) }) } ================================================ FILE: internal/tuya/README.md ================================================ # Tuya [`new in v1.9.13`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.13) by [@seydx](https://github.com/seydx) [Tuya](https://www.tuya.com/) is a proprietary camera protocol with **two-way audio** support. go2rtc supports `Tuya Smart API` and `Tuya Cloud API`. **Tuya Smart API (recommended)**: - **Smart Life accounts are NOT supported**, you need to create a Tuya Smart account. If the cameras are already added to the Smart Life app, you need to remove them and add them again to the [Tuya Smart](https://play.google.com/store/apps/details?id=com.tuya.smart) app. - Cameras can be discovered through the go2rtc web interface via Tuya Smart account (Add > Tuya > Select region and fill in email and password > Login). **Tuya Cloud API**: - Requires setting up a cloud project in the Tuya Developer Platform. - Obtain `device_id`, `client_id`, `client_secret`, and `uid` from [Tuya IoT Platform](https://iot.tuya.com/). [Here's a guide](https://xzetsubou.github.io/hass-localtuya/cloud_api/). - Please ensure that you have subscribed to the `IoT Video Live Stream` service (Free Trial) in the Tuya Developer Platform, otherwise the stream will not work (Tuya Developer Platform > Service API > Authorize > IoT Video Live Stream). ## Configuration Use the `resolution` parameter to select the stream (not all cameras support an `hd` stream through WebRTC even if the camera supports it): - `hd` - HD stream (default) - `sd` - SD stream ```yaml streams: # Tuya Smart API: WebRTC main stream (use Add > Tuya to discover the URL) tuya_main: - tuya://protect-us.ismartlife.me?device_id=XXX&email=XXX&password=XXX # Tuya Smart API: WebRTC sub stream (use Add > Tuya to discover the URL) tuya_sub: - tuya://protect-us.ismartlife.me?device_id=XXX&email=XXX&password=XXX&resolution=sd # Tuya Cloud API: WebRTC main stream tuya_webrtc: - tuya://openapi.tuyaus.com?device_id=XXX&uid=XXX&client_id=XXX&client_secret=XXX # Tuya Cloud API: WebRTC sub stream tuya_webrtc_sd: - tuya://openapi.tuyaus.com?device_id=XXX&uid=XXX&client_id=XXX&client_secret=XXX&resolution=sd ``` ================================================ FILE: internal/tuya/tuya.go ================================================ package tuya import ( "bytes" "encoding/json" "errors" "fmt" "net/http" "net/url" "strconv" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/tuya" ) func Init() { streams.HandleFunc("tuya", func(source string) (core.Producer, error) { return tuya.Dial(source) }) api.HandleFunc("api/tuya", apiTuya) } func apiTuya(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() region := query.Get("region") email := query.Get("email") password := query.Get("password") if email == "" || password == "" || region == "" { http.Error(w, "email, password and region are required", http.StatusBadRequest) return } var tuyaRegion *tuya.Region for _, r := range tuya.AvailableRegions { if r.Host == region { tuyaRegion = &r break } } if tuyaRegion == nil { http.Error(w, fmt.Sprintf("invalid region: %s", region), http.StatusBadRequest) return } httpClient := tuya.CreateHTTPClientWithSession() _, err := login(httpClient, tuyaRegion.Host, email, password, tuyaRegion.Continent) if err != nil { http.Error(w, fmt.Sprintf("login failed: %v", err), http.StatusInternalServerError) return } tuyaAPI, err := tuya.NewTuyaSmartApiClient( httpClient, tuyaRegion.Host, email, password, "", ) if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var devices []tuya.Device homes, _ := tuyaAPI.GetHomeList() if homes != nil && len(homes.Result) > 0 { for _, home := range homes.Result { roomList, err := tuyaAPI.GetRoomList(strconv.Itoa(home.Gid)) if err != nil { continue } for _, room := range roomList.Result { for _, device := range room.DeviceList { if (device.Category == "sp" || device.Category == "dghsxj") && !containsDevice(devices, device.DeviceId) { devices = append(devices, device) } } } } } sharedHomes, _ := tuyaAPI.GetSharedHomeList() if sharedHomes != nil && len(sharedHomes.Result.SecurityWebCShareInfoList) > 0 { for _, sharedHome := range sharedHomes.Result.SecurityWebCShareInfoList { for _, device := range sharedHome.DeviceInfoList { if (device.Category == "sp" || device.Category == "dghsxj") && !containsDevice(devices, device.DeviceId) { devices = append(devices, device) } } } } if len(devices) == 0 { http.Error(w, "no cameras found", http.StatusNotFound) return } var items []*api.Source for _, device := range devices { cleanQuery := url.Values{} cleanQuery.Set("device_id", device.DeviceId) cleanQuery.Set("email", email) cleanQuery.Set("password", password) url := fmt.Sprintf("tuya://%s?%s", tuyaRegion.Host, cleanQuery.Encode()) items = append(items, &api.Source{ Name: device.DeviceName, URL: url, }) } api.ResponseSources(w, items) } func login(client *http.Client, serverHost, email, password, countryCode string) (*tuya.LoginResult, error) { tokenResp, err := getLoginToken(client, serverHost, email, countryCode) if err != nil { return nil, err } encryptedPassword, err := tuya.EncryptPassword(password, tokenResp.Result.PbKey) if err != nil { return nil, fmt.Errorf("failed to encrypt password: %v", err) } var loginResp *tuya.PasswordLoginResponse var url string loginReq := tuya.PasswordLoginRequest{ CountryCode: countryCode, Passwd: encryptedPassword, Token: tokenResp.Result.Token, IfEncrypt: 1, Options: `{"group":1}`, } if tuya.IsEmailAddress(email) { url = fmt.Sprintf("https://%s/api/private/email/login", serverHost) loginReq.Email = email } else { url = fmt.Sprintf("https://%s/api/private/phone/login", serverHost) loginReq.Mobile = email } loginResp, err = performLogin(client, url, loginReq, serverHost) if err != nil { return nil, err } if !loginResp.Success { return nil, errors.New(loginResp.ErrorMsg) } return &loginResp.Result, nil } func getLoginToken(client *http.Client, serverHost, username, countryCode string) (*tuya.LoginTokenResponse, error) { url := fmt.Sprintf("https://%s/api/login/token", serverHost) tokenReq := tuya.LoginTokenRequest{ CountryCode: countryCode, Username: username, IsUid: false, } jsonData, err := json.Marshal(tokenReq) if err != nil { return nil, err } req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Set("Accept", "*/*") req.Header.Set("Origin", fmt.Sprintf("https://%s", serverHost)) req.Header.Set("Referer", fmt.Sprintf("https://%s/login", serverHost)) req.Header.Set("X-Requested-With", "XMLHttpRequest") resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() var tokenResp tuya.LoginTokenResponse if err = json.NewDecoder(resp.Body).Decode(&tokenResp); err != nil { return nil, err } if !tokenResp.Success { return nil, errors.New("tuya: " + tokenResp.Msg) } return &tokenResp, nil } func performLogin(client *http.Client, url string, loginReq tuya.PasswordLoginRequest, serverHost string) (*tuya.PasswordLoginResponse, error) { jsonData, err := json.Marshal(loginReq) if err != nil { return nil, err } req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Set("Accept", "*/*") req.Header.Set("Origin", fmt.Sprintf("https://%s", serverHost)) req.Header.Set("Referer", fmt.Sprintf("https://%s/login", serverHost)) req.Header.Set("X-Requested-With", "XMLHttpRequest") resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() var loginResp tuya.PasswordLoginResponse if err := json.NewDecoder(resp.Body).Decode(&loginResp); err != nil { return nil, err } return &loginResp, nil } func containsDevice(devices []tuya.Device, deviceID string) bool { for _, device := range devices { if device.DeviceId == deviceID { return true } } return false } ================================================ FILE: internal/v4l2/README.md ================================================ # Video4Linux [`new in v1.9.9`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.9) What you should to know about [V4L2](https://en.wikipedia.org/wiki/Video4Linux): - V4L2 (Video for Linux API version 2) works only in Linux - supports USB cameras and other similar devices - one device can only be connected to one software simultaneously - cameras support a fixed list of formats, resolutions and frame rates - basic cameras supports only RAW (non-compressed) pixel formats - regular cameras supports MJPEG format (series of JPEG frames) - advances cameras support H264 format (MSE/MP4, WebRTC compatible) - using MJPEG and H264 formats (if the camera supports them) won't cost you the CPU usage - transcoding RAW format to MJPEG or H264 - will cost you a significant CPU usage - H265 (HEVC) format is also supported (if the camera supports it) Tests show that the basic Keenetic router with MIPS processor can broadcast three MJPEG cameras in the following resolutions: 1600х1200 + 640х480 + 640х480. The USB bus bandwidth is no more enough for larger resolutions. CPU consumption is no more than 5%. Supported formats for your camera can be found here: **Go2rtc > WebUI > Add > V4L2**. ## RAW format Example: ```yaml streams: camera1: v4l2:device?video=/dev/video0&input_format=yuyv422&video_size=1280x720&framerate=10 ``` Go2rtc supports built-in transcoding of RAW to MJPEG format. This does not need to be additionally configured. ``` ffplay http://localhost:1984/api/stream.mjpeg?src=camera1 ``` **Important.** You don't have to transcode the RAW format to transmit it over the network. You can stream it in `y4m` format, which is perfectly supported by ffmpeg. It won't cost you a CPU usage. But will require high network bandwidth. ``` ffplay http://localhost:1984/api/stream.y4m?src=camera1 ``` ================================================ FILE: internal/v4l2/v4l2.go ================================================ //go:build !(linux && (386 || arm || mipsle || amd64 || arm64)) package v4l2 func Init() { // not supported } ================================================ FILE: internal/v4l2/v4l2_linux.go ================================================ //go:build linux && (386 || arm || mipsle || amd64 || arm64) package v4l2 import ( "encoding/binary" "fmt" "net/http" "os" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/v4l2" "github.com/AlexxIT/go2rtc/pkg/v4l2/device" ) func Init() { streams.HandleFunc("v4l2", func(source string) (core.Producer, error) { return v4l2.Open(source) }) api.HandleFunc("api/v4l2", apiV4L2) } func apiV4L2(w http.ResponseWriter, r *http.Request) { files, err := os.ReadDir("/dev") if err != nil { return } var sources []*api.Source for _, file := range files { if !strings.HasPrefix(file.Name(), core.KindVideo) { continue } path := "/dev/" + file.Name() dev, err := device.Open(path) if err != nil { continue } formats, _ := dev.ListFormats() for _, fourCC := range formats { name, ffmpeg := findFormat(fourCC) source := &api.Source{Name: name} sizes, _ := dev.ListSizes(fourCC) for _, wh := range sizes { if source.Info != "" { source.Info += " " } source.Info += fmt.Sprintf("%dx%d", wh[0], wh[1]) frameRates, _ := dev.ListFrameRates(fourCC, wh[0], wh[1]) for _, fr := range frameRates { source.Info += fmt.Sprintf("@%d", fr) if source.URL == "" && ffmpeg != "" { source.URL = fmt.Sprintf( "v4l2:device?video=%s&input_format=%s&video_size=%dx%d&framerate=%d", path, ffmpeg, wh[0], wh[1], fr, ) } } } if source.Info != "" { sources = append(sources, source) } } _ = dev.Close() } api.ResponseSources(w, sources) } func findFormat(fourCC uint32) (name, ffmpeg string) { for _, format := range device.Formats { if format.FourCC == fourCC { return format.Name, format.FFmpeg } } return string(binary.LittleEndian.AppendUint32(nil, fourCC)), "" } ================================================ FILE: internal/webrtc/README.md ================================================ # WebRTC ## WebRTC Client [`new in v1.3.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.3.0) This source type supports four connection formats. ### Creality [`new in v1.9.10`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.10) [Creality](https://www.creality.com/) 3D printer camera. Read more [here](https://github.com/AlexxIT/go2rtc/issues/1600). ```yaml streams: creality_k2p: webrtc:http://192.168.1.123:8000/call/webrtc_local#format=creality ``` ### go2rtc This format is only supported in go2rtc. Unlike WHEP, it supports asynchronous WebRTC connections and two-way audio. ```yaml streams: webrtc-go2rtc: webrtc:ws://192.168.1.123:1984/api/ws?src=camera1 ``` ### Kinesis [`new in v1.6.1`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.6.1) Supports [Amazon Kinesis Video Streams](https://aws.amazon.com/kinesis/video-streams/), using WebRTC protocol. You need to specify the signaling WebSocket URL with all credentials in query params, `client_id` and `ice_servers` list in [JSON format](https://developer.mozilla.org/en-US/docs/Web/API/RTCIceServer). ```yaml streams: webrtc-kinesis: webrtc:wss://...amazonaws.com/?...#format=kinesis#client_id=...#ice_servers=[{...},{...}] ``` **PS.** For `kinesis` sources, you can use [echo](../echo/README.md) to get connection params using `bash`, `python` or any other script language. ### OpenIPC [`new in v1.7.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.7.0) Cameras on open-source [OpenIPC](https://openipc.org/) firmware. ```yaml streams: webrtc-openipc: webrtc:ws://192.168.1.123/webrtc_ws#format=openipc#ice_servers=[{"urls":"stun:stun.kinesisvideo.eu-north-1.amazonaws.com:443"}] ``` ### SwitchBot Support connection to [SwitchBot](https://us.switch-bot.com/) cameras that are based on Kinesis Video Streams. Specifically, this includes [Pan/Tilt Cam Plus 2K](https://us.switch-bot.com/pages/switchbot-pan-tilt-cam-plus-2k) and [Pan/Tilt Cam Plus 3K](https://us.switch-bot.com/pages/switchbot-pan-tilt-cam-plus-3k) and [Smart Video Doorbell](https://www.switchbot.jp/products/switchbot-smart-video-doorbell). `Outdoor Spotlight Cam 1080P`, `Outdoor Spotlight Cam 2K`, `Pan/Tilt Cam`, `Pan/Tilt Cam 2K`, `Indoor Cam` are based on Tuya, so this feature is not available. ```yaml streams: webrtc-switchbot: webrtc:wss://...amazonaws.com/?...#format=switchbot#resolution=hd#play_type=0#client_id=...#ice_servers=[{...},{...}] ``` ### WHEP [WebRTC/WHEP](https://datatracker.ietf.org/doc/draft-murillo-whep/) is replaced by [WebRTC/WISH](https://datatracker.ietf.org/doc/charter-ietf-wish/02/) standard for WebRTC video/audio viewers. But it may already be supported in some third-party software. It is supported in go2rtc. ```yaml streams: webrtc-whep: webrtc:http://192.168.1.123:1984/api/webrtc?src=camera1 ``` ### Wyze [`new in v1.6.1`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.6.1) Legacy method to connect to [Wyze](https://www.wyze.com/) cameras using WebRTC protocol via [docker-wyze-bridge](https://github.com/mrlt8/docker-wyze-bridge). For native P2P support without docker-wyze-bridge, see [Source: Wyze](../wyze/README.md). ```yaml streams: webrtc-wyze: webrtc:http://192.168.1.123:5000/signaling/camera1?kvs#format=wyze ``` ## WebRTC Server What you should know about WebRTC: - It's almost always a **direct [peer-to-peer](https://en.wikipedia.org/wiki/Peer-to-peer) connection** from your browser to the go2rtc app - When you use Home Assistant, Frigate, Nginx, Nabu Casa, Cloudflare, and other software, they are only **involved in establishing** the connection; they are **not involved in transferring** media data - WebRTC media cannot be transferred inside an HTTP connection - Usually, WebRTC uses random UDP ports on the client and server to establish a connection - Usually, WebRTC uses public [STUN](https://en.wikipedia.org/wiki/STUN) servers to establish a connection outside the LAN; these servers are only needed to establish a connection and are not involved in data transfer - Usually, WebRTC will automatically discover all of your local and public addresses and try to establish a connection If an external connection via STUN is used: - Uses [UDP hole punching](https://en.wikipedia.org/wiki/UDP_hole_punching) technology to bypass NAT even if you haven't opened your server to the world - For about 20% of users, the technology will not work because of the [Symmetric NAT](https://tomchen.github.io/symmetric-nat-test/) - UDP is not suitable for transmitting 2K and 4K high bit rate video over open networks because of the high loss rate: - https://habr.com/ru/companies/flashphoner/articles/480006/ - https://www.youtube.com/watch?v=FXVg2ckuKfs ### Configuration suggestions - by default, WebRTC uses both TCP and UDP on port 8555 for connections - you can use this port for external access - you can change the port in YAML config: ```yaml webrtc: listen: ":8555" # address of your local server and port (TCP/UDP) ``` #### Static public IP - forward the port 8555 on your router (you can use the same 8555 port or any other as external port) - add your external IP address and external port to the YAML config ```yaml webrtc: candidates: - 216.58.210.174:8555 # if you have a static public IP address ``` #### Dynamic public IP - forward the port 8555 on your router (you can use the same 8555 port or any other as the external port) - add `stun` word and external port to YAML config - go2rtc automatically detects your external address with STUN server ```yaml webrtc: candidates: - stun:8555 # if you have a dynamic public IP address ``` #### Hard tech way 1. Own TCP-tunnel If you have a personal [VPS](https://en.wikipedia.org/wiki/Virtual_private_server), you can create a TCP tunnel and setup in the same way as "Static public IP". But use your VPS IP address in the YAML config. #### Hard tech way 2. Using TURN-server If you have personal [VPS](https://en.wikipedia.org/wiki/Virtual_private_server), you can install TURN server (e.g. [coturn](https://github.com/coturn/coturn), config [example](https://github.com/AlexxIT/WebRTC/wiki/Coturn-Example)). ```yaml webrtc: ice_servers: - urls: [stun:stun.l.google.com:19302] - urls: [turn:123.123.123.123:3478] username: your_user credential: your_pass ``` ### Full configuration **Important!** This example is not for copy/pasting! ```yaml webrtc: # fix local TCP or UDP or both ports for WebRTC media listen: ":8555" # address of your local server # add additional host candidates manually # order is important, the first will have a higher priority candidates: - 216.58.210.174:8555 # if you have static public IP-address - stun:8555 # if you have dynamic public IP-address - home.duckdns.org:8555 # if you have domain # add custom STUN and TURN servers # use `ice_servers: []` to remove defaults and leave it empty ice_servers: - urls: [ stun:stun1.l.google.com:19302 ] - urls: [ turn:123.123.123.123:3478 ] username: your_user credential: your_pass # optional filter list for auto-discovery logic # some settings only make sense if you don't specify a fixed UDP port filters: # list of host candidates from auto-discovery to be sent # includes candidates from the `listen` option # use `candidates: []` to remove all auto-discovery candidates candidates: [ 192.168.1.123 ] # enable localhost candidates loopback: true # list of network types to be used for the connection # includes candidates from the `listen` option networks: [ udp4, udp6, tcp4, tcp6 ] # list of interfaces to be used for the connection # includes interfaces from unspecified `listen` option (empty host) interfaces: [ eno1 ] # list of host IP addresses to be used for the connection # includes IPs from unspecified `listen` option (empty host) ips: [ 192.168.1.123 ] # range for random UDP ports [min, max] to be used for connection # not related to the `listen` option udp_ports: [ 50000, 50100 ] ``` By default, go2rtc uses a **fixed TCP** port and **fixed UDP** ports for each **direct** WebRTC connection: `listen: ":8555"`. You can set a **fixed TCP** port and a **random UDP** port for all connections: `listen: ":8555/tcp"`. You can also disable the TCP port and leave only random UDP ports: `listen: ""`. ### Configuration filters **Important!** By default, go2rtc excludes all Docker-like candidates (`172.16.0.0/12`). This cannot be disabled. Filters allow you to exclude unnecessary candidates. Extra candidates don't make your connection worse or better. But the wrong filter settings can break everything. Skip this setting if you don't understand it. For example, go2rtc is installed on the host system. And there are unnecessary interfaces. You can keep only the relevant via `interfaces` or `ips` options. You can also exclude IPv6 candidates if your server supports them but your home network does not. ```yaml webrtc: listen: ":8555/tcp" # use fixed TCP port and random UDP ports filters: ips: [ 192.168.1.2 ] # IP-address of your server networks: [ udp4, tcp4 ] # skip IPv6, if it's not supported for you ``` For example, go2rtc is inside a closed Docker container (e.g. [Frigate](https://frigate.video/)). You shouldn't filter Docker interfaces; otherwise, go2rtc won't be able to connect anywhere. But you can filter the Docker candidates because no one can connect to them. ```yaml webrtc: listen: ":8555" # use fixed TCP and UDP ports candidates: [ 192.168.1.2:8555 ] # add manual host candidate (use docker port forwarding) ``` ## Streaming ingest ### Ingest: Browser [`new in v1.3.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.3.0) You can turn the browser of any PC or mobile into an IP camera with support for video and two-way audio. Or even broadcast your PC screen: 1. Create empty stream in the `go2rtc.yaml` 2. Go to go2rtc WebUI 3. Open `links` page for your stream 4. Select `camera+microphone` or `display+speaker` option 5. Open `webrtc` local page (your go2rtc **should work over HTTPS!**) or `share link` via [WebTorrent](../webtorrent/README.md) technology (work over HTTPS by default) ### Ingest: WHIP [`new in v1.3.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.3.0) You can use **OBS Studio** or any other broadcast software with [WHIP](https://www.ietf.org/archive/id/draft-ietf-wish-whip-01.html) protocol support. This standard has not yet been approved. But you can download OBS Studio [dev version](https://github.com/obsproject/obs-studio/actions/runs/3969201209): - Settings > Stream > Service: WHIP > `http://192.168.1.123:1984/api/webrtc?dst=camera1` ## Useful links - https://www.ietf.org/archive/id/draft-ietf-wish-whip-01.html - https://www.ietf.org/id/draft-murillo-whep-01.html - https://github.com/Glimesh/broadcast-box/ - https://github.com/obsproject/obs-studio/pull/7926 - https://misi.github.io/webrtc-c0d3l4b/ - https://github.com/webtorrent/webtorrent/blob/master/docs/faq.md ================================================ FILE: internal/webrtc/candidates.go ================================================ package webrtc import ( "net" "strings" "github.com/AlexxIT/go2rtc/internal/api/ws" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webrtc" "github.com/AlexxIT/go2rtc/pkg/xnet" pion "github.com/pion/webrtc/v4" ) type Address struct { host string Port string Network string Priority uint32 } var stuns []string func (a *Address) Host() string { if a.host == "stun" { ip, err := webrtc.GetCachedPublicIP(stuns...) if err != nil { return "" } return ip.String() } return a.host } func (a *Address) Marshal() string { if host := a.Host(); host != "" { return webrtc.CandidateICE(a.Network, host, a.Port, a.Priority) } return "" } var addresses []*Address var filters webrtc.Filters func AddCandidate(network, address string) { if network == "" { AddCandidate("tcp", address) AddCandidate("udp", address) return } host, port, err := net.SplitHostPort(address) if err != nil { return } // start from 1, so manual candidates will be lower than built-in // and every next candidate will have a lower priority candidateIndex := 1 + len(addresses) priority := webrtc.CandidateHostPriority(network, candidateIndex) addresses = append(addresses, &Address{host, port, network, priority}) } func GetCandidates() (candidates []string) { for _, address := range addresses { if candidate := address.Marshal(); candidate != "" { candidates = append(candidates, candidate) } } return } // FilterCandidate return true if candidate passed the check func FilterCandidate(candidate *pion.ICECandidate) bool { if candidate == nil { return false } // remove any Docker-like IP from candidates if ip := net.ParseIP(candidate.Address); ip != nil && xnet.Docker.Contains(ip) { return false } // host candidate should be in the hosts list if candidate.Typ == pion.ICECandidateTypeHost && filters.Candidates != nil { if !core.Contains(filters.Candidates, candidate.Address) { return false } } if filters.Networks != nil { networkType := NetworkType(candidate.Protocol.String(), candidate.Address) if !core.Contains(filters.Networks, networkType) { return false } } return true } // NetworkType convert tcp/udp network to tcp4/tcp6/udp4/udp6 func NetworkType(network, host string) string { if strings.IndexByte(host, ':') >= 0 { return network + "6" } else { return network + "4" } } func asyncCandidates(tr *ws.Transport, cons *webrtc.Conn) { tr.WithContext(func(ctx map[any]any) { if candidates, ok := ctx["candidate"].([]string); ok { // process candidates that receive before this moment for _, candidate := range candidates { _ = cons.AddCandidate(candidate) } // remove already processed candidates delete(ctx, "candidate") } // set variable for process candidates after this moment ctx["webrtc"] = cons }) for _, candidate := range GetCandidates() { log.Trace().Str("candidate", candidate).Msg("[webrtc] config") tr.Write(&ws.Message{Type: "webrtc/candidate", Value: candidate}) } } func candidateHandler(tr *ws.Transport, msg *ws.Message) error { // process incoming candidate in sync function tr.WithContext(func(ctx map[any]any) { candidate := msg.String() log.Trace().Str("candidate", candidate).Msg("[webrtc] remote") if cons, ok := ctx["webrtc"].(*webrtc.Conn); ok { // if webrtc.Server already initialized - process candidate _ = cons.AddCandidate(candidate) } else { // or collect candidate and process it later list, _ := ctx["candidate"].([]string) ctx["candidate"] = append(list, candidate) } }) return nil } ================================================ FILE: internal/webrtc/client.go ================================================ package webrtc import ( "encoding/base64" "errors" "io" "net/http" "net/url" "strings" "sync" "time" "github.com/AlexxIT/go2rtc/internal/api/ws" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webrtc" "github.com/gorilla/websocket" pion "github.com/pion/webrtc/v4" ) // streamsHandler supports: // 1. WHEP: webrtc:http://192.168.1.123:1984/api/webrtc?src=camera1 // 2. go2rtc: webrtc:ws://192.168.1.123:1984/api/ws?src=camera1 // 3. Wyze: webrtc:http://192.168.1.123:5000/signaling/camera1?kvs#format=wyze // 4. Kinesis: webrtc:wss://...amazonaws.com/?...#format=kinesis#client_id=...#ice_servers=[{...},{...}] func streamsHandler(rawURL string) (core.Producer, error) { var query url.Values if i := strings.IndexByte(rawURL, '#'); i > 0 { query = streams.ParseQuery(rawURL[i+1:]) rawURL = rawURL[:i] } rawURL = rawURL[7:] // remove webrtc: if i := strings.IndexByte(rawURL, ':'); i > 0 { scheme := rawURL[:i] format := query.Get("format") switch scheme { case "ws", "wss": if format == "kinesis" { // https://aws.amazon.com/kinesis/video-streams/ // https://docs.aws.amazon.com/kinesisvideostreams-webrtc-dg/latest/devguide/what-is-kvswebrtc.html // https://github.com/orgs/awslabs/repositories?q=kinesis+webrtc return kinesisClient(rawURL, query, "webrtc/kinesis", nil) } else if format == "openipc" { return openIPCClient(rawURL, query) } else if format == "switchbot" { return switchbotClient(rawURL, query) } else { return go2rtcClient(rawURL) } case "http", "https": if format == "milestone" { return milestoneClient(rawURL, query) } else if format == "wyze" { // https://github.com/mrlt8/docker-wyze-bridge return wyzeClient(rawURL) } else if format == "creality" { return crealityClient(rawURL) } else { return whepClient(rawURL) } } } return nil, errors.New("unsupported url: " + rawURL) } // go2rtcClient can connect only to go2rtc server // ex: ws://localhost:1984/api/ws?src=camera1 func go2rtcClient(url string) (core.Producer, error) { // 1. Connect to signalign server conn, _, err := Dial(url) if err != nil { return nil, err } // close websocket when we ready return Producer or connection error defer conn.Close() // 2. Create PeerConnection pc, err := PeerConnection(true) if err != nil { return nil, err } defer func() { if err != nil { _ = pc.Close() } }() // waiter will wait PC error or WS error or nil (connection OK) var connState core.Waiter var connMu sync.Mutex prod := webrtc.NewConn(pc) prod.Mode = core.ModeActiveProducer prod.Protocol = "ws" prod.URL = url prod.Listen(func(msg any) { switch msg := msg.(type) { case *pion.ICECandidate: s := msg.ToJSON().Candidate log.Trace().Str("candidate", s).Msg("[webrtc] local ") connMu.Lock() _ = conn.WriteJSON(&ws.Message{Type: "webrtc/candidate", Value: s}) connMu.Unlock() case pion.PeerConnectionState: switch msg { case pion.PeerConnectionStateConnecting: case pion.PeerConnectionStateConnected: connState.Done(nil) default: connState.Done(errors.New("webrtc: " + msg.String())) } } }) medias := []*core.Media{ {Kind: core.KindVideo, Direction: core.DirectionRecvonly}, {Kind: core.KindAudio, Direction: core.DirectionRecvonly}, {Kind: core.KindAudio, Direction: core.DirectionSendonly}, } // 3. Create offer offer, err := prod.CreateOffer(medias) if err != nil { return nil, err } // 4. Send offer msg := &ws.Message{Type: "webrtc/offer", Value: offer} connMu.Lock() _ = conn.WriteJSON(msg) connMu.Unlock() // 5. Get answer if err = conn.ReadJSON(msg); err != nil { return nil, err } if msg.Type != "webrtc/answer" { err = errors.New("wrong answer: " + msg.String()) return nil, err } answer := msg.String() if err = prod.SetAnswer(answer); err != nil { return nil, err } // 6. Continue to receiving candidates go func() { var err error for { // receive data from remote var msg ws.Message if err = conn.ReadJSON(&msg); err != nil { break } switch msg.Type { case "webrtc/candidate": if msg.Value != nil { _ = prod.AddCandidate(msg.String()) } } } connState.Done(err) }() if err = connState.Wait(); err != nil { return nil, err } return prod, nil } // whepClient - support WebRTC-HTTP Egress Protocol (WHEP) // ex: http://localhost:1984/api/webrtc?src=camera1 func whepClient(url string) (core.Producer, error) { // 2. Create PeerConnection pc, err := PeerConnection(true) if err != nil { log.Error().Err(err).Caller().Send() return nil, err } prod := webrtc.NewConn(pc) prod.Mode = core.ModeActiveProducer prod.Protocol = "http" prod.URL = url medias := []*core.Media{ {Kind: core.KindVideo, Direction: core.DirectionRecvonly}, {Kind: core.KindAudio, Direction: core.DirectionRecvonly}, } // 3. Create offer offer, err := prod.CreateCompleteOffer(medias) if err != nil { return nil, err } req, err := http.NewRequest("POST", url, strings.NewReader(offer)) if err != nil { return nil, err } req.Header.Set("Content-Type", MimeSDP) client := http.Client{Timeout: time.Second * 5000} defer client.CloseIdleConnections() res, err := client.Do(req) if err != nil { return nil, err } answer, err := io.ReadAll(res.Body) if err != nil { return nil, err } if err = prod.SetAnswer(string(answer)); err != nil { return nil, err } return prod, nil } // Dial - websocket.Dial with Basic auth support func Dial(rawURL string) (*websocket.Conn, *http.Response, error) { u, err := url.Parse(rawURL) if err != nil { return nil, nil, err } if u.User == nil { return websocket.DefaultDialer.Dial(rawURL, nil) } user := u.User.Username() pass, _ := u.User.Password() u.User = nil header := http.Header{ "Authorization": []string{ "Basic " + base64.StdEncoding.EncodeToString([]byte(user+":"+pass)), }, } return websocket.DefaultDialer.Dial(u.String(), header) } ================================================ FILE: internal/webrtc/client_creality.go ================================================ package webrtc import ( "encoding/base64" "encoding/json" "io" "net/http" "strings" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webrtc" "github.com/pion/sdp/v3" ) // https://github.com/AlexxIT/go2rtc/issues/1600 func crealityClient(url string) (core.Producer, error) { pc, err := PeerConnection(true) if err != nil { return nil, err } prod := webrtc.NewConn(pc) prod.FormatName = "webrtc/creality" prod.Mode = core.ModeActiveProducer prod.Protocol = "http" prod.URL = url medias := []*core.Media{ {Kind: core.KindVideo, Direction: core.DirectionRecvonly}, } // TODO: return webrtc.SessionDescription offer, err := prod.CreateCompleteOffer(medias) if err != nil { return nil, err } log.Trace().Msgf("[webrtc] offer:\n%s", offer) body, err := offerToB64(offer) if err != nil { return nil, err } req, err := http.NewRequest("POST", url, body) if err != nil { return nil, err } req.Header.Set("Content-Type", "plain/text") // TODO: change http.DefaultClient settings client := http.Client{Timeout: time.Second * 5000} defer client.CloseIdleConnections() res, err := client.Do(req) if err != nil { return nil, err } answer, err := answerFromB64(res.Body) if err != nil { return nil, err } log.Trace().Msgf("[webrtc] answer:\n%s", answer) if answer, err = fixCrealitySDP(answer); err != nil { return nil, err } if err = prod.SetAnswer(answer); err != nil { return nil, err } return prod, nil } func offerToB64(sdp string) (io.Reader, error) { // JS object v := map[string]string{ "type": "offer", "sdp": sdp, } // bytes b, err := json.Marshal(v) if err != nil { return nil, err } // base64, why? who knows... s := base64.StdEncoding.EncodeToString(b) return strings.NewReader(s), nil } func answerFromB64(r io.Reader) (string, error) { // base64 b, err := io.ReadAll(r) if err != nil { return "", err } // bytes if b, err = base64.StdEncoding.DecodeString(string(b)); err != nil { return "", err } // JS object var v map[string]string if err = json.Unmarshal(b, &v); err != nil { return "", err } // string "v=0..." return v["sdp"], nil } func fixCrealitySDP(value string) (string, error) { var sd sdp.SessionDescription if err := sd.UnmarshalString(value); err != nil { return "", err } md := sd.MediaDescriptions[0] // important to skip first codec, because second codec will be used skip := md.MediaName.Formats[0] md.MediaName.Formats = md.MediaName.Formats[1:] attrs := make([]sdp.Attribute, 0, len(md.Attributes)) for _, attr := range md.Attributes { switch attr.Key { case "fmtp", "rtpmap": // important to skip fmtp with x-google, because this is second fmtp for same codec // and pion library will fail parsing this SDP if strings.HasPrefix(attr.Value, skip) || strings.Contains(attr.Value, "x-google") { continue } } attrs = append(attrs, attr) } md.Attributes = attrs b, err := sd.Marshal() if err != nil { return "", err } return string(b), nil } ================================================ FILE: internal/webrtc/kinesis.go ================================================ package webrtc import ( "encoding/json" "errors" "fmt" "io" "net/http" "net/url" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webrtc" "github.com/gorilla/websocket" pion "github.com/pion/webrtc/v4" ) type kinesisRequest struct { Action string `json:"action"` ClientID string `json:"recipientClientId"` Payload []byte `json:"messagePayload"` } func (k kinesisRequest) String() string { return fmt.Sprintf("action=%s, payload=%s", k.Action, k.Payload) } type kinesisResponse struct { Payload []byte `json:"messagePayload"` Type string `json:"messageType"` } func (k kinesisResponse) String() string { return fmt.Sprintf("type=%s, payload=%s", k.Type, k.Payload) } func kinesisClient( rawURL string, query url.Values, format string, sdpOffer func(prod *webrtc.Conn, query url.Values) (any, error), ) (core.Producer, error) { // 1. Connect to signalign server conn, _, err := websocket.DefaultDialer.Dial(rawURL, nil) if err != nil { return nil, err } // 2. Load ICEServers from query param (base64 json) conf := pion.Configuration{} if s := query.Get("ice_servers"); s != "" { conf.ICEServers, err = webrtc.UnmarshalICEServers([]byte(s)) if err != nil { log.Warn().Err(err).Caller().Send() } } // close websocket when we ready return Producer or connection error defer conn.Close() // 3. Create Peer Connection api, err := webrtc.NewAPI() if err != nil { return nil, err } pc, err := api.NewPeerConnection(conf) if err != nil { return nil, err } // protect from sending ICE candidate before Offer var sendOffer core.Waiter // protect from blocking on errors defer sendOffer.Done(nil) // waiter will wait PC error or WS error or nil (connection OK) var connState core.Waiter req := kinesisRequest{ ClientID: query.Get("client_id"), } prod := webrtc.NewConn(pc) prod.FormatName = format prod.Mode = core.ModeActiveProducer prod.Protocol = "ws" prod.URL = rawURL prod.Listen(func(msg any) { switch msg := msg.(type) { case *pion.ICECandidate: _ = sendOffer.Wait() req.Action = "ICE_CANDIDATE" req.Payload, _ = json.Marshal(msg.ToJSON()) if err = conn.WriteJSON(&req); err != nil { connState.Done(err) return } log.Trace().Msgf("[webrtc] kinesis send: %s", req) case pion.PeerConnectionState: switch msg { case pion.PeerConnectionStateConnecting: case pion.PeerConnectionStateConnected: connState.Done(nil) default: connState.Done(errors.New("webrtc: " + msg.String())) } } }) var payload any if sdpOffer == nil { medias := []*core.Media{ {Kind: core.KindVideo, Direction: core.DirectionRecvonly}, {Kind: core.KindAudio, Direction: core.DirectionRecvonly}, } // 4. Create offer var offer string if offer, err = prod.CreateOffer(medias); err != nil { return nil, err } // 5. Send offer payload = pion.SessionDescription{ Type: pion.SDPTypeOffer, SDP: offer, } } else { if payload, err = sdpOffer(prod, query); err != nil { return nil, err } } req.Action = "SDP_OFFER" req.Payload, _ = json.Marshal(payload) if err = conn.WriteJSON(req); err != nil { return nil, err } log.Trace().Msgf("[webrtc] kinesis send: %s", req) sendOffer.Done(nil) go func() { var err error // will be closed when conn will be closed for { var res kinesisResponse if err = conn.ReadJSON(&res); err != nil { // some buggy messages from Amazon servers if errors.Is(err, io.ErrUnexpectedEOF) { continue } break } log.Trace().Msgf("[webrtc] kinesis recv: %s", res) switch res.Type { case "SDP_ANSWER": // 6. Get answer var sd pion.SessionDescription if err = json.Unmarshal(res.Payload, &sd); err != nil { break } if err = prod.SetAnswer(sd.SDP); err != nil { break } case "ICE_CANDIDATE": // 7. Continue to receiving candidates var ci pion.ICECandidateInit if err = json.Unmarshal(res.Payload, &ci); err != nil { break } if err = prod.AddCandidate(ci.Candidate); err != nil { break } } } connState.Done(err) }() if err = connState.Wait(); err != nil { return nil, err } return prod, nil } type wyzeKVS struct { ClientId string `json:"ClientId"` Cam string `json:"cam"` Result string `json:"result"` Servers json.RawMessage `json:"servers"` URL string `json:"signalingUrl"` } func wyzeClient(rawURL string) (core.Producer, error) { client := http.Client{Timeout: 5 * time.Second} res, err := client.Get(rawURL) if err != nil { return nil, err } b, err := io.ReadAll(res.Body) if err != nil { return nil, err } var kvs wyzeKVS if err = json.Unmarshal(b, &kvs); err != nil { return nil, err } if kvs.Result != "ok" { return nil, errors.New("wyse: wrong result: " + kvs.Result) } query := url.Values{ "client_id": []string{kvs.ClientId}, "ice_servers": []string{string(kvs.Servers)}, } return kinesisClient(kvs.URL, query, "webrtc/wyze", nil) } ================================================ FILE: internal/webrtc/milestone.go ================================================ package webrtc import ( "bytes" "encoding/json" "errors" "net/http" "net/url" "strconv" "strings" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/tcp" "github.com/AlexxIT/go2rtc/pkg/webrtc" pion "github.com/pion/webrtc/v4" ) // This package handles the Milestone WebRTC session lifecycle, including authentication, // session creation, and session update with an SDP answer. It is designed to be used with // a specific URL format that encodes session parameters. For example: // webrtc:https://milestone-host/api#format=milestone#username=User#password=TestPassword#cameraId=a539f254-af05-4d67-a1bb-cd9b3c74d122 // // https://github.com/milestonesys/mipsdk-samples-protocol/tree/main/WebRTC_JavaScript type milestoneAPI struct { url string query url.Values token string sessionID string } func (m *milestoneAPI) GetToken() error { data := url.Values{ "client_id": {"GrantValidatorClient"}, "grant_type": {"password"}, "username": m.query["username"], "password": m.query["password"], } req, err := http.NewRequest("POST", m.url+"/IDP/connect/token", strings.NewReader(data.Encode())) if err != nil { return err } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") // support httpx protocol res, err := tcp.Do(req) if err != nil { return err } defer res.Body.Close() if res.StatusCode != http.StatusOK { return errors.New("milesone: authentication failed: " + res.Status) } var payload map[string]interface{} if err = json.NewDecoder(res.Body).Decode(&payload); err != nil { return err } token, ok := payload["access_token"].(string) if !ok { return errors.New("milesone: token not found in the response") } m.token = token return nil } func parseFloat(s string) float64 { if s == "" { return 0 } f, _ := strconv.ParseFloat(s, 64) return f } func (m *milestoneAPI) GetOffer() (string, error) { request := struct { CameraId string `json:"cameraId"` StreamId string `json:"streamId,omitempty"` PlaybackTimeNode struct { PlaybackTime string `json:"playbackTime,omitempty"` SkipGaps bool `json:"skipGaps,omitempty"` Speed float64 `json:"speed,omitempty"` } `json:"playbackTimeNode,omitempty"` //ICEServers []string `json:"iceServers,omitempty"` //Resolution string `json:"resolution,omitempty"` }{ CameraId: m.query.Get("cameraId"), StreamId: m.query.Get("streamId"), } request.PlaybackTimeNode.PlaybackTime = m.query.Get("playbackTime") request.PlaybackTimeNode.SkipGaps = m.query.Has("skipGaps") request.PlaybackTimeNode.Speed = parseFloat(m.query.Get("speed")) data, err := json.Marshal(request) if err != nil { return "", err } req, err := http.NewRequest("POST", m.url+"/REST/v1/WebRTC/Session", bytes.NewBuffer(data)) if err != nil { return "", err } req.Header.Set("Authorization", "Bearer "+m.token) req.Header.Set("Content-Type", "application/json") res, err := tcp.Do(req) if err != nil { return "", err } defer res.Body.Close() if res.StatusCode != http.StatusOK { return "", errors.New("milesone: create session: " + res.Status) } var response struct { SessionId string `json:"sessionId"` OfferSDP string `json:"offerSDP"` } if err = json.NewDecoder(res.Body).Decode(&response); err != nil { return "", err } var offer pion.SessionDescription if err = json.Unmarshal([]byte(response.OfferSDP), &offer); err != nil { return "", err } m.sessionID = response.SessionId return offer.SDP, nil } func (m *milestoneAPI) SetAnswer(sdp string) error { answer := pion.SessionDescription{ Type: pion.SDPTypeAnswer, SDP: sdp, } data, err := json.Marshal(answer) if err != nil { return err } request := struct { AnswerSDP string `json:"answerSDP"` }{ AnswerSDP: string(data), } if data, err = json.Marshal(request); err != nil { return err } req, err := http.NewRequest("PATCH", m.url+"/REST/v1/WebRTC/Session/"+m.sessionID, bytes.NewBuffer(data)) if err != nil { return err } req.Header.Set("Authorization", "Bearer "+m.token) req.Header.Set("Content-Type", "application/json") res, err := tcp.Do(req) if err != nil { return err } defer res.Body.Close() if res.StatusCode != http.StatusOK { return errors.New("milesone: patch session: " + res.Status) } return nil } func milestoneClient(rawURL string, query url.Values) (core.Producer, error) { mc := &milestoneAPI{url: rawURL, query: query} if err := mc.GetToken(); err != nil { return nil, err } api, err := webrtc.NewAPI() if err != nil { return nil, err } conf := pion.Configuration{} pc, err := api.NewPeerConnection(conf) if err != nil { return nil, err } prod := webrtc.NewConn(pc) prod.FormatName = "webrtc/milestone" prod.Mode = core.ModeActiveProducer prod.Protocol = "http" prod.URL = rawURL offer, err := mc.GetOffer() if err != nil { return nil, err } if err = prod.SetOffer(offer); err != nil { return nil, err } answer, err := prod.GetAnswer() if err != nil { return nil, err } if err = mc.SetAnswer(answer); err != nil { return nil, err } return prod, nil } ================================================ FILE: internal/webrtc/openipc.go ================================================ package webrtc import ( "encoding/json" "errors" "io" "net/url" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webrtc" "github.com/gorilla/websocket" pion "github.com/pion/webrtc/v4" ) func openIPCClient(rawURL string, query url.Values) (core.Producer, error) { // 1. Connect to signalign server conn, _, err := websocket.DefaultDialer.Dial(rawURL, nil) if err != nil { return nil, err } // 2. Load ICEServers from query param (base64 json) var conf pion.Configuration if s := query.Get("ice_servers"); s != "" { conf.ICEServers, err = webrtc.UnmarshalICEServers([]byte(s)) if err != nil { log.Warn().Err(err).Caller().Send() } } // close websocket when we ready return Producer or connection error defer conn.Close() // 3. Create Peer Connection api, err := webrtc.NewAPI() if err != nil { return nil, err } pc, err := api.NewPeerConnection(conf) if err != nil { return nil, err } // protect from sending ICE candidate before Offer var sendAnswer core.Waiter // protect from blocking on errors defer sendAnswer.Done(nil) // waiter will wait PC error or WS error or nil (connection OK) var connState core.Waiter prod := webrtc.NewConn(pc) prod.FormatName = "webrtc/openipc" prod.Mode = core.ModeActiveProducer prod.Protocol = "ws" prod.URL = rawURL prod.Listen(func(msg any) { switch msg := msg.(type) { case *pion.ICECandidate: _ = sendAnswer.Wait() req := openIPCReq{ Data: msg.ToJSON().Candidate, Req: "candidate", } if err = conn.WriteJSON(&req); err != nil { connState.Done(err) return } log.Trace().Msgf("[webrtc] openipc send: %s", req) case pion.PeerConnectionState: switch msg { case pion.PeerConnectionStateConnecting: case pion.PeerConnectionStateConnected: connState.Done(nil) default: connState.Done(errors.New("webrtc: " + msg.String())) } } }) go func() { var err error // will be closed when conn will be closed for err == nil { var rep openIPCReply if err = conn.ReadJSON(&rep); err != nil { // some buggy messages from Amazon servers if errors.Is(err, io.ErrUnexpectedEOF) { continue } break } log.Trace().Msgf("[webrtc] openipc recv: %s", rep) switch rep.Reply { case "webrtc_answer": // 6. Get answer var sd pion.SessionDescription if err = json.Unmarshal(rep.Data, &sd); err != nil { break } if err = prod.SetOffer(sd.SDP); err != nil { break } var answer string if answer, err = prod.GetAnswer(); err != nil { break } req := openIPCReq{Data: answer, Req: "answer"} if err = conn.WriteJSON(req); err != nil { break } log.Trace().Msgf("[webrtc] kinesis send: %s", req) sendAnswer.Done(nil) case "webrtc_candidate": // 7. Continue to receiving candidates var ci pion.ICECandidateInit if err = json.Unmarshal(rep.Data, &ci); err != nil { break } if err = prod.AddCandidate(ci.Candidate); err != nil { break } } } connState.Done(err) }() if err = connState.Wait(); err != nil { return nil, err } return prod, nil } type openIPCReply struct { Data json.RawMessage `json:"data"` Reply string `json:"reply"` } func (r openIPCReply) String() string { b, _ := json.Marshal(r) return string(b) } type openIPCReq struct { Data string `json:"data"` Req string `json:"req"` } func (r openIPCReq) String() string { b, _ := json.Marshal(r) return string(b) } ================================================ FILE: internal/webrtc/server.go ================================================ package webrtc import ( "encoding/base64" "encoding/json" "io" "net/http" "strconv" "strings" "time" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webrtc" pion "github.com/pion/webrtc/v4" ) const MimeSDP = "application/sdp" var sessions = map[string]*webrtc.Conn{} func syncHandler(w http.ResponseWriter, r *http.Request) { switch r.Method { case "POST": query := r.URL.Query() if query.Get("src") != "" { // WHEP or JSON SDP or raw SDP exchange outputWebRTC(w, r) } else if query.Get("dst") != "" { // WHIP SDP exchange inputWebRTC(w, r) } else { http.Error(w, "", http.StatusBadRequest) } case "PATCH": // TODO: WHEP/WHIP http.Error(w, "", http.StatusMethodNotAllowed) case "DELETE": if id := r.URL.Query().Get("id"); id != "" { if conn, ok := sessions[id]; ok { delete(sessions, id) _ = conn.Close() } else { http.Error(w, "", http.StatusNotFound) } } else { http.Error(w, "", http.StatusBadRequest) } case "OPTIONS": w.WriteHeader(http.StatusNoContent) default: http.Error(w, "", http.StatusMethodNotAllowed) } } // outputWebRTC support API depending on Content-Type: // 1. application/json - receive {"type":"offer","sdp":"v=0\r\n..."} and response {"type":"answer","sdp":"v=0\r\n..."} // 2. application/sdp - receive/response SDP via WebRTC-HTTP Egress Protocol (WHEP) // 3. other - receive/response raw SDP func outputWebRTC(w http.ResponseWriter, r *http.Request) { u := r.URL.Query().Get("src") stream := streams.Get(u) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } mediaType := r.Header.Get("Content-Type") if mediaType != "" { mediaType, _, _ = strings.Cut(mediaType, ";") mediaType = strings.ToLower(strings.TrimSpace(mediaType)) } var offer string switch mediaType { case "application/json": var desc pion.SessionDescription if err := json.NewDecoder(r.Body).Decode(&desc); err != nil { log.Error().Err(err).Caller().Send() http.Error(w, err.Error(), http.StatusBadRequest) return } offer = desc.SDP case "application/x-www-form-urlencoded": if err := r.ParseForm(); err != nil { log.Error().Err(err).Caller().Send() http.Error(w, err.Error(), http.StatusBadRequest) return } offerB64 := r.Form.Get("data") b, err := base64.StdEncoding.DecodeString(offerB64) if err != nil { log.Error().Err(err).Caller().Send() http.Error(w, err.Error(), http.StatusBadRequest) return } offer = string(b) default: body, err := io.ReadAll(r.Body) if err != nil { log.Error().Err(err).Caller().Send() http.Error(w, err.Error(), http.StatusInternalServerError) return } offer = string(body) } var desc string switch mediaType { case "application/json": desc = "webrtc/json" case MimeSDP: desc = "webrtc/whep" default: desc = "webrtc/post" } answer, err := ExchangeSDP(stream, offer, desc, r.UserAgent()) if err != nil { log.Error().Err(err).Caller().Send() http.Error(w, err.Error(), http.StatusInternalServerError) return } switch mediaType { case "application/json": w.Header().Set("Content-Type", mediaType) v := pion.SessionDescription{ Type: pion.SDPTypeAnswer, SDP: answer, } err = json.NewEncoder(w).Encode(v) case "application/x-www-form-urlencoded": w.Header().Set("Content-Type", mediaType) answerB64 := base64.StdEncoding.EncodeToString([]byte(answer)) _, err = w.Write([]byte(answerB64)) case MimeSDP: w.Header().Set("Content-Type", mediaType) w.WriteHeader(http.StatusCreated) _, err = w.Write([]byte(answer)) default: w.Header().Set("Content-Type", mediaType) _, err = w.Write([]byte(answer)) } if err != nil { log.Error().Err(err).Caller().Send() http.Error(w, err.Error(), http.StatusInternalServerError) } } func inputWebRTC(w http.ResponseWriter, r *http.Request) { dst := r.URL.Query().Get("dst") stream := streams.Get(dst) if stream == nil { http.Error(w, api.StreamNotFound, http.StatusNotFound) return } // 1. Get offer offer, err := io.ReadAll(r.Body) if err != nil { log.Error().Err(err).Caller().Send() http.Error(w, err.Error(), http.StatusInternalServerError) return } log.Trace().Msgf("[webrtc] WHIP offer\n%s", offer) pc, err := PeerConnection(false) if err != nil { log.Error().Err(err).Caller().Send() http.Error(w, err.Error(), http.StatusInternalServerError) return } // create new webrtc instance prod := webrtc.NewConn(pc) prod.Mode = core.ModePassiveProducer prod.Protocol = "http" prod.UserAgent = r.UserAgent() if err = prod.SetOffer(string(offer)); err != nil { log.Warn().Err(err).Caller().Send() http.Error(w, err.Error(), http.StatusInternalServerError) return } answer, err := prod.GetCompleteAnswer(GetCandidates(), FilterCandidate) if err != nil { log.Warn().Err(err).Caller().Send() http.Error(w, err.Error(), http.StatusInternalServerError) return } log.Trace().Msgf("[webrtc] WHIP answer\n%s", answer) id := strconv.FormatInt(time.Now().UnixNano(), 36) sessions[id] = prod prod.Listen(func(msg any) { switch msg := msg.(type) { case pion.PeerConnectionState: if msg == pion.PeerConnectionStateClosed { stream.RemoveProducer(prod) delete(sessions, id) } } }) stream.AddProducer(prod) w.Header().Set("Content-Type", MimeSDP) w.Header().Set("Location", "webrtc?id="+id) w.WriteHeader(http.StatusCreated) if _, err = w.Write([]byte(answer)); err != nil { log.Warn().Err(err).Caller().Send() return } } ================================================ FILE: internal/webrtc/switchbot.go ================================================ package webrtc import ( "net/url" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webrtc" ) func switchbotClient(rawURL string, query url.Values) (core.Producer, error) { return kinesisClient(rawURL, query, "webrtc/switchbot", func(prod *webrtc.Conn, query url.Values) (any, error) { medias := []*core.Media{ {Kind: core.KindVideo, Direction: core.DirectionRecvonly}, } offer, err := prod.CreateOffer(medias) if err != nil { return nil, err } v := struct { Type string `json:"type"` SDP string `json:"sdp"` Resolution int `json:"resolution"` PlayType int `json:"play_type"` }{ Type: "offer", SDP: offer, } switch query.Get("resolution") { case "hd": v.Resolution = 0 case "sd": v.Resolution = 1 case "auto": v.Resolution = 2 } v.PlayType = core.Atoi(query.Get("play_type")) // zero by default return v, nil }) } ================================================ FILE: internal/webrtc/webrtc.go ================================================ package webrtc import ( "errors" "net" "strings" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api/ws" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webrtc" pion "github.com/pion/webrtc/v4" "github.com/rs/zerolog" ) func Init() { var cfg struct { Mod struct { Listen string `yaml:"listen"` Candidates []string `yaml:"candidates"` IceServers []pion.ICEServer `yaml:"ice_servers"` Filters webrtc.Filters `yaml:"filters"` } `yaml:"webrtc"` } cfg.Mod.Listen = ":8555" cfg.Mod.IceServers = []pion.ICEServer{ {URLs: []string{"stun:stun.cloudflare.com:3478", "stun:stun.l.google.com:19302"}}, } app.LoadConfig(&cfg) log = app.GetLogger("webrtc") if log.Debug().Enabled() { itfs, _ := net.Interfaces() for _, itf := range itfs { addrs, _ := itf.Addrs() log.Debug().Msgf("[webrtc] interface %+v addrs %v", itf, addrs) } } address, network, _ := strings.Cut(cfg.Mod.Listen, "/") for _, candidate := range cfg.Mod.Candidates { AddCandidate(network, candidate) if strings.HasPrefix(candidate, "stun:") && stuns == nil { for _, ice := range cfg.Mod.IceServers { for _, url := range ice.URLs { if strings.HasPrefix(url, "stun:") { stuns = append(stuns, url[5:]) } } } } } webrtc.OnNewListener = func(ln any) { switch ln := ln.(type) { case *net.TCPListener: log.Info().Stringer("addr", ln.Addr()).Msg("[webrtc] listen tcp") case *net.UDPConn: log.Info().Stringer("addr", ln.LocalAddr()).Msg("[webrtc] listen udp") } } var err error // create pionAPI with custom codecs list and custom network settings serverAPI, err = webrtc.NewServerAPI(network, address, &cfg.Mod.Filters) if err != nil { log.Error().Err(err).Caller().Send() return } // use same API for WebRTC server and client if no address clientAPI = serverAPI if address != "" { clientAPI, _ = webrtc.NewAPI() } pionConf := pion.Configuration{ ICEServers: cfg.Mod.IceServers, SDPSemantics: pion.SDPSemanticsUnifiedPlanWithFallback, } PeerConnection = func(active bool) (*pion.PeerConnection, error) { // active - client, passive - server if active { return clientAPI.NewPeerConnection(pionConf) } else { return serverAPI.NewPeerConnection(pionConf) } } // async WebRTC server (two API versions) ws.HandleFunc("webrtc", asyncHandler) ws.HandleFunc("webrtc/offer", asyncHandler) ws.HandleFunc("webrtc/candidate", candidateHandler) // sync WebRTC server (two API versions) api.HandleFunc("api/webrtc", syncHandler) // WebRTC client streams.HandleFunc("webrtc", streamsHandler) } var serverAPI, clientAPI *pion.API var log zerolog.Logger var PeerConnection func(active bool) (*pion.PeerConnection, error) func asyncHandler(tr *ws.Transport, msg *ws.Message) (err error) { var stream *streams.Stream var mode core.Mode query := tr.Request.URL.Query() if name := query.Get("src"); name != "" { stream, _ = streams.GetOrPatch(query) mode = core.ModePassiveConsumer log.Debug().Str("src", name).Msg("[webrtc] new consumer") } else if name = query.Get("dst"); name != "" { stream = streams.Get(name) mode = core.ModePassiveProducer log.Debug().Str("src", name).Msg("[webrtc] new producer") } if stream == nil { return errors.New(api.StreamNotFound) } var offer struct { Type string `json:"type"` SDP string `json:"sdp"` ICEServers []pion.ICEServer `json:"ice_servers"` } // V2 - json/object exchange, V1 - raw SDP exchange apiV2 := msg.Type == "webrtc" if apiV2 { if err = msg.Unmarshal(&offer); err != nil { return err } } else { offer.SDP = msg.String() } // create new PeerConnection instance var pc *pion.PeerConnection if offer.ICEServers == nil { pc, err = PeerConnection(false) } else { pc, err = serverAPI.NewPeerConnection(pion.Configuration{ICEServers: offer.ICEServers}) } if err != nil { log.Error().Err(err).Caller().Send() return err } var sendAnswer core.Waiter // protect from blocking on errors defer sendAnswer.Done(nil) conn := webrtc.NewConn(pc) conn.Mode = mode conn.Protocol = "ws" conn.UserAgent = tr.Request.UserAgent() conn.Listen(func(msg any) { switch msg := msg.(type) { case pion.PeerConnectionState: if msg != pion.PeerConnectionStateClosed { return } switch mode { case core.ModePassiveConsumer: stream.RemoveConsumer(conn) case core.ModePassiveProducer: stream.RemoveProducer(conn) } case *pion.ICECandidate: if !FilterCandidate(msg) { return } _ = sendAnswer.Wait() s := msg.ToJSON().Candidate log.Trace().Str("candidate", s).Msg("[webrtc] local ") tr.Write(&ws.Message{Type: "webrtc/candidate", Value: s}) } }) log.Trace().Msgf("[webrtc] offer:\n%s", offer.SDP) // 1. SetOffer, so we can get remote client codecs if err = conn.SetOffer(offer.SDP); err != nil { log.Warn().Err(err).Caller().Send() return err } switch mode { case core.ModePassiveConsumer: // 2. AddConsumer, so we get new tracks if err = stream.AddConsumer(conn); err != nil { log.Debug().Err(err).Msg("[webrtc] add consumer") _ = conn.Close() return err } case core.ModePassiveProducer: stream.AddProducer(conn) } // 3. Exchange SDP without waiting all candidates answer, err := conn.GetAnswer() log.Trace().Msgf("[webrtc] answer\n%s", answer) if err != nil { log.Error().Err(err).Caller().Send() return err } if apiV2 { desc := pion.SessionDescription{Type: pion.SDPTypeAnswer, SDP: answer} tr.Write(&ws.Message{Type: "webrtc", Value: desc}) } else { tr.Write(&ws.Message{Type: "webrtc/answer", Value: answer}) } sendAnswer.Done(nil) asyncCandidates(tr, conn) return nil } func ExchangeSDP(stream *streams.Stream, offer, desc, userAgent string) (answer string, err error) { pc, err := PeerConnection(false) if err != nil { log.Error().Err(err).Caller().Send() return } // create new webrtc instance conn := webrtc.NewConn(pc) conn.FormatName = desc conn.UserAgent = userAgent conn.Protocol = "http" conn.Listen(func(msg any) { switch msg := msg.(type) { case pion.PeerConnectionState: if msg != pion.PeerConnectionStateClosed { return } if conn.Mode == core.ModePassiveConsumer { stream.RemoveConsumer(conn) } else { stream.RemoveProducer(conn) } } }) // 1. SetOffer, so we can get remote client codecs log.Trace().Msgf("[webrtc] offer:\n%s", offer) if err = conn.SetOffer(offer); err != nil { log.Warn().Err(err).Caller().Send() return } if IsConsumer(conn) { conn.Mode = core.ModePassiveConsumer // 2. AddConsumer, so we get new tracks if err = stream.AddConsumer(conn); err != nil { log.Warn().Err(err).Caller().Send() _ = conn.Close() return } } else { conn.Mode = core.ModePassiveProducer stream.AddProducer(conn) } answer, err = conn.GetCompleteAnswer(GetCandidates(), FilterCandidate) log.Trace().Msgf("[webrtc] answer\n%s", answer) if err != nil { log.Error().Err(err).Caller().Send() } return } func IsConsumer(conn *webrtc.Conn) bool { // if wants get video - consumer for _, media := range conn.GetMedias() { if media.Kind == core.KindVideo && media.Direction == core.DirectionSendonly { return true } } // if wants send video - producer for _, media := range conn.GetMedias() { if media.Kind == core.KindVideo && media.Direction == core.DirectionRecvonly { return false } } // if wants something - consumer for _, media := range conn.GetMedias() { if media.Direction == core.DirectionSendonly { return true } } return false } ================================================ FILE: internal/webrtc/webrtc_test.go ================================================ package webrtc import ( "encoding/json" "strings" "testing" "github.com/AlexxIT/go2rtc/internal/api/ws" pion "github.com/pion/webrtc/v4" "github.com/stretchr/testify/require" ) func TestWebRTCAPIv1(t *testing.T) { raw := `{"type":"webrtc/offer","value":"v=0\n..."}` msg := new(ws.Message) err := json.Unmarshal([]byte(raw), msg) require.Nil(t, err) require.Equal(t, "v=0\n...", msg.String()) } func TestWebRTCAPIv2(t *testing.T) { raw := `{"type":"webrtc","value":{"type":"offer","sdp":"v=0\n...","ice_servers":[{"urls":["stun:stun.l.google.com:19302"]}]}}` msg := new(ws.Message) err := json.Unmarshal([]byte(raw), msg) require.Nil(t, err) var offer struct { Type string `json:"type"` SDP string `json:"sdp"` ICEServers []pion.ICEServer `json:"ice_servers"` } err = msg.Unmarshal(&offer) require.Nil(t, err) require.Equal(t, "offer", offer.Type) require.Equal(t, "v=0\n...", offer.SDP) require.Equal(t, "stun:stun.l.google.com:19302", offer.ICEServers[0].URLs[0]) } func TestCrealitySDP(t *testing.T) { sdp := `v=0 o=- 1495799811084970 1495799811084970 IN IP4 0.0.0.0 s=- t=0 0 a=msid-semantic:WMS * a=group:BUNDLE 0 m=video 9 UDP/TLS/RTP/SAVPF 96 98 a=rtcp-fb:98 nack a=rtcp-fb:98 nack pli a=fmtp:96 profile-level-id=42e01f;level-asymmetry-allowed=1 a=fmtp:98 profile-level-id=42e01f;packetization-mode=1;level-asymmetry-allowed=1 a=fmtp:98 x-google-max-bitrate=6000;x-google-min-bitrate=2000;x-google-start-bitrate=4000 a=rtpmap:96 H264/90000 a=rtpmap:98 H264/90000 a=ssrc:1 cname:pear c=IN IP4 0.0.0.0 a=sendonly a=mid:0 a=rtcp-mux a=ice-ufrag:7AVa a=ice-pwd:T+F/5y05Paw+mtG5Jrd8N3 a=ice-options:trickle a=fingerprint:sha-256 A5:AB:C0:4E:29:5B:BD:3B:7D:88:24:6C:56:6B:2A:79:A3:76:99:35:57:75:AD:C8:5A:A6:34:20:88:1B:68:EF a=setup:passive a=candidate:1 1 UDP 2015363327 172.22.233.10 48929 typ host a=candidate:2 1 TCP 1015021823 172.22.233.10 0 typ host tcptype active a=candidate:3 1 TCP 1010827519 172.22.233.10 60677 typ host tcptype passive ` sdp, err := fixCrealitySDP(sdp) require.Nil(t, err) require.False(t, strings.Contains(sdp, "x-google-max-bitrate")) } ================================================ FILE: internal/webtorrent/README.md ================================================ # WebTorrent > [!NOTE] > This section needs some improvement. ## WebTorrent Client [`new in v1.3.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.3.0) This source can get a stream from another go2rtc via [WebTorrent](https://en.wikipedia.org/wiki/WebTorrent) protocol. ### Client Configuration ```yaml streams: webtorrent1: webtorrent:?share=huofssuxaty00izc&pwd=k3l2j9djeg8v8r7e ``` ## WebTorrent Server [`new in v1.3.0`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.3.0) This module supports: - Share any local stream via [WebTorrent](https://webtorrent.io/) technology - Get any [incoming stream](../webrtc/README.md#ingest-browser) from PC or mobile via [WebTorrent](https://webtorrent.io/) technology - Get any remote go2rtc source via [WebTorrent](https://webtorrent.io/) technology Securely and freely. You do not need to open a public access to the go2rtc server. But in some cases (Symmetric NAT), you may need to set up external access to [WebRTC module](../webrtc/README.md). To generate a sharing link or incoming link, go to the go2rtc WebUI (stream links page). This link is **temporary** and will stop working after go2rtc is restarted! ### Server Configuration You can create permanent external links in the go2rtc config: ```yaml webtorrent: shares: super-secret-share: # share name, should be unique among all go2rtc users! pwd: super-secret-password src: rtsp-dahua1 # stream name from streams section ``` Link example: `https://go2rtc.org/webtorrent/#share=02SNtgjKXY&pwd=wznEQqznxW&media=video+audio` ================================================ FILE: internal/webtorrent/init.go ================================================ package webtorrent import ( "errors" "fmt" "net/http" "net/url" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/internal/webrtc" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webtorrent" "github.com/rs/zerolog" ) func Init() { var cfg struct { Mod struct { Trackers []string `yaml:"trackers"` Shares map[string]struct { Pwd string `yaml:"pwd"` Src string `yaml:"src"` } `yaml:"shares"` } `yaml:"webtorrent"` } cfg.Mod.Trackers = []string{"wss://tracker.openwebtorrent.com"} app.LoadConfig(&cfg) if len(cfg.Mod.Trackers) == 0 { return } log = app.GetLogger("webtorrent") streams.HandleFunc("webtorrent", streamHandle) api.HandleFunc("api/webtorrent", apiHandle) srv = &webtorrent.Server{ URL: cfg.Mod.Trackers[0], Exchange: func(src, offer string) (answer string, err error) { stream := streams.Get(src) if stream == nil { return "", errors.New(api.StreamNotFound) } return webrtc.ExchangeSDP(stream, offer, "webtorrent", "") }, } if log.Debug().Enabled() { srv.Listen(func(msg any) { switch msg.(type) { case string, error: log.Debug().Msgf("[webtorrent] %s", msg) case *webtorrent.Message: log.Trace().Any("msg", msg).Msgf("[webtorrent]") } }) } for name, share := range cfg.Mod.Shares { if len(name) < 8 { log.Warn().Str("name", name).Msgf("min share name len - 8 symbols") continue } if len(share.Pwd) < 4 { log.Warn().Str("name", name).Str("pwd", share.Pwd).Msgf("min share pwd len - 4 symbols") continue } if streams.Get(share.Src) == nil { log.Warn().Str("stream", share.Src).Msgf("stream not exists") continue } srv.AddShare(name, share.Pwd, share.Src) // adds to GET /api/webtorrent shares[name] = name } } var log zerolog.Logger var shares = map[string]string{} var srv *webtorrent.Server func apiHandle(w http.ResponseWriter, r *http.Request) { src := r.URL.Query().Get("src") share, ok := shares[src] switch r.Method { case "GET": // support act as WebTorrent tracker (for testing purposes) if r.Header.Get("Connection") == "Upgrade" { tracker(w, r) return } if src != "" { // response one share if ok { pwd := srv.GetSharePwd(share) data := fmt.Sprintf(`{"share":%q,"pwd":%q}`, share, pwd) _, _ = w.Write([]byte(data)) } else { http.Error(w, "", http.StatusNotFound) } } else { // response all shares var items []*api.Source for src, share := range shares { pwd := srv.GetSharePwd(share) source := fmt.Sprintf("webtorrent:?share=%s&pwd=%s", share, pwd) items = append(items, &api.Source{ID: src, URL: source}) } api.ResponseSources(w, items) } case "POST": // check if share already exist if ok { http.Error(w, "", http.StatusBadRequest) return } // check if stream exists if stream := streams.Get(src); stream == nil { http.Error(w, "", http.StatusNotFound) return } // create new random share share = core.RandString(10, 62) pwd := core.RandString(10, 62) srv.AddShare(share, pwd, src) shares[src] = share w.WriteHeader(http.StatusCreated) data := fmt.Sprintf(`{"share":%q,"pwd":%q}`, share, pwd) api.Response(w, data, api.MimeJSON) case "DELETE": if ok { srv.RemoveShare(share) delete(shares, src) } else { http.Error(w, "", http.StatusNotFound) } } } func streamHandle(rawURL string) (core.Producer, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } query := u.Query() share := query.Get("share") pwd := query.Get("pwd") if len(share) < 8 || len(pwd) < 4 { return nil, errors.New("wrong URL: " + rawURL) } pc, err := webrtc.PeerConnection(true) if err != nil { return nil, err } return webtorrent.NewClient(srv.URL, share, pwd, pc) } ================================================ FILE: internal/webtorrent/tracker.go ================================================ package webtorrent import ( "fmt" "net/http" "github.com/AlexxIT/go2rtc/pkg/webtorrent" "github.com/gorilla/websocket" ) var upgrader *websocket.Upgrader var hashes map[string]map[string]*websocket.Conn func tracker(w http.ResponseWriter, r *http.Request) { if upgrader == nil { upgrader = &websocket.Upgrader{ ReadBufferSize: 1024, WriteBufferSize: 2028, } upgrader.CheckOrigin = func(r *http.Request) bool { return true } } ws, err := upgrader.Upgrade(w, r, nil) if err != nil { log.Warn().Err(err).Send() return } defer ws.Close() for { var msg webtorrent.Message if err = ws.ReadJSON(&msg); err != nil { return } //log.Trace().Msgf("[webtorrent] message=%v", msg) if msg.InfoHash == "" || msg.PeerId == "" { continue } if hashes == nil { hashes = map[string]map[string]*websocket.Conn{} } // new or old client with offers clients := hashes[msg.InfoHash] if clients == nil { clients = map[string]*websocket.Conn{ msg.PeerId: ws, } hashes[msg.InfoHash] = clients } else { clients[msg.PeerId] = ws } switch { case msg.Offers != nil: // ask for ping raw := fmt.Sprintf( `{"action":"announce","interval":120,"info_hash":"%s","complete":0,"incomplete":1}`, msg.InfoHash, ) if err = ws.WriteMessage(websocket.TextMessage, []byte(raw)); err != nil { log.Warn().Err(err).Send() return } // skip if no offers (server) if len(msg.Offers) == 0 { continue } // get and check only first offer offer := msg.Offers[0] if offer.OfferId == "" || offer.Offer.Type != "offer" || offer.Offer.SDP == "" { continue } // send offer to all clients (one of them - server) raw = fmt.Sprintf( `{"action":"announce","info_hash":"%s","peer_id":"%s","offer_id":"%s","offer":{"type":"offer","sdp":"%s"}}`, msg.InfoHash, msg.PeerId, offer.OfferId, offer.Offer.SDP, ) for _, server := range clients { if server != ws { _ = server.WriteMessage(websocket.TextMessage, []byte(raw)) } } case msg.OfferId != "" && msg.ToPeerId != "" && msg.Answer != nil: ws1, ok := clients[msg.ToPeerId] if !ok { continue } raw := fmt.Sprintf( `{"action":"announce","info_hash":"%s","peer_id":"%s","offer_id":"%s","answer":{"type":"answer","sdp":"%s"}}`, msg.InfoHash, msg.PeerId, msg.OfferId, msg.Answer.SDP, ) _ = ws1.WriteMessage(websocket.TextMessage, []byte(raw)) } } } ================================================ FILE: internal/wyoming/README.md ================================================ # Wyoming > [!NOTE] > The format is under development and does not yet work stably. This module provide [Wyoming Protocol](https://www.home-assistant.io/integrations/wyoming/) support to create local voice assistants using [Home Assistant](https://www.home-assistant.io/). - go2rtc can act as [Wyoming Satellite](https://github.com/rhasspy/wyoming-satellite) - go2rtc can act as [Wyoming External Microphone](https://github.com/rhasspy/wyoming-mic-external) - go2rtc can act as [Wyoming External Sound](https://github.com/rhasspy/wyoming-snd-external) - any supported audio source with PCM codec can be used as audio input - any supported two-way audio source with PCM codec can be used as audio output - any desktop/server microphone/speaker can be used as two-way audio source - supported any OS via FFmpeg or any similar software - supported Linux via alsa source - you can change the behavior using the built-in scripting engine ## Typical Voice Pipeline 1. Audio stream (MIC) - any audio source with PCM codec support (include PCMA/PCMU) 2. Voice Activity Detector (VAD) 3. Wake Word (WAKE) - [OpenWakeWord](https://www.home-assistant.io/voice_control/create_wake_word/) 4. Speech-to-Text (STT) - [Whisper](https://github.com/home-assistant/addons/blob/master/whisper/) - [Vosk](https://github.com/rhasspy/hassio-addons/blob/master/vosk/) 5. Conversation agent (INTENT) - [Home Assistant](https://www.home-assistant.io/integrations/conversation/) 6. Text-to-speech (TTS) - [Google Translate](https://www.home-assistant.io/integrations/google_translate/) - [Piper](https://github.com/home-assistant/addons/blob/master/piper/) 7. Audio stream (SND) - any source with two-way audio (backchannel) and PCM codec support (include PCMA/PCMU) You can use a large number of different projects for WAKE, STT, INTENT and TTS thanks to the Home Assistant. And you can use a large number of different technologies for MIC and SND thanks to Go2rtc. ## Configuration You can optionally specify WAKE service. So go2rtc will start transmitting audio to Home Assistant only after WAKE word. If the WAKE service cannot be connected to or not specified - go2rtc will pass all audio to Home Assistant. In this case WAKE service must be configured in your Voice Assistant pipeline. You can optionally specify VAD threshold. So go2rtc will start transmitting audio to WAKE service only after some audio noise. Your stream must support audio transmission in PCM codec (include PCMA/PCMU). ```yaml wyoming: stream_name_from_streams_section: listen: :10700 name: "My Satellite" # optional name wake_uri: tcp://192.168.1.23:10400 # optional WAKE service vad_threshold: 1 # optional VAD threshold (from 0.1 to 3.5) ``` Home Assistant -> Settings -> Integrations -> Add -> Wyoming Protocol -> Host + Port from `go2rtc.yaml` Select one or multiple wake words: ```yaml wake_uri: tcp://192.168.1.23:10400?name=alexa_v0.1&name=hey_jarvis_v0.1&name=hey_mycroft_v0.1&name=hey_rhasspy_v0.1&name=ok_nabu_v0.1 ``` ## Events You can add wyoming event handling using the [expr](../expr/README.md) language. For example, to pronounce TTS on some media player from HA. Turn on the logs to see what kind of events happens. This is what the default scripts look like: ```yaml wyoming: script_example: event: run-satellite: Detect() pause-satellite: Stop() voice-stopped: Pause() audio-stop: PlayAudio() && WriteEvent("played") && Detect() error: Detect() internal-run: WriteEvent("run-pipeline", '{"start_stage":"wake","end_stage":"tts"}') && Stream() internal-detection: WriteEvent("run-pipeline", '{"start_stage":"asr","end_stage":"tts"}') && Stream() ``` Supported functions and variables: - `Detect()` - start the VAD and WAKE word detection process - `Stream()` - start transmission of audio data to the client (Home Assistant) - `Stop()` - stop and disconnect stream without disconnecting client (Home Assistant) - `Pause()` - temporary pause of audio transfer, without disconnecting the stream - `PlayAudio()` - playing the last audio that was sent from client (Home Assistant) - `WriteEvent(type, data)` - send event to client (Home Assistant) - `Sleep(duration)` - temporary script pause (ex. `Sleep('1.5s')`) - `PlayFile(path)` - play audio from `wav` file - `Type` - type (name) of event - `Data` - event data in JSON format (ex. `{"text":"how are you"}`) - also available other functions from [expr](../expr/README.md) module (ex. `fetch`) If you write a script for an event - the default action is no longer executed. You need to repeat the necessary steps yourself. In addition to the standard events, there are two additional events: - `internal-run` - called after `Detect()` when VAD detected, but WAKE service unavailable - `internal-detection` - called after `Detect()` when WAKE word detected **Example 1.** You want to play a sound file when a wake word detected (only `wav` supported): - `PlayFile` and `PlayAudio` functions are executed synchronously, the following steps will be executed only after they are completed ```yaml wyoming: script_example: event: internal-detection: PlayFile('/media/beep.wav') && WriteEvent("run-pipeline", '{"start_stage":"asr","end_stage":"tts"}') && Stream() ``` **Example 2.** You want to play TTS on a Home Assistant media player: Each event has a `Type` and `Data` in JSON format. You can use their values in scripts. - in the `synthesize` step, we get the value of the `text` and call the HA REST API - in the `audio-stop` step we get the duration of the TTS in seconds, wait for this time and start the pipeline again ```yaml wyoming: script_example: event: synthesize: | let text = fromJSON(Data).text; let token = 'eyJhbGci...'; fetch('http://localhost:8123/api/services/tts/speak', { method: 'POST', headers: {'Authorization': 'Bearer '+token,'Content-Type': 'application/json'}, body: toJSON({ entity_id: 'tts.google_translate_com', media_player_entity_id: 'media_player.google_nest', message: text, language: 'en', }), }).ok audio-stop: | let timestamp = fromJSON(Data).timestamp; let delay = string(timestamp)+'s'; Sleep(delay) && WriteEvent("played") && Detect() ``` ## Config examples Satellite on Windows server using FFmpeg and FFplay. ```yaml streams: satellite_win: - exec:ffmpeg -hide_banner -f dshow -i "audio=Microphone (High Definition Audio Device)" -c pcm_s16le -ar 16000 -ac 1 -f wav - - exec:ffplay -hide_banner -nodisp -probesize 32 -f s16le -ar 22050 -#backchannel=1#audio=s16le/22050 wyoming: satellite_win: listen: :10700 name: "Windows Satellite" wake_uri: tcp://192.168.1.23:10400 vad_threshold: 1 ``` Satellite on Dahua camera with two-way audio support. ```yaml streams: dahua_camera: - rtsp://admin:password@192.168.1.123/cam/realmonitor?channel=1&subtype=1&unicast=true&proto=Onvif wyoming: dahua_camera: listen: :10700 name: "Dahua Satellite" wake_uri: tcp://192.168.1.23:10400 vad_threshold: 1 ``` Satellite on external wyoming Microphone and Sound. ```yaml streams: wyoming_external: - wyoming://192.168.1.23:10600 # wyoming-mic-external - wyoming://192.168.1.23:10601?backchannel=1 # wyoming-snd-external wyoming: wyoming_external: listen: :10700 name: "Wyoming Satellite" wake_uri: tcp://192.168.1.23:10400 vad_threshold: 1 ``` ## Wyoming External Microphone and Sound Advanced users, who want to enjoy the [Wyoming Satellite](https://github.com/rhasspy/wyoming-satellite) project, can use go2rtc as a [Wyoming External Microphone](https://github.com/rhasspy/wyoming-mic-external) or [Wyoming External Sound](https://github.com/rhasspy/wyoming-snd-external). **go2rtc.yaml** ```yaml streams: wyoming_mic_external: - exec:ffmpeg -hide_banner -f dshow -i "audio=Microphone (High Definition Audio Device)" -c pcm_s16le -ar 16000 -ac 1 -f wav - wyoming_snd_external: - exec:ffplay -hide_banner -nodisp -probesize 32 -f s16le -ar 22050 -#backchannel=1#audio=s16le/22050 wyoming: wyoming_mic_external: listen: :10600 mode: mic wyoming_snd_external: listen: :10601 mode: snd ``` **docker-compose.yml** ```yaml version: "3.8" services: satellite: build: wyoming-satellite # https://github.com/rhasspy/wyoming-satellite ports: - "10700:10700" command: - "--name" - "my satellite" - "--mic-uri" - "tcp://192.168.1.23:10600" - "--snd-uri" - "tcp://192.168.1.23:10601" - "--debug" ``` ## Wyoming External Source **go2rtc.yaml** ```yaml streams: wyoming_external: - wyoming://192.168.1.23:10600 - wyoming://192.168.1.23:10601?backchannel=1 ``` **docker-compose.yml** ```yaml version: "3.8" services: microphone: build: wyoming-mic-external # https://github.com/rhasspy/wyoming-mic-external ports: - "10600:10600" devices: - /dev/snd:/dev/snd group_add: - audio command: - "--device" - "sysdefault" - "--debug" playback: build: wyoming-snd-external # https://github.com/rhasspy/wyoming-snd-external ports: - "10601:10601" devices: - /dev/snd:/dev/snd group_add: - audio command: - "--device" - "sysdefault" - "--debug" ``` ## Debug ```yaml log: wyoming: trace ``` ================================================ FILE: internal/wyoming/wyoming.go ================================================ package wyoming import ( "net" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/wyoming" "github.com/rs/zerolog" ) func Init() { streams.HandleFunc("wyoming", wyoming.Dial) // server var cfg struct { Mod map[string]struct { Listen string `yaml:"listen"` Name string `yaml:"name"` Mode string `yaml:"mode"` Event map[string]string `yaml:"event"` WakeURI string `yaml:"wake_uri"` VADThreshold float32 `yaml:"vad_threshold"` } `yaml:"wyoming"` } app.LoadConfig(&cfg) log = app.GetLogger("wyoming") for name, cfg := range cfg.Mod { stream := streams.Get(name) if stream == nil { log.Warn().Msgf("[wyoming] missing stream: %s", name) continue } if cfg.Name == "" { cfg.Name = name } srv := &wyoming.Server{ Name: cfg.Name, Event: cfg.Event, VADThreshold: int16(1000 * cfg.VADThreshold), // 1.0 => 1000 WakeURI: cfg.WakeURI, MicHandler: func(cons core.Consumer) error { if err := stream.AddConsumer(cons); err != nil { return err } // not best solution if i, ok := cons.(interface{ OnClose(func()) }); ok { i.OnClose(func() { stream.RemoveConsumer(cons) }) } return nil }, SndHandler: func(prod core.Producer) error { return stream.Play(prod) }, Trace: func(format string, v ...any) { log.Trace().Msgf("[wyoming] "+format, v...) }, Error: func(format string, v ...any) { log.Error().Msgf("[wyoming] "+format, v...) }, } go serve(srv, cfg.Mode, cfg.Listen) } } var log zerolog.Logger func serve(srv *wyoming.Server, mode, address string) { ln, err := net.Listen("tcp", address) if err != nil { log.Warn().Err(err).Msgf("[wyoming] listen") } for { conn, err := ln.Accept() if err != nil { return } go handle(srv, mode, conn) } } func handle(srv *wyoming.Server, mode string, conn net.Conn) { addr := conn.RemoteAddr() log.Trace().Msgf("[wyoming] %s connected", addr) switch mode { case "mic": srv.HandleMic(conn) case "snd": srv.HandleSnd(conn) default: srv.Handle(conn) } log.Trace().Msgf("[wyoming] %s disconnected", addr) } ================================================ FILE: internal/wyze/README.md ================================================ # Wyze [`new in v1.9.14`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.14) by [@seydx](https://github.com/seydx) This source allows you to stream from [Wyze](https://wyze.com/) cameras using native P2P protocol without the Wyze app or SDK. **Important:** 1. **Requires Wyze account**. You need to login once via the WebUI to load your cameras. 2. **Requires firmware with DTLS**. Only cameras with DTLS-enabled firmware are supported. 3. Internet access is only needed when loading cameras from your account. After that, all streaming is local P2P. 4. Connection to the camera is local only (direct P2P to camera IP). **Features:** - H.264 and H.265 video codec support - AAC, G.711, PCM, and Opus audio codec support - Two-way audio (intercom) support - Resolution switching (HD/SD) ## Setup 1. Get your API Key from [Wyze Developer Portal](https://support.wyze.com/hc/en-us/articles/16129834216731) 2. Go to go2rtc WebUI > Add > Wyze 3. Enter your API ID, API Key, email, and password 4. Select cameras to add - stream URLs are generated automatically **Example Config** ```yaml wyze: user@email.com: api_id: "your-api-id" api_key: "your-api-key" password: "yourpassword" # or MD5 triple-hash with "md5:" prefix streams: wyze_cam: wyze://192.168.1.123?uid=WYZEUID1234567890AB&enr=xxx&mac=AABBCCDDEEFF&model=HL_CAM4&dtls=true ``` ## Stream URL Format The stream URL is automatically generated when you add cameras via the WebUI: ``` wyze://[IP]?uid=[P2P_ID]&enr=[ENR]&mac=[MAC]&model=[MODEL]&subtype=[hd|sd]&dtls=true ``` | Parameter | Description | |-----------|-------------------------------------------------| | `IP` | Camera's local IP address | | `uid` | P2P identifier (20 chars) | | `enr` | Encryption key for DTLS | | `mac` | Device MAC address | | `model` | Camera model (e.g., HL_CAM4) | | `dtls` | Enable DTLS encryption (default: true) | | `subtype` | Camera resolution: `hd` or `sd` (default: `hd`) | ## Configuration ### Resolution You can change the camera's resolution using the `subtype` parameter: ```yaml streams: wyze_hd: wyze://...&subtype=hd wyze_sd: wyze://...&subtype=sd ``` ### Two-Way Audio Two-way audio (intercom) is supported automatically. When a consumer sends audio to the stream, it will be transmitted to the camera's speaker. ## Camera Compatibility | Name | Model | Firmware | Protocol | Encryption | Codecs | |-----------------------------|----------------|--------------|----------|------------|------------| | Wyze Cam v4 | HL_CAM4 | 4.52.9.4188 | TUTK | TransCode | h264, aac | | | | 4.52.9.5332 | TUTK | HMAC-SHA1 | h264, aac | | Wyze Cam v3 Pro | | | TUTK | | | | Wyze Cam v3 | WYZE_CAKP2JFUS | 4.36.14.3497 | TUTK | TransCode | h264, pcm | | Wyze Cam v2 | WYZEC1-JZ | 4.9.9.3006 | TUTK | TransCode | h264, pcmu | | Wyze Cam v1 | | | TUTK | | | | Wyze Cam Pan v4 | | | Gwell* | | | | Wyze Cam Pan v3 | | | TUTK | | | | Wyze Cam Pan v2 | | | TUTK | | | | Wyze Cam Pan v1 | | | TUTK | | | | Wyze Cam OG | | | Gwell* | | | | Wyze Cam OG Telephoto | | | Gwell* | | | | Wyze Cam OG (2025) | | | Gwell* | | | | Wyze Cam Outdoor v2 | | | TUTK | | | | Wyze Cam Outdoor v1 | | | TUTK | | | | Wyze Cam Floodlight Pro | | | ? | | | | Wyze Cam Floodlight v2 | | | TUTK | | | | Wyze Cam Floodlight | | | TUTK | | | | Wyze Video Doorbell v2 | HL_DB2 | 4.51.3.4992 | TUTK | TransCode | h264, pcm | | Wyze Video Doorbell v1 | | | TUTK | | | | Wyze Video Doorbell Pro | | | ? | | | | Wyze Battery Video Doorbell | | | ? | | | | Wyze Duo Cam Doorbell | | | ? | | | | Wyze Battery Cam Pro | | | ? | | | | Wyze Solar Cam Pan | | | ? | | | | Wyze Duo Cam Pan | | | ? | | | | Wyze Window Cam | | | ? | | | | Wyze Bulb Cam | | | ? | | | _* Gwell based protocols are not yet supported._ ================================================ FILE: internal/wyze/wyze.go ================================================ package wyze import ( "encoding/json" "fmt" "net/http" "net/url" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/wyze" ) type AccountConfig struct { APIKey string `yaml:"api_key"` APIID string `yaml:"api_id"` Password string `yaml:"password"` } var accounts map[string]AccountConfig func Init() { var v struct { Cfg map[string]AccountConfig `yaml:"wyze"` } app.LoadConfig(&v) accounts = v.Cfg log := app.GetLogger("wyze") streams.HandleFunc("wyze", func(rawURL string) (core.Producer, error) { log.Debug().Msgf("wyze: dial %s", rawURL) return wyze.NewProducer(rawURL) }) api.HandleFunc("api/wyze", apiWyze) } func getCloud(email string) (*wyze.Cloud, error) { cfg, ok := accounts[email] if !ok { return nil, fmt.Errorf("wyze: account not found: %s", email) } if cfg.APIKey == "" || cfg.APIID == "" { return nil, fmt.Errorf("wyze: api_key and api_id required for account: %s", email) } cloud := wyze.NewCloud(cfg.APIKey, cfg.APIID) if err := cloud.Login(email, cfg.Password); err != nil { return nil, err } return cloud, nil } func apiWyze(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": apiDeviceList(w, r) case "POST": apiAuth(w, r) } } func apiDeviceList(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() email := query.Get("id") if email == "" { accountList := make([]string, 0, len(accounts)) for id := range accounts { accountList = append(accountList, id) } api.ResponseJSON(w, accountList) return } err := func() error { cloud, err := getCloud(email) if err != nil { return err } cameras, err := cloud.GetCameraList() if err != nil { return err } var items []*api.Source for _, cam := range cameras { items = append(items, &api.Source{ Name: cam.Nickname, Info: fmt.Sprintf("%s | %s | %s", cam.ProductModel, cam.MAC, cam.IP), URL: buildStreamURL(cam), }) } api.ResponseSources(w, items) return nil }() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } func apiAuth(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } email := r.Form.Get("email") password := r.Form.Get("password") apiKey := r.Form.Get("api_key") apiID := r.Form.Get("api_id") if email == "" || password == "" || apiKey == "" || apiID == "" { http.Error(w, "email, password, api_key and api_id required", http.StatusBadRequest) return } // Try to login cloud := wyze.NewCloud(apiKey, apiID) if err := cloud.Login(email, password); err != nil { // Check for MFA error var authErr *wyze.AuthError if ok := isAuthError(err, &authErr); ok { w.Header().Set("Content-Type", api.MimeJSON) w.WriteHeader(http.StatusUnauthorized) _ = json.NewEncoder(w).Encode(authErr) return } http.Error(w, err.Error(), http.StatusInternalServerError) return } cfg := map[string]string{ "password": password, "api_key": apiKey, "api_id": apiID, } if err := app.PatchConfig([]string{"wyze", email}, cfg); err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } if accounts == nil { accounts = make(map[string]AccountConfig) } accounts[email] = AccountConfig{ APIKey: apiKey, APIID: apiID, Password: password, } cameras, err := cloud.GetCameraList() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) return } var items []*api.Source for _, cam := range cameras { items = append(items, &api.Source{ Name: cam.Nickname, Info: fmt.Sprintf("%s | %s | %s", cam.ProductModel, cam.MAC, cam.IP), URL: buildStreamURL(cam), }) } api.ResponseSources(w, items) } func buildStreamURL(cam *wyze.Camera) string { query := url.Values{} query.Set("uid", cam.P2PID) query.Set("enr", cam.ENR) query.Set("mac", cam.MAC) query.Set("model", cam.ProductModel) if cam.DTLS == 1 { query.Set("dtls", "true") } return fmt.Sprintf("wyze://%s?%s", cam.IP, query.Encode()) } func isAuthError(err error, target **wyze.AuthError) bool { if e, ok := err.(*wyze.AuthError); ok { *target = e return true } return false } ================================================ FILE: internal/xiaomi/README.md ================================================ # Xiaomi Mi Home [`new in v1.9.13`](https://github.com/AlexxIT/go2rtc/releases/tag/v1.9.13) This source allows you to view cameras from the [Xiaomi Mi Home](https://home.mi.com/) ecosystem. Since 2020, Xiaomi has introduced a unified protocol for cameras called `miss`. I think it means **Mi Secure Streaming**. Until this point, the camera protocols were in chaos. Almost every model had different authorization, encryption, command lists, and media packet formats. go2rtc supports two formats: `xiaomi/mess` and `xiaomi/legacy`. And multiple P2P protocols: `cs2+udp`, `cs2+tcp`, several versions of `tutk+udp`. Almost all cameras in the `xiaomi/mess` format and the `cs2` protocol work well. Older `xiaomi/legacy` format cameras may have support issues. The `tutk` protocol is the worst thing that's ever happened to the P2P world. It works terribly. **Important:** 1. **Not all cameras are supported**. The list of supported cameras is collected in [this issue](https://github.com/AlexxIT/go2rtc/issues/1982). 2. Each time you connect to the camera, you need Internet access to obtain encryption keys. 3. Connection to the camera is local only. **Features:** - Multiple Xiaomi accounts supported - Cameras from multiple regions are supported for a single account - Two-way audio is supported - Cameras with multiple lenses are supported ## Setup 1. Go to go2rtc WebUI > Add > Xiaomi > Login with username and password 2. Receive verification code by email or phone if required. 3. Complete the captcha if required. 4. If everything is OK, your account will be added, and you can load cameras from it. **Example** ```yaml xiaomi: 1234567890: V1:*** streams: xiaomi1: xiaomi://1234567890:cn@192.168.1.123?did=9876543210&model=isa.camera.hlc7 ``` ## Configuration Quality in the `miss` protocol is specified by a number from 0 to 5. Usually 0 means auto, 1 - sd, 2 - hd. Go2rtc by default sets quality to 2. But some new cameras have HD quality at number 3. Old cameras may have broken codec settings at number 3, so this number should not be set for all cameras. You can change camera quality: `subtype=hd/sd/auto/0-5`. ```yaml streams: xiaomi1: xiaomi://***&subtype=sd ``` You can use a second channel for dual cameras: `channel=2`. ```yaml streams: xiaomi1: xiaomi://***&channel=2 ``` ================================================ FILE: internal/xiaomi/xiaomi.go ================================================ package xiaomi import ( "encoding/hex" "encoding/json" "errors" "fmt" "net/http" "net/url" "strings" "sync" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/xiaomi" "github.com/AlexxIT/go2rtc/pkg/xiaomi/crypto" "github.com/rs/zerolog" ) func Init() { var v struct { Cfg map[string]string `yaml:"xiaomi"` } app.LoadConfig(&v) tokens = v.Cfg log = app.GetLogger("xiaomi") streams.HandleFunc("xiaomi", func(rawURL string) (core.Producer, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } if u.User != nil { rawURL, err = getCameraURL(u) if err != nil { return nil, err } } log.Debug().Msgf("xiaomi: dial %s", rawURL) return xiaomi.Dial(rawURL) }) api.HandleFunc("api/xiaomi", apiXiaomi) } var log zerolog.Logger var tokens map[string]string var clouds map[string]*xiaomi.Cloud var cloudsMu sync.Mutex func getCloud(userID string) (*xiaomi.Cloud, error) { cloudsMu.Lock() defer cloudsMu.Unlock() if cloud := clouds[userID]; cloud != nil { return cloud, nil } cloud := xiaomi.NewCloud(AppXiaomiHome) if err := cloud.LoginWithToken(userID, tokens[userID]); err != nil { return nil, err } if clouds == nil { clouds = map[string]*xiaomi.Cloud{userID: cloud} } else { clouds[userID] = cloud } return cloud, nil } func cloudRequest(userID, region, apiURL, params string) ([]byte, error) { cloud, err := getCloud(userID) if err != nil { return nil, err } return cloud.Request(GetBaseURL(region), apiURL, params, nil) } func cloudUserRequest(user *url.Userinfo, apiURL, params string) ([]byte, error) { userID := user.Username() region, _ := user.Password() return cloudRequest(userID, region, apiURL, params) } func getCameraURL(url *url.URL) (string, error) { model := url.Query().Get("model") // It is not known which models need to be awakened. // Probably all the doorbells and all the battery cameras. if strings.Contains(model, ".cateye.") { _ = wakeUpCamera(url) } // The getMissURL request has a fallback to getP2PURL. // But for known models we can save one request to the cloud. if xiaomi.IsLegacy(model) { return getLegacyURL(url) } return getMissURL(url) } func getLegacyURL(url *url.URL) (string, error) { query := url.Query() clientPublic, clientPrivate, err := crypto.GenerateKey() if err != nil { return "", err } params := fmt.Sprintf(`{"did":"%s","toSignAppData":"%x"}`, query.Get("did"), clientPublic) userID := url.User.Username() region, _ := url.User.Password() res, err := cloudRequest(userID, region, "/device/devicepass", params) if err != nil { return "", err } var v struct { UID string `json:"p2p_id"` Password string `json:"password"` PublicKey string `json:"p2p_dev_public_key"` Sign string `json:"signForAppData"` } if err = json.Unmarshal(res, &v); err != nil { return "", err } query.Set("uid", v.UID) if v.Sign != "" { query.Set("client_public", hex.EncodeToString(clientPublic)) query.Set("client_private", hex.EncodeToString(clientPrivate)) query.Set("device_public", v.PublicKey) query.Set("sign", v.Sign) } else { query.Set("password", v.Password) } url.RawQuery = query.Encode() return url.String(), nil } func getMissURL(url *url.URL) (string, error) { clientPublic, clientPrivate, err := crypto.GenerateKey() if err != nil { return "", err } query := url.Query() params := fmt.Sprintf( `{"app_pubkey":"%x","did":"%s","support_vendors":"TUTK_CS2_MTP"}`, clientPublic, query.Get("did"), ) res, err := cloudUserRequest(url.User, "/v2/device/miss_get_vendor", params) if err != nil { if strings.Contains(err.Error(), "no available vendor support") { return getLegacyURL(url) } return "", err } var v struct { Vendor struct { ID byte `json:"vendor"` Params struct { UID string `json:"p2p_id"` } `json:"vendor_params"` } `json:"vendor"` PublicKey string `json:"public_key"` Sign string `json:"sign"` } if err = json.Unmarshal(res, &v); err != nil { return "", err } query.Set("client_public", hex.EncodeToString(clientPublic)) query.Set("client_private", hex.EncodeToString(clientPrivate)) query.Set("device_public", v.PublicKey) query.Set("sign", v.Sign) query.Set("vendor", getVendorName(v.Vendor.ID)) if v.Vendor.ID == 1 { query.Set("uid", v.Vendor.Params.UID) } url.RawQuery = query.Encode() return url.String(), nil } func getVendorName(i byte) string { switch i { case 1: return "tutk" case 3: return "agora" case 4: return "cs2" case 6: return "mtp" } return fmt.Sprintf("%d", i) } func wakeUpCamera(url *url.URL) error { const params = `{"id":1,"method":"wakeup","params":{"video":"1"}}` did := url.Query().Get("did") _, err := cloudUserRequest(url.User, "/home/rpc/"+did, params) return err } func apiXiaomi(w http.ResponseWriter, r *http.Request) { switch r.Method { case "GET": apiDeviceList(w, r) case "POST": apiAuth(w, r) } } func apiDeviceList(w http.ResponseWriter, r *http.Request) { query := r.URL.Query() user := query.Get("id") if user == "" { cloudsMu.Lock() users := make([]string, 0, len(tokens)) for s := range tokens { users = append(users, s) } cloudsMu.Unlock() api.ResponseJSON(w, users) return } err := func() error { region := query.Get("region") res, err := cloudRequest(user, region, "/v2/home/device_list_page", "{}") if err != nil { return err } var v struct { List []*Device `json:"list"` } log.Trace().Str("user", user).Msgf("[xiaomi] devices list: %s", res) if err = json.Unmarshal(res, &v); err != nil { return err } var items []*api.Source for _, device := range v.List { if !device.HasCamera() { continue } items = append(items, &api.Source{ Name: device.Name, Info: fmt.Sprintf("ip: %s, mac: %s", device.IP, device.MAC), URL: fmt.Sprintf("xiaomi://%s:%s@%s?did=%s&model=%s", user, region, device.IP, device.Did, device.Model), }) } api.ResponseSources(w, items) return nil }() if err != nil { http.Error(w, err.Error(), http.StatusInternalServerError) } } type Device struct { Did string `json:"did"` Name string `json:"name"` Model string `json:"model"` MAC string `json:"mac"` IP string `json:"localip"` } func (d *Device) HasCamera() bool { return strings.Contains(d.Model, ".camera.") || strings.Contains(d.Model, ".cateye.") || strings.Contains(d.Model, ".feeder.") } var auth *xiaomi.Cloud func apiAuth(w http.ResponseWriter, r *http.Request) { if err := r.ParseForm(); err != nil { http.Error(w, err.Error(), http.StatusBadRequest) return } username := r.Form.Get("username") password := r.Form.Get("password") captcha := r.Form.Get("captcha") verify := r.Form.Get("verify") var err error switch { case username != "" || password != "": auth = xiaomi.NewCloud(AppXiaomiHome) err = auth.Login(username, password) case captcha != "": err = auth.LoginWithCaptcha(captcha) case verify != "": err = auth.LoginWithVerify(verify) default: http.Error(w, "wrong request", http.StatusBadRequest) return } if err == nil { userID, token := auth.UserToken() auth = nil cloudsMu.Lock() if tokens == nil { tokens = map[string]string{userID: token} } else { tokens[userID] = token } cloudsMu.Unlock() err = app.PatchConfig([]string{"xiaomi", userID}, token) } if err != nil { var login *xiaomi.LoginError if errors.As(err, &login) { w.Header().Set("Content-Type", api.MimeJSON) w.WriteHeader(http.StatusUnauthorized) _ = json.NewEncoder(w).Encode(err) return } http.Error(w, err.Error(), http.StatusInternalServerError) } } const AppXiaomiHome = "xiaomiio" func GetBaseURL(region string) string { switch region { case "de", "i2", "ru", "sg", "us": return "https://" + region + ".api.io.mi.com/app" } return "https://api.io.mi.com/app" } ================================================ FILE: internal/yandex/README.md ================================================ # Yandex Source for receiving stream from new [Yandex IP camera](https://alice.yandex.ru/smart-home/security/ipcamera). ## Get Yandex token 1. Install HomeAssistant integration [YandexStation](https://github.com/AlexxIT/YandexStation). 2. Copy token from HomeAssistant config folder: `/config/.storage/core.config_entries`, key: `"x_token"`. ## Get device ID 1. Open this link in any browser: https://iot.quasar.yandex.ru/m/v3/user/devices 2. Copy ID of your camera, key: `"id"`. ## Configuration ```yaml streams: yandex_stream: yandex:?x_token=XXXX&device_id=XXXX yandex_snapshot: yandex:?x_token=XXXX&device_id=XXXX&snapshot yandex_snapshot_custom_size: yandex:?x_token=XXXX&device_id=XXXX&snapshot=h=540 ``` ================================================ FILE: internal/yandex/goloom.go ================================================ package yandex import ( "encoding/json" "errors" "fmt" "time" "github.com/AlexxIT/go2rtc/internal/webrtc" "github.com/AlexxIT/go2rtc/pkg/core" xwebrtc "github.com/AlexxIT/go2rtc/pkg/webrtc" "github.com/google/uuid" "github.com/gorilla/websocket" pion "github.com/pion/webrtc/v4" ) func goloomClient(serviceURL, serviceName, roomId, participantId, credentials string) (core.Producer, error) { conn, _, err := websocket.DefaultDialer.Dial(serviceURL, nil) if err != nil { return nil, err } defer func() { time.Sleep(time.Second) _ = conn.Close() }() s := fmt.Sprintf(`{"hello": { "credentials":"%s","participantId":"%s","roomId":"%s","serviceName":"%s","sdkInitializationId":"%s", "capabilitiesOffer":{},"sendAudio":false,"sendSharing":false,"sendVideo":false, "sdkInfo":{"hwConcurrency":4,"implementation":"browser","version":"5.4.0"}, "participantAttributes":{"description":"","name":"mike","role":"SPEAKER"}, "participantMeta":{"description":"","name":"mike","role":"SPEAKER","sendAudio":false,"sendVideo":false} },"uid":"%s"}`, credentials, participantId, roomId, serviceName, uuid.NewString(), uuid.NewString(), ) err = conn.WriteMessage(websocket.TextMessage, []byte(s)) if err != nil { return nil, err } if _, _, err = conn.ReadMessage(); err != nil { return nil, err } pc, err := webrtc.PeerConnection(true) if err != nil { return nil, err } prod := xwebrtc.NewConn(pc) prod.FormatName = "yandex" prod.Mode = core.ModeActiveProducer prod.Protocol = "wss" var connState core.Waiter prod.Listen(func(msg any) { switch msg := msg.(type) { case pion.PeerConnectionState: switch msg { case pion.PeerConnectionStateConnecting: case pion.PeerConnectionStateConnected: connState.Done(nil) default: connState.Done(errors.New("webrtc: " + msg.String())) } } }) go func() { for { var msg map[string]json.RawMessage if err = conn.ReadJSON(&msg); err != nil { return } for k, v := range msg { switch k { case "uid": continue case "serverHello": case "subscriberSdpOffer": var sdp subscriberSdp if err = json.Unmarshal(v, &sdp); err != nil { return } //log.Trace().Msgf("offer:\n%s", sdp.Sdp) if err = prod.SetOffer(sdp.Sdp); err != nil { return } if sdp.Sdp, err = prod.GetAnswer(); err != nil { return } //log.Trace().Msgf("answer:\n%s", sdp.Sdp) var raw []byte if raw, err = json.Marshal(sdp); err != nil { return } s = fmt.Sprintf(`{"uid":"%s","subscriberSdpAnswer":%s}`, uuid.NewString(), raw) if err = conn.WriteMessage(websocket.TextMessage, []byte(s)); err != nil { return } case "webrtcIceCandidate": var candidate webrtcIceCandidate if err = json.Unmarshal(v, &candidate); err != nil { return } if err = prod.AddCandidate(candidate.Candidate); err != nil { return } } //log.Trace().Msgf("%s : %s", k, v) } if msg["ack"] != nil { continue } s = fmt.Sprintf(`{"uid":%s,"ack":{"status":{"code":"OK"}}}`, msg["uid"]) if err = conn.WriteMessage(websocket.TextMessage, []byte(s)); err != nil { return } } }() if err = connState.Wait(); err != nil { return nil, err } s = fmt.Sprintf(`{"uid":"%s","setSlots":{"slots":[{"width":0,"height":0}],"audioSlotsCount":0,"key":1,"shutdownAllVideo":false,"withSelfView":false,"selfViewVisibility":"ON_LOADING_THEN_HIDE","gridConfig":{}}}`, uuid.NewString()) if err = conn.WriteMessage(websocket.TextMessage, []byte(s)); err != nil { return nil, err } return prod, nil } type subscriberSdp struct { PcSeq int `json:"pcSeq"` Sdp string `json:"sdp"` } type webrtcIceCandidate struct { PcSeq int `json:"pcSeq"` Target string `json:"target"` Candidate string `json:"candidate"` SdpMid string `json:"sdpMid"` SdpMlineIndex int `json:"sdpMlineIndex"` } ================================================ FILE: internal/yandex/yandex.go ================================================ package yandex import ( "net/url" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/yandex" ) func Init() { streams.HandleFunc("yandex", func(source string) (core.Producer, error) { u, err := url.Parse(source) if err != nil { return nil, err } query := u.Query() token := query.Get("x_token") session, err := yandex.GetSession(token) if err != nil { return nil, err } deviceID := query.Get("device_id") if query.Has("snapshot") { rawURL, err := session.GetSnapshotURL(deviceID) if err != nil { return nil, err } rawURL += "/current.jpg?" + query.Get("snapshot") + "#header=Cookie:" + session.GetCookieString(rawURL) return streams.GetProducer(rawURL) } room, err := session.WebrtcCreateRoom(deviceID) if err != nil { return nil, err } return goloomClient(room.ServiceUrl, room.ServiceName, room.RoomId, room.ParticipantId, room.Credentials) }) } ================================================ FILE: main.go ================================================ package main import ( "slices" "github.com/AlexxIT/go2rtc/internal/alsa" "github.com/AlexxIT/go2rtc/internal/api" "github.com/AlexxIT/go2rtc/internal/api/ws" "github.com/AlexxIT/go2rtc/internal/app" "github.com/AlexxIT/go2rtc/internal/bubble" "github.com/AlexxIT/go2rtc/internal/debug" "github.com/AlexxIT/go2rtc/internal/doorbird" "github.com/AlexxIT/go2rtc/internal/dvrip" "github.com/AlexxIT/go2rtc/internal/echo" "github.com/AlexxIT/go2rtc/internal/eseecloud" "github.com/AlexxIT/go2rtc/internal/exec" "github.com/AlexxIT/go2rtc/internal/expr" "github.com/AlexxIT/go2rtc/internal/ffmpeg" "github.com/AlexxIT/go2rtc/internal/flussonic" "github.com/AlexxIT/go2rtc/internal/gopro" "github.com/AlexxIT/go2rtc/internal/hass" "github.com/AlexxIT/go2rtc/internal/hls" "github.com/AlexxIT/go2rtc/internal/homekit" "github.com/AlexxIT/go2rtc/internal/http" "github.com/AlexxIT/go2rtc/internal/isapi" "github.com/AlexxIT/go2rtc/internal/ivideon" "github.com/AlexxIT/go2rtc/internal/kasa" "github.com/AlexxIT/go2rtc/internal/mjpeg" "github.com/AlexxIT/go2rtc/internal/mp4" "github.com/AlexxIT/go2rtc/internal/mpeg" "github.com/AlexxIT/go2rtc/internal/multitrans" "github.com/AlexxIT/go2rtc/internal/nest" "github.com/AlexxIT/go2rtc/internal/ngrok" "github.com/AlexxIT/go2rtc/internal/onvif" "github.com/AlexxIT/go2rtc/internal/pinggy" "github.com/AlexxIT/go2rtc/internal/ring" "github.com/AlexxIT/go2rtc/internal/roborock" "github.com/AlexxIT/go2rtc/internal/rtmp" "github.com/AlexxIT/go2rtc/internal/rtsp" "github.com/AlexxIT/go2rtc/internal/srtp" "github.com/AlexxIT/go2rtc/internal/streams" "github.com/AlexxIT/go2rtc/internal/tapo" "github.com/AlexxIT/go2rtc/internal/tuya" "github.com/AlexxIT/go2rtc/internal/v4l2" "github.com/AlexxIT/go2rtc/internal/webrtc" "github.com/AlexxIT/go2rtc/internal/webtorrent" "github.com/AlexxIT/go2rtc/internal/wyoming" "github.com/AlexxIT/go2rtc/internal/wyze" "github.com/AlexxIT/go2rtc/internal/xiaomi" "github.com/AlexxIT/go2rtc/internal/yandex" "github.com/AlexxIT/go2rtc/pkg/shell" ) func main() { // version will be set later from -buildvcs info, this used only as fallback app.Version = "1.9.14" type module struct { name string init func() } modules := []module{ {"", app.Init}, // init config and logs {"api", api.Init}, // init API before all others {"ws", ws.Init}, // init WS API endpoint {"", streams.Init}, // Main sources and servers {"http", http.Init}, // rtsp source, HTTP server {"rtsp", rtsp.Init}, // rtsp source, RTSP server {"webrtc", webrtc.Init}, // webrtc source, WebRTC server // Main API {"mp4", mp4.Init}, // MP4 API {"hls", hls.Init}, // HLS API {"mjpeg", mjpeg.Init}, // MJPEG API // Other sources and servers {"hass", hass.Init}, // hass source, Hass API server {"homekit", homekit.Init}, // homekit source, HomeKit server {"onvif", onvif.Init}, // onvif source, ONVIF API server {"rtmp", rtmp.Init}, // rtmp source, RTMP server {"webtorrent", webtorrent.Init}, // webtorrent source, WebTorrent module {"wyoming", wyoming.Init}, // Exec and script sources {"echo", echo.Init}, {"exec", exec.Init}, {"expr", expr.Init}, {"ffmpeg", ffmpeg.Init}, // Hardware sources {"alsa", alsa.Init}, {"v4l2", v4l2.Init}, // Other sources {"bubble", bubble.Init}, {"doorbird", doorbird.Init}, {"dvrip", dvrip.Init}, {"eseecloud", eseecloud.Init}, {"flussonic", flussonic.Init}, {"gopro", gopro.Init}, {"isapi", isapi.Init}, {"ivideon", ivideon.Init}, {"kasa", kasa.Init}, {"mpegts", mpeg.Init}, {"multitrans", multitrans.Init}, {"nest", nest.Init}, {"ring", ring.Init}, {"roborock", roborock.Init}, {"tapo", tapo.Init}, {"tuya", tuya.Init}, {"wyze", wyze.Init}, {"xiaomi", xiaomi.Init}, {"yandex", yandex.Init}, // Helper modules {"debug", debug.Init}, {"ngrok", ngrok.Init}, {"pinggy", pinggy.Init}, {"srtp", srtp.Init}, } for _, m := range modules { if app.Modules == nil || m.name == "" || slices.Contains(app.Modules, m.name) { m.init() } } shell.RunUntilSignal() } ================================================ FILE: package.json ================================================ { "devDependencies": { "@types/node": "^25.2.0", "eslint": "^9.39.2", "eslint-plugin-html": "^8.1.4", "vitepress": "^2.0.0-alpha.16" }, "scripts": { "docs:dev": "vitepress dev website --host", "docs:build": "vitepress build website", "docs:preview": "vitepress preview website" }, "eslintConfig": { "env": { "browser": true, "es6": true }, "parserOptions": { "ecmaVersion": 2017, "sourceType": "module" }, "rules": { "no-var": "error", "no-undef": "error", "no-unused-vars": "warn", "prefer-const": "error", "quotes": [ "error", "single" ], "semi": "error" }, "plugins": [ "html" ], "overrides": [ { "files": [ "*.html" ], "parserOptions": { "sourceType": "script" } } ] } } ================================================ FILE: pkg/README.md ================================================ # Notes go2rtc tries to name formats, protocols and codecs the same way they are named in FFmpeg. Some formats and protocols go2rtc supports exclusively. They have no equivalent in FFmpeg. ## Producers (input) - The initiator of the connection can be go2rtc - **Source protocols** - The initiator of the connection can be an external program - **Ingress protocols** - Codecs can be incoming - **Receiver codecs** - Codecs can be outgoing (two way audio) - **Sender codecs** | Group | Format | Protocols | Ingress | Receiver codecs | Sender codecs | Example | |------------|--------------|-----------------|---------|---------------------------------|---------------------|---------------| | Devices | alsa | pipe | | | pcm | `alsa:` | | Devices | v4l2 | pipe | | | | `v4l2:` | | Files | adts | http, tcp, pipe | http | aac | | `http:` | | Files | flv | http, tcp, pipe | http | h264, aac | | `http:` | | Files | h264 | http, tcp, pipe | http | h264 | | `http:` | | Files | hevc | http, tcp, pipe | http | hevc | | `http:` | | Files | hls | http | | h264, h265, aac, opus | | `http:` | | Files | mjpeg | http, tcp, pipe | http | mjpeg | | `http:` | | Files | mpegts | http, tcp, pipe | http | h264, hevc, aac, opus | | `http:` | | Files | wav | http, tcp, pipe | http | pcm_alaw, pcm_mulaw | | `http:` | | Net (pub) | mpjpeg | http, tcp, pipe | http | mjpeg | | `http:` | | Net (pub) | onvif | rtsp | | | | `onvif:` | | Net (pub) | rtmp | rtmp | rtmp | h264, aac | | `rtmp:` | | Net (pub) | rtsp | rtsp, ws | rtsp | h264, hevc, aac, pcm*, opus | pcm*, opus | `rtsp:` | | Net (pub) | webrtc* | webrtc | webrtc | h264, pcm_alaw, pcm_mulaw, opus | pcm_alaw, pcm_mulaw | `webrtc:` | | Net (pub) | yuv4mpegpipe | http, tcp, pipe | http | rawvideo | | `http:` | | Net (priv) | bubble | http | | h264, hevc, pcm_alaw | | `bubble:` | | Net (priv) | doorbird | http | | | | `doorbird:` | | Net (priv) | dvrip | tcp | | h264, hevc, pcm_alaw, pcm_mulaw | pcm_alaw | `dvrip:` | | Net (priv) | eseecloud | http | | | | `eseecloud:` | | Net (priv) | gopro | udp | | TODO | | `gopro:` | | Net (priv) | hass | webrtc | | TODO | | `hass:` | | Net (priv) | homekit | hap | | h264, eld* | | `homekit:` | | Net (priv) | isapi | http | | | pcm_alaw, pcm_mulaw | `isapi:` | | Net (priv) | kasa | http | | h264, pcm_mulaw | | `kasa:` | | Net (priv) | nest | rtsp, webrtc | | TODO | | `nest:` | | Net (priv) | ring | webrtc | | | | `ring:` | | Net (priv) | roborock | webrtc | | h264, opus | opus | `roborock:` | | Net (priv) | tapo | http | | h264, pcma | pcm_alaw | `tapo:` | | Net (priv) | tuya | webrtc | | | | `tuya:` | | Net (priv) | vigi | http | | | | `vigi:` | | Net (priv) | webtorrent | webrtc | TODO | TODO | TODO | `webtorrent:` | | Net (priv) | xiaomi* | cs2, tutk | | | | `xiaomi:` | | Services | flussonic | ws | | | | `flussonic:` | | Services | ivideon | ws | | h264 | | `ivideon:` | | Services | yandex | webrtc | | | | `yandex:` | | Other | echo | * | | | | `echo:` | | Other | exec | pipe, rtsp | | | | `exec:` | | Other | expr | * | | | | `expr:` | | Other | ffmpeg | pipe, rtsp | | | | `ffmpeg:` | | Other | stdin | pipe | | | pcm_alaw, pcm_mulaw | `stdin:` | - **eld** - rare variant of aac codec - **pcm** - pcm_alaw pcm_mulaw pcm_s16be pcm_s16le - **webrtc** - webrtc/kinesis, webrtc/openipc, webrtc/milestone, webrtc/wyze, webrtc/whep ## Consumers (output) | Format | Protocol | Send codecs | Recv codecs | Example | |--------------|----------|---------------------------------|---------------------------|---------------------------------------| | adts | http | aac | | `GET /api/stream.adts` | | ascii | http | mjpeg | | `GET /api/stream.ascii` | | flv | http | h264, aac | | `GET /api/stream.flv` | | hls/mpegts | http | h264, hevc, aac | | `GET /api/stream.m3u8` | | hls/fmp4 | http | h264, hevc, aac, pcm*, opus | | `GET /api/stream.m3u8?mp4` | | homekit | hap | h264, opus | | Apple HomeKit app | | mjpeg | ws | mjpeg | | `{"type":"mjpeg"}` -> `/api/ws` | | mpjpeg | http | mjpeg | | `GET /api/stream.mjpeg` | | mp4 | http | h264, hevc, aac, pcm*, opus | | `GET /api/stream.mp4` | | mse/fmp4 | ws | h264, hevc, aac, pcm*, opus | | `{"type":"mse"}` -> `/api/ws` | | mpegts | http | h264, hevc, aac | | `GET /api/stream.ts` | | rtmp | rtmp | h264, aac | | `rtmp://localhost:1935/{stream_name}` | | rtsp | rtsp | h264, hevc, aac, pcm*, opus | | `rtsp://localhost:8554/{stream_name}` | | webrtc | webrtc | h264, pcm_alaw, pcm_mulaw, opus | pcm_alaw, pcm_mulaw, opus | `{"type":"webrtc"}` -> `/api/ws` | | yuv4mpegpipe | http | rawvideo | | `GET /api/stream.y4m` | - **pcm** - pcm_alaw pcm_mulaw pcm_s16be pcm_s16le ## Snapshots | Format | Protocol | Send codecs | Example | |--------|----------|-------------|-----------------------| | jpeg | http | mjpeg | `GET /api/frame.jpeg` | | mp4 | http | h264,hevc | `GET /api/frame.mp4` | ## Developers **File naming:** - `pkg/{format}/producer.go` - producer for this format (also if support backchannel) - `pkg/{format}/consumer.go` - consumer for this format - `pkg/{format}/backchannel.go` - producer with only backchannel func **Mentioning modules:** - [`main.go`](../main.go) - [`README.md`](../README.md) - [`internal/README.md`](../internal/README.md) - [`website/.vitepress/config.js`](../website/.vitepress/config.js) - [`website/api/openapi.yaml`](../website/api/openapi.yaml) - [`www/schema.json`](../www/schema.json) ## Useful links - https://www.wowza.com/blog/streaming-protocols - https://vimeo.com/blog/post/rtmp-stream/ - https://sanjeev-pandey.medium.com/understanding-the-mpeg-4-moov-atom-pseudo-streaming-in-mp4-93935e1b9e9a - [Android Supported media formats](https://developer.android.com/guide/topics/media/media-formats) - [THEOplayer](https://www.theoplayer.com/test-your-stream-hls-dash-hesp) - [How Generate DTS/PTS](https://www.ramugedia.com/how-generate-dts-pts-from-elementary-stream) ================================================ FILE: pkg/aac/README.md ================================================ ## AAC-LD and AAC-ELD | Codec | Rate | QuickTime | ffmpeg | VLC | |---------|-------|-----------|--------|-----| | AAC-LD | 8000 | yes | no | no | | AAC-LD | 16000 | yes | no | no | | AAC-LD | 22050 | yes | yes | no | | AAC-LD | 24000 | yes | yes | no | | AAC-LD | 32000 | yes | yes | no | | AAC-ELD | 8000 | yes | no | no | | AAC-ELD | 16000 | yes | no | no | | AAC-ELD | 22050 | yes | yes | yes | | AAC-ELD | 24000 | yes | yes | yes | | AAC-ELD | 32000 | yes | yes | yes | ## Useful links - [4.6.20 Enhanced Low Delay Codec](https://csclub.uwaterloo.ca/~ehashman/ISO14496-3-2009.pdf) - https://stackoverflow.com/questions/40014508/aac-adts-for-aacobject-eld-packets - https://code.videolan.org/videolan/vlc/-/blob/master/modules/packetizer/mpeg4audio.c ================================================ FILE: pkg/aac/aac.go ================================================ package aac import ( "encoding/hex" "fmt" "github.com/AlexxIT/go2rtc/pkg/bits" "github.com/AlexxIT/go2rtc/pkg/core" ) const ( TypeAACMain = 1 TypeAACLC = 2 // Low Complexity TypeAACLD = 23 // Low Delay (48000, 44100, 32000, 24000, 22050) TypeESCAPE = 31 TypeAACELD = 39 // Enhanced Low Delay AUTime = 1024 // FMTP streamtype=5 - audio stream FMTP = "streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=" ) var sampleRates = [16]uint32{ 96000, 88200, 64000, 48000, 44100, 32000, 24000, 22050, 16000, 12000, 11025, 8000, 7350, 0, 0, 0, // protection from request sampleRates[15] } func ConfigToCodec(conf []byte) *core.Codec { // https://en.wikipedia.org/wiki/MPEG-4_Part_3#MPEG-4_Audio_Object_Types rd := bits.NewReader(conf) codec := &core.Codec{ FmtpLine: FMTP + hex.EncodeToString(conf), PayloadType: core.PayloadTypeRAW, } objType := rd.ReadBits(5) if objType == TypeESCAPE { objType = 32 + rd.ReadBits(6) } switch objType { case TypeAACLC, TypeAACLD, TypeAACELD: codec.Name = core.CodecAAC default: codec.Name = fmt.Sprintf("AAC-%X", objType) } if sampleRateIdx := rd.ReadBits8(4); sampleRateIdx < 0x0F { codec.ClockRate = sampleRates[sampleRateIdx] } else { codec.ClockRate = rd.ReadBits(24) } codec.Channels = rd.ReadBits8(4) return codec } func DecodeConfig(b []byte) (objType, sampleFreqIdx, channels byte, sampleRate uint32) { rd := bits.NewReader(b) objType = rd.ReadBits8(5) if objType == 0b11111 { objType = 32 + rd.ReadBits8(6) } sampleFreqIdx = rd.ReadBits8(4) if sampleFreqIdx == 0b1111 { sampleRate = rd.ReadBits(24) } else { sampleRate = sampleRates[sampleFreqIdx] } channels = rd.ReadBits8(4) return } func EncodeConfig(objType byte, sampleRate uint32, channels byte, shortFrame bool) []byte { wr := bits.NewWriter(nil) if objType < TypeESCAPE { wr.WriteBits8(objType, 5) } else { wr.WriteBits8(TypeESCAPE, 5) wr.WriteBits8(objType-32, 6) } i := indexUint32(sampleRates[:], sampleRate) if i >= 0 { wr.WriteBits8(byte(i), 4) } else { wr.WriteBits8(0xF, 4) wr.WriteBits(sampleRate, 24) } wr.WriteBits8(channels, 4) switch objType { case TypeAACLD: // https://github.com/FFmpeg/FFmpeg/blob/67d392b97941bb51fb7af3a3c9387f5ab895fa46/libavcodec/aacdec_template.c#L841 wr.WriteBool(shortFrame) wr.WriteBit(0) // dependsOnCoreCoder wr.WriteBit(0) // extension_flag wr.WriteBits8(0, 2) // ep_config case TypeAACELD: // https://github.com/FFmpeg/FFmpeg/blob/67d392b97941bb51fb7af3a3c9387f5ab895fa46/libavcodec/aacdec_template.c#L922 wr.WriteBool(shortFrame) wr.WriteBits8(0, 3) // res_flags wr.WriteBit(0) // ldSbrPresentFlag wr.WriteBits8(0, 4) // ELDEXT_TERM wr.WriteBits8(0, 2) // ep_config } return wr.Bytes() } func indexUint32(s []uint32, v uint32) int { for i := range s { if v == s[i] { return i } } return -1 } ================================================ FILE: pkg/aac/aac_test.go ================================================ package aac import ( "encoding/hex" "testing" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/stretchr/testify/require" ) func TestConfigToCodec(t *testing.T) { s := "profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=F8EC3000" s = core.Between(s, "config=", ";") src, err := hex.DecodeString(s) require.Nil(t, err) codec := ConfigToCodec(src) require.Equal(t, core.CodecAAC, codec.Name) require.Equal(t, uint32(24000), codec.ClockRate) require.Equal(t, uint16(1), codec.Channels) dst := EncodeConfig(TypeAACELD, 24000, 1, true) require.Equal(t, src, dst) } func TestADTS(t *testing.T) { // FFmpeg MPEG-TS AAC (one packet) s := "fff15080021ffc210049900219002380fff15080021ffc212049900219002380" //... src, err := hex.DecodeString(s) require.Nil(t, err) codec := ADTSToCodec(src) require.Equal(t, uint32(44100), codec.ClockRate) require.Equal(t, uint16(2), codec.Channels) size := ReadADTSSize(src) require.Equal(t, uint16(16), size) dst := CodecToADTS(codec) WriteADTSSize(dst, size) require.Equal(t, src[:len(dst)], dst) } func TestEncodeConfig(t *testing.T) { conf := EncodeConfig(TypeAACLC, 48000, 1, false) require.Equal(t, "1188", hex.EncodeToString(conf)) conf = EncodeConfig(TypeAACLC, 16000, 1, false) require.Equal(t, "1408", hex.EncodeToString(conf)) conf = EncodeConfig(TypeAACLC, 8000, 1, false) require.Equal(t, "1588", hex.EncodeToString(conf)) } ================================================ FILE: pkg/aac/adts.go ================================================ package aac import ( "encoding/hex" "github.com/AlexxIT/go2rtc/pkg/bits" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) const ADTSHeaderSize = 7 func ADTSHeaderLen(b []byte) int { if HasCRC(b) { return 9 // 7 bytes header + 2 bytes CRC } return ADTSHeaderSize } func IsADTS(b []byte) bool { // AAAAAAAA AAAABCCD EEFFFFGH HHIJKLMM MMMMMMMM MMMOOOOO OOOOOOPP (QQQQQQQQ QQQQQQQQ) // A 12 Syncword, all bits must be set to 1. // C 2 Layer, always set to 0. return len(b) >= ADTSHeaderSize && b[0] == 0xFF && b[1]&0b1111_0110 == 0xF0 } func HasCRC(b []byte) bool { // AAAAAAAA AAAABCCD EEFFFFGH HHIJKLMM MMMMMMMM MMMOOOOO OOOOOOPP (QQQQQQQQ QQQQQQQQ) // D 1 Protection absence, set to 1 if there is no CRC and 0 if there is CRC. return b[1]&0b1 == 0 } func ADTSToCodec(b []byte) *core.Codec { // 1. Check ADTS header if !IsADTS(b) { return nil } // 2. Decode ADTS params // https://wiki.multimedia.cx/index.php/ADTS rd := bits.NewReader(b) _ = rd.ReadBits(12) // Syncword, all bits must be set to 1 _ = rd.ReadBit() // MPEG Version, set to 0 for MPEG-4 and 1 for MPEG-2 _ = rd.ReadBits(2) // Layer, always set to 0 _ = rd.ReadBit() // Protection absence, set to 1 if there is no CRC and 0 if there is CRC objType := rd.ReadBits8(2) + 1 // Profile, the MPEG-4 Audio Object Type minus 1 sampleRateIdx := rd.ReadBits8(4) // MPEG-4 Sampling Frequency Index _ = rd.ReadBit() // Private bit, guaranteed never to be used by MPEG, set to 0 when encoding, ignore when decoding channels := rd.ReadBits8(3) // MPEG-4 Channel Configuration //_ = rd.ReadBit() // Originality, set to 1 to signal originality of the audio and 0 otherwise //_ = rd.ReadBit() // Home, set to 1 to signal home usage of the audio and 0 otherwise //_ = rd.ReadBit() // Copyright ID bit //_ = rd.ReadBit() // Copyright ID start //_ = rd.ReadBits(13) // Frame length //_ = rd.ReadBits(11) // Buffer fullness //_ = rd.ReadBits(2) // Number of AAC frames (Raw Data Blocks) in ADTS frame minus 1 //_ = rd.ReadBits(16) // CRC check // 3. Encode RTP config wr := bits.NewWriter(nil) wr.WriteBits8(objType, 5) wr.WriteBits8(sampleRateIdx, 4) wr.WriteBits8(channels, 4) conf := wr.Bytes() codec := &core.Codec{ Name: core.CodecAAC, ClockRate: sampleRates[sampleRateIdx], Channels: channels, FmtpLine: FMTP + hex.EncodeToString(conf), } return codec } func ReadADTSSize(b []byte) uint16 { // AAAAAAAA AAAABCCD EEFFFFGH HHIJKLMM MMMMMMMM MMMOOOOO OOOOOOPP (QQQQQQQQ QQQQQQQQ) _ = b[5] // bounds return uint16(b[3]&0b11)<<11 | uint16(b[4])<<3 | uint16(b[5]>>5) } func WriteADTSSize(b []byte, size uint16) { // AAAAAAAA AAAABCCD EEFFFFGH HHIJKLMM MMMMMMMM MMMOOOOO OOOOOOPP (QQQQQQQQ QQQQQQQQ) _ = b[5] // bounds b[3] |= byte(size >> (8 + 3)) b[4] = byte(size >> 3) b[5] |= byte(size << 5) return } func ADTSTimeSize(b []byte) uint32 { var units uint32 for len(b) > ADTSHeaderSize { auSize := ReadADTSSize(b) b = b[auSize:] units++ } return units * AUTime } func CodecToADTS(codec *core.Codec) []byte { s := core.Between(codec.FmtpLine, "config=", ";") conf, err := hex.DecodeString(s) if err != nil { return nil } objType, sampleFreqIdx, channels, _ := DecodeConfig(conf) profile := objType - 1 wr := bits.NewWriter(nil) wr.WriteAllBits(1, 12) // Syncword, all bits must be set to 1 wr.WriteBit(0) // MPEG Version, set to 0 for MPEG-4 and 1 for MPEG-2 wr.WriteBits8(0, 2) // Layer, always set to 0 wr.WriteBit(1) // Protection absence, set to 1 if there is no CRC and 0 if there is CRC wr.WriteBits8(profile, 2) // Profile, the MPEG-4 Audio Object Type minus 1 wr.WriteBits8(sampleFreqIdx, 4) // MPEG-4 Sampling Frequency Index wr.WriteBit(0) // Private bit, guaranteed never to be used by MPEG, set to 0 when encoding, ignore when decoding wr.WriteBits8(channels, 3) // MPEG-4 Channel Configuration wr.WriteBit(0) // Originality, set to 1 to signal originality of the audio and 0 otherwise wr.WriteBit(0) // Home, set to 1 to signal home usage of the audio and 0 otherwise wr.WriteBit(0) // Copyright ID bit wr.WriteBit(0) // Copyright ID start wr.WriteBits16(0, 13) // Frame length wr.WriteAllBits(1, 11) // Buffer fullness (variable bitrate) wr.WriteBits8(0, 2) // Number of AAC frames (Raw Data Blocks) in ADTS frame minus 1 return wr.Bytes() } func EncodeToADTS(codec *core.Codec, handler core.HandlerFunc) core.HandlerFunc { adts := CodecToADTS(codec) return func(packet *rtp.Packet) { if !IsADTS(packet.Payload) { b := make([]byte, ADTSHeaderSize+len(packet.Payload)) copy(b, adts) copy(b[ADTSHeaderSize:], packet.Payload) WriteADTSSize(b, uint16(len(b))) clone := *packet clone.Payload = b handler(&clone) } else { handler(packet) } } } ================================================ FILE: pkg/aac/consumer.go ================================================ package aac import ( "io" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type Consumer struct { core.Connection wr *core.WriteBuffer } func NewConsumer() *Consumer { medias := []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecAAC}, }, }, } wr := core.NewWriteBuffer(nil) return &Consumer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "adts", Medias: medias, Transport: wr, }, wr: wr, } } func (c *Consumer) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { sender := core.NewSender(media, track.Codec) sender.Handler = func(pkt *rtp.Packet) { if n, err := c.wr.Write(pkt.Payload); err == nil { c.Send += n } } if track.Codec.IsRTP() { sender.Handler = RTPToADTS(track.Codec, sender.Handler) } else { sender.Handler = EncodeToADTS(track.Codec, sender.Handler) } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } func (c *Consumer) WriteTo(wr io.Writer) (int64, error) { return c.wr.WriteTo(wr) } ================================================ FILE: pkg/aac/producer.go ================================================ package aac import ( "bufio" "errors" "io" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type Producer struct { core.Connection rd *bufio.Reader } func Open(r io.Reader) (*Producer, error) { rd := bufio.NewReader(r) b, err := rd.Peek(ADTSHeaderSize) if err != nil { return nil, err } codec := ADTSToCodec(b) if codec == nil { return nil, errors.New("adts: wrong header") } codec.PayloadType = core.PayloadTypeRAW medias := []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, }, } return &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "adts", Medias: medias, Transport: r, }, rd: rd, }, nil } func (c *Producer) Start() error { for { // read ADTS header adts := make([]byte, ADTSHeaderSize) if _, err := io.ReadFull(c.rd, adts); err != nil { return err } auSize := ReadADTSSize(adts) - ADTSHeaderSize if HasCRC(adts) { // skip CRC after header if _, err := c.rd.Discard(2); err != nil { return err } auSize -= 2 } // read AAC payload after header payload := make([]byte, auSize) if _, err := io.ReadFull(c.rd, payload); err != nil { return err } c.Recv += int(auSize) if len(c.Receivers) == 0 { continue } pkt := &rtp.Packet{ Header: rtp.Header{Timestamp: core.Now90000()}, Payload: payload, } c.Receivers[0].WriteRTP(pkt) } } ================================================ FILE: pkg/aac/rtp.go ================================================ package aac import ( "encoding/binary" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) const RTPPacketVersionAAC = 0 func RTPDepay(handler core.HandlerFunc) core.HandlerFunc { var timestamp uint32 return func(packet *rtp.Packet) { // support ONLY 2 bytes header size! // streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1408 // https://datatracker.ietf.org/doc/html/rfc3640 headersSize := binary.BigEndian.Uint16(packet.Payload) >> 3 //log.Printf("[RTP/AAC] units: %d, size: %4d, ts: %10d, %t", headersSize/2, len(packet.Payload), packet.Timestamp, packet.Marker) if len(packet.Payload) < int(2+headersSize) { // In very rare cases noname cameras may send data not according to the standard // https://github.com/AlexxIT/go2rtc/issues/1328 if IsADTS(packet.Payload) { clone := *packet clone.Version = RTPPacketVersionAAC clone.Timestamp = timestamp clone.Payload = clone.Payload[ADTSHeaderSize:] handler(&clone) } return } headers := packet.Payload[2 : 2+headersSize] units := packet.Payload[2+headersSize:] for len(headers) >= 2 { unitSize := binary.BigEndian.Uint16(headers) >> 3 if len(units) < int(unitSize) { return } unit := units[:unitSize] headers = headers[2:] units = units[unitSize:] timestamp += AUTime clone := *packet clone.Version = RTPPacketVersionAAC clone.Timestamp = timestamp if IsADTS(unit) { clone.Payload = unit[ADTSHeaderSize:] } else { clone.Payload = unit } handler(&clone) } } } func RTPPay(handler core.HandlerFunc) core.HandlerFunc { var seq uint16 var ts uint32 return func(packet *rtp.Packet) { if packet.Version != RTPPacketVersionAAC { handler(packet) return } // support ONLY one unit in payload auSize := uint16(len(packet.Payload)) // 2 bytes header size + 2 bytes first payload size payload := make([]byte, 2+2+auSize) payload[1] = 16 // header size in bits binary.BigEndian.PutUint16(payload[2:], auSize<<3) copy(payload[4:], packet.Payload) clone := rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: true, SequenceNumber: seq, Timestamp: ts, }, Payload: payload, } handler(&clone) seq++ ts += AUTime } } func ADTStoRTP(src []byte) (dst []byte) { dst = make([]byte, 2) // header bytes for i, n := 0, len(src)-ADTSHeaderSize; i < n; { auSize := ReadADTSSize(src[i:]) dst = append(dst, byte(auSize>>5), byte(auSize<<3)) // size in bits i += int(auSize) } hdrSize := uint16(len(dst) - 2) binary.BigEndian.PutUint16(dst, hdrSize<<3) // size in bits return append(dst, src...) } func RTPTimeSize(b []byte) uint32 { // convert RTP header size to units count units := binary.BigEndian.Uint16(b) >> 4 return uint32(units) * AUTime } func RTPToADTS(codec *core.Codec, handler core.HandlerFunc) core.HandlerFunc { adts := CodecToADTS(codec) return func(packet *rtp.Packet) { src := packet.Payload dst := make([]byte, 0, len(src)) headersSize := binary.BigEndian.Uint16(src) >> 3 headers := src[2 : 2+headersSize] units := src[2+headersSize:] for len(headers) > 0 { unitSize := binary.BigEndian.Uint16(headers) >> 3 headers = headers[2:] unit := units[:unitSize] units = units[unitSize:] if !IsADTS(unit) { i := len(dst) dst = append(dst, adts...) WriteADTSSize(dst[i:], ADTSHeaderSize+uint16(len(unit))) } dst = append(dst, unit...) } clone := *packet clone.Version = RTPPacketVersionAAC clone.Payload = dst handler(&clone) } } func RTPToCodec(b []byte) *core.Codec { hdrSize := binary.BigEndian.Uint16(b) / 8 return ADTSToCodec(b[2+hdrSize:]) } ================================================ FILE: pkg/aac/rtp_test.go ================================================ package aac import ( "encoding/hex" "testing" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" "github.com/stretchr/testify/require" ) func TestBuggy_RTSP_AAC(t *testing.T) { // https: //github.com/AlexxIT/go2rtc/issues/1328 payload, _ := hex.DecodeString("fff16080431ffc211ad4458aa309a1c0a8761a230502b7c74b2b5499252a010555e32e460128303c8ace4fd3260d654a424f7e7c65eddc96735fc6f1ac0edf94fdefa0e0bd6370da1c07b9c0e77a9d6e86b196a1ac7439dcafadcffcf6d89f60ac67f8884868e931383ad3e40cf5495470d1f606ef6f7624d285b951ebfa0e42641ab98f1371182b237d14f1bd16ad714fa2f1c6a7d23ebde7a0e34a2eca156a608a4caec49d9dca4b6fe2a09e9cdbf762c5b4148a3914abb7959c991228b0837b5988334b9fc18b8fac689b5ca1e4661573bbb8b253a86cae7ec14ace49969a9a76fd571ab6e650764cb59114d61dcedf07ac61b39e4ac66adebfd0d0ab45d518dd3c161049823f150864d977cf0855172ac8482e4b25fe911325d19617558c5405af74aff5492e4599bee53f2dbdf0503730af37078550f84c956b7ee89aae83c154fa2fa6e6792c5ddd5cd5cf6bb96bf055fee7f93bed59ffb039daee5ea7e5593cb194e9091e417c67d8f73026a6a6ae056e808f7c65c03d1b9197d3709ceb63bc7b979f7ba71df5e7c6395d99d6ea229000a6bc16fb4346d6b27d32f5d8d1200736d9366d59c0c9547210813b602473da9c46f9015bbb37594c1dd90cd6a36e96bd5d6a1445ab93c9e65505ec2c722bb4cc27a10600139a48c83594dde145253c386f6627d8c6e5102fe3828a590c709bc87f55b37e97d1ae72b017b09c6bb2c13299817bb45cc67318e10b6822075b97c6a03ec1c0") packet := &rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: true, SequenceNumber: 36944, Timestamp: 4217191328, SSRC: 12892774, }, Payload: payload, } var size int RTPDepay(func(packet *core.Packet) { size = len(packet.Payload) })(packet) require.Equal(t, len(payload), size+ADTSHeaderSize) } ================================================ FILE: pkg/alsa/README.md ================================================ ## Build ```shell x86_64-linux-gnu-gcc -w -static asound_arch.c -o asound_amd64 i686-linux-gnu-gcc -w -static asound_arch.c -o asound_i386 aarch64-linux-gnu-gcc -w -static asound_arch.c -o asound_arm64 arm-linux-gnueabihf-gcc -w -static asound_arch.c -o asound_arm mipsel-linux-gnu-gcc -w -static asound_arch.c -o asound_mipsle -D_TIME_BITS=32 ``` ## Useful links - https://github.com/torvalds/linux/blob/master/include/uapi/sound/asound.h - https://github.com/yobert/alsa - https://github.com/Narsil/alsa-go - https://github.com/alsa-project/alsa-lib - https://github.com/anisse/alsa - https://github.com/tinyalsa/tinyalsa **Broken pipe** - https://stackoverflow.com/questions/26545139/alsa-cannot-recovery-from-underrun-prepare-failed-broken-pipe - https://klipspringer.avadeaux.net/alsa-broken-pipe-errors/ ================================================ FILE: pkg/alsa/capture_linux.go ================================================ package alsa import ( "github.com/AlexxIT/go2rtc/pkg/alsa/device" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/pcm" "github.com/pion/rtp" ) type Capture struct { core.Connection dev *device.Device closed core.Waiter } func newCapture(dev *device.Device) (*Capture, error) { medias := []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ {Name: core.CodecPCML, ClockRate: 16000}, }, }, } return &Capture{ Connection: core.Connection{ ID: core.NewID(), FormatName: "alsa", Medias: medias, Transport: dev, }, dev: dev, }, nil } func (c *Capture) Start() error { dst := c.Medias[0].Codecs[0] src := &core.Codec{ Name: dst.Name, ClockRate: c.dev.GetRateNear(dst.ClockRate), Channels: c.dev.GetChannelsNear(dst.Channels), } if err := c.dev.SetHWParams(device.SNDRV_PCM_FORMAT_S16_LE, src.ClockRate, src.Channels); err != nil { return err } transcode := transcodeFunc(dst, src) frameBytes := int(pcm.BytesPerFrame(src)) var ts uint32 // readBufferSize for 20ms interval readBufferSize := 20 * frameBytes * int(src.ClockRate) / 1000 b := make([]byte, readBufferSize) for { n, err := c.dev.Read(b) if err != nil { return err } c.Recv += n if len(c.Receivers) == 0 { continue } pkt := &rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: true, Timestamp: ts, }, Payload: transcode(b[:n]), } c.Receivers[0].WriteRTP(pkt) ts += uint32(n / frameBytes) } } func transcodeFunc(dst, src *core.Codec) func([]byte) []byte { if dst.ClockRate == src.ClockRate && dst.Channels == src.Channels { return func(b []byte) []byte { return b } } return pcm.Transcode(dst, src) } ================================================ FILE: pkg/alsa/device/asound_32bit.go ================================================ //go:build 386 || arm package device type unsigned_char = byte type signed_int = int32 type unsigned_int = uint32 type signed_long = int64 type unsigned_long = uint64 type __u32 = uint32 type void__user = uintptr const ( SNDRV_PCM_STREAM_PLAYBACK = 0 SNDRV_PCM_STREAM_CAPTURE = 1 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED = 0 SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED = 1 SNDRV_PCM_ACCESS_MMAP_COMPLEX = 2 SNDRV_PCM_ACCESS_RW_INTERLEAVED = 3 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED = 4 SNDRV_PCM_FORMAT_S8 = 0 SNDRV_PCM_FORMAT_U8 = 1 SNDRV_PCM_FORMAT_S16_LE = 2 SNDRV_PCM_FORMAT_S16_BE = 3 SNDRV_PCM_FORMAT_U16_LE = 4 SNDRV_PCM_FORMAT_U16_BE = 5 SNDRV_PCM_FORMAT_S24_LE = 6 SNDRV_PCM_FORMAT_S24_BE = 7 SNDRV_PCM_FORMAT_U24_LE = 8 SNDRV_PCM_FORMAT_U24_BE = 9 SNDRV_PCM_FORMAT_S32_LE = 10 SNDRV_PCM_FORMAT_S32_BE = 11 SNDRV_PCM_FORMAT_U32_LE = 12 SNDRV_PCM_FORMAT_U32_BE = 13 SNDRV_PCM_FORMAT_FLOAT_LE = 14 SNDRV_PCM_FORMAT_FLOAT_BE = 15 SNDRV_PCM_FORMAT_FLOAT64_LE = 16 SNDRV_PCM_FORMAT_FLOAT64_BE = 17 SNDRV_PCM_FORMAT_MU_LAW = 20 SNDRV_PCM_FORMAT_A_LAW = 21 SNDRV_PCM_FORMAT_MPEG = 23 SNDRV_PCM_IOCTL_PVERSION = 0x80044100 SNDRV_PCM_IOCTL_INFO = 0x81204101 SNDRV_PCM_IOCTL_HW_REFINE = 0xc25c4110 SNDRV_PCM_IOCTL_HW_PARAMS = 0xc25c4111 SNDRV_PCM_IOCTL_SW_PARAMS = 0xc0684113 SNDRV_PCM_IOCTL_PREPARE = 0x00004140 SNDRV_PCM_IOCTL_WRITEI_FRAMES = 0x400c4150 SNDRV_PCM_IOCTL_READI_FRAMES = 0x800c4151 ) type snd_pcm_info struct { // size 288 device unsigned_int // offset 0, size 4 subdevice unsigned_int // offset 4, size 4 stream signed_int // offset 8, size 4 card signed_int // offset 12, size 4 id [64]unsigned_char // offset 16, size 64 name [80]unsigned_char // offset 80, size 80 subname [32]unsigned_char // offset 160, size 32 dev_class signed_int // offset 192, size 4 dev_subclass signed_int // offset 196, size 4 subdevices_count unsigned_int // offset 200, size 4 subdevices_avail unsigned_int // offset 204, size 4 pad1 [16]unsigned_char reserved [64]unsigned_char // offset 224, size 64 } type snd_pcm_uframes_t = unsigned_long type snd_pcm_sframes_t = signed_long type snd_xferi struct { // size 12 result snd_pcm_sframes_t // offset 0, size 4 buf void__user // offset 4, size 4 frames snd_pcm_uframes_t // offset 8, size 4 } const ( SNDRV_PCM_HW_PARAM_ACCESS = 0 SNDRV_PCM_HW_PARAM_FORMAT = 1 SNDRV_PCM_HW_PARAM_SUBFORMAT = 2 SNDRV_PCM_HW_PARAM_FIRST_MASK = 0 SNDRV_PCM_HW_PARAM_LAST_MASK = 2 SNDRV_PCM_HW_PARAM_SAMPLE_BITS = 8 SNDRV_PCM_HW_PARAM_FRAME_BITS = 9 SNDRV_PCM_HW_PARAM_CHANNELS = 10 SNDRV_PCM_HW_PARAM_RATE = 11 SNDRV_PCM_HW_PARAM_PERIOD_TIME = 12 SNDRV_PCM_HW_PARAM_PERIOD_SIZE = 13 SNDRV_PCM_HW_PARAM_PERIOD_BYTES = 14 SNDRV_PCM_HW_PARAM_PERIODS = 15 SNDRV_PCM_HW_PARAM_BUFFER_TIME = 16 SNDRV_PCM_HW_PARAM_BUFFER_SIZE = 17 SNDRV_PCM_HW_PARAM_BUFFER_BYTES = 18 SNDRV_PCM_HW_PARAM_TICK_TIME = 19 SNDRV_PCM_HW_PARAM_FIRST_INTERVAL = 8 SNDRV_PCM_HW_PARAM_LAST_INTERVAL = 19 SNDRV_MASK_MAX = 256 SNDRV_PCM_TSTAMP_NONE = 0 SNDRV_PCM_TSTAMP_ENABLE = 1 ) type snd_mask struct { // size 32 bits [(SNDRV_MASK_MAX + 31) / 32]__u32 // offset 0, size 32 } type snd_interval struct { // size 12 min unsigned_int // offset 0, size 4 max unsigned_int // offset 4, size 4 bit unsigned_int } type snd_pcm_hw_params struct { // size 604 flags unsigned_int // offset 0, size 4 masks [SNDRV_PCM_HW_PARAM_LAST_MASK - SNDRV_PCM_HW_PARAM_FIRST_MASK + 1]snd_mask // offset 4, size 96 mres [5]snd_mask // offset 100, size 160 intervals [SNDRV_PCM_HW_PARAM_LAST_INTERVAL - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1]snd_interval // offset 260, size 144 ires [9]snd_interval // offset 404, size 108 rmask unsigned_int // offset 512, size 4 cmask unsigned_int // offset 516, size 4 info unsigned_int // offset 520, size 4 msbits unsigned_int // offset 524, size 4 rate_num unsigned_int // offset 528, size 4 rate_den unsigned_int // offset 532, size 4 fifo_size snd_pcm_uframes_t // offset 536, size 4 reserved [64]unsigned_char // offset 540, size 64 } type snd_pcm_sw_params struct { // size 104 tstamp_mode signed_int // offset 0, size 4 period_step unsigned_int // offset 4, size 4 sleep_min unsigned_int // offset 8, size 4 avail_min snd_pcm_uframes_t // offset 12, size 4 xfer_align snd_pcm_uframes_t // offset 16, size 4 start_threshold snd_pcm_uframes_t // offset 20, size 4 stop_threshold snd_pcm_uframes_t // offset 24, size 4 silence_threshold snd_pcm_uframes_t // offset 28, size 4 silence_size snd_pcm_uframes_t // offset 32, size 4 boundary snd_pcm_uframes_t // offset 36, size 4 proto unsigned_int // offset 40, size 4 tstamp_type unsigned_int // offset 44, size 4 reserved [56]unsigned_char // offset 48, size 56 } ================================================ FILE: pkg/alsa/device/asound_64bit.go ================================================ //go:build amd64 || arm64 package device type unsigned_char = byte type signed_int = int32 type unsigned_int = uint32 type signed_long = int64 type unsigned_long = uint64 type __u32 = uint32 type void__user = uintptr const ( SNDRV_PCM_STREAM_PLAYBACK = 0 SNDRV_PCM_STREAM_CAPTURE = 1 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED = 0 SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED = 1 SNDRV_PCM_ACCESS_MMAP_COMPLEX = 2 SNDRV_PCM_ACCESS_RW_INTERLEAVED = 3 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED = 4 SNDRV_PCM_FORMAT_S8 = 0 SNDRV_PCM_FORMAT_U8 = 1 SNDRV_PCM_FORMAT_S16_LE = 2 SNDRV_PCM_FORMAT_S16_BE = 3 SNDRV_PCM_FORMAT_U16_LE = 4 SNDRV_PCM_FORMAT_U16_BE = 5 SNDRV_PCM_FORMAT_S24_LE = 6 SNDRV_PCM_FORMAT_S24_BE = 7 SNDRV_PCM_FORMAT_U24_LE = 8 SNDRV_PCM_FORMAT_U24_BE = 9 SNDRV_PCM_FORMAT_S32_LE = 10 SNDRV_PCM_FORMAT_S32_BE = 11 SNDRV_PCM_FORMAT_U32_LE = 12 SNDRV_PCM_FORMAT_U32_BE = 13 SNDRV_PCM_FORMAT_FLOAT_LE = 14 SNDRV_PCM_FORMAT_FLOAT_BE = 15 SNDRV_PCM_FORMAT_FLOAT64_LE = 16 SNDRV_PCM_FORMAT_FLOAT64_BE = 17 SNDRV_PCM_FORMAT_MU_LAW = 20 SNDRV_PCM_FORMAT_A_LAW = 21 SNDRV_PCM_FORMAT_MPEG = 23 SNDRV_PCM_IOCTL_PVERSION = 0x80044100 SNDRV_PCM_IOCTL_INFO = 0x81204101 SNDRV_PCM_IOCTL_HW_REFINE = 0xc2604110 SNDRV_PCM_IOCTL_HW_PARAMS = 0xc2604111 SNDRV_PCM_IOCTL_SW_PARAMS = 0xc0884113 SNDRV_PCM_IOCTL_PREPARE = 0x00004140 SNDRV_PCM_IOCTL_WRITEI_FRAMES = 0x40184150 SNDRV_PCM_IOCTL_READI_FRAMES = 0x80184151 ) type snd_pcm_info struct { // size 288 device unsigned_int // offset 0, size 4 subdevice unsigned_int // offset 4, size 4 stream signed_int // offset 8, size 4 card signed_int // offset 12, size 4 id [64]unsigned_char // offset 16, size 64 name [80]unsigned_char // offset 80, size 80 subname [32]unsigned_char // offset 160, size 32 dev_class signed_int // offset 192, size 4 dev_subclass signed_int // offset 196, size 4 subdevices_count unsigned_int // offset 200, size 4 subdevices_avail unsigned_int // offset 204, size 4 pad1 [16]unsigned_char reserved [64]unsigned_char // offset 224, size 64 } type snd_pcm_uframes_t = unsigned_long type snd_pcm_sframes_t = signed_long type snd_xferi struct { // size 24 result snd_pcm_sframes_t // offset 0, size 8 buf void__user // offset 8, size 8 frames snd_pcm_uframes_t // offset 16, size 8 } const ( SNDRV_PCM_HW_PARAM_ACCESS = 0 SNDRV_PCM_HW_PARAM_FORMAT = 1 SNDRV_PCM_HW_PARAM_SUBFORMAT = 2 SNDRV_PCM_HW_PARAM_FIRST_MASK = 0 SNDRV_PCM_HW_PARAM_LAST_MASK = 2 SNDRV_PCM_HW_PARAM_SAMPLE_BITS = 8 SNDRV_PCM_HW_PARAM_FRAME_BITS = 9 SNDRV_PCM_HW_PARAM_CHANNELS = 10 SNDRV_PCM_HW_PARAM_RATE = 11 SNDRV_PCM_HW_PARAM_PERIOD_TIME = 12 SNDRV_PCM_HW_PARAM_PERIOD_SIZE = 13 SNDRV_PCM_HW_PARAM_PERIOD_BYTES = 14 SNDRV_PCM_HW_PARAM_PERIODS = 15 SNDRV_PCM_HW_PARAM_BUFFER_TIME = 16 SNDRV_PCM_HW_PARAM_BUFFER_SIZE = 17 SNDRV_PCM_HW_PARAM_BUFFER_BYTES = 18 SNDRV_PCM_HW_PARAM_TICK_TIME = 19 SNDRV_PCM_HW_PARAM_FIRST_INTERVAL = 8 SNDRV_PCM_HW_PARAM_LAST_INTERVAL = 19 SNDRV_MASK_MAX = 256 SNDRV_PCM_TSTAMP_NONE = 0 SNDRV_PCM_TSTAMP_ENABLE = 1 ) type snd_mask struct { // size 32 bits [(SNDRV_MASK_MAX + 31) / 32]__u32 // offset 0, size 32 } type snd_interval struct { // size 12 min unsigned_int // offset 0, size 4 max unsigned_int // offset 4, size 4 bit unsigned_int } type snd_pcm_hw_params struct { // size 608 flags unsigned_int // offset 0, size 4 masks [SNDRV_PCM_HW_PARAM_LAST_MASK - SNDRV_PCM_HW_PARAM_FIRST_MASK + 1]snd_mask // offset 4, size 96 mres [5]snd_mask // offset 100, size 160 intervals [SNDRV_PCM_HW_PARAM_LAST_INTERVAL - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1]snd_interval // offset 260, size 144 ires [9]snd_interval // offset 404, size 108 rmask unsigned_int // offset 512, size 4 cmask unsigned_int // offset 516, size 4 info unsigned_int // offset 520, size 4 msbits unsigned_int // offset 524, size 4 rate_num unsigned_int // offset 528, size 4 rate_den unsigned_int // offset 532, size 4 fifo_size snd_pcm_uframes_t // offset 536, size 8 reserved [64]unsigned_char // offset 544, size 64 } type snd_pcm_sw_params struct { // size 136 tstamp_mode signed_int // offset 0, size 4 period_step unsigned_int // offset 4, size 4 sleep_min unsigned_int // offset 8, size 4 avail_min snd_pcm_uframes_t // offset 16, size 8 xfer_align snd_pcm_uframes_t // offset 24, size 8 start_threshold snd_pcm_uframes_t // offset 32, size 8 stop_threshold snd_pcm_uframes_t // offset 40, size 8 silence_threshold snd_pcm_uframes_t // offset 48, size 8 silence_size snd_pcm_uframes_t // offset 56, size 8 boundary snd_pcm_uframes_t // offset 64, size 8 proto unsigned_int // offset 72, size 4 tstamp_type unsigned_int // offset 76, size 4 reserved [56]unsigned_char // offset 80, size 56 } ================================================ FILE: pkg/alsa/device/asound_arch.c ================================================ //go:build ignore #include #include #include #include #define print_line(text) printf("%s\n", text) #define print_hex_const(name) printf("\t%s = 0x%08lx\n", #name, name) #define print_int_const(con) printf("\t%s = %d\n", #con, con) #define print_struct_header(str) printf("type %s struct { // size %lu\n", #str, sizeof(struct str)) #define print_struct_member(str, mem, typ) printf("\t%s %s // offset %lu, size %lu\n", #mem == "type" ? "typ" : #mem, typ, offsetof(struct str, mem), sizeof((struct str){0}.mem)) // https://github.com/torvalds/linux/blob/master/include/uapi/sound/asound.h int main() { print_line("package device\n"); print_line("type unsigned_char = byte"); print_line("type signed_int = int32"); print_line("type unsigned_int = uint32"); print_line("type signed_long = int64"); print_line("type unsigned_long = uint64"); print_line("type __u32 = uint32"); print_line("type void__user = uintptr\n"); print_line("const ("); print_int_const(SNDRV_PCM_STREAM_PLAYBACK); print_int_const(SNDRV_PCM_STREAM_CAPTURE); print_line(""); print_int_const(SNDRV_PCM_ACCESS_MMAP_INTERLEAVED); print_int_const(SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED); print_int_const(SNDRV_PCM_ACCESS_MMAP_COMPLEX); print_int_const(SNDRV_PCM_ACCESS_RW_INTERLEAVED); print_int_const(SNDRV_PCM_ACCESS_RW_NONINTERLEAVED); print_line(""); print_int_const(SNDRV_PCM_FORMAT_S8); print_int_const(SNDRV_PCM_FORMAT_U8); print_int_const(SNDRV_PCM_FORMAT_S16_LE); print_int_const(SNDRV_PCM_FORMAT_S16_BE); print_int_const(SNDRV_PCM_FORMAT_U16_LE); print_int_const(SNDRV_PCM_FORMAT_U16_BE); print_int_const(SNDRV_PCM_FORMAT_S24_LE); print_int_const(SNDRV_PCM_FORMAT_S24_BE); print_int_const(SNDRV_PCM_FORMAT_U24_LE); print_int_const(SNDRV_PCM_FORMAT_U24_BE); print_int_const(SNDRV_PCM_FORMAT_S32_LE); print_int_const(SNDRV_PCM_FORMAT_S32_BE); print_int_const(SNDRV_PCM_FORMAT_U32_LE); print_int_const(SNDRV_PCM_FORMAT_U32_BE); print_int_const(SNDRV_PCM_FORMAT_FLOAT_LE); print_int_const(SNDRV_PCM_FORMAT_FLOAT_BE); print_int_const(SNDRV_PCM_FORMAT_FLOAT64_LE); print_int_const(SNDRV_PCM_FORMAT_FLOAT64_BE); print_int_const(SNDRV_PCM_FORMAT_MU_LAW); print_int_const(SNDRV_PCM_FORMAT_A_LAW); print_int_const(SNDRV_PCM_FORMAT_MPEG); print_line(""); print_hex_const(SNDRV_PCM_IOCTL_PVERSION); // A 0x00 print_hex_const(SNDRV_PCM_IOCTL_INFO); // A 0x01 print_hex_const(SNDRV_PCM_IOCTL_HW_REFINE); // A 0x10 print_hex_const(SNDRV_PCM_IOCTL_HW_PARAMS); // A 0x11 print_hex_const(SNDRV_PCM_IOCTL_SW_PARAMS); // A 0x13 print_hex_const(SNDRV_PCM_IOCTL_PREPARE); // A 0x40 print_hex_const(SNDRV_PCM_IOCTL_WRITEI_FRAMES); // A 0x50 print_hex_const(SNDRV_PCM_IOCTL_READI_FRAMES); // A 0x51 print_line(")\n"); print_struct_header(snd_pcm_info); print_struct_member(snd_pcm_info, device, "unsigned_int"); print_struct_member(snd_pcm_info, subdevice, "unsigned_int"); print_struct_member(snd_pcm_info, stream, "signed_int"); print_struct_member(snd_pcm_info, card, "signed_int"); print_struct_member(snd_pcm_info, id, "[64]unsigned_char"); print_struct_member(snd_pcm_info, name, "[80]unsigned_char"); print_struct_member(snd_pcm_info, subname, "[32]unsigned_char"); print_struct_member(snd_pcm_info, dev_class, "signed_int"); print_struct_member(snd_pcm_info, dev_subclass, "signed_int"); print_struct_member(snd_pcm_info, subdevices_count, "unsigned_int"); print_struct_member(snd_pcm_info, subdevices_avail, "unsigned_int"); print_line("\tpad1 [16]unsigned_char"); print_struct_member(snd_pcm_info, reserved, "[64]unsigned_char"); print_line("}\n"); print_line("type snd_pcm_uframes_t = unsigned_long"); print_line("type snd_pcm_sframes_t = signed_long\n"); print_struct_header(snd_xferi); print_struct_member(snd_xferi, result, "snd_pcm_sframes_t"); print_struct_member(snd_xferi, buf, "void__user"); print_struct_member(snd_xferi, frames, "snd_pcm_uframes_t"); print_line("}\n"); print_line("const ("); print_int_const(SNDRV_PCM_HW_PARAM_ACCESS); print_int_const(SNDRV_PCM_HW_PARAM_FORMAT); print_int_const(SNDRV_PCM_HW_PARAM_SUBFORMAT); print_int_const(SNDRV_PCM_HW_PARAM_FIRST_MASK); print_int_const(SNDRV_PCM_HW_PARAM_LAST_MASK); print_line(""); print_int_const(SNDRV_PCM_HW_PARAM_SAMPLE_BITS); print_int_const(SNDRV_PCM_HW_PARAM_FRAME_BITS); print_int_const(SNDRV_PCM_HW_PARAM_CHANNELS); print_int_const(SNDRV_PCM_HW_PARAM_RATE); print_int_const(SNDRV_PCM_HW_PARAM_PERIOD_TIME); print_int_const(SNDRV_PCM_HW_PARAM_PERIOD_SIZE); print_int_const(SNDRV_PCM_HW_PARAM_PERIOD_BYTES); print_int_const(SNDRV_PCM_HW_PARAM_PERIODS); print_int_const(SNDRV_PCM_HW_PARAM_BUFFER_TIME); print_int_const(SNDRV_PCM_HW_PARAM_BUFFER_SIZE); print_int_const(SNDRV_PCM_HW_PARAM_BUFFER_BYTES); print_int_const(SNDRV_PCM_HW_PARAM_TICK_TIME); print_int_const(SNDRV_PCM_HW_PARAM_FIRST_INTERVAL); print_int_const(SNDRV_PCM_HW_PARAM_LAST_INTERVAL); print_line(""); print_int_const(SNDRV_MASK_MAX); print_line(""); print_int_const(SNDRV_PCM_TSTAMP_NONE); print_int_const(SNDRV_PCM_TSTAMP_ENABLE); print_line(")\n"); print_struct_header(snd_mask); print_struct_member(snd_mask, bits, "[(SNDRV_MASK_MAX+31)/32]__u32"); print_line("}\n"); print_struct_header(snd_interval); print_struct_member(snd_interval, min, "unsigned_int"); print_struct_member(snd_interval, max, "unsigned_int"); print_line("\tbit unsigned_int"); print_line("}\n"); print_struct_header(snd_pcm_hw_params); print_struct_member(snd_pcm_hw_params, flags, "unsigned_int"); print_struct_member(snd_pcm_hw_params, masks, "[SNDRV_PCM_HW_PARAM_LAST_MASK-SNDRV_PCM_HW_PARAM_FIRST_MASK+1]snd_mask"); print_struct_member(snd_pcm_hw_params, mres, "[5]snd_mask"); print_struct_member(snd_pcm_hw_params, intervals, "[SNDRV_PCM_HW_PARAM_LAST_INTERVAL-SNDRV_PCM_HW_PARAM_FIRST_INTERVAL+1]snd_interval"); print_struct_member(snd_pcm_hw_params, ires, "[9]snd_interval"); print_struct_member(snd_pcm_hw_params, rmask, "unsigned_int"); print_struct_member(snd_pcm_hw_params, cmask, "unsigned_int"); print_struct_member(snd_pcm_hw_params, info, "unsigned_int"); print_struct_member(snd_pcm_hw_params, msbits, "unsigned_int"); print_struct_member(snd_pcm_hw_params, rate_num, "unsigned_int"); print_struct_member(snd_pcm_hw_params, rate_den, "unsigned_int"); print_struct_member(snd_pcm_hw_params, fifo_size, "snd_pcm_uframes_t"); print_struct_member(snd_pcm_hw_params, reserved, "[64]unsigned_char"); print_line("}\n"); print_struct_header(snd_pcm_sw_params); print_struct_member(snd_pcm_sw_params, tstamp_mode, "signed_int"); print_struct_member(snd_pcm_sw_params, period_step, "unsigned_int"); print_struct_member(snd_pcm_sw_params, sleep_min, "unsigned_int"); print_struct_member(snd_pcm_sw_params, avail_min, "snd_pcm_uframes_t"); print_struct_member(snd_pcm_sw_params, xfer_align, "snd_pcm_uframes_t"); print_struct_member(snd_pcm_sw_params, start_threshold, "snd_pcm_uframes_t"); print_struct_member(snd_pcm_sw_params, stop_threshold, "snd_pcm_uframes_t"); print_struct_member(snd_pcm_sw_params, silence_threshold, "snd_pcm_uframes_t"); print_struct_member(snd_pcm_sw_params, silence_size, "snd_pcm_uframes_t"); print_struct_member(snd_pcm_sw_params, boundary, "snd_pcm_uframes_t"); print_struct_member(snd_pcm_sw_params, proto, "unsigned_int"); print_struct_member(snd_pcm_sw_params, tstamp_type, "unsigned_int"); print_struct_member(snd_pcm_sw_params, reserved, "[56]unsigned_char"); print_line("}\n"); return 0; } ================================================ FILE: pkg/alsa/device/asound_mipsle.go ================================================ package device type unsigned_char = byte type signed_int = int32 type unsigned_int = uint32 type signed_long = int64 type unsigned_long = uint64 type __u32 = uint32 type void__user = uintptr const ( SNDRV_PCM_STREAM_PLAYBACK = 0 SNDRV_PCM_STREAM_CAPTURE = 1 SNDRV_PCM_ACCESS_MMAP_INTERLEAVED = 0 SNDRV_PCM_ACCESS_MMAP_NONINTERLEAVED = 1 SNDRV_PCM_ACCESS_MMAP_COMPLEX = 2 SNDRV_PCM_ACCESS_RW_INTERLEAVED = 3 SNDRV_PCM_ACCESS_RW_NONINTERLEAVED = 4 SNDRV_PCM_FORMAT_S8 = 0 SNDRV_PCM_FORMAT_U8 = 1 SNDRV_PCM_FORMAT_S16_LE = 2 SNDRV_PCM_FORMAT_S16_BE = 3 SNDRV_PCM_FORMAT_U16_LE = 4 SNDRV_PCM_FORMAT_U16_BE = 5 SNDRV_PCM_FORMAT_S24_LE = 6 SNDRV_PCM_FORMAT_S24_BE = 7 SNDRV_PCM_FORMAT_U24_LE = 8 SNDRV_PCM_FORMAT_U24_BE = 9 SNDRV_PCM_FORMAT_S32_LE = 10 SNDRV_PCM_FORMAT_S32_BE = 11 SNDRV_PCM_FORMAT_U32_LE = 12 SNDRV_PCM_FORMAT_U32_BE = 13 SNDRV_PCM_FORMAT_FLOAT_LE = 14 SNDRV_PCM_FORMAT_FLOAT_BE = 15 SNDRV_PCM_FORMAT_FLOAT64_LE = 16 SNDRV_PCM_FORMAT_FLOAT64_BE = 17 SNDRV_PCM_FORMAT_MU_LAW = 20 SNDRV_PCM_FORMAT_A_LAW = 21 SNDRV_PCM_FORMAT_MPEG = 23 SNDRV_PCM_IOCTL_PVERSION = 0x40044100 SNDRV_PCM_IOCTL_INFO = 0x41204101 SNDRV_PCM_IOCTL_HW_REFINE = 0xc25c4110 SNDRV_PCM_IOCTL_HW_PARAMS = 0xc25c4111 SNDRV_PCM_IOCTL_SW_PARAMS = 0xc0684113 SNDRV_PCM_IOCTL_PREPARE = 0x20004140 SNDRV_PCM_IOCTL_WRITEI_FRAMES = 0x800c4150 SNDRV_PCM_IOCTL_READI_FRAMES = 0x400c4151 ) type snd_pcm_info struct { // size 288 device unsigned_int // offset 0, size 4 subdevice unsigned_int // offset 4, size 4 stream signed_int // offset 8, size 4 card signed_int // offset 12, size 4 id [64]unsigned_char // offset 16, size 64 name [80]unsigned_char // offset 80, size 80 subname [32]unsigned_char // offset 160, size 32 dev_class signed_int // offset 192, size 4 dev_subclass signed_int // offset 196, size 4 subdevices_count unsigned_int // offset 200, size 4 subdevices_avail unsigned_int // offset 204, size 4 pad1 [16]unsigned_char reserved [64]unsigned_char // offset 224, size 64 } type snd_pcm_uframes_t = unsigned_long type snd_pcm_sframes_t = signed_long type snd_xferi struct { // size 12 result snd_pcm_sframes_t // offset 0, size 4 buf void__user // offset 4, size 4 frames snd_pcm_uframes_t // offset 8, size 4 } const ( SNDRV_PCM_HW_PARAM_ACCESS = 0 SNDRV_PCM_HW_PARAM_FORMAT = 1 SNDRV_PCM_HW_PARAM_SUBFORMAT = 2 SNDRV_PCM_HW_PARAM_FIRST_MASK = 0 SNDRV_PCM_HW_PARAM_LAST_MASK = 2 SNDRV_PCM_HW_PARAM_SAMPLE_BITS = 8 SNDRV_PCM_HW_PARAM_FRAME_BITS = 9 SNDRV_PCM_HW_PARAM_CHANNELS = 10 SNDRV_PCM_HW_PARAM_RATE = 11 SNDRV_PCM_HW_PARAM_PERIOD_TIME = 12 SNDRV_PCM_HW_PARAM_PERIOD_SIZE = 13 SNDRV_PCM_HW_PARAM_PERIOD_BYTES = 14 SNDRV_PCM_HW_PARAM_PERIODS = 15 SNDRV_PCM_HW_PARAM_BUFFER_TIME = 16 SNDRV_PCM_HW_PARAM_BUFFER_SIZE = 17 SNDRV_PCM_HW_PARAM_BUFFER_BYTES = 18 SNDRV_PCM_HW_PARAM_TICK_TIME = 19 SNDRV_PCM_HW_PARAM_FIRST_INTERVAL = 8 SNDRV_PCM_HW_PARAM_LAST_INTERVAL = 19 SNDRV_MASK_MAX = 256 SNDRV_PCM_TSTAMP_NONE = 0 SNDRV_PCM_TSTAMP_ENABLE = 1 ) type snd_mask struct { // size 32 bits [(SNDRV_MASK_MAX + 31) / 32]__u32 // offset 0, size 32 } type snd_interval struct { // size 12 min unsigned_int // offset 0, size 4 max unsigned_int // offset 4, size 4 bit unsigned_int } type snd_pcm_hw_params struct { // size 604 flags unsigned_int // offset 0, size 4 masks [SNDRV_PCM_HW_PARAM_LAST_MASK - SNDRV_PCM_HW_PARAM_FIRST_MASK + 1]snd_mask // offset 4, size 96 mres [5]snd_mask // offset 100, size 160 intervals [SNDRV_PCM_HW_PARAM_LAST_INTERVAL - SNDRV_PCM_HW_PARAM_FIRST_INTERVAL + 1]snd_interval // offset 260, size 144 ires [9]snd_interval // offset 404, size 108 rmask unsigned_int // offset 512, size 4 cmask unsigned_int // offset 516, size 4 info unsigned_int // offset 520, size 4 msbits unsigned_int // offset 524, size 4 rate_num unsigned_int // offset 528, size 4 rate_den unsigned_int // offset 532, size 4 fifo_size snd_pcm_uframes_t // offset 536, size 4 reserved [64]unsigned_char // offset 540, size 64 } type snd_pcm_sw_params struct { // size 104 tstamp_mode signed_int // offset 0, size 4 period_step unsigned_int // offset 4, size 4 sleep_min unsigned_int // offset 8, size 4 avail_min snd_pcm_uframes_t // offset 12, size 4 xfer_align snd_pcm_uframes_t // offset 16, size 4 start_threshold snd_pcm_uframes_t // offset 20, size 4 stop_threshold snd_pcm_uframes_t // offset 24, size 4 silence_threshold snd_pcm_uframes_t // offset 28, size 4 silence_size snd_pcm_uframes_t // offset 32, size 4 boundary snd_pcm_uframes_t // offset 36, size 4 proto unsigned_int // offset 40, size 4 tstamp_type unsigned_int // offset 44, size 4 reserved [56]unsigned_char // offset 48, size 56 } ================================================ FILE: pkg/alsa/device/device_linux.go ================================================ package device import ( "fmt" "syscall" "unsafe" ) type Device struct { fd uintptr path string hwparams snd_pcm_hw_params frameBytes int // sample size * channels } func Open(path string) (*Device, error) { // important to use nonblock because can get lock fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_NONBLOCK, 0) if err != nil { return nil, err } // important to remove nonblock because better to handle reads and writes if err = syscall.SetNonblock(fd, false); err != nil { return nil, err } d := &Device{fd: uintptr(fd), path: path} d.init() // load all supported formats, channels, rates, etc. if err = ioctl(d.fd, SNDRV_PCM_IOCTL_HW_REFINE, &d.hwparams); err != nil { _ = d.Close() return nil, err } d.setMask(SNDRV_PCM_HW_PARAM_ACCESS, SNDRV_PCM_ACCESS_RW_INTERLEAVED) return d, nil } func (d *Device) Close() error { return syscall.Close(int(d.fd)) } func (d *Device) IsCapture() bool { // path: /dev/snd/pcmC0D0c, where p - playback, c - capture return d.path[len(d.path)-1] == 'c' } type Info struct { Card int Device int SubDevice int Stream int ID string Name string SubName string } func (d *Device) Info() (*Info, error) { var info snd_pcm_info if err := ioctl(d.fd, SNDRV_PCM_IOCTL_INFO, &info); err != nil { return nil, err } return &Info{ Card: int(info.card), Device: int(info.device), SubDevice: int(info.subdevice), Stream: int(info.stream), ID: str(info.id[:]), Name: str(info.name[:]), SubName: str(info.subname[:]), }, nil } func (d *Device) CheckFormat(format byte) bool { return d.checkMask(SNDRV_PCM_HW_PARAM_FORMAT, uint32(format)) } func (d *Device) ListFormats() (formats []byte) { for i := byte(0); i <= 28; i++ { if d.CheckFormat(i) { formats = append(formats, i) } } return } func (d *Device) RangeRates() (uint32, uint32) { return d.getInterval(SNDRV_PCM_HW_PARAM_RATE) } func (d *Device) RangeChannels() (byte, byte) { minCh, maxCh := d.getInterval(SNDRV_PCM_HW_PARAM_CHANNELS) return byte(minCh), byte(maxCh) } func (d *Device) GetRateNear(rate uint32) uint32 { r1, r2 := d.RangeRates() if rate < r1 { return r1 } if rate > r2 { return r2 } return rate } func (d *Device) GetChannelsNear(channels byte) byte { c1, c2 := d.RangeChannels() if channels < c1 { return c1 } if channels > c2 { return c2 } return channels } const bufferSize = 4096 func (d *Device) SetHWParams(format byte, rate uint32, channels byte) error { d.setInterval(SNDRV_PCM_HW_PARAM_CHANNELS, uint32(channels)) d.setInterval(SNDRV_PCM_HW_PARAM_RATE, rate) d.setMask(SNDRV_PCM_HW_PARAM_FORMAT, uint32(format)) //d.setMask(SNDRV_PCM_HW_PARAM_SUBFORMAT, 0) // important for smooth playback d.setInterval(SNDRV_PCM_HW_PARAM_BUFFER_SIZE, bufferSize) //d.setInterval(SNDRV_PCM_HW_PARAM_PERIOD_SIZE, 2000) if err := ioctl(d.fd, SNDRV_PCM_IOCTL_HW_PARAMS, &d.hwparams); err != nil { return fmt.Errorf("[alsa] set hw_params: %w", err) } _, i := d.getInterval(SNDRV_PCM_HW_PARAM_FRAME_BITS) d.frameBytes = int(i / 8) _, periods := d.getInterval(SNDRV_PCM_HW_PARAM_PERIODS) _, periodSize := d.getInterval(SNDRV_PCM_HW_PARAM_PERIOD_SIZE) threshold := snd_pcm_uframes_t(periods * periodSize) // same as bufferSize swparams := snd_pcm_sw_params{ //tstamp_mode: SNDRV_PCM_TSTAMP_ENABLE, period_step: 1, avail_min: 1, // start as soon as possible stop_threshold: threshold, } if d.IsCapture() { swparams.start_threshold = 1 } else { swparams.start_threshold = threshold } if err := ioctl(d.fd, SNDRV_PCM_IOCTL_SW_PARAMS, &swparams); err != nil { return fmt.Errorf("[alsa] set sw_params: %w", err) } if err := ioctl(d.fd, SNDRV_PCM_IOCTL_PREPARE, nil); err != nil { return fmt.Errorf("[alsa] prepare: %w", err) } return nil } func (d *Device) Write(b []byte) (n int, err error) { xfer := &snd_xferi{ buf: uintptr(unsafe.Pointer(&b[0])), frames: snd_pcm_uframes_t(len(b) / d.frameBytes), } err = ioctl(d.fd, SNDRV_PCM_IOCTL_WRITEI_FRAMES, xfer) if err == syscall.EPIPE { // auto handle underrun state // https://stackoverflow.com/questions/59396728/how-to-properly-handle-xrun-in-alsa-programming-when-playing-audio-with-snd-pcm err = ioctl(d.fd, SNDRV_PCM_IOCTL_PREPARE, nil) } n = int(xfer.result) * d.frameBytes return } func (d *Device) Read(b []byte) (n int, err error) { xfer := &snd_xferi{ buf: uintptr(unsafe.Pointer(&b[0])), frames: snd_pcm_uframes_t(len(b) / d.frameBytes), } err = ioctl(d.fd, SNDRV_PCM_IOCTL_READI_FRAMES, xfer) n = int(xfer.result) * d.frameBytes return } func (d *Device) init() { for i := range d.hwparams.masks { d.hwparams.masks[i].bits[0] = 0xFFFFFFFF d.hwparams.masks[i].bits[1] = 0xFFFFFFFF } for i := range d.hwparams.intervals { d.hwparams.intervals[i].max = 0xFFFFFFFF } d.hwparams.rmask = 0xFFFFFFFF d.hwparams.cmask = 0 d.hwparams.info = 0xFFFFFFFF } func (d *Device) setInterval(param, val uint32) { d.hwparams.intervals[param-SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].min = val d.hwparams.intervals[param-SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].max = val d.hwparams.intervals[param-SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].bit = 0b0100 // integer } func (d *Device) setIntervalMin(param, val uint32) { d.hwparams.intervals[param-SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].min = val } func (d *Device) getInterval(param uint32) (uint32, uint32) { return d.hwparams.intervals[param-SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].min, d.hwparams.intervals[param-SNDRV_PCM_HW_PARAM_FIRST_INTERVAL].max } func (d *Device) setMask(mask, val uint32) { d.hwparams.masks[mask].bits[0] = 0 d.hwparams.masks[mask].bits[1] = 0 d.hwparams.masks[mask].bits[val>>5] = 1 << (val & 0x1F) } func (d *Device) checkMask(mask, val uint32) bool { return d.hwparams.masks[mask].bits[val>>5]&(1<<(val&0x1F)) > 0 } ================================================ FILE: pkg/alsa/device/ioctl_linux.go ================================================ package device import ( "bytes" "reflect" "syscall" ) func ioctl(fd, req uintptr, arg any) error { var ptr uintptr if arg != nil { ptr = reflect.ValueOf(arg).Pointer() } _, _, err := syscall.Syscall(syscall.SYS_IOCTL, fd, req, ptr) if err != 0 { return err } return nil } func str(b []byte) string { if i := bytes.IndexByte(b, 0); i >= 0 { return string(b[:i]) } return string(b) } ================================================ FILE: pkg/alsa/open_linux.go ================================================ package alsa import ( "errors" "fmt" "net/url" "github.com/AlexxIT/go2rtc/pkg/alsa/device" "github.com/AlexxIT/go2rtc/pkg/core" ) func Open(rawURL string) (core.Producer, error) { // Example (ffmpeg source compatible): // alsa:device?audio=/dev/snd/pcmC0D0p // TODO: ?audio=default // TODO: ?audio=hw:0,0 // TODO: &sample_rate=48000&channels=2 // TODO: &backchannel=1 u, err := url.Parse(rawURL) if err != nil { return nil, err } path := u.Query().Get("audio") dev, err := device.Open(path) if err != nil { return nil, err } if !dev.CheckFormat(device.SNDRV_PCM_FORMAT_S16_LE) { _ = dev.Close() return nil, errors.New("alsa: format S16LE not supported") } switch path[len(path)-1] { case 'p': // playback return newPlayback(dev) case 'c': // capture return newCapture(dev) } _ = dev.Close() return nil, fmt.Errorf("alsa: unknown path: %s", path) } ================================================ FILE: pkg/alsa/playback_linux.go ================================================ package alsa import ( "fmt" "github.com/AlexxIT/go2rtc/pkg/alsa/device" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/pcm" "github.com/pion/rtp" ) type Playback struct { core.Connection dev *device.Device closed core.Waiter } func newPlayback(dev *device.Device) (*Playback, error) { medias := []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecPCML}, // support ffmpeg producer (auto transcode) {Name: core.CodecPCMA, ClockRate: 8000}, // support webrtc producer }, }, } return &Playback{ Connection: core.Connection{ ID: core.NewID(), FormatName: "alsa", Medias: medias, Transport: dev, }, dev: dev, }, nil } func (p *Playback) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return nil, core.ErrCantGetTrack } func (p *Playback) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { src := track.Codec dst := &core.Codec{ Name: core.CodecPCML, ClockRate: p.dev.GetRateNear(src.ClockRate), Channels: p.dev.GetChannelsNear(src.Channels), } sender := core.NewSender(media, dst) sender.Handler = func(pkt *rtp.Packet) { if n, err := p.dev.Write(pkt.Payload); err == nil { p.Send += n } } if sender.Handler = pcm.TranscodeHandler(dst, src, sender.Handler); sender.Handler == nil { return fmt.Errorf("alsa: can't convert %s to %s", src, dst) } // typical card support: // - Formats: S16_LE, S32_LE // - ClockRates: 8000 - 192000 // - Channels: 2 - 10 err := p.dev.SetHWParams(device.SNDRV_PCM_FORMAT_S16_LE, dst.ClockRate, byte(dst.Channels)) if err != nil { return err } sender.HandleRTP(track) p.Senders = append(p.Senders, sender) return nil } func (p *Playback) Start() (err error) { return p.closed.Wait() } func (p *Playback) Stop() error { p.closed.Done(nil) return p.Connection.Stop() } ================================================ FILE: pkg/ascii/README.md ================================================ ## Useful links - https://en.wikipedia.org/wiki/ANSI_escape_code - https://paulbourke.net/dataformats/asciiart/ - https://github.com/kutuluk/xterm-color-chart - https://github.com/hugomd/parrot.live ================================================ FILE: pkg/ascii/ascii.go ================================================ package ascii import ( "bytes" "fmt" "image/jpeg" "io" "net/http" "unicode/utf8" ) func NewWriter(w io.Writer, foreground, background, text string) io.Writer { // once clear screen _, _ = w.Write([]byte(csiClear)) // every frame - move to home a := &writer{wr: w, buf: []byte(csiHome)} // https://en.wikipedia.org/wiki/ANSI_escape_code switch foreground { case "": case "8": a.color = func(r, g, b uint8) { idx := xterm256color(r, g, b, 8) a.appendEsc(fmt.Sprintf("\033[%dm", 30+idx)) } case "256": a.color = func(r, g, b uint8) { idx := xterm256color(r, g, b, 255) a.appendEsc(fmt.Sprintf("\033[38;5;%dm", idx)) } case "rgb": a.color = func(r, g, b uint8) { a.appendEsc(fmt.Sprintf("\033[38;2;%d;%d;%dm", r, g, b)) } default: a.buf = append(a.buf, "\033["+foreground+"m"...) } switch background { case "": case "8": a.color = func(r, g, b uint8) { idx := xterm256color(r, g, b, 8) a.appendEsc(fmt.Sprintf("\033[%dm", 40+idx)) } case "256": a.color = func(r, g, b uint8) { idx := xterm256color(r, g, b, 255) a.appendEsc(fmt.Sprintf("\033[48;5;%dm", idx)) } case "rgb": a.color = func(r, g, b uint8) { a.appendEsc(fmt.Sprintf("\033[48;2;%d;%d;%dm", r, g, b)) } default: a.buf = append(a.buf, "\033["+background+"m"...) } a.pre = len(a.buf) // save prefix size if len(text) == 1 { // fast 1 symbol version a.text = func(_, _, _ uint32) { a.buf = append(a.buf, text[0]) } } else { switch text { case "": text = ` .::--~~==++**##%%$@` // default for empty text case "block": text = " ░░▒▒▓▓█" // https://en.wikipedia.org/wiki/Block_Elements } if runes := []rune(text); len(runes) != len(text) { k := float32(len(runes)-1) / 255 a.text = func(r, g, b uint32) { i := gray(r, g, b, k) a.buf = utf8.AppendRune(a.buf, runes[i]) } } else { k := float32(len(text)-1) / 255 a.text = func(r, g, b uint32) { i := gray(r, g, b, k) a.buf = append(a.buf, text[i]) } } } return a } type writer struct { wr io.Writer buf []byte pre int esc string color func(r, g, b uint8) text func(r, g, b uint32) } // https://stackoverflow.com/questions/37774983/clearing-the-screen-by-printing-a-character const csiClear = "\033[2J" const csiHome = "\033[H" func (a *writer) Write(p []byte) (n int, err error) { img, err := jpeg.Decode(bytes.NewReader(p)) if err != nil { return 0, err } a.buf = a.buf[:a.pre] // restore prefix w := img.Bounds().Dx() h := img.Bounds().Dy() for y := 0; y < h; y++ { for x := 0; x < w; x++ { r, g, b, _ := img.At(x, y).RGBA() if a.color != nil { a.color(uint8(r>>8), uint8(g>>8), uint8(b>>8)) } a.text(r, g, b) } a.buf = append(a.buf, '\n') } a.appendEsc("\033[0m") if _, err = a.wr.Write(a.buf); err != nil { return 0, err } a.wr.(http.Flusher).Flush() return len(p), nil } // appendEsc - append ESC code to buffer, and skip duplicates func (a *writer) appendEsc(s string) { if a.esc != s { a.esc = s a.buf = append(a.buf, s...) } } func gray(r, g, b uint32, k float32) uint8 { gr := (19595*r + 38470*g + 7471*b + 1<<15) >> 24 // uint8 return uint8(float32(gr) * k) } const x256r = "\x00\x80\x00\x80\x00\x80\x00\xc0\x80\xff\x00\xff\x00\xff\x00\xff\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x5f\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\x87\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xaf\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xd7\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\x08\x12\x1c\x26\x30\x3a\x44\x4e\x58\x60\x66\x76\x80\x8a\x94\x9e\xa8\xb2\xbc\xc6\xd0\xda\xe4\xee" const x256g = "\x00\x00\x80\x80\x00\x00\x80\xc0\x80\x00\xff\xff\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00\x5f\x5f\x5f\x5f\x5f\x5f\x87\x87\x87\x87\x87\x87\xaf\xaf\xaf\xaf\xaf\xaf\xd7\xd7\xd7\xd7\xd7\xd7\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x5f\x5f\x5f\x5f\x5f\x5f\x87\x87\x87\x87\x87\x87\xaf\xaf\xaf\xaf\xaf\xaf\xd7\xd7\xd7\xd7\xd7\xd7\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x5f\x5f\x5f\x5f\x5f\x5f\x87\x87\x87\x87\x87\x87\xaf\xaf\xaf\xaf\xaf\xaf\xd7\xd7\xd7\xd7\xd7\xd7\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x5f\x5f\x5f\x5f\x5f\x5f\x87\x87\x87\x87\x87\x87\xaf\xaf\xaf\xaf\xaf\xaf\xd7\xd7\xd7\xd7\xd7\xd7\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x5f\x5f\x5f\x5f\x5f\x5f\x87\x87\x87\x87\x87\x87\xaf\xaf\xaf\xaf\xaf\xaf\xd7\xd7\xd7\xd7\xd7\xd7\xff\xff\xff\xff\xff\xff\x00\x00\x00\x00\x00\x00\x5f\x5f\x5f\x5f\x5f\x5f\x87\x87\x87\x87\x87\x87\xaf\xaf\xaf\xaf\xaf\xaf\xd7\xd7\xd7\xd7\xd7\xd7\xff\xff\xff\xff\xff\xff\x08\x12\x1c\x26\x30\x3a\x44\x4e\x58\x60\x66\x76\x80\x8a\x94\x9e\xa8\xb2\xbc\xc6\xd0\xda\xe4\xee" const x256b = "\x00\x00\x00\x00\x80\x80\x80\xc0\x80\x00\x00\x00\xff\xff\xff\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x00\x5f\x87\xaf\xd7\xff\x08\x12\x1c\x26\x30\x3a\x44\x4e\x58\x60\x66\x76\x80\x8a\x94\x9e\xa8\xb2\xbc\xc6\xd0\xda\xe4\xee" func xterm256color(r, g, b uint8, n int) (index uint8) { best := uint16(0xFFFF) for i := 0; i < n; i++ { diff := sqDiff(r, x256r[i]) + sqDiff(g, x256g[i]) + sqDiff(b, x256b[i]) if diff < best { best = diff index = uint8(i) } } return } // sqDiff - just like from image/color/color.go func sqDiff(x, y uint8) uint16 { d := uint16(x - y) //return d return (d * d) >> 2 } ================================================ FILE: pkg/bits/reader.go ================================================ package bits type Reader struct { EOF bool // if end of buffer raised during reading buf []byte // total buf byte byte // current byte bits byte // bits left in byte pos int // current pos in buf } func NewReader(b []byte) *Reader { return &Reader{buf: b} } //goland:noinspection GoStandardMethods func (r *Reader) ReadByte() byte { if r.bits != 0 { return r.ReadBits8(8) } if r.pos >= len(r.buf) { r.EOF = true return 0 } b := r.buf[r.pos] r.pos++ return b } func (r *Reader) ReadUint16() uint16 { if r.bits != 0 { return r.ReadBits16(16) } return uint16(r.ReadByte())<<8 | uint16(r.ReadByte()) } func (r *Reader) ReadUint24() uint32 { if r.bits != 0 { return r.ReadBits(24) } return uint32(r.ReadByte())<<16 | uint32(r.ReadByte())<<8 | uint32(r.ReadByte()) } func (r *Reader) ReadUint32() uint32 { if r.bits != 0 { return r.ReadBits(32) } return uint32(r.ReadByte())<<24 | uint32(r.ReadByte())<<16 | uint32(r.ReadByte())<<8 | uint32(r.ReadByte()) } func (r *Reader) ReadBit() byte { if r.bits == 0 { r.byte = r.ReadByte() r.bits = 7 } else { r.bits-- } return (r.byte >> r.bits) & 0b1 } func (r *Reader) ReadBits(n byte) (res uint32) { for i := n - 1; i != 255; i-- { res |= uint32(r.ReadBit()) << i } return } func (r *Reader) ReadBits8(n byte) (res uint8) { for i := n - 1; i != 255; i-- { res |= r.ReadBit() << i } return } func (r *Reader) ReadBits16(n byte) (res uint16) { for i := n - 1; i != 255; i-- { res |= uint16(r.ReadBit()) << i } return } func (r *Reader) ReadBits64(n byte) (res uint64) { for i := n - 1; i != 255; i-- { res |= uint64(r.ReadBit()) << i } return } func (r *Reader) ReadFloat32() float64 { i := r.ReadUint16() f := r.ReadUint16() return float64(i) + float64(f)/65536 } func (r *Reader) ReadBytes(n int) (b []byte) { if r.bits == 0 { if r.pos+n > len(r.buf) { r.EOF = true return nil } b = r.buf[r.pos : r.pos+n] r.pos += n } else { b = make([]byte, n) for i := 0; i < n; i++ { b[i] = r.ReadByte() } } return } // ReadUEGolomb - ReadExponentialGolomb (unsigned) func (r *Reader) ReadUEGolomb() uint32 { var size byte for size = 0; size < 32; size++ { if b := r.ReadBit(); b != 0 || r.EOF { break } } return r.ReadBits(size) + (1 << size) - 1 } // ReadSEGolomb - ReadSignedExponentialGolomb func (r *Reader) ReadSEGolomb() int32 { if b := r.ReadUEGolomb(); b%2 == 0 { return -int32(b / 2) } else { return int32((b + 1) / 2) } } func (r *Reader) Left() []byte { return r.buf[r.pos:] } func (r *Reader) Pos() (int, byte) { return r.pos - 1, r.bits } ================================================ FILE: pkg/bits/writer.go ================================================ package bits type Writer struct { buf []byte // total buf byte *byte // pointer to current byte bits byte // bits left in byte } func NewWriter(buf []byte) *Writer { return &Writer{buf: buf} } //goland:noinspection GoStandardMethods func (w *Writer) WriteByte(b byte) { if w.bits != 0 { w.WriteBits8(b, 8) } w.buf = append(w.buf, b) } func (w *Writer) WriteBit(b byte) { if w.bits == 0 { w.buf = append(w.buf, 0) w.byte = &w.buf[len(w.buf)-1] w.bits = 7 } else { w.bits-- } *w.byte |= (b & 1) << w.bits } func (w *Writer) WriteBits(v uint32, n byte) { for i := n - 1; i != 255; i-- { w.WriteBit(byte(v>>i) & 0b1) } } func (w *Writer) WriteBits16(v uint16, n byte) { for i := n - 1; i != 255; i-- { w.WriteBit(byte(v>>i) & 0b1) } } func (w *Writer) WriteBits8(v, n byte) { for i := n - 1; i != 255; i-- { w.WriteBit((v >> i) & 0b1) } } func (w *Writer) WriteAllBits(bit, n byte) { for i := byte(0); i < n; i++ { w.WriteBit(bit) } } func (w *Writer) WriteBool(b bool) { if b { w.WriteBit(1) } else { w.WriteBit(0) } } func (w *Writer) WriteUint16(v uint16) { if w.bits != 0 { w.WriteBits16(v, 16) } w.buf = append(w.buf, byte(v>>8), byte(v)) } func (w *Writer) WriteBytes(bytes ...byte) { if w.bits != 0 { for _, b := range bytes { w.WriteByte(b) } } w.buf = append(w.buf, bytes...) } func (w *Writer) Bytes() []byte { return w.buf } func (w *Writer) Len() int { return len(w.buf) } func (w *Writer) Reset() { w.buf = w.buf[:0] w.bits = 0 } ================================================ FILE: pkg/bubble/client.go ================================================ // Package bubble, because: // Request URL: /bubble/live?ch=0&stream=0 // Response Conten-Type: video/bubble // https://github.com/Lynch234ok/lynch-git/blob/master/app_rebulid/src/bubble.c package bubble import ( "bufio" "encoding/binary" "errors" "io" "net" "net/http" "net/url" "regexp" "strings" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/AlexxIT/go2rtc/pkg/tcp" "github.com/pion/rtp" ) // Deprecated: should be rewritten to core.Connection type Client struct { core.Listener url string conn net.Conn videoCodec string channel int stream int r *bufio.Reader medias []*core.Media receivers []*core.Receiver videoTrack *core.Receiver audioTrack *core.Receiver recv int } func Dial(rawURL string) (*Client, error) { client := &Client{url: rawURL} if err := client.Dial(); err != nil { return nil, err } return client, nil } const ( SyncByte = 0xAA PacketAuth = 0x00 PacketMedia = 0x01 PacketStart = 0x0A ) const Timeout = time.Second * 5 func (c *Client) Dial() (err error) { u, err := url.Parse(c.url) if err != nil { return } if c.conn, err = net.DialTimeout("tcp", u.Host, Timeout); err != nil { return } if err = c.conn.SetDeadline(time.Now().Add(Timeout)); err != nil { return } req := &tcp.Request{Method: "GET", URL: &url.URL{Path: u.Path, RawQuery: u.RawQuery}, Proto: "HTTP/1.1"} if err = req.Write(c.conn); err != nil { return } c.r = bufio.NewReader(c.conn) res, err := tcp.ReadResponse(c.r) if err != nil { return } if res.StatusCode != http.StatusOK { return errors.New("wrong response: " + res.Status) } // 1. Read 1024 bytes with XML, some cameras returns exact 1024, but some - 923 xml := make([]byte, 1024) if _, err = c.r.Read(xml); err != nil { return } // 2. Write size uint32 + unknown 4b + user 20b + pass 20b b := make([]byte, 48) binary.BigEndian.PutUint32(b, 44) if u.User != nil { copy(b[8:], u.User.Username()) pass, _ := u.User.Password() copy(b[28:], pass) } else { copy(b[8:], "admin") } if err = c.Write(PacketAuth, 0x0E16C271, b); err != nil { return } // 3. Read response cmd, b, err := c.Read() if err != nil { return } if cmd != PacketAuth || len(b) != 44 || b[4] != 3 || b[8] != 1 { return errors.New("wrong auth response") } // 4. Parse XML (from 1) query := u.Query() stream := query.Get("stream") if stream != "" { c.stream = core.Atoi(stream) } else { stream = "0" } // // // // // re := regexp.MustCompile("]+") stream = re.FindString(string(xml)) if strings.Contains(stream, ".265") { c.videoCodec = core.CodecH265 } else { c.videoCodec = core.CodecH264 } if ch := query.Get("ch"); ch != "" { c.channel = core.Atoi(ch) } return } func (c *Client) Write(command byte, timestamp uint32, payload []byte) error { if err := c.conn.SetWriteDeadline(time.Now().Add(Timeout)); err != nil { return err } // 0xAA + size uint32 + cmd byte + ts uint32 + payload b := make([]byte, 14+len(payload)) b[0] = SyncByte binary.BigEndian.PutUint32(b[1:], uint32(5+len(payload))) b[5] = command binary.BigEndian.PutUint32(b[6:], timestamp) copy(b[10:], payload) _, err := c.conn.Write(b) return err } func (c *Client) Read() (byte, []byte, error) { if err := c.conn.SetReadDeadline(time.Now().Add(Timeout)); err != nil { return 0, nil, err } // 0xAA + size uint32 + cmd byte + ts uint32 + payload b := make([]byte, 10) if _, err := io.ReadFull(c.r, b); err != nil { return 0, nil, err } if b[0] != SyncByte { return 0, nil, errors.New("wrong start byte") } size := binary.BigEndian.Uint32(b[1:]) payload := make([]byte, size-1-4) if _, err := io.ReadFull(c.r, payload); err != nil { return 0, nil, err } //timestamp := binary.BigEndian.Uint32(b[6:]) // in ms return b[5], payload, nil } func (c *Client) Play() error { // yeah, there's no mistake about the little endian b := make([]byte, 16) binary.LittleEndian.PutUint32(b, uint32(c.channel)) binary.LittleEndian.PutUint32(b[4:], uint32(c.stream)) binary.LittleEndian.PutUint32(b[8:], 1) // opened return c.Write(PacketStart, 0x0E16C2DF, b) } func (c *Client) Handle() error { var audioTS uint32 for { cmd, b, err := c.Read() if err != nil { return err } c.recv += len(b) if cmd != PacketMedia { continue } // size uint32 + type 1b + channel 1b // type = 1 for keyframe, 2 for other frame, 0 for audio if b[4] > 0 { if c.videoTrack == nil { continue } pkt := &rtp.Packet{ Header: rtp.Header{ Timestamp: core.Now90000(), }, Payload: annexb.EncodeToAVCC(b[6:]), } c.videoTrack.WriteRTP(pkt) } else { if c.audioTrack == nil { continue } //binary.LittleEndian.Uint32(b[6:]) // entries (always 1) //size := binary.LittleEndian.Uint32(b[10:]) // size //mk := binary.LittleEndian.Uint64(b[14:]) // pts (uint64_t) //binary.LittleEndian.Uint32(b[22:]) // gtime (time_t) //name := b[26:34] // g711 //rate := binary.LittleEndian.Uint32(b[34:]) // sample rate //width := binary.LittleEndian.Uint32(b[38:]) // samplewidth pkt := &rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: true, Timestamp: audioTS, }, Payload: b[6+36:], } audioTS += uint32(len(pkt.Payload)) c.audioTrack.WriteRTP(pkt) } } } func (c *Client) Close() error { return c.conn.Close() } ================================================ FILE: pkg/bubble/producer.go ================================================ package bubble import ( "encoding/json" "github.com/AlexxIT/go2rtc/pkg/core" ) func (c *Client) GetMedias() []*core.Media { if c.medias == nil { c.medias = []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ {Name: c.videoCodec, ClockRate: 90000, PayloadType: core.PayloadTypeRAW}, }, }, { Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ {Name: core.CodecPCMA, ClockRate: 8000, PayloadType: 8}, }, }, } } return c.medias } func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { for _, track := range c.receivers { if track.Codec == codec { return track, nil } } track := core.NewReceiver(media, codec) switch media.Kind { case core.KindVideo: c.videoTrack = track case core.KindAudio: c.audioTrack = track } c.receivers = append(c.receivers, track) return track, nil } func (c *Client) Start() error { if err := c.Play(); err != nil { return err } return c.Handle() } func (c *Client) Stop() error { for _, receiver := range c.receivers { receiver.Close() } return c.Close() } func (c *Client) MarshalJSON() ([]byte, error) { info := &core.Connection{ ID: core.ID(c), FormatName: "bubble", Protocol: "http", Medias: c.medias, Recv: c.recv, Receivers: c.receivers, } if c.conn != nil { info.RemoteAddr = c.conn.RemoteAddr().String() } return json.Marshal(info) } ================================================ FILE: pkg/core/README.md ================================================ ## PCM **RTSP** - PayloadType=10 - L16/44100/2 - Linear PCM 16-bit big endian - PayloadType=11 - L16/44100/1 - Linear PCM 16-bit big endian https://en.wikipedia.org/wiki/RTP_payload_formats **Apple QuickTime** - `raw` - 16-bit data is stored in little endian format - `twos` - 16-bit data is stored in big endian format - `sowt` - 16-bit data is stored in little endian format - `in24` - denotes 24-bit, big endian - `in32` - denotes 32-bit, big endian - `fl32` - denotes 32-bit floating point PCM - `fl64` - denotes 64-bit floating point PCM - `alaw` - denotes A-law logarithmic PCM - `ulaw` - denotes mu-law logarithmic PCM https://wiki.multimedia.cx/index.php/PCM **FFmpeg RTSP** ``` pcm_s16be, 44100 Hz, stereo => 10 pcm_s16be, 48000 Hz, stereo => 96 L16/48000/2 pcm_s16be, 44100 Hz, mono => 11 pcm_s16le, 48000 Hz, stereo => 96 (b=AS:1536) pcm_s16le, 44100 Hz, stereo => 96 (b=AS:1411) pcm_s16le, 16000 Hz, stereo => 96 (b=AS:512) pcm_s16le, 8000 Hz, stereo => 96 (b=AS:256) pcm_s16le, 48000 Hz, mono => 96 (b=AS:768) pcm_s16le, 44100 Hz, mono => 96 (b=AS:705) pcm_s16le, 16000 Hz, mono => 96 (b=AS:256) pcm_s16le, 8000 Hz, mono => 96 (b=AS:128) ``` ================================================ FILE: pkg/core/codec.go ================================================ package core import ( "encoding/base64" "encoding/json" "fmt" "strings" "unicode" "github.com/pion/sdp/v3" ) type Codec struct { Name string // H264, PCMU, PCMA, opus... ClockRate uint32 // 90000, 8000, 16000... Channels uint8 // 0, 1, 2 FmtpLine string PayloadType uint8 } // MarshalJSON - return FFprobe compatible output func (c *Codec) MarshalJSON() ([]byte, error) { info := map[string]any{} if name := FFmpegCodecName(c.Name); name != "" { info["codec_name"] = name info["codec_type"] = c.Kind() } if c.Name == CodecH264 { profile, level := DecodeH264(c.FmtpLine) if profile != "" { info["profile"] = profile info["level"] = level } } if c.ClockRate != 0 && c.ClockRate != 90000 { info["sample_rate"] = c.ClockRate } if c.Channels > 0 { info["channels"] = c.Channels } return json.Marshal(info) } func FFmpegCodecName(name string) string { switch name { case CodecH264: return "h264" case CodecH265: return "hevc" case CodecJPEG: return "mjpeg" case CodecRAW: return "rawvideo" case CodecPCMA: return "pcm_alaw" case CodecPCMU: return "pcm_mulaw" case CodecPCM: return "pcm_s16be" case CodecPCML: return "pcm_s16le" case CodecAAC: return "aac" case CodecOpus: return "opus" case CodecVP8: return "vp8" case CodecVP9: return "vp9" case CodecAV1: return "av1" case CodecELD: return "aac/eld" case CodecFLAC: return "flac" case CodecMP3: return "mp3" } return name } func (c *Codec) String() (s string) { s = c.Name if c.ClockRate != 0 && c.ClockRate != 90000 { s += fmt.Sprintf("/%d", c.ClockRate) } if c.Channels > 0 { s += fmt.Sprintf("/%d", c.Channels) } return } func (c *Codec) IsRTP() bool { return c.PayloadType != PayloadTypeRAW } func (c *Codec) IsVideo() bool { return c.Kind() == KindVideo } func (c *Codec) IsAudio() bool { return c.Kind() == KindAudio } func (c *Codec) Kind() string { return GetKind(c.Name) } func (c *Codec) PrintName() string { switch c.Name { case CodecAAC: return "AAC" case CodecPCM: return "S16B" case CodecPCML: return "S16L" } return c.Name } func (c *Codec) Clone() *Codec { clone := *c return &clone } func (c *Codec) Match(remote *Codec) bool { switch remote.Name { case CodecAll, CodecAny: return true } return c.Name == remote.Name && (c.ClockRate == remote.ClockRate || remote.ClockRate == 0) && (c.Channels == remote.Channels || remote.Channels == 0) } func UnmarshalCodec(md *sdp.MediaDescription, payloadType string) *Codec { c := &Codec{PayloadType: byte(Atoi(payloadType))} for _, attr := range md.Attributes { switch { case c.Name == "" && attr.Key == "rtpmap" && strings.HasPrefix(attr.Value, payloadType): i := strings.IndexByte(attr.Value, ' ') ss := strings.Split(attr.Value[i+1:], "/") c.Name = strings.ToUpper(ss[0]) // fix tailing space: `a=rtpmap:96 H264/90000 ` c.ClockRate = uint32(Atoi(strings.TrimRightFunc(ss[1], unicode.IsSpace))) if len(ss) == 3 && ss[2] == "2" { c.Channels = 2 } case c.FmtpLine == "" && attr.Key == "fmtp" && strings.HasPrefix(attr.Value, payloadType): if i := strings.IndexByte(attr.Value, ' '); i > 0 { c.FmtpLine = attr.Value[i+1:] } } } switch c.Name { case "PCM": // https://www.reddit.com/r/Hikvision/comments/17elxex/comment/k642g2r/ // check pkg/rtsp/rtsp_test.go TestHikvisionPCM c.Name = CodecPCML case "": // https://en.wikipedia.org/wiki/RTP_payload_formats switch payloadType { case "0": c.Name = CodecPCMU c.ClockRate = 8000 case "8": c.Name = CodecPCMA c.ClockRate = 8000 case "10": c.Name = CodecPCM c.ClockRate = 44100 c.Channels = 2 case "11": c.Name = CodecPCM c.ClockRate = 44100 case "14": c.Name = CodecMP3 c.ClockRate = 90000 // it's not real sample rate case "26": c.Name = CodecJPEG c.ClockRate = 90000 case "96", "97", "98": if len(md.Bandwidth) == 0 { c.Name = payloadType break } // FFmpeg + RTSP + pcm_s16le = doesn't pass info about codec name and params // so try to guess the codec based on bitrate // https://github.com/AlexxIT/go2rtc/issues/523 switch md.Bandwidth[0].Bandwidth { case 128: c.ClockRate = 8000 case 256: c.ClockRate = 16000 case 384: c.ClockRate = 24000 case 512: c.ClockRate = 32000 case 705: c.ClockRate = 44100 case 768: c.ClockRate = 48000 case 1411: // default Windows DShow c.ClockRate = 44100 c.Channels = 2 case 1536: // default Linux ALSA c.ClockRate = 48000 c.Channels = 2 default: c.Name = payloadType break } c.Name = CodecPCML default: c.Name = payloadType } } return c } func DecodeH264(fmtp string) (profile string, level byte) { if ps := Between(fmtp, "sprop-parameter-sets=", ","); ps != "" { if sps, _ := base64.StdEncoding.DecodeString(ps); len(sps) >= 4 { switch sps[1] { case 0x42: profile = "Baseline" case 0x4D: profile = "Main" case 0x58: profile = "Extended" case 0x64: profile = "High" default: profile = fmt.Sprintf("0x%02X", sps[1]) } level = sps[3] } } return } func ParseCodecString(s string) *Codec { var codec Codec ss := strings.Split(s, "/") switch strings.ToLower(ss[0]) { case "pcm_s16be", "s16be", "pcm": codec.Name = CodecPCM case "pcm_s16le", "s16le", "pcml": codec.Name = CodecPCML case "pcm_alaw", "alaw", "pcma", "g711a": codec.Name = CodecPCMA case "pcm_mulaw", "mulaw", "pcmu", "g711u": codec.Name = CodecPCMU case "aac", "mpeg4-generic": codec.Name = CodecAAC case "opus": codec.Name = CodecOpus case "flac": codec.Name = CodecFLAC default: return nil } if len(ss) >= 2 { codec.ClockRate = uint32(Atoi(ss[1])) } if len(ss) >= 3 { codec.Channels = uint8(Atoi(ss[2])) } return &codec } ================================================ FILE: pkg/core/connection.go ================================================ package core import ( "io" "net/http" "reflect" "sync/atomic" ) func NewID() uint32 { return id.Add(1) } // Deprecated: use NewID instead func ID(v any) uint32 { p := uintptr(reflect.ValueOf(v).UnsafePointer()) return 0x8000_0000 | uint32(p) } var id atomic.Uint32 type Info interface { SetProtocol(string) SetRemoteAddr(string) SetSource(string) SetURL(string) WithRequest(*http.Request) GetSource() string } // Connection just like webrtc.PeerConnection // - ID and RemoteAddr used for building Connection(s) graph // - FormatName, Protocol, RemoteAddr, Source, URL, SDP, UserAgent used for info about Connection // - FormatName and Protocol has FFmpeg compatible names // - Transport used for auto closing on Stop type Connection struct { ID uint32 `json:"id,omitempty"` FormatName string `json:"format_name,omitempty"` // rtsp, webrtc, mp4, mjpeg, mpjpeg... Protocol string `json:"protocol,omitempty"` // tcp, udp, http, ws, pipe... RemoteAddr string `json:"remote_addr,omitempty"` // host:port other info Source string `json:"source,omitempty"` URL string `json:"url,omitempty"` SDP string `json:"sdp,omitempty"` UserAgent string `json:"user_agent,omitempty"` Medias []*Media `json:"medias,omitempty"` Receivers []*Receiver `json:"receivers,omitempty"` Senders []*Sender `json:"senders,omitempty"` Recv int `json:"bytes_recv,omitempty"` Send int `json:"bytes_send,omitempty"` Transport any `json:"-"` } func (c *Connection) GetMedias() []*Media { return c.Medias } func (c *Connection) GetTrack(media *Media, codec *Codec) (*Receiver, error) { for _, receiver := range c.Receivers { if receiver.Codec == codec { return receiver, nil } } receiver := NewReceiver(media, codec) c.Receivers = append(c.Receivers, receiver) return receiver, nil } func (c *Connection) Stop() error { for _, receiver := range c.Receivers { receiver.Close() } for _, sender := range c.Senders { sender.Close() } if closer, ok := c.Transport.(io.Closer); ok { return closer.Close() } return nil } // Deprecated: func (c *Connection) Codecs() []*Codec { codecs := make([]*Codec, len(c.Senders)) for i, sender := range c.Senders { codecs[i] = sender.Codec } return codecs } func (c *Connection) SetProtocol(s string) { c.Protocol = s } func (c *Connection) SetRemoteAddr(s string) { if c.RemoteAddr == "" { c.RemoteAddr = s } else { c.RemoteAddr += " forwarded " + s } } func (c *Connection) SetSource(s string) { c.Source = s } func (c *Connection) SetURL(s string) { c.URL = s } func (c *Connection) WithRequest(r *http.Request) { if r.Header.Get("Upgrade") == "websocket" { c.Protocol = "ws" } else { c.Protocol = "http" } c.RemoteAddr = r.RemoteAddr if remote := r.Header.Get("X-Forwarded-For"); remote != "" { c.RemoteAddr += " forwarded " + remote } c.UserAgent = r.UserAgent() } func (c *Connection) GetSource() string { return c.Source } // Create like os.Create, init Consumer with existing Transport func Create(w io.Writer) (*Connection, error) { return &Connection{Transport: w}, nil } // Open like os.Open, init Producer from existing Transport func Open(r io.Reader) (*Connection, error) { return &Connection{Transport: r}, nil } // Dial like net.Dial, init Producer via Dialing func Dial(rawURL string) (*Connection, error) { return &Connection{}, nil } ================================================ FILE: pkg/core/core.go ================================================ package core import "encoding/json" const ( DirectionRecvonly = "recvonly" DirectionSendonly = "sendonly" DirectionSendRecv = "sendrecv" ) const ( KindVideo = "video" KindAudio = "audio" ) const ( CodecH264 = "H264" // payloadType: 96 CodecH265 = "H265" CodecVP8 = "VP8" CodecVP9 = "VP9" CodecAV1 = "AV1" CodecJPEG = "JPEG" // payloadType: 26 CodecRAW = "RAW" CodecPCMU = "PCMU" // payloadType: 0 CodecPCMA = "PCMA" // payloadType: 8 CodecAAC = "MPEG4-GENERIC" CodecOpus = "OPUS" // payloadType: 111 CodecG722 = "G722" CodecMP3 = "MPA" // payload: 14, aka MPEG-1 Layer III CodecPCM = "L16" // Linear PCM (big endian) CodecPCML = "PCML" // Linear PCM (little endian) CodecELD = "ELD" // AAC-ELD CodecFLAC = "FLAC" CodecAll = "ALL" CodecAny = "ANY" ) const PayloadTypeRAW byte = 255 type Producer interface { // GetMedias - return Media(s) with local Media.Direction: // - recvonly for Producer Video/Audio // - sendonly for Producer backchannel GetMedias() []*Media // GetTrack - return Receiver, that can only produce rtp.Packet(s) GetTrack(media *Media, codec *Codec) (*Receiver, error) // Deprecated: rename to Run() Start() error // Deprecated: rename to Close() Stop() error } type Consumer interface { // GetMedias - return Media(s) with local Media.Direction: // - sendonly for Consumer Video/Audio // - recvonly for Consumer backchannel GetMedias() []*Media AddTrack(media *Media, codec *Codec, track *Receiver) error // Deprecated: rename to Close() Stop() error } type Mode byte const ( ModeActiveProducer Mode = iota + 1 // typical source (client) ModePassiveConsumer ModePassiveProducer ModeActiveConsumer ) func (m Mode) String() string { switch m { case ModeActiveProducer: return "active producer" case ModePassiveConsumer: return "passive consumer" case ModePassiveProducer: return "passive producer" case ModeActiveConsumer: return "active consumer" } return "unknown" } func (m Mode) MarshalJSON() ([]byte, error) { return json.Marshal(m.String()) } ================================================ FILE: pkg/core/core_test.go ================================================ package core import ( "fmt" "testing" "github.com/stretchr/testify/require" ) type producer struct { Medias []*Media Receivers []*Receiver id byte } func (p *producer) GetMedias() []*Media { return p.Medias } func (p *producer) GetTrack(_ *Media, codec *Codec) (*Receiver, error) { for _, receiver := range p.Receivers { if receiver.Codec == codec { return receiver, nil } } receiver := NewReceiver(nil, codec) p.Receivers = append(p.Receivers, receiver) return receiver, nil } func (p *producer) Start() error { pkt := &Packet{Payload: []byte{p.id}} p.Receivers[0].Input(pkt) return nil } func (p *producer) Stop() error { for _, receiver := range p.Receivers { receiver.Close() } return nil } type consumer struct { Medias []*Media Senders []*Sender cache chan byte } func (c *consumer) GetMedias() []*Media { return c.Medias } func (c *consumer) AddTrack(_ *Media, _ *Codec, track *Receiver) error { c.cache = make(chan byte, 1) sender := NewSender(nil, track.Codec) sender.Output = func(packet *Packet) { c.cache <- packet.Payload[0] } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } func (c *consumer) Stop() error { for _, sender := range c.Senders { sender.Close() } return nil } func (c *consumer) read() byte { return <-c.cache } func TestName(t *testing.T) { GetProducer := func(b byte) Producer { return &producer{ Medias: []*Media{ { Kind: KindVideo, Direction: DirectionRecvonly, Codecs: []*Codec{ {Name: CodecH264}, }, }, }, id: b, } } // stage1 prod1 := GetProducer(1) cons2 := &consumer{} media1 := prod1.GetMedias()[0] track1, _ := prod1.GetTrack(media1, media1.Codecs[0]) _ = cons2.AddTrack(nil, nil, track1) _ = prod1.Start() require.Equal(t, byte(1), cons2.read()) // stage2 prod2 := GetProducer(2) media2 := prod2.GetMedias()[0] require.NotEqual(t, fmt.Sprintf("%p", media1), fmt.Sprintf("%p", media2)) track2, _ := prod2.GetTrack(media2, media2.Codecs[0]) track1.Replace(track2) _ = prod1.Stop() _ = prod2.Start() require.Equal(t, byte(2), cons2.read()) // stage3 _ = prod2.Stop() } func TestStripUserinfo(t *testing.T) { s := `streams: test: - ffmpeg:rtsp://username:password@10.1.2.3:554/stream1 - ffmpeg:rtsp://10.1.2.3:554/stream1@#video=copy ` s = StripUserinfo(s) require.Equal(t, `streams: test: - ffmpeg:rtsp://***@10.1.2.3:554/stream1 - ffmpeg:rtsp://10.1.2.3:554/stream1@#video=copy `, s) } ================================================ FILE: pkg/core/helpers.go ================================================ package core import ( "crypto/rand" "runtime" "strconv" "strings" "time" ) const ( BufferSize = 64 * 1024 // 64K ConnDialTimeout = 5 * time.Second ConnDeadline = 5 * time.Second ProbeTimeout = 5 * time.Second ) // Now90000 - timestamp for Video (clock rate = 90000 samples per second) func Now90000() uint32 { return uint32(time.Duration(time.Now().UnixNano()) * 90000 / time.Second) } const symbols = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_" // RandString base10 - numbers, base16 - hex, base36 - digits+letters // base64 - URL safe symbols, base0 - crypto random func RandString(size, base byte) string { b := make([]byte, size) if _, err := rand.Read(b); err != nil { panic(err) } if base == 0 { return string(b) } for i := byte(0); i < size; i++ { b[i] = symbols[b[i]%base] } return string(b) } func Before(s, sep string) string { if i := strings.Index(s, sep); i > 0 { return s[:i] } return s } func Between(s, sub1, sub2 string) string { i := strings.Index(s, sub1) if i < 0 { return "" } s = s[i+len(sub1):] if i = strings.Index(s, sub2); i >= 0 { return s[:i] } return s } func Atoi(s string) (i int) { if s != "" { i, _ = strconv.Atoi(s) } return } // ParseByte - fast parsing string to byte function func ParseByte(s string) (b byte) { for i, ch := range []byte(s) { ch -= '0' if ch > 9 { return 0 } if i > 0 { b *= 10 } b += ch } return } func Assert(ok bool) { if !ok { _, file, line, _ := runtime.Caller(1) panic(file + ":" + strconv.Itoa(line)) } } func Caller() string { _, file, line, _ := runtime.Caller(1) return file + ":" + strconv.Itoa(line) } ================================================ FILE: pkg/core/listener.go ================================================ package core type EventFunc func(msg any) // Listener base struct for all classes with support feedback type Listener struct { events []EventFunc } func (l *Listener) Listen(f EventFunc) { l.events = append(l.events, f) } func (l *Listener) Fire(msg any) { for _, f := range l.events { f(msg) } } ================================================ FILE: pkg/core/media.go ================================================ package core import ( "encoding/json" "fmt" "strings" "github.com/pion/sdp/v3" ) // Media take best from: // - deepch/vdk/format/rtsp/sdp.Media // - pion/sdp.MediaDescription type Media struct { Kind string `json:"kind,omitempty"` // video or audio Direction string `json:"direction,omitempty"` // sendonly, recvonly Codecs []*Codec `json:"codecs,omitempty"` ID string `json:"id,omitempty"` // MID for WebRTC, Control for RTSP } func (m *Media) String() string { s := fmt.Sprintf("%s, %s", m.Kind, m.Direction) for _, codec := range m.Codecs { name := codec.String() if strings.Contains(s, name) { continue } s += ", " + name } return s } func (m *Media) MarshalJSON() ([]byte, error) { return json.Marshal(m.String()) } func (m *Media) Clone() *Media { clone := *m clone.Codecs = make([]*Codec, len(m.Codecs)) for i, codec := range m.Codecs { clone.Codecs[i] = codec.Clone() } return &clone } func (m *Media) MatchMedia(remote *Media) (codec, remoteCodec *Codec) { // check same kind and opposite dirrection if m.Kind != remote.Kind || m.Direction == DirectionSendonly && remote.Direction != DirectionRecvonly || m.Direction == DirectionRecvonly && remote.Direction != DirectionSendonly { return nil, nil } for _, codec = range m.Codecs { for _, remoteCodec = range remote.Codecs { if codec.Match(remoteCodec) { return } } } return nil, nil } func (m *Media) MatchCodec(remote *Codec) *Codec { for _, codec := range m.Codecs { if codec.Match(remote) { return codec } } return nil } func (m *Media) MatchAll() bool { for _, codec := range m.Codecs { if codec.Name == CodecAll { return true } } return false } func (m *Media) Equal(media *Media) bool { if media.ID != "" { return m.ID == media.ID } return m.String() == media.String() } func GetKind(name string) string { switch name { case CodecH264, CodecH265, CodecVP8, CodecVP9, CodecAV1, CodecJPEG, CodecRAW: return KindVideo case CodecPCMU, CodecPCMA, CodecAAC, CodecOpus, CodecG722, CodecMP3, CodecPCM, CodecPCML, CodecELD, CodecFLAC: return KindAudio } return "" } func MarshalSDP(name string, medias []*Media) ([]byte, error) { sd := &sdp.SessionDescription{ Origin: sdp.Origin{ Username: "-", SessionID: 1, SessionVersion: 1, NetworkType: "IN", AddressType: "IP4", UnicastAddress: "0.0.0.0", }, SessionName: sdp.SessionName(name), ConnectionInformation: &sdp.ConnectionInformation{ NetworkType: "IN", AddressType: "IP4", Address: &sdp.Address{ Address: "0.0.0.0", }, }, TimeDescriptions: []sdp.TimeDescription{ {Timing: sdp.Timing{}}, }, } for _, media := range medias { if media.Codecs == nil { continue } codec := media.Codecs[0] switch codec.Name { case CodecELD: name = CodecAAC case CodecPCML: name = CodecPCM // beacuse we using pcm.LittleToBig for RTSP server default: name = codec.Name } md := &sdp.MediaDescription{ MediaName: sdp.MediaName{ Media: media.Kind, Protos: []string{"RTP", "AVP"}, }, } md.WithCodec(codec.PayloadType, name, codec.ClockRate, uint16(codec.Channels), codec.FmtpLine) if media.Direction != "" { md.WithPropertyAttribute(media.Direction) } if media.ID != "" { md.WithValueAttribute("control", media.ID) } sd.MediaDescriptions = append(sd.MediaDescriptions, md) } return sd.Marshal() } func UnmarshalMedia(md *sdp.MediaDescription) *Media { m := &Media{ Kind: md.MediaName.Media, } for _, attr := range md.Attributes { switch attr.Key { case DirectionSendonly, DirectionRecvonly, DirectionSendRecv: m.Direction = attr.Key case "control", "mid": m.ID = attr.Value } } for _, format := range md.MediaName.Formats { m.Codecs = append(m.Codecs, UnmarshalCodec(md, format)) } return m } func ParseQuery(query map[string][]string) (medias []*Media) { // set media candidates from query list for key, values := range query { switch key { case KindVideo, KindAudio: for _, value := range values { media := &Media{Kind: key, Direction: DirectionSendonly} for _, name := range strings.Split(value, ",") { name = strings.ToUpper(name) // check aliases switch name { case "", "COPY": name = CodecAny case "MJPEG": name = CodecJPEG case "AAC": name = CodecAAC case "MP3": name = CodecMP3 } media.Codecs = append(media.Codecs, &Codec{Name: name}) } medias = append(medias, media) } } } return } ================================================ FILE: pkg/core/media_test.go ================================================ package core import ( "fmt" "net/url" "testing" "github.com/pion/sdp/v3" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestSDP(t *testing.T) { medias := []*Media{{ Kind: KindAudio, Direction: DirectionSendonly, Codecs: []*Codec{ {Name: CodecPCMU, ClockRate: 8000}, }, }} data, err := MarshalSDP("go2rtc/1.0.0", medias) assert.Empty(t, err) sd := &sdp.SessionDescription{} err = sd.Unmarshal(data) assert.Empty(t, err) } func TestParseQuery(t *testing.T) { u, _ := url.Parse("rtsp://localhost:8554/camera1") medias := ParseQuery(u.Query()) assert.Nil(t, medias) for _, rawULR := range []string{ "rtsp://localhost:8554/camera1?video", "rtsp://localhost:8554/camera1?video=copy", "rtsp://localhost:8554/camera1?video=any", } { u, _ = url.Parse(rawULR) medias = ParseQuery(u.Query()) assert.Equal(t, []*Media{ {Kind: KindVideo, Direction: DirectionSendonly, Codecs: []*Codec{{Name: CodecAny}}}, }, medias) } } func TestClone(t *testing.T) { media1 := &Media{ Kind: KindVideo, Direction: DirectionRecvonly, Codecs: []*Codec{ {Name: CodecPCMU, ClockRate: 8000}, }, } media2 := media1.Clone() p1 := fmt.Sprintf("%p", media1) p2 := fmt.Sprintf("%p", media2) require.NotEqualValues(t, p1, p2) p3 := fmt.Sprintf("%p", media1.Codecs[0]) p4 := fmt.Sprintf("%p", media2.Codecs[0]) require.NotEqualValues(t, p3, p4) } ================================================ FILE: pkg/core/node.go ================================================ package core import ( "sync" "github.com/pion/rtp" ) //type Packet struct { // Payload []byte // Timestamp uint32 // PTS if DTS == 0 else DTS // Composition uint32 // CTS = PTS-DTS (for support B-frames) // Sequence uint16 //} type Packet = rtp.Packet // HandlerFunc - process input packets (just like http.HandlerFunc) type HandlerFunc func(packet *Packet) // Filter - a decorator for any HandlerFunc type Filter func(handler HandlerFunc) HandlerFunc // Node - Receiver or Sender or Filter (transform) type Node struct { Codec *Codec Input HandlerFunc Output HandlerFunc id uint32 childs []*Node parent *Node mu sync.Mutex } func (n *Node) WithParent(parent *Node) *Node { parent.AppendChild(n) return n } func (n *Node) AppendChild(child *Node) { n.mu.Lock() n.childs = append(n.childs, child) n.mu.Unlock() child.parent = n } func (n *Node) RemoveChild(child *Node) { n.mu.Lock() for i, ch := range n.childs { if ch == child { n.childs = append(n.childs[:i], n.childs[i+1:]...) break } } n.mu.Unlock() } func (n *Node) Close() { if parent := n.parent; parent != nil { parent.RemoveChild(n) if len(parent.childs) == 0 { parent.Close() } } else { for _, childs := range n.childs { childs.Close() } } } func MoveNode(dst, src *Node) { src.mu.Lock() childs := src.childs src.childs = nil src.mu.Unlock() dst.mu.Lock() dst.childs = childs dst.mu.Unlock() for _, child := range childs { child.parent = dst } } ================================================ FILE: pkg/core/readbuffer.go ================================================ package core import ( "errors" "io" ) // ProbeSize // in my tests MPEG-TS 40Mbit/s 4K-video require more than 1MB for probe const ProbeSize = 5 * 1024 * 1024 // 5MB const ( BufferDisable = 0 BufferDrainAndClear = -1 ) // ReadBuffer support buffering and Seek over buffer // positive BufferSize will enable buffering mode // Seek to negative offset will clear buffer // Seek with a positive BufferSize will continue buffering after the last read from the buffer // Seek with a negative BufferSize will clear buffer after the last read from the buffer // Read more than BufferSize will raise error type ReadBuffer struct { io.Reader BufferSize int buf []byte pos int } func NewReadBuffer(rd io.Reader) *ReadBuffer { if rs, ok := rd.(*ReadBuffer); ok { return rs } return &ReadBuffer{Reader: rd} } func (r *ReadBuffer) Read(p []byte) (n int, err error) { // with zero buffer - read as usual if r.BufferSize == BufferDisable { return r.Reader.Read(p) } // if buffer not empty - read from it if r.pos < len(r.buf) { n = copy(p, r.buf[r.pos:]) r.pos += n return } // with negative buffer - empty it and read as usual if r.BufferSize < 0 { r.BufferSize = BufferDisable r.buf = nil r.pos = 0 return r.Reader.Read(p) } n, err = r.Reader.Read(p) if len(r.buf)+n > r.BufferSize { return 0, errors.New("probe reader overflow") } r.buf = append(r.buf, p[:n]...) r.pos += n return } func (r *ReadBuffer) Close() error { if closer, ok := r.Reader.(io.Closer); ok { return closer.Close() } return nil } func (r *ReadBuffer) Seek(offset int64, whence int) (int64, error) { var pos int switch whence { case io.SeekStart: pos = int(offset) case io.SeekCurrent: pos = r.pos + int(offset) case io.SeekEnd: pos = len(r.buf) + int(offset) } // negative offset - empty buffer if pos < 0 { r.buf = nil r.pos = 0 } else if pos >= len(r.buf) { r.pos = len(r.buf) } else { r.pos = pos } return int64(r.pos), nil } func (r *ReadBuffer) Peek(n int) ([]byte, error) { r.BufferSize = n b := make([]byte, n) if _, err := io.ReadAtLeast(r, b, n); err != nil { return nil, err } r.Reset() return b, nil } func (r *ReadBuffer) Reset() { r.BufferSize = BufferDrainAndClear r.pos = 0 } ================================================ FILE: pkg/core/readbuffer_test.go ================================================ package core import ( "bytes" "io" "testing" "github.com/stretchr/testify/require" ) func TestReadSeeker(t *testing.T) { b := []byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9} buf := bytes.NewReader(b) rd := NewReadBuffer(buf) rd.BufferSize = ProbeSize // 1. Read to buffer b = make([]byte, 3) n, err := rd.Read(b) require.Nil(t, err) require.Equal(t, []byte{0, 1, 2}, b[:n]) // 2. Seek to start _, err = rd.Seek(0, io.SeekStart) require.Nil(t, err) // 3. Read from buffer b = make([]byte, 2) n, err = rd.Read(b) require.Nil(t, err) require.Equal(t, []byte{0, 1}, b[:n]) // 4. Read from buffer n, err = rd.Read(b) require.Nil(t, err) require.Equal(t, []byte{2}, b[:n]) // 5. Read to buffer n, err = rd.Read(b) require.Nil(t, err) require.Equal(t, []byte{3, 4}, b[:n]) // 6. Seek to start _, err = rd.Seek(0, io.SeekStart) require.Nil(t, err) // 7. Disable buffer rd.BufferSize = -1 // 8. Read from buffer b = make([]byte, 10) n, err = rd.Read(b) require.Nil(t, err) require.Equal(t, []byte{0, 1, 2, 3, 4}, b[:n]) // 9. Direct read n, err = rd.Read(b) require.Nil(t, err) require.Equal(t, []byte{5, 6, 7, 8, 9}, b[:n]) // 10. Check buffer empty require.Nil(t, rd.buf) } ================================================ FILE: pkg/core/slices.go ================================================ package core // This code copied from go1.21 for backward support in go1.20. // We need to support go1.20 for Windows 7 // Index returns the index of the first occurrence of v in s, // or -1 if not present. func Index[S ~[]E, E comparable](s S, v E) int { for i := range s { if v == s[i] { return i } } return -1 } // Contains reports whether v is present in s. func Contains[S ~[]E, E comparable](s S, v E) bool { return Index(s, v) >= 0 } type Ordered interface { ~int | ~int8 | ~int16 | ~int32 | ~int64 | ~uint | ~uint8 | ~uint16 | ~uint32 | ~uint64 | ~uintptr | ~float32 | ~float64 | ~string } // Max returns the maximal value in x. It panics if x is empty. // For floating-point E, Max propagates NaNs (any NaN value in x // forces the output to be NaN). func Max[S ~[]E, E Ordered](x S) E { if len(x) < 1 { panic("slices.Max: empty list") } m := x[0] for i := 1; i < len(x); i++ { if x[i] > m { m = x[i] } } return m } ================================================ FILE: pkg/core/track.go ================================================ package core import ( "encoding/json" "errors" "github.com/pion/rtp" ) var ErrCantGetTrack = errors.New("can't get track") type Receiver struct { Node // Deprecated: should be removed Media *Media `json:"-"` // Deprecated: should be removed ID byte `json:"-"` // Channel for RTSP, PayloadType for MPEG-TS Bytes int `json:"bytes,omitempty"` Packets int `json:"packets,omitempty"` } func NewReceiver(media *Media, codec *Codec) *Receiver { r := &Receiver{ Node: Node{id: NewID(), Codec: codec}, Media: media, } r.Input = func(packet *Packet) { r.Bytes += len(packet.Payload) r.Packets++ for _, child := range r.childs { child.Input(packet) } } return r } // Deprecated: should be removed func (r *Receiver) WriteRTP(packet *rtp.Packet) { r.Input(packet) } // Deprecated: should be removed func (r *Receiver) Senders() []*Sender { if len(r.childs) > 0 { return []*Sender{{}} } else { return nil } } // Deprecated: should be removed func (r *Receiver) Replace(target *Receiver) { MoveNode(&target.Node, &r.Node) } func (r *Receiver) Close() { r.Node.Close() } type Sender struct { Node // Deprecated: Media *Media `json:"-"` // Deprecated: Handler HandlerFunc `json:"-"` Bytes int `json:"bytes,omitempty"` Packets int `json:"packets,omitempty"` Drops int `json:"drops,omitempty"` buf chan *Packet done chan struct{} } func NewSender(media *Media, codec *Codec) *Sender { var bufSize uint16 if GetKind(codec.Name) == KindVideo { if codec.IsRTP() { // in my tests 40Mbit/s 4K-video can generate up to 1500 items // for the h264.RTPDepay => RTPPay queue bufSize = 4096 } else { bufSize = 64 } } else { bufSize = 128 } buf := make(chan *Packet, bufSize) s := &Sender{ Node: Node{id: NewID(), Codec: codec}, Media: media, buf: buf, } s.Input = func(packet *Packet) { s.mu.Lock() // unblock write to nil chan - OK, write to closed chan - panic select { case s.buf <- packet: s.Bytes += len(packet.Payload) s.Packets++ default: s.Drops++ } s.mu.Unlock() } s.Output = func(packet *Packet) { s.Handler(packet) } return s } // Deprecated: should be removed func (s *Sender) HandleRTP(parent *Receiver) { s.WithParent(parent) s.Start() } // Deprecated: should be removed func (s *Sender) Bind(parent *Receiver) { s.WithParent(parent) } func (s *Sender) WithParent(parent *Receiver) *Sender { s.Node.WithParent(&parent.Node) return s } func (s *Sender) Start() { s.mu.Lock() defer s.mu.Unlock() if s.buf == nil || s.done != nil { return } s.done = make(chan struct{}) // pass buf directly so that it's impossible for buf to be nil go func(buf chan *Packet) { for packet := range buf { s.Output(packet) } close(s.done) }(s.buf) } func (s *Sender) Wait() { if done := s.done; done != nil { <-done } } func (s *Sender) State() string { if s.buf == nil { return "closed" } if s.done == nil { return "new" } return "connected" } func (s *Sender) Close() { // close buffer if exists s.mu.Lock() if s.buf != nil { close(s.buf) // exit from for range loop s.buf = nil // prevent writing to closed chan } s.mu.Unlock() s.Node.Close() } func (r *Receiver) MarshalJSON() ([]byte, error) { v := struct { ID uint32 `json:"id"` Codec *Codec `json:"codec"` Childs []uint32 `json:"childs,omitempty"` Bytes int `json:"bytes,omitempty"` Packets int `json:"packets,omitempty"` }{ ID: r.Node.id, Codec: r.Node.Codec, Bytes: r.Bytes, Packets: r.Packets, } for _, child := range r.childs { v.Childs = append(v.Childs, child.id) } return json.Marshal(v) } func (s *Sender) MarshalJSON() ([]byte, error) { v := struct { ID uint32 `json:"id"` Codec *Codec `json:"codec"` Parent uint32 `json:"parent,omitempty"` Bytes int `json:"bytes,omitempty"` Packets int `json:"packets,omitempty"` Drops int `json:"drops,omitempty"` }{ ID: s.Node.id, Codec: s.Node.Codec, Bytes: s.Bytes, Packets: s.Packets, Drops: s.Drops, } if s.parent != nil { v.Parent = s.parent.id } return json.Marshal(v) } ================================================ FILE: pkg/core/track_test.go ================================================ package core import ( "testing" "github.com/stretchr/testify/require" ) func TestSenser(t *testing.T) { recv := make(chan *Packet) // blocking receiver sender := NewSender(nil, &Codec{}) sender.Output = func(packet *Packet) { recv <- packet } require.Equal(t, "new", sender.State()) sender.Start() require.Equal(t, "connected", sender.State()) sender.Input(&Packet{}) sender.Input(&Packet{}) require.Equal(t, 2, sender.Packets) require.Equal(t, 0, sender.Drops) // important to read one before close // because goroutine in Start() can run with nil chan // it's OK in real life, but bad for test _, ok := <-recv require.True(t, ok) sender.Close() require.Equal(t, "closed", sender.State()) sender.Input(&Packet{}) require.Equal(t, 2, sender.Packets) require.Equal(t, 1, sender.Drops) // read 2nd _, ok = <-recv require.True(t, ok) // read 3rd select { case <-recv: ok = true default: ok = false } require.False(t, ok) } ================================================ FILE: pkg/core/waiter.go ================================================ package core import ( "sync" ) // Waiter support: // - autotart on first Wait // - block new waiters after last Done // - safe Done after finish type Waiter struct { sync.WaitGroup mu sync.Mutex state int // state < 0 means finish err error } func (w *Waiter) Add(delta int) { w.mu.Lock() if w.state >= 0 { w.state += delta w.WaitGroup.Add(delta) } w.mu.Unlock() } func (w *Waiter) Wait() error { w.mu.Lock() // first wait auto start waiter if w.state == 0 { w.state++ w.WaitGroup.Add(1) } w.mu.Unlock() w.WaitGroup.Wait() return w.err } func (w *Waiter) Done(err error) { w.mu.Lock() // safe run Done only when have tasks if w.state > 0 { w.state-- w.WaitGroup.Done() } // block waiter for any operations after last done if w.state == 0 { w.state = -1 w.err = err } w.mu.Unlock() } func (w *Waiter) WaitChan() <-chan error { var ch chan error w.mu.Lock() if w.state >= 0 { ch = make(chan error) go func() { ch <- w.Wait() }() } w.mu.Unlock() return ch } ================================================ FILE: pkg/core/worker.go ================================================ package core import ( "time" ) type Worker struct { timer *time.Timer done chan struct{} } // NewWorker run f after d func NewWorker(d time.Duration, f func() time.Duration) *Worker { timer := time.NewTimer(d) done := make(chan struct{}) go func() { for { select { case <-timer.C: if d = f(); d > 0 { timer.Reset(d) continue } case <-done: timer.Stop() } break } }() return &Worker{timer: timer, done: done} } // Do - instant timer run func (w *Worker) Do() { if w == nil { return } w.timer.Reset(0) } func (w *Worker) Stop() { if w == nil { return } select { case w.done <- struct{}{}: default: } } ================================================ FILE: pkg/core/writebuffer.go ================================================ package core import ( "bytes" "io" "net/http" "sync" ) // WriteBuffer by defaul Write(s) to bytes.Buffer. // But after WriteTo to new io.Writer - calls Reset. // Reset will flush current buffer data to new writer and starts to Write to new io.Writer // WriteTo will be locked until Write fails or Close will be called. type WriteBuffer struct { io.Writer err error mu sync.Mutex wg sync.WaitGroup state byte } func NewWriteBuffer(wr io.Writer) *WriteBuffer { if wr == nil { wr = bytes.NewBuffer(nil) } return &WriteBuffer{Writer: wr} } func (w *WriteBuffer) Write(p []byte) (n int, err error) { w.mu.Lock() if w.err != nil { err = w.err } else if n, err = w.Writer.Write(p); err != nil { w.err = err w.done() } else if f, ok := w.Writer.(http.Flusher); ok { f.Flush() } w.mu.Unlock() return } func (w *WriteBuffer) WriteTo(wr io.Writer) (n int64, err error) { w.Reset(wr) w.wg.Wait() return 0, w.err // TODO: fix counter } func (w *WriteBuffer) Close() error { if closer, ok := w.Writer.(io.Closer); ok { return closer.Close() } w.mu.Lock() w.done() w.mu.Unlock() return nil } func (w *WriteBuffer) Reset(wr io.Writer) { w.mu.Lock() w.add() if buf, ok := w.Writer.(*bytes.Buffer); ok && buf.Len() != 0 { if _, err := io.Copy(wr, buf); err != nil { w.err = err w.done() } } w.Writer = wr w.mu.Unlock() } const ( none = iota start end ) func (w *WriteBuffer) add() { if w.state == none { w.state = start w.wg.Add(1) } } func (w *WriteBuffer) done() { if w.state == start { w.state = end w.wg.Done() } } // OnceBuffer will catch only first message type OnceBuffer struct { buf []byte } func (o *OnceBuffer) Write(p []byte) (n int, err error) { if o.buf == nil { o.buf = p } return 0, io.EOF } func (o *OnceBuffer) WriteTo(w io.Writer) (n int64, err error) { return io.Copy(w, bytes.NewReader(o.buf)) } func (o *OnceBuffer) Buffer() []byte { return o.buf } func (o *OnceBuffer) Len() int { return len(o.buf) } ================================================ FILE: pkg/creds/README.md ================================================ # Credentials This module allows you to get variables: - from custom storage (ex. config file) - from [credential files](https://systemd.io/CREDENTIALS/) - from environment variables ================================================ FILE: pkg/creds/creds.go ================================================ package creds import ( "errors" "os" "path/filepath" "regexp" "strings" ) type Storage interface { SetValue(name, value string) error GetValue(name string) (string, bool) } var storage Storage func SetStorage(s Storage) { storage = s } func SetValue(name, value string) error { if storage == nil { return errors.New("credentials: storage not initialized") } if err := storage.SetValue(name, value); err != nil { return err } AddSecret(value) return nil } func GetValue(name string) (value string, ok bool) { value, ok = getValue(name) AddSecret(value) return } func getValue(name string) (string, bool) { if storage != nil { if value, ok := storage.GetValue(name); ok { return value, true } } if dir, ok := os.LookupEnv("CREDENTIALS_DIRECTORY"); ok { if value, _ := os.ReadFile(filepath.Join(dir, name)); value != nil { return strings.TrimSpace(string(value)), true } } return os.LookupEnv(name) } // ReplaceVars - support format ${CAMERA_PASSWORD} and ${RTSP_USER:admin} func ReplaceVars(data []byte) []byte { re := regexp.MustCompile(`\${([^}{]+)}`) return re.ReplaceAllFunc(data, func(match []byte) []byte { key := string(match[2 : len(match)-1]) var def string var defok bool if i := strings.IndexByte(key, ':'); i > 0 { key, def = key[:i], key[i+1:] defok = true } if value, ok := GetValue(key); ok { return []byte(value) } if defok { return []byte(def) } return match }) } ================================================ FILE: pkg/creds/secrets.go ================================================ package creds import ( "io" "net/http" "regexp" "slices" "strings" "sync" ) func AddSecret(value string) { if value == "" { return } secretsMu.Lock() defer secretsMu.Unlock() if slices.Contains(secrets, value) { return } secrets = append(secrets, value) secretsReplacer = nil } var secrets []string var secretsMu sync.Mutex var secretsReplacer *strings.Replacer var userinfoRegexp *regexp.Regexp func getReplacer() *strings.Replacer { secretsMu.Lock() defer secretsMu.Unlock() if secretsReplacer == nil { oldnew := make([]string, 0, 2*len(secrets)) for _, s := range secrets { oldnew = append(oldnew, s, "***") } secretsReplacer = strings.NewReplacer(oldnew...) } if userinfoRegexp == nil { userinfoRegexp = regexp.MustCompile(`://[` + userinfo + `]+@`) } return secretsReplacer } // Uniform Resource Identifier (URI) // https://datatracker.ietf.org/doc/html/rfc3986 const ( unreserved = `A-Za-z0-9-._~` subdelims = `!$&'()*+,;=` userinfo = unreserved + subdelims + `%:` ) func SecretString(s string) string { re := getReplacer() s = userinfoRegexp.ReplaceAllString(s, `://***@`) return re.Replace(s) } func SecretWrite(w io.Writer, s string) (n int, err error) { re := getReplacer() s = userinfoRegexp.ReplaceAllString(s, `://***@`) return re.WriteString(w, s) } func SecretWriter(w io.Writer) io.Writer { return &secretWriter{w} } type secretWriter struct { w io.Writer } func (s *secretWriter) Write(b []byte) (int, error) { return SecretWrite(s.w, string(b)) } func SecretResponse(w http.ResponseWriter) http.ResponseWriter { return &secretResponse{w} } type secretResponse struct { http.ResponseWriter } func (s *secretResponse) Write(b []byte) (int, error) { return SecretWrite(s.ResponseWriter, string(b)) } ================================================ FILE: pkg/creds/secrets_test.go ================================================ package creds import ( "testing" "github.com/stretchr/testify/require" ) func TestString(t *testing.T) { AddSecret("admin") AddSecret("pa$$word") s := SecretString("rtsp://admin:pa$$word@192.168.1.123/stream1") require.Equal(t, "rtsp://***:***@192.168.1.123/stream1", s) } ================================================ FILE: pkg/debug/conn.go ================================================ package debug import ( "bytes" "math/rand" "net" ) type badConn struct { net.Conn delay int buf []byte } func NewBadConn(conn net.Conn) net.Conn { return &badConn{Conn: conn} } const ( missChance = 0.05 delayChance = 0.1 ) func (c *badConn) Read(b []byte) (n int, err error) { if rand.Float32() < missChance { if _, err = c.Conn.Read(b); err != nil { return } //log.Printf("bad conn: miss") } if c.delay > 0 { if c.delay--; c.delay == 0 { n = copy(b, c.buf) return } } else if rand.Float32() < delayChance { if n, err = c.Conn.Read(b); err != nil { return } c.delay = 1 + rand.Intn(5) c.buf = bytes.Clone(b[:n]) //log.Printf("bad conn: delay %d", c.delay) } return c.Conn.Read(b) } ================================================ FILE: pkg/debug/debug.go ================================================ package debug import ( "fmt" "time" "github.com/pion/rtp" ) func Logger(include func(packet *rtp.Packet) bool) func(packet *rtp.Packet) { var lastTime = time.Now() var lastTS uint32 var secCnt int var secSize int var secTS uint32 var secTime time.Time return func(packet *rtp.Packet) { if include != nil && !include(packet) { return } now := time.Now() fmt.Printf( "%s: size=%6d ts=%10d type=%2d ssrc=%d seq=%5d mark=%t dts=%4d dtime=%3dms\n", now.Format("15:04:05.000"), len(packet.Payload), packet.Timestamp, packet.PayloadType, packet.SSRC, packet.SequenceNumber, packet.Marker, packet.Timestamp-lastTS, now.Sub(lastTime).Milliseconds(), ) lastTS = packet.Timestamp lastTime = now if secTS == 0 { secTS = lastTS secTime = now return } if dt := now.Sub(secTime); dt > time.Second { fmt.Printf( "%s: size=%6d cnt=%d dts=%d dtime=%3dms\n", now.Format("15:04:05.000"), secSize, secCnt, lastTS-secTS, dt.Milliseconds(), ) secCnt = 0 secSize = 0 secTS = lastTS secTime = now } secCnt++ secSize += len(packet.Payload) } } ================================================ FILE: pkg/doorbird/backchannel.go ================================================ package doorbird import ( "fmt" "net" "net/url" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type Client struct { core.Connection conn net.Conn } func Dial(rawURL string) (*Client, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } user := u.User.Username() pass, _ := u.User.Password() if u.Port() == "" { u.Host += ":80" } conn, err := net.DialTimeout("tcp", u.Host, core.ConnDialTimeout) if err != nil { return nil, err } s := fmt.Sprintf("POST /bha-api/audio-transmit.cgi?http-user=%s&http-password=%s HTTP/1.0\r\n", user, pass) + "Content-Type: audio/basic\r\n" + "Content-Length: 9999999\r\n" + "Connection: Keep-Alive\r\n" + "Cache-Control: no-cache\r\n" + "\r\n" _ = conn.SetWriteDeadline(time.Now().Add(core.ConnDeadline)) if _, err = conn.Write([]byte(s)); err != nil { return nil, err } medias := []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecPCMU, ClockRate: 8000}, }, }, } return &Client{ core.Connection{ ID: core.NewID(), FormatName: "doorbird", Protocol: "http", URL: rawURL, Medias: medias, Transport: conn, }, conn, }, nil } func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return nil, core.ErrCantGetTrack } func (c *Client) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { sender := core.NewSender(media, track.Codec) sender.Handler = func(pkt *rtp.Packet) { _ = c.conn.SetWriteDeadline(time.Now().Add(core.ConnDeadline)) if n, err := c.conn.Write(pkt.Payload); err == nil { c.Send += n } } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } func (c *Client) Start() (err error) { // just block until c.conn closed b := make([]byte, 1) _, err = c.conn.Read(b) return } ================================================ FILE: pkg/dvrip/backchannel.go ================================================ package dvrip import ( "encoding/binary" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type Backchannel struct { core.Connection client *Client } func (c *Backchannel) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return nil, core.ErrCantGetTrack } func (c *Backchannel) Start() error { if err := c.client.conn.SetReadDeadline(time.Time{}); err != nil { return err } b := make([]byte, 4096) for { if _, err := c.client.rd.Read(b); err != nil { return err } } } func (c *Backchannel) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error { if err := c.client.Talk(); err != nil { return err } const PacketSize = 320 buf := make([]byte, 8+PacketSize) binary.BigEndian.PutUint32(buf, 0x1FA) switch track.Codec.Name { case core.CodecPCMU: buf[4] = 10 case core.CodecPCMA: buf[4] = 14 } //for i, rate := range sampleRates { // if rate == track.Codec.ClockRate { // buf[5] = byte(i) + 1 // break // } //} buf[5] = 2 // ClockRate=8000 binary.LittleEndian.PutUint16(buf[6:], PacketSize) var payload []byte sender := core.NewSender(media, track.Codec) sender.Handler = func(packet *rtp.Packet) { payload = append(payload, packet.Payload...) for len(payload) >= PacketSize { buf = append(buf[:8], payload[:PacketSize]...) if n, err := c.client.WriteCmd(OPTalkData, buf); err != nil { c.Send += n } payload = payload[PacketSize:] } } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } ================================================ FILE: pkg/dvrip/client.go ================================================ package dvrip import ( "bufio" "bytes" "crypto/md5" "encoding/binary" "encoding/json" "errors" "fmt" "io" "net" "net/url" "time" ) const ( Login = 1000 OPMonitorClaim = 1413 OPMonitorStart = 1410 OPTalkClaim = 1434 OPTalkStart = 1430 OPTalkData = 1432 ) type Client struct { conn net.Conn session uint32 seq uint32 stream string rd io.Reader buf []byte } func (c *Client) Dial(rawURL string) (err error) { u, err := url.Parse(rawURL) if err != nil { return } if u.Port() == "" { // add default TCP port u.Host += ":34567" } c.conn, err = net.DialTimeout("tcp", u.Host, time.Second*3) if err != nil { return } if query := u.Query(); query.Get("backchannel") != "1" { channel := query.Get("channel") if channel == "" { channel = "0" } subtype := query.Get("subtype") switch subtype { case "", "0": subtype = "Main" case "1": subtype = "Extra1" } c.stream = fmt.Sprintf( `{"Channel":%s,"CombinMode":"NONE","StreamType":"%s","TransMode":"TCP"}`, channel, subtype, ) } c.rd = bufio.NewReader(c.conn) if u.User != nil { pass, _ := u.User.Password() return c.Login(u.User.Username(), pass) } else { return c.Login("admin", "admin") } } func (c *Client) Close() error { return c.conn.Close() } func (c *Client) Login(user, pass string) (err error) { data := fmt.Sprintf( `{"EncryptType":"MD5","LoginType":"DVRIP-Web","PassWord":"%s","UserName":"%s"}`+"\x0A\x00", SofiaHash(pass), user, ) if _, err = c.WriteCmd(Login, []byte(data)); err != nil { return } _, err = c.ReadJSON() return } func (c *Client) Play() error { format := `{"Name":"OPMonitor","SessionID":"0x%08X","OPMonitor":{"Action":"%s","Parameter":%s}}` + "\x0A\x00" data := fmt.Sprintf(format, c.session, "Claim", c.stream) if _, err := c.WriteCmd(OPMonitorClaim, []byte(data)); err != nil { return err } if _, err := c.ReadJSON(); err != nil { return err } data = fmt.Sprintf(format, c.session, "Start", c.stream) _, err := c.WriteCmd(OPMonitorStart, []byte(data)) return err } func (c *Client) Talk() error { format := `{"Name":"OPTalk","SessionID":"0x%08X","OPTalk":{"Action":"%s","AudioFormat":{"EncodeType":"G711_ALAW"}}}` + "\x0A\x00" data := fmt.Sprintf(format, c.session, "Claim") if _, err := c.WriteCmd(OPTalkClaim, []byte(data)); err != nil { return err } if _, err := c.ReadJSON(); err != nil { return err } data = fmt.Sprintf(format, c.session, "Start") _, err := c.WriteCmd(OPTalkStart, []byte(data)) return err } func (c *Client) WriteCmd(cmd uint16, payload []byte) (n int, err error) { b := make([]byte, 20, 128) b[0] = 255 binary.LittleEndian.PutUint32(b[4:], c.session) binary.LittleEndian.PutUint32(b[8:], c.seq) binary.LittleEndian.PutUint16(b[14:], cmd) binary.LittleEndian.PutUint32(b[16:], uint32(len(payload))) b = append(b, payload...) c.seq++ if err = c.conn.SetWriteDeadline(time.Now().Add(time.Second * 5)); err != nil { return 0, err } return c.conn.Write(b) } func (c *Client) ReadChunk() (b []byte, err error) { if err = c.conn.SetReadDeadline(time.Now().Add(time.Second * 5)); err != nil { return } b = make([]byte, 20) if _, err = io.ReadFull(c.rd, b); err != nil { return } if b[0] != 255 { return nil, errors.New("read error") } c.session = binary.LittleEndian.Uint32(b[4:]) size := binary.LittleEndian.Uint32(b[16:]) b = make([]byte, size) if _, err = io.ReadFull(c.rd, b); err != nil { return } return } func (c *Client) ReadPacket() (pType byte, payload []byte, err error) { var b []byte // many cameras may split packet to multiple chunks // some rare cameras may put multiple packets to single chunk for len(c.buf) < 16 { if b, err = c.ReadChunk(); err != nil { return 0, nil, err } c.buf = append(c.buf, b...) } if !bytes.HasPrefix(c.buf, []byte{0, 0, 1}) { return 0, nil, fmt.Errorf("dvrip: wrong packet: %0.16x", c.buf) } var size int switch pType = c.buf[3]; pType { case 0xFC, 0xFE: size = int(binary.LittleEndian.Uint32(c.buf[12:])) + 16 case 0xFD: // PFrame size = int(binary.LittleEndian.Uint32(c.buf[4:])) + 8 case 0xFA, 0xF9: size = int(binary.LittleEndian.Uint16(c.buf[6:])) + 8 default: return 0, nil, fmt.Errorf("dvrip: unknown packet type: %X", pType) } for len(c.buf) < size { if b, err = c.ReadChunk(); err != nil { return 0, nil, err } c.buf = append(c.buf, b...) } payload = c.buf[:size] c.buf = c.buf[size:] return } type Response map[string]any func (c *Client) ReadJSON() (res Response, err error) { b, err := c.ReadChunk() if err != nil { return } res = Response{} if err = json.Unmarshal(b[:len(b)-2], &res); err != nil { return } if v, ok := res["Ret"].(float64); !ok || (v != 100 && v != 515) { err = fmt.Errorf("wrong response: %s", b) } return } func SofiaHash(password string) string { const chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" sofia := make([]byte, 0, 8) hash := md5.Sum([]byte(password)) for i := 0; i < md5.Size; i += 2 { j := uint16(hash[i]) + uint16(hash[i+1]) sofia = append(sofia, chars[j%62]) } return string(sofia) } ================================================ FILE: pkg/dvrip/dvrip.go ================================================ package dvrip import "github.com/AlexxIT/go2rtc/pkg/core" func Dial(url string) (core.Producer, error) { client := &Client{} if err := client.Dial(url); err != nil { return nil, err } conn := core.Connection{ ID: core.NewID(), FormatName: "dvrip", Protocol: "tcp", RemoteAddr: client.conn.RemoteAddr().String(), Transport: client.conn, } if client.stream != "" { prod := &Producer{Connection: conn, client: client} if err := prod.probe(); err != nil { return nil, err } return prod, nil } else { conn.Medias = []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ // leave only one codec here for better compatibility with cameras // https://github.com/AlexxIT/go2rtc/issues/1111 {Name: core.CodecPCMA, ClockRate: 8000, PayloadType: 8}, }, }, } return &Backchannel{Connection: conn, client: client}, nil } } ================================================ FILE: pkg/dvrip/producer.go ================================================ package dvrip import ( "encoding/base64" "encoding/binary" "errors" "fmt" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/pion/rtp" ) type Producer struct { core.Connection client *Client video, audio *core.Receiver videoTS uint32 videoDT uint32 audioTS uint32 audioSeq uint16 } func (c *Producer) Start() error { for { pType, b, err := c.client.ReadPacket() if err != nil { return err } //log.Printf("[DVR] type: %d, len: %d", dataType, len(b)) switch pType { case 0xFC, 0xFE, 0xFD: if c.video == nil { continue } var payload []byte if pType != 0xFD { payload = b[16:] // iframe } else { payload = b[8:] // pframe } c.videoTS += c.videoDT packet := &rtp.Packet{ Header: rtp.Header{Timestamp: c.videoTS}, Payload: annexb.EncodeToAVCC(payload), } //log.Printf("[AVC] %v, len: %d, ts: %10d", h265.Types(payload), len(payload), packet.Timestamp) c.video.WriteRTP(packet) case 0xFA: // audio if c.audio == nil { continue } payload := b[8:] c.audioTS += uint32(len(payload)) c.audioSeq++ packet := &rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: true, SequenceNumber: c.audioSeq, Timestamp: c.audioTS, }, Payload: payload, } //log.Printf("[DVR] len: %d, ts: %10d", len(packet.Payload), packet.Timestamp) c.audio.WriteRTP(packet) case 0xF9: // unknown default: println(fmt.Sprintf("dvrip: unknown packet type: %d", pType)) } } } func (c *Producer) probe() error { if err := c.client.Play(); err != nil { return err } rd := core.NewReadBuffer(c.client.rd) rd.BufferSize = core.ProbeSize defer func() { c.client.buf = nil rd.Reset() }() c.client.rd = rd // some awful cameras has VERY rare keyframes // so we wait video+audio for default probe time // and wait anything for 15 seconds timeoutBoth := time.Now().Add(core.ProbeTimeout) timeoutAny := time.Now().Add(time.Second * 15) for { if now := time.Now(); now.Before(timeoutBoth) { if c.video != nil && c.audio != nil { return nil } } else if now.Before(timeoutAny) { if c.video != nil || c.audio != nil { return nil } } else { return errors.New("dvrip: can't probe medias") } tag, b, err := c.client.ReadPacket() if err != nil { return err } switch tag { case 0xFC, 0xFE: // video if c.video != nil { continue } fps := b[5] //width := uint16(b[6]) * 8 //height := uint16(b[7]) * 8 //println(width, height) ts := b[8:] // the exact value of the start TS does not matter c.videoTS = binary.LittleEndian.Uint32(ts) c.videoDT = 90000 / uint32(fps) payload := annexb.EncodeToAVCC(b[16:]) c.addVideoTrack(b[4], payload) case 0xFA: // audio if c.audio != nil { continue } // the exact value of the start TS does not matter c.audioTS = c.videoTS c.addAudioTrack(b[4], b[5]) } } } func (c *Producer) addVideoTrack(mediaCode byte, payload []byte) { var codec *core.Codec switch mediaCode { case 0x02, 0x12: codec = &core.Codec{ Name: core.CodecH264, ClockRate: 90000, PayloadType: core.PayloadTypeRAW, FmtpLine: h264.GetFmtpLine(payload), } case 0x03, 0x13, 0x43, 0x53: codec = &core.Codec{ Name: core.CodecH265, ClockRate: 90000, PayloadType: core.PayloadTypeRAW, FmtpLine: "profile-id=1", } for { size := 4 + int(binary.BigEndian.Uint32(payload)) switch h265.NALUType(payload) { case h265.NALUTypeVPS: codec.FmtpLine += ";sprop-vps=" + base64.StdEncoding.EncodeToString(payload[4:size]) case h265.NALUTypeSPS: codec.FmtpLine += ";sprop-sps=" + base64.StdEncoding.EncodeToString(payload[4:size]) case h265.NALUTypePPS: codec.FmtpLine += ";sprop-pps=" + base64.StdEncoding.EncodeToString(payload[4:size]) } if size < len(payload) { payload = payload[size:] } else { break } } default: println("[DVRIP] unsupported video codec:", mediaCode) return } media := &core.Media{ Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, } c.Medias = append(c.Medias, media) c.video = core.NewReceiver(media, codec) c.Receivers = append(c.Receivers, c.video) } var sampleRates = []uint32{4000, 8000, 11025, 16000, 20000, 22050, 32000, 44100, 48000} func (c *Producer) addAudioTrack(mediaCode byte, sampleRate byte) { // https://github.com/vigoss30611/buildroot-ltc/blob/master/system/qm/ipc/ProtocolService/src/ZhiNuo/inc/zn_dh_base_type.h // PCM8 = 7, G729, IMA_ADPCM, G711U, G721, PCM8_VWIS, MS_ADPCM, G711A, PCM16 var codec *core.Codec switch mediaCode { case 10: // G711U codec = &core.Codec{ Name: core.CodecPCMU, } case 14: // G711A codec = &core.Codec{ Name: core.CodecPCMA, } default: println("[DVRIP] unsupported audio codec:", mediaCode) return } if sampleRate <= byte(len(sampleRates)) { codec.ClockRate = sampleRates[sampleRate-1] } media := &core.Media{ Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, } c.Medias = append(c.Medias, media) c.audio = core.NewReceiver(media, codec) c.Receivers = append(c.Receivers, c.audio) } //func (c *Client) MarshalJSON() ([]byte, error) { // info := &core.Info{ // Type: "DVRIP active producer", // RemoteAddr: c.conn.RemoteAddr().String(), // Medias: c.Medias, // Receivers: c.Receivers, // Recv: c.Recv, // } // return json.Marshal(info) //} ================================================ FILE: pkg/eseecloud/eseecloud.go ================================================ package eseecloud import ( "bytes" "encoding/binary" "errors" "io" "net/http" "regexp" "strings" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/pion/rtp" ) type Producer struct { core.Connection rd *core.ReadBuffer videoPT, audioPT uint8 } func Dial(rawURL string) (core.Producer, error) { rawURL, _ = strings.CutPrefix(rawURL, "eseecloud") res, err := http.Get("http" + rawURL) if err != nil { return nil, err } prod, err := Open(res.Body) if err != nil { return nil, err } if info, ok := prod.(core.Info); ok { info.SetProtocol("http") info.SetURL(rawURL) } return prod, nil } func Open(r io.Reader) (core.Producer, error) { prod := &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "eseecloud", Transport: r, }, rd: core.NewReadBuffer(r), } if err := prod.probe(); err != nil { return nil, err } return prod, nil } func (p *Producer) probe() error { b, err := p.rd.Peek(1024) if err != nil { return err } i := bytes.Index(b, []byte("\r\n\r\n")) if i == -1 { return io.EOF } b = make([]byte, i+4) _, _ = p.rd.Read(b) re := regexp.MustCompile(`m=(video|audio) (\d+) (\w+)/(\d+)\S*`) for _, item := range re.FindAllStringSubmatch(string(b), 2) { p.SDP += item[0] + "\n" switch item[3] { case "H264", "H265": p.Medias = append(p.Medias, &core.Media{ Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ { Name: item[3], ClockRate: 90000, PayloadType: core.PayloadTypeRAW, }, }, }) p.videoPT = byte(core.Atoi(item[2])) case "G711": p.Medias = append(p.Medias, &core.Media{ Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ { Name: core.CodecPCMA, ClockRate: 8000, }, }, }) p.audioPT = byte(core.Atoi(item[2])) } } return nil } func (p *Producer) Start() error { receivers := make(map[uint8]*core.Receiver) for _, receiver := range p.Receivers { switch receiver.Codec.Kind() { case core.KindVideo: receivers[p.videoPT] = receiver case core.KindAudio: receivers[p.audioPT] = receiver } } for { pkt, err := p.readPacket() if err != nil { return err } if recv := receivers[pkt.PayloadType]; recv != nil { switch recv.Codec.Name { case core.CodecH264, core.CodecH265: // timestamp = seconds x 1000000 pkt = &rtp.Packet{ Header: rtp.Header{ Timestamp: uint32(uint64(pkt.Timestamp) * 90000 / 1000000), }, Payload: annexb.EncodeToAVCC(pkt.Payload), } case core.CodecPCMA: pkt = &rtp.Packet{ Header: rtp.Header{ Version: 2, SequenceNumber: pkt.SequenceNumber, Timestamp: uint32(uint64(pkt.Timestamp) * 8000 / 1000000), }, Payload: pkt.Payload, } } recv.WriteRTP(pkt) } } } func (p *Producer) readPacket() (*core.Packet, error) { b := make([]byte, 8) if _, err := io.ReadFull(p.rd, b); err != nil { return nil, err } if b[0] != '$' { return nil, errors.New("eseecloud: wrong start byte") } size := binary.BigEndian.Uint32(b[4:]) b = make([]byte, size) if _, err := io.ReadFull(p.rd, b); err != nil { return nil, err } pkt := &core.Packet{} if err := pkt.Unmarshal(b); err != nil { return nil, err } p.Recv += int(size) return pkt, nil } ================================================ FILE: pkg/expr/expr.go ================================================ package expr import ( "bytes" "encoding/json" "fmt" "io" "net/http" "net/http/cookiejar" "net/url" "regexp" "strings" "time" "github.com/expr-lang/expr" "github.com/expr-lang/expr/vm" ) func newRequest(rawURL string, options map[string]any) (*http.Request, error) { var method, contentType string var rd io.Reader // method from js fetch if s, ok := options["method"].(string); ok { method = s } else { method = "GET" } // params key from python requests if kv, ok := options["params"].(map[string]any); ok { rawURL += "?" + url.Values(kvToString(kv)).Encode() } // json key from python requests // data key from python requests // body key from js fetch if v, ok := options["json"]; ok { b, err := json.Marshal(v) if err != nil { return nil, err } contentType = "application/json" rd = bytes.NewReader(b) } else if kv, ok := options["data"].(map[string]any); ok { contentType = "application/x-www-form-urlencoded" rd = strings.NewReader(url.Values(kvToString(kv)).Encode()) } else if s, ok := options["body"].(string); ok { rd = strings.NewReader(s) } req, err := http.NewRequest(method, rawURL, rd) if err != nil { return nil, err } if kv, ok := options["headers"].(map[string]any); ok { req.Header = kvToString(kv) } if contentType != "" && req.Header.Get("Content-Type") == "" { req.Header.Set("Content-Type", contentType) } return req, nil } func kvToString(kv map[string]any) map[string][]string { dst := make(map[string][]string, len(kv)) for k, v := range kv { dst[k] = []string{fmt.Sprintf("%v", v)} } return dst } func regExp(params ...any) (*regexp.Regexp, error) { exp := params[0].(string) if len(params) >= 2 { // support: // i case-insensitive (default false) // m multi-line mode: ^ and $ match begin/end line (default false) // s let . match \n (default false) // https://pkg.go.dev/regexp/syntax flags := params[1].(string) exp = "(?" + flags + ")" + exp } return regexp.Compile(exp) } func Compile(input string) (*vm.Program, error) { // support http sessions jar, _ := cookiejar.New(nil) client := http.Client{ Jar: jar, Timeout: 5 * time.Second, } return expr.Compile( input, expr.Function( "fetch", func(params ...any) (any, error) { var req *http.Request var err error rawURL := params[0].(string) if len(params) == 2 { options := params[1].(map[string]any) req, err = newRequest(rawURL, options) } else { req, err = http.NewRequest("GET", rawURL, nil) } if err != nil { return nil, err } res, err := client.Do(req) if err != nil { return nil, err } b, _ := io.ReadAll(res.Body) return map[string]any{ "ok": res.StatusCode < 400, "status": res.Status, "text": string(b), "json": func() (v any) { _ = json.Unmarshal(b, &v) return }, }, nil }, //new(func(url string) map[string]any), //new(func(url string, options map[string]any) map[string]any), ), expr.Function( "match", func(params ...any) (any, error) { re, err := regExp(params[1:]...) if err != nil { return nil, err } str := params[0].(string) return re.FindStringSubmatch(str), nil }, //new(func(str, expr string) []string), //new(func(str, expr, flags string) []string), ), ) } func Eval(input string, env any) (any, error) { program, err := Compile(input) if err != nil { return nil, err } return expr.Run(program, env) } func Run(program *vm.Program, env any) (any, error) { return vm.Run(program, env) } ================================================ FILE: pkg/expr/expr_test.go ================================================ package expr import ( "testing" "github.com/stretchr/testify/require" ) func TestMatchHost(t *testing.T) { v, err := Eval(` let url = "rtsp://user:pass@192.168.1.123/cam/realmonitor?..."; let host = match(url, "//[^/]+")[0][2:]; host `, nil) require.Nil(t, err) require.Equal(t, "user:pass@192.168.1.123", v) } ================================================ FILE: pkg/ffmpeg/README.md ================================================ ## FFplay output [FFplay](https://stackoverflow.com/questions/27778678/what-are-mv-fd-aq-vq-sq-and-f-in-a-video-stream) `7.11 A-V: 0.003 fd= 1 aq= 21KB vq= 321KB sq= 0B f=0/0`: - `7.11` - master clock, is the time from start of the stream/video - `A-V` - av_diff, difference between audio and video timestamps - `fd` - frames dropped - `aq` - audio queue (0 - no delay) - `vq` - video queue (0 - no delay) - `sq` - subtitle queue - `f` - timestamp error correction rate (Not 100% sure) `M-V`, `M-A` means video stream only, audio stream only respectively. ## Devices Windows ``` >ffmpeg -hide_banner -f dshow -list_options true -i video="VMware Virtual USB Video Device" [dshow @ 0000025695e52900] DirectShow video device options (from video devices) [dshow @ 0000025695e52900] Pin "Record" (alternative pin name "0") [dshow @ 0000025695e52900] pixel_format=yuyv422 min s=1280x720 fps=1 max s=1280x720 fps=10 [dshow @ 0000025695e52900] pixel_format=yuyv422 min s=1280x720 fps=1 max s=1280x720 fps=10 (tv, bt470bg/bt709/unknown, topleft) [dshow @ 0000025695e52900] pixel_format=nv12 min s=1280x720 fps=1 max s=1280x720 fps=23 [dshow @ 0000025695e52900] pixel_format=nv12 min s=1280x720 fps=1 max s=1280x720 fps=23 (tv, bt470bg/bt709/unknown, topleft) ``` ## Devices Mac ``` % ./ffmpeg -hide_banner -f avfoundation -list_devices true -i "" [AVFoundation indev @ 0x7f8b1f504d80] AVFoundation video devices: [AVFoundation indev @ 0x7f8b1f504d80] [0] FaceTime HD Camera [AVFoundation indev @ 0x7f8b1f504d80] [1] Capture screen 0 [AVFoundation indev @ 0x7f8b1f504d80] AVFoundation audio devices: [AVFoundation indev @ 0x7f8b1f504d80] [0] Soundflower (2ch) [AVFoundation indev @ 0x7f8b1f504d80] [1] Built-in Microphone [AVFoundation indev @ 0x7f8b1f504d80] [2] Soundflower (64ch) ``` ## Devices Linux ``` # ffmpeg -hide_banner -f v4l2 -list_formats all -i /dev/video0 [video4linux2,v4l2 @ 0x7f7de7c58bc0] Raw : yuyv422 : YUYV 4:2:2 : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360 752x416 800x448 800x600 864x480 960x544 960x720 1024x576 1184x656 1280x720 1280x960 [video4linux2,v4l2 @ 0x7f7de7c58bc0] Compressed: mjpeg : Motion-JPEG : 640x480 160x120 176x144 320x176 320x240 352x288 432x240 544x288 640x360 752x416 800x448 800x600 864x480 960x544 960x720 1024x576 1184x656 1280x720 1280x960 ``` ## TTS ```yaml streams: tts: ffmpeg:#input=-readrate 1 -readrate_initial_burst 0.001 -f lavfi -i "flite=text='1 2 3 4 5 6 7 8 9 0'"#audio=pcma ``` ## Useful links - https://superuser.com/questions/564402/explanation-of-x264-tune - https://stackoverflow.com/questions/33624016/why-sliced-thread-affect-so-much-on-realtime-encoding-using-ffmpeg-x264 - https://codec.fandom.com/ru/wiki/X264_-_описание_ключей_кодирования - https://html5test.com/ - https://trac.ffmpeg.org/wiki/Capture/Webcam - https://trac.ffmpeg.org/wiki/DirectShow - https://stackoverflow.com/questions/53207692/libav-mjpeg-encoding-and-huffman-table - https://github.com/tuupola/esp_video/blob/master/README.md - https://github.com/leandromoreira/ffmpeg-libav-tutorial - https://www.reddit.com/user/VeritablePornocopium/comments/okw130/ffmpeg_with_libfdk_aac_for_windows_x64/ - https://slhck.info/video/2017/02/24/vbr-settings.html - [HomeKit audio samples problem](https://superuser.com/questions/1290996/non-monotonous-dts-with-igndts-flag) ================================================ FILE: pkg/ffmpeg/ffmpeg.go ================================================ package ffmpeg import ( "bytes" "strconv" "strings" ) // correlation of libavformat versions with ffmpeg versions const ( Version50 = "59. 16" Version51 = "59. 27" Version60 = "60. 3" Version61 = "60. 16" Version70 = "61. 1" ) type Args struct { Bin string // ffmpeg Global string // -hide_banner -v error Input string // -re -stream_loop -1 -i /media/bunny.mp4 Codecs []string // -c:v libx264 -g:v 30 -preset:v ultrafast -tune:v zerolatency Filters []string // scale=1920:1080 Output string // -f rtsp {output} Version string // libavformat version, it's more reliable than the ffmpeg version Video, Audio int // count of Video and Audio params } func (a *Args) AddCodec(codec string) { a.Codecs = append(a.Codecs, codec) } func (a *Args) AddFilter(filter string) { a.Filters = append(a.Filters, filter) } func (a *Args) InsertFilter(filter string) { a.Filters = append([]string{filter}, a.Filters...) } func (a *Args) HasFilters(filters ...string) bool { for _, f1 := range a.Filters { for _, f2 := range filters { if strings.HasPrefix(f1, f2) { return true } } } return false } func (a *Args) String() string { b := bytes.NewBuffer(make([]byte, 0, 512)) b.WriteString(a.Bin) if a.Global != "" { b.WriteByte(' ') b.WriteString(a.Global) } b.WriteByte(' ') // starting from FFmpeg 6.1 readrate=1 has default initial bust 0.5 sec // it might make us miss the first couple seconds of the file if strings.HasPrefix(a.Input, "-re ") && a.Version >= Version61 { b.WriteString("-readrate_initial_burst 0.001 ") } b.WriteString(a.Input) multimode := a.Video > 1 || a.Audio > 1 var iv, ia int for _, codec := range a.Codecs { // support multiple video and/or audio codecs if multimode && len(codec) >= 5 { switch codec[:5] { case "-c:v ": codec = "-map 0:v:0? " + strings.ReplaceAll(codec, ":v ", ":v:"+strconv.Itoa(iv)+" ") iv++ case "-c:a ": codec = "-map 0:a:0? " + strings.ReplaceAll(codec, ":a ", ":a:"+strconv.Itoa(ia)+" ") ia++ } } b.WriteByte(' ') b.WriteString(codec) } if len(a.Filters) > 0 { for i, filter := range a.Filters { if i == 0 { b.WriteString(` -vf "`) } else { b.WriteByte(',') } b.WriteString(filter) } b.WriteByte('"') } b.WriteByte(' ') b.WriteString(a.Output) return b.String() } func ParseVersion(b []byte) (ffmpeg string, libavformat string) { if len(b) > 100 { // ffmpeg version n7.0-30-g8b0fe91754-20240520 Copyright (c) 2000-2024 the FFmpeg developers if i := bytes.IndexByte(b[15:], ' '); i > 0 { ffmpeg = string(b[15 : 15+i]) } // libavformat 60. 16.100 / 60. 16.100 if i := strings.Index(string(b), "libavformat"); i > 0 { libavformat = string(b[i+15 : i+25]) } } return } ================================================ FILE: pkg/flussonic/flussonic.go ================================================ package flussonic import ( "strings" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/iso" "github.com/gorilla/websocket" "github.com/pion/rtp" ) type Producer struct { core.Connection conn *websocket.Conn videoTrackID, audioTrackID uint32 videoTimeScale, audioTimeScale float32 } func Dial(source string) (core.Producer, error) { url, _ := strings.CutPrefix(source, "flussonic:") conn, _, err := websocket.DefaultDialer.Dial(url, nil) if err != nil { return nil, err } prod := &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "flussonic", Protocol: core.Before(url, ":"), // wss RemoteAddr: conn.RemoteAddr().String(), URL: url, Transport: conn, }, conn: conn, } if err = prod.probe(); err != nil { _ = conn.Close() return nil, err } return prod, nil } func (p *Producer) probe() error { var init struct { //Metadata struct { // Tracks []struct { // Width int `json:"width,omitempty"` // Height int `json:"height,omitempty"` // Fps int `json:"fps,omitempty"` // Content string `json:"content"` // TrackId string `json:"trackId"` // Bitrate int `json:"bitrate"` // } `json:"tracks"` //} `json:"metadata"` Tracks []struct { Content string `json:"content"` Id uint32 `json:"id"` Payload []byte `json:"payload"` } `json:"tracks"` //Type string `json:"type"` } if err := p.conn.ReadJSON(&init); err != nil { return err } var timeScale uint32 for _, track := range init.Tracks { atoms, _ := iso.DecodeAtoms(track.Payload) for _, atom := range atoms { switch atom := atom.(type) { case *iso.AtomMdhd: timeScale = atom.TimeScale case *iso.AtomVideo: switch atom.Name { case "avc1": codec := h264.AVCCToCodec(atom.Config) p.Medias = append(p.Medias, &core.Media{ Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, }) p.videoTrackID = track.Id p.videoTimeScale = float32(codec.ClockRate) / float32(timeScale) } case *iso.AtomAudio: switch atom.Name { case "mp4a": codec := aac.ConfigToCodec(atom.Config) p.Medias = append(p.Medias, &core.Media{ Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, }) p.audioTrackID = track.Id p.audioTimeScale = float32(codec.ClockRate) / float32(timeScale) } } } } return nil } func (p *Producer) Start() error { if err := p.conn.WriteMessage(websocket.TextMessage, []byte("resume")); err != nil { return err } receivers := make(map[uint32]*core.Receiver) timeScales := make(map[uint32]float32) for _, receiver := range p.Receivers { switch receiver.Codec.Kind() { case core.KindVideo: receivers[p.videoTrackID] = receiver timeScales[p.videoTrackID] = p.videoTimeScale case core.KindAudio: receivers[p.audioTrackID] = receiver timeScales[p.audioTrackID] = p.audioTimeScale } } ch := make(chan []byte, 10) defer close(ch) go func() { for b := range ch { atoms, err := iso.DecodeAtoms(b) if err != nil { continue } var trackID uint32 var decodeTime uint64 for _, atom := range atoms { switch atom := atom.(type) { case *iso.AtomTfhd: trackID = atom.TrackID case *iso.AtomTfdt: decodeTime = atom.DecodeTime case *iso.AtomMdat: b = atom.Data } } if recv := receivers[trackID]; recv != nil { timestamp := uint32(float32(decodeTime) * timeScales[trackID]) packet := &rtp.Packet{ Header: rtp.Header{Timestamp: timestamp}, Payload: b, } recv.WriteRTP(packet) } } }() for { mType, b, err := p.conn.ReadMessage() if err != nil { return err } if mType == websocket.BinaryMessage { p.Recv += len(b) ch <- b } } } ================================================ FILE: pkg/flv/amf/amf.go ================================================ package amf import ( "encoding/binary" "errors" "math" ) const ( TypeNumber byte = iota TypeBoolean TypeString TypeObject TypeNull = 5 TypeEcmaArray = 8 TypeObjectEnd = 9 ) // AMF spec: http://download.macromedia.com/pub/labs/amf/amf0_spec_121207.pdf type AMF struct { buf []byte pos int } var ErrRead = errors.New("amf: read error") func NewReader(b []byte) *AMF { return &AMF{buf: b} } func (a *AMF) ReadItems() ([]any, error) { var items []any for a.pos < len(a.buf) { v, err := a.ReadItem() if err != nil { return nil, err } items = append(items, v) } return items, nil } func (a *AMF) ReadItem() (any, error) { dataType, err := a.ReadByte() if err != nil { return nil, err } switch dataType { case TypeNumber: return a.ReadNumber() case TypeBoolean: b, err := a.ReadByte() return b != 0, err case TypeString: return a.ReadString() case TypeObject: return a.ReadObject() case TypeEcmaArray: return a.ReadEcmaArray() case TypeNull: return nil, nil case TypeObjectEnd: return nil, nil } return nil, ErrRead } func (a *AMF) ReadByte() (byte, error) { if a.pos >= len(a.buf) { return 0, ErrRead } v := a.buf[a.pos] a.pos++ return v, nil } func (a *AMF) ReadNumber() (float64, error) { if a.pos+8 > len(a.buf) { return 0, ErrRead } v := binary.BigEndian.Uint64(a.buf[a.pos : a.pos+8]) a.pos += 8 return math.Float64frombits(v), nil } func (a *AMF) ReadString() (string, error) { if a.pos+2 > len(a.buf) { return "", ErrRead } size := int(binary.BigEndian.Uint16(a.buf[a.pos:])) a.pos += 2 if a.pos+size > len(a.buf) { return "", ErrRead } s := string(a.buf[a.pos : a.pos+size]) a.pos += size return s, nil } func (a *AMF) ReadObject() (map[string]any, error) { obj := make(map[string]any) for { k, err := a.ReadString() if err != nil { return nil, err } v, err := a.ReadItem() if err != nil { return nil, err } if k == "" { break } obj[k] = v } return obj, nil } func (a *AMF) ReadEcmaArray() (map[string]any, error) { if a.pos+4 > len(a.buf) { return nil, ErrRead } a.pos += 4 // skip size return a.ReadObject() } func NewWriter() *AMF { return &AMF{} } func (a *AMF) Bytes() []byte { return a.buf } func (a *AMF) WriteNumber(n float64) { b := math.Float64bits(n) a.buf = append( a.buf, TypeNumber, byte(b>>56), byte(b>>48), byte(b>>40), byte(b>>32), byte(b>>24), byte(b>>16), byte(b>>8), byte(b), ) } func (a *AMF) WriteBool(b bool) { if b { a.buf = append(a.buf, TypeBoolean, 1) } else { a.buf = append(a.buf, TypeBoolean, 0) } } func (a *AMF) WriteString(s string) { n := len(s) a.buf = append(a.buf, TypeString, byte(n>>8), byte(n)) a.buf = append(a.buf, s...) } func (a *AMF) WriteObject(obj map[string]any) { a.buf = append(a.buf, TypeObject) a.writeKV(obj) a.buf = append(a.buf, 0, 0, TypeObjectEnd) } func (a *AMF) WriteEcmaArray(obj map[string]any) { n := len(obj) a.buf = append(a.buf, TypeEcmaArray, byte(n>>24), byte(n>>16), byte(n>>8), byte(n)) a.writeKV(obj) a.buf = append(a.buf, 0, 0, TypeObjectEnd) } func (a *AMF) writeKV(obj map[string]any) { for k, v := range obj { n := len(k) a.buf = append(a.buf, byte(n>>8), byte(n)) a.buf = append(a.buf, k...) switch v := v.(type) { case string: a.WriteString(v) case int: a.WriteNumber(float64(v)) case uint16: a.WriteNumber(float64(v)) case uint32: a.WriteNumber(float64(v)) case float64: a.WriteNumber(v) case bool: a.WriteBool(v) default: panic(v) } } } func (a *AMF) WriteNull() { a.buf = append(a.buf, TypeNull) } func EncodeItems(items ...any) []byte { a := &AMF{} for _, item := range items { switch v := item.(type) { case float64: a.WriteNumber(v) case int: a.WriteNumber(float64(v)) case string: a.WriteString(v) case map[string]any: a.WriteObject(v) case nil: a.WriteNull() default: panic(v) } } return a.Bytes() } ================================================ FILE: pkg/flv/amf/amf_test.go ================================================ package amf import ( "encoding/hex" "testing" "github.com/stretchr/testify/require" ) func TestNewReader(t *testing.T) { tests := []struct { name string actual string expect []any }{ { name: "ffmpeg-http", actual: "02000a6f6e4d65746144617461080000001000086475726174696f6e000000000000000000000577696474680040940000000000000006686569676874004086800000000000000d766964656f646174617261746500409e62770000000000096672616d6572617465004038000000000000000c766964656f636f646563696400401c000000000000000d617564696f646174617261746500405ea93000000000000f617564696f73616d706c65726174650040e5888000000000000f617564696f73616d706c6573697a65004030000000000000000673746572656f0101000c617564696f636f6465636964004024000000000000000b6d616a6f725f6272616e640200046d703432000d6d696e6f725f76657273696f6e020001300011636f6d70617469626c655f6272616e647302000c69736f6d617663316d7034320007656e636f64657202000c4c61766636302e352e313030000866696c6573697a65000000000000000000000009", expect: []any{ "onMetaData", map[string]any{ "compatible_brands": "isomavc1mp42", "major_brand": "mp42", "minor_version": "0", "encoder": "Lavf60.5.100", "filesize": float64(0), "duration": float64(0), "videocodecid": float64(7), "width": float64(1280), "height": float64(720), "framerate": float64(24), "videodatarate": 1944.6162109375, "audiocodecid": float64(10), "audiosamplerate": float64(44100), "stereo": true, "audiosamplesize": float64(16), "audiodatarate": 122.6435546875, }, }, }, { name: "ffmpeg-file", actual: "02000a6f6e4d65746144617461080000000800086475726174696f6e004000000000000000000577696474680040940000000000000006686569676874004086800000000000000d766964656f646174617261746500000000000000000000096672616d6572617465004039000000000000000c766964656f636f646563696400401c0000000000000007656e636f64657202000c4c61766636302e352e313030000866696c6573697a6500411f541400000000000009", expect: []any{ "onMetaData", map[string]any{ "encoder": "Lavf60.5.100", "filesize": float64(513285), "duration": float64(2), "videocodecid": float64(7), "width": float64(1280), "height": float64(720), "framerate": float64(25), "videodatarate": float64(0), }, }, }, { name: "reolink-1", actual: "0200075f726573756c74003ff0000000000000030006666d7356657202000d464d532f332c302c312c313233000c6361706162696c697469657300403f0000000000000000090300056c6576656c0200067374617475730004636f646502001d4e6574436f6e6e656374696f6e2e436f6e6e6563742e53756363657373000b6465736372697074696f6e020015436f6e6e656374696f6e207375636365656465642e000e6f626a656374456e636f64696e67000000000000000000000009", expect: []any{ "_result", float64(1), map[string]any{ "capabilities": float64(31), "fmsVer": "FMS/3,0,1,123", }, map[string]any{ "code": "NetConnection.Connect.Success", "description": "Connection succeeded.", "level": "status", "objectEncoding": float64(0), }, }, }, { name: "reolink-2", actual: "0200075f726573756c7400400000000000000005003ff0000000000000", expect: []any{ "_result", float64(2), nil, float64(1), }, }, { name: "reolink-3", actual: "0200086f6e537461747573000000000000000000050300056c6576656c0200067374617475730004636f64650200144e657453747265616d2e506c61792e5374617274000b6465736372697074696f6e020015537461727420766964656f206f6e2064656d616e64000009", expect: []any{ "onStatus", float64(0), nil, map[string]any{ "code": "NetStream.Play.Start", "description": "Start video on demand", "level": "status", }, }, }, { name: "reolink-4", actual: "0200117c52746d7053616d706c6541636365737301010101", expect: []any{ "|RtmpSampleAccess", true, true, }, }, { name: "reolink-5", actual: "02000a6f6e4d6574614461746103000577696474680040a4000000000000000668656967687400409e000000000000000c646973706c617957696474680040a4000000000000000d646973706c617948656967687400409e00000000000000086475726174696f6e000000000000000000000c766964656f636f646563696400401c000000000000000c617564696f636f6465636964004024000000000000000f617564696f73616d706c65726174650040cf40000000000000096672616d657261746500403e000000000000000009", expect: []any{ "onMetaData", map[string]any{ "duration": float64(0), "videocodecid": float64(7), "width": float64(2560), "height": float64(1920), "displayWidth": float64(2560), "displayHeight": float64(1920), "framerate": float64(30), "audiocodecid": float64(10), "audiosamplerate": float64(16000), }, }, }, { name: "mediamtx", actual: "02000d40736574446174614672616d6502000a6f6e4d6574614461746103000d766964656f6461746172617465000000000000000000000c766964656f636f646563696400401c000000000000000d617564696f6461746172617465000000000000000000000c617564696f636f6465636964004024000000000000000009", expect: []any{ "@setDataFrame", "onMetaData", map[string]any{ "videocodecid": float64(7), "videodatarate": float64(0), "audiocodecid": float64(10), "audiodatarate": float64(0), }, }, }, { name: "mediamtx", actual: "0200075f726573756c74003ff0000000000000030006666d7356657202000d4c4e5820392c302c3132342c32000c6361706162696c697469657300403f0000000000000000090300056c6576656c0200067374617475730004636f646502001d4e6574436f6e6e656374696f6e2e436f6e6e6563742e53756363657373000b6465736372697074696f6e020015436f6e6e656374696f6e207375636365656465642e000e6f626a656374456e636f64696e67000000000000000000000009", expect: []any{ "_result", float64(1), map[string]any{ "capabilities": float64(31), "fmsVer": "LNX 9,0,124,2", }, map[string]any{ "code": "NetConnection.Connect.Success", "description": "Connection succeeded.", "level": "status", "objectEncoding": float64(0), }, }, }, { name: "mediamtx", actual: "0200075f726573756c7400401000000000000005003ff0000000000000", expect: []any{"_result", float64(4), any(nil), float64(1)}, }, { name: "mediamtx", actual: "0200086f6e537461747573004014000000000000050300056c6576656c0200067374617475730004636f64650200144e657453747265616d2e506c61792e5265736574000b6465736372697074696f6e02000a706c6179207265736574000009", expect: []any{ "onStatus", float64(5), any(nil), map[string]any{ "code": "NetStream.Play.Reset", "description": "play reset", "level": "status", }, }, }, { name: "mediamtx", actual: "0200086f6e537461747573004014000000000000050300056c6576656c0200067374617475730004636f64650200144e657453747265616d2e506c61792e5374617274000b6465736372697074696f6e02000a706c6179207374617274000009", expect: []any{ "onStatus", float64(5), any(nil), map[string]any{ "code": "NetStream.Play.Start", "description": "play start", "level": "status", }, }, }, { name: "mediamtx", actual: "0200086f6e537461747573004014000000000000050300056c6576656c0200067374617475730004636f64650200144e657453747265616d2e446174612e5374617274000b6465736372697074696f6e02000a64617461207374617274000009", expect: []any{ "onStatus", float64(5), any(nil), map[string]any{ "code": "NetStream.Data.Start", "description": "data start", "level": "status", }, }, }, { name: "mediamtx", actual: "0200086f6e537461747573004014000000000000050300056c6576656c0200067374617475730004636f646502001c4e657453747265616d2e506c61792e5075626c6973684e6f74696679000b6465736372697074696f6e02000e7075626c697368206e6f74696679000009", expect: []any{ "onStatus", float64(5), any(nil), map[string]any{ "code": "NetStream.Play.PublishNotify", "description": "publish notify", "level": "status", }, }, }, { name: "obs-connect", actual: "020007636f6e6e656374003ff000000000000003000361707002000c617070312f73747265616d3100047479706502000a6e6f6e70726976617465000e737570706f727473476f4177617901010008666c61736856657202001f464d4c452f332e302028636f6d70617469626c653b20464d53632f312e3029000673776655726c02002272746d703a2f2f3139322e3136382e31302e3130312f617070312f73747265616d310005746355726c02002272746d703a2f2f3139322e3136382e31302e3130312f617070312f73747265616d31000009", expect: []any{ "connect", float64(1), map[string]any{ "app": "app1/stream1", "flashVer": "FMLE/3.0 (compatible; FMSc/1.0)", "supportsGoAway": true, "swfUrl": "rtmp://192.168.10.101/app1/stream1", "tcUrl": "rtmp://192.168.10.101/app1/stream1", "type": "nonprivate", }, }, }, { name: "obs-key", actual: "02000d72656c6561736553747265616d004000000000000000050200046b657931", expect: []any{ "releaseStream", float64(2), nil, "key1", }, }, { name: "obs", actual: "02000d40736574446174614672616d6502000a6f6e4d65746144617461080000001400086475726174696f6e000000000000000000000866696c6553697a65000000000000000000000577696474680040840000000000000006686569676874004076800000000000000c766964656f636f646563696400401c000000000000000d766964656f64617461726174650040a388000000000000096672616d6572617465004039000000000000000c617564696f636f6465636964004024000000000000000d617564696f6461746172617465004064000000000000000f617564696f73616d706c65726174650040e5888000000000000f617564696f73616d706c6573697a65004030000000000000000d617564696f6368616e6e656c73004000000000000000000673746572656f01010003322e3101000003332e3101000003342e3001000003342e3101000003352e3101000003372e3101000007656e636f6465720200376f62732d6f7574707574206d6f64756c6520286c69626f62732076657273696f6e2032392e302e302d36322d6739303031323131663829000009", expect: []any{ "@setDataFrame", "onMetaData", map[string]any{ "2.1": false, "3.1": false, "4.0": false, "4.1": false, "5.1": false, "7.1": false, "audiochannels": float64(2), "audiocodecid": float64(10), "audiodatarate": float64(160), "audiosamplerate": float64(44100), "audiosamplesize": float64(16), "duration": float64(0), "encoder": "obs-output module (libobs version 29.0.0-62-g9001211f8)", "fileSize": float64(0), "framerate": float64(25), "height": float64(360), "stereo": true, "videocodecid": float64(7), "videodatarate": float64(2500), "width": float64(640), }, }, }, { name: "telegram-2", actual: "0200075f726573756c7400400000000000000005", expect: []any{ "_result", float64(2), nil, }, }, { name: "telegram-4", actual: "0200075f726573756c7400401000000000000005003ff0000000000000", expect: []any{ "_result", float64(4), nil, float64(1), }, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { b, err := hex.DecodeString(test.actual) require.Nil(t, err) rd := NewReader(b) v, err := rd.ReadItems() require.Nil(t, err) require.Equal(t, test.expect, v) }) } } ================================================ FILE: pkg/flv/consumer.go ================================================ package flv import ( "io" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/pion/rtp" ) type Consumer struct { core.Connection wr *core.WriteBuffer muxer *Muxer } func NewConsumer() *Consumer { medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecH264}, }, }, { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecAAC}, }, }, } wr := core.NewWriteBuffer(nil) return &Consumer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "flv", Medias: medias, Transport: wr, }, wr: wr, muxer: &Muxer{}, } } func (c *Consumer) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { sender := core.NewSender(media, track.Codec) switch track.Codec.Name { case core.CodecH264: payload := c.muxer.GetPayloader(track.Codec) sender.Handler = func(pkt *rtp.Packet) { b := payload(pkt) if n, err := c.wr.Write(b); err == nil { c.Send += n } } if track.Codec.IsRTP() { sender.Handler = h264.RTPDepay(track.Codec, sender.Handler) } else { sender.Handler = h264.RepairAVCC(track.Codec, sender.Handler) } case core.CodecAAC: payload := c.muxer.GetPayloader(track.Codec) sender.Handler = func(pkt *rtp.Packet) { b := payload(pkt) if n, err := c.wr.Write(b); err == nil { c.Send += n } } if track.Codec.IsRTP() { sender.Handler = aac.RTPDepay(sender.Handler) } } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } func (c *Consumer) WriteTo(wr io.Writer) (int64, error) { b := c.muxer.GetInit() if _, err := wr.Write(b); err != nil { return 0, err } return c.wr.WriteTo(wr) } ================================================ FILE: pkg/flv/flv_test.go ================================================ package flv import ( "testing" "github.com/stretchr/testify/require" ) func TestTimeToRTP(t *testing.T) { // Reolink camera has 20 FPS // Video timestamp increases by 50ms, SampleRate 90000, RTP timestamp increases by 4500 // Audio timestamp increases by 64ms, SampleRate 16000, RTP timestamp increases by 1024 frameN := 1 for i := 0; i < 32; i++ { // 1000ms/(90000/4500) = 50ms require.Equal(t, uint32(frameN*4500), TimeToRTP(uint32(frameN*50), 90000)) // 1000ms/(16000/1024) = 64ms require.Equal(t, uint32(frameN*1024), TimeToRTP(uint32(frameN*64), 16000)) frameN *= 2 } } ================================================ FILE: pkg/flv/muxer.go ================================================ package flv import ( "encoding/binary" "encoding/hex" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/flv/amf" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/pion/rtp" ) type Muxer struct { codecs []*core.Codec } const ( FlagsVideo = 0b001 FlagsAudio = 0b100 ) func (m *Muxer) GetInit() []byte { b := []byte{ 'F', 'L', 'V', // signature 1, // version 0, // flags (has video/audio) 0, 0, 0, 9, // header size 0, 0, 0, 0, // tag 0 size } obj := map[string]any{} for _, codec := range m.codecs { switch codec.Name { case core.CodecH264: b[4] |= FlagsVideo obj["videocodecid"] = CodecH264 case core.CodecAAC: b[4] |= FlagsAudio obj["audiocodecid"] = CodecAAC obj["audiosamplerate"] = codec.ClockRate obj["audiosamplesize"] = 16 obj["stereo"] = codec.Channels == 2 } } data := amf.EncodeItems("@setDataFrame", "onMetaData", obj) b = append(b, EncodeTag(TagData, 0, data)...) for _, codec := range m.codecs { switch codec.Name { case core.CodecH264: sps, pps := h264.GetParameterSet(codec.FmtpLine) if len(sps) == 0 { sps = []byte{0x67, 0x42, 0x00, 0x0a, 0xf8, 0x41, 0xa2} } else { h264.FixPixFmt(sps) } if len(pps) == 0 { pps = []byte{0x68, 0xce, 0x38, 0x80} } config := h264.EncodeConfig(sps, pps) video := append(encodeAVData(codec, 0), config...) b = append(b, EncodeTag(TagVideo, 0, video)...) case core.CodecAAC: s := core.Between(codec.FmtpLine, "config=", ";") config, _ := hex.DecodeString(s) audio := append(encodeAVData(codec, 0), config...) b = append(b, EncodeTag(TagAudio, 0, audio)...) } } return b } func (m *Muxer) GetPayloader(codec *core.Codec) func(packet *rtp.Packet) []byte { m.codecs = append(m.codecs, codec) var ts0 uint32 var k = codec.ClockRate / 1000 switch codec.Name { case core.CodecH264: buf := encodeAVData(codec, 1) return func(packet *rtp.Packet) []byte { if h264.IsKeyframe(packet.Payload) { buf[0] = 1<<4 | 7 } else { buf[0] = 2<<4 | 7 } buf = append(buf[:5], packet.Payload...) // reset buffer to previous place if ts0 == 0 { ts0 = packet.Timestamp } timeMS := (packet.Timestamp - ts0) / k return EncodeTag(TagVideo, timeMS, buf) } case core.CodecAAC: buf := encodeAVData(codec, 1) return func(packet *rtp.Packet) []byte { buf = append(buf[:2], packet.Payload...) if ts0 == 0 { ts0 = packet.Timestamp } timeMS := (packet.Timestamp - ts0) / k return EncodeTag(TagAudio, timeMS, buf) } } return nil } func EncodeTag(tagType byte, timeMS uint32, payload []byte) []byte { payloadSize := uint32(len(payload)) tagSize := payloadSize + 11 b := make([]byte, tagSize+4) b[0] = tagType b[1] = byte(payloadSize >> 16) b[2] = byte(payloadSize >> 8) b[3] = byte(payloadSize) b[4] = byte(timeMS >> 16) b[5] = byte(timeMS >> 8) b[6] = byte(timeMS) b[7] = byte(timeMS >> 24) copy(b[11:], payload) binary.BigEndian.PutUint32(b[tagSize:], tagSize) return b } func encodeAVData(codec *core.Codec, isFrame byte) []byte { switch codec.Name { case core.CodecH264: return []byte{ 1<<4 | 7, // keyframe + AVC isFrame, // 0 - config, 1 - frame 0, 0, 0, // composition time = 0 } case core.CodecAAC: var b0 byte = 10 << 4 // AAC switch codec.ClockRate { case 11025: b0 |= 1 << 2 case 22050: b0 |= 2 << 2 case 44100: b0 |= 3 << 2 } b0 |= 1 << 1 // 16 bits if codec.Channels == 2 { b0 |= 1 } return []byte{b0, isFrame} // 0 - config, 1 - frame } return nil } ================================================ FILE: pkg/flv/producer.go ================================================ package flv import ( "bytes" "encoding/binary" "errors" "io" "time" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/pion/rtp" ) type Producer struct { core.Connection rd *core.ReadBuffer video, audio *core.Receiver } func Open(rd io.Reader) (*Producer, error) { prod := &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "flv", Transport: rd, }, rd: core.NewReadBuffer(rd), } if err := prod.probe(); err != nil { return nil, err } return prod, nil } const ( Signature = "FLV" TagAudio = 8 TagVideo = 9 TagData = 18 CodecAAC = 10 CodecH264 = 7 CodecHEVC = 12 ) const ( PacketTypeAVCHeader = iota PacketTypeAVCNALU PacketTypeAVCEnd ) const ( PacketTypeSequenceStart = iota PacketTypeCodedFrames PacketTypeSequenceEnd PacketTypeCodedFramesX PacketTypeMetadata PacketTypeMPEG2TSSequenceStart ) func (c *Producer) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { receiver, _ := c.Connection.GetTrack(media, codec) if media.Kind == core.KindVideo { c.video = receiver } else { c.audio = receiver } return receiver, nil } func (c *Producer) Start() error { for { pkt, err := c.readPacket() if err != nil { return err } c.Recv += len(pkt.Payload) switch pkt.PayloadType { case TagAudio: if c.audio == nil || pkt.Payload[1] == 0 { continue } pkt.Timestamp = TimeToRTP(pkt.Timestamp, c.audio.Codec.ClockRate) pkt.Payload = pkt.Payload[2:] c.audio.WriteRTP(pkt) case TagVideo: if c.video == nil { continue } if isExHeader(pkt.Payload) { switch packetType := pkt.Payload[0] & 0b1111; packetType { case PacketTypeCodedFrames: // frame type 4b, packet type 4b, fourCC 32b, composition time 24b pkt.Payload = pkt.Payload[8:] case PacketTypeCodedFramesX: // frame type 4b, packet type 4b, fourCC 32b pkt.Payload = pkt.Payload[5:] default: continue } } else { switch pkt.Payload[1] { case PacketTypeAVCNALU: // frame type 4b, codecID 4b, avc packet type 8b, composition time 24b pkt.Payload = pkt.Payload[5:] default: continue } } pkt.Timestamp = TimeToRTP(pkt.Timestamp, c.video.Codec.ClockRate) c.video.WriteRTP(pkt) } } } func (c *Producer) probe() error { if err := c.readHeader(); err != nil { return err } c.rd.BufferSize = core.ProbeSize defer c.rd.Reset() // Normal software sends: // 1. Video/audio flag in header // 2. MetaData as first tag (with video/audio codec info) // 3. Video/audio headers in 2nd and 3rd tag // Reolink camera sends: // 1. Empty video/audio flag // 2. MedaData without stereo key for AAC // 3. Audio header after Video keyframe tag // OpenIPC camera (on old firmwares) sends: // 1. Empty video/audio flag // 2. No MetaData packet // 3. Sends a video packet in more than 3 seconds waitVideo := true waitAudio := true timeout := time.Now().Add(time.Second * 5) for (waitVideo || waitAudio) && time.Now().Before(timeout) { pkt, err := c.readPacket() if err != nil { return err } //log.Printf("%d %0.20s", pkt.PayloadType, pkt.Payload) switch pkt.PayloadType { case TagAudio: if !waitAudio { continue } _ = pkt.Payload[1] // bounds codecID := pkt.Payload[0] >> 4 // SoundFormat _ = pkt.Payload[0] & 0b1100 // SoundRate _ = pkt.Payload[0] & 0b0010 // SoundSize _ = pkt.Payload[0] & 0b0001 // SoundType if codecID != CodecAAC { continue } if pkt.Payload[1] != 0 { // check if header continue } codec := aac.ConfigToCodec(pkt.Payload[2:]) media := &core.Media{ Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, } c.Medias = append(c.Medias, media) waitAudio = false case TagVideo: if !waitVideo { continue } var codec *core.Codec if isExHeader(pkt.Payload) { if string(pkt.Payload[1:5]) != "hvc1" { continue } if packetType := pkt.Payload[0] & 0b1111; packetType != PacketTypeSequenceStart { continue } codec = h265.ConfigToCodec(pkt.Payload[5:]) } else { _ = pkt.Payload[0] >> 4 // FrameType if packetType := pkt.Payload[1]; packetType != PacketTypeAVCHeader { // check if header continue } switch codecID := pkt.Payload[0] & 0b1111; codecID { case CodecH264: codec = h264.ConfigToCodec(pkt.Payload[5:]) case CodecHEVC: codec = h265.ConfigToCodec(pkt.Payload[5:]) default: continue } } media := &core.Media{ Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, } c.Medias = append(c.Medias, media) waitVideo = false case TagData: if !bytes.Contains(pkt.Payload, []byte("onMetaData")) { continue } // Dahua cameras doesn't send videocodecid if !bytes.Contains(pkt.Payload, []byte("videocodecid")) && !bytes.Contains(pkt.Payload, []byte("width")) && !bytes.Contains(pkt.Payload, []byte("framerate")) { waitVideo = false } if !bytes.Contains(pkt.Payload, []byte("audiocodecid")) { waitAudio = false } } } return nil } func (c *Producer) readHeader() error { b := make([]byte, 9) if _, err := io.ReadFull(c.rd, b); err != nil { return err } if string(b[:3]) != Signature { return errors.New("flv: wrong header") } _ = b[4] // flags (skip because unsupported by Reolink cameras) if skip := binary.BigEndian.Uint32(b[5:]) - 9; skip > 0 { if _, err := io.ReadFull(c.rd, make([]byte, skip)); err != nil { return err } } return nil } func (c *Producer) readPacket() (*rtp.Packet, error) { // https://rtmp.veriskope.com/pdf/video_file_format_spec_v10.pdf b := make([]byte, 4+11) if _, err := io.ReadFull(c.rd, b); err != nil { return nil, err } b = b[4 : 4+11] // skip previous tag size size := uint32(b[1])<<16 | uint32(b[2])<<8 | uint32(b[3]) pkt := &rtp.Packet{ Header: rtp.Header{ PayloadType: b[0], Timestamp: uint32(b[4])<<16 | uint32(b[5])<<8 | uint32(b[6]) | uint32(b[7])<<24, }, Payload: make([]byte, size), } if _, err := io.ReadFull(c.rd, pkt.Payload); err != nil { return nil, err } //log.Printf("[FLV] %d %.40x", pkt.PayloadType, pkt.Payload) return pkt, nil } // TimeToRTP convert time in milliseconds to RTP time func TimeToRTP(timeMS, clockRate uint32) uint32 { // for clockRates 90000, 16000, 8000, etc. - we can use: // return timeMS * (clockRate / 1000) // but for clockRates 44100, 22050, 11025 - we should use: return uint32(uint64(timeMS) * uint64(clockRate) / 1000) } func isExHeader(data []byte) bool { return data[0]&0b1000_0000 != 0 } ================================================ FILE: pkg/gopro/discovery.go ================================================ package gopro import ( "net" "net/http" "regexp" ) func Discovery() (urls []string) { ints, err := net.Interfaces() if err != nil { return nil } // The socket address for USB connections is 172.2X.1YZ.51:8080 // https://gopro.github.io/OpenGoPro/http_2_0#socket-address re := regexp.MustCompile(`^172\.2\d\.1\d\d\.`) for _, itf := range ints { addrs, err := itf.Addrs() if err != nil { continue } for _, addr := range addrs { host := addr.String() if !re.MatchString(host) { continue } host = host[:11] + "51" // 172.2x.1xx.xxx res, err := http.Get("http://" + host + ":8080/gopro/webcam/status") if err != nil { continue } _ = res.Body.Close() urls = append(urls, host) } } return } ================================================ FILE: pkg/gopro/producer.go ================================================ package gopro import ( "errors" "io" "net" "net/http" "net/url" "time" "github.com/AlexxIT/go2rtc/pkg/mpegts" ) func Dial(rawURL string) (*mpegts.Producer, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } r := &listener{host: u.Host} if err = r.command("/gopro/webcam/stop"); err != nil { return nil, err } if err = r.listen(); err != nil { return nil, err } if err = r.command("/gopro/webcam/start"); err != nil { return nil, err } prod, err := mpegts.Open(r) if err != nil { return nil, err } prod.FormatName = "gopro" prod.RemoteAddr = u.Host return prod, nil } type listener struct { conn net.PacketConn host string packet []byte packets chan []byte } func (r *listener) Read(p []byte) (n int, err error) { if r.packet == nil { var ok bool if r.packet, ok = <-r.packets; !ok { return 0, io.EOF // channel closed } } n = copy(p, r.packet) if n < len(r.packet) { r.packet = r.packet[n:] } else { r.packet = nil } return } func (r *listener) Close() error { return r.conn.Close() } func (r *listener) command(api string) error { client := &http.Client{Timeout: 5 * time.Second} res, err := client.Get("http://" + r.host + ":8080" + api) if err != nil { return err } _ = res.Body.Close() if res.StatusCode != http.StatusOK { return errors.New("gopro: wrong response: " + res.Status) } return nil } func (r *listener) listen() (err error) { if r.conn, err = net.ListenPacket("udp4", ":8554"); err != nil { return } r.packets = make(chan []byte, 1024) go r.worker() return } func (r *listener) worker() { b := make([]byte, 1500) for { if err := r.conn.SetReadDeadline(time.Now().Add(3 * time.Second)); err != nil { break } n, _, err := r.conn.ReadFrom(b) if err != nil { break } packet := make([]byte, n) copy(packet, b) r.packets <- packet } close(r.packets) _ = r.command("/gopro/webcam/stop") } ================================================ FILE: pkg/h264/README.md ================================================ # H264 Payloader code taken from [pion](https://github.com/pion/rtp) library and changed to AVC packets support. ## Useful Links - [RTP Payload Format for H.264 Video](https://datatracker.ietf.org/doc/html/rfc6184) - [The H264 Sequence parameter set](https://www.cardinalpeak.com/blog/the-h-264-sequence-parameter-set) - [H.264 Video Types (Microsoft)](https://docs.microsoft.com/en-us/windows/win32/directshow/h-264-video-types) - [Automatic Generation of H.264 Parameter Sets to Recover Video File Fragments](https://arxiv.org/pdf/2104.14522.pdf) - [Chromium sources](https://chromium.googlesource.com/external/webrtc/+/HEAD/common_video/h264) - [AVC levels](https://en.wikipedia.org/wiki/Advanced_Video_Coding#Levels) - [AVC profiles table](https://developer.mozilla.org/ru/docs/Web/Media/Formats/codecs_parameter) - [Supported Media for Google Cast](https://developers.google.com/cast/docs/media) - [Two stream formats, Annex-B, AVCC (H.264) and HVCC (H.265)](https://www.programmersought.com/article/3901815022/) - https://docs.aws.amazon.com/kinesisvideostreams/latest/dg/producer-reference-nal.html ================================================ FILE: pkg/h264/annexb/annexb.go ================================================ // Package annexb - universal for H264 and H265 package annexb import ( "bytes" "encoding/binary" ) const StartCode = "\x00\x00\x00\x01" const startAUD = StartCode + "\x09\xF0" const startAUDstart = startAUD + StartCode // EncodeToAVCC // // FFmpeg MPEG-TS: 00000001 AUD 00000001 SPS 00000001 PPS 000001 IFrame // FFmpeg H264: 00000001 SPS 00000001 PPS 000001 IFrame 00000001 PFrame // Reolink: 000001 AUD 000001 VPS 00000001 SPS 00000001 PPS 00000001 IDR 00000001 IDR func EncodeToAVCC(annexb []byte) (avc []byte) { var start int avc = make([]byte, 0, len(annexb)+4) // init memory with little overhead for i := 0; ; i++ { var offset int if i+3 < len(annexb) { // search next separator if annexb[i] == 0 && annexb[i+1] == 0 { if annexb[i+2] == 1 { offset = 3 // 00 00 01 } else if annexb[i+2] == 0 && annexb[i+3] == 1 { offset = 4 // 00 00 00 01 } else { continue } } else { continue } } else { i = len(annexb) // move i to data end } if start != 0 { size := uint32(i - start) avc = binary.BigEndian.AppendUint32(avc, size) avc = append(avc, annexb[start:i]...) } // sometimes FFmpeg put separator at the end if i += offset; i == len(annexb) { break } if isAUD(annexb[i]) { start = 0 // skip this NALU } else { start = i // save this position } } return } func isAUD(b byte) bool { const h264 = 9 const h265 = 35 << 1 return b&0b0001_1111 == h264 || b&0b0111_1110 == h265 } func DecodeAVCC(b []byte, safeClone bool) []byte { if safeClone { b = bytes.Clone(b) } for i := 0; i < len(b); { size := int(binary.BigEndian.Uint32(b[i:])) b[i] = 0 b[i+1] = 0 b[i+2] = 0 b[i+3] = 1 i += 4 + size } return b } // DecodeAVCCWithAUD - AUD doesn't important for FFmpeg, but important for Safari func DecodeAVCCWithAUD(src []byte) []byte { dst := make([]byte, len(startAUD)+len(src)) copy(dst, startAUD) copy(dst[len(startAUD):], src) DecodeAVCC(dst[len(startAUD):], false) return dst } const ( h264PFrame = 1 h264IFrame = 5 h264SPS = 7 h264PPS = 8 h265VPS = 32 h265PFrame = 1 ) // IndexFrame - get new frame start position in the AnnexB stream func IndexFrame(b []byte) int { if len(b) < len(startAUDstart) { return -1 } for i := len(startAUDstart); ; { if di := bytes.Index(b[i:], []byte(StartCode)); di < 0 { break } else { i += di + 4 // move to NALU start } if i >= len(b) { break } h264Type := b[i] & 0b1_1111 switch h264Type { case h264PFrame, h264SPS: return i - 4 // move to start code case h264IFrame, h264PPS: continue } h265Type := (b[i] >> 1) & 0b11_1111 switch h265Type { case h265PFrame, h265VPS: return i - 4 // move to start code } } return -1 } func FixAnnexBInAVCC(b []byte) []byte { for i := 0; i < len(b); { if i+4 >= len(b) { break } size := bytes.Index(b[i+4:], []byte{0, 0, 0, 1}) if size < 0 { size = len(b) - (i + 4) } binary.BigEndian.PutUint32(b[i:], uint32(size)) i += size + 4 } return b } ================================================ FILE: pkg/h264/annexb/annexb_test.go ================================================ package annexb import ( "bytes" "encoding/binary" "encoding/hex" "strings" "testing" "github.com/stretchr/testify/require" ) func decode(s string) []byte { b, _ := hex.DecodeString(strings.ReplaceAll(s, " ", "")) return b } func naluTypes(avcc []byte) (types []byte) { for { types = append(types, avcc[4]) size := 4 + binary.BigEndian.Uint32(avcc) if size < uint32(len(avcc)) { avcc = avcc[size:] } else { break } } return } func TestFFmpegH264(t *testing.T) { // ffmpeg -re -i bbb.mp4 -c copy -f h264 - s := "000000016764001fac2484014016ec0440000003004000000c23c60c92 0000000168ee32c8b0 00000165888080033ffef5f8454f32cb1bb4203f854dd69bc2ca91b2bce1fb3527440000030000030000030000030050999841d1afd324aea000000300000f600011c0001b40004e40011f0003b80010800059000238000be0005e000220001100000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300004041 00000001" b := EncodeToAVCC(decode(s)) require.True(t, bytes.HasSuffix(b, []byte{0x40, 0x41})) n := naluTypes(b) require.Equal(t, []byte{0x67, 0x68, 0x65}, n) } func TestFFmpegMPEGTSH264(t *testing.T) { // ffmpeg -re -i bbb.mp4 -c copy -f mpegts - s := "00000001 09f0 000000016764001fac2484014016ec0440000003004000000c23c60c92 0000000168ee32c8b0 00000165888080033ffef5f8454f32cb1bb4203f854dd69bc2ca91b2bce1fb3527440000030000030000030000030050999841d1afd324aea000000300000f600011c0001b40004e40011f0003b80010800059000238000be0005e000220001100000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300000300004041" b := EncodeToAVCC(decode(s)) n := naluTypes(b) require.Equal(t, []byte{0x67, 0x68, 0x65}, n) } func TestFFmpegHEVC(t *testing.T) { // ffmpeg -re -i bbb.mp4 -c libx265 -preset superfast -tune zerolatency -f hevc - s := "0000000140010c01ffff01600000030090000003000003005dba0240 0000000142010101600000030090000003000003005da00280802d165bab930bc05a7080000003008000000c04 000000014401c1718312 0000014e0105ffffffffffffffffff2b2ca2de09b51747dbbb55a4fe7fc2fc4e7832363520286275696c642032303829202d20332e352b3131312d6330616631386464353a5b57696e646f77735d5b4743432031332e322e305d5b3634206269745d20386269742b31306269742b3132626974202d20482e3236352f4845564320636f646563202d20436f7079726967687420323031332d3230313820286329204d756c7469636f7265776172" b := EncodeToAVCC(decode(s)) n := naluTypes(b) require.Equal(t, []byte{0x40, 0x42, 0x44, 0x4e}, n) } func TestFFmpegHEVC2(t *testing.T) { // ffmpeg -re -i bbb.mp4 -c libx265 -preset superfast -tune zerolatency -f hevc - s := "0000000140010c01ffff01600000030090000003000003005dba0240 0000000142010101600000030090000003000003005da00280802d165bab930bc05a7080000003008000000c04 000000014401c1718312 0000014e0105ffffffffffffffffff2b2ca2de09b51747dbbb55a4fe7fc2fc4e7832363520286275696c642032303829202d20332e352b3131312d6330616631386464353a5b57696e646f77735d5b4743432031332e322e305d5b3634206269745d20386269742b31306269742b3132626974202d20482e3236352f4845564320636f646563202d20436f7079726967687420323031332d3230313820286329204d756c7469636f7265776172652c20496e63202d20687474703a2f2f783236352e6f7267202d206f7074696f6e733a2063707569643d31303439303731206672616d652d746872656164733d32206e756d612d706f6f6c733d3420777070206e6f2d706d6f6465206e6f2d706d65206e6f2d70736e72206e6f2d7373696d206c6f672d6c6576656c3d322062697464657074683d3820696e7075742d6373703d31206670733d32342f3120696e7075742d7265733d313238307837323020696e7465726c6163653d3020746f74616c2d6672616d65733d30206c6576656c2d6964633d3020686967682d746965723d31207568642d62643d30207265663d31206e6f2d616c6c6f772d6e6f6e2d636f6e666f726d616e6365207265706561742d6865616465727320616e6e657862206e6f2d617564206e6f2d656f62206e6f2d656f73206e6f2d68726420696e666f20686173683d302074656d706f72616c2d6c61796572733d30206f70656e2d676f70206d696e2d6b6579696e743d3234206b6579696e743d32353020676f702d6c6f6f6b61686561643d3020626672616d65733d3020622d61646170743d30206e6f2d622d707972616d696420626672616d652d626961733d302072632d6c6f6f6b61686561643d30206c6f6f6b61686561642d736c696365733d34207363656e656375743d30206e6f2d686973742d7363656e65637574207261646c3d30206e6f2d73706c696365206e6f2d696e7472612d72656672657368206374753d3332206d696e2d63752d73697a653d38206e6f2d72656374206e6f2d616d70206d61782d74752d73697a653d33322074752d696e7465722d64657074683d312074752d696e7472612d64657074683d31206c696d69742d74753d302072646f712d6c6576656c3d302064796e616d69632d72643d302e3030206e6f2d7373696d2d7264207369676e68696465206e6f2d74736b6970206e722d696e7472613d30206e722d696e7465723d30206e6f2d636f6e73747261696e65642d696e747261207374726f6e672d696e7472612d736d6f6f7468696e67206d61782d6d657267653d32206c696d69742d726566733d30206e6f2d6c696d69742d6d6f646573206d653d31207375626d653d31206d6572616e67653d35372074656d706f72616c2d6d7670206e6f2d6672616d652d647570206e6f2d686d65206e6f2d77656967687470206e6f2d77656967687462206e6f2d616e616c797a652d7372632d70696373206465626c6f636b3d303a30206e6f2d73616f206e6f2d73616f2d6e6f6e2d6465626c6f636b2072643d322073656c6563746976652d73616f3d30206561726c792d736b69702072736b697020666173742d696e747261206e6f2d74736b69702d66617374206e6f2d63752d6c6f73736c657373206e6f2d622d696e747261206e6f2d73706c697472642d736b697020726470656e616c74793d30207073792d72643d322e3030207073792d72646f713d302e3030206e6f2d72642d726566696e65206e6f2d6c6f73736c65737320636271706f6666733d3020637271706f6666733d302072633d637266206372663d32382e302071636f6d703d302e3630207170737465703d342073746174732d77726974653d302073746174732d726561643d30206970726174696f3d312e34302061712d6d6f64653d302061712d737472656e6774683d302e3030206e6f2d637574726565207a6f6e652d636f756e743d30206e6f2d7374726963742d6362722071672d73697a653d3332206e6f2d72632d677261696e2071706d61783d36392071706d696e3d30206e6f2d636f6e73742d766276207361723d31206f7665727363616e3d3020766964656f666f726d61743d352072616e67653d3020636f6c6f727072696d3d32207472616e736665723d3220636f6c6f726d61747269783d32206368726f6d616c6f633d31206368726f6d616c6f632d746f703d30206368726f6d616c6f632d626f74746f6d3d3020646973706c61792d77696e646f773d3020636c6c3d302c30206d696e2d6c756d613d30206d61782d6c756d613d323535206c6f67322d6d61782d706f632d6c73623d38207675692d74696d696e672d696e666f207675692d6872642d696e666f20736c696365733d31206e6f2d6f70742d71702d707073206e6f2d6f70742d7265662d6c6973742d6c656e6774682d707073206e6f2d6d756c74692d706173732d6f70742d727073207363656e656375742d626961733d302e3035206e6f2d6f70742d63752d64656c74612d7170206e6f2d61712d6d6f74696f6e206e6f2d6864723130206e6f2d68647231302d6f7074206e6f2d6468647231302d6f7074206e6f2d6964722d7265636f766572792d73656920616e616c797369732d72657573652d6c6576656c3d3020616e616c797369732d736176652d72657573652d6c6576656c3d3020616e616c797369732d6c6f61642d72657573652d6c6576656c3d30207363616c652d666163746f723d3020726566696e652d696e7472613d3020726566696e652d696e7465723d3020726566696e652d6d763d3120726566696e652d6374752d646973746f7274696f6e3d30206e6f2d6c696d69742d73616f206374752d696e666f3d30206e6f2d6c6f77706173732d64637420726566696e652d616e616c797369732d747970653d3020636f70792d7069633d31206d61782d617573697a652d666163746f723d312e30206e6f2d64796e616d69632d726566696e65206e6f2d73696e676c652d736569206e6f2d686576632d6171206e6f2d737674206e6f2d6669656c642071702d61646170746174696f6e2d72616e67653d312e3030207363656e656375742d61776172652d71703d30636f6e666f726d616e63652d77696e646f772d6f6666736574732072696768743d3020626f74746f6d3d30206465636f6465722d6d61782d726174653d30206e6f2d7662762d6c6976652d6d756c74692d70617373206e6f2d6d63737466206e6f2d7362726380 0000012801adc2e5bca307b9ce6b18b5ad6a525294a6d117ffd3917322eebaeda718a0000003000003000003021207706824da718a00000300000300000300044408d5db4e31400000030000030000030012500c2725a000000300000300000300002a600e4880000003000003000003000019301180000003000003000003000007d400000300000300000300000300010b000003000003000003000003001810000003000003000003000003019100000300000300000300000d38000003000003000003000067c000000300000300000300025e000003000003000003000c58000003000003000003002b60000003000003000003007f80000003000003000003016300000300000300000303b2000003000003000006e400000300000300000e18000003000003000018d00000030000030000292000000300000300003ce00000030000030000030000030000030000bb80" b := EncodeToAVCC(decode(s)) n := naluTypes(b) require.Equal(t, []byte{0x40, 0x42, 0x44, 0x4e, 0x28}, n) } func TestFFmpegMPEGTSHEVC(t *testing.T) { // ffmpeg -re -i bbb.mp4 -c libx265 -preset superfast -tune zerolatency -an -f mpegts - s := "00000001460150 0000000140010c01ffff01600000030090000003000003005dba0240 0000000142010101600000030090000003000003005da00280802d165bab930bc05a7080000003008000000c04 000000014401c1718312 0000014e0105ffffffffffffffffff2b2ca2de09b51747dbbb55a4fe7fc2fc4e7832363520286275696c642032303829202d20332e352b3131312d6330616631386464353a5b57696e646f77735d5b4743432031332e322e305d5b3634206269745d20386269742b31306269742b3132626974202d20482e3236352f4845564320636f646563202d20436f7079726967687420323031332d3230313820286329204d756c7469636f7265776172652c20496e63202d20687474703a2f2f783236352e6f7267202d206f7074696f6e733a2063707569643d31303439303731206672616d652d746872656164733d32206e756d612d706f6f6c733d3420777070206e6f2d706d6f6465206e6f2d706d65206e6f2d70736e72206e6f2d7373696d206c6f672d6c6576656c3d322062697464657074683d3820696e7075742d6373703d31206670733d32342f3120696e7075742d7265733d313238307837323020696e7465726c6163653d3020746f74616c2d6672616d65733d30206c6576656c2d6964633d3020686967682d746965723d31207568642d62643d30207265663d31206e6f2d616c6c6f772d6e6f6e2d636f6e666f726d616e6365207265706561742d6865616465727320616e6e657862206e6f2d617564206e6f2d656f62206e6f2d656f73206e6f2d68726420696e666f20686173683d302074656d706f72616c2d6c61796572733d30206f70656e2d676f70206d696e2d6b6579696e743d3234206b6579696e743d32353020676f702d6c6f6f6b61686561643d3020626672616d65733d3020622d61646170743d30206e6f2d622d707972616d696420626672616d652d626961733d302072632d6c6f6f6b61686561643d30206c6f6f6b61686561642d736c696365733d34207363656e656375743d30206e6f2d686973742d7363656e65637574207261646c3d30206e6f2d73706c696365206e6f2d696e7472612d72656672657368206374753d3332206d696e2d63752d73697a653d38206e6f2d72656374206e6f2d616d70206d61782d74752d73697a653d33322074752d696e7465722d64657074683d312074752d696e7472612d64657074683d31206c696d69742d74753d302072646f712d6c6576656c3d302064796e616d69632d72643d302e3030206e6f2d7373696d2d7264207369676e68696465206e6f2d74736b6970206e722d696e7472613d30206e722d696e7465723d30206e6f2d636f6e73747261696e65642d696e747261207374726f6e672d696e7472612d736d6f6f7468696e67206d61782d6d657267653d32206c696d69742d726566733d30206e6f2d6c696d69742d6d6f646573206d653d31207375626d653d31206d6572616e67653d35372074656d706f72616c2d6d7670206e6f2d6672616d652d647570206e6f2d686d65206e6f2d77656967687470206e6f2d77656967687462206e6f2d616e616c797a652d7372632d70696373206465626c6f636b3d303a30206e6f2d73616f206e6f2d73616f2d6e6f6e2d6465626c6f636b2072643d322073656c6563746976652d73616f3d30206561726c792d736b69702072736b697020666173742d696e747261206e6f2d74736b69702d66617374206e6f2d63752d6c6f73736c657373206e6f2d622d696e747261206e6f2d73706c697472642d736b697020726470656e616c74793d30207073792d72643d322e3030207073792d72646f713d302e3030206e6f2d72642d726566696e65206e6f2d6c6f73736c65737320636271706f6666733d3020637271706f6666733d302072633d637266206372663d32382e302071636f6d703d302e3630207170737465703d342073746174732d77726974653d302073746174732d726561643d30206970726174696f3d312e34302061712d6d6f64653d302061712d737472656e6774683d302e3030206e6f2d637574726565207a6f6e652d636f756e743d30206e6f2d7374726963742d6362722071672d73697a653d3332206e6f2d72632d677261696e2071706d61783d36392071706d696e3d30206e6f2d636f6e73742d766276207361723d31206f7665727363616e3d3020766964656f666f726d61743d352072616e67653d3020636f6c6f727072696d3d32207472616e736665723d3220636f6c6f726d61747269783d32206368726f6d616c6f633d31206368726f6d616c6f632d746f703d30206368726f6d616c6f632d626f74746f6d3d3020646973706c61792d77696e646f773d3020636c6c3d302c30206d696e2d6c756d613d30206d61782d6c756d613d323535206c6f67322d6d61782d706f632d6c73623d38207675692d74696d696e672d696e666f207675692d6872642d696e666f20736c696365733d31206e6f2d6f70742d71702d707073206e6f2d6f70742d7265662d6c6973742d6c656e6774682d707073206e6f2d6d756c74692d706173732d6f70742d727073207363656e656375742d626961733d302e3035206e6f2d6f70742d63752d64656c74612d7170206e6f2d61712d6d6f74696f6e206e6f2d6864723130206e6f2d68647231302d6f7074206e6f2d6468647231302d6f7074206e6f2d6964722d7265636f766572792d73656920616e616c797369732d72657573652d6c6576656c3d3020616e616c797369732d736176652d72657573652d6c6576656c3d3020616e616c797369732d6c6f61642d72657573652d6c6576656c3d30207363616c652d666163746f723d3020726566696e652d696e7472613d3020726566696e652d696e7465723d3020726566696e652d6d763d3120726566696e652d6374752d646973746f7274696f6e3d30206e6f2d6c696d69742d73616f206374752d696e666f3d30206e6f2d6c6f77706173732d64637420726566696e652d616e616c797369732d747970653d3020636f70792d7069633d31206d61782d617573697a652d666163746f723d312e30206e6f2d64796e616d69632d726566696e65206e6f2d73696e676c652d736569206e6f2d686576632d6171206e6f2d737674206e6f2d6669656c642071702d61646170746174696f6e2d72616e67653d312e3030207363656e656375742d61776172652d71703d30636f6e666f726d616e63652d77696e646f772d6f6666736574732072696768743d3020626f74746f6d3d30206465636f6465722d6d61782d726174653d30206e6f2d7662762d6c6976652d6d756c74692d70617373206e6f2d6d63737466206e6f2d7362726380 0000012801adc2e5bca307b9ce6b18b5ad6a525294a6d117ffd3917322eebaeda718a0000003000003000003021207706824da718a00000300000300000300044408d5db4e31400000030000030000030012500c2725a000000300000300000300002a600e4880000003000003000003000019301180000003000003000003000007d400000300000300000300000300010b000003000003000003000003001810000003000003000003000003019100000300000300000300000d38000003000003000003000067c000000300000300000300025e000003000003000003000c58000003000003000003002b60000003000003000003007f80000003000003000003016300000300000300000303b2000003000003000006e400000300000300000e18000003000003000018d00000030000030000292000000300000300003ce00000030000030000030000030000030000bb80" b := EncodeToAVCC(decode(s)) n := naluTypes(b) require.Equal(t, []byte{0x40, 0x42, 0x44, 0x4e, 0x28}, n) } func TestReolink(t *testing.T) { s := "000001460150 00000140010C01FFFF01600000030000030000030000030096AC09 0000000142010101600000030000030000030000030096A001E020021C7F8AAD3BA24BB804000013D800018CE008 000000014401C072F0941E3648 000000012601" b := EncodeToAVCC(decode(s)) n := naluTypes(b) require.Equal(t, []byte{0x40, 0x42, 0x44, 0x26}, n) } func TestDahua(t *testing.T) { s := "00000001460150 00000140010c01ffff01400000030000030000030000030099ac0900 0000000142010101400000030000030000030000030099a001402005a1fe5aee46c1ae550400 000000014401c073c04c9000 000000012601" b := EncodeToAVCC(decode(s)) n := naluTypes(b) require.Equal(t, []byte{0x40, 0x42, 0x44, 0x26}, n) } func TestUSB(t *testing.T) { s := "00 00 00 01 67 4D 00 1F 8D 8D 40 28 02 DD 37 01 01 01 40 00 01 C2 00 00 57 E4 01 00 00 00 01 68 EE 3C 80 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 65 88 80 00" b := EncodeToAVCC(decode(s)) n := naluTypes(b) require.Equal(t, []byte{0x67, 0x68, 0x65}, n) s = "00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 41 9A 00 4C" b = EncodeToAVCC(decode(s)) n = naluTypes(b) require.Equal(t, []byte{0x41}, n) } ================================================ FILE: pkg/h264/avc.go ================================================ package h264 import ( "bytes" "encoding/binary" ) const forbiddenZeroBit = 0x80 const nalUnitType = 0x1F // Deprecated: DecodeStream - find and return first AU in AVC format // useful for processing live streams with unknown separator size func DecodeStream(annexb []byte) ([]byte, int) { startPos := -1 i := 0 for { // search next separator if i = IndexFrom(annexb, []byte{0, 0, 1}, i); i < 0 { break } // move i to next AU if i += 3; i >= len(annexb) { break } // check if AU type valid octet := annexb[i] if octet&forbiddenZeroBit != 0 { continue } // 0 => AUD => SPS/IF/PF => AUD // 0 => SPS/PF => SPS/PF nalType := octet & nalUnitType if startPos >= 0 { switch nalType { case NALUTypeAUD, NALUTypeSPS, NALUTypePFrame: if annexb[i-4] == 0 { return DecodeAnnexB(annexb[startPos : i-4]), i - 4 } else { return DecodeAnnexB(annexb[startPos : i-3]), i - 3 } } } else { switch nalType { case NALUTypeSPS, NALUTypePFrame: if i >= 4 && annexb[i-4] == 0 { startPos = i - 4 } else { startPos = i - 3 } } } } return nil, 0 } // DecodeAnnexB - convert AnnexB to AVC format // support unknown separator size func DecodeAnnexB(b []byte) []byte { if b[2] == 1 { // convert: 0 0 1 => 0 0 0 1 b = append([]byte{0}, b...) } startPos := 0 i := 4 for { // search next separato if i = IndexFrom(b, []byte{0, 0, 1}, i); i < 0 { break } // move i to next AU if i += 3; i >= len(b) { break } // check if AU type valid octet := b[i] if octet&forbiddenZeroBit != 0 { continue } switch octet & nalUnitType { case NALUTypePFrame, NALUTypeIFrame, NALUTypeSPS, NALUTypePPS: if b[i-4] != 0 { // prefix: 0 0 1 binary.BigEndian.PutUint32(b[startPos:], uint32(i-startPos-7)) tmp := make([]byte, 0, len(b)+1) tmp = append(tmp, b[:i]...) tmp = append(tmp, 0) b = append(tmp, b[i:]...) startPos = i - 3 } else { // prefix: 0 0 0 1 binary.BigEndian.PutUint32(b[startPos:], uint32(i-startPos-8)) startPos = i - 4 } } } binary.BigEndian.PutUint32(b[startPos:], uint32(len(b)-startPos-4)) return b } func IndexFrom(b []byte, sep []byte, from int) int { if from > 0 { if from < len(b) { if i := bytes.Index(b[from:], sep); i >= 0 { return from + i } } return -1 } return bytes.Index(b, sep) } ================================================ FILE: pkg/h264/avcc.go ================================================ // Package h264 - AVCC format related functions package h264 import ( "bytes" "encoding/base64" "encoding/binary" "encoding/hex" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) func RepairAVCC(codec *core.Codec, handler core.HandlerFunc) core.HandlerFunc { sps, pps := GetParameterSet(codec.FmtpLine) ps := JoinNALU(sps, pps) return func(packet *rtp.Packet) { // this can happen for FLV from FFmpeg if NALUType(packet.Payload) == NALUTypeSEI { size := int(binary.BigEndian.Uint32(packet.Payload)) + 4 packet.Payload = packet.Payload[size:] } if NALUType(packet.Payload) == NALUTypeIFrame { packet.Payload = Join(ps, packet.Payload) } handler(packet) } } func JoinNALU(nalus ...[]byte) (avcc []byte) { var i, n int for _, nalu := range nalus { if i = len(nalu); i > 0 { n += 4 + i } } avcc = make([]byte, n) n = 0 for _, nal := range nalus { if i = len(nal); i > 0 { binary.BigEndian.PutUint32(avcc[n:], uint32(i)) n += 4 + copy(avcc[n+4:], nal) } } return } func SplitNALU(avcc []byte) [][]byte { var nals [][]byte for { // get AVC length size := int(binary.BigEndian.Uint32(avcc)) + 4 // check if multiple items in one packet if size < len(avcc) { nals = append(nals, avcc[:size]) avcc = avcc[size:] } else { nals = append(nals, avcc) break } } return nals } func NALUTypes(avcc []byte) []byte { var types []byte for { types = append(types, NALUType(avcc)) size := 4 + int(binary.BigEndian.Uint32(avcc)) if size < len(avcc) { avcc = avcc[size:] } else { break } } return types } func AVCCToCodec(avcc []byte) *core.Codec { buf := bytes.NewBufferString("packetization-mode=1") for { n := len(avcc) if n < 4 { break } size := 4 + int(binary.BigEndian.Uint32(avcc)) if n < size { break } switch NALUType(avcc) { case NALUTypeSPS: buf.WriteString(";profile-level-id=") buf.WriteString(hex.EncodeToString(avcc[5:8])) buf.WriteString(";sprop-parameter-sets=") buf.WriteString(base64.StdEncoding.EncodeToString(avcc[4:size])) case NALUTypePPS: buf.WriteString(",") buf.WriteString(base64.StdEncoding.EncodeToString(avcc[4:size])) } avcc = avcc[size:] } return &core.Codec{ Name: core.CodecH264, ClockRate: 90000, FmtpLine: buf.String(), PayloadType: core.PayloadTypeRAW, } } ================================================ FILE: pkg/h264/h264.go ================================================ package h264 import ( "encoding/base64" "encoding/binary" "encoding/hex" "fmt" "strings" "github.com/AlexxIT/go2rtc/pkg/core" ) const ( NALUTypePFrame = 1 // Coded slice of a non-IDR picture NALUTypeIFrame = 5 // Coded slice of an IDR picture NALUTypeSEI = 6 // Supplemental enhancement information (SEI) NALUTypeSPS = 7 // Sequence parameter set NALUTypePPS = 8 // Picture parameter set NALUTypeAUD = 9 // Access unit delimiter ) func NALUType(b []byte) byte { return b[4] & 0x1F } // IsKeyframe - check if any NALU in one AU is Keyframe func IsKeyframe(b []byte) bool { for { switch NALUType(b) { case NALUTypePFrame: return false case NALUTypeIFrame: return true } size := int(binary.BigEndian.Uint32(b)) + 4 if size < len(b) { b = b[size:] continue } else { return false } } } func Join(ps, iframe []byte) []byte { b := make([]byte, len(ps)+len(iframe)) i := copy(b, ps) copy(b[i:], iframe) return b } // https://developers.google.com/cast/docs/media const ( ProfileBaseline = 0x42 ProfileMain = 0x4D ProfileHigh = 0x64 CapabilityBaseline = 0xE0 CapabilityMain = 0x40 ) // GetProfileLevelID - get profile from fmtp line // Some devices won't play video with high level, so limit max profile and max level. // And return some profile even if fmtp line is empty. func GetProfileLevelID(fmtp string) string { // avc1.640029 - H.264 high 4.1 (Chromecast 1st and 2nd Gen) profile := byte(ProfileHigh) capab := byte(0) level := byte(41) if fmtp != "" { var conf []byte // some cameras has wrong profile-level-id // https://github.com/AlexxIT/go2rtc/issues/155 if s := core.Between(fmtp, "sprop-parameter-sets=", ","); s != "" { if sps, _ := base64.StdEncoding.DecodeString(s); len(sps) >= 4 { conf = sps[1:4] } } else if s = core.Between(fmtp, "profile-level-id=", ";"); s != "" { conf, _ = hex.DecodeString(s) } if len(conf) == 3 { // sanitize profile, capab and level to supported values switch conf[0] { case ProfileBaseline, ProfileMain: profile = conf[0] } switch conf[1] { case CapabilityBaseline, CapabilityMain: capab = conf[1] } switch conf[2] { case 30, 31, 40: level = conf[2] } } } return fmt.Sprintf("%02X%02X%02X", profile, capab, level) } func GetParameterSet(fmtp string) (sps, pps []byte) { if fmtp == "" { return } s := core.Between(fmtp, "sprop-parameter-sets=", ";") if s == "" { return } i := strings.IndexByte(s, ',') if i < 0 { return } sps, _ = base64.StdEncoding.DecodeString(s[:i]) pps, _ = base64.StdEncoding.DecodeString(s[i+1:]) return } // GetFmtpLine from SPS+PPS+IFrame in AVC format func GetFmtpLine(avc []byte) string { s := "packetization-mode=1" for { size := 4 + int(binary.BigEndian.Uint32(avc)) switch NALUType(avc) { case NALUTypeSPS: s += ";profile-level-id=" + hex.EncodeToString(avc[5:8]) s += ";sprop-parameter-sets=" + base64.StdEncoding.EncodeToString(avc[4:size]) case NALUTypePPS: s += "," + base64.StdEncoding.EncodeToString(avc[4:size]) } if size < len(avc) { avc = avc[size:] } else { return s } } } ================================================ FILE: pkg/h264/h264_test.go ================================================ package h264 import ( "encoding/base64" "encoding/hex" "testing" "github.com/stretchr/testify/require" ) func TestDecodeConfig(t *testing.T) { s := "01640033ffe1000c67640033ac1514a02800f19001000468ee3cb0" src, err := hex.DecodeString(s) require.Nil(t, err) profile, sps, pps := DecodeConfig(src) require.NotNil(t, profile) require.NotNil(t, sps) require.NotNil(t, pps) dst := EncodeConfig(sps, pps) require.Equal(t, src, dst) } func TestDecodeSPS(t *testing.T) { s := "Z0IAMukAUAHjQgAAB9IAAOqcCAA=" // Amcrest AD410 b, err := base64.StdEncoding.DecodeString(s) require.Nil(t, err) sps := DecodeSPS(b) require.Equal(t, uint16(2560), sps.Width()) require.Equal(t, uint16(1920), sps.Height()) s = "R00AKZmgHgCJ+WEAAAMD6AAATiCE" // Sonoff b, err = base64.StdEncoding.DecodeString(s) require.Nil(t, err) sps = DecodeSPS(b) require.Equal(t, uint16(1920), sps.Width()) require.Equal(t, uint16(1080), sps.Height()) s = "Z01AMqaAKAC1kAA=" // Dahua b, err = base64.StdEncoding.DecodeString(s) require.Nil(t, err) sps = DecodeSPS(b) require.Equal(t, uint16(2560), sps.Width()) require.Equal(t, uint16(1440), sps.Height()) s = "Z2QAM6wVFKAoAPGQ" // Reolink b, err = base64.StdEncoding.DecodeString(s) require.Nil(t, err) sps = DecodeSPS(b) require.Equal(t, uint16(2560), sps.Width()) require.Equal(t, uint16(1920), sps.Height()) s = "Z2QAKKwa0AoAt03AQEBQAAADABAAAAMB6PFCKg==" // TP-Link b, err = base64.StdEncoding.DecodeString(s) require.Nil(t, err) sps = DecodeSPS(b) require.Equal(t, uint16(1280), sps.Width()) require.Equal(t, uint16(720), sps.Height()) s = "Z2QAFqwa0BQF/yzcBAQFAAADAAEAAAMAHo8UIqA=" // TP-Link sub b, err = base64.StdEncoding.DecodeString(s) require.Nil(t, err) sps = DecodeSPS(b) require.Equal(t, uint16(640), sps.Width()) require.Equal(t, uint16(360), sps.Height()) } func TestGetProfileLevelID(t *testing.T) { // OpenIPC https://github.com/OpenIPC s := "profile-level-id=0033e7; packetization-mode=1; " profile := GetProfileLevelID(s) require.Equal(t, "640029", profile) // Eufy T8400 https://github.com/AlexxIT/go2rtc/issues/155 s = "packetization-mode=1;profile-level-id=276400" profile = GetProfileLevelID(s) require.Equal(t, "640029", profile) } func TestDecodeSPS2(t *testing.T) { s := "6764001fad84010c20086100430802184010c200843b50740932" b, err := hex.DecodeString(s) require.Nil(t, err) sps := DecodeSPS(b) require.Equal(t, uint16(928), sps.Width()) require.Equal(t, uint16(576), sps.Height()) s = "Z2QAHq2EAQwgCGEAQwgCGEAQwgCEO1BQF/yzcBAQFAAAD6AAAXcCEA==" // unknown b, err = base64.StdEncoding.DecodeString(s) require.Nil(t, err) sps = DecodeSPS(b) require.Equal(t, uint16(640), sps.Width()) require.Equal(t, uint16(360), sps.Height()) } func TestAVCCToCodec(t *testing.T) { s := "000000196764001fac2484014016ec0440000003004000000c23c60c920000000568ee32c8b0000000d365" b, _ := hex.DecodeString(s) codec := AVCCToCodec(b) require.Equal(t, "packetization-mode=1;profile-level-id=64001f;sprop-parameter-sets=Z2QAH6wkhAFAFuwEQAAAAwBAAAAMI8YMkg==,aO4yyLA=", codec.FmtpLine) } ================================================ FILE: pkg/h264/mpeg4.go ================================================ // Package h264 - MPEG4 format related functions package h264 import ( "bytes" "encoding/base64" "encoding/binary" "encoding/hex" "github.com/AlexxIT/go2rtc/pkg/core" ) // DecodeConfig - extract profile, SPS and PPS from MPEG4 config func DecodeConfig(conf []byte) (profile []byte, sps []byte, pps []byte) { if len(conf) < 6 || conf[0] != 1 { return } profile = conf[1:4] count := conf[5] & 0x1F conf = conf[6:] for i := byte(0); i < count; i++ { if len(conf) < 2 { return } size := 2 + int(binary.BigEndian.Uint16(conf)) if len(conf) < size { return } if sps == nil { sps = conf[2:size] } conf = conf[size:] } count = conf[0] conf = conf[1:] for i := byte(0); i < count; i++ { if len(conf) < 2 { return } size := 2 + int(binary.BigEndian.Uint16(conf)) if len(conf) < size { return } if pps == nil { pps = conf[2:size] } conf = conf[size:] } return } func EncodeConfig(sps, pps []byte) []byte { spsSize := uint16(len(sps)) ppsSize := uint16(len(pps)) buf := make([]byte, 5+3+spsSize+3+ppsSize) buf[0] = 1 copy(buf[1:], sps[1:4]) // profile buf[4] = 3 | 0xFC // ? LengthSizeMinusOne b := buf[5:] _ = b[3] b[0] = 1 | 0xE0 // ? sps count binary.BigEndian.PutUint16(b[1:], spsSize) copy(b[3:], sps) b = buf[5+3+spsSize:] _ = b[3] b[0] = 1 // pps count binary.BigEndian.PutUint16(b[1:], ppsSize) copy(b[3:], pps) return buf } func ConfigToCodec(conf []byte) *core.Codec { buf := bytes.NewBufferString("packetization-mode=1") profile, sps, pps := DecodeConfig(conf) if profile != nil { buf.WriteString(";profile-level-id=") buf.WriteString(hex.EncodeToString(profile)) } if sps != nil && pps != nil { buf.WriteString(";sprop-parameter-sets=") buf.WriteString(base64.StdEncoding.EncodeToString(sps)) buf.WriteString(",") buf.WriteString(base64.StdEncoding.EncodeToString(pps)) } return &core.Codec{ Name: core.CodecH264, ClockRate: 90000, FmtpLine: buf.String(), PayloadType: core.PayloadTypeRAW, } } ================================================ FILE: pkg/h264/payloader.go ================================================ package h264 import "encoding/binary" // Payloader payloads H264 packets type Payloader struct { IsAVC bool stapANalu []byte } const ( stapaNALUType = 24 fuaNALUType = 28 fubNALUType = 29 spsNALUType = 7 ppsNALUType = 8 audNALUType = 9 fillerNALUType = 12 fuaHeaderSize = 2 //stapaHeaderSize = 1 //stapaNALULengthSize = 2 naluTypeBitmask = 0x1F naluRefIdcBitmask = 0x60 //fuStartBitmask = 0x80 //fuEndBitmask = 0x40 outputStapAHeader = 0x78 ) //func annexbNALUStartCode() []byte { return []byte{0x00, 0x00, 0x00, 0x01} } func EmitNalus(nals []byte, isAVC bool, emit func([]byte)) { if !isAVC { nextInd := func(nalu []byte, start int) (indStart int, indLen int) { zeroCount := 0 for i, b := range nalu[start:] { if b == 0 { zeroCount++ continue } else if b == 1 { if zeroCount >= 2 { return start + i - zeroCount, zeroCount + 1 } } zeroCount = 0 } return -1, -1 } nextIndStart, nextIndLen := nextInd(nals, 0) if nextIndStart == -1 { emit(nals) } else { for nextIndStart != -1 { prevStart := nextIndStart + nextIndLen nextIndStart, nextIndLen = nextInd(nals, prevStart) if nextIndStart != -1 { emit(nals[prevStart:nextIndStart]) } else { // Emit until end of stream, no end indicator found emit(nals[prevStart:]) } } } } else { for { n := uint32(len(nals)) if n < 4 { break } end := 4 + binary.BigEndian.Uint32(nals) if n < end { break } emit(nals[4:end]) nals = nals[end:] } } } // Payload fragments a H264 packet across one or more byte arrays func (p *Payloader) Payload(mtu uint16, payload []byte) [][]byte { var payloads [][]byte if len(payload) == 0 { return payloads } EmitNalus(payload, p.IsAVC, func(nalu []byte) { if len(nalu) == 0 { return } naluType := nalu[0] & naluTypeBitmask naluRefIdc := nalu[0] & naluRefIdcBitmask switch naluType { case audNALUType, fillerNALUType: return case spsNALUType, ppsNALUType: if p.stapANalu == nil { p.stapANalu = []byte{outputStapAHeader} } p.stapANalu = append(p.stapANalu, byte(len(nalu)>>8), byte(len(nalu))) p.stapANalu = append(p.stapANalu, nalu...) return } if p.stapANalu != nil { // Pack current NALU with SPS and PPS as STAP-A // Supports multiple PPS in a row if len(p.stapANalu) <= int(mtu) { payloads = append(payloads, p.stapANalu) } p.stapANalu = nil } // Single NALU if len(nalu) <= int(mtu) { out := make([]byte, len(nalu)) copy(out, nalu) payloads = append(payloads, out) return } // FU-A maxFragmentSize := int(mtu) - fuaHeaderSize // The FU payload consists of fragments of the payload of the fragmented // NAL unit so that if the fragmentation unit payloads of consecutive // FUs are sequentially concatenated, the payload of the fragmented NAL // unit can be reconstructed. The NAL unit type octet of the fragmented // NAL unit is not included as such in the fragmentation unit payload, // but rather the information of the NAL unit type octet of the // fragmented NAL unit is conveyed in the F and NRI fields of the FU // indicator octet of the fragmentation unit and in the type field of // the FU header. An FU payload MAY have any number of octets and MAY // be empty. naluData := nalu // According to the RFC, the first octet is skipped due to redundant information naluDataIndex := 1 naluDataLength := len(nalu) - naluDataIndex naluDataRemaining := naluDataLength if min(maxFragmentSize, naluDataRemaining) <= 0 { return } for naluDataRemaining > 0 { currentFragmentSize := min(maxFragmentSize, naluDataRemaining) out := make([]byte, fuaHeaderSize+currentFragmentSize) // +---------------+ // |0|1|2|3|4|5|6|7| // +-+-+-+-+-+-+-+-+ // |F|NRI| Type | // +---------------+ out[0] = fuaNALUType out[0] |= naluRefIdc // +---------------+ // |0|1|2|3|4|5|6|7| // +-+-+-+-+-+-+-+-+ // |S|E|R| Type | // +---------------+ out[1] = naluType if naluDataRemaining == naluDataLength { // Set start bit out[1] |= 1 << 7 } else if naluDataRemaining-currentFragmentSize == 0 { // Set end bit out[1] |= 1 << 6 } copy(out[fuaHeaderSize:], naluData[naluDataIndex:naluDataIndex+currentFragmentSize]) payloads = append(payloads, out) naluDataRemaining -= currentFragmentSize naluDataIndex += currentFragmentSize } }) return payloads } func min(a, b int) int { if a < b { return a } return b } ================================================ FILE: pkg/h264/rtp.go ================================================ package h264 import ( "encoding/binary" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/pion/rtp" "github.com/pion/rtp/codecs" ) const RTPPacketVersionAVC = 0 const PSMaxSize = 128 // the biggest SPS I've seen is 48 (EZVIZ CS-CV210) func RTPDepay(codec *core.Codec, handler core.HandlerFunc) core.HandlerFunc { depack := &codecs.H264Packet{IsAVC: true} sps, pps := GetParameterSet(codec.FmtpLine) ps := JoinNALU(sps, pps) buf := make([]byte, 0, 512*1024) // 512K return func(packet *rtp.Packet) { //log.Printf("[RTP] codec: %s, nalu: %2d, size: %6d, ts: %10d, pt: %2d, ssrc: %d, seq: %d, %v", codec.Name, packet.Payload[0]&0x1F, len(packet.Payload), packet.Timestamp, packet.PayloadType, packet.SSRC, packet.SequenceNumber, packet.Marker) payload, err := depack.Unmarshal(packet.Payload) if len(payload) == 0 || err != nil { return } // Memory overflow protection. Can happen if we miss a lot of packets with the marker. // https://github.com/AlexxIT/go2rtc/issues/675 if len(buf) > 5*1024*1024 { buf = buf[: 0 : 512*1024] } // Fix TP-Link Tapo TC70: sends SPS and PPS with packet.Marker = true // Reolink Duo 2: sends SPS with Marker and PPS without if packet.Marker && len(payload) < PSMaxSize { switch NALUType(payload) { case NALUTypeSPS, NALUTypePPS: buf = append(buf, payload...) return case NALUTypeSEI: // RtspServer https://github.com/AlexxIT/go2rtc/issues/244 // sends, marked SPS, marked PPS, marked SEI, marked IFrame return } } if len(buf) == 0 { for { // Amcrest IP4M-1051: 9, 7, 8, 6, 28... // Amcrest IP4M-1051: 9, 6, 1 switch NALUType(payload) { case NALUTypeIFrame: // fix IFrame without SPS,PPS buf = append(buf, ps...) case NALUTypeSEI, NALUTypeAUD: // fix ffmpeg with transcoding first frame i := int(4 + binary.BigEndian.Uint32(payload)) // check if only one NAL (fix ffmpeg transcoding for Reolink RLC-510A) if i == len(payload) { return } payload = payload[i:] continue case NALUTypePFrame, NALUTypeSPS, NALUTypePPS: // pass default: return // skip any unknown NAL unit type } break } } // collect all NALs for Access Unit if !packet.Marker { buf = append(buf, payload...) return } if len(buf) > 0 { payload = append(buf, payload...) buf = buf[:0] } // should not be that huge SPS if NALUType(payload) == NALUTypeSPS && binary.BigEndian.Uint32(payload) >= PSMaxSize { // some Chinese buggy cameras have a single packet with SPS+PPS+IFrame separated by 00 00 00 01 // https://github.com/AlexxIT/WebRTC/issues/391 // https://github.com/AlexxIT/WebRTC/issues/392 payload = annexb.FixAnnexBInAVCC(payload) } //log.Printf("[AVC] %v, len: %d, ts: %10d, seq: %d", NALUTypes(payload), len(payload), packet.Timestamp, packet.SequenceNumber) clone := *packet clone.Version = RTPPacketVersionAVC clone.Payload = payload handler(&clone) } } func RTPPay(mtu uint16, handler core.HandlerFunc) core.HandlerFunc { if mtu == 0 { mtu = 1472 } payloader := &Payloader{IsAVC: true} sequencer := rtp.NewRandomSequencer() mtu -= 12 // rtp.Header size return func(packet *rtp.Packet) { if packet.Version != RTPPacketVersionAVC { handler(packet) return } payloads := payloader.Payload(mtu, packet.Payload) last := len(payloads) - 1 for i, payload := range payloads { clone := rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: i == last, SequenceNumber: sequencer.NextSequenceNumber(), Timestamp: packet.Timestamp, }, Payload: payload, } handler(&clone) } } } ================================================ FILE: pkg/h264/sps.go ================================================ package h264 import ( "fmt" "github.com/AlexxIT/go2rtc/pkg/bits" ) // http://www.itu.int/rec/T-REC-H.264 // https://webrtc.googlesource.com/src/+/refs/heads/main/common_video/h264/sps_parser.cc //goland:noinspection GoSnakeCaseUsage type SPS struct { profile_idc uint8 profile_iop uint8 level_idc uint8 seq_parameter_set_id uint32 chroma_format_idc uint32 separate_colour_plane_flag byte bit_depth_luma_minus8 uint32 bit_depth_chroma_minus8 uint32 qpprime_y_zero_transform_bypass_flag byte seq_scaling_matrix_present_flag byte log2_max_frame_num_minus4 uint32 pic_order_cnt_type uint32 log2_max_pic_order_cnt_lsb_minus4 uint32 delta_pic_order_always_zero_flag byte offset_for_non_ref_pic int32 offset_for_top_to_bottom_field int32 num_ref_frames_in_pic_order_cnt_cycle uint32 num_ref_frames uint32 gaps_in_frame_num_value_allowed_flag byte pic_width_in_mbs_minus_1 uint32 pic_height_in_map_units_minus_1 uint32 frame_mbs_only_flag byte mb_adaptive_frame_field_flag byte direct_8x8_inference_flag byte frame_cropping_flag byte frame_crop_left_offset uint32 frame_crop_right_offset uint32 frame_crop_top_offset uint32 frame_crop_bottom_offset uint32 vui_parameters_present_flag byte aspect_ratio_info_present_flag byte aspect_ratio_idc byte sar_width uint16 sar_height uint16 overscan_info_present_flag byte overscan_appropriate_flag byte video_signal_type_present_flag byte video_format uint8 video_full_range_flag byte colour_description_present_flag byte colour_description uint32 chroma_loc_info_present_flag byte chroma_sample_loc_type_top_field uint32 chroma_sample_loc_type_bottom_field uint32 timing_info_present_flag byte num_units_in_tick uint32 time_scale uint32 fixed_frame_rate_flag byte } func (s *SPS) Width() uint16 { width := 16 * (s.pic_width_in_mbs_minus_1 + 1) crop := 2 * (s.frame_crop_left_offset + s.frame_crop_right_offset) return uint16(width - crop) } func (s *SPS) Height() uint16 { height := 16 * (s.pic_height_in_map_units_minus_1 + 1) crop := 2 * (s.frame_crop_top_offset + s.frame_crop_bottom_offset) if s.frame_mbs_only_flag == 0 { height *= 2 } return uint16(height - crop) } func DecodeSPS(sps []byte) *SPS { // https://developer.ridgerun.com/wiki/index.php/H264_Analysis_Tools // ffmpeg -i file.h264 -c copy -bsf:v trace_headers -f null - r := bits.NewReader(sps) hdr := r.ReadByte() if hdr&0x1F != NALUTypeSPS { return nil } s := &SPS{ profile_idc: r.ReadByte(), profile_iop: r.ReadByte(), level_idc: r.ReadByte(), seq_parameter_set_id: r.ReadUEGolomb(), } switch s.profile_idc { case 100, 110, 122, 244, 44, 83, 86, 118, 128, 138, 139, 134, 135: n := byte(8) s.chroma_format_idc = r.ReadUEGolomb() if s.chroma_format_idc == 3 { s.separate_colour_plane_flag = r.ReadBit() n = 12 } s.bit_depth_luma_minus8 = r.ReadUEGolomb() s.bit_depth_chroma_minus8 = r.ReadUEGolomb() s.qpprime_y_zero_transform_bypass_flag = r.ReadBit() s.seq_scaling_matrix_present_flag = r.ReadBit() if s.seq_scaling_matrix_present_flag != 0 { for i := byte(0); i < n; i++ { //goland:noinspection GoSnakeCaseUsage seq_scaling_list_present_flag := r.ReadBit() if seq_scaling_list_present_flag != 0 { if i < 6 { s.scaling_list(r, 16) } else { s.scaling_list(r, 64) } } } } } s.log2_max_frame_num_minus4 = r.ReadUEGolomb() s.pic_order_cnt_type = r.ReadUEGolomb() switch s.pic_order_cnt_type { case 0: s.log2_max_pic_order_cnt_lsb_minus4 = r.ReadUEGolomb() case 1: s.delta_pic_order_always_zero_flag = r.ReadBit() s.offset_for_non_ref_pic = r.ReadSEGolomb() s.offset_for_top_to_bottom_field = r.ReadSEGolomb() s.num_ref_frames_in_pic_order_cnt_cycle = r.ReadUEGolomb() for i := uint32(0); i < s.num_ref_frames_in_pic_order_cnt_cycle; i++ { _ = r.ReadSEGolomb() // offset_for_ref_frame[i] } } s.num_ref_frames = r.ReadUEGolomb() s.gaps_in_frame_num_value_allowed_flag = r.ReadBit() s.pic_width_in_mbs_minus_1 = r.ReadUEGolomb() s.pic_height_in_map_units_minus_1 = r.ReadUEGolomb() s.frame_mbs_only_flag = r.ReadBit() if s.frame_mbs_only_flag == 0 { s.mb_adaptive_frame_field_flag = r.ReadBit() } s.direct_8x8_inference_flag = r.ReadBit() s.frame_cropping_flag = r.ReadBit() if s.frame_cropping_flag != 0 { s.frame_crop_left_offset = r.ReadUEGolomb() s.frame_crop_right_offset = r.ReadUEGolomb() s.frame_crop_top_offset = r.ReadUEGolomb() s.frame_crop_bottom_offset = r.ReadUEGolomb() } s.vui_parameters_present_flag = r.ReadBit() if s.vui_parameters_present_flag != 0 { s.aspect_ratio_info_present_flag = r.ReadBit() if s.aspect_ratio_info_present_flag != 0 { s.aspect_ratio_idc = r.ReadByte() if s.aspect_ratio_idc == 255 { s.sar_width = r.ReadUint16() s.sar_height = r.ReadUint16() } } s.overscan_info_present_flag = r.ReadBit() if s.overscan_info_present_flag != 0 { s.overscan_appropriate_flag = r.ReadBit() } s.video_signal_type_present_flag = r.ReadBit() if s.video_signal_type_present_flag != 0 { s.video_format = r.ReadBits8(3) s.video_full_range_flag = r.ReadBit() s.colour_description_present_flag = r.ReadBit() if s.colour_description_present_flag != 0 { s.colour_description = r.ReadUint24() } } s.chroma_loc_info_present_flag = r.ReadBit() if s.chroma_loc_info_present_flag != 0 { s.chroma_sample_loc_type_top_field = r.ReadUEGolomb() s.chroma_sample_loc_type_bottom_field = r.ReadUEGolomb() } s.timing_info_present_flag = r.ReadBit() if s.timing_info_present_flag != 0 { s.num_units_in_tick = r.ReadUint32() s.time_scale = r.ReadUint32() s.fixed_frame_rate_flag = r.ReadBit() } //... } if r.EOF { return nil } return s } //goland:noinspection GoSnakeCaseUsage func (s *SPS) scaling_list(r *bits.Reader, sizeOfScalingList int) { lastScale := int32(8) nextScale := int32(8) for j := 0; j < sizeOfScalingList; j++ { if nextScale != 0 { delta_scale := r.ReadSEGolomb() nextScale = (lastScale + delta_scale + 256) % 256 } if nextScale != 0 { lastScale = nextScale } } } func (s *SPS) Profile() string { switch s.profile_idc { case 0x42: return "Baseline" case 0x4D: return "Main" case 0x58: return "Extended" case 0x64: return "High" } return fmt.Sprintf("0x%02X", s.profile_idc) } func (s *SPS) PixFmt() string { if s.bit_depth_luma_minus8 == 0 { switch s.chroma_format_idc { case 1: if s.video_full_range_flag == 1 { return "yuvj420p" } return "yuv420p" case 2: return "yuv422p" case 3: return "yuv444p" } } return "" } func (s *SPS) String() string { return fmt.Sprintf( "%s %d.%d, %s, %dx%d", s.Profile(), s.level_idc/10, s.level_idc%10, s.PixFmt(), s.Width(), s.Height(), ) } // FixPixFmt - change yuvj420p to yuv420p in SPS // same as "-c:v copy -bsf:v h264_metadata=video_full_range_flag=0" func FixPixFmt(sps []byte) { r := bits.NewReader(sps) _ = r.ReadByte() profile := r.ReadByte() _ = r.ReadByte() _ = r.ReadByte() _ = r.ReadUEGolomb() switch profile { case 100, 110, 122, 244, 44, 83, 86, 118, 128, 138, 139, 134, 135: n := byte(8) if r.ReadUEGolomb() == 3 { _ = r.ReadBit() n = 12 } _ = r.ReadUEGolomb() _ = r.ReadUEGolomb() _ = r.ReadBit() if r.ReadBit() != 0 { for i := byte(0); i < n; i++ { if r.ReadBit() != 0 { return // skip } } } } _ = r.ReadUEGolomb() switch r.ReadUEGolomb() { case 0: _ = r.ReadUEGolomb() case 1: _ = r.ReadBit() _ = r.ReadSEGolomb() _ = r.ReadSEGolomb() n := r.ReadUEGolomb() for i := uint32(0); i < n; i++ { _ = r.ReadSEGolomb() } } _ = r.ReadUEGolomb() _ = r.ReadBit() _ = r.ReadUEGolomb() _ = r.ReadUEGolomb() if r.ReadBit() == 0 { _ = r.ReadBit() } _ = r.ReadBit() if r.ReadBit() != 0 { _ = r.ReadUEGolomb() _ = r.ReadUEGolomb() _ = r.ReadUEGolomb() _ = r.ReadUEGolomb() } if r.ReadBit() != 0 { if r.ReadBit() != 0 { if r.ReadByte() == 255 { _ = r.ReadUint16() _ = r.ReadUint16() } } if r.ReadBit() != 0 { _ = r.ReadBit() } if r.ReadBit() != 0 { _ = r.ReadBits8(3) if r.ReadBit() == 1 { pos, bit := r.Pos() sps[pos] &= ^byte(1 << bit) } } } } ================================================ FILE: pkg/h265/README.md ================================================ # H265 Payloader code taken from [pion](https://github.com/pion/rtp) library branch [h265](https://github.com/pion/rtp/tree/h265), because it's still not in release. Thanks to [@kevmo314](https://github.com/kevmo314). ## Useful links - https://datatracker.ietf.org/doc/html/rfc7798 - [Add initial support for WebRTC HEVC](https://trac.webkit.org/changeset/259452/webkit) ================================================ FILE: pkg/h265/avc.go ================================================ package h265 import "github.com/AlexxIT/go2rtc/pkg/h264" const forbiddenZeroBit = 0x80 const nalUnitType = 0x3F // Deprecated: DecodeStream - find and return first AU in AVC format // useful for processing live streams with unknown separator size func DecodeStream(annexb []byte) ([]byte, int) { startPos := -1 i := 0 for { // search next separator if i = h264.IndexFrom(annexb, []byte{0, 0, 1}, i); i < 0 { break } // move i to next AU if i += 3; i >= len(annexb) { break } // check if AU type valid octet := annexb[i] if octet&forbiddenZeroBit != 0 { continue } nalType := (octet >> 1) & nalUnitType if startPos >= 0 { switch nalType { case NALUTypeVPS, NALUTypePFrame: if annexb[i-4] == 0 { return h264.DecodeAnnexB(annexb[startPos : i-4]), i - 4 } else { return h264.DecodeAnnexB(annexb[startPos : i-3]), i - 3 } } } else { switch nalType { case NALUTypeVPS, NALUTypePFrame: if i >= 4 && annexb[i-4] == 0 { startPos = i - 4 } else { startPos = i - 3 } } } } return nil, 0 } ================================================ FILE: pkg/h265/avcc.go ================================================ // Package h265 - AVCC format related functions package h265 import ( "bytes" "encoding/base64" "encoding/binary" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/pion/rtp" ) func RepairAVCC(codec *core.Codec, handler core.HandlerFunc) core.HandlerFunc { vds, sps, pps := GetParameterSet(codec.FmtpLine) ps := h264.JoinNALU(vds, sps, pps) return func(packet *rtp.Packet) { switch NALUType(packet.Payload) { case NALUTypeIFrame, NALUTypeIFrame2, NALUTypeIFrame3: clone := *packet clone.Payload = h264.Join(ps, packet.Payload) handler(&clone) default: handler(packet) } } } func AVCCToCodec(avcc []byte) *core.Codec { buf := bytes.NewBufferString("profile-id=1") for { size := 4 + int(binary.BigEndian.Uint32(avcc)) switch NALUType(avcc) { case NALUTypeVPS: buf.WriteString(";sprop-vps=") buf.WriteString(base64.StdEncoding.EncodeToString(avcc[4:size])) case NALUTypeSPS: buf.WriteString(";sprop-sps=") buf.WriteString(base64.StdEncoding.EncodeToString(avcc[4:size])) case NALUTypePPS: buf.WriteString(";sprop-pps=") buf.WriteString(base64.StdEncoding.EncodeToString(avcc[4:size])) } if size < len(avcc) { avcc = avcc[size:] } else { break } } return &core.Codec{ Name: core.CodecH265, ClockRate: 90000, FmtpLine: buf.String(), PayloadType: core.PayloadTypeRAW, } } ================================================ FILE: pkg/h265/h265_test.go ================================================ package h265 import ( "encoding/base64" "testing" "github.com/stretchr/testify/require" ) func TestDecodeSPS(t *testing.T) { s := "QgEBAWAAAAMAAAMAAAMAAAMAmaAAoAgBaH+KrTuiS7/8AAQABbAgApMuADN/mAE=" b, err := base64.StdEncoding.DecodeString(s) require.Nil(t, err) sps := DecodeSPS(b) require.NotNil(t, sps) require.Equal(t, uint16(5120), sps.Width()) require.Equal(t, uint16(1440), sps.Height()) } func TestDecodeSPS2(t *testing.T) { s := "QgEBIUAAAAMAkAAAAwAAAwCWoAUCAWlnpbkShc1AQIC4QAAAAwBAAAAFFEn/eEAOpgAV+V8IBBA=" b, err := base64.StdEncoding.DecodeString(s) require.Nil(t, err) sps := DecodeSPS(b) require.NotNil(t, sps) require.Equal(t, uint16(640), sps.Width()) require.Equal(t, uint16(360), sps.Height()) } ================================================ FILE: pkg/h265/helper.go ================================================ package h265 import ( "encoding/base64" "encoding/binary" "github.com/AlexxIT/go2rtc/pkg/core" ) const ( NALUTypePFrame = 1 NALUTypeIFrame = 19 NALUTypeIFrame2 = 20 NALUTypeIFrame3 = 21 NALUTypeVPS = 32 NALUTypeSPS = 33 NALUTypePPS = 34 NALUTypePrefixSEI = 39 NALUTypeSuffixSEI = 40 NALUTypeFU = 49 ) func NALUType(b []byte) byte { return (b[4] >> 1) & 0x3F } func IsKeyframe(b []byte) bool { for { switch NALUType(b) { case NALUTypePFrame: return false case NALUTypeIFrame, NALUTypeIFrame2, NALUTypeIFrame3: return true } size := int(binary.BigEndian.Uint32(b)) + 4 if size < len(b) { b = b[size:] continue } else { return false } } } func Types(data []byte) []byte { var types []byte for { types = append(types, NALUType(data)) size := 4 + int(binary.BigEndian.Uint32(data)) if size < len(data) { data = data[size:] } else { break } } return types } func GetParameterSet(fmtp string) (vps, sps, pps []byte) { if fmtp == "" { return } s := core.Between(fmtp, "sprop-vps=", ";") vps, _ = base64.StdEncoding.DecodeString(s) s = core.Between(fmtp, "sprop-sps=", ";") sps, _ = base64.StdEncoding.DecodeString(s) s = core.Between(fmtp, "sprop-pps=", ";") pps, _ = base64.StdEncoding.DecodeString(s) return } ================================================ FILE: pkg/h265/mpeg4.go ================================================ // Package h265 - MPEG4 format related functions package h265 import ( "bytes" "encoding/base64" "encoding/binary" "github.com/AlexxIT/go2rtc/pkg/core" ) func DecodeConfig(conf []byte) (profile, vps, sps, pps []byte) { profile = conf[1:4] b := conf[23:] if binary.BigEndian.Uint16(b[1:]) != 1 { return } vpsSize := binary.BigEndian.Uint16(b[3:]) vps = b[5 : 5+vpsSize] b = conf[23+5+vpsSize:] if binary.BigEndian.Uint16(b[1:]) != 1 { return } spsSize := binary.BigEndian.Uint16(b[3:]) sps = b[5 : 5+spsSize] b = conf[23+5+vpsSize+5+spsSize:] if binary.BigEndian.Uint16(b[1:]) != 1 { return } ppsSize := binary.BigEndian.Uint16(b[3:]) pps = b[5 : 5+ppsSize] return } func EncodeConfig(vps, sps, pps []byte) []byte { vpsSize := uint16(len(vps)) spsSize := uint16(len(sps)) ppsSize := uint16(len(pps)) buf := make([]byte, 23+5+vpsSize+5+spsSize+5+ppsSize) buf[0] = 1 copy(buf[1:], sps[3:6]) // profile buf[21] = 3 // ? buf[22] = 3 // ? b := buf[23:] _ = b[5] b[0] = (vps[0] >> 1) & 0x3F binary.BigEndian.PutUint16(b[1:], 1) // VPS count binary.BigEndian.PutUint16(b[3:], vpsSize) copy(b[5:], vps) b = buf[23+5+vpsSize:] _ = b[5] b[0] = (sps[0] >> 1) & 0x3F binary.BigEndian.PutUint16(b[1:], 1) // SPS count binary.BigEndian.PutUint16(b[3:], spsSize) copy(b[5:], sps) b = buf[23+5+vpsSize+5+spsSize:] _ = b[5] b[0] = (pps[0] >> 1) & 0x3F binary.BigEndian.PutUint16(b[1:], 1) // PPS count binary.BigEndian.PutUint16(b[3:], ppsSize) copy(b[5:], pps) return buf } func ConfigToCodec(conf []byte) *core.Codec { buf := bytes.NewBufferString("profile-id=1") _, vps, sps, pps := DecodeConfig(conf) if vps != nil { buf.WriteString(";sprop-vps=") buf.WriteString(base64.StdEncoding.EncodeToString(vps)) } if sps != nil { buf.WriteString(";sprop-sps=") buf.WriteString(base64.StdEncoding.EncodeToString(sps)) } if pps != nil { buf.WriteString(";sprop-pps=") buf.WriteString(base64.StdEncoding.EncodeToString(pps)) } return &core.Codec{ Name: core.CodecH265, ClockRate: 90000, FmtpLine: buf.String(), PayloadType: core.PayloadTypeRAW, } } ================================================ FILE: pkg/h265/payloader.go ================================================ package h265 import ( "encoding/binary" "math" "github.com/AlexxIT/go2rtc/pkg/h264" ) // // Network Abstraction Unit Header implementation // const ( // sizeof(uint16) h265NaluHeaderSize = 2 // https://datatracker.ietf.org/doc/html/rfc7798#section-4.4.2 h265NaluAggregationPacketType = 48 // https://datatracker.ietf.org/doc/html/rfc7798#section-4.4.3 h265NaluFragmentationUnitType = 49 // https://datatracker.ietf.org/doc/html/rfc7798#section-4.4.4 h265NaluPACIPacketType = 50 ) // H265NALUHeader is a H265 NAL Unit Header // https://datatracker.ietf.org/doc/html/rfc7798#section-1.1.4 // +---------------+---------------+ // // |0|1|2|3|4|5|6|7|0|1|2|3|4|5|6|7| // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+ // |F| Type | LayerID | TID | // +-------------+-----------------+ type H265NALUHeader uint16 func newH265NALUHeader(highByte, lowByte uint8) H265NALUHeader { return H265NALUHeader((uint16(highByte) << 8) | uint16(lowByte)) } // F is the forbidden bit, should always be 0. func (h H265NALUHeader) F() bool { return (uint16(h) >> 15) != 0 } // Type of NAL Unit. func (h H265NALUHeader) Type() uint8 { // 01111110 00000000 const mask = 0b01111110 << 8 return uint8((uint16(h) & mask) >> (8 + 1)) } // IsTypeVCLUnit returns whether or not the NAL Unit type is a VCL NAL unit. func (h H265NALUHeader) IsTypeVCLUnit() bool { // Type is coded on 6 bits const msbMask = 0b00100000 return (h.Type() & msbMask) == 0 } // LayerID should always be 0 in non-3D HEVC context. func (h H265NALUHeader) LayerID() uint8 { // 00000001 11111000 const mask = (0b00000001 << 8) | 0b11111000 return uint8((uint16(h) & mask) >> 3) } // TID is the temporal identifier of the NAL unit +1. func (h H265NALUHeader) TID() uint8 { const mask = 0b00000111 return uint8(uint16(h) & mask) } // IsAggregationPacket returns whether or not the packet is an Aggregation packet. func (h H265NALUHeader) IsAggregationPacket() bool { return h.Type() == h265NaluAggregationPacketType } // IsFragmentationUnit returns whether or not the packet is a Fragmentation Unit packet. func (h H265NALUHeader) IsFragmentationUnit() bool { return h.Type() == h265NaluFragmentationUnitType } // IsPACIPacket returns whether or not the packet is a PACI packet. func (h H265NALUHeader) IsPACIPacket() bool { return h.Type() == h265NaluPACIPacketType } // // Fragmentation Unit implementation // const ( // sizeof(uint8) h265FragmentationUnitHeaderSize = 1 ) // H265FragmentationUnitHeader is a H265 FU Header // +---------------+ // |0|1|2|3|4|5|6|7| // +-+-+-+-+-+-+-+-+ // |S|E| FuType | // +---------------+ type H265FragmentationUnitHeader uint8 // S represents the start of a fragmented NAL unit. func (h H265FragmentationUnitHeader) S() bool { const mask = 0b10000000 return ((h & mask) >> 7) != 0 } // E represents the end of a fragmented NAL unit. func (h H265FragmentationUnitHeader) E() bool { const mask = 0b01000000 return ((h & mask) >> 6) != 0 } // FuType MUST be equal to the field Type of the fragmented NAL unit. func (h H265FragmentationUnitHeader) FuType() uint8 { const mask = 0b00111111 return uint8(h) & mask } // Payloader payloads H265 packets type Payloader struct { AddDONL bool SkipAggregation bool donl uint16 } // Payload fragments a H265 packet across one or more byte arrays func (p *Payloader) Payload(mtu uint16, payload []byte) [][]byte { var payloads [][]byte if len(payload) == 0 { return payloads } bufferedNALUs := make([][]byte, 0) aggregationBufferSize := 0 flushBufferedNals := func() { if len(bufferedNALUs) == 0 { return } if len(bufferedNALUs) == 1 { // emit this as a single NALU packet nalu := bufferedNALUs[0] if p.AddDONL { buf := make([]byte, len(nalu)+2) // copy the NALU header to the payload header copy(buf[0:h265NaluHeaderSize], nalu[0:h265NaluHeaderSize]) // copy the DONL into the header binary.BigEndian.PutUint16(buf[h265NaluHeaderSize:h265NaluHeaderSize+2], p.donl) // write the payload copy(buf[h265NaluHeaderSize+2:], nalu[h265NaluHeaderSize:]) p.donl++ payloads = append(payloads, buf) } else { // write the nalu directly to the payload payloads = append(payloads, nalu) } } else { // construct an aggregation packet aggregationPacketSize := aggregationBufferSize + 2 buf := make([]byte, aggregationPacketSize) layerID := uint8(math.MaxUint8) tid := uint8(math.MaxUint8) for _, nalu := range bufferedNALUs { header := newH265NALUHeader(nalu[0], nalu[1]) headerLayerID := header.LayerID() headerTID := header.TID() if headerLayerID < layerID { layerID = headerLayerID } if headerTID < tid { tid = headerTID } } binary.BigEndian.PutUint16(buf[0:2], (uint16(h265NaluAggregationPacketType)<<9)|(uint16(layerID)<<3)|uint16(tid)) index := 2 for i, nalu := range bufferedNALUs { if p.AddDONL { if i == 0 { binary.BigEndian.PutUint16(buf[index:index+2], p.donl) index += 2 } else { buf[index] = byte(i - 1) index++ } } binary.BigEndian.PutUint16(buf[index:index+2], uint16(len(nalu))) index += 2 index += copy(buf[index:], nalu) } payloads = append(payloads, buf) } // clear the buffered NALUs bufferedNALUs = make([][]byte, 0) aggregationBufferSize = 0 } h264.EmitNalus(payload, true, func(nalu []byte) { if len(nalu) == 0 { return } if len(nalu) <= int(mtu) { // this nalu fits into a single packet, either it can be emitted as // a single nalu or appended to the previous aggregation packet marginalAggregationSize := len(nalu) + 2 if p.AddDONL { marginalAggregationSize += 1 } if aggregationBufferSize+marginalAggregationSize > int(mtu) { flushBufferedNals() } bufferedNALUs = append(bufferedNALUs, nalu) aggregationBufferSize += marginalAggregationSize if p.SkipAggregation { // emit this immediately. flushBufferedNals() } } else { // if this nalu doesn't fit in the current mtu, it needs to be fragmented fuPacketHeaderSize := h265FragmentationUnitHeaderSize + 2 /* payload header size */ if p.AddDONL { fuPacketHeaderSize += 2 } // then, fragment the nalu maxFUPayloadSize := int(mtu) - fuPacketHeaderSize naluHeader := newH265NALUHeader(nalu[0], nalu[1]) // the nalu header is omitted from the fragmentation packet payload nalu = nalu[h265NaluHeaderSize:] if maxFUPayloadSize == 0 || len(nalu) == 0 { return } // flush any buffered aggregation packets. flushBufferedNals() fullNALUSize := len(nalu) for len(nalu) > 0 { curentFUPayloadSize := len(nalu) if curentFUPayloadSize > maxFUPayloadSize { curentFUPayloadSize = maxFUPayloadSize } out := make([]byte, fuPacketHeaderSize+curentFUPayloadSize) // write the payload header binary.BigEndian.PutUint16(out[0:2], uint16(naluHeader)) out[0] = (out[0] & 0b10000001) | h265NaluFragmentationUnitType<<1 // write the fragment header out[2] = byte(H265FragmentationUnitHeader(naluHeader.Type())) if len(nalu) == fullNALUSize { // Set start bit out[2] |= 1 << 7 } else if len(nalu)-curentFUPayloadSize == 0 { // Set end bit out[2] |= 1 << 6 } if p.AddDONL { // write the DONL header binary.BigEndian.PutUint16(out[3:5], p.donl) p.donl++ // copy the fragment payload copy(out[5:], nalu[0:curentFUPayloadSize]) } else { // copy the fragment payload copy(out[3:], nalu[0:curentFUPayloadSize]) } // append the fragment to the payload payloads = append(payloads, out) // advance the nalu data pointer nalu = nalu[curentFUPayloadSize:] } } }) flushBufferedNals() return payloads } ================================================ FILE: pkg/h265/rtp.go ================================================ package h265 import ( "encoding/binary" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/pion/rtp" ) func RTPDepay(codec *core.Codec, handler core.HandlerFunc) core.HandlerFunc { vps, sps, pps := GetParameterSet(codec.FmtpLine) ps := h264.JoinNALU(vps, sps, pps) buf := make([]byte, 0, 512*1024) // 512K var nuStart int var seqNum uint16 return func(packet *rtp.Packet) { data := packet.Payload if len(data) < 3 { return } nuType := (data[0] >> 1) & 0x3F //log.Printf("[RTP] codec: %s, nalu: %2d, size: %6d, ts: %10d, pt: %2d, ssrc: %d, seq: %d, %v", track.Codec.Name, nuType, len(packet.Payload), packet.Timestamp, packet.PayloadType, packet.SSRC, packet.SequenceNumber, packet.Marker) // Fix for RtspServer https://github.com/AlexxIT/go2rtc/issues/244 if packet.Marker && len(data) < h264.PSMaxSize { switch nuType { case NALUTypeVPS, NALUTypeSPS, NALUTypePPS: packet.Marker = false case NALUTypePrefixSEI, NALUTypeSuffixSEI: return } } // when we collect data into one buffer, we need to make sure // that all of it falls into the same sequence if len(buf) > 0 && packet.SequenceNumber-seqNum != 1 { //log.Printf("broken H265 sequence") buf = buf[:0] // drop data return } seqNum = packet.SequenceNumber if nuType == NALUTypeFU { switch data[2] >> 6 { case 0b10: // begin nuType = data[2] & 0x3F // push PS data before keyframe if len(buf) == 0 && nuType >= 19 && nuType <= 21 { buf = append(buf, ps...) } nuStart = len(buf) buf = append(buf, 0, 0, 0, 0) // NAL unit size buf = append(buf, (data[0]&0x81)|(nuType<<1), data[1]) buf = append(buf, data[3:]...) return case 0b00: // continue if len(buf) == 0 { //log.Printf("broken H265 fragment") return } buf = append(buf, data[3:]...) return case 0b01: // end if len(buf) == 0 { //log.Printf("broken H265 fragment") return } buf = append(buf, data[3:]...) if nuStart > len(buf)+4 { //log.Printf("broken H265 fragment") buf = buf[:0] // drop data return } binary.BigEndian.PutUint32(buf[nuStart:], uint32(len(buf)-nuStart-4)) case 0b11: // wrong RFC 7798 realisation from OpenIPC project // A non-fragmented NAL unit MUST NOT be transmitted in one FU; i.e., // the Start bit and End bit must not both be set to 1 in the same FU // header. nuType = data[2] & 0x3F buf = binary.BigEndian.AppendUint32(buf, uint32(len(data))-1) // NAL unit size buf = append(buf, (data[0]&0x81)|(nuType<<1), data[1]) buf = append(buf, data[3:]...) } } else { buf = binary.BigEndian.AppendUint32(buf, uint32(len(data))) // NAL unit size buf = append(buf, data...) } // collect all NAL Units for Access Unit if !packet.Marker { return } //log.Printf("[HEVC] %v, len: %d", Types(buf), len(buf)) clone := *packet clone.Version = h264.RTPPacketVersionAVC clone.Payload = buf buf = buf[:0] handler(&clone) } } func RTPPay(mtu uint16, handler core.HandlerFunc) core.HandlerFunc { if mtu == 0 { mtu = 1472 } payloader := &Payloader{} sequencer := rtp.NewRandomSequencer() mtu -= 12 // rtp.Header size return func(packet *rtp.Packet) { if packet.Version != h264.RTPPacketVersionAVC { handler(packet) return } payloads := payloader.Payload(mtu, packet.Payload) last := len(payloads) - 1 for i, payload := range payloads { clone := rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: i == last, SequenceNumber: sequencer.NextSequenceNumber(), Timestamp: packet.Timestamp, }, Payload: payload, } handler(&clone) } } } // SafariPay - generate Safari friendly payload for H265 // https://github.com/AlexxIT/Blog/issues/5 func SafariPay(mtu uint16, handler core.HandlerFunc) core.HandlerFunc { sequencer := rtp.NewRandomSequencer() size := int(mtu - 12) // rtp.Header size return func(packet *rtp.Packet) { if packet.Version != h264.RTPPacketVersionAVC { handler(packet) return } // protect original packets from modification au := make([]byte, len(packet.Payload)) copy(au, packet.Payload) var start byte for i := 0; i < len(au); { size := int(binary.BigEndian.Uint32(au[i:])) + 4 // convert AVC to Annex-B au[i] = 0 au[i+1] = 0 au[i+2] = 0 au[i+3] = 1 switch NALUType(au[i:]) { case NALUTypeIFrame, NALUTypeIFrame2, NALUTypeIFrame3: start = 3 default: if start == 0 { start = 2 } } i += size } // rtp.Packet payload b := make([]byte, 1, size) size-- // minus header byte for au != nil { b[0] = start if start > 1 { start -= 2 } if len(au) > size { b = append(b, au[:size]...) au = au[size:] } else { b = append(b, au...) au = nil } clone := rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: au == nil, SequenceNumber: sequencer.NextSequenceNumber(), Timestamp: packet.Timestamp, }, Payload: b, } handler(&clone) b = b[:1] // clear buffer } } } ================================================ FILE: pkg/h265/sps.go ================================================ package h265 import ( "bytes" "github.com/AlexxIT/go2rtc/pkg/bits" ) // http://www.itu.int/rec/T-REC-H.265 //goland:noinspection GoSnakeCaseUsage type SPS struct { sps_video_parameter_set_id uint8 sps_max_sub_layers_minus1 uint8 sps_temporal_id_nesting_flag byte general_profile_space uint8 general_tier_flag byte general_profile_idc uint8 general_profile_compatibility_flags uint32 general_level_idc uint8 sub_layer_profile_present_flag []byte sub_layer_level_present_flag []byte sps_seq_parameter_set_id uint32 chroma_format_idc uint32 separate_colour_plane_flag byte pic_width_in_luma_samples uint32 pic_height_in_luma_samples uint32 } func (s *SPS) Width() uint16 { return uint16(s.pic_width_in_luma_samples) } func (s *SPS) Height() uint16 { return uint16(s.pic_height_in_luma_samples) } func DecodeSPS(nalu []byte) *SPS { rbsp := bytes.ReplaceAll(nalu[2:], []byte{0, 0, 3}, []byte{0, 0}) r := bits.NewReader(rbsp) s := &SPS{} s.sps_video_parameter_set_id = r.ReadBits8(4) s.sps_max_sub_layers_minus1 = r.ReadBits8(3) s.sps_temporal_id_nesting_flag = r.ReadBit() if !s.profile_tier_level(r) { return nil } s.sps_seq_parameter_set_id = r.ReadUEGolomb() s.chroma_format_idc = r.ReadUEGolomb() if s.chroma_format_idc == 3 { s.separate_colour_plane_flag = r.ReadBit() } s.pic_width_in_luma_samples = r.ReadUEGolomb() s.pic_height_in_luma_samples = r.ReadUEGolomb() //... if r.EOF { return nil } return s } // profile_tier_level supports ONLY general_profile_idc == 1 // over variants very complicated... // //goland:noinspection GoSnakeCaseUsage func (s *SPS) profile_tier_level(r *bits.Reader) bool { s.general_profile_space = r.ReadBits8(2) s.general_tier_flag = r.ReadBit() s.general_profile_idc = r.ReadBits8(5) s.general_profile_compatibility_flags = r.ReadBits(32) _ = r.ReadBits64(48) // other flags if s.general_profile_idc != 1 { return false } s.general_level_idc = r.ReadBits8(8) s.sub_layer_profile_present_flag = make([]byte, s.sps_max_sub_layers_minus1) s.sub_layer_level_present_flag = make([]byte, s.sps_max_sub_layers_minus1) for i := byte(0); i < s.sps_max_sub_layers_minus1; i++ { s.sub_layer_profile_present_flag[i] = r.ReadBit() s.sub_layer_level_present_flag[i] = r.ReadBit() } if s.sps_max_sub_layers_minus1 > 0 { for i := s.sps_max_sub_layers_minus1; i < 8; i++ { _ = r.ReadBits8(2) // reserved_zero_2bits } } for i := byte(0); i < s.sps_max_sub_layers_minus1; i++ { if s.sub_layer_profile_present_flag[i] != 0 { _ = r.ReadBits8(2) // sub_layer_profile_space _ = r.ReadBit() // sub_layer_tier_flag sub_layer_profile_idc := r.ReadBits8(5) // sub_layer_profile_idc _ = r.ReadBits(32) // sub_layer_profile_compatibility_flag _ = r.ReadBits64(48) // other flags if sub_layer_profile_idc != 1 { return false } } if s.sub_layer_level_present_flag[i] != 0 { _ = r.ReadBits8(8) } } return true } ================================================ FILE: pkg/hap/README.md ================================================ # Home Accessory Protocol > PS. Character = Characteristic **Device** - HomeKit end device (swith, camera, etc) - mDNS name: `MyCamera._hap._tcp.local.` - DeviceID - mac-like: `0E:AA:CE:2B:35:71` - HomeKit device is described by: - one or more `Accessories` - has `AID` and `Services` - `Services` - has `IID`, `Type` and `Characters` - `Characters` - has `IID`, `Type`, `Format` and `Value` **Client** - HomeKit client (iPhone, iPad, MacBook or opensource library) - ClientID - static random UUID - ClientPublic/ClientPrivate - static random 32 byte keypair - can pair with Device (exchange ClientID/ClientPublic, ServerID/ServerPublic using Pin) - can auth to Device using ClientPrivate - holding persistant Secure connection to device - can read device Accessories - can read and write device Characters - can subscribe on device Characters change (Event) **Server** - HomeKit server (soft on end device or opensource library) - ServerID - same as DeviceID (using for Client auth) - ServerPublic/ServerPrivate - static random 32 byte keypair ## AAC ELD Requires ffmpeg built with `--enable-libfdk-aac` ``` -acodec libfdk_aac -aprofile aac_eld ``` | SampleRate | RTPTime | constantDuration | objectType | |------------|---------|--------------------|--------------| | 8000 | 60 | =8000/1000*60=480 | 39 (AAC ELD) | | 16000 | 30 | =16000/1000*30=480 | 39 (AAC ELD) | | 24000 | 20 | =24000/1000*20=480 | 39 (AAC ELD) | | 16000 | 60 | =16000/1000*60=960 | 23 (AAC LD) | | 24000 | 40 | =24000/1000*40=960 | 23 (AAC LD) | ## Useful links - https://github.com/apple/HomeKitADK/blob/master/Documentation/crypto.md - https://github.com/apple/HomeKitADK/blob/master/HAP/HAPPairingPairSetup.c - [Extracting HomeKit Pairing Keys](https://pvieito.com/2019/12/extract-homekit-pairing-keys) - [HAP in AirPlay2 receiver](https://github.com/openairplay/airplay2-receiver/blob/master/ap2/pairing/hap.py) - [HomeKit Secure Video Unofficial Specification](https://github.com/Supereg/secure-video-specification) - [Homebridge Camera FFmpeg](https://sunoo.github.io/homebridge-camera-ffmpeg/configs/) - https://github.com/ljezny/Particle-HAP/blob/master/HAP-Specification-Non-Commercial-Version.pdf ================================================ FILE: pkg/hap/accessory.go ================================================ package hap import ( "fmt" "strconv" ) const ( FormatString = "string" FormatBool = "bool" FormatFloat = "float" FormatUInt8 = "uint8" FormatUInt16 = "uint16" FormatUInt32 = "uint32" FormatInt32 = "int32" FormatUInt64 = "uint64" FormatData = "data" FormatTLV8 = "tlv8" UnitPercentage = "percentage" ) var PR = []string{"pr"} var PW = []string{"pw"} var PRPW = []string{"pr", "pw"} var EVPRPW = []string{"ev", "pr", "pw"} var EVPR = []string{"ev", "pr"} type Accessory struct { AID uint8 `json:"aid"` // 150 unique accessories per bridge Services []*Service `json:"services"` } func (a *Accessory) InitIID() { serviceN := map[string]byte{} for _, service := range a.Services { if len(service.Type) > 3 { panic(service.Type) } n := serviceN[service.Type] + 1 serviceN[service.Type] = n if n > 15 { panic(n) } // ServiceID = ANSSS000 s := fmt.Sprintf("%x%x%03s000", a.AID, n, service.Type) service.IID, _ = strconv.ParseUint(s, 16, 64) for _, character := range service.Characters { if len(character.Type) > 3 { panic(character.Type) } // CharacterID = ANSSSCCC character.IID, _ = strconv.ParseUint(character.Type, 16, 64) character.IID += service.IID } } } func (a *Accessory) GetService(servType string) *Service { for _, serv := range a.Services { if serv.Type == servType { return serv } } return nil } func (a *Accessory) GetCharacter(charType string) *Character { for _, serv := range a.Services { for _, char := range serv.Characters { if char.Type == charType { return char } } } return nil } func (a *Accessory) GetCharacterByID(iid uint64) *Character { for _, serv := range a.Services { for _, char := range serv.Characters { if char.IID == iid { return char } } } return nil } type Service struct { Desc string `json:"description,omitempty"` Type string `json:"type"` IID uint64 `json:"iid"` Primary bool `json:"primary,omitempty"` Characters []*Character `json:"characteristics"` Linked []int `json:"linked,omitempty"` } func (s *Service) GetCharacter(charType string) *Character { for _, char := range s.Characters { if char.Type == charType { return char } } return nil } func ServiceAccessoryInformation(manuf, model, name, serial, firmware string) *Service { return &Service{ Type: "3E", // AccessoryInformation Characters: []*Character{ { Type: "14", Format: FormatBool, Perms: PW, //Descr: "Identify", }, { Type: "20", Format: FormatString, Value: manuf, Perms: PR, //Descr: "Manufacturer", //MaxLen: 64, }, { Type: "21", Format: FormatString, Value: model, Perms: PR, //Descr: "Model", //MaxLen: 64, }, { Type: "23", Format: FormatString, Value: name, Perms: PR, //Descr: "Name", //MaxLen: 64, }, { Type: "30", Format: FormatString, Value: serial, Perms: PR, //Descr: "Serial Number", //MaxLen: 64, }, { Type: "52", Format: FormatString, Value: firmware, Perms: PR, //Descr: "Firmware Revision", }, }, } } func ServiceHAPProtocolInformation() *Service { return &Service{ Type: "A2", // 'HAPProtocolInformation' Characters: []*Character{ { Type: "37", Format: FormatString, Value: "1.1.0", Perms: PR, //Descr: "Version", //MaxLen: 64, }, }, } } ================================================ FILE: pkg/hap/camera/README.md ================================================ ## Useful links - https://github.com/bauer-andreas/secure-video-specification ================================================ FILE: pkg/hap/camera/accessory.go ================================================ package camera import ( "github.com/AlexxIT/go2rtc/pkg/hap" "github.com/AlexxIT/go2rtc/pkg/hap/tlv8" ) func NewAccessory(manuf, model, name, serial, firmware string) *hap.Accessory { acc := &hap.Accessory{ AID: hap.DeviceAID, Services: []*hap.Service{ hap.ServiceAccessoryInformation(manuf, model, name, serial, firmware), ServiceCameraRTPStreamManagement(), //hap.ServiceHAPProtocolInformation(), ServiceMicrophone(), }, } acc.InitIID() return acc } func ServiceMicrophone() *hap.Service { return &hap.Service{ Type: "112", // 'Microphone' Characters: []*hap.Character{ { Type: "11A", Format: hap.FormatBool, Value: 0, Perms: hap.EVPRPW, //Descr: "Mute", }, //{ // Type: "119", // Format: hap.FormatUInt8, // Value: 100, // Perms: hap.EVPRPW, // //Descr: "Volume", // //Unit: hap.UnitPercentage, // //MinValue: 0, // //MaxValue: 100, // //MinStep: 1, //}, }, } } func ServiceCameraRTPStreamManagement() *hap.Service { val120, _ := tlv8.MarshalBase64(StreamingStatus{ Status: StreamingStatusAvailable, }) val114, _ := tlv8.MarshalBase64(SupportedVideoStreamConfiguration{ Codecs: []VideoCodecConfiguration{ { CodecType: VideoCodecTypeH264, CodecParams: []VideoCodecParameters{ { ProfileID: []byte{VideoCodecProfileMain}, Level: []byte{VideoCodecLevel31, VideoCodecLevel40}, }, }, VideoAttrs: []VideoCodecAttributes{ {Width: 1920, Height: 1080, Framerate: 30}, {Width: 1280, Height: 720, Framerate: 30}, // important for iPhones {Width: 320, Height: 240, Framerate: 15}, // apple watch }, }, }, }) val115, _ := tlv8.MarshalBase64(SupportedAudioStreamConfiguration{ Codecs: []AudioCodecConfiguration{ { CodecType: AudioCodecTypeOpus, CodecParams: []AudioCodecParameters{ { Channels: 1, BitrateMode: AudioCodecBitrateVariable, SampleRate: []byte{AudioCodecSampleRate16Khz}, }, }, }, }, ComfortNoiseSupport: 0, }) val116, _ := tlv8.MarshalBase64(SupportedRTPConfiguration{ SRTPCryptoType: []byte{CryptoAES_CM_128_HMAC_SHA1_80}, }) service := &hap.Service{ Type: "110", // 'CameraRTPStreamManagement' Characters: []*hap.Character{ { Type: TypeStreamingStatus, Format: hap.FormatTLV8, Value: val120, Perms: hap.EVPR, //Descr: "Streaming Status", }, { Type: TypeSupportedVideoStreamConfiguration, Format: hap.FormatTLV8, Value: val114, Perms: hap.PR, //Descr: "Supported Video Stream Configuration", }, { Type: TypeSupportedAudioStreamConfiguration, Format: hap.FormatTLV8, Value: val115, Perms: hap.PR, //Descr: "Supported Audio Stream Configuration", }, { Type: TypeSupportedRTPConfiguration, Format: hap.FormatTLV8, Value: val116, Perms: hap.PR, //Descr: "Supported RTP Configuration", }, { Type: "B0", Format: hap.FormatUInt8, Value: 1, Perms: hap.EVPRPW, //Descr: "Active", //MinValue: 0, //MaxValue: 1, //MinStep: 1, //ValidVal: []any{0, 1}, }, { Type: TypeSelectedStreamConfiguration, Format: hap.FormatTLV8, Value: "", // important empty Perms: hap.PRPW, //Descr: "Selected RTP Stream Configuration", }, { Type: TypeSetupEndpoints, Format: hap.FormatTLV8, Value: "", // important empty Perms: hap.PRPW, //Descr: "Setup Endpoints", }, }, } return service } ================================================ FILE: pkg/hap/camera/accessory_test.go ================================================ package camera import ( "encoding/base64" "strings" "testing" "github.com/AlexxIT/go2rtc/pkg/hap" "github.com/stretchr/testify/require" ) func TestNilCharacter(t *testing.T) { var res SetupEndpoints char := &hap.Character{} err := char.ReadTLV8(&res) require.NotNil(t, err) require.NotNil(t, strings.Contains(err.Error(), "can't read value")) } type testTLV8 struct { name string value string actual any expect any noequal bool } func (test testTLV8) run(t *testing.T) { if test.actual == nil { return } src := &hap.Character{Value: test.value, Format: hap.FormatTLV8} err := src.ReadTLV8(test.actual) require.Nil(t, err) require.Equal(t, test.expect, test.actual) dst := &hap.Character{Format: hap.FormatTLV8} err = dst.Write(test.actual) require.Nil(t, err) a, _ := base64.StdEncoding.DecodeString(test.value) b, _ := base64.StdEncoding.DecodeString(dst.Value.(string)) t.Logf("%x\n", a) t.Logf("%x\n", b) if !test.noequal { require.Equal(t, test.value, dst.Value) } } func TestAqaraG3(t *testing.T) { tests := []testTLV8{ { name: "120", value: "AQEA", actual: &StreamingStatus{}, expect: &StreamingStatus{ Status: StreamingStatusAvailable, }, }, { name: "114", value: "AaoBAQACEQEBAQIBAAAAAgECAwEABAEAAwsBAoAHAgI4BAMBHgAAAwsBAgAFAgLQAgMBHgAAAwsBAoACAgJoAQMBHgAAAwsBAuABAgIOAQMBHgAAAwsBAkABAgK0AAMBHgAAAwsBAgAFAgLAAwMBHgAAAwsBAgAEAgIAAwMBHgAAAwsBAoACAgLgAQMBHgAAAwsBAuABAgJoAQMBHgAAAwsBAkABAgLwAAMBHg==", actual: &SupportedVideoStreamConfiguration{}, expect: &SupportedVideoStreamConfiguration{ Codecs: []VideoCodecConfiguration{ { CodecType: VideoCodecTypeH264, CodecParams: []VideoCodecParameters{ { ProfileID: []byte{VideoCodecProfileMain}, Level: []byte{VideoCodecLevel31, VideoCodecLevel40}, CVOEnabled: []byte{0}, }, }, VideoAttrs: []VideoCodecAttributes{ {Width: 1920, Height: 1080, Framerate: 30}, {Width: 1280, Height: 720, Framerate: 30}, {Width: 640, Height: 360, Framerate: 30}, {Width: 480, Height: 270, Framerate: 30}, {Width: 320, Height: 180, Framerate: 30}, {Width: 1280, Height: 960, Framerate: 30}, {Width: 1024, Height: 768, Framerate: 30}, {Width: 640, Height: 480, Framerate: 30}, {Width: 480, Height: 360, Framerate: 30}, {Width: 320, Height: 240, Framerate: 30}, }, }, }, }, }, { name: "115", value: "AQ4BAQICCQEBAQIBAAMBAQIBAA==", actual: &SupportedAudioStreamConfiguration{}, expect: &SupportedAudioStreamConfiguration{ Codecs: []AudioCodecConfiguration{ { CodecType: AudioCodecTypeAACELD, CodecParams: []AudioCodecParameters{ { Channels: 1, BitrateMode: AudioCodecBitrateVariable, SampleRate: []byte{AudioCodecSampleRate16Khz}, }, }, }, }, ComfortNoiseSupport: 0, }, }, { name: "116", value: "AgEAAAACAQEAAAIBAg==", actual: &SupportedRTPConfiguration{}, expect: &SupportedRTPConfiguration{ SRTPCryptoType: []byte{CryptoAES_CM_128_HMAC_SHA1_80, CryptoAES_CM_256_HMAC_SHA1_80, CryptoDisabled}, }, }, } for _, test := range tests { t.Run(test.name, test.run) } } func TestHomebridge(t *testing.T) { tests := []testTLV8{ { name: "114", value: "AcUBAQACHQEBAAAAAQEBAAABAQICAQAAAAIBAQAAAgECAwEAAwsBAkABAgK0AAMBHgAAAwsBAkABAgLwAAMBDwAAAwsBAkABAgLwAAMBHgAAAwsBAuABAgIOAQMBHgAAAwsBAuABAgJoAQMBHgAAAwsBAoACAgJoAQMBHgAAAwsBAoACAgLgAQMBHgAAAwsBAgAFAgLQAgMBHgAAAwsBAgAFAgLAAwMBHgAAAwsBAoAHAgI4BAMBHgAAAwsBAkAGAgKwBAMBHg==", actual: &SupportedVideoStreamConfiguration{}, expect: &SupportedVideoStreamConfiguration{ Codecs: []VideoCodecConfiguration{ { CodecType: VideoCodecTypeH264, CodecParams: []VideoCodecParameters{ { ProfileID: []byte{VideoCodecProfileConstrainedBaseline, VideoCodecProfileMain, VideoCodecProfileHigh}, Level: []byte{VideoCodecLevel31, VideoCodecLevel32, VideoCodecLevel40}, }, }, VideoAttrs: []VideoCodecAttributes{ {Width: 320, Height: 180, Framerate: 30}, {Width: 320, Height: 240, Framerate: 15}, {Width: 320, Height: 240, Framerate: 30}, {Width: 480, Height: 270, Framerate: 30}, {Width: 480, Height: 360, Framerate: 30}, {Width: 640, Height: 360, Framerate: 30}, {Width: 640, Height: 480, Framerate: 30}, {Width: 1280, Height: 720, Framerate: 30}, {Width: 1280, Height: 960, Framerate: 30}, {Width: 1920, Height: 1080, Framerate: 30}, {Width: 1600, Height: 1200, Framerate: 30}, }, }, }, }, }, { name: "116", value: "AgEA", actual: &SupportedRTPConfiguration{}, expect: &SupportedRTPConfiguration{ SRTPCryptoType: []byte{CryptoAES_CM_128_HMAC_SHA1_80}, }, }, } for _, test := range tests { t.Run(test.name, test.run) } } func TestScrypted(t *testing.T) { tests := []testTLV8{ { name: "114", value: "AVIBAQACEwEBAQIBAAAAAgEBAAACAQIDAQADCwECAA8CAnAIAwEeAAADCwECgAcCAjgEAwEeAAADCwECAAUCAtACAwEeAAADCwECQAECAvAAAwEP", actual: &SupportedVideoStreamConfiguration{}, expect: &SupportedVideoStreamConfiguration{ Codecs: []VideoCodecConfiguration{ { CodecType: VideoCodecTypeH264, CodecParams: []VideoCodecParameters{ { ProfileID: []byte{VideoCodecProfileMain}, Level: []byte{VideoCodecLevel31, VideoCodecLevel32, VideoCodecLevel40}, }, }, VideoAttrs: []VideoCodecAttributes{ {Width: 3840, Height: 2160, Framerate: 30}, {Width: 1920, Height: 1080, Framerate: 30}, {Width: 1280, Height: 720, Framerate: 30}, {Width: 320, Height: 240, Framerate: 15}, }, }, }, }, }, { name: "115", value: "AScBAQMCIgEBAQIBAAMBAAAAAwEAAAADAQEAAAMBAQAAAwECAAADAQICAQA=", actual: &SupportedAudioStreamConfiguration{}, expect: &SupportedAudioStreamConfiguration{ Codecs: []AudioCodecConfiguration{ { CodecType: AudioCodecTypeOpus, CodecParams: []AudioCodecParameters{ { Channels: 1, BitrateMode: AudioCodecBitrateVariable, SampleRate: []byte{ AudioCodecSampleRate8Khz, AudioCodecSampleRate8Khz, AudioCodecSampleRate16Khz, AudioCodecSampleRate16Khz, AudioCodecSampleRate24Khz, AudioCodecSampleRate24Khz, }, }, }, }, }, ComfortNoiseSupport: 0, }, }, { name: "116", value: "AgEAAAACAQI=", actual: &SupportedRTPConfiguration{}, expect: &SupportedRTPConfiguration{ SRTPCryptoType: []byte{CryptoAES_CM_128_HMAC_SHA1_80, CryptoDisabled}, }, }, } for _, test := range tests { t.Run(test.name, test.run) } } func TestHass(t *testing.T) { tests := []testTLV8{ { name: "114", value: "AdABAQACFQMBAAEBAAEBAQEBAgIBAAIBAQIBAgMMAQJAAQICtAADAg8AAwwBAkABAgLwAAMCDwADDAECQAECArQAAwIeAAMMAQJAAQIC8AADAh4AAwwBAuABAgIOAQMCHgADDAEC4AECAmgBAwIeAAMMAQKAAgICaAEDAh4AAwwBAoACAgLgAQMCHgADDAECAAQCAkACAwIeAAMMAQIABAICAAMDAh4AAwwBAgAFAgLQAgMCHgADDAECAAUCAsADAwIeAAMMAQKABwICOAQDAh4A", }, { name: "115", value: "AQ4BAQMCCQEBAQIBAAMBAgEOAQEDAgkBAQECAQADAQECAQA=", }, } for _, test := range tests { t.Run(test.name, test.run) } } ================================================ FILE: pkg/hap/camera/ch114_supported_video.go ================================================ package camera const TypeSupportedVideoStreamConfiguration = "114" type SupportedVideoStreamConfiguration struct { Codecs []VideoCodecConfiguration `tlv8:"1"` } type VideoCodecConfiguration struct { CodecType byte `tlv8:"1"` CodecParams []VideoCodecParameters `tlv8:"2"` VideoAttrs []VideoCodecAttributes `tlv8:"3"` RTPParams []RTPParams `tlv8:"4"` } //goland:noinspection ALL const ( VideoCodecTypeH264 = 0 VideoCodecProfileConstrainedBaseline = 0 VideoCodecProfileMain = 1 VideoCodecProfileHigh = 2 VideoCodecLevel31 = 0 VideoCodecLevel32 = 1 VideoCodecLevel40 = 2 VideoCodecPacketizationModeNonInterleaved = 0 VideoCodecCvoNotSuppported = 0 VideoCodecCvoSuppported = 1 ) type VideoCodecParameters struct { ProfileID []byte `tlv8:"1"` // 0 - baseline, 1 - main, 2 - high Level []byte `tlv8:"2"` // 0 - 3.1, 1 - 3.2, 2 - 4.0 PacketizationMode byte `tlv8:"3"` // only 0 - non interleaved CVOEnabled []byte `tlv8:"4"` // 0 - not supported, 1 - supported CVOID []byte `tlv8:"5"` // ID for CVO RTP extensio } type VideoCodecAttributes struct { Width uint16 `tlv8:"1"` Height uint16 `tlv8:"2"` Framerate uint8 `tlv8:"3"` } ================================================ FILE: pkg/hap/camera/ch115_supported_audio.go ================================================ package camera const TypeSupportedAudioStreamConfiguration = "115" type SupportedAudioStreamConfiguration struct { Codecs []AudioCodecConfiguration `tlv8:"1"` ComfortNoiseSupport byte `tlv8:"2"` } //goland:noinspection ALL const ( AudioCodecTypePCMU = 0 AudioCodecTypePCMA = 1 AudioCodecTypeAACELD = 2 AudioCodecTypeOpus = 3 AudioCodecTypeMSBC = 4 AudioCodecTypeAMR = 5 AudioCodecTypeARMWB = 6 AudioCodecBitrateVariable = 0 AudioCodecBitrateConstant = 1 AudioCodecSampleRate8Khz = 0 AudioCodecSampleRate16Khz = 1 AudioCodecSampleRate24Khz = 2 RTPTimeAACELD8 = 60 // 8000/1000*60=480 RTPTimeAACELD16 = 30 // 16000/1000*30=480 RTPTimeAACELD24 = 20 // 24000/1000*20=480 RTPTimeAACLD16 = 60 // 16000/1000*60=960 RTPTimeAACLD24 = 40 // 24000/1000*40=960 ) type AudioCodecConfiguration struct { CodecType byte `tlv8:"1"` CodecParams []AudioCodecParameters `tlv8:"2"` RTPParams []RTPParams `tlv8:"3"` ComfortNoise []byte `tlv8:"4"` } type AudioCodecParameters struct { Channels uint8 `tlv8:"1"` BitrateMode byte `tlv8:"2"` // 0 - variable, 1 - constant SampleRate []byte `tlv8:"3"` // 0 - 8000, 1 - 16000, 2 - 24000 RTPTime []uint8 `tlv8:"4"` // 20, 30, 40, 60 } ================================================ FILE: pkg/hap/camera/ch116_supported_rtp.go ================================================ package camera const TypeSupportedRTPConfiguration = "116" //goland:noinspection ALL const ( CryptoAES_CM_128_HMAC_SHA1_80 = 0 CryptoAES_CM_256_HMAC_SHA1_80 = 1 CryptoDisabled = 2 ) type SupportedRTPConfiguration struct { SRTPCryptoType []byte `tlv8:"2"` } ================================================ FILE: pkg/hap/camera/ch117_selected_stream.go ================================================ package camera const TypeSelectedStreamConfiguration = "117" type SelectedStreamConfiguration struct { Control SessionControl `tlv8:"1"` VideoCodec VideoCodecConfiguration `tlv8:"2"` AudioCodec AudioCodecConfiguration `tlv8:"3"` } //goland:noinspection ALL const ( SessionCommandEnd = 0 SessionCommandStart = 1 SessionCommandSuspend = 2 SessionCommandResume = 3 SessionCommandReconfigure = 4 ) type SessionControl struct { SessionID string `tlv8:"1"` Command byte `tlv8:"2"` } type RTPParams struct { PayloadType uint8 `tlv8:"1"` SSRC uint32 `tlv8:"2"` MaxBitrate uint16 `tlv8:"3"` RTCPInterval float32 `tlv8:"4"` MaxMTU []uint16 `tlv8:"5"` ComfortNoisePayloadType []uint8 `tlv8:"6"` } ================================================ FILE: pkg/hap/camera/ch118_setup_endpoints.go ================================================ package camera const TypeSetupEndpoints = "118" type SetupEndpointsRequest struct { SessionID string `tlv8:"1"` Address Address `tlv8:"3"` VideoCrypto SRTPCryptoSuite `tlv8:"4"` AudioCrypto SRTPCryptoSuite `tlv8:"5"` } type SetupEndpointsResponse struct { SessionID string `tlv8:"1"` Status byte `tlv8:"2"` Address Address `tlv8:"3"` VideoCrypto SRTPCryptoSuite `tlv8:"4"` AudioCrypto SRTPCryptoSuite `tlv8:"5"` VideoSSRC uint32 `tlv8:"6"` AudioSSRC uint32 `tlv8:"7"` } type Address struct { IPVersion byte `tlv8:"1"` IPAddr string `tlv8:"2"` VideoRTPPort uint16 `tlv8:"3"` AudioRTPPort uint16 `tlv8:"4"` } type SRTPCryptoSuite struct { CryptoSuite byte `tlv8:"1"` MasterKey string `tlv8:"2"` // 16 (AES_CM_128) or 32 (AES_256_CM) MasterSalt string `tlv8:"3"` // 14 byte } ================================================ FILE: pkg/hap/camera/ch120_streaming_status.go ================================================ package camera const TypeStreamingStatus = "120" type StreamingStatus struct { Status byte `tlv8:"1"` } //goland:noinspection ALL const ( StreamingStatusAvailable = 0 StreamingStatusInUse = 1 StreamingStatusUnavailable = 2 ) ================================================ FILE: pkg/hap/camera/ch130_data_stream_transport.go ================================================ package camera const TypeSupportedDataStreamTransportConfiguration = "130" type SupportedDataStreamTransportConfiguration struct { Configs []TransferTransportConfiguration `tlv8:"1"` } type TransferTransportConfiguration struct { TransportType byte `tlv8:"1"` } ================================================ FILE: pkg/hap/camera/ch131_data_stream.go ================================================ package camera const TypeSetupDataStreamTransport = "131" type SetupDataStreamTransportRequest struct { SessionCommandType byte `tlv8:"1"` TransportType byte `tlv8:"2"` ControllerKeySalt string `tlv8:"3"` } type SetupDataStreamTransportResponse struct { Status byte `tlv8:"1"` TransportTypeSessionParameters struct { TCPListeningPort uint16 `tlv8:"1"` } `tlv8:"2"` AccessoryKeySalt string `tlv8:"3"` } ================================================ FILE: pkg/hap/camera/ch205.go ================================================ package camera const TypeSupportedCameraRecordingConfiguration = "205" type SupportedCameraRecordingConfiguration struct { PrebufferLength uint32 `tlv8:"1"` EventTriggerOptions uint64 `tlv8:"2"` MediaContainerConfigurations `tlv8:"3"` } type MediaContainerConfigurations struct { MediaContainerType uint8 `tlv8:"1"` MediaContainerParameters `tlv8:"2"` } type MediaContainerParameters struct { FragmentLength uint32 `tlv8:"1"` } ================================================ FILE: pkg/hap/camera/ch206.go ================================================ package camera const TypeSupportedVideoRecordingConfiguration = "206" type SupportedVideoRecordingConfiguration struct { CodecConfigs []VideoRecordingCodecConfiguration `tlv8:"1"` } type VideoRecordingCodecConfiguration struct { CodecType uint8 `tlv8:"1"` CodecParams VideoRecordingCodecParameters `tlv8:"2"` CodecAttrs VideoCodecAttributes `tlv8:"3"` } type VideoRecordingCodecParameters struct { ProfileID uint8 `tlv8:"1"` Level uint8 `tlv8:"2"` Bitrate uint32 `tlv8:"3"` IFrameInterval uint32 `tlv8:"4"` } ================================================ FILE: pkg/hap/camera/ch207.go ================================================ package camera const TypeSupportedAudioRecordingConfiguration = "207" type SupportedAudioRecordingConfiguration struct { CodecConfigs []AudioRecordingCodecConfiguration `tlv8:"1"` } type AudioRecordingCodecConfiguration struct { CodecType byte `tlv8:"1"` CodecParams []AudioRecordingCodecParameters `tlv8:"2"` } type AudioRecordingCodecParameters struct { Channels uint8 `tlv8:"1"` BitrateMode []byte `tlv8:"2"` SampleRate []byte `tlv8:"3"` MaxAudioBitrate []uint32 `tlv8:"4"` } ================================================ FILE: pkg/hap/camera/ch209.go ================================================ package camera const TypeSelectedCameraRecordingConfiguration = "209" type SelectedCameraRecordingConfiguration struct { GeneralConfig SupportedCameraRecordingConfiguration `tlv8:"1"` VideoConfig SupportedVideoRecordingConfiguration `tlv8:"2"` AudioConfig SupportedAudioRecordingConfiguration `tlv8:"3"` } ================================================ FILE: pkg/hap/camera/stream.go ================================================ package camera import ( "errors" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/hap" "github.com/AlexxIT/go2rtc/pkg/srtp" ) type Stream struct { id string client *hap.Client service *hap.Service } func NewStream( client *hap.Client, videoCodec *VideoCodecConfiguration, audioCodec *AudioCodecConfiguration, videoSession, audioSession *srtp.Session, bitrate int, ) (*Stream, error) { stream := &Stream{ id: core.RandString(16, 0), client: client, } if err := stream.GetFreeStream(); err != nil { return nil, err } if err := stream.ExchangeEndpoints(videoSession, audioSession); err != nil { return nil, err } if bitrate != 0 { bitrate /= 1024 // convert bps to kbps } else { bitrate = 4096 // default kbps for general FullHD camera } videoCodec.RTPParams = []RTPParams{ { PayloadType: 99, SSRC: videoSession.Local.SSRC, MaxBitrate: uint16(bitrate), // iPhone query 299Kbps, iPad/AppleTV query 802Kbps RTCPInterval: 0.5, MaxMTU: []uint16{1378}, }, } audioCodec.RTPParams = []RTPParams{ { PayloadType: 110, SSRC: audioSession.Local.SSRC, MaxBitrate: 24, // any iDevice query 24Kbps (this is OK for 16KHz and 1 channel) RTCPInterval: 5, ComfortNoisePayloadType: []uint8{13}, }, } audioCodec.ComfortNoise = []byte{0} config := &SelectedStreamConfiguration{ Control: SessionControl{ SessionID: stream.id, Command: SessionCommandStart, }, VideoCodec: *videoCodec, AudioCodec: *audioCodec, } if err := stream.SetStreamConfig(config); err != nil { return nil, err } return stream, nil } // GetFreeStream search free streaming service. // Usual every HomeKit camera can stream only to two clients simultaniosly. // So it has two similar services for streaming. func (s *Stream) GetFreeStream() error { acc, err := s.client.GetFirstAccessory() if err != nil { return err } for _, srv := range acc.Services { for _, char := range srv.Characters { if char.Type == TypeStreamingStatus { var status StreamingStatus if err = char.ReadTLV8(&status); err != nil { return err } if status.Status == StreamingStatusAvailable { s.service = srv return nil } } } } return errors.New("hap: no free streams") } func (s *Stream) ExchangeEndpoints(videoSession, audioSession *srtp.Session) error { req := SetupEndpointsRequest{ SessionID: s.id, Address: Address{ IPVersion: 0, IPAddr: videoSession.Local.Addr, VideoRTPPort: videoSession.Local.Port, AudioRTPPort: audioSession.Local.Port, }, VideoCrypto: SRTPCryptoSuite{ MasterKey: string(videoSession.Local.MasterKey), MasterSalt: string(videoSession.Local.MasterSalt), }, AudioCrypto: SRTPCryptoSuite{ MasterKey: string(audioSession.Local.MasterKey), MasterSalt: string(audioSession.Local.MasterSalt), }, } char := s.service.GetCharacter(TypeSetupEndpoints) if err := char.Write(&req); err != nil { return err } if err := s.client.PutCharacters(char); err != nil { return err } var res SetupEndpointsResponse if err := s.client.GetCharacter(char); err != nil { return err } if err := char.ReadTLV8(&res); err != nil { return err } videoSession.Remote = &srtp.Endpoint{ Addr: res.Address.IPAddr, Port: res.Address.VideoRTPPort, MasterKey: []byte(res.VideoCrypto.MasterKey), MasterSalt: []byte(res.VideoCrypto.MasterSalt), SSRC: res.VideoSSRC, } audioSession.Remote = &srtp.Endpoint{ Addr: res.Address.IPAddr, Port: res.Address.AudioRTPPort, MasterKey: []byte(res.AudioCrypto.MasterKey), MasterSalt: []byte(res.AudioCrypto.MasterSalt), SSRC: res.AudioSSRC, } return nil } func (s *Stream) SetStreamConfig(config *SelectedStreamConfiguration) error { char := s.service.GetCharacter(TypeSelectedStreamConfiguration) if err := char.Write(config); err != nil { return err } if err := s.client.PutCharacters(char); err != nil { return err } return s.client.GetCharacter(char) } func (s *Stream) Close() error { config := &SelectedStreamConfiguration{ Control: SessionControl{ SessionID: s.id, Command: SessionCommandEnd, }, } char := s.service.GetCharacter(TypeSelectedStreamConfiguration) if err := char.Write(config); err != nil { return err } return s.client.PutCharacters(char) } ================================================ FILE: pkg/hap/chacha20poly1305/chacha20poly1305.go ================================================ package chacha20poly1305 import ( "errors" "golang.org/x/crypto/chacha20poly1305" ) var ErrInvalidParams = errors.New("chacha20poly1305: invalid params") // Decrypt - decrypt without verify func Decrypt(key32 []byte, nonce8 string, ciphertext []byte) ([]byte, error) { return DecryptAndVerify(key32, nil, []byte(nonce8), ciphertext, nil) } // Encrypt - encrypt without seal func Encrypt(key32 []byte, nonce8 string, plaintext []byte) ([]byte, error) { return EncryptAndSeal(key32, nil, []byte(nonce8), plaintext, nil) } func DecryptAndVerify(key32, dst, nonce8, ciphertext, verify []byte) ([]byte, error) { if len(key32) != chacha20poly1305.KeySize || len(nonce8) != 8 { return nil, ErrInvalidParams } aead, err := chacha20poly1305.New(key32) if err != nil { return nil, err } nonce := make([]byte, chacha20poly1305.NonceSize) copy(nonce[4:], nonce8) return aead.Open(dst, nonce, ciphertext, verify) } func EncryptAndSeal(key32, dst, nonce8, plaintext, verify []byte) ([]byte, error) { if len(key32) != chacha20poly1305.KeySize || len(nonce8) != 8 { return nil, ErrInvalidParams } aead, err := chacha20poly1305.New(key32) if err != nil { return nil, err } nonce := make([]byte, chacha20poly1305.NonceSize) copy(nonce[4:], nonce8) return aead.Seal(dst, nonce, plaintext, verify), nil } ================================================ FILE: pkg/hap/character.go ================================================ package hap import ( "bytes" "encoding/json" "fmt" "io" "net/http" "github.com/AlexxIT/go2rtc/pkg/hap/tlv8" ) // Character - Aqara props order // Value should be omit for PW // Value may be empty for PR type Character struct { Desc string `json:"description,omitempty"` IID uint64 `json:"iid"` Type string `json:"type"` Format string `json:"format"` Value any `json:"value,omitempty"` Perms []string `json:"perms"` //MaxLen int `json:"maxLen,omitempty"` //Unit string `json:"unit,omitempty"` //MinValue any `json:"minValue,omitempty"` //MaxValue any `json:"maxValue,omitempty"` //MinStep any `json:"minStep,omitempty"` //ValidVal []any `json:"valid-values,omitempty"` listeners map[io.Writer]bool } func (c *Character) AddListener(w io.Writer) { // TODO: sync.Mutex if c.listeners == nil { c.listeners = map[io.Writer]bool{} } c.listeners[w] = true } func (c *Character) RemoveListener(w io.Writer) { delete(c.listeners, w) if len(c.listeners) == 0 { c.listeners = nil } } func (c *Character) NotifyListeners(ignore io.Writer) error { if c.listeners == nil { return nil } data, err := c.GenerateEvent() if err != nil { return err } for w := range c.listeners { if w == ignore { continue } if _, err = w.Write(data); err != nil { // error not a problem - just remove listener c.RemoveListener(w) } } return nil } // GenerateEvent with raw HTTP headers func (c *Character) GenerateEvent() (data []byte, err error) { v := JSONCharacters{ Value: []JSONCharacter{ {AID: DeviceAID, IID: c.IID, Value: c.Value}, }, } if data, err = json.Marshal(v); err != nil { return } res := http.Response{ StatusCode: http.StatusOK, ProtoMajor: 1, ProtoMinor: 0, Header: http.Header{"Content-Type": []string{MimeJSON}}, ContentLength: int64(len(data)), Body: io.NopCloser(bytes.NewReader(data)), } buf := bytes.NewBuffer([]byte{0}) if err = res.Write(buf); err != nil { return } copy(buf.Bytes(), "EVENT") return buf.Bytes(), err } // Set new value and NotifyListeners func (c *Character) Set(v any) (err error) { if err = c.Write(v); err != nil { return } return c.NotifyListeners(nil) } // Write new value with right format func (c *Character) Write(v any) (err error) { switch c.Format { case "tlv8": c.Value, err = tlv8.MarshalBase64(v) case "bool": switch v := v.(type) { case bool: c.Value = v case float64: c.Value = v != 0 } } return } // ReadTLV8 value to right struct func (c *Character) ReadTLV8(v any) (err error) { if s, ok := c.Value.(string); ok { return tlv8.UnmarshalBase64(s, v) } return fmt.Errorf("hap: can't read value: %v", v) } func (c *Character) ReadBool() (bool, error) { if v, ok := c.Value.(bool); ok { return v, nil } return false, fmt.Errorf("hap: can't read value: %v", c.Value) } func (c *Character) String() string { data, err := json.Marshal(c) if err != nil { return "ERROR" } return string(data) } ================================================ FILE: pkg/hap/client.go ================================================ package hap import ( "bufio" "bytes" "encoding/hex" "encoding/json" "errors" "fmt" "io" "net" "net/http" "net/url" "strings" "time" "github.com/AlexxIT/go2rtc/pkg/hap/chacha20poly1305" "github.com/AlexxIT/go2rtc/pkg/hap/curve25519" "github.com/AlexxIT/go2rtc/pkg/hap/ed25519" "github.com/AlexxIT/go2rtc/pkg/hap/hkdf" "github.com/AlexxIT/go2rtc/pkg/hap/tlv8" "github.com/AlexxIT/go2rtc/pkg/mdns" ) const ( ConnDialTimeout = time.Second * 3 ConnDeadline = time.Second * 3 ) // Client for HomeKit. DevicePublic can be null. type Client struct { DeviceAddress string // including port DeviceID string // aka. Accessory DevicePublic []byte ClientID string // aka. Controller ClientPrivate []byte OnEvent func(res *http.Response) //Output func(msg any) Conn net.Conn reader *bufio.Reader res chan *http.Response err error } func Dial(rawURL string) (*Client, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } query := u.Query() c := &Client{ DeviceAddress: u.Host, DeviceID: query.Get("device_id"), DevicePublic: DecodeKey(query.Get("device_public")), ClientID: query.Get("client_id"), ClientPrivate: DecodeKey(query.Get("client_private")), } if err = c.Dial(); err != nil { return nil, err } return c, nil } func (c *Client) ClientPublic() []byte { return c.ClientPrivate[32:] } func (c *Client) URL() string { return fmt.Sprintf( "homekit://%s?device_id=%s&device_public=%16x&client_id=%s&client_private=%32x", c.DeviceAddress, c.DeviceID, c.DevicePublic, c.ClientID, c.ClientPrivate, ) } func (c *Client) DeviceHost() string { if i := strings.IndexByte(c.DeviceAddress, ':'); i > 0 { return c.DeviceAddress[:i] } return c.DeviceAddress } func (c *Client) Dial() (err error) { if len(c.ClientID) == 0 || len(c.ClientPrivate) == 0 { return errors.New("hap: can't dial witout client_id or client_private") } // update device address (host and/or port) before dial _ = mdns.QueryOrDiscovery(c.DeviceHost(), mdns.ServiceHAP, func(entry *mdns.ServiceEntry) bool { if entry.Complete() && entry.Info["id"] == c.DeviceID { c.DeviceAddress = entry.Addr() return true } return false }) // TODO: close conn on error if c.Conn, err = net.DialTimeout("tcp", c.DeviceAddress, ConnDialTimeout); err != nil { return } c.reader = bufio.NewReader(c.Conn) // STEP M1: send our session public to device sessionPublic, sessionPrivate := curve25519.GenerateKeyPair() // 1. Send sessionPublic plainM1 := struct { PublicKey string `tlv8:"3"` State byte `tlv8:"6"` }{ PublicKey: string(sessionPublic), State: StateM1, } res, err := c.Post(PathPairVerify, MimeTLV8, tlv8.MarshalReader(plainM1)) if err != nil { return } // STEP M2: unpack deviceID from response var cipherM2 struct { PublicKey string `tlv8:"3"` EncryptedData string `tlv8:"5"` State byte `tlv8:"6"` } if err = tlv8.UnmarshalReader(res.Body, res.ContentLength, &cipherM2); err != nil { return err } if cipherM2.State != StateM2 { return newResponseError(plainM1, cipherM2) } // 1. generate session shared key sessionShared, err := curve25519.SharedSecret(sessionPrivate, []byte(cipherM2.PublicKey)) if err != nil { return } sessionKey, err := hkdf.Sha512( sessionShared, "Pair-Verify-Encrypt-Salt", "Pair-Verify-Encrypt-Info", ) if err != nil { return } // 2. decrypt M2 response with session key b, err := chacha20poly1305.Decrypt(sessionKey, "PV-Msg02", []byte(cipherM2.EncryptedData)) if err != nil { return } // 3. unpack payload from TLV8 var plainM2 struct { Identifier string `tlv8:"1"` Signature string `tlv8:"10"` } if err = tlv8.Unmarshal(b, &plainM2); err != nil { return } // 4. verify signature for M2 response with device public // device session + device id + our session if c.DevicePublic != nil { b = Append(cipherM2.PublicKey, plainM2.Identifier, sessionPublic) if !ed25519.ValidateSignature(c.DevicePublic, b, []byte(plainM2.Signature)) { return errors.New("hap: ValidateSignature") } } // STEP M3: send our clientID to device // 1. generate signature with our private key // (our session + our ID + device session) b = Append(sessionPublic, c.ClientID, cipherM2.PublicKey) if b, err = ed25519.Signature(c.ClientPrivate, b); err != nil { return } // 2. generate payload plainM3 := struct { Identifier string `tlv8:"1"` Signature string `tlv8:"10"` }{ Identifier: c.ClientID, Signature: string(b), } if b, err = tlv8.Marshal(plainM3); err != nil { return } // 4. encrypt payload with session key if b, err = chacha20poly1305.Encrypt(sessionKey, "PV-Msg03", b); err != nil { return } // 4. generate request cipherM3 := struct { EncryptedData string `tlv8:"5"` State byte `tlv8:"6"` }{ State: StateM3, EncryptedData: string(b), } if res, err = c.Post(PathPairVerify, MimeTLV8, tlv8.MarshalReader(cipherM3)); err != nil { return } // STEP M4. Read response var plainM4 struct { State byte `tlv8:"6"` } if err = tlv8.UnmarshalReader(res.Body, res.ContentLength, &plainM4); err != nil { return } if plainM4.State != StateM4 { return newResponseError(cipherM3, plainM4) } rw := bufio.NewReadWriter(c.reader, bufio.NewWriter(c.Conn)) // like tls.Client wrapper over net.Conn if c.Conn, err = NewConn(c.Conn, rw, sessionShared, true); err != nil { return } // new reader for new conn c.reader = bufio.NewReader(c.Conn) return } func (c *Client) Close() error { if c.Conn == nil { return nil } return c.Conn.Close() } func (c *Client) eventsReader() { c.res = make(chan *http.Response) for { var res *http.Response if res, c.err = ReadResponse(c.reader, nil); c.err != nil { break } var body []byte if body, c.err = io.ReadAll(res.Body); c.err != nil { break } res.Body = io.NopCloser(bytes.NewReader(body)) if res.Proto != ProtoEvent { c.res <- res } else if c.OnEvent != nil { c.OnEvent(res) } } close(c.res) } func (c *Client) GetAccessories() ([]*Accessory, error) { res, err := c.Get(PathAccessories) if err != nil { return nil, err } var v JSONAccessories if err = json.NewDecoder(res.Body).Decode(&v); err != nil { return nil, err } return v.Value, nil } func (c *Client) GetFirstAccessory() (*Accessory, error) { accs, err := c.GetAccessories() if err != nil { return nil, err } if len(accs) == 0 { return nil, errors.New("hap: GetAccessories zero answer") } return accs[0], nil } func (c *Client) GetCharacters(query string) ([]JSONCharacter, error) { res, err := c.Get(PathCharacteristics + "?id=" + query) if err != nil { return nil, err } data, err := io.ReadAll(res.Body) if err != nil { return nil, err } var v JSONCharacters if err = json.Unmarshal(data, &v); err != nil { return nil, err } return v.Value, nil } func (c *Client) GetCharacter(char *Character) error { query := fmt.Sprintf("%d.%d", DeviceAID, char.IID) chars, err := c.GetCharacters(query) if err != nil { return err } char.Value = chars[0].Value return nil } func (c *Client) PutCharacters(characters ...*Character) error { var v JSONCharacters for i, char := range characters { v.Value = append(v.Value, JSONCharacter{ AID: 1, IID: char.IID, Value: char.Value, }) characters[i] = char } body, err := json.Marshal(v) if err != nil { return err } res, err := c.Put(PathCharacteristics, MimeJSON, bytes.NewReader(body)) if err != nil { return err } _, _ = io.ReadAll(res.Body) // important to "clear" body return nil } func (c *Client) GetImage(width, height int) ([]byte, error) { s := fmt.Sprintf( `{"image-width":%d,"image-height":%d,"resource-type":"image","reason":0}`, width, height, ) res, err := c.Post(PathResource, MimeJSON, bytes.NewBufferString(s)) if err != nil { return nil, err } return io.ReadAll(res.Body) } func (c *Client) LocalIP() string { if c.Conn == nil { return "" } addr := c.Conn.LocalAddr().(*net.TCPAddr) return addr.IP.String() } func DecodeKey(s string) []byte { if s == "" { return nil } data, err := hex.DecodeString(s) if err != nil { return nil } return data } ================================================ FILE: pkg/hap/client_http.go ================================================ package hap import ( "bufio" "errors" "io" "net/http" ) const ( MimeTLV8 = "application/pairing+tlv8" MimeJSON = "application/hap+json" PathPairSetup = "/pair-setup" PathPairVerify = "/pair-verify" PathPairings = "/pairings" PathAccessories = "/accessories" PathCharacteristics = "/characteristics" PathResource = "/resource" ) func (c *Client) Do(req *http.Request) (*http.Response, error) { if err := req.Write(c.Conn); err != nil { return nil, err } if c.res != nil { return <-c.res, c.err } return http.ReadResponse(c.reader, req) } func (c *Client) Request(method, path, contentType string, body io.Reader) (*http.Response, error) { req, err := http.NewRequest(method, "http://"+c.DeviceAddress+path, body) if err != nil { return nil, err } if contentType != "" { req.Header.Set("Content-Type", contentType) } res, err := c.Do(req) if err == nil && res.StatusCode >= http.StatusBadRequest { err = errors.New("hap: wrong http status: " + res.Status) } return res, err } func (c *Client) Get(path string) (*http.Response, error) { return c.Request("GET", path, "", nil) } func (c *Client) Post(path, contentType string, body io.Reader) (*http.Response, error) { return c.Request("POST", path, contentType, body) } func (c *Client) Put(path, contentType string, body io.Reader) (*http.Response, error) { return c.Request("PUT", path, contentType, body) } const ProtoEvent = "EVENT/1.0" func ReadResponse(r *bufio.Reader, req *http.Request) (*http.Response, error) { b, err := r.Peek(9) if err != nil { return nil, err } if string(b) != ProtoEvent { return http.ReadResponse(r, req) } copy(b, "HTTP/1.1 ") res, err := http.ReadResponse(r, req) if err != nil { return nil, err } res.Proto = ProtoEvent return res, nil } func WriteEvent(w io.Writer, res *http.Response) error { return res.Write(&eventWriter{w: w}) } type eventWriter struct { w io.Writer done bool } func (e *eventWriter) Write(p []byte) (n int, err error) { if !e.done { p = append([]byte("EVENT/1.0"), p[8:]...) e.done = true } return e.w.Write(p) } ================================================ FILE: pkg/hap/client_pairing.go ================================================ package hap import ( "bufio" "crypto/sha512" "errors" "net" "net/url" "github.com/AlexxIT/go2rtc/pkg/hap/chacha20poly1305" "github.com/AlexxIT/go2rtc/pkg/hap/ed25519" "github.com/AlexxIT/go2rtc/pkg/hap/hkdf" "github.com/AlexxIT/go2rtc/pkg/hap/tlv8" "github.com/tadglines/go-pkgs/crypto/srp" ) // Pair homekit func Pair(rawURL string) (*Client, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } query := u.Query() c := &Client{ DeviceAddress: u.Host, DeviceID: query.Get("device_id"), ClientID: query.Get("client_id"), ClientPrivate: DecodeKey(query.Get("client_private")), } if c.ClientID == "" { c.ClientID = GenerateUUID() } if c.ClientPrivate == nil { c.ClientPrivate = GenerateKey() } if err = c.Pair(query.Get("feature"), query.Get("pin")); err != nil { return nil, err } return c, nil } func Unpair(rawURL string) error { u, err := url.Parse(rawURL) if err != nil { return err } query := u.Query() conn := &Client{ DeviceAddress: u.Host, DeviceID: query.Get("device_id"), DevicePublic: DecodeKey(query.Get("device_public")), ClientID: query.Get("client_id"), ClientPrivate: DecodeKey(query.Get("client_private")), } if err = conn.Dial(); err != nil { return err } defer conn.Close() if err = conn.ListPairings(); err != nil { return err } return conn.DeletePairing(conn.ClientID) } func (c *Client) Pair(feature, pin string) (err error) { if pin, err = SanitizePin(pin); err != nil { return err } c.Conn, err = net.DialTimeout("tcp", c.DeviceAddress, ConnDialTimeout) if err != nil { return } c.reader = bufio.NewReader(c.Conn) // STEP M1. Send HELLO plainM1 := struct { Method byte `tlv8:"0"` State byte `tlv8:"6"` }{ Method: MethodPair, State: StateM1, } if feature == "1" { plainM1.Method = MethodPairMFi // ff=1 => method=1, ff=2 => method=0 } res, err := c.Post(PathPairSetup, MimeTLV8, tlv8.MarshalReader(plainM1)) if err != nil { return } // STEP M2. Read Device Salt and session PublicKey var plainM2 struct { Salt string `tlv8:"2"` SessionKey string `tlv8:"3"` // server public key, aka session.B State byte `tlv8:"6"` Error byte `tlv8:"7"` } if err = tlv8.UnmarshalReader(res.Body, res.ContentLength, &plainM2); err != nil { return } if plainM2.State != StateM2 { return newResponseError(plainM1, plainM2) } if plainM2.Error != 0 { return newPairingError(plainM2.Error) } // STEP M3. Generate SRP Session using pin username := []byte("Pair-Setup") // Stanford Secure Remote Password (SRP) / Password Authenticated Key Exchange (PAKE) pake, err := srp.NewSRP("rfc5054.3072", sha512.New, keyDerivativeFuncRFC2945(username)) if err != nil { return } pake.SaltLength = 16 // username: "Pair-Setup", password: PIN (with dashes) session := pake.NewClientSession(username, []byte(pin)) sessionShared, err := session.ComputeKey([]byte(plainM2.Salt), []byte(plainM2.SessionKey)) if err != nil { return } // STEP M3. Send request plainM3 := struct { SessionKey string `tlv8:"3"` Proof string `tlv8:"4"` State byte `tlv8:"6"` }{ SessionKey: string(session.GetA()), // client public key, aka session.A Proof: string(session.ComputeAuthenticator()), State: StateM3, } if res, err = c.Post(PathPairSetup, MimeTLV8, tlv8.MarshalReader(plainM3)); err != nil { return } // STEP M4. Read response var plainM4 struct { Proof string `tlv8:"4"` // server proof State byte `tlv8:"6"` Error byte `tlv8:"7"` EncryptedData string `tlv8:"5"` // skip EncryptedData validation (for MFi devices) } if err = tlv8.UnmarshalReader(res.Body, res.ContentLength, &plainM4); err != nil { return } if plainM4.State != StateM4 { return newResponseError(plainM3, plainM4) } if plainM4.Error != 0 { return newPairingError(plainM4.Error) } // STEP M4. Verify response if !session.VerifyServerAuthenticator([]byte(plainM4.Proof)) { return errors.New("hap: VerifyServerAuthenticator") } // STEP M5. Generate signature localSign, err := hkdf.Sha512( sessionShared, "Pair-Setup-Controller-Sign-Salt", "Pair-Setup-Controller-Sign-Info", ) if err != nil { return } b := Append(localSign, c.ClientID, c.ClientPublic()) signature, err := ed25519.Signature(c.ClientPrivate, b) if err != nil { return } // STEP M5. Generate payload plainM5 := struct { Identifier string `tlv8:"1"` PublicKey string `tlv8:"3"` Signature string `tlv8:"10"` }{ Identifier: c.ClientID, PublicKey: string(c.ClientPublic()), Signature: string(signature), } if b, err = tlv8.Marshal(plainM5); err != nil { return } // STEP M5. Encrypt payload encryptKey, err := hkdf.Sha512( sessionShared, "Pair-Setup-Encrypt-Salt", "Pair-Setup-Encrypt-Info", ) if err != nil { return } if b, err = chacha20poly1305.Encrypt(encryptKey, "PS-Msg05", b); err != nil { return } // STEP M5. Send request cipherM5 := struct { EncryptedData string `tlv8:"5"` State byte `tlv8:"6"` }{ EncryptedData: string(b), State: StateM5, } if res, err = c.Post(PathPairSetup, MimeTLV8, tlv8.MarshalReader(cipherM5)); err != nil { return } // STEP M6. Read response cipherM6 := struct { EncryptedData string `tlv8:"5"` State byte `tlv8:"6"` Error byte `tlv8:"7"` }{} if err = tlv8.UnmarshalReader(res.Body, res.ContentLength, &cipherM6); err != nil { return } if cipherM6.State != StateM6 || cipherM6.Error != 0 { return newResponseError(plainM5, cipherM6) } // STEP M6. Decrypt payload b, err = chacha20poly1305.Decrypt(encryptKey, "PS-Msg06", []byte(cipherM6.EncryptedData)) if err != nil { return } plainM6 := struct { Identifier string `tlv8:"1"` PublicKey string `tlv8:"3"` Signature string `tlv8:"10"` }{} if err = tlv8.Unmarshal(b, &plainM6); err != nil { return } // STEP M6. Verify payload remoteSign, err := hkdf.Sha512( sessionShared, "Pair-Setup-Accessory-Sign-Salt", "Pair-Setup-Accessory-Sign-Info", ) if err != nil { return } b = Append(remoteSign, plainM6.Identifier, plainM6.PublicKey) if !ed25519.ValidateSignature([]byte(plainM6.PublicKey), b, []byte(plainM6.Signature)) { return errors.New("hap: ValidateSignature") } if c.DeviceID != plainM6.Identifier { return errors.New("hap: wrong DeviceID: " + plainM6.Identifier) } c.DevicePublic = []byte(plainM6.PublicKey) return nil } func (c *Client) ListPairings() error { plainM1 := struct { Method byte `tlv8:"0"` State byte `tlv8:"6"` }{ Method: MethodListPairings, State: StateM1, } res, err := c.Post(PathPairings, MimeTLV8, tlv8.MarshalReader(plainM1)) if err != nil { return err } // TODO: don't know how to fix array of items var plainM2 struct { Identifier string `tlv8:"1"` PublicKey string `tlv8:"3"` State byte `tlv8:"6"` Permission byte `tlv8:"11"` } if err = tlv8.UnmarshalReader(res.Body, res.ContentLength, &plainM2); err != nil { return err } return nil } func (c *Client) PairingsAdd(clientID string, clientPublic []byte, admin bool) error { plainM1 := struct { Method byte `tlv8:"0"` Identifier string `tlv8:"1"` PublicKey string `tlv8:"3"` State byte `tlv8:"6"` Permission byte `tlv8:"11"` }{ Method: MethodAddPairing, Identifier: clientID, PublicKey: string(clientPublic), State: StateM1, Permission: PermissionUser, } if admin { plainM1.Permission = PermissionAdmin } res, err := c.Post(PathPairings, MimeTLV8, tlv8.MarshalReader(plainM1)) if err != nil { return err } var plainM2 struct { State byte `tlv8:"6"` Unknown byte `tlv8:"7"` } if err = tlv8.UnmarshalReader(res.Body, res.ContentLength, &plainM2); err != nil { return err } return nil } func (c *Client) DeletePairing(id string) error { plainM1 := struct { Method byte `tlv8:"0"` Identifier string `tlv8:"1"` State byte `tlv8:"6"` }{ Method: MethodDeletePairing, Identifier: id, State: StateM1, } res, err := c.Post(PathPairings, MimeTLV8, tlv8.MarshalReader(plainM1)) if err != nil { return err } var plainM2 struct { State byte `tlv8:"6"` } if err = tlv8.UnmarshalReader(res.Body, res.ContentLength, &plainM2); err != nil { return err } if plainM2.State != StateM2 { return newResponseError(plainM1, plainM2) } return nil } func newPairingError(code byte) error { var text string // https://github.com/apple/HomeKitADK/blob/fb201f98f5fdc7fef6a455054f08b59cca5d1ec8/HAP/HAPPairing.h#L89 switch code { case 1: text = "Generic error to handle unexpected errors" case 2: text = "Setup code or signature verification failed" case 3: text = "Client must look at the retry delay TLV item and wait that many seconds before retrying" case 4: text = "Server cannot accept any more pairings" case 5: text = "Server reached its maximum number of authentication attempts" case 6: text = "Server pairing method is unavailable" case 7: text = "Server is busy and cannot accept a pairing request at this time" default: text = "Unknown pairing error" } return errors.New("hap: " + text) } func keyDerivativeFuncRFC2945(username []byte) srp.KeyDerivationFunc { return func(salt, password []byte) []byte { h1 := sha512.New() h1.Write(username) h1.Write([]byte(":")) h1.Write(password) h2 := sha512.New() h2.Write(salt) h2.Write(h1.Sum(nil)) return h2.Sum(nil) } } ================================================ FILE: pkg/hap/conn.go ================================================ package hap import ( "bufio" "encoding/binary" "encoding/json" "errors" "io" "net" "sync" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/hap/chacha20poly1305" "github.com/AlexxIT/go2rtc/pkg/hap/hkdf" ) type Conn struct { conn net.Conn rw *bufio.ReadWriter wmu sync.Mutex encryptKey []byte decryptKey []byte encryptCnt uint64 decryptCnt uint64 //ClientID string SharedKey []byte recv int send int } func (c *Conn) MarshalJSON() ([]byte, error) { conn := core.Connection{ ID: core.ID(c), FormatName: "homekit", Protocol: "hap", RemoteAddr: c.conn.RemoteAddr().String(), Recv: c.recv, Send: c.send, } return json.Marshal(conn) } func NewConn(conn net.Conn, rw *bufio.ReadWriter, sharedKey []byte, isClient bool) (*Conn, error) { key1, err := hkdf.Sha512(sharedKey, "Control-Salt", "Control-Read-Encryption-Key") if err != nil { return nil, err } key2, err := hkdf.Sha512(sharedKey, "Control-Salt", "Control-Write-Encryption-Key") if err != nil { return nil, err } c := &Conn{ conn: conn, rw: rw, SharedKey: sharedKey, } if isClient { c.encryptKey, c.decryptKey = key2, key1 } else { c.encryptKey, c.decryptKey = key1, key2 } return c, nil } const ( // packetSizeMax is the max length of encrypted packets packetSizeMax = 0x400 VerifySize = 2 NonceSize = 8 Overhead = 16 // chacha20poly1305.Overhead ) func (c *Conn) Read(b []byte) (n int, err error) { if cap(b) < packetSizeMax { return 0, errors.New("hap: read buffer is too small") } verify := make([]byte, VerifySize) // verify = plain message size if _, err = io.ReadFull(c.rw, verify); err != nil { return } n = int(binary.LittleEndian.Uint16(verify)) ciphertext := make([]byte, n+Overhead) if _, err = io.ReadFull(c.rw, ciphertext); err != nil { return } nonce := make([]byte, NonceSize) binary.LittleEndian.PutUint64(nonce, c.decryptCnt) c.decryptCnt++ _, err = chacha20poly1305.DecryptAndVerify(c.decryptKey, b[:0], nonce, ciphertext, verify) c.recv += n return } func (c *Conn) Write(b []byte) (n int, err error) { c.wmu.Lock() defer c.wmu.Unlock() buf := make([]byte, 0, packetSizeMax+Overhead) nonce := make([]byte, NonceSize) verify := make([]byte, VerifySize) for len(b) > 0 { size := len(b) if size > packetSizeMax { size = packetSizeMax } binary.LittleEndian.PutUint16(verify, uint16(size)) if _, err = c.rw.Write(verify); err != nil { return } binary.LittleEndian.PutUint64(nonce, c.encryptCnt) c.encryptCnt++ _, err = chacha20poly1305.EncryptAndSeal(c.encryptKey, buf, nonce, b[:size], verify) if err != nil { return } if _, err = c.rw.Write(buf[:size+Overhead]); err != nil { return } b = b[size:] n += size } err = c.rw.Flush() c.send += n return } func (c *Conn) Close() error { return c.conn.Close() } func (c *Conn) LocalAddr() net.Addr { return c.conn.LocalAddr() } func (c *Conn) RemoteAddr() net.Addr { return c.conn.RemoteAddr() } func (c *Conn) SetDeadline(t time.Time) error { return c.conn.SetDeadline(t) } func (c *Conn) SetReadDeadline(t time.Time) error { return c.conn.SetReadDeadline(t) } func (c *Conn) SetWriteDeadline(t time.Time) error { return c.conn.SetWriteDeadline(t) } ================================================ FILE: pkg/hap/curve25519/curve25519.go ================================================ package curve25519 import ( "crypto/rand" "golang.org/x/crypto/curve25519" ) func GenerateKeyPair() ([]byte, []byte) { var publicKey, privateKey [32]byte _, _ = rand.Read(privateKey[:]) curve25519.ScalarBaseMult(&publicKey, &privateKey) return publicKey[:], privateKey[:] } func SharedSecret(privateKey, otherPublicKey []byte) ([]byte, error) { return curve25519.X25519(privateKey, otherPublicKey) } ================================================ FILE: pkg/hap/ed25519/ed25519.go ================================================ package ed25519 import ( "crypto/ed25519" "errors" ) var ErrInvalidParams = errors.New("ed25519: invalid params") func ValidateSignature(key, data, signature []byte) bool { if len(key) != ed25519.PublicKeySize || len(signature) != ed25519.SignatureSize { return false } return ed25519.Verify(key, data, signature) } func Signature(key, data []byte) ([]byte, error) { if len(key) != ed25519.PrivateKeySize { return nil, ErrInvalidParams } return ed25519.Sign(key, data), nil } ================================================ FILE: pkg/hap/hds/hds.go ================================================ // Package hds - HomeKit Data Stream package hds import ( "bufio" "encoding/binary" "encoding/json" "errors" "io" "net" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/hap" "github.com/AlexxIT/go2rtc/pkg/hap/chacha20poly1305" "github.com/AlexxIT/go2rtc/pkg/hap/hkdf" ) func NewConn(conn net.Conn, key []byte, salt string, controller bool) (*Conn, error) { writeKey, err := hkdf.Sha512(key, salt, "HDS-Write-Encryption-Key") if err != nil { return nil, err } readKey, err := hkdf.Sha512(key, salt, "HDS-Read-Encryption-Key") if err != nil { return nil, err } c := &Conn{ conn: conn, rd: bufio.NewReaderSize(conn, 32*1024), wr: bufio.NewWriterSize(conn, 32*1024), } if controller { c.decryptKey, c.encryptKey = readKey, writeKey } else { c.decryptKey, c.encryptKey = writeKey, readKey } return c, nil } type Conn struct { conn net.Conn rd *bufio.Reader wr *bufio.Writer decryptKey []byte encryptKey []byte decryptCnt uint64 encryptCnt uint64 recv int send int } func (c *Conn) MarshalJSON() ([]byte, error) { conn := core.Connection{ ID: core.ID(c), FormatName: "homekit", Protocol: "hds", RemoteAddr: c.conn.RemoteAddr().String(), Recv: c.recv, Send: c.send, } return json.Marshal(conn) } func (c *Conn) read() (b []byte, err error) { verify := make([]byte, 4) if _, err = io.ReadFull(c.rd, verify); err != nil { return } n := int(binary.BigEndian.Uint32(verify) & 0xFFFFFF) ciphertext := make([]byte, n+hap.Overhead) if _, err = io.ReadFull(c.rd, ciphertext); err != nil { return } nonce := make([]byte, hap.NonceSize) binary.LittleEndian.PutUint64(nonce, c.decryptCnt) c.decryptCnt++ c.recv += n return chacha20poly1305.DecryptAndVerify(c.decryptKey, ciphertext[:0], nonce, ciphertext, verify) } func (c *Conn) Read(p []byte) (n int, err error) { b, err := c.read() if err != nil { return 0, err } n = copy(p, b) if len(b) > n { err = errors.New("hds: read buffer too small") } return } func (c *Conn) WriteTo(w io.Writer) (int64, error) { var total int64 for { b, err := c.read() if err != nil { return total, err } n, err := w.Write(b) total += int64(n) if err != nil { return total, err } } } func (c *Conn) Write(b []byte) (n int, err error) { n = len(b) if n > 0xFFFFFF { return 0, errors.New("hds: write buffer too big") } verify := make([]byte, 4) binary.BigEndian.PutUint32(verify, 0x01000000|uint32(n)) if _, err = c.wr.Write(verify); err != nil { return } nonce := make([]byte, hap.NonceSize) binary.LittleEndian.PutUint64(nonce, c.encryptCnt) c.encryptCnt++ buf := make([]byte, n+hap.Overhead) if _, err = chacha20poly1305.EncryptAndSeal(c.encryptKey, buf[:0], nonce, b, verify); err != nil { return } if _, err = c.wr.Write(buf); err != nil { return } err = c.wr.Flush() c.send += n return } func (c *Conn) Close() error { return c.conn.Close() } func (c *Conn) LocalAddr() net.Addr { return c.conn.LocalAddr() } func (c *Conn) RemoteAddr() net.Addr { return c.conn.RemoteAddr() } func (c *Conn) SetDeadline(t time.Time) error { return c.conn.SetDeadline(t) } func (c *Conn) SetReadDeadline(t time.Time) error { return c.conn.SetReadDeadline(t) } func (c *Conn) SetWriteDeadline(t time.Time) error { return c.conn.SetWriteDeadline(t) } ================================================ FILE: pkg/hap/hds/hds_test.go ================================================ package hds import ( "bufio" "bytes" "testing" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/stretchr/testify/require" ) func TestEncryption(t *testing.T) { key := []byte(core.RandString(16, 0)) salt := core.RandString(32, 0) c, err := Client(nil, key, salt, true) require.NoError(t, err) buf := bytes.NewBuffer(nil) c.wr = bufio.NewWriter(buf) n, err := c.Write([]byte("test")) require.NoError(t, err) require.Equal(t, 4, n) c, err = Client(nil, key, salt, false) c.rd = bufio.NewReader(buf) require.NoError(t, err) b := make([]byte, 32) n, err = c.Read(b) require.NoError(t, err) require.Equal(t, "test", string(b[:n])) } ================================================ FILE: pkg/hap/helpers.go ================================================ package hap import ( "crypto/ed25519" "crypto/rand" "crypto/sha512" "encoding/base64" "encoding/hex" "errors" "fmt" "strings" ) const ( TXTConfigNumber = "c#" // Current configuration number (ex. 1, 2, 3) TXTDeviceID = "id" // Device ID of the accessory (ex. 77:75:87:A0:7D:F4) TXTModel = "md" // Model name of the accessory (ex. MJCTD02YL) TXTProtoVersion = "pv" // Protocol version string (ex. 1.1) TXTStateNumber = "s#" // Current state number (ex. 1) TXTCategory = "ci" // Accessory Category Identifier (ex. 2, 5, 17) TXTSetupHash = "sh" // Setup hash (ex. Y9w9hQ==) // TXTFeatureFlags // - 0001b - Supports Apple Authentication Coprocessor // - 0010b - Supports Software Authentication TXTFeatureFlags = "ff" // Pairing Feature flags (ex. 0, 1, 2) // TXTStatusFlags // - 0001b - Accessory has not been paired with any controllers // - 0100b - A problem has been detected on the accessory TXTStatusFlags = "sf" // Status flags (ex. 0, 1) StatusPaired = "0" StatusNotPaired = "1" CategoryBridge = "2" CategoryCamera = "17" CategoryDoorbell = "18" StateM1 = 1 StateM2 = 2 StateM3 = 3 StateM4 = 4 StateM5 = 5 StateM6 = 6 MethodPair = 0 MethodPairMFi = 1 // if device has MFI cert MethodVerifyPair = 2 MethodAddPairing = 3 MethodDeletePairing = 4 MethodListPairings = 5 PermissionUser = 0 PermissionAdmin = 1 ) const DeviceAID = 1 // TODO: fix someday type JSONAccessories struct { Value []*Accessory `json:"accessories"` } type JSONCharacters struct { Value []JSONCharacter `json:"characteristics"` } type JSONCharacter struct { AID uint8 `json:"aid"` IID uint64 `json:"iid"` Status any `json:"status,omitempty"` Value any `json:"value,omitempty"` Event any `json:"ev,omitempty"` } // 4.2.1.2 Invalid Setup Codes const insecurePINs = "00000000 11111111 22222222 33333333 44444444 55555555 66666666 77777777 88888888 99999999 12345678 87654321" func SanitizePin(pin string) (string, error) { s := strings.ReplaceAll(pin, "-", "") if len(s) != 8 { return "", errors.New("hap: wrong PIN format: " + pin) } if strings.Contains(insecurePINs, s) { return "", errors.New("hap: insecure PIN: " + pin) } // 123-45-678 return s[:3] + "-" + s[3:5] + "-" + s[5:], nil } func GenerateKey() []byte { _, key, _ := ed25519.GenerateKey(nil) return key } func GenerateUUID() string { //12345678-9012-3456-7890-123456789012 data := make([]byte, 16) _, _ = rand.Read(data) s := hex.EncodeToString(data) return s[:8] + "-" + s[8:12] + "-" + s[12:16] + "-" + s[16:20] + "-" + s[20:] } func SetupHash(setupID, deviceID string) string { // should be setup_id (random 4 alphanum) + device_id (mac address) b := sha512.Sum512([]byte(setupID + deviceID)) return base64.StdEncoding.EncodeToString(b[:4]) } func Append(items ...any) (b []byte) { for _, item := range items { switch v := item.(type) { case string: b = append(b, v...) case []byte: b = append(b, v[:]...) default: panic(v) } } return } func newRequestError(req any) error { return fmt.Errorf("hap: wrong request: %#v", req) } func newResponseError(req, res any) error { return fmt.Errorf("hap: wrong response: %#v, on request: %#v", res, req) } ================================================ FILE: pkg/hap/hkdf/hkdf.go ================================================ package hkdf import ( "crypto/sha512" "io" "golang.org/x/crypto/hkdf" ) func Sha512(key []byte, salt, info string) ([]byte, error) { r := hkdf.New(sha512.New, key, []byte(salt), []byte(info)) buf := make([]byte, 32) _, err := io.ReadFull(r, buf) return buf, err } ================================================ FILE: pkg/hap/server.go ================================================ package hap import ( "bufio" "crypto/sha512" "errors" "fmt" "net/http" "github.com/AlexxIT/go2rtc/pkg/hap/chacha20poly1305" "github.com/AlexxIT/go2rtc/pkg/hap/curve25519" "github.com/AlexxIT/go2rtc/pkg/hap/ed25519" "github.com/AlexxIT/go2rtc/pkg/hap/hkdf" "github.com/AlexxIT/go2rtc/pkg/hap/tlv8" "github.com/tadglines/go-pkgs/crypto/srp" ) type Server struct { Pin string DeviceID string DevicePrivate []byte // GetClientPublic may be nil, so client validation will be disabled GetClientPublic func(id string) []byte } func (s *Server) ServerPublic() []byte { return s.DevicePrivate[32:] } //func (s *Server) Status() string { // if len(s.Pairings) == 0 { // return StatusNotPaired // } // return StatusPaired //} func (s *Server) PairSetup(req *http.Request, rw *bufio.ReadWriter) (id string, publicKey []byte, err error) { // STEP 1. Request from iPhone var plainM1 struct { State byte `tlv8:"6"` Method byte `tlv8:"0"` Flags uint32 `tlv8:"19"` } if err = tlv8.UnmarshalReader(req.Body, req.ContentLength, &plainM1); err != nil { return } if plainM1.State != StateM1 { err = newRequestError(plainM1) return } username := []byte("Pair-Setup") // Stanford Secure Remote Password (SRP) / Password Authenticated Key Exchange (PAKE) pake, err := srp.NewSRP("rfc5054.3072", sha512.New, keyDerivativeFuncRFC2945(username)) if err != nil { return } pake.SaltLength = 16 salt, verifier, err := pake.ComputeVerifier([]byte(s.Pin)) if err != nil { return } session := pake.NewServerSession(username, salt, verifier) // STEP 2. Response to iPhone plainM2 := struct { State byte `tlv8:"6"` PublicKey string `tlv8:"3"` Salt string `tlv8:"2"` }{ State: StateM2, PublicKey: string(session.GetB()), Salt: string(salt), } body, err := tlv8.Marshal(plainM2) if err != nil { return } if err = WriteResponse(rw.Writer, http.StatusOK, MimeTLV8, body); err != nil { return } // STEP 3. Request from iPhone if req, err = http.ReadRequest(rw.Reader); err != nil { return } var plainM3 struct { State byte `tlv8:"6"` PublicKey string `tlv8:"3"` Proof string `tlv8:"4"` } if err = tlv8.UnmarshalReader(req.Body, req.ContentLength, &plainM3); err != nil { return } if plainM3.State != StateM3 { err = newRequestError(plainM3) return } // important to compute key before verify client sessionShared, err := session.ComputeKey([]byte(plainM3.PublicKey)) if err != nil { return } if !session.VerifyClientAuthenticator([]byte(plainM3.Proof)) { err = errors.New("hap: VerifyClientAuthenticator") return } proof := session.ComputeAuthenticator([]byte(plainM3.Proof)) // server proof // STEP 4. Response to iPhone payloadM4 := struct { State byte `tlv8:"6"` Proof string `tlv8:"4"` }{ State: StateM4, Proof: string(proof), } if body, err = tlv8.Marshal(payloadM4); err != nil { return } if err = WriteResponse(rw.Writer, http.StatusOK, MimeTLV8, body); err != nil { return } // STEP 5. Request from iPhone if req, err = http.ReadRequest(rw.Reader); err != nil { return } var cipherM5 struct { State byte `tlv8:"6"` EncryptedData string `tlv8:"5"` } if err = tlv8.UnmarshalReader(req.Body, req.ContentLength, &cipherM5); err != nil { return } if cipherM5.State != StateM5 { err = newRequestError(cipherM5) return } // decrypt message using session shared encryptKey, err := hkdf.Sha512(sessionShared, "Pair-Setup-Encrypt-Salt", "Pair-Setup-Encrypt-Info") if err != nil { return } b, err := chacha20poly1305.Decrypt(encryptKey, "PS-Msg05", []byte(cipherM5.EncryptedData)) if err != nil { return } // unpack message from TLV8 var plainM5 struct { Identifier string `tlv8:"1"` PublicKey string `tlv8:"3"` Signature string `tlv8:"10"` } if err = tlv8.Unmarshal(b, &plainM5); err != nil { return } // 3. verify client ID and Public remoteSign, err := hkdf.Sha512( sessionShared, "Pair-Setup-Controller-Sign-Salt", "Pair-Setup-Controller-Sign-Info", ) if err != nil { return } b = Append(remoteSign, plainM5.Identifier, plainM5.PublicKey) if !ed25519.ValidateSignature([]byte(plainM5.PublicKey), b, []byte(plainM5.Signature)) { err = errors.New("hap: ValidateSignature") return } // 4. generate signature to our ID and Public localSign, err := hkdf.Sha512( sessionShared, "Pair-Setup-Accessory-Sign-Salt", "Pair-Setup-Accessory-Sign-Info", ) if err != nil { return } b = Append(localSign, s.DeviceID, s.ServerPublic()) // ServerPublic signature, err := ed25519.Signature(s.DevicePrivate, b) if err != nil { return } // 5. pack our ID and Public plainM6 := struct { Identifier string `tlv8:"1"` PublicKey string `tlv8:"3"` Signature string `tlv8:"10"` }{ Identifier: s.DeviceID, PublicKey: string(s.ServerPublic()), Signature: string(signature), } if b, err = tlv8.Marshal(plainM6); err != nil { return } // 6. encrypt message b, err = chacha20poly1305.Encrypt(encryptKey, "PS-Msg06", b) if err != nil { return } // STEP 6. Response to iPhone cipherM6 := struct { State byte `tlv8:"6"` EncryptedData string `tlv8:"5"` }{ State: StateM6, EncryptedData: string(b), } if body, err = tlv8.Marshal(cipherM6); err != nil { return } if err = WriteResponse(rw.Writer, http.StatusOK, MimeTLV8, body); err != nil { return } id = plainM5.Identifier publicKey = []byte(plainM5.PublicKey) return } func (s *Server) PairVerify(req *http.Request, rw *bufio.ReadWriter) (id string, sessionKey []byte, err error) { // Request from iPhone var plainM1 struct { State byte `tlv8:"6"` PublicKey string `tlv8:"3"` } if err = tlv8.UnmarshalReader(req.Body, req.ContentLength, &plainM1); err != nil { return } if plainM1.State != StateM1 { err = newRequestError(plainM1) return } // Generate the key pair sessionPublic, sessionPrivate := curve25519.GenerateKeyPair() sessionShared, err := curve25519.SharedSecret(sessionPrivate, []byte(plainM1.PublicKey)) if err != nil { return } encryptKey, err := hkdf.Sha512( sessionShared, "Pair-Verify-Encrypt-Salt", "Pair-Verify-Encrypt-Info", ) if err != nil { return } b := Append(sessionPublic, s.DeviceID, plainM1.PublicKey) signature, err := ed25519.Signature(s.DevicePrivate, b) if err != nil { return } // STEP M2. Response to iPhone plainM2 := struct { Identifier string `tlv8:"1"` Signature string `tlv8:"10"` }{ Identifier: s.DeviceID, Signature: string(signature), } if b, err = tlv8.Marshal(plainM2); err != nil { return } b, err = chacha20poly1305.Encrypt(encryptKey, "PV-Msg02", b) if err != nil { return } cipherM2 := struct { State byte `tlv8:"6"` PublicKey string `tlv8:"3"` EncryptedData string `tlv8:"5"` }{ State: StateM2, PublicKey: string(sessionPublic), EncryptedData: string(b), } body, err := tlv8.Marshal(cipherM2) if err != nil { return } if err = WriteResponse(rw.Writer, http.StatusOK, MimeTLV8, body); err != nil { return } // STEP M3. Request from iPhone if req, err = http.ReadRequest(rw.Reader); err != nil { return } var cipherM3 struct { State byte `tlv8:"6"` EncryptedData string `tlv8:"5"` } if err = tlv8.UnmarshalReader(req.Body, req.ContentLength, &cipherM3); err != nil { return } if cipherM3.State != StateM3 { err = newRequestError(cipherM3) return } b, err = chacha20poly1305.Decrypt(encryptKey, "PV-Msg03", []byte(cipherM3.EncryptedData)) if err != nil { return } var plainM3 struct { Identifier string `tlv8:"1"` Signature string `tlv8:"10"` } if err = tlv8.Unmarshal(b, &plainM3); err != nil { return } if s.GetClientPublic != nil { clientPublic := s.GetClientPublic(plainM3.Identifier) if clientPublic == nil { err = errors.New("hap: PairVerify with unknown client_id: " + plainM3.Identifier) return } b = Append(plainM1.PublicKey, plainM3.Identifier, sessionPublic) if !ed25519.ValidateSignature(clientPublic, b, []byte(plainM3.Signature)) { err = errors.New("hap: ValidateSignature") return } } // STEP M4. Response to iPhone payloadM4 := struct { State byte `tlv8:"6"` }{ State: StateM4, } if body, err = tlv8.Marshal(payloadM4); err != nil { return } if err = WriteResponse(rw.Writer, http.StatusOK, MimeTLV8, body); err != nil { return } id = plainM3.Identifier sessionKey = sessionShared return } func WriteResponse(w *bufio.Writer, statusCode int, contentType string, body []byte) error { header := fmt.Sprintf( "HTTP/1.1 %d %s\r\nContent-Type: %s\r\nContent-Length: %d\r\n\r\n", statusCode, http.StatusText(statusCode), contentType, len(body), ) body = append([]byte(header), body...) if _, err := w.Write(body); err != nil { return err } return w.Flush() } //func WriteBackoff(rw *bufio.ReadWriter) error { // plainM2 := struct { // State byte `tlv8:"6"` // Error byte `tlv8:"7"` // }{ // State: StateM2, // Error: 3, // BackoffError // } // body, err := tlv8.Marshal(plainM2) // if err != nil { // return err // } // return WriteResponse(rw.Writer, http.StatusOK, MimeTLV8, body) //} ================================================ FILE: pkg/hap/setup/setup.go ================================================ package setup import ( "strconv" "strings" ) const ( FlagNFC = 1 FlagIP = 2 FlagBLE = 4 FlagWAC = 8 // Wireless Accessory Configuration (WAC)/Apples MFi ) func GenerateSetupURI(category, pin, setupID string) string { c, _ := strconv.Atoi(category) p, _ := strconv.Atoi(strings.ReplaceAll(pin, "-", "")) payload := int64(c&0xFF)<<31 | int64(FlagIP&0xF)<<27 | int64(p&0x7FFFFFF) return "X-HM://" + FormatInt36(payload, 9) + setupID } // FormatInt36 equal to strings.ToUpper(fmt.Sprintf("%0"+strconv.Itoa(n)+"s", strconv.FormatInt(value, 36))) func FormatInt36(value int64, n int) string { b := make([]byte, n) for i := n - 1; 0 <= i; i-- { b[i] = digits[value%36] value /= 36 } return string(b) } const digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" ================================================ FILE: pkg/hap/setup/setup_test.go ================================================ package setup import ( "fmt" "strconv" "strings" "testing" "github.com/stretchr/testify/require" ) func TestFormatAlphaNum(t *testing.T) { value := int64(999) n := 5 s1 := strings.ToUpper(fmt.Sprintf("%0"+strconv.Itoa(n)+"s", strconv.FormatInt(value, 36))) s2 := FormatInt36(value, n) require.Equal(t, s1, s2) } ================================================ FILE: pkg/hap/tlv8/tlv8.go ================================================ package tlv8 import ( "bytes" "encoding/base64" "encoding/binary" "errors" "fmt" "io" "math" "reflect" "strconv" ) type errReader struct { err error } func (e *errReader) Read([]byte) (int, error) { return 0, e.err } func MarshalBase64(v any) (string, error) { b, err := Marshal(v) if err != nil { return "", err } return base64.StdEncoding.EncodeToString(b), nil } func MarshalReader(v any) io.Reader { b, err := Marshal(v) if err != nil { return &errReader{err: err} } return bytes.NewReader(b) } func Marshal(v any) ([]byte, error) { value := reflect.ValueOf(v) kind := value.Type().Kind() if kind == reflect.Pointer { value = value.Elem() kind = value.Type().Kind() } switch kind { case reflect.Slice: return appendSlice(nil, value) case reflect.Struct: return appendStruct(nil, value) } return nil, errors.New("tlv8: not implemented: " + kind.String()) } // separator the most confusing meaning in the documentation. // It can have a value of 0x00 or 0xFF or even 0x05. const separator = 0xFF func appendSlice(b []byte, value reflect.Value) ([]byte, error) { for i := 0; i < value.Len(); i++ { if i > 0 { b = append(b, separator, 0) } var err error if b, err = appendStruct(b, value.Index(i)); err != nil { return nil, err } } return b, nil } func appendStruct(b []byte, value reflect.Value) ([]byte, error) { valueType := value.Type() for i := 0; i < value.NumField(); i++ { refField := value.Field(i) s, ok := valueType.Field(i).Tag.Lookup("tlv8") if !ok { continue } tag, err := strconv.Atoi(s) if err != nil { return nil, err } b, err = appendValue(b, byte(tag), refField) if err != nil { return nil, err } } return b, nil } func appendValue(b []byte, tag byte, value reflect.Value) ([]byte, error) { var err error switch value.Kind() { case reflect.Uint8: v := value.Uint() return append(b, tag, 1, byte(v)), nil case reflect.Uint16: v := value.Uint() return append(b, tag, 2, byte(v), byte(v>>8)), nil case reflect.Uint32: v := value.Uint() return append(b, tag, 4, byte(v), byte(v>>8), byte(v>>16), byte(v>>24)), nil case reflect.Uint64: v := value.Uint() return binary.LittleEndian.AppendUint64(append(b, tag, 8), v), nil case reflect.Float32: v := math.Float32bits(float32(value.Float())) return append(b, tag, 4, byte(v), byte(v>>8), byte(v>>16), byte(v>>24)), nil case reflect.String: v := value.String() l := len(v) // support "big" string for ; l > 255; l -= 255 { b = append(b, tag, 255) b = append(b, v[:255]...) v = v[255:] } b = append(b, tag, byte(l)) return append(b, v...), nil case reflect.Array: if value.Type().Elem().Kind() == reflect.Uint8 { n := value.Len() b = append(b, tag, byte(n)) for i := 0; i < n; i++ { b = append(b, byte(value.Index(i).Uint())) } return b, nil } case reflect.Slice: for i := 0; i < value.Len(); i++ { if i > 0 { b = append(b, separator, 0) } if b, err = appendValue(b, tag, value.Index(i)); err != nil { return nil, err } } return b, nil case reflect.Struct: b = append(b, tag, 0) i := len(b) if b, err = appendStruct(b, value); err != nil { return nil, err } b[i-1] = byte(len(b) - i) // set struct size return b, nil } return nil, errors.New("tlv8: not implemented: " + value.Kind().String()) } func UnmarshalBase64(in any, out any) error { s, _ := in.(string) // protect from in == nil data, err := base64.StdEncoding.DecodeString(s) if err != nil { return err } return Unmarshal(data, out) } func UnmarshalReader(r io.Reader, n int64, v any) error { var data []byte var err error if n > 0 { data = make([]byte, n) _, err = io.ReadFull(r, data) } else { data, err = io.ReadAll(r) } if err != nil { return err } return Unmarshal(data, v) } func Unmarshal(data []byte, v any) error { if len(data) == 0 { return errors.New("tlv8: unmarshal zero data") } value := reflect.ValueOf(v) kind := value.Kind() if kind != reflect.Pointer { return errors.New("tlv8: value should be pointer: " + kind.String()) } value = value.Elem() kind = value.Kind() if kind == reflect.Interface { value = value.Elem() kind = value.Kind() } switch kind { case reflect.Slice: return unmarshalSlice(data, value) case reflect.Struct: return unmarshalStruct(data, value) } return errors.New("tlv8: not implemented: " + kind.String()) } // unmarshalTLV can return two types of errors: // - critical and then the value of []byte will be nil // - not critical and then []byte will contain the value func unmarshalTLV(b []byte, value reflect.Value) ([]byte, error) { if len(b) < 2 { return nil, errors.New("tlv8: wrong size: " + value.Type().Name()) } t := b[0] l := int(b[1]) // array item divider (t == 0x00 || t == 0xFF) if l == 0 { return b[2:], errors.New("tlv8: zero item") } var v []byte for { if len(b) < 2+l { return nil, errors.New("tlv8: wrong size: " + value.Type().Name()) } v = append(v, b[2:2+l]...) b = b[2+l:] // if size == 255 and same tag - continue read big payload if l < 255 || len(b) < 2 || b[0] != t { break } l = int(b[1]) } tag := strconv.Itoa(int(t)) valueField, ok := getStructField(value, tag) if !ok { return b, fmt.Errorf("tlv8: can't find T=%d,L=%d,V=%x for: %s", t, l, v, value.Type().Name()) } if err := unmarshalValue(v, valueField); err != nil { return nil, err } return b, nil } func unmarshalSlice(b []byte, value reflect.Value) error { valueIndex := value.Index(growSlice(value)) for len(b) > 0 { var err error if b, err = unmarshalTLV(b, valueIndex); err != nil { if b != nil { valueIndex = value.Index(growSlice(value)) continue } return err } } return nil } func unmarshalStruct(b []byte, value reflect.Value) error { for len(b) > 0 { var err error if b, err = unmarshalTLV(b, value); b == nil && err != nil { return err } } return nil } func unmarshalValue(v []byte, value reflect.Value) error { switch value.Kind() { case reflect.Uint8: if len(v) != 1 { return errors.New("tlv8: wrong size: " + value.Type().Name()) } value.SetUint(uint64(v[0])) case reflect.Uint16: if len(v) != 2 { return errors.New("tlv8: wrong size: " + value.Type().Name()) } value.SetUint(uint64(v[0]) | uint64(v[1])<<8) case reflect.Uint32: if len(v) != 4 { return errors.New("tlv8: wrong size: " + value.Type().Name()) } value.SetUint(uint64(v[0]) | uint64(v[1])<<8 | uint64(v[2])<<16 | uint64(v[3])<<24) case reflect.Uint64: if len(v) != 8 { return errors.New("tlv8: wrong size: " + value.Type().Name()) } value.SetUint(binary.LittleEndian.Uint64(v)) case reflect.Float32: f := math.Float32frombits(binary.LittleEndian.Uint32(v)) value.SetFloat(float64(f)) case reflect.String: value.SetString(string(v)) case reflect.Array: if kind := value.Type().Elem().Kind(); kind != reflect.Uint8 { return errors.New("tlv8: unsupported array: " + kind.String()) } for i, b := range v { value.Index(i).SetUint(uint64(b)) } return nil case reflect.Slice: i := growSlice(value) return unmarshalValue(v, value.Index(i)) case reflect.Struct: return unmarshalStruct(v, value) default: return errors.New("tlv8: not implemented: " + value.Kind().String()) } return nil } func getStructField(value reflect.Value, tag string) (reflect.Value, bool) { valueType := value.Type() for i := 0; i < value.NumField(); i++ { valueField := value.Field(i) if s, ok := valueType.Field(i).Tag.Lookup("tlv8"); ok && s == tag { return valueField, true } } return reflect.Value{}, false } func growSlice(value reflect.Value) int { size := value.Len() if size >= value.Cap() { newcap := value.Cap() + value.Cap()/2 if newcap < 4 { newcap = 4 } newValue := reflect.MakeSlice(value.Type(), value.Len(), newcap) reflect.Copy(newValue, value) value.Set(newValue) } if size >= value.Len() { value.SetLen(size + 1) } return size } ================================================ FILE: pkg/hap/tlv8/tlv8_test.go ================================================ package tlv8 import ( "encoding/hex" "strings" "testing" "github.com/stretchr/testify/require" ) func TestMarshal(t *testing.T) { type Struct struct { Byte byte `tlv8:"1"` Uint16 uint16 `tlv8:"2"` Uint32 uint32 `tlv8:"3"` Float32 float32 `tlv8:"4"` String string `tlv8:"5"` Slice []byte `tlv8:"6"` Array [4]byte `tlv8:"7"` } src := Struct{ Byte: 1, Uint16: 2, Uint32: 3, Float32: 1.23, String: "123", Slice: []byte{1, 2, 3}, Array: [4]byte{1, 2, 3, 4}, } b, err := Marshal(src) require.Nil(t, err) var dst Struct err = Unmarshal(b, &dst) require.Nil(t, err) require.Equal(t, src, dst) } func TestBytes(t *testing.T) { bytes := make([]byte, 255) for i := 0; i < len(bytes); i++ { bytes[i] = byte(i) } type Struct struct { String string `tlv8:"1"` } src := Struct{ String: string(bytes), } b, err := Marshal(src) require.Nil(t, err) var dst Struct err = Unmarshal(b, &dst) require.Nil(t, err) require.Equal(t, src, dst) require.Equal(t, bytes, []byte(dst.String)) } func TestVideoCodecParams(t *testing.T) { type VideoCodecParams struct { ProfileID []byte `tlv8:"1"` Level []byte `tlv8:"2"` PacketizationMode byte `tlv8:"3"` CVOEnabled []byte `tlv8:"4"` CVOID []byte `tlv8:"5"` } src, err := hex.DecodeString("0101010201000000020102030100040100") require.Nil(t, err) var v VideoCodecParams err = Unmarshal(src, &v) require.Nil(t, err) dst, err := Marshal(v) require.Nil(t, err) require.Equal(t, src, dst) } func TestInterface(t *testing.T) { type Struct struct { Byte byte `tlv8:"1"` } src := Struct{ Byte: 1, } var v1 any = &src b, err := Marshal(v1) require.Nil(t, err) require.Equal(t, []byte{1, 1, 1}, b) var dst Struct var v2 any = &dst err = Unmarshal(b, v2) require.Nil(t, err) require.Equal(t, src, dst) } func TestSlice1(t *testing.T) { var v struct { VideoAttrs []struct { Width uint16 `tlv8:"1"` Height uint16 `tlv8:"2"` Framerate uint8 `tlv8:"3"` } `tlv8:"3"` } s := `030b010280070202380403011e ff00 030b010200050202d00203011e` b1, err := hex.DecodeString(strings.ReplaceAll(s, " ", "")) require.NoError(t, err) err = Unmarshal(b1, &v) require.NoError(t, err) require.Len(t, v.VideoAttrs, 2) b2, err := Marshal(v) require.NoError(t, err) require.Equal(t, b1, b2) } func TestSlice2(t *testing.T) { var v []struct { Width uint16 `tlv8:"1"` Height uint16 `tlv8:"2"` Framerate uint8 `tlv8:"3"` } s := `010280070202380403011e ff00 010200050202d00203011e` b1, err := hex.DecodeString(strings.ReplaceAll(s, " ", "")) require.NoError(t, err) err = Unmarshal(b1, &v) require.NoError(t, err) require.Len(t, v, 2) b2, err := Marshal(v) require.NoError(t, err) require.Equal(t, b1, b2) } ================================================ FILE: pkg/hass/api.go ================================================ package hass import ( "errors" "os" "github.com/gorilla/websocket" ) type API struct { ws *websocket.Conn } func NewAPI(url, token string) (*API, error) { ws, _, err := websocket.DefaultDialer.Dial(url, nil) if err != nil { return nil, err } api := &API{ws: ws} if err = api.Auth(token); err != nil { _ = ws.Close() return nil, err } return api, nil } func (a *API) Auth(token string) error { var res ResponseAuth if err := a.ws.ReadJSON(&res); err != nil { return err } if res.Type != "auth_required" { return errors.New("hass: wrong type: " + res.Type) } s := `{"type":"auth","access_token":"` + token + `"}` if err := a.ws.WriteMessage(websocket.TextMessage, []byte(s)); err != nil { return err } if err := a.ws.ReadJSON(&res); err != nil { return err } if res.Type != "auth_ok" { return errors.New("hass: wrong type: " + res.Type) } return nil } func (a *API) Close() error { return a.ws.Close() } func (a *API) ExchangeSDP(entityID, offer string) (string, error) { var msg = map[string]any{ "id": 1, "type": "camera/web_rtc_offer", "entity_id": entityID, "offer": offer, } if err := a.ws.WriteJSON(msg); err != nil { return "", err } var res ResponseOffer if err := a.ws.ReadJSON(&res); err != nil { return "", err } if res.Type != "result" || !res.Success { return "", errors.New("hass: wrong response") } return res.Result.Answer, nil } func (a *API) GetWebRTCEntities() (map[string]string, error) { s := `{"id":1,"type":"get_states"}` if err := a.ws.WriteMessage(websocket.TextMessage, []byte(s)); err != nil { return nil, err } var res ResponseStates if err := a.ws.ReadJSON(&res); err != nil { return nil, err } if res.Type != "result" || !res.Success { return nil, errors.New("hass: wrong response") } entities := map[string]string{} for _, entity := range res.Result { if entity.Attributes.FrontendStreamType == "web_rtc" { entities[entity.Attributes.FriendlyName] = entity.EntityId } } return entities, nil } type ResponseAuth struct { Type string `json:"type"` } type ResponseStates struct { //Id int `json:"id"` Type string `json:"type"` Success bool `json:"success"` Result []struct { EntityId string `json:"entity_id"` //State string `json:"state"` Attributes struct { //ModelName string `json:"model_name"` //Brand string `json:"brand"` FrontendStreamType string `json:"frontend_stream_type"` FriendlyName string `json:"friendly_name"` //SupportedFeatures int `json:"supported_features"` } `json:"attributes"` //LastChanged time.Time `json:"last_changed"` //LastUpdated time.Time `json:"last_updated"` //Context struct { // Id string `json:"id"` // ParentId interface{} `json:"parent_id"` // UserId interface{} `json:"user_id"` //} `json:"context"` } `json:"result"` } type ResponseOffer struct { //Id int `json:"id"` Type string `json:"type"` Success bool `json:"success"` Result struct { Answer string `json:"answer"` } `json:"result"` } func SupervisorToken() string { return os.Getenv("SUPERVISOR_TOKEN") } ================================================ FILE: pkg/hass/client.go ================================================ package hass import ( "errors" "net/url" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webrtc" pion "github.com/pion/webrtc/v4" ) type Client struct { conn *webrtc.Conn } func NewClient(rawURL string) (*Client, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } query := u.Query() entityID := query.Get("entity_id") if entityID == "" { return nil, errors.New("hass: no entity_id") } var uri, token string if u.Host == "supervisor" { uri = "ws://supervisor/core/websocket" token = SupervisorToken() } else { uri = "ws://" + u.Host + "/api/websocket" token = query.Get("token") } if token == "" { return nil, errors.New("hass: no token") } // 1. Check connection to Hass hassAPI, err := NewAPI(uri, token) if err != nil { return nil, err } defer hassAPI.Close() // 2. Create WebRTC client rtcAPI, err := webrtc.NewAPI() if err != nil { return nil, err } conf := pion.Configuration{} pc, err := rtcAPI.NewPeerConnection(conf) if err != nil { return nil, err } conn := webrtc.NewConn(pc) conn.FormatName = "hass/webrtc" conn.Mode = core.ModeActiveProducer conn.Protocol = "ws" conn.URL = rawURL // https://developers.google.com/nest/device-access/traits/device/camera-live-stream#generatewebrtcstream-request-fields medias := []*core.Media{ {Kind: core.KindAudio, Direction: core.DirectionRecvonly}, {Kind: core.KindVideo, Direction: core.DirectionRecvonly}, {Kind: "app"}, // important for Nest } // 3. Create offer with candidates offer, err := conn.CreateCompleteOffer(medias) if err != nil { return nil, err } // 4. Exchange SDP via Hass answer, err := hassAPI.ExchangeSDP(entityID, offer) if err != nil { return nil, err } // 5. Set answer with remote medias if err = conn.SetAnswer(answer); err != nil { return nil, err } return &Client{conn: conn}, nil } func (c *Client) GetMedias() []*core.Media { return c.conn.GetMedias() } func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return c.conn.GetTrack(media, codec) } func (c *Client) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { return c.conn.AddTrack(media, codec, track) } func (c *Client) Start() error { return c.conn.Start() } func (c *Client) Stop() error { return c.conn.Stop() } func (c *Client) MarshalJSON() ([]byte, error) { return c.conn.MarshalJSON() } ================================================ FILE: pkg/hls/producer.go ================================================ package hls import ( "io" "net/url" "github.com/AlexxIT/go2rtc/pkg/mpegts" ) func OpenURL(u *url.URL, body io.ReadCloser) (*mpegts.Producer, error) { rd, err := NewReader(u, body) if err != nil { return nil, err } prod, err := mpegts.Open(rd) if err != nil { return nil, err } prod.FormatName = "hls/mpegts" prod.RemoteAddr = u.Host return prod, nil } ================================================ FILE: pkg/hls/reader.go ================================================ package hls import ( "bytes" "io" "net/http" "net/url" "regexp" "time" "github.com/AlexxIT/go2rtc/pkg/core" ) type reader struct { client *http.Client request *http.Request playlist []byte lastSegment []byte lastTime time.Time buf []byte } func NewReader(u *url.URL, body io.ReadCloser) (io.Reader, error) { b, err := io.ReadAll(body) if err != nil { return nil, err } var rawURL string re := regexp.MustCompile(`#EXT-X-STREAM-INF.+?\n(\S+)`) m := re.FindSubmatch(b) if m != nil { ref, err := url.Parse(string(m[1])) if err != nil { return nil, err } rawURL = u.ResolveReference(ref).String() } else { rawURL = u.String() } req, err := http.NewRequest("GET", rawURL, nil) if err != nil { return nil, err } rd := &reader{ client: &http.Client{Timeout: core.ConnDialTimeout}, request: req, } return rd, nil } func (r *reader) Read(dst []byte) (n int, err error) { // 1. Check temporary tempbuffer if len(r.buf) == 0 { src, err2 := r.getSegment() if err2 != nil { return 0, err2 } // 2. Check if the message fits in the buffer if len(src) <= len(dst) { return copy(dst, src), nil } // 3. Put the message into a temporary buffer r.buf = src } // 4. Send temporary buffer n = copy(dst, r.buf) r.buf = r.buf[n:] return } func (r *reader) Close() error { r.client.Transport = r // after close we fail on next request return nil } func (r *reader) RoundTrip(_ *http.Request) (*http.Response, error) { return nil, io.EOF } func (r *reader) getSegment() ([]byte, error) { for i := 0; i < 10; i++ { if r.playlist == nil { if wait := time.Second - time.Since(r.lastTime); wait > 0 { time.Sleep(wait) } // 1. Load playlist res, err := r.client.Do(r.request) if err != nil { return nil, err } r.playlist, err = io.ReadAll(res.Body) if err != nil { return nil, err } r.lastTime = time.Now() //log.Printf("[hls] load playlist\n%s", r.playlist) } for r.playlist != nil { // 2. Remove all previous segments from playlist if i := bytes.Index(r.playlist, r.lastSegment); i > 0 { r.playlist = r.playlist[i:] } // 3. Get link to new segment segment := getSegment(r.playlist) if segment == nil { r.playlist = nil break } //log.Printf("[hls] load segment: %s", segment) ref, err := url.Parse(string(segment)) if err != nil { return nil, err } ref = r.request.URL.ResolveReference(ref) res, err := r.client.Get(ref.String()) if err != nil { return nil, err } r.lastSegment = segment return io.ReadAll(res.Body) } } return nil, io.EOF } func getSegment(src []byte) []byte { for ok := false; !ok; { ok = bytes.HasPrefix(src, []byte("#EXTINF")) i := bytes.IndexByte(src, '\n') + 1 if i == 0 { return nil } src = src[i:] } if i := bytes.IndexByte(src, '\n'); i > 0 { return src[:i] } return src } ================================================ FILE: pkg/homekit/consumer.go ================================================ package homekit import ( "fmt" "io" "math/rand" "net" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/hap/camera" "github.com/AlexxIT/go2rtc/pkg/opus" "github.com/AlexxIT/go2rtc/pkg/srtp" "github.com/pion/rtp" ) type Consumer struct { core.Connection conn net.Conn srtp *srtp.Server deadline *time.Timer sessionID string videoSession *srtp.Session audioSession *srtp.Session audioRTPTime byte } func NewConsumer(conn net.Conn, server *srtp.Server) *Consumer { medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecH264}, }, }, { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecOpus}, }, }, } return &Consumer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "homekit", Protocol: "rtp", RemoteAddr: conn.RemoteAddr().String(), Medias: medias, Transport: conn, }, conn: conn, srtp: server, } } func (c *Consumer) SessionID() string { return c.sessionID } func (c *Consumer) SetOffer(offer *camera.SetupEndpointsRequest) { c.sessionID = offer.SessionID c.videoSession = &srtp.Session{ Remote: &srtp.Endpoint{ Addr: offer.Address.IPAddr, Port: offer.Address.VideoRTPPort, MasterKey: []byte(offer.VideoCrypto.MasterKey), MasterSalt: []byte(offer.VideoCrypto.MasterSalt), }, } c.audioSession = &srtp.Session{ Remote: &srtp.Endpoint{ Addr: offer.Address.IPAddr, Port: offer.Address.AudioRTPPort, MasterKey: []byte(offer.AudioCrypto.MasterKey), MasterSalt: []byte(offer.AudioCrypto.MasterSalt), }, } } func (c *Consumer) GetAnswer() *camera.SetupEndpointsResponse { c.videoSession.Local = c.srtpEndpoint() c.audioSession.Local = c.srtpEndpoint() return &camera.SetupEndpointsResponse{ SessionID: c.sessionID, Status: camera.StreamingStatusAvailable, Address: camera.Address{ IPAddr: c.videoSession.Local.Addr, VideoRTPPort: c.videoSession.Local.Port, AudioRTPPort: c.audioSession.Local.Port, }, VideoCrypto: camera.SRTPCryptoSuite{ MasterKey: string(c.videoSession.Local.MasterKey), MasterSalt: string(c.videoSession.Local.MasterSalt), }, AudioCrypto: camera.SRTPCryptoSuite{ MasterKey: string(c.audioSession.Local.MasterKey), MasterSalt: string(c.audioSession.Local.MasterSalt), }, VideoSSRC: c.videoSession.Local.SSRC, AudioSSRC: c.audioSession.Local.SSRC, } } func (c *Consumer) SetConfig(conf *camera.SelectedStreamConfiguration) bool { if c.sessionID != conf.Control.SessionID { return false } c.SDP = fmt.Sprintf("%+v\n%+v", conf.VideoCodec, conf.AudioCodec) c.videoSession.Remote.SSRC = conf.VideoCodec.RTPParams[0].SSRC c.videoSession.PayloadType = conf.VideoCodec.RTPParams[0].PayloadType c.videoSession.RTCPInterval = toDuration(conf.VideoCodec.RTPParams[0].RTCPInterval) c.audioSession.Remote.SSRC = conf.AudioCodec.RTPParams[0].SSRC c.audioSession.PayloadType = conf.AudioCodec.RTPParams[0].PayloadType c.audioSession.RTCPInterval = toDuration(conf.AudioCodec.RTPParams[0].RTCPInterval) c.audioRTPTime = conf.AudioCodec.CodecParams[0].RTPTime[0] c.srtp.AddSession(c.videoSession) c.srtp.AddSession(c.audioSession) return true } func (c *Consumer) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { var session *srtp.Session if codec.Kind() == core.KindVideo { session = c.videoSession } else { session = c.audioSession } sender := core.NewSender(media, track.Codec) if c.deadline == nil { c.deadline = time.NewTimer(time.Second * 30) sender.Handler = func(packet *rtp.Packet) { c.deadline.Reset(core.ConnDeadline) if n, err := session.WriteRTP(packet); err == nil { c.Send += n } } } else { sender.Handler = func(packet *rtp.Packet) { if n, err := session.WriteRTP(packet); err == nil { c.Send += n } } } switch codec.Name { case core.CodecH264: sender.Handler = h264.RTPPay(1378, sender.Handler) if track.Codec.IsRTP() { sender.Handler = h264.RTPDepay(track.Codec, sender.Handler) } else { sender.Handler = h264.RepairAVCC(track.Codec, sender.Handler) } case core.CodecOpus: sender.Handler = opus.RepackToHAP(c.audioRTPTime, sender.Handler) } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } func (c *Consumer) WriteTo(io.Writer) (int64, error) { if c.deadline != nil { <-c.deadline.C } return 0, nil } func (c *Consumer) Stop() error { if c.deadline != nil { c.deadline.Reset(0) } return c.Connection.Stop() } func (c *Consumer) srtpEndpoint() *srtp.Endpoint { addr := c.conn.LocalAddr().(*net.TCPAddr) return &srtp.Endpoint{ Addr: addr.IP.To4().String(), Port: uint16(c.srtp.Port()), MasterKey: []byte(core.RandString(16, 0)), MasterSalt: []byte(core.RandString(14, 0)), SSRC: rand.Uint32(), } } func toDuration(seconds float32) time.Duration { return time.Duration(seconds * float32(time.Second)) } ================================================ FILE: pkg/homekit/helpers.go ================================================ package homekit import ( "encoding/hex" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/hap/camera" ) var videoCodecs = [...]string{core.CodecH264} var videoProfiles = [...]string{"4200", "4D00", "6400"} var videoLevels = [...]string{"1F", "20", "28"} func videoToMedia(codecs []camera.VideoCodecConfiguration) *core.Media { media := &core.Media{ Kind: core.KindVideo, Direction: core.DirectionRecvonly, } for _, codec := range codecs { for _, param := range codec.CodecParams { // get best profile and level profileID := core.Max(param.ProfileID) level := core.Max(param.Level) profile := videoProfiles[profileID] + videoLevels[level] mediaCodec := &core.Codec{ Name: videoCodecs[codec.CodecType], ClockRate: 90000, FmtpLine: "profile-level-id=" + profile, } media.Codecs = append(media.Codecs, mediaCodec) } } return media } var audioCodecs = [...]string{core.CodecPCMU, core.CodecPCMA, core.CodecELD, core.CodecOpus} var audioSampleRates = [...]uint32{8000, 16000, 24000} func audioToMedia(codecs []camera.AudioCodecConfiguration) *core.Media { media := &core.Media{ Kind: core.KindAudio, Direction: core.DirectionRecvonly, } for _, codec := range codecs { for _, param := range codec.CodecParams { for _, sampleRate := range param.SampleRate { mediaCodec := &core.Codec{ Name: audioCodecs[codec.CodecType], ClockRate: audioSampleRates[sampleRate], Channels: param.Channels, } if mediaCodec.Name == core.CodecELD { // only this version works with FFmpeg conf := aac.EncodeConfig(aac.TypeAACELD, 24000, 1, true) mediaCodec.FmtpLine = aac.FMTP + hex.EncodeToString(conf) } media.Codecs = append(media.Codecs, mediaCodec) } } } return media } func trackToVideo(track *core.Receiver, video0 *camera.VideoCodecConfiguration, maxWidth, maxHeight int) *camera.VideoCodecConfiguration { profileID := video0.CodecParams[0].ProfileID[0] level := video0.CodecParams[0].Level[0] var attrs camera.VideoCodecAttributes if track != nil { profile := h264.GetProfileLevelID(track.Codec.FmtpLine) for i, s := range videoProfiles { if s == profile[:4] { profileID = byte(i) break } } for i, s := range videoLevels { if s == profile[4:] { level = byte(i) break } } for _, s := range video0.VideoAttrs { if (maxWidth > 0 && int(s.Width) > maxWidth) || (maxHeight > 0 && int(s.Height) > maxHeight) { continue } if s.Width > attrs.Width || s.Height > attrs.Height { attrs = s } } } return &camera.VideoCodecConfiguration{ CodecType: video0.CodecType, CodecParams: []camera.VideoCodecParameters{ { ProfileID: []byte{profileID}, Level: []byte{level}, }, }, VideoAttrs: []camera.VideoCodecAttributes{attrs}, } } func trackToAudio(track *core.Receiver, audio0 *camera.AudioCodecConfiguration) *camera.AudioCodecConfiguration { codecType := audio0.CodecType channels := audio0.CodecParams[0].Channels sampleRate := audio0.CodecParams[0].SampleRate[0] if track != nil { channels = uint8(track.Codec.Channels) for i, s := range audioCodecs { if s == track.Codec.Name { codecType = byte(i) break } } for i, s := range audioSampleRates { if s == track.Codec.ClockRate { sampleRate = byte(i) break } } } return &camera.AudioCodecConfiguration{ CodecType: codecType, CodecParams: []camera.AudioCodecParameters{ { Channels: channels, SampleRate: []byte{sampleRate}, RTPTime: []uint8{20}, }, }, } } ================================================ FILE: pkg/homekit/log/debug.go ================================================ package log import ( "bytes" "io" "log" "net/http" ) func Debug(v any) { switch v := v.(type) { case *http.Request: if v == nil { return } if v.ContentLength != 0 { b, err := io.ReadAll(v.Body) if err != nil { panic(err) } v.Body = io.NopCloser(bytes.NewReader(b)) log.Printf("[homekit] request: %s %s\n%s", v.Method, v.RequestURI, b) } else { log.Printf("[homekit] request: %s %s ", v.Method, v.RequestURI) } case *http.Response: if v == nil { return } if v.Header.Get("Content-Type") == "image/jpeg" { log.Printf("[homekit] response: %d ", v.StatusCode) return } if v.ContentLength != 0 { b, err := io.ReadAll(v.Body) if err != nil { panic(err) } v.Body = io.NopCloser(bytes.NewReader(b)) log.Printf("[homekit] response: %s %d\n%s", v.Proto, v.StatusCode, b) } else { log.Printf("[homekit] response: %s %d ", v.Proto, v.StatusCode) } } } ================================================ FILE: pkg/homekit/producer.go ================================================ package homekit import ( "errors" "fmt" "math/rand" "net" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/hap" "github.com/AlexxIT/go2rtc/pkg/hap/camera" "github.com/AlexxIT/go2rtc/pkg/srtp" "github.com/pion/rtp" ) // Deprecated: rename to Producer type Client struct { core.Connection hap *hap.Client srtp *srtp.Server videoConfig camera.SupportedVideoStreamConfiguration audioConfig camera.SupportedAudioStreamConfiguration videoSession *srtp.Session audioSession *srtp.Session stream *camera.Stream MaxWidth int `json:"-"` MaxHeight int `json:"-"` Bitrate int `json:"-"` // in bits/s } func Dial(rawURL string, server *srtp.Server) (*Client, error) { conn, err := hap.Dial(rawURL) if err != nil { return nil, err } client := &Client{ Connection: core.Connection{ ID: core.NewID(), FormatName: "homekit", Protocol: "udp", RemoteAddr: conn.Conn.RemoteAddr().String(), Source: rawURL, Transport: conn, }, hap: conn, srtp: server, } return client, nil } func (c *Client) Conn() net.Conn { return c.hap.Conn } func (c *Client) GetMedias() []*core.Media { if c.Medias != nil { return c.Medias } acc, err := c.hap.GetFirstAccessory() if err != nil { return nil } char := acc.GetCharacter(camera.TypeSupportedVideoStreamConfiguration) if char == nil { return nil } if err = char.ReadTLV8(&c.videoConfig); err != nil { return nil } char = acc.GetCharacter(camera.TypeSupportedAudioStreamConfiguration) if char == nil { return nil } if err = char.ReadTLV8(&c.audioConfig); err != nil { return nil } c.SDP = fmt.Sprintf("%+v\n%+v", c.videoConfig, c.audioConfig) c.Medias = []*core.Media{ videoToMedia(c.videoConfig.Codecs), audioToMedia(c.audioConfig.Codecs), { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ { Name: core.CodecJPEG, ClockRate: 90000, PayloadType: core.PayloadTypeRAW, }, }, }, } return c.Medias } func (c *Client) Start() error { if c.Receivers == nil { return errors.New("producer without tracks") } if c.Receivers[0].Codec.Name == core.CodecJPEG { return c.startMJPEG() } videoTrack := c.trackByKind(core.KindVideo) videoCodec := trackToVideo(videoTrack, &c.videoConfig.Codecs[0], c.MaxWidth, c.MaxHeight) audioTrack := c.trackByKind(core.KindAudio) audioCodec := trackToAudio(audioTrack, &c.audioConfig.Codecs[0]) c.videoSession = &srtp.Session{Local: c.srtpEndpoint()} c.audioSession = &srtp.Session{Local: c.srtpEndpoint()} var err error c.stream, err = camera.NewStream(c.hap, videoCodec, audioCodec, c.videoSession, c.audioSession, c.Bitrate) if err != nil { return err } c.srtp.AddSession(c.videoSession) c.srtp.AddSession(c.audioSession) deadline := time.NewTimer(core.ConnDeadline) if videoTrack != nil { c.videoSession.OnReadRTP = func(packet *rtp.Packet) { deadline.Reset(core.ConnDeadline) videoTrack.WriteRTP(packet) c.Recv += len(packet.Payload) } if audioTrack != nil { c.audioSession.OnReadRTP = func(packet *rtp.Packet) { audioTrack.WriteRTP(packet) c.Recv += len(packet.Payload) } } } else { c.audioSession.OnReadRTP = func(packet *rtp.Packet) { deadline.Reset(core.ConnDeadline) audioTrack.WriteRTP(packet) c.Recv += len(packet.Payload) } } if c.audioSession.OnReadRTP != nil { c.audioSession.OnReadRTP = timekeeper(c.audioSession.OnReadRTP) } <-deadline.C return nil } func (c *Client) Stop() error { if c.videoSession != nil && c.videoSession.Remote != nil { c.srtp.DelSession(c.videoSession) } if c.audioSession != nil && c.audioSession.Remote != nil { c.srtp.DelSession(c.audioSession) } return c.Connection.Stop() } func (c *Client) trackByKind(kind string) *core.Receiver { for _, receiver := range c.Receivers { if receiver.Codec.Kind() == kind { return receiver } } return nil } func (c *Client) startMJPEG() error { receiver := c.Receivers[0] for { b, err := c.hap.GetImage(1920, 1080) if err != nil { return err } c.Recv += len(b) packet := &rtp.Packet{ Header: rtp.Header{Timestamp: core.Now90000()}, Payload: b, } receiver.WriteRTP(packet) } } func (c *Client) srtpEndpoint() *srtp.Endpoint { return &srtp.Endpoint{ Addr: c.hap.LocalIP(), Port: uint16(c.srtp.Port()), MasterKey: []byte(core.RandString(16, 0)), MasterSalt: []byte(core.RandString(14, 0)), SSRC: rand.Uint32(), } } func timekeeper(handler core.HandlerFunc) core.HandlerFunc { const sampleRate = 16000 const sampleSize = 480 var send time.Duration var firstTime time.Time return func(packet *rtp.Packet) { now := time.Now() if send != 0 { elapsed := now.Sub(firstTime) * sampleRate / time.Second if send+sampleSize > elapsed { return // drop overflow frame } } else { firstTime = now } send += sampleSize packet.Timestamp = uint32(send) handler(packet) } } ================================================ FILE: pkg/homekit/proxy.go ================================================ package homekit import ( "bufio" "bytes" "encoding/json" "io" "net" "net/http" "time" "github.com/AlexxIT/go2rtc/pkg/hap" "github.com/AlexxIT/go2rtc/pkg/hap/camera" "github.com/AlexxIT/go2rtc/pkg/hap/hds" "github.com/AlexxIT/go2rtc/pkg/hap/tlv8" ) type ServerProxy interface { ServerPair AddConn(conn any) DelConn(conn any) } func ProxyHandler(srv ServerProxy, acc net.Conn) HandlerFunc { return func(con net.Conn) error { defer con.Close() pr := &Proxy{ con: con.(*hap.Conn), acc: acc.(*hap.Conn), res: make(chan *http.Response), } // accessory (ex. Camera) => controller (ex. iPhone) go pr.handleAcc() // controller => accessory return pr.handleCon(srv) } } type Proxy struct { con *hap.Conn acc *hap.Conn res chan *http.Response } func (p *Proxy) handleCon(srv ServerProxy) error { var hdsCharIID uint64 rd := bufio.NewReader(p.con) for { req, err := http.ReadRequest(rd) if err != nil { return err } var hdsConSalt string switch { case req.Method == "POST" && req.URL.Path == hap.PathPairings: var res *http.Response if res, err = handlePairings(req, srv); err != nil { return err } if err = res.Write(p.con); err != nil { return err } continue case req.Method == "PUT" && req.URL.Path == hap.PathCharacteristics && hdsCharIID != 0: body, _ := io.ReadAll(req.Body) var v hap.JSONCharacters _ = json.Unmarshal(body, &v) for _, char := range v.Value { if char.IID == hdsCharIID { var hdsReq camera.SetupDataStreamTransportRequest _ = tlv8.UnmarshalBase64(char.Value, &hdsReq) hdsConSalt = hdsReq.ControllerKeySalt break } } req.Body = io.NopCloser(bytes.NewReader(body)) } if err = req.Write(p.acc); err != nil { return err } res := <-p.res switch { case req.Method == "GET" && req.URL.Path == hap.PathAccessories: body, _ := io.ReadAll(res.Body) var v hap.JSONAccessories if err = json.Unmarshal(body, &v); err != nil { return err } for _, acc := range v.Value { if char := acc.GetCharacter(camera.TypeSetupDataStreamTransport); char != nil { hdsCharIID = char.IID } break } res.Body = io.NopCloser(bytes.NewReader(body)) case hdsConSalt != "": body, _ := io.ReadAll(res.Body) var v hap.JSONCharacters _ = json.Unmarshal(body, &v) for i, char := range v.Value { if char.IID == hdsCharIID { var hdsRes camera.SetupDataStreamTransportResponse _ = tlv8.UnmarshalBase64(char.Value, &hdsRes) hdsAccSalt := hdsRes.AccessoryKeySalt hdsPort := int(hdsRes.TransportTypeSessionParameters.TCPListeningPort) // swtich accPort to conPort hdsPort, err = p.listenHDS(srv, hdsPort, hdsConSalt+hdsAccSalt) if err != nil { return err } hdsRes.TransportTypeSessionParameters.TCPListeningPort = uint16(hdsPort) if v.Value[i].Value, err = tlv8.MarshalBase64(hdsRes); err != nil { return err } body, _ = json.Marshal(v) res.ContentLength = int64(len(body)) break } } res.Body = io.NopCloser(bytes.NewReader(body)) } if err = res.Write(p.con); err != nil { return err } } } func (p *Proxy) handleAcc() error { rd := bufio.NewReader(p.acc) for { res, err := hap.ReadResponse(rd, nil) if err != nil { return err } if res.Proto == hap.ProtoEvent { if err = hap.WriteEvent(p.con, res); err != nil { return err } continue } // important to read body before next read response body, err := io.ReadAll(res.Body) if err != nil { return err } res.Body = io.NopCloser(bytes.NewReader(body)) p.res <- res } } func (p *Proxy) listenHDS(srv ServerProxy, accPort int, salt string) (int, error) { // The TCP port range for HDS must be >= 32768. ln, err := net.ListenTCP("tcp", nil) if err != nil { return 0, err } go func() { defer ln.Close() _ = ln.SetDeadline(time.Now().Add(30 * time.Second)) // raw controller conn conn1, err := ln.Accept() if err != nil { return } defer conn1.Close() // secured controller conn (controlle=false because we are accessory) con, err := hds.NewConn(conn1, p.con.SharedKey, salt, false) if err != nil { return } srv.AddConn(con) defer srv.DelConn(con) accIP := p.acc.RemoteAddr().(*net.TCPAddr).IP // raw accessory conn conn2, err := net.DialTCP("tcp", nil, &net.TCPAddr{IP: accIP, Port: accPort}) if err != nil { return } defer conn2.Close() // secured accessory conn (controller=true because we are controller) acc, err := hds.NewConn(conn2, p.acc.SharedKey, salt, true) if err != nil { return } go io.Copy(con, acc) _, _ = io.Copy(acc, con) }() conPort := ln.Addr().(*net.TCPAddr).Port return conPort, nil } ================================================ FILE: pkg/homekit/server.go ================================================ package homekit import ( "bufio" "bytes" "encoding/json" "errors" "io" "net" "net/http" "strconv" "strings" "github.com/AlexxIT/go2rtc/pkg/hap" "github.com/AlexxIT/go2rtc/pkg/hap/tlv8" ) type HandlerFunc func(net.Conn) error type Server interface { ServerPair ServerAccessory } type ServerPair interface { GetPair(id string) []byte AddPair(id string, public []byte, permissions byte) DelPair(id string) } type ServerAccessory interface { GetAccessories(conn net.Conn) []*hap.Accessory GetCharacteristic(conn net.Conn, aid uint8, iid uint64) any SetCharacteristic(conn net.Conn, aid uint8, iid uint64, value any) GetImage(conn net.Conn, width, height int) []byte } func ServerHandler(server Server) HandlerFunc { return handleRequest(func(conn net.Conn, req *http.Request) (*http.Response, error) { switch req.URL.Path { case hap.PathPairings: return handlePairings(req, server) case hap.PathAccessories: body := hap.JSONAccessories{Value: server.GetAccessories(conn)} return makeResponse(hap.MimeJSON, body) case hap.PathCharacteristics: switch req.Method { case "GET": var v hap.JSONCharacters id := req.URL.Query().Get("id") for _, id = range strings.Split(id, ",") { s1, s2, _ := strings.Cut(id, ".") aid, _ := strconv.Atoi(s1) iid, _ := strconv.ParseUint(s2, 10, 64) val := server.GetCharacteristic(conn, uint8(aid), iid) v.Value = append(v.Value, hap.JSONCharacter{AID: uint8(aid), IID: iid, Value: val}) } return makeResponse(hap.MimeJSON, v) case "PUT": var v struct { Value []struct { AID uint8 `json:"aid"` IID uint64 `json:"iid"` Value any `json:"value"` } `json:"characteristics"` } if err := json.NewDecoder(req.Body).Decode(&v); err != nil { return nil, err } for _, char := range v.Value { server.SetCharacteristic(conn, char.AID, char.IID, char.Value) } res := &http.Response{ StatusCode: http.StatusNoContent, Proto: "HTTP", ProtoMajor: 1, ProtoMinor: 1, } return res, nil } case hap.PathResource: var v struct { Width int `json:"image-width"` Height int `json:"image-height"` Type string `json:"resource-type"` } if err := json.NewDecoder(req.Body).Decode(&v); err != nil { return nil, err } body := server.GetImage(conn, v.Width, v.Height) return makeResponse("image/jpeg", body) } return nil, errors.New("hap: unsupported path: " + req.RequestURI) }) } func handleRequest(handle func(conn net.Conn, req *http.Request) (*http.Response, error)) HandlerFunc { return func(conn net.Conn) error { rw := bufio.NewReaderSize(conn, 16*1024) wr := bufio.NewWriterSize(conn, 16*1024) for { req, err := http.ReadRequest(rw) //debug(req) if err != nil { return err } res, err := handle(conn, req) //debug(res) if err != nil { return err } if err = res.Write(wr); err != nil { return err } if err = wr.Flush(); err != nil { return err } } } } func handlePairings(req *http.Request, srv ServerPair) (*http.Response, error) { cmd := struct { Method byte `tlv8:"0"` Identifier string `tlv8:"1"` PublicKey string `tlv8:"3"` State byte `tlv8:"6"` Permissions byte `tlv8:"11"` }{} if err := tlv8.UnmarshalReader(req.Body, req.ContentLength, &cmd); err != nil { return nil, err } switch cmd.Method { case 3: // add srv.AddPair(cmd.Identifier, []byte(cmd.PublicKey), cmd.Permissions) case 4: // delete srv.DelPair(cmd.Identifier) } body := struct { State byte `tlv8:"6"` }{ State: hap.StateM2, } return makeResponse(hap.MimeTLV8, body) } func makeResponse(mime string, v any) (*http.Response, error) { var body []byte var err error switch mime { case hap.MimeJSON: body, err = json.Marshal(v) case hap.MimeTLV8: body, err = tlv8.Marshal(v) case "image/jpeg": body = v.([]byte) } if err != nil { return nil, err } res := &http.Response{ StatusCode: http.StatusOK, Proto: "HTTP", ProtoMajor: 1, ProtoMinor: 1, Header: http.Header{ "Content-Type": []string{mime}, "Content-Length": []string{strconv.Itoa(len(body))}, }, ContentLength: int64(len(body)), Body: io.NopCloser(bytes.NewReader(body)), } return res, nil } ================================================ FILE: pkg/image/producer.go ================================================ package image import ( "errors" "io" "net/http" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/tcp" "github.com/pion/rtp" ) type Producer struct { core.Connection closed bool res *http.Response } func Open(res *http.Response) (*Producer, error) { return &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "image", Protocol: "http", RemoteAddr: res.Request.URL.Host, Transport: res.Body, Medias: []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ { Name: core.CodecJPEG, ClockRate: 90000, PayloadType: core.PayloadTypeRAW, }, }, }, }, }, res: res, }, nil } func (c *Producer) Start() error { body, err := io.ReadAll(c.res.Body) if err != nil { return err } pkt := &rtp.Packet{ Header: rtp.Header{Timestamp: core.Now90000()}, Payload: body, } c.Receivers[0].WriteRTP(pkt) c.Recv += len(body) req := c.res.Request for !c.closed { res, err := tcp.Do(req) if err != nil { return err } if res.StatusCode != http.StatusOK { return errors.New("wrong status: " + res.Status) } body, err = io.ReadAll(res.Body) if err != nil { return err } c.Recv += len(body) pkt = &rtp.Packet{ Header: rtp.Header{Timestamp: core.Now90000()}, Payload: body, } c.Receivers[0].WriteRTP(pkt) } return nil } func (c *Producer) Stop() error { c.closed = true return c.Connection.Stop() } ================================================ FILE: pkg/ioctl/README.md ================================================ # IOCTL This is just an example how Linux IOCTL constants works. ================================================ FILE: pkg/ioctl/ioctl.go ================================================ package ioctl import ( "bytes" ) func Str(b []byte) string { if i := bytes.IndexByte(b, 0); i >= 0 { return string(b[:i]) } return string(b) } func io(mode byte, type_ byte, number byte, size uint16) uintptr { return uintptr(mode)<<30 | uintptr(size)<<16 | uintptr(type_)<<8 | uintptr(number) } func IOR(type_ byte, number byte, size uint16) uintptr { return io(read, type_, number, size) } func IOW(type_ byte, number byte, size uint16) uintptr { return io(write, type_, number, size) } func IORW(type_ byte, number byte, size uint16) uintptr { return io(read|write, type_, number, size) } ================================================ FILE: pkg/ioctl/ioctl_be.go ================================================ //go:build arm || arm64 || 386 || amd64 package ioctl const ( write = 1 read = 2 ) ================================================ FILE: pkg/ioctl/ioctl_le.go ================================================ //go:build mipsle package ioctl const ( read = 1 write = 2 ) ================================================ FILE: pkg/ioctl/ioctl_linux.go ================================================ package ioctl import ( "syscall" "unsafe" ) func Ioctl(fd int, req uint, arg unsafe.Pointer) error { _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(req), uintptr(arg)) if err != 0 { return err } return nil } ================================================ FILE: pkg/ioctl/ioctl_test.go ================================================ package ioctl import ( "runtime" "testing" "github.com/stretchr/testify/require" ) func TestIOR(t *testing.T) { // #define SNDRV_PCM_IOCTL_INFO _IOR('A', 0x01, struct snd_pcm_info) if runtime.GOARCH == "arm64" { c := IOR('A', 0x01, 288) require.Equal(t, uintptr(0x81204101), c) } } ================================================ FILE: pkg/isapi/backchannel.go ================================================ package isapi import ( "encoding/json" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) func (c *Client) GetMedias() []*core.Media { return c.medias } func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return nil, core.ErrCantGetTrack } func (c *Client) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error { if c.sender == nil { c.sender = core.NewSender(media, track.Codec) c.sender.Handler = func(packet *rtp.Packet) { if c.conn == nil { return } c.send += len(packet.Payload) _, _ = c.conn.Write(packet.Payload) } } c.sender.HandleRTP(track) return nil } func (c *Client) Start() (err error) { if err = c.Open(); err != nil { return } return } func (c *Client) Stop() (err error) { if c.sender != nil { c.sender.Close() } if c.conn != nil { _ = c.Close() return c.conn.Close() } return nil } func (c *Client) MarshalJSON() ([]byte, error) { info := &core.Connection{ ID: core.ID(c), FormatName: "isapi", Protocol: "http", Medias: c.medias, Send: c.send, } if c.conn != nil { info.RemoteAddr = c.conn.RemoteAddr().String() } if c.sender != nil { info.Senders = []*core.Sender{c.sender} } return json.Marshal(info) } ================================================ FILE: pkg/isapi/client.go ================================================ package isapi import ( "errors" "io" "net" "net/http" "net/url" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/tcp" ) // Deprecated: should be rewritten to core.Connection type Client struct { core.Listener url string channel string conn net.Conn medias []*core.Media sender *core.Sender send int } func Dial(rawURL string) (*Client, error) { // check if url is valid url u, err := url.Parse(rawURL) if err != nil { return nil, err } u.Scheme = "http" u.Path = "" client := &Client{url: u.String()} if err = client.Dial(); err != nil { return nil, err } return client, err } func (c *Client) Dial() (err error) { link := c.url + "/ISAPI/System/TwoWayAudio/channels" req, err := http.NewRequest("GET", link, nil) if err != nil { return err } res, err := tcp.Do(req) if err != nil { return } if res.StatusCode != http.StatusOK { tcp.Close(res) return errors.New(res.Status) } b, err := io.ReadAll(res.Body) if err != nil { return err } xml := string(b) codec := core.Between(xml, ``, `<`) switch codec { case "G.711ulaw": codec = core.CodecPCMU case "G.711alaw": codec = core.CodecPCMA default: return nil } c.channel = core.Between(xml, ``, `<`) media := &core.Media{ Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: codec, ClockRate: 8000}, }, } c.medias = append(c.medias, media) return nil } func (c *Client) Open() (err error) { // Hikvision ISAPI may not accept a new open request if the previous one was not closed (e.g. // using the test button on-camera or via curl command) but a close request can be sent even if // the audio is already closed. So, we send a close request first and then open it again. Seems // janky but it works. if err = c.Close(); err != nil { return err } link := c.url + "/ISAPI/System/TwoWayAudio/channels/" + c.channel req, err := http.NewRequest("PUT", link+"/open", nil) if err != nil { return err } res, err := tcp.Do(req) if err != nil { return } tcp.Close(res) ctx, pconn := tcp.WithConn() req, err = http.NewRequestWithContext(ctx, "PUT", link+"/audioData", nil) if err != nil { return err } req.Header.Set("Content-Type", "application/octet-stream") req.Header.Set("Content-Length", "0") res, err = tcp.Do(req) if err != nil { return err } c.conn = *pconn // just block until c.conn closed b := make([]byte, 1) _, _ = c.conn.Read(b) tcp.Close(res) return nil } func (c *Client) Close() (err error) { link := c.url + "/ISAPI/System/TwoWayAudio/channels/" + c.channel req, err := http.NewRequest("PUT", link+"/close", nil) if err != nil { return err } res, err := tcp.Do(req) if err != nil { return err } tcp.Close(res) return nil } //type XMLChannels struct { // Channels []Channel `xml:"TwoWayAudioChannel"` //} //type Channel struct { // ID string `xml:"id"` // Enabled string `xml:"enabled"` // Codec string `xml:"audioCompressionType"` //} ================================================ FILE: pkg/iso/atoms.go ================================================ package iso const ( Ftyp = "ftyp" Moov = "moov" MoovMvhd = "mvhd" MoovTrak = "trak" MoovTrakTkhd = "tkhd" MoovTrakMdia = "mdia" MoovTrakMdiaMdhd = "mdhd" MoovTrakMdiaHdlr = "hdlr" MoovTrakMdiaMinf = "minf" MoovTrakMdiaMinfVmhd = "vmhd" MoovTrakMdiaMinfSmhd = "smhd" MoovTrakMdiaMinfDinf = "dinf" MoovTrakMdiaMinfDinfDref = "dref" MoovTrakMdiaMinfDinfDrefUrl = "url " MoovTrakMdiaMinfStbl = "stbl" MoovTrakMdiaMinfStblStsd = "stsd" MoovTrakMdiaMinfStblStts = "stts" MoovTrakMdiaMinfStblStsc = "stsc" MoovTrakMdiaMinfStblStsz = "stsz" MoovTrakMdiaMinfStblStco = "stco" MoovMvex = "mvex" MoovMvexTrex = "trex" Moof = "moof" MoofMfhd = "mfhd" MoofTraf = "traf" MoofTrafTfhd = "tfhd" MoofTrafTfdt = "tfdt" MoofTrafTrun = "trun" Mdat = "mdat" ) const ( sampleIsNonSync = 0x10000 sampleDependsOn1 = 0x1000000 sampleDependsOn2 = 0x2000000 SampleVideoIFrame = sampleDependsOn2 SampleVideoNonIFrame = sampleDependsOn1 | sampleIsNonSync SampleAudio = sampleDependsOn2 //sampleIsNonSync ) func (m *Movie) WriteFileType() { m.StartAtom(Ftyp) m.WriteString("iso5") m.WriteUint32(512) m.WriteString("iso5") m.WriteString("iso6") m.WriteString("mp41") m.EndAtom() } func (m *Movie) WriteMovieHeader() { m.StartAtom(MoovMvhd) m.Skip(1) // version m.Skip(3) // flags m.Skip(4) // create time m.Skip(4) // modify time m.WriteUint32(1000) // time scale m.Skip(4) // duration m.WriteFloat32(1) // preferred rate m.WriteFloat16(1) // preferred volume m.Skip(10) // reserved m.WriteMatrix() m.Skip(6 * 4) // predefined? m.WriteUint32(0xFFFFFFFF) // next track ID m.EndAtom() } func (m *Movie) WriteTrackHeader(id uint32, width, height uint16) { const ( TkhdTrackEnabled = 0x0001 TkhdTrackInMovie = 0x0002 TkhdTrackInPreview = 0x0004 TkhdTrackInPoster = 0x0008 ) // https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-32963 m.StartAtom(MoovTrakTkhd) m.Skip(1) // version m.WriteUint24(TkhdTrackEnabled | TkhdTrackInMovie) m.Skip(4) // create time m.Skip(4) // modify time m.WriteUint32(id) // trackID m.Skip(4) // reserved m.Skip(4) // duration m.Skip(8) // reserved m.Skip(2) // layer if width > 0 { m.Skip(2) m.Skip(2) } else { m.WriteUint16(1) // alternate group m.WriteFloat16(1) // volume } m.Skip(2) // reserved m.WriteMatrix() if width > 0 { m.WriteFloat32(float64(width)) m.WriteFloat32(float64(height)) } else { m.Skip(4) m.Skip(4) } m.EndAtom() } func (m *Movie) WriteMediaHeader(timescale uint32) { // https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-32999 m.StartAtom(MoovTrakMdiaMdhd) m.Skip(1) // version m.Skip(3) // flags m.Skip(4) // creation time m.Skip(4) // modification time m.WriteUint32(timescale) // timescale m.Skip(4) // duration m.WriteUint16(0x55C4) // language (Unspecified) m.Skip(2) // quality m.EndAtom() } func (m *Movie) WriteMediaHandler(s, name string) { // https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-33004 m.StartAtom(MoovTrakMdiaHdlr) m.Skip(1) // version m.Skip(3) // flags m.Skip(4) m.WriteString(s) // handler type (4 byte!) m.Skip(3 * 4) // reserved m.WriteString(name) // handler name (any len) m.Skip(1) // end string m.EndAtom() } func (m *Movie) WriteVideoMediaInfo() { // https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-33012 m.StartAtom(MoovTrakMdiaMinfVmhd) m.Skip(1) // version m.WriteUint24(1) // flags (You should always set this flag to 1) m.Skip(2) // graphics mode m.Skip(3 * 2) // op color m.EndAtom() } func (m *Movie) WriteAudioMediaInfo() { m.StartAtom(MoovTrakMdiaMinfSmhd) m.Skip(1) // version m.Skip(3) // flags m.Skip(4) // balance m.EndAtom() } func (m *Movie) WriteDataInfo() { // https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-25680 m.StartAtom(MoovTrakMdiaMinfDinf) m.StartAtom(MoovTrakMdiaMinfDinfDref) m.Skip(1) // version m.Skip(3) // flags m.WriteUint32(1) // childrens m.StartAtom(MoovTrakMdiaMinfDinfDrefUrl) m.Skip(1) // version m.WriteUint24(1) // flags (self reference) m.EndAtom() m.EndAtom() // DREF m.EndAtom() // DINF } func (m *Movie) WriteSampleTable(writeSampleDesc func()) { // https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap2/qtff2.html#//apple_ref/doc/uid/TP40000939-CH204-33040 m.StartAtom(MoovTrakMdiaMinfStbl) m.StartAtom(MoovTrakMdiaMinfStblStsd) m.Skip(1) // version m.Skip(3) // flags m.WriteUint32(1) // entry count writeSampleDesc() m.EndAtom() m.StartAtom(MoovTrakMdiaMinfStblStts) m.Skip(1) // version m.Skip(3) // flags m.Skip(4) // entry count m.EndAtom() m.StartAtom(MoovTrakMdiaMinfStblStsc) m.Skip(1) // version m.Skip(3) // flags m.Skip(4) // entry count m.EndAtom() m.StartAtom(MoovTrakMdiaMinfStblStsz) m.Skip(1) // version m.Skip(3) // flags m.Skip(4) // sample size m.Skip(4) // entry count m.EndAtom() m.StartAtom(MoovTrakMdiaMinfStblStco) m.Skip(1) // version m.Skip(3) // flags m.Skip(4) // entry count m.EndAtom() m.EndAtom() } func (m *Movie) WriteTrackExtend(id uint32) { m.StartAtom(MoovMvexTrex) m.Skip(1) // version m.Skip(3) // flags m.WriteUint32(id) // trackID m.WriteUint32(1) // default sample description index m.Skip(4) // default sample duration m.Skip(4) // default sample size m.Skip(4) // default sample flags m.EndAtom() } func (m *Movie) WriteVideoTrack(id uint32, codec string, timescale uint32, width, height uint16, conf []byte) { m.StartAtom(MoovTrak) m.WriteTrackHeader(id, width, height) m.StartAtom(MoovTrakMdia) m.WriteMediaHeader(timescale) m.WriteMediaHandler("vide", "VideoHandler") m.StartAtom(MoovTrakMdiaMinf) m.WriteVideoMediaInfo() m.WriteDataInfo() m.WriteSampleTable(func() { m.WriteVideo(codec, width, height, conf) }) m.EndAtom() // MINF m.EndAtom() // MDIA m.EndAtom() // TRAK } func (m *Movie) WriteAudioTrack(id uint32, codec string, timescale uint32, channels uint16, conf []byte) { m.StartAtom(MoovTrak) m.WriteTrackHeader(id, 0, 0) m.StartAtom(MoovTrakMdia) m.WriteMediaHeader(timescale) m.WriteMediaHandler("soun", "SoundHandler") m.StartAtom(MoovTrakMdiaMinf) m.WriteAudioMediaInfo() m.WriteDataInfo() m.WriteSampleTable(func() { m.WriteAudio(codec, channels, timescale, conf) }) m.EndAtom() // MINF m.EndAtom() // MDIA m.EndAtom() // TRAK } const ( TfhdDefaultSampleDuration = 0x000008 TfhdDefaultSampleSize = 0x000010 TfhdDefaultSampleFlags = 0x000020 TfhdDefaultBaseIsMoof = 0x020000 ) const ( TrunDataOffset = 0x000001 TrunFirstSampleFlags = 0x000004 TrunSampleDuration = 0x0000100 TrunSampleSize = 0x0000200 TrunSampleFlags = 0x0000400 TrunSampleCTS = 0x0000800 ) func (m *Movie) WriteMovieFragment(seq, tid, duration, size, flags uint32, dts uint64, cts uint32) { m.StartAtom(Moof) m.StartAtom(MoofMfhd) m.Skip(1) // version m.Skip(3) // flags m.WriteUint32(seq) // sequence number m.EndAtom() m.StartAtom(MoofTraf) m.StartAtom(MoofTrafTfhd) m.Skip(1) // version m.WriteUint24( TfhdDefaultSampleDuration | TfhdDefaultSampleSize | TfhdDefaultSampleFlags | TfhdDefaultBaseIsMoof, ) m.WriteUint32(tid) // track id m.WriteUint32(duration) // default sample duration m.WriteUint32(size) // default sample size m.WriteUint32(flags) // default sample flags m.EndAtom() m.StartAtom(MoofTrafTfdt) m.WriteBytes(1) // version m.Skip(3) // flags m.WriteUint64(dts) // base media decode time m.EndAtom() m.StartAtom(MoofTrafTrun) m.Skip(1) // version if cts == 0 { m.WriteUint24(TrunDataOffset) // flags m.WriteUint32(1) // sample count // data offset: current pos + uint32 len + MDAT header len m.WriteUint32(uint32(len(m.b)) + 4 + 8) } else { m.WriteUint24(TrunDataOffset | TrunSampleCTS) m.WriteUint32(1) // data offset: current pos + uint32 len + CTS + MDAT header len m.WriteUint32(uint32(len(m.b)) + 4 + 4 + 8) m.WriteUint32(cts) } m.EndAtom() // TRUN m.EndAtom() // TRAF m.EndAtom() // MOOF } func (m *Movie) WriteData(b []byte) { m.StartAtom(Mdat) m.Write(b) m.EndAtom() } ================================================ FILE: pkg/iso/codecs.go ================================================ package iso import ( "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/pcm" ) func (m *Movie) WriteVideo(codec string, width, height uint16, conf []byte) { // https://developer.apple.com/library/archive/documentation/QuickTime/QTFF/QTFFChap3/qtff3.html switch codec { case core.CodecH264: m.StartAtom("avc1") case core.CodecH265: m.StartAtom("hev1") default: panic("unsupported iso video: " + codec) } m.Skip(6) m.WriteUint16(1) // data_reference_index m.Skip(2) // version m.Skip(2) // revision m.Skip(4) // vendor m.Skip(4) // temporal quality m.Skip(4) // spatial quality m.WriteUint16(width) // width m.WriteUint16(height) // height m.WriteFloat32(72) // horizontal resolution m.WriteFloat32(72) // vertical resolution m.Skip(4) // reserved m.WriteUint16(1) // frame count m.Skip(32) // compressor name m.WriteUint16(24) // depth m.WriteUint16(0xFFFF) // color table id (-1) switch codec { case core.CodecH264: m.StartAtom("avcC") case core.CodecH265: m.StartAtom("hvcC") } m.Write(conf) m.EndAtom() // AVCC m.StartAtom("pasp") // Pixel Aspect Ratio m.WriteUint32(1) // hSpacing m.WriteUint32(1) // vSpacing m.EndAtom() m.EndAtom() // AVC1 } func (m *Movie) WriteAudio(codec string, channels uint16, sampleRate uint32, conf []byte) { switch codec { case core.CodecAAC, core.CodecMP3: m.StartAtom("mp4a") // supported in all players and browsers case core.CodecFLAC: m.StartAtom("fLaC") // supported in all players and browsers case core.CodecOpus: m.StartAtom("Opus") // supported in Chrome and Firefox case core.CodecPCMU: m.StartAtom("ulaw") case core.CodecPCMA: m.StartAtom("alaw") default: panic("unsupported iso audio: " + codec) } if channels == 0 { channels = 1 } m.Skip(6) m.WriteUint16(1) // data_reference_index m.Skip(2) // version m.Skip(2) // revision m.Skip(4) // vendor m.WriteUint16(channels) // channel_count m.WriteUint16(16) // sample_size m.Skip(2) // compression id m.Skip(2) // reserved m.WriteFloat32(float64(sampleRate)) // sample_rate switch codec { case core.CodecAAC: m.WriteEsdsAAC(conf) case core.CodecMP3: m.WriteEsdsMP3() case core.CodecFLAC: m.StartAtom("dfLa") m.Write(pcm.FLACHeader(false, sampleRate)) m.EndAtom() case core.CodecOpus: m.WriteOpus(channels, sampleRate) case core.CodecPCMU, core.CodecPCMA: // don't know what means this magic m.StartAtom("chan") m.WriteBytes(0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 4, 0, 0, 0, 0) m.EndAtom() } m.EndAtom() // MP4A/OPUS } func (m *Movie) WriteEsdsAAC(conf []byte) { m.StartAtom("esds") m.Skip(1) // version m.Skip(3) // flags // MP4ESDescrTag[3]: // - MP4DecConfigDescrTag[4]: // - MP4DecSpecificDescrTag[5]: conf // - Other[6] const header = 5 const size3 = 3 const size4 = 13 size5 := byte(len(conf)) const size6 = 1 m.WriteBytes(3, 0x80, 0x80, 0x80, size3+header+size4+header+size5+header+size6) m.Skip(2) // es id m.Skip(1) // es flags // https://learn.microsoft.com/en-us/windows/win32/medfound/mpeg-4-file-sink#aac-audio m.WriteBytes(4, 0x80, 0x80, 0x80, size4+header+size5) m.WriteBytes(0x40) // object id m.WriteBytes(0x15) // stream type m.Skip(3) // buffer size db m.Skip(4) // max bitraga m.Skip(4) // avg bitraga m.WriteBytes(5, 0x80, 0x80, 0x80, size5) m.Write(conf) m.WriteBytes(6, 0x80, 0x80, 0x80, 1) m.WriteBytes(2) // ? m.EndAtom() // ESDS } func (m *Movie) WriteEsdsMP3() { m.StartAtom("esds") m.Skip(1) // version m.Skip(3) // flags // MP4ESDescrTag[3]: // - MP4DecConfigDescrTag[4]: // - Other[6] const header = 5 const size3 = 3 const size4 = 13 const size6 = 1 m.WriteBytes(3, 0x80, 0x80, 0x80, size3+header+size4+header+size6) m.Skip(2) // es id m.Skip(1) // es flags // https://learn.microsoft.com/en-us/windows/win32/medfound/mpeg-4-file-sink#mp3-audio m.WriteBytes(4, 0x80, 0x80, 0x80, size4) m.WriteBytes(0x6B) // object id m.WriteBytes(0x15) // stream type m.Skip(3) // buffer size db m.Skip(4) // max bitraga m.Skip(4) // avg bitraga m.WriteBytes(6, 0x80, 0x80, 0x80, 1) m.WriteBytes(2) // ? m.EndAtom() // ESDS } func (m *Movie) WriteOpus(channels uint16, sampleRate uint32) { // https://www.opus-codec.org/docs/opus_in_isobmff.html m.StartAtom("dOps") m.Skip(1) // version m.WriteBytes(byte(channels)) m.WriteUint16(0) // PreSkip ??? m.WriteUint32(sampleRate) m.Skip(2) // OutputGain m.Skip(1) // signed int(16) OutputGain; m.EndAtom() } ================================================ FILE: pkg/iso/iso.go ================================================ package iso import ( "encoding/binary" "math" ) type Movie struct { b []byte start []int } func NewMovie(size int) *Movie { return &Movie{b: make([]byte, 0, size)} } func (m *Movie) Bytes() []byte { return m.b } func (m *Movie) StartAtom(name string) { m.start = append(m.start, len(m.b)) m.b = append(m.b, 0, 0, 0, 0) m.b = append(m.b, name...) } func (m *Movie) EndAtom() { n := len(m.start) - 1 i := m.start[n] size := uint32(len(m.b) - i) binary.BigEndian.PutUint32(m.b[i:], size) m.start = m.start[:n] } func (m *Movie) Write(b []byte) { m.b = append(m.b, b...) } func (m *Movie) WriteBytes(b ...byte) { m.b = append(m.b, b...) } func (m *Movie) WriteString(s string) { m.b = append(m.b, s...) } func (m *Movie) Skip(n int) { m.b = append(m.b, make([]byte, n)...) } func (m *Movie) WriteUint16(v uint16) { m.b = append(m.b, byte(v>>8), byte(v)) } func (m *Movie) WriteUint24(v uint32) { m.b = append(m.b, byte(v>>16), byte(v>>8), byte(v)) } func (m *Movie) WriteUint32(v uint32) { m.b = append(m.b, byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) } func (m *Movie) WriteUint64(v uint64) { m.b = append(m.b, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) } func (m *Movie) WriteFloat16(f float64) { i, f := math.Modf(f) f *= 256 m.b = append(m.b, byte(i), byte(f)) } func (m *Movie) WriteFloat32(f float64) { i, f := math.Modf(f) f *= 65536 m.b = append(m.b, byte(uint16(i)>>8), byte(i), byte(uint16(f)>>8), byte(f)) } func (m *Movie) WriteMatrix() { m.WriteUint32(0x00010000) m.Skip(4) m.Skip(4) m.Skip(4) m.WriteUint32(0x00010000) m.Skip(4) m.Skip(4) m.Skip(4) m.WriteUint32(0x40000000) } ================================================ FILE: pkg/iso/reader.go ================================================ package iso import ( "bytes" "encoding/binary" "io" "github.com/AlexxIT/go2rtc/pkg/bits" ) type Atom struct { Name string Data []byte } type AtomTkhd struct { TrackID uint32 } type AtomMdhd struct { TimeScale uint32 } type AtomVideo struct { Name string Config []byte } type AtomAudio struct { Name string Channels uint16 SampleRate uint32 Config []byte } type AtomMfhd struct { Sequence uint32 } type AtomMdat struct { Data []byte } type AtomTfhd struct { TrackID uint32 SampleDuration uint32 SampleSize uint32 SampleFlags uint32 } type AtomTfdt struct { DecodeTime uint64 } type AtomTrun struct { DataOffset uint32 FirstSampleFlags uint32 SamplesDuration []uint32 SamplesSize []uint32 SamplesFlags []uint32 SamplesCTS []uint32 } func DecodeAtom(b []byte) (any, error) { size := binary.BigEndian.Uint32(b) if len(b) < int(size) { return nil, io.EOF } name := string(b[4:8]) data := b[8:size] switch name { // useful containers case Moov, MoovTrak, MoovTrakMdia, MoovTrakMdiaMinf, MoovTrakMdiaMinfStbl, Moof, MoofTraf: return DecodeAtoms(data) case MoovTrakTkhd: return &AtomTkhd{TrackID: binary.BigEndian.Uint32(data[1+3+4+4:])}, nil case MoovTrakMdiaMdhd: return &AtomMdhd{TimeScale: binary.BigEndian.Uint32(data[1+3+4+4:])}, nil case MoovTrakMdiaMinfStblStsd: // support only 1 codec entry if n := binary.BigEndian.Uint32(data[1+3:]); n == 1 { return DecodeAtom(data[1+3+4:]) } case "avc1", "hev1": b = data[6+2+2+2+4+4+4+2+2+4+4+4+2+32+2+2:] atom, err := DecodeAtom(b) if err != nil { return nil, err } if conf, ok := atom.(*Atom); ok { return &AtomVideo{Name: name, Config: conf.Data}, nil } case "mp4a": atom := &AtomAudio{Name: name} rd := bits.NewReader(data) rd.ReadBytes(6 + 2 + 2 + 2 + 4) // skip atom.Channels = rd.ReadUint16() rd.ReadBytes(2 + 2 + 2) // skip atom.SampleRate = uint32(rd.ReadFloat32()) atom2, _ := DecodeAtom(rd.Left()) if conf, ok := atom2.(*Atom); ok { _, b, _ = bytes.Cut(conf.Data, []byte{5, 0x80, 0x80, 0x80}) if n := len(b); n > 0 && n > 1+int(b[0]) { atom.Config = b[1 : 1+b[0]] } } return atom, nil case MoofMfhd: return &AtomMfhd{Sequence: binary.BigEndian.Uint32(data[4:])}, nil case MoofTrafTfhd: rd := bits.NewReader(data) _ = rd.ReadByte() // version flags := rd.ReadUint24() atom := &AtomTfhd{ TrackID: rd.ReadUint32(), } if flags&TfhdDefaultSampleDuration != 0 { atom.SampleDuration = rd.ReadUint32() } if flags&TfhdDefaultSampleSize != 0 { atom.SampleSize = rd.ReadUint32() } if flags&TfhdDefaultSampleFlags != 0 { atom.SampleFlags = rd.ReadUint32() // skip } return atom, nil case MoofTrafTfdt: return &AtomTfdt{DecodeTime: binary.BigEndian.Uint64(data[4:])}, nil case MoofTrafTrun: rd := bits.NewReader(data) _ = rd.ReadByte() // version flags := rd.ReadUint24() samples := rd.ReadUint32() atom := &AtomTrun{} if flags&TrunDataOffset != 0 { atom.DataOffset = rd.ReadUint32() } if flags&TrunFirstSampleFlags != 0 { atom.FirstSampleFlags = rd.ReadUint32() } for i := uint32(0); i < samples; i++ { if flags&TrunSampleDuration != 0 { atom.SamplesDuration = append(atom.SamplesDuration, rd.ReadUint32()) } if flags&TrunSampleSize != 0 { atom.SamplesSize = append(atom.SamplesSize, rd.ReadUint32()) } if flags&TrunSampleFlags != 0 { atom.SamplesFlags = append(atom.SamplesFlags, rd.ReadUint32()) } if flags&TrunSampleCTS != 0 { atom.SamplesCTS = append(atom.SamplesCTS, rd.ReadUint32()) } } return atom, nil case Mdat: return &AtomMdat{Data: data}, nil } return &Atom{Name: name, Data: data}, nil } func DecodeAtoms(b []byte) (atoms []any, err error) { for len(b) > 0 { atom, err := DecodeAtom(b) if err != nil { return nil, err } if childs, ok := atom.([]any); ok { atoms = append(atoms, childs...) } else { atoms = append(atoms, atom) } size := binary.BigEndian.Uint32(b) b = b[size:] } return atoms, nil } ================================================ FILE: pkg/ivideon/ivideon.go ================================================ package ivideon import ( "encoding/json" "errors" "fmt" "net/http" "strings" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/mp4" "github.com/gorilla/websocket" ) type Producer struct { core.Connection conn *websocket.Conn buf []byte dem *mp4.Demuxer } func Dial(source string) (core.Producer, error) { id := strings.Replace(source[8:], "/", ":", 1) url, err := GetLiveStream(id) if err != nil { return nil, err } conn, _, err := websocket.DefaultDialer.Dial(url, nil) if err != nil { return nil, err } prod := &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "ivideon", Protocol: core.Before(url, ":"), // wss RemoteAddr: conn.RemoteAddr().String(), Source: source, URL: url, Transport: conn, }, conn: conn, } if err = prod.probe(); err != nil { _ = conn.Close() return nil, err } return prod, nil } func GetLiveStream(id string) (string, error) { // &video_codecs=h264,h265&audio_codecs=aac,mp3,pcma,pcmu,none resp, err := http.Get( "https://openapi-alpha.ivideon.com/cameras/" + id + "/live_stream?op=GET&access_token=public&q=2&video_codecs=h264&format=ws-fmp4", ) if err != nil { return "", err } var v struct { Message string `json:"message"` Result struct { URL string `json:"url"` } `json:"result"` Success bool `json:"success"` } if err = json.NewDecoder(resp.Body).Decode(&v); err != nil { return "", err } if !v.Success { return "", fmt.Errorf("ivideon: can't get live_stream: " + v.Message) } return v.Result.URL, nil } func (p *Producer) Start() error { receivers := make(map[uint32]*core.Receiver) for _, receiver := range p.Receivers { trackID := p.dem.GetTrackID(receiver.Codec) receivers[trackID] = receiver } ch := make(chan []byte, 10) defer close(ch) ch <- p.buf go func() { // add delay to the stream for smooth playing (not a best solution) t0 := time.Now() for data := range ch { trackID, packets := p.dem.Demux(data) if receiver := receivers[trackID]; receiver != nil { clockRate := time.Duration(receiver.Codec.ClockRate) for _, packet := range packets { // synchronize framerate for WebRTC and MSE ts := time.Second * time.Duration(packet.Timestamp) / clockRate d := ts - time.Since(t0) if d < 0 { d = 10 * time.Millisecond } time.Sleep(d) receiver.WriteRTP(packet) } } } }() for { var msg message if err := p.conn.ReadJSON(&msg); err != nil { return err } switch msg.Type { case "stream-init", "metadata": continue case "fragment": _, b, err := p.conn.ReadMessage() if err != nil { return err } p.Recv += len(b) ch <- b default: return errors.New("ivideon: wrong message type: " + msg.Type) } } } func (p *Producer) probe() (err error) { p.dem = &mp4.Demuxer{} for { var msg message if err = p.conn.ReadJSON(&msg); err != nil { return err } switch msg.Type { case "metadata": continue case "stream-init": // it's difficult to maintain audio if strings.HasPrefix(msg.CodecString, "avc1") { medias := p.dem.Probe(msg.Data) p.Medias = append(p.Medias, medias...) } case "fragment": _, p.buf, err = p.conn.ReadMessage() return default: return errors.New("ivideon: wrong message type: " + msg.Type) } } } type message struct { Type string `json:"type"` CodecString string `json:"codec_string"` Data []byte `json:"data"` //TrackID byte `json:"track_id"` //Track byte `json:"track"` //StartTime float32 `json:"start_time"` //Duration float32 `json:"duration"` //IsKey bool `json:"is_key"` //DataOffset uint32 `json:"data_offset"` } ================================================ FILE: pkg/kasa/producer.go ================================================ package kasa import ( "bufio" "errors" "io" "net/http" "net/http/httputil" "strconv" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/AlexxIT/go2rtc/pkg/mpjpeg" "github.com/AlexxIT/go2rtc/pkg/tcp" "github.com/pion/rtp" ) type Producer struct { core.Connection rd *core.ReadBuffer reader *bufio.Reader } func Dial(url string) (*Producer, error) { req, err := http.NewRequest("GET", url, nil) if err != nil { return nil, err } req.URL.Scheme = "httpx" res, err := tcp.Do(req) if err != nil { return nil, err } // KC200 // HTTP/1.0 200 OK // Content-Type: multipart/x-mixed-replace;boundary=data-boundary-- // KD110, KC401, KC420WS: // HTTP/1.0 200 OK // Content-Type: multipart/x-mixed-replace;boundary=data-boundary-- // Transfer-Encoding: chunked // HTTP/1.0 + chunked = out of standard, so golang remove this header // and we need to check first two bytes buf := bufio.NewReader(res.Body) b, err := buf.Peek(2) if err != nil { return nil, err } rd := struct { io.Reader io.Closer }{ buf, res.Body, } if string(b) != "--" { rd.Reader = httputil.NewChunkedReader(buf) } prod := &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "kasa", Protocol: "http", Transport: rd, }, rd: core.NewReadBuffer(rd), } if err = prod.probe(); err != nil { return nil, err } return prod, nil } func (c *Producer) Start() error { if len(c.Receivers) == 0 { return errors.New("multipart: no receivers") } var video, audio *core.Receiver for _, receiver := range c.Receivers { switch receiver.Codec.Name { case core.CodecH264: video = receiver case core.CodecPCMU: audio = receiver } } for { header, body, err := mpjpeg.Next(c.reader) if err != nil { return err } c.Recv += len(body) ct := header.Get("Content-Type") switch ct { case MimeVideo: if video != nil { ts := GetTimestamp(header) pkt := &rtp.Packet{ Header: rtp.Header{ Timestamp: uint32(ts * 90000), }, Payload: annexb.EncodeToAVCC(body), } video.WriteRTP(pkt) } case MimeG711U: if audio != nil { ts := GetTimestamp(header) pkt := &rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: true, Timestamp: uint32(ts * 8000), }, Payload: body, } audio.WriteRTP(pkt) } } } } const ( MimeVideo = "video/x-h264" MimeG711U = "audio/g711u" ) func (c *Producer) probe() error { c.rd.BufferSize = core.ProbeSize c.reader = bufio.NewReader(c.rd) defer func() { c.rd.Reset() c.reader = bufio.NewReader(c.rd) }() waitVideo, waitAudio := true, true timeout := time.Now().Add(core.ProbeTimeout) for (waitVideo || waitAudio) && time.Now().Before(timeout) { header, body, err := mpjpeg.Next(c.reader) if err != nil { return err } var media *core.Media ct := header.Get("Content-Type") switch ct { case MimeVideo: if !waitVideo { continue } waitVideo = false body = annexb.EncodeToAVCC(body) codec := h264.AVCCToCodec(body) media = &core.Media{ Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, } case MimeG711U: if !waitAudio { continue } waitAudio = false media = &core.Media{ Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ { Name: core.CodecPCMU, ClockRate: 8000, }, }, } default: return errors.New("kasa: unsupported type: " + ct) } c.Medias = append(c.Medias, media) } return nil } // GetTimestamp - return timestamp in seconds func GetTimestamp(header http.Header) float64 { if s := header.Get("X-Timestamp"); s != "" { if f, _ := strconv.ParseFloat(s, 32); f != 0 { return f } } return float64(time.Duration(time.Now().UnixNano()) / time.Second) } ================================================ FILE: pkg/magic/bitstream/producer.go ================================================ package bitstream import ( "encoding/hex" "errors" "io" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/pion/rtp" ) type Producer struct { core.Connection rd *core.ReadBuffer } func Open(r io.Reader) (*Producer, error) { rd := core.NewReadBuffer(r) buf, err := rd.Peek(256) if err != nil { return nil, err } buf = annexb.EncodeToAVCC(buf) // won't break original buffer var codec *core.Codec var format string switch { case h264.NALUType(buf) == h264.NALUTypeSPS: codec = h264.AVCCToCodec(buf) format = "h264" case h265.NALUType(buf) == h265.NALUTypeVPS: codec = h265.AVCCToCodec(buf) format = "hevc" default: return nil, errors.New("bitstream: unsupported header: " + hex.EncodeToString(buf[:8])) } medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, }, } return &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: format, Medias: medias, Transport: r, }, rd: rd, }, nil } func (c *Producer) Start() error { var buf []byte b := make([]byte, core.BufferSize) for { n, err := c.rd.Read(b) if err != nil { return err } c.Recv += n buf = append(buf, b[:n]...) for { i := annexb.IndexFrame(buf) if i < 0 { break } if len(c.Receivers) > 0 { pkt := &rtp.Packet{ Header: rtp.Header{Timestamp: core.Now90000()}, Payload: annexb.EncodeToAVCC(buf[:i]), } c.Receivers[0].WriteRTP(pkt) //log.Printf("[AVC] %v, len: %d", h264.Types(pkt.Payload), len(pkt.Payload)) } buf = buf[i:] } } } ================================================ FILE: pkg/magic/keyframe.go ================================================ package magic import ( "io" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/AlexxIT/go2rtc/pkg/mjpeg" "github.com/pion/rtp" ) type Keyframe struct { core.Connection wr *core.WriteBuffer } // Deprecated: should be rewritten func NewKeyframe() *Keyframe { medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecJPEG}, {Name: core.CodecRAW}, {Name: core.CodecH264}, {Name: core.CodecH265}, }, }, } wr := core.NewWriteBuffer(nil) return &Keyframe{ Connection: core.Connection{ ID: core.NewID(), FormatName: "keyframe", Medias: medias, Transport: wr, }, wr: wr, } } func (k *Keyframe) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error { sender := core.NewSender(media, track.Codec) switch track.Codec.Name { case core.CodecH264: sender.Handler = func(packet *rtp.Packet) { if !h264.IsKeyframe(packet.Payload) { return } b := annexb.DecodeAVCC(packet.Payload, true) if n, err := k.wr.Write(b); err == nil { k.Send += n } } if track.Codec.IsRTP() { sender.Handler = h264.RTPDepay(track.Codec, sender.Handler) } else { sender.Handler = h264.RepairAVCC(track.Codec, sender.Handler) } case core.CodecH265: sender.Handler = func(packet *rtp.Packet) { if !h265.IsKeyframe(packet.Payload) { return } b := annexb.DecodeAVCC(packet.Payload, true) if n, err := k.wr.Write(b); err == nil { k.Send += n } } if track.Codec.IsRTP() { sender.Handler = h265.RTPDepay(track.Codec, sender.Handler) } case core.CodecJPEG: sender.Handler = func(packet *rtp.Packet) { if n, err := k.wr.Write(packet.Payload); err == nil { k.Send += n } } if track.Codec.IsRTP() { sender.Handler = mjpeg.RTPDepay(sender.Handler) } case core.CodecRAW: sender.Handler = func(packet *rtp.Packet) { if n, err := k.wr.Write(packet.Payload); err == nil { k.Send += n } } sender.Handler = mjpeg.Encoder(track.Codec, 5, sender.Handler) } sender.HandleRTP(track) k.Senders = append(k.Senders, sender) return nil } func (k *Keyframe) CodecName() string { if len(k.Senders) != 1 { return "" } return k.Senders[0].Codec.Name } func (k *Keyframe) WriteTo(wr io.Writer) (int64, error) { return k.wr.WriteTo(wr) } ================================================ FILE: pkg/magic/mjpeg/producer.go ================================================ package mjpeg import ( "bytes" "io" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type Producer struct { core.Connection rd *core.ReadBuffer } func Open(rd io.Reader) (*Producer, error) { medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ { Name: core.CodecJPEG, ClockRate: 90000, PayloadType: core.PayloadTypeRAW, }, }, }, } return &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "mjpeg", Medias: medias, Transport: rd, }, rd: core.NewReadBuffer(rd), }, nil } func (c *Producer) Start() error { var buf []byte // total bufer b := make([]byte, core.BufferSize) // reading buffer for { // one JPEG end and next start i := bytes.Index(buf, []byte{0xFF, 0xD9, 0xFF, 0xD8}) if i < 0 { n, err := c.rd.Read(b) if err != nil { return err } c.Recv += n buf = append(buf, b[:n]...) // if we receive frame if n >= 2 && b[n-2] == 0xFF && b[n-1] == 0xD9 { i = len(buf) } else { continue } } else { i += 2 } pkt := &rtp.Packet{ Header: rtp.Header{Timestamp: core.Now90000()}, Payload: buf[:i], } c.Receivers[0].WriteRTP(pkt) //log.Printf("[mjpeg] ts=%d size=%d", pkt.Header.Timestamp, len(pkt.Payload)) buf = buf[i:] } } ================================================ FILE: pkg/magic/producer.go ================================================ package magic import ( "bytes" "encoding/hex" "errors" "io" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/flv" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/AlexxIT/go2rtc/pkg/magic/bitstream" "github.com/AlexxIT/go2rtc/pkg/magic/mjpeg" "github.com/AlexxIT/go2rtc/pkg/mpegts" "github.com/AlexxIT/go2rtc/pkg/mpjpeg" "github.com/AlexxIT/go2rtc/pkg/wav" "github.com/AlexxIT/go2rtc/pkg/y4m" ) func Open(r io.Reader) (core.Producer, error) { rd := core.NewReadBuffer(r) b, err := rd.Peek(4) if err != nil { return nil, err } switch string(b) { case annexb.StartCode: return bitstream.Open(rd) case wav.FourCC: return wav.Open(rd) case y4m.FourCC: return y4m.Open(rd) } switch string(b[:3]) { case flv.Signature: return flv.Open(rd) } switch string(b[:2]) { case "\xFF\xD8": return mjpeg.Open(rd) case "\xFF\xF1", "\xFF\xF9": return aac.Open(rd) case "--": return mpjpeg.Open(rd) } switch b[0] { case mpegts.SyncByte: return mpegts.Open(rd) } // support MJPEG with trash on start // https://github.com/AlexxIT/go2rtc/issues/747 if b, err = rd.Peek(4096); err != nil { return nil, err } if i := bytes.Index(b, []byte{0xFF, 0xD8, 0xFF, 0xDB}); i > 0 { _, _ = io.ReadFull(rd, make([]byte, i)) return mjpeg.Open(rd) } return nil, errors.New("magic: unsupported header: " + hex.EncodeToString(b[:4])) } ================================================ FILE: pkg/mdns/README.md ================================================ # Useful links - https://grouper.ieee.org/groups/1722/contributions/2009/Bonjour%20Device%20Discovery.pdf ================================================ FILE: pkg/mdns/client.go ================================================ package mdns import ( "context" "encoding/json" "errors" "fmt" "net" "strings" "syscall" "time" "github.com/AlexxIT/go2rtc/pkg/xnet" "github.com/miekg/dns" // awesome library for parsing mDNS records ) const ( ServiceDNSSD = "_services._dns-sd._udp.local." ServiceHAP = "_hap._tcp.local." // HomeKit Accessory Protocol ) type ServiceEntry struct { Name string `json:"name,omitempty"` IP net.IP `json:"ip,omitempty"` Port uint16 `json:"port,omitempty"` Info map[string]string `json:"info,omitempty"` } func (e *ServiceEntry) String() string { b, err := json.Marshal(e) if err != nil { return err.Error() } return string(b) } func (e *ServiceEntry) TXT() []string { var txt []string for k, v := range e.Info { txt = append(txt, k+"="+v) } return txt } func (e *ServiceEntry) Complete() bool { return e.IP != nil && e.Port > 0 && e.Info != nil } func (e *ServiceEntry) Addr() string { return fmt.Sprintf("%s:%d", e.IP, e.Port) } func (e *ServiceEntry) Host(service string) string { return e.name() + "." + strings.TrimRight(service, ".") } func (e *ServiceEntry) name() string { b := []byte(e.Name) for i, c := range b { if 'a' <= c && c <= 'z' || 'A' <= c && c <= 'Z' || '0' <= c && c <= '9' { continue } b[i] = '-' } return string(b) } var MulticastAddr = &net.UDPAddr{ IP: net.IP{224, 0, 0, 251}, Port: 5353, } const sendTimeout = time.Millisecond * 505 const respTimeout = time.Second * 3 // BasicDiscovery - default golang Multicast UDP listener. // Does not work well with multiple interfaces. func BasicDiscovery(service string, onentry func(*ServiceEntry) bool) error { conn, err := net.ListenMulticastUDP("udp4", nil, MulticastAddr) if err != nil { return err } b := Browser{ Service: service, Addr: MulticastAddr, Recv: conn, Sends: []net.PacketConn{conn}, RecvTimeout: respTimeout, SendTimeout: sendTimeout, } defer b.Close() return b.Browse(onentry) } // Discovery - better discovery version. Works well with multiple interfaces. func Discovery(service string, onentry func(*ServiceEntry) bool) error { b := Browser{ Service: service, Addr: MulticastAddr, RecvTimeout: respTimeout, SendTimeout: sendTimeout, } if err := b.ListenMulticastUDP(); err != nil { return err } defer b.Close() return b.Browse(onentry) } // Query - direct Discovery request on device IP-address. Works even over VPN. func Query(host, service string) (entry *ServiceEntry, err error) { conn, err := net.ListenPacket("udp4", ":0") // shouldn't use ":5353" if err != nil { return } br := Browser{ Service: service, Addr: &net.UDPAddr{ IP: net.ParseIP(host), Port: 5353, }, Recv: conn, Sends: []net.PacketConn{conn}, SendTimeout: time.Millisecond * 255, RecvTimeout: time.Second, } defer br.Close() err = br.Browse(func(en *ServiceEntry) bool { entry = en return true }) return } // QueryOrDiscovery - useful if we know previous device host and want // to update port or any other information. Will work even over VPN. func QueryOrDiscovery(host, service string, onentry func(*ServiceEntry) bool) error { entry, _ := Query(host, service) if entry != nil && onentry(entry) { return nil } return Discovery(service, onentry) } type Browser struct { Service string Addr net.Addr Nets []*net.IPNet Recv net.PacketConn Sends []net.PacketConn RecvTimeout time.Duration SendTimeout time.Duration } // ListenMulticastUDP - creates multiple senders socket (each for IP4 interface). // And one receiver with multicast membership for each sender. // Receiver will get multicast responses on senders requests. func (b *Browser) ListenMulticastUDP() error { // 1. Collect IPv4 interfaces nets, err := xnet.IPNets(func(ip net.IP) bool { return !xnet.Docker.Contains(ip) }) if err != nil { return err } // 2. Create senders lc1 := net.ListenConfig{ Control: func(network, address string, c syscall.RawConn) error { return c.Control(func(fd uintptr) { // 1. Allow multicast UDP to listen concurrently across multiple listeners _ = SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1) }) }, } ctx := context.Background() for _, ipn := range nets { conn, err := lc1.ListenPacket(ctx, "udp4", ipn.IP.String()+":5353") // same port important if err != nil { continue } b.Nets = append(b.Nets, ipn) b.Sends = append(b.Sends, conn) } if b.Sends == nil { return errors.New("no interfaces for listen") } // 3. Create receiver lc2 := net.ListenConfig{ Control: func(network, address string, c syscall.RawConn) error { return c.Control(func(fd uintptr) { // 1. Allow multicast UDP to listen concurrently across multiple listeners _ = SetsockoptInt(fd, syscall.SOL_SOCKET, syscall.SO_REUSEADDR, 1) // 2. Disable loop responses _ = SetsockoptInt(fd, syscall.IPPROTO_IP, syscall.IP_MULTICAST_LOOP, 0) // 3. Allow receive multicast responses on all this addresses mreq := &syscall.IPMreq{ Multiaddr: [4]byte{224, 0, 0, 251}, } _ = SetsockoptIPMreq(fd, syscall.IPPROTO_IP, syscall.IP_ADD_MEMBERSHIP, mreq) for _, send := range b.Sends { addr := send.LocalAddr().(*net.UDPAddr) mreq.Interface = [4]byte(addr.IP.To4()) _ = SetsockoptIPMreq(fd, syscall.IPPROTO_IP, syscall.IP_ADD_MEMBERSHIP, mreq) } }) }, } b.Recv, err = lc2.ListenPacket(ctx, "udp4", ":5353") return err } func (b *Browser) Browse(onentry func(*ServiceEntry) bool) error { msg := &dns.Msg{ Question: []dns.Question{ {Name: b.Service, Qtype: dns.TypePTR, Qclass: dns.ClassINET}, }, } query, err := msg.Pack() if err != nil { return err } if err = b.Recv.SetDeadline(time.Now().Add(b.RecvTimeout)); err != nil { return err } go func() { for { for _, send := range b.Sends { if _, err := send.WriteTo(query, b.Addr); err != nil { return } } time.Sleep(b.SendTimeout) } }() processed := map[string]struct{}{"": {}} b2 := make([]byte, 1500) for { // in the Hass docker network can receive same msg from different address n, addr, err := b.Recv.ReadFrom(b2) if err != nil { break } if err = msg.Unpack(b2[:n]); err != nil { continue } ptr := GetPTR(msg, b.Service) if _, ok := processed[ptr]; ok { continue } ip := addr.(*net.UDPAddr).IP for _, entry := range NewServiceEntries(msg, ip) { if onentry(entry) { return nil } } processed[ptr] = struct{}{} } return nil } func (b *Browser) Close() error { if b.Recv != nil { _ = b.Recv.Close() } for _, send := range b.Sends { _ = send.Close() } return nil } func GetPTR(msg *dns.Msg, service string) string { for _, record := range msg.Answer { if ptr, ok := record.(*dns.PTR); ok && ptr.Hdr.Name == service { return ptr.Ptr } } return "" } func NewServiceEntries(msg *dns.Msg, ip net.IP) (entries []*ServiceEntry) { records := make([]dns.RR, 0, len(msg.Answer)+len(msg.Ns)+len(msg.Extra)) records = append(records, msg.Answer...) records = append(records, msg.Ns...) records = append(records, msg.Extra...) // PTR ptr=SomeName._hap._tcp.local. hdr=_hap._tcp.local. // TXT txt=... hdr=SomeName._hap._tcp.local. // SRV target=SomeName.local. hdr=SomeName._hap._tcp.local. // A a=192.168.1.123 hdr=SomeName.local. for _, record := range records { ptr, ok := record.(*dns.PTR) if !ok { continue } entry := &ServiceEntry{} if i := strings.IndexByte(ptr.Ptr, '.'); i > 0 { entry.Name = strings.ReplaceAll(ptr.Ptr[:i], `\ `, " ") } var txt *dns.TXT var srv *dns.SRV var a *dns.A for _, record = range records { if txt, ok = record.(*dns.TXT); ok && txt.Hdr.Name == ptr.Ptr { entry.Info = make(map[string]string, len(txt.Txt)) for _, s := range txt.Txt { k, v, _ := strings.Cut(s, "=") entry.Info[k] = v } break } } for _, record = range records { if srv, ok = record.(*dns.SRV); ok && srv.Hdr.Name == ptr.Ptr { entry.Port = srv.Port for _, record = range records { if a, ok = record.(*dns.A); ok && a.Hdr.Name == srv.Target { // device can send multiple IP addresses (ex. Homebridge) // use first IP from the list or same IP from sender if entry.IP == nil || ip.Equal(a.A) { entry.IP = a.A } } } break } } entries = append(entries, entry) } return } ================================================ FILE: pkg/mdns/mdns_test.go ================================================ package mdns import ( "testing" "github.com/stretchr/testify/require" ) func TestDiscovery(t *testing.T) { onentry := func(entry *ServiceEntry) bool { return true } err := Discovery(ServiceHAP, onentry) //err := Discovery("_ewelink._tcp.local.", time.Second, onentry) // err := Discovery("_googlecast._tcp.local.", time.Second, onentry) require.Nil(t, err) } ================================================ FILE: pkg/mdns/server.go ================================================ package mdns import ( "net" "github.com/miekg/dns" ) // ClassCacheFlush https://datatracker.ietf.org/doc/html/rfc6762#section-10.2 const ClassCacheFlush = 0x8001 func Serve(service string, entries []*ServiceEntry) error { b := Browser{Service: service} if err := b.ListenMulticastUDP(); err != nil { return err } return b.Serve(entries) } func (b *Browser) Serve(entries []*ServiceEntry) error { names := make(map[string]*ServiceEntry, len(entries)) for _, entry := range entries { name := entry.name() + "." + b.Service names[name] = entry } buf := make([]byte, 1500) for { n, addr, err := b.Recv.ReadFrom(buf) if err != nil { break } var req dns.Msg // request if err = req.Unpack(buf[:n]); err != nil { continue } // skip messages without Questions if req.Question == nil { continue } remoteIP := addr.(*net.UDPAddr).IP localIP := b.MatchLocalIP(remoteIP) // skip messages from unknown networks (can be docker network) if localIP == nil { continue } var res dns.Msg // response for _, q := range req.Question { if q.Qtype != dns.TypePTR || q.Qclass != dns.ClassINET { continue } if q.Name == ServiceDNSSD { AppendDNSSD(&res, b.Service) } else if q.Name == b.Service { for _, entry := range entries { AppendEntry(&res, entry, b.Service, localIP) } } else if entry, ok := names[q.Name]; ok { AppendEntry(&res, entry, b.Service, localIP) } } if res.Answer == nil { continue } res.MsgHdr.Response = true res.MsgHdr.Authoritative = true data, err := res.Pack() if err != nil { continue } for _, send := range b.Sends { _, _ = send.WriteTo(data, MulticastAddr) } } return nil } func (b *Browser) MatchLocalIP(remote net.IP) net.IP { for _, ipn := range b.Nets { if ipn.Contains(remote) { return ipn.IP } } return nil } func AppendDNSSD(msg *dns.Msg, service string) { msg.Answer = append( msg.Answer, &dns.PTR{ Hdr: dns.RR_Header{ Name: ServiceDNSSD, // _services._dns-sd._udp.local. Rrtype: dns.TypePTR, // 12 Class: dns.ClassINET, // 1 Ttl: 4500, }, Ptr: service, // _home-assistant._tcp.local. }, ) } func AppendEntry(msg *dns.Msg, entry *ServiceEntry, service string, ip net.IP) { ptrName := entry.name() + "." + service srvName := entry.name() + ".local." msg.Answer = append( msg.Answer, &dns.PTR{ Hdr: dns.RR_Header{ Name: service, // _home-assistant._tcp.local. Rrtype: dns.TypePTR, // 12 Class: dns.ClassINET, // 1 Ttl: 4500, }, Ptr: ptrName, // Home\ Assistant._home-assistant._tcp.local. }, ) msg.Extra = append( msg.Extra, &dns.TXT{ Hdr: dns.RR_Header{ Name: ptrName, // Home\ Assistant._home-assistant._tcp.local. Rrtype: dns.TypeTXT, // 16 Class: ClassCacheFlush, // 32769 Ttl: 4500, }, Txt: entry.TXT(), }, &dns.SRV{ Hdr: dns.RR_Header{ Name: ptrName, // Home\ Assistant._home-assistant._tcp.local. Rrtype: dns.TypeSRV, // 33 Class: ClassCacheFlush, // 32769 Ttl: 120, }, Port: entry.Port, // 8123 Target: srvName, // 963f1fa82b7142809711cebe7c826322.local. }, &dns.A{ Hdr: dns.RR_Header{ Name: srvName, // 963f1fa82b7142809711cebe7c826322.local. Rrtype: dns.TypeA, // 1 Class: ClassCacheFlush, // 32769 Ttl: 120, }, A: ip, }, ) } ================================================ FILE: pkg/mdns/syscall.go ================================================ //go:build !(darwin || ios || freebsd || openbsd || netbsd || dragonfly || windows) package mdns import ( "syscall" ) func SetsockoptInt(fd uintptr, level, opt int, value int) (err error) { return syscall.SetsockoptInt(int(fd), level, opt, value) } func SetsockoptIPMreq(fd uintptr, level, opt int, mreq *syscall.IPMreq) (err error) { return syscall.SetsockoptIPMreq(int(fd), level, opt, mreq) } ================================================ FILE: pkg/mdns/syscall_bsd.go ================================================ //go:build darwin || ios || freebsd || openbsd || netbsd || dragonfly package mdns import ( "syscall" ) func SetsockoptInt(fd uintptr, level, opt int, value int) (err error) { // change SO_REUSEADDR and REUSEPORT flags simultaneously for BSD-like OS // https://github.com/AlexxIT/go2rtc/issues/626 // https://stackoverflow.com/questions/14388706/how-do-so-reuseaddr-and-so-reuseport-differ/14388707 if opt == syscall.SO_REUSEADDR { if err = syscall.SetsockoptInt(int(fd), level, opt, value); err != nil { return } opt = syscall.SO_REUSEPORT } return syscall.SetsockoptInt(int(fd), level, opt, value) } func SetsockoptIPMreq(fd uintptr, level, opt int, mreq *syscall.IPMreq) (err error) { return syscall.SetsockoptIPMreq(int(fd), level, opt, mreq) } ================================================ FILE: pkg/mdns/syscall_windows.go ================================================ //go:build windows package mdns import "syscall" func SetsockoptInt(fd uintptr, level, opt int, value int) (err error) { return syscall.SetsockoptInt(syscall.Handle(fd), level, opt, value) } func SetsockoptIPMreq(fd uintptr, level, opt int, mreq *syscall.IPMreq) (err error) { return syscall.SetsockoptIPMreq(syscall.Handle(fd), level, opt, mreq) } ================================================ FILE: pkg/mjpeg/README.md ================================================ ## Useful links - https://www.rfc-editor.org/rfc/rfc2435 - https://github.com/GStreamer/gst-plugins-good/blob/master/gst/rtp/gstrtpjpegdepay.c - https://mjpeg.sanford.io/ ================================================ FILE: pkg/mjpeg/consumer.go ================================================ package mjpeg import ( "io" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type Consumer struct { core.Connection wr *core.WriteBuffer } func NewConsumer() *Consumer { medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecJPEG}, {Name: core.CodecRAW}, }, }, } wr := core.NewWriteBuffer(nil) return &Consumer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "mjpeg", Medias: medias, Transport: wr, }, wr: wr, } } func (c *Consumer) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error { sender := core.NewSender(media, track.Codec) sender.Handler = func(packet *rtp.Packet) { if n, err := c.wr.Write(packet.Payload); err == nil { c.Send += n } } if track.Codec.IsRTP() { sender.Handler = RTPDepay(sender.Handler) } else if track.Codec.Name == core.CodecRAW { sender.Handler = Encoder(track.Codec, 0, sender.Handler) } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } func (c *Consumer) WriteTo(wr io.Writer) (int64, error) { return c.wr.WriteTo(wr) } ================================================ FILE: pkg/mjpeg/helpers.go ================================================ package mjpeg import ( "bytes" "image/jpeg" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/y4m" "github.com/pion/rtp" ) func FixJPEG(b []byte) []byte { // skip non-JPEG if len(b) < 10 || b[0] != 0xFF || b[1] != markerSOI { return b } // skip JPEG without app marker if b[2] == 0xFF && b[3] == markerDQT { return b } switch string(b[6:10]) { case "JFIF", "Exif": // skip if header OK for imghdr library // - https://docs.python.org/3/library/imghdr.html return b case "AVI1": // adds DHT tables to JPEG file before SOS marker // useful when you want to save a JPEG frame from an MJPEG stream // - https://github.com/image-rs/jpeg-decoder/issues/76 // - https://github.com/pion/mediadevices/pull/493 // - https://bugzilla.mozilla.org/show_bug.cgi?id=963907#c18 return InjectDHT(b) } // reencode JPEG if it has wrong header // // for example, this app produce "bad" images: // https://github.com/jacksonliam/mjpg-streamer // // and they can't be uploaded to the Telegram servers: // {"ok":false,"error_code":400,"description":"Bad Request: IMAGE_PROCESS_FAILED"} img, err := jpeg.Decode(bytes.NewReader(b)) if err != nil { return b } buf := bytes.NewBuffer(nil) if err = jpeg.Encode(buf, img, nil); err != nil { return b } return buf.Bytes() } // Encoder convert YUV frame to Img. // Support skipping empty frames, for example if USB cam needs time to start. func Encoder(codec *core.Codec, skipEmpty int, handler core.HandlerFunc) core.HandlerFunc { newImage := y4m.NewImage(codec.FmtpLine) return func(packet *rtp.Packet) { img := newImage(packet.Payload) if skipEmpty != 0 && y4m.HasSameColor(img) { skipEmpty-- return } buf := bytes.NewBuffer(nil) if err := jpeg.Encode(buf, img, nil); err != nil { return } clone := *packet clone.Payload = buf.Bytes() handler(&clone) } } const dhtSize = 432 // known size for 4 default tables func InjectDHT(b []byte) []byte { if bytes.Index(b, []byte{0xFF, markerDHT}) > 0 { return b // already exist } i := bytes.Index(b, []byte{0xFF, markerSOS}) if i < 0 { return b } dht := make([]byte, 0, dhtSize) dht = MakeHuffmanHeaders(dht) tmp := make([]byte, len(b)+dhtSize) copy(tmp, b[:i]) copy(tmp[i:], dht) copy(tmp[i+dhtSize:], b[i:]) return tmp } ================================================ FILE: pkg/mjpeg/jpeg.go ================================================ package mjpeg const ( markerSOF = 0xC0 // Start Of Frame (Baseline Sequential) markerSOI = 0xD8 // Start Of Image markerEOI = 0xD9 // End Of Image markerSOS = 0xDA // Start Of Scan markerDQT = 0xDB // Define Quantization Table markerDHT = 0xC4 // Define Huffman Table ) ================================================ FILE: pkg/mjpeg/mjpeg_test.go ================================================ package mjpeg import ( "testing" "github.com/stretchr/testify/require" ) func TestRFC2435(t *testing.T) { lqt, cqt := MakeTables(71) require.Equal(t, byte(9), lqt[0]) require.Equal(t, byte(10), cqt[0]) } ================================================ FILE: pkg/mjpeg/rfc2435.go ================================================ package mjpeg // RFC 2435. Appendix A // don't know why two tables are not respect RFC // https://github.com/FFmpeg/FFmpeg/blob/master/libavformat/rtpdec_jpeg.c var jpeg_luma_quantizer = [64]byte{ 16, 11, 12, 14, 12, 10, 16, 14, 13, 14, 18, 17, 16, 19, 24, 40, 26, 24, 22, 22, 24, 49, 35, 37, 29, 40, 58, 51, 61, 60, 57, 51, 56, 55, 64, 72, 92, 78, 64, 68, 87, 69, 55, 56, 80, 109, 81, 87, 95, 98, 103, 104, 103, 62, 77, 113, 121, 112, 100, 120, 92, 101, 103, 99, } var jpeg_chroma_quantizer = [64]byte{ 17, 18, 18, 24, 21, 24, 47, 26, 26, 47, 99, 66, 56, 66, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, 99, } func MakeTables(q byte) (lqt, cqt []byte) { var factor int switch { case q < 1: factor = 1 case q > 99: factor = 99 default: factor = int(q) } if q < 50 { factor = 5000 / factor } else { factor = 200 - factor*2 } lqt = make([]byte, 64) cqt = make([]byte, 64) for i := 0; i < 64; i++ { lq := (int(jpeg_luma_quantizer[i])*factor + 50) / 100 cq := (int(jpeg_chroma_quantizer[i])*factor + 50) / 100 /* Limit the quantizers to 1 <= q <= 255 */ switch { case lq < 1: lqt[i] = 1 case lq > 255: lqt[i] = 255 default: lqt[i] = byte(lq) } switch { case cq < 1: cqt[i] = 1 case cq > 255: cqt[i] = 255 default: cqt[i] = byte(cq) } } return } // RFC 2435. Appendix B var lum_dc_codelens = []byte{ 0, 1, 5, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, } var lum_dc_symbols = []byte{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, } var lum_ac_codelens = []byte{ 0, 2, 1, 3, 3, 2, 4, 3, 5, 5, 4, 4, 0, 0, 1, 0x7d, } var lum_ac_symbols = []byte{ 0x01, 0x02, 0x03, 0x00, 0x04, 0x11, 0x05, 0x12, 0x21, 0x31, 0x41, 0x06, 0x13, 0x51, 0x61, 0x07, 0x22, 0x71, 0x14, 0x32, 0x81, 0x91, 0xa1, 0x08, 0x23, 0x42, 0xb1, 0xc1, 0x15, 0x52, 0xd1, 0xf0, 0x24, 0x33, 0x62, 0x72, 0x82, 0x09, 0x0a, 0x16, 0x17, 0x18, 0x19, 0x1a, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe1, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, } var chm_dc_codelens = []byte{ 0, 3, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, } var chm_dc_symbols = []byte{ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, } var chm_ac_codelens = []byte{ 0, 2, 1, 2, 4, 4, 3, 4, 7, 5, 4, 4, 0, 1, 2, 0x77, } var chm_ac_symbols = []byte{ 0x00, 0x01, 0x02, 0x03, 0x11, 0x04, 0x05, 0x21, 0x31, 0x06, 0x12, 0x41, 0x51, 0x07, 0x61, 0x71, 0x13, 0x22, 0x32, 0x81, 0x08, 0x14, 0x42, 0x91, 0xa1, 0xb1, 0xc1, 0x09, 0x23, 0x33, 0x52, 0xf0, 0x15, 0x62, 0x72, 0xd1, 0x0a, 0x16, 0x24, 0x34, 0xe1, 0x25, 0xf1, 0x17, 0x18, 0x19, 0x1a, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3a, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4a, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0x59, 0x5a, 0x63, 0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7a, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87, 0x88, 0x89, 0x8a, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x99, 0x9a, 0xa2, 0xa3, 0xa4, 0xa5, 0xa6, 0xa7, 0xa8, 0xa9, 0xaa, 0xb2, 0xb3, 0xb4, 0xb5, 0xb6, 0xb7, 0xb8, 0xb9, 0xba, 0xc2, 0xc3, 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xd2, 0xd3, 0xd4, 0xd5, 0xd6, 0xd7, 0xd8, 0xd9, 0xda, 0xe2, 0xe3, 0xe4, 0xe5, 0xe6, 0xe7, 0xe8, 0xe9, 0xea, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, } func MakeHeaders(p []byte, t byte, w, h uint16, lqt, cqt []byte) []byte { // Appendix A from https://www.rfc-editor.org/rfc/rfc2435 p = append(p, 0xFF, markerSOI) p = MakeQuantHeader(p, lqt, 0) p = MakeQuantHeader(p, cqt, 1) if t == 0 { t = 0x21 // hsamp = 2, vsamp = 1 } else { t = 0x22 // hsamp = 2, vsamp = 2 } p = append(p, 0xFF, markerSOF, 0, 17, // size 8, // bits per component byte(h>>8), byte(h&0xFF), byte(w>>8), byte(w&0xFF), 3, // number of components 0, // comp 0 t, 0, // quant table 0 1, // comp 1 0x11, // hsamp = 1, vsamp = 1 1, // quant table 1 2, // comp 2 0x11, // hsamp = 1, vsamp = 1 1, // quant table 1 ) p = MakeHuffmanHeaders(p) return append(p, 0xFF, markerSOS, 0, 12, // size 3, // 3 components 0, // comp 0 0, // huffman table 0 1, // comp 1 0x11, // huffman table 1 2, // comp 2 0x11, // huffman table 1 0, // first DCT coeff 63, // last DCT coeff 0, // sucessive approx ) } func MakeQuantHeader(p []byte, qt []byte, tableNo byte) []byte { p = append(p, 0xFF, markerDQT, 0, 67, tableNo) return append(p, qt...) } func MakeHuffmanHeader(p, codelens, symbols []byte, tableNo, tableClass byte) []byte { p = append(p, 0xFF, markerDHT, 0, byte(3+len(codelens)+len(symbols)), // size (tableClass<<4)|tableNo, ) p = append(p, codelens...) return append(p, symbols...) } func MakeHuffmanHeaders(p []byte) []byte { p = MakeHuffmanHeader(p, lum_dc_codelens, lum_dc_symbols, 0, 0) p = MakeHuffmanHeader(p, lum_ac_codelens, lum_ac_symbols, 0, 1) p = MakeHuffmanHeader(p, chm_dc_codelens, chm_dc_symbols, 1, 0) p = MakeHuffmanHeader(p, chm_ac_codelens, chm_ac_symbols, 1, 1) return p } ================================================ FILE: pkg/mjpeg/rtp.go ================================================ package mjpeg import ( "bytes" "encoding/binary" "image" "image/jpeg" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) func RTPDepay(handlerFunc core.HandlerFunc) core.HandlerFunc { buf := make([]byte, 0, 512*1024) // 512K return func(packet *rtp.Packet) { //log.Printf("[RTP] codec: %s, size: %6d, ts: %10d, pt: %2d, ssrc: %d, seq: %d, mark: %v", track.Codec.Name, len(packet.Payload), packet.Timestamp, packet.PayloadType, packet.SSRC, packet.SequenceNumber, packet.Marker) // https://www.rfc-editor.org/rfc/rfc2435#section-3.1 b := packet.Payload // 3.1. JPEG header t := b[4] // 3.1.7. Restart Marker header if 64 <= t && t <= 127 { b = b[12:] // skip it } else { b = b[8:] } if len(buf) == 0 { var lqt, cqt []byte // 3.1.8. Quantization Table header q := packet.Payload[5] if q >= 128 { lqt = b[4:68] cqt = b[68:132] b = b[132:] } else { lqt, cqt = MakeTables(q) } // https://www.rfc-editor.org/rfc/rfc2435#section-3.1.5 // The maximum width is 2040 pixels. w := uint16(packet.Payload[6]) << 3 h := uint16(packet.Payload[7]) << 3 // fix sizes more than 2040 switch { // 512x1920 512x1440 case w == cutSize(2560) && (h == 1920 || h == 1440): w = 2560 // 1792x112 case w == cutSize(3840) && h == cutSize(2160): w = 3840 h = 2160 // 256x1296 case w == cutSize(2304) && h == 1296: w = 2304 } //fmt.Printf("t: %d, q: %d, w: %d, h: %d\n", t, q, w, h) buf = MakeHeaders(buf, t, w, h, lqt, cqt) } // 3.1.9. JPEG Payload buf = append(buf, b...) if !packet.Marker { return } if end := buf[len(buf)-2:]; end[0] != 0xFF && end[1] != 0xD9 { buf = append(buf, 0xFF, 0xD9) } clone := *packet clone.Payload = buf buf = buf[:0] // clear buffer handlerFunc(&clone) } } func cutSize(size uint16) uint16 { return ((size >> 3) & 0xFF) << 3 } func RTPPay(handlerFunc core.HandlerFunc) core.HandlerFunc { const packetSize = 1436 sequencer := rtp.NewRandomSequencer() return func(packet *rtp.Packet) { // reincode image to more common form p, err := Transcode(packet.Payload) if err != nil { return } h1 := make([]byte, 8) h1[4] = 1 // Type h1[5] = 255 // Q // MBZ=0, Precision=0, Length=128 h2 := make([]byte, 4, 132) h2[3] = 128 var jpgData []byte for jpgData == nil { // 2 bytes h1 if p[0] != 0xFF { return } size := binary.BigEndian.Uint16(p[2:]) + 2 // 2 bytes payload size (include 2 bytes) switch p[1] { case 0xD8: // 0. Start Of Image (size=0) p = p[2:] continue case 0xDB: // 1. Define Quantization Table (size=130) for i := uint16(4 + 1); i < size; i += 1 + 64 { h2 = append(h2, p[i:i+64]...) } case 0xC0: // 2. Start Of Frame (size=15) if p[4] != 8 { return } h := binary.BigEndian.Uint16(p[5:]) w := binary.BigEndian.Uint16(p[7:]) h1[6] = uint8(w >> 3) h1[7] = uint8(h >> 3) case 0xC4: // 3. Define Huffman Table (size=416) case 0xDA: // 4. Start Of Scan (size=10) jpgData = p[size:] } p = p[size:] } offset := 0 p = make([]byte, 0) for jpgData != nil { p = p[:0] if offset > 0 { h1[1] = byte(offset >> 16) h1[2] = byte(offset >> 8) h1[3] = byte(offset) p = append(p, h1...) } else { p = append(p, h1...) p = append(p, h2...) } dataLen := packetSize - len(p) if dataLen < len(jpgData) { p = append(p, jpgData[:dataLen]...) jpgData = jpgData[dataLen:] offset += dataLen } else { p = append(p, jpgData...) jpgData = nil } clone := rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: jpgData == nil, SequenceNumber: sequencer.NextSequenceNumber(), Timestamp: packet.Timestamp, }, Payload: p, } handlerFunc(&clone) } } } func Transcode(b []byte) ([]byte, error) { img, err := jpeg.Decode(bytes.NewReader(b)) if err != nil { return nil, err } wh := img.Bounds().Size() w := wh.X h := wh.Y if w > 2040 { w = 2040 } else if w&3 > 0 { w &= 3 } if h > 2040 { h = 2040 } else if h&3 > 0 { h &= 3 } if w != wh.X || h != wh.Y { x0 := (wh.X - w) / 2 y0 := (wh.Y - h) / 2 rect := image.Rect(x0, y0, x0+w, y0+h) img = img.(*image.YCbCr).SubImage(rect) } buf := bytes.NewBuffer(nil) if err = jpeg.Encode(buf, img, nil); err != nil { return nil, err } return buf.Bytes(), nil } ================================================ FILE: pkg/mjpeg/writer.go ================================================ package mjpeg import ( "io" "net/http" "strconv" ) func NewWriter(w io.Writer) io.Writer { h := w.(http.ResponseWriter).Header() h.Set("Content-Type", "multipart/x-mixed-replace; boundary=frame") return &writer{wr: w, buf: []byte(header)} } const header = "--frame\r\nContent-Type: image/jpeg\r\nContent-Length: " type writer struct { wr io.Writer buf []byte } func (w *writer) Write(p []byte) (n int, err error) { w.buf = w.buf[:len(header)] w.buf = append(w.buf, strconv.Itoa(len(p))...) w.buf = append(w.buf, "\r\n\r\n"...) w.buf = append(w.buf, p...) w.buf = append(w.buf, "\r\n"...) // Chrome bug: mjpeg image always shows the second to last image // https://bugs.chromium.org/p/chromium/issues/detail?id=527446 if _, err = w.wr.Write(w.buf); err != nil { return 0, err } w.wr.(http.Flusher).Flush() return len(p), nil } ================================================ FILE: pkg/mp4/README.md ================================================ ## Fragmented MP4 ``` ffmpeg -i "rtsp://..." -movflags +frag_keyframe+separate_moof+default_base_moof+empty_moov -frag_duration 1 -c copy -t 5 sample.mp4 ``` - movflags frag_keyframe Start a new fragment at each video keyframe. - frag_duration duration Create fragments that are duration microseconds long. - movflags separate_moof Write a separate moof (movie fragment) atom for each track. - movflags default_base_moof Similarly to the omit_tfhd_offset, this flag avoids writing the absolute base_data_offset field in tfhd atoms, but does so by using the new default-base-is-moof flag instead. https://ffmpeg.org/ffmpeg-formats.html#Options-13 ## HEVC | Browser | avc1 | hvc1 | hev1 | |-------------|------|------|------| | Mac Chrome | + | - | + | | Mac Safari | + | + | - | | iOS 15? | + | + | - | | Mac Firefox | + | - | - | | iOS 12 | + | - | - | | Android 13 | + | - | - | ## Useful links - https://stackoverflow.com/questions/63468587/what-hevc-codec-tag-to-use-with-fmp4-hvc1-or-hev1 - https://stackoverflow.com/questions/32152090/encode-h265-to-hvc1-codec - https://jellyfin.org/docs/general/clients/codec-support.html - https://github.com/StaZhu/enable-chromium-hevc-hardware-decoding - https://developer.mozilla.org/ru/docs/Web/Media/Formats/codecs_parameter - https://gstreamer-devel.narkive.com/rhkUolp2/rtp-dts-pts-result-in-varying-mp4-frame-durations ================================================ FILE: pkg/mp4/consumer.go ================================================ package mp4 import ( "errors" "io" "sync" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/AlexxIT/go2rtc/pkg/pcm" "github.com/pion/rtp" ) type Consumer struct { core.Connection wr *core.WriteBuffer muxer *Muxer mu sync.Mutex start bool Rotate int `json:"-"` ScaleX int `json:"-"` ScaleY int `json:"-"` } func NewConsumer(medias []*core.Media) *Consumer { if medias == nil { // default local medias medias = []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecH264}, {Name: core.CodecH265}, }, }, { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecAAC}, }, }, } } wr := core.NewWriteBuffer(nil) return &Consumer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "mp4", Medias: medias, Transport: wr, }, muxer: &Muxer{}, wr: wr, } } func (c *Consumer) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error { trackID := byte(len(c.Senders)) codec := track.Codec.Clone() handler := core.NewSender(media, codec) switch track.Codec.Name { case core.CodecH264: handler.Handler = func(packet *rtp.Packet) { if !c.start { if !h264.IsKeyframe(packet.Payload) { return } c.start = true } // important to use Mutex because right fragment order c.mu.Lock() b := c.muxer.GetPayload(trackID, packet) if n, err := c.wr.Write(b); err == nil { c.Send += n } c.mu.Unlock() } if track.Codec.IsRTP() { handler.Handler = h264.RTPDepay(track.Codec, handler.Handler) } else { handler.Handler = h264.RepairAVCC(track.Codec, handler.Handler) } case core.CodecH265: handler.Handler = func(packet *rtp.Packet) { if !c.start { if !h265.IsKeyframe(packet.Payload) { return } c.start = true } // important to use Mutex because right fragment order c.mu.Lock() b := c.muxer.GetPayload(trackID, packet) if n, err := c.wr.Write(b); err == nil { c.Send += n } c.mu.Unlock() } if track.Codec.IsRTP() { handler.Handler = h265.RTPDepay(track.Codec, handler.Handler) } else { handler.Handler = h265.RepairAVCC(track.Codec, handler.Handler) } default: handler.Handler = func(packet *rtp.Packet) { if !c.start { return } // important to use Mutex because right fragment order c.mu.Lock() b := c.muxer.GetPayload(trackID, packet) if n, err := c.wr.Write(b); err == nil { c.Send += n } c.mu.Unlock() } switch track.Codec.Name { case core.CodecAAC: if track.Codec.IsRTP() { handler.Handler = aac.RTPDepay(handler.Handler) } case core.CodecOpus, core.CodecMP3: // no changes case core.CodecPCMA, core.CodecPCMU, core.CodecPCM, core.CodecPCML: codec.Name = core.CodecFLAC if codec.Channels == 2 { // hacky way for support two channels audio codec.Channels = 1 codec.ClockRate *= 2 } handler.Handler = pcm.FLACEncoder(track.Codec.Name, codec.ClockRate, handler.Handler) default: handler.Handler = nil } } if handler.Handler == nil { s := "mp4: unsupported codec: " + track.Codec.String() println(s) return errors.New(s) } c.muxer.AddTrack(codec) handler.HandleRTP(track) c.Senders = append(c.Senders, handler) return nil } func (c *Consumer) WriteTo(wr io.Writer) (int64, error) { if len(c.Senders) == 1 && c.Senders[0].Codec.IsAudio() { c.start = true } init, err := c.muxer.GetInit() if err != nil { return 0, err } if c.Rotate != 0 { PatchVideoRotate(init, c.Rotate) } if c.ScaleX != 0 && c.ScaleY != 0 { PatchVideoScale(init, c.ScaleX, c.ScaleY) } if _, err = wr.Write(init); err != nil { return 0, err } return c.wr.WriteTo(wr) } ================================================ FILE: pkg/mp4/demuxer.go ================================================ package mp4 import ( "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/iso" "github.com/pion/rtp" ) type Demuxer struct { codecs map[uint32]*core.Codec timeScales map[uint32]float32 } func (d *Demuxer) Probe(init []byte) (medias []*core.Media) { var trackID, timeScale uint32 if d.codecs == nil { d.codecs = make(map[uint32]*core.Codec) d.timeScales = make(map[uint32]float32) } atoms, _ := iso.DecodeAtoms(init) for _, atom := range atoms { var codec *core.Codec switch atom := atom.(type) { case *iso.AtomTkhd: trackID = atom.TrackID case *iso.AtomMdhd: timeScale = atom.TimeScale case *iso.AtomVideo: switch atom.Name { case "avc1": codec = h264.ConfigToCodec(atom.Config) } case *iso.AtomAudio: switch atom.Name { case "mp4a": codec = aac.ConfigToCodec(atom.Config) } } if codec != nil { d.codecs[trackID] = codec d.timeScales[trackID] = float32(codec.ClockRate) / float32(timeScale) medias = append(medias, &core.Media{ Kind: codec.Kind(), Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, }) } } return } func (d *Demuxer) GetTrackID(codec *core.Codec) uint32 { for trackID, c := range d.codecs { if c == codec { return trackID } } return 0 } func (d *Demuxer) Demux(data2 []byte) (trackID uint32, packets []*core.Packet) { atoms, err := iso.DecodeAtoms(data2) if err != nil { return 0, nil } var ts uint32 var trun *iso.AtomTrun var data []byte for _, atom := range atoms { switch atom := atom.(type) { case *iso.AtomTfhd: trackID = atom.TrackID case *iso.AtomTfdt: ts = uint32(atom.DecodeTime) case *iso.AtomTrun: trun = atom case *iso.AtomMdat: data = atom.Data } } timeScale := d.timeScales[trackID] if timeScale == 0 { return 0, nil } n := len(trun.SamplesDuration) packets = make([]*core.Packet, n) for i := 0; i < n; i++ { duration := trun.SamplesDuration[i] size := trun.SamplesSize[i] // can be SPS, PPS and IFrame in one packet timestamp := uint32(float32(ts) * timeScale) packets[i] = &rtp.Packet{ Header: rtp.Header{Timestamp: timestamp}, Payload: data[:size], } data = data[size:] ts += duration } return } ================================================ FILE: pkg/mp4/helpers.go ================================================ package mp4 import ( "bytes" "encoding/binary" "strings" "github.com/AlexxIT/go2rtc/pkg/core" ) // ParseQuery - like usual parse, but with mp4 param handler func ParseQuery(query map[string][]string) []*core.Media { if v := query["mp4"]; len(v) != 0 { medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecH264}, {Name: core.CodecH265}, }, }, { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecAAC}, }, }, } if v[0] == "" { return medias // legacy } medias[1].Codecs = append(medias[1].Codecs, &core.Codec{Name: core.CodecPCMA}, &core.Codec{Name: core.CodecPCMU}, &core.Codec{Name: core.CodecPCM}, &core.Codec{Name: core.CodecPCML}, ) if v[0] == "flac" { return medias // modern browsers } medias[1].Codecs = append(medias[1].Codecs, &core.Codec{Name: core.CodecOpus}, &core.Codec{Name: core.CodecMP3}, ) return medias // Chrome, FFmpeg, VLC } return core.ParseQuery(query) } func ParseCodecs(codecs string, parseAudio bool) (medias []*core.Media) { var videos []*core.Codec var audios []*core.Codec for _, name := range strings.Split(codecs, ",") { switch name { case MimeH264: codec := &core.Codec{Name: core.CodecH264} videos = append(videos, codec) case MimeH265: codec := &core.Codec{Name: core.CodecH265} videos = append(videos, codec) case MimeAAC: codec := &core.Codec{Name: core.CodecAAC} audios = append(audios, codec) case MimeFlac: audios = append(audios, &core.Codec{Name: core.CodecPCMA}, &core.Codec{Name: core.CodecPCMU}, &core.Codec{Name: core.CodecPCM}, &core.Codec{Name: core.CodecPCML}, ) case MimeOpus: codec := &core.Codec{Name: core.CodecOpus} audios = append(audios, codec) } } if videos != nil { media := &core.Media{ Kind: core.KindVideo, Direction: core.DirectionSendonly, Codecs: videos, } medias = append(medias, media) } if audios != nil && parseAudio { media := &core.Media{ Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: audios, } medias = append(medias, media) } return } // PatchVideoRotate - update video track transformation matrix. // Rotation supported by many players and browsers (except Safari). // Scale has low support and better not to use it. // Supported only 0, 90, 180, 270 degrees. func PatchVideoRotate(init []byte, degrees int) bool { // search video atom i := bytes.Index(init, []byte("vide")) if i < 0 { return false } // seek to video matrix position i -= 4 + 3 + 1 + 8 + 32 + 8 + 4 + 4 + 4*9 // Rotation matrix: // [ cos sin 0] // [ -sin cos 0] // [ 0 0 16384] var cos, sin uint16 switch degrees { case 0: cos = 1 sin = 0 case 90: cos = 0 sin = 1 case 180: cos = 0xFFFF // -1 sin = 0 case 270: cos = 0 sin = 0xFFFF // -1 default: return false } binary.BigEndian.PutUint16(init[i:], cos) binary.BigEndian.PutUint16(init[i+4:], sin) binary.BigEndian.PutUint16(init[i+12:], -sin) binary.BigEndian.PutUint16(init[i+16:], cos) return true } // PatchVideoScale - update "Pixel Aspect Ratio" atom. // Supported by many players and browsers (except Firefox). // Supported only positive integers. func PatchVideoScale(init []byte, scaleX, scaleY int) bool { // search video atom i := bytes.Index(init, []byte("pasp")) if i < 0 { return false } binary.BigEndian.PutUint32(init[i+4:], uint32(scaleX)) binary.BigEndian.PutUint32(init[i+8:], uint32(scaleY)) return true } ================================================ FILE: pkg/mp4/keyframe.go ================================================ package mp4 import ( "io" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/pion/rtp" ) type Keyframe struct { core.Connection wr *core.WriteBuffer muxer *Muxer } // Deprecated: should be rewritten func NewKeyframe(medias []*core.Media) *Keyframe { if medias == nil { medias = []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecH264}, {Name: core.CodecH265}, }, }, } } wr := core.NewWriteBuffer(nil) cons := &Keyframe{ Connection: core.Connection{ ID: core.NewID(), FormatName: "mp4", Transport: wr, }, muxer: &Muxer{}, wr: wr, } cons.Medias = medias return cons } func (c *Keyframe) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error { c.muxer.AddTrack(track.Codec) init, err := c.muxer.GetInit() if err != nil { return err } handler := core.NewSender(media, track.Codec) switch track.Codec.Name { case core.CodecH264: handler.Handler = func(packet *rtp.Packet) { if !h264.IsKeyframe(packet.Payload) { return } // important to use Mutex because right fragment order b := c.muxer.GetPayload(0, packet) b = append(init, b...) if n, err := c.wr.Write(b); err == nil { c.Send += n } } if track.Codec.IsRTP() { handler.Handler = h264.RTPDepay(track.Codec, handler.Handler) } else { handler.Handler = h264.RepairAVCC(track.Codec, handler.Handler) } case core.CodecH265: handler.Handler = func(packet *rtp.Packet) { if !h265.IsKeyframe(packet.Payload) { return } // important to use Mutex because right fragment order b := c.muxer.GetPayload(0, packet) b = append(init, b...) if n, err := c.wr.Write(b); err == nil { c.Send += n } } if track.Codec.IsRTP() { handler.Handler = h265.RTPDepay(track.Codec, handler.Handler) } } handler.HandleRTP(track) c.Senders = append(c.Senders, handler) return nil } func (c *Keyframe) WriteTo(wr io.Writer) (int64, error) { return c.wr.WriteTo(wr) } ================================================ FILE: pkg/mp4/mime.go ================================================ package mp4 import ( "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" ) const ( MimeH264 = "avc1.640029" MimeH265 = "hvc1.1.6.L153.B0" MimeAAC = "mp4a.40.2" MimeFlac = "flac" MimeOpus = "opus" ) func MimeCodecs(codecs []*core.Codec) string { var s string for i, codec := range codecs { if i > 0 { s += "," } switch codec.Name { case core.CodecH264: s += "avc1." + h264.GetProfileLevelID(codec.FmtpLine) case core.CodecH265: // H.265 profile=main level=5.1 // hvc1 - supported in Safari, hev1 - doesn't, both supported in Chrome s += MimeH265 case core.CodecAAC: s += MimeAAC case core.CodecOpus: s += MimeOpus case core.CodecFLAC: s += MimeFlac } } return s } func ContentType(codecs []*core.Codec) string { return `video/mp4; codecs="` + MimeCodecs(codecs) + `"` } ================================================ FILE: pkg/mp4/muxer.go ================================================ package mp4 import ( "encoding/hex" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/AlexxIT/go2rtc/pkg/iso" "github.com/pion/rtp" ) type Muxer struct { index uint32 dts []uint64 pts []uint32 codecs []*core.Codec } func (m *Muxer) AddTrack(codec *core.Codec) { m.dts = append(m.dts, 0) m.pts = append(m.pts, 0) m.codecs = append(m.codecs, codec) } func (m *Muxer) GetInit() ([]byte, error) { mv := iso.NewMovie(1024) mv.WriteFileType() mv.StartAtom(iso.Moov) mv.WriteMovieHeader() for i, codec := range m.codecs { switch codec.Name { case core.CodecH264: sps, pps := h264.GetParameterSet(codec.FmtpLine) // some dummy SPS and PPS not a problem for MP4, but problem for HLS :( if len(sps) == 0 { sps = []byte{0x67, 0x42, 0x00, 0x0a, 0xf8, 0x41, 0xa2} } if len(pps) == 0 { pps = []byte{0x68, 0xce, 0x38, 0x80} } var width, height uint16 if s := h264.DecodeSPS(sps); s != nil { width = s.Width() height = s.Height() } else { width = 1920 height = 1080 } mv.WriteVideoTrack( uint32(i+1), codec.Name, codec.ClockRate, width, height, h264.EncodeConfig(sps, pps), ) case core.CodecH265: vps, sps, pps := h265.GetParameterSet(codec.FmtpLine) // some dummy SPS and PPS not a problem if len(vps) == 0 { vps = []byte{0x40, 0x01, 0x0c, 0x01, 0xff, 0xff, 0x01, 0x40, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x99, 0xac, 0x09} } if len(sps) == 0 { sps = []byte{0x42, 0x01, 0x01, 0x01, 0x40, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x00, 0x03, 0x00, 0x99, 0xa0, 0x01, 0x40, 0x20, 0x05, 0xa1, 0xfe, 0x5a, 0xee, 0x46, 0xc1, 0xae, 0x55, 0x04} } if len(pps) == 0 { pps = []byte{0x44, 0x01, 0xc0, 0x73, 0xc0, 0x4c, 0x90} } var width, height uint16 if s := h265.DecodeSPS(sps); s != nil { width = s.Width() height = s.Height() } else { width = 1920 height = 1080 } mv.WriteVideoTrack( uint32(i+1), codec.Name, codec.ClockRate, width, height, h265.EncodeConfig(vps, sps, pps), ) case core.CodecAAC: s := core.Between(codec.FmtpLine, "config=", ";") b, err := hex.DecodeString(s) if err != nil { return nil, err } mv.WriteAudioTrack( uint32(i+1), codec.Name, codec.ClockRate, uint16(codec.Channels), b, ) case core.CodecOpus, core.CodecMP3, core.CodecPCMA, core.CodecPCMU, core.CodecFLAC: mv.WriteAudioTrack( uint32(i+1), codec.Name, codec.ClockRate, uint16(codec.Channels), nil, ) } } mv.StartAtom(iso.MoovMvex) for i := range m.codecs { mv.WriteTrackExtend(uint32(i + 1)) } mv.EndAtom() // MVEX mv.EndAtom() // MOOV return mv.Bytes(), nil } func (m *Muxer) Reset() { m.index = 0 for i := range m.dts { m.dts[i] = 0 m.pts[i] = 0 } } func (m *Muxer) GetPayload(trackID byte, packet *rtp.Packet) []byte { codec := m.codecs[trackID] m.index++ duration := packet.Timestamp - m.pts[trackID] m.pts[trackID] = packet.Timestamp // flags important for Apple Finder video preview var flags uint32 switch codec.Name { case core.CodecH264: if h264.IsKeyframe(packet.Payload) { flags = iso.SampleVideoIFrame } else { flags = iso.SampleVideoNonIFrame } case core.CodecH265: if h265.IsKeyframe(packet.Payload) { flags = iso.SampleVideoIFrame } else { flags = iso.SampleVideoNonIFrame } case core.CodecAAC: duration = 1024 // important for Apple Finder and QuickTime flags = iso.SampleAudio // not important? default: flags = iso.SampleAudio // important for FLAC on Android Telegram } // minumum duration important for MSE in Apple Safari if duration == 0 || duration > codec.ClockRate { duration = codec.ClockRate/1000 + 1 m.pts[trackID] += duration } size := len(packet.Payload) mv := iso.NewMovie(1024 + size) mv.WriteMovieFragment( // ExtensionProfile - wrong place for CTS (supported by mpegts.Demuxer) m.index, uint32(trackID+1), duration, uint32(size), flags, m.dts[trackID], uint32(packet.ExtensionProfile), ) mv.WriteData(packet.Payload) //log.Printf("[MP4] idx:%3d trk:%d dts:%6d cts:%4d dur:%5d time:%10d len:%5d", m.index, trackID+1, m.dts[trackID], packet.SSRC, duration, packet.Timestamp, len(packet.Payload)) m.dts[trackID] += uint64(duration) return mv.Bytes() } ================================================ FILE: pkg/mpegts/README.md ================================================ ## PTS/DTS/CTS ``` if DTS == 0 { // for I and P frames packet.Timestamp = PTS (presentation time) } else { // for B frames packet.Timestamp = DTS (decode time) CTS = PTS-DTS (composition time) } ``` - MPEG-TS container uses PTS and optional DTS. - MP4 container uses DTS and CTS - RTP container uses PTS ## MPEG-TS FFmpeg: - PMTID=4096 - H264: PESID=256, StreamType=27, StreamID=224 - H265: PESID=256, StreamType=36, StreamID=224 - AAC: PESID=257, StreamType=15, StreamID=192 Tapo: - PMTID=18 - H264: PESID=68, StreamType=27, StreamID=224 - AAC: PESID=69, StreamType=144, StreamID=192 ## Useful links - https://github.com/theREDspace/video-onboarding/blob/main/MPEGTS%20Knowledge.md - https://en.wikipedia.org/wiki/MPEG_transport_stream - https://en.wikipedia.org/wiki/Program-specific_information ================================================ FILE: pkg/mpegts/checksum.go ================================================ package mpegts // have to create this table manually because it is in another endian // https://github.com/arturvt/TSreader/blob/master/src/br/ufpe/cin/tool/mpegts/CRC32.java var table = [256]uint32{ 0x00000000, 0xB71DC104, 0x6E3B8209, 0xD926430D, 0xDC760413, 0x6B6BC517, 0xB24D861A, 0x0550471E, 0xB8ED0826, 0x0FF0C922, 0xD6D68A2F, 0x61CB4B2B, 0x649B0C35, 0xD386CD31, 0x0AA08E3C, 0xBDBD4F38, 0x70DB114C, 0xC7C6D048, 0x1EE09345, 0xA9FD5241, 0xACAD155F, 0x1BB0D45B, 0xC2969756, 0x758B5652, 0xC836196A, 0x7F2BD86E, 0xA60D9B63, 0x11105A67, 0x14401D79, 0xA35DDC7D, 0x7A7B9F70, 0xCD665E74, 0xE0B62398, 0x57ABE29C, 0x8E8DA191, 0x39906095, 0x3CC0278B, 0x8BDDE68F, 0x52FBA582, 0xE5E66486, 0x585B2BBE, 0xEF46EABA, 0x3660A9B7, 0x817D68B3, 0x842D2FAD, 0x3330EEA9, 0xEA16ADA4, 0x5D0B6CA0, 0x906D32D4, 0x2770F3D0, 0xFE56B0DD, 0x494B71D9, 0x4C1B36C7, 0xFB06F7C3, 0x2220B4CE, 0x953D75CA, 0x28803AF2, 0x9F9DFBF6, 0x46BBB8FB, 0xF1A679FF, 0xF4F63EE1, 0x43EBFFE5, 0x9ACDBCE8, 0x2DD07DEC, 0x77708634, 0xC06D4730, 0x194B043D, 0xAE56C539, 0xAB068227, 0x1C1B4323, 0xC53D002E, 0x7220C12A, 0xCF9D8E12, 0x78804F16, 0xA1A60C1B, 0x16BBCD1F, 0x13EB8A01, 0xA4F64B05, 0x7DD00808, 0xCACDC90C, 0x07AB9778, 0xB0B6567C, 0x69901571, 0xDE8DD475, 0xDBDD936B, 0x6CC0526F, 0xB5E61162, 0x02FBD066, 0xBF469F5E, 0x085B5E5A, 0xD17D1D57, 0x6660DC53, 0x63309B4D, 0xD42D5A49, 0x0D0B1944, 0xBA16D840, 0x97C6A5AC, 0x20DB64A8, 0xF9FD27A5, 0x4EE0E6A1, 0x4BB0A1BF, 0xFCAD60BB, 0x258B23B6, 0x9296E2B2, 0x2F2BAD8A, 0x98366C8E, 0x41102F83, 0xF60DEE87, 0xF35DA999, 0x4440689D, 0x9D662B90, 0x2A7BEA94, 0xE71DB4E0, 0x500075E4, 0x892636E9, 0x3E3BF7ED, 0x3B6BB0F3, 0x8C7671F7, 0x555032FA, 0xE24DF3FE, 0x5FF0BCC6, 0xE8ED7DC2, 0x31CB3ECF, 0x86D6FFCB, 0x8386B8D5, 0x349B79D1, 0xEDBD3ADC, 0x5AA0FBD8, 0xEEE00C69, 0x59FDCD6D, 0x80DB8E60, 0x37C64F64, 0x3296087A, 0x858BC97E, 0x5CAD8A73, 0xEBB04B77, 0x560D044F, 0xE110C54B, 0x38368646, 0x8F2B4742, 0x8A7B005C, 0x3D66C158, 0xE4408255, 0x535D4351, 0x9E3B1D25, 0x2926DC21, 0xF0009F2C, 0x471D5E28, 0x424D1936, 0xF550D832, 0x2C769B3F, 0x9B6B5A3B, 0x26D61503, 0x91CBD407, 0x48ED970A, 0xFFF0560E, 0xFAA01110, 0x4DBDD014, 0x949B9319, 0x2386521D, 0x0E562FF1, 0xB94BEEF5, 0x606DADF8, 0xD7706CFC, 0xD2202BE2, 0x653DEAE6, 0xBC1BA9EB, 0x0B0668EF, 0xB6BB27D7, 0x01A6E6D3, 0xD880A5DE, 0x6F9D64DA, 0x6ACD23C4, 0xDDD0E2C0, 0x04F6A1CD, 0xB3EB60C9, 0x7E8D3EBD, 0xC990FFB9, 0x10B6BCB4, 0xA7AB7DB0, 0xA2FB3AAE, 0x15E6FBAA, 0xCCC0B8A7, 0x7BDD79A3, 0xC660369B, 0x717DF79F, 0xA85BB492, 0x1F467596, 0x1A163288, 0xAD0BF38C, 0x742DB081, 0xC3307185, 0x99908A5D, 0x2E8D4B59, 0xF7AB0854, 0x40B6C950, 0x45E68E4E, 0xF2FB4F4A, 0x2BDD0C47, 0x9CC0CD43, 0x217D827B, 0x9660437F, 0x4F460072, 0xF85BC176, 0xFD0B8668, 0x4A16476C, 0x93300461, 0x242DC565, 0xE94B9B11, 0x5E565A15, 0x87701918, 0x306DD81C, 0x353D9F02, 0x82205E06, 0x5B061D0B, 0xEC1BDC0F, 0x51A69337, 0xE6BB5233, 0x3F9D113E, 0x8880D03A, 0x8DD09724, 0x3ACD5620, 0xE3EB152D, 0x54F6D429, 0x7926A9C5, 0xCE3B68C1, 0x171D2BCC, 0xA000EAC8, 0xA550ADD6, 0x124D6CD2, 0xCB6B2FDF, 0x7C76EEDB, 0xC1CBA1E3, 0x76D660E7, 0xAFF023EA, 0x18EDE2EE, 0x1DBDA5F0, 0xAAA064F4, 0x738627F9, 0xC49BE6FD, 0x09FDB889, 0xBEE0798D, 0x67C63A80, 0xD0DBFB84, 0xD58BBC9A, 0x62967D9E, 0xBBB03E93, 0x0CADFF97, 0xB110B0AF, 0x060D71AB, 0xDF2B32A6, 0x6836F3A2, 0x6D66B4BC, 0xDA7B75B8, 0x035D36B5, 0xB440F7B1, } func checksum(data []byte) uint32 { crc := uint32(0xFFFFFFFF) for _, b := range data { crc = table[b^byte(crc)] ^ (crc >> 8) } return crc } ================================================ FILE: pkg/mpegts/consumer.go ================================================ package mpegts import ( "io" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/pion/rtp" ) type Consumer struct { core.Connection muxer *Muxer wr *core.WriteBuffer } func NewConsumer() *Consumer { medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecH264}, {Name: core.CodecH265}, }, }, { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecAAC}, }, }, } wr := core.NewWriteBuffer(nil) return &Consumer{ core.Connection{ ID: core.NewID(), FormatName: "mpegts", Medias: medias, Transport: wr, }, NewMuxer(), wr, } } func (c *Consumer) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { sender := core.NewSender(media, track.Codec) switch track.Codec.Name { case core.CodecH264: pid := c.muxer.AddTrack(StreamTypeH264) sender.Handler = func(pkt *rtp.Packet) { b := c.muxer.GetPayload(pid, pkt.Timestamp, pkt.Payload) if n, err := c.wr.Write(b); err == nil { c.Send += n } } if track.Codec.IsRTP() { sender.Handler = h264.RTPDepay(track.Codec, sender.Handler) } else { sender.Handler = h264.RepairAVCC(track.Codec, sender.Handler) } case core.CodecH265: pid := c.muxer.AddTrack(StreamTypeH265) sender.Handler = func(pkt *rtp.Packet) { b := c.muxer.GetPayload(pid, pkt.Timestamp, pkt.Payload) if n, err := c.wr.Write(b); err == nil { c.Send += n } } if track.Codec.IsRTP() { sender.Handler = h265.RTPDepay(track.Codec, sender.Handler) } case core.CodecAAC: pid := c.muxer.AddTrack(StreamTypeAAC) // convert timestamp to 90000Hz clock dt := 90000 / float64(track.Codec.ClockRate) sender.Handler = func(pkt *rtp.Packet) { pts := uint32(float64(pkt.Timestamp) * dt) b := c.muxer.GetPayload(pid, pts, pkt.Payload) if n, err := c.wr.Write(b); err == nil { c.Send += n } } if track.Codec.IsRTP() { sender.Handler = aac.RTPToADTS(track.Codec, sender.Handler) } else { sender.Handler = aac.EncodeToADTS(track.Codec, sender.Handler) } } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } func (c *Consumer) WriteTo(wr io.Writer) (int64, error) { b := c.muxer.GetHeader() if _, err := wr.Write(b); err != nil { return 0, err } return c.wr.WriteTo(wr) } //func TimestampFromRTP(rtp *rtp.Packet, codec *core.Codec) { // if codec.ClockRate == ClockRate { // return // } // rtp.Timestamp = uint32(float64(rtp.Timestamp) / float64(codec.ClockRate) * ClockRate) //} ================================================ FILE: pkg/mpegts/demuxer.go ================================================ package mpegts import ( "bytes" "errors" "io" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/bits" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/pion/rtp" ) type Demuxer struct { buf [PacketSize]byte // total buf byte byte // current byte bits byte // bits left in byte pos byte // current pos in buf end byte // end position pmtID uint16 // Program Map Table (PMT) PID pes map[uint16]*PES } func NewDemuxer() *Demuxer { return &Demuxer{} } const skipRead = 0xFF func (d *Demuxer) ReadPacket(rd io.Reader) (*rtp.Packet, error) { for { if d.pos != skipRead { if _, err := io.ReadFull(rd, d.buf[:]); err != nil { return nil, err } } pid, start, err := d.readPacketHeader() if err != nil { return nil, err } if d.pes == nil { switch pid { case 0: // PAT ID d.readPAT() // PAT: Program Association Table case d.pmtID: d.readPMT() // PMT : Program Map Table pkt := &rtp.Packet{ Payload: make([]byte, 0, len(d.pes)), } for _, pes := range d.pes { pkt.Payload = append(pkt.Payload, pes.StreamType) } return pkt, nil } continue } if pkt := d.readPES(pid, start); pkt != nil { return pkt, nil } } } func (d *Demuxer) readPacketHeader() (pid uint16, start bool, err error) { d.reset() sb := d.readByte() // Sync byte if sb != SyncByte { return 0, false, errors.New("mpegts: wrong sync byte") } _ = d.readBit() // Transport error indicator (TEI) pusi := d.readBit() // Payload unit start indicator (PUSI) _ = d.readBit() // Transport priority pid = d.readBits16(13) // PID _ = d.readBits(2) // Transport scrambling control (TSC) af := d.readBit() // Adaptation field _ = d.readBit() // Payload _ = d.readBits(4) // Continuity counter if af != 0 { adSize := d.readByte() // Adaptation field length if adSize > PacketSize-6 { return 0, false, errors.New("mpegts: wrong adaptation size") } d.skip(adSize) } return pid, pusi != 0, nil } func (d *Demuxer) skip(i byte) { d.pos += i } func (d *Demuxer) readBytes(i byte) []byte { d.pos += i return d.buf[d.pos-i : d.pos] } func (d *Demuxer) readPSIHeader() { // https://en.wikipedia.org/wiki/Program-specific_information#Table_Sections pointer := d.readByte() // Pointer field d.skip(pointer) // Pointer filler bytes _ = d.readByte() // Table ID _ = d.readBit() // Section syntax indicator _ = d.readBit() // Private bit _ = d.readBits(2) // Reserved bits _ = d.readBits(2) // Section length unused bits size := d.readBits(10) // Section length d.setSize(byte(size)) _ = d.readBits(16) // Table ID extension _ = d.readBits(2) // Reserved bits _ = d.readBits(5) // Version number _ = d.readBit() // Current/next indicator _ = d.readByte() // Section number _ = d.readByte() // Last section number } // ReadPAT (Program Association Table) func (d *Demuxer) readPAT() { // https://en.wikipedia.org/wiki/Program-specific_information#PAT_(Program_Association_Table) d.readPSIHeader() const CRCSize = 4 for d.left() > CRCSize { num := d.readBits(16) // Program num _ = d.readBits(3) // Reserved bits pid := d.readBits16(13) // Program map PID if num != 0 { d.pmtID = pid } } d.skip(4) // CRC32 } // ReadPMT (Program map specific data) func (d *Demuxer) readPMT() { // https://en.wikipedia.org/wiki/Program-specific_information#PMT_(Program_map_specific_data) d.readPSIHeader() _ = d.readBits(3) // Reserved bits _ = d.readBits(13) // PCR PID _ = d.readBits(4) // Reserved bits _ = d.readBits(2) // Program info length unused bits size := d.readBits(10) // Program info length d.skip(byte(size)) d.pes = map[uint16]*PES{} const CRCSize = 4 for d.left() > CRCSize { streamType := d.readByte() // Stream type _ = d.readBits(3) // Reserved bits pid := d.readBits16(13) // Elementary PID _ = d.readBits(4) // Reserved bits _ = d.readBits(2) // ES Info length unused bits size = d.readBits(10) // ES Info length info := d.readBytes(byte(size)) if streamType == StreamTypePrivate && bytes.HasPrefix(info, opusInfo) { streamType = StreamTypePrivateOPUS } d.pes[pid] = &PES{StreamType: streamType} } d.skip(4) // CRC32 } func (d *Demuxer) readPES(pid uint16, start bool) *rtp.Packet { pes := d.pes[pid] if pes == nil { return nil } // if new payload beging if start { if len(pes.Payload) != 0 { d.pos = skipRead return pes.GetPacket() // finish previous packet } // https://en.wikipedia.org/wiki/Packetized_elementary_stream // Packet start code prefix if d.readByte() != 0 || d.readByte() != 0 || d.readByte() != 1 { return nil } pes.StreamID = d.readByte() // Stream id packetSize := d.readBits16(16) // PES Packet length _ = d.readBits(2) // Marker bits _ = d.readBits(2) // Scrambling control _ = d.readBit() // Priority _ = d.readBit() // Data alignment indicator _ = d.readBit() // Copyright _ = d.readBit() // Original or Copy ptsi := d.readBit() // PTS indicator dtsi := d.readBit() // DTS indicator _ = d.readBit() // ESCR flag _ = d.readBit() // ES rate flag _ = d.readBit() // DSM trick mode flag _ = d.readBit() // Additional copy info flag _ = d.readBit() // CRC flag _ = d.readBit() // extension flag headerSize := d.readByte() // PES header length if packetSize != 0 { packetSize -= uint16(3 + headerSize) } if ptsi != 0 { pes.PTS = d.readTime() headerSize -= 5 } else { pes.PTS = 0 } if dtsi != 0 { pes.DTS = d.readTime() headerSize -= 5 } else { pes.DTS = 0 } d.skip(headerSize) pes.SetBuffer(packetSize, d.bytes()) } else { pes.AppendBuffer(d.bytes()) } if pes.Size != 0 && len(pes.Payload) >= pes.Size { return pes.GetPacket() // finish current packet } return nil } func (d *Demuxer) reset() { d.pos = 0 d.end = PacketSize d.bits = 0 } //goland:noinspection GoStandardMethods func (d *Demuxer) readByte() byte { if d.bits != 0 { return byte(d.readBits(8)) } b := d.buf[d.pos] d.pos++ return b } func (d *Demuxer) readBit() byte { if d.bits == 0 { d.byte = d.readByte() d.bits = 7 } else { d.bits-- } return (d.byte >> d.bits) & 0b1 } func (d *Demuxer) readBits(n byte) (res uint32) { for i := n - 1; i != 255; i-- { res |= uint32(d.readBit()) << i } return } func (d *Demuxer) readBits16(n byte) (res uint16) { for i := n - 1; i != 255; i-- { res |= uint16(d.readBit()) << i } return } func (d *Demuxer) readTime() uint32 { // https://en.wikipedia.org/wiki/Packetized_elementary_stream // xxxxAAAx BBBBBBBB BBBBBBBx CCCCCCCC CCCCCCCx _ = d.readBits(4) // 0010b or 0011b or 0001b ts := d.readBits(3) << 30 _ = d.readBits(1) // 1b ts |= d.readBits(15) << 15 _ = d.readBits(1) // 1b ts |= d.readBits(15) _ = d.readBits(1) // 1b return ts } func (d *Demuxer) bytes() []byte { return d.buf[d.pos:PacketSize] } func (d *Demuxer) left() byte { return d.end - d.pos } func (d *Demuxer) setSize(size byte) { d.end = d.pos + size } const ( PacketSize = 188 SyncByte = 0x47 // Uppercase G ClockRate = 90000 // fixed clock rate for PTS/DTS of any type ) // https://en.wikipedia.org/wiki/Program-specific_information#Elementary_stream_types const ( StreamTypeMetadata = 0 // Reserved StreamTypePrivate = 0x06 // PCMU or PCMA or FLAC from FFmpeg StreamTypeAAC = 0x0F StreamTypeH264 = 0x1B StreamTypeH265 = 0x24 StreamTypePCMATapo = 0x90 StreamTypePCMUTapo = 0x91 StreamTypePrivateOPUS = 0xEB ) // PES - Packetized Elementary Stream type PES struct { StreamID byte // from each PES header StreamType byte // from PMT table Sequence uint16 // manual Timestamp uint32 // manual PTS uint32 // from extra header, always 90000Hz DTS uint32 Payload []byte // from PES body Size int // from PES header, can be 0 wr *bits.Writer } func (p *PES) SetBuffer(size uint16, b []byte) { p.Payload = make([]byte, 0, size) p.Payload = append(p.Payload, b...) p.Size = int(size) } func (p *PES) AppendBuffer(b []byte) { p.Payload = append(p.Payload, b...) } func (p *PES) GetPacket() (pkt *rtp.Packet) { switch p.StreamType { case StreamTypeH264, StreamTypeH265: pkt = &rtp.Packet{ Header: rtp.Header{ PayloadType: p.StreamType, }, Payload: annexb.EncodeToAVCC(p.Payload), } if p.DTS != 0 { pkt.Timestamp = p.DTS // wrong place for CTS, but we don't have another one pkt.ExtensionProfile = uint16(p.PTS - p.DTS) } else { pkt.Timestamp = p.PTS } case StreamTypeAAC: p.Sequence++ pkt = &rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: true, PayloadType: p.StreamType, SequenceNumber: p.Sequence, Timestamp: p.PTS, //Timestamp: p.Timestamp, }, Payload: aac.ADTStoRTP(p.Payload), } //p.Timestamp += aac.RTPTimeSize(pkt.Payload) // update next timestamp! case StreamTypePCMATapo, StreamTypePCMUTapo: p.Sequence++ pkt = &rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: true, PayloadType: p.StreamType, SequenceNumber: p.Sequence, Timestamp: p.PTS, //Timestamp: p.Timestamp, }, Payload: p.Payload, } //p.Timestamp += uint32(len(p.Payload)) // update next timestamp! case StreamTypePrivateOPUS: p.Sequence++ pkt = &rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: true, PayloadType: p.StreamType, SequenceNumber: p.Sequence, Timestamp: p.PTS, }, } pkt.Payload, p.Payload = CutOPUSPacket(p.Payload) p.PTS += opusDT return } p.Payload = nil return } ================================================ FILE: pkg/mpegts/muxer.go ================================================ package mpegts import ( "encoding/binary" "github.com/AlexxIT/go2rtc/pkg/bits" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" ) type Muxer struct { pes map[uint16]*PES } func NewMuxer() *Muxer { return &Muxer{ pes: map[uint16]*PES{}, } } func (m *Muxer) AddTrack(streamType byte) (pid uint16) { pes := &PES{StreamType: streamType} // Audio streams (0xC0-0xDF), Video streams (0xE0-0xEF) switch streamType { case StreamTypeH264, StreamTypeH265: pes.StreamID = 0xE0 case StreamTypeAAC, StreamTypePCMATapo: pes.StreamID = 0xC0 } pid = pes0PID + uint16(len(m.pes)) m.pes[pid] = pes return } func (m *Muxer) GetHeader() []byte { bw := bits.NewWriter(nil) m.writePAT(bw) m.writePMT(bw) return bw.Bytes() } // GetPayload - safe to run concurently with different pid func (m *Muxer) GetPayload(pid uint16, timestamp uint32, payload []byte) []byte { pes := m.pes[pid] switch pes.StreamType { case StreamTypeH264, StreamTypeH265: payload = annexb.DecodeAVCCWithAUD(payload) } if pes.Timestamp != 0 { pes.PTS += timestamp - pes.Timestamp } pes.Timestamp = timestamp // min header size (3 byte) + adv header size (PES) size := 3 + 5 + len(payload) b := make([]byte, 6+3+5) b[0], b[1], b[2] = 0, 0, 1 // Packet start code prefix b[3] = pes.StreamID // Stream ID // PES Packet length (zero value OK for video) if size <= 0xFFFF { binary.BigEndian.PutUint16(b[4:], uint16(size)) } // Optional PES header: b[6] = 0x80 // Marker bits (binary) b[7] = 0x80 // PTS indicator b[8] = 5 // PES header length WriteTime(b[9:], pes.PTS) pes.Payload = append(b, payload...) pes.Size = 1 // set PUSI in first PES if pes.wr == nil { pes.wr = bits.NewWriter(nil) } else { pes.wr.Reset() } for len(pes.Payload) > 0 { m.writePES(pes.wr, pid, pes) pes.Sequence++ pes.Size = 0 } return pes.wr.Bytes() } const patPID = 0 const pmtPID = 0x1000 const pes0PID = 0x100 func (m *Muxer) writePAT(wr *bits.Writer) { m.writeHeader(wr, patPID) i := wr.Len() + 1 // start for CRC32 m.writePSIHeader(wr, 0, 4) wr.WriteUint16(1) // Program num wr.WriteBits8(0b111, 3) // Reserved bits (all to 1) wr.WriteBits16(pmtPID, 13) // Program map PID crc := checksum(wr.Bytes()[i:]) wr.WriteBytes(byte(crc), byte(crc>>8), byte(crc>>16), byte(crc>>24)) // CRC32 (little endian) m.WriteTail(wr) } func (m *Muxer) writePMT(wr *bits.Writer) { m.writeHeader(wr, pmtPID) i := wr.Len() + 1 // start for CRC32 m.writePSIHeader(wr, 2, 4+uint16(len(m.pes))*5) // 4 bytes below + 5 bytes each PES wr.WriteBits8(0b111, 3) // Reserved bits (all to 1) wr.WriteBits16(0x1FFF, 13) // Program map PID (not used) wr.WriteBits8(0b1111, 4) // Reserved bits (all to 1) wr.WriteBits8(0, 2) // Program info length unused bits (all to 0) wr.WriteBits16(0, 10) // Program info length for pid := uint16(pes0PID); ; pid++ { pes, ok := m.pes[pid] if !ok { break } wr.WriteByte(pes.StreamType) // Stream type wr.WriteBits8(0b111, 3) // Reserved bits (all to 1) wr.WriteBits16(pid, 13) // Elementary PID wr.WriteBits8(0b1111, 4) // Reserved bits (all to 1) wr.WriteBits(0, 2) // ES Info length unused bits wr.WriteBits16(0, 10) // ES Info length } crc := checksum(wr.Bytes()[i:]) wr.WriteBytes(byte(crc), byte(crc>>8), byte(crc>>16), byte(crc>>24)) // CRC32 (little endian) m.WriteTail(wr) } func (m *Muxer) writePES(wr *bits.Writer, pid uint16, pes *PES) { const flagPUSI = 0b01000000_00000000 const flagAdaptation = 0b00100000 const flagPayload = 0b00010000 wr.WriteByte(SyncByte) if pes.Size != 0 { pid |= flagPUSI // Payload unit start indicator (PUSI) } wr.WriteUint16(pid) counter := byte(pes.Sequence) & 0xF if size := len(pes.Payload); size < PacketSize-4 { wr.WriteByte(flagAdaptation | flagPayload | counter) // adaptation + payload // for 183 payload will be zero adSize := PacketSize - 4 - 1 - byte(size) wr.WriteByte(adSize) wr.WriteBytes(make([]byte, adSize)...) wr.WriteBytes(pes.Payload...) pes.Payload = nil } else { wr.WriteByte(flagPayload | counter) // only payload wr.WriteBytes(pes.Payload[:PacketSize-4]...) pes.Payload = pes.Payload[PacketSize-4:] } } func (m *Muxer) writeHeader(wr *bits.Writer, pid uint16) { wr.WriteByte(SyncByte) wr.WriteBit(0) // Transport error indicator (TEI) wr.WriteBit(1) // Payload unit start indicator (PUSI) wr.WriteBit(0) // Transport priority wr.WriteBits16(pid, 13) // PID wr.WriteBits8(0, 2) // Transport scrambling control (TSC) wr.WriteBit(0) // Adaptation field wr.WriteBit(1) // Payload wr.WriteBits8(0, 4) // Continuity counter } func (m *Muxer) writePSIHeader(wr *bits.Writer, tableID byte, size uint16) { wr.WriteByte(0) // Pointer field wr.WriteByte(tableID) // Table ID wr.WriteBit(1) // Section syntax indicator wr.WriteBit(0) // Private bit wr.WriteBits8(0b11, 2) // Reserved bits (all to 1) wr.WriteBits8(0, 2) // Section length unused bits (all to 0) wr.WriteBits16(5+size+4, 10) // Section length (5 bytes below + content + 4 bytes CRC32) wr.WriteUint16(1) // Table ID extension wr.WriteBits8(0b11, 2) // Reserved bits (all to 1) wr.WriteBits8(0, 5) // Version number wr.WriteBit(1) // Current/next indicator wr.WriteByte(0) // Section number wr.WriteByte(0) // Last section number } func (m *Muxer) WriteTail(wr *bits.Writer) { size := PacketSize - wr.Len()%PacketSize wr.WriteBytes(make([]byte, size)...) } func WriteTime(b []byte, t uint32) { _ = b[4] // bounds const onlyPTS = 0x20 b[0] = onlyPTS | byte(t>>(32-3)) | 1 b[1] = byte(t >> (24 - 2)) b[2] = byte(t>>(16-2)) | 1 b[3] = byte(t >> (8 - 1)) b[4] = byte(t<<1) | 1 // t>>(0-1) } ================================================ FILE: pkg/mpegts/opus.go ================================================ package mpegts import ( "github.com/AlexxIT/go2rtc/pkg/bits" ) // opusDT - each AU from FFmpeg has 5 OPUS packets. Each packet len = 960 in the 48000 clock. const opusDT = 960 * ClockRate / 48000 // https://opus-codec.org/docs/ var opusInfo = []byte{ // registration_descriptor 0x05, // descriptor_tag 0x04, // descriptor_length 'O', 'p', 'u', 's', // format_identifier } //goland:noinspection GoSnakeCaseUsage func CutOPUSPacket(b []byte) (packet []byte, left []byte) { r := bits.NewReader(b) size := opus_control_header(r) if size == 0 { return nil, nil } packet = r.ReadBytes(size) left = r.Left() return } //goland:noinspection GoSnakeCaseUsage func opus_control_header(r *bits.Reader) int { control_header_prefix := r.ReadBits(11) if control_header_prefix != 0x3FF { return 0 } start_trim_flag := r.ReadBit() end_trim_flag := r.ReadBit() control_extension_flag := r.ReadBit() _ = r.ReadBits(2) // reserved var payload_size int for { i := r.ReadByte() payload_size += int(i) if i < 255 { break } } if start_trim_flag != 0 { _ = r.ReadBits(3) _ = r.ReadBits(13) } if end_trim_flag != 0 { _ = r.ReadBits(3) _ = r.ReadBits(13) } if control_extension_flag != 0 { control_extension_length := r.ReadByte() _ = r.ReadBytes(int(control_extension_length)) // reserved } return payload_size } ================================================ FILE: pkg/mpegts/producer.go ================================================ package mpegts import ( "bytes" "io" "time" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/pion/rtp" ) type Producer struct { core.Connection rd *core.ReadBuffer } func Open(rd io.Reader) (*Producer, error) { prod := &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "mpegts", Transport: rd, }, rd: core.NewReadBuffer(rd), } if err := prod.probe(); err != nil { return nil, err } return prod, nil } func (c *Producer) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { receiver, _ := c.Connection.GetTrack(media, codec) receiver.ID = StreamType(codec) return receiver, nil } func (c *Producer) Start() error { rd := NewDemuxer() for { pkt, err := rd.ReadPacket(c.rd) if err != nil { return err } c.Recv += len(pkt.Payload) //log.Printf("[mpegts] size: %6d, muxer: %10d, pt: %2d", len(pkt.Payload), pkt.Timestamp, pkt.PayloadType) for _, receiver := range c.Receivers { if receiver.ID == pkt.PayloadType { TimestampToRTP(pkt, receiver.Codec) receiver.WriteRTP(pkt) break } } } } func (c *Producer) probe() error { c.rd.BufferSize = core.ProbeSize defer c.rd.Reset() rd := NewDemuxer() // Strategy: // 1. Wait packet with metadata, init other packets for wait // 2. Wait other packets // 3. Stop after timeout waitType := []byte{StreamTypeMetadata} timeout := time.Now().Add(core.ProbeTimeout) for len(waitType) != 0 && time.Now().Before(timeout) { pkt, err := rd.ReadPacket(c.rd) if err != nil { return err } // check if we wait this type if i := bytes.IndexByte(waitType, pkt.PayloadType); i < 0 { continue } else { waitType = append(waitType[:i], waitType[i+1:]...) } switch pkt.PayloadType { case StreamTypeMetadata: for _, streamType := range pkt.Payload { switch streamType { case StreamTypeH264, StreamTypeH265, StreamTypeAAC, StreamTypePrivateOPUS, StreamTypePCMATapo: waitType = append(waitType, streamType) } } case StreamTypeH264: codec := h264.AVCCToCodec(pkt.Payload) media := &core.Media{ Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, } c.Medias = append(c.Medias, media) case StreamTypeH265: codec := h265.AVCCToCodec(pkt.Payload) media := &core.Media{ Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, } c.Medias = append(c.Medias, media) case StreamTypeAAC: codec := aac.RTPToCodec(pkt.Payload) media := &core.Media{ Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, } c.Medias = append(c.Medias, media) case StreamTypePrivateOPUS: codec := &core.Codec{ Name: core.CodecOpus, ClockRate: 48000, Channels: 2, } media := &core.Media{ Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, } c.Medias = append(c.Medias, media) case StreamTypePCMATapo: codec := &core.Codec{ Name: core.CodecPCMA, ClockRate: 8000, } media := &core.Media{ Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, } c.Medias = append(c.Medias, media) } } return nil } func StreamType(codec *core.Codec) uint8 { switch codec.Name { case core.CodecH264: return StreamTypeH264 case core.CodecH265: return StreamTypeH265 case core.CodecAAC: return StreamTypeAAC case core.CodecPCMA: return StreamTypePCMATapo case core.CodecOpus: return StreamTypePrivateOPUS } return 0 } func TimestampToRTP(rtp *rtp.Packet, codec *core.Codec) { if codec.ClockRate == ClockRate { return } rtp.Timestamp = uint32(float64(rtp.Timestamp) * float64(codec.ClockRate) / ClockRate) } ================================================ FILE: pkg/mpjpeg/multipart.go ================================================ package mpjpeg import ( "bufio" "errors" "io" "net/http" "net/textproto" "strconv" "strings" ) func Next(rd *bufio.Reader) (http.Header, []byte, error) { for { // search next boundary and skip empty lines s, err := rd.ReadString('\n') if err != nil { return nil, nil, err } if s == "\r\n" { continue } if !strings.HasPrefix(s, "--") { return nil, nil, errors.New("multipart: wrong boundary: " + s) } // Foscam G2 has a awful implementation of MJPEG // https://github.com/AlexxIT/go2rtc/issues/1258 if b, _ := rd.Peek(2); string(b) == "--" { continue } break } tp := textproto.NewReader(rd) header, err := tp.ReadMIMEHeader() if err != nil { return nil, nil, err } s := header.Get("Content-Length") if s == "" { return nil, nil, errors.New("multipart: no content length") } size, err := strconv.Atoi(s) if err != nil { return nil, nil, err } buf := make([]byte, size) if _, err = io.ReadFull(rd, buf); err != nil { return nil, nil, err } return http.Header(header), buf, nil } ================================================ FILE: pkg/mpjpeg/producer.go ================================================ package mpjpeg import ( "bufio" "errors" "io" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type Producer struct { core.Connection rd *bufio.Reader } func Open(rd io.Reader) (*Producer, error) { return &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "mpjpeg", // Multipart JPEG Transport: rd, Medias: []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ { Name: core.CodecJPEG, ClockRate: 90000, PayloadType: core.PayloadTypeRAW, }, }, }, }, }, }, nil } func (c *Producer) Start() error { if len(c.Receivers) != 1 { return errors.New("mjpeg: no receivers") } rd := bufio.NewReader(c.Transport.(io.Reader)) mjpeg := c.Receivers[0] for { _, body, err := Next(rd) if err != nil { return err } c.Recv += len(body) if mjpeg != nil { packet := &rtp.Packet{ Header: rtp.Header{Timestamp: core.Now90000()}, Payload: body, } mjpeg.WriteRTP(packet) } } } ================================================ FILE: pkg/mqtt/client.go ================================================ package mqtt import ( "bytes" "encoding/binary" "errors" "io" "net" "time" ) const Timeout = time.Second * 5 type Client struct { conn net.Conn mid uint16 } func NewClient(conn net.Conn) *Client { return &Client{conn: conn, mid: 2} } func (c *Client) Connect(clientID, username, password string) (err error) { if err = c.conn.SetDeadline(time.Now().Add(Timeout)); err != nil { return } msg := NewConnect(clientID, username, password) if _, err = c.conn.Write(msg.b); err != nil { return } b := make([]byte, 4) if _, err = io.ReadFull(c.conn, b); err != nil { return } if !bytes.Equal(b, []byte{CONNACK, 2, 0, 0}) { return errors.New("wrong login") } return } func (c *Client) Subscribe(topic string) (err error) { if err = c.conn.SetDeadline(time.Now().Add(Timeout)); err != nil { return } c.mid++ msg := NewSubscribe(c.mid, topic, 1) _, err = c.conn.Write(msg.b) return } func (c *Client) Publish(topic string, payload []byte) (err error) { if err = c.conn.SetDeadline(time.Now().Add(Timeout)); err != nil { return } c.mid++ msg := NewPublishQOS1(c.mid, topic, payload) _, err = c.conn.Write(msg.b) return } func (c *Client) Read() (string, []byte, error) { if err := c.conn.SetDeadline(time.Now().Add(Timeout)); err != nil { return "", nil, err } b := make([]byte, 1) if _, err := io.ReadFull(c.conn, b); err != nil { return "", nil, err } size, err := ReadLen(c.conn) if err != nil { return "", nil, err } b0 := b[0] b = make([]byte, size) if _, err = io.ReadFull(c.conn, b); err != nil { return "", nil, err } if b0&0xF0 != PUBLISH { return "", nil, nil } i := binary.BigEndian.Uint16(b) if uint32(i) > size { return "", nil, errors.New("wrong topic size") } b = b[2:] if qos := (b0 >> 1) & 0b11; qos == 0 { return string(b[:i]), b[i:], nil } // response with packet ID _, _ = c.conn.Write([]byte{PUBACK, 2, b[i], b[i+1]}) return string(b[2:i]), b[i+2:], nil } func (c *Client) Close() error { // TODO: Teardown return c.conn.Close() } ================================================ FILE: pkg/mqtt/message.go ================================================ package mqtt import ( "io" ) type Message struct { b []byte } // https://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html const ( CONNECT = 0x10 CONNACK = 0x20 PUBLISH = 0x30 PUBACK = 0x40 SUBSCRIBE = 0x82 SUBACK = 0x90 QOS1 = 0x02 ) func (m *Message) WriteByte(b byte) { m.b = append(m.b, b) } func (m *Message) WriteBytes(b []byte) { m.b = append(m.b, b...) } func (m *Message) WriteUint16(i uint16) { m.b = append(m.b, byte(i>>8), byte(i)) } func (m *Message) WriteLen(i int) { for i > 0 { b := byte(i % 128) if i /= 128; i > 0 { b |= 0x80 } m.WriteByte(b) } } func (m *Message) WriteString(s string) { m.WriteUint16(uint16(len(s))) m.b = append(m.b, s...) } func (m *Message) Bytes() []byte { return m.b } const ( flagCleanStart = 0x02 flagUsername = 0x80 flagPassword = 0x40 ) func NewConnect(clientID, username, password string) *Message { m := &Message{} m.WriteByte(CONNECT) m.WriteLen(16 + len(clientID) + len(username) + len(password)) m.WriteString("MQTT") m.WriteByte(4) // MQTT version m.WriteByte(flagCleanStart | flagUsername | flagPassword) m.WriteUint16(30) // keepalive m.WriteString(clientID) m.WriteString(username) m.WriteString(password) return m } func NewSubscribe(mid uint16, topic string, qos byte) *Message { m := &Message{} m.WriteByte(SUBSCRIBE) m.WriteLen(5 + len(topic)) m.WriteUint16(mid) m.WriteString(topic) m.WriteByte(qos) return m } func NewPublish(topic string, payload []byte) *Message { m := &Message{} m.WriteByte(PUBLISH) m.WriteLen(2 + len(topic) + len(payload)) m.WriteString(topic) m.WriteBytes(payload) return m } func NewPublishQOS1(mid uint16, topic string, payload []byte) *Message { m := &Message{} m.WriteByte(PUBLISH | QOS1) m.WriteLen(4 + len(topic) + len(payload)) m.WriteString(topic) m.WriteUint16(mid) m.WriteBytes(payload) return m } func ReadLen(r io.Reader) (uint32, error) { var i uint32 var shift byte b := []byte{0x80} for b[0]&0x80 != 0 { if _, err := r.Read(b); err != nil { return 0, err } i += uint32(b[0]&0x7F) << shift shift += 7 } return i, nil } ================================================ FILE: pkg/multitrans/client.go ================================================ package multitrans import ( "bufio" "bytes" "errors" "fmt" "net" "net/http" "net/url" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/tcp" "github.com/google/uuid" "github.com/pion/rtp" ) type Client struct { core.Connection conn net.Conn rd *bufio.Reader closed core.Waiter } func Dial(rawURL string) (core.Producer, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } if u.Port() == "" { u.Host += ":554" } conn, err := net.DialTimeout("tcp", u.Host, core.ConnDialTimeout) if err != nil { return nil, err } c := &Client{ conn: conn, rd: bufio.NewReader(conn), } if err = c.handshake(u); err != nil { _ = conn.Close() return nil, err } c.Connection = core.Connection{ ID: core.NewID(), FormatName: "multitrans", Protocol: "rtsp", RemoteAddr: conn.RemoteAddr().String(), Source: rawURL, Medias: []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{{Name: core.CodecPCMA, ClockRate: 8000, PayloadType: 8}}, }, }, Transport: conn, } return c, nil } func (c *Client) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error { sender := core.NewSender(media, track.Codec) sender.Handler = func(packet *rtp.Packet) { clone := rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: packet.Marker, PayloadType: 8, SequenceNumber: packet.SequenceNumber, Timestamp: packet.Timestamp, SSRC: packet.SSRC, }, Payload: packet.Payload, } // Encapsulate in RTSP Interleaved Frame (Channel 1) // $ + Channel(1 byte) + Length(2 bytes) + Packet size := 12 + len(clone.Payload) b := make([]byte, 4+size) b[0] = '$' b[1] = 1 // Channel 1 for audio b[2] = byte(size >> 8) b[3] = byte(size) if _, err := clone.MarshalTo(b[4:]); err != nil { return } if _, err := c.conn.Write(b); err != nil { return } } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } func (c *Client) handshake(u *url.URL) error { // Step 1: Get Challenge uid := uuid.New().String() uri := fmt.Sprintf("rtsp://%s/multitrans", u.Host) data := fmt.Sprintf("MULTITRANS %s RTSP/1.0\r\nCSeq: 0\r\nX-Client-UUID: %s\r\n\r\n", uri, uid) if _, err := c.conn.Write([]byte(data)); err != nil { return err } res, err := tcp.ReadResponse(c.rd) if err != nil { return err } if res.StatusCode != http.StatusUnauthorized { return errors.New("multitrans: expected 401, got " + res.Status) } auth := res.Header.Get("WWW-Authenticate") realm := tcp.Between(auth, `realm="`, `"`) nonce := tcp.Between(auth, `nonce="`, `"`) // Step 2: Send Auth user := u.User.Username() pass, _ := u.User.Password() ha1 := tcp.HexMD5(user, realm, pass) ha2 := tcp.HexMD5("MULTITRANS", uri) response := tcp.HexMD5(ha1, nonce, ha2) authHeader := fmt.Sprintf(`Digest username="%s", realm="%s", nonce="%s", uri="%s", response="%s"`, user, realm, nonce, uri, response) data = fmt.Sprintf("MULTITRANS %s RTSP/1.0\r\nCSeq: 1\r\nAuthorization: %s\r\nX-Client-UUID: %s\r\n\r\n", uri, authHeader, uid) if _, err = c.conn.Write([]byte(data)); err != nil { return err } res, err = tcp.ReadResponse(c.rd) if err != nil { return err } if res.StatusCode != http.StatusOK { return errors.New("multitrans: auth failed: " + res.Status) } // Session: 7116520596809429228 session := res.Header.Get("Session") if session == "" { return errors.New("multitrans: no session") } return c.openTalkChannel(uri, session) } func (c *Client) openTalkChannel(uri, session string) error { payload := `{"type":"request","seq":0,"params":{"method":"get","talk":{"mode":"full_duplex"}}}` data := fmt.Sprintf("MULTITRANS %s RTSP/1.0\r\nCSeq: 2\r\nSession: %s\r\nContent-Type: application/json\r\nContent-Length: %d\r\n\r\n%s", uri, session, len(payload), payload) if _, err := c.conn.Write([]byte(data)); err != nil { return err } res, err := tcp.ReadResponse(c.rd) if err != nil { return err } if res.StatusCode != http.StatusOK { return errors.New("multitrans: talkback failed: " + res.Status) } // Python checks for "error_code":0 in body. if !bytes.Contains(res.Body, []byte(`"error_code":0`)) { return fmt.Errorf("multitrans: talkback error: %s", string(res.Body)) } return nil } func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return nil, core.ErrCantGetTrack } func (c *Client) Start() error { _ = c.closed.Wait() return nil } func (c *Client) Stop() error { c.closed.Done(nil) return c.Connection.Stop() } ================================================ FILE: pkg/nest/api.go ================================================ package nest import ( "bytes" "encoding/json" "errors" "net/http" "net/url" "strings" "sync" "time" ) type API struct { Token string ExpiresAt time.Time StreamProjectID string StreamDeviceID string StreamExpiresAt time.Time // WebRTC StreamSessionID string // RTSP StreamToken string StreamExtensionToken string extendTimer *time.Timer } type Auth struct { AccessToken string } type DeviceInfo struct { Name string DeviceID string Protocols []string } var cache = map[string]*API{} var cacheMu sync.Mutex func NewAPI(clientID, clientSecret, refreshToken string) (*API, error) { cacheMu.Lock() defer cacheMu.Unlock() key := clientID + ":" + clientSecret + ":" + refreshToken now := time.Now() if api := cache[key]; api != nil && now.Before(api.ExpiresAt) { return api, nil } data := url.Values{ "grant_type": []string{"refresh_token"}, "client_id": []string{clientID}, "client_secret": []string{clientSecret}, "refresh_token": []string{refreshToken}, } client := &http.Client{Timeout: time.Second * 5000} res, err := client.PostForm("https://www.googleapis.com/oauth2/v4/token", data) if err != nil { return nil, err } defer res.Body.Close() if res.StatusCode != 200 { return nil, errors.New("nest: wrong status: " + res.Status) } var resv struct { AccessToken string `json:"access_token"` ExpiresIn time.Duration `json:"expires_in"` Scope string `json:"scope"` TokenType string `json:"token_type"` } if err = json.NewDecoder(res.Body).Decode(&resv); err != nil { return nil, err } api := &API{ Token: resv.AccessToken, ExpiresAt: now.Add(resv.ExpiresIn * time.Second), } cache[key] = api return api, nil } func (a *API) GetDevices(projectID string) ([]DeviceInfo, error) { uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" + projectID + "/devices" req, err := http.NewRequest("GET", uri, nil) if err != nil { return nil, err } req.Header.Set("Authorization", "Bearer "+a.Token) client := &http.Client{Timeout: time.Second * 5000} res, err := client.Do(req) if err != nil { return nil, err } defer res.Body.Close() if res.StatusCode != 200 { return nil, errors.New("nest: wrong status: " + res.Status) } var resv struct { Devices []Device } if err = json.NewDecoder(res.Body).Decode(&resv); err != nil { return nil, err } devices := make([]DeviceInfo, 0, len(resv.Devices)) for _, device := range resv.Devices { // only RTSP and WEB_RTC available (both supported) if len(device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols) == 0 { continue } i := strings.LastIndexByte(device.Name, '/') if i <= 0 { continue } name := device.Traits.SdmDevicesTraitsInfo.CustomName // Devices configured through the Nest app use the container/room name as opposed to the customName trait if name == "" && len(device.ParentRelations) > 0 { name = device.ParentRelations[0].DisplayName } devices = append(devices, DeviceInfo{ Name: name, DeviceID: device.Name[i+1:], Protocols: device.Traits.SdmDevicesTraitsCameraLiveStream.SupportedProtocols, }) } return devices, nil } func (a *API) ExchangeSDP(projectID, deviceID, offer string) (string, error) { var reqv struct { Command string `json:"command"` Params struct { Offer string `json:"offerSdp"` } `json:"params"` } reqv.Command = "sdm.devices.commands.CameraLiveStream.GenerateWebRtcStream" reqv.Params.Offer = offer b, err := json.Marshal(reqv) if err != nil { return "", err } uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" + projectID + "/devices/" + deviceID + ":executeCommand" maxRetries := 3 retryDelay := time.Second * 30 for attempt := 0; attempt < maxRetries; attempt++ { req, err := http.NewRequest("POST", uri, bytes.NewReader(b)) if err != nil { return "", err } req.Header.Set("Authorization", "Bearer "+a.Token) client := &http.Client{Timeout: time.Second * 5000} res, err := client.Do(req) if err != nil { return "", err } // Handle 409 (Conflict), 429 (Too Many Requests), and 401 (Unauthorized) if res.StatusCode == 409 || res.StatusCode == 429 || res.StatusCode == 401 { res.Body.Close() if attempt < maxRetries-1 { // Get new token from Google if err := a.refreshToken(); err != nil { return "", err } time.Sleep(retryDelay) retryDelay *= 2 // exponential backoff continue } } defer res.Body.Close() if res.StatusCode != 200 { return "", errors.New("nest: wrong status: " + res.Status) } var resv struct { Results struct { Answer string `json:"answerSdp"` ExpiresAt time.Time `json:"expiresAt"` MediaSessionID string `json:"mediaSessionId"` } `json:"results"` } if err = json.NewDecoder(res.Body).Decode(&resv); err != nil { return "", err } a.StreamProjectID = projectID a.StreamDeviceID = deviceID a.StreamSessionID = resv.Results.MediaSessionID a.StreamExpiresAt = resv.Results.ExpiresAt return resv.Results.Answer, nil } return "", errors.New("nest: max retries exceeded") } func (a *API) refreshToken() error { // Get the cached API with matching token to get credentials var refreshKey string cacheMu.Lock() for key, api := range cache { if api.Token == a.Token { refreshKey = key break } } cacheMu.Unlock() if refreshKey == "" { return errors.New("nest: unable to find cached credentials") } // Parse credentials from cache key parts := strings.Split(refreshKey, ":") if len(parts) != 3 { return errors.New("nest: invalid cache key format") } clientID, clientSecret, refreshToken := parts[0], parts[1], parts[2] // Get new API instance which will refresh the token newAPI, err := NewAPI(clientID, clientSecret, refreshToken) if err != nil { return err } // Update current API with new token a.Token = newAPI.Token a.ExpiresAt = newAPI.ExpiresAt return nil } func (a *API) ExtendStream() error { var reqv struct { Command string `json:"command"` Params struct { MediaSessionID string `json:"mediaSessionId,omitempty"` StreamExtensionToken string `json:"streamExtensionToken,omitempty"` } `json:"params"` } if a.StreamToken != "" { // RTSP reqv.Command = "sdm.devices.commands.CameraLiveStream.ExtendRtspStream" reqv.Params.StreamExtensionToken = a.StreamExtensionToken } else { // WebRTC reqv.Command = "sdm.devices.commands.CameraLiveStream.ExtendWebRtcStream" reqv.Params.MediaSessionID = a.StreamSessionID } b, err := json.Marshal(reqv) if err != nil { return err } uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" + a.StreamProjectID + "/devices/" + a.StreamDeviceID + ":executeCommand" req, err := http.NewRequest("POST", uri, bytes.NewReader(b)) if err != nil { return err } req.Header.Set("Authorization", "Bearer "+a.Token) client := &http.Client{Timeout: time.Second * 5000} res, err := client.Do(req) if err != nil { return err } defer res.Body.Close() if res.StatusCode != 200 { return errors.New("nest: wrong status: " + res.Status) } var resv struct { Results struct { ExpiresAt time.Time `json:"expiresAt"` MediaSessionID string `json:"mediaSessionId"` StreamExtensionToken string `json:"streamExtensionToken"` StreamToken string `json:"streamToken"` } `json:"results"` } if err = json.NewDecoder(res.Body).Decode(&resv); err != nil { return err } a.StreamSessionID = resv.Results.MediaSessionID a.StreamExpiresAt = resv.Results.ExpiresAt a.StreamExtensionToken = resv.Results.StreamExtensionToken a.StreamToken = resv.Results.StreamToken return nil } func (a *API) GenerateRtspStream(projectID, deviceID string) (string, error) { var reqv struct { Command string `json:"command"` Params struct{} `json:"params"` } reqv.Command = "sdm.devices.commands.CameraLiveStream.GenerateRtspStream" b, err := json.Marshal(reqv) if err != nil { return "", err } uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" + projectID + "/devices/" + deviceID + ":executeCommand" req, err := http.NewRequest("POST", uri, bytes.NewReader(b)) if err != nil { return "", err } req.Header.Set("Authorization", "Bearer "+a.Token) client := &http.Client{Timeout: time.Second * 5000} res, err := client.Do(req) if err != nil { return "", err } if res.StatusCode != 200 { return "", errors.New("nest: wrong status: " + res.Status) } var resv struct { Results struct { StreamURLs map[string]string `json:"streamUrls"` StreamExtensionToken string `json:"streamExtensionToken"` StreamToken string `json:"streamToken"` ExpiresAt time.Time `json:"expiresAt"` } `json:"results"` } if err = json.NewDecoder(res.Body).Decode(&resv); err != nil { return "", err } if _, ok := resv.Results.StreamURLs["rtspUrl"]; !ok { return "", errors.New("nest: failed to generate rtsp url") } a.StreamProjectID = projectID a.StreamDeviceID = deviceID a.StreamToken = resv.Results.StreamToken a.StreamExtensionToken = resv.Results.StreamExtensionToken a.StreamExpiresAt = resv.Results.ExpiresAt return resv.Results.StreamURLs["rtspUrl"], nil } func (a *API) StopRTSPStream() error { if a.StreamProjectID == "" || a.StreamDeviceID == "" { return errors.New("nest: tried to stop rtsp stream without a project or device ID") } var reqv struct { Command string `json:"command"` Params struct { StreamExtensionToken string `json:"streamExtensionToken"` } `json:"params"` } reqv.Command = "sdm.devices.commands.CameraLiveStream.StopRtspStream" reqv.Params.StreamExtensionToken = a.StreamExtensionToken b, err := json.Marshal(reqv) if err != nil { return err } uri := "https://smartdevicemanagement.googleapis.com/v1/enterprises/" + a.StreamProjectID + "/devices/" + a.StreamDeviceID + ":executeCommand" req, err := http.NewRequest("POST", uri, bytes.NewReader(b)) if err != nil { return err } req.Header.Set("Authorization", "Bearer "+a.Token) client := &http.Client{Timeout: time.Second * 5000} res, err := client.Do(req) if err != nil { return err } if res.StatusCode != 200 { return errors.New("nest: wrong status: " + res.Status) } a.StreamProjectID = "" a.StreamDeviceID = "" a.StreamExtensionToken = "" a.StreamToken = "" return nil } type Device struct { Name string `json:"name"` Type string `json:"type"` //Assignee string `json:"assignee"` Traits struct { SdmDevicesTraitsInfo struct { CustomName string `json:"customName"` } `json:"sdm.devices.traits.Info"` SdmDevicesTraitsCameraLiveStream struct { VideoCodecs []string `json:"videoCodecs"` AudioCodecs []string `json:"audioCodecs"` SupportedProtocols []string `json:"supportedProtocols"` } `json:"sdm.devices.traits.CameraLiveStream"` //SdmDevicesTraitsCameraImage struct { // MaxImageResolution struct { // Width int `json:"width"` // Height int `json:"height"` // } `json:"maxImageResolution"` //} `json:"sdm.devices.traits.CameraImage"` //SdmDevicesTraitsCameraPerson struct { //} `json:"sdm.devices.traits.CameraPerson"` //SdmDevicesTraitsCameraMotion struct { //} `json:"sdm.devices.traits.CameraMotion"` //SdmDevicesTraitsDoorbellChime struct { //} `json:"sdm.devices.traits.DoorbellChime"` //SdmDevicesTraitsCameraClipPreview struct { //} `json:"sdm.devices.traits.CameraClipPreview"` } `json:"traits"` ParentRelations []struct { Parent string `json:"parent"` DisplayName string `json:"displayName"` } `json:"parentRelations"` } func (a *API) StartExtendStreamTimer() { if a.extendTimer != nil { return } a.extendTimer = time.NewTimer(time.Until(a.StreamExpiresAt) - time.Minute) go func() { <-a.extendTimer.C if err := a.ExtendStream(); err != nil { return } }() } func (a *API) StopExtendStreamTimer() { if a.extendTimer != nil { a.extendTimer.Stop() a.extendTimer = nil } } ================================================ FILE: pkg/nest/client.go ================================================ package nest import ( "errors" "net/url" "strings" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/rtsp" "github.com/AlexxIT/go2rtc/pkg/webrtc" pion "github.com/pion/webrtc/v4" ) type WebRTCClient struct { conn *webrtc.Conn api *API } type RTSPClient struct { conn *rtsp.Conn api *API } func Dial(rawURL string) (core.Producer, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } query := u.Query() cliendID := query.Get("client_id") cliendSecret := query.Get("client_secret") refreshToken := query.Get("refresh_token") projectID := query.Get("project_id") deviceID := query.Get("device_id") if cliendID == "" || cliendSecret == "" || refreshToken == "" || projectID == "" || deviceID == "" { return nil, errors.New("nest: wrong query") } maxRetries := 3 retryDelay := time.Second * 30 var nestAPI *API var lastErr error for attempt := 0; attempt < maxRetries; attempt++ { nestAPI, err = NewAPI(cliendID, cliendSecret, refreshToken) if err == nil { break } lastErr = err if attempt < maxRetries-1 { time.Sleep(retryDelay) retryDelay *= 2 // exponential backoff } } if nestAPI == nil { return nil, lastErr } protocols := strings.Split(query.Get("protocols"), ",") if len(protocols) > 0 && protocols[0] == "RTSP" { return rtspConn(nestAPI, rawURL, projectID, deviceID) } // Default to WEB_RTC for backwards compataiility return rtcConn(nestAPI, rawURL, projectID, deviceID) } func (c *WebRTCClient) GetMedias() []*core.Media { return c.conn.GetMedias() } func (c *WebRTCClient) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return c.conn.GetTrack(media, codec) } func (c *WebRTCClient) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { return c.conn.AddTrack(media, codec, track) } func (c *WebRTCClient) Start() error { c.api.StartExtendStreamTimer() return c.conn.Start() } func (c *WebRTCClient) Stop() error { c.api.StopExtendStreamTimer() return c.conn.Stop() } func (c *WebRTCClient) MarshalJSON() ([]byte, error) { return c.conn.MarshalJSON() } func rtcConn(nestAPI *API, rawURL, projectID, deviceID string) (*WebRTCClient, error) { maxRetries := 3 retryDelay := time.Second * 30 var lastErr error for attempt := 0; attempt < maxRetries; attempt++ { rtcAPI, err := webrtc.NewAPI() if err != nil { return nil, err } conf := pion.Configuration{} pc, err := rtcAPI.NewPeerConnection(conf) if err != nil { return nil, err } conn := webrtc.NewConn(pc) conn.FormatName = "nest/webrtc" conn.Mode = core.ModeActiveProducer conn.Protocol = "http" conn.URL = rawURL // https://developers.google.com/nest/device-access/traits/device/camera-live-stream#generatewebrtcstream-request-fields medias := []*core.Media{ {Kind: core.KindAudio, Direction: core.DirectionRecvonly}, {Kind: core.KindVideo, Direction: core.DirectionRecvonly}, {Kind: "app"}, // important for Nest } // 3. Create offer with candidates offer, err := conn.CreateCompleteOffer(medias) if err != nil { return nil, err } // 4. Exchange SDP via Hass answer, err := nestAPI.ExchangeSDP(projectID, deviceID, offer) if err != nil { lastErr = err if attempt < maxRetries-1 { time.Sleep(retryDelay) retryDelay *= 2 continue } return nil, err } // 5. Set answer with remote medias if err = conn.SetAnswer(answer); err != nil { return nil, err } return &WebRTCClient{conn: conn, api: nestAPI}, nil } return nil, lastErr } func rtspConn(nestAPI *API, rawURL, projectID, deviceID string) (*RTSPClient, error) { rtspURL, err := nestAPI.GenerateRtspStream(projectID, deviceID) if err != nil { return nil, err } rtspClient := rtsp.NewClient(rtspURL) if err := rtspClient.Dial(); err != nil { return nil, err } if err := rtspClient.Describe(); err != nil { return nil, err } return &RTSPClient{conn: rtspClient, api: nestAPI}, nil } func (c *RTSPClient) GetMedias() []*core.Media { result := c.conn.GetMedias() return result } func (c *RTSPClient) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return c.conn.GetTrack(media, codec) } func (c *RTSPClient) Start() error { c.api.StartExtendStreamTimer() return c.conn.Start() } func (c *RTSPClient) Stop() error { c.api.StopRTSPStream() c.api.StopExtendStreamTimer() return c.conn.Stop() } func (c *RTSPClient) MarshalJSON() ([]byte, error) { return c.conn.MarshalJSON() } ================================================ FILE: pkg/ngrok/ngrok.go ================================================ package ngrok import ( "bufio" "encoding/json" "io" "os/exec" "strings" "github.com/AlexxIT/go2rtc/pkg/core" ) type Ngrok struct { core.Listener Tunnels map[string]string reader *bufio.Reader } type Message struct { Msg string `json:"msg"` Addr string `json:"addr"` URL string `json:"url"` Line string } func NewNgrok(command any) (*Ngrok, error) { var arg []string switch command.(type) { case string: arg = strings.Split(command.(string), " ") case []string: arg = command.([]string) } arg = append(arg, "--log", "stdout", "--log-format", "json") cmd := exec.Command(arg[0], arg[1:]...) r, err := cmd.StdoutPipe() if err != nil { return nil, err } cmd.Stderr = cmd.Stdout n := &Ngrok{ Tunnels: map[string]string{}, reader: bufio.NewReader(r), } if err = cmd.Start(); err != nil { return nil, err } return n, nil } func (n *Ngrok) Serve() error { for { line, _, err := n.reader.ReadLine() if err != nil { if err != io.EOF { return err } return nil } msg := new(Message) _ = json.Unmarshal(line, msg) if msg.Msg == "started tunnel" { n.Tunnels[msg.Addr] = msg.URL } msg.Line = string(line) n.Fire(msg) } } ================================================ FILE: pkg/onvif/README.md ================================================ ## Profiles - Profile A - For access control configuration - Profile C - For door control and event management - Profile S - For basic video streaming - Video streaming and configuration - Profile T - For advanced video streaming - H.264 / H.265 video compression - Imaging settings - Motion alarm and tampering events - Metadata streaming - Bi-directional audio ## Services https://www.onvif.org/profiles/specifications/ - https://www.onvif.org/ver10/device/wsdl/devicemgmt.wsdl - https://www.onvif.org/ver20/imaging/wsdl/imaging.wsdl - https://www.onvif.org/ver10/media/wsdl/media.wsdl ## TMP | | Dahua | Reolink | TP-Link | |------------------------|---------|---------|---------| | GetCapabilities | no auth | no auth | no auth | | GetServices | no auth | no auth | no auth | | GetServiceCapabilities | no auth | no auth | auth | | GetSystemDateAndTime | no auth | no auth | no auth | | GetNetworkInterfaces | auth | auth | auth | | GetDeviceInformation | auth | auth | auth | | GetProfiles | auth | auth | auth | | GetScopes | auth | auth | auth | - Dahua - onvif://192.168.10.90:80 - Reolink - onvif://192.168.10.92:8000 - TP-Link - onvif://192.168.10.91:2020/onvif/device_service - ================================================ FILE: pkg/onvif/client.go ================================================ package onvif import ( "bytes" "errors" "html" "io" "net/http" "net/url" "regexp" "strings" "time" ) const PathDevice = "/onvif/device_service" type Client struct { url *url.URL deviceURL string mediaURL string imaginURL string } func NewClient(rawURL string) (*Client, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } baseURL := "http://" + u.Host client := &Client{url: u} client.deviceURL = baseURL + GetPath(u.Path, PathDevice) b, err := client.DeviceRequest(DeviceGetCapabilities) if err != nil { return nil, err } s := FindTagValue(b, "Media.+?XAddr") client.mediaURL = baseURL + GetPath(s, "/onvif/media_service") s = FindTagValue(b, "Imaging.+?XAddr") client.imaginURL = baseURL + GetPath(s, "/onvif/imaging_service") return client, nil } func (c *Client) GetURI() (string, error) { query := c.url.Query() token := query.Get("subtype") // support empty if i := atoi(token); i >= 0 { tokens, err := c.GetProfilesTokens() if err != nil { return "", err } if i >= len(tokens) { return "", errors.New("onvif: wrong subtype") } token = tokens[i] } getUri := c.GetStreamUri if query.Has("snapshot") { getUri = c.GetSnapshotUri } b, err := getUri(token) if err != nil { return "", err } rawURL := FindTagValue(b, "Uri") rawURL = strings.TrimSpace(html.UnescapeString(rawURL)) u, err := url.Parse(rawURL) if err != nil { return "", err } if u.User == nil && c.url.User != nil { u.User = c.url.User } return u.String(), nil } func (c *Client) GetName() (string, error) { b, err := c.DeviceRequest(DeviceGetDeviceInformation) if err != nil { return "", err } return FindTagValue(b, "Manufacturer") + " " + FindTagValue(b, "Model"), nil } func (c *Client) GetProfilesTokens() ([]string, error) { b, err := c.MediaRequest(MediaGetProfiles) if err != nil { return nil, err } var tokens []string re := regexp.MustCompile(`Profiles.+?token="([^"]+)`) for _, s := range re.FindAllStringSubmatch(string(b), 10) { tokens = append(tokens, s[1]) } return tokens, nil } func (c *Client) HasSnapshots() bool { b, err := c.GetServiceCapabilities() if err != nil { return false } return strings.Contains(string(b), `SnapshotUri="true"`) } func (c *Client) GetProfile(token string) ([]byte, error) { return c.Request( c.mediaURL, ``+token+``, ) } func (c *Client) GetVideoSourceConfiguration(token string) ([]byte, error) { return c.Request(c.mediaURL, ` `+token+` `) } func (c *Client) GetStreamUri(token string) ([]byte, error) { return c.Request(c.mediaURL, ` RTP-Unicast RTSP `+token+` `) } func (c *Client) GetSnapshotUri(token string) ([]byte, error) { return c.Request( c.imaginURL, ``+token+``, ) } func (c *Client) GetServiceCapabilities() ([]byte, error) { // some cameras answer GetServiceCapabilities for media only for path = "/onvif/media" return c.Request( c.mediaURL, ``, ) } func (c *Client) DeviceRequest(operation string) ([]byte, error) { switch operation { case DeviceGetServices: operation = `true` case DeviceGetCapabilities: operation = `All` default: operation = `` } return c.Request(c.deviceURL, operation) } func (c *Client) MediaRequest(operation string) ([]byte, error) { operation = `` return c.Request(c.mediaURL, operation) } func (c *Client) Request(url, body string) ([]byte, error) { if url == "" { return nil, errors.New("onvif: unsupported service") } e := NewEnvelopeWithUser(c.url.User) e.Append(body) client := &http.Client{Timeout: time.Second * 5000} res, err := client.Post(url, `application/soap+xml;charset=utf-8`, bytes.NewReader(e.Bytes())) if err != nil { return nil, err } defer res.Body.Close() if res.StatusCode != http.StatusOK { return nil, errors.New("onvif: wrong response " + res.Status) } return io.ReadAll(res.Body) } ================================================ FILE: pkg/onvif/envelope.go ================================================ package onvif import ( "crypto/sha1" "encoding/base64" "fmt" "net/url" "time" "github.com/AlexxIT/go2rtc/pkg/core" ) type Envelope struct { buf []byte } const ( prefix1 = `` prefix2 = `` suffix = `` ) func NewEnvelope() *Envelope { e := &Envelope{buf: make([]byte, 0, 1024)} e.Append(prefix1, prefix2) return e } func NewEnvelopeWithUser(user *url.Userinfo) *Envelope { if user == nil { return NewEnvelope() } nonce := core.RandString(16, 36) created := time.Now().UTC().Format(time.RFC3339Nano) pass, _ := user.Password() h := sha1.New() h.Write([]byte(nonce + created + pass)) e := &Envelope{buf: make([]byte, 0, 1024)} e.Append(prefix1) e.Appendf(` %s %s %s %s `, user.Username(), base64.StdEncoding.EncodeToString(h.Sum(nil)), base64.StdEncoding.EncodeToString([]byte(nonce)), created) e.Append(prefix2) return e } func (e *Envelope) Append(args ...string) { for _, s := range args { e.buf = append(e.buf, s...) } } func (e *Envelope) Appendf(format string, args ...any) { e.buf = fmt.Appendf(e.buf, format, args...) } func (e *Envelope) Bytes() []byte { return append(e.buf, suffix...) } ================================================ FILE: pkg/onvif/helpers.go ================================================ package onvif import ( "fmt" "net" "net/url" "regexp" "strconv" "strings" "time" "github.com/AlexxIT/go2rtc/pkg/core" ) type DiscoveryDevice struct { URL string Name string Hardware string } func FindTagValue(b []byte, tag string) string { re := regexp.MustCompile(`(?s)<(?:\w+:)?` + tag + `\b[^>]*>([^<]+)`) m := re.FindSubmatch(b) if len(m) != 2 { return "" } return string(m[1]) } // UUID - generate something like 44302cbf-0d18-4feb-79b3-33b575263da3 func UUID() string { s := core.RandString(32, 16) return s[:8] + "-" + s[8:12] + "-" + s[12:16] + "-" + s[16:20] + "-" + s[20:] } // DiscoveryStreamingDevices return list of tuple (onvif_url, name, hardware) func DiscoveryStreamingDevices() ([]DiscoveryDevice, error) { conn, err := net.ListenUDP("udp4", nil) if err != nil { return nil, err } defer conn.Close() // https://www.onvif.org/wp-content/uploads/2016/12/ONVIF_Feature_Discovery_Specification_16.07.pdf // 5.3 Discovery Procedure: msg := ` http://schemas.xmlsoap.org/ws/2005/04/discovery/Probe urn:uuid:` + UUID() + ` urn:schemas-xmlsoap-org:ws:2005:04:discovery ` addr := &net.UDPAddr{ IP: net.IP{239, 255, 255, 250}, Port: 3702, } if _, err = conn.WriteTo([]byte(msg), addr); err != nil { return nil, err } _ = conn.SetReadDeadline(time.Now().Add(5 * time.Second)) var devices []DiscoveryDevice b := make([]byte, 8192) for { n, addr, err := conn.ReadFromUDP(b) if err != nil { break } //log.Printf("[onvif] discovery response addr=%s:\n%s", addr, b[:n]) // ignore printers, etc if !strings.Contains(string(b[:n]), "onvif") { continue } device := DiscoveryDevice{ URL: FindTagValue(b[:n], "XAddrs"), } if device.URL == "" { continue } // fix some buggy cameras // http://0.0.0.0:8080/onvif/device_service if s, ok := strings.CutPrefix(device.URL, "http://0.0.0.0"); ok { device.URL = "http://" + addr.IP.String() + s } // try to find the camera name and model (hardware) scopes := FindTagValue(b[:n], "Scopes") device.Name = findScope(scopes, "onvif://www.onvif.org/name/") device.Hardware = findScope(scopes, "onvif://www.onvif.org/hardware/") devices = append(devices, device) } return devices, nil } func findScope(s, prefix string) string { s = core.Between(s, prefix, " ") s, _ = url.QueryUnescape(s) return s } func atoi(s string) int { if s == "" { return 0 } i, err := strconv.Atoi(s) if err != nil { return -1 } return i } func GetPosixTZ(current time.Time) string { // Thanks to https://github.com/Path-Variable/go-posix-time _, offset := current.Zone() if current.IsDST() { _, end := current.ZoneBounds() endPlus1 := end.Add(time.Hour * 25) _, offset = endPlus1.Zone() } var prefix string if offset < 0 { prefix = "GMT+" offset = -offset / 60 } else { prefix = "GMT-" offset = offset / 60 } return prefix + fmt.Sprintf("%02d:%02d", offset/60, offset%60) } func GetPath(urlOrPath, defPath string) string { if urlOrPath == "" || urlOrPath[0] == '/' { return defPath } u, err := url.Parse(urlOrPath) if err != nil { return defPath } return GetPath(u.Path, defPath) } ================================================ FILE: pkg/onvif/onvif_test.go ================================================ package onvif import ( "html" "net/url" "strings" "testing" "github.com/stretchr/testify/require" ) func TestGetStreamUri(t *testing.T) { tests := []struct { name string xml string url string }{ { name: "Dahua stream default", xml: `rtsp://192.168.1.123:554/cam/realmonitor?channel=1&subtype=1&unicast=true&proto=OnviftruetruePT0S`, url: "rtsp://192.168.1.123:554/cam/realmonitor?channel=1&subtype=1&unicast=true&proto=Onvif", }, { name: "Dahua snapshot default", xml: `http://192.168.1.123/onvifsnapshot/media_service/snapshot?channel=1&subtype=1falsefalsePT0S`, url: "http://192.168.1.123/onvifsnapshot/media_service/snapshot?channel=1&subtype=1", }, { name: "Dahua stream formatted", xml: ` rtsp://192.168.1.123:554/cam/realmonitor?channel=1&subtype=1&unicast=true&proto=Onvif true true PT0S `, url: "rtsp://192.168.1.123:554/cam/realmonitor?channel=1&subtype=1&unicast=true&proto=Onvif", }, { name: "Dahua snapshot formatted", xml: ` http://192.168.1.123/onvifsnapshot/media_service/snapshot?channel=1&subtype=1 false false PT0S `, url: "http://192.168.1.123/onvifsnapshot/media_service/snapshot?channel=1&subtype=1", }, { name: "Unknown", xml: ` rtsp://192.168.5.53:8090/profile1=r `, url: "rtsp://192.168.5.53:8090/profile1=r", }, { name: "go2rtc 1.9.4", xml: ` rtsp://192.168.1.123:8554/rtsp-dahua1 `, url: "rtsp://192.168.1.123:8554/rtsp-dahua1", }, { name: "go2rtc 1.9.8", xml: ` rtsp://192.168.1.123:8554/rtsp-dahua2 `, url: "rtsp://192.168.1.123:8554/rtsp-dahua2", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { uri := FindTagValue([]byte(test.xml), "Uri") uri = strings.TrimSpace(html.UnescapeString(uri)) u, err := url.Parse(uri) require.Nil(t, err) require.Equal(t, test.url, u.String()) }) } } func TestGetCapabilities(t *testing.T) { tests := []struct { name string xml string }{ { name: "Dahua default", xml: `http://192.168.1.123/onvif/analytics_servicetruetruehttp://192.168.1.123/onvif/device_servicefalsefalsefalsefalsefalsefalsetruefalsefalsetruetrue200210220230240242161218061812190619122006truefalsefalsefalse21falsefalsefalsefalsefalsefalsefalsefalsefalsefalsefalse0falsehttp://192.168.1.123/onvif/event_servicetruetruefalsehttp://192.168.1.123/onvif/imaging_servicehttp://192.168.1.123/onvif/media_servicetruetruetrue6http://192.168.1.123/onvif/deviceIO_service10111`, }, { name: "Dahua formatted", xml: ` http://192.168.1.123/onvif/analytics_service true true http://192.168.1.123/onvif/device_service false false false false false ... 2 1 false ... http://192.168.1.123/onvif/event_service true true false http://192.168.1.123/onvif/imaging_service http://192.168.1.123/onvif/media_service true true true 6 http://192.168.1.123/onvif/deviceIO_service 1 0 1 1 1 `, }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { rawURL := FindTagValue([]byte(test.xml), "Media.+?XAddr") require.Equal(t, "http://192.168.1.123/onvif/media_service", rawURL) rawURL = FindTagValue([]byte(test.xml), "Imaging.+?XAddr") require.Equal(t, "http://192.168.1.123/onvif/imaging_service", rawURL) }) } } ================================================ FILE: pkg/onvif/server.go ================================================ package onvif import ( "bytes" "regexp" "time" ) const ServiceGetServiceCapabilities = "GetServiceCapabilities" const ( DeviceGetCapabilities = "GetCapabilities" DeviceGetDeviceInformation = "GetDeviceInformation" DeviceGetDiscoveryMode = "GetDiscoveryMode" DeviceGetDNS = "GetDNS" DeviceGetHostname = "GetHostname" DeviceGetNetworkDefaultGateway = "GetNetworkDefaultGateway" DeviceGetNetworkInterfaces = "GetNetworkInterfaces" DeviceGetNetworkProtocols = "GetNetworkProtocols" DeviceGetNTP = "GetNTP" DeviceGetScopes = "GetScopes" DeviceGetServices = "GetServices" DeviceGetSystemDateAndTime = "GetSystemDateAndTime" DeviceSetSystemDateAndTime = "SetSystemDateAndTime" DeviceSystemReboot = "SystemReboot" ) const ( MediaGetAudioEncoderConfigurations = "GetAudioEncoderConfigurations" MediaGetAudioSources = "GetAudioSources" MediaGetAudioSourceConfigurations = "GetAudioSourceConfigurations" MediaGetProfile = "GetProfile" MediaGetProfiles = "GetProfiles" MediaGetSnapshotUri = "GetSnapshotUri" MediaGetStreamUri = "GetStreamUri" MediaGetVideoEncoderConfiguration = "GetVideoEncoderConfiguration" MediaGetVideoEncoderConfigurations = "GetVideoEncoderConfigurations" MediaGetVideoEncoderConfigurationOptions = "GetVideoEncoderConfigurationOptions" MediaGetVideoSources = "GetVideoSources" MediaGetVideoSourceConfiguration = "GetVideoSourceConfiguration" MediaGetVideoSourceConfigurations = "GetVideoSourceConfigurations" ) func GetRequestAction(b []byte) string { // // re := regexp.MustCompile(`Body[^<]+<([^ />]+)`) m := re.FindSubmatch(b) if len(m) != 2 { return "" } if i := bytes.IndexByte(m[1], ':'); i > 0 { return string(m[1][i+1:]) } return string(m[1]) } func GetCapabilitiesResponse(host string) []byte { e := NewEnvelope() e.Appendf(` http://%s/onvif/device_service http://%s/onvif/media_service false false true `, host, host) return e.Bytes() } func GetServicesResponse(host string) []byte { e := NewEnvelope() e.Appendf(` http://www.onvif.org/ver10/device/wsdl http://%s/onvif/device_service 25 http://www.onvif.org/ver10/media/wsdl http://%s/onvif/media_service 25 `, host, host) return e.Bytes() } func GetSystemDateAndTimeResponse() []byte { loc := time.Now() utc := loc.UTC() e := NewEnvelope() e.Appendf(` NTP true %s %d%d%d %d%d%d %d%d%d %d%d%d `, GetPosixTZ(loc), utc.Hour(), utc.Minute(), utc.Second(), utc.Year(), utc.Month(), utc.Day(), loc.Hour(), loc.Minute(), loc.Second(), loc.Year(), loc.Month(), loc.Day(), ) return e.Bytes() } func GetDeviceInformationResponse(manuf, model, firmware, serial string) []byte { e := NewEnvelope() e.Appendf(` %s %s %s %s 1.00 `, manuf, model, firmware, serial) return e.Bytes() } func GetProfilesResponse(names []string) []byte { e := NewEnvelope() e.Append(``) for _, name := range names { appendProfile(e, "Profiles", name) } e.Append(``) return e.Bytes() } func GetProfileResponse(name string) []byte { e := NewEnvelope() e.Append(``) appendProfile(e, "Profile", name) e.Append(``) return e.Bytes() } func appendProfile(e *Envelope, tag, name string) { // go2rtc name = ONVIF Profile Name = ONVIF Profile token e.Appendf(``, tag, name) e.Appendf(`%s`, name) appendVideoSourceConfiguration(e, "VideoSourceConfiguration", name) appendVideoEncoderConfiguration(e, "VideoEncoderConfiguration") e.Appendf(``, tag) } func GetVideoSourcesResponse(names []string) []byte { // go2rtc name = ONVIF VideoSource token e := NewEnvelope() e.Append(``) for _, name := range names { e.Appendf(` 30.000000 19201080 `, name) } e.Append(``) return e.Bytes() } func GetVideoSourceConfigurationsResponse(names []string) []byte { e := NewEnvelope() e.Append(``) for _, name := range names { appendVideoSourceConfiguration(e, "Configurations", name) } e.Append(``) return e.Bytes() } func GetVideoSourceConfigurationResponse(name string) []byte { e := NewEnvelope() e.Append(``) appendVideoSourceConfiguration(e, "Configuration", name) e.Append(``) return e.Bytes() } func appendVideoSourceConfiguration(e *Envelope, tag, name string) { // go2rtc name = ONVIF VideoSourceConfiguration token e.Appendf(` VSC %s `, tag, name, name, tag) } func GetVideoEncoderConfigurationsResponse() []byte { e := NewEnvelope() e.Append(``) appendVideoEncoderConfiguration(e, "VideoEncoderConfigurations") e.Append(``) return e.Bytes() } func GetVideoEncoderConfigurationResponse() []byte { e := NewEnvelope() e.Append(``) appendVideoEncoderConfiguration(e, "VideoEncoderConfiguration") e.Append(``) return e.Bytes() } func appendVideoEncoderConfiguration(e *Envelope, tag string) { // empty `RateControl` important for UniFi Protect e.Appendf(` VEC 1 H264 19201080 0 3018192 10Main PT10S `, tag, tag) } func GetStreamUriResponse(uri string) []byte { e := NewEnvelope() e.Appendf(`%s`, uri) return e.Bytes() } func GetSnapshotUriResponse(uri string) []byte { e := NewEnvelope() e.Appendf(`%s`, uri) return e.Bytes() } func StaticResponse(operation string) []byte { switch operation { case DeviceGetSystemDateAndTime: return GetSystemDateAndTimeResponse() case MediaGetVideoEncoderConfiguration: return GetVideoEncoderConfigurationResponse() case MediaGetVideoEncoderConfigurations: return GetVideoEncoderConfigurationsResponse() } e := NewEnvelope() e.Append(responses[operation]) return e.Bytes() } var responses = map[string]string{ ServiceGetServiceCapabilities: ` `, DeviceGetDiscoveryMode: `Discoverable`, DeviceGetDNS: ``, DeviceGetHostname: ``, DeviceGetNetworkDefaultGateway: ``, DeviceGetNTP: ``, DeviceSetSystemDateAndTime: ``, DeviceSystemReboot: `OK`, DeviceGetNetworkInterfaces: ``, DeviceGetNetworkProtocols: ``, DeviceGetScopes: ` Fixedonvif://www.onvif.org/name/go2rtc Fixedonvif://www.onvif.org/location/github Fixedonvif://www.onvif.org/Profile/Streaming Fixedonvif://www.onvif.org/type/Network_Video_Transmitter `, MediaGetAudioEncoderConfigurations: ``, MediaGetAudioSources: ``, MediaGetAudioSourceConfigurations: ``, MediaGetVideoEncoderConfigurationOptions: ` 16 19201080 0100 130 1100 Main `, } ================================================ FILE: pkg/opus/README.md ================================================ ## Useful links - [RFC 3550: RTP: A Transport Protocol for Real-Time Applications](https://datatracker.ietf.org/doc/html/rfc3550) - [RFC 6716: Definition of the Opus Audio Codec](https://datatracker.ietf.org/doc/html/rfc6716) - [RFC 7587: RTP Payload Format for the Opus Speech and Audio Codec](https://datatracker.ietf.org/doc/html/rfc7587) ================================================ FILE: pkg/opus/homekit.go ================================================ package opus import ( "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) // Some info about this magic: // - Apple has no respect for RFC 7587 standard and using RFC 3550 for RTP timestamps // - Apple can request packets with 20ms duration over LAN connection and 60ms over LTE // - FFmpeg produce packets with 20ms duration by default and only one frame per packet // - FFmpeg should use "-min_comp 0" option, so every packet will be same duration // - Apple doesn't care about real sample rate of track // - Apple only cares about proper timestamp based on REQUESTED sample rate // RepackToHAP - convert standart RTP packet with OPUS to HAP packet // We expect that: // - incoming packet will be 20ms duration and only one frame per packet // - outgouing packet will be 20ms or 60ms duration // - incoming sample rate will be any (but not very big if we needs 60ms packets for output) // - outgouing sample rate will be 16000 // https://github.com/AlexxIT/go2rtc/issues/667 func RepackToHAP(rtpTime byte, handler core.HandlerFunc) core.HandlerFunc { switch rtpTime { case 20: return repackToHAP20(handler) case 60: return repackToHAP60(handler) } return handler } // we using only one sample rate in the pkg/hap/camera/accessory.go const ( timestamp20 = 16000 * 0.020 timestamp60 = 16000 * 0.060 ) // repackToHAP20 - just fix RTP timestamp from RFC 7587 to RFC 3550 func repackToHAP20(handler core.HandlerFunc) core.HandlerFunc { var timestamp uint32 return func(pkt *rtp.Packet) { timestamp += timestamp20 clone := *pkt clone.Timestamp = timestamp handler(&clone) } } // repackToHAP60 - collect 20ms frames to single 60ms packet // thanks to @civita idea https://github.com/AlexxIT/go2rtc/pull/843 func repackToHAP60(handler core.HandlerFunc) core.HandlerFunc { var sequence uint16 var timestamp uint32 var framesCount byte var framesSize []byte var framesData []byte return func(pkt *rtp.Packet) { framesData = append(framesData, pkt.Payload[1:]...) if framesCount++; framesCount < 3 { if frameSize := len(pkt.Payload) - 1; frameSize >= 252 { b0 := 252 + byte(frameSize)&0b11 framesSize = append(framesSize, b0, byte(frameSize/4)-b0) } else { framesSize = append(framesSize, byte(frameSize)) } return } toc := pkt.Payload[0] payload := make([]byte, 2, 2+len(framesSize)+len(framesData)) payload[0] = toc | 0b11 // code 3 (multiple frames per packet) payload[1] = 0b1000_0011 // VBR, no padding, 3 frames payload = append(payload, framesSize...) payload = append(payload, framesData...) sequence++ timestamp += timestamp60 clone := *pkt clone.Payload = payload clone.SequenceNumber = sequence clone.Timestamp = timestamp handler(&clone) framesCount = 0 framesSize = framesSize[:0] framesData = framesData[:0] } } ================================================ FILE: pkg/opus/opus.go ================================================ package opus import ( "time" ) type Header struct { Mode string SampleRate uint16 FrameSize time.Duration Channels byte Frames byte } func UnmarshalHeader(b []byte) *Header { // https://datatracker.ietf.org/doc/html/rfc6716#section-3.1 b0 := b[0] config := b0 >> 3 return &Header{ Mode: parseMode(config), SampleRate: parseSampleRate(config), FrameSize: parseFrameSize(config), Channels: parseChannels(b0 >> 2 & 0b1), Frames: parseFrames(b0 & 0b11), } } func parseMode(config byte) string { if config <= 11 { return "silk" } if config <= 15 { return "hybrid" } return "celt" } func parseSampleRate(config byte) uint16 { switch config { case 0, 1, 2, 3, 16, 17, 18, 19: return 8000 // NB (narrowband) case 4, 5, 6, 7: return 12000 // MB (medium-band) case 8, 9, 10, 11, 20, 21, 22, 23: return 16000 // WB (wideband) case 12, 13, 24, 25, 26, 27: return 24000 // SWB (super-wideband) case 14, 15, 28, 29, 30, 31: return 48000 // FB (fullband) } return 0 } func parseFrameSize(config byte) time.Duration { switch config { case 0, 4, 8, 12, 14, 18, 22, 26, 30: return 10_000_000 case 1, 5, 9, 13, 15, 19, 23, 27, 31: return 20_000_000 case 2, 6, 10: return 40_000_000 case 3, 7, 11: return 60_000_000 case 16, 20, 24, 28: return 2_500_000 case 17, 21, 25, 29: return 5_000_000 } return 0 } func parseChannels(s byte) byte { if s == 1 { return 2 } return 1 } func parseFrames(c byte) byte { switch c { case 0: return 1 case 1, 2: return 2 } return 0xFF } func JoinFrames(b1, b2 []byte) []byte { // can't join if b1[0]&0b11 != 0 || b2[0]&0b11 != 0 { return append(b1, b2...) } size1, size2 := len(b1)-1, len(b2)-1 // join same sizes if size1 == size2 { b := make([]byte, 1+size1+size2) copy(b, b1) copy(b[1+size1:], b2[1:]) b[0] |= 0b01 return b } b := make([]byte, 1, 3+size1+size2) b[0] = b1[0] | 0b10 if size1 >= 252 { b0 := 252 + byte(size1)&0b11 b = append(b, b0, byte(size1/4)-b0) } else { b = append(b, byte(size1)) } b = append(b, b1[1:]...) b = append(b, b2[1:]...) return b } ================================================ FILE: pkg/pcm/backchannel.go ================================================ package pcm import ( "errors" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/shell" "github.com/pion/rtp" ) type Backchannel struct { core.Connection cmd *shell.Command } func NewBackchannel(cmd *shell.Command, audio string) (core.Producer, error) { var codec *core.Codec if audio == "" { // default codec codec = &core.Codec{Name: core.CodecPCML, ClockRate: 16000} } else if codec = core.ParseCodecString(audio); codec == nil { return nil, errors.New("pcm: unsupported audio format: " + audio) } medias := []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{codec}, }, } return &Backchannel{ Connection: core.Connection{ ID: core.NewID(), FormatName: "pcm", Protocol: "pipe", Medias: medias, Transport: cmd, }, cmd: cmd, }, nil } func (c *Backchannel) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return nil, core.ErrCantGetTrack } func (c *Backchannel) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { wr, err := c.cmd.StdinPipe() if err != nil { return err } sender := core.NewSender(media, track.Codec) sender.Handler = func(packet *rtp.Packet) { if n, err := wr.Write(packet.Payload); err != nil { c.Send += n } } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } func (c *Backchannel) Start() error { return c.cmd.Run() } ================================================ FILE: pkg/pcm/flac.go ================================================ // Package pcm - support raw (verbatim) PCM 16 bit in the FLAC container: // - only 1 channel // - only 16 bit per sample // - only 8000, 16000, 24000, 48000 sample rate package pcm import ( "encoding/binary" "unicode/utf8" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" "github.com/sigurn/crc16" "github.com/sigurn/crc8" ) func FLACHeader(magic bool, sampleRate uint32) []byte { b := make([]byte, 42) if magic { copy(b, "fLaC") // [0..3] } // https://xiph.org/flac/format.html#metadata_block_header b[4] = 0x80 // [4] lastMetadata=1 (1 bit), blockType=0 - STREAMINFO (7 bit) b[7] = 0x22 // [5..7] blockLength=34 (24 bit) // Important for Apple QuickTime player: // 1. Both values should be same // 2. Maximum value = 32768 binary.BigEndian.PutUint16(b[8:], 32768) // [8..9] info.BlockSizeMin=16 (16 bit) binary.BigEndian.PutUint16(b[10:], 32768) // [10..11] info.BlockSizeMin=65535 (16 bit) // [12..14] info.FrameSizeMin=0 (24 bit) // [15..17] info.FrameSizeMax=0 (24 bit) b[18] = byte(sampleRate >> 12) b[19] = byte(sampleRate >> 4) b[20] = byte(sampleRate << 4) // [18..20] info.SampleRate=8000 (20 bit), info.NChannels=1-1 (3 bit) b[21] = 0xF0 // [21..25] info.BitsPerSample=16-1 (5 bit), info.NSamples (36 bit) // [26..41] MD5sum (16 bytes) return b } var table8 *crc8.Table var table16 *crc16.Table func FLACEncoder(codecName string, clockRate uint32, handler core.HandlerFunc) core.HandlerFunc { var sr byte switch clockRate { case 8000: sr = 0b0100 case 16000: sr = 0b0101 case 22050: sr = 0b0110 case 24000: sr = 0b0111 case 32000: sr = 0b1000 case 44100: sr = 0b1001 case 48000: sr = 0b1010 case 96000: sr = 0b1011 default: return nil } if table8 == nil { table8 = crc8.MakeTable(crc8.CRC8) } if table16 == nil { table16 = crc16.MakeTable(crc16.CRC16_BUYPASS) } var sampleNumber int32 return func(packet *rtp.Packet) { samples := uint16(len(packet.Payload)) if codecName == core.CodecPCM || codecName == core.CodecPCML { samples /= 2 } // https://xiph.org/flac/format.html#frame_header buf := make([]byte, samples*2+30) // 1. Frame header buf[0] = 0xFF buf[1] = 0xF9 // [0..1] syncCode=0xFFF8 - reserved (15 bit), blockStrategy=1 - variable-blocksize (1 bit) buf[2] = 0x70 | sr // blockSizeType=7 (4 bit), sampleRate=4 - 8000 (4 bit) buf[3] = 0x08 // channels=1-1 (4 bit), sampleSize=4 - 16 (3 bit), reserved=0 (1 bit) n := 4 + utf8.EncodeRune(buf[4:], sampleNumber) // 4 bytes max sampleNumber += int32(samples) // this is wrong but very simple frame block size value binary.BigEndian.PutUint16(buf[n:], samples-1) n += 2 buf[n] = crc8.Checksum(buf[:n], table8) n += 1 // 2. Subframe header buf[n] = 0x02 // padding=0 (1 bit), subframeType=1 - verbatim (6 bit), wastedFlag=0 (1 bit) n += 1 // 3. Subframe switch codecName { case core.CodecPCMA: for _, b := range packet.Payload { s16 := PCMAtoPCM(b) buf[n] = byte(s16 >> 8) buf[n+1] = byte(s16) n += 2 } case core.CodecPCMU: for _, b := range packet.Payload { s16 := PCMUtoPCM(b) buf[n] = byte(s16 >> 8) buf[n+1] = byte(s16) n += 2 } case core.CodecPCM: n += copy(buf[n:], packet.Payload) case core.CodecPCML: // reverse endian from little to big size := len(packet.Payload) for i := 0; i < size; i += 2 { buf[n] = packet.Payload[i+1] buf[n+1] = packet.Payload[i] n += 2 } } // 4. Frame footer crc := crc16.Checksum(buf[:n], table16) binary.BigEndian.PutUint16(buf[n:], crc) n += 2 clone := *packet clone.Payload = buf[:n] handler(&clone) } } ================================================ FILE: pkg/pcm/handlers.go ================================================ package pcm import ( "sync" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) // RepackG711 - Repack G.711 PCMA/PCMU into frames of size 1024 // 1. Fixes WebRTC audio quality issue (monotonic timestamp) // 2. Fixes Reolink Doorbell backchannel issue (zero timestamp) // https://github.com/AlexxIT/go2rtc/issues/331 func RepackG711(zeroTS bool, handler core.HandlerFunc) core.HandlerFunc { const PacketSize = 1024 var buf []byte var seq uint16 var ts uint32 // fix https://github.com/AlexxIT/go2rtc/issues/432 var mu sync.Mutex return func(packet *rtp.Packet) { mu.Lock() buf = append(buf, packet.Payload...) if len(buf) < PacketSize { mu.Unlock() return } pkt := &rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: true, // should be true PayloadType: packet.PayloadType, // will be owerwriten SequenceNumber: seq, SSRC: packet.SSRC, }, Payload: buf[:PacketSize], } seq++ // don't know if zero TS important for Reolink Doorbell // don't have this strange devices for tests if !zeroTS { pkt.Timestamp = ts ts += PacketSize } buf = buf[PacketSize:] mu.Unlock() handler(pkt) } } // LittleToBig - convert PCM little endian to PCM big endian func LittleToBig(handler core.HandlerFunc) core.HandlerFunc { return func(packet *rtp.Packet) { clone := *packet clone.Payload = FlipEndian(packet.Payload) handler(&clone) } } func TranscodeHandler(dst, src *core.Codec, handler core.HandlerFunc) core.HandlerFunc { var ts uint32 k := float32(BytesPerFrame(dst)) / float32(BytesPerFrame(src)) f := Transcode(dst, src) return func(packet *rtp.Packet) { ts += uint32(k * float32(len(packet.Payload))) clone := *packet clone.Payload = f(packet.Payload) clone.Timestamp = ts handler(&clone) } } func BytesPerSample(codec *core.Codec) int { switch codec.Name { case core.CodecPCML, core.CodecPCM: return 2 case core.CodecPCMU, core.CodecPCMA: return 1 } return 0 } func BytesPerFrame(codec *core.Codec) int { if codec.Channels <= 1 { return BytesPerSample(codec) } return int(codec.Channels) * BytesPerSample(codec) } func FramesPerDuration(codec *core.Codec, duration time.Duration) int { return int(time.Duration(codec.ClockRate) * duration / time.Second) } func BytesPerDuration(codec *core.Codec, duration time.Duration) int { return BytesPerFrame(codec) * FramesPerDuration(codec, duration) } ================================================ FILE: pkg/pcm/pcm.go ================================================ package pcm import ( "math" "github.com/AlexxIT/go2rtc/pkg/core" ) func ceil(x float32) int { d, fract := math.Modf(float64(x)) if fract == 0.0 { return int(d) } return int(d) + 1 } func Downsample(k float32) func([]int16) []int16 { var sampleN, sampleSum float32 return func(src []int16) (dst []int16) { var i int dst = make([]int16, ceil((float32(len(src))+sampleN)/k)) for _, sample := range src { sampleSum += float32(sample) sampleN++ if sampleN >= k { dst[i] = int16(sampleSum / k) i++ sampleSum = 0 sampleN -= k } } return } } func Upsample(k float32) func([]int16) []int16 { var sampleN float32 return func(src []int16) (dst []int16) { var i int dst = make([]int16, ceil(k*float32(len(src)))) for _, sample := range src { sampleN += k for sampleN > 0 { dst[i] = sample i++ sampleN -= 1 } } return } } func FlipEndian(src []byte) (dst []byte) { var i, j int n := len(src) dst = make([]byte, n) for i < n { x := src[i] i++ dst[j] = src[i] j++ i++ dst[j] = x j++ } return } func Transcode(dst, src *core.Codec) func([]byte) []byte { var reader func([]byte) []int16 var writer func([]int16) []byte var filters []func([]int16) []int16 switch src.Name { case core.CodecPCML: reader = func(src []byte) (dst []int16) { var i, j int n := len(src) dst = make([]int16, n/2) for i < n { lo := src[i] i++ hi := src[i] i++ dst[j] = int16(hi)<<8 | int16(lo) j++ } return } case core.CodecPCM: reader = func(src []byte) (dst []int16) { var i, j int n := len(src) dst = make([]int16, n/2) for i < n { hi := src[i] i++ lo := src[i] i++ dst[j] = int16(hi)<<8 | int16(lo) j++ } return } case core.CodecPCMU: reader = func(src []byte) (dst []int16) { var i int dst = make([]int16, len(src)) for _, sample := range src { dst[i] = PCMUtoPCM(sample) i++ } return } case core.CodecPCMA: reader = func(src []byte) (dst []int16) { var i int dst = make([]int16, len(src)) for _, sample := range src { dst[i] = PCMAtoPCM(sample) i++ } return } } if src.Channels > 1 { filters = append(filters, Downsample(float32(src.Channels))) } if src.ClockRate > dst.ClockRate { filters = append(filters, Downsample(float32(src.ClockRate)/float32(dst.ClockRate))) } else if src.ClockRate < dst.ClockRate { filters = append(filters, Upsample(float32(dst.ClockRate)/float32(src.ClockRate))) } if dst.Channels > 1 { filters = append(filters, Upsample(float32(dst.Channels))) } switch dst.Name { case core.CodecPCML: writer = func(src []int16) (dst []byte) { var i int dst = make([]byte, len(src)*2) for _, sample := range src { dst[i] = byte(sample) i++ dst[i] = byte(sample >> 8) i++ } return } case core.CodecPCM: writer = func(src []int16) (dst []byte) { var i int dst = make([]byte, len(src)*2) for _, sample := range src { dst[i] = byte(sample >> 8) i++ dst[i] = byte(sample) i++ } return } case core.CodecPCMU: writer = func(src []int16) (dst []byte) { var i int dst = make([]byte, len(src)) for _, sample := range src { dst[i] = PCMtoPCMU(sample) i++ } return } case core.CodecPCMA: writer = func(src []int16) (dst []byte) { var i int dst = make([]byte, len(src)) for _, sample := range src { dst[i] = PCMtoPCMA(sample) i++ } return } } return func(b []byte) []byte { samples := reader(b) for _, filter := range filters { samples = filter(samples) } return writer(samples) } } func ConsumerCodecs() []*core.Codec { return []*core.Codec{ {Name: core.CodecPCML}, {Name: core.CodecPCM}, {Name: core.CodecPCMA}, {Name: core.CodecPCMU}, } } func ProducerCodecs() []*core.Codec { return []*core.Codec{ {Name: core.CodecPCML, ClockRate: 16000}, {Name: core.CodecPCM, ClockRate: 16000}, {Name: core.CodecPCML, ClockRate: 8000}, {Name: core.CodecPCM, ClockRate: 8000}, {Name: core.CodecPCMA, ClockRate: 8000}, {Name: core.CodecPCMU, ClockRate: 8000}, {Name: core.CodecPCML, ClockRate: 22050}, // wyoming-snd-external } } ================================================ FILE: pkg/pcm/pcm_test.go ================================================ package pcm import ( "encoding/hex" "fmt" "testing" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/stretchr/testify/require" ) func TestTranscode(t *testing.T) { tests := []struct { name string src core.Codec dst core.Codec source string expect string }{ { name: "s16be->s16be", src: core.Codec{Name: core.CodecPCM, ClockRate: 8000, Channels: 1}, dst: core.Codec{Name: core.CodecPCM, ClockRate: 8000, Channels: 1}, source: "FCCA00130343062808130B510D9E0F7610DA111113EA15BD16F2168215D41561", expect: "FCCA00130343062808130B510D9E0F7610DA111113EA15BD16F2168215D41561", }, { name: "s16be->s16le", src: core.Codec{Name: core.CodecPCM, ClockRate: 8000, Channels: 1}, dst: core.Codec{Name: core.CodecPCML, ClockRate: 8000, Channels: 1}, source: "FCCA00130343062808130B510D9E0F7610DA111113EA15BD16F2168215D41561", expect: "CAFC1300430328061308510B9E0D760FDA101111EA13BD15F2168216D4156115", }, { name: "s16be->mulaw", src: core.Codec{Name: core.CodecPCM, ClockRate: 8000, Channels: 1}, dst: core.Codec{Name: core.CodecPCMU, ClockRate: 8000, Channels: 1}, source: "FCCA00130343062808130B510D9E0F7610DA111113EA15BD16F2168215D41561", expect: "52FDD1C5BEB8B3B0AEAEABA9A8A8A9AA", }, { name: "s16be->alaw", src: core.Codec{Name: core.CodecPCM, ClockRate: 8000, Channels: 1}, dst: core.Codec{Name: core.CodecPCMA, ClockRate: 8000, Channels: 1}, source: "FCCA00130343062808130B510D9E0F7610DA111113EA15BD16F2168215D41561", expect: "7CD4FFED95939E9B8584868083838080", }, { name: "2ch->1ch", src: core.Codec{Name: core.CodecPCM, ClockRate: 8000, Channels: 2}, dst: core.Codec{Name: core.CodecPCM, ClockRate: 8000, Channels: 1}, source: "FCCAFCCA001300130343034306280628081308130B510B510D9E0D9E0F760F76", expect: "FCCA00130343062808130B510D9E0F76", }, { name: "1ch->2ch", src: core.Codec{Name: core.CodecPCM, ClockRate: 8000, Channels: 1}, dst: core.Codec{Name: core.CodecPCM, ClockRate: 8000, Channels: 2}, source: "FCCA00130343062808130B510D9E0F76", expect: "FCCAFCCA001300130343034306280628081308130B510B510D9E0D9E0F760F76", }, { name: "16khz->8khz", src: core.Codec{Name: core.CodecPCM, ClockRate: 16000, Channels: 1}, dst: core.Codec{Name: core.CodecPCM, ClockRate: 8000, Channels: 1}, source: "FCCAFCCA001300130343034306280628081308130B510B510D9E0D9E0F760F76", expect: "FCCA00130343062808130B510D9E0F76", }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { f := Transcode(&test.dst, &test.src) b, _ := hex.DecodeString(test.source) b = f(b) s := fmt.Sprintf("%X", b) require.Equal(t, test.expect, s) }) } } ================================================ FILE: pkg/pcm/pcma.go ================================================ // Package pcm // https://www.codeproject.com/Articles/14237/Using-the-G711-standard package pcm const alawMax = 0x7FFF func PCMAtoPCM(alaw byte) int16 { alaw ^= 0xD5 data := int16(((alaw & 0x0F) << 4) + 8) exponent := (alaw & 0x70) >> 4 if exponent != 0 { data |= 0x100 } if exponent > 1 { data <<= exponent - 1 } // sign if alaw&0x80 == 0 { return data } else { return -data } } func PCMtoPCMA(pcm int16) byte { var alaw byte if pcm < 0 { pcm = -pcm alaw = 0x80 } if pcm > alawMax { pcm = alawMax } exponent := byte(7) for expMask := int16(0x4000); (pcm&expMask) == 0 && exponent > 0; expMask >>= 1 { exponent-- } if exponent == 0 { alaw |= byte(pcm>>4) & 0x0F } else { alaw |= (exponent << 4) | (byte(pcm>>(exponent+3)) & 0x0F) } return alaw ^ 0xD5 } ================================================ FILE: pkg/pcm/pcmu.go ================================================ // Package pcm // https://www.codeproject.com/Articles/14237/Using-the-G711-standard package pcm const bias = 0x84 // 132 or 1000 0100 const ulawMax = alawMax - bias func PCMUtoPCM(ulaw byte) int16 { ulaw = ^ulaw exponent := (ulaw & 0x70) >> 4 data := (int16((((ulaw&0x0F)|0x10)<<1)+1) << (exponent + 2)) - bias // sign if ulaw&0x80 == 0 { return data } else if data == 0 { return -1 } else { return -data } } func PCMtoPCMU(pcm int16) byte { var ulaw byte if pcm < 0 { pcm = -pcm ulaw = 0x80 } if pcm > ulawMax { pcm = ulawMax } pcm += bias exponent := byte(7) for expMask := int16(0x4000); (pcm & expMask) == 0; expMask >>= 1 { exponent-- } // mantisa ulaw |= byte(pcm>>(exponent+3)) & 0x0F if exponent > 0 { ulaw |= exponent << 4 } return ^ulaw } ================================================ FILE: pkg/pcm/producer.go ================================================ package pcm import ( "io" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type Producer struct { core.Connection rd io.Reader } func Open(rd io.Reader) (*Producer, error) { medias := []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ {Name: core.CodecPCMU, ClockRate: 8000}, }, }, } return &Producer{ core.Connection{ ID: core.NewID(), FormatName: "pcm", Medias: medias, Transport: rd, }, rd, }, nil } func (c *Producer) Start() error { for { payload := make([]byte, 1024) if _, err := io.ReadFull(c.rd, payload); err != nil { return err } c.Recv += 1024 if len(c.Receivers) == 0 { continue } pkt := &rtp.Packet{ Header: rtp.Header{Timestamp: core.Now90000()}, Payload: payload, } c.Receivers[0].WriteRTP(pkt) } } ================================================ FILE: pkg/pcm/producer_sync.go ================================================ package pcm import ( "io" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type ProducerSync struct { core.Connection src *core.Codec rd io.Reader onClose func() } func OpenSync(codec *core.Codec, rd io.Reader) *ProducerSync { medias := []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: ProducerCodecs(), }, } return &ProducerSync{ Connection: core.Connection{ ID: core.NewID(), FormatName: "pcm", Medias: medias, Transport: rd, }, src: codec, rd: rd, } } func (p *ProducerSync) OnClose(f func()) { p.onClose = f } func (p *ProducerSync) Start() error { if len(p.Receivers) == 0 { return nil } var pktSeq uint16 var pktTS uint32 // time in frames var pktTime time.Duration // time in seconds t0 := time.Now() dst := p.Receivers[0].Codec transcode := Transcode(dst, p.src) const chunkDuration = 20 * time.Millisecond chunkBytes := BytesPerDuration(p.src, chunkDuration) chunkFrames := uint32(FramesPerDuration(dst, chunkDuration)) for { buf := make([]byte, chunkBytes) n, _ := io.ReadFull(p.rd, buf) if n == 0 { break } pkt := &core.Packet{ Header: rtp.Header{ Version: 2, Marker: true, SequenceNumber: pktSeq, Timestamp: pktTS, }, Payload: transcode(buf[:n]), } if d := pktTime - time.Since(t0); d > 0 { time.Sleep(d) } p.Receivers[0].WriteRTP(pkt) p.Recv += n pktSeq++ pktTS += chunkFrames pktTime += chunkDuration } if p.onClose != nil { p.onClose() } return nil } ================================================ FILE: pkg/pcm/s16le/s16le.go ================================================ package s16le func PeaksRMS(b []byte) int16 { // RMS of sine wave = peak / sqrt2 // https://en.wikipedia.org/wiki/Root_mean_square // https://www.youtube.com/watch?v=MUDkL4KZi0I var peaks int32 var peaksSum int32 var prevSample int16 var prevUp bool var i int for n := len(b); i < n; { lo := b[i] i++ hi := b[i] i++ sample := int16(hi)<<8 | int16(lo) up := sample >= prevSample if i >= 4 { if up != prevUp { if prevSample >= 0 { peaksSum += int32(prevSample) } else { peaksSum -= int32(prevSample) } peaks++ } } prevSample = sample prevUp = up } if peaks == 0 { return 0 } return int16(peaksSum / peaks) } ================================================ FILE: pkg/pcm/v1/pcm.go ================================================ // Package v1 // http://web.archive.org/web/20110719132013/http://hazelware.luggle.com/tutorials/mulawcompression.html package v1 const cBias = 0x84 const cClip = 32635 var MuLawCompressTable = [256]byte{ 0, 0, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, } func LinearToMuLawSample(sample int16) byte { sign := byte(sample>>8) & 0x80 if sign != 0 { sample = -sample } if sample > cClip { sample = cClip } sample = sample + cBias exponent := MuLawCompressTable[(sample>>7)&0xFF] mantissa := byte(sample>>(exponent+3)) & 0x0F compressedByte := ^(sign | (exponent << 4) | mantissa) return compressedByte } var ALawCompressTable = [128]byte{ 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, } func LinearToALawSample(sample int16) byte { sign := byte((^sample)>>8) & 0x80 if sign == 0 { sample = -sample } if sample > cClip { sample = cClip } var compressedByte byte if sample >= 256 { exponent := ALawCompressTable[(sample>>8)&0x7F] mantissa := byte(sample>>(exponent+3)) & 0x0F compressedByte = (exponent << 4) | mantissa } else { compressedByte = byte(sample >> 4) } compressedByte ^= sign ^ 0x55 return compressedByte } var MuLawDecompressTable = [256]int16{ -32124, -31100, -30076, -29052, -28028, -27004, -25980, -24956, -23932, -22908, -21884, -20860, -19836, -18812, -17788, -16764, -15996, -15484, -14972, -14460, -13948, -13436, -12924, -12412, -11900, -11388, -10876, -10364, -9852, -9340, -8828, -8316, -7932, -7676, -7420, -7164, -6908, -6652, -6396, -6140, -5884, -5628, -5372, -5116, -4860, -4604, -4348, -4092, -3900, -3772, -3644, -3516, -3388, -3260, -3132, -3004, -2876, -2748, -2620, -2492, -2364, -2236, -2108, -1980, -1884, -1820, -1756, -1692, -1628, -1564, -1500, -1436, -1372, -1308, -1244, -1180, -1116, -1052, -988, -924, -876, -844, -812, -780, -748, -716, -684, -652, -620, -588, -556, -524, -492, -460, -428, -396, -372, -356, -340, -324, -308, -292, -276, -260, -244, -228, -212, -196, -180, -164, -148, -132, -120, -112, -104, -96, -88, -80, -72, -64, -56, -48, -40, -32, -24, -16, -8, -1, 32124, 31100, 30076, 29052, 28028, 27004, 25980, 24956, 23932, 22908, 21884, 20860, 19836, 18812, 17788, 16764, 15996, 15484, 14972, 14460, 13948, 13436, 12924, 12412, 11900, 11388, 10876, 10364, 9852, 9340, 8828, 8316, 7932, 7676, 7420, 7164, 6908, 6652, 6396, 6140, 5884, 5628, 5372, 5116, 4860, 4604, 4348, 4092, 3900, 3772, 3644, 3516, 3388, 3260, 3132, 3004, 2876, 2748, 2620, 2492, 2364, 2236, 2108, 1980, 1884, 1820, 1756, 1692, 1628, 1564, 1500, 1436, 1372, 1308, 1244, 1180, 1116, 1052, 988, 924, 876, 844, 812, 780, 748, 716, 684, 652, 620, 588, 556, 524, 492, 460, 428, 396, 372, 356, 340, 324, 308, 292, 276, 260, 244, 228, 212, 196, 180, 164, 148, 132, 120, 112, 104, 96, 88, 80, 72, 64, 56, 48, 40, 32, 24, 16, 8, 0, } var ALawDecompressTable = [256]int16{ -5504, -5248, -6016, -5760, -4480, -4224, -4992, -4736, -7552, -7296, -8064, -7808, -6528, -6272, -7040, -6784, -2752, -2624, -3008, -2880, -2240, -2112, -2496, -2368, -3776, -3648, -4032, -3904, -3264, -3136, -3520, -3392, -22016, -20992, -24064, -23040, -17920, -16896, -19968, -18944, -30208, -29184, -32256, -31232, -26112, -25088, -28160, -27136, -11008, -10496, -12032, -11520, -8960, -8448, -9984, -9472, -15104, -14592, -16128, -15616, -13056, -12544, -14080, -13568, -344, -328, -376, -360, -280, -264, -312, -296, -472, -456, -504, -488, -408, -392, -440, -424, -88, -72, -120, -104, -24, -8, -56, -40, -216, -200, -248, -232, -152, -136, -184, -168, -1376, -1312, -1504, -1440, -1120, -1056, -1248, -1184, -1888, -1824, -2016, -1952, -1632, -1568, -1760, -1696, -688, -656, -752, -720, -560, -528, -624, -592, -944, -912, -1008, -976, -816, -784, -880, -848, 5504, 5248, 6016, 5760, 4480, 4224, 4992, 4736, 7552, 7296, 8064, 7808, 6528, 6272, 7040, 6784, 2752, 2624, 3008, 2880, 2240, 2112, 2496, 2368, 3776, 3648, 4032, 3904, 3264, 3136, 3520, 3392, 22016, 20992, 24064, 23040, 17920, 16896, 19968, 18944, 30208, 29184, 32256, 31232, 26112, 25088, 28160, 27136, 11008, 10496, 12032, 11520, 8960, 8448, 9984, 9472, 15104, 14592, 16128, 15616, 13056, 12544, 14080, 13568, 344, 328, 376, 360, 280, 264, 312, 296, 472, 456, 504, 488, 408, 392, 440, 424, 88, 72, 120, 104, 24, 8, 56, 40, 216, 200, 248, 232, 152, 136, 184, 168, 1376, 1312, 1504, 1440, 1120, 1056, 1248, 1184, 1888, 1824, 2016, 1952, 1632, 1568, 1760, 1696, 688, 656, 752, 720, 560, 528, 624, 592, 944, 912, 1008, 976, 816, 784, 880, 848, } ================================================ FILE: pkg/pcm/v1/pcm_test.go ================================================ package v1 import ( "testing" v2 "github.com/AlexxIT/go2rtc/pkg/pcm" "github.com/stretchr/testify/require" ) func TestPCMUtoPCM(t *testing.T) { for pcmu := byte(0); pcmu < 255; pcmu++ { pcm1 := MuLawDecompressTable[pcmu] pcm2 := v2.PCMUtoPCM(pcmu) require.Equal(t, pcm1, pcm2) } } func TestPCMAtoPCM(t *testing.T) { for pcma := byte(0); pcma < 255; pcma++ { pcm1 := ALawDecompressTable[pcma] pcm2 := v2.PCMAtoPCM(pcma) require.Equal(t, pcm1, pcm2) } } func TestPCMtoPCMU(t *testing.T) { for pcm := int16(-32768); pcm < 32767; pcm++ { pcmu1 := LinearToMuLawSample(pcm) pcmu2 := v2.PCMtoPCMU(pcm) require.Equal(t, pcmu1, pcmu2) } } func TestPCMtoPCMA(t *testing.T) { for pcm := int16(-32768); pcm < 32767; pcm++ { pcma1 := LinearToALawSample(pcm) pcma2 := v2.PCMtoPCMA(pcm) require.Equal(t, pcma1, pcma2) } } ================================================ FILE: pkg/pinggy/pinggy.go ================================================ package pinggy import ( "context" "encoding/json" "errors" "io" "net" "net/http" "time" "golang.org/x/crypto/ssh" ) type Client struct { SSH *ssh.Client TCP net.Listener API *http.Client } func NewClient(proto string) (*Client, error) { switch proto { case "http", "tcp", "tls", "tlstcp": case "": proto = "http" default: return nil, errors.New("pinggy: unsupported proto: " + proto) } config := &ssh.ClientConfig{ User: "auth+" + proto, Auth: []ssh.AuthMethod{ssh.Password("nopass")}, HostKeyCallback: ssh.InsecureIgnoreHostKey(), Timeout: 5 * time.Second, } client, err := ssh.Dial("tcp", "a.pinggy.io:443", config) if err != nil { return nil, err } ln, err := client.Listen("tcp", "0.0.0.0:0") if err != nil { _ = client.Close() return nil, err } c := &Client{ SSH: client, TCP: ln, API: &http.Client{ Transport: &http.Transport{ DialContext: func(ctx context.Context, network, addr string) (net.Conn, error) { return client.Dial(network, addr) }, }, }, } if proto == "http" { if err = c.NewSession(); err != nil { _ = client.Close() return nil, err } } return c, nil } func (c *Client) Close() error { return errors.Join(c.SSH.Close(), c.TCP.Close()) } func (c *Client) NewSession() error { session, err := c.SSH.NewSession() if err != nil { return err } return session.Shell() } func (c *Client) GetURLs() ([]string, error) { res, err := c.API.Get("http://localhost:4300/urls") if err != nil { return nil, err } defer res.Body.Close() var v struct { URLs []string `json:"urls"` } if err = json.NewDecoder(res.Body).Decode(&v); err != nil { return nil, err } return v.URLs, nil } func (c *Client) Proxy(address string) error { defer c.TCP.Close() for { conn, err := c.TCP.Accept() if err != nil { return err } go proxy(conn, address) } } func proxy(conn1 net.Conn, address string) { defer conn1.Close() conn2, err := net.Dial("tcp", address) if err != nil { return } defer conn2.Close() go io.Copy(conn2, conn1) io.Copy(conn1, conn2) } // DialTLS like ssh.Dial but with TLS //func DialTLS(network, addr, sni string, config *ssh.ClientConfig) (*ssh.Client, error) { // conn, err := net.DialTimeout(network, addr, config.Timeout) // if err != nil { // return nil, err // } // conn = tls.Client(conn, &tls.Config{ServerName: sni, InsecureSkipVerify: sni == ""}) // c, chans, reqs, err := ssh.NewClientConn(conn, addr, config) // if err != nil { // return nil, err // } // return ssh.NewClient(c, chans, reqs), nil //} ================================================ FILE: pkg/probe/consumer.go ================================================ package probe import ( "net/url" "strings" "github.com/AlexxIT/go2rtc/pkg/core" ) type Probe struct { core.Connection } func Create(name string, query url.Values) *Probe { medias := core.ParseQuery(query) for _, value := range query["microphone"] { media := &core.Media{Kind: core.KindAudio, Direction: core.DirectionRecvonly} for _, name := range strings.Split(value, ",") { name = strings.ToUpper(name) switch name { case "", "COPY": name = core.CodecAny } media.Codecs = append(media.Codecs, &core.Codec{Name: name}) } medias = append(medias, media) } return &Probe{ Connection: core.Connection{ ID: core.NewID(), FormatName: name, Medias: medias, }, } } func (p *Probe) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { sender := core.NewSender(media, track.Codec) sender.Handler = func(pkt *core.Packet) { p.Send += len(pkt.Payload) } sender.HandleRTP(track) p.Senders = append(p.Senders, sender) return nil } func (p *Probe) Start() error { return nil } ================================================ FILE: pkg/ring/api.go ================================================ package ring import ( "bytes" "crypto/sha256" "encoding/base64" "encoding/hex" "encoding/json" "fmt" "io" "net/http" "reflect" "strings" "sync" "time" ) var clientCache = map[string]*RingApi{} var cacheMutex sync.Mutex type RefreshTokenAuth struct { RefreshToken string } type EmailAuth struct { Email string Password string } type AuthConfig struct { RT string `json:"rt"` // Refresh Token HID string `json:"hid"` // Hardware ID } type AuthTokenResponse struct { AccessToken string `json:"access_token"` ExpiresIn int `json:"expires_in"` RefreshToken string `json:"refresh_token"` Scope string `json:"scope"` // Always "client" TokenType string `json:"token_type"` // Always "Bearer" } type Auth2faResponse struct { Error string `json:"error"` ErrorDescription string `json:"error_description"` TSVState string `json:"tsv_state"` Phone string `json:"phone"` NextTimeInSecs int `json:"next_time_in_secs"` } type SocketTicketResponse struct { Ticket string `json:"ticket"` ResponseTimestamp int64 `json:"response_timestamp"` } type SessionResponse struct { Profile struct { ID int64 `json:"id"` Email string `json:"email"` FirstName string `json:"first_name"` LastName string `json:"last_name"` } `json:"profile"` } type RingApi struct { httpClient *http.Client authConfig *AuthConfig hardwareID string authToken *AuthTokenResponse tokenExpiry time.Time Using2FA bool PromptFor2FA string RefreshToken string auth interface{} // EmailAuth or RefreshTokenAuth onTokenRefresh func(string) authMutex sync.Mutex session *SessionResponse sessionExpiry time.Time sessionMutex sync.Mutex cacheKey string } type CameraKind string type CameraData struct { ID int `json:"id"` Description string `json:"description"` DeviceID string `json:"device_id"` Kind string `json:"kind"` LocationID string `json:"location_id"` } type RingDeviceType string type RingDevicesResponse struct { Doorbots []CameraData `json:"doorbots"` AuthorizedDoorbots []CameraData `json:"authorized_doorbots"` StickupCams []CameraData `json:"stickup_cams"` AllCameras []CameraData `json:"all_cameras"` Chimes []CameraData `json:"chimes"` Other []map[string]interface{} `json:"other"` } const ( Doorbot CameraKind = "doorbot" Doorbell CameraKind = "doorbell" DoorbellV3 CameraKind = "doorbell_v3" DoorbellV4 CameraKind = "doorbell_v4" DoorbellV5 CameraKind = "doorbell_v5" DoorbellOyster CameraKind = "doorbell_oyster" DoorbellPortal CameraKind = "doorbell_portal" DoorbellScallop CameraKind = "doorbell_scallop" DoorbellScallopLite CameraKind = "doorbell_scallop_lite" DoorbellGraham CameraKind = "doorbell_graham_cracker" LpdV1 CameraKind = "lpd_v1" LpdV2 CameraKind = "lpd_v2" LpdV4 CameraKind = "lpd_v4" JboxV1 CameraKind = "jbox_v1" StickupCam CameraKind = "stickup_cam" StickupCamV3 CameraKind = "stickup_cam_v3" StickupCamElite CameraKind = "stickup_cam_elite" StickupCamLongfin CameraKind = "stickup_cam_longfin" StickupCamLunar CameraKind = "stickup_cam_lunar" SpotlightV2 CameraKind = "spotlightw_v2" HpCamV1 CameraKind = "hp_cam_v1" HpCamV2 CameraKind = "hp_cam_v2" StickupCamV4 CameraKind = "stickup_cam_v4" FloodlightV1 CameraKind = "floodlight_v1" FloodlightV2 CameraKind = "floodlight_v2" FloodlightPro CameraKind = "floodlight_pro" CocoaCamera CameraKind = "cocoa_camera" CocoaDoorbell CameraKind = "cocoa_doorbell" CocoaFloodlight CameraKind = "cocoa_floodlight" CocoaSpotlight CameraKind = "cocoa_spotlight" StickupCamMini CameraKind = "stickup_cam_mini" OnvifCamera CameraKind = "onvif_camera" ) const ( IntercomHandsetAudio RingDeviceType = "intercom_handset_audio" OnvifCameraType RingDeviceType = "onvif_camera" ) const ( clientAPIBaseURL = "https://api.ring.com/clients_api/" deviceAPIBaseURL = "https://api.ring.com/devices/v1/" commandsAPIBaseURL = "https://api.ring.com/commands/v1/" appAPIBaseURL = "https://prd-api-us.prd.rings.solutions/api/v1/" oauthURL = "https://oauth.ring.com/oauth/token" apiVersion = 11 defaultTimeout = 20 * time.Second maxRetries = 3 sessionValidTime = 12 * time.Hour ) func NewRestClient(auth interface{}, onTokenRefresh func(string)) (*RingApi, error) { var cacheKey string // Create cache key based on auth data switch a := auth.(type) { case RefreshTokenAuth: if a.RefreshToken == "" { return nil, fmt.Errorf("refresh token is required") } cacheKey = "refresh:" + a.RefreshToken case EmailAuth: if a.Email == "" || a.Password == "" { return nil, fmt.Errorf("email and password are required") } cacheKey = "email:" + a.Email + ":" + a.Password default: return nil, fmt.Errorf("invalid auth type") } cacheMutex.Lock() defer cacheMutex.Unlock() if cachedClient, ok := clientCache[cacheKey]; ok { // Check if token is not nil and not expired if cachedClient.authToken != nil && time.Now().Before(cachedClient.tokenExpiry) { cachedClient.onTokenRefresh = onTokenRefresh return cachedClient, nil } } client := &RingApi{ httpClient: &http.Client{Timeout: defaultTimeout}, onTokenRefresh: onTokenRefresh, hardwareID: generateHardwareID(), auth: auth, cacheKey: cacheKey, } switch a := auth.(type) { case RefreshTokenAuth: config, err := parseAuthConfig(a.RefreshToken) if err != nil { return nil, fmt.Errorf("failed to parse refresh token: %w", err) } client.authConfig = config client.hardwareID = config.HID client.RefreshToken = a.RefreshToken } clientCache[cacheKey] = client return client, nil } func ClientAPI(path string) string { return clientAPIBaseURL + path } func DeviceAPI(path string) string { return deviceAPIBaseURL + path } func CommandsAPI(path string) string { return commandsAPIBaseURL + path } func AppAPI(path string) string { return appAPIBaseURL + path } func (c *RingApi) GetAuth(twoFactorAuthCode string) (*AuthTokenResponse, error) { var grantData map[string]string if c.authConfig != nil && twoFactorAuthCode == "" { grantData = map[string]string{ "grant_type": "refresh_token", "refresh_token": c.authConfig.RT, } } else { authEmail, ok := c.auth.(EmailAuth) if !ok { return nil, fmt.Errorf("invalid auth type for email authentication") } grantData = map[string]string{ "grant_type": "password", "username": authEmail.Email, "password": authEmail.Password, } } grantData["client_id"] = "ring_official_android" grantData["scope"] = "client" body, err := json.Marshal(grantData) if err != nil { return nil, fmt.Errorf("failed to marshal auth request: %w", err) } req, err := http.NewRequest("POST", oauthURL, bytes.NewReader(body)) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") req.Header.Set("hardware_id", c.hardwareID) req.Header.Set("User-Agent", "android:com.ringapp") req.Header.Set("2fa-support", "true") if twoFactorAuthCode != "" { req.Header.Set("2fa-code", twoFactorAuthCode) } resp, err := c.httpClient.Do(req) if err != nil { return nil, err } defer resp.Body.Close() // Handle 2FA Responses if resp.StatusCode == http.StatusPreconditionFailed || (resp.StatusCode == http.StatusBadRequest && strings.Contains(resp.Header.Get("WWW-Authenticate"), "Verification Code")) { var tfaResp Auth2faResponse if err := json.NewDecoder(resp.Body).Decode(&tfaResp); err != nil { return nil, err } c.Using2FA = true if resp.StatusCode == http.StatusBadRequest { c.PromptFor2FA = "Invalid 2fa code entered. Please try again." return nil, fmt.Errorf("invalid 2FA code") } if tfaResp.TSVState != "" { prompt := "from your authenticator app" if tfaResp.TSVState != "totp" { prompt = fmt.Sprintf("sent to %s via %s", tfaResp.Phone, tfaResp.TSVState) } c.PromptFor2FA = fmt.Sprintf("Please enter the code %s", prompt) } else { c.PromptFor2FA = "Please enter the code sent to your text/email" } return nil, fmt.Errorf("2FA required") } // Handle errors if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) return nil, fmt.Errorf("auth request failed with status %d: %s", resp.StatusCode, string(body)) } var authResp AuthTokenResponse if err := json.NewDecoder(resp.Body).Decode(&authResp); err != nil { return nil, fmt.Errorf("failed to decode auth response: %w", err) } // Refresh token and expiry c.authToken = &authResp c.authConfig = &AuthConfig{ RT: authResp.RefreshToken, HID: c.hardwareID, } // Set token expiry (1 minute before actual expiry) expiresIn := time.Duration(authResp.ExpiresIn-60) * time.Second c.tokenExpiry = time.Now().Add(expiresIn) c.RefreshToken = encodeAuthConfig(c.authConfig) if c.onTokenRefresh != nil { c.onTokenRefresh(c.RefreshToken) } // Refresh the cached client cacheMutex.Lock() clientCache[c.cacheKey] = c cacheMutex.Unlock() return c.authToken, nil } func (c *RingApi) FetchRingDevices() (*RingDevicesResponse, error) { response, err := c.Request("GET", ClientAPI("ring_devices"), nil) if err != nil { return nil, fmt.Errorf("failed to fetch ring devices: %w", err) } var devices RingDevicesResponse if err := json.Unmarshal(response, &devices); err != nil { return nil, fmt.Errorf("failed to unmarshal devices response: %w", err) } // Process "other" devices var onvifCameras []CameraData var intercoms []CameraData for _, device := range devices.Other { kind, ok := device["kind"].(string) if !ok { continue } switch RingDeviceType(kind) { case OnvifCameraType: var camera CameraData if deviceJson, err := json.Marshal(device); err == nil { if err := json.Unmarshal(deviceJson, &camera); err == nil { onvifCameras = append(onvifCameras, camera) } } case IntercomHandsetAudio: var intercom CameraData if deviceJson, err := json.Marshal(device); err == nil { if err := json.Unmarshal(deviceJson, &intercom); err == nil { intercoms = append(intercoms, intercom) } } } } // Combine all cameras into AllCameras slice allCameras := make([]CameraData, 0) allCameras = append(allCameras, interfaceSlice(devices.Doorbots)...) allCameras = append(allCameras, interfaceSlice(devices.StickupCams)...) allCameras = append(allCameras, interfaceSlice(devices.AuthorizedDoorbots)...) allCameras = append(allCameras, interfaceSlice(onvifCameras)...) allCameras = append(allCameras, interfaceSlice(intercoms)...) devices.AllCameras = allCameras return &devices, nil } func (c *RingApi) GetSocketTicket() (*SocketTicketResponse, error) { response, err := c.Request("POST", AppAPI("clap/ticket/request/signalsocket"), nil) if err != nil { return nil, fmt.Errorf("failed to fetch socket ticket: %w", err) } var ticket SocketTicketResponse if err := json.Unmarshal(response, &ticket); err != nil { return nil, fmt.Errorf("failed to unmarshal socket ticket response: %w", err) } return &ticket, nil } func (c *RingApi) Request(method, url string, body interface{}) ([]byte, error) { // Ensure we have a valid session if err := c.ensureSession(); err != nil { return nil, fmt.Errorf("session validation failed: %w", err) } var bodyReader io.Reader if body != nil { jsonBody, err := json.Marshal(body) if err != nil { return nil, fmt.Errorf("failed to marshal request body: %w", err) } bodyReader = bytes.NewReader(jsonBody) } // Create request req, err := http.NewRequest(method, url, bodyReader) if err != nil { return nil, fmt.Errorf("failed to create request: %w", err) } // Set headers req.Header.Set("Authorization", "Bearer "+c.authToken.AccessToken) req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") req.Header.Set("hardware_id", c.hardwareID) req.Header.Set("User-Agent", "android:com.ringapp") // Make request with retries var resp *http.Response var responseBody []byte for attempt := 0; attempt <= maxRetries; attempt++ { resp, err = c.httpClient.Do(req) if err != nil { if attempt == maxRetries { return nil, fmt.Errorf("request failed after %d retries: %w", maxRetries, err) } time.Sleep(5 * time.Second) continue } defer resp.Body.Close() responseBody, err = io.ReadAll(resp.Body) if err != nil { return nil, fmt.Errorf("failed to read response body: %w", err) } // Handle 401 by refreshing auth and retrying if resp.StatusCode == http.StatusUnauthorized { // Reset token to force refresh c.authMutex.Lock() c.authToken = nil c.tokenExpiry = time.Time{} // Reset token expiry c.authMutex.Unlock() if attempt == maxRetries { return nil, fmt.Errorf("authentication failed after %d retries", maxRetries) } // By 401 with Auth AND Session start over c.sessionMutex.Lock() c.session = nil c.sessionExpiry = time.Time{} // Reset session expiry c.sessionMutex.Unlock() if err := c.ensureSession(); err != nil { return nil, fmt.Errorf("failed to refresh session: %w", err) } req.Header.Set("Authorization", "Bearer "+c.authToken.AccessToken) continue } // Handle 404 error with hardware_id reference - session issue if resp.StatusCode == 404 && strings.Contains(url, clientAPIBaseURL) { var errorBody map[string]interface{} if err := json.Unmarshal(responseBody, &errorBody); err == nil { if errorStr, ok := errorBody["error"].(string); ok && strings.Contains(errorStr, c.hardwareID) { // Session with hardware_id not found, refresh session c.sessionMutex.Lock() c.session = nil c.sessionExpiry = time.Time{} // Reset session expiry c.sessionMutex.Unlock() if attempt == maxRetries { return nil, fmt.Errorf("session refresh failed after %d retries", maxRetries) } if err := c.ensureSession(); err != nil { return nil, fmt.Errorf("failed to refresh session: %w", err) } continue } } } // Handle other error status codes if resp.StatusCode >= 400 { return nil, fmt.Errorf("request failed with status %d: %s", resp.StatusCode, string(responseBody)) } break } return responseBody, nil } func (c *RingApi) ensureSession() error { c.sessionMutex.Lock() defer c.sessionMutex.Unlock() // If session is still valid, use it if c.session != nil && time.Now().Before(c.sessionExpiry) { return nil } // Make sure we have a valid auth token if err := c.ensureAuth(); err != nil { return fmt.Errorf("authentication failed while creating session: %w", err) } sessionPayload := map[string]interface{}{ "device": map[string]interface{}{ "hardware_id": c.hardwareID, "metadata": map[string]interface{}{ "api_version": apiVersion, "device_model": "ring-client-go", }, "os": "android", }, } body, err := json.Marshal(sessionPayload) if err != nil { return fmt.Errorf("failed to marshal session request: %w", err) } req, err := http.NewRequest("POST", ClientAPI("session"), bytes.NewReader(body)) if err != nil { return err } req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") req.Header.Set("Authorization", "Bearer "+c.authToken.AccessToken) req.Header.Set("hardware_id", c.hardwareID) req.Header.Set("User-Agent", "android:com.ringapp") resp, err := c.httpClient.Do(req) if err != nil { return err } defer resp.Body.Close() if resp.StatusCode < 200 || resp.StatusCode >= 300 { respBody, _ := io.ReadAll(resp.Body) return fmt.Errorf("session request failed with status %d: %s", resp.StatusCode, string(respBody)) } var sessionResp SessionResponse if err := json.NewDecoder(resp.Body).Decode(&sessionResp); err != nil { return fmt.Errorf("failed to decode session response: %w", err) } c.session = &sessionResp c.sessionExpiry = time.Now().Add(sessionValidTime) // Aktualisiere den gecachten Client cacheMutex.Lock() clientCache[c.cacheKey] = c cacheMutex.Unlock() return nil } func (c *RingApi) ensureAuth() error { c.authMutex.Lock() defer c.authMutex.Unlock() // If token exists and is not expired, use it if c.authToken != nil && time.Now().Before(c.tokenExpiry) { return nil } var grantData = map[string]string{ "grant_type": "refresh_token", "refresh_token": c.authConfig.RT, } // Add common fields grantData["client_id"] = "ring_official_android" grantData["scope"] = "client" // Make auth request body, err := json.Marshal(grantData) if err != nil { return fmt.Errorf("failed to marshal auth request: %w", err) } req, err := http.NewRequest("POST", oauthURL, bytes.NewReader(body)) if err != nil { return fmt.Errorf("failed to create auth request: %w", err) } req.Header.Set("Content-Type", "application/json") req.Header.Set("Accept", "application/json") req.Header.Set("hardware_id", c.hardwareID) req.Header.Set("User-Agent", "android:com.ringapp") req.Header.Set("2fa-support", "true") resp, err := c.httpClient.Do(req) if err != nil { return fmt.Errorf("auth request failed: %w", err) } defer resp.Body.Close() if resp.StatusCode == http.StatusPreconditionFailed { return fmt.Errorf("2FA required. Please see documentation for handling 2FA") } if resp.StatusCode != http.StatusOK { body, _ := io.ReadAll(resp.Body) return fmt.Errorf("auth request failed with status %d: %s", resp.StatusCode, string(body)) } var authResp AuthTokenResponse if err := json.NewDecoder(resp.Body).Decode(&authResp); err != nil { return fmt.Errorf("failed to decode auth response: %w", err) } // Update auth config and refresh token c.authToken = &authResp c.authConfig = &AuthConfig{ RT: authResp.RefreshToken, HID: c.hardwareID, } // Set token expiry (1 minute before actual expiry) expiresIn := time.Duration(authResp.ExpiresIn-60) * time.Second c.tokenExpiry = time.Now().Add(expiresIn) // Encode and notify about new refresh token if c.onTokenRefresh != nil { newRefreshToken := encodeAuthConfig(c.authConfig) c.onTokenRefresh(newRefreshToken) } // Refreshn the token in the client c.RefreshToken = encodeAuthConfig(c.authConfig) // Refresh the cached client cacheMutex.Lock() clientCache[c.cacheKey] = c cacheMutex.Unlock() return nil } func parseAuthConfig(refreshToken string) (*AuthConfig, error) { decoded, err := base64.StdEncoding.DecodeString(refreshToken) if err != nil { return nil, err } var config AuthConfig if err := json.Unmarshal(decoded, &config); err != nil { // Handle legacy format where refresh token is the raw token return &AuthConfig{RT: refreshToken}, nil } return &config, nil } func encodeAuthConfig(config *AuthConfig) string { jsonBytes, _ := json.Marshal(config) return base64.StdEncoding.EncodeToString(jsonBytes) } func generateHardwareID() string { h := sha256.New() h.Write([]byte("ring-client-go2rtc")) return hex.EncodeToString(h.Sum(nil)[:16]) } func interfaceSlice(slice interface{}) []CameraData { s := reflect.ValueOf(slice) if s.Kind() != reflect.Slice { return nil } ret := make([]CameraData, s.Len()) for i := 0; i < s.Len(); i++ { if camera, ok := s.Index(i).Interface().(CameraData); ok { ret[i] = camera } } return ret } ================================================ FILE: pkg/ring/client.go ================================================ package ring import ( "encoding/json" "errors" "fmt" "net/url" "strconv" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webrtc" "github.com/google/uuid" pion "github.com/pion/webrtc/v4" ) type Client struct { api *RingApi wsClient *WSClient prod core.Producer cameraID int dialogID string connected core.Waiter closed bool } func Dial(rawURL string) (*Client, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } query := u.Query() encodedToken := query.Get("refresh_token") cameraID := query.Get("camera_id") deviceID := query.Get("device_id") _, isSnapshot := query["snapshot"] if encodedToken == "" || deviceID == "" || cameraID == "" { return nil, errors.New("ring: wrong query") } client := &Client{ dialogID: uuid.NewString(), } client.cameraID, err = strconv.Atoi(cameraID) if err != nil { return nil, fmt.Errorf("ring: invalid camera_id: %w", err) } refreshToken, err := url.QueryUnescape(encodedToken) if err != nil { return nil, fmt.Errorf("ring: invalid refresh token encoding: %w", err) } client.api, err = NewRestClient(RefreshTokenAuth{RefreshToken: refreshToken}, nil) if err != nil { return nil, err } // Snapshot Flow if isSnapshot { client.prod = NewSnapshotProducer(client.api, client.cameraID) return client, nil } client.wsClient, err = StartWebsocket(client.cameraID, client.api) if err != nil { client.Stop() return nil, err } // Create Peer Connection conf := pion.Configuration{ ICEServers: []pion.ICEServer{ {URLs: []string{ "stun:stun.kinesisvideo.us-east-1.amazonaws.com:443", "stun:stun.kinesisvideo.us-east-2.amazonaws.com:443", "stun:stun.kinesisvideo.us-west-2.amazonaws.com:443", "stun:stun.l.google.com:19302", "stun:stun1.l.google.com:19302", "stun:stun2.l.google.com:19302", "stun:stun3.l.google.com:19302", "stun:stun4.l.google.com:19302", }}, }, ICETransportPolicy: pion.ICETransportPolicyAll, BundlePolicy: pion.BundlePolicyBalanced, } api, err := webrtc.NewAPI() if err != nil { client.Stop() return nil, err } pc, err := api.NewPeerConnection(conf) if err != nil { client.Stop() return nil, err } // protect from sending ICE candidate before Offer var sendOffer core.Waiter // protect from blocking on errors defer sendOffer.Done(nil) prod := webrtc.NewConn(pc) prod.FormatName = "ring/webrtc" prod.Mode = core.ModeActiveProducer prod.Protocol = "ws" prod.URL = rawURL client.wsClient.onMessage = func(msg WSMessage) { client.onWSMessage(msg) } client.wsClient.onError = func(err error) { // fmt.Printf("ring: error: %s\n", err.Error()) client.Stop() client.connected.Done(err) } client.wsClient.onClose = func() { // fmt.Println("ring: disconnect") client.Stop() client.connected.Done(errors.New("ring: disconnect")) } prod.Listen(func(msg any) { switch msg := msg.(type) { case *pion.ICECandidate: _ = sendOffer.Wait() iceCandidate := msg.ToJSON() // skip empty ICE candidates if iceCandidate.Candidate == "" { return } icePayload := map[string]interface{}{ "ice": iceCandidate.Candidate, "mlineindex": iceCandidate.SDPMLineIndex, } if err = client.wsClient.sendSessionMessage("ice", icePayload); err != nil { client.connected.Done(err) return } case pion.PeerConnectionState: switch msg { case pion.PeerConnectionStateNew: break case pion.PeerConnectionStateConnecting: break case pion.PeerConnectionStateConnected: client.connected.Done(nil) default: client.Stop() client.connected.Done(errors.New("ring: " + msg.String())) } } }) client.prod = prod // Setup media configuration medias := []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionSendRecv, Codecs: []*core.Codec{ { Name: "opus", ClockRate: 48000, Channels: 2, }, }, }, { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ { Name: "H264", ClockRate: 90000, }, }, }, } // Create offer offer, err := prod.CreateOffer(medias) if err != nil { client.Stop() return nil, err } // Send offer offerPayload := map[string]interface{}{ "stream_options": map[string]bool{ "audio_enabled": true, "video_enabled": true, }, "sdp": offer, } if err = client.wsClient.sendSessionMessage("live_view", offerPayload); err != nil { client.Stop() return nil, err } sendOffer.Done(nil) if err = client.connected.Wait(); err != nil { return nil, err } return client, nil } func (c *Client) onWSMessage(msg WSMessage) { rawMsg, _ := json.Marshal(msg) // fmt.Printf("ring: onWSMessage: %s\n", string(rawMsg)) // check if "doorbot_id" is present if _, ok := msg.Body["doorbot_id"]; !ok { return } // check if the message is from the correct doorbot doorbotID := msg.Body["doorbot_id"].(float64) if int(doorbotID) != c.cameraID { return } if msg.Method == "session_created" || msg.Method == "session_started" { if _, ok := msg.Body["session_id"]; ok && c.wsClient.sessionID == "" { c.wsClient.sessionID = msg.Body["session_id"].(string) } } // check if the message is from the correct session if _, ok := msg.Body["session_id"]; ok { if msg.Body["session_id"].(string) != c.wsClient.sessionID { return } } switch msg.Method { case "sdp": if prod, ok := c.prod.(*webrtc.Conn); ok { // Get answer var msg AnswerMessage if err := json.Unmarshal(rawMsg, &msg); err != nil { c.Stop() c.connected.Done(err) return } if err := prod.SetAnswer(msg.Body.SDP); err != nil { c.Stop() c.connected.Done(err) return } if err := c.wsClient.activateSession(); err != nil { c.Stop() c.connected.Done(err) return } prod.SDP = msg.Body.SDP } case "ice": if prod, ok := c.prod.(*webrtc.Conn); ok { var msg IceCandidateMessage if err := json.Unmarshal(rawMsg, &msg); err != nil { break } // Skip empty candidates if msg.Body.Ice == "" { break } if err := prod.AddCandidate(msg.Body.Ice); err != nil { c.Stop() c.connected.Done(err) return } } case "close": c.Stop() c.connected.Done(errors.New("ring: close")) case "pong": // Ignore } } func (c *Client) GetMedias() []*core.Media { return c.prod.GetMedias() } func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return c.prod.GetTrack(media, codec) } func (c *Client) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { if webrtcProd, ok := c.prod.(*webrtc.Conn); ok { if media.Kind == core.KindAudio { // Enable speaker speakerPayload := map[string]interface{}{ "stealth_mode": false, } _ = c.wsClient.sendSessionMessage("camera_options", speakerPayload) } return webrtcProd.AddTrack(media, codec, track) } return fmt.Errorf("add track not supported for snapshot") } func (c *Client) Start() error { return c.prod.Start() } func (c *Client) Stop() error { if c.closed { return nil } c.closed = true if c.prod != nil { _ = c.prod.Stop() } if c.wsClient != nil { _ = c.wsClient.Close() } return nil } func (c *Client) MarshalJSON() ([]byte, error) { return json.Marshal(c.prod) } ================================================ FILE: pkg/ring/snapshot.go ================================================ package ring import ( "fmt" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type SnapshotProducer struct { core.Connection client *RingApi cameraID int } func NewSnapshotProducer(client *RingApi, cameraID int) *SnapshotProducer { return &SnapshotProducer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "ring/snapshot", Protocol: "https", RemoteAddr: "app-snaps.ring.com", Medias: []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ { Name: core.CodecJPEG, ClockRate: 90000, PayloadType: core.PayloadTypeRAW, }, }, }, }, }, client: client, cameraID: cameraID, } } func (p *SnapshotProducer) Start() error { response, err := p.client.Request("GET", fmt.Sprintf("https://app-snaps.ring.com/snapshots/next/%d", p.cameraID), nil) if err != nil { return err } pkt := &rtp.Packet{ Header: rtp.Header{Timestamp: core.Now90000()}, Payload: response, } p.Receivers[0].WriteRTP(pkt) return nil } func (p *SnapshotProducer) Stop() error { return p.Connection.Stop() } ================================================ FILE: pkg/ring/ws.go ================================================ package ring import ( "fmt" "net/http" "net/url" "sync" "time" "github.com/google/uuid" "github.com/gorilla/websocket" ) type SessionBody struct { DoorbotID int `json:"doorbot_id"` SessionID string `json:"session_id"` } type AnswerMessage struct { Method string `json:"method"` // "sdp" Body struct { SessionBody SDP string `json:"sdp"` Type string `json:"type"` // "answer" } `json:"body"` } type IceCandidateMessage struct { Method string `json:"method"` // "ice" Body struct { SessionBody Ice string `json:"ice"` MLineIndex int `json:"mlineindex"` } `json:"body"` } type SessionMessage struct { Method string `json:"method"` // "session_created" or "session_started" Body SessionBody `json:"body"` } type PongMessage struct { Method string `json:"method"` // "pong" Body SessionBody `json:"body"` } type NotificationMessage struct { Method string `json:"method"` // "notification" Body struct { SessionBody IsOK bool `json:"is_ok"` Text string `json:"text"` } `json:"body"` } type StreamInfoMessage struct { Method string `json:"method"` // "stream_info" Body struct { SessionBody Transcoding bool `json:"transcoding"` TranscodingReason string `json:"transcoding_reason"` } `json:"body"` } type CloseRequest struct { Method string `json:"method"` // "close" Body struct { SessionBody Reason struct { Code int `json:"code"` Text string `json:"text"` } `json:"reason"` } `json:"body"` } type WSMessage struct { Method string `json:"method"` Body map[string]any `json:"body"` } type WSClient struct { ws *websocket.Conn api *RingApi wsMutex sync.Mutex cameraID int dialogID string sessionID string onMessage func(msg WSMessage) onError func(err error) onClose func() closed chan struct{} } const ( CloseReasonNormalClose = 0 CloseReasonAuthenticationFailed = 5 CloseReasonTimeout = 6 ) func StartWebsocket(cameraID int, api *RingApi) (*WSClient, error) { client := &WSClient{ api: api, cameraID: cameraID, dialogID: uuid.NewString(), closed: make(chan struct{}), } ticket, err := client.api.GetSocketTicket() if err != nil { return nil, err } url := fmt.Sprintf("wss://api.prod.signalling.ring.devices.a2z.com/ws?api_version=4.0&auth_type=ring_solutions&client_id=ring_site-%s&token=%s", uuid.NewString(), url.QueryEscape(ticket.Ticket)) httpHeader := http.Header{} httpHeader.Set("User-Agent", "android:com.ringapp") client.ws, _, err = websocket.DefaultDialer.Dial(url, httpHeader) if err != nil { return nil, err } client.ws.SetCloseHandler(func(code int, text string) error { client.onWsClose() return nil }) go client.startPingLoop() go client.startMessageLoop() return client, nil } func (c *WSClient) Close() error { select { case <-c.closed: return nil default: close(c.closed) } closePayload := map[string]interface{}{ "reason": map[string]interface{}{ "code": CloseReasonNormalClose, "text": "", }, } _ = c.sendSessionMessage("close", closePayload) return c.ws.Close() } func (c *WSClient) startPingLoop() { ticker := time.NewTicker(5 * time.Second) defer ticker.Stop() for { select { case <-c.closed: return case <-ticker.C: if err := c.sendSessionMessage("ping", nil); err != nil { return } } } } func (c *WSClient) startMessageLoop() { for { select { case <-c.closed: return default: var res WSMessage if err := c.ws.ReadJSON(&res); err != nil { select { case <-c.closed: // Ignore error if closed default: c.onWsError(err) } return } c.onWsMessage(res) } } } func (c *WSClient) activateSession() error { if err := c.sendSessionMessage("activate_session", nil); err != nil { return err } streamPayload := map[string]interface{}{ "audio_enabled": true, "video_enabled": true, } if err := c.sendSessionMessage("stream_options", streamPayload); err != nil { return err } return nil } func (c *WSClient) sendSessionMessage(method string, payload map[string]interface{}) error { select { case <-c.closed: return nil default: // continue } c.wsMutex.Lock() defer c.wsMutex.Unlock() if payload == nil { payload = make(map[string]interface{}) } payload["doorbot_id"] = c.cameraID if c.sessionID != "" { payload["session_id"] = c.sessionID } msg := map[string]interface{}{ "method": method, "dialog_id": c.dialogID, "body": payload, } // rawMsg, _ := json.Marshal(msg) // fmt.Printf("ring: sendSessionMessage: %s: %s\n", method, string(rawMsg)) if err := c.ws.WriteJSON(msg); err != nil { return err } return nil } func (c *WSClient) onWsMessage(msg WSMessage) { if c.onMessage != nil { c.onMessage(msg) } } func (c *WSClient) onWsError(err error) { if c.onError != nil { c.onError(err) } } func (c *WSClient) onWsClose() { if c.onClose != nil { c.onClose() } } ================================================ FILE: pkg/roborock/api.go ================================================ package roborock import ( "crypto/hmac" "crypto/md5" "crypto/sha256" "encoding/base64" "encoding/json" "fmt" "net/http" "net/url" "strconv" "time" "github.com/AlexxIT/go2rtc/pkg/core" ) type UserInfo struct { Token string `json:"token"` IoT struct { User string `json:"u"` Pass string `json:"s"` Hash string `json:"h"` Domain string `json:"k"` URL struct { API string `json:"a"` MQTT string `json:"m"` } `json:"r"` } `json:"rriot"` } func GetBaseURL(username string) (string, error) { u := "https://euiot.roborock.com/api/v1/getUrlByEmail?email=" + url.QueryEscape(username) req, err := http.NewRequest("POST", u, nil) if err != nil { return "", err } client := http.Client{Timeout: time.Second * 5000} res, err := client.Do(req) var v struct { Msg string `json:"msg"` Code int `json:"code"` Data struct { URL string `json:"url"` } `json:"data"` } if err = json.NewDecoder(res.Body).Decode(&v); err != nil { return "", err } if v.Code != 200 { return "", fmt.Errorf("%d: %s", v.Code, v.Msg) } return v.Data.URL, nil } func Login(baseURL, username, password string) (*UserInfo, error) { u := baseURL + "/api/v1/login?username=" + url.QueryEscape(username) + "&password=" + url.QueryEscape(password) + "&needtwostepauth=false" req, err := http.NewRequest("POST", u, nil) if err != nil { return nil, err } clientID := core.RandString(16, 64) clientID = base64.StdEncoding.EncodeToString([]byte(clientID)) req.Header.Set("header_clientid", clientID) client := http.Client{Timeout: time.Second * 5000} res, err := client.Do(req) var v struct { Msg string `json:"msg"` Code int `json:"code"` Data UserInfo `json:"data"` } if err = json.NewDecoder(res.Body).Decode(&v); err != nil { return nil, err } if v.Code != 200 { return nil, fmt.Errorf("%d: %s", v.Code, v.Msg) } return &v.Data, nil } func GetHomeID(baseURL, token string) (int, error) { req, err := http.NewRequest("GET", baseURL+"/api/v1/getHomeDetail", nil) if err != nil { return 0, err } req.Header.Set("Authorization", token) client := http.Client{Timeout: time.Second * 5000} res, err := client.Do(req) if err != nil { return 0, err } var v struct { Msg string `json:"msg"` Code int `json:"code"` Data struct { HomeID int `json:"rrHomeId"` } `json:"data"` } if err = json.NewDecoder(res.Body).Decode(&v); err != nil { return 0, err } if v.Code != 200 { return 0, fmt.Errorf("%d: %s", v.Code, v.Msg) } return v.Data.HomeID, nil } type DeviceInfo struct { DID string `json:"duid"` Name string `json:"name"` Key string `json:"localKey"` } func GetDevices(ui *UserInfo, homeID int) ([]DeviceInfo, error) { nonce := core.RandString(6, 64) ts := time.Now().Unix() path := "/user/homes/" + strconv.Itoa(homeID) mac := fmt.Sprintf( "%s:%s:%s:%d:%x::", ui.IoT.User, ui.IoT.Pass, nonce, ts, md5.Sum([]byte(path)), ) hash := hmac.New(sha256.New, []byte(ui.IoT.Hash)) hash.Write([]byte(mac)) mac = base64.StdEncoding.EncodeToString(hash.Sum(nil)) auth := fmt.Sprintf( `Hawk id="%s", s="%s", ts="%d", nonce="%s", mac="%s"`, ui.IoT.User, ui.IoT.Pass, ts, nonce, mac, ) req, err := http.NewRequest("GET", ui.IoT.URL.API+path, nil) if err != nil { return nil, err } req.Header.Set("Authorization", auth) client := http.Client{Timeout: time.Second * 5000} res, err := client.Do(req) if err != nil { return nil, err } var v struct { Result struct { Devices []DeviceInfo `json:"devices"` } `json:"result"` } if err = json.NewDecoder(res.Body).Decode(&v); err != nil { return nil, err } return v.Result.Devices, nil } ================================================ FILE: pkg/roborock/client.go ================================================ package roborock import ( "crypto/md5" "encoding/base64" "encoding/json" "errors" "fmt" "net/rpc" "net/url" "strconv" "sync" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/roborock/iot" "github.com/AlexxIT/go2rtc/pkg/webrtc" pion "github.com/pion/webrtc/v4" ) // Deprecated: should be rewritten to core.Connection type Client struct { core.Listener url string conn *webrtc.Conn iot *rpc.Client devKey string pin string devTopic string audio bool backchannel bool } func Dial(rawURL string) (*Client, error) { client := &Client{url: rawURL} if err := client.Dial(); err != nil { return nil, err } if err := client.Connect(); err != nil { return nil, err } return client, nil } func (c *Client) Dial() error { u, err := url.Parse(c.url) if err != nil { return err } if c.iot, err = iot.Dial(c.url); err != nil { return err } c.pin = u.Query().Get("pin") if c.pin != "" { c.pin = fmt.Sprintf("%x", md5.Sum([]byte(c.pin))) return c.CheckHomesecPassword() } return nil } func (c *Client) Connect() error { // 1. Check if camera ready for connection for i := 0; ; i++ { clientID, err := c.GetHomesecConnectStatus() if err != nil { return err } if clientID == "none" { break } if err = c.StopCameraPreview(clientID); err != nil { return err } if i == 5 { return errors.New("camera not ready") } time.Sleep(time.Second) } // 2. Start camera if err := c.StartCameraPreview(); err != nil { return err } // 3. Get TURN config conf := pion.Configuration{} if turn, _ := c.GetTurnServer(); turn != nil { conf.ICEServers = append(conf.ICEServers, *turn) } // 4. Create Peer Connection api, err := webrtc.NewAPI() if err != nil { return err } pc, err := api.NewPeerConnection(conf) if err != nil { return err } var connected = make(chan bool) var sendOffer sync.WaitGroup c.conn = webrtc.NewConn(pc) c.conn.FormatName = "roborock" c.conn.Mode = core.ModeActiveProducer c.conn.Protocol = "mqtt" c.conn.URL = c.url c.conn.Listen(func(msg any) { switch msg := msg.(type) { case *pion.ICECandidate: if msg != nil && msg.Component == 1 { sendOffer.Wait() _ = c.SendICEtoRobot(msg.ToJSON().Candidate, "0") } case pion.PeerConnectionState: if msg == pion.PeerConnectionStateConnecting { return } // unblocking write to channel select { case connected <- msg == pion.PeerConnectionStateConnected: default: } } }) // 5. Send Offer sendOffer.Add(1) medias := []*core.Media{ {Kind: core.KindVideo, Direction: core.DirectionRecvonly}, {Kind: core.KindAudio, Direction: core.DirectionSendRecv}, } if _, err = c.conn.CreateOffer(medias); err != nil { return err } offer := pc.LocalDescription() //log.Printf("[roborock] offer\n%s", offer.SDP) if err = c.SendSDPtoRobot(offer); err != nil { return err } sendOffer.Done() // 6. Receive answer ts := time.Now().Add(time.Second * 5) for { time.Sleep(time.Second) if desc, _ := c.GetDeviceSDP(); desc != nil { //log.Printf("[roborock] answer\n%s", desc.SDP) if err = c.conn.SetAnswer(desc.SDP); err != nil { return err } break } if time.Now().After(ts) { return errors.New("can't get device SDP") } } ticker := time.NewTicker(time.Second * 2) for { select { case <-ticker.C: // 7. Receive remote candidates if pc.ICEConnectionState() == pion.ICEConnectionStateCompleted { ticker.Stop() continue } if ice, _ := c.GetDeviceICE(); ice != nil { for _, candidate := range ice { _ = c.conn.AddCandidate(candidate) } } case ok := <-connected: // 8. Wait connected result (true or false) if !ok { return errors.New("can't connect") } return nil } } } func (c *Client) CheckHomesecPassword() (err error) { var ok bool params := `{"password":"` + c.pin + `"}` if err = c.iot.Call("check_homesec_password", params, &ok); err != nil { return } if !ok { return errors.New("wrong pin code") } return nil } func (c *Client) GetHomesecConnectStatus() (clientID string, err error) { var res []byte if err = c.iot.Call("get_homesec_connect_status", nil, &res); err != nil { return } var v struct { Status int `json:"status"` ClientID string `json:"client_id"` } if err = json.Unmarshal(res, &v); err != nil { return } return v.ClientID, nil } func (c *Client) StartCameraPreview() error { params := `{"client_id":"676f32727463","quality":"HD","password":"` + c.pin + `"}` return c.Request("start_camera_preview", params) } func (c *Client) StopCameraPreview(clientID string) error { params := `{"client_id":"` + clientID + `"}` return c.Request("stop_camera_preview", params) } func (c *Client) GetTurnServer() (turn *pion.ICEServer, err error) { var res []byte if err = c.iot.Call("get_turn_server", nil, &res); err != nil { return } var v struct { URL string `json:"url"` User string `json:"user"` Pwd string `json:"pwd"` } if err = json.Unmarshal(res, &v); err != nil { return nil, err } turn = &pion.ICEServer{ URLs: []string{v.URL}, Username: v.User, Credential: v.Pwd, } return } func (c *Client) SendSDPtoRobot(offer *pion.SessionDescription) (err error) { b, err := json.Marshal(offer) if err != nil { return } params := `{"app_sdp":"` + base64.StdEncoding.EncodeToString(b) + `"}` return c.iot.Call("send_sdp_to_robot", params, nil) } func (c *Client) SendICEtoRobot(candidate string, mid string) (err error) { b := []byte(`{"candidate":"` + candidate + `","sdpMLineIndex":` + mid + `,"sdpMid":"` + mid + `"}`) params := `{"app_ice":"` + base64.StdEncoding.EncodeToString(b) + `"}` return c.iot.Call("send_ice_to_robot", params, nil) } func (c *Client) GetDeviceSDP() (sd *pion.SessionDescription, err error) { var res []byte if err = c.iot.Call("get_device_sdp", nil, &res); err != nil { return } if string(res) == `{"dev_sdp":"retry"}` { return nil, nil } var v struct { SDP []byte `json:"dev_sdp"` } if err = json.Unmarshal(res, &v); err != nil { return nil, err } sd = &pion.SessionDescription{} if err = json.Unmarshal(v.SDP, sd); err != nil { return nil, err } return } func (c *Client) GetDeviceICE() (ice []string, err error) { var res []byte if err = c.iot.Call("get_device_ice", nil, &res); err != nil { return } if string(res) == `{"dev_ice":"retry"}` { return nil, nil } var v struct { ICE [][]byte `json:"dev_ice"` } if err = json.Unmarshal(res, &v); err != nil { return } for _, b := range v.ICE { init := pion.ICECandidateInit{} if err = json.Unmarshal(b, &init); err != nil { return } ice = append(ice, init.Candidate) } return } func (c *Client) StartVoiceChat() error { // record - audio from robot, play - audio to robot? params := fmt.Sprintf(`{"record":%t,"play":%t}`, c.audio, c.backchannel) return c.Request("start_voice_chat", params) } func (c *Client) SwitchVideoQuality(hd bool) error { if hd { return c.Request("switch_video_quality", `{"quality":"HD"}`) } else { return c.Request("switch_video_quality", `{"quality":"SD"}`) } } func (c *Client) SetVoiceChatVolume(volume int) error { params := `{"volume":` + strconv.Itoa(volume) + `}` return c.Request("set_voice_chat_volume", params) } func (c *Client) EnableHomesecVoice(enable bool) error { if enable { return c.Request("enable_homesec_voice", `{"enable":true}`) } else { return c.Request("enable_homesec_voice", `{"enable":false}`) } } func (c *Client) Request(method string, args any) (err error) { var reply string if err = c.iot.Call(method, args, &reply); err != nil { return } if reply != `["ok"]` { return errors.New(reply) } return } ================================================ FILE: pkg/roborock/iot/client.go ================================================ package iot import ( "crypto/md5" "crypto/tls" "encoding/hex" "encoding/json" "fmt" "net" "net/rpc" "net/url" "time" "github.com/AlexxIT/go2rtc/pkg/mqtt" ) type Codec struct { mqtt *mqtt.Client devTopic string devKey string body json.RawMessage } type dps struct { Dps struct { Req string `json:"101,omitempty"` Res string `json:"102,omitempty"` } `json:"dps"` T uint32 `json:"t"` } type response struct { ID uint64 `json:"id"` Result json.RawMessage `json:"result"` Error struct { Code int `json:"code"` Message string `json:"message"` } `json:"error"` } func (c *Codec) WriteRequest(r *rpc.Request, v any) error { if v == nil { v = "[]" } ts := uint32(time.Now().Unix()) msg := dps{T: ts} msg.Dps.Req = fmt.Sprintf( `{"id":%d,"method":"%s","params":%s}`, r.Seq, r.ServiceMethod, v, ) payload, err := json.Marshal(msg) if err != nil { return err } //log.Printf("[roborock] send: %s", payload) payload = c.Encrypt(payload, ts, ts, ts) return c.mqtt.Publish("rr/m/i/"+c.devTopic, payload) } func (c *Codec) ReadResponseHeader(r *rpc.Response) error { for { // receive any message from MQTT _, payload, err := c.mqtt.Read() if err != nil { return err } // skip if it is not PUBLISH message if payload == nil { continue } // decrypt MQTT PUBLISH payload if payload, err = c.Decrypt(payload); err != nil { continue } // skip if we can't decrypt this payload (ex. binary payload) if payload == nil { continue } //log.Printf("[roborock] recv %s", payload) // get content from response payload: // {"t":1676871268,"dps":{"102":"{\"id\":315003,\"result\":[\"ok\"]}"}} var msg dps if err = json.Unmarshal(payload, &msg); err != nil { continue } var res response if err = json.Unmarshal([]byte(msg.Dps.Res), &res); err != nil { continue } r.Seq = res.ID if res.Error.Code != 0 { r.Error = res.Error.Message } else { c.body = res.Result } return nil } } func (c *Codec) ReadResponseBody(v any) error { switch vv := v.(type) { case *[]byte: *vv = c.body case *string: *vv = string(c.body) case *bool: *vv = string(c.body) == `["ok"]` } return nil } func (c *Codec) Close() error { return c.mqtt.Close() } func Dial(rawURL string) (*rpc.Client, error) { link, err := url.Parse(rawURL) if err != nil { return nil, err } // dial to MQTT conn, err := net.DialTimeout("tcp", link.Host, time.Second*5) if err != nil { return nil, err } // process MQTT SSL conf := &tls.Config{ServerName: link.Hostname()} sconn := tls.Client(conn, conf) if err = sconn.Handshake(); err != nil { return nil, err } query := link.Query() // send MQTT login uk := md5.Sum([]byte(query.Get("u") + ":" + query.Get("k"))) sk := md5.Sum([]byte(query.Get("s") + ":" + query.Get("k"))) user := hex.EncodeToString(uk[1:5]) pass := hex.EncodeToString(sk[8:]) c := &Codec{ mqtt: mqtt.NewClient(sconn), devKey: query.Get("key"), devTopic: query.Get("u") + "/" + user + "/" + query.Get("did"), } if err = c.mqtt.Connect("com.roborock.smart:mbrriot", user, pass); err != nil { return nil, err } // subscribe on device topic if err = c.mqtt.Subscribe("rr/m/o/" + c.devTopic); err != nil { return nil, err } return rpc.NewClientWithCodec(c), nil } ================================================ FILE: pkg/roborock/iot/crypto.go ================================================ package iot import ( "crypto/aes" "crypto/md5" "encoding/binary" "errors" "hash/crc32" ) // key - convert timestamp to key func (c *Codec) key(timestamp uint32) []byte { const salt = "TXdfu$jyZ#TZHsg4" key := md5.Sum([]byte(encodeTimestamp(timestamp) + c.devKey + salt)) return key[:] } func (c *Codec) Decrypt(cipherText []byte) ([]byte, error) { if len(cipherText) < 32 || string(cipherText[:3]) != "1.0" { return nil, errors.New("wrong message prefix") } i := len(cipherText) - 4 if binary.BigEndian.Uint32(cipherText[i:]) != crc32.ChecksumIEEE(cipherText[:i]) { return nil, errors.New("wrong message checksum") } if proto := binary.BigEndian.Uint16(cipherText[15:]); proto != 102 { return nil, nil } timestamp := binary.BigEndian.Uint32(cipherText[11:]) return decryptECB(cipherText[19:i], c.key(timestamp)), nil } func (c *Codec) Encrypt(plainText []byte, seq, random, timestamp uint32) []byte { const proto = 101 cipherText := encryptECB(plainText, c.key(timestamp)) size := uint16(len(cipherText)) msg := make([]byte, 23+size) copy(msg, "1.0") binary.BigEndian.PutUint32(msg[3:], seq) binary.BigEndian.PutUint32(msg[7:], random) binary.BigEndian.PutUint32(msg[11:], timestamp) binary.BigEndian.PutUint16(msg[15:], proto) binary.BigEndian.PutUint16(msg[17:], size) copy(msg[19:], cipherText) crc := crc32.ChecksumIEEE(msg[:19+size]) binary.BigEndian.PutUint32(msg[19+size:], crc) return msg } func encodeTimestamp(i uint32) string { const hextable = "0123456789abcdef" b := []byte{ hextable[i>>8&0xF], hextable[i>>4&0xF], hextable[i>>16&0xF], hextable[i&0xF], hextable[i>>24&0xF], hextable[i>>20&0xF], hextable[i>>28&0xF], hextable[i>>12&0xF], } return string(b) } func pad(plainText []byte, blockSize int) []byte { b0 := byte(blockSize - len(plainText)%blockSize) for i := byte(0); i < b0; i++ { plainText = append(plainText, b0) } return plainText } func unpad(paddedText []byte) []byte { padSize := int(paddedText[len(paddedText)-1]) return paddedText[:len(paddedText)-padSize] } func encryptECB(plainText, key []byte) []byte { block, err := aes.NewCipher(key) if err != nil { panic(err) } blockSize := block.BlockSize() plainText = pad(plainText, blockSize) cipherText := plainText for len(plainText) > 0 { block.Encrypt(plainText, plainText) plainText = plainText[blockSize:] } return cipherText } func decryptECB(cipherText, key []byte) []byte { block, err := aes.NewCipher(key) if err != nil { panic(err) } blockSize := block.BlockSize() paddedText := cipherText for len(cipherText) > 0 { block.Decrypt(cipherText, cipherText) cipherText = cipherText[blockSize:] } return unpad(paddedText) } ================================================ FILE: pkg/roborock/producer.go ================================================ package roborock import ( "github.com/AlexxIT/go2rtc/pkg/core" ) func (c *Client) GetMedias() []*core.Media { return c.conn.GetMedias() } func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { if media.Kind == core.KindAudio { c.audio = true } return c.conn.GetTrack(media, codec) } func (c *Client) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { c.backchannel = true return c.conn.AddTrack(media, codec, track) } func (c *Client) Start() error { if c.audio || c.backchannel { if err := c.StartVoiceChat(); err != nil { return err } if c.backchannel { if err := c.SetVoiceChatVolume(80); err != nil { return err } } } return c.conn.Start() } func (c *Client) Stop() error { _ = c.iot.Close() return c.conn.Stop() } func (c *Client) MarshalJSON() ([]byte, error) { return c.conn.MarshalJSON() } ================================================ FILE: pkg/rtmp/README.md ================================================ ## Tests - go2rtc rtmp client => Reolink - go2rtc rtmp server <= Dahua - go2rtc rtmp publish => YouTube - go2rtc rtmp publish => Telegram ## Logs ``` request []interface {}{"connect", 1, map[string]interface {}{"app":"s", "flashVer":"FMLE/3.0 (compatible; FMSc/1.0)", "tcUrl":"rtmps://xxx.rtmp.t.me/s/xxxxx"}} response []interface {}{"_result", 1, map[string]interface {}{"capabilities":31, "fmsVer":"FMS/3,0,1,123"}, map[string]interface {}{"code":"NetConnection.Connect.Success", "description":"Connection succeeded.", "level":"status", "objectEncoding":0}} request []interface {}{"releaseStream", 2, interface {}(nil), "xxxxx"} request []interface {}{"FCPublish", 3, interface {}(nil), "xxxxx"} request []interface {}{"createStream", 4, interface {}(nil)} response []interface {}{"_result", 2, interface {}(nil)} response []interface {}{"_result", 4, interface {}(nil), 1} request []interface {}{"publish", 5, interface {}(nil), "xxxxx", "live"} response []interface {}{"onStatus", 0, interface {}(nil), map[string]interface {}{"code":"NetStream.Publish.Start", "description":"xxxxx is now published", "detail":"xxxxx", "level":"status"}} ``` ## Useful links - https://en.wikipedia.org/wiki/Flash_Video - https://en.wikipedia.org/wiki/Real-Time_Messaging_Protocol - https://rtmp.veriskope.com/pdf/rtmp_specification_1.0.pdf - https://rtmp.veriskope.com/docs/spec/ ================================================ FILE: pkg/rtmp/client.go ================================================ package rtmp import ( "bufio" "io" "net" "net/url" "strings" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/flv" "github.com/AlexxIT/go2rtc/pkg/tcp" ) func DialPlay(rawURL string) (*flv.Producer, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } conn, err := tcp.Dial(u, core.ConnDialTimeout) if err != nil { return nil, err } client, err := NewClient(conn, u) if err != nil { return nil, err } if err = client.play(); err != nil { return nil, err } return client.Producer() } func DialPublish(rawURL string, cons *flv.Consumer) (io.Writer, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } conn, err := tcp.Dial(u, core.ConnDialTimeout) if err != nil { return nil, err } client, err := NewClient(conn, u) if err != nil { return nil, err } if err = client.publish(); err != nil { return nil, err } cons.FormatName = "rtmp" cons.Protocol = "rtmp" cons.RemoteAddr = conn.RemoteAddr().String() cons.URL = rawURL return client, nil } func NewClient(conn net.Conn, u *url.URL) (*Conn, error) { c := &Conn{ url: u.String(), conn: conn, rd: bufio.NewReaderSize(conn, core.BufferSize), wr: conn, chunks: map[uint8]*chunk{}, rdPacketSize: 128, wrPacketSize: 4096, // OBS - 4096, Reolink - 4096 } if args := strings.Split(u.Path, "/"); len(args) >= 2 { c.App = args[1] if len(args) >= 3 { c.Stream = args[2] if u.RawQuery != "" { c.Stream += "?" + u.RawQuery } } } if err := c.clienHandshake(); err != nil { return nil, err } if err := c.writePacketSize(); err != nil { return nil, err } return c, nil } func (c *Conn) clienHandshake() error { // simple handshake without real random and check response b := make([]byte, 1+1536) b[0] = 0x03 // write C0+C1 if _, err := c.conn.Write(b); err != nil { return err } // read S0+S1 if _, err := io.ReadFull(c.rd, b); err != nil { return err } // write S1 if _, err := c.conn.Write(b[1:]); err != nil { return err } // read C1, skip check if _, err := io.ReadFull(c.rd, b[1:]); err != nil { return err } return nil } func (c *Conn) play() error { if err := c.writeConnect(); err != nil { return err } if err := c.writeCreateStream(); err != nil { return err } if err := c.writePlay(); err != nil { return err } return nil } func (c *Conn) publish() error { if err := c.writeConnect(); err != nil { return err } if err := c.writeReleaseStream(); err != nil { return err } if err := c.writeCreateStream(); err != nil { return err } if err := c.writePublish(); err != nil { return err } go func() { for { _, _, _, err := c.readMessage() //log.Printf("!!! %d %d %.30x", msgType, timeMS, b) if err != nil { return } } }() return nil } ================================================ FILE: pkg/rtmp/conn.go ================================================ package rtmp import ( "encoding/binary" "fmt" "io" "net" "strings" "sync" "github.com/AlexxIT/go2rtc/pkg/flv/amf" ) const ( TypeSetPacketSize = 1 TypeServerBandwidth = 5 TypeClientBandwidth = 6 TypeAudio = 8 TypeVideo = 9 TypeData = 18 TypeCommand = 20 ) type Conn struct { App string Stream string Intent string rdPacketSize uint32 wrPacketSize uint32 chunks map[byte]*chunk streamID byte url string conn net.Conn rd io.Reader wr io.Writer rdBuf []byte wrBuf []byte mu sync.Mutex } func (c *Conn) Close() error { return c.conn.Close() } func (c *Conn) readResponse(wait func(items []any) bool) ([]any, error) { for { msgType, _, b, err := c.readMessage() if err != nil { return nil, err } //log.Printf("[rtmp] type=%d data=%s", msgType, b) switch msgType { case TypeSetPacketSize: c.rdPacketSize = binary.BigEndian.Uint32(b) case TypeCommand: items, _ := amf.NewReader(b).ReadItems() if wait(items) { return items, nil } } } } type chunk struct { conn *Conn rawTime uint32 dataSize uint32 tagType byte streamID uint32 timeMS uint32 } func (c *chunk) readHeader(typ byte) error { switch typ { case 0: // 12 byte header (full header) b, err := c.conn.readSize(11) if err != nil { return err } c.rawTime = Uint24(b) c.dataSize = Uint24(b[3:]) c.tagType = b[6] c.streamID = binary.LittleEndian.Uint32(b[7:]) c.timeMS = c.readExtendedTime() case 1: // 8 bytes - like type b00, not including message ID (4 last bytes) b, err := c.conn.readSize(7) if err != nil { return err } c.rawTime = Uint24(b) c.dataSize = Uint24(b[3:]) // msgdatalen c.tagType = b[6] // msgtypeid c.timeMS += c.readExtendedTime() case 2: // 4 bytes - Basic Header and timestamp (3 bytes) are included b, err := c.conn.readSize(3) if err != nil { return err } c.rawTime = Uint24(b) // timestamp c.timeMS += c.readExtendedTime() case 3: // 1 byte - only the Basic Header is included // use here hdr from previous msg with same session ID (sid) } return nil } func (c *chunk) readExtendedTime() uint32 { if c.rawTime == 0xFFFFFF { if b, err := c.conn.readSize(4); err == nil { return binary.BigEndian.Uint32(b) } } return c.rawTime } //var ErrNotImplemented = errors.New("rtmp: not implemented") func (c *Conn) readMessage() (byte, uint32, []byte, error) { b, err := c.readSize(1) // doesn't support big chunkID!!! if err != nil { return 0, 0, nil, err } hdrType := b[0] >> 6 chunkID := b[0] & 0b111111 // storing header information for support header type 3 ch, ok := c.chunks[chunkID] if !ok { ch = &chunk{conn: c} c.chunks[chunkID] = ch } if err = ch.readHeader(hdrType); err != nil { return 0, 0, nil, err } //log.Printf("[rtmp] hdr=%d chunkID=%d timeMS=%d size=%d tagType=%d streamID=%d", hdrType, chunkID, ch.timeMS, ch.dataSize, ch.tagType, ch.streamID) // 1. Response zero size if ch.dataSize == 0 { return ch.tagType, ch.timeMS, nil, nil } data := make([]byte, ch.dataSize) // 2. Response small packet if ch.dataSize <= c.rdPacketSize { if _, err = io.ReadFull(c.rd, data); err != nil { return 0, 0, nil, err } return ch.tagType, ch.timeMS, data, nil } // 3. Response big packet var i0 uint32 for i1 := c.rdPacketSize; i1 < ch.dataSize; i1 += c.rdPacketSize { if _, err = io.ReadFull(c.rd, data[i0:i1]); err != nil { return 0, 0, nil, err } // hopefully this will be hdrType=3 with same chunkID if _, err = c.readSize(1); err != nil { return 0, 0, nil, err } _ = ch.readExtendedTime() i0 = i1 } if _, err = io.ReadFull(c.rd, data[i0:]); err != nil { return 0, 0, nil, err } return ch.tagType, ch.timeMS, data, nil } func (c *Conn) writeMessage(chunkID, tagType byte, timeMS uint32, payload []byte) error { c.mu.Lock() c.resetBuffer() b := payload size := uint32(len(b)) if size > c.wrPacketSize { c.appendType0(chunkID, tagType, timeMS, size, b[:c.wrPacketSize]) for { b = b[c.wrPacketSize:] if uint32(len(b)) > c.wrPacketSize { c.appendType3(chunkID, b[:c.wrPacketSize]) } else { c.appendType3(chunkID, b) break } } } else { c.appendType0(chunkID, tagType, timeMS, size, b) } //log.Printf("%d %2d %5d %6d %.32x", chunkID, tagType, timeMS, size, payload) _, err := c.wr.Write(c.wrBuf) c.mu.Unlock() return err } func (c *Conn) resetBuffer() { c.wrBuf = c.wrBuf[:0] } func (c *Conn) appendType0(chunkID, tagType byte, timeMS, size uint32, payload []byte) { // TODO: timeMS more than 24 bit c.wrBuf = append(c.wrBuf, chunkID, byte(timeMS>>16), byte(timeMS>>8), byte(timeMS), byte(size>>16), byte(size>>8), byte(size), tagType, c.streamID, 0, 0, 0, // little endian streamID ) c.wrBuf = append(c.wrBuf, payload...) } func (c *Conn) appendType3(chunkID byte, payload []byte) { c.wrBuf = append(c.wrBuf, 3<<6|chunkID) c.wrBuf = append(c.wrBuf, payload...) } func (c *Conn) writePacketSize() error { b := binary.BigEndian.AppendUint32(nil, c.wrPacketSize) return c.writeMessage(2, TypeSetPacketSize, 0, b) } func (c *Conn) writeConnect() error { b := amf.EncodeItems("connect", 1, map[string]any{ "app": c.App, "flashVer": "FMLE/3.0 (compatible; FMSc/1.0)", "tcUrl": c.url, }) if err := c.writeMessage(3, TypeCommand, 0, b); err != nil { return err } v, err := c.readResponse(func(items []any) bool { return len(items) >= 3 && items[0] == "_result" && items[1] == float64(1) }) if err != nil { return err } code := getString(v, 3, "code") if code != "NetConnection.Connect.Success" { return fmt.Errorf("rtmp: wrong response %#v", v) } return nil } func (c *Conn) writeReleaseStream() error { b := amf.EncodeItems("releaseStream", 2, nil, c.Stream) if err := c.writeMessage(3, TypeCommand, 0, b); err != nil { return err } b = amf.EncodeItems("FCPublish", 3, nil, c.Stream) if err := c.writeMessage(3, TypeCommand, 0, b); err != nil { return err } return nil } func (c *Conn) writeCreateStream() error { b := amf.EncodeItems("createStream", 4, nil) if err := c.writeMessage(3, TypeCommand, 0, b); err != nil { return err } v, err := c.readResponse(func(items []any) bool { return len(items) >= 3 && items[0] == "_result" && items[1] == float64(4) }) if err != nil { return err } if len(v) == 4 { if f, ok := v[3].(float64); ok { c.streamID = byte(f) return nil } } return fmt.Errorf("rtmp: wrong response %#v", v) } func (c *Conn) writePublish() error { b := amf.EncodeItems("publish", 5, nil, c.Stream, "live") if err := c.writeMessage(3, TypeCommand, 0, b); err != nil { return err } // YouTube can response with "onBWDone 0" v, err := c.readResponse(func(items []any) bool { return len(items) >= 3 && items[0] == "onStatus" }) if err != nil { return nil } code := getString(v, 3, "code") if code != "NetStream.Publish.Start" { return fmt.Errorf("rtmp: wrong response %#v", v) } return nil } func (c *Conn) writePlay() error { b := amf.EncodeItems("play", 5, nil, c.Stream) if err := c.writeMessage(3, TypeCommand, 0, b); err != nil { return err } // Reolink response with ID=0, other software respose with ID=5 v, err := c.readResponse(func(items []any) bool { return len(items) >= 3 && items[0] == "onStatus" }) if err != nil { return nil } code := getString(v, 3, "code") if !strings.HasPrefix(code, "NetStream.Play.") { return fmt.Errorf("rtmp: wrong response %#v", v) } return nil } func (c *Conn) readSize(n uint32) ([]byte, error) { b := make([]byte, n) if _, err := io.ReadFull(c.rd, b); err != nil { return nil, err } return b, nil } func PutUint24(b []byte, v uint32) { _ = b[2] b[0] = byte(v >> 16) b[1] = byte(v >> 8) b[2] = byte(v) } func Uint24(b []byte) uint32 { _ = b[2] return uint32(b[0])<<16 | uint32(b[1])<<8 | uint32(b[2]) } func getString(v []any, i int, key string) string { if len(v) <= i { return "" } if v, ok := v[i].(map[string]any); ok { if s, ok := v[key].(string); ok { return s } } return "" } ================================================ FILE: pkg/rtmp/flv.go ================================================ package rtmp import ( "github.com/AlexxIT/go2rtc/pkg/flv" ) func (c *Conn) Producer() (*flv.Producer, error) { c.rdBuf = []byte{ 'F', 'L', 'V', // signature 1, // version 0, // flags (has video/audio) 0, 0, 0, 9, // header size } prod, err := flv.Open(c) if err != nil { return nil, err } prod.FormatName = "rtmp" prod.Protocol = "rtmp" prod.RemoteAddr = c.conn.RemoteAddr().String() prod.URL = c.url return prod, nil } // Read - convert RTMP to FLV format func (c *Conn) Read(p []byte) (n int, err error) { // 1. Check temporary tempbuffer if len(c.rdBuf) == 0 { msgType, timeMS, payload, err2 := c.readMessage() if err2 != nil { return 0, err2 } // previous tag size (4 byte) + header (11 byte) + payload n = 4 + 11 + len(payload) // 2. Check if the message fits in the buffer if n <= len(p) { encodeFLV(p, msgType, timeMS, payload) return } // 3. Put the message into a temporary buffer c.rdBuf = make([]byte, n) encodeFLV(c.rdBuf, msgType, timeMS, payload) } // 4. Send temporary buffer n = copy(p, c.rdBuf) c.rdBuf = c.rdBuf[n:] return } func encodeFLV(b []byte, msgType byte, time uint32, payload []byte) { _ = b[4+11] b[0] = 0 b[1] = 0 b[2] = 0 b[3] = 0 b[4+0] = msgType PutUint24(b[4+1:], uint32(len(payload))) PutUint24(b[4+4:], time) b[4+7] = byte(time >> 24) copy(b[4+11:], payload) } // Write - convert FLV format to RTMP format func (c *Conn) Write(p []byte) (n int, err error) { n = len(p) if p[0] == 'F' { p = p[9+4:] // skip first msg with FLV header for len(p) > 0 { size := 11 + uint16(p[2])<<8 + uint16(p[3]) + 4 if _, err = c.Write(p[:size]); err != nil { return 0, err } p = p[size:] } return } // decode FLV: 11 bytes header + payload + 4 byte size tagType := p[0] timeMS := uint32(p[4])<<16 | uint32(p[5])<<8 | uint32(p[6]) | uint32(p[7])<<24 payload := p[11 : len(p)-4] err = c.writeMessage(4, tagType, timeMS, payload) return } ================================================ FILE: pkg/rtmp/server.go ================================================ package rtmp import ( "bufio" "crypto/rand" "encoding/binary" "errors" "fmt" "io" "net" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/flv/amf" ) func NewServer(conn net.Conn) (*Conn, error) { c := &Conn{ conn: conn, rd: bufio.NewReaderSize(conn, core.BufferSize), wr: conn, chunks: map[uint8]*chunk{}, rdPacketSize: 128, wrPacketSize: 4096, } if err := c.serverHandshake(); err != nil { return nil, err } if err := c.writePacketSize(); err != nil { return nil, err } return c, nil } func (c *Conn) serverHandshake() error { // based on https://rtmp.veriskope.com/docs/spec/ _ = c.conn.SetDeadline(time.Now().Add(core.ConnDeadline)) // read C0 b := make([]byte, 1) if _, err := io.ReadFull(c.rd, b); err != nil { return err } if b[0] != 3 { return errors.New("rtmp: wrong handshake") } // write S0 if _, err := c.conn.Write([]byte{3}); err != nil { return err } b = make([]byte, 1536) // write S1 tsS1 := nowMS() binary.BigEndian.PutUint32(b, tsS1) binary.BigEndian.PutUint32(b[4:], 0) _, _ = rand.Read(b[8:]) if _, err := c.conn.Write(b); err != nil { return err } // read C1 if _, err := io.ReadFull(c.rd, b); err != nil { return err } // write S2 tsS2 := nowMS() binary.BigEndian.PutUint32(b, tsS1) binary.BigEndian.PutUint32(b[4:], tsS2) if _, err := c.conn.Write(b); err != nil { return err } // read C2 if _, err := io.ReadFull(c.rd, b); err != nil { return err } _ = c.conn.SetDeadline(time.Time{}) return nil } func (c *Conn) ReadCommands() error { for { msgType, _, b, err := c.readMessage() if err != nil { return err } //log.Printf("%d %.256x", msgType, b) switch msgType { case TypeSetPacketSize: c.rdPacketSize = binary.BigEndian.Uint32(b) case TypeCommand: if err = c.acceptCommand(b); err != nil { return err } if c.Intent != "" { return nil } } } } const ( CommandConnect = "connect" CommandReleaseStream = "releaseStream" CommandFCPublish = "FCPublish" CommandCreateStream = "createStream" CommandPublish = "publish" CommandPlay = "play" ) func (c *Conn) acceptCommand(b []byte) error { items, err := amf.NewReader(b).ReadItems() if err != nil { return nil } //log.Printf("%#v", items) if len(items) < 2 { return fmt.Errorf("rtmp: read command %x", b) } cmd, ok := items[0].(string) if !ok { return fmt.Errorf("rtmp: read command %x", b) } tID, ok := items[1].(float64) // transaction ID if !ok { return fmt.Errorf("rtmp: read command %x", b) } switch cmd { case CommandConnect: if len(items) == 3 { if v, ok := items[2].(map[string]any); ok { c.App, _ = v["app"].(string) } } payload := amf.EncodeItems( "_result", tID, map[string]any{"fmsVer": "FMS/3,0,1,123"}, map[string]any{"code": "NetConnection.Connect.Success"}, ) return c.writeMessage(3, TypeCommand, 0, payload) case CommandReleaseStream: // if app is empty - will use key as app if c.App == "" && len(items) == 4 { c.App, _ = items[3].(string) } payload := amf.EncodeItems("_result", tID, nil) return c.writeMessage(3, TypeCommand, 0, payload) case CommandFCPublish: // no response case CommandCreateStream: payload := amf.EncodeItems("_result", tID, nil, 1) return c.writeMessage(3, TypeCommand, 0, payload) case CommandPublish, CommandPlay: // response later c.Intent = cmd c.streamID = 1 default: println("rtmp: unknown command: " + cmd) } return nil } func (c *Conn) WriteStart() error { var code string if c.Intent == CommandPublish { code = "NetStream.Publish.Start" } else { code = "NetStream.Play.Start" } payload := amf.EncodeItems("onStatus", 0, nil, map[string]any{"code": code}) return c.writeMessage(3, TypeCommand, 0, payload) } func nowMS() uint32 { return uint32(time.Now().UnixNano() / int64(time.Millisecond)) } ================================================ FILE: pkg/rtsp/README.md ================================================ ## Useful links - https://www.kurento.org/blog/rtp-i-intro-rtp-and-sdp ================================================ FILE: pkg/rtsp/client.go ================================================ package rtsp import ( "bufio" "errors" "fmt" "net" "net/http" "net/url" "strconv" "strings" "sync" "time" "github.com/AlexxIT/go2rtc/pkg/tcp/websocket" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/tcp" ) var Timeout = time.Second * 5 func NewClient(uri string) *Conn { return &Conn{ Connection: core.Connection{ ID: core.NewID(), FormatName: "rtsp", }, uri: uri, } } func (c *Conn) Dial() (err error) { if c.URL, err = url.Parse(c.uri); err != nil { return } var conn net.Conn switch c.Transport { case "", "tcp", "udp": var timeout time.Duration if c.Timeout != 0 { timeout = time.Second * time.Duration(c.Timeout) } else { timeout = core.ConnDialTimeout } conn, err = tcp.Dial(c.URL, timeout) if c.Transport != "udp" { c.Protocol = "rtsp+tcp" } else { c.Protocol = "rtsp+udp" } default: conn, err = websocket.Dial(c.Transport) c.Protocol = "ws" } if err != nil { return } // remove UserInfo from URL c.auth = tcp.NewAuth(c.URL.User) c.URL.User = nil c.conn = conn c.reader = bufio.NewReaderSize(conn, core.BufferSize) c.session = "" c.sequence = 0 c.state = StateConn c.udpConn = nil c.udpAddr = nil c.Connection.RemoteAddr = conn.RemoteAddr().String() c.Connection.Transport = conn c.Connection.URL = c.uri return nil } // Do send WriteRequest and receive and process WriteResponse func (c *Conn) Do(req *tcp.Request) (*tcp.Response, error) { if err := c.WriteRequest(req); err != nil { return nil, err } res, err := c.ReadResponse() if err != nil { return nil, err } c.Fire(res) switch res.StatusCode { case http.StatusOK: return res, nil case http.StatusMovedPermanently, http.StatusFound: rawURL := res.Header.Get("Location") var u *url.URL if u, err = url.Parse(rawURL); err != nil { return nil, err } if u.User == nil { u.User = c.auth.UserInfo() // restore auth if we don't have it in the new URL } c.uri = u.String() // so auth will be saved on reconnect _ = c.conn.Close() if err = c.Dial(); err != nil { return nil, err } req.URL = c.URL // because path was changed return c.Do(req) case http.StatusUnauthorized: switch c.auth.Method { case tcp.AuthNone: if c.auth.ReadNone(res) { return c.Do(req) } return nil, errors.New("user/pass not provided") case tcp.AuthUnknown: if c.auth.Read(res) { return c.Do(req) } default: return nil, errors.New("wrong user/pass") } } return res, fmt.Errorf("wrong response on %s", req.Method) } func (c *Conn) Options() error { req := &tcp.Request{Method: MethodOptions, URL: c.URL} res, err := c.Do(req) if err != nil { return err } if val := res.Header.Get("Content-Base"); val != "" { c.URL, err = urlParse(val) if err != nil { return err } } return nil } func (c *Conn) Describe() error { // 5.3 Back channel connection // https://www.onvif.org/specs/stream/ONVIF-Streaming-Spec.pdf req := &tcp.Request{ Method: MethodDescribe, URL: c.URL, Header: map[string][]string{ "Accept": {"application/sdp"}, }, } if c.Backchannel { req.Header.Set("Require", "www.onvif.org/ver20/backchannel") } if c.UserAgent != "" { // this camera will answer with 401 on DESCRIBE without User-Agent // https://github.com/AlexxIT/go2rtc/issues/235 req.Header.Set("User-Agent", c.UserAgent) } res, err := c.Do(req) if err != nil { return err } if val := res.Header.Get("Content-Base"); val != "" { c.URL, err = urlParse(val) if err != nil { return err } } c.SDP = string(res.Body) // for info medias, err := UnmarshalSDP(res.Body) if err != nil { return err } if c.Media != "" { clone := make([]*core.Media, 0, len(medias)) for _, media := range medias { if strings.Contains(c.Media, media.Kind) { clone = append(clone, media) } } medias = clone } // TODO: rewrite more smart if c.Medias == nil { c.Medias = medias } else if len(c.Medias) > len(medias) { c.Medias = c.Medias[:len(medias)] } c.mode = core.ModeActiveProducer return nil } func (c *Conn) Announce() (err error) { req := &tcp.Request{ Method: MethodAnnounce, URL: c.URL, Header: map[string][]string{ "Content-Type": {"application/sdp"}, }, } req.Body, err = core.MarshalSDP(c.SessionName, c.Medias) if err != nil { return err } _, err = c.Do(req) return } func (c *Conn) Record() (err error) { req := &tcp.Request{ Method: MethodRecord, URL: c.URL, Header: map[string][]string{ "Range": {"npt=0.000-"}, }, } _, err = c.Do(req) return } func (c *Conn) SetupMedia(media *core.Media) (byte, error) { var transport string if c.Transport == "udp" { conn1, conn2, err := ListenUDPPair() if err != nil { return 0, err } c.udpConn = append(c.udpConn, conn1, conn2) port := conn1.LocalAddr().(*net.UDPAddr).Port transport = fmt.Sprintf("RTP/AVP;unicast;client_port=%d-%d", port, port+1) } else { // try to use media position as channel number for i, m := range c.Medias { if m.Equal(media) { transport = fmt.Sprintf( // i - RTP (data channel) // i+1 - RTCP (control channel) "RTP/AVP/TCP;unicast;interleaved=%d-%d", i*2, i*2+1, ) break } } } if transport == "" { return 0, fmt.Errorf("wrong media: %v", media) } rawURL := media.ID // control if !strings.Contains(rawURL, "://") { rawURL = c.URL.String() // prefix check for https://github.com/AlexxIT/go2rtc/issues/1236 if !strings.HasSuffix(rawURL, "/") && !strings.HasPrefix(media.ID, "/") { rawURL += "/" } rawURL += media.ID } trackURL, err := urlParse(rawURL) if err != nil { return 0, err } req := &tcp.Request{ Method: MethodSetup, URL: trackURL, Header: map[string][]string{ "Transport": {transport}, }, } res, err := c.Do(req) if err != nil { // some Dahua/Amcrest cameras fail here because two simultaneous // backchannel connections if c.Backchannel { c.Backchannel = false if err = c.Reconnect(); err != nil { return 0, err } return c.SetupMedia(media) } return 0, err } if c.session == "" { // Session: 7116520596809429228 // Session: 216525287999;timeout=60 if s := res.Header.Get("Session"); s != "" { if i := strings.IndexByte(s, ';'); i > 0 { c.session = s[:i] if i = strings.Index(s, "timeout="); i > 0 { c.keepalive, _ = strconv.Atoi(s[i+8:]) } } else { c.session = s } } } // Parse server response transport = res.Header.Get("Transport") if c.Transport == "udp" { channel := byte(len(c.udpConn) - 2) // Dahua: RTP/AVP/UDP;unicast;client_port=49292-49293;server_port=43670-43671;ssrc=7CB694B4 // OpenIPC: RTP/AVP/UDP;unicast;client_port=59612-59613 if s := core.Between(transport, "server_port=", ";"); s != "" { s1, s2, _ := strings.Cut(s, "-") port1 := core.Atoi(s1) port2 := core.Atoi(s2) // TODO: more smart handling empty server ports if port1 > 0 && port2 > 0 { remoteIP := c.conn.RemoteAddr().(*net.TCPAddr).IP c.udpAddr = append(c.udpAddr, &net.UDPAddr{IP: remoteIP, Port: port1}, &net.UDPAddr{IP: remoteIP, Port: port2}, ) go func() { // Try to open a hole in the NAT router (to allow incoming UDP packets) // by send a UDP packet for RTP and RTCP to the remote RTSP server. // https://github.com/FFmpeg/FFmpeg/blob/aa91ae25b88e195e6af4248e0ab30605735ca1cd/libavformat/rtpdec.c#L416-L438 _, _ = c.WriteToUDP([]byte{0x80, 0x00, 0x00, 0x00}, channel) _, _ = c.WriteToUDP([]byte{0x80, 0xC8, 0x00, 0x01}, channel+1) }() } } return channel, nil } else { // we send our `interleaved`, but camera can answer with another // Transport: RTP/AVP/TCP;unicast;interleaved=10-11;ssrc=10117CB7 // Transport: RTP/AVP/TCP;unicast;destination=192.168.1.111;source=192.168.1.222;interleaved=0 // Transport: RTP/AVP/TCP;ssrc=22345682;interleaved=0-1 // Escam Q6 has a bug: // Transport: RTP/AVP;unicast;destination=192.168.1.111;source=192.168.1.222;interleaved=0-1 s := core.Between(transport, "interleaved=", "-") i, err := strconv.Atoi(s) if err != nil { return 0, fmt.Errorf("wrong transport: %s", transport) } return byte(i), nil } } func (c *Conn) Play() (err error) { req := &tcp.Request{Method: MethodPlay, URL: c.URL} return c.WriteRequest(req) } func (c *Conn) Teardown() (err error) { // allow TEARDOWN from any state (ex. ANNOUNCE > SETUP) req := &tcp.Request{Method: MethodTeardown, URL: c.URL} return c.WriteRequest(req) } func (c *Conn) Close() error { if c.mode == core.ModeActiveProducer { _ = c.Teardown() } if c.OnClose != nil { _ = c.OnClose() } for _, conn := range c.udpConn { _ = conn.Close() } return c.conn.Close() } func (c *Conn) WriteToUDP(b []byte, channel byte) (int, error) { return c.udpConn[channel].WriteToUDP(b, c.udpAddr[channel]) } const listenUDPAttemps = 10 var listenUDPMu sync.Mutex func ListenUDPPair() (*net.UDPConn, *net.UDPConn, error) { listenUDPMu.Lock() defer listenUDPMu.Unlock() for i := 0; i < listenUDPAttemps; i++ { // Get a random even port from the OS ln1, err := net.ListenUDP("udp", &net.UDPAddr{IP: nil, Port: 0}) if err != nil { continue } var port1 = ln1.LocalAddr().(*net.UDPAddr).Port var port2 int // 11. RTP over Network and Transport Protocols (https://www.ietf.org/rfc/rfc3550.txt) // For UDP and similar protocols, // RTP SHOULD use an even destination port number and the corresponding // RTCP stream SHOULD use the next higher (odd) destination port number if port1&1 > 0 { port2 = port1 - 1 } else { port2 = port1 + 1 } ln2, err := net.ListenUDP("udp", &net.UDPAddr{IP: nil, Port: port2}) if err != nil { _ = ln1.Close() continue } if port1 < port2 { return ln1, ln2, nil } else { return ln2, ln1, nil } } return nil, nil, fmt.Errorf("can't open two UDP ports") } ================================================ FILE: pkg/rtsp/client_test.go ================================================ package rtsp import ( "net" "os" "testing" "time" "github.com/stretchr/testify/require" ) func TestTimeout(t *testing.T) { Timeout = time.Millisecond ln, err := net.Listen("tcp", "localhost:0") require.Nil(t, err) client := NewClient("rtsp://" + ln.Addr().String() + "/stream") client.Backchannel = true err = client.Dial() require.Nil(t, err) err = client.Describe() require.ErrorIs(t, err, os.ErrDeadlineExceeded) } func TestMissedControl(t *testing.T) { Timeout = time.Millisecond ln, err := net.Listen("tcp", "localhost:0") require.Nil(t, err) go func() { conn, err := ln.Accept() require.Nil(t, err) b := make([]byte, 8192) for { n, err := conn.Read(b) require.Nil(t, err) req := string(b[:n]) switch req[:4] { case "DESC": _, _ = conn.Write([]byte(`RTSP/1.0 200 OK Cseq: 1 Content-Length: 495 Content-Type: application/sdp v=0 o=- 1 1 IN IP4 0.0.0.0 s=go2rtc/1.2.0 c=IN IP4 0.0.0.0 t=0 0 m=audio 0 RTP/AVP 96 a=rtpmap:96 MPEG4-GENERIC/48000/2 a=fmtp:96 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3; config=119056E500 m=audio 0 RTP/AVP 97 a=rtpmap:97 OPUS/48000/2 a=fmtp:97 sprop-stereo=1 m=video 0 RTP/AVP 98 a=rtpmap:98 H264/90000 a=fmtp:98 packetization-mode=1; sprop-parameter-sets=Z2QAKaw0yAeAIn5cBagICAoAAAfQAAE4gdDAAjhAACOEF3lxoYAEcIAARwgu8uFA,aO48MAA=; profile-level-id=640029 `)) case "SETU": _, _ = conn.Write([]byte(`RTSP/1.0 200 OK Transport: RTP/AVP/TCP;unicast;interleaved=4-5 Cseq: 3 Session: 1 `)) default: t.Fail() } } }() client := NewClient("rtsp://" + ln.Addr().String() + "/stream") client.Backchannel = true err = client.Dial() require.Nil(t, err) err = client.Describe() require.Nil(t, err) require.Len(t, client.Medias, 3) ch, err := client.SetupMedia(client.Medias[2]) require.Nil(t, err) require.Equal(t, ch, byte(4)) } ================================================ FILE: pkg/rtsp/conn.go ================================================ package rtsp import ( "bufio" "context" "encoding/binary" "fmt" "io" "net" "net/url" "strconv" "sync" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/tcp" "github.com/pion/rtp" ) type Conn struct { core.Connection core.Listener // public Backchannel bool Media string OnClose func() error PacketSize uint16 SessionName string Timeout int Transport string // custom transport support, ex. RTSP over WebSocket URL *url.URL // internal auth *tcp.Auth conn net.Conn keepalive int mode core.Mode playOK bool playErr error reader *bufio.Reader sequence int session string uri string state State stateMu sync.Mutex udpConn []*net.UDPConn udpAddr []*net.UDPAddr } const ( ProtoRTSP = "RTSP/1.0" MethodOptions = "OPTIONS" MethodSetup = "SETUP" MethodTeardown = "TEARDOWN" MethodDescribe = "DESCRIBE" MethodPlay = "PLAY" MethodPause = "PAUSE" MethodAnnounce = "ANNOUNCE" MethodRecord = "RECORD" ) type State byte func (s State) String() string { switch s { case StateNone: return "NONE" case StateConn: return "CONN" case StateSetup: return MethodSetup case StatePlay: return MethodPlay } return strconv.Itoa(int(s)) } const ( StateNone State = iota StateConn StateSetup StatePlay ) func (c *Conn) Handle() (err error) { var timeout time.Duration switch c.mode { case core.ModeActiveProducer: var keepaliveDT time.Duration if c.keepalive > 5 { keepaliveDT = time.Duration(c.keepalive-5) * time.Second } else { keepaliveDT = 25 * time.Second } ctx, cancel := context.WithCancel(context.Background()) go c.handleKeepalive(ctx, keepaliveDT) defer cancel() if c.Timeout == 0 { // polling frames from remote RTSP Server (ex Camera) timeout = time.Second * 5 if len(c.Receivers) == 0 || c.Transport == "udp" { // if we only send audio to camera // https://github.com/AlexxIT/go2rtc/issues/659 timeout += keepaliveDT } } else { timeout = time.Second * time.Duration(c.Timeout) } case core.ModePassiveProducer: // polling frames from remote RTSP Client (ex FFmpeg) if c.Timeout == 0 { timeout = time.Second * 15 } else { timeout = time.Second * time.Duration(c.Timeout) } case core.ModePassiveConsumer: // pushing frames to remote RTSP Client (ex VLC) timeout = time.Second * 60 default: return fmt.Errorf("wrong RTSP conn mode: %d", c.mode) } for i := 0; i < len(c.udpConn); i++ { go c.handleUDPData(byte(i)) } for c.state != StateNone { ts := time.Now() _ = c.conn.SetReadDeadline(ts.Add(timeout)) if err = c.handleTCPData(); err != nil { return } } return } func (c *Conn) handleKeepalive(ctx context.Context, d time.Duration) { ticker := time.NewTicker(d) for { select { case <-ticker.C: req := &tcp.Request{Method: MethodOptions, URL: c.URL} if err := c.WriteRequest(req); err != nil { return } case <-ctx.Done(): return } } } func (c *Conn) handleUDPData(channel byte) { // TODO: handle timeouts and drop TCP connection after any error conn := c.udpConn[channel] for { // TP-Link Tapo camera has crazy 10000 bytes packet size buf := make([]byte, 10240) n, _, err := conn.ReadFromUDP(buf) if err != nil { return } if err = c.handleRawPacket(channel, buf[:n]); err != nil { return } } } func (c *Conn) handleTCPData() error { // we can read: // 1. RTP interleaved: `$` + 1B channel number + 2B size // 2. RTSP response: RTSP/1.0 200 OK // 3. RTSP request: OPTIONS ... var buf4 []byte // `$` + 1B channel number + 2B size var err error buf4, err = c.reader.Peek(4) if err != nil { return err } var channel byte var size uint16 if buf4[0] != '$' { switch string(buf4) { case "RTSP": var res *tcp.Response if res, err = c.ReadResponse(); err != nil { return err } c.Fire(res) // for playing backchannel only after OK response on play c.playOK = true return nil case "OPTI", "TEAR", "DESC", "SETU", "PLAY", "PAUS", "RECO", "ANNO", "GET_", "SET_": var req *tcp.Request if req, err = c.ReadRequest(); err != nil { return err } c.Fire(req) if req.Method == MethodOptions { res := &tcp.Response{Request: req} if err = c.WriteResponse(res); err != nil { return err } } return nil default: c.Fire("RTSP wrong input") for i := 0; ; i++ { // search next start symbol if _, err = c.reader.ReadBytes('$'); err != nil { return err } if channel, err = c.reader.ReadByte(); err != nil { return err } // TODO: better check maximum good channel ID if channel >= 20 { continue } buf4 = make([]byte, 2) if _, err = io.ReadFull(c.reader, buf4); err != nil { return err } // check if size good for RTP size = binary.BigEndian.Uint16(buf4) if size <= 1500 { break } // 10 tries to find good packet if i >= 10 { return fmt.Errorf("RTSP wrong input") } } } } else { // hope that the odd channels are always RTCP channel = buf4[1] // get data size size = binary.BigEndian.Uint16(buf4[2:]) // skip 4 bytes from c.reader.Peek if _, err = c.reader.Discard(4); err != nil { return err } } // init memory for data buf := make([]byte, size) if _, err = io.ReadFull(c.reader, buf); err != nil { return err } c.Recv += int(size) return c.handleRawPacket(channel, buf) } func (c *Conn) handleRawPacket(channel byte, buf []byte) error { if channel&1 == 0 { packet := &rtp.Packet{} if err := packet.Unmarshal(buf); err != nil { return err } for _, receiver := range c.Receivers { if receiver.ID == channel { receiver.WriteRTP(packet) break } } } else { msg := &RTCP{Channel: channel} if err := msg.Header.Unmarshal(buf); err != nil { return nil } //var err error //msg.Packets, err = rtcp.Unmarshal(buf) //if err != nil { // return nil //} c.Fire(msg) } return nil } func (c *Conn) WriteRequest(req *tcp.Request) error { if req.Proto == "" { req.Proto = ProtoRTSP } if req.Header == nil { req.Header = make(map[string][]string) } c.sequence++ // important to send case sensitive CSeq // https://github.com/AlexxIT/go2rtc/issues/7 req.Header["CSeq"] = []string{strconv.Itoa(c.sequence)} c.auth.Write(req) if c.session != "" { req.Header.Set("Session", c.session) } if req.Body != nil { val := strconv.Itoa(len(req.Body)) req.Header.Set("Content-Length", val) } c.Fire(req) if err := c.conn.SetWriteDeadline(time.Now().Add(Timeout)); err != nil { return err } return req.Write(c.conn) } func (c *Conn) ReadRequest() (*tcp.Request, error) { if err := c.conn.SetReadDeadline(time.Now().Add(Timeout)); err != nil { return nil, err } return tcp.ReadRequest(c.reader) } func (c *Conn) WriteResponse(res *tcp.Response) error { if res.Proto == "" { res.Proto = ProtoRTSP } if res.Status == "" { res.Status = "200 OK" } if res.Header == nil { res.Header = make(map[string][]string) } if res.Request != nil && res.Request.Header != nil { seq := res.Request.Header.Get("CSeq") if seq != "" { res.Header.Set("CSeq", seq) } } if c.session != "" { if res.Request != nil && res.Request.Method == MethodSetup { res.Header.Set("Session", c.session+";timeout=60") } else { res.Header.Set("Session", c.session) } } if res.Body != nil { val := strconv.Itoa(len(res.Body)) res.Header.Set("Content-Length", val) } c.Fire(res) if err := c.conn.SetWriteDeadline(time.Now().Add(Timeout)); err != nil { return err } return res.Write(c.conn) } func (c *Conn) ReadResponse() (*tcp.Response, error) { if err := c.conn.SetReadDeadline(time.Now().Add(Timeout)); err != nil { return nil, err } return tcp.ReadResponse(c.reader) } ================================================ FILE: pkg/rtsp/consumer.go ================================================ package rtsp import ( "time" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/AlexxIT/go2rtc/pkg/mjpeg" "github.com/AlexxIT/go2rtc/pkg/pcm" "github.com/pion/rtp" ) func (c *Conn) GetMedias() []*core.Media { //core.Assert(c.Medias != nil) return c.Medias } func (c *Conn) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) (err error) { var channel byte switch c.mode { case core.ModeActiveProducer: // backchannel c.stateMu.Lock() defer c.stateMu.Unlock() if c.state == StatePlay { if err = c.Reconnect(); err != nil { return } } if channel, err = c.SetupMedia(media); err != nil { return } c.state = StateSetup case core.ModePassiveConsumer: channel = byte(len(c.Senders)) * 2 // for consumer is better to use original track codec codec = track.Codec.Clone() // generate new payload type, starting from 96 codec.PayloadType = byte(96 + len(c.Senders)) default: panic(core.Caller()) } // save original codec to sender (can have Codec.Name = ANY) sender := core.NewSender(media, codec) // important to send original codec for valid IsRTP check sender.Handler = c.packetWriter(track.Codec, channel, codec.PayloadType) if c.mode == core.ModeActiveProducer && track.Codec.Name == core.CodecPCMA { // Fix Reolink Doorbell https://github.com/AlexxIT/go2rtc/issues/331 sender.Handler = pcm.RepackG711(true, sender.Handler) } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } const ( startVideoBuf = 32 * 1024 // 32KB startAudioBuf = 2 * 1024 // 2KB maxBuf = 1024 * 1024 // 1MB rtpHdr = 12 // basic RTP header size intHdr = 4 // interleaved header size ) func (c *Conn) packetWriter(codec *core.Codec, channel, payloadType uint8) core.HandlerFunc { var buf []byte var n int video := codec.IsVideo() if video { buf = make([]byte, startVideoBuf) } else { buf = make([]byte, startAudioBuf) } flushBuf := func() { //log.Printf("[rtsp] channel:%2d write_size:%6d buffer_size:%6d", channel, n, len(buf)) if err := c.writeInterleavedData(buf[:n]); err != nil { c.Send += n } n = 0 } handlerFunc := func(packet *rtp.Packet) { if c.state == StateNone { return } clone := rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: packet.Marker, PayloadType: payloadType, SequenceNumber: packet.SequenceNumber, Timestamp: packet.Timestamp, SSRC: packet.SSRC, }, Payload: packet.Payload, } if !video { packet.Marker = true // better to have marker on all audio packets } size := rtpHdr + len(packet.Payload) if l := len(buf); n+intHdr+size > l { if l < maxBuf { buf = append(buf, make([]byte, l)...) // double buffer size } else { flushBuf() } } //log.Printf("[RTP] codec: %s, size: %6d, ts: %10d, pt: %2d, ssrc: %d, seq: %d, mark: %v", codec.Name, len(packet.Payload), packet.Timestamp, packet.PayloadType, packet.SSRC, packet.SequenceNumber, packet.Marker) chunk := buf[n:] _ = chunk[4] // bounds chunk[0] = '$' chunk[1] = channel chunk[2] = byte(size >> 8) chunk[3] = byte(size) if _, err := clone.MarshalTo(chunk[4:]); err != nil { return } n += 4 + size if !packet.Marker || !c.playOK { // collect continious video packets to buffer // or wait OK for PLAY command for backchannel //log.Printf("[rtsp] collecting buffer ok=%t", c.playOK) return } flushBuf() } if !codec.IsRTP() { switch codec.Name { case core.CodecH264: handlerFunc = h264.RTPPay(c.PacketSize, handlerFunc) case core.CodecH265: handlerFunc = h265.RTPPay(c.PacketSize, handlerFunc) case core.CodecAAC: handlerFunc = aac.RTPPay(handlerFunc) case core.CodecJPEG: handlerFunc = mjpeg.RTPPay(handlerFunc) } } else if codec.Name == core.CodecPCML { handlerFunc = pcm.LittleToBig(handlerFunc) } else if c.PacketSize != 0 { switch codec.Name { case core.CodecH264: handlerFunc = h264.RTPPay(c.PacketSize, handlerFunc) handlerFunc = h264.RTPDepay(codec, handlerFunc) case core.CodecH265: handlerFunc = h265.RTPPay(c.PacketSize, handlerFunc) handlerFunc = h265.RTPDepay(codec, handlerFunc) } } return handlerFunc } func (c *Conn) writeInterleavedData(data []byte) error { if c.Transport != "udp" { _ = c.conn.SetWriteDeadline(time.Now().Add(Timeout)) _, err := c.conn.Write(data) return err } for len(data) >= 4 && data[0] == '$' { channel := data[1] size := uint16(data[2])<<8 | uint16(data[3]) rtpData := data[4 : 4+size] if _, err := c.WriteToUDP(rtpData, channel); err != nil { return err } data = data[4+size:] } return nil } ================================================ FILE: pkg/rtsp/helpers.go ================================================ package rtsp import ( "bytes" "io" "net/url" "regexp" "strconv" "strings" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtcp" "github.com/pion/sdp/v3" ) type RTCP struct { Channel byte Header rtcp.Header Packets []rtcp.Packet } const sdpHeader = `v=0 o=- 0 0 IN IP4 0.0.0.0 s=- t=0 0` func UnmarshalSDP(rawSDP []byte) ([]*core.Media, error) { sd := &sdp.SessionDescription{} if err := sd.Unmarshal(rawSDP); err != nil { // fix multiple `s=` https://github.com/AlexxIT/WebRTC/issues/417 rawSDP = regexp.MustCompile("\ns=[^\n]+").ReplaceAll(rawSDP, nil) // fix broken `c=` https://github.com/AlexxIT/go2rtc/issues/1426 rawSDP = regexp.MustCompile("\nc=[^\n]+").ReplaceAll(rawSDP, nil) // fix SDP header for some cameras if i := bytes.Index(rawSDP, []byte("\nm=")); i > 0 { rawSDP = append([]byte(sdpHeader), rawSDP[i:]...) } // Fix invalid media type (errSDPInvalidValue) caused by // some TP-LINK IP camera, e.g. TL-IPC44GW for _, b := range regexp.MustCompile("m=[^ ]+ ").FindAll(rawSDP, -1) { switch string(b[2 : len(b)-1]) { case "audio", "video", "application": default: rawSDP = bytes.Replace(rawSDP, b, []byte("m=application "), 1) } } if err == io.EOF { rawSDP = append(rawSDP, '\n') } sd = &sdp.SessionDescription{} err = sd.Unmarshal(rawSDP) if err != nil { return nil, err } } // fix buggy camera https://github.com/AlexxIT/go2rtc/issues/771 forceDirection := sd.Origin.Username == "CV-RTSPHandler" var medias []*core.Media for _, md := range sd.MediaDescriptions { media := core.UnmarshalMedia(md) // Check buggy SDP with fmtp for H264 on another track // https://github.com/AlexxIT/WebRTC/issues/419 for _, codec := range media.Codecs { switch codec.Name { case core.CodecH264: if codec.FmtpLine == "" { codec.FmtpLine = findFmtpLine(codec.PayloadType, sd.MediaDescriptions) } case core.CodecH265: if codec.FmtpLine != "" { // all three parameters are needed for a valid fmtp line // https://github.com/AlexxIT/go2rtc/pull/1588 if !strings.Contains(codec.FmtpLine, "sprop-vps=") || !strings.Contains(codec.FmtpLine, "sprop-sps=") || !strings.Contains(codec.FmtpLine, "sprop-pps=") { codec.FmtpLine = "" } } case core.CodecOpus: // fix OPUS for some cameras https://datatracker.ietf.org/doc/html/rfc7587 codec.ClockRate = 48000 codec.Channels = 2 } } if media.Direction == "" || forceDirection { media.Direction = core.DirectionRecvonly } medias = append(medias, media) } return medias, nil } func findFmtpLine(payloadType uint8, descriptions []*sdp.MediaDescription) string { s := strconv.Itoa(int(payloadType)) for _, md := range descriptions { codec := core.UnmarshalCodec(md, s) if codec.FmtpLine != "" { return codec.FmtpLine } } return "" } // urlParse fix bugs: // 1. Content-Base: rtsp://::ffff:192.168.1.123/onvif/profile.1/ // 2. Content-Base: rtsp://rtsp://turret2-cam.lan:554/stream1/ // 3. Content-Base: 192.168.253.220:1935/ func urlParse(rawURL string) (*url.URL, error) { // fix https://github.com/AlexxIT/go2rtc/issues/830 if strings.HasPrefix(rawURL, "rtsp://rtsp://") { rawURL = rawURL[7:] } // fix https://github.com/AlexxIT/go2rtc/issues/1852 if !strings.Contains(rawURL, "://") { rawURL = "rtsp://" + rawURL } u, err := url.Parse(rawURL) if err != nil && strings.HasSuffix(err.Error(), "after host") { if i := indexN(rawURL, '/', 3); i > 0 { return urlParse(rawURL[:i] + ":" + rawURL[i:]) } } return u, err } func indexN(s string, c byte, n int) int { var offset int for { i := strings.IndexByte(s[offset:], c) if i < 0 { break } if n--; n == 0 { return offset + i } offset += i + 1 } return -1 } ================================================ FILE: pkg/rtsp/producer.go ================================================ package rtsp import ( "encoding/json" "errors" "github.com/AlexxIT/go2rtc/pkg/core" ) func (c *Conn) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { core.Assert(media.Direction == core.DirectionRecvonly) for _, track := range c.Receivers { if track.Codec == codec { return track, nil } } c.stateMu.Lock() defer c.stateMu.Unlock() var channel byte switch c.mode { case core.ModeActiveProducer: if c.state == StatePlay { if err := c.Reconnect(); err != nil { return nil, err } } var err error channel, err = c.SetupMedia(media) if err != nil { return nil, err } c.state = StateSetup case core.ModePassiveConsumer: // Backchannel channel = byte(len(c.Senders)) * 2 default: return nil, errors.New("rtsp: wrong mode for GetTrack") } track := core.NewReceiver(media, codec) track.ID = channel c.Receivers = append(c.Receivers, track) return track, nil } func (c *Conn) Start() (err error) { core.Assert(c.mode == core.ModeActiveProducer || c.mode == core.ModePassiveProducer) for { ok := false c.stateMu.Lock() switch c.state { case StateNone: err = nil case StateConn: err = errors.New("start from CONN state") case StateSetup: switch c.mode { case core.ModeActiveProducer: err = c.Play() case core.ModePassiveProducer: err = nil default: err = errors.New("start from wrong mode: " + c.mode.String()) } if err == nil { c.state = StatePlay ok = true } } c.stateMu.Unlock() if !ok { return } // Handler can return different states: // 1. None after PLAY should exit without error // 2. Play after PLAY should exit from Start with error // 3. Setup after PLAY should Play once again err = c.Handle() } } func (c *Conn) Stop() (err error) { for _, receiver := range c.Receivers { receiver.Close() } for _, sender := range c.Senders { sender.Close() } c.stateMu.Lock() if c.state != StateNone { c.state = StateNone err = c.Close() } c.stateMu.Unlock() return } func (c *Conn) MarshalJSON() ([]byte, error) { return json.Marshal(c.Connection) } func (c *Conn) Reconnect() error { c.Fire("RTSP reconnect") // close current session _ = c.Close() // start new session if err := c.Dial(); err != nil { return err } if err := c.Describe(); err != nil { return err } // restore previous medias for _, receiver := range c.Receivers { if _, err := c.SetupMedia(receiver.Media); err != nil { return err } } for _, sender := range c.Senders { if _, err := c.SetupMedia(sender.Media); err != nil { return err } } return nil } ================================================ FILE: pkg/rtsp/rtsp_test.go ================================================ package rtsp import ( "testing" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/stretchr/testify/assert" ) func TestURLParse(t *testing.T) { // https://github.com/AlexxIT/WebRTC/issues/395 base := "rtsp://::ffff:192.168.1.123/onvif/profile.1/" u, err := urlParse(base) assert.NoError(t, err) assert.Equal(t, "::ffff:192.168.1.123:", u.Host) // https://github.com/AlexxIT/go2rtc/issues/208 base = "rtsp://rtsp://turret2-cam.lan:554/stream1/" u, err = urlParse(base) assert.NoError(t, err) assert.Equal(t, "turret2-cam.lan:554", u.Host) // https://github.com/AlexxIT/go2rtc/issues/1852 base = "192.168.253.220:1935/" u, err = urlParse(base) assert.NoError(t, err) assert.Equal(t, "192.168.253.220:1935", u.Host) } func TestBugSDP1(t *testing.T) { // https://github.com/AlexxIT/WebRTC/issues/417 s := `v=0 o=- 91674849066 1 IN IP4 192.168.1.123 s=RtspServer i=live t=0 0 a=control:* a=range:npt=0- m=video 0 RTP/AVP 96 c=IN IP4 0.0.0.0 s=RtspServer i=live a=control:track0 a=range:npt=0- a=rtpmap:96 H264/90000 a=fmtp:96 packetization-mode=1;profile-level-id=42001E;sprop-parameter-sets=Z0IAHvQCgC3I,aM48gA== a=control:track0 m=audio 0 RTP/AVP 97 c=IN IP4 0.0.0.0 s=RtspServer i=live a=control:track1 a=range:npt=0- a=rtpmap:97 MPEG4-GENERIC/8000/1 a=fmtp:97 profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1588 a=control:track1 ` medias, err := UnmarshalSDP([]byte(s)) assert.Nil(t, err) assert.NotNil(t, medias) } func TestBugSDP2(t *testing.T) { // https://github.com/AlexxIT/WebRTC/issues/419 s := `v=0 o=- 1675628282 1675628283 IN IP4 192.168.1.123 s=streamed by the RTSP server t=0 0 m=video 0 RTP/AVP 96 a=rtpmap:96 H264/90000 a=control:track0 m=audio 0 RTP/AVP 8 a=rtpmap:0 pcma/8000/1 a=control:track1 a=framerate:25 a=range:npt=now- a=fmtp:96 packetization-mode=1;profile-level-id=64001F;sprop-parameter-sets=Z0IAH5WoFAFuQA==,aM48gA== ` medias, err := UnmarshalSDP([]byte(s)) assert.Nil(t, err) assert.NotNil(t, medias) assert.NotEqual(t, "", medias[0].Codecs[0].FmtpLine) } func TestBugSDP3(t *testing.T) { s := `v=0 o=- 1680614126554766 1 IN IP4 192.168.0.3 s=Session streamed by "preview" t=0 0 a=tool:BC Streaming Media v202210012022.10.01 a=type:broadcast a=control:* a=range:npt=now- a=x-qt-text-nam:Session streamed by "preview" m=video 0 RTP/AVP 96 c=IN IP4 0.0.0.0 b=AS:8192 a=rtpmap:96 H264/90000 a=range:npt=now- a=fmtp:96 packetization-mode=1;profile-level-id=640033;sprop-parameter-sets=Z2QAM6wVFKAoAPGQ,aO48sA== a=recvonly a=control:track1 m=audio 0 RTP/AVP 97 c=IN IP4 0.0.0.0 b=AS:8192 a=rtpmap:97 MPEG4-GENERIC/16000 a=fmtp:97 streamtype=5;profile-level-id=1;mode=AAC-hbr;sizelength=13;indexlength=3;indexdeltalength=3;config=1408; a=recvonly a=control:track2 m=audio 0 RTP/AVP 8 a=control:track3 a=rtpmap:8 PCMA/8000 a=sendonly` medias, err := UnmarshalSDP([]byte(s)) assert.Nil(t, err) assert.Len(t, medias, 3) } func TestBugSDP4(t *testing.T) { s := `v=0 o=- 14665860 31787219 1 IN IP4 10.0.0.94 s=Session streamed by "MERCURY RTSP Server" t=0 0 m=video 0 RTP/AVP 96 c=IN IP4 0.0.0.0 b=AS:4096 a=range:npt=0- a=control:track1 a=rtpmap:96 H264/90000 a=fmtp:96 packetization-mode=1; profile-level-id=640016; sprop-parameter-sets=Z2QAFqzGoCgPaEAAAAMAQAAAB6E=,aOqPLA== m=audio 0 RTP/AVP 8 a=rtpmap:8 PCMA/8000 a=control:track2 m=application/MERCURY 0 RTP/AVP smart/1/90000 a=rtpmap:95 MERCURY/90000 a=control:track3 ` medias, err := UnmarshalSDP([]byte(s)) assert.Nil(t, err) assert.Len(t, medias, 3) } func TestBugSDP5(t *testing.T) { s := `v=0 o=CV-RTSPHandler 1123412 0 IN IP4 192.168.1.22 s=Camera c=IN IP4 192.168.1.22 t=0 0 a=charset:Shift_JIS a=range:npt=0- a=control:* a=etag:1234567890 m=video 0 RTP/AVP 99 a=rtpmap:99 H264/90000 a=fmtp:99 profile-level-id=42A01E;packetization-mode=1;sprop-parameter-sets=Z0KgKedAPAET8uAIEAABd2AAK/IGAAADAC+vCAAAHc1lP//jAAADABfXhAAADuayn//wIA==,aN48gA== a=control:trackID=1 a=sendonly m=audio 0 RTP/AVP 127 a=rtpmap:127 mpeg4-generic/8000/1 a=fmtp:127 streamtype=5; profile-level-id=15; mode=AAC-hbr; sizeLength=13; indexLength=3; indexDeltalength=3; config=1588; CTSDeltaLength=0; DTSDeltaLength=0; a=control:trackID=2 ` medias, err := UnmarshalSDP([]byte(s)) assert.Nil(t, err) assert.Len(t, medias, 2) assert.Equal(t, "recvonly", medias[0].Direction) assert.Equal(t, "recvonly", medias[1].Direction) } func TestBugSDP6(t *testing.T) { // https://github.com/AlexxIT/go2rtc/issues/1278 s := `v=0 o=- 3730506281693 1 IN IP4 172.20.0.215 s=IP camera Live streaming i=stream1 t=0 0 a=tool:LIVE555 Streaming Media v2014.02.04 a=type:broadcast a=control:* a=range:npt=0- a=x-qt-text-nam:IP camera Live streaming a=x-qt-text-inf:stream1 m=video 0 RTP/AVP 26 c=IN IP4 172.20.0.215 b=AS:1500 a=x-bufferdelay:0.55000 a=x-dimensions:1280,960 a=control:track1 m=audio 0 RTP/AVP 0 c=IN IP4 172.20.0.215 b=AS:64 a=x-bufferdelay:0.55000 a=control:track2 m=application 0 RTP/AVP 107 c=IN IP4 172.20.0.215 b=AS:1 a=x-bufferdelay:0.55000 a=rtpmap:107 vnd.onvif.metadata/90000/500 a=control:track4 m=vana 0 RTP/AVP 108 c=IN IP4 172.20.0.215 b=AS:1 a=x-bufferdelay:0.55000 a=rtpmap:108 video.analysis/90000/500 a=control:track5 ` medias, err := UnmarshalSDP([]byte(s)) assert.Nil(t, err) assert.Len(t, medias, 4) } func TestBugSDP7(t *testing.T) { // https://github.com/AlexxIT/go2rtc/issues/1426 s := `v=0 o=- 1001 1 IN s=VCP IPC Realtime stream m=video 0 RTP/AVP 105 c=IN a=control:rtsp://1.0.1.113/media/video2/video a=rtpmap:105 H264/90000 a=fmtp:105 profile-level-id=640016; packetization-mode=1; sprop-parameter-sets=Z2QAFqw7UFAX/LCAAAH0AABOIEI=,aOqPLA== a=recvonly m=audio 0 RTP/AVP 0 c=IN a=fmtp:0 RTCP=0 a=control:rtsp://1.0.1.113/media/video2/audio1 a=recvonly m=audio 0 RTP/AVP 0 c=IN a=control:rtsp://1.0.1.113/media/video2/backchannel a=rtpmap:0 PCMA/8000 a=rtpmap:0 PCMU/8000 a=sendonly m=application 0 RTP/AVP 107 c=IN a=control:rtsp://1.0.1.113/media/video2/metadata a=rtpmap:107 vnd.onvif.metadata/90000 a=fmtp:107 DecoderTag=h3c-v3 RTCP=0 a=recvonly ` medias, err := UnmarshalSDP([]byte(s)) assert.Nil(t, err) assert.Len(t, medias, 4) } func TestHikvisionPCM(t *testing.T) { s := `v=0 o=- 1721969533379665 1721969533379665 IN IP4 192.168.1.12 s=Media Presentation e=NONE b=AS:5100 t=0 0 a=control:rtsp://192.168.1.12:554/Streaming/channels/101/ m=video 0 RTP/AVP 96 c=IN IP4 0.0.0.0 b=AS:5000 a=recvonly a=x-dimensions:3200,1800 a=control:rtsp://192.168.1.12:554/Streaming/channels/101/trackID=1 a=rtpmap:96 H264/90000 a=fmtp:96 profile-level-id=420029; packetization-mode=1; sprop-parameter-sets=Z2QAM6wVFKAyAOP5f/AAEAAWyAAAH0AAB1MAIA==,aO48sA== m=audio 0 RTP/AVP 11 c=IN IP4 0.0.0.0 b=AS:50 a=recvonly a=control:rtsp://192.168.1.12:554/Streaming/channels/101/trackID=2 a=rtpmap:11 PCM/48000 a=Media_header:MEDIAINFO=494D4B4801030000040000010170011080BB0000007D000000000000000000000000000000000000; a=appversion:1.0 ` medias, err := UnmarshalSDP([]byte(s)) assert.Nil(t, err) assert.Len(t, medias, 2) assert.Equal(t, core.CodecPCML, medias[1].Codecs[0].Name) } ================================================ FILE: pkg/rtsp/server.go ================================================ package rtsp import ( "bufio" "errors" "fmt" "net" "net/url" "strconv" "strings" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/tcp" ) var FailedAuth = errors.New("failed authentication") func NewServer(conn net.Conn) *Conn { return &Conn{ Connection: core.Connection{ ID: core.NewID(), FormatName: "rtsp", Protocol: "rtsp+tcp", RemoteAddr: conn.RemoteAddr().String(), }, conn: conn, reader: bufio.NewReader(conn), } } func (c *Conn) Auth(username, password string) { info := url.UserPassword(username, password) c.auth = tcp.NewAuth(info) } func (c *Conn) Accept() error { for { req, err := c.ReadRequest() if err != nil { return err } if c.URL == nil { c.URL = req.URL c.UserAgent = req.Header.Get("User-Agent") } c.Fire(req) if valid, empty := c.auth.Validate(req); !valid { res := &tcp.Response{ Status: "401 Unauthorized", Header: map[string][]string{"Www-Authenticate": {`Basic realm="go2rtc"`}}, Request: req, } if err = c.WriteResponse(res); err != nil { return err } if empty { // eliminate false positive: ffmpeg sends first request without // authorization header even if the user provides credentials continue } return FailedAuth } // Receiver: OPTIONS > DESCRIBE > SETUP... > PLAY > TEARDOWN // Sender: OPTIONS > ANNOUNCE > SETUP... > RECORD > TEARDOWN switch req.Method { case MethodOptions: res := &tcp.Response{ Header: map[string][]string{ "Public": {"OPTIONS, SETUP, TEARDOWN, DESCRIBE, PLAY, PAUSE, ANNOUNCE, RECORD"}, }, Request: req, } if err = c.WriteResponse(res); err != nil { return err } case MethodAnnounce: if req.Header.Get("Content-Type") != "application/sdp" { return errors.New("wrong content type") } c.SDP = string(req.Body) // for info c.Medias, err = UnmarshalSDP(req.Body) if err != nil { return err } // TODO: fix someday... for i, media := range c.Medias { track := core.NewReceiver(media, media.Codecs[0]) track.ID = byte(i * 2) c.Receivers = append(c.Receivers, track) } c.mode = core.ModePassiveProducer c.Fire(MethodAnnounce) res := &tcp.Response{Request: req} if err = c.WriteResponse(res); err != nil { return err } case MethodDescribe: c.mode = core.ModePassiveConsumer c.Fire(MethodDescribe) if c.Senders == nil { res := &tcp.Response{ Status: "404 Not Found", Request: req, } return c.WriteResponse(res) } res := &tcp.Response{ Header: map[string][]string{ "Content-Type": {"application/sdp"}, }, Request: req, } // convert tracks to real output medias medias var medias []*core.Media for i, track := range c.Senders { media := &core.Media{ Kind: core.GetKind(track.Codec.Name), Direction: core.DirectionRecvonly, Codecs: []*core.Codec{track.Codec}, ID: "trackID=" + strconv.Itoa(i), } medias = append(medias, media) } for i, track := range c.Receivers { media := &core.Media{ Kind: core.GetKind(track.Codec.Name), Direction: core.DirectionSendonly, Codecs: []*core.Codec{track.Codec}, ID: "trackID=" + strconv.Itoa(i+len(c.Senders)), } medias = append(medias, media) } res.Body, err = core.MarshalSDP(c.SessionName, medias) if err != nil { return err } c.SDP = string(res.Body) // for info if err = c.WriteResponse(res); err != nil { return err } case MethodSetup: res := &tcp.Response{ Header: map[string][]string{}, Request: req, } // Test if client requests TCP transport, otherwise return 461 Transport not supported // This allows smart clients who initially requested UDP to fall back on TCP transport if tr := req.Header.Get("Transport"); strings.HasPrefix(tr, "RTP/AVP/TCP") { c.session = core.RandString(8, 10) c.state = StateSetup if c.mode == core.ModePassiveConsumer { if i := reqTrackID(req); i >= 0 && i < len(c.Senders)+len(c.Receivers) { if i < len(c.Senders) { c.Senders[i].Media.ID = MethodSetup } else { c.Receivers[i-len(c.Senders)].Media.ID = MethodSetup } tr = fmt.Sprintf("RTP/AVP/TCP;unicast;interleaved=%d-%d", i*2, i*2+1) res.Header.Set("Transport", tr) } else { res.Status = "400 Bad Request" } } else { res.Header.Set("Transport", tr) } } else { res.Status = "461 Unsupported transport" } if err = c.WriteResponse(res); err != nil { return err } case MethodRecord, MethodPlay: if c.mode == core.ModePassiveConsumer { // stop unconfigured senders for _, track := range c.Senders { if track.Media.ID != MethodSetup { track.Close() } } } res := &tcp.Response{Request: req} err = c.WriteResponse(res) c.playOK = true return err case MethodTeardown: res := &tcp.Response{Request: req} _ = c.WriteResponse(res) c.state = StateNone return c.conn.Close() default: return fmt.Errorf("unsupported method: %s", req.Method) } } } func reqTrackID(req *tcp.Request) int { var s string if req.URL.RawQuery != "" { s = req.URL.RawQuery } else { s = req.URL.Path } if i := strings.LastIndexByte(s, '='); i > 0 { if i, err := strconv.Atoi(s[i+1:]); err == nil { return i } } return -1 } ================================================ FILE: pkg/shell/command.go ================================================ package shell import ( "context" "os/exec" ) // Command like exec.Cmd, but with support: // - io.Closer interface // - Wait from multiple places // - Done channel type Command struct { *exec.Cmd ctx context.Context cancel context.CancelFunc err error } func NewCommand(s string) *Command { ctx, cancel := context.WithCancel(context.Background()) args := QuoteSplit(s) cmd := exec.CommandContext(ctx, args[0], args[1:]...) cmd.SysProcAttr = procAttr return &Command{cmd, ctx, cancel, nil} } func (c *Command) Start() error { if err := c.Cmd.Start(); err != nil { return err } go func() { c.err = c.Cmd.Wait() c.cancel() // release context resources }() return nil } func (c *Command) Wait() error { <-c.ctx.Done() return c.err } func (c *Command) Run() error { if err := c.Start(); err != nil { return err } return c.Wait() } func (c *Command) Done() <-chan struct{} { return c.ctx.Done() } func (c *Command) Close() error { c.cancel() return nil } ================================================ FILE: pkg/shell/procattr.go ================================================ //go:build !linux package shell import "syscall" var procAttr *syscall.SysProcAttr ================================================ FILE: pkg/shell/procattr_linux.go ================================================ package shell import "syscall" // will stop child if parent died (even with SIGKILL) var procAttr = &syscall.SysProcAttr{Pdeathsig: syscall.SIGTERM} ================================================ FILE: pkg/shell/shell.go ================================================ package shell import ( "os" "os/signal" "strings" "syscall" ) func QuoteSplit(s string) []string { var a []string for len(s) > 0 { switch c := s[0]; c { case '\t', '\n', '\r', ' ': // unicode.IsSpace s = s[1:] case '"', '\'': // quote chars if i := strings.IndexByte(s[1:], c); i > 0 { a = append(a, s[1:i+1]) s = s[i+2:] } else { return nil // error } default: i := strings.IndexAny(s, "\t\n\r ") if i > 0 { a = append(a, s[:i]) s = s[i:] } else { a = append(a, s) s = "" } } } return a } func RunUntilSignal() { sigs := make(chan os.Signal, 1) signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) println("exit with signal:", (<-sigs).String()) } ================================================ FILE: pkg/shell/shell_test.go ================================================ package shell import ( "testing" "github.com/stretchr/testify/require" ) func TestQuoteSplit(t *testing.T) { s := ` python "-c" 'import time print("time", time.time())' ` require.Equal(t, []string{"python", "-c", "import time\nprint(\"time\", time.time())"}, QuoteSplit(s)) s = `ffmpeg -i "video=FaceTime HD Camera" -i "DeckLink SDI (2)"` require.Equal(t, []string{"ffmpeg", "-i", `video=FaceTime HD Camera`, "-i", "DeckLink SDI (2)"}, QuoteSplit(s)) } ================================================ FILE: pkg/srtp/server.go ================================================ package srtp import ( "encoding/binary" "net" "strconv" "sync" ) type Server struct { address string conn net.PacketConn sessions map[uint32]*Session mu sync.Mutex } func NewServer(address string) *Server { return &Server{ address: address, sessions: map[uint32]*Session{}, } } func (s *Server) Port() int { if s.conn != nil { return s.conn.LocalAddr().(*net.UDPAddr).Port } _, a, _ := net.SplitHostPort(s.address) i, _ := strconv.Atoi(a) return i } func (s *Server) AddSession(session *Session) { s.mu.Lock() defer s.mu.Unlock() if err := session.init(); err != nil { return } if len(s.sessions) == 0 { var err error if s.conn, err = net.ListenPacket("udp", s.address); err != nil { return } go s.handle() } session.conn = s.conn s.sessions[session.Remote.SSRC] = session } func (s *Server) DelSession(session *Session) { s.mu.Lock() delete(s.sessions, session.Remote.SSRC) // check s.conn for https://github.com/AlexxIT/go2rtc/issues/734 if len(s.sessions) == 0 && s.conn != nil { _ = s.conn.Close() } s.mu.Unlock() } func (s *Server) GetSession(ssrc uint32) (session *Session) { s.mu.Lock() session = s.sessions[ssrc] s.mu.Unlock() return } func (s *Server) handle() error { b := make([]byte, 2048) for { n, _, err := s.conn.ReadFrom(b) if err != nil { return err } // Multiplexing RTP Data and Control Packets on a Single Port // https://datatracker.ietf.org/doc/html/rfc5761 switch packetType := b[1]; packetType { case 99, 110, 0x80 | 99, 0x80 | 110: // this is default position for SSRC in RTP packet ssrc := binary.BigEndian.Uint32(b[8:]) if session := s.GetSession(ssrc); session != nil { session.ReadRTP(b[:n]) } case 200, 201, 202, 203, 204, 205, 206, 207: // this is default position for SSRC in RTCP packet ssrc := binary.BigEndian.Uint32(b[4:]) if session := s.GetSession(ssrc); session != nil { session.ReadRTCP(b[:n]) } } } } ================================================ FILE: pkg/srtp/session.go ================================================ package srtp import ( "net" "time" "github.com/pion/rtcp" "github.com/pion/rtp" "github.com/pion/srtp/v3" ) type Session struct { Local *Endpoint Remote *Endpoint OnReadRTP func(packet *rtp.Packet) Recv int // bytes recv Send int // bytes send conn net.PacketConn // local conn endpoint PayloadType uint8 RTCPInterval time.Duration senderRTCP rtcp.SenderReport senderTime time.Time } type Endpoint struct { Addr string Port uint16 MasterKey []byte MasterSalt []byte SSRC uint32 addr net.Addr srtp *srtp.Context } func (e *Endpoint) init() (err error) { e.addr = &net.UDPAddr{IP: net.ParseIP(e.Addr), Port: int(e.Port)} e.srtp, err = srtp.CreateContext(e.MasterKey, e.MasterSalt, profile(e.MasterKey)) return } func profile(key []byte) srtp.ProtectionProfile { switch len(key) { case 16: return srtp.ProtectionProfileAes128CmHmacSha1_80 //case 32: // return srtp.ProtectionProfileAes256CmHmacSha1_80 } return 0 } func (s *Session) init() error { if err := s.Local.init(); err != nil { return err } if err := s.Remote.init(); err != nil { return err } s.senderRTCP.SSRC = s.Local.SSRC s.senderTime = time.Now().Add(s.RTCPInterval) return nil } func (s *Session) WriteRTP(packet *rtp.Packet) (int, error) { if s.Local.srtp == nil { return 0, nil // before init call } if now := time.Now(); now.After(s.senderTime) { s.senderRTCP.NTPTime = uint64(now.UnixNano()) s.senderTime = now.Add(s.RTCPInterval) _, _ = s.WriteRTCP(&s.senderRTCP) } clone := rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: packet.Marker, PayloadType: s.PayloadType, SequenceNumber: packet.SequenceNumber, Timestamp: packet.Timestamp, SSRC: s.Local.SSRC, }, Payload: packet.Payload, } b, err := clone.Marshal() if err != nil { return 0, err } s.senderRTCP.PacketCount++ s.senderRTCP.RTPTime = clone.Timestamp s.senderRTCP.OctetCount += uint32(len(clone.Payload)) if b, err = s.Local.srtp.EncryptRTP(nil, b, nil); err != nil { return 0, err } return s.conn.WriteTo(b, s.Remote.addr) } func (s *Session) WriteRTCP(packet rtcp.Packet) (int, error) { b, err := packet.Marshal() if err != nil { return 0, err } b, err = s.Local.srtp.EncryptRTCP(nil, b, nil) if err != nil { return 0, err } return s.conn.WriteTo(b, s.Remote.addr) } func (s *Session) ReadRTP(b []byte) { packet := &rtp.Packet{} b, err := s.Remote.srtp.DecryptRTP(nil, b, &packet.Header) if err != nil { return } if err = packet.Unmarshal(b); err != nil { return } if s.OnReadRTP != nil { s.OnReadRTP(packet) } } func (s *Session) ReadRTCP(b []byte) { header := rtcp.Header{} b, err := s.Remote.srtp.DecryptRTCP(nil, b, &header) if err != nil { return } //packets, err := rtcp.Unmarshal(b) //if err != nil { // return //} //if report, ok := packets[0].(*rtcp.SenderReport); ok { // log.Printf("[srtp] rtcp type=%d report=%v", header.Type, report) //} if header.Type != rtcp.TypeSenderReport { return } receiverRTCP := rtcp.ReceiverReport{SSRC: s.Local.SSRC} _, _ = s.WriteRTCP(&receiverRTCP) } ================================================ FILE: pkg/tapo/backchannel.go ================================================ package tapo import ( "bytes" "strconv" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/mpegts" "github.com/pion/rtp" ) func (c *Client) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error { if c.sender == nil { if err := c.SetupBackchannel(); err != nil { return err } muxer := mpegts.NewMuxer() pid := muxer.AddTrack(mpegts.StreamTypePCMATapo) if err := c.WriteBackchannel(muxer.GetHeader()); err != nil { return err } c.sender = core.NewSender(media, track.Codec) c.sender.Handler = func(packet *rtp.Packet) { b := muxer.GetPayload(pid, packet.Timestamp, packet.Payload) _ = c.WriteBackchannel(b) } } c.sender.HandleRTP(track) return nil } func (c *Client) SetupBackchannel() (err error) { // if conn1 is not used - we will use it for backchannel // or we need to start another conn for session2 if c.session1 != "" { if c.conn2, err = c.newConn(); err != nil { return } } else { c.conn2 = c.conn1 } c.session2, err = c.Request(c.conn2, []byte(`{"params":{"talk":{"mode":"aec"},"method":"get"},"seq":3,"type":"request"}`)) return } func (c *Client) WriteBackchannel(body []byte) (err error) { // TODO: fixme (size) buf := bytes.NewBuffer(nil) buf.WriteString("----client-stream-boundary--\r\n") buf.WriteString("Content-Type: audio/mp2t\r\n") buf.WriteString("X-If-Encrypt: 0\r\n") buf.WriteString("X-Session-Id: " + c.session2 + "\r\n") buf.WriteString("Content-Length: " + strconv.Itoa(len(body)) + "\r\n\r\n") buf.Write(body) _, err = buf.WriteTo(c.conn2) return } ================================================ FILE: pkg/tapo/client.go ================================================ package tapo import ( "bufio" "bytes" "crypto/aes" "crypto/cipher" "crypto/md5" "crypto/sha256" "encoding/json" "errors" "fmt" "io" "mime/multipart" "net" "net/http" "net/url" "strconv" "strings" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/mpegts" "github.com/AlexxIT/go2rtc/pkg/pcm" "github.com/AlexxIT/go2rtc/pkg/tcp" ) // Deprecated: should be rewritten to core.Connection type Client struct { core.Listener url *url.URL medias []*core.Media receivers []*core.Receiver sender *core.Sender conn1 net.Conn conn2 net.Conn decrypt func(b []byte) []byte session1 string session2 string request string recv int send int } // block ciphers using cipher block chaining. type cbcMode interface { cipher.BlockMode SetIV([]byte) } // Dial support different urls: // - tapo://{cloud-password}@192.168.1.123 - auth to Tapo cameras // with cloud password (autodetect hash method) // - tapo://admin:{hashed-cloud-password}@192.168.1.123 - auth to Tapo cameras // with pre-hashed cloud password // - vigi://admin:{password}@192.168.1.123 - auth to Vigi cameras with password // for admin account (other not supported) func Dial(rawURL string) (*Client, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } if u.Port() == "" { u.Host += ":8800" } c := &Client{url: u} if c.conn1, err = c.newConn(); err != nil { return nil, err } return c, nil } func (c *Client) newConn() (net.Conn, error) { req, err := http.NewRequest("POST", "http://"+c.url.Host+"/stream", nil) if err != nil { return nil, err } query := c.url.Query() if deviceId := query.Get("deviceId"); deviceId != "" { req.URL.RawQuery = "deviceId=" + deviceId } req.Header.Set("Content-Type", "multipart/mixed; boundary=--client-stream-boundary--") username := c.url.User.Username() password, _ := c.url.User.Password() conn, res, err := dial(req, c.url.Scheme, username, password) if err != nil { return nil, err } if res.StatusCode != http.StatusOK { return nil, errors.New(res.Status) } if c.decrypt == nil { c.newDectypter(res, c.url.Scheme, username, password) } channel := query.Get("channel") if channel == "" { channel = "0" } subtype := query.Get("subtype") switch subtype { case "", "0": subtype = "HD" case "1": subtype = "VGA" } c.request = fmt.Sprintf( `{"params":{"preview":{"audio":["default"],"channels":[%s],"resolutions":["%s"]},"method":"get"},"seq":1,"type":"request"}`, channel, subtype, ) return conn, nil } func (c *Client) newDectypter(res *http.Response, brand, username, password string) { exchange := res.Header.Get("Key-Exchange") nonce := core.Between(exchange, `nonce="`, `"`) if brand == "tapo" && password == "" { if strings.Contains(exchange, `encrypt_type="3"`) { password = fmt.Sprintf("%32X", sha256.Sum256([]byte(username))) } else { password = fmt.Sprintf("%16X", md5.Sum([]byte(username))) } username = "admin" } if strings.Contains(exchange, `username="none"`) { // https://nvd.nist.gov/vuln/detail/CVE-2022-37255 username = "none" password = "TPL075526460603" } key := md5.Sum([]byte(nonce + ":" + password)) iv := md5.Sum([]byte(username + ":" + nonce)) block, err := aes.NewCipher(key[:]) if err != nil { return } cbc := cipher.NewCBCDecrypter(block, iv[:]).(cbcMode) c.decrypt = func(b []byte) []byte { // restore IV cbc.SetIV(iv[:]) // decrypt cbc.CryptBlocks(b, b) // unpad n := len(b) padSize := int(b[n-1]) return b[:n-padSize] } } func (c *Client) SetupStream() (err error) { if c.session1 != "" { return } // audio: default, disable, enable c.session1, err = c.Request(c.conn1, []byte(c.request)) return } // Handle - first run will be in probe state func (c *Client) Handle() error { rd := multipart.NewReader(c.conn1, "--device-stream-boundary--") demux := mpegts.NewDemuxer() var transcode func([]byte) []byte for { p, err := rd.NextRawPart() if err != nil { return err } if ct := p.Header.Get("Content-Type"); ct != "video/mp2t" { continue } cl := p.Header.Get("Content-Length") size, err := strconv.Atoi(cl) if err != nil { return err } c.recv += size body := make([]byte, size) b := body for { if n, err2 := p.Read(b); err2 == nil { b = b[n:] } else { break } } body = c.decrypt(body) bytesRd := bytes.NewReader(body) for { pkt, err2 := demux.ReadPacket(bytesRd) if pkt == nil || err2 == io.EOF { break } if err2 != nil { return err2 } if pkt.PayloadType == mpegts.StreamTypePCMUTapo { // TODO: rewrite this part in the future // Some cameras in the new firmware began to use PCMU/16000. // https://github.com/AlexxIT/go2rtc/issues/1954 // I don't know why Tapo considers this an improvement. The codec is no better than the previous one. // Unfortunately, we don't know in advance what codec the camera will use. // Therefore, it's easier to transcode to a standard codec that all Tapo cameras have. if transcode == nil { transcode = pcm.Transcode( &core.Codec{Name: core.CodecPCMA, ClockRate: 8000}, &core.Codec{Name: core.CodecPCMU, ClockRate: 16000}, ) } pkt.PayloadType = mpegts.StreamTypePCMATapo pkt.Payload = transcode(pkt.Payload) } for _, receiver := range c.receivers { if receiver.ID == pkt.PayloadType { mpegts.TimestampToRTP(pkt, receiver.Codec) receiver.WriteRTP(pkt) break } } } } } func (c *Client) Close() (err error) { if c.conn1 != nil { err = c.conn1.Close() } if c.conn2 != nil { _ = c.conn2.Close() } return } func (c *Client) Request(conn net.Conn, body []byte) (string, error) { // TODO: fixme (size) buf := bytes.NewBuffer(nil) buf.WriteString("----client-stream-boundary--\r\n") buf.WriteString("Content-Type: application/json\r\n") buf.WriteString("Content-Length: " + strconv.Itoa(len(body)) + "\r\n\r\n") buf.Write(body) buf.WriteString("\r\n") if _, err := buf.WriteTo(conn); err != nil { return "", err } mpReader := multipart.NewReader(conn, "--device-stream-boundary--") for { p, err := mpReader.NextRawPart() if err != nil { return "", err } var v struct { Params struct { SessionID string `json:"session_id"` } `json:"params"` } if err = json.NewDecoder(p).Decode(&v); err != nil { return "", err } return v.Params.SessionID, nil } } func dial(req *http.Request, brand, username, password string) (net.Conn, *http.Response, error) { conn, err := net.DialTimeout("tcp", req.URL.Host, core.ConnDialTimeout) if err != nil { return nil, nil, err } if err = req.Write(conn); err != nil { return nil, nil, err } r := bufio.NewReader(conn) res, err := http.ReadResponse(r, req) if err != nil { return nil, nil, err } _, _ = io.Copy(io.Discard, res.Body) // discard leftovers _ = res.Body.Close() // ignore response body auth := res.Header.Get("WWW-Authenticate") if res.StatusCode != http.StatusUnauthorized || !strings.HasPrefix(auth, "Digest") { return nil, nil, errors.New("tapo: wrond status: " + res.Status) } if brand == "tapo" && password == "" { // support cloud password in place of username if strings.Contains(auth, `encrypt_type="3"`) { password = fmt.Sprintf("%32X", sha256.Sum256([]byte(username))) } else { password = fmt.Sprintf("%16X", md5.Sum([]byte(username))) } username = "admin" } else if brand == "vigi" && username == "admin" { password = securityEncode(password) } realm := tcp.Between(auth, `realm="`, `"`) nonce := tcp.Between(auth, `nonce="`, `"`) qop := tcp.Between(auth, `qop="`, `"`) uri := req.URL.RequestURI() ha1 := tcp.HexMD5(username, realm, password) ha2 := tcp.HexMD5(req.Method, uri) nc := "00000001" cnonce := core.RandString(32, 64) response := tcp.HexMD5(ha1, nonce, nc, cnonce, qop, ha2) // https://datatracker.ietf.org/doc/html/rfc7616 header := fmt.Sprintf( `Digest username="%s", realm="%s", nonce="%s", uri="%s", qop=%s, nc=%s, cnonce="%s", response="%s"`, username, realm, nonce, uri, qop, nc, cnonce, response, ) if opaque := tcp.Between(auth, `opaque="`, `"`); opaque != "" { header += fmt.Sprintf(`, opaque="%s", algorithm=MD5`, opaque) } req.Header.Set("Authorization", header) if err = req.Write(conn); err != nil { return nil, nil, err } if res, err = http.ReadResponse(r, req); err != nil { return nil, nil, err } return conn, res, nil } const ( keyShort = "RDpbLfCPsJZ7fiv" keyLong = "yLwVl0zKqws7LgKPRQ84Mdt708T1qQ3Ha7xv3H7NyU84p21BriUWBU43odz3iP4rBL3cD02KZciXTysVXiV8ngg6vL48rPJyAUw0HurW20xqxv9aYb4M9wK1Ae0wlro510qXeU07kV57fQMc8L6aLgMLwygtc0F10a0Dg70TOoouyFhdysuRMO51yY5ZlOZZLEal1h0t9YQW0Ko7oBwmCAHoic4HYbUyVeU3sfQ1xtXcPcf1aT303wAQhv66qzW" ) func securityEncode(s string) string { size := len(s) var n int // max if size > len(keyShort) { n = size } else { n = len(keyShort) } b := make([]byte, n) for i := 0; i < n; i++ { c1 := 187 c2 := 187 if i >= size { c1 = int(keyShort[i]) } else if i >= len(keyShort) { c2 = int(s[i]) } else { c1 = int(keyShort[i]) c2 = int(s[i]) } b[i] = keyLong[(c1^c2)%len(keyLong)] } return string(b) } ================================================ FILE: pkg/tapo/producer.go ================================================ package tapo import ( "encoding/json" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/mpegts" ) func (c *Client) GetMedias() []*core.Media { if c.medias == nil { // don't know if all Tapo has this capabilities... c.medias = []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ {Name: core.CodecH264, ClockRate: 90000, PayloadType: core.PayloadTypeRAW}, }, }, { Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ {Name: core.CodecPCMA, ClockRate: 8000, PayloadType: 8}, }, }, { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecPCMA, ClockRate: 8000, PayloadType: 8}, }, }, } } return c.medias } func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { for _, track := range c.receivers { if track.Codec == codec { return track, nil } } if err := c.SetupStream(); err != nil { return nil, err } track := core.NewReceiver(media, codec) switch media.Kind { case core.KindVideo: track.ID = mpegts.StreamTypeH264 case core.KindAudio: track.ID = mpegts.StreamTypePCMATapo } c.receivers = append(c.receivers, track) return track, nil } func (c *Client) Start() error { return c.Handle() } func (c *Client) Stop() error { for _, receiver := range c.receivers { receiver.Close() } if c.sender != nil { c.sender.Close() } return c.Close() } func (c *Client) MarshalJSON() ([]byte, error) { info := &core.Connection{ ID: core.ID(c), FormatName: c.url.Scheme, Protocol: "http", Medias: c.medias, Recv: c.recv, Receivers: c.receivers, Send: c.send, } if c.sender != nil { info.Senders = []*core.Sender{c.sender} } if c.conn1 != nil { info.RemoteAddr = c.conn1.RemoteAddr().String() } return json.Marshal(info) } ================================================ FILE: pkg/tcp/auth.go ================================================ package tcp import ( "crypto/md5" "encoding/base64" "encoding/hex" "fmt" "net/url" "strings" ) type Auth struct { Method byte user string pass string header string h1nonce string } const ( AuthNone byte = iota AuthUnknown AuthBasic AuthDigest AuthTPLink // https://drmnsamoliu.github.io/video.html ) func NewAuth(user *url.Userinfo) *Auth { a := new(Auth) a.user = user.Username() a.pass, _ = user.Password() if a.user != "" { a.Method = AuthUnknown } return a } func (a *Auth) Read(res *Response) bool { auth := res.Header.Get("WWW-Authenticate") if len(auth) < 6 { return false } switch auth[:6] { case "Basic ": a.header = "Basic " + B64(a.user, a.pass) a.Method = AuthBasic return true case "Digest": realm := Between(auth, `realm="`, `"`) nonce := Between(auth, `nonce="`, `"`) a.h1nonce = HexMD5(a.user, realm, a.pass) + ":" + nonce a.header = fmt.Sprintf( `Digest username="%s", realm="%s", nonce="%s"`, a.user, realm, nonce, ) a.Method = AuthDigest return true default: return false } } func (a *Auth) Write(req *Request) { if a == nil { return } switch a.Method { case AuthBasic: req.Header.Set("Authorization", a.header) case AuthDigest: // important to use String except RequestURL for RtspServer: // https://github.com/AlexxIT/go2rtc/issues/244 uri := req.URL.String() h2 := HexMD5(req.Method, uri) response := HexMD5(a.h1nonce, h2) header := a.header + fmt.Sprintf( `, uri="%s", response="%s"`, uri, response, ) req.Header.Set("Authorization", header) case AuthTPLink: req.URL.Host = "127.0.0.1" } } func (a *Auth) Validate(req *Request) (valid, empty bool) { if a == nil { return true, true } header := req.Header.Get("Authorization") if header == "" { return false, true } if a.Method == AuthUnknown { a.Method = AuthBasic a.header = "Basic " + B64(a.user, a.pass) } return header == a.header, false } func (a *Auth) ReadNone(res *Response) bool { auth := res.Header.Get("WWW-Authenticate") if strings.Contains(auth, "TP-LINK Streaming Media") { a.Method = AuthTPLink return true } return false } func (a *Auth) UserInfo() *url.Userinfo { return url.UserPassword(a.user, a.pass) } func Between(s, sub1, sub2 string) string { i := strings.Index(s, sub1) if i < 0 { return "" } s = s[i+len(sub1):] i = strings.Index(s, sub2) if i < 0 { return "" } return s[:i] } func HexMD5(s ...string) string { b := md5.Sum([]byte(strings.Join(s, ":"))) return hex.EncodeToString(b[:]) } func B64(s ...string) string { b := []byte(strings.Join(s, ":")) return base64.StdEncoding.EncodeToString(b) } ================================================ FILE: pkg/tcp/dial.go ================================================ package tcp import ( "crypto/tls" "errors" "net" "net/url" "strings" "time" ) // Dial - for RTSP(S|X) and RTMP(S|X) func Dial(u *url.URL, timeout time.Duration) (net.Conn, error) { var address string var hostname string // without port if i := strings.IndexByte(u.Host, ':'); i > 0 { address = u.Host hostname = u.Host[:i] } else { switch u.Scheme { case "rtsp", "rtsps", "rtspx": address = u.Host + ":554" case "rtmp": address = u.Host + ":1935" case "rtmps", "rtmpx": address = u.Host + ":443" } hostname = u.Host } var secure *tls.Config switch u.Scheme { case "rtsp", "rtmp": case "rtsps", "rtspx", "rtmps", "rtmpx": if u.Scheme[4] == 'x' || IsIP(hostname) { secure = &tls.Config{InsecureSkipVerify: true} } else { secure = &tls.Config{ServerName: hostname} } default: return nil, errors.New("unsupported scheme: " + u.Scheme) } conn, err := net.DialTimeout("tcp", address, timeout) if err != nil { return nil, err } if secure == nil { return conn, nil } tlsConn := tls.Client(conn, secure) if err = tlsConn.Handshake(); err != nil { return nil, err } if u.Scheme[4] == 'x' { u.Scheme = u.Scheme[:4] + "s" } return tlsConn, nil } ================================================ FILE: pkg/tcp/request.go ================================================ package tcp import ( "context" "crypto/tls" "errors" "fmt" "net" "net/http" "strings" "github.com/AlexxIT/go2rtc/pkg/core" ) // Do - http.Client with support Digest Authorization func Do(req *http.Request) (*http.Response, error) { var secure *tls.Config switch req.URL.Scheme { case "httpx": secure = insecureConfig req.URL.Scheme = "https" case "https": if hostname := req.URL.Hostname(); IsIP(hostname) { secure = insecureConfig } } if secure != nil { ctx := context.WithValue(req.Context(), secureKey, secure) req = req.WithContext(ctx) } if client == nil { transport := http.DefaultTransport.(*http.Transport).Clone() dial := transport.DialContext transport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) { conn, err := dial(ctx, network, addr) if pconn, ok := ctx.Value(connKey).(*net.Conn); ok { *pconn = conn } return conn, err } transport.DialTLSContext = func(ctx context.Context, network, addr string) (net.Conn, error) { conn, err := dial(ctx, network, addr) if err != nil { return nil, err } var conf *tls.Config if v, ok := ctx.Value(secureKey).(*tls.Config); ok { conf = v } else if host, _, err := net.SplitHostPort(addr); err != nil { conf = &tls.Config{ServerName: addr} } else { conf = &tls.Config{ServerName: host} } tlsConn := tls.Client(conn, conf) if err = tlsConn.Handshake(); err != nil { return nil, err } if pconn, ok := ctx.Value(connKey).(*net.Conn); ok { *pconn = tlsConn } return tlsConn, err } client = &http.Client{Transport: transport} } user := req.URL.User // Hikvision won't answer on Basic auth with any headers if strings.HasPrefix(req.URL.Path, "/ISAPI/") { req.URL.User = nil } res, err := client.Do(req) if err != nil { return nil, err } if res.StatusCode == http.StatusUnauthorized && user != nil { Close(res) auth := res.Header.Get("WWW-Authenticate") if !strings.HasPrefix(auth, "Digest") { return nil, errors.New("unsupported auth: " + auth) } realm := Between(auth, `realm="`, `"`) nonce := Between(auth, `nonce="`, `"`) qop := Between(auth, `qop="`, `"`) username := user.Username() password, _ := user.Password() ha1 := HexMD5(username, realm, password) uri := req.URL.RequestURI() ha2 := HexMD5(req.Method, uri) var header string switch qop { case "": response := HexMD5(ha1, nonce, ha2) header = fmt.Sprintf( `Digest username="%s", realm="%s", nonce="%s", uri="%s", response="%s"`, username, realm, nonce, uri, response, ) case "auth": nc := "00000001" cnonce := core.RandString(32, 64) response := HexMD5(ha1, nonce, nc, cnonce, qop, ha2) header = fmt.Sprintf( `Digest username="%s", realm="%s", nonce="%s", uri="%s", qop=%s, nc=%s, cnonce="%s", response="%s"`, username, realm, nonce, uri, qop, nc, cnonce, response, ) default: return nil, errors.New("unsupported qop: " + auth) } req.Header.Set("Authorization", header) if res, err = client.Do(req); err != nil { return nil, err } } return res, nil } var client *http.Client type key string var connKey = key("conn") var secureKey = key("secure") var insecureConfig = &tls.Config{ InsecureSkipVerify: true, CipherSuites: []uint16{ tls.TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, tls.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, tls.TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384, tls.TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384, tls.TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305, tls.TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305, tls.TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA, tls.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA, tls.TLS_ECDHE_ECDSA_WITH_AES_256_CBC_SHA, tls.TLS_ECDHE_RSA_WITH_AES_256_CBC_SHA, // this cipher suites disabled starting from https://tip.golang.org/doc/go1.22 // but cameras can't work without them https://github.com/AlexxIT/go2rtc/issues/1172 tls.TLS_RSA_WITH_AES_128_GCM_SHA256, // insecure tls.TLS_RSA_WITH_AES_256_GCM_SHA384, // insecure }, } func WithConn() (context.Context, *net.Conn) { pconn := new(net.Conn) return context.WithValue(context.Background(), connKey, pconn), pconn } func Close(res *http.Response) { if res.Body != nil { _ = res.Body.Close() } } func IsIP(hostname string) bool { return net.ParseIP(hostname) != nil } ================================================ FILE: pkg/tcp/textproto.go ================================================ package tcp import ( "bufio" "errors" "fmt" "io" "net/textproto" "net/url" "strconv" "strings" ) const EndLine = "\r\n" // Response like http.Response, but with any proto type Response struct { Status string StatusCode int Proto string Header textproto.MIMEHeader Body []byte Request *Request } func (r Response) String() string { s := r.Proto + " " + r.Status + EndLine for k, v := range r.Header { s += k + ": " + v[0] + EndLine } s += EndLine if r.Body != nil { s += string(r.Body) } return s } func (r *Response) Write(w io.Writer) (err error) { _, err = w.Write([]byte(r.String())) return } func ReadResponse(r *bufio.Reader) (*Response, error) { tp := textproto.NewReader(r) line, err := tp.ReadLine() if err != nil { return nil, err } if line == "" { return nil, errors.New("empty response on RTSP request") } ss := strings.SplitN(line, " ", 3) if len(ss) != 3 { return nil, fmt.Errorf("malformed response: %s", line) } res := &Response{ Status: ss[1] + " " + ss[2], Proto: ss[0], } res.StatusCode, err = strconv.Atoi(ss[1]) if err != nil { return nil, err } res.Header, err = tp.ReadMIMEHeader() if err != nil { return nil, err } if val := res.Header.Get("Content-Length"); val != "" { var i int i, err = strconv.Atoi(val) res.Body = make([]byte, i) if _, err = io.ReadAtLeast(r, res.Body, i); err != nil { return nil, err } } return res, nil } // Request like http.Request, but with any proto type Request struct { Method string URL *url.URL Proto string Header textproto.MIMEHeader Body []byte } func (r *Request) String() string { s := r.Method + " " + r.URL.String() + " " + r.Proto + EndLine for k, v := range r.Header { s += k + ": " + v[0] + EndLine } s += EndLine if r.Body != nil { s += string(r.Body) } return s } func (r *Request) Write(w io.Writer) (err error) { _, err = w.Write([]byte(r.String())) return } func ReadRequest(r *bufio.Reader) (*Request, error) { tp := textproto.NewReader(r) line, err := tp.ReadLine() if err != nil { return nil, err } ss := strings.SplitN(line, " ", 3) if len(ss) != 3 { return nil, fmt.Errorf("wrong request: %s", line) } req := &Request{ Method: ss[0], Proto: ss[2], } req.URL, err = url.Parse(ss[1]) if err != nil { return nil, err } req.Header, err = tp.ReadMIMEHeader() if err != nil { return nil, err } if val := req.Header.Get("Content-Length"); val != "" { var i int i, err = strconv.Atoi(val) req.Body = make([]byte, i) if _, err = io.ReadAtLeast(r, req.Body, i); err != nil { return nil, err } } return req, nil } ================================================ FILE: pkg/tcp/textproto_test.go ================================================ package tcp import ( "bufio" "bytes" "net/http" "testing" ) func assert(t *testing.T, one, two any) { if one != two { t.FailNow() } } func TestName(t *testing.T) { data := []byte(`RTSP/1.0 401 Unauthorized WWW-Authenticate: Digest realm="testrealm@host.com", nonce="dcd98b7102dd2f0e8b11d0f600bfb0c093", `) buf := bytes.NewBuffer(data) r := bufio.NewReader(buf) res, err := ReadResponse(r) assert(t, err, nil) assert(t, res.StatusCode, http.StatusUnauthorized) } ================================================ FILE: pkg/tcp/websocket/client.go ================================================ package websocket import ( cryptorand "crypto/rand" "encoding/binary" "fmt" "io" "net" "time" ) const BinaryMessage = 2 type Client struct { conn net.Conn remain int } func NewClient(conn net.Conn) *Client { return &Client{conn: conn} } const finalBit = 0x80 const maskBit = 0x80 func (w *Client) Read(b []byte) (n int, err error) { if w.remain == 0 { b2 := make([]byte, 2) if _, err = io.ReadFull(w.conn, b2); err != nil { return 0, err } frameType := b2[0] & 0xF w.remain = int(b2[1] & 0x7F) switch frameType { case BinaryMessage: default: return 0, fmt.Errorf("unsupported frame type: %d", frameType) } switch w.remain { case 126: if _, err = io.ReadFull(w.conn, b2); err != nil { return 0, err } w.remain = int(binary.BigEndian.Uint16(b2)) case 127: b8 := make([]byte, 8) if _, err = io.ReadFull(w.conn, b8); err != nil { return 0, err } w.remain = int(binary.BigEndian.Uint64(b8)) } } if w.remain > len(b) { n, err = io.ReadFull(w.conn, b) w.remain -= n return } n, err = io.ReadFull(w.conn, b[:w.remain]) w.remain = 0 return } func (w *Client) Write(b []byte) (n int, err error) { var data []byte var start byte size := len(b) switch { case size > 65535: start = 10 data = make([]byte, size+14) data[1] = maskBit | 127 binary.BigEndian.PutUint64(data[2:], uint64(size)) case size > 125: start = 4 data = make([]byte, size+8) data[1] = maskBit | 126 binary.BigEndian.PutUint16(data[2:], uint16(size)) default: start = 2 data = make([]byte, size+6) data[1] = maskBit | byte(size) } data[0] = BinaryMessage | finalBit mask := data[start : start+4] msg := data[start+4:] if _, err = cryptorand.Read(mask); err != nil { return 0, err } for i := 0; i < len(b); i++ { msg[i] = b[i] ^ mask[i%4] } return w.conn.Write(data) } func (w *Client) Close() error { return w.conn.Close() } func (w *Client) LocalAddr() net.Addr { return w.conn.LocalAddr() } func (w *Client) RemoteAddr() net.Addr { return w.conn.RemoteAddr() } func (w *Client) SetDeadline(t time.Time) error { return w.conn.SetDeadline(t) } func (w *Client) SetReadDeadline(t time.Time) error { return w.conn.SetReadDeadline(t) } func (w *Client) SetWriteDeadline(t time.Time) error { return w.conn.SetWriteDeadline(t) } ================================================ FILE: pkg/tcp/websocket/dial.go ================================================ package websocket import ( cryptorand "crypto/rand" "crypto/sha1" "encoding/base64" "errors" "net" "net/http" "strings" "github.com/AlexxIT/go2rtc/pkg/tcp" ) func Dial(address string) (net.Conn, error) { if strings.HasPrefix(address, "ws") { address = "http" + address[2:] // support http and https } // using custom client for support Digest Auth // https://github.com/AlexxIT/go2rtc/issues/415 ctx, pconn := tcp.WithConn() req, err := http.NewRequestWithContext(ctx, "GET", address, nil) if err != nil { return nil, err } key, accept := GetKeyAccept() // Version, Key, Protocol important for Axis cameras req.Header.Set("Connection", "Upgrade") req.Header.Set("Upgrade", "websocket") req.Header.Set("Sec-WebSocket-Version", "13") req.Header.Set("Sec-WebSocket-Key", key) req.Header.Set("Sec-WebSocket-Protocol", "binary") res, err := tcp.Do(req) if err != nil { return nil, err } if res.StatusCode != http.StatusSwitchingProtocols { return nil, errors.New("wrong status: " + res.Status) } if res.Header.Get("Sec-Websocket-Accept") != accept { return nil, errors.New("wrong websocket accept") } return NewClient(*pconn), nil } func GetKeyAccept() (key, accept string) { b := make([]byte, 16) _, _ = cryptorand.Read(b) key = base64.StdEncoding.EncodeToString(b) h := sha1.New() h.Write([]byte(key)) h.Write([]byte("258EAFA5-E914-47DA-95CA-C5AB0DC85B11")) accept = base64.StdEncoding.EncodeToString(h.Sum(nil)) return } ================================================ FILE: pkg/tutk/codec.go ================================================ package tutk // https://github.com/seydx/tutk_wyze#11-codec-reference const ( CodecMPEG4 byte = 0x4C CodecH263 byte = 0x4D CodecH264 byte = 0x4E CodecMJPEG byte = 0x4F CodecH265 byte = 0x50 ) const ( CodecAACRaw byte = 0x86 CodecAACADTS byte = 0x87 CodecAACLATM byte = 0x88 CodecPCMU byte = 0x89 CodecPCMA byte = 0x8A CodecADPCM byte = 0x8B CodecPCML byte = 0x8C CodecSPEEX byte = 0x8D CodecMP3 byte = 0x8E CodecG726 byte = 0x8F CodecAACAlt byte = 0x90 CodecOpus byte = 0x92 ) var sampleRates = [9]uint32{8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000} func GetSampleRateIndex(sampleRate uint32) uint8 { for i, rate := range sampleRates { if rate == sampleRate { return uint8(i) } } return 3 // default 16kHz } func GetSamplesPerFrame(codecID byte) uint32 { switch codecID { case CodecAACRaw, CodecAACADTS, CodecAACLATM, CodecAACAlt: return 1024 case CodecPCMU, CodecPCMA, CodecPCML, CodecADPCM, CodecSPEEX, CodecG726: return 160 case CodecMP3: return 1152 case CodecOpus: return 960 default: return 1024 } } func IsVideoCodec(id byte) bool { return id >= CodecMPEG4 && id <= CodecH265 } func IsAudioCodec(id byte) bool { return id >= CodecAACRaw && id <= CodecOpus } ================================================ FILE: pkg/tutk/conn.go ================================================ package tutk import ( "fmt" "io" "net" "sync" "sync/atomic" "time" ) func Dial(host, uid, username, password string) (*Conn, error) { addr, err := net.ResolveUDPAddr("udp", host) if err != nil { // Default port for listening incoming LAN connections. // Important. It's not using for real connection. addr = &net.UDPAddr{IP: net.ParseIP(host), Port: 32761} } udpConn, err := net.ListenUDP("udp", nil) if err != nil { return nil, err } c := &Conn{UDPConn: udpConn, addr: addr} sid := GenSessionID() _ = c.SetDeadline(time.Now().Add(5 * time.Second)) if addr.Port != 10001 { err = c.connectDirect(uid, sid) } else { err = c.connectRemote(uid, sid) } if err != nil { _ = c.Close() return nil, err } if c.ver[0] >= 25 { c.session = NewSession25(c, sid) } else { c.session = NewSession16(c, sid) } if err = c.clientStart(username, password); err != nil { _ = c.Close() return nil, err } go c.worker() return c, nil } type Conn struct { *net.UDPConn addr *net.UDPAddr session Session ver []byte err error cmdMu sync.Mutex cmdAck func() } // Read overwrite net.Conn func (c *Conn) Read(buf []byte) (n int, err error) { for { var addr *net.UDPAddr if n, addr, err = c.UDPConn.ReadFromUDP(buf); err != nil { return 0, err } if string(c.addr.IP) != string(addr.IP) || n < 16 { continue // skip messages from another IP } if c.addr.Port != addr.Port { c.addr.Port = addr.Port } ReverseTransCodePartial(buf, buf[:n]) //log.Printf("<- %x", buf[:n]) return n, nil } } // Write overwrite net.Conn func (c *Conn) Write(b []byte) (n int, err error) { //log.Printf("-> %x", b) return c.UDPConn.WriteToUDP(TransCodePartial(nil, b), c.addr) } // RemoteAddr overwrite net.Conn func (c *Conn) RemoteAddr() net.Addr { return c.addr } func (c *Conn) Protocol() string { return "tutk+udp" } func (c *Conn) Version() string { if len(c.ver) == 1 { return fmt.Sprintf("TUTK/%d", c.ver[0]) } return fmt.Sprintf("TUTK/%d SDK %d.%d.%d.%d", c.ver[0], c.ver[1], c.ver[2], c.ver[3], c.ver[4]) } func (c *Conn) ReadCommand() (ctrlType uint32, ctrlData []byte, err error) { return c.session.RecvIOCtrl() } func (c *Conn) WriteCommand(ctrlType uint32, ctrlData []byte) error { c.cmdMu.Lock() defer c.cmdMu.Unlock() var repeat atomic.Int32 repeat.Store(5) timeout := time.NewTicker(time.Second) defer timeout.Stop() c.cmdAck = func() { repeat.Store(0) timeout.Reset(1) } buf := c.session.SendIOCtrl(ctrlType, ctrlData) for { if err := c.session.SessionWrite(0, buf); err != nil { return err } <-timeout.C r := repeat.Add(-1) if r < 0 { return nil } if r == 0 { return fmt.Errorf("%s: can't send command %d", "tutk", ctrlType) } } } func (c *Conn) ReadPacket() (hdr, payload []byte, err error) { return c.session.RecvFrameData() } func (c *Conn) WritePacket(hdr, payload []byte) error { buf := c.session.SendFrameData(hdr, payload) return c.session.SessionWrite(1, buf) } func (c *Conn) Error() error { if c.err != nil { return c.err } return io.EOF } func (c *Conn) worker() { defer c.session.Close() buf := make([]byte, 1200) for { n, err := c.Read(buf) if err != nil { c.err = fmt.Errorf("%s: %w", "tutk", err) return } switch c.handleMsg(buf[:n]) { case msgUnknown: fmt.Printf("tutk: unknown msg: %x\n", buf[:n]) case msgError: return case msgCommandAck: if c.cmdAck != nil { c.cmdAck() } } } } const ( msgUnknown = iota msgError msgPing msgUnknownPing msgClientStart msgClientStart2 msgClientStartAck2 msgCommand msgCommandAck msgCounters msgMediaChunk msgMediaFrame msgMediaReorder msgMediaLost msgCh5 msgUnknown0007 // time sync without data? msgUnknown0008 // time sync with data? msgUnknown0010 msgUnknown0013 msgUnknown0900 msgUnknown0a08 msgUnknownCh1c msgDafang0012 ) func (c *Conn) handleMsg(msg []byte) int { // off sample // 0 0402 tutk magic // 2 120a tutk version (120a, 190a...) // 4 0800 msg size = len(b)-16 // 6 0000 channel seq // 8 28041200 msg type // 14 0100 channel (not all msg) // 28 0700 msg data (not all msg) switch msg[8] { case 0x08: switch ch := msg[14]; ch { case 0, 1: return c.session.SessionRead(ch, msg[28:]) case 5: if len(msg) == 48 { _, _ = c.Write(msgAckCh5(msg)) return msgCh5 } case 0x1c: return msgUnknownCh1c } case 0x18: return msgUnknownPing case 0x28: if len(msg) == 24 { _, _ = c.Write(msgAckPing(msg)) return msgPing } } return msgUnknown } func msgAckPing(msg []byte) []byte { // <- [24] 0402120a 08000000 28041200 000000005b0d4202070aa8c0 // -> [24] 04021a0a 08000000 27042100 000000005b0d4202070aa8c0 msg[8] = 0x27 msg[10] = 0x21 return msg } func msgAckCh5(msg []byte) []byte { // <- [48] 0402190a 20000400 07042100 7ecc05000c0000007ecc93c456c2561f 5a97c2f101050000000000000000000000010000 // -> [48] 0402190a 20000400 08041200 7ecc05000c0000007ecc93c456c2561f 5a97c2f141050000000000000000000000010000 msg[8] = 0x07 msg[10] = 0x21 msg[32] = 0x41 return msg } ================================================ FILE: pkg/tutk/crypto.go ================================================ package tutk import ( "encoding/binary" "math/bits" ) // I'd like to say hello to Charlie. Your name is forever etched into the history of streaming software. const charlie = "Charlie is the designer of P2P!!" func ReverseTransCodePartial(dst, src []byte) []byte { n := len(src) tmp := make([]byte, n) if len(dst) < n { dst = make([]byte, n) } src16 := src tmp16 := tmp dst16 := dst for ; n >= 16; n -= 16 { for i := 0; i != 16; i += 4 { x := binary.LittleEndian.Uint32(src16[i:]) binary.LittleEndian.PutUint32(tmp16[i:], bits.RotateLeft32(x, i+3)) } swap(dst16, tmp16, 16) for i := 0; i != 16; i++ { tmp16[i] = dst16[i] ^ charlie[i] } for i := 0; i != 16; i += 4 { x := binary.LittleEndian.Uint32(tmp16[i:]) binary.LittleEndian.PutUint32(dst16[i:], bits.RotateLeft32(x, i+1)) } tmp16 = tmp16[16:] dst16 = dst16[16:] src16 = src16[16:] } swap(tmp16, src16, n) for i := 0; i < n; i++ { dst16[i] = tmp16[i] ^ charlie[i] } return dst } func ReverseTransCodeBlob(src []byte) []byte { if len(src) < 16 { return ReverseTransCodePartial(nil, src) } dst := make([]byte, len(src)) header := ReverseTransCodePartial(nil, src[:16]) copy(dst, header) if len(src) > 16 { if dst[3]&1 != 0 { // Partial encryption (check decrypted header) remaining := len(src) - 16 decryptLen := min(remaining, 48) if decryptLen > 0 { decrypted := ReverseTransCodePartial(nil, src[16:16+decryptLen]) copy(dst[16:], decrypted) } if remaining > 48 { copy(dst[64:], src[64:]) } } else { // Full decryption decrypted := ReverseTransCodePartial(nil, src[16:]) copy(dst[16:], decrypted) } } return dst } func TransCodePartial(dst, src []byte) []byte { n := len(src) tmp := make([]byte, n) if len(dst) < n { dst = make([]byte, n) } src16 := src tmp16 := tmp dst16 := dst for ; n >= 16; n -= 16 { for i := 0; i != 16; i += 4 { x := binary.LittleEndian.Uint32(src16[i:]) binary.LittleEndian.PutUint32(tmp16[i:], bits.RotateLeft32(x, -i-1)) } for i := 0; i != 16; i++ { dst16[i] = tmp16[i] ^ charlie[i] } swap(tmp16, dst16, 16) for i := 0; i != 16; i += 4 { x := binary.LittleEndian.Uint32(tmp16[i:]) binary.LittleEndian.PutUint32(dst16[i:], bits.RotateLeft32(x, -i-3)) } tmp16 = tmp16[16:] dst16 = dst16[16:] src16 = src16[16:] } for i := 0; i < n; i++ { tmp16[i] = src16[i] ^ charlie[i] } swap(dst16, tmp16, n) return dst } func TransCodeBlob(src []byte) []byte { if len(src) < 16 { return TransCodePartial(nil, src) } dst := make([]byte, len(src)) header := TransCodePartial(nil, src[:16]) copy(dst, header) if len(src) > 16 { if src[3]&1 != 0 { // Partial encryption remaining := len(src) - 16 encryptLen := min(remaining, 48) if encryptLen > 0 { encrypted := TransCodePartial(nil, src[16:16+encryptLen]) copy(dst[16:], encrypted) } if remaining > 48 { copy(dst[64:], src[64:]) } } else { // Full encryption encrypted := TransCodePartial(nil, src[16:]) copy(dst[16:], encrypted) } } return dst } func swap(dst, src []byte, n int) { switch n { case 2: _, _ = src[1], dst[1] dst[0] = src[1] dst[1] = src[0] return case 4: _, _ = src[3], dst[3] dst[0] = src[2] dst[1] = src[3] dst[2] = src[0] dst[3] = src[1] return case 8: _, _ = src[7], dst[7] dst[0] = src[7] dst[1] = src[4] dst[2] = src[3] dst[3] = src[2] dst[4] = src[1] dst[5] = src[6] dst[6] = src[5] dst[7] = src[0] return case 16: _, _ = src[15], dst[15] dst[0] = src[11] dst[1] = src[9] dst[2] = src[8] dst[3] = src[15] dst[4] = src[13] dst[5] = src[10] dst[6] = src[12] dst[7] = src[14] dst[8] = src[2] dst[9] = src[1] dst[10] = src[5] dst[11] = src[0] dst[12] = src[6] dst[13] = src[4] dst[14] = src[7] dst[15] = src[3] return } copy(dst, src[:n]) } const delta = 0x9e3779b9 func XXTEADecrypt(dst, src, key []byte) { const n = int8(4) // support only 16 bytes src var w, k [n]uint32 for i := int8(0); i < n; i++ { w[i] = binary.LittleEndian.Uint32(src) k[i] = binary.LittleEndian.Uint32(key) src = src[4:] key = key[4:] } rounds := 52/n + 6 sum := uint32(rounds) * delta for ; rounds > 0; rounds-- { w0 := w[0] i2 := int8((sum >> 2) & 3) for i := n - 1; i >= 0; i-- { wi := w[(i-1)&3] ki := k[i^i2] t1 := (w0 ^ sum) + (wi ^ ki) t2 := (wi >> 5) ^ (w0 << 2) t3 := (w0 >> 3) ^ (wi << 4) w[i] -= t1 ^ (t2 + t3) w0 = w[i] } sum -= delta } for _, i := range w { binary.LittleEndian.PutUint32(dst, i) dst = dst[4:] } } func XXTEADecryptVar(data, key []byte) []byte { if len(data) < 8 || len(key) < 16 { return nil } k := make([]uint32, 4) for i := range 4 { k[i] = binary.LittleEndian.Uint32(key[i*4:]) } n := max(len(data)/4, 2) v := make([]uint32, n) for i := 0; i < len(data)/4; i++ { v[i] = binary.LittleEndian.Uint32(data[i*4:]) } rounds := 6 + 52/n sum := uint32(rounds) * delta y := v[0] for rounds > 0 { e := (sum >> 2) & 3 for p := n - 1; p > 0; p-- { z := v[p-1] v[p] -= xxteaMX(sum, y, z, p, e, k) y = v[p] } z := v[n-1] v[0] -= xxteaMX(sum, y, z, 0, e, k) y = v[0] sum -= delta rounds-- } result := make([]byte, n*4) for i := range n { binary.LittleEndian.PutUint32(result[i*4:], v[i]) } return result[:len(data)] } func xxteaMX(sum, y, z uint32, p int, e uint32, k []uint32) uint32 { return ((z>>5 ^ y<<2) + (y>>3 ^ z<<4)) ^ ((sum ^ y) + (k[(p&3)^int(e)] ^ z)) } ================================================ FILE: pkg/tutk/crypto_test.go ================================================ package tutk import ( "testing" "github.com/stretchr/testify/require" ) func TestXXTEADecrypt(t *testing.T) { buf := []byte("WERhJxb87WF3zgPa") key := []byte("GAgDiwVPg2E4GMke") XXTEADecrypt(buf, buf, key) require.Equal(t, "\xc4\xa6\x2c\xa1\x10\x64\x17\xa5\xda\x02\xe1\x62\xa5\xf0\x62\x71", string(buf)) } ================================================ FILE: pkg/tutk/dtls/auth.go ================================================ package dtls import ( "crypto/sha256" "encoding/base64" "strings" ) func CalculateAuthKey(enr, mac string) []byte { data := enr + strings.ToUpper(mac) hash := sha256.Sum256([]byte(data)) b64 := base64.StdEncoding.EncodeToString(hash[:6]) b64 = strings.ReplaceAll(b64, "+", "Z") b64 = strings.ReplaceAll(b64, "/", "9") b64 = strings.ReplaceAll(b64, "=", "A") return []byte(b64) } func DerivePSK(enr string) []byte { // DerivePSK derives the DTLS PSK from ENR // TUTK SDK treats the PSK as a NULL-terminated C string, so if SHA256(ENR) // contains a 0x00 byte, the PSK is truncated at that position. hash := sha256.Sum256([]byte(enr)) pskLen := 32 for i := range 32 { if hash[i] == 0x00 { pskLen = i break } } psk := make([]byte, 32) copy(psk[:pskLen], hash[:pskLen]) return psk } ================================================ FILE: pkg/tutk/dtls/cipher.go ================================================ package dtls import ( "crypto/cipher" "crypto/sha256" "encoding/binary" "errors" "fmt" "hash" "sync/atomic" "github.com/pion/dtls/v3" "github.com/pion/dtls/v3/pkg/crypto/clientcertificate" "github.com/pion/dtls/v3/pkg/crypto/prf" "github.com/pion/dtls/v3/pkg/protocol" "github.com/pion/dtls/v3/pkg/protocol/recordlayer" "golang.org/x/crypto/chacha20poly1305" ) const CipherSuiteID_CCAC dtls.CipherSuiteID = 0xCCAC const ( chachaTagLength = 16 chachaNonceLength = 12 ) var ( errDecryptPacket = &protocol.TemporaryError{Err: errors.New("failed to decrypt packet")} errCipherSuiteNotInit = &protocol.TemporaryError{Err: errors.New("CipherSuite not initialized")} ) type ChaCha20Poly1305Cipher struct { localCipher, remoteCipher cipher.AEAD localWriteIV, remoteWriteIV []byte } func NewChaCha20Poly1305Cipher(localKey, localWriteIV, remoteKey, remoteWriteIV []byte) (*ChaCha20Poly1305Cipher, error) { localCipher, err := chacha20poly1305.New(localKey) if err != nil { return nil, err } remoteCipher, err := chacha20poly1305.New(remoteKey) if err != nil { return nil, err } return &ChaCha20Poly1305Cipher{ localCipher: localCipher, localWriteIV: localWriteIV, remoteCipher: remoteCipher, remoteWriteIV: remoteWriteIV, }, nil } func generateAEADAdditionalData(h *recordlayer.Header, payloadLen int) []byte { var additionalData [13]byte binary.BigEndian.PutUint64(additionalData[:], h.SequenceNumber) binary.BigEndian.PutUint16(additionalData[:], h.Epoch) additionalData[8] = byte(h.ContentType) additionalData[9] = h.Version.Major additionalData[10] = h.Version.Minor binary.BigEndian.PutUint16(additionalData[11:], uint16(payloadLen)) return additionalData[:] } func computeNonce(iv []byte, epoch uint16, sequenceNumber uint64) []byte { nonce := make([]byte, chachaNonceLength) binary.BigEndian.PutUint64(nonce[4:], sequenceNumber) binary.BigEndian.PutUint16(nonce[4:], epoch) for i := range chachaNonceLength { nonce[i] ^= iv[i] } return nonce } func (c *ChaCha20Poly1305Cipher) Encrypt(pkt *recordlayer.RecordLayer, raw []byte) ([]byte, error) { payload := raw[pkt.Header.Size():] raw = raw[:pkt.Header.Size()] nonce := computeNonce(c.localWriteIV, pkt.Header.Epoch, pkt.Header.SequenceNumber) additionalData := generateAEADAdditionalData(&pkt.Header, len(payload)) encryptedPayload := c.localCipher.Seal(nil, nonce, payload, additionalData) r := make([]byte, len(raw)+len(encryptedPayload)) copy(r, raw) copy(r[len(raw):], encryptedPayload) binary.BigEndian.PutUint16(r[pkt.Header.Size()-2:], uint16(len(r)-pkt.Header.Size())) return r, nil } func (c *ChaCha20Poly1305Cipher) Decrypt(header recordlayer.Header, in []byte) ([]byte, error) { err := header.Unmarshal(in) switch { case err != nil: return nil, err case header.ContentType == protocol.ContentTypeChangeCipherSpec: return in, nil case len(in) <= header.Size()+chachaTagLength: return nil, fmt.Errorf("ciphertext too short: %d <= %d", len(in), header.Size()+chachaTagLength) } nonce := computeNonce(c.remoteWriteIV, header.Epoch, header.SequenceNumber) out := in[header.Size():] additionalData := generateAEADAdditionalData(&header, len(out)-chachaTagLength) out, err = c.remoteCipher.Open(out[:0], nonce, out, additionalData) if err != nil { return nil, fmt.Errorf("%w: %v", errDecryptPacket, err) } return append(in[:header.Size()], out...), nil } type TLSEcdhePskWithChacha20Poly1305Sha256 struct { aead atomic.Value } func NewTLSEcdhePskWithChacha20Poly1305Sha256() *TLSEcdhePskWithChacha20Poly1305Sha256 { return &TLSEcdhePskWithChacha20Poly1305Sha256{} } func (c *TLSEcdhePskWithChacha20Poly1305Sha256) CertificateType() clientcertificate.Type { return clientcertificate.Type(0) } func (c *TLSEcdhePskWithChacha20Poly1305Sha256) KeyExchangeAlgorithm() dtls.CipherSuiteKeyExchangeAlgorithm { return dtls.CipherSuiteKeyExchangeAlgorithmPsk | dtls.CipherSuiteKeyExchangeAlgorithmEcdhe } func (c *TLSEcdhePskWithChacha20Poly1305Sha256) ECC() bool { return true } func (c *TLSEcdhePskWithChacha20Poly1305Sha256) ID() dtls.CipherSuiteID { return CipherSuiteID_CCAC } func (c *TLSEcdhePskWithChacha20Poly1305Sha256) String() string { return "TLS_ECDHE_PSK_WITH_CHACHA20_POLY1305_SHA256" } func (c *TLSEcdhePskWithChacha20Poly1305Sha256) HashFunc() func() hash.Hash { return sha256.New } func (c *TLSEcdhePskWithChacha20Poly1305Sha256) AuthenticationType() dtls.CipherSuiteAuthenticationType { return dtls.CipherSuiteAuthenticationTypePreSharedKey } func (c *TLSEcdhePskWithChacha20Poly1305Sha256) IsInitialized() bool { return c.aead.Load() != nil } func (c *TLSEcdhePskWithChacha20Poly1305Sha256) Init(masterSecret, clientRandom, serverRandom []byte, isClient bool) error { const ( prfMacLen = 0 prfKeyLen = 32 prfIvLen = 12 ) keys, err := prf.GenerateEncryptionKeys( masterSecret, clientRandom, serverRandom, prfMacLen, prfKeyLen, prfIvLen, c.HashFunc(), ) if err != nil { return err } var aead *ChaCha20Poly1305Cipher if isClient { aead, err = NewChaCha20Poly1305Cipher( keys.ClientWriteKey, keys.ClientWriteIV, keys.ServerWriteKey, keys.ServerWriteIV, ) } else { aead, err = NewChaCha20Poly1305Cipher( keys.ServerWriteKey, keys.ServerWriteIV, keys.ClientWriteKey, keys.ClientWriteIV, ) } if err != nil { return err } c.aead.Store(aead) return nil } func (c *TLSEcdhePskWithChacha20Poly1305Sha256) Encrypt(pkt *recordlayer.RecordLayer, raw []byte) ([]byte, error) { aead, ok := c.aead.Load().(*ChaCha20Poly1305Cipher) if !ok { return nil, fmt.Errorf("%w: unable to encrypt", errCipherSuiteNotInit) } return aead.Encrypt(pkt, raw) } func (c *TLSEcdhePskWithChacha20Poly1305Sha256) Decrypt(h recordlayer.Header, raw []byte) ([]byte, error) { aead, ok := c.aead.Load().(*ChaCha20Poly1305Cipher) if !ok { return nil, fmt.Errorf("%w: unable to decrypt", errCipherSuiteNotInit) } return aead.Decrypt(h, raw) } func CustomCipherSuites() []dtls.CipherSuite { return []dtls.CipherSuite{ NewTLSEcdhePskWithChacha20Poly1305Sha256(), } } ================================================ FILE: pkg/tutk/dtls/conn_dtls.go ================================================ package dtls import ( "context" "crypto/hmac" "crypto/sha1" "encoding/binary" "fmt" "io" "net" "sync" "time" "github.com/AlexxIT/go2rtc/pkg/tutk" "github.com/pion/dtls/v3" ) const ( magicCC51 = "\x51\xcc" // (wyze specific?) sdkVersion42 = "\x01\x01\x02\x04" // 4.2.1.1 sdkVersion43 = "\x00\x08\x03\x04" // 4.3.8.0 ) const ( cmdDiscoReq uint16 = 0x0601 cmdDiscoRes uint16 = 0x0602 cmdSessionReq uint16 = 0x0402 cmdSessionRes uint16 = 0x0404 cmdDataTX uint16 = 0x0407 cmdDataRX uint16 = 0x0408 cmdKeepaliveReq uint16 = 0x0427 cmdKeepaliveRes uint16 = 0x0428 headerSize = 16 discoBodySize = 72 discoSize = headerSize + discoBodySize sessionBody = 36 sessionSize = headerSize + sessionBody ) const ( cmdDiscoCC51 uint16 = 0x1002 cmdKeepaliveCC51 uint16 = 0x1202 cmdDTLSCC51 uint16 = 0x1502 payloadSizeCC51 uint16 = 0x0028 packetSizeCC51 = 52 headerSizeCC51 = 28 authSizeCC51 = 20 keepaliveSizeCC51 = 48 ) const ( magicAVLoginResp uint16 = 0x2100 magicIOCtrl uint16 = 0x7000 magicChannelMsg uint16 = 0x1000 magicACK uint16 = 0x0009 magicAVLogin1 uint16 = 0x0000 magicAVLogin2 uint16 = 0x2000 ) const ( protoVersion uint16 = 0x000c defaultCaps uint32 = 0x001f07fb ) const ( iotcChannelMain = 0 // Main AV (we = DTLS Client) iotcChannelBack = 1 // Backchannel (we = DTLS Server) ) type DTLSConn struct { conn *net.UDPConn addr *net.UDPAddr frames *tutk.FrameHandler err error verbose bool ctx context.Context cancel context.CancelFunc wg sync.WaitGroup mu sync.RWMutex // DTLS clientConn *dtls.Conn serverConn *dtls.Conn clientBuf chan []byte serverBuf chan []byte rawCmd chan []byte // Identity uid string authKey string enr string psk []byte // Session sid []byte ticket uint16 hasTwoWayStreaming bool // Protocol isCC51 bool seq uint16 seqCmd uint16 avSeq uint32 kaSeq uint32 audioSeq uint32 audioFrameNo uint32 // Ack ackFlags uint16 rxSeqStart uint16 rxSeqEnd uint16 rxSeqInit bool cmdAck func() } func DialDTLS(host string, port int, uid, authKey, enr string, verbose bool) (*DTLSConn, error) { udp, err := net.ListenUDP("udp", nil) if err != nil { return nil, err } _ = udp.SetReadBuffer(2 * 1024 * 1024) ctx, cancel := context.WithCancel(context.Background()) psk := DerivePSK(enr) if port == 0 { port = 32761 } c := &DTLSConn{ conn: udp, addr: &net.UDPAddr{IP: net.ParseIP(host), Port: port}, uid: uid, authKey: authKey, enr: enr, psk: psk, verbose: verbose, ctx: ctx, cancel: cancel, rxSeqStart: 0xffff, rxSeqEnd: 0xffff, } if err = c.discovery(); err != nil { _ = c.Close() return nil, err } c.clientBuf = make(chan []byte, 64) c.serverBuf = make(chan []byte, 64) c.rawCmd = make(chan []byte, 16) c.frames = tutk.NewFrameHandler(c.verbose) c.wg.Add(1) go c.reader() if err = c.connect(); err != nil { _ = c.Close() return nil, err } c.wg.Add(1) go c.worker() return c, nil } func (c *DTLSConn) AVClientStart(timeout time.Duration) error { randomID := tutk.GenSessionID() pkt1 := c.msgAVLogin(magicAVLogin1, 570, 0x0001, randomID) pkt2 := c.msgAVLogin(magicAVLogin2, 572, 0x0000, randomID) pkt2[20]++ // pkt2 has randomID incremented by 1 if _, err := c.clientConn.Write(pkt1); err != nil { return fmt.Errorf("av login 1 failed: %w", err) } time.Sleep(10 * time.Millisecond) if _, err := c.clientConn.Write(pkt2); err != nil { return fmt.Errorf("av login 2 failed: %w", err) } // Wait for response timer := time.NewTimer(timeout) defer timer.Stop() for { select { case data, ok := <-c.rawCmd: if !ok { return io.EOF } if len(data) >= 32 && binary.LittleEndian.Uint16(data) == magicAVLoginResp { c.hasTwoWayStreaming = data[31] == 1 ack := c.msgACK() c.clientConn.Write(ack) // Start ACK sender for continuous streaming c.wg.Add(1) go func() { defer c.wg.Done() ackTicker := time.NewTicker(100 * time.Millisecond) defer ackTicker.Stop() for { select { case <-c.ctx.Done(): return case <-ackTicker.C: if c.clientConn != nil { ack := c.msgACK() c.clientConn.Write(ack) } } } }() return nil } case <-timer.C: return context.DeadlineExceeded } } } func (c *DTLSConn) AVServStart() error { conn, err := NewDTLSServer(c.ctx, iotcChannelBack, c.addr, c.WriteDTLS, c.serverBuf, c.psk) if err != nil { return fmt.Errorf("dtls: server handshake failed: %w", err) } if c.verbose { fmt.Printf("[DTLS] Server handshake complete on channel %d\n", iotcChannelBack) fmt.Printf("[SERVER] Waiting for AV Login request from camera...\n") } // Wait for AV Login request from camera buf := make([]byte, 1024) conn.SetReadDeadline(time.Now().Add(5 * time.Second)) n, err := conn.Read(buf) if err != nil { go conn.Close() return fmt.Errorf("read av login: %w", err) } if c.verbose { fmt.Printf("[SERVER] AV Login request len=%d data:\n%s", n, hexDump(buf[:n])) } if n < 24 { go conn.Close() return fmt.Errorf("av login too short: %d bytes", n) } checksum := binary.LittleEndian.Uint32(buf[20:]) resp := c.msgAVLoginResponse(checksum) if c.verbose { fmt.Printf("[SERVER] Sending AV Login response: %d bytes\n", len(resp)) } if _, err = conn.Write(resp); err != nil { go conn.Close() return fmt.Errorf("write av login response: %w", err) } if c.verbose { fmt.Printf("[SERVER] AV Login response sent, waiting for possible resend...\n") } // Camera may resend, respond again conn.SetReadDeadline(time.Now().Add(500 * time.Millisecond)) if n, _ = conn.Read(buf); n > 0 { if c.verbose { fmt.Printf("[SERVER] Received AV Login resend: %d bytes\n", n) } conn.Write(resp) } conn.SetReadDeadline(time.Time{}) if c.verbose { fmt.Printf("[SERVER] AV Login complete, ready for two way streaming\n") } c.mu.Lock() c.serverConn = conn c.mu.Unlock() return nil } func (c *DTLSConn) AVServStop() error { c.mu.Lock() serverConn := c.serverConn c.serverConn = nil // Reset audio TX state c.audioSeq = 0 c.audioFrameNo = 0 c.mu.Unlock() if serverConn == nil { return nil } go serverConn.Close() return nil } func (c *DTLSConn) AVRecvFrameData() (*tutk.Packet, error) { select { case pkt, ok := <-c.frames.Recv(): if !ok { return nil, c.Error() } return pkt, nil case <-c.ctx.Done(): return nil, c.Error() } } func (c *DTLSConn) AVSendAudioData(codec byte, payload []byte, timestampUS uint32, sampleRate uint32, channels uint8) error { c.mu.Lock() conn := c.serverConn if conn == nil { c.mu.Unlock() return fmt.Errorf("av server not ready") } frame := c.msgAudioFrame(payload, timestampUS, codec, sampleRate, channels) c.mu.Unlock() n, err := conn.Write(frame) if c.verbose { if err != nil { fmt.Printf("[SERVER TX] DTLS Write ERROR: %v\n", err) } else { fmt.Printf("[SERVER TX] len=%d, data:\n%s", n, hexDump(frame)) } } return err } func (c *DTLSConn) Write(data []byte) error { if c.isCC51 { _, err := c.conn.WriteToUDP(data, c.addr) return err } _, err := c.conn.WriteToUDP(tutk.TransCodeBlob(data), c.addr) return err } func (c *DTLSConn) WriteDTLS(payload []byte, channel byte) error { var frame []byte if c.isCC51 { frame = c.msgTxDataCC51(payload, channel) } else { frame = c.msgTxData(payload, channel) } return c.Write(frame) } func (c *DTLSConn) WriteIOCtrl(payload []byte) error { _, err := c.conn.Write(c.msgIOCtrl(payload)) return err } func (c *DTLSConn) WriteAndWait(req []byte, ok func(res []byte) bool) ([]byte, error) { var t *time.Timer t = time.AfterFunc(1, func() { if err := c.Write(req); err == nil && t != nil { t.Reset(time.Second) } }) defer t.Stop() _ = c.conn.SetDeadline(time.Now().Add(5 * time.Second)) defer c.conn.SetDeadline(time.Time{}) buf := make([]byte, 2048) for { n, addr, err := c.conn.ReadFromUDP(buf) if err != nil { return nil, err } if string(addr.IP) != string(c.addr.IP) || n < 16 { continue } var res []byte if c.isCC51 { res = buf[:n] } else { res = tutk.ReverseTransCodeBlob(buf[:n]) } if ok(res) { c.addr.Port = addr.Port return res, nil } } } func (c *DTLSConn) WriteAndWaitIOCtrl(payload []byte, match func([]byte) bool, timeout time.Duration) ([]byte, error) { frame := c.msgIOCtrl(payload) var t *time.Timer t = time.AfterFunc(1, func() { c.mu.RLock() conn := c.clientConn c.mu.RUnlock() if conn != nil { if _, err := conn.Write(frame); err == nil && t != nil { t.Reset(time.Second) } } }) defer t.Stop() timer := time.NewTimer(timeout) defer timer.Stop() for { select { case data, ok := <-c.rawCmd: if !ok { return nil, io.EOF } ack := c.msgACK() c.clientConn.Write(ack) if match(data) { return data, nil } case <-timer.C: return nil, fmt.Errorf("timeout waiting for response") } } } func (c *DTLSConn) HasTwoWayStreaming() bool { return c.hasTwoWayStreaming } func (c *DTLSConn) IsBackchannelReady() bool { c.mu.RLock() defer c.mu.RUnlock() return c.serverConn != nil } func (c *DTLSConn) RemoteAddr() *net.UDPAddr { return c.addr } func (c *DTLSConn) LocalAddr() *net.UDPAddr { return c.conn.LocalAddr().(*net.UDPAddr) } func (c *DTLSConn) SetDeadline(t time.Time) error { return c.conn.SetDeadline(t) } func (c *DTLSConn) Close() error { c.cancel() c.mu.Lock() if conn := c.serverConn; conn != nil { c.serverConn = nil go conn.Close() } if conn := c.clientConn; conn != nil { c.clientConn = nil go conn.Close() } if c.frames != nil { c.frames.Close() } c.mu.Unlock() c.wg.Wait() return c.conn.Close() } func (c *DTLSConn) Error() error { if c.err != nil { return c.err } return io.EOF } func (c *DTLSConn) discovery() error { c.sid = tutk.GenSessionID() pktIOTC := tutk.TransCodeBlob(c.msgDisco(1)) pktCC51 := c.msgDiscoCC51(0, 0, false) buf := make([]byte, 2048) deadline := time.Now().Add(5 * time.Second) for time.Now().Before(deadline) { c.conn.WriteToUDP(pktIOTC, c.addr) c.conn.WriteToUDP(pktCC51, c.addr) c.conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) n, addr, err := c.conn.ReadFromUDP(buf) if err != nil { continue } if !addr.IP.Equal(c.addr.IP) { continue } // CC51 protocol if n >= packetSizeCC51 && string(buf[:2]) == magicCC51 { if binary.LittleEndian.Uint16(buf[4:]) == cmdDiscoCC51 { c.addr, c.isCC51, c.ticket = addr, true, binary.LittleEndian.Uint16(buf[14:]) if n >= 24 { copy(c.sid, buf[16:24]) } return c.discoDoneCC51() } continue } // IOTC Protocol (Basis) data := tutk.ReverseTransCodeBlob(buf[:n]) if len(data) >= 16 && binary.LittleEndian.Uint16(data[8:]) == cmdDiscoRes { c.addr, c.isCC51 = addr, false return c.discoDone() } } return fmt.Errorf("discovery timeout") } func (c *DTLSConn) discoDone() error { c.Write(c.msgDisco(2)) time.Sleep(100 * time.Millisecond) _, err := c.WriteAndWait(c.msgSession(), func(res []byte) bool { return len(res) >= 16 && binary.LittleEndian.Uint16(res[8:]) == cmdSessionRes }) return err } func (c *DTLSConn) discoDoneCC51() error { _, err := c.WriteAndWait(c.msgDiscoCC51(2, c.ticket, false), func(res []byte) bool { if len(res) < packetSizeCC51 || string(res[:2]) != magicCC51 { return false } cmd := binary.LittleEndian.Uint16(res[4:]) dir := binary.LittleEndian.Uint16(res[8:]) seq := binary.LittleEndian.Uint16(res[12:]) return cmd == cmdDiscoCC51 && dir == 0xFFFF && seq == 3 }) return err } func (c *DTLSConn) connect() error { conn, err := NewDTLSClient(c.ctx, iotcChannelMain, c.addr, c.WriteDTLS, c.clientBuf, c.psk) if err != nil { return fmt.Errorf("dtls: client handshake failed: %w", err) } c.mu.Lock() c.clientConn = conn c.mu.Unlock() if c.verbose { fmt.Printf("[DTLS] Client handshake complete on channel %d\n", iotcChannelMain) } return nil } func (c *DTLSConn) worker() { defer c.wg.Done() buf := make([]byte, 2048) for { select { case <-c.ctx.Done(): return default: } n, err := c.clientConn.Read(buf) if err != nil { c.err = err return } if n < 2 { continue } data := buf[:n] magic := binary.LittleEndian.Uint16(data) if c.verbose { fmt.Printf("[DTLS RX] magic=0x%04x len=%d\n", magic, n) } switch magic { case magicAVLoginResp: c.queue(c.rawCmd, data) case magicIOCtrl, magicChannelMsg: c.queue(c.rawCmd, data) case protoVersion: // Seq-Tracking if len(data) >= 8 { seq := binary.LittleEndian.Uint16(data[4:]) if !c.rxSeqInit { c.rxSeqInit = true } if seq > c.rxSeqEnd || c.rxSeqEnd == 0xffff { c.rxSeqEnd = seq } } c.queue(c.rawCmd, data) case magicACK: c.mu.RLock() ack := c.cmdAck c.mu.RUnlock() if ack != nil { ack() } default: channel := data[0] if channel == tutk.ChannelAudio || channel == tutk.ChannelIVideo || channel == tutk.ChannelPVideo { c.frames.Handle(data) } } } } func (c *DTLSConn) reader() { defer c.wg.Done() buf := make([]byte, 2048) for { select { case <-c.ctx.Done(): return default: } c.conn.SetReadDeadline(time.Now().Add(100 * time.Millisecond)) n, addr, err := c.conn.ReadFromUDP(buf) if err != nil { if netErr, ok := err.(net.Error); ok && netErr.Timeout() { continue } return } if !addr.IP.Equal(c.addr.IP) { if c.verbose { fmt.Printf("Ignored packet from unknown IP: %s\n", addr.IP.String()) } continue } if addr.Port != c.addr.Port { c.addr.Port = addr.Port } // CC51 Protocol if c.isCC51 && n >= 12 && string(buf[:2]) == magicCC51 { cmd := binary.LittleEndian.Uint16(buf[4:]) switch cmd { case cmdKeepaliveCC51: if n >= keepaliveSizeCC51 { _ = c.Write(c.msgKeepaliveCC51()) } case cmdDTLSCC51: if n >= headerSizeCC51+authSizeCC51 { ch := byte(binary.LittleEndian.Uint16(buf[12:]) >> 8) dtlsData := buf[headerSizeCC51 : n-authSizeCC51] switch ch { case iotcChannelMain: c.queue(c.clientBuf, dtlsData) case iotcChannelBack: c.queue(c.serverBuf, dtlsData) } } } continue } // IOTC Protocol (Basis) data := tutk.ReverseTransCodeBlob(buf[:n]) if len(data) < 16 { continue } switch binary.LittleEndian.Uint16(data[8:]) { case cmdKeepaliveRes: if len(data) > 24 { _ = c.Write(c.msgKeepalive(data[16:])) } case cmdDataRX: if len(data) > 28 { ch := data[14] switch ch { case iotcChannelMain: c.queue(c.clientBuf, data[28:]) case iotcChannelBack: c.queue(c.serverBuf, data[28:]) } } } } } func (c *DTLSConn) queue(ch chan []byte, data []byte) { b := make([]byte, len(data)) copy(b, data) select { case ch <- b: default: select { case <-ch: default: } ch <- b } } func (c *DTLSConn) msgDisco(stage byte) []byte { b := make([]byte, discoSize) copy(b, "\x04\x02\x1a\x02") // marker + mode binary.LittleEndian.PutUint16(b[4:], discoBodySize) // body size binary.LittleEndian.PutUint16(b[8:], cmdDiscoReq) // 0x0601 binary.LittleEndian.PutUint16(b[10:], 0x0021) // flags body := b[headerSize:] copy(body[:20], c.uid) copy(body[36:], sdkVersion42) // SDK 4.2.1.1 copy(body[40:], c.sid) body[48] = stage if stage == 1 && len(c.authKey) > 0 { copy(body[58:], c.authKey) } return b } func (c *DTLSConn) msgDiscoCC51(seq, ticket uint16, isResponse bool) []byte { b := make([]byte, packetSizeCC51) copy(b[:2], magicCC51) binary.LittleEndian.PutUint16(b[4:], cmdDiscoCC51) // 0x1002 binary.LittleEndian.PutUint16(b[6:], payloadSizeCC51) // 40 bytes if isResponse { binary.LittleEndian.PutUint16(b[8:], 0xFFFF) // response } binary.LittleEndian.PutUint16(b[12:], seq) binary.LittleEndian.PutUint16(b[14:], ticket) copy(b[16:24], c.sid) copy(b[24:28], sdkVersion43) // SDK 4.3.8.0 b[28] = 0x1d // unknown field (capability/build flag?) h := hmac.New(sha1.New, append([]byte(c.uid), c.authKey...)) h.Write(b[:32]) copy(b[32:52], h.Sum(nil)) return b } func (c *DTLSConn) msgKeepaliveCC51() []byte { c.kaSeq += 2 b := make([]byte, keepaliveSizeCC51) copy(b[:2], magicCC51) binary.LittleEndian.PutUint16(b[4:], cmdKeepaliveCC51) // 0x1202 binary.LittleEndian.PutUint16(b[6:], 0x0024) // 36 bytes payload binary.LittleEndian.PutUint32(b[16:], c.kaSeq) // counter copy(b[20:28], c.sid) // session ID h := hmac.New(sha1.New, append([]byte(c.uid), c.authKey...)) h.Write(b[:28]) copy(b[28:48], h.Sum(nil)) return b } func (c *DTLSConn) msgSession() []byte { b := make([]byte, sessionSize) copy(b, "\x04\x02\x1a\x02") // marker + mode binary.LittleEndian.PutUint16(b[4:], sessionBody) // body size binary.LittleEndian.PutUint16(b[8:], cmdSessionReq) // 0x0402 binary.LittleEndian.PutUint16(b[10:], 0x0033) // flags body := b[headerSize:] copy(body[:20], c.uid) copy(body[20:], c.sid) binary.LittleEndian.PutUint32(body[32:], uint32(time.Now().Unix())) return b } func (c *DTLSConn) msgAVLogin(magic uint16, size int, flags uint16, randomID []byte) []byte { b := make([]byte, size) binary.LittleEndian.PutUint16(b, magic) binary.LittleEndian.PutUint16(b[2:], protoVersion) binary.LittleEndian.PutUint16(b[16:], uint16(size-24)) // payload size binary.LittleEndian.PutUint16(b[18:], flags) copy(b[20:], randomID[:4]) copy(b[24:], "admin") // username copy(b[280:], c.enr) // password/ENR binary.LittleEndian.PutUint32(b[540:], 4) // security_mode ? binary.LittleEndian.PutUint32(b[552:], defaultCaps) // capabilities return b } func (c *DTLSConn) msgAVLoginResponse(checksum uint32) []byte { b := make([]byte, 60) binary.LittleEndian.PutUint16(b, 0x2100) // magic binary.LittleEndian.PutUint16(b[2:], 0x000c) // version b[4] = 0x10 // success binary.LittleEndian.PutUint32(b[16:], 0x24) // payload size binary.LittleEndian.PutUint32(b[20:], checksum) // echo checksum b[29] = 0x01 // enable flag b[31] = 0x01 // two-way streaming binary.LittleEndian.PutUint32(b[36:], 0x04) // buffer config binary.LittleEndian.PutUint32(b[40:], defaultCaps) binary.LittleEndian.PutUint16(b[54:], 0x0003) // channel info binary.LittleEndian.PutUint16(b[56:], 0x0002) return b } func (c *DTLSConn) msgAudioFrame(payload []byte, timestampUS uint32, codec byte, sampleRate uint32, channels uint8) []byte { c.audioSeq++ c.audioFrameNo++ prevFrame := uint32(0) if c.audioFrameNo > 1 { prevFrame = c.audioFrameNo - 1 } totalPayload := len(payload) + 16 // payload + frameinfo b := make([]byte, 36+totalPayload) // Outer header (36 bytes) b[0] = tutk.ChannelAudio // 0x03 b[1] = tutk.FrameTypeStartAlt // 0x09 binary.LittleEndian.PutUint16(b[2:], protoVersion) binary.LittleEndian.PutUint32(b[4:], c.audioSeq) binary.LittleEndian.PutUint32(b[8:], timestampUS) if c.audioFrameNo == 1 { binary.LittleEndian.PutUint32(b[12:], 0x00000001) } else { binary.LittleEndian.PutUint32(b[12:], 0x00100001) } // Inner header b[16] = tutk.ChannelAudio b[17] = tutk.FrameTypeEndSingle binary.LittleEndian.PutUint16(b[18:], uint16(prevFrame)) binary.LittleEndian.PutUint16(b[20:], 0x0001) // pkt_total binary.LittleEndian.PutUint16(b[22:], 0x0010) // flags binary.LittleEndian.PutUint32(b[24:], uint32(totalPayload)) binary.LittleEndian.PutUint32(b[28:], prevFrame) binary.LittleEndian.PutUint32(b[32:], c.audioFrameNo) copy(b[36:], payload) // Payload + FrameInfo fi := b[36+len(payload):] fi[0] = codec // Codec ID (low byte) fi[1] = 0 // Codec ID (high byte, unused) // Audio flags: [3:2]=sampleRateIdx [1]=16bit [0]=stereo srIdx := tutk.GetSampleRateIndex(sampleRate) fi[2] = (srIdx << 2) | 0x02 // 16-bit always set if channels == 2 { fi[2] |= 0x01 } fi[4] = 1 // online binary.LittleEndian.PutUint32(fi[12:], (c.audioFrameNo-1)*tutk.GetSamplesPerFrame(codec)*1000/sampleRate) return b } func (c *DTLSConn) msgTxData(payload []byte, channel byte) []byte { bodySize := 12 + len(payload) b := make([]byte, 16+bodySize) copy(b, "\x04\x02\x1a\x0b") // marker + mode=data binary.LittleEndian.PutUint16(b[4:], uint16(bodySize)) // body size binary.LittleEndian.PutUint16(b[6:], c.seq) // sequence c.seq++ binary.LittleEndian.PutUint16(b[8:], cmdDataTX) // 0x0407 binary.LittleEndian.PutUint16(b[10:], 0x0021) // flags copy(b[12:], c.sid[:2]) // rid[0:2] b[14] = channel // channel b[15] = 0x01 // marker binary.LittleEndian.PutUint32(b[16:], 0x0000000c) // const copy(b[20:], c.sid[:8]) // rid copy(b[28:], payload) return b } func (c *DTLSConn) msgTxDataCC51(payload []byte, channel byte) []byte { payloadSize := uint16(16 + len(payload) + authSizeCC51) b := make([]byte, headerSizeCC51+len(payload)+authSizeCC51) copy(b[:2], magicCC51) binary.LittleEndian.PutUint16(b[4:], cmdDTLSCC51) // 0x1502 binary.LittleEndian.PutUint16(b[6:], payloadSize) binary.LittleEndian.PutUint16(b[12:], uint16(0x0010)|(uint16(channel)<<8)) // channel in high byte binary.LittleEndian.PutUint16(b[14:], c.ticket) copy(b[16:24], c.sid) binary.LittleEndian.PutUint32(b[24:], 1) // const copy(b[headerSizeCC51:], payload) h := hmac.New(sha1.New, append([]byte(c.uid), c.authKey...)) h.Write(b[:headerSizeCC51]) copy(b[headerSizeCC51+len(payload):], h.Sum(nil)) return b } func (c *DTLSConn) msgACK() []byte { c.ackFlags++ b := make([]byte, 24) binary.LittleEndian.PutUint16(b[0:], magicACK) // 0x0009 binary.LittleEndian.PutUint16(b[2:], protoVersion) // 0x000c binary.LittleEndian.PutUint32(b[4:], c.avSeq) // TX seq c.avSeq++ binary.LittleEndian.PutUint16(b[8:], c.rxSeqStart) // RX start (last acked) binary.LittleEndian.PutUint16(b[10:], c.rxSeqEnd) // RX end (highest received) if c.rxSeqInit { c.rxSeqStart = c.rxSeqEnd } binary.LittleEndian.PutUint16(b[12:], c.ackFlags) // AckFlags binary.LittleEndian.PutUint32(b[16:], uint32(c.ackFlags)<<16) // AckCounter ts := uint32(time.Now().UnixMilli() & 0xFFFF) binary.LittleEndian.PutUint16(b[20:], uint16(ts)) // Timestamp return b } func (c *DTLSConn) msgKeepalive(incoming []byte) []byte { b := make([]byte, 24) copy(b, "\x04\x02\x1a\x0a") // marker + mode binary.LittleEndian.PutUint16(b[4:], 8) // body size binary.LittleEndian.PutUint16(b[8:], cmdKeepaliveReq) // 0x0427 binary.LittleEndian.PutUint16(b[10:], 0x0021) // flags if len(incoming) >= 8 { copy(b[16:], incoming[:8]) // echo payload } return b } func (c *DTLSConn) msgIOCtrl(payload []byte) []byte { b := make([]byte, 40+len(payload)) binary.LittleEndian.PutUint16(b, protoVersion) // magic binary.LittleEndian.PutUint16(b[2:], protoVersion) // version binary.LittleEndian.PutUint32(b[4:], c.avSeq) // av seq c.avSeq++ binary.LittleEndian.PutUint16(b[16:], magicIOCtrl) // 0x7000 binary.LittleEndian.PutUint16(b[18:], c.seqCmd) // sub channel binary.LittleEndian.PutUint32(b[20:], 1) // ioctl seq binary.LittleEndian.PutUint32(b[24:], uint32(len(payload)+4)) // payload size binary.LittleEndian.PutUint32(b[28:], uint32(c.seqCmd)) // flag b[37] = 0x01 copy(b[40:], payload) c.seqCmd++ return b } func hexDump(data []byte) string { const maxBytes = 650 totalLen := len(data) truncated := totalLen > maxBytes if truncated { data = data[:maxBytes] } var result string for i := 0; i < len(data); i += 16 { end := min(i+16, len(data)) line := fmt.Sprintf(" %04x:", i) for j := i; j < end; j++ { line += fmt.Sprintf(" %02x", data[j]) } result += line + "\n" } if truncated { result += fmt.Sprintf(" ... (truncated, showing %d of %d bytes)\n", maxBytes, totalLen) } return result } ================================================ FILE: pkg/tutk/dtls/dtls.go ================================================ package dtls import ( "context" "net" "sync" "time" "github.com/pion/dtls/v3" ) func NewDTLSClient(ctx context.Context, channel uint8, addr net.Addr, writeFn func([]byte, uint8) error, readChan chan []byte, psk []byte) (*dtls.Conn, error) { return dialDTLS(ctx, channel, addr, writeFn, readChan, psk, false) } func NewDTLSServer(ctx context.Context, channel uint8, addr net.Addr, writeFn func([]byte, uint8) error, readChan chan []byte, psk []byte) (*dtls.Conn, error) { return dialDTLS(ctx, channel, addr, writeFn, readChan, psk, true) } func dialDTLS(ctx context.Context, channel uint8, addr net.Addr, writeFn func([]byte, uint8) error, readChan chan []byte, psk []byte, isServer bool) (*dtls.Conn, error) { adapter := &channelAdapter{ ctx: ctx, channel: channel, addr: addr, writeFn: writeFn, readChan: readChan, } var conn *dtls.Conn var err error if isServer { conn, err = dtls.Server(adapter, addr, buildDTLSConfig(psk, true)) } else { conn, err = dtls.Client(adapter, addr, buildDTLSConfig(psk, false)) } if err != nil { return nil, err } timeout := 5 * time.Second adapter.SetReadDeadline(time.Now().Add(timeout)) hsCtx, cancel := context.WithTimeout(ctx, timeout) defer cancel() if err := conn.HandshakeContext(hsCtx); err != nil { go conn.Close() return nil, err } adapter.SetReadDeadline(time.Time{}) return conn, nil } func buildDTLSConfig(psk []byte, isServer bool) *dtls.Config { config := &dtls.Config{ PSK: func(hint []byte) ([]byte, error) { return psk, nil }, PSKIdentityHint: []byte("AUTHPWD_admin"), InsecureSkipVerify: true, InsecureSkipVerifyHello: true, MTU: 1200, FlightInterval: 300 * time.Millisecond, ExtendedMasterSecret: dtls.DisableExtendedMasterSecret, } if isServer { config.CipherSuites = []dtls.CipherSuiteID{dtls.TLS_PSK_WITH_AES_128_CBC_SHA256} } else { config.CustomCipherSuites = CustomCipherSuites } return config } type channelAdapter struct { ctx context.Context channel uint8 writeFn func([]byte, uint8) error readChan chan []byte addr net.Addr mu sync.Mutex readDeadline time.Time } func (a *channelAdapter) ReadFrom(p []byte) (n int, addr net.Addr, err error) { a.mu.Lock() deadline := a.readDeadline a.mu.Unlock() if !deadline.IsZero() { timeout := time.Until(deadline) if timeout <= 0 { return 0, nil, &timeoutError{} } timer := time.NewTimer(timeout) defer timer.Stop() select { case data := <-a.readChan: return copy(p, data), a.addr, nil case <-timer.C: return 0, nil, &timeoutError{} case <-a.ctx.Done(): return 0, nil, net.ErrClosed } } select { case data := <-a.readChan: return copy(p, data), a.addr, nil case <-a.ctx.Done(): return 0, nil, net.ErrClosed } } func (a *channelAdapter) WriteTo(p []byte, _ net.Addr) (int, error) { if err := a.writeFn(p, a.channel); err != nil { return 0, err } return len(p), nil } func (a *channelAdapter) Close() error { return nil } func (a *channelAdapter) LocalAddr() net.Addr { return &net.UDPAddr{} } func (a *channelAdapter) SetDeadline(t time.Time) error { a.mu.Lock() a.readDeadline = t a.mu.Unlock() return nil } func (a *channelAdapter) SetReadDeadline(t time.Time) error { a.mu.Lock() a.readDeadline = t a.mu.Unlock() return nil } func (a *channelAdapter) SetWriteDeadline(time.Time) error { return nil } type timeoutError struct{} func (e *timeoutError) Error() string { return "i/o timeout" } func (e *timeoutError) Timeout() bool { return true } func (e *timeoutError) Temporary() bool { return true } ================================================ FILE: pkg/tutk/frame.go ================================================ package tutk import ( "encoding/binary" "encoding/hex" "fmt" "sync" "github.com/AlexxIT/go2rtc/pkg/aac" ) const ( FrameTypeStart uint8 = 0x08 // Extended start (36-byte header) FrameTypeStartAlt uint8 = 0x09 // StartAlt (36-byte header) FrameTypeCont uint8 = 0x00 // Continuation (28-byte header) FrameTypeContAlt uint8 = 0x04 // Continuation alt FrameTypeEndSingle uint8 = 0x01 // Single-packet frame (28-byte) FrameTypeEndMulti uint8 = 0x05 // Multi-packet end (28-byte) FrameTypeEndExt uint8 = 0x0d // Extended end (36-byte) ) const ( ChannelIVideo uint8 = 0x05 ChannelAudio uint8 = 0x03 ChannelPVideo uint8 = 0x07 ) const frameInfoSize = 40 // FrameInfo - Wyze extended FRAMEINFO (40 bytes at end of packet) // Video: 40 bytes, Audio: 16 bytes (uses same struct, fields 16+ are zero) // // Offset Size Field // 0-1 2 CodecID - 0x4E=H264, 0x7B=H265, 0x90=AAC_WYZE // 2 1 Flags - Video: 1=Keyframe, 0=P-frame | Audio: sample rate/bits/channels // 3 1 CamIndex - Camera index // 4 1 OnlineNum - Online number // 5 1 FPS - Framerate (e.g. 20) // 6 1 ResTier - Video: 1=Low(360P), 4=High(HD/2K) | Audio: 0 // 7 1 Bitrate - Video: 30=360P, 100=HD, 200=2K | Audio: 1 // 8-11 4 Timestamp - Timestamp (increases ~50000/frame for 20fps video) // 12-15 4 SessionID - Session marker (constant per stream) // 16-19 4 PayloadSize - Frame payload size in bytes // 20-23 4 FrameNo - Global frame number // 24-35 12 DeviceID - MAC address (ASCII) - video only // 36-39 4 Padding - Always 0 - video only type FrameInfo struct { CodecID byte // 0 (only low byte used) Flags uint8 // 2 CamIndex uint8 // 3 OnlineNum uint8 // 4 FPS uint8 // 5: Framerate ResTier uint8 // 6: Resolution tier (1=Low, 4=High) Bitrate uint8 // 7: Bitrate index (30=360P, 100=HD, 200=2K) Timestamp uint32 // 8-11: Timestamp SessionID uint32 // 12-15: Session marker (constant) PayloadSize uint32 // 16-19: Payload size FrameNo uint32 // 20-23: Frame number } func (fi *FrameInfo) IsKeyframe() bool { return fi.Flags == 0x01 } func (fi *FrameInfo) SampleRate() uint32 { idx := (fi.Flags >> 2) & 0x0F if idx < uint8(len(sampleRates)) { return sampleRates[idx] } return 16000 } func (fi *FrameInfo) Channels() uint8 { if fi.Flags&0x01 == 1 { return 2 } return 1 } func ParseFrameInfo(data []byte) *FrameInfo { if len(data) < frameInfoSize { return nil } offset := len(data) - frameInfoSize fi := data[offset:] return &FrameInfo{ CodecID: fi[0], Flags: fi[2], CamIndex: fi[3], OnlineNum: fi[4], FPS: fi[5], ResTier: fi[6], Bitrate: fi[7], Timestamp: binary.LittleEndian.Uint32(fi[8:]), SessionID: binary.LittleEndian.Uint32(fi[12:]), PayloadSize: binary.LittleEndian.Uint32(fi[16:]), FrameNo: binary.LittleEndian.Uint32(fi[20:]), } } type Packet struct { Channel uint8 Codec byte Timestamp uint32 Payload []byte IsKeyframe bool FrameNo uint32 SampleRate uint32 Channels uint8 } type PacketHeader struct { Channel byte FrameType byte HeaderSize int FrameNo uint32 PktIdx uint16 PktTotal uint16 PayloadSize uint16 HasFrameInfo bool } func ParsePacketHeader(data []byte) *PacketHeader { if len(data) < 28 { return nil } frameType := data[1] hdr := &PacketHeader{ Channel: data[0], FrameType: frameType, } switch frameType { case FrameTypeStart, FrameTypeStartAlt, FrameTypeEndExt: hdr.HeaderSize = 36 default: hdr.HeaderSize = 28 } if len(data) < hdr.HeaderSize { return nil } if hdr.HeaderSize == 28 { hdr.PktTotal = binary.LittleEndian.Uint16(data[12:]) pktIdxOrMarker := binary.LittleEndian.Uint16(data[14:]) hdr.PayloadSize = binary.LittleEndian.Uint16(data[16:]) hdr.FrameNo = binary.LittleEndian.Uint32(data[24:]) if pktIdxOrMarker == 0x0028 && (IsEndFrame(frameType) || hdr.PktTotal == 1) { hdr.HasFrameInfo = true if hdr.PktTotal > 0 { hdr.PktIdx = hdr.PktTotal - 1 } } else { hdr.PktIdx = pktIdxOrMarker } } else { hdr.PktTotal = binary.LittleEndian.Uint16(data[20:]) pktIdxOrMarker := binary.LittleEndian.Uint16(data[22:]) hdr.PayloadSize = binary.LittleEndian.Uint16(data[24:]) hdr.FrameNo = binary.LittleEndian.Uint32(data[32:]) if pktIdxOrMarker == 0x0028 && (IsEndFrame(frameType) || hdr.PktTotal == 1) { hdr.HasFrameInfo = true if hdr.PktTotal > 0 { hdr.PktIdx = hdr.PktTotal - 1 } } else { hdr.PktIdx = pktIdxOrMarker } } return hdr } func IsStartFrame(frameType uint8) bool { return frameType == FrameTypeStart || frameType == FrameTypeStartAlt } func IsEndFrame(frameType uint8) bool { return frameType == FrameTypeEndSingle || frameType == FrameTypeEndMulti || frameType == FrameTypeEndExt } func IsContinuationFrame(frameType uint8) bool { return frameType == FrameTypeCont || frameType == FrameTypeContAlt } type channelState struct { frameNo uint32 // current frame being assembled pktTotal uint16 // expected total packets waitSeq uint16 // next expected packet index (0, 1, 2, ...) waitData []byte // accumulated payload data frameInfo *FrameInfo // frame info (from end packet) hasStarted bool // received first packet of frame lastPktIdx uint16 // last received packet index (for OOO detection) } func (cs *channelState) reset() { cs.frameNo = 0 cs.pktTotal = 0 cs.waitSeq = 0 cs.waitData = cs.waitData[:0] cs.frameInfo = nil cs.hasStarted = false cs.lastPktIdx = 0 } const tsWrapPeriod uint32 = 1000000 type tsTracker struct { lastRawTS uint32 accumUS uint64 firstTS bool } func (t *tsTracker) update(rawTS uint32) uint64 { if !t.firstTS { t.firstTS = true t.lastRawTS = rawTS return 0 } var delta uint32 if rawTS >= t.lastRawTS { delta = rawTS - t.lastRawTS } else { // Wrapped: delta = (wrap - last) + new delta = (tsWrapPeriod - t.lastRawTS) + rawTS } t.accumUS += uint64(delta) t.lastRawTS = rawTS return t.accumUS } type FrameHandler struct { channels map[byte]*channelState videoTS tsTracker audioTS tsTracker output chan *Packet verbose bool closed bool closeMu sync.Mutex } func NewFrameHandler(verbose bool) *FrameHandler { return &FrameHandler{ channels: make(map[byte]*channelState), output: make(chan *Packet, 128), verbose: verbose, } } func (h *FrameHandler) Recv() <-chan *Packet { return h.output } func (h *FrameHandler) Close() { h.closeMu.Lock() defer h.closeMu.Unlock() if h.closed { return } h.closed = true close(h.output) } func (h *FrameHandler) Handle(data []byte) { hdr := ParsePacketHeader(data) if hdr == nil { return } payload, fi := h.extractPayload(data, hdr.Channel) if payload == nil { return } if h.verbose { fiStr := "" if hdr.HasFrameInfo { fiStr = " +FI" } fmt.Printf("[RX] ch=0x%02x type=0x%02x #%d pkt=%d/%d data=%dB%s\n", hdr.Channel, hdr.FrameType, hdr.FrameNo, hdr.PktIdx, hdr.PktTotal, len(payload), fiStr) } switch hdr.Channel { case ChannelAudio: h.handleAudio(payload, fi) case ChannelIVideo, ChannelPVideo: h.handleVideo(hdr.Channel, hdr, payload, fi) } } func (h *FrameHandler) extractPayload(data []byte, channel byte) ([]byte, *FrameInfo) { if len(data) < 2 { return nil, nil } frameType := data[1] headerSize := 28 fiSize := 0 switch frameType { case FrameTypeStart: headerSize = 36 case FrameTypeStartAlt: headerSize = 36 if len(data) >= 22 { pktTotal := binary.LittleEndian.Uint16(data[20:]) if pktTotal == 1 { fiSize = frameInfoSize } } case FrameTypeCont, FrameTypeContAlt: headerSize = 28 case FrameTypeEndSingle, FrameTypeEndMulti: headerSize = 28 fiSize = frameInfoSize case FrameTypeEndExt: headerSize = 36 fiSize = frameInfoSize default: headerSize = 28 } if len(data) < headerSize { return nil, nil } if fiSize == 0 { return data[headerSize:], nil } if len(data) < headerSize+fiSize { return data[headerSize:], nil } fi := ParseFrameInfo(data) validCodec := false switch channel { case ChannelIVideo, ChannelPVideo: validCodec = IsVideoCodec(fi.CodecID) case ChannelAudio: validCodec = IsAudioCodec(fi.CodecID) } if validCodec { payload := data[headerSize : len(data)-fiSize] return payload, fi } return data[headerSize:], nil } func (h *FrameHandler) handleVideo(channel byte, hdr *PacketHeader, payload []byte, fi *FrameInfo) { cs := h.channels[channel] if cs == nil { cs = &channelState{} h.channels[channel] = cs } // New frame number - reset and start fresh if hdr.FrameNo != cs.frameNo { // Check if previous frame was incomplete if cs.hasStarted && cs.waitSeq < cs.pktTotal { fmt.Printf("[DROP] ch=0x%02x #%d INCOMPLETE: got %d/%d pkts\n", channel, cs.frameNo, cs.waitSeq, cs.pktTotal) } cs.reset() cs.frameNo = hdr.FrameNo cs.pktTotal = hdr.PktTotal } // If packet index doesn't match expected, reset (data loss) if hdr.PktIdx != cs.waitSeq { fmt.Printf("[OOO] ch=0x%02x #%d frameType=0x%02x pktTotal=%d expected pkt %d, got %d - reset\n", channel, hdr.FrameNo, hdr.FrameType, hdr.PktTotal, cs.waitSeq, hdr.PktIdx) cs.reset() return } // First packet - mark as started if cs.waitSeq == 0 { cs.hasStarted = true } cs.waitData = append(cs.waitData, payload...) cs.waitSeq++ // Store frame info if present if fi != nil { cs.frameInfo = fi } // Check if frame is complete if cs.waitSeq != cs.pktTotal || cs.frameInfo == nil { return } fi = cs.frameInfo defer cs.reset() if fi.PayloadSize > 0 && uint32(len(cs.waitData)) != fi.PayloadSize { fmt.Printf("[SIZE] ch=0x%02x #%d mismatch: expected %d, got %d\n", channel, cs.frameNo, fi.PayloadSize, len(cs.waitData)) return } if len(cs.waitData) == 0 { return } accumUS := h.videoTS.update(fi.Timestamp) rtpTS := uint32(accumUS * 90000 / 1000000) pkt := &Packet{ Channel: channel, Payload: append([]byte{}, cs.waitData...), Codec: fi.CodecID, Timestamp: rtpTS, IsKeyframe: fi.IsKeyframe(), FrameNo: fi.FrameNo, } if h.verbose { frameType := "P" if fi.IsKeyframe() { frameType = "KEY" } fmt.Printf("[OK] ch=0x%02x #%d codec=0x%02x %s size=%d\n", channel, fi.FrameNo, fi.CodecID, frameType, len(pkt.Payload)) fmt.Printf(" [0-1]codec=0x%02x [2]flags=0x%x [3]=%d [4]=%d\n", fi.CodecID, fi.Flags, fi.CamIndex, fi.OnlineNum) fmt.Printf(" [5]=%d [6]=%d [7]=%d [8-11]ts=%d\n", fi.FPS, fi.ResTier, fi.Bitrate, fi.Timestamp) fmt.Printf(" [12-15]=0x%x [16-19]payload=%d [20-23]frameNo=%d\n", fi.SessionID, fi.PayloadSize, fi.FrameNo) fmt.Printf(" rtp_ts=%d accum_us=%d\n", rtpTS, accumUS) fmt.Printf(" hex: %s\n", dumpHex(fi)) } h.queue(pkt) } func (h *FrameHandler) handleAudio(payload []byte, fi *FrameInfo) { if len(payload) == 0 || fi == nil { return } var sampleRate uint32 var channels uint8 switch fi.CodecID { case CodecAACRaw, CodecAACADTS, CodecAACLATM, CodecAACAlt: sampleRate, channels = parseAudioParams(payload, fi) default: sampleRate = fi.SampleRate() channels = fi.Channels() } accumUS := h.audioTS.update(fi.Timestamp) rtpTS := uint32(accumUS * uint64(sampleRate) / 1000000) payloadCopy := make([]byte, len(payload)) copy(payloadCopy, payload) pkt := &Packet{ Channel: ChannelAudio, Payload: payloadCopy, Codec: fi.CodecID, Timestamp: rtpTS, SampleRate: sampleRate, Channels: channels, FrameNo: fi.FrameNo, } if h.verbose { bits := 8 if fi.Flags&0x02 != 0 { bits = 16 } fmt.Printf("[OK] Audio #%d codec=0x%02x size=%d\n", fi.FrameNo, fi.CodecID, len(payload)) fmt.Printf(" [0-1]codec=0x%02x [2]flags=0x%x(%dHz/%dbit/%dch)\n", fi.CodecID, fi.Flags, sampleRate, bits, channels) fmt.Printf(" [8-11]ts=%d [12-15]=0x%x rtp_ts=%d\n", fi.Timestamp, fi.SessionID, rtpTS) fmt.Printf(" hex: %s\n", dumpHex(fi)) } h.queue(pkt) } func (h *FrameHandler) queue(pkt *Packet) { h.closeMu.Lock() defer h.closeMu.Unlock() if h.closed { return } select { case h.output <- pkt: default: // Queue full - drop oldest select { case <-h.output: default: } select { case h.output <- pkt: default: // Queue still full, drop this packet } } } func parseAudioParams(payload []byte, fi *FrameInfo) (sampleRate uint32, channels uint8) { if aac.IsADTS(payload) { codec := aac.ADTSToCodec(payload) if codec != nil { return codec.ClockRate, codec.Channels } } if fi != nil { return fi.SampleRate(), fi.Channels() } return 16000, 1 } func dumpHex(fi *FrameInfo) string { b := make([]byte, frameInfoSize) b[0] = fi.CodecID b[1] = 0 // High byte (unused) b[2] = fi.Flags b[3] = fi.CamIndex b[4] = fi.OnlineNum b[5] = fi.FPS b[6] = fi.ResTier b[7] = fi.Bitrate binary.LittleEndian.PutUint32(b[8:], fi.Timestamp) binary.LittleEndian.PutUint32(b[12:], fi.SessionID) binary.LittleEndian.PutUint32(b[16:], fi.PayloadSize) binary.LittleEndian.PutUint32(b[20:], fi.FrameNo) // Bytes 24-39 are DeviceID and Padding (not stored in struct) hexStr := hex.EncodeToString(b) formatted := "" for i := 0; i < len(hexStr); i += 2 { if i > 0 { formatted += " " } formatted += hexStr[i : i+2] } return formatted } ================================================ FILE: pkg/tutk/helpers.go ================================================ package tutk import ( "encoding/binary" "time" ) func GenSessionID() []byte { b := make([]byte, 8) binary.LittleEndian.PutUint64(b, uint64(time.Now().UnixNano())) return b } func ICAM(cmd uint32, args ...byte) []byte { // 0 4943414d ICAM // 4 d807ff00 command // 8 00000000000000 // 15 02 args count // 16 00000000000000 // 23 0101 args n := byte(len(args)) b := make([]byte, 23+n) copy(b, "ICAM") binary.LittleEndian.PutUint32(b[4:], cmd) b[15] = n copy(b[23:], args) return b } func HL(cmdID uint16, payload []byte) []byte { // 0-1 "HL" magic // 2 version (typically 5) // 3 reserved // 4-5 cmdID command ID (uint16 LE) // 6-7 payloadLen payload length (uint16 LE) // 8-15 reserved // 16+ payload const headerSize = 16 const version = 5 b := make([]byte, headerSize+len(payload)) copy(b, "HL") b[2] = version binary.LittleEndian.PutUint16(b[4:], cmdID) binary.LittleEndian.PutUint16(b[6:], uint16(len(payload))) copy(b[headerSize:], payload) return b } func ParseHL(data []byte) (cmdID uint16, payload []byte, ok bool) { if len(data) < 16 || data[0] != 'H' || data[1] != 'L' { return 0, nil, false } cmdID = binary.LittleEndian.Uint16(data[4:]) payloadLen := binary.LittleEndian.Uint16(data[6:]) if len(data) >= 16+int(payloadLen) { payload = data[16 : 16+payloadLen] } else if len(data) > 16 { payload = data[16:] } return cmdID, payload, true } func FindHL(data []byte, offset int) []byte { for i := offset; i+16 <= len(data); i++ { if data[i] == 'H' && data[i+1] == 'L' { return data[i:] } } return nil } ================================================ FILE: pkg/tutk/session0.go ================================================ package tutk import ( "bytes" "encoding/binary" "net" "time" ) func (c *Conn) connectDirect(uid string, sid []byte) error { res, err := writeAndWait( c, func(res []byte) bool { return bytes.Index(res, []byte("\x02\x06\x12\x00")) == 8 }, ConnectByUID(stageBroadcast, uid, sid), ) if err != nil { return err } n := len(res) // should be 200 c.ver = []byte{res[2], res[n-13], res[n-14], res[n-15], res[n-16]} _, err = c.Write(ConnectByUID(stageDirect, uid, sid)) return err } func (c *Conn) connectRemote(uid string, sid []byte) error { res, err := writeAndWait( c, func(res []byte) bool { return bytes.Index(res, []byte("\x01\x03\x43")) == 8 }, ConnectByUID(stageGetRemoteIP, uid, sid), ) if err != nil { return err } // Read real IP from cloud server response. // Important ot use net.IPv4 because slice will be 16 bytes. c.addr.IP = net.IPv4(res[40], res[41], res[42], res[43]) c.addr.Port = int(binary.BigEndian.Uint16(res[38:])) res, err = writeAndWait( c, func(res []byte) bool { return bytes.Index(res, []byte("\x04\x04\x33")) == 8 }, ConnectByUID(stageRemoteAck, uid, sid), ) if err != nil { return err } if len(res) == 52 { c.ver = []byte{res[2], res[51], res[50], res[49], res[48]} } else { c.ver = []byte{res[2]} } _, err = c.Write(ConnectByUID(stageRemoteOK, uid, sid)) return err } func (c *Conn) clientStart(username, password string) error { _, err := writeAndWait( c, func(res []byte) bool { return len(res) >= 84 && res[28] == 0 && (res[29] == 0x14 || res[29] == 0x21) }, c.session.ClientStart(0, username, password), c.session.ClientStart(1, username, password), ) return err } func writeAndWait(conn net.Conn, ok func(res []byte) bool, req ...[]byte) ([]byte, error) { var t *time.Timer t = time.AfterFunc(1, func() { for _, b := range req { if _, err := conn.Write(b); err != nil { return } } if t != nil { t.Reset(time.Second) } }) defer t.Stop() buf := make([]byte, 1200) for { n, err := conn.Read(buf) if err != nil { return nil, err } if ok(buf[:n]) { return buf[:n], nil } } } const ( magic = "\x04\x02\x19" // include version 0x19 sdkVersion = "\x06\x00\x03\x03" // 3.3.0.6 ) const ( stageBroadcast = iota + 1 stageDirect stageGetPublicIP stageGetRemoteIP stageRemoteReq stageRemoteAck stageRemoteOK ) func ConnectByUID(stage byte, uid string, sid8 []byte) []byte { var b []byte switch stage { case stageBroadcast, stageDirect: b = make([]byte, 68) copy(b[8:], "\x01\x06\x21") copy(b[52:], sdkVersion) copy(b[56:], sid8) b[64] = stage // 1 or 2 case stageGetPublicIP: b = make([]byte, 54) copy(b[8:], "\x07\x10\x18") case stageGetRemoteIP: b = make([]byte, 112) copy(b[8:], "\x03\x02\x34") copy(b[100:], sid8) b[108] = stageDirect case stageRemoteReq: b = make([]byte, 52) copy(b[8:], "\x01\x04\x33") copy(b[36:], sid8) copy(b[48:], sdkVersion) case stageRemoteAck: b = make([]byte, 44) copy(b[8:], "\x02\x04\x33") copy(b[36:], sid8) case stageRemoteOK: b = make([]byte, 52) copy(b[8:], "\x04\x04\x33") copy(b[36:], sid8) copy(b[48:], sdkVersion) } copy(b, magic) b[3] = 0x02 // connection stage binary.LittleEndian.PutUint16(b[4:], uint16(len(b))-16) copy(b[16:], uid) return b } ================================================ FILE: pkg/tutk/session16.go ================================================ package tutk import ( "bytes" "encoding/binary" "io" "net" "time" ) type Session interface { Close() error ClientStart(i byte, username, password string) []byte SendIOCtrl(ctrlType uint32, ctrlData []byte) []byte SendFrameData(frameInfo, frameData []byte) []byte RecvIOCtrl() (ctrlType uint32, ctrlData []byte, err error) RecvFrameData() (frameInfo, frameData []byte, err error) SessionRead(chID byte, buf []byte) int SessionWrite(chID byte, buf []byte) error } func NewSession16(conn net.Conn, sid8 []byte) *Session16 { sid16 := make([]byte, 16) copy(sid16[8:], sid8) copy(sid16, sid8[:2]) sid16[4] = 0x0c return &Session16{ conn: conn, sid16: sid16, rawCmd: make(chan []byte, 10), rawPkt: make(chan [2][]byte, 100), } } type Session16 struct { conn net.Conn sid16 []byte rawCmd chan []byte rawPkt chan [2][]byte seqSendCh0 uint16 seqSendCh1 uint16 seqSendCmd1 uint16 seqSendAud uint16 waitFSeq uint16 waitCSeq uint16 waitSize int waitData []byte } func (s *Session16) Close() error { close(s.rawCmd) close(s.rawPkt) return nil } func (s *Session16) Msg(size uint16) []byte { b := make([]byte, size) copy(b, magic) b[3] = 0x0a // connected stage binary.LittleEndian.PutUint16(b[4:], size-16) copy(b[8:], "\x07\x04\x21") // client request copy(b[12:], s.sid16) return b } const ( msgHhrSize = 28 cmdHdrSize = 24 ) func (s *Session16) ClientStart(i byte, username, password string) []byte { const size = 566 + 32 msg := s.Msg(size) // 0 00000b0000000000000000000000000022020000fcfc7284 // 24 4d69737300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 281 636c69656e740000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 // 538 0100000004000000fb071f000000000000000000000003000000000001000000 cmd := msg[msgHhrSize:] copy(cmd, "\x00\x00\x0b\x00") binary.LittleEndian.PutUint16(cmd[16:], size-52) if i == 0 { cmd[18] = 1 } else { cmd[1] = 0x20 } binary.LittleEndian.PutUint32(cmd[20:], uint32(time.Now().UnixMilli())) // important values for some cameras (not for df3) data := cmd[cmdHdrSize:] copy(data, username) copy(data[257:], password) // 0100000004000000fb071f000000000000000000000003000000000001000000 cfg := data[257+257:] //cfg[0] = 1 // 0 - simple proto, 1 - complex proto with "0Cxx" commands cfg[4] = 4 copy(cfg[8:], "\xfb\x07\x1f\x00") cfg[22] = 3 //cfg[28] = 1 // unknown return msg } func (s *Session16) SendIOCtrl(ctrlType uint32, ctrlData []byte) []byte { dataSize := 4 + uint16(len(ctrlData)) msg := s.Msg(msgHhrSize + cmdHdrSize + dataSize) cmd := msg[msgHhrSize:] copy(cmd, "\x00\x70\x0b\x00") s.seqSendCmd1++ // start from 1, important! binary.LittleEndian.PutUint16(cmd[4:], s.seqSendCmd1) binary.LittleEndian.PutUint16(cmd[16:], dataSize) binary.LittleEndian.PutUint32(cmd[20:], uint32(time.Now().UnixMilli())) data := cmd[cmdHdrSize:] binary.LittleEndian.PutUint32(data, ctrlType) copy(data[4:], ctrlData) return msg } func (s *Session16) SendFrameData(frameInfo, frameData []byte) []byte { // -> 01030b001d0000008802000000002800b0020bf501000000 ... 4f4455412000000088020000030400001d000000000000000bf51f7a9b0100000000000000000000 n := uint16(len(frameData)) dataSize := n + 8 + 32 msg := s.Msg(msgHhrSize + cmdHdrSize + dataSize) // 0 01030b00 command + version // 4 1d000000 seq // 8 8802 media size (648) // 10 00000000 // 14 2800 tail (pkt header) size? // 16 b002 size (648 + 8 + 32) // 18 0bf5 random msg id (unixms) // 20 01000000 fixed cmd := msg[msgHhrSize:] copy(cmd, "\x01\x03\x0b\x00") binary.LittleEndian.PutUint16(cmd[4:], s.seqSendAud) s.seqSendAud++ binary.LittleEndian.PutUint16(cmd[8:], n) cmd[14] = 0x28 // important! binary.LittleEndian.PutUint16(cmd[16:], dataSize) binary.LittleEndian.PutUint16(cmd[18:], uint16(time.Now().UnixMilli())) cmd[20] = 1 data := cmd[cmdHdrSize:] copy(data, frameData) copy(data[n:], "ODUA\x20\x00\x00\x00") copy(data[n+8:], frameInfo) return msg } func (s *Session16) RecvIOCtrl() (ctrlType uint32, ctrlData []byte, err error) { buf, ok := <-s.rawCmd if !ok { return 0, nil, io.EOF } return binary.LittleEndian.Uint32(buf), buf[4:], nil } func (s *Session16) RecvFrameData() (frameInfo, frameData []byte, err error) { buf, ok := <-s.rawPkt if !ok { return nil, nil, io.EOF } return buf[0], buf[1], nil } func (s *Session16) SessionRead(chID byte, cmd []byte) int { if chID != 0 { return s.handleCh1(cmd) } // 0 01030800 command + version // 4 00000000 frame seq // 8 ac880100 total size // 12 6200 chunk seq // 14 2000 tail (pkt header) size // 16 cc00 size // 18 0000 // 20 01000000 fixed switch cmd[0] { case 0x01: var packetData [2][]byte switch cmd[1] { case 0x03: frameSeq := binary.LittleEndian.Uint16(cmd[4:]) chunkSeq := binary.LittleEndian.Uint16(cmd[12:]) if chunkSeq == 0 { s.waitFSeq = frameSeq s.waitCSeq = 0 s.waitData = s.waitData[:0] payloadSize := binary.LittleEndian.Uint32(cmd[8:]) hdrSize := binary.LittleEndian.Uint16(cmd[14:]) s.waitSize = int(hdrSize) + int(payloadSize) } else if frameSeq != s.waitFSeq || chunkSeq != s.waitCSeq { s.waitCSeq = 0 return msgMediaLost } s.waitData = append(s.waitData, cmd[24:]...) if n := len(s.waitData); n < s.waitSize { s.waitCSeq++ return msgMediaChunk } s.waitCSeq = 0 payloadSize := binary.LittleEndian.Uint32(cmd[8:]) packetData[0] = bytes.Clone(s.waitData[payloadSize:]) packetData[1] = bytes.Clone(s.waitData[:payloadSize]) case 0x04: data := cmd[24:] hdrSize := binary.LittleEndian.Uint16(cmd[14:]) packetData[0] = bytes.Clone(data[:hdrSize]) packetData[1] = bytes.Clone(data[hdrSize:]) default: return msgUnknown } select { case s.rawPkt <- packetData: default: return msgError } return msgMediaFrame case 0x00: switch cmd[1] { case 0x70: _ = s.SessionWrite(0, s.msgAck0070(cmd)) select { case s.rawCmd <- append([]byte{}, cmd[24:]...): default: } return msgCommand case 0x12: _ = s.SessionWrite(0, s.msgAck0012(cmd)) return msgDafang0012 case 0x71: return msgCommandAck } } return msgUnknown } func (s *Session16) msgAck0070(msg28 []byte) []byte { // <- 00700800010000000000000000000000340000007625a02f ... // -> 00710800010000000000000000000000000000007625a02f msg := s.Msg(msgHhrSize + cmdHdrSize) cmd := msg[msgHhrSize:] copy(cmd, "\x00\x71") copy(cmd[2:], msg28[2:6]) // same version and seq copy(cmd[20:], msg28[20:24]) // same msg random return msg } func (s *Session16) msgAck0012(msg28 []byte) []byte { // <- 001208000000000000000000000000000c00000000000000 020000000100000001000000 // -> 00130b000000000000000000000000001400000000000000 0200000001000000010000000000000000000000 const dataSize = 20 msg := s.Msg(msgHhrSize + cmdHdrSize + dataSize) cmd := msg[msgHhrSize:] copy(cmd, "\x00\x13\x0b\x00") cmd[16] = dataSize data := cmd[cmdHdrSize:] copy(data, msg28[cmdHdrSize:]) return msg } func (s *Session16) handleCh1(cmd []byte) int { // Channel 1 used for two-way audio. It's important: // - answer on 0000 command with exact config response (can't set simple proto) // - send 0012 command at start // - respond on every 0008 command for smooth playback switch cid := string(cmd[:2]); cid { case "\x00\x00": // client start _ = s.SessionWrite(1, s.msgAck0000(cmd)) _ = s.SessionWrite(1, s.msg0012()) return msgClientStart case "\x00\x07": // time sync without data _ = s.SessionWrite(1, s.msgAck0007(cmd)) return msgUnknown0007 case "\x00\x08": // time sync with data _ = s.SessionWrite(1, s.msgAck0008(cmd)) return msgUnknown0008 case "\x00\x13": // ack for 0012 return msgUnknown0013 } return msgUnknown } func (s *Session16) msgAck0000(msg28 []byte) []byte { // <- 000008000000000000000000000000001a0200004f47c714 ... 00000000000000000100000004000000fb071f00000000000000000000000300 // -> 00140b00000000000000000000000000200000004f47c714 00000000000000000100000004000000fb071f00000000000000000000000300 const cmdDataSize = 32 msg := s.Msg(msgHhrSize + cmdHdrSize + cmdDataSize) cmd := msg[msgHhrSize:] copy(cmd, "\x00\x14\x0b\x00") cmd[16] = cmdDataSize copy(cmd[20:], msg28[20:24]) // request id (random) // Important to answer with same data. data := cmd[cmdHdrSize:] copy(data, msg28[len(msg28)-32:]) return msg } func (s *Session16) msg0012() []byte { // -> 00120b000000000000000000000000000c00000000000000020000000100000001000000 const dataSize = 12 msg := s.Msg(msgHhrSize + cmdHdrSize + dataSize) cmd := msg[msgHhrSize:] copy(cmd, "\x00\x12\x0b\x00") cmd[16] = dataSize data := cmd[cmdHdrSize:] data[0] = 2 data[4] = 1 data[9] = 1 return msg } func (s *Session16) msgAck0007(msg28 []byte) []byte { // <- 000708000000000000000000000000000c00000001000000000000001c551f7a00000000 // -> 010a0b00000000000000000000000000000000000100000000000000 msg := s.Msg(msgHhrSize + 28) cmd := msg[msgHhrSize:] copy(cmd, "\x01\x0a\x0b\x00") cmd[20] = 1 return msg } func (s *Session16) msgAck0008(msg28 []byte) []byte { // <- 000808000000000000000000000000000000f9f0010000000200000050f31f7a // -> 01090b0000000000000000000000000000000000010000000200000050f31f7a msg := s.Msg(msgHhrSize + 28) cmd := msg[msgHhrSize:] copy(cmd, "\x01\x09\x0b\x00") copy(cmd[20:], msg28[20:]) return msg } func (s *Session16) SessionWrite(chID byte, buf []byte) error { switch chID { case 0: binary.LittleEndian.PutUint16(buf[6:], s.seqSendCh0) s.seqSendCh0++ case 1: binary.LittleEndian.PutUint16(buf[6:], s.seqSendCh1) s.seqSendCh1++ buf[14] = 1 // channel } _, err := s.conn.Write(buf) return err } ================================================ FILE: pkg/tutk/session25.go ================================================ package tutk import ( "bytes" "encoding/binary" "net" "time" ) func NewSession25(conn net.Conn, sid []byte) *Session25 { return &Session25{ Session16: NewSession16(conn, sid), rb: NewReorderBuffer(5), } } type Session25 struct { *Session16 rb *ReorderBuffer seqSendCmd2 uint16 seqSendCnt uint16 seqRecvPkt0 uint16 seqRecvPkt1 uint16 seqRecvCmd2 uint16 } const cmdHdrSize25 = 28 func (s *Session25) SendIOCtrl(ctrlType uint32, ctrlData []byte) []byte { size := msgHhrSize + cmdHdrSize25 + 4 + uint16(len(ctrlData)) msg := s.Msg(size) // 0 0070 command // 2 0b00 version // 4 1000 seq // 6 0076 ??? cmd := msg[msgHhrSize:] copy(cmd, "\x00\x70\x0b\x00") binary.LittleEndian.PutUint16(cmd[4:], s.seqSendCmd1) s.seqSendCmd1++ // 8 0070 command (second time) // 10 0300 seq // 12 0100 chunks count // 14 0000 chunk seq (starts from 0) // 16 5500 size // 18 0000 random msg id (always 0) // 20 03000000 seq (second time) // 24 00000000 // 28 01010000 ctrlType cmd[9] = 0x70 cmd[12] = 1 binary.LittleEndian.PutUint16(cmd[16:], size-52) binary.LittleEndian.PutUint16(cmd[10:], s.seqSendCmd2) binary.LittleEndian.PutUint16(cmd[20:], s.seqSendCmd2) s.seqSendCmd2++ data := cmd[28:] binary.LittleEndian.PutUint32(data, ctrlType) copy(data[4:], ctrlData) return msg } func (s *Session25) SendFrameData(frameInfo, frameData []byte) []byte { return nil } func (s *Session25) SessionRead(chID byte, cmd []byte) (res int) { if chID != 0 { return s.handleCh1(cmd) } switch cmd[0] { case 0x03, 0x05, 0x07: for i := 0; cmd != nil; i++ { res = s.handleChunk(cmd, i == 0) cmd = s.rb.Pop() } return case 0x00: _ = s.SessionWrite(0, s.msgAckCounters()) s.seqRecvCmd2 = binary.LittleEndian.Uint16(cmd[2:]) switch cmd[1] { case 0x10: return msgUnknown0010 // unknown case 0x21: return msgClientStartAck2 case 0x70: select { case s.rawCmd <- cmd[28:]: default: } return msgCommand // cmd from camera case 0x71: return msgCommandAck } case 0x09: // off sample // 0 09000b00 cmd1 // 4 0d000000 seqCmd1 // 12 0000 seqRecvCmd2 seq := binary.LittleEndian.Uint16(cmd[12:]) if s.seqSendCmd1 > seq { return msgCommandAck } return msgCounters case 0x0a: // seq sample // 0 0a080b00 // 4 03000000 // 8 e2043200 // 12 01000000 _ = s.SessionWrite(0, s.msgAck0A08(cmd)) return msgUnknown0a08 } return msgUnknown } func (s *Session25) handleChunk(cmd []byte, checkSeq bool) int { var cmd2 []byte flags := cmd[1] if flags&0b1000 == 0 { // off sample // 0 0700 command // 2 0b00 version // 4 2700 seq // 6 0000 ??? // 8 0700 command (second time) // 10 1400 seq // 12 1300 chunks count per this frame // 14 1100 chunk seq, starts from 0 (0x20 for last chunk) // 16 0004 frame data size // 18 0000 random msg id (always 0) // 20 02000000 previous frame seq, starts from 0 // 24 03000000 current frame seq, starts from 1 cmd2 = cmd[8:] } else { // off sample // 0 070d0b00 // 4 30000000 // 8 5c965500 ??? // 12 ffff0000 ??? // 16 0701 fixed command // 18 190001002000a802000006000000070000000 cmd2 = cmd[16:] } seq := binary.LittleEndian.Uint16(cmd2[2:]) if checkSeq { if s.rb.Check(seq) { s.rb.Next() } else { s.rb.Push(seq, cmd) return msgMediaReorder } } // Check if this is first chunk for frame. // Handle protocol bug "0x20 chunk seq for last chunk" and sometimes // "0x20 chunk seq for first chunk if only one chunk". if binary.LittleEndian.Uint16(cmd2[6:]) == 0 || binary.LittleEndian.Uint16(cmd2[4:]) == 1 { s.waitData = s.waitData[:0] s.waitCSeq = seq } else if seq != s.waitCSeq { return msgMediaLost } s.waitData = append(s.waitData, cmd2[20:]...) if flags&0b0001 == 0 { s.waitCSeq++ return msgMediaChunk } s.seqRecvPkt1 = seq _ = s.SessionWrite(0, s.msgAckCounters()) n := len(s.waitData) - 32 packetData := [2][]byte{bytes.Clone(s.waitData[n:]), bytes.Clone(s.waitData[:n])} select { case s.rawPkt <- packetData: default: return msgError } return msgMediaFrame } func (s *Session25) msgAckCounters() []byte { msg := s.Msg(msgHhrSize + cmdHdrSize) // off sample // 0 09000b00 cmd1 // 4 2700 seqCmd1 // 6 0000 // 8 1300 seqRecvPkt0 // 10 2600 seqRecvPkt1 // 12 0400 seqRecvCmd2 // 14 00000000 // 18 1400 seqSendCnt // 20 d91a random // 22 0000 cmd := msg[msgHhrSize:] copy(cmd, "\x09\x00\x0b\x00") binary.LittleEndian.PutUint16(cmd[4:], s.seqSendCmd1) s.seqSendCmd1++ // seqRecvPkt0 stores previous value of seqRecvPkt1 // don't understand why this needs binary.LittleEndian.PutUint16(cmd[8:], s.seqRecvPkt0) s.seqRecvPkt0 = s.seqRecvPkt1 binary.LittleEndian.PutUint16(cmd[10:], s.seqRecvPkt1) binary.LittleEndian.PutUint16(cmd[12:], s.seqRecvCmd2) binary.LittleEndian.PutUint16(cmd[18:], s.seqSendCnt) s.seqSendCnt++ binary.LittleEndian.PutUint16(cmd[20:], uint16(time.Now().UnixMilli())) return msg } func (s *Session25) handleCh1(cmd []byte) int { switch cid := string(cmd[:2]); cid { case "\x00\x00": // client start return msgClientStart case "\x00\x07": // time sync without data _ = s.SessionWrite(1, s.msgAck0007(cmd)) return msgUnknown0007 case "\x00\x20": // client start2 _ = s.SessionWrite(1, s.msgAck0020(cmd)) return msgClientStart2 case "\x09\x00": return msgUnknown0900 case "\x0a\x08": return msgUnknown0a08 } return msgUnknown } func (s *Session25) msgAck0020(msg28 []byte) []byte { const cmdDataSize = 36 msg := s.Msg(msgHhrSize + cmdHdrSize25 + cmdDataSize) cmd := msg[msgHhrSize:] copy(cmd, "\x00\x21\x0b\x00") cmd[16] = cmdDataSize copy(cmd[20:], msg28[20:24]) // request id (random) // 0 00000000 // 4 00010001 // 8 01000000 // 12 04000000 // 16 fb071f00 // 20 00000000 // 24 00000000 // 28 00000300 // 32 01000000 data := cmd[cmdHdrSize25:] data[5] = 1 data[7] = 1 data[8] = 1 data[12] = 4 copy(data[16:], "\xfb\x07\x1f\x00") data[30] = 3 data[32] = 1 return msg } func (s *Session25) msgAck0A08(msg28 []byte) []byte { // <- 0a080b005b0000000b51590002000000 // -> 0b000b00000001000b5103000300000000000000 msg := s.Msg(msgHhrSize + 20) cmd := msg[msgHhrSize:] copy(cmd, "\x0b\x00\x0b\x00") copy(cmd[8:], msg28[8:10]) return msg } // ReorderBuffer used for UDP incoming data. Because the order of the packets may be mixed up. type ReorderBuffer struct { buf map[uint16][]byte seq uint16 size int } func NewReorderBuffer(size int) *ReorderBuffer { return &ReorderBuffer{buf: make(map[uint16][]byte), size: size} } // Check return OK if this is the seq we are waiting for. func (r *ReorderBuffer) Check(seq uint16) (ok bool) { return seq == r.seq } func (r *ReorderBuffer) Next() { r.seq++ } // Available return how much free slots is in the buffer. func (r *ReorderBuffer) Available() int { return r.size - len(r.buf) } // Push new item to buffer. Important! There is no buffer full check here. func (r *ReorderBuffer) Push(seq uint16, data []byte) { //log.Printf("push seq=%d wait=%d", seq, r.seq) r.buf[seq] = bytes.Clone(data) } // Pop latest item from buffer. OK - if items wasn't dropped. func (r *ReorderBuffer) Pop() []byte { for { if data := r.buf[r.seq]; data != nil { delete(r.buf, r.seq) r.Next() //log.Printf("pop seq=%d", r.seq) return data } if r.Available() > 0 { return nil } //log.Printf("drop seq=%d", r.seq) r.Next() // drop item } } ================================================ FILE: pkg/tuya/README.md ================================================ ## Useful links - https://developer.tuya.com/en/docs/iot/webrtc?id=Kacsd4x2hl0se - https://github.com/tuya/webrtc-demo-go - https://github.com/bacco007/HomeAssistantConfig/blob/master/custom_components/xtend_tuya/multi_manager/tuya_iot/ipc/webrtc/xt_tuya_iot_webrtc_manager.py - https://github.com/tuya/tuya-device-sharing-sdk - https://github.com/make-all/tuya-local/blob/main/custom_components/tuya_local/cloud.py - https://ipc-us.ismartlife.me/ - https://protect-us.ismartlife.me/ ================================================ FILE: pkg/tuya/client.go ================================================ package tuya import ( "encoding/json" "errors" "fmt" "net/url" "regexp" "strings" "sync" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webrtc" "github.com/pion/rtp" pion "github.com/pion/webrtc/v4" ) type Client struct { api TuyaAPI conn *webrtc.Conn pc *pion.PeerConnection connected core.Waiter closed bool // HEVC only: dc *pion.DataChannel videoSSRC *uint32 audioSSRC *uint32 streamType int isHEVC bool handlersMu sync.RWMutex handlers map[uint32]func(*rtp.Packet) } type DataChannelMessage struct { Type string `json:"type"` // "codec", "start", "recv", "complete" Msg string `json:"msg"` } // RecvMessage contains SSRC values for video/audio streams type RecvMessage struct { Video struct { SSRC uint32 `json:"ssrc"` } `json:"video"` Audio struct { SSRC uint32 `json:"ssrc"` } `json:"audio"` } func Dial(rawURL string) (core.Producer, error) { escapedURL := strings.ReplaceAll(rawURL, "#", "%23") u, err := url.Parse(escapedURL) if err != nil { return nil, err } query := u.Query() // Tuya Smart API email := query.Get("email") password := query.Get("password") // Tuya Cloud API uid := query.Get("uid") clientId := query.Get("client_id") clientSecret := query.Get("client_secret") // Shared params deviceId := query.Get("device_id") // Stream params streamResolution := query.Get("resolution") useSmartApi := deviceId != "" && email != "" && password != "" useCloudApi := deviceId != "" && uid != "" && clientId != "" && clientSecret != "" if streamResolution == "" || (streamResolution != "hd" && streamResolution != "sd") { streamResolution = "hd" } if !useSmartApi && !useCloudApi { return nil, errors.New("tuya: wrong query params") } client := &Client{ handlers: make(map[uint32]func(*rtp.Packet)), } if useSmartApi { if client.api, err = NewTuyaSmartApiClient(nil, u.Hostname(), email, password, deviceId); err != nil { return nil, fmt.Errorf("tuya: %w", err) } } else { if client.api, err = NewTuyaCloudApiClient(u.Hostname(), uid, deviceId, clientId, clientSecret); err != nil { return nil, fmt.Errorf("tuya: %w", err) } } if err := client.api.Init(); err != nil { return nil, fmt.Errorf("tuya: %w", err) } client.streamType = client.api.GetStreamType(streamResolution) client.isHEVC = client.api.IsHEVC(client.streamType) // Create a new PeerConnection conf := pion.Configuration{ ICEServers: client.api.GetICEServers(), ICETransportPolicy: pion.ICETransportPolicyAll, BundlePolicy: pion.BundlePolicyMaxBundle, } api, err := webrtc.NewAPI() if err != nil { client.Close(err) return nil, err } client.pc, err = api.NewPeerConnection(conf) if err != nil { client.Close(err) return nil, err } // protect from sending ICE candidate before Offer var sendOffer core.Waiter // protect from blocking on errors defer sendOffer.Done(nil) // Create new WebRTC connection client.conn = webrtc.NewConn(client.pc) client.conn.FormatName = "tuya/webrtc" client.conn.Mode = core.ModeActiveProducer client.conn.Protocol = "mqtt" mqttClient := client.api.GetMqtt() if mqttClient == nil { err = errors.New("tuya: no mqtt client") client.Close(err) return nil, err } // Set up MQTT handlers mqttClient.handleAnswer = func(answer AnswerFrame) { // fmt.Printf("tuya: answer: %s\n", answer.Sdp) desc := pion.SessionDescription{ Type: pion.SDPTypePranswer, SDP: answer.Sdp, } if err = client.pc.SetRemoteDescription(desc); err != nil { client.Close(err) return } if err = client.conn.SetAnswer(answer.Sdp); err != nil { client.Close(err) return } if client.isHEVC { // Tuya responds with H264/90000 even for HEVC streams // So we need to replace video codecs with HEVC ones from API for _, media := range client.conn.Medias { if media.Kind == core.KindVideo { codecs := client.api.GetVideoCodecs() if codecs != nil { media.Codecs = codecs } } } // Audio codecs from API as well // Tuya responds with multiple audio codecs (PCMU, PCMA) // But the quality is bad if we use PCMU and skill only has PCMA for _, media := range client.conn.Medias { if media.Kind == core.KindAudio { codecs := client.api.GetAudioCodecs() if codecs != nil { media.Codecs = codecs } } } } } mqttClient.handleCandidate = func(candidate CandidateFrame) { // fmt.Printf("tuya: candidate: %s\n", candidate.Candidate) if candidate.Candidate != "" { client.conn.AddCandidate(candidate.Candidate) if err != nil { client.Close(err) } } } mqttClient.handleDisconnect = func() { // fmt.Println("tuya: disconnect") client.Close(errors.New("mqtt: disconnect")) } mqttClient.handleError = func(err error) { // fmt.Printf("tuya: error: %s\n", err.Error()) client.Close(err) } if client.isHEVC { maxRetransmits := uint16(5) ordered := true client.dc, err = client.pc.CreateDataChannel("fmp4Stream", &pion.DataChannelInit{ MaxRetransmits: &maxRetransmits, Ordered: &ordered, }) // DataChannel receives two types of messages: // 1. String messages: Control messages (codec, recv) // 2. Binary messages: RTP packets with video/audio client.dc.OnMessage(func(msg pion.DataChannelMessage) { if msg.IsString { // Handle control messages (codec, recv, etc.) if connected, err := client.probe(msg); err != nil { client.Close(err) } else if connected { client.connected.Done(nil) } } else { // Handle RTP packets - Route by SSRC retrieved from "recv" message packet := &rtp.Packet{} if err := packet.Unmarshal(msg.Data); err != nil { // Skip invalid packets return } if handler, ok := client.getHandler(packet.SSRC); ok { handler(packet) } } }) client.dc.OnError(func(err error) { // fmt.Printf("tuya: datachannel error: %s\n", err.Error()) client.Close(err) }) client.dc.OnClose(func() { // fmt.Println("tuya: datachannel closed") client.Close(errors.New("datachannel: closed")) }) client.dc.OnOpen(func() { // fmt.Println("tuya: datachannel opened") codecRequest, _ := json.Marshal(DataChannelMessage{ Type: "codec", Msg: "", }) if err := client.sendMessageToDataChannel(codecRequest); err != nil { client.Close(fmt.Errorf("failed to send codec request: %w", err)) } }) } // Set up pc handler client.conn.Listen(func(msg any) { switch msg := msg.(type) { case *pion.ICECandidate: _ = sendOffer.Wait() if err := mqttClient.SendCandidate("a=" + msg.ToJSON().Candidate); err != nil { client.Close(err) } case pion.PeerConnectionState: switch msg { case pion.PeerConnectionStateNew: break case pion.PeerConnectionStateConnecting: break case pion.PeerConnectionStateConnected: // On HEVC, wait for DataChannel to be opened and camera to send codec info if !client.isHEVC { if streamResolution == "hd" { _ = mqttClient.SendResolution(0) } client.connected.Done(nil) } case pion.PeerConnectionStateClosed: client.Close(errors.New("webrtc: " + msg.String())) default: // client.Close(errors.New("webrtc: " + msg.String())) } } }) // Audio first, otherwise tuya will send corrupt sdp medias := []*core.Media{ {Kind: core.KindAudio, Direction: core.DirectionSendRecv}, {Kind: core.KindVideo, Direction: core.DirectionRecvonly}, } // Create offer offer, err := client.conn.CreateOffer(medias) if err != nil { client.Close(err) return nil, err } // horter sdp, remove a=extmap... line, device ONLY allow 8KB json payload // https://github.com/tuya/webrtc-demo-go/blob/04575054f18ccccb6bc9d82939dd46d449544e20/static/js/main.js#L224 re := regexp.MustCompile(`\r\na=extmap[^\r\n]*`) offer = re.ReplaceAllString(offer, "") // Send offer if err := mqttClient.SendOffer(offer, streamResolution, client.streamType, client.isHEVC); err != nil { err = fmt.Errorf("tuya: %w", err) client.Close(err) return nil, err } sendOffer.Done(nil) // Wait for connection if err = client.connected.Wait(); err != nil { err = fmt.Errorf("tuya: %w", err) client.Close(err) return nil, err } return client, nil } func (c *Client) GetMedias() []*core.Media { return c.conn.GetMedias() } func (c *Client) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return c.conn.GetTrack(media, codec) } func (c *Client) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { localTrack := c.conn.GetSenderTrack(media.ID) if localTrack == nil { return errors.New("webrtc: can't get track") } // DISABLED: Speaker Protocol 312 command // JavaScript client doesn't send this on first call either // Only subsequent calls (when speakerChloron is set) send Protocol 312 // mqttClient := c.api.GetMqtt() // if mqttClient != nil { // _ = mqttClient.SendSpeaker(1) // } payloadType := codec.PayloadType sender := core.NewSender(media, codec) switch track.Codec.Name { case core.CodecPCMA, core.CodecPCMU, core.CodecPCM, core.CodecPCML: // Frame size affects audio delay with Tuya cameras: // Browser sends standard 20ms frames (160 bytes for G.711), but this causes // up to 4s delay on some Tuya cameras. Increasing to 240 bytes (30ms) reduces // delay to ~2s. Higher values (320+ bytes) don't work and cause issues. // Using 240 bytes (30ms) as optimal balance between latency and stability. frameSize := 240 var buf []byte var seq uint16 var ts uint32 sender.Handler = func(packet *rtp.Packet) { buf = append(buf, packet.Payload...) for len(buf) >= frameSize { payload := buf[:frameSize] pkt := &rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: true, PayloadType: payloadType, SequenceNumber: seq, Timestamp: ts, SSRC: packet.SSRC, }, Payload: payload, } seq++ ts += uint32(frameSize) buf = buf[frameSize:] c.conn.Send += pkt.MarshalSize() _ = localTrack.WriteRTP(payloadType, pkt) } } default: sender.Handler = func(packet *rtp.Packet) { c.conn.Send += packet.MarshalSize() _ = localTrack.WriteRTP(payloadType, packet) } } sender.HandleRTP(track) c.conn.Senders = append(c.conn.Senders, sender) return nil } func (c *Client) Start() error { if len(c.conn.Receivers) == 0 { return errors.New("tuya: no receivers") } var video, audio *core.Receiver for _, receiver := range c.conn.Receivers { if receiver.Codec.IsVideo() { video = receiver } else if receiver.Codec.IsAudio() { audio = receiver } } if c.videoSSRC != nil { c.setHandler(*c.videoSSRC, func(packet *rtp.Packet) { if video != nil { video.WriteRTP(packet) } }) } if c.audioSSRC != nil { c.setHandler(*c.audioSSRC, func(packet *rtp.Packet) { if audio != nil { audio.WriteRTP(packet) } }) } return c.conn.Start() } func (c *Client) Stop() error { if c.closed { return nil } c.closed = true c.clearHandlers() if c.conn != nil { _ = c.conn.Stop() } if c.api != nil { c.api.Close() } return nil } func (c *Client) Close(err error) error { c.connected.Done(err) return c.Stop() } func (c *Client) MarshalJSON() ([]byte, error) { return c.conn.MarshalJSON() } func (c *Client) setHandler(ssrc uint32, handler func(*rtp.Packet)) { c.handlersMu.Lock() defer c.handlersMu.Unlock() c.handlers[ssrc] = handler } func (c *Client) getHandler(ssrc uint32) (func(*rtp.Packet), bool) { c.handlersMu.RLock() defer c.handlersMu.RUnlock() handler, ok := c.handlers[ssrc] return handler, ok } func (c *Client) clearHandlers() { c.handlersMu.Lock() defer c.handlersMu.Unlock() for ssrc := range c.handlers { delete(c.handlers, ssrc) } } func (c *Client) probe(msg pion.DataChannelMessage) (bool, error) { // fmt.Printf("[tuya] Received string message: %s\n", string(msg.Data)) var message DataChannelMessage if err := json.Unmarshal([]byte(msg.Data), &message); err != nil { return false, err } switch message.Type { case "codec": // Camera responded to our codec request - now request frame start frameRequest, _ := json.Marshal(DataChannelMessage{ Type: "start", Msg: "frame", }) err := c.sendMessageToDataChannel(frameRequest) if err != nil { return false, err } case "recv": // Camera sends SSRC values for video/audio streams // We need these to route incoming RTP packets correctly var recvMessage RecvMessage if err := json.Unmarshal([]byte(message.Msg), &recvMessage); err != nil { return false, err } videoSSRC := recvMessage.Video.SSRC audioSSRC := recvMessage.Audio.SSRC c.videoSSRC = &videoSSRC c.audioSSRC = &audioSSRC // Send "complete" to tell camera we're ready to receive RTP packets completeMsg, _ := json.Marshal(DataChannelMessage{ Type: "complete", Msg: "", }) err := c.sendMessageToDataChannel(completeMsg) if err != nil { return false, err } return true, nil } return false, nil } func (c *Client) sendMessageToDataChannel(message []byte) error { if c.dc != nil { // fmt.Printf("[tuya] sending message to data channel: %s\n", message) return c.dc.Send(message) } return nil } ================================================ FILE: pkg/tuya/cloud_api.go ================================================ package tuya import ( "bytes" "crypto/md5" "encoding/json" "errors" "fmt" "io" "net/http" "strconv" "time" "github.com/AlexxIT/go2rtc/pkg/webrtc" "github.com/google/uuid" ) type Token struct { UID string `json:"uid"` AccessToken string `json:"access_token"` RefreshToken string `json:"refresh_token"` ExpireTime int64 `json:"expire_time"` } type WebRTCConfigResponse struct { Timestamp int64 `json:"t"` Success bool `json:"success"` Result WebRTCConfig `json:"result"` Msg string `json:"msg,omitempty"` Code int `json:"code,omitempty"` } type TokenResponse struct { Timestamp int64 `json:"t"` Success bool `json:"success"` Result Token `json:"result"` Msg string `json:"msg,omitempty"` Code int `json:"code,omitempty"` } type OpenIoTHubConfigRequest struct { UID string `json:"uid"` UniqueID string `json:"unique_id"` LinkType string `json:"link_type"` Topics string `json:"topics"` } type OpenIoTHubConfig struct { Url string `json:"url"` ClientID string `json:"client_id"` Username string `json:"username"` Password string `json:"password"` SinkTopic struct { IPC string `json:"ipc"` } `json:"sink_topic"` SourceSink struct { IPC string `json:"ipc"` } `json:"source_topic"` ExpireTime int `json:"expire_time"` } type OpenIoTHubConfigResponse struct { Timestamp int `json:"t"` Success bool `json:"success"` Result OpenIoTHubConfig `json:"result"` Msg string `json:"msg,omitempty"` Code int `json:"code,omitempty"` } type TuyaCloudApiClient struct { TuyaClient uid string clientId string clientSecret string accessToken string refreshToken string refreshingToken bool } func NewTuyaCloudApiClient(baseUrl, uid, deviceId, clientId, clientSecret string) (*TuyaCloudApiClient, error) { mqttClient := NewTuyaMqttClient(deviceId) client := &TuyaCloudApiClient{ TuyaClient: TuyaClient{ httpClient: &http.Client{Timeout: 15 * time.Second}, mqtt: mqttClient, deviceId: deviceId, expireTime: 0, baseUrl: baseUrl, }, uid: uid, clientId: clientId, clientSecret: clientSecret, refreshingToken: false, } return client, nil } // WebRTC Flow func (c *TuyaCloudApiClient) Init() error { if err := c.initToken(); err != nil { return fmt.Errorf("failed to initialize token: %w", err) } webrtcConfig, err := c.loadWebrtcConfig() if err != nil { return fmt.Errorf("failed to load webrtc config: %w", err) } hubConfig, err := c.loadHubConfig() if err != nil { return fmt.Errorf("failed to load hub config: %w", err) } if err := c.mqtt.Start(hubConfig, webrtcConfig, c.skill.WebRTC); err != nil { return fmt.Errorf("failed to start MQTT: %w", err) } if c.skill.LowPower > 0 { _ = c.mqtt.WakeUp(c.localKey) } return nil } func (c *TuyaCloudApiClient) GetStreamUrl(streamType string) (streamUrl string, err error) { if err := c.initToken(); err != nil { return "", fmt.Errorf("failed to initialize token: %w", err) } url := fmt.Sprintf("https://%s/v1.0/devices/%s/stream/actions/allocate", c.baseUrl, c.deviceId) request := &AllocateRequest{ Type: streamType, } body, err := c.request("POST", url, request) if err != nil { return "", err } var allocResponse AllocateResponse err = json.Unmarshal(body, &allocResponse) if err != nil { return "", err } if !allocResponse.Success { return "", errors.New(allocResponse.Msg) } return allocResponse.Result.URL, nil } func (c *TuyaCloudApiClient) initToken() (err error) { if c.refreshingToken { return nil } now := time.Now().Unix() if (c.expireTime - 60) > now { return nil } c.refreshingToken = true url := fmt.Sprintf("https://%s/v1.0/token?grant_type=1", c.baseUrl) c.accessToken = "" c.refreshToken = "" body, err := c.request("GET", url, nil) if err != nil { return err } var tokenResponse TokenResponse err = json.Unmarshal(body, &tokenResponse) if err != nil { return err } if !tokenResponse.Success { return errors.New(tokenResponse.Msg) } c.accessToken = tokenResponse.Result.AccessToken c.refreshToken = tokenResponse.Result.RefreshToken c.expireTime = tokenResponse.Timestamp + tokenResponse.Result.ExpireTime c.refreshingToken = false return nil } func (c *TuyaCloudApiClient) loadWebrtcConfig() (*WebRTCConfig, error) { url := fmt.Sprintf("https://%s/v1.0/users/%s/devices/%s/webrtc-configs", c.baseUrl, c.uid, c.deviceId) body, err := c.request("GET", url, nil) if err != nil { return nil, err } var webRTCConfigResponse WebRTCConfigResponse err = json.Unmarshal(body, &webRTCConfigResponse) if err != nil { return nil, err } if !webRTCConfigResponse.Success { return nil, fmt.Errorf(webRTCConfigResponse.Msg) } err = json.Unmarshal([]byte(webRTCConfigResponse.Result.Skill), &c.skill) if err != nil { return nil, err } // Store LocalKey (not sure if cloud api provides this, but we need it for low power cameras) c.localKey = webRTCConfigResponse.Result.LocalKey iceServers, err := json.Marshal(&webRTCConfigResponse.Result.P2PConfig.Ices) if err != nil { return nil, err } c.iceServers, err = webrtc.UnmarshalICEServers(iceServers) if err != nil { return nil, err } return &webRTCConfigResponse.Result, nil } func (c *TuyaCloudApiClient) loadHubConfig() (config *MQTTConfig, err error) { url := fmt.Sprintf("https://%s/v2.0/open-iot-hub/access/config", c.baseUrl) request := &OpenIoTHubConfigRequest{ UID: c.uid, UniqueID: uuid.New().String(), LinkType: "mqtt", Topics: "ipc", } body, err := c.request("POST", url, request) if err != nil { return nil, err } var openIoTHubConfigResponse OpenIoTHubConfigResponse err = json.Unmarshal(body, &openIoTHubConfigResponse) if err != nil { return nil, err } if !openIoTHubConfigResponse.Success { return nil, fmt.Errorf(openIoTHubConfigResponse.Msg) } return &MQTTConfig{ Url: openIoTHubConfigResponse.Result.Url, Username: openIoTHubConfigResponse.Result.Username, Password: openIoTHubConfigResponse.Result.Password, ClientID: openIoTHubConfigResponse.Result.ClientID, PublishTopic: openIoTHubConfigResponse.Result.SinkTopic.IPC, SubscribeTopic: openIoTHubConfigResponse.Result.SourceSink.IPC, }, nil } func (c *TuyaCloudApiClient) request(method string, url string, body any) ([]byte, error) { var bodyReader io.Reader if body != nil { jsonBody, err := json.Marshal(body) if err != nil { return nil, err } bodyReader = bytes.NewReader(jsonBody) } req, err := http.NewRequest(method, url, bodyReader) if err != nil { return nil, err } ts := time.Now().UnixNano() / 1000000 sign := c.calBusinessSign(ts) req.Header.Set("Accept", "*") req.Header.Set("Content-Type", "application/json") req.Header.Set("Access-Control-Allow-Origin", "*") req.Header.Set("Access-Control-Allow-Methods", "*") req.Header.Set("Access-Control-Allow-Headers", "*") req.Header.Set("mode", "no-cors") req.Header.Set("client_id", c.clientId) req.Header.Set("access_token", c.accessToken) req.Header.Set("sign", sign) req.Header.Set("t", strconv.FormatInt(ts, 10)) response, err := c.httpClient.Do(req) if err != nil { return nil, err } defer response.Body.Close() res, err := io.ReadAll(response.Body) if err != nil { return nil, err } if response.StatusCode != http.StatusOK { return nil, err } return res, nil } func (c *TuyaCloudApiClient) calBusinessSign(ts int64) string { data := fmt.Sprintf("%s%s%s%d", c.clientId, c.accessToken, c.clientSecret, ts) val := md5.Sum([]byte(data)) res := fmt.Sprintf("%X", val) return res } ================================================ FILE: pkg/tuya/helper.go ================================================ package tuya import ( "crypto/md5" cryptoRand "crypto/rand" "crypto/rsa" "crypto/x509" "encoding/hex" "encoding/pem" "errors" "net/http" "net/http/cookiejar" "regexp" "time" "golang.org/x/net/publicsuffix" ) func EncryptPassword(password, pbKey string) (string, error) { // Hash password with MD5 hasher := md5.New() hasher.Write([]byte(password)) hashedPassword := hex.EncodeToString(hasher.Sum(nil)) // Decode PEM public key block, _ := pem.Decode([]byte("-----BEGIN PUBLIC KEY-----\n" + pbKey + "\n-----END PUBLIC KEY-----")) if block == nil { return "", errors.New("failed to decode PEM block") } pubKey, err := x509.ParsePKIXPublicKey(block.Bytes) if err != nil { return "", err } rsaPubKey, ok := pubKey.(*rsa.PublicKey) if !ok { return "", errors.New("not an RSA public key") } // Encrypt with RSA encrypted, err := rsa.EncryptPKCS1v15(cryptoRand.Reader, rsaPubKey, []byte(hashedPassword)) if err != nil { return "", err } // Convert to hex string return hex.EncodeToString(encrypted), nil } func IsEmailAddress(input string) bool { emailRegex := regexp.MustCompile(`^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$`) return emailRegex.MatchString(input) } func CreateHTTPClientWithSession() *http.Client { jar, err := cookiejar.New(&cookiejar.Options{ PublicSuffixList: publicsuffix.List, }) if err != nil { return nil } return &http.Client{ Timeout: 30 * time.Second, Jar: jar, } } ================================================ FILE: pkg/tuya/interface.go ================================================ package tuya import ( "net/http" "github.com/AlexxIT/go2rtc/pkg/core" pionWebrtc "github.com/pion/webrtc/v4" ) type TuyaAPI interface { GetMqtt() *TuyaMqttClient GetStreamType(streamResolution string) int IsHEVC(streamType int) bool GetVideoCodecs() []*core.Codec GetAudioCodecs() []*core.Codec GetStreamUrl(streamUrl string) (string, error) GetICEServers() []pionWebrtc.ICEServer Init() error Close() } type TuyaClient struct { TuyaAPI httpClient *http.Client mqtt *TuyaMqttClient baseUrl string expireTime int64 deviceId string localKey string skill *Skill iceServers []pionWebrtc.ICEServer } type AudioAttributes struct { CallMode []int `json:"call_mode"` // 1 = one way, 2 = two way HardwareCapability []int `json:"hardware_capability"` // 1 = mic, 2 = speaker } type ICEServer struct { Urls string `json:"urls"` Username string `json:"username,omitempty"` Credential string `json:"credential,omitempty"` TTL int `json:"ttl,omitempty"` } type WebICE struct { Urls string `json:"urls"` Username string `json:"username,omitempty"` Credential string `json:"credential,omitempty"` } type P2PConfig struct { Ices []ICEServer `json:"ices"` } type AudioSkill struct { Channels int `json:"channels"` DataBit int `json:"dataBit"` CodecType int `json:"codecType"` SampleRate int `json:"sampleRate"` } type VideoSkill struct { StreamType int `json:"streamType"` // 2 = main stream (HD), 4 = sub stream (SD) CodecType int `json:"codecType"` // 2 = H264, 4 = H265 (HEVC) Width int `json:"width"` Height int `json:"height"` SampleRate int `json:"sampleRate"` ProfileId string `json:"profileId,omitempty"` } type Skill struct { WebRTC int `json:"webrtc"` // Bit flags: bit 4=speaker, bit 5=clarity, bit 6=record LowPower int `json:"lowPower,omitempty"` // 1 = battery-powered camera Audios []AudioSkill `json:"audios"` Videos []VideoSkill `json:"videos"` } type WebRTCConfig struct { AudioAttributes AudioAttributes `json:"audio_attributes"` Auth string `json:"auth"` ID string `json:"id"` LocalKey string `json:"local_key,omitempty"` MotoID string `json:"moto_id"` P2PConfig P2PConfig `json:"p2p_config"` ProtocolVersion string `json:"protocol_version"` Skill string `json:"skill"` SupportsWebRTCRecord bool `json:"supports_webrtc_record"` SupportsWebRTC bool `json:"supports_webrtc"` VedioClaritiy int `json:"vedio_clarity"` VideoClaritiy int `json:"video_clarity"` VideoClarities []int `json:"video_clarities"` } type MQTTConfig struct { Url string `json:"url"` PublishTopic string `json:"publish_topic"` SubscribeTopic string `json:"subscribe_topic"` ClientID string `json:"client_id"` Username string `json:"username"` Password string `json:"password"` } type Allocate struct { URL string `json:"url"` } type AllocateRequest struct { Type string `json:"type"` } type AllocateResponse struct { Success bool `json:"success"` Result Allocate `json:"result"` Msg string `json:"msg,omitempty"` } func (c *TuyaClient) GetICEServers() []pionWebrtc.ICEServer { return c.iceServers } func (c *TuyaClient) GetMqtt() *TuyaMqttClient { return c.mqtt } // GetStreamType returns the Skill StreamType for the requested resolution // Returns Skill values (2 or 4), not MQTT values (0 or 1) // - "hd" → highest resolution streamType (usually 2 = mainStream) // - "sd" → lowest resolution streamType (usually 4 = substream) // // These values must be mapped before sending to MQTT: // - streamType 2 → MQTT stream_type 0 // - streamType 4 → MQTT stream_type 1 func (c *TuyaClient) GetStreamType(streamResolution string) int { // Default streamType if nothing is found defaultStreamType := 1 if c.skill == nil || len(c.skill.Videos) == 0 { return defaultStreamType } // Find the highest and lowest resolution based on pixel count var highestResType = defaultStreamType var highestRes = 0 var lowestResType = defaultStreamType var lowestRes = 0 for _, video := range c.skill.Videos { res := video.Width * video.Height // Highest Resolution if res > highestRes { highestRes = res highestResType = video.StreamType } // Lower Resolution (or first if not set yet) if lowestRes == 0 || res < lowestRes { lowestRes = res lowestResType = video.StreamType } } // Return the streamType based on the selection switch streamResolution { case "hd": return highestResType case "sd": return lowestResType default: return defaultStreamType } } // IsHEVC checks if the given streamType uses H265 (HEVC) codec // HEVC cameras use DataChannel, H264 cameras use RTP tracks // - codecType 4 = H265 (HEVC) → DataChannel mode // - codecType 2 = H264 → Normal RTP mode func (c *TuyaClient) IsHEVC(streamType int) bool { for _, video := range c.skill.Videos { if video.StreamType == streamType { return video.CodecType == 4 // 4 = H265/HEVC } } return false } func (c *TuyaClient) GetVideoCodecs() []*core.Codec { if len(c.skill.Videos) > 0 { codecs := make([]*core.Codec, 0) for _, video := range c.skill.Videos { name := core.CodecH264 if c.IsHEVC(video.StreamType) { name = core.CodecH265 } codec := &core.Codec{ Name: name, ClockRate: uint32(video.SampleRate), } codecs = append(codecs, codec) } if len(codecs) > 0 { return codecs } } return nil } func (c *TuyaClient) GetAudioCodecs() []*core.Codec { if len(c.skill.Audios) > 0 { codecs := make([]*core.Codec, 0) for _, audio := range c.skill.Audios { name := getAudioCodecName(&audio) codec := &core.Codec{ Name: name, ClockRate: uint32(audio.SampleRate), Channels: uint8(audio.Channels), } codecs = append(codecs, codec) } if len(codecs) > 0 { return codecs } } return nil } func (c *TuyaClient) Close() { c.mqtt.Stop() c.httpClient.CloseIdleConnections() } // https://protect-us.ismartlife.me/ func getAudioCodecName(audioSkill *AudioSkill) string { switch audioSkill.CodecType { // case 100: // return "ADPCM" case 101: return core.CodecPCML case 102, 103, 104: return core.CodecAAC case 105: return core.CodecPCMU case 106: return core.CodecPCMA // case 107: // return "G726-32" // case 108: // return "SPEEX" case 109: return core.CodecMP3 default: return core.CodecPCML } } ================================================ FILE: pkg/tuya/mqtt.go ================================================ package tuya import ( "encoding/hex" "encoding/json" "errors" "fmt" "hash/crc32" "strings" "time" "github.com/AlexxIT/go2rtc/pkg/core" mqtt "github.com/eclipse/paho.mqtt.golang" ) type TuyaMqttClient struct { client mqtt.Client waiter core.Waiter wakeupWaiter core.Waiter speakerWaiter core.Waiter publishTopic string subscribeTopic string auth string iceServers []ICEServer uid string motoId string deviceId string sessionId string closed bool webrtcVersion int handleAnswer func(answer AnswerFrame) handleCandidate func(candidate CandidateFrame) handleDisconnect func() handleError func(err error) } type MqttFrameHeader struct { Type string `json:"type"` From string `json:"from"` To string `json:"to"` SubDevID string `json:"sub_dev_id"` SessionID string `json:"sessionid"` MotoID string `json:"moto_id"` TransactionID string `json:"tid"` } type MqttFrame struct { Header MqttFrameHeader `json:"header"` Message json.RawMessage `json:"msg"` } type OfferFrame struct { Mode string `json:"mode"` Sdp string `json:"sdp"` StreamType int `json:"stream_type"` // 0: mainStream(HD), 1: substream(SD) Auth string `json:"auth"` DatachannelEnable bool `json:"datachannel_enable"` // true for HEVC, false for H264 Token []ICEServer `json:"token"` } type AnswerFrame struct { Mode string `json:"mode"` Sdp string `json:"sdp"` } type CandidateFrame struct { Mode string `json:"mode"` Candidate string `json:"candidate"` } type ResolutionFrame struct { Mode string `json:"mode"` Value int `json:"cmdValue"` // 0: HD, 1: SD } type SpeakerFrame struct { Mode string `json:"mode"` Value int `json:"cmdValue"` // 0: off, 1: on } type DisconnectFrame struct { Mode string `json:"mode"` } type MqttLowPowerMessage struct { Protocol int `json:"protocol"` T int `json:"t"` S int `json:"s,omitempty"` Type string `json:"type,omitempty"` Data struct { DevID string `json:"devId,omitempty"` Online bool `json:"online,omitempty"` LastOnlineChangeTime int64 `json:"lastOnlineChangeTime,omitempty"` GwID string `json:"gwId,omitempty"` Cmd string `json:"cmd,omitempty"` Dps map[string]interface{} `json:"dps,omitempty"` } `json:"data"` } type MqttMessage struct { Protocol int `json:"protocol"` Pv string `json:"pv"` T int64 `json:"t"` Data MqttFrame `json:"data"` } func NewTuyaMqttClient(deviceId string) *TuyaMqttClient { return &TuyaMqttClient{ deviceId: deviceId, sessionId: core.RandString(6, 62), waiter: core.Waiter{}, wakeupWaiter: core.Waiter{}, } } func (c *TuyaMqttClient) Start(hubConfig *MQTTConfig, webrtcConfig *WebRTCConfig, webrtcVersion int) error { c.webrtcVersion = webrtcVersion c.motoId = webrtcConfig.MotoID c.auth = webrtcConfig.Auth c.iceServers = webrtcConfig.P2PConfig.Ices c.publishTopic = hubConfig.PublishTopic c.subscribeTopic = hubConfig.SubscribeTopic c.publishTopic = strings.Replace(c.publishTopic, "moto_id", c.motoId, 1) c.publishTopic = strings.Replace(c.publishTopic, "{device_id}", c.deviceId, 1) parts := strings.Split(c.subscribeTopic, "/") c.uid = parts[3] opts := mqtt.NewClientOptions().AddBroker(hubConfig.Url). SetClientID(hubConfig.ClientID). SetUsername(hubConfig.Username). SetPassword(hubConfig.Password). SetOnConnectHandler(c.onConnect). SetAutoReconnect(true). SetMaxReconnectInterval(30 * time.Second). SetConnectTimeout(30 * time.Second). SetKeepAlive(60 * time.Second). SetPingTimeout(20 * time.Second) c.client = mqtt.NewClient(opts) if token := c.client.Connect(); token.Wait() && token.Error() != nil { return token.Error() } if err := c.waiter.Wait(); err != nil { return err } return nil } func (c *TuyaMqttClient) Stop() { c.waiter.Done(errors.New("mqtt: stopped")) c.wakeupWaiter.Done(errors.New("mqtt: stopped")) c.speakerWaiter.Done(errors.New("mqtt: stopped")) if c.client != nil { _ = c.SendDisconnect() c.client.Disconnect(100) } c.closed = true } // WakeUp sends a wake-up signal to battery-powered cameras (LowPower mode). // The camera wakes up and starts responding immediately - we don't wait for dps[149]. // Note: LowPower cameras sleep after ~3 minutes of inactivity. func (c *TuyaMqttClient) WakeUp(localKey string) error { // Calculate CRC32 of localKey as wake-up payload crc := crc32.ChecksumIEEE([]byte(localKey)) // Convert to hex string hexStr := fmt.Sprintf("%08x", crc) // Convert hex string to byte array (2 chars at a time) payload := make([]byte, len(hexStr)/2) for i := 0; i < len(hexStr); i += 2 { b, err := hex.DecodeString(hexStr[i : i+2]) if err != nil { return fmt.Errorf("failed to decode hex: %w", err) } payload[i/2] = b[0] } // Publish to wake-up topic: m/w/{deviceId} wakeUpTopic := fmt.Sprintf("m/w/%s", c.deviceId) token := c.client.Publish(wakeUpTopic, 1, false, payload) if token.Wait() && token.Error() != nil { return fmt.Errorf("failed to publish wake-up message: %w", token.Error()) } // Subscribe to lowPower topic to receive dps[149] status updates // (we don't wait for this signal - camera responds immediately) lowPowerTopic := fmt.Sprintf("smart/decrypt/in/%s", c.deviceId) if token := c.client.Subscribe(lowPowerTopic, 1, c.onLowPowerMessage); token.Wait() && token.Error() != nil { return fmt.Errorf("failed to subscribe to lowPower topic: %w", token.Error()) } return nil } func (c *TuyaMqttClient) SendOffer(sdp string, streamResolution string, streamType int, isHEVC bool) error { // Map Skill StreamType to MQTT stream_type values // streamType comes from GetStreamType() and uses Skill StreamType values: // - mainStream = 2 (HD) // - substream = 4 (SD) // // But MQTT expects mapped stream_type values: // - mainStream (2) → stream_type: 0 // - substream (4) → stream_type: 1 mqttStreamType := streamType switch streamType { case 2: mqttStreamType = 0 // mainStream (HD) case 4: mqttStreamType = 1 // substream (SD) } return c.sendMqttMessage("offer", 302, "", OfferFrame{ Mode: "webrtc", Sdp: sdp, StreamType: mqttStreamType, Auth: c.auth, DatachannelEnable: isHEVC, // must be true for HEVC Token: c.iceServers, }) } func (c *TuyaMqttClient) SendCandidate(candidate string) error { return c.sendMqttMessage("candidate", 302, "", CandidateFrame{ Mode: "webrtc", Candidate: candidate, }) } func (c *TuyaMqttClient) SendResolution(resolution int) error { // Check if camera supports clarity switching isClaritySupported := (c.webrtcVersion & (1 << 5)) != 0 if !isClaritySupported { return nil } return c.sendMqttMessage("resolution", 312, "", ResolutionFrame{ Mode: "webrtc", Value: resolution, // 0: HD, 1: SD }) } func (c *TuyaMqttClient) SendSpeaker(speaker int) error { if err := c.sendMqttMessage("speaker", 312, "", SpeakerFrame{ Mode: "webrtc", Value: speaker, // 0: off, 1: on }); err != nil { return err } // Wait for camera response if err := c.speakerWaiter.Wait(); err != nil { return fmt.Errorf("speaker wait failed: %w", err) } return nil } func (c *TuyaMqttClient) SendDisconnect() error { return c.sendMqttMessage("disconnect", 302, "", DisconnectFrame{ Mode: "webrtc", }) } func (c *TuyaMqttClient) onConnect(client mqtt.Client) { if token := client.Subscribe(c.subscribeTopic, 1, c.onMessage); token.Wait() && token.Error() != nil { c.waiter.Done(token.Error()) return } c.waiter.Done(nil) } func (c *TuyaMqttClient) onMessage(client mqtt.Client, msg mqtt.Message) { var rmqtt MqttMessage if err := json.Unmarshal(msg.Payload(), &rmqtt); err != nil { c.onError(err) return } // Filter by session ID to prevent processing messages from other sessions if rmqtt.Data.Header.SessionID != c.sessionId { return } switch rmqtt.Data.Header.Type { case "answer": c.onMqttAnswer(&rmqtt) case "candidate": c.onMqttCandidate(&rmqtt) case "disconnect": c.onMqttDisconnect() case "speaker": c.onMqttSpeaker(&rmqtt) } } func (c *TuyaMqttClient) onLowPowerMessage(client mqtt.Client, msg mqtt.Message) { var message MqttLowPowerMessage if err := json.Unmarshal(msg.Payload(), &message); err != nil { return } // Check if protocol is 4 and dps[149] is true // https://developer.tuya.com/en/docs/iot-device-dev/doorbell_solution?id=Kayamyivh15ox#title-2-Battery if message.Protocol == 4 { if val, ok := message.Data.Dps["149"]; ok { if ready, ok := val.(bool); ok && ready { // Camera is now ready after wake-up (dps[149]:true received). // However, we don't wait for this signal (like ismartlife.me doesn't either). // The camera starts responding immediately after WakeUp() is called, // so we proceed with the connection without blocking. // This waiter is kept for potential future use. c.wakeupWaiter.Done(nil) } } } } func (c *TuyaMqttClient) onMqttAnswer(msg *MqttMessage) { var answerFrame AnswerFrame if err := json.Unmarshal(msg.Data.Message, &answerFrame); err != nil { c.onError(err) return } c.onAnswer(answerFrame) } func (c *TuyaMqttClient) onMqttCandidate(msg *MqttMessage) { var candidateFrame CandidateFrame if err := json.Unmarshal(msg.Data.Message, &candidateFrame); err != nil { c.onError(err) return } // fix candidates candidateFrame.Candidate = strings.TrimPrefix(candidateFrame.Candidate, "a=") candidateFrame.Candidate = strings.TrimSuffix(candidateFrame.Candidate, "\r\n") c.onCandidate(candidateFrame) } func (c *TuyaMqttClient) onMqttDisconnect() { c.closed = true c.onDisconnect() } func (c *TuyaMqttClient) onMqttSpeaker(msg *MqttMessage) { var speakerResponse struct { ResCode int `json:"resCode"` } if err := json.Unmarshal(msg.Data.Message, &speakerResponse); err == nil { if speakerResponse.ResCode != 0 { c.speakerWaiter.Done(fmt.Errorf("speaker failed with resCode: %d", speakerResponse.ResCode)) return } } c.speakerWaiter.Done(nil) } func (c *TuyaMqttClient) onAnswer(answer AnswerFrame) { if c.handleAnswer != nil { c.handleAnswer(answer) } } func (c *TuyaMqttClient) onCandidate(candidate CandidateFrame) { if c.handleCandidate != nil { c.handleCandidate(candidate) } } func (c *TuyaMqttClient) onDisconnect() { if c.handleDisconnect != nil { c.handleDisconnect() } } func (c *TuyaMqttClient) onError(err error) { if c.handleError != nil { c.handleError(err) } } func (c *TuyaMqttClient) sendMqttMessage(messageType string, protocol int, transactionID string, data interface{}) error { if c.closed { return fmt.Errorf("mqtt client is closed, send mqtt message fail") } jsonMessage, err := json.Marshal(data) if err != nil { return err } msg := &MqttMessage{ Protocol: protocol, Pv: "2.2", T: time.Now().Unix(), Data: MqttFrame{ Header: MqttFrameHeader{ Type: messageType, From: c.uid, To: c.deviceId, SessionID: c.sessionId, MotoID: c.motoId, TransactionID: transactionID, }, Message: jsonMessage, }, } payload, err := json.Marshal(msg) if err != nil { return err } token := c.client.Publish(c.publishTopic, 1, false, payload) if token.Wait() && token.Error() != nil { return token.Error() } return nil } ================================================ FILE: pkg/tuya/smart_api.go ================================================ package tuya import ( "bytes" "encoding/json" "errors" "fmt" "io" "math/rand" "net/http" "time" "github.com/AlexxIT/go2rtc/pkg/webrtc" ) type LoginTokenRequest struct { CountryCode string `json:"countryCode"` Username string `json:"username"` IsUid bool `json:"isUid"` } type LoginTokenResponse struct { Result LoginToken `json:"result"` Success bool `json:"success"` Msg string `json:"errorMsg,omitempty"` } type LoginToken struct { Token string `json:"token"` Exponent string `json:"exponent"` PublicKey string `json:"publicKey"` PbKey string `json:"pbKey"` } type PasswordLoginRequest struct { CountryCode string `json:"countryCode"` Email string `json:"email,omitempty"` Mobile string `json:"mobile,omitempty"` Passwd string `json:"passwd"` Token string `json:"token"` IfEncrypt int `json:"ifencrypt"` Options string `json:"options"` } type PasswordLoginResponse struct { Result LoginResult `json:"result"` Success bool `json:"success"` Status string `json:"status"` ErrorMsg string `json:"errorMsg,omitempty"` } type LoginResult struct { Attribute int `json:"attribute"` ClientId string `json:"clientId"` DataVersion int `json:"dataVersion"` Domain Domain `json:"domain"` Ecode string `json:"ecode"` Email string `json:"email"` Extras Extras `json:"extras"` HeadPic string `json:"headPic"` ImproveCompanyInfo bool `json:"improveCompanyInfo"` Nickname string `json:"nickname"` PartnerIdentity string `json:"partnerIdentity"` PhoneCode string `json:"phoneCode"` Receiver string `json:"receiver"` RegFrom int `json:"regFrom"` Sid string `json:"sid"` SnsNickname string `json:"snsNickname"` TempUnit int `json:"tempUnit"` Timezone string `json:"timezone"` TimezoneId string `json:"timezoneId"` Uid string `json:"uid"` UserType int `json:"userType"` Username string `json:"username"` } type Domain struct { AispeechHttpsUrl string `json:"aispeechHttpsUrl"` AispeechQuicUrl string `json:"aispeechQuicUrl"` DeviceHttpUrl string `json:"deviceHttpUrl"` DeviceHttpsPskUrl string `json:"deviceHttpsPskUrl"` DeviceHttpsUrl string `json:"deviceHttpsUrl"` DeviceMediaMqttUrl string `json:"deviceMediaMqttUrl"` DeviceMediaMqttsUrl string `json:"deviceMediaMqttsUrl"` DeviceMqttsPskUrl string `json:"deviceMqttsPskUrl"` DeviceMqttsUrl string `json:"deviceMqttsUrl"` GwApiUrl string `json:"gwApiUrl"` GwMqttUrl string `json:"gwMqttUrl"` HttpPort int `json:"httpPort"` HttpsPort int `json:"httpsPort"` HttpsPskPort int `json:"httpsPskPort"` MobileApiUrl string `json:"mobileApiUrl"` MobileMediaMqttUrl string `json:"mobileMediaMqttUrl"` MobileMqttUrl string `json:"mobileMqttUrl"` MobileMqttsUrl string `json:"mobileMqttsUrl"` MobileQuicUrl string `json:"mobileQuicUrl"` MqttPort int `json:"mqttPort"` MqttQuicUrl string `json:"mqttQuicUrl"` MqttsPort int `json:"mqttsPort"` MqttsPskPort int `json:"mqttsPskPort"` RegionCode string `json:"regionCode"` } type Extras struct { HomeId string `json:"homeId"` SceneType string `json:"sceneType"` } type AppInfoResponse struct { Result AppInfo `json:"result"` T int64 `json:"t"` Success bool `json:"success"` Msg string `json:"errorMsg,omitempty"` } type AppInfo struct { AppId int `json:"appId"` AppName string `json:"appName"` ClientId string `json:"clientId"` Icon string `json:"icon"` } type MQTTConfigResponse struct { Result SmartApiMQTTConfig `json:"result"` Success bool `json:"success"` Msg string `json:"errorMsg,omitempty"` } type SmartApiMQTTConfig struct { Msid string `json:"msid"` Password string `json:"password"` } type HomeListResponse struct { Result []Home `json:"result"` T int64 `json:"t"` Success bool `json:"success"` Msg string `json:"errorMsg,omitempty"` } type SharedHomeListResponse struct { Result SharedHome `json:"result"` T int64 `json:"t"` Success bool `json:"success"` Msg string `json:"errorMsg,omitempty"` } type SharedHome struct { SecurityWebCShareInfoList []struct { DeviceInfoList []Device `json:"deviceInfoList"` Nickname string `json:"nickname"` Username string `json:"username"` } `json:"securityWebCShareInfoList"` } type Home struct { Admin bool `json:"admin"` Background string `json:"background"` DealStatus int `json:"dealStatus"` DisplayOrder int `json:"displayOrder"` GeoName string `json:"geoName"` Gid int `json:"gid"` GmtCreate int64 `json:"gmtCreate"` GmtModified int64 `json:"gmtModified"` GroupId int `json:"groupId"` GroupUserId int `json:"groupUserId"` Id int `json:"id"` Lat float64 `json:"lat"` Lon float64 `json:"lon"` ManagementStatus bool `json:"managementStatus"` Name string `json:"name"` OwnerId string `json:"ownerId"` Role int `json:"role"` Status bool `json:"status"` Uid string `json:"uid"` } type RoomListRequest struct { HomeId string `json:"homeId"` } type RoomListResponse struct { Result []Room `json:"result"` T int64 `json:"t"` Success bool `json:"success"` Msg string `json:"errorMsg,omitempty"` } type Room struct { DeviceCount int `json:"deviceCount"` DeviceList []Device `json:"deviceList"` RoomId string `json:"roomId"` RoomName string `json:"roomName"` } type Device struct { Category string `json:"category"` DeviceId string `json:"deviceId"` DeviceName string `json:"deviceName"` P2pType int `json:"p2pType"` ProductId string `json:"productId"` SupportCloudStorage bool `json:"supportCloudStorage"` Uuid string `json:"uuid"` } type SmartApiWebRTCConfigRequest struct { DevId string `json:"devId"` ClientTraceId string `json:"clientTraceId"` } type SmartApiWebRTCConfigResponse struct { Result SmartApiWebRTCConfig `json:"result"` Success bool `json:"success"` Msg string `json:"errorMsg,omitempty"` } type SmartApiWebRTCConfig struct { AudioAttributes AudioAttributes `json:"audioAttributes"` Auth string `json:"auth"` GatewayId string `json:"gatewayId"` Id string `json:"id"` LocalKey string `json:"localKey"` MotoId string `json:"motoId"` NodeId string `json:"nodeId"` P2PConfig P2PConfig `json:"p2pConfig"` ProtocolVersion string `json:"protocolVersion"` Skill string `json:"skill"` Sub bool `json:"sub"` SupportWebrtcRecord bool `json:"supportWebrtcRecord"` SupportsPtz bool `json:"supportsPtz"` SupportsWebrtc bool `json:"supportsWebrtc"` VedioClarity int `json:"vedioClarity"` VedioClaritys []int `json:"vedioClaritys"` VideoClarity int `json:"videoClarity"` } type TuyaSmartApiClient struct { TuyaClient email string password string countryCode string mqttsUrl string } type Region struct { Name string `json:"name"` Host string `json:"host"` Description string `json:"description"` Continent string `json:"continent"` } var AvailableRegions = []Region{ {"eu-central", "protect-eu.ismartlife.me", "Central Europe", "EU"}, {"eu-east", "protect-we.ismartlife.me", "East Europe", "EU"}, {"us-west", "protect-us.ismartlife.me", "West America", "AZ"}, {"us-east", "protect-ue.ismartlife.me", "East America", "AZ"}, {"china", "protect.ismartlife.me", "China", "AY"}, {"india", "protect-in.ismartlife.me", "India", "IN"}, } func NewTuyaSmartApiClient(httpClient *http.Client, baseUrl, email, password, deviceId string) (*TuyaSmartApiClient, error) { var region *Region for _, r := range AvailableRegions { if r.Host == baseUrl { region = &r break } } if region == nil { return nil, fmt.Errorf("invalid region: %s", baseUrl) } if httpClient == nil { httpClient = CreateHTTPClientWithSession() } mqttClient := NewTuyaMqttClient(deviceId) client := &TuyaSmartApiClient{ TuyaClient: TuyaClient{ httpClient: httpClient, mqtt: mqttClient, deviceId: deviceId, expireTime: 0, baseUrl: baseUrl, }, email: email, password: password, countryCode: region.Continent, } return client, nil } // WebRTC Flow func (c *TuyaSmartApiClient) Init() error { if err := c.initToken(); err != nil { return fmt.Errorf("failed to initialize token: %w", err) } webrtcConfig, err := c.loadWebrtcConfig() if err != nil { return fmt.Errorf("failed to load webrtc config: %w", err) } hubConfig, err := c.loadHubConfig() if err != nil { return fmt.Errorf("failed to load hub config: %w", err) } if err := c.mqtt.Start(hubConfig, webrtcConfig, c.skill.WebRTC); err != nil { return fmt.Errorf("failed to start MQTT: %w", err) } if c.skill.LowPower > 0 { _ = c.mqtt.WakeUp(c.localKey) } return nil } func (c *TuyaSmartApiClient) GetStreamUrl(streamType string) (streamUrl string, err error) { return "", errors.New("not supported") } func (c *TuyaSmartApiClient) GetAppInfo() (*AppInfoResponse, error) { url := fmt.Sprintf("https://%s/api/customized/web/app/info", c.baseUrl) body, err := c.request("POST", url, nil) if err != nil { return nil, err } var appInfoResponse AppInfoResponse if err := json.Unmarshal(body, &appInfoResponse); err != nil { return nil, err } if !appInfoResponse.Success { return nil, errors.New(appInfoResponse.Msg) } return &appInfoResponse, nil } func (c *TuyaSmartApiClient) GetHomeList() (*HomeListResponse, error) { url := fmt.Sprintf("https://%s/api/new/common/homeList", c.baseUrl) body, err := c.request("POST", url, nil) if err != nil { return nil, err } var homeListResponse HomeListResponse if err := json.Unmarshal(body, &homeListResponse); err != nil { return nil, err } if !homeListResponse.Success { return nil, errors.New(homeListResponse.Msg) } return &homeListResponse, nil } func (c *TuyaSmartApiClient) GetSharedHomeList() (*SharedHomeListResponse, error) { url := fmt.Sprintf("https://%s/api/new/playback/shareList", c.baseUrl) body, err := c.request("POST", url, nil) if err != nil { return nil, err } var sharedHomeListResponse SharedHomeListResponse if err := json.Unmarshal(body, &sharedHomeListResponse); err != nil { return nil, err } if !sharedHomeListResponse.Success { return nil, errors.New(sharedHomeListResponse.Msg) } return &sharedHomeListResponse, nil } func (c *TuyaSmartApiClient) GetRoomList(homeId string) (*RoomListResponse, error) { url := fmt.Sprintf("https://%s/api/new/common/roomList", c.baseUrl) data := RoomListRequest{ HomeId: homeId, } body, err := c.request("POST", url, data) if err != nil { return nil, err } var roomListResponse RoomListResponse if err := json.Unmarshal(body, &roomListResponse); err != nil { return nil, err } if !roomListResponse.Success { return nil, errors.New(roomListResponse.Msg) } return &roomListResponse, nil } func (c *TuyaSmartApiClient) initToken() error { tokenUrl := fmt.Sprintf("https://%s/api/login/token", c.baseUrl) tokenReq := LoginTokenRequest{ CountryCode: c.countryCode, Username: c.email, IsUid: false, } body, err := c.request("POST", tokenUrl, tokenReq) if err != nil { return err } var tokenResp LoginTokenResponse if err := json.Unmarshal(body, &tokenResp); err != nil { return err } if !tokenResp.Success { return errors.New(tokenResp.Msg) } encryptedPassword, err := EncryptPassword(c.password, tokenResp.Result.PbKey) if err != nil { return fmt.Errorf("failed to encrypt password: %v", err) } var loginUrl string loginReq := PasswordLoginRequest{ CountryCode: c.countryCode, Passwd: encryptedPassword, Token: tokenResp.Result.Token, IfEncrypt: 1, Options: `{"group":1}`, } if IsEmailAddress(c.email) { loginUrl = fmt.Sprintf("https://%s/api/private/email/login", c.baseUrl) loginReq.Email = c.email } else { loginUrl = fmt.Sprintf("https://%s/api/private/phone/login", c.baseUrl) loginReq.Mobile = c.email } body, err = c.request("POST", loginUrl, loginReq) if err != nil { return err } var loginResp *PasswordLoginResponse if err := json.Unmarshal(body, &loginResp); err != nil { return err } if !loginResp.Success { return errors.New(loginResp.ErrorMsg) } c.mqttsUrl = fmt.Sprintf("ssl://%s:%d", loginResp.Result.Domain.MobileMqttsUrl, loginResp.Result.Domain.MqttsPort) c.expireTime = time.Now().Unix() + 2*24*60*60 // 2 days in seconds return nil } func (c *TuyaSmartApiClient) loadWebrtcConfig() (*WebRTCConfig, error) { url := fmt.Sprintf("https://%s/api/jarvis/config", c.baseUrl) data := SmartApiWebRTCConfigRequest{ DevId: c.deviceId, ClientTraceId: fmt.Sprintf("%x", rand.Int63()), } body, err := c.request("POST", url, data) if err != nil { return nil, err } var webRTCConfigResponse SmartApiWebRTCConfigResponse err = json.Unmarshal(body, &webRTCConfigResponse) if err != nil { return nil, err } if !webRTCConfigResponse.Success { return nil, errors.New(webRTCConfigResponse.Msg) } err = json.Unmarshal([]byte(webRTCConfigResponse.Result.Skill), &c.skill) if err != nil { return nil, err } // Store LocalKey c.localKey = webRTCConfigResponse.Result.LocalKey iceServers, err := json.Marshal(&webRTCConfigResponse.Result.P2PConfig.Ices) if err != nil { return nil, err } c.iceServers, err = webrtc.UnmarshalICEServers(iceServers) if err != nil { return nil, err } return &WebRTCConfig{ AudioAttributes: webRTCConfigResponse.Result.AudioAttributes, Auth: webRTCConfigResponse.Result.Auth, ID: webRTCConfigResponse.Result.Id, MotoID: webRTCConfigResponse.Result.MotoId, P2PConfig: webRTCConfigResponse.Result.P2PConfig, ProtocolVersion: webRTCConfigResponse.Result.ProtocolVersion, Skill: webRTCConfigResponse.Result.Skill, SupportsWebRTCRecord: webRTCConfigResponse.Result.SupportWebrtcRecord, SupportsWebRTC: webRTCConfigResponse.Result.SupportsWebrtc, VedioClaritiy: webRTCConfigResponse.Result.VedioClarity, VideoClaritiy: webRTCConfigResponse.Result.VideoClarity, VideoClarities: webRTCConfigResponse.Result.VedioClaritys, }, nil } func (c *TuyaSmartApiClient) loadHubConfig() (config *MQTTConfig, err error) { mqttUrl := fmt.Sprintf("https://%s/api/jarvis/mqtt", c.baseUrl) mqttBody, err := c.request("POST", mqttUrl, nil) if err != nil { return nil, err } var mqttConfigResponse MQTTConfigResponse err = json.Unmarshal(mqttBody, &mqttConfigResponse) if err != nil { return nil, err } if !mqttConfigResponse.Success { return nil, errors.New(mqttConfigResponse.Msg) } return &MQTTConfig{ Url: c.mqttsUrl, ClientID: fmt.Sprintf("web_%s", mqttConfigResponse.Result.Msid), Username: fmt.Sprintf("web_%s", mqttConfigResponse.Result.Msid), Password: mqttConfigResponse.Result.Password, PublishTopic: "/av/moto/moto_id/u/{device_id}", SubscribeTopic: fmt.Sprintf("/av/u/%s", mqttConfigResponse.Result.Msid), }, nil } func (c *TuyaSmartApiClient) request(method string, url string, body any) ([]byte, error) { var bodyReader io.Reader if body != nil { jsonBody, err := json.Marshal(body) if err != nil { return nil, err } bodyReader = bytes.NewReader(jsonBody) } req, err := http.NewRequest(method, url, bodyReader) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json; charset=utf-8") req.Header.Set("Accept", "*/*") req.Header.Set("Origin", fmt.Sprintf("https://%s", c.baseUrl)) response, err := c.httpClient.Do(req) if err != nil { return nil, err } defer response.Body.Close() res, err := io.ReadAll(response.Body) if err != nil { return nil, err } if response.StatusCode != http.StatusOK { return nil, err } return res, nil } ================================================ FILE: pkg/v4l2/device/README.md ================================================ # Video For Linux Two Build on Ubuntu ```bash sudo apt install gcc-x86-64-linux-gnu sudo apt install gcc-i686-linux-gnu sudo apt install gcc-aarch64-linux-gnu binutils sudo apt install gcc-arm-linux-gnueabihf sudo apt install gcc-mipsel-linux-gnu x86_64-linux-gnu-gcc -w -static videodev2_arch.c -o videodev2_x86_64 i686-linux-gnu-gcc -w -static videodev2_arch.c -o videodev2_i686 aarch64-linux-gnu-gcc -w -static videodev2_arch.c -o videodev2_aarch64 arm-linux-gnueabihf-gcc -w -static videodev2_arch.c -o videodev2_armhf mipsel-linux-gnu-gcc -w -static videodev2_arch.c -o videodev2_mipsel -D_TIME_BITS=32 ``` ## Useful links - https://github.com/torvalds/linux/blob/master/include/uapi/linux/videodev2.h ================================================ FILE: pkg/v4l2/device/device.go ================================================ //go:build linux package device import ( "bytes" "errors" "fmt" "syscall" "unsafe" ) type Device struct { fd int bufs [][]byte pixFmt uint32 } func Open(path string) (*Device, error) { fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CLOEXEC, 0) if err != nil { return nil, err } return &Device{fd: fd}, nil } const buffersCount = 2 type Capability struct { Driver string Card string BusInfo string Version string } func (d *Device) Capability() (*Capability, error) { c := v4l2_capability{} if err := ioctl(d.fd, VIDIOC_QUERYCAP, unsafe.Pointer(&c)); err != nil { return nil, err } return &Capability{ Driver: str(c.driver[:]), Card: str(c.card[:]), BusInfo: str(c.bus_info[:]), Version: fmt.Sprintf("%d.%d.%d", byte(c.version>>16), byte(c.version>>8), byte(c.version)), }, nil } func (d *Device) ListFormats() ([]uint32, error) { var items []uint32 for i := uint32(0); ; i++ { fd := v4l2_fmtdesc{ index: i, typ: V4L2_BUF_TYPE_VIDEO_CAPTURE, } if err := ioctl(d.fd, VIDIOC_ENUM_FMT, unsafe.Pointer(&fd)); err != nil { if !errors.Is(err, syscall.EINVAL) { return nil, err } break } items = append(items, fd.pixelformat) } return items, nil } func (d *Device) ListSizes(pixFmt uint32) ([][2]uint32, error) { var items [][2]uint32 for i := uint32(0); ; i++ { fs := v4l2_frmsizeenum{ index: i, pixel_format: pixFmt, } if err := ioctl(d.fd, VIDIOC_ENUM_FRAMESIZES, unsafe.Pointer(&fs)); err != nil { if !errors.Is(err, syscall.EINVAL) { return nil, err } break } if fs.typ != V4L2_FRMSIZE_TYPE_DISCRETE { continue } items = append(items, [2]uint32{fs.discrete.width, fs.discrete.height}) } return items, nil } func (d *Device) ListFrameRates(pixFmt, width, height uint32) ([]uint32, error) { var items []uint32 for i := uint32(0); ; i++ { fi := v4l2_frmivalenum{ index: i, pixel_format: pixFmt, width: width, height: height, } if err := ioctl(d.fd, VIDIOC_ENUM_FRAMEINTERVALS, unsafe.Pointer(&fi)); err != nil { if !errors.Is(err, syscall.EINVAL) { return nil, err } break } if fi.typ != V4L2_FRMIVAL_TYPE_DISCRETE || fi.discrete.numerator != 1 { continue } items = append(items, fi.discrete.denominator) } return items, nil } func (d *Device) SetFormat(width, height, pixFmt uint32) error { d.pixFmt = pixFmt f := v4l2_format{ typ: V4L2_BUF_TYPE_VIDEO_CAPTURE, pix: v4l2_pix_format{ width: width, height: height, pixelformat: pixFmt, field: V4L2_FIELD_NONE, colorspace: V4L2_COLORSPACE_DEFAULT, }, } return ioctl(d.fd, VIDIOC_S_FMT, unsafe.Pointer(&f)) } func (d *Device) SetParam(fps uint32) error { p := v4l2_streamparm{ typ: V4L2_BUF_TYPE_VIDEO_CAPTURE, capture: v4l2_captureparm{ timeperframe: v4l2_fract{numerator: 1, denominator: fps}, }, } return ioctl(d.fd, VIDIOC_S_PARM, unsafe.Pointer(&p)) } func (d *Device) StreamOn() (err error) { rb := v4l2_requestbuffers{ count: buffersCount, typ: V4L2_BUF_TYPE_VIDEO_CAPTURE, memory: V4L2_MEMORY_MMAP, } if err = ioctl(d.fd, VIDIOC_REQBUFS, unsafe.Pointer(&rb)); err != nil { return err } d.bufs = make([][]byte, buffersCount) for i := uint32(0); i < buffersCount; i++ { qb := v4l2_buffer{ index: i, typ: V4L2_BUF_TYPE_VIDEO_CAPTURE, memory: V4L2_MEMORY_MMAP, } if err = ioctl(d.fd, VIDIOC_QUERYBUF, unsafe.Pointer(&qb)); err != nil { return err } if d.bufs[i], err = syscall.Mmap( d.fd, int64(qb.offset), int(qb.length), syscall.PROT_READ, syscall.MAP_SHARED, ); nil != err { return err } if err = ioctl(d.fd, VIDIOC_QBUF, unsafe.Pointer(&qb)); err != nil { return err } } typ := uint32(V4L2_BUF_TYPE_VIDEO_CAPTURE) return ioctl(d.fd, VIDIOC_STREAMON, unsafe.Pointer(&typ)) } func (d *Device) StreamOff() (err error) { typ := uint32(V4L2_BUF_TYPE_VIDEO_CAPTURE) if err = ioctl(d.fd, VIDIOC_STREAMOFF, unsafe.Pointer(&typ)); err != nil { return err } for i := range d.bufs { _ = syscall.Munmap(d.bufs[i]) } rb := v4l2_requestbuffers{ count: 0, typ: V4L2_BUF_TYPE_VIDEO_CAPTURE, memory: V4L2_MEMORY_MMAP, } return ioctl(d.fd, VIDIOC_REQBUFS, unsafe.Pointer(&rb)) } func (d *Device) Capture() ([]byte, error) { dec := v4l2_buffer{ typ: V4L2_BUF_TYPE_VIDEO_CAPTURE, memory: V4L2_MEMORY_MMAP, } if err := ioctl(d.fd, VIDIOC_DQBUF, unsafe.Pointer(&dec)); err != nil { return nil, err } src := d.bufs[dec.index][:dec.bytesused] dst := make([]byte, dec.bytesused) switch d.pixFmt { case V4L2_PIX_FMT_YUYV: YUYVtoYUV(dst, src) case V4L2_PIX_FMT_NV12: NV12toYUV(dst, src) default: copy(dst, d.bufs[dec.index][:dec.bytesused]) } enc := v4l2_buffer{ typ: V4L2_BUF_TYPE_VIDEO_CAPTURE, memory: V4L2_MEMORY_MMAP, index: dec.index, } if err := ioctl(d.fd, VIDIOC_QBUF, unsafe.Pointer(&enc)); err != nil { return nil, err } return dst, nil } func (d *Device) Close() error { return syscall.Close(d.fd) } func ioctl(fd int, req uint, arg unsafe.Pointer) error { _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(fd), uintptr(req), uintptr(arg)) if err != 0 { return err } return nil } func str(b []byte) string { if i := bytes.IndexByte(b, 0); i >= 0 { return string(b[:i]) } return string(b) } ================================================ FILE: pkg/v4l2/device/formats.go ================================================ package device const ( V4L2_PIX_FMT_YUYV = 'Y' | 'U'<<8 | 'Y'<<16 | 'V'<<24 V4L2_PIX_FMT_NV12 = 'N' | 'V'<<8 | '1'<<16 | '2'<<24 V4L2_PIX_FMT_MJPEG = 'M' | 'J'<<8 | 'P'<<16 | 'G'<<24 V4L2_PIX_FMT_H264 = 'H' | '2'<<8 | '6'<<16 | '4'<<24 V4L2_PIX_FMT_HEVC = 'H' | 'E'<<8 | 'V'<<16 | 'C'<<24 ) type Format struct { FourCC uint32 Name string FFmpeg string } var Formats = []Format{ {V4L2_PIX_FMT_YUYV, "YUV 4:2:2", "yuyv422"}, {V4L2_PIX_FMT_NV12, "Y/UV 4:2:0", "nv12"}, {V4L2_PIX_FMT_MJPEG, "Motion-JPEG", "mjpeg"}, {V4L2_PIX_FMT_H264, "H.264", "h264"}, {V4L2_PIX_FMT_HEVC, "HEVC", "hevc"}, } func YUYVtoYUV(dst, src []byte) { n := len(src) i0 := 0 iy := 0 iu := n / 2 iv := n / 4 * 3 for i0 < n { dst[iy] = src[i0] i0++ iy++ dst[iu] = src[i0] i0++ iu++ dst[iy] = src[i0] i0++ iy++ dst[iv] = src[i0] i0++ iv++ } } func NV12toYUV(dst, src []byte) { n := len(src) k := n / 6 i0 := k * 4 iu := i0 iv := i0 + k copy(dst, src[:i0]) // copy Y for i0 < n { dst[iu] = src[i0] i0++ iu++ dst[iv] = src[i0] i0++ iv++ } } ================================================ FILE: pkg/v4l2/device/videodev2_386.go ================================================ package device const ( VIDIOC_QUERYCAP = 0x80685600 VIDIOC_ENUM_FMT = 0xc0405602 VIDIOC_G_FMT = 0xc0cc5604 VIDIOC_S_FMT = 0xc0cc5605 VIDIOC_REQBUFS = 0xc0145608 VIDIOC_QUERYBUF = 0xc0445609 VIDIOC_QBUF = 0xc044560f VIDIOC_DQBUF = 0xc0445611 VIDIOC_STREAMON = 0x40045612 VIDIOC_STREAMOFF = 0x40045613 VIDIOC_G_PARM = 0xc0cc5615 VIDIOC_S_PARM = 0xc0cc5616 VIDIOC_ENUM_FRAMESIZES = 0xc02c564a VIDIOC_ENUM_FRAMEINTERVALS = 0xc034564b ) const ( V4L2_BUF_TYPE_VIDEO_CAPTURE = 1 V4L2_COLORSPACE_DEFAULT = 0 V4L2_FIELD_NONE = 1 V4L2_FRMIVAL_TYPE_DISCRETE = 1 V4L2_FRMSIZE_TYPE_DISCRETE = 1 V4L2_MEMORY_MMAP = 1 ) type v4l2_capability struct { // size 104 driver [16]byte // offset 0, size 16 card [32]byte // offset 16, size 32 bus_info [32]byte // offset 48, size 32 version uint32 // offset 80, size 4 capabilities uint32 // offset 84, size 4 device_caps uint32 // offset 88, size 4 reserved [3]uint32 // offset 92, size 12 } type v4l2_format struct { // size 204 typ uint32 // offset 0, size 4 _ [0]byte // align pix v4l2_pix_format // offset 4, size 48 _ [152]byte // filler } type v4l2_pix_format struct { // size 48 width uint32 // offset 0, size 4 height uint32 // offset 4, size 4 pixelformat uint32 // offset 8, size 4 field uint32 // offset 12, size 4 bytesperline uint32 // offset 16, size 4 sizeimage uint32 // offset 20, size 4 colorspace uint32 // offset 24, size 4 priv uint32 // offset 28, size 4 flags uint32 // offset 32, size 4 ycbcr_enc uint32 // offset 36, size 4 quantization uint32 // offset 40, size 4 xfer_func uint32 // offset 44, size 4 } type v4l2_streamparm struct { // size 204 typ uint32 // offset 0, size 4 capture v4l2_captureparm // offset 4, size 40 _ [160]byte // filler } type v4l2_captureparm struct { // size 40 capability uint32 // offset 0, size 4 capturemode uint32 // offset 4, size 4 timeperframe v4l2_fract // offset 8, size 8 extendedmode uint32 // offset 16, size 4 readbuffers uint32 // offset 20, size 4 reserved [4]uint32 // offset 24, size 16 } type v4l2_fract struct { // size 8 numerator uint32 // offset 0, size 4 denominator uint32 // offset 4, size 4 } type v4l2_requestbuffers struct { // size 20 count uint32 // offset 0, size 4 typ uint32 // offset 4, size 4 memory uint32 // offset 8, size 4 capabilities uint32 // offset 12, size 4 flags uint8 // offset 16, size 1 reserved [3]uint8 // offset 17, size 3 } type v4l2_buffer struct { // size 68 index uint32 // offset 0, size 4 typ uint32 // offset 4, size 4 bytesused uint32 // offset 8, size 4 flags uint32 // offset 12, size 4 field uint32 // offset 16, size 4 _ [8]byte // align timecode v4l2_timecode // offset 28, size 16 sequence uint32 // offset 44, size 4 memory uint32 // offset 48, size 4 offset uint32 // offset 52, size 4 _ [0]byte // align length uint32 // offset 56, size 4 _ [8]byte // filler } type v4l2_timecode struct { // size 16 typ uint32 // offset 0, size 4 flags uint32 // offset 4, size 4 frames uint8 // offset 8, size 1 seconds uint8 // offset 9, size 1 minutes uint8 // offset 10, size 1 hours uint8 // offset 11, size 1 userbits [4]uint8 // offset 12, size 4 } type v4l2_fmtdesc struct { // size 64 index uint32 // offset 0, size 4 typ uint32 // offset 4, size 4 flags uint32 // offset 8, size 4 description [32]byte // offset 12, size 32 pixelformat uint32 // offset 44, size 4 mbus_code uint32 // offset 48, size 4 reserved [3]uint32 // offset 52, size 12 } type v4l2_frmsizeenum struct { // size 44 index uint32 // offset 0, size 4 pixel_format uint32 // offset 4, size 4 typ uint32 // offset 8, size 4 discrete v4l2_frmsize_discrete // offset 12, size 8 _ [24]byte // filler } type v4l2_frmsize_discrete struct { // size 8 width uint32 // offset 0, size 4 height uint32 // offset 4, size 4 } type v4l2_frmivalenum struct { // size 52 index uint32 // offset 0, size 4 pixel_format uint32 // offset 4, size 4 width uint32 // offset 8, size 4 height uint32 // offset 12, size 4 typ uint32 // offset 16, size 4 discrete v4l2_fract // offset 20, size 8 _ [24]byte // filler } ================================================ FILE: pkg/v4l2/device/videodev2_arch.c ================================================ //go:build ignore #include #include #include #define printconst1(con) printf("\t%s = 0x%08lx\n", #con, con) #define printconst2(con) printf("\t%s = %d\n", #con, con) #define printstruct(str) printf("type %s struct { // size %lu\n", #str, sizeof(struct str)) #define printmember(str, mem, typ) printf("\t%s %s // offset %lu, size %lu\n", #mem == "type" ? "typ" : #mem, typ, offsetof(struct str, mem), sizeof((struct str){0}.mem)) #define printunimem(str, uni, mem, typ) printf("\t%s %s // offset %lu, size %lu\n", #mem, typ, offsetof(struct str, uni.mem), sizeof((struct str){0}.uni.mem)) #define printalign1(str, mem2, mem1) printf("\t_ [%lu]byte // align\n", offsetof(struct str, mem2) - offsetof(struct str, mem1) - sizeof((struct str){0}.mem1)) #define printfiller(str, mem) printf("\t_ [%lu]byte // filler\n", sizeof(struct str) - offsetof(struct str, mem) - sizeof((struct str){0}.mem)) int main() { printf("const (\n"); printconst1(VIDIOC_QUERYCAP); printconst1(VIDIOC_ENUM_FMT); printconst1(VIDIOC_G_FMT); printconst1(VIDIOC_S_FMT); printconst1(VIDIOC_REQBUFS); printconst1(VIDIOC_QUERYBUF); printf("\n"); printconst1(VIDIOC_QBUF); printconst1(VIDIOC_DQBUF); printconst1(VIDIOC_STREAMON); printconst1(VIDIOC_STREAMOFF); printconst1(VIDIOC_G_PARM); printconst1(VIDIOC_S_PARM); printf("\n"); printconst1(VIDIOC_ENUM_FRAMESIZES); printconst1(VIDIOC_ENUM_FRAMEINTERVALS); printf(")\n\n"); printf("const (\n"); printconst2(V4L2_BUF_TYPE_VIDEO_CAPTURE); printconst2(V4L2_COLORSPACE_DEFAULT); printconst2(V4L2_FIELD_NONE); printconst2(V4L2_FRMIVAL_TYPE_DISCRETE); printconst2(V4L2_FRMSIZE_TYPE_DISCRETE); printconst2(V4L2_MEMORY_MMAP); printf(")\n\n"); printstruct(v4l2_capability); printmember(v4l2_capability, driver, "[16]byte"); printmember(v4l2_capability, card, "[32]byte"); printmember(v4l2_capability, bus_info, "[32]byte"); printmember(v4l2_capability, version, "uint32"); printmember(v4l2_capability, capabilities, "uint32"); printmember(v4l2_capability, device_caps, "uint32"); printmember(v4l2_capability, reserved, "[3]uint32"); printf("}\n\n"); printstruct(v4l2_format); printmember(v4l2_format, type, "uint32"); printalign1(v4l2_format, fmt, type); printunimem(v4l2_format, fmt, pix, "v4l2_pix_format"); printfiller(v4l2_format, fmt.pix); printf("}\n\n"); printstruct(v4l2_pix_format); printmember(v4l2_pix_format, width, "uint32"); printmember(v4l2_pix_format, height, "uint32"); printmember(v4l2_pix_format, pixelformat, "uint32"); printmember(v4l2_pix_format, field, "uint32"); printmember(v4l2_pix_format, bytesperline, "uint32"); printmember(v4l2_pix_format, sizeimage, "uint32"); printmember(v4l2_pix_format, colorspace, "uint32"); printmember(v4l2_pix_format, priv, "uint32"); printmember(v4l2_pix_format, flags, "uint32"); printmember(v4l2_pix_format, ycbcr_enc, "uint32"); printmember(v4l2_pix_format, quantization, "uint32"); printmember(v4l2_pix_format, xfer_func, "uint32"); printf("}\n\n"); printstruct(v4l2_streamparm); printmember(v4l2_streamparm, type, "uint32"); printunimem(v4l2_streamparm, parm, capture, "v4l2_captureparm"); printfiller(v4l2_streamparm, parm.capture); printf("}\n\n"); printstruct(v4l2_captureparm); printmember(v4l2_captureparm, capability, "uint32"); printmember(v4l2_captureparm, capturemode, "uint32"); printmember(v4l2_captureparm, timeperframe, "v4l2_fract"); printmember(v4l2_captureparm, extendedmode, "uint32"); printmember(v4l2_captureparm, readbuffers, "uint32"); printmember(v4l2_captureparm, reserved, "[4]uint32"); printf("}\n\n"); printstruct(v4l2_fract); printmember(v4l2_fract, numerator, "uint32"); printmember(v4l2_fract, denominator, "uint32"); printf("}\n\n"); printstruct(v4l2_requestbuffers); printmember(v4l2_requestbuffers, count, "uint32"); printmember(v4l2_requestbuffers, type, "uint32"); printmember(v4l2_requestbuffers, memory, "uint32"); printmember(v4l2_requestbuffers, capabilities, "uint32"); printmember(v4l2_requestbuffers, flags, "uint8"); printmember(v4l2_requestbuffers, reserved, "[3]uint8"); printf("}\n\n"); printstruct(v4l2_buffer); printmember(v4l2_buffer, index, "uint32"); printmember(v4l2_buffer, type, "uint32"); printmember(v4l2_buffer, bytesused, "uint32"); printmember(v4l2_buffer, flags, "uint32"); printmember(v4l2_buffer, field, "uint32"); printalign1(v4l2_buffer, timecode, field); printmember(v4l2_buffer, timecode, "v4l2_timecode"); printmember(v4l2_buffer, sequence, "uint32"); printmember(v4l2_buffer, memory, "uint32"); printunimem(v4l2_buffer, m, offset, "uint32"); printalign1(v4l2_buffer, length, m.offset); printmember(v4l2_buffer, length, "uint32"); printfiller(v4l2_buffer, length); printf("}\n\n"); printstruct(v4l2_timecode); printmember(v4l2_timecode, type, "uint32"); printmember(v4l2_timecode, flags, "uint32"); printmember(v4l2_timecode, frames, "uint8"); printmember(v4l2_timecode, seconds, "uint8"); printmember(v4l2_timecode, minutes, "uint8"); printmember(v4l2_timecode, hours, "uint8"); printmember(v4l2_timecode, userbits, "[4]uint8"); printf("}\n\n"); printstruct(v4l2_fmtdesc); printmember(v4l2_fmtdesc, index, "uint32"); printmember(v4l2_fmtdesc, type, "uint32"); printmember(v4l2_fmtdesc, flags, "uint32"); printmember(v4l2_fmtdesc, description, "[32]byte"); printmember(v4l2_fmtdesc, pixelformat, "uint32"); printmember(v4l2_fmtdesc, mbus_code, "uint32"); printmember(v4l2_fmtdesc, reserved, "[3]uint32"); printf("}\n\n"); printstruct(v4l2_frmsizeenum); printmember(v4l2_frmsizeenum, index, "uint32"); printmember(v4l2_frmsizeenum, pixel_format, "uint32"); printmember(v4l2_frmsizeenum, type, "uint32"); printmember(v4l2_frmsizeenum, discrete, "v4l2_frmsize_discrete"); printfiller(v4l2_frmsizeenum, discrete); printf("}\n\n"); printstruct(v4l2_frmsize_discrete); printmember(v4l2_frmsize_discrete, width, "uint32"); printmember(v4l2_frmsize_discrete, height, "uint32"); printf("}\n\n"); printstruct(v4l2_frmivalenum); printmember(v4l2_frmivalenum, index, "uint32"); printmember(v4l2_frmivalenum, pixel_format, "uint32"); printmember(v4l2_frmivalenum, width, "uint32"); printmember(v4l2_frmivalenum, height, "uint32"); printmember(v4l2_frmivalenum, type, "uint32"); printmember(v4l2_frmivalenum, discrete, "v4l2_fract"); printfiller(v4l2_frmivalenum, discrete); printf("}\n\n"); return 0; } ================================================ FILE: pkg/v4l2/device/videodev2_arm.go ================================================ package device const ( VIDIOC_QUERYCAP = 0x80685600 VIDIOC_ENUM_FMT = 0xc0405602 VIDIOC_G_FMT = 0xc0cc5604 VIDIOC_S_FMT = 0xc0cc5605 VIDIOC_REQBUFS = 0xc0145608 VIDIOC_QUERYBUF = 0xc0505609 VIDIOC_QBUF = 0xc050560f VIDIOC_DQBUF = 0xc0505611 VIDIOC_STREAMON = 0x40045612 VIDIOC_STREAMOFF = 0x40045613 VIDIOC_G_PARM = 0xc0cc5615 VIDIOC_S_PARM = 0xc0cc5616 VIDIOC_ENUM_FRAMESIZES = 0xc02c564a VIDIOC_ENUM_FRAMEINTERVALS = 0xc034564b ) const ( V4L2_BUF_TYPE_VIDEO_CAPTURE = 1 V4L2_COLORSPACE_DEFAULT = 0 V4L2_FIELD_NONE = 1 V4L2_FRMIVAL_TYPE_DISCRETE = 1 V4L2_FRMSIZE_TYPE_DISCRETE = 1 V4L2_MEMORY_MMAP = 1 ) type v4l2_capability struct { // size 104 driver [16]byte // offset 0, size 16 card [32]byte // offset 16, size 32 bus_info [32]byte // offset 48, size 32 version uint32 // offset 80, size 4 capabilities uint32 // offset 84, size 4 device_caps uint32 // offset 88, size 4 reserved [3]uint32 // offset 92, size 12 } type v4l2_format struct { // size 204 typ uint32 // offset 0, size 4 _ [0]byte // align pix v4l2_pix_format // offset 4, size 48 _ [152]byte // filler } type v4l2_pix_format struct { // size 48 width uint32 // offset 0, size 4 height uint32 // offset 4, size 4 pixelformat uint32 // offset 8, size 4 field uint32 // offset 12, size 4 bytesperline uint32 // offset 16, size 4 sizeimage uint32 // offset 20, size 4 colorspace uint32 // offset 24, size 4 priv uint32 // offset 28, size 4 flags uint32 // offset 32, size 4 ycbcr_enc uint32 // offset 36, size 4 quantization uint32 // offset 40, size 4 xfer_func uint32 // offset 44, size 4 } type v4l2_streamparm struct { // size 204 typ uint32 // offset 0, size 4 capture v4l2_captureparm // offset 4, size 40 _ [160]byte // filler } type v4l2_captureparm struct { // size 40 capability uint32 // offset 0, size 4 capturemode uint32 // offset 4, size 4 timeperframe v4l2_fract // offset 8, size 8 extendedmode uint32 // offset 16, size 4 readbuffers uint32 // offset 20, size 4 reserved [4]uint32 // offset 24, size 16 } type v4l2_fract struct { // size 8 numerator uint32 // offset 0, size 4 denominator uint32 // offset 4, size 4 } type v4l2_requestbuffers struct { // size 20 count uint32 // offset 0, size 4 typ uint32 // offset 4, size 4 memory uint32 // offset 8, size 4 capabilities uint32 // offset 12, size 4 flags uint8 // offset 16, size 1 reserved [3]uint8 // offset 17, size 3 } type v4l2_buffer struct { // size 80 index uint32 // offset 0, size 4 typ uint32 // offset 4, size 4 bytesused uint32 // offset 8, size 4 flags uint32 // offset 12, size 4 field uint32 // offset 16, size 4 _ [20]byte // align timecode v4l2_timecode // offset 40, size 16 sequence uint32 // offset 56, size 4 memory uint32 // offset 60, size 4 offset uint32 // offset 64, size 4 _ [0]byte // align length uint32 // offset 68, size 4 _ [8]byte // filler } type v4l2_timecode struct { // size 16 typ uint32 // offset 0, size 4 flags uint32 // offset 4, size 4 frames uint8 // offset 8, size 1 seconds uint8 // offset 9, size 1 minutes uint8 // offset 10, size 1 hours uint8 // offset 11, size 1 userbits [4]uint8 // offset 12, size 4 } type v4l2_fmtdesc struct { // size 64 index uint32 // offset 0, size 4 typ uint32 // offset 4, size 4 flags uint32 // offset 8, size 4 description [32]byte // offset 12, size 32 pixelformat uint32 // offset 44, size 4 mbus_code uint32 // offset 48, size 4 reserved [3]uint32 // offset 52, size 12 } type v4l2_frmsizeenum struct { // size 44 index uint32 // offset 0, size 4 pixel_format uint32 // offset 4, size 4 typ uint32 // offset 8, size 4 discrete v4l2_frmsize_discrete // offset 12, size 8 _ [24]byte // filler } type v4l2_frmsize_discrete struct { // size 8 width uint32 // offset 0, size 4 height uint32 // offset 4, size 4 } type v4l2_frmivalenum struct { // size 52 index uint32 // offset 0, size 4 pixel_format uint32 // offset 4, size 4 width uint32 // offset 8, size 4 height uint32 // offset 12, size 4 typ uint32 // offset 16, size 4 discrete v4l2_fract // offset 20, size 8 _ [24]byte // filler } ================================================ FILE: pkg/v4l2/device/videodev2_mipsle.go ================================================ package device const ( VIDIOC_QUERYCAP = 0x40685600 VIDIOC_ENUM_FMT = 0xc0405602 VIDIOC_G_FMT = 0xc0cc5604 VIDIOC_S_FMT = 0xc0cc5605 VIDIOC_REQBUFS = 0xc0145608 VIDIOC_QUERYBUF = 0xc0445609 VIDIOC_QBUF = 0xc044560f VIDIOC_DQBUF = 0xc0445611 VIDIOC_STREAMON = 0x80045612 VIDIOC_STREAMOFF = 0x80045613 VIDIOC_G_PARM = 0xc0cc5615 VIDIOC_S_PARM = 0xc0cc5616 VIDIOC_ENUM_FRAMESIZES = 0xc02c564a VIDIOC_ENUM_FRAMEINTERVALS = 0xc034564b ) const ( V4L2_BUF_TYPE_VIDEO_CAPTURE = 1 V4L2_COLORSPACE_DEFAULT = 0 V4L2_FIELD_NONE = 1 V4L2_FRMIVAL_TYPE_DISCRETE = 1 V4L2_FRMSIZE_TYPE_DISCRETE = 1 V4L2_MEMORY_MMAP = 1 ) type v4l2_capability struct { // size 104 driver [16]byte // offset 0, size 16 card [32]byte // offset 16, size 32 bus_info [32]byte // offset 48, size 32 version uint32 // offset 80, size 4 capabilities uint32 // offset 84, size 4 device_caps uint32 // offset 88, size 4 reserved [3]uint32 // offset 92, size 12 } type v4l2_format struct { // size 204 typ uint32 // offset 0, size 4 _ [0]byte // align pix v4l2_pix_format // offset 4, size 48 _ [152]byte // filler } type v4l2_pix_format struct { // size 48 width uint32 // offset 0, size 4 height uint32 // offset 4, size 4 pixelformat uint32 // offset 8, size 4 field uint32 // offset 12, size 4 bytesperline uint32 // offset 16, size 4 sizeimage uint32 // offset 20, size 4 colorspace uint32 // offset 24, size 4 priv uint32 // offset 28, size 4 flags uint32 // offset 32, size 4 ycbcr_enc uint32 // offset 36, size 4 quantization uint32 // offset 40, size 4 xfer_func uint32 // offset 44, size 4 } type v4l2_streamparm struct { // size 204 typ uint32 // offset 0, size 4 capture v4l2_captureparm // offset 4, size 40 _ [160]byte // filler } type v4l2_captureparm struct { // size 40 capability uint32 // offset 0, size 4 capturemode uint32 // offset 4, size 4 timeperframe v4l2_fract // offset 8, size 8 extendedmode uint32 // offset 16, size 4 readbuffers uint32 // offset 20, size 4 reserved [4]uint32 // offset 24, size 16 } type v4l2_fract struct { // size 8 numerator uint32 // offset 0, size 4 denominator uint32 // offset 4, size 4 } type v4l2_requestbuffers struct { // size 20 count uint32 // offset 0, size 4 typ uint32 // offset 4, size 4 memory uint32 // offset 8, size 4 capabilities uint32 // offset 12, size 4 flags uint8 // offset 16, size 1 reserved [3]uint8 // offset 17, size 3 } type v4l2_buffer struct { // size 68 index uint32 // offset 0, size 4 typ uint32 // offset 4, size 4 bytesused uint32 // offset 8, size 4 flags uint32 // offset 12, size 4 field uint32 // offset 16, size 4 _ [8]byte // align timecode v4l2_timecode // offset 28, size 16 sequence uint32 // offset 44, size 4 memory uint32 // offset 48, size 4 offset uint32 // offset 52, size 4 _ [0]byte // align length uint32 // offset 56, size 4 _ [8]byte // filler } type v4l2_timecode struct { // size 16 typ uint32 // offset 0, size 4 flags uint32 // offset 4, size 4 frames uint8 // offset 8, size 1 seconds uint8 // offset 9, size 1 minutes uint8 // offset 10, size 1 hours uint8 // offset 11, size 1 userbits [4]uint8 // offset 12, size 4 } type v4l2_fmtdesc struct { // size 64 index uint32 // offset 0, size 4 typ uint32 // offset 4, size 4 flags uint32 // offset 8, size 4 description [32]byte // offset 12, size 32 pixelformat uint32 // offset 44, size 4 mbus_code uint32 // offset 48, size 4 reserved [3]uint32 // offset 52, size 12 } type v4l2_frmsizeenum struct { // size 44 index uint32 // offset 0, size 4 pixel_format uint32 // offset 4, size 4 typ uint32 // offset 8, size 4 discrete v4l2_frmsize_discrete // offset 12, size 8 _ [24]byte // filler } type v4l2_frmsize_discrete struct { // size 8 width uint32 // offset 0, size 4 height uint32 // offset 4, size 4 } type v4l2_frmivalenum struct { // size 52 index uint32 // offset 0, size 4 pixel_format uint32 // offset 4, size 4 width uint32 // offset 8, size 4 height uint32 // offset 12, size 4 typ uint32 // offset 16, size 4 discrete v4l2_fract // offset 20, size 8 _ [24]byte // filler } ================================================ FILE: pkg/v4l2/device/videodev2_x64.go ================================================ //go:build amd64 || arm64 package device const ( VIDIOC_QUERYCAP = 0x80685600 VIDIOC_ENUM_FMT = 0xc0405602 VIDIOC_G_FMT = 0xc0d05604 VIDIOC_S_FMT = 0xc0d05605 VIDIOC_REQBUFS = 0xc0145608 VIDIOC_QUERYBUF = 0xc0585609 VIDIOC_QBUF = 0xc058560f VIDIOC_DQBUF = 0xc0585611 VIDIOC_STREAMON = 0x40045612 VIDIOC_STREAMOFF = 0x40045613 VIDIOC_G_PARM = 0xc0cc5615 VIDIOC_S_PARM = 0xc0cc5616 VIDIOC_ENUM_FRAMESIZES = 0xc02c564a VIDIOC_ENUM_FRAMEINTERVALS = 0xc034564b ) const ( V4L2_BUF_TYPE_VIDEO_CAPTURE = 1 V4L2_COLORSPACE_DEFAULT = 0 V4L2_FIELD_NONE = 1 V4L2_FRMIVAL_TYPE_DISCRETE = 1 V4L2_FRMSIZE_TYPE_DISCRETE = 1 V4L2_MEMORY_MMAP = 1 ) type v4l2_capability struct { // size 104 driver [16]byte // offset 0, size 16 card [32]byte // offset 16, size 32 bus_info [32]byte // offset 48, size 32 version uint32 // offset 80, size 4 capabilities uint32 // offset 84, size 4 device_caps uint32 // offset 88, size 4 reserved [3]uint32 // offset 92, size 12 } type v4l2_format struct { // size 208 typ uint32 // offset 0, size 4 _ [4]byte // align pix v4l2_pix_format // offset 8, size 48 _ [152]byte // filler } type v4l2_pix_format struct { // size 48 width uint32 // offset 0, size 4 height uint32 // offset 4, size 4 pixelformat uint32 // offset 8, size 4 field uint32 // offset 12, size 4 bytesperline uint32 // offset 16, size 4 sizeimage uint32 // offset 20, size 4 colorspace uint32 // offset 24, size 4 priv uint32 // offset 28, size 4 flags uint32 // offset 32, size 4 ycbcr_enc uint32 // offset 36, size 4 quantization uint32 // offset 40, size 4 xfer_func uint32 // offset 44, size 4 } type v4l2_streamparm struct { // size 204 typ uint32 // offset 0, size 4 capture v4l2_captureparm // offset 4, size 40 _ [160]byte // filler } type v4l2_captureparm struct { // size 40 capability uint32 // offset 0, size 4 capturemode uint32 // offset 4, size 4 timeperframe v4l2_fract // offset 8, size 8 extendedmode uint32 // offset 16, size 4 readbuffers uint32 // offset 20, size 4 reserved [4]uint32 // offset 24, size 16 } type v4l2_fract struct { // size 8 numerator uint32 // offset 0, size 4 denominator uint32 // offset 4, size 4 } type v4l2_requestbuffers struct { // size 20 count uint32 // offset 0, size 4 typ uint32 // offset 4, size 4 memory uint32 // offset 8, size 4 capabilities uint32 // offset 12, size 4 flags uint8 // offset 16, size 1 reserved [3]uint8 // offset 17, size 3 } type v4l2_buffer struct { // size 88 index uint32 // offset 0, size 4 typ uint32 // offset 4, size 4 bytesused uint32 // offset 8, size 4 flags uint32 // offset 12, size 4 field uint32 // offset 16, size 4 _ [20]byte // align timecode v4l2_timecode // offset 40, size 16 sequence uint32 // offset 56, size 4 memory uint32 // offset 60, size 4 offset uint32 // offset 64, size 4 _ [4]byte // align length uint32 // offset 72, size 4 _ [12]byte // filler } type v4l2_timecode struct { // size 16 typ uint32 // offset 0, size 4 flags uint32 // offset 4, size 4 frames uint8 // offset 8, size 1 seconds uint8 // offset 9, size 1 minutes uint8 // offset 10, size 1 hours uint8 // offset 11, size 1 userbits [4]uint8 // offset 12, size 4 } type v4l2_fmtdesc struct { // size 64 index uint32 // offset 0, size 4 typ uint32 // offset 4, size 4 flags uint32 // offset 8, size 4 description [32]byte // offset 12, size 32 pixelformat uint32 // offset 44, size 4 mbus_code uint32 // offset 48, size 4 reserved [3]uint32 // offset 52, size 12 } type v4l2_frmsizeenum struct { // size 44 index uint32 // offset 0, size 4 pixel_format uint32 // offset 4, size 4 typ uint32 // offset 8, size 4 discrete v4l2_frmsize_discrete // offset 12, size 8 _ [24]byte // filler } type v4l2_frmsize_discrete struct { // size 8 width uint32 // offset 0, size 4 height uint32 // offset 4, size 4 } type v4l2_frmivalenum struct { // size 52 index uint32 // offset 0, size 4 pixel_format uint32 // offset 4, size 4 width uint32 // offset 8, size 4 height uint32 // offset 12, size 4 typ uint32 // offset 16, size 4 discrete v4l2_fract // offset 20, size 8 _ [24]byte // filler } ================================================ FILE: pkg/v4l2/producer.go ================================================ //go:build linux package v4l2 import ( "errors" "net/url" "strings" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/AlexxIT/go2rtc/pkg/v4l2/device" "github.com/pion/rtp" ) type Producer struct { core.Connection dev *device.Device } func Open(rawURL string) (*Producer, error) { // Example (ffmpeg source compatible): // v4l2:device?video=/dev/video0&input_format=mjpeg&video_size=1280x720 u, err := url.Parse(rawURL) if err != nil { return nil, err } query := u.Query() dev, err := device.Open(query.Get("video")) if err != nil { return nil, err } codec := &core.Codec{ ClockRate: 90000, PayloadType: core.PayloadTypeRAW, } var width, height, pixFmt uint32 if wh := strings.Split(query.Get("video_size"), "x"); len(wh) == 2 { codec.FmtpLine = "width=" + wh[0] + ";height=" + wh[1] width = uint32(core.Atoi(wh[0])) height = uint32(core.Atoi(wh[1])) } switch query.Get("input_format") { case "yuyv422": if codec.FmtpLine == "" { return nil, errors.New("v4l2: invalid video_size") } codec.Name = core.CodecRAW codec.FmtpLine += ";colorspace=422" pixFmt = device.V4L2_PIX_FMT_YUYV case "nv12": if codec.FmtpLine == "" { return nil, errors.New("v4l2: invalid video_size") } codec.Name = core.CodecRAW codec.FmtpLine += ";colorspace=420mpeg2" // maybe 420jpeg pixFmt = device.V4L2_PIX_FMT_NV12 case "mjpeg": codec.Name = core.CodecJPEG pixFmt = device.V4L2_PIX_FMT_MJPEG case "h264": codec.Name = core.CodecH264 pixFmt = device.V4L2_PIX_FMT_H264 case "hevc": codec.Name = core.CodecH265 pixFmt = device.V4L2_PIX_FMT_HEVC default: return nil, errors.New("v4l2: invalid input_format") } if err = dev.SetFormat(width, height, pixFmt); err != nil { return nil, err } if fps := core.Atoi(query.Get("framerate")); fps > 0 { if err = dev.SetParam(uint32(fps)); err != nil { return nil, err } } medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, }, } return &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "v4l2", Medias: medias, }, dev: dev, }, nil } func (c *Producer) Start() error { if err := c.dev.StreamOn(); err != nil { return err } var bitstream bool switch c.Medias[0].Codecs[0].Name { case core.CodecH264, core.CodecH265: bitstream = true } for { buf, err := c.dev.Capture() if err != nil { return err } c.Recv += len(buf) if len(c.Receivers) == 0 { continue } if bitstream { buf = annexb.EncodeToAVCC(buf) } pkt := &rtp.Packet{ Header: rtp.Header{Timestamp: core.Now90000()}, Payload: buf, } c.Receivers[0].WriteRTP(pkt) } } func (c *Producer) Stop() error { _ = c.Connection.Stop() return errors.Join(c.dev.StreamOff(), c.dev.Close()) } ================================================ FILE: pkg/wav/backchannel.go ================================================ package wav import ( "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/shell" "github.com/pion/rtp" ) type Backchannel struct { core.Connection cmd *shell.Command } func NewBackchannel(cmd *shell.Command) (core.Producer, error) { medias := []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ //{Name: core.CodecPCML}, {Name: core.CodecPCMA}, {Name: core.CodecPCMU}, }, }, } return &Backchannel{ Connection: core.Connection{ ID: core.NewID(), FormatName: "wav", Protocol: "pipe", Medias: medias, Transport: cmd, }, cmd: cmd, }, nil } func (c *Backchannel) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return nil, core.ErrCantGetTrack } func (c *Backchannel) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { wr, err := c.cmd.StdinPipe() if err != nil { return err } b := Header(track.Codec) if _, err = wr.Write(b); err != nil { return err } sender := core.NewSender(media, track.Codec) sender.Handler = func(packet *rtp.Packet) { if n, err := wr.Write(packet.Payload); err != nil { c.Send += n } } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } func (c *Backchannel) Start() error { return c.cmd.Run() } ================================================ FILE: pkg/wav/producer.go ================================================ package wav import ( "bufio" "errors" "io" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) const FourCC = "RIFF" func Open(r io.Reader) (*Producer, error) { // https://en.wikipedia.org/wiki/WAV // https://www.mmsp.ece.mcgill.ca/Documents/AudioFormats/WAVE/WAVE.html rd := bufio.NewReaderSize(r, core.BufferSize) codec, err := ReadHeader(r) if err != nil { return nil, err } if codec.Name == "" { return nil, errors.New("waw: unsupported codec") } medias := []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{codec}, }, } return &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "wav", Medias: medias, Transport: r, }, rd: rd, }, nil } type Producer struct { core.Connection rd *bufio.Reader } func (c *Producer) Start() error { var seq uint16 var ts uint32 const PacketSize = 0.040 * 8000 // 40ms for { payload := make([]byte, PacketSize) if _, err := io.ReadFull(c.rd, payload); err != nil { return err } c.Recv += PacketSize if len(c.Receivers) == 0 { continue } pkt := &rtp.Packet{ Header: rtp.Header{ Version: 2, Marker: true, SequenceNumber: seq, Timestamp: ts, }, Payload: payload, } c.Receivers[0].WriteRTP(pkt) seq++ ts += PacketSize } } ================================================ FILE: pkg/wav/wav.go ================================================ package wav import ( "encoding/binary" "io" "github.com/AlexxIT/go2rtc/pkg/core" ) func Header(codec *core.Codec) []byte { var fmt, size, extra byte switch codec.Name { case core.CodecPCML: fmt = 1 size = 2 case core.CodecPCMA: fmt = 6 size = 1 extra = 2 case core.CodecPCMU: fmt = 7 size = 1 extra = 2 default: return nil } channels := byte(codec.Channels) if channels == 0 { channels = 1 } b := make([]byte, 0, 46) // cap with extra b = append(b, "RIFF\xFF\xFF\xFF\xFFWAVEfmt "...) b = append(b, 0x10+extra, 0, 0, 0) b = append(b, fmt, 0) b = append(b, channels, 0) b = binary.LittleEndian.AppendUint32(b, codec.ClockRate) b = binary.LittleEndian.AppendUint32(b, uint32(size*channels)*codec.ClockRate) b = append(b, size*channels, 0) b = append(b, size*8, 0) if extra > 0 { b = append(b, 0, 0) // ExtraParamSize (if PCM, then doesn't exist) } b = append(b, "data\xFF\xFF\xFF\xFF"...) return b } func ReadHeader(r io.Reader) (*core.Codec, error) { // skip Master RIFF chunk if _, err := io.ReadFull(r, make([]byte, 12)); err != nil { return nil, err } var codec core.Codec for { chunkID, data, err := readChunk(r) if err != nil { return nil, err } if chunkID == "data" { break } if chunkID == "fmt " { // https://audiocoding.cc/articles/2008-05-22-wav-file-structure/wav_formats.txt switch data[0] { case 1: codec.Name = core.CodecPCML case 6: codec.Name = core.CodecPCMA case 7: codec.Name = core.CodecPCMU } codec.Channels = data[2] codec.ClockRate = binary.LittleEndian.Uint32(data[4:]) } } return &codec, nil } func readChunk(r io.Reader) (chunkID string, data []byte, err error) { b := make([]byte, 8) if _, err = io.ReadFull(r, b); err != nil { return } if chunkID = string(b[:4]); chunkID != "data" { size := binary.LittleEndian.Uint32(b[4:]) data = make([]byte, size) _, err = io.ReadFull(r, data) } return } ================================================ FILE: pkg/webrtc/README.md ================================================ ## StateChange 1. offer = pc.CreateOffer() 2. pc.SetLocalDescription(offer) 3. OnICEGatheringStateChange: gathering 4. OnSignalingStateChange: have-local-offer *. OnICEGatheringStateChange: complete 5. pc.SetRemoteDescription(answer) 6. OnSignalingStateChange: stable 7. OnICEConnectionStateChange: checking 8. OnICEConnectionStateChange: connected ================================================ FILE: pkg/webrtc/api.go ================================================ package webrtc import ( "fmt" "net" "slices" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/xnet" "github.com/pion/ice/v4" "github.com/pion/interceptor" "github.com/pion/webrtc/v4" ) // ReceiveMTU = Ethernet MTU (1500) - IP Header (20) - UDP Header (8) // https://ffmpeg.org/ffmpeg-all.html#Muxer const ReceiveMTU = 1472 func NewAPI() (*webrtc.API, error) { return NewServerAPI("", "", nil) } type Filters struct { Candidates []string `yaml:"candidates"` Loopback bool `yaml:"loopback"` Interfaces []string `yaml:"interfaces"` IPs []string `yaml:"ips"` Networks []string `yaml:"networks"` UDPPorts []uint16 `yaml:"udp_ports"` } func (f *Filters) Network(protocol string) string { if f == nil || f.Networks == nil { return protocol } v4 := slices.Contains(f.Networks, protocol+"4") v6 := slices.Contains(f.Networks, protocol+"6") if v4 && v6 { return protocol } else if v4 { return protocol + "4" } else if v6 { return protocol + "6" } return "" } func (f *Filters) NetIPs() (ips []net.IP) { itfs, _ := net.Interfaces() for _, itf := range itfs { if itf.Flags&net.FlagUp == 0 { continue } if !f.IncludeLoopback() && itf.Flags&net.FlagLoopback != 0 { continue } if !f.InterfaceFilter(itf.Name) { continue } addrs, _ := itf.Addrs() for _, addr := range addrs { ip := parseNetAddr(addr) if ip == nil || !f.IPFilter(ip) { continue } ips = append(ips, ip) } } return } func parseNetAddr(addr net.Addr) net.IP { switch addr := addr.(type) { case *net.IPNet: return addr.IP case *net.IPAddr: return addr.IP } return nil } func (f *Filters) IncludeLoopback() bool { return f != nil && f.Loopback } func (f *Filters) InterfaceFilter(name string) bool { return f == nil || f.Interfaces == nil || slices.Contains(f.Interfaces, name) } func (f *Filters) IPFilter(ip net.IP) bool { return f == nil || f.IPs == nil || core.Contains(f.IPs, ip.String()) } func NewServerAPI(network, address string, filters *Filters) (*webrtc.API, error) { // for debug logs add to env: `PION_LOG_DEBUG=all` m := &webrtc.MediaEngine{} //if err := m.RegisterDefaultCodecs(); err != nil { // return nil, err //} if err := RegisterDefaultCodecs(m); err != nil { return nil, err } i := &interceptor.Registry{} if err := webrtc.RegisterDefaultInterceptors(m, i); err != nil { return nil, err } s := webrtc.SettingEngine{} // fix https://github.com/pion/webrtc/pull/2407 s.SetDTLSInsecureSkipHelloVerify(true) if filters != nil && filters.Loopback { s.SetIncludeLoopbackCandidate(true) } var interfaceFilter func(name string) bool if filters != nil && filters.Interfaces != nil { interfaceFilter = func(name string) bool { return core.Contains(filters.Interfaces, name) } } else { // default interfaces - all, except loopback } s.SetInterfaceFilter(interfaceFilter) var ipFilter func(ip net.IP) bool if filters != nil && filters.IPs != nil { ipFilter = func(ip net.IP) bool { return core.Contains(filters.IPs, ip.String()) } } else { // try filter all Docker-like interfaces ipFilter = func(ip net.IP) bool { return !xnet.Docker.Contains(ip) } // if there are no such interfaces - disable the filter // the user will need to enable port forwarding if nets, _ := xnet.IPNets(ipFilter); len(nets) == 0 { ipFilter = nil } } s.SetIPFilter(ipFilter) var networkTypes []webrtc.NetworkType if filters != nil && filters.Networks != nil { for _, s := range filters.Networks { if networkType, err := webrtc.NewNetworkType(s); err == nil { networkTypes = append(networkTypes, networkType) } } } else { // default network types - all networkTypes = []webrtc.NetworkType{ webrtc.NetworkTypeUDP4, webrtc.NetworkTypeUDP6, webrtc.NetworkTypeTCP4, webrtc.NetworkTypeTCP6, } } s.SetNetworkTypes(networkTypes) if filters != nil && len(filters.UDPPorts) == 2 { _ = s.SetEphemeralUDPPortRange(filters.UDPPorts[0], filters.UDPPorts[1]) } // If you don't specify an address, this won't cause an error. // Connections can still be established using random UDP addresses. if address != "" { // Both newMux functions respect filters and do not raise an error // if the port cannot be listened on. if network == "" || network == "tcp" { tcpMux := newTCPMux(address, filters) s.SetICETCPMux(tcpMux) } if network == "" || network == "udp" { udpMux := newUDPMux(address, filters) s.SetICEUDPMux(udpMux) } } return webrtc.NewAPI( webrtc.WithMediaEngine(m), webrtc.WithInterceptorRegistry(i), webrtc.WithSettingEngine(s), ), nil } // OnNewListener temporary ugly solution for log var OnNewListener = func(ln any) {} func newTCPMux(address string, filters *Filters) ice.TCPMux { networkTCP := filters.Network("tcp") // tcp or tcp4 or tcp6 if ln, _ := net.Listen(networkTCP, address); ln != nil { OnNewListener(ln) return webrtc.NewICETCPMux(nil, ln, 8) } return nil } func newUDPMux(address string, filters *Filters) ice.UDPMux { host, port, err := net.SplitHostPort(address) if err != nil { return nil } // UDPMux should not listening on unspecified address. // So we will create a listener on all available interfaces. // We can't use ice.NewMultiUDPMuxFromPort, because it sometimes crashes with an error: // listen udp [***]:8555: bind: cannot assign requested address var addrs []string if host == "" { for _, ip := range filters.NetIPs() { addrs = append(addrs, fmt.Sprintf("%s:%s", ip, port)) } } else { addrs = []string{address} } networkUDP := filters.Network("udp") // udp or udp4 or udp6 var muxes []ice.UDPMux for _, addr := range addrs { if ln, _ := net.ListenPacket(networkUDP, addr); ln != nil { OnNewListener(ln) mux := ice.NewUDPMuxDefault(ice.UDPMuxParams{UDPConn: ln}) muxes = append(muxes, mux) } } switch len(muxes) { case 0: return nil case 1: return muxes[0] } return ice.NewMultiUDPMuxDefault(muxes...) } func RegisterDefaultCodecs(m *webrtc.MediaEngine) error { for _, codec := range []webrtc.RTPCodecParameters{ { RTPCodecCapability: webrtc.RTPCodecCapability{ MimeType: webrtc.MimeTypeOpus, ClockRate: 48000, Channels: 2, SDPFmtpLine: "minptime=10;useinbandfec=1", }, PayloadType: 101, //111, }, { RTPCodecCapability: webrtc.RTPCodecCapability{ MimeType: webrtc.MimeTypePCMU, ClockRate: 8000, }, PayloadType: 0, }, { RTPCodecCapability: webrtc.RTPCodecCapability{ MimeType: webrtc.MimeTypePCMA, ClockRate: 8000, }, PayloadType: 8, }, } { if err := m.RegisterCodec(codec, webrtc.RTPCodecTypeAudio); err != nil { return err } } videoRTCPFeedback := []webrtc.RTCPFeedback{ {"goog-remb", ""}, {"ccm", "fir"}, {"nack", ""}, {"nack", "pli"}, } for _, codec := range []webrtc.RTPCodecParameters{ { RTPCodecCapability: webrtc.RTPCodecCapability{ MimeType: webrtc.MimeTypeH264, ClockRate: 90000, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42001f", RTCPFeedback: videoRTCPFeedback, }, PayloadType: 96, // Chrome v110 - PayloadType: 102 }, { RTPCodecCapability: webrtc.RTPCodecCapability{ MimeType: webrtc.MimeTypeH264, ClockRate: 90000, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=42e01f", RTCPFeedback: videoRTCPFeedback, }, PayloadType: 97, // Chrome v110 - PayloadType: 106 }, { RTPCodecCapability: webrtc.RTPCodecCapability{ MimeType: webrtc.MimeTypeH264, ClockRate: 90000, SDPFmtpLine: "level-asymmetry-allowed=1;packetization-mode=1;profile-level-id=640032", RTCPFeedback: videoRTCPFeedback, }, PayloadType: 98, // Chrome v110 - PayloadType: 112 }, // macOS Safari 15.1 { RTPCodecCapability: webrtc.RTPCodecCapability{ MimeType: webrtc.MimeTypeH265, ClockRate: 90000, RTCPFeedback: videoRTCPFeedback, }, PayloadType: 100, }, } { if err := m.RegisterCodec(codec, webrtc.RTPCodecTypeVideo); err != nil { return err } } return nil } ================================================ FILE: pkg/webrtc/client.go ================================================ package webrtc import ( "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/sdp/v3" "github.com/pion/webrtc/v4" ) func (c *Conn) CreateOffer(medias []*core.Media) (string, error) { // 1. Create transeivers with proper kind and direction for _, media := range medias { var err error switch media.Direction { case core.DirectionRecvonly: _, err = c.pc.AddTransceiverFromKind( webrtc.NewRTPCodecType(media.Kind), webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionRecvonly}, ) case core.DirectionSendonly: _, err = c.pc.AddTransceiverFromTrack( NewTrack(media.Kind), webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly}, ) case core.DirectionSendRecv: // default transceiver is sendrecv _, err = c.pc.AddTransceiverFromTrack(NewTrack(media.Kind)) default: // Nest cameras require data channel _, err = c.pc.CreateDataChannel(media.Kind, nil) } if err != nil { return "", err } } // 2. Create local offer desc, err := c.pc.CreateOffer(nil) if err != nil { return "", err } // 3. Start gathering phase if err = c.pc.SetLocalDescription(desc); err != nil { return "", err } return c.pc.LocalDescription().SDP, nil } func (c *Conn) CreateCompleteOffer(medias []*core.Media) (string, error) { if _, err := c.CreateOffer(medias); err != nil { return "", err } <-webrtc.GatheringCompletePromise(c.pc) return c.pc.LocalDescription().SDP, nil } func (c *Conn) SetAnswer(answer string) (err error) { desc := webrtc.SessionDescription{ Type: webrtc.SDPTypeAnswer, SDP: fakeFormatsInAnswer(c.pc.LocalDescription().SDP, answer), } if err = c.pc.SetRemoteDescription(desc); err != nil { return err } sd := &sdp.SessionDescription{} if err = sd.Unmarshal([]byte(answer)); err != nil { return err } c.Medias = UnmarshalMedias(sd.MediaDescriptions) return nil } // fakeFormatsInAnswer - fix pion bug with remote SDP parsing: // pion will process formats only from first media of each kind // so we add all formats from first offer media to the first answer media func fakeFormatsInAnswer(offer, answer string) string { sd2 := &sdp.SessionDescription{} if err := sd2.Unmarshal([]byte(answer)); err != nil { return answer } // check if answer has recvonly audio var ok bool for _, md2 := range sd2.MediaDescriptions { if md2.MediaName.Media == "audio" { if _, ok = md2.Attribute("recvonly"); ok { break } } } if !ok { return answer } sd1 := &sdp.SessionDescription{} if err := sd1.Unmarshal([]byte(offer)); err != nil { return answer } var formats []string var attrs []sdp.Attribute for _, md1 := range sd1.MediaDescriptions { if md1.MediaName.Media == "audio" { for _, attr := range md1.Attributes { switch attr.Key { case "rtpmap", "fmtp", "rtcp-fb", "extmap": attrs = append(attrs, attr) } } formats = md1.MediaName.Formats break } } for _, md2 := range sd2.MediaDescriptions { if md2.MediaName.Media == "audio" { for _, attr := range md2.Attributes { switch attr.Key { case "rtpmap", "fmtp", "rtcp-fb", "extmap": default: attrs = append(attrs, attr) } } md2.MediaName.Formats = formats md2.Attributes = attrs break } } b, err := sd2.Marshal() if err != nil { return answer } return string(b) } ================================================ FILE: pkg/webrtc/client_test.go ================================================ package webrtc import ( "testing" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/webrtc/v4" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) func TestClient(t *testing.T) { api, err := NewAPI() require.Nil(t, err) pc, err := api.NewPeerConnection(webrtc.Configuration{}) require.Nil(t, err) prod := NewConn(pc) medias := []*core.Media{ {Kind: core.KindVideo, Direction: core.DirectionRecvonly}, {Kind: core.KindAudio, Direction: core.DirectionRecvonly}, {Kind: core.KindAudio, Direction: core.DirectionSendonly}, } offer, err := prod.CreateOffer(medias) require.Nil(t, err) assert.NotEmpty(t, offer) require.Len(t, prod.pc.GetReceivers(), 2) require.Len(t, prod.pc.GetSenders(), 1) answer := `v=0 o=- 1934370540648269799 1678277622 IN IP4 0.0.0.0 s=- t=0 0 a=fingerprint:sha-256 77:8C:9A:62:51:81:69:EA:4E:BE:93:6B:4E:DF:51:D2:2F:E3:DF:E7:F4:8A:18:1A:C0:74:FA:AE:B8:98:29:9B a=extmap-allow-mixed a=group:BUNDLE 0 1 2 m=video 9 UDP/TLS/RTP/SAVPF 97 c=IN IP4 0.0.0.0 a=setup:active a=mid:0 a=ice-ufrag:xxx a=ice-pwd:xxx a=rtcp-mux a=rtcp-rsize a=rtpmap:97 H264/90000 a=fmtp:97 packetization-mode=1;profile-level-id=42e01f a=extmap:1 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01 a=ssrc:2815449682 cname:go2rtc a=ssrc:2815449682 msid:go2rtc video a=ssrc:2815449682 mslabel:go2rtc a=ssrc:2815449682 label:video a=msid:go2rtc video a=sendonly m=audio 9 UDP/TLS/RTP/SAVPF 8 c=IN IP4 0.0.0.0 a=setup:active a=mid:1 a=ice-ufrag:xxx a=ice-pwd:xxx a=rtcp-mux a=rtcp-rsize a=rtpmap:8 PCMA/8000 a=extmap:1 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01 a=ssrc:1392166302 cname:go2rtc a=ssrc:1392166302 msid:go2rtc audio a=ssrc:1392166302 mslabel:go2rtc a=ssrc:1392166302 label:audio a=msid:go2rtc audio a=sendonly m=audio 9 UDP/TLS/RTP/SAVPF 0 c=IN IP4 0.0.0.0 a=setup:active a=mid:2 a=ice-ufrag:xxx a=ice-pwd:xxx a=rtcp-mux a=rtcp-rsize a=rtpmap:0 PCMU/8000 a=extmap:1 http://www.ietf.org/id/draft-holmer-rmcat-transport-wide-cc-extensions-01 a=recvonly ` err = prod.SetAnswer(answer) require.Nil(t, err) sender := prod.pc.GetSenders()[0] caps := webrtc.RTPCodecCapability{ MimeType: webrtc.MimeTypePCMU, ClockRate: 8000, Channels: 0, } track := sender.Track() track, err = webrtc.NewTrackLocalStaticRTP(caps, track.ID(), track.StreamID()) require.Nil(t, err) err = sender.ReplaceTrack(track) require.Nil(t, err) } func TestUnmarshalICEServers(t *testing.T) { s := `[{"credential":"xxx","urls":"xxx","username":"xxx"},{"credential":null,"urls":"xxx","username":null}]` servers, err := UnmarshalICEServers([]byte(s)) require.Nil(t, err) require.Len(t, servers, 2) require.Equal(t, []string{"xxx"}, servers[0].URLs) s = `[{"urls":"xxx"},{"urls":["yyy","zzz"]}]` servers, err = UnmarshalICEServers([]byte(s)) require.Nil(t, err) require.Len(t, servers, 2) require.Equal(t, []string{"xxx"}, servers[0].URLs) require.Equal(t, []string{"yyy", "zzz"}, servers[1].URLs) } ================================================ FILE: pkg/webrtc/conn.go ================================================ package webrtc import ( "encoding/json" "fmt" "strings" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtcp" "github.com/pion/rtp" "github.com/pion/webrtc/v4" ) type Conn struct { core.Connection core.Listener Mode core.Mode `json:"mode"` pc *webrtc.PeerConnection offer string closed core.Waiter } func NewConn(pc *webrtc.PeerConnection) *Conn { c := &Conn{ Connection: core.Connection{ ID: core.NewID(), FormatName: "webrtc", Transport: pc, }, pc: pc, } pc.OnICECandidate(func(candidate *webrtc.ICECandidate) { // last candidate will be empty if candidate != nil { c.Fire(candidate) } }) pc.OnDataChannel(func(channel *webrtc.DataChannel) { c.Fire(channel) }) pc.OnICEConnectionStateChange(func(state webrtc.ICEConnectionState) { if state != webrtc.ICEConnectionStateChecking { return } pc.SCTP().Transport().ICETransport().OnSelectedCandidatePairChange( func(pair *webrtc.ICECandidatePair) { // fix situation when candidate pair changes multiple times if i := strings.IndexByte(c.Protocol, '+'); i > 0 { c.Protocol = c.Protocol[:i] } c.Protocol += "+" + pair.Remote.Protocol.String() c.RemoteAddr = fmt.Sprintf( "%s:%d %s", sanitizeIP6(pair.Remote.Address), pair.Remote.Port, pair.Remote.Typ, ) if pair.Remote.RelatedAddress != "" { c.RemoteAddr += fmt.Sprintf( " %s:%d", sanitizeIP6(pair.Remote.RelatedAddress), pair.Remote.RelatedPort, ) } }, ) }) pc.OnTrack(func(remote *webrtc.TrackRemote, receiver *webrtc.RTPReceiver) { media, codec := c.getMediaCodec(remote) if media == nil { return } track, err := c.GetTrack(media, codec) if err != nil { return } switch c.Mode { case core.ModePassiveProducer, core.ModeActiveProducer: // replace the theoretical list of codecs with the actual list of codecs if len(media.Codecs) > 1 { media.Codecs = []*core.Codec{codec} } } if c.Mode == core.ModePassiveProducer && remote.Kind() == webrtc.RTPCodecTypeVideo { go func() { pkts := []rtcp.Packet{&rtcp.PictureLossIndication{MediaSSRC: uint32(remote.SSRC())}} for range time.NewTicker(time.Second * 2).C { if err := pc.WriteRTCP(pkts); err != nil { return } } }() } for { b := make([]byte, ReceiveMTU) n, _, err := remote.Read(b) if err != nil { return } c.Recv += n packet := &rtp.Packet{} if err := packet.Unmarshal(b[:n]); err != nil { return } if len(packet.Payload) == 0 { continue } track.WriteRTP(packet) } }) // OK connection: // 15:01:46 ICE connection state changed: checking // 15:01:46 peer connection state changed: connected // 15:01:54 peer connection state changed: disconnected // 15:02:20 peer connection state changed: failed // // Fail connection: // 14:53:08 ICE connection state changed: checking // 14:53:39 peer connection state changed: failed pc.OnConnectionStateChange(func(state webrtc.PeerConnectionState) { c.Fire(state) switch state { case webrtc.PeerConnectionStateConnected: for _, sender := range c.Senders { sender.Start() } case webrtc.PeerConnectionStateDisconnected, webrtc.PeerConnectionStateFailed, webrtc.PeerConnectionStateClosed: // disconnect event comes earlier, than failed // but it comes only for success connections _ = c.Close() } }) return c } func (c *Conn) MarshalJSON() ([]byte, error) { return json.Marshal(c.Connection) } func (c *Conn) Close() error { c.closed.Done(nil) return c.pc.Close() } func (c *Conn) AddCandidate(candidate string) error { // pion uses only candidate value from json/object candidate struct return c.pc.AddICECandidate(webrtc.ICECandidateInit{Candidate: candidate}) } func (c *Conn) GetSenderTrack(mid string) *Track { if tr := c.getTranseiver(mid); tr != nil { if s := tr.Sender(); s != nil { if t := s.Track().(*Track); t != nil { return t } } } return nil } func (c *Conn) getTranseiver(mid string) *webrtc.RTPTransceiver { for _, tr := range c.pc.GetTransceivers() { if tr.Mid() == mid { return tr } } return nil } func (c *Conn) getMediaCodec(remote *webrtc.TrackRemote) (*core.Media, *core.Codec) { for _, tr := range c.pc.GetTransceivers() { // search Transeiver for this TrackRemote if tr.Receiver() == nil || tr.Receiver().Track() != remote { continue } // search Media for this MID for _, media := range c.Medias { if media.ID != tr.Mid() || media.Direction != core.DirectionRecvonly { continue } // search codec for this PayloadType for _, codec := range media.Codecs { if codec.PayloadType != uint8(remote.PayloadType()) { continue } return media, codec } } } // fix moment when core.ModePassiveProducer or core.ModeActiveProducer // sends new codec with new payload type to same media // check GetTrack panic(core.Caller()) return nil, nil } func sanitizeIP6(host string) string { if strings.IndexByte(host, ':') > 0 { return "[" + host + "]" } return host } ================================================ FILE: pkg/webrtc/consumer.go ================================================ package webrtc import ( "errors" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/AlexxIT/go2rtc/pkg/pcm" "github.com/pion/rtp" ) func (c *Conn) GetMedias() []*core.Media { return WithResampling(c.Medias) } func (c *Conn) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { core.Assert(media.Direction == core.DirectionSendonly) for _, sender := range c.Senders { if sender.Codec == codec { sender.Bind(track) return nil } } switch c.Mode { case core.ModePassiveConsumer: // video/audio for browser case core.ModeActiveProducer: // go2rtc as WebRTC client (backchannel) case core.ModePassiveProducer: // WebRTC/WHIP default: panic(core.Caller()) } localTrack := c.GetSenderTrack(media.ID) if localTrack == nil { return errors.New("webrtc: can't get track") } payloadType := codec.PayloadType sender := core.NewSender(media, codec) sender.Handler = func(packet *rtp.Packet) { c.Send += packet.MarshalSize() //important to send with remote PayloadType _ = localTrack.WriteRTP(payloadType, packet) } switch track.Codec.Name { case core.CodecH264: sender.Handler = h264.RTPPay(1200, sender.Handler) if track.Codec.IsRTP() { sender.Handler = h264.RTPDepay(track.Codec, sender.Handler) } else { sender.Handler = h264.RepairAVCC(track.Codec, sender.Handler) } case core.CodecH265: sender.Handler = h265.RTPPay(1200, sender.Handler) if track.Codec.IsRTP() { sender.Handler = h265.RTPDepay(track.Codec, sender.Handler) } else { sender.Handler = h265.RepairAVCC(track.Codec, sender.Handler) } case core.CodecPCMA, core.CodecPCMU, core.CodecPCM, core.CodecPCML: // Fix audio quality https://github.com/AlexxIT/WebRTC/issues/500 // should be before ResampleToG711, because it will be called last sender.Handler = pcm.RepackG711(false, sender.Handler) if codec.ClockRate == 0 { if codec.Name == core.CodecPCM || codec.Name == core.CodecPCML { codec.Name = core.CodecPCMA } codec.ClockRate = 8000 sender.Handler = pcm.TranscodeHandler(codec, track.Codec, sender.Handler) } } // TODO: rewrite this dirty logic // maybe not best solution, but ActiveProducer connected before AddTrack if c.Mode != core.ModeActiveProducer { sender.Bind(track) } else { sender.HandleRTP(track) } c.Senders = append(c.Senders, sender) return nil } ================================================ FILE: pkg/webrtc/helpers.go ================================================ package webrtc import ( "encoding/json" "errors" "fmt" "hash/crc32" "net" "strconv" "strings" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/ice/v4" "github.com/pion/sdp/v3" "github.com/pion/stun/v3" "github.com/pion/webrtc/v4" ) func UnmarshalMedias(descriptions []*sdp.MediaDescription) (medias []*core.Media) { // 1. Sort medias, so video will always be before audio // 2. Ignore application media from Hass default lovelace card // 3. Ignore media without direction (inactive media) // 4. Inverse media direction (because it is remote peer medias list) for _, kind := range []string{core.KindVideo, core.KindAudio} { for _, md := range descriptions { if md.MediaName.Media != kind { continue } media := core.UnmarshalMedia(md) switch media.Direction { case core.DirectionSendRecv: media.Direction = core.DirectionRecvonly medias = append(medias, media) media = media.Clone() media.Direction = core.DirectionSendonly case core.DirectionRecvonly: media.Direction = core.DirectionSendonly case core.DirectionSendonly: media.Direction = core.DirectionRecvonly case "": continue } // skip non-media codecs to avoid confusing users in info and logs media.Codecs = SkipNonMediaCodecs(media.Codecs) medias = append(medias, media) } } return } func SkipNonMediaCodecs(input []*core.Codec) (output []*core.Codec) { for _, codec := range input { switch codec.Name { case "RTX", "RED", "ULPFEC", "FLEXFEC-03": continue case "CN", "TELEPHONE-EVENT": continue // https://datatracker.ietf.org/doc/html/rfc7874 } // VP8, VP9, H264, H265, AV1 // OPUS, G722, PCMU, PCMA output = append(output, codec) } return } // WithResampling - will add for consumer: PCMA/0, PCMU/0, PCM/0, PCML/0 // so it can add resampling for PCMA/PCMU and repack for PCM/PCML func WithResampling(medias []*core.Media) []*core.Media { for _, media := range medias { if media.Kind != core.KindAudio || media.Direction != core.DirectionSendonly { continue } var pcma, pcmu, pcm, pcml *core.Codec for _, codec := range media.Codecs { switch codec.Name { case core.CodecPCMA: if codec.ClockRate != 0 { pcma = codec } else { pcma = nil } case core.CodecPCMU: if codec.ClockRate != 0 { pcmu = codec } else { pcmu = nil } case core.CodecPCM: pcm = codec case core.CodecPCML: pcml = codec } } if pcma != nil { pcma = pcma.Clone() pcma.ClockRate = 0 // reset clock rate so will match any media.Codecs = append(media.Codecs, pcma) } if pcmu != nil { pcmu = pcmu.Clone() pcmu.ClockRate = 0 media.Codecs = append(media.Codecs, pcmu) } if pcma != nil && pcm == nil { pcm = pcma.Clone() pcm.Name = core.CodecPCM media.Codecs = append(media.Codecs, pcm) } if pcma != nil && pcml == nil { pcml = pcma.Clone() pcml.Name = core.CodecPCML media.Codecs = append(media.Codecs, pcml) } } return medias } func NewCandidate(network, address string) (string, error) { i := strings.LastIndexByte(address, ':') if i < 0 { return "", errors.New("wrong candidate: " + address) } host, port := address[:i], address[i+1:] i, err := strconv.Atoi(port) if err != nil { return "", err } config := &ice.CandidateHostConfig{ Network: network, Address: host, Port: i, Component: ice.ComponentRTP, } if network == "tcp" { config.TCPType = ice.TCPTypePassive } cand, err := ice.NewCandidateHost(config) if err != nil { return "", err } return "candidate:" + cand.Marshal(), nil } func LookupIP(address string) (string, error) { if strings.HasPrefix(address, "stun:") { ip, err := GetCachedPublicIP() if err != nil { return "", err } return ip.String() + address[4:], nil } if IsIP(address) { return address, nil } i := strings.IndexByte(address, ':') ips, err := net.LookupIP(address[:i]) if err != nil { return "", err } if len(ips) == 0 { return "", fmt.Errorf("can't resolve: %s", address) } return ips[0].String() + address[i:], nil } // GetPublicIP example from https://github.com/pion/stun func GetPublicIP(address string) (net.IP, error) { conn, err := net.Dial("udp", address) if err != nil { return nil, err } c, err := stun.NewClient(conn) if err != nil { return nil, err } if err = conn.SetDeadline(time.Now().Add(time.Second * 3)); err != nil { return nil, err } var res stun.Event message := stun.MustBuild(stun.TransactionID, stun.BindingRequest) if err = c.Do(message, func(e stun.Event) { res = e }); err != nil { return nil, err } if err = c.Close(); err != nil { return nil, err } if res.Error != nil { return nil, res.Error } var xorAddr stun.XORMappedAddress if err = xorAddr.GetFrom(res.Message); err != nil { return nil, err } return xorAddr.IP, nil } var cachedIP net.IP var cachedTS time.Time func GetCachedPublicIP(stuns ...string) (net.IP, error) { if now := time.Now(); now.After(cachedTS) { for _, addr := range stuns { if ip, _ := GetPublicIP(addr); ip != nil { cachedIP = ip cachedTS = now.Add(time.Minute * 5) return ip, nil } } } if cachedIP == nil { return nil, errors.New("webrtc: can't get public IP") } return cachedIP, nil } func IsIP(host string) bool { for _, i := range host { if i >= 'A' { return false } } return true } func MimeType(codec *core.Codec) string { switch codec.Name { case core.CodecH264: return webrtc.MimeTypeH264 case core.CodecH265: return webrtc.MimeTypeH265 case core.CodecVP8: return webrtc.MimeTypeVP8 case core.CodecVP9: return webrtc.MimeTypeVP9 case core.CodecAV1: return webrtc.MimeTypeAV1 case core.CodecPCMU: return webrtc.MimeTypePCMU case core.CodecPCMA: return webrtc.MimeTypePCMA case core.CodecOpus: return webrtc.MimeTypeOpus case core.CodecG722: return webrtc.MimeTypeG722 } panic("not implemented") } func CandidateICE(network, host, port string, priority uint32) string { // 1. Foundation // 2. Component, always 1 because RTP // 3. "udp" or "tcp" // 4. Priority // 5. Host - IP4 or IP6 or domain name // 6. Port // 7. "typ host" foundation := crc32.ChecksumIEEE([]byte("host" + host + network + "4")) s := fmt.Sprintf("candidate:%d 1 %s %d %s %s typ host", foundation, network, priority, host, port) if network == "tcp" { return s + " tcptype passive" } return s } // Priority = type << 24 + local << 8 + component // https://www.rfc-editor.org/rfc/rfc8445#section-5.1.2.1 const PriorityHostUDP uint32 = 0x001F_FFFF | 126<<24 | // udp host 7<<21 // udp const PriorityHostTCPPassive uint32 = 0x001F_FFFF | 99<<24 | // tcp host 4<<21 // tcp passive // CandidateHostPriority (lower indexes has a higher priority) func CandidateHostPriority(network string, index int) uint32 { switch network { case "udp": return PriorityHostUDP - uint32(index) case "tcp": return PriorityHostTCPPassive - uint32(index) } return 0 } func UnmarshalICEServers(b []byte) ([]webrtc.ICEServer, error) { type ICEServer struct { URLs any `json:"urls"` Username string `json:"username,omitempty"` Credential string `json:"credential,omitempty"` } var src []ICEServer if err := json.Unmarshal(b, &src); err != nil { return nil, err } var dst []webrtc.ICEServer for i := range src { srv := webrtc.ICEServer{ Username: src[i].Username, Credential: src[i].Credential, } switch v := src[i].URLs.(type) { case []any: for _, u := range v { if s, ok := u.(string); ok { srv.URLs = append(srv.URLs, s) } } case string: srv.URLs = []string{v} } dst = append(dst, srv) } return dst, nil } ================================================ FILE: pkg/webrtc/producer.go ================================================ package webrtc import ( "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/webrtc/v4" ) func (c *Conn) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { core.Assert(media.Direction == core.DirectionRecvonly) for _, track := range c.Receivers { if track.Codec == codec { return track, nil } } switch c.Mode { case core.ModePassiveConsumer: // backchannel from browser // set codec for consumer recv track so remote peer should send media with this codec params := webrtc.RTPCodecParameters{ RTPCodecCapability: webrtc.RTPCodecCapability{ MimeType: MimeType(codec), ClockRate: codec.ClockRate, Channels: uint16(codec.Channels), }, PayloadType: 0, // don't know if this necessary } tr := c.getTranseiver(media.ID) _ = tr.SetCodecPreferences([]webrtc.RTPCodecParameters{params}) case core.ModePassiveProducer, core.ModeActiveProducer: // Passive producers: OBS Studio via WHIP or Browser // Active producers: go2rtc as WebRTC client or WebTorrent default: panic(core.Caller()) } track := core.NewReceiver(media, codec) c.Receivers = append(c.Receivers, track) return track, nil } func (c *Conn) Start() error { c.closed.Wait() return nil } ================================================ FILE: pkg/webrtc/server.go ================================================ package webrtc import ( "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/sdp/v3" "github.com/pion/webrtc/v4" ) func (c *Conn) SetOffer(offer string) (err error) { c.offer = offer sd := &sdp.SessionDescription{} if err = sd.Unmarshal([]byte(offer)); err != nil { return } // create transceivers with opposite direction for _, md := range sd.MediaDescriptions { var mid string var tr *webrtc.RTPTransceiver for _, attr := range md.Attributes { switch attr.Key { case core.DirectionSendRecv: tr, _ = c.pc.AddTransceiverFromTrack(NewTrack(md.MediaName.Media)) case core.DirectionSendonly: tr, _ = c.pc.AddTransceiverFromKind( webrtc.NewRTPCodecType(md.MediaName.Media), webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionRecvonly}, ) case core.DirectionRecvonly: tr, _ = c.pc.AddTransceiverFromTrack( NewTrack(md.MediaName.Media), webrtc.RTPTransceiverInit{Direction: webrtc.RTPTransceiverDirectionSendonly}, ) case "mid": mid = attr.Value } } if mid != "" && tr != nil { _ = tr.SetMid(mid) } } c.Medias = UnmarshalMedias(sd.MediaDescriptions) return } func (c *Conn) GetAnswer() (answer string, err error) { // we need to process remote offer after we create transeivers desc := webrtc.SessionDescription{Type: webrtc.SDPTypeOffer, SDP: c.offer} if err = c.pc.SetRemoteDescription(desc); err != nil { return "", err } // disable transceivers if we don't have track, make direction=inactive transeivers: for _, tr := range c.pc.GetTransceivers() { for _, sender := range c.Senders { if sender.Media.ID == tr.Mid() { continue transeivers } } switch tr.Direction() { case webrtc.RTPTransceiverDirectionSendrecv: _ = tr.Sender().Stop() // don't know if necessary _ = tr.SetSender(tr.Sender(), nil) // set direction to recvonly case webrtc.RTPTransceiverDirectionSendonly: _ = tr.Stop() } } if desc, err = c.pc.CreateAnswer(nil); err != nil { return } if err = c.pc.SetLocalDescription(desc); err != nil { return } return c.pc.LocalDescription().SDP, nil } // GetCompleteAnswer - get SDP answer with candidates inside func (c *Conn) GetCompleteAnswer(candidates []string, filter func(*webrtc.ICECandidate) bool) (string, error) { var done = make(chan struct{}) c.pc.OnICECandidate(func(candidate *webrtc.ICECandidate) { if candidate != nil { if filter == nil || filter(candidate) { candidates = append(candidates, candidate.ToJSON().Candidate) } } else { done <- struct{}{} } }) answer, err := c.GetAnswer() if err != nil { return "", err } <-done sd := &sdp.SessionDescription{} if err = sd.Unmarshal([]byte(answer)); err != nil { return "", err } md := sd.MediaDescriptions[0] for _, candidate := range candidates { md.WithPropertyAttribute(candidate) } b, err := sd.Marshal() if err != nil { return "", err } return string(b), nil } ================================================ FILE: pkg/webrtc/track.go ================================================ package webrtc import ( "sync" "github.com/pion/rtp" "github.com/pion/webrtc/v4" ) type Track struct { kind string id string streamID string sequence uint16 ssrc uint32 writer webrtc.TrackLocalWriter mu sync.Mutex } func NewTrack(kind string) *Track { return &Track{ kind: kind, id: "go2rtc-" + kind, streamID: "go2rtc", } } func (t *Track) Bind(context webrtc.TrackLocalContext) (webrtc.RTPCodecParameters, error) { t.mu.Lock() t.ssrc = uint32(context.SSRC()) t.writer = context.WriteStream() t.mu.Unlock() for _, parameters := range context.CodecParameters() { // return first parameters return parameters, nil } return webrtc.RTPCodecParameters{}, nil } func (t *Track) Unbind(context webrtc.TrackLocalContext) error { t.mu.Lock() t.writer = nil t.mu.Unlock() return nil } func (t *Track) ID() string { return t.id } func (t *Track) RID() string { return "" // don't know what it is } func (t *Track) StreamID() string { return t.streamID } func (t *Track) Kind() webrtc.RTPCodecType { return webrtc.NewRTPCodecType(t.kind) } func (t *Track) WriteRTP(payloadType uint8, packet *rtp.Packet) (err error) { // using mutex because Unbind https://github.com/AlexxIT/go2rtc/issues/994 t.mu.Lock() // in case when we start WriteRTP before Track.Bind if t.writer != nil { // important to have internal counter if input packets from different sources t.sequence++ header := packet.Header header.SSRC = t.ssrc header.PayloadType = payloadType header.SequenceNumber = t.sequence _, err = t.writer.WriteRTP(&header, packet.Payload) } t.mu.Unlock() return } ================================================ FILE: pkg/webrtc/webrtc_test.go ================================================ package webrtc import ( "testing" "github.com/pion/webrtc/v4" "github.com/stretchr/testify/require" ) func TestAlexa(t *testing.T) { // from https://github.com/AlexxIT/go2rtc/issues/825 offer := `v=0 o=- 3911343731 3911343731 IN IP4 0.0.0.0 s=a 2 z c=IN IP4 0.0.0.0 t=0 0 a=group:BUNDLE audio0 video0 m=audio 1 UDP/TLS/RTP/SAVPF 96 0 8 a=candidate:1 1 UDP 2013266431 52.90.193.210 60128 typ host a=candidate:2 1 TCP 1015021823 52.90.193.210 9 typ host tcptype active a=candidate:3 1 TCP 1010827519 52.90.193.210 45962 typ host tcptype passive a=candidate:1 2 UDP 2013266430 52.90.193.210 46109 typ host a=candidate:2 2 TCP 1015021822 52.90.193.210 9 typ host tcptype active a=candidate:3 2 TCP 1010827518 52.90.193.210 53795 typ host tcptype passive a=setup:actpass a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time a=rtpmap:96 opus/48000/2 a=rtpmap:0 PCMU/8000 a=rtpmap:8 PCMA/8000 a=rtcp:9 IN IP4 0.0.0.0 a=rtcp-mux a=sendrecv a=mid:audio0 a=ssrc:3573704076 cname:user3856789923@host-9dd1dd33 a=ice-ufrag:gxfV a=ice-pwd:KepKrlQ1+LD+RGTAFaqVck a=fingerprint:sha-256 A2:93:53:50:E4:2F:C5:4E:DF:7C:70:99:5A:A7:39:50:1A:63:E5:B2:CA:73:70:7A:C5:F4:01:BF:BD:99:57:FC m=video 1 UDP/TLS/RTP/SAVPF 99 a=candidate:1 1 UDP 2013266431 52.90.193.210 60128 typ host a=candidate:1 2 UDP 2013266430 52.90.193.210 46109 typ host a=candidate:2 1 TCP 1015021823 52.90.193.210 9 typ host tcptype active a=candidate:3 1 TCP 1010827519 52.90.193.210 45962 typ host tcptype passive a=candidate:3 2 TCP 1010827518 52.90.193.210 53795 typ host tcptype passive a=candidate:2 2 TCP 1015021822 52.90.193.210 9 typ host tcptype active b=AS:2500 a=setup:actpass a=extmap:3 http://www.webrtc.org/experiments/rtp-hdrext/abs-send-time a=rtpmap:99 H264/90000 a=rtcp:9 IN IP4 0.0.0.0 a=rtcp-mux a=sendrecv a=mid:video0 a=rtcp-fb:99 nack a=rtcp-fb:99 nack pli a=rtcp-fb:99 ccm fir a=ssrc:3778078295 cname:user3856789923@host-9dd1dd33 a=ice-ufrag:gxfV a=ice-pwd:KepKrlQ1+LD+RGTAFaqVck a=fingerprint:sha-256 A2:93:53:50:E4:2F:C5:4E:DF:7C:70:99:5A:A7:39:50:1A:63:E5:B2:CA:73:70:7A:C5:F4:01:BF:BD:99:57:FC ` pc, err := webrtc.NewPeerConnection(webrtc.Configuration{}) require.Nil(t, err) conn := NewConn(pc) err = conn.SetOffer(offer) require.Nil(t, err) _, err = conn.GetAnswer() require.Nil(t, err) } ================================================ FILE: pkg/webtorrent/client.go ================================================ package webtorrent import ( "encoding/base64" "fmt" "strconv" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/webrtc" "github.com/gorilla/websocket" pion "github.com/pion/webrtc/v4" ) func NewClient(tracker, share, pwd string, pc *pion.PeerConnection) (*webrtc.Conn, error) { // 1. Create WebRTC producer prod := webrtc.NewConn(pc) prod.FormatName = "webtorrent" prod.Mode = core.ModeActiveProducer prod.Protocol = "ws" medias := []*core.Media{ {Kind: core.KindVideo, Direction: core.DirectionRecvonly}, {Kind: core.KindAudio, Direction: core.DirectionRecvonly}, } // 2. Create offer offer, err := prod.CreateCompleteOffer(medias) if err != nil { return nil, err } // 3. Encrypt offer nonce := strconv.FormatInt(time.Now().UnixNano(), 36) cipher, err := NewCipher(share, pwd, nonce) if err != nil { return nil, err } enc := cipher.Encrypt([]byte(offer)) // 4. Connect to Tracker ws, _, err := websocket.DefaultDialer.Dial(tracker, nil) if err != nil { return nil, err } defer ws.Close() // 5. Send offer msg := fmt.Sprintf( `{"action":"announce","info_hash":"%s","peer_id":"%s","offers":[{"offer_id":"%s","offer":{"type":"offer","sdp":"%s"}}],"numwant":1}`, InfoHash(share), core.RandString(16, 36), nonce, base64.StdEncoding.EncodeToString(enc), ) if err = ws.WriteMessage(websocket.TextMessage, []byte(msg)); err != nil { return nil, err } // wait 30 seconds until full answer if err = ws.SetReadDeadline(time.Now().Add(time.Second * 30)); err != nil { return nil, err } for { // 6. Read answer var v Message if err = ws.ReadJSON(&v); err != nil { return nil, err } if v.Answer == nil { continue } // 7. Decrypt answer enc, err = base64.StdEncoding.DecodeString(v.Answer.SDP) if err != nil { return nil, err } answer, err := cipher.Decrypt(enc) if err != nil { return nil, err } // 8. Set answer if err = prod.SetAnswer(string(answer)); err != nil { return nil, err } return prod, nil } } ================================================ FILE: pkg/webtorrent/crypto.go ================================================ package webtorrent import ( "crypto/aes" "crypto/cipher" "crypto/sha256" "encoding/base64" "fmt" "strconv" "time" ) type Cipher struct { gcm cipher.AEAD iv []byte nonce []byte } func NewCipher(share, pwd, nonce string) (*Cipher, error) { timestamp, err := strconv.ParseInt(nonce, 36, 64) if err != nil { return nil, err } delta := time.Duration(time.Now().UnixNano() - timestamp) if delta < 0 { delta = -delta } // protect from replay attack, but respect wrong timezone on server if delta > 12*time.Hour { return nil, fmt.Errorf("wrong timedelta %s", delta) } c := &Cipher{} hash := sha256.New() hash.Write([]byte(nonce + ":" + pwd)) key := hash.Sum(nil) hash.Reset() hash.Write([]byte(share + ":" + nonce)) c.iv = hash.Sum(nil)[:12] block, err := aes.NewCipher(key) if err != nil { return nil, err } c.gcm, err = cipher.NewGCM(block) if err != nil { return nil, err } c.nonce = []byte(nonce) return c, nil } func (c *Cipher) Decrypt(ciphertext []byte) ([]byte, error) { return c.gcm.Open(nil, c.iv, ciphertext, c.nonce) } func (c *Cipher) Encrypt(plaintext []byte) []byte { return c.gcm.Seal(nil, c.iv, plaintext, c.nonce) } func InfoHash(share string) string { hash := sha256.New() hash.Write([]byte(share)) sum := hash.Sum(nil) return base64.StdEncoding.EncodeToString(sum) } ================================================ FILE: pkg/webtorrent/server.go ================================================ package webtorrent import ( "encoding/base64" "fmt" "sync" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/gorilla/websocket" ) type Server struct { core.Listener URL string Exchange func(src, offer string) (answer string, err error) shares map[string]*Share mu sync.Mutex announce *core.Worker } type Share struct { name string pwd string src string } func (s *Server) AddShare(name, pwd, src string) { s.mu.Lock() if s.shares == nil { s.shares = map[string]*Share{} } if len(s.shares) == 0 { go s.Serve() } hash := InfoHash(name) s.shares[hash] = &Share{ name: name, pwd: pwd, src: src, } s.announce.Do() s.mu.Unlock() } func (s *Server) GetSharePwd(name string) (pwd string) { hash := InfoHash(name) s.mu.Lock() if share, ok := s.shares[hash]; ok { pwd = share.pwd } s.mu.Unlock() return } func (s *Server) RemoveShare(name string) { hash := InfoHash(name) s.mu.Lock() if _, ok := s.shares[hash]; ok { delete(s.shares, hash) } s.mu.Unlock() } // Serve - run reconnection loop, will exit on?? func (s *Server) Serve() error { for s.HasShares() { s.Fire("connect to tracker: " + s.URL) ws, _, err := websocket.DefaultDialer.Dial(s.URL, nil) if err != nil { s.Fire(err) time.Sleep(time.Minute) continue } peerID := core.RandString(16, 36) // instant run announce worker s.announce = core.NewWorker(0, func() time.Duration { if err = s.writer(ws, peerID); err != nil { return 0 } return time.Minute }) // run reader forewer for { if err = s.reader(ws, peerID); err != nil { break } } // stop announcing worker s.announce.Stop() // ensure ws is stopped _ = ws.Close() time.Sleep(time.Minute) } s.Fire("disconnect") return nil } // reader - receive offers in the loop, will exit on ws.Close func (s *Server) reader(ws *websocket.Conn, peerID string) error { var v Message if err := ws.ReadJSON(&v); err != nil { return err } s.Fire(&v) s.mu.Lock() share, ok := s.shares[v.InfoHash] s.mu.Unlock() // skip any unknown shares if !ok || v.OfferId == "" || v.Offer == nil { return nil } s.Fire("new offer: " + v.OfferId) cipher, err := NewCipher(share.name, share.pwd, v.OfferId) if err != nil { s.Fire(err) return nil } enc, err := base64.StdEncoding.DecodeString(v.Offer.SDP) if err != nil { s.Fire(err) return nil } offer, err := cipher.Decrypt(enc) if err != nil { s.Fire(err) return nil } answer, err := s.Exchange(share.src, string(offer)) if err != nil { s.Fire(err) return nil } enc = cipher.Encrypt([]byte(answer)) raw := fmt.Sprintf( `{"action":"announce","info_hash":"%s","peer_id":"%s","offer_id":"%s","answer":{"type":"answer","sdp":"%s"},"to_peer_id":"%s"}`, v.InfoHash, peerID, v.OfferId, base64.StdEncoding.EncodeToString(enc), v.PeerId, ) return ws.WriteMessage(websocket.TextMessage, []byte(raw)) } func (s *Server) writer(ws *websocket.Conn, peerID string) error { s.mu.Lock() defer s.mu.Unlock() if len(s.shares) == 0 { return ws.Close() } for hash := range s.shares { msg := fmt.Sprintf( `{"action":"announce","info_hash":"%s","peer_id":"%s","offers":[],"numwant":10}`, hash, peerID, ) if err := ws.WriteMessage(websocket.TextMessage, []byte(msg)); err != nil { return err } } return nil } func (s *Server) HasShares() bool { s.mu.Lock() defer s.mu.Unlock() return len(s.shares) > 0 } type Message struct { Action string `json:"action"` InfoHash string `json:"info_hash"` // Announce msg Numwant int `json:"numwant,omitempty"` PeerId string `json:"peer_id,omitempty"` Offers []struct { OfferId string `json:"offer_id"` Offer struct { Type string `json:"type"` SDP string `json:"sdp"` } `json:"offer"` } `json:"offers,omitempty"` // Interval msg Interval int `json:"interval,omitempty"` Complete int `json:"complete,omitempty"` Incomplete int `json:"incomplete,omitempty"` // Offer msg OfferId string `json:"offer_id,omitempty"` Offer *struct { Type string `json:"type"` SDP string `json:"sdp"` } `json:"offer,omitempty"` // Answer msg ToPeerId string `json:"to_peer_id,omitempty"` Answer *struct { Type string `json:"type"` SDP string `json:"sdp"` } `json:"answer,omitempty"` } ================================================ FILE: pkg/wyoming/README.md ================================================ ## Default wake words - alexa_v0.1 - hey_jarvis_v0.1 - hey_mycroft_v0.1 - hey_rhasspy_v0.1 - ok_nabu_v0.1 ## Useful Links - https://github.com/rhasspy/wyoming-satellite - https://github.com/rhasspy/wyoming-openwakeword - https://github.com/fwartner/home-assistant-wakewords-collection - https://github.com/esphome/micro-wake-word-models/tree/main?tab=readme-ov-file ================================================ FILE: pkg/wyoming/api.go ================================================ package wyoming import ( "bufio" "encoding/json" "io" "net" "github.com/AlexxIT/go2rtc/pkg/core" ) type API struct { conn net.Conn rd *bufio.Reader } func DialAPI(address string) (*API, error) { conn, err := net.DialTimeout("tcp", address, core.ConnDialTimeout) if err != nil { return nil, err } return NewAPI(conn), nil } const Version = "1.5.4" func NewAPI(conn net.Conn) *API { return &API{conn: conn, rd: bufio.NewReader(conn)} } func (w *API) WriteEvent(evt *Event) (err error) { hdr := EventHeader{ Type: evt.Type, Version: Version, DataLength: len(evt.Data), PayloadLength: len(evt.Payload), } buf, err := json.Marshal(hdr) if err != nil { return err } buf = append(buf, '\n') buf = append(buf, evt.Data...) buf = append(buf, evt.Payload...) _, err = w.conn.Write(buf) return err } func (w *API) ReadEvent() (*Event, error) { data, err := w.rd.ReadBytes('\n') if err != nil { return nil, err } var hdr EventHeader if err = json.Unmarshal(data, &hdr); err != nil { return nil, err } evt := Event{Type: hdr.Type} if hdr.DataLength > 0 { data = make([]byte, hdr.DataLength) if _, err = io.ReadFull(w.rd, data); err != nil { return nil, err } evt.Data = string(data) } if hdr.PayloadLength > 0 { evt.Payload = make([]byte, hdr.PayloadLength) if _, err = io.ReadFull(w.rd, evt.Payload); err != nil { return nil, err } } return &evt, nil } func (w *API) Close() error { return w.conn.Close() } type Event struct { Type string Data string Payload []byte } type EventHeader struct { Type string `json:"type"` Version string `json:"version"` DataLength int `json:"data_length,omitempty"` PayloadLength int `json:"payload_length,omitempty"` } ================================================ FILE: pkg/wyoming/backchannel.go ================================================ package wyoming import ( "fmt" "net" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type Backchannel struct { core.Connection api *API } func newBackchannel(conn net.Conn) *Backchannel { return &Backchannel{ core.Connection{ ID: core.NewID(), FormatName: "wyoming", Medias: []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecPCML, ClockRate: 22050}, }, }, }, Transport: conn, }, NewAPI(conn), } } func (b *Backchannel) GetTrack(media *core.Media, codec *core.Codec) (*core.Receiver, error) { return nil, core.ErrCantGetTrack } func (b *Backchannel) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { sender := core.NewSender(media, codec) sender.Handler = func(pkt *rtp.Packet) { ts := time.Now().Nanosecond() evt := &Event{ Type: "audio-chunk", Data: fmt.Sprintf(`{"rate":22050,"width":2,"channels":1,"timestamp":%d}`, ts), Payload: pkt.Payload, } _ = b.api.WriteEvent(evt) } sender.HandleRTP(track) b.Senders = append(b.Senders, sender) return nil } func (b *Backchannel) Start() error { for { if _, err := b.api.ReadEvent(); err != nil { return err } } } ================================================ FILE: pkg/wyoming/expr.go ================================================ package wyoming import ( "bytes" "fmt" "os" "time" "github.com/AlexxIT/go2rtc/pkg/expr" "github.com/AlexxIT/go2rtc/pkg/wav" ) type env struct { *satellite Type string Data string } func (s *satellite) handleEvent(evt *Event) { switch evt.Type { case "describe": // {"asr": [], "tts": [], "handle": [], "intent": [], "wake": [], "satellite": {"name": "my satellite", "attribution": {"name": "", "url": ""}, "installed": true, "description": "my satellite", "version": "1.4.1", "area": null, "snd_format": null}} data := fmt.Sprintf(`{"satellite":{"name":%q,"attribution":{"name":"go2rtc","url":"https://github.com/AlexxIT/go2rtc"},"installed":true}}`, s.srv.Name) s.WriteEvent("info", data) case "run-satellite": s.Detect() case "pause-satellite": s.Stop() case "detect": // WAKE_WORD_START {"names": null} case "detection": // WAKE_WORD_END {"name": "ok_nabu_v0.1", "timestamp": 17580, "speaker": null} case "transcribe": // STT_START {"language": "en"} case "voice-started": // STT_VAD_START {"timestamp": 1160} case "voice-stopped": // STT_VAD_END {"timestamp": 2470} s.Pause() case "transcript": // STT_END {"text": "how are you"} case "synthesize": // TTS_START {"text": "Sorry, I couldn't understand that", "voice": {"language": "en"}} case "audio-start": // TTS_END {"rate": 22050, "width": 2, "channels": 1, "timestamp": 0} case "audio-chunk": // {"rate": 22050, "width": 2, "channels": 1, "timestamp": 0} case "audio-stop": // {"timestamp": 2.880000000000002} // run async because PlayAudio takes some time go func() { s.PlayAudio() s.WriteEvent("played") s.Detect() }() case "error": s.Detect() case "internal-run": s.WriteEvent("run-pipeline", `{"start_stage":"wake","end_stage":"tts"}`) s.Stream() case "internal-detection": s.WriteEvent("run-pipeline", `{"start_stage":"asr","end_stage":"tts"}`) s.Stream() } } func (s *satellite) handleScript(evt *Event) { var script string if s.srv.Event != nil { script = s.srv.Event[evt.Type] } s.srv.Trace("event=%s data=%s payload size=%d", evt.Type, evt.Data, len(evt.Payload)) if script == "" { s.handleEvent(evt) return } // run async because script can have sleeps go func() { e := &env{satellite: s, Type: evt.Type, Data: evt.Data} if res, err := expr.Eval(script, e); err != nil { s.srv.Trace("event=%s expr error=%s", evt.Type, err) s.handleEvent(evt) } else { s.srv.Trace("event=%s expr result=%v", evt.Type, res) } }() } func (s *satellite) Detect() bool { return s.setMicState(stateWaitVAD) } func (s *satellite) Stream() bool { return s.setMicState(stateActive) } func (s *satellite) Pause() bool { return s.setMicState(stateIdle) } func (s *satellite) Stop() bool { s.micStop() return true } func (s *satellite) WriteEvent(args ...string) bool { if len(args) == 0 { return false } evt := &Event{Type: args[0]} if len(args) > 1 { evt.Data = args[1] } if err := s.api.WriteEvent(evt); err != nil { return false } return true } func (s *satellite) PlayAudio() bool { return s.playAudio(sndCodec, bytes.NewReader(s.sndAudio)) } func (s *satellite) PlayFile(path string) bool { f, err := os.Open(path) if err != nil { return false } codec, err := wav.ReadHeader(f) if err != nil { return false } return s.playAudio(codec, f) } func (e *env) Sleep(s string) bool { d, err := time.ParseDuration(s) if err != nil { return false } time.Sleep(d) return true } ================================================ FILE: pkg/wyoming/mic.go ================================================ package wyoming import ( "fmt" "net" "github.com/AlexxIT/go2rtc/pkg/core" ) func (s *Server) HandleMic(conn net.Conn) { defer conn.Close() var closed core.Waiter var timestamp int api := NewAPI(conn) mic := newMicConsumer(func(chunk []byte) { data := fmt.Sprintf(`{"rate":16000,"width":2,"channels":1,"timestamp":%d}`, timestamp) evt := &Event{Type: "audio-chunk", Data: data, Payload: chunk} if err := api.WriteEvent(evt); err != nil { closed.Done(nil) } timestamp += len(chunk) / 2 }) mic.RemoteAddr = api.conn.RemoteAddr().String() if err := s.MicHandler(mic); err != nil { s.Error("mic error: %s", err) return } _ = closed.Wait() _ = mic.Stop() } ================================================ FILE: pkg/wyoming/producer.go ================================================ package wyoming import ( "net" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type Producer struct { core.Connection api *API } func newProducer(conn net.Conn) *Producer { return &Producer{ core.Connection{ ID: core.NewID(), FormatName: "wyoming", Medias: []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ {Name: core.CodecPCML, ClockRate: 16000}, }, }, }, Transport: conn, }, NewAPI(conn), } } func (p *Producer) Start() error { var seq uint16 var ts uint32 for { evt, err := p.api.ReadEvent() if err != nil { return err } if evt.Type != "audio-chunk" { continue } p.Recv += len(evt.Payload) pkt := &core.Packet{ Header: rtp.Header{ Version: 2, Marker: true, SequenceNumber: seq, Timestamp: ts, }, Payload: evt.Payload, } p.Receivers[0].WriteRTP(pkt) seq++ ts += uint32(len(evt.Payload) / 2) } } ================================================ FILE: pkg/wyoming/satellite.go ================================================ package wyoming import ( "context" "fmt" "io" "net" "sync" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/pcm" "github.com/AlexxIT/go2rtc/pkg/pcm/s16le" "github.com/pion/rtp" ) type Server struct { Name string Event map[string]string VADThreshold int16 WakeURI string MicHandler func(cons core.Consumer) error SndHandler func(prod core.Producer) error Trace func(format string, v ...any) Error func(format string, v ...any) } func (s *Server) Serve(l net.Listener) error { for { conn, err := l.Accept() if err != nil { return err } go s.Handle(conn) } } func (s *Server) Handle(conn net.Conn) { api := NewAPI(conn) sat := newSatellite(api, s) defer sat.Close() for { evt, err := api.ReadEvent() if err != nil { return } switch evt.Type { case "ping": // {"text": null} _ = api.WriteEvent(&Event{Type: "pong", Data: evt.Data}) case "audio-start": // TTS_END {"rate": 22050, "width": 2, "channels": 1, "timestamp": 0} sat.sndAudio = sat.sndAudio[:0] case "audio-chunk": // {"rate": 22050, "width": 2, "channels": 1, "timestamp": 0} sat.sndAudio = append(sat.sndAudio, evt.Payload...) default: sat.handleScript(evt) } } } // states like http.ConnState const ( stateError = -2 stateClosed = -1 stateNew = 0 stateIdle = 1 stateWaitVAD = 2 // aka wait VAD stateWaitWakeWord = 3 stateActive = 4 ) type satellite struct { api *API srv *Server micState int8 micTS int micMu sync.Mutex sndAudio []byte mic *micConsumer wake *WakeWord } func newSatellite(api *API, srv *Server) *satellite { sat := &satellite{api: api, srv: srv} return sat } func (s *satellite) Close() error { s.Stop() return s.api.Close() } const wakeTimeout = 5 * 2 * 16000 // 5 seconds func (s *satellite) setMicState(state int8) bool { s.micMu.Lock() defer s.micMu.Unlock() if s.micState == stateNew { s.mic = newMicConsumer(s.onMicChunk) s.mic.RemoteAddr = s.api.conn.RemoteAddr().String() if err := s.srv.MicHandler(s.mic); err != nil { s.micState = stateError s.srv.Error("can't get mic: %w", err) _ = s.api.Close() } else { s.micState = stateIdle } } if s.micState < stateIdle { return false } s.micState = state s.micTS = 0 return true } func (s *satellite) micStop() { s.micMu.Lock() s.micState = stateClosed if s.mic != nil { _ = s.mic.Stop() s.mic = nil } if s.wake != nil { _ = s.wake.Close() s.wake = nil } s.micMu.Unlock() } func (s *satellite) onMicChunk(chunk []byte) { s.micMu.Lock() defer s.micMu.Unlock() if s.micState == stateIdle { return } if s.micState == stateWaitVAD { // tests show that values over 1000 are most likely speech if s.srv.VADThreshold == 0 || s16le.PeaksRMS(chunk) > s.srv.VADThreshold { if s.wake == nil && s.srv.WakeURI != "" { s.wake, _ = DialWakeWord(s.srv.WakeURI) } if s.wake == nil { // some problems with wake word - redirect to HA s.micState = stateIdle go s.handleScript(&Event{Type: "internal-run"}) } else { s.micState = stateWaitWakeWord } s.micTS = 0 } } if s.micState == stateWaitWakeWord { if s.wake.Detection != "" { // check if wake word detected s.micState = stateIdle go s.handleScript(&Event{Type: "internal-detection", Data: `{"name":"` + s.wake.Detection + `"}`}) } else if err := s.wake.WriteChunk(chunk); err != nil { // wake word service failed s.micState = stateWaitVAD _ = s.wake.Close() s.wake = nil } else if s.micTS > wakeTimeout { // wake word detection timeout s.micState = stateWaitVAD } } else if s.wake != nil { _ = s.wake.Close() s.wake = nil } if s.micState == stateActive { data := fmt.Sprintf(`{"rate":16000,"width":2,"channels":1,"timestamp":%d}`, s.micTS) evt := &Event{Type: "audio-chunk", Data: data, Payload: chunk} _ = s.api.WriteEvent(evt) } s.micTS += len(chunk) / 2 } func (s *satellite) playAudio(codec *core.Codec, rd io.Reader) bool { ctx, cancel := context.WithCancel(context.Background()) defer cancel() prod := pcm.OpenSync(codec, rd) prod.OnClose(cancel) if err := s.srv.SndHandler(prod); err != nil { return false } else { <-ctx.Done() return true } } type micConsumer struct { core.Connection onData func(chunk []byte) onClose func() } func newMicConsumer(onData func(chunk []byte)) *micConsumer { medias := []*core.Media{ { Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: pcm.ConsumerCodecs(), }, } return &micConsumer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "wyoming", Protocol: "tcp", Medias: medias, }, onData: onData, } } func (c *micConsumer) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { src := track.Codec dst := &core.Codec{ Name: core.CodecPCML, ClockRate: 16000, Channels: 1, } sender := core.NewSender(media, dst) sender.Handler = pcm.TranscodeHandler(dst, src, repack(func(packet *core.Packet) { c.onData(packet.Payload) }), ) sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } func (c *micConsumer) Stop() error { if c.onClose != nil { c.onClose() } return c.Connection.Stop() } func repack(handler core.HandlerFunc) core.HandlerFunc { const PacketSize = 2 * 16000 / 50 // 20ms var buf []byte return func(pkt *rtp.Packet) { buf = append(buf, pkt.Payload...) for len(buf) >= PacketSize { pkt = &core.Packet{Payload: buf[:PacketSize]} buf = buf[PacketSize:] handler(pkt) } } } ================================================ FILE: pkg/wyoming/snd.go ================================================ package wyoming import ( "bytes" "net" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/pcm" ) func (s *Server) HandleSnd(conn net.Conn) { defer conn.Close() var snd []byte api := NewAPI(conn) for { evt, err := api.ReadEvent() if err != nil { return } s.Trace("event: %s data: %s payload: %d", evt.Type, evt.Data, len(evt.Payload)) switch evt.Type { case "audio-start": snd = snd[:0] case "audio-chunk": snd = append(snd, evt.Payload...) case "audio-stop": prod := pcm.OpenSync(sndCodec, bytes.NewReader(snd)) if err = s.SndHandler(prod); err != nil { s.Error("snd error: %s", err) return } } } } var sndCodec = &core.Codec{Name: core.CodecPCML, ClockRate: 22050} ================================================ FILE: pkg/wyoming/wakeword.go ================================================ package wyoming import ( "encoding/json" "fmt" "net/url" ) type WakeWord struct { *API names []string send int Detection string } func DialWakeWord(rawURL string) (*WakeWord, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } api, err := DialAPI(u.Host) if err != nil { return nil, err } names := u.Query()["name"] if len(names) == 0 { names = []string{"ok_nabu_v0.1"} } wake := &WakeWord{API: api, names: names} if err = wake.Start(); err != nil { _ = wake.Close() return nil, err } go wake.handle() return wake, nil } func (w *WakeWord) handle() { defer w.Close() for { evt, err := w.ReadEvent() if err != nil { return } if evt.Type == "detection" { var data struct { Name string `json:"name"` } if err = json.Unmarshal([]byte(evt.Data), &data); err != nil { return } w.Detection = data.Name } } } //func (w *WakeWord) Describe() error { // if err := w.WriteEvent(&Event{Type: "describe"}); err != nil { // return err // } // // evt, err := w.ReadEvent() // if err != nil { // return err // } // // var info struct { // Wake []struct { // Models []struct { // Name string `json:"name"` // } `json:"models"` // } `json:"wake"` // } // if err = json.Unmarshal(evt.Data, &info); err != nil { // return err // } // // return nil //} func (w *WakeWord) Start() error { msg := struct { Names []string `json:"names"` }{ Names: w.names, } data, err := json.Marshal(msg) if err != nil { return err } evt := &Event{Type: "detect", Data: string(data)} if err := w.WriteEvent(evt); err != nil { return err } evt = &Event{Type: "audio-start", Data: audioData(0)} return w.WriteEvent(evt) } func (w *WakeWord) Close() error { return w.conn.Close() } func (w *WakeWord) WriteChunk(payload []byte) error { evt := &Event{Type: "audio-chunk", Data: audioData(w.send), Payload: payload} w.send += len(payload) return w.WriteEvent(evt) } func audioData(send int) string { // timestamp in ms = send / 2 * 1000 / 16000 = send / 32 return fmt.Sprintf(`{"rate":16000,"width":2,"channels":1,"timestamp":%d}`, send/32) } ================================================ FILE: pkg/wyoming/wyoming.go ================================================ package wyoming import ( "net" "net/url" "github.com/AlexxIT/go2rtc/pkg/core" ) func Dial(rawURL string) (core.Producer, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } conn, err := net.DialTimeout("tcp", u.Host, core.ConnDialTimeout) if err != nil { return nil, err } if u.Query().Get("backchannel") != "1" { return newProducer(conn), nil } else { return newBackchannel(conn), nil } } ================================================ FILE: pkg/wyze/backchannel.go ================================================ package wyze import ( "fmt" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/tutk" "github.com/pion/rtp" ) func (p *Producer) AddTrack(media *core.Media, codec *core.Codec, track *core.Receiver) error { if err := p.client.StartIntercom(); err != nil { return fmt.Errorf("wyze: failed to enable intercom: %w", err) } // Get the camera's audio codec info (what it sent us = what it accepts) tutkCodec, sampleRate, channels := p.client.GetBackchannelCodec() if tutkCodec == 0 { return fmt.Errorf("wyze: no audio codec detected from camera") } if p.client.verbose { fmt.Printf("[Wyze] Intercom enabled, using codec=0x%04x rate=%d ch=%d\n", tutkCodec, sampleRate, channels) } sender := core.NewSender(media, track.Codec) // Track our own timestamp - camera expects timestamps starting from 0 // and incrementing by frame duration in microseconds var timestamp uint32 = 0 samplesPerFrame := tutk.GetSamplesPerFrame(tutkCodec) frameDurationUS := samplesPerFrame * 1000000 / sampleRate sender.Handler = func(pkt *rtp.Packet) { if err := p.client.WriteAudio(tutkCodec, pkt.Payload, timestamp, sampleRate, channels); err == nil { p.Send += len(pkt.Payload) } timestamp += frameDurationUS } switch track.Codec.Name { case core.CodecAAC: if track.Codec.IsRTP() { sender.Handler = aac.RTPToADTS(codec, sender.Handler) } else { sender.Handler = aac.EncodeToADTS(codec, sender.Handler) } } sender.HandleRTP(track) p.Senders = append(p.Senders, sender) return nil } ================================================ FILE: pkg/wyze/client.go ================================================ package wyze import ( "crypto/rand" "encoding/binary" "encoding/json" "fmt" "net" "net/url" "strconv" "strings" "sync" "time" "github.com/AlexxIT/go2rtc/pkg/tutk" "github.com/AlexxIT/go2rtc/pkg/tutk/dtls" ) const ( FrameSize1080P = 0 FrameSize360P = 1 FrameSize720P = 2 FrameSize2K = 3 FrameSizeFloodlight = 4 ) const ( BitrateMax uint16 = 0xF0 BitrateSD uint16 = 0x3C ) const ( MediaTypeVideo = 1 MediaTypeAudio = 2 MediaTypeReturnAudio = 3 MediaTypeRDT = 4 ) const ( KCmdAuth = 10000 KCmdChallenge = 10001 KCmdChallengeResp = 10002 KCmdAuthResult = 10003 KCmdControlChannel = 10010 KCmdControlChannelResp = 10011 KCmdSetResolutionDB = 10052 KCmdSetResolutionDBRes = 10053 KCmdSetResolution = 10056 KCmdSetResolutionResp = 10057 ) type Client struct { conn *dtls.DTLSConn host string uid string enr string mac string model string authKey string verbose bool closed bool closeMu sync.Mutex hasAudio bool hasIntercom bool audioCodecID byte audioSampleRate uint32 audioChannels uint8 } type AuthResponse struct { ConnectionRes string `json:"connectionRes"` CameraInfo map[string]any `json:"cameraInfo"` } func Dial(rawURL string) (*Client, error) { u, err := url.Parse(rawURL) if err != nil { return nil, fmt.Errorf("wyze: invalid URL: %w", err) } query := u.Query() if query.Get("dtls") != "true" { return nil, fmt.Errorf("wyze: only DTLS cameras are supported") } c := &Client{ host: u.Host, uid: query.Get("uid"), enr: query.Get("enr"), mac: query.Get("mac"), model: query.Get("model"), verbose: query.Get("verbose") == "true", } c.authKey = string(dtls.CalculateAuthKey(c.enr, c.mac)) if c.verbose { fmt.Printf("[Wyze] Connecting to %s (UID: %s)\n", c.host, c.uid) } if err := c.connect(); err != nil { c.Close() return nil, err } if err := c.doAVLogin(); err != nil { c.Close() return nil, err } if err := c.doKAuth(); err != nil { c.Close() return nil, err } if c.verbose { fmt.Printf("[Wyze] Connection established\n") } return c, nil } func (c *Client) SupportsAudio() bool { return c.hasAudio } func (c *Client) SupportsIntercom() bool { return c.hasIntercom } func (c *Client) SetBackchannelCodec(codecID byte, sampleRate uint32, channels uint8) { c.audioCodecID = codecID c.audioSampleRate = sampleRate c.audioChannels = channels } func (c *Client) GetBackchannelCodec() (codecID byte, sampleRate uint32, channels uint8) { return c.audioCodecID, c.audioSampleRate, c.audioChannels } func (c *Client) SetResolution(quality byte) error { var frameSize uint8 var bitrate uint16 switch quality { case 0: // Auto/HD - use model's best frameSize = c.hdFrameSize() bitrate = BitrateMax case FrameSize360P: // 1 = SD/360P frameSize = FrameSize360P bitrate = BitrateSD case FrameSize720P: // 2 = 720P frameSize = FrameSize720P bitrate = BitrateMax case FrameSize2K: // 3 = 2K if c.is2K() { frameSize = FrameSize2K } else { frameSize = c.hdFrameSize() } bitrate = BitrateMax case FrameSizeFloodlight: // 4 = Floodlight frameSize = c.hdFrameSize() bitrate = BitrateMax default: frameSize = quality bitrate = BitrateMax } if c.verbose { fmt.Printf("[Wyze] SetResolution: quality=%d frameSize=%d bitrate=%d model=%s\n", quality, frameSize, bitrate, c.model) } // Use K10052 (doorbell format) for certain models if c.useDoorbellResolution() { k10052 := c.buildK10052(frameSize, bitrate) _, err := c.conn.WriteAndWaitIOCtrl(k10052, c.matchHL(KCmdSetResolutionDBRes), 5*time.Second) return err } k10056 := c.buildK10056(frameSize, bitrate) _, err := c.conn.WriteAndWaitIOCtrl(k10056, c.matchHL(KCmdSetResolutionResp), 5*time.Second) return err } func (c *Client) StartVideo() error { k10010 := c.buildK10010(MediaTypeVideo, true) _, err := c.conn.WriteAndWaitIOCtrl(k10010, c.matchHL(KCmdControlChannelResp), 5*time.Second) return err } func (c *Client) StartAudio() error { k10010 := c.buildK10010(MediaTypeAudio, true) _, err := c.conn.WriteAndWaitIOCtrl(k10010, c.matchHL(KCmdControlChannelResp), 5*time.Second) return err } func (c *Client) StartIntercom() error { if c.conn == nil { return fmt.Errorf("connection is nil") } if c.conn.IsBackchannelReady() { return nil } k10010 := c.buildK10010(MediaTypeReturnAudio, true) if _, err := c.conn.WriteAndWaitIOCtrl(k10010, c.matchHL(KCmdControlChannelResp), 5*time.Second); err != nil { return fmt.Errorf("enable return audio: %w", err) } if c.verbose { fmt.Printf("[Wyze] Speaker channel enabled, waiting for readiness...\n") } return c.conn.AVServStart() } func (c *Client) StopIntercom() error { if c.conn == nil || !c.conn.IsBackchannelReady() { return nil } k10010 := c.buildK10010(MediaTypeReturnAudio, false) c.conn.WriteIOCtrl(k10010) return c.conn.AVServStop() } func (c *Client) ReadPacket() (*tutk.Packet, error) { return c.conn.AVRecvFrameData() } func (c *Client) WriteAudio(codec byte, payload []byte, timestamp uint32, sampleRate uint32, channels uint8) error { if !c.conn.IsBackchannelReady() { return fmt.Errorf("speaker channel not connected") } if c.verbose { fmt.Printf("[Wyze] WriteAudio: codec=0x%02x, payload=%d bytes, rate=%d, ch=%d\n", codec, len(payload), sampleRate, channels) } return c.conn.AVSendAudioData(codec, payload, timestamp, sampleRate, channels) } func (c *Client) SetDeadline(t time.Time) error { if c.conn != nil { return c.conn.SetDeadline(t) } return nil } func (c *Client) Protocol() string { return "wyze/dtls" } func (c *Client) RemoteAddr() net.Addr { if c.conn != nil { return c.conn.RemoteAddr() } return nil } func (c *Client) Close() error { c.closeMu.Lock() if c.closed { c.closeMu.Unlock() return nil } c.closed = true c.closeMu.Unlock() if c.verbose { fmt.Printf("[Wyze] Closing connection\n") } c.StopIntercom() if c.conn != nil { c.conn.Close() } if c.verbose { fmt.Printf("[Wyze] Connection closed\n") } return nil } func (c *Client) connect() error { host := c.host port := 0 if idx := strings.Index(host, ":"); idx > 0 { if p, err := strconv.Atoi(host[idx+1:]); err == nil { port = p } host = host[:idx] } conn, err := dtls.DialDTLS(host, port, c.uid, c.authKey, c.enr, c.verbose) if err != nil { return fmt.Errorf("wyze: connect failed: %w", err) } c.conn = conn if c.verbose { fmt.Printf("[Wyze] Connected to %s (IOTC + DTLS)\n", conn.RemoteAddr()) } return nil } func (c *Client) doAVLogin() error { if c.verbose { fmt.Printf("[Wyze] Sending AV Login\n") } if err := c.conn.AVClientStart(5 * time.Second); err != nil { return fmt.Errorf("wyze: av login failed: %w", err) } if c.verbose { fmt.Printf("[Wyze] AV Login response received\n") } return nil } func (c *Client) doKAuth() error { // Step 1: K10000 -> K10001 (Challenge) data, err := c.conn.WriteAndWaitIOCtrl(c.buildK10000(), c.matchHL(KCmdChallenge), 5*time.Second) if err != nil { return fmt.Errorf("wyze: K10001 failed: %w", err) } hlData := c.extractHL(data) challenge, status, err := c.parseK10001(hlData) if err != nil { return fmt.Errorf("wyze: K10001 parse failed: %w", err) } if c.verbose { fmt.Printf("[Wyze] K10001 challenge received, status=%d\n", status) } // Step 2: K10002 -> K10003 (Auth) data, err = c.conn.WriteAndWaitIOCtrl(c.buildK10002(challenge, status), c.matchHL(KCmdAuthResult), 5*time.Second) if err != nil { return fmt.Errorf("wyze: K10002 failed: %w", err) } hlData = c.extractHL(data) // Parse K10003 response authResp, err := c.parseK10003(hlData) if err != nil { return fmt.Errorf("wyze: K10003 parse failed: %w", err) } if c.verbose && authResp != nil { if jsonBytes, err := json.MarshalIndent(authResp, "", " "); err == nil { fmt.Printf("[Wyze] K10003 response:\n%s\n", jsonBytes) } } // Extract audio capability from cameraInfo if authResp != nil && authResp.CameraInfo != nil { if channelResult, ok := authResp.CameraInfo["channelRequestResult"].(map[string]any); ok { if audio, ok := channelResult["audio"].(string); ok { c.hasAudio = audio == "1" } else { c.hasAudio = true } } else { c.hasAudio = true } } else { c.hasAudio = true } if c.verbose { fmt.Printf("[Wyze] K10003 auth success\n") } c.hasIntercom = c.conn.HasTwoWayStreaming() if c.verbose { fmt.Printf("[Wyze] K-auth complete\n") } return nil } func (c *Client) buildK10000() []byte { json := []byte(`{"cameraInfo":{"audioEncoderList":[137,138,140]}}`) // 137=PCMU, 138=PCMA, 140=PCM b := make([]byte, 16+len(json)) copy(b, "HL") // magic b[2] = 5 // version binary.LittleEndian.PutUint16(b[4:], KCmdAuth) // 10000 binary.LittleEndian.PutUint16(b[6:], uint16(len(json))) // payload len copy(b[16:], json) return b } func (c *Client) buildK10002(challenge []byte, status byte) []byte { resp := generateChallengeResponse(challenge, c.enr, status) sessionID := make([]byte, 4) rand.Read(sessionID) b := make([]byte, 38) copy(b, "HL") // magic b[2] = 5 // version binary.LittleEndian.PutUint16(b[4:], KCmdChallengeResp) // 10002 b[6] = 22 // payload len copy(b[16:], resp[:16]) // challenge response copy(b[32:], sessionID) // random session ID b[36] = 1 // video enabled/disabled b[37] = 1 // audio enabled/disabled return b } func (c *Client) buildK10010(mediaType byte, enabled bool) []byte { b := make([]byte, 18) copy(b, "HL") // magic b[2] = 5 // version binary.LittleEndian.PutUint16(b[4:], KCmdControlChannel) // 10010 binary.LittleEndian.PutUint16(b[6:], 2) // payload len b[16] = mediaType // 1=video, 2=audio, 3=return audio b[17] = 1 // 1=enable, 2=disable if !enabled { b[17] = 2 } return b } func (c *Client) buildK10052(frameSize uint8, bitrate uint16) []byte { b := make([]byte, 22) copy(b, "HL") // magic b[2] = 5 // version binary.LittleEndian.PutUint16(b[4:], KCmdSetResolutionDB) // 10052 binary.LittleEndian.PutUint16(b[6:], 6) // payload len binary.LittleEndian.PutUint16(b[16:], bitrate) // bitrate (2 bytes) b[18] = frameSize + 1 // frame size (1 byte) // b[19] = fps, b[20:22] = zeros return b } func (c *Client) buildK10056(frameSize uint8, bitrate uint16) []byte { b := make([]byte, 21) copy(b, "HL") // magic b[2] = 5 // version binary.LittleEndian.PutUint16(b[4:], KCmdSetResolution) // 10056 binary.LittleEndian.PutUint16(b[6:], 5) // payload len b[16] = frameSize + 1 // frame size binary.LittleEndian.PutUint16(b[17:], bitrate) // bitrate // b[19:21] = FPS (0 = auto) return b } func (c *Client) parseK10001(data []byte) (challenge []byte, status byte, err error) { if c.verbose { fmt.Printf("[Wyze] parseK10001: received %d bytes\n", len(data)) } if len(data) < 33 { return nil, 0, fmt.Errorf("data too short: %d bytes", len(data)) } if data[0] != 'H' || data[1] != 'L' { return nil, 0, fmt.Errorf("invalid HL magic: %x %x", data[0], data[1]) } cmdID := binary.LittleEndian.Uint16(data[4:]) if cmdID != KCmdChallenge { return nil, 0, fmt.Errorf("expected cmdID 10001, got %d", cmdID) } status = data[16] challenge = make([]byte, 16) copy(challenge, data[17:33]) return challenge, status, nil } func (c *Client) parseK10003(data []byte) (*AuthResponse, error) { if c.verbose { fmt.Printf("[Wyze] parseK10003: received %d bytes\n", len(data)) } if len(data) < 16 { return &AuthResponse{}, nil } if data[0] != 'H' || data[1] != 'L' { return &AuthResponse{}, nil } cmdID := binary.LittleEndian.Uint16(data[4:]) textLen := binary.LittleEndian.Uint16(data[6:]) if cmdID != KCmdAuthResult { return &AuthResponse{}, nil } if len(data) > 16 && textLen > 0 { jsonData := data[16:] for i := range jsonData { if jsonData[i] == '{' { var resp AuthResponse if err := json.Unmarshal(jsonData[i:], &resp); err == nil { if c.verbose { fmt.Printf("[Wyze] parseK10003: parsed JSON\n") } return &resp, nil } break } } } return &AuthResponse{}, nil } func (c *Client) useDoorbellResolution() bool { switch c.model { case "WYZEDB3", "WVOD1", "HL_WCO2", "WYZEC1": return true } return false } func (c *Client) hdFrameSize() uint8 { if c.isFloodlight() { return FrameSizeFloodlight } if c.is2K() { return FrameSize2K } return FrameSize1080P } func (c *Client) is2K() bool { switch c.model { case "HL_CAM3P", "HL_PANP", "HL_CAM4", "HL_DB2", "HL_CFL2": return true } return false } func (c *Client) isFloodlight() bool { return c.model == "HL_CFL2" } func (c *Client) matchHL(expectCmd uint16) func([]byte) bool { return func(data []byte) bool { hlData := c.extractHL(data) if hlData == nil { return false } cmd, _, ok := tutk.ParseHL(hlData) return ok && cmd == expectCmd } } func (c *Client) extractHL(data []byte) []byte { // Try offset 32 (magicIOCtrl, protoVersion) if hlData := tutk.FindHL(data, 32); hlData != nil { return hlData } // Try offset 36 (magicChannelMsg) if len(data) >= 36 && data[16] == 0x00 { return tutk.FindHL(data, 36) } return nil } const ( statusDefault byte = 1 statusENR16 byte = 3 statusENR32 byte = 6 ) func generateChallengeResponse(challengeBytes []byte, enr string, status byte) []byte { var secretKey []byte switch status { case statusDefault: secretKey = []byte("FFFFFFFFFFFFFFFF") case statusENR16: if len(enr) >= 16 { secretKey = []byte(enr[:16]) } else { secretKey = make([]byte, 16) copy(secretKey, enr) } case statusENR32: if len(enr) >= 16 { firstKey := []byte(enr[:16]) challengeBytes = tutk.XXTEADecryptVar(challengeBytes, firstKey) } if len(enr) >= 32 { secretKey = []byte(enr[16:32]) } else if len(enr) > 16 { secretKey = make([]byte, 16) copy(secretKey, []byte(enr[16:])) } else { secretKey = []byte("FFFFFFFFFFFFFFFF") } default: secretKey = []byte("FFFFFFFFFFFFFFFF") } return tutk.XXTEADecryptVar(challengeBytes, secretKey) } ================================================ FILE: pkg/wyze/cloud.go ================================================ package wyze import ( "crypto/md5" "encoding/hex" "encoding/json" "errors" "fmt" "io" "net/http" "strings" "time" "github.com/AlexxIT/go2rtc/pkg/core" ) const ( baseURLAuth = "https://auth-prod.api.wyze.com" baseURLAPI = "https://api.wyzecam.com" appName = "com.hualai.WyzeCam" appVersion = "2.50.0" ) type Cloud struct { client *http.Client apiKey string keyID string accessToken string phoneID string cameras []*Camera } type Camera struct { MAC string `json:"mac"` P2PID string `json:"p2p_id"` ENR string `json:"enr"` IP string `json:"ip"` Nickname string `json:"nickname"` ProductModel string `json:"product_model"` ProductType string `json:"product_type"` DTLS int `json:"dtls"` FirmwareVer string `json:"firmware_ver"` IsOnline bool `json:"is_online"` } type deviceListResponse struct { Code string `json:"code"` Msg string `json:"msg"` Data struct { DeviceList []deviceInfo `json:"device_list"` } `json:"data"` } type deviceInfo struct { MAC string `json:"mac"` ENR string `json:"enr"` Nickname string `json:"nickname"` ProductModel string `json:"product_model"` ProductType string `json:"product_type"` FirmwareVer string `json:"firmware_ver"` ConnState int `json:"conn_state"` DeviceParams deviceParams `json:"device_params"` } type deviceParams struct { P2PID string `json:"p2p_id"` P2PType int `json:"p2p_type"` IP string `json:"ip"` DTLS int `json:"dtls"` } type p2pInfoResponse struct { Code string `json:"code"` Msg string `json:"msg"` Data map[string]any `json:"data"` } type loginResponse struct { AccessToken string `json:"access_token"` RefreshToken string `json:"refresh_token"` UserID string `json:"user_id"` MFAOptions []string `json:"mfa_options"` SMSSessionID string `json:"sms_session_id"` EmailSessionID string `json:"email_session_id"` } func NewCloud(apiKey, keyID string) *Cloud { return &Cloud{ client: &http.Client{Timeout: 30 * time.Second}, phoneID: generatePhoneID(), apiKey: apiKey, keyID: keyID, } } func (c *Cloud) Login(email, password string) error { payload := map[string]string{ "email": strings.TrimSpace(email), "password": hashPassword(password), } jsonData, _ := json.Marshal(payload) req, err := http.NewRequest("POST", baseURLAuth+"/api/user/login", strings.NewReader(string(jsonData))) if err != nil { return err } req.Header.Set("Content-Type", "application/json") req.Header.Set("Apikey", c.apiKey) req.Header.Set("Keyid", c.keyID) req.Header.Set("User-Agent", "go2rtc") resp, err := c.client.Do(req) if err != nil { return err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return err } var errResp apiError _ = json.Unmarshal(body, &errResp) if errResp.hasError() { return fmt.Errorf("wyze: login failed (code %s): %s", errResp.code(), errResp.message()) } var result loginResponse if err := json.Unmarshal(body, &result); err != nil { return fmt.Errorf("wyze: failed to parse login response: %w", err) } if len(result.MFAOptions) > 0 { return &AuthError{ Message: "MFA required", NeedsMFA: true, MFAType: strings.Join(result.MFAOptions, ","), } } if result.AccessToken == "" { return errors.New("wyze: no access token in response") } c.accessToken = result.AccessToken return nil } func (c *Cloud) GetCameraList() ([]*Camera, error) { payload := map[string]any{ "access_token": c.accessToken, "phone_id": c.phoneID, "app_name": appName, "app_ver": appName + "___" + appVersion, "app_version": appVersion, "phone_system_type": 1, "sc": "9f275790cab94a72bd206c8876429f3c", "sv": "9d74946e652647e9b6c9d59326aef104", "ts": time.Now().UnixMilli(), } jsonData, _ := json.Marshal(payload) req, err := http.NewRequest("POST", baseURLAPI+"/app/v2/home_page/get_object_list", strings.NewReader(string(jsonData))) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json") resp, err := c.client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } var result deviceListResponse if err := json.Unmarshal(body, &result); err != nil { return nil, fmt.Errorf("wyze: failed to parse device list: %w", err) } if result.Code != "1" { return nil, fmt.Errorf("wyze: API error: %s - %s", result.Code, result.Msg) } c.cameras = nil for _, dev := range result.Data.DeviceList { if dev.ProductType != "Camera" { continue } if dev.DeviceParams.IP == "" { continue // skip cameras without IP (gwell protocol) } c.cameras = append(c.cameras, &Camera{ MAC: dev.MAC, P2PID: dev.DeviceParams.P2PID, ENR: dev.ENR, IP: dev.DeviceParams.IP, Nickname: dev.Nickname, ProductModel: dev.ProductModel, ProductType: dev.ProductType, DTLS: dev.DeviceParams.DTLS, FirmwareVer: dev.FirmwareVer, IsOnline: dev.ConnState == 1, }) } return c.cameras, nil } func (c *Cloud) GetCamera(id string) (*Camera, error) { if c.cameras == nil { if _, err := c.GetCameraList(); err != nil { return nil, err } } id = strings.ToUpper(id) for _, cam := range c.cameras { if strings.ToUpper(cam.MAC) == id || strings.EqualFold(cam.Nickname, id) { return cam, nil } } return nil, fmt.Errorf("wyze: camera not found: %s", id) } func (c *Cloud) GetP2PInfo(mac string) (map[string]any, error) { payload := map[string]any{ "access_token": c.accessToken, "phone_id": c.phoneID, "device_mac": mac, "app_name": appName, "app_ver": appName + "___" + appVersion, "app_version": appVersion, "phone_system_type": 1, "sc": "9f275790cab94a72bd206c8876429f3c", "sv": "9d74946e652647e9b6c9d59326aef104", "ts": time.Now().UnixMilli(), } jsonData, _ := json.Marshal(payload) req, err := http.NewRequest("POST", baseURLAPI+"/app/v2/device/get_iotc_info", strings.NewReader(string(jsonData))) if err != nil { return nil, err } req.Header.Set("Content-Type", "application/json") resp, err := c.client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() body, err := io.ReadAll(resp.Body) if err != nil { return nil, err } var result p2pInfoResponse if err := json.Unmarshal(body, &result); err != nil { return nil, err } if result.Code != "1" { return nil, fmt.Errorf("wyze: API error: %s - %s", result.Code, result.Msg) } return result.Data, nil } type apiError struct { Code string `json:"code"` ErrorCode int `json:"errorCode"` Msg string `json:"msg"` Description string `json:"description"` } func (e *apiError) hasError() bool { if e.Code == "1" || e.Code == "0" { return false } if e.Code == "" && e.ErrorCode == 0 { return false } return e.Code != "" || e.ErrorCode != 0 } func (e *apiError) message() string { if e.Msg != "" { return e.Msg } return e.Description } func (e *apiError) code() string { if e.Code != "" { return e.Code } return fmt.Sprintf("%d", e.ErrorCode) } type AuthError struct { Message string `json:"message"` NeedsMFA bool `json:"needs_mfa,omitempty"` MFAType string `json:"mfa_type,omitempty"` } func (e *AuthError) Error() string { return e.Message } func generatePhoneID() string { return core.RandString(16, 16) // 16 hex chars } func hashPassword(password string) string { encoded := strings.TrimSpace(password) if strings.HasPrefix(strings.ToLower(encoded), "md5:") { return encoded[4:] } for range 3 { hash := md5.Sum([]byte(encoded)) encoded = hex.EncodeToString(hash[:]) } return encoded } ================================================ FILE: pkg/wyze/producer.go ================================================ package wyze import ( "fmt" "net/url" "time" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/AlexxIT/go2rtc/pkg/tutk" "github.com/pion/rtp" ) type Producer struct { core.Connection client *Client model string } func NewProducer(rawURL string) (*Producer, error) { client, err := Dial(rawURL) if err != nil { return nil, err } u, _ := url.Parse(rawURL) query := u.Query() // 0 = HD (default), 1 = SD/360P, 2 = 720P, 3 = 2K, 4 = Floodlight var quality byte switch s := query.Get("subtype"); s { case "", "hd": quality = 0 case "sd": quality = FrameSize360P default: quality = core.ParseByte(s) } medias, err := probe(client, quality) if err != nil { _ = client.Close() return nil, err } prod := &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "wyze", Protocol: client.Protocol(), RemoteAddr: client.RemoteAddr().String(), Source: rawURL, Medias: medias, Transport: client, }, client: client, model: query.Get("model"), } return prod, nil } func (p *Producer) Start() error { for { if p.client.verbose { fmt.Println("[Wyze] Reading packet...") } _ = p.client.SetDeadline(time.Now().Add(core.ConnDeadline)) pkt, err := p.client.ReadPacket() if err != nil { return err } if pkt == nil { continue } var name string var pkt2 *core.Packet switch codecID := pkt.Codec; codecID { case tutk.CodecH264: name = core.CodecH264 pkt2 = &core.Packet{ Header: rtp.Header{SequenceNumber: uint16(pkt.FrameNo), Timestamp: pkt.Timestamp}, Payload: annexb.EncodeToAVCC(pkt.Payload), } case tutk.CodecH265: name = core.CodecH265 pkt2 = &core.Packet{ Header: rtp.Header{SequenceNumber: uint16(pkt.FrameNo), Timestamp: pkt.Timestamp}, Payload: annexb.EncodeToAVCC(pkt.Payload), } case tutk.CodecPCMU: name = core.CodecPCMU pkt2 = &core.Packet{ Header: rtp.Header{Version: 2, Marker: true, SequenceNumber: uint16(pkt.FrameNo), Timestamp: pkt.Timestamp}, Payload: pkt.Payload, } case tutk.CodecPCMA: name = core.CodecPCMA pkt2 = &core.Packet{ Header: rtp.Header{Version: 2, Marker: true, SequenceNumber: uint16(pkt.FrameNo), Timestamp: pkt.Timestamp}, Payload: pkt.Payload, } case tutk.CodecAACADTS, tutk.CodecAACAlt, tutk.CodecAACRaw, tutk.CodecAACLATM: name = core.CodecAAC payload := pkt.Payload if aac.IsADTS(payload) { payload = payload[aac.ADTSHeaderLen(payload):] } pkt2 = &core.Packet{ Header: rtp.Header{Version: aac.RTPPacketVersionAAC, Marker: true, SequenceNumber: uint16(pkt.FrameNo), Timestamp: pkt.Timestamp}, Payload: payload, } case tutk.CodecOpus: name = core.CodecOpus pkt2 = &core.Packet{ Header: rtp.Header{Version: 2, Marker: true, SequenceNumber: uint16(pkt.FrameNo), Timestamp: pkt.Timestamp}, Payload: pkt.Payload, } case tutk.CodecPCML: name = core.CodecPCML pkt2 = &core.Packet{ Header: rtp.Header{Version: 2, Marker: true, SequenceNumber: uint16(pkt.FrameNo), Timestamp: pkt.Timestamp}, Payload: pkt.Payload, } case tutk.CodecMP3: name = core.CodecMP3 pkt2 = &core.Packet{ Header: rtp.Header{Version: 2, Marker: true, SequenceNumber: uint16(pkt.FrameNo), Timestamp: pkt.Timestamp}, Payload: pkt.Payload, } case tutk.CodecMJPEG: name = core.CodecJPEG pkt2 = &core.Packet{ Header: rtp.Header{SequenceNumber: uint16(pkt.FrameNo), Timestamp: pkt.Timestamp}, Payload: pkt.Payload, } default: continue } for _, recv := range p.Receivers { if recv.Codec.Name == name { recv.WriteRTP(pkt2) break } } } } func probe(client *Client, quality byte) ([]*core.Media, error) { client.SetResolution(quality) client.SetDeadline(time.Now().Add(core.ProbeTimeout)) var vcodec, acodec *core.Codec var tutkAudioCodec byte for { if client.verbose { fmt.Println("[Wyze] Probing for codecs...") } pkt, err := client.ReadPacket() if err != nil { return nil, fmt.Errorf("wyze: probe: %w", err) } if pkt == nil || len(pkt.Payload) < 5 { continue } switch pkt.Codec { case tutk.CodecH264: if vcodec == nil { buf := annexb.EncodeToAVCC(pkt.Payload) if len(buf) >= 5 && h264.NALUType(buf) == h264.NALUTypeSPS { vcodec = h264.AVCCToCodec(buf) } } case tutk.CodecH265: if vcodec == nil { buf := annexb.EncodeToAVCC(pkt.Payload) if len(buf) >= 5 && h265.NALUType(buf) == h265.NALUTypeVPS { vcodec = h265.AVCCToCodec(buf) } } case tutk.CodecPCMU: if acodec == nil { acodec = &core.Codec{Name: core.CodecPCMU, ClockRate: pkt.SampleRate, Channels: pkt.Channels} tutkAudioCodec = pkt.Codec } case tutk.CodecPCMA: if acodec == nil { acodec = &core.Codec{Name: core.CodecPCMA, ClockRate: pkt.SampleRate, Channels: pkt.Channels} tutkAudioCodec = pkt.Codec } case tutk.CodecAACAlt, tutk.CodecAACADTS, tutk.CodecAACRaw, tutk.CodecAACLATM: if acodec == nil { config := aac.EncodeConfig(aac.TypeAACLC, pkt.SampleRate, pkt.Channels, false) acodec = aac.ConfigToCodec(config) tutkAudioCodec = pkt.Codec } case tutk.CodecOpus: if acodec == nil { acodec = &core.Codec{Name: core.CodecOpus, ClockRate: 48000, Channels: 2} tutkAudioCodec = pkt.Codec } case tutk.CodecPCML: if acodec == nil { acodec = &core.Codec{Name: core.CodecPCML, ClockRate: pkt.SampleRate, Channels: pkt.Channels} tutkAudioCodec = pkt.Codec } case tutk.CodecMP3: if acodec == nil { acodec = &core.Codec{Name: core.CodecMP3, ClockRate: pkt.SampleRate, Channels: pkt.Channels} tutkAudioCodec = pkt.Codec } case tutk.CodecMJPEG: if vcodec == nil { vcodec = &core.Codec{Name: core.CodecJPEG, ClockRate: 90000, PayloadType: core.PayloadTypeRAW} } } if vcodec != nil && (acodec != nil || !client.SupportsAudio()) { break } } _ = client.SetDeadline(time.Time{}) medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{vcodec}, }, } if acodec != nil { medias = append(medias, &core.Media{ Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{acodec}, }) if client.SupportsIntercom() { client.SetBackchannelCodec(tutkAudioCodec, acodec.ClockRate, uint8(acodec.Channels)) medias = append(medias, &core.Media{ Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{acodec.Clone()}, }) } } if client.verbose { fmt.Printf("[Wyze] Probed codecs: video=%s audio=%s\n", vcodec.Name, acodec.Name) if client.SupportsIntercom() { fmt.Printf("[Wyze] Intercom supported, audio send codec=%s\n", acodec.Name) } } return medias, nil } ================================================ FILE: pkg/xiaomi/cloud.go ================================================ package xiaomi import ( "bytes" "crypto/md5" "crypto/rand" "crypto/rc4" "crypto/sha1" "crypto/sha256" "encoding/base64" "encoding/binary" "encoding/json" "errors" "fmt" "io" "net/http" "net/url" "strconv" "strings" "time" "github.com/AlexxIT/go2rtc/pkg/core" ) type Cloud struct { client *http.Client sid string cookies string // for auth ssecurity []byte // for encryption userID string passToken string auth map[string]string } func NewCloud(sid string) *Cloud { return &Cloud{ client: &http.Client{Timeout: 15 * time.Second}, sid: sid, } } func (c *Cloud) Login(username, password string) error { res, err := c.client.Get("https://account.xiaomi.com/pass/serviceLogin?_json=true&sid=" + c.sid) if err != nil { return err } var v1 struct { Qs string `json:"qs"` Sign string `json:"_sign"` Sid string `json:"sid"` Callback string `json:"callback"` } if _, err = readLoginResponse(res.Body, &v1); err != nil { return err } hash := fmt.Sprintf("%X", md5.Sum([]byte(password))) form := url.Values{ "_json": {"true"}, "hash": {hash}, "sid": {v1.Sid}, "callback": {v1.Callback}, "_sign": {v1.Sign}, "qs": {v1.Qs}, "user": {username}, } cookies := "deviceId=" + core.RandString(16, 62) // login after captcha if c.auth != nil && c.auth["captcha_code"] != "" { form.Set("captCode", c.auth["captcha_code"]) cookies += "; ick=" + c.auth["ick"] } req := Request{ Method: "POST", URL: "https://account.xiaomi.com/pass/serviceLoginAuth2", Body: form, RawCookies: cookies, }.Encode() res, err = c.client.Do(req) if err != nil { return err } var v2 struct { Ssecurity []byte `json:"ssecurity"` PassToken string `json:"passToken"` Location string `json:"location"` CaptchaURL string `json:"captchaURL"` NotificationURL string `json:"notificationUrl"` } body, err := readLoginResponse(res.Body, &v2) if err != nil { return err } // save auth for two-step verification c.auth = map[string]string{ "username": username, "password": password, } if v2.CaptchaURL != "" { return c.getCaptcha(v2.CaptchaURL) } if v2.NotificationURL != "" { return c.authStart(v2.NotificationURL) } if v2.Location == "" { return fmt.Errorf("xiaomi: %s", body) } c.auth = nil c.ssecurity = v2.Ssecurity c.passToken = v2.PassToken return c.finishAuth(v2.Location) } func (c *Cloud) LoginWithCaptcha(captcha string) error { if c.auth == nil || c.auth["ick"] == "" { panic("wrong login step") } c.auth["captcha_code"] = captcha // check if captcha after verify if c.auth["flag"] != "" { return c.sendTicket() } return c.Login(c.auth["username"], c.auth["password"]) } func (c *Cloud) LoginWithVerify(ticket string) error { if c.auth == nil || c.auth["flag"] == "" { panic("wrong login step") } req := Request{ Method: "POST", URL: "https://account.xiaomi.com/identity/auth/verify" + c.verifyName(), RawParams: "_flag" + c.auth["flag"] + "&ticket=" + ticket + "&trust=false&_json=true", RawCookies: "identity_session=" + c.auth["identity_session"], }.Encode() res, err := c.client.Do(req) if err != nil { return err } var v1 struct { Location string `json:"location"` } body, err := readLoginResponse(res.Body, &v1) if err != nil { return err } if v1.Location == "" { return fmt.Errorf("xiaomi: %s", body) } return c.finishAuth(v1.Location) } func (c *Cloud) getCaptcha(captchaURL string) error { res, err := c.client.Get("https://account.xiaomi.com" + captchaURL) if err != nil { return err } defer res.Body.Close() body, err := io.ReadAll(res.Body) if err != nil { return err } c.auth["ick"] = findCookie(res, "ick") return &LoginError{ Captcha: body, } } func (c *Cloud) authStart(notificationURL string) error { rawURL := strings.Replace(notificationURL, "/fe/service/identity/authStart", "/identity/list", 1) res, err := c.client.Get(rawURL) if err != nil { return err } var v1 struct { Code int `json:"code"` Flag int `json:"flag"` } if _, err = readLoginResponse(res.Body, &v1); err != nil { return err } c.auth["flag"] = strconv.Itoa(v1.Flag) c.auth["identity_session"] = findCookie(res, "identity_session") return c.sendTicket() } func findCookie(res *http.Response, name string) string { for _, cookie := range res.Cookies() { if cookie.Name == name { return cookie.Value } } return "" } func (c *Cloud) verifyName() string { switch c.auth["flag"] { case "4": return "Phone" case "8": return "Email" } return "" } func (c *Cloud) sendTicket() error { name := c.verifyName() cookies := "identity_session=" + c.auth["identity_session"] req := Request{ URL: "https://account.xiaomi.com/identity/auth/verify" + name, RawParams: "_flag=" + c.auth["flag"] + "&_json=true", RawCookies: cookies, }.Encode() res, err := c.client.Do(req) if err != nil { return err } var v1 struct { Code int `json:"code"` MaskedPhone string `json:"maskedPhone"` MaskedEmail string `json:"maskedEmail"` } if _, err = readLoginResponse(res.Body, &v1); err != nil { return err } // verify after captcha captCode := c.auth["captcha_code"] if captCode != "" { cookies += "; ick=" + c.auth["ick"] } form := url.Values{ "_json": {"true"}, "icode": {captCode}, "retry": {"0"}, } req = Request{ Method: "POST", URL: "https://account.xiaomi.com/identity/auth/send" + name + "Ticket", Body: form, RawCookies: cookies, }.Encode() res, err = c.client.Do(req) if err != nil { return err } var v2 struct { Code int `json:"code"` CaptchaURL string `json:"captchaURL"` } body, err := readLoginResponse(res.Body, &v2) if err != nil { return err } if v2.CaptchaURL != "" { return c.getCaptcha(v2.CaptchaURL) } if v2.Code != 0 { return fmt.Errorf("xiaomi: %s", body) } return &LoginError{ VerifyPhone: v1.MaskedPhone, VerifyEmail: v1.MaskedEmail, } } type LoginError struct { Captcha []byte `json:"captcha,omitempty"` VerifyPhone string `json:"verify_phone,omitempty"` VerifyEmail string `json:"verify_email,omitempty"` } func (l *LoginError) Error() string { return "" } func (c *Cloud) finishAuth(location string) error { res, err := c.client.Get(location) if err != nil { return err } defer res.Body.Close() // LoginWithVerify // - userId, cUserId, serviceToken from cookies // - passToken from redirect cookies // - ssecurity from extra header // LoginWithToken // - userId, cUserId, serviceToken from cookies var cUserId, serviceToken string for res != nil { for _, cookie := range res.Cookies() { switch cookie.Name { case "userId": c.userID = cookie.Value case "cUserId": cUserId = cookie.Value case "serviceToken": serviceToken = cookie.Value case "passToken": c.passToken = cookie.Value } } if s := res.Header.Get("Extension-Pragma"); s != "" { var v1 struct { Ssecurity []byte `json:"ssecurity"` } if err = json.Unmarshal([]byte(s), &v1); err != nil { return err } c.ssecurity = v1.Ssecurity } res = res.Request.Response } c.cookies = fmt.Sprintf("userId=%s; cUserId=%s; serviceToken=%s", c.userID, cUserId, serviceToken) return nil } func (c *Cloud) LoginWithToken(userID, passToken string) error { req, err := http.NewRequest("GET", "https://account.xiaomi.com/pass/serviceLogin?_json=true&sid="+c.sid, nil) if err != nil { return err } req.Header.Set("Cookie", fmt.Sprintf("userId=%s; passToken=%s", userID, passToken)) res, err := c.client.Do(req) if err != nil { return err } var v1 struct { Ssecurity []byte `json:"ssecurity"` PassToken string `json:"passToken"` Location string `json:"location"` } if _, err = readLoginResponse(res.Body, &v1); err != nil { return err } c.ssecurity = v1.Ssecurity c.passToken = v1.PassToken return c.finishAuth(v1.Location) } func (c *Cloud) UserToken() (string, string) { return c.userID, c.passToken } func (c *Cloud) Request(baseURL, apiURL, params string, headers map[string]string) ([]byte, error) { form := url.Values{"data": {params}} nonce := genNonce() signedNonce := genSignedNonce(c.ssecurity, nonce) // 1. gen hash for data param form.Set("rc4_hash__", genSignature64("POST", apiURL, form, signedNonce)) // 2. encrypt data and hash params for _, v := range form { ciphertext, err := crypt(signedNonce, []byte(v[0])) if err != nil { return nil, err } v[0] = base64.StdEncoding.EncodeToString(ciphertext) } // 3. add signature for encrypted data and hash params form.Set("signature", genSignature64("POST", apiURL, form, signedNonce)) // 4. add nonce form.Set("_nonce", base64.StdEncoding.EncodeToString(nonce)) req, err := http.NewRequest("POST", baseURL+apiURL, strings.NewReader(form.Encode())) if err != nil { return nil, err } req.Header.Set("Cookie", c.cookies) req.Header.Set("Content-Type", "application/x-www-form-urlencoded") for k, v := range headers { req.Header.Set(k, v) } res, err := c.client.Do(req) if err != nil { return nil, err } defer res.Body.Close() if res.StatusCode != http.StatusOK { return nil, errors.New(res.Status) } body, err := io.ReadAll(res.Body) if err != nil { return nil, err } ciphertext, err := base64.StdEncoding.DecodeString(string(body)) if err != nil { return nil, err } plaintext, err := crypt(signedNonce, ciphertext) if err != nil { return nil, err } var res1 struct { Code int `json:"code"` Message string `json:"message"` Result json.RawMessage `json:"result"` } if err = json.Unmarshal(plaintext, &res1); err != nil { return nil, err } if res1.Code != 0 { return nil, errors.New("xiaomi: " + res1.Message) } return res1.Result, nil } func readLoginResponse(rc io.ReadCloser, v any) ([]byte, error) { defer rc.Close() body, err := io.ReadAll(rc) if err != nil { return nil, err } body, ok := bytes.CutPrefix(body, []byte("&&&START&&&")) if !ok { return nil, fmt.Errorf("xiaomi: %s", body) } return body, json.Unmarshal(body, &v) } func genNonce() []byte { ts := time.Now().Unix() / 60 nonce := make([]byte, 12) _, _ = rand.Read(nonce[:8]) binary.BigEndian.PutUint32(nonce[8:], uint32(ts)) return nonce } func genSignedNonce(ssecurity, nonce []byte) []byte { hasher := sha256.New() hasher.Write(ssecurity) hasher.Write(nonce) return hasher.Sum(nil) } func crypt(key, plaintext []byte) ([]byte, error) { cipher, err := rc4.NewCipher(key) if err != nil { return nil, err } tmp := make([]byte, 1024) cipher.XORKeyStream(tmp, tmp) ciphertext := make([]byte, len(plaintext)) cipher.XORKeyStream(ciphertext, plaintext) return ciphertext, nil } func genSignature64(method, path string, values url.Values, signedNonce []byte) string { s := method + "&" + path + "&data=" + values.Get("data") if values.Has("rc4_hash__") { s += "&rc4_hash__=" + values.Get("rc4_hash__") } s += "&" + base64.StdEncoding.EncodeToString(signedNonce) hasher := sha1.New() hasher.Write([]byte(s)) signature := hasher.Sum(nil) return base64.StdEncoding.EncodeToString(signature) } type Request struct { Method string URL string RawParams string Body url.Values Headers url.Values RawCookies string } func (r Request) Encode() *http.Request { if r.RawParams != "" { r.URL += "?" + r.RawParams } var body io.Reader if r.Body != nil { body = strings.NewReader(r.Body.Encode()) } req, err := http.NewRequest(r.Method, r.URL, body) if err != nil { return nil } if r.Headers != nil { req.Header = http.Header(r.Headers) } if r.Body != nil { req.Header.Set("Content-Type", "application/x-www-form-urlencoded") } if r.RawCookies != "" { req.Header.Set("Cookie", r.RawCookies) } return req } ================================================ FILE: pkg/xiaomi/crypto/crypto.go ================================================ package crypto import ( "crypto/rand" "encoding/hex" "golang.org/x/crypto/chacha20" "golang.org/x/crypto/nacl/box" ) func GenerateKey() ([]byte, []byte, error) { public, private, err := box.GenerateKey(rand.Reader) if err != nil { return nil, nil, err } return public[:], private[:], err } func CalcSharedKey(devicePublicB64, clientPrivateB64 string) ([]byte, error) { var sharedKey, publicKey, privateKey [32]byte if _, err := hex.Decode(publicKey[:], []byte(devicePublicB64)); err != nil { return nil, err } if _, err := hex.Decode(privateKey[:], []byte(clientPrivateB64)); err != nil { return nil, err } box.Precompute(&sharedKey, &publicKey, &privateKey) return sharedKey[:], nil } func Encode(src, key32 []byte) ([]byte, error) { dst := make([]byte, len(src)+8) if _, err := rand.Read(dst[:8]); err != nil { return nil, err } nonce12 := make([]byte, 12) copy(nonce12[4:], dst[:8]) c, err := chacha20.NewUnauthenticatedCipher(key32, nonce12) if err != nil { return nil, err } c.XORKeyStream(dst[8:], src) return dst, nil } func Decode(src, key32 []byte) ([]byte, error) { return DecodeNonce(src[8:], src[:8], key32) } func DecodeNonce(src, nonce8, key32 []byte) ([]byte, error) { nonce12 := make([]byte, 12) copy(nonce12[4:], nonce8) c, err := chacha20.NewUnauthenticatedCipher(key32, nonce12) if err != nil { return nil, err } dst := make([]byte, len(src)) c.XORKeyStream(dst, src) return dst, nil } ================================================ FILE: pkg/xiaomi/legacy/client.go ================================================ package legacy import ( "encoding/binary" "errors" "fmt" "net/url" "github.com/AlexxIT/go2rtc/pkg/tutk" "github.com/AlexxIT/go2rtc/pkg/xiaomi/crypto" ) func NewClient(rawURL string) (*Client, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } query := u.Query() model := query.Get("model") var username, password string var key []byte if query.Has("sign") { // Legacy with encryption key, err = crypto.CalcSharedKey(query.Get("device_public"), query.Get("client_private")) if err != nil { return nil, err } username = fmt.Sprintf( `{"public_key":"%s","sign":"%s","account":"admin"}`, query.Get("client_public"), query.Get("sign"), ) } else if model == ModelMijia || model == ModelXiaobai { username = "admin" password = query.Get("password") } else if model == ModelDafang || model == ModelXiaofang { username = "admin" } else { return nil, fmt.Errorf("xiaomi: unsupported model: %s", model) } conn, err := tutk.Dial(u.Host, query.Get("uid"), username, password) if err != nil { return nil, err } if model == ModelDafang || model == ModelXiaofang { err = xiaofangLogin(conn, query.Get("password")) if err != nil { _ = conn.Close() return nil, err } } c := &Client{ Conn: conn, key: key, model: model, } return c, nil } func xiaofangLogin(conn *tutk.Conn, password string) error { data := tutk.ICAM(0x0400be) // ask login if err := conn.WriteCommand(0x0100, data); err != nil { return err } _, data, err := conn.ReadCommand() // login request if err != nil { return err } enc := data[24:] // data[23] == 3 tutk.XXTEADecrypt(enc, enc, []byte(password)) enc = append(enc, 0, 0, 0, 0, 1, 1, 1) data = tutk.ICAM(0x0400c0, enc...) // login response if err = conn.WriteCommand(0x0100, data); err != nil { return err } _, data, err = conn.ReadCommand() return err } type Client struct { *tutk.Conn key []byte model string } func (c *Client) Version() string { return fmt.Sprintf("%s (%s)", c.Conn.Version(), c.model) } func (c *Client) ReadPacket() (hdr, payload []byte, err error) { hdr, payload, err = c.Conn.ReadPacket() if err != nil { return } if c.key != nil { if c.model == ModelAqaraG2 && hdr[0] == tutk.CodecH265 { payload, err = DecodeVideo(payload, c.key) } else { // ModelAqaraG2: audio AAC // ModelIMILABA1: video HEVC, audio PCMA payload, err = crypto.Decode(payload, c.key) } } return } const ( cmdVideoStart = 0x01ff cmdVideoStop = 0x02ff cmdAudioStart = 0x0300 cmdAudioStop = 0x0301 cmdStreamCtrlReq = 0x0320 ) func (c *Client) WriteCommandJSON(ctrlType uint32, format string, a ...any) error { if len(a) > 0 { format = fmt.Sprintf(format, a...) } return c.WriteCommand(ctrlType, []byte(format)) } func (c *Client) StartMedia(video, audio string) error { switch c.model { case ModelAqaraG2: // 0 - 1920x1080, 1 - 1280x720, 2 - ? switch video { case "", "fhd": video = "0" case "hd": video = "1" case "sd": video = "2" } return errors.Join( c.WriteCommandJSON(cmdVideoStart, `{}`), c.WriteCommandJSON(0x0605, `{"channel":%s}`, video), c.WriteCommandJSON(0x0704, `{}`), // don't know why ) case ModelIMILABA1, ModelMijia: // 0 - auto, 1 - low, 3 - hd switch video { case "", "hd": video = "3" case "sd": video = "1" // 2 is also low quality case "auto": video = "0" } // quality after start return errors.Join( c.WriteCommandJSON(cmdAudioStart, `{}`), c.WriteCommandJSON(cmdVideoStart, `{}`), c.WriteCommandJSON(cmdStreamCtrlReq, `{"videoquality":%s}`, video), ) case ModelXiaobai: // 00030000 7b7d audio on // 01030000 7b7d audio off // 20030000 0000000001000000 fhd (1920x1080) // 20030000 0000000002000000 hd (1280x720) // 20030000 0000000004000000 low (640x360) // 20030000 00000000ff000000 auto (1920x1080) // ff010000 7b7d video tart // ff020000 7b7d video stop var b byte switch video { case "", "fhd": b = 1 case "hd": b = 2 case "sd": b = 4 case "auto": b = 0xff } // quality before start return errors.Join( c.WriteCommandJSON(cmdAudioStart, `{}`), c.WriteCommand(cmdStreamCtrlReq, []byte{0, 0, 0, 0, b, 0, 0, 0}), c.WriteCommandJSON(cmdVideoStart, `{}`), ) case ModelDafang, ModelXiaofang: // 00010000 4943414d 95010400000000000000000600000000000000d20400005a07 - 90k bitrate // 00010000 4943414d 95010400000000000000000600000000000000d20400001e07 - 30k bitrate //var b byte //switch video { //case "", "hd": // b = 0x5a // bitrate 90k //case "sd": // b = 0x1e // bitrate 30k //} //data := tutk.ICAM(0x040195, 0xd2, 4, 0, 0, b, 7) //if err := c.WriteCommand(0x100, data); err != nil { // return err //} return nil } return fmt.Errorf("xiaomi: unsupported model: %s", c.model) } func (c *Client) StopMedia() error { return errors.Join( c.WriteCommandJSON(cmdVideoStop, `{}`), c.WriteCommand(cmdVideoStop, make([]byte, 8)), ) } func DecodeVideo(data, key []byte) ([]byte, error) { if string(data[:4]) == "\x00\x00\x00\x01" || data[8] == 0 { return data, nil } if data[8] != 1 { // Support could be added, but I haven't seen such cameras. return nil, fmt.Errorf("xiaomi: unsupported encryption") } nonce8 := data[:8] i1 := binary.LittleEndian.Uint32(data[9:]) i2 := binary.LittleEndian.Uint32(data[13:]) data = data[17:] src := data[i1 : i1+i2] for i := 32; i+16 < len(src); i += 160 { dst, err := crypto.DecodeNonce(src[i:i+16], nonce8, key) if err != nil { return nil, err } copy(src[i:], dst) // copy result in same place } return data, nil } const ( ModelAqaraG2 = "lumi.camera.gwagl01" ModelIMILABA1 = "chuangmi.camera.ipc019e" ModelLoockV1 = "loock.cateye.v01" ModelXiaobai = "chuangmi.camera.xiaobai" ModelXiaofang = "isa.camera.isc5" // ModelMijia support miss format for new fw and legacy format for old fw ModelMijia = "chuangmi.camera.v2" // ModelDafang support miss format for new fw and legacy format for old fw ModelDafang = "isa.camera.df3" ) func Supported(model string) bool { switch model { case ModelAqaraG2, ModelIMILABA1, ModelLoockV1, ModelXiaobai, ModelXiaofang: return true } return false } ================================================ FILE: pkg/xiaomi/legacy/producer.go ================================================ package legacy import ( "net/url" "time" "github.com/AlexxIT/go2rtc/pkg/aac" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/AlexxIT/go2rtc/pkg/tutk" "github.com/pion/rtp" ) func Dial(rawURL string) (*Producer, error) { client, err := NewClient(rawURL) if err != nil { return nil, err } u, _ := url.Parse(rawURL) query := u.Query() err = client.StartMedia(query.Get("subtype"), "") if err != nil { _ = client.Close() return nil, err } medias, err := probe(client) if err != nil { _ = client.Close() return nil, err } c := &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "xiaomi/legacy", Protocol: "tutk+udp", RemoteAddr: client.RemoteAddr().String(), UserAgent: client.Version(), Medias: medias, Transport: client, }, client: client, } return c, nil } type Producer struct { core.Connection client *Client } const codecXiaobaiPCMA = 1 // chuangmi.camera.xiaobai func probe(client *Client) ([]*core.Media, error) { _ = client.SetDeadline(time.Now().Add(15 * time.Second)) var vcodec, acodec *core.Codec for { // 0 5000 codec // 2 0000 codec params // 4 01 active clients // 5 34 unknown const // 6 0600 unknown seq(s) // 8 80026801 unknown fixed // 12 ed8d5c69 time in sec // 16 4c03 time in 1/1000 // 18 0000 hdr, payload, err := client.ReadPacket() if err != nil { return nil, err } switch codec := hdr[0]; codec { case tutk.CodecH264, tutk.CodecH265: if vcodec == nil { avcc := annexb.EncodeToAVCC(payload) if codec == tutk.CodecH264 { if h264.NALUType(avcc) == h264.NALUTypeSPS { vcodec = h264.AVCCToCodec(avcc) } } else { if h265.NALUType(avcc) == h265.NALUTypeVPS { vcodec = h265.AVCCToCodec(avcc) } } } case tutk.CodecPCMA, codecXiaobaiPCMA: if acodec == nil { acodec = &core.Codec{Name: core.CodecPCMA, ClockRate: 8000} } case tutk.CodecPCML: if acodec == nil { acodec = &core.Codec{Name: core.CodecPCML, ClockRate: 8000} } case tutk.CodecAACLATM: if acodec == nil { acodec = aac.ADTSToCodec(payload) if acodec != nil { acodec.PayloadType = core.PayloadTypeRAW } } } if vcodec != nil && acodec != nil { break } } medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{vcodec}, }, { Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{acodec}, }, } return medias, nil } func (c *Producer) Protocol() string { return "tutk+udp" } func (c *Producer) Start() error { var audioTS uint32 var videoSeq, audioSeq uint16 for { _ = c.client.SetDeadline(time.Now().Add(5 * time.Second)) hdr, payload, err := c.client.ReadPacket() if err != nil { return err } n := len(payload) c.Recv += n // TODO: rewrite this var name string var pkt *core.Packet switch codec := hdr[0]; codec { case tutk.CodecH264, tutk.CodecH265: pkt = &core.Packet{ Header: rtp.Header{ SequenceNumber: videoSeq, Timestamp: core.Now90000(), }, Payload: annexb.EncodeToAVCC(payload), } videoSeq++ if codec == tutk.CodecH264 { name = core.CodecH264 } else { name = core.CodecH265 } case tutk.CodecPCMA, tutk.CodecPCML, codecXiaobaiPCMA: pkt = &core.Packet{ Header: rtp.Header{ Version: 2, Marker: true, SequenceNumber: audioSeq, Timestamp: audioTS, }, Payload: payload, } audioSeq++ switch codec { case tutk.CodecPCMA, codecXiaobaiPCMA: name = core.CodecPCMA audioTS += uint32(n) case tutk.CodecPCML: name = core.CodecPCML audioTS += uint32(n / 2) // because 16bit } case tutk.CodecAACLATM: pkt = &core.Packet{ Header: rtp.Header{ SequenceNumber: audioSeq, Timestamp: audioTS, }, Payload: payload, } audioSeq++ name = core.CodecAAC audioTS += 1024 } for _, recv := range c.Receivers { if recv.Codec.Name == name { recv.WriteRTP(pkt) break } } } } func (c *Producer) Stop() error { _ = c.client.StopMedia() return c.Connection.Stop() } ================================================ FILE: pkg/xiaomi/miss/backchannel.go ================================================ package miss import ( "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/opus" "github.com/AlexxIT/go2rtc/pkg/pcm" "github.com/pion/rtp" ) func (p *Producer) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error { if err := p.client.StartSpeaker(); err != nil { return err } // TODO: check this!!! time.Sleep(time.Second) sender := core.NewSender(media, track.Codec) switch track.Codec.Name { case core.CodecPCMA: var buf []byte if p.client.SpeakerCodec() == codecPCM { dst := &core.Codec{Name: core.CodecPCML, ClockRate: 8000} transcode := pcm.Transcode(dst, track.Codec) sender.Handler = func(pkt *rtp.Packet) { buf = append(buf, transcode(pkt.Payload)...) const size = 2 * 8000 * 0.040 // 16bit 40ms for len(buf) >= size { p.Send += size _ = p.client.WriteAudio(codecPCM, buf[:size]) buf = buf[size:] } } } else { sender.Handler = func(pkt *rtp.Packet) { buf = append(buf, pkt.Payload...) const size = 8000 * 0.040 // 8bit 40 ms for len(buf) >= size { p.Send += size _ = p.client.WriteAudio(codecPCMA, buf[:size]) buf = buf[size:] } } } case core.CodecOpus: if p.client.SpeakerCodec() == codecOPUS { var buf []byte sender.Handler = func(pkt *rtp.Packet) { if buf == nil { buf = pkt.Payload } else { // convert two 20ms to one 40ms buf = opus.JoinFrames(buf, pkt.Payload) p.Send += len(buf) _ = p.client.WriteAudio(codecOPUS, buf) buf = nil } } } else { sender.Handler = func(pkt *rtp.Packet) { p.Send += len(pkt.Payload) _ = p.client.WriteAudio(codecOPUS, pkt.Payload) } } } sender.HandleRTP(track) p.Senders = append(p.Senders, sender) return nil } ================================================ FILE: pkg/xiaomi/miss/client.go ================================================ package miss import ( "bytes" "encoding/binary" "encoding/hex" "errors" "fmt" "net" "net/url" "time" "github.com/AlexxIT/go2rtc/pkg/tutk" "github.com/AlexxIT/go2rtc/pkg/xiaomi/crypto" "github.com/AlexxIT/go2rtc/pkg/xiaomi/miss/cs2" ) const ( codecH264 = 4 codecH265 = 5 codecPCM = 1024 codecPCMU = 1026 codecPCMA = 1027 codecOPUS = 1032 ) type Conn interface { Protocol() string Version() string ReadCommand() (cmd uint32, data []byte, err error) WriteCommand(cmd uint32, data []byte) error ReadPacket() (hdr, payload []byte, err error) WritePacket(hdr, payload []byte) error RemoteAddr() net.Addr SetDeadline(t time.Time) error Close() error } func NewClient(rawURL string) (*Client, error) { u, err := url.Parse(rawURL) if err != nil { return nil, err } // 1. Check if we can create shared key. query := u.Query() key, err := crypto.CalcSharedKey(query.Get("device_public"), query.Get("client_private")) if err != nil { return nil, err } model := query.Get("model") // 2. Check if this vendor supported. var conn Conn switch s := query.Get("vendor"); s { case "cs2": conn, err = cs2.Dial(u.Host, query.Get("transport")) case "tutk": conn, err = tutk.Dial(u.Host, query.Get("uid"), "Miss", "client") default: err = fmt.Errorf("miss: unsupported vendor %s", s) } if err != nil { return nil, err } err = login(conn, query.Get("client_public"), query.Get("sign")) if err != nil { _ = conn.Close() return nil, err } return &Client{Conn: conn, key: key, model: model}, nil } type Client struct { Conn key []byte model string } const ( cmdAuthReq = 0x100 cmdAuthRes = 0x101 cmdVideoStart = 0x102 cmdVideoStop = 0x103 cmdAudioStart = 0x104 cmdAudioStop = 0x105 cmdSpeakerStartReq = 0x106 cmdSpeakerStartRes = 0x107 cmdSpeakerStop = 0x108 cmdStreamCtrlReq = 0x109 cmdStreamCtrlRes = 0x10A cmdGetAudioFormatReq = 0x10B cmdGetAudioFormatRes = 0x10C cmdPlaybackReq = 0x10D cmdPlaybackRes = 0x10E cmdDevInfoReq = 0x110 cmdDevInfoRes = 0x111 cmdMotorReq = 0x112 cmdMotorRes = 0x113 cmdEncoded = 0x1001 ) func login(conn Conn, clientPublic, sign string) error { s := fmt.Sprintf(`{"public_key":"%s","sign":"%s","uuid":"","support_encrypt":0}`, clientPublic, sign) if err := conn.WriteCommand(cmdAuthReq, []byte(s)); err != nil { return err } _, data, err := conn.ReadCommand() if err != nil { return err } if !bytes.Contains(data, []byte(`"result":"success"`)) { return fmt.Errorf("miss: auth: %s", data) } return nil } func (c *Client) Version() string { return fmt.Sprintf("%s (%s)", c.Conn.Version(), c.model) } func (c *Client) WriteCommand(data []byte) error { data, err := crypto.Encode(data, c.key) if err != nil { return err } return c.Conn.WriteCommand(cmdEncoded, data) } const ( ModelDafang = "isa.camera.df3" ModelLoockV2 = "loock.cateye.v02" ModelC200 = "chuangmi.camera.046c04" ModelC300 = "chuangmi.camera.72ac1" // ModelXiaofang looks like it has the same firmware as the ModelDafang. // There is also an older model "isa.camera.isc5" that only works with the legacy protocol. ModelXiaofang = "isa.camera.isc5c1" ) func (c *Client) StartMedia(channel, quality, audio string) error { switch c.model { case ModelDafang, ModelXiaofang: var q, a byte if quality == "sd" { q = 1 // 0 - hd, 1 - sd, default - hd } if audio != "0" { a = 1 // 0 - off, 1 - on, default - on } return errors.Join( c.WriteCommand(dafangVideoQuality(q)), c.WriteCommand(dafangVideoStart(1, a)), ) } // 0 - auto, 1 - sd, 2 - hd, default - hd switch quality { case "", "hd": // Some models have broken codec settings in quality 3. // Some models have low quality in quality 2. // Different models require different default quality settings. switch c.model { case ModelC200, ModelC300: quality = "3" default: quality = "2" } case "sd": quality = "1" case "auto": quality = "0" } if audio == "" { audio = "1" } data := binary.BigEndian.AppendUint32(nil, cmdVideoStart) switch channel { case "", "0": data = fmt.Appendf(data, `{"videoquality":%s,"enableaudio":%s}`, quality, audio) default: data = fmt.Appendf(data, `{"videoquality":-1,"videoquality2":%s,"enableaudio":%s}`, quality, audio) } return c.WriteCommand(data) } func (c *Client) StopMedia() error { data := binary.BigEndian.AppendUint32(nil, cmdVideoStop) return c.WriteCommand(data) } func (c *Client) StartAudio() error { data := binary.BigEndian.AppendUint32(nil, cmdAudioStart) return c.WriteCommand(data) } func (c *Client) StartSpeaker() error { data := binary.BigEndian.AppendUint32(nil, cmdSpeakerStartReq) return c.WriteCommand(data) } // SpeakerCodec if the camera model has a non-standard two-way codec. func (c *Client) SpeakerCodec() uint32 { switch c.model { case ModelDafang, ModelXiaofang, "isa.camera.hlc6": return codecPCM case "chuangmi.camera.72ac1": return codecOPUS } return 0 } const hdrSize = 32 func (c *Client) ReadPacket() (*Packet, error) { hdr, payload, err := c.Conn.ReadPacket() if err != nil { return nil, fmt.Errorf("miss: read media: %w", err) } if len(hdr) < hdrSize { return nil, fmt.Errorf("miss: packet header too small") } payload, err = crypto.Decode(payload, c.key) if err != nil { return nil, err } pkt := &Packet{ CodecID: binary.LittleEndian.Uint32(hdr[4:]), Sequence: binary.LittleEndian.Uint32(hdr[8:]), Flags: binary.LittleEndian.Uint32(hdr[12:]), Payload: payload, } switch c.model { case ModelDafang, ModelXiaofang, ModelLoockV2: // Dafang has ts in sec // LoockV2 has ts in msec for video, but zero ts for audio pkt.Timestamp = uint64(time.Now().UnixMilli()) default: pkt.Timestamp = binary.LittleEndian.Uint64(hdr[16:]) } return pkt, nil } func (c *Client) WriteAudio(codecID uint32, payload []byte) error { payload, err := crypto.Encode(payload, c.key) // new payload will have new size! if err != nil { return err } n := uint32(len(payload)) header := make([]byte, hdrSize) binary.LittleEndian.PutUint32(header, n) binary.LittleEndian.PutUint32(header[4:], codecID) binary.LittleEndian.PutUint64(header[16:], uint64(time.Now().UnixMilli())) // not really necessary return c.Conn.WritePacket(header, payload) } type Packet struct { //Length uint32 CodecID uint32 Sequence uint32 Flags uint32 Timestamp uint64 // msec //TimestampS uint32 //Reserved uint32 Payload []byte } func (p *Packet) SampleRate() uint32 { // flag: 1 0011 000 - sample rate 16000 // flag: 100 00 01 0000 000 - sample rate 8000 v := (p.Flags >> 3) & 0b1111 if v != 0 { return 16000 } return 8000 } //func (p *Packet) AudioUnknown1() byte { // return byte((p.Flags >> 7) & 0b11) //} // //func (p *Packet) AudioUnknown2() byte { // return byte((p.Flags >> 9) & 0b11) //} func dafangRaw(cmd uint32, args ...byte) []byte { payload := tutk.ICAM(cmd, args...) data := make([]byte, 4+len(payload)*2) copy(data, "\x7f\xff\xff\xff") hex.Encode(data[4:], payload) return data } // DafangVideoQuality 0 - hd, 1 - sd func dafangVideoQuality(quality uint8) []byte { return dafangRaw(0xff07d5, quality) } func dafangVideoStart(video, audio uint8) []byte { return dafangRaw(0xff07d8, video, audio) } //func dafangLeft() []byte { // return dafangRaw(0xff2404, 2, 0, 5) //} // //func dafangRight() []byte { // return dafangRaw(0xff2404, 1, 0, 5) //} // //func dafangUp() []byte { // return dafangRaw(0xff2404, 0, 2, 5) //} // //func dafangDown() []byte { // return dafangRaw(0xff2404, 0, 1, 5) //} // //func dafangStop() []byte { // return dafangRaw(0xff2404, 0, 0, 5) //} ================================================ FILE: pkg/xiaomi/miss/cs2/conn.go ================================================ package cs2 import ( "bufio" "bytes" "encoding/binary" "fmt" "io" "net" "sync" "sync/atomic" "time" ) func Dial(host, transport string) (*Conn, error) { conn, err := handshake(host, transport) if err != nil { return nil, err } _, isTCP := conn.(*tcpConn) c := &Conn{ Conn: conn, isTCP: isTCP, channels: [4]*dataChannel{ newDataChannel(0, 10), nil, newDataChannel(250, 100), nil, }, } go c.worker() return c, nil } type Conn struct { net.Conn isTCP bool err error seqCh0 uint16 seqCh3 uint16 channels [4]*dataChannel cmdMu sync.Mutex cmdAck func() } const ( magic = 0xF1 magicDrw = 0xD1 magicTCP = 0x68 msgLanSearch = 0x30 msgPunchPkt = 0x41 msgP2PRdyUDP = 0x42 msgP2PRdyTCP = 0x43 msgDrw = 0xD0 msgDrwAck = 0xD1 msgPing = 0xE0 msgPong = 0xE1 msgClose = 0xF0 msgCloseAck = 0xF1 ) func handshake(host, transport string) (net.Conn, error) { conn, err := newUDPConn(host, 32108) if err != nil { return nil, err } _ = conn.SetDeadline(time.Now().Add(5 * time.Second)) req := []byte{magic, msgLanSearch, 0, 0} res, err := conn.(*udpConn).WriteUntil(req, func(res []byte) bool { return res[1] == msgPunchPkt }) if err != nil { _ = conn.Close() return nil, err } var msgUDP, msgTCP byte if transport == "" || transport == "udp" { msgUDP = msgP2PRdyUDP } if transport == "" || transport == "tcp" { msgTCP = msgP2PRdyTCP } res, err = conn.(*udpConn).WriteUntil(res, func(res []byte) bool { return res[1] == msgUDP || res[1] == msgTCP }) if err != nil { _ = conn.Close() return nil, err } _ = conn.SetDeadline(time.Time{}) if res[1] == msgTCP { _ = conn.Close() //host := fmt.Sprintf("%d.%d.%d.%d:%d", b[31], b[30], b[29], b[28], uint16(b[27])<<8|uint16(b[26])) return newTCPConn(conn.RemoteAddr().String()) } return conn, nil } func (c *Conn) worker() { defer func() { c.channels[0].Close() c.channels[2].Close() }() var keepaliveTS time.Time // only for TCP buf := make([]byte, 1200) for { n, err := c.Conn.Read(buf) if err != nil { c.err = fmt.Errorf("%s: %w", "cs2", err) return } // 0 f1d0 magic // 2 005d size = total size + 4 // 4 d1 magic // 5 00 channel // 6 0000 seq switch buf[1] { case msgDrw: ch := buf[5] channel := c.channels[ch] if c.isTCP { // For TCP we should send ping every second to keep connection alive. // Based on PCAP analysis: official Mi Home app sends PING every ~1s. if now := time.Now(); now.After(keepaliveTS) { _, _ = c.Conn.Write([]byte{magic, msgPing, 0, 0}) keepaliveTS = now.Add(time.Second) } err = channel.Push(buf[8:n]) } else { var pushed int seqHI, seqLO := buf[6], buf[7] seq := uint16(seqHI)<<8 | uint16(seqLO) pushed, err = channel.PushSeq(seq, buf[8:n]) if pushed >= 0 { // For UDP we should send ACK. ack := []byte{magic, msgDrwAck, 0, 6, magicDrw, ch, 0, 1, seqHI, seqLO} _, _ = c.Conn.Write(ack) } } if err != nil { c.err = fmt.Errorf("%s: %w", "cs2", err) return } case msgPing: _, _ = c.Conn.Write([]byte{magic, msgPong, 0, 0}) case msgPong, msgP2PRdyUDP, msgP2PRdyTCP, msgClose, msgCloseAck: // skip it case msgDrwAck: // only for UDP if c.cmdAck != nil { c.cmdAck() } default: fmt.Printf("%s: unknown msg: %x\n", "cs2", buf[:n]) } } } func (c *Conn) Protocol() string { if c.isTCP { return "cs2+tcp" } return "cs2+udp" } func (c *Conn) Version() string { return "CS2" } func (c *Conn) Error() error { if c.err != nil { return c.err } return io.EOF } func (c *Conn) ReadCommand() (cmd uint32, data []byte, err error) { buf, ok := c.channels[0].Pop() if !ok { return 0, nil, c.Error() } cmd = binary.LittleEndian.Uint32(buf) data = buf[4:] return } func (c *Conn) WriteCommand(cmd uint32, data []byte) error { c.cmdMu.Lock() defer c.cmdMu.Unlock() req := marshalCmd(0, c.seqCh0, cmd, data) c.seqCh0++ if c.isTCP { _, err := c.Conn.Write(req) return err } var repeat atomic.Int32 repeat.Store(5) timeout := time.NewTicker(time.Second) defer timeout.Stop() c.cmdAck = func() { repeat.Store(0) timeout.Reset(1) } for { if _, err := c.Conn.Write(req); err != nil { return err } <-timeout.C r := repeat.Add(-1) if r < 0 { return nil } if r == 0 { return fmt.Errorf("%s: can't send command %d", "cs2", cmd) } } } const hdrSize = 32 func (c *Conn) ReadPacket() (hdr, payload []byte, err error) { data, ok := c.channels[2].Pop() if !ok { return nil, nil, c.Error() } return data[:hdrSize], data[hdrSize:], nil } func (c *Conn) WritePacket(hdr, payload []byte) error { const offset = 12 n := hdrSize + uint32(len(payload)) req := make([]byte, n+offset) req[0] = magic req[1] = msgDrw binary.BigEndian.PutUint16(req[2:], uint16(n+8)) req[4] = magicDrw req[5] = 3 // channel binary.BigEndian.PutUint16(req[6:], c.seqCh3) c.seqCh3++ binary.BigEndian.PutUint32(req[8:], n) copy(req[offset:], hdr) copy(req[offset+hdrSize:], hdr) _, err := c.Conn.Write(req) return err } func marshalCmd(channel byte, seq uint16, cmd uint32, payload []byte) []byte { size := len(payload) req := make([]byte, 4+4+4+4+size) // 1. message header (4 bytes) req[0] = magic req[1] = msgDrw binary.BigEndian.PutUint16(req[2:], uint16(4+4+4+size)) // 2. drw? header (4 bytes) req[4] = magicDrw req[5] = channel binary.BigEndian.PutUint16(req[6:], seq) // 3. payload size (4 bytes) binary.BigEndian.PutUint32(req[8:], uint32(4+size)) // 4. payload command (4 bytes) binary.BigEndian.PutUint32(req[12:], cmd) // 5. payload copy(req[16:], payload) return req } func newUDPConn(host string, port int) (net.Conn, error) { // We using raw net.UDPConn, because RemoteAddr should be changed during handshake. conn, err := net.ListenUDP("udp", nil) if err != nil { return nil, err } addr, err := net.ResolveUDPAddr("udp", host) if err != nil { addr = &net.UDPAddr{IP: net.ParseIP(host), Port: port} } return &udpConn{UDPConn: conn, addr: addr}, nil } type udpConn struct { *net.UDPConn addr *net.UDPAddr } func (c *udpConn) Read(b []byte) (n int, err error) { var addr *net.UDPAddr for { n, addr, err = c.UDPConn.ReadFromUDP(b) if err != nil { return 0, err } if string(addr.IP) == string(c.addr.IP) || n >= 8 { //log.Printf("<- %x", b[:n]) return } } } func (c *udpConn) Write(b []byte) (n int, err error) { //log.Printf("-> %x", b) return c.UDPConn.WriteToUDP(b, c.addr) } func (c *udpConn) RemoteAddr() net.Addr { return c.addr } func (c *udpConn) WriteUntil(req []byte, ok func(res []byte) bool) ([]byte, error) { var t *time.Timer t = time.AfterFunc(1, func() { if _, err := c.Write(req); err == nil && t != nil { t.Reset(time.Second) } }) defer t.Stop() buf := make([]byte, 1200) for { n, addr, err := c.UDPConn.ReadFromUDP(buf) if err != nil { return nil, err } if string(addr.IP) != string(c.addr.IP) || n < 16 { continue // skip messages from another IP } if ok(buf[:n]) { c.addr.Port = addr.Port return buf[:n], nil } } } func newTCPConn(addr string) (net.Conn, error) { conn, err := net.DialTimeout("tcp", addr, 3*time.Second) if err != nil { return nil, err } return &tcpConn{conn.(*net.TCPConn), bufio.NewReader(conn)}, nil } type tcpConn struct { *net.TCPConn rd *bufio.Reader } func (c *tcpConn) Read(p []byte) (n int, err error) { tmp := make([]byte, 8) if _, err = io.ReadFull(c.rd, tmp); err != nil { return } n = int(binary.BigEndian.Uint16(tmp)) if len(p) < n { return 0, fmt.Errorf("tcp: buffer too small") } _, err = io.ReadFull(c.rd, p[:n]) //log.Printf("<- %x%x", tmp, p[:n]) return } func (c *tcpConn) Write(req []byte) (n int, err error) { n = len(req) buf := make([]byte, 8+n) binary.BigEndian.PutUint16(buf, uint16(n)) buf[2] = magicTCP copy(buf[8:], req) //log.Printf("-> %x", buf) _, err = c.TCPConn.Write(buf) return } func newDataChannel(pushSize, popSize int) *dataChannel { c := &dataChannel{} if pushSize > 0 { c.pushBuf = make(map[uint16][]byte, pushSize) c.pushSize = pushSize } if popSize >= 0 { c.popBuf = make(chan []byte, popSize) } return c } type dataChannel struct { waitSeq uint16 pushBuf map[uint16][]byte pushSize int waitData []byte waitSize int popBuf chan []byte } func (c *dataChannel) Push(b []byte) error { c.waitData = append(c.waitData, b...) for len(c.waitData) > 4 { // Every new data starts with size. There can be several data inside one packet. if c.waitSize == 0 { c.waitSize = int(binary.BigEndian.Uint32(c.waitData)) c.waitData = c.waitData[4:] } if c.waitSize > len(c.waitData) { break } select { case c.popBuf <- c.waitData[:c.waitSize]: default: return fmt.Errorf("pop buffer is full") } c.waitData = c.waitData[c.waitSize:] c.waitSize = 0 } return nil } func (c *dataChannel) Pop() ([]byte, bool) { data, ok := <-c.popBuf return data, ok } func (c *dataChannel) Close() { close(c.popBuf) } // PushSeq returns how many seq were processed. // Returns 0 if seq was saved or processed earlier. // Returns -1 if seq could not be saved (buffer full or disabled). func (c *dataChannel) PushSeq(seq uint16, data []byte) (int, error) { diff := int16(seq - c.waitSeq) // Check if this is seq from the future. if diff > 0 { // Support disabled buffer. if c.pushSize == 0 { return -1, nil // couldn't save seq } // Check if we don't have this seq in the buffer. if c.pushBuf[seq] == nil { // Check if there is enough space in the buffer. if len(c.pushBuf) == c.pushSize { return -1, nil // couldn't save seq } c.pushBuf[seq] = bytes.Clone(data) //log.Printf("push buf wait=%d seq=%d len=%d", c.waitSeq, seq, len(c.pushBuf)) } return 0, nil } // Check if this is seq from the past. if diff < 0 { return 0, nil } for i := 1; ; i++ { if err := c.Push(data); err != nil { return i, err } c.waitSeq++ // Check if we have next seq in the buffer. if data = c.pushBuf[c.waitSeq]; data != nil { delete(c.pushBuf, c.waitSeq) } else { return i, nil } } } ================================================ FILE: pkg/xiaomi/miss/producer.go ================================================ package miss import ( "fmt" "net/url" "time" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/h264" "github.com/AlexxIT/go2rtc/pkg/h264/annexb" "github.com/AlexxIT/go2rtc/pkg/h265" "github.com/pion/rtp" ) type Producer struct { core.Connection client *Client } func Dial(rawURL string) (core.Producer, error) { client, err := NewClient(rawURL) if err != nil { return nil, err } u, _ := url.Parse(rawURL) query := u.Query() err = client.StartMedia(query.Get("channel"), query.Get("subtype"), query.Get("audio")) if err != nil { _ = client.Close() return nil, err } medias, err := probe(client, query.Get("audio") != "0") if err != nil { _ = client.Close() return nil, err } return &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "xiaomi/miss", Protocol: client.Protocol(), RemoteAddr: client.RemoteAddr().String(), UserAgent: client.Version(), Medias: medias, Transport: client, }, client: client, }, nil } func probe(client *Client, audio bool) ([]*core.Media, error) { _ = client.SetDeadline(time.Now().Add(15 * time.Second)) var vcodec, acodec *core.Codec for { pkt, err := client.ReadPacket() if err != nil { if vcodec != nil { err = fmt.Errorf("no audio") } else if acodec != nil { err = fmt.Errorf("no video") } return nil, fmt.Errorf("xiaomi: probe: %w", err) } switch pkt.CodecID { case codecH264: if vcodec == nil { buf := annexb.EncodeToAVCC(pkt.Payload) if h264.NALUType(buf) == h264.NALUTypeSPS { vcodec = h264.AVCCToCodec(buf) } } case codecH265: if vcodec == nil { buf := annexb.EncodeToAVCC(pkt.Payload) if h265.NALUType(buf) == h265.NALUTypeVPS { vcodec = h265.AVCCToCodec(buf) } } case codecPCMA: if acodec == nil { acodec = &core.Codec{Name: core.CodecPCMA, ClockRate: pkt.SampleRate()} } case codecOPUS: if acodec == nil { acodec = &core.Codec{Name: core.CodecOpus, ClockRate: 48000, Channels: 2} } } if vcodec != nil && (acodec != nil || !audio) { break } } _ = client.SetDeadline(time.Time{}) medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{vcodec}, }, } if acodec != nil { medias = append(medias, &core.Media{ Kind: core.KindAudio, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{acodec}, }) medias = append(medias, &core.Media{ Kind: core.KindAudio, Direction: core.DirectionSendonly, Codecs: []*core.Codec{acodec.Clone()}, }) } return medias, nil } const timestamp40ms = 48000 * 0.040 func (p *Producer) Start() error { var audioTS uint32 for { _ = p.client.SetDeadline(time.Now().Add(10 * time.Second)) pkt, err := p.client.ReadPacket() if err != nil { return err } p.Recv += len(pkt.Payload) // TODO: rewrite this var name string var pkt2 *core.Packet switch pkt.CodecID { case codecH264, codecH265: pkt2 = &core.Packet{ Header: rtp.Header{ SequenceNumber: uint16(pkt.Sequence), Timestamp: TimeToRTP(pkt.Timestamp, 90000), }, Payload: annexb.EncodeToAVCC(pkt.Payload), } if pkt.CodecID == codecH264 { name = core.CodecH264 } else { name = core.CodecH265 } case codecPCMA: name = core.CodecPCMA pkt2 = &core.Packet{ Header: rtp.Header{ Version: 2, Marker: true, SequenceNumber: uint16(pkt.Sequence), Timestamp: audioTS, }, Payload: pkt.Payload, } audioTS += uint32(len(pkt.Payload)) case codecOPUS: name = core.CodecOpus pkt2 = &core.Packet{ Header: rtp.Header{ Version: 2, Marker: true, SequenceNumber: uint16(pkt.Sequence), Timestamp: audioTS, }, Payload: pkt.Payload, } // known cameras sends packets with 40ms long audioTS += timestamp40ms } for _, recv := range p.Receivers { if recv.Codec.Name == name { recv.WriteRTP(pkt2) break } } } } func (p *Producer) Stop() error { _ = p.client.StopMedia() return p.Connection.Stop() } // TimeToRTP convert time in milliseconds to RTP time func TimeToRTP(timeMS, clockRate uint64) uint32 { return uint32(timeMS * clockRate / 1000) } ================================================ FILE: pkg/xiaomi/producer.go ================================================ package xiaomi import ( "strings" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/AlexxIT/go2rtc/pkg/xiaomi/legacy" "github.com/AlexxIT/go2rtc/pkg/xiaomi/miss" ) func Dial(rawURL string) (core.Producer, error) { // Format: xiaomi/miss if strings.Contains(rawURL, "vendor") { return miss.Dial(rawURL) } // Format: xiaomi/legacy return legacy.Dial(rawURL) } func IsLegacy(model string) bool { return legacy.Supported(model) } ================================================ FILE: pkg/xnet/net.go ================================================ package xnet import ( "net" "strconv" ) // Docker has common docker addresses (class B): // https://en.wikipedia.org/wiki/Private_network // - docker0 172.17.0.1/16 // - br-xxxx 172.18.0.1/16 // - hassio 172.30.32.1/23 var Docker = net.IPNet{ IP: []byte{172, 16, 0, 0}, Mask: []byte{255, 240, 0, 0}, } // ParseUnspecifiedPort will return port if address is unspecified // ex. ":8555" or "0.0.0.0:8555" func ParseUnspecifiedPort(address string) int { host, port, err := net.SplitHostPort(address) if err != nil { return 0 } if host != "" && host != "0.0.0.0" && host != "[::]" { return 0 } i, _ := strconv.Atoi(port) return i } func IPNets(ipFilter func(ip net.IP) bool) ([]*net.IPNet, error) { ifaces, err := net.Interfaces() if err != nil { return nil, err } var nets []*net.IPNet for _, iface := range ifaces { if iface.Flags&net.FlagUp == 0 || iface.Flags&net.FlagLoopback != 0 { continue } addrs, _ := iface.Addrs() // range on nil slice is OK for _, addr := range addrs { switch v := addr.(type) { case *net.IPNet: ip := v.IP.To4() if ip == nil { continue } if ipFilter != nil && !ipFilter(ip) { continue } nets = append(nets, v) } } } return nets, nil } ================================================ FILE: pkg/xnet/tls/tls.go ================================================ package tls import ( "crypto/rand" "crypto/rsa" "crypto/tls" "crypto/x509" "crypto/x509/pkix" "encoding/pem" "math/big" "net" "time" ) func CreateCertificate() (*tls.Certificate, error) { // 1. Generate an RSA private key privateKey, err := rsa.GenerateKey(rand.Reader, 2048) if err != nil { return nil, err } // 2. Define the certificate template serialNumberLimit := new(big.Int).Lsh(big.NewInt(1), 128) serialNumber, err := rand.Int(rand.Reader, serialNumberLimit) if err != nil { return nil, err } template := x509.Certificate{ SerialNumber: serialNumber, Subject: pkix.Name{ Organization: []string{"home"}, CommonName: "localhost", }, NotBefore: time.Now(), NotAfter: time.Now().Add(365 * 24 * time.Hour), // Valid for 1 year KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, BasicConstraintsValid: true, // Add localhost as a valid IP and DNS name IPAddresses: []net.IP{[]byte{127, 0, 0, 1}}, DNSNames: []string{"localhost"}, } // 3. Create a self-signed certificate // The parent is the template itself, and we use the generated public and private keys. derBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey) if err != nil { return nil, err } derBytes = pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: derBytes}) keyBytes := pem.EncodeToMemory(&pem.Block{Type: "RSA PRIVATE KEY", Bytes: x509.MarshalPKCS1PrivateKey(privateKey)}) cert, err := tls.X509KeyPair(derBytes, keyBytes) if err != nil { return nil, err } return &cert, nil } ================================================ FILE: pkg/y4m/README.md ================================================ ## Planar YUV formats Packed YUV - yuyv422 - YUYV 4:2:2 Semi-Planar - nv12 - Y/CbCr 4:2:0 Planar YUV - yuv420p - Planar YUV 4:2:0 - aka. [cosited](https://manned.org/yuv4mpeg.5) ``` [video4linux2,v4l2 @ 0x55fddc42a940] Raw : yuyv422 : YUYV 4:2:2 : 1920x1080 [video4linux2,v4l2 @ 0x55fddc42a940] Raw : nv12 : Y/CbCr 4:2:0 : 1920x1080 [video4linux2,v4l2 @ 0x55fddc42a940] Raw : yuv420p : Planar YUV 4:2:0 : 1920x1080 ``` ## Useful links - https://learn.microsoft.com/en-us/windows/win32/medfound/recommended-8-bit-yuv-formats-for-video-rendering - https://developer.mozilla.org/en-US/docs/Web/Media/Formats/Video_concepts - https://fourcc.org/yuv.php#YV12 - https://docs.kernel.org/userspace-api/media/v4l/pixfmt-yuv-planar.html - https://gist.github.com/Jim-Bar/3cbba684a71d1a9d468a6711a6eddbeb ================================================ FILE: pkg/y4m/consumer.go ================================================ package y4m import ( "fmt" "io" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) type Consumer struct { core.Connection wr *core.WriteBuffer } func NewConsumer() *Consumer { wr := core.NewWriteBuffer(nil) return &Consumer{ core.Connection{ ID: core.NewID(), Transport: wr, FormatName: "yuv4mpegpipe", Medias: []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionSendonly, Codecs: []*core.Codec{ {Name: core.CodecRAW}, }, }, }, }, wr, } } func (c *Consumer) AddTrack(media *core.Media, _ *core.Codec, track *core.Receiver) error { sender := core.NewSender(media, track.Codec) sender.Handler = func(packet *rtp.Packet) { if n, err := c.wr.Write([]byte(frameHdr)); err == nil { c.Send += n } if n, err := c.wr.Write(packet.Payload); err == nil { c.Send += n } } hdr := fmt.Sprintf( "YUV4MPEG2 W%s H%s C%s\n", core.Between(track.Codec.FmtpLine, "width=", ";"), core.Between(track.Codec.FmtpLine, "height=", ";"), core.Between(track.Codec.FmtpLine, "colorspace=", ";"), ) if _, err := c.wr.Write([]byte(hdr)); err != nil { return err } sender.HandleRTP(track) c.Senders = append(c.Senders, sender) return nil } func (c *Consumer) WriteTo(wr io.Writer) (int64, error) { return c.wr.WriteTo(wr) } ================================================ FILE: pkg/y4m/producer.go ================================================ package y4m import ( "bufio" "errors" "io" "github.com/AlexxIT/go2rtc/pkg/core" "github.com/pion/rtp" ) func Open(r io.Reader) (*Producer, error) { rd := bufio.NewReaderSize(r, core.BufferSize) b, err := rd.ReadBytes('\n') if err != nil { return nil, err } b = b[:len(b)-1] // remove \n fmtp := ParseHeader(b) if GetSize(fmtp) == 0 { return nil, errors.New("y4m: unsupported format: " + string(b)) } medias := []*core.Media{ { Kind: core.KindVideo, Direction: core.DirectionRecvonly, Codecs: []*core.Codec{ { Name: core.CodecRAW, ClockRate: 90000, FmtpLine: fmtp, PayloadType: core.PayloadTypeRAW, }, }, }, } return &Producer{ Connection: core.Connection{ ID: core.NewID(), FormatName: "yuv4mpegpipe", Medias: medias, SDP: string(b), Transport: r, }, rd: rd, }, nil } type Producer struct { core.Connection rd *bufio.Reader } func (c *Producer) Start() error { size := GetSize(c.Medias[0].Codecs[0].FmtpLine) for { if _, err := c.rd.Discard(len(frameHdr)); err != nil { return err } frame := make([]byte, size) if _, err := io.ReadFull(c.rd, frame); err != nil { return err } c.Recv += size if len(c.Receivers) == 0 { continue } pkt := &rtp.Packet{ Header: rtp.Header{Timestamp: core.Now90000()}, Payload: frame, } c.Receivers[0].WriteRTP(pkt) } } ================================================ FILE: pkg/y4m/y4m.go ================================================ package y4m import ( "bytes" "image" "github.com/AlexxIT/go2rtc/pkg/core" ) const FourCC = "YUV4" const frameHdr = "FRAME\n" func ParseHeader(b []byte) (fmtp string) { for b != nil { // YUV4MPEG2 W1280 H720 F24:1 Ip A1:1 C420mpeg2 XYSCSS=420MPEG2 // https://manned.org/yuv4mpeg.5 // https://github.com/FFmpeg/FFmpeg/blob/master/libavformat/yuv4mpegenc.c key := b[0] var value string if i := bytes.IndexByte(b, ' '); i > 0 { value = string(b[1:i]) b = b[i+1:] } else { value = string(b[1:]) b = nil } switch key { case 'W': fmtp = "width=" + value case 'H': fmtp += ";height=" + value case 'C': fmtp += ";colorspace=" + value } } return } func GetSize(fmtp string) int { w := core.Atoi(core.Between(fmtp, "width=", ";")) h := core.Atoi(core.Between(fmtp, "height=", ";")) switch core.Between(fmtp, "colorspace=", ";") { case "mono": return w * h case "420mpeg2", "420jpeg": return w * h * 3 / 2 case "422": return w * h * 2 case "444": return w * h * 3 } return 0 } func NewImage(fmtp string) func(frame []byte) image.Image { w := core.Atoi(core.Between(fmtp, "width=", ";")) h := core.Atoi(core.Between(fmtp, "height=", ";")) rect := image.Rect(0, 0, w, h) switch core.Between(fmtp, "colorspace=", ";") { case "mono": return func(frame []byte) image.Image { return &image.Gray{ Pix: frame, Stride: w, Rect: rect, } } case "420mpeg2", "420jpeg": i1 := w * h i2 := i1 + i1/4 i3 := i2 + i1/4 return func(frame []byte) image.Image { return &image.YCbCr{ Y: frame[:i1], Cb: frame[i1:i2], Cr: frame[i2:i3], YStride: w, CStride: w / 2, SubsampleRatio: image.YCbCrSubsampleRatio420, Rect: rect, } } case "422": i1 := w * h i2 := i1 + i1/2 i3 := i2 + i1/2 return func(frame []byte) image.Image { return &image.YCbCr{ Y: frame[:i1], Cb: frame[i1:i2], Cr: frame[i2:i3], YStride: w, CStride: w / 2, SubsampleRatio: image.YCbCrSubsampleRatio422, Rect: rect, } } case "444": i1 := w * h i2 := i1 + i1 i3 := i2 + i1 return func(frame []byte) image.Image { return &image.YCbCr{ Y: frame[:i1], Cb: frame[i1:i2], Cr: frame[i2:i3], YStride: w, CStride: w, SubsampleRatio: image.YCbCrSubsampleRatio444, Rect: rect, } } } return nil } // HasSameColor checks if all pixels has same color func HasSameColor(img image.Image) bool { var pix []byte switch img := img.(type) { case *image.Gray: pix = img.Pix case *image.YCbCr: pix = img.Y } if len(pix) == 0 { return false } i0 := pix[0] for _, i := range pix { if i != i0 { return false } } return true } ================================================ FILE: pkg/yaml/yaml.go ================================================ package yaml import ( "bytes" "errors" "gopkg.in/yaml.v3" ) func Unmarshal(in []byte, out interface{}) (err error) { return yaml.Unmarshal(in, out) } func Encode(v any, indent int) ([]byte, error) { b := bytes.NewBuffer(nil) e := yaml.NewEncoder(b) e.SetIndent(indent) if err := e.Encode(v); err != nil { return nil, err } return b.Bytes(), nil } func Patch(in []byte, path []string, value any) ([]byte, error) { out, err := patch(in, path, value) if err != nil { return nil, err } // validate if err = yaml.Unmarshal(out, map[string]any{}); err != nil { return nil, err } return out, nil } func patch(in []byte, path []string, value any) ([]byte, error) { var root yaml.Node if err := yaml.Unmarshal(in, &root); err != nil { // invalid yaml return nil, err } // empty in if len(root.Content) != 1 { return addToEnd(in, path, value) } // yaml is not dict if root.Content[0].Kind != yaml.MappingNode { return nil, errors.New("yaml: can't patch") } // dict items list nodes := root.Content[0].Content n := len(path) - 1 // parent node key/value pKey, pVal := findNode(nodes, path[:n]) if pKey == nil { // no parent node return addToEnd(in, path, value) } var paste []byte if value != nil { // nil value means delete key var err error v := map[string]any{path[n]: value} if paste, err = Encode(v, 2); err != nil { return nil, err } } iKey, _ := findNode(pVal.Content, path[n:]) if iKey != nil { // key item not nil (replace value) paste = addIndent(paste, iKey.Column-1) i0, i1 := nodeBounds(in, iKey) return join(in[:i0], paste, in[i1:]), nil } if pVal.Content != nil { // parent value not nil (use first child indent) paste = addIndent(paste, pVal.Column-1) } else { // parent value is nil (use parent indent + 2) paste = addIndent(paste, pKey.Column+1) } _, i1 := nodeBounds(in, pKey) return join(in[:i1], paste, in[i1:]), nil } func findNode(nodes []*yaml.Node, keys []string) (key, value *yaml.Node) { for i, name := range keys { for j := 0; j < len(nodes); j += 2 { if nodes[j].Value == name { if i < len(keys)-1 { nodes = nodes[j+1].Content break } return nodes[j], nodes[j+1] } } } return nil, nil } func nodeBounds(in []byte, node *yaml.Node) (offset0, offset1 int) { // start from next line after node offset0 = lineOffset(in, node.Line) offset1 = lineOffset(in, node.Line+1) if offset1 < 0 { return offset0, len(in) } for i := offset1; i < len(in); { indent, length := parseLine(in[i:]) if indent+1 != length { if node.Column < indent+1 { offset1 = i + length } else { break } } i += length } return } func addToEnd(in []byte, path []string, value any) ([]byte, error) { if len(path) != 2 || value == nil { return nil, errors.New("yaml: path not exist") } v := map[string]map[string]any{ path[0]: {path[1]: value}, } paste, err := Encode(v, 2) if err != nil { return nil, err } return join(in, paste), nil } func join(items ...[]byte) []byte { n := len(items) - 1 for _, b := range items { n += len(b) } buf := make([]byte, 0, n) for _, b := range items { if len(b) == 0 { continue } if n = len(buf); n > 0 && buf[n-1] != '\n' { buf = append(buf, '\n') } buf = append(buf, b...) } return buf } func addPrefix(src, pre []byte) (dst []byte) { for len(src) > 0 { dst = append(dst, pre...) i := bytes.IndexByte(src, '\n') + 1 if i == 0 { dst = append(dst, src...) break } dst = append(dst, src[:i]...) src = src[i:] } return } func addIndent(in []byte, indent int) (dst []byte) { pre := make([]byte, indent) for i := 0; i < indent; i++ { pre[i] = ' ' } return addPrefix(in, pre) } func lineOffset(in []byte, line int) (offset int) { for l := 1; ; l++ { if l == line { return offset } i := bytes.IndexByte(in[offset:], '\n') + 1 if i == 0 { break } offset += i } return -1 } func parseLine(b []byte) (indent int, length int) { prefix := true for ; length < len(b); length++ { switch b[length] { case ' ': if prefix { indent++ } case '\n': length++ return default: prefix = false } } return } ================================================ FILE: pkg/yaml/yaml_test.go ================================================ package yaml import ( "testing" "github.com/stretchr/testify/require" ) func TestPatch(t *testing.T) { tests := []struct { name string src string path []string value any expect string }{ { name: "empty config", src: "", path: []string{"streams", "camera1"}, value: "val1", expect: "streams:\n camera1: val1\n", }, { name: "empty main key", src: "#dummy", path: []string{"streams", "camera1"}, value: "val1", expect: "#dummy\nstreams:\n camera1: val1\n", }, { name: "single line value", src: "streams:\n camera1: url1\n camera2: url2", path: []string{"streams", "camera1"}, value: "val1", expect: "streams:\n camera1: val1\n camera2: url2", }, { name: "next line value", src: "streams:\n camera1:\n url1\n camera2: url2", path: []string{"streams", "camera1"}, value: "val1", expect: "streams:\n camera1: val1\n camera2: url2", }, { name: "two lines value", src: "streams:\n camera1: url1\n url2\n camera2: url2", path: []string{"streams", "camera1"}, value: "val1", expect: "streams:\n camera1: val1\n camera2: url2", }, { name: "next two lines value", src: "streams:\n camera1:\n url1\n url2\n camera2: url2", path: []string{"streams", "camera1"}, value: "val1", expect: "streams:\n camera1: val1\n camera2: url2", }, { name: "add array", src: "", path: []string{"streams", "camera1"}, value: []string{"val1", "val2"}, expect: "streams:\n camera1:\n - val1\n - val2\n", }, { name: "remove value", src: "streams:\n camera1: url1\n camera2: url2", path: []string{"streams", "camera1"}, value: nil, expect: "streams:\n camera2: url2", }, { name: "add pairings", src: "homekit:\n camera1:\nstreams:\n camera1: url1", path: []string{"homekit", "camera1", "pairings"}, value: []string{"val1"}, expect: "homekit:\n camera1:\n pairings:\n - val1\nstreams:\n camera1: url1", }, { name: "remove pairings", src: "homekit:\n camera1:\n pairings:\n - val1\nstreams:\n camera1: url1", path: []string{"homekit", "camera1", "pairings"}, value: nil, expect: "homekit:\n camera1:\nstreams:\n camera1: url1", }, { name: "no new line", src: "streams:\n camera1: url1", path: []string{"streams", "camera1"}, value: "val1", expect: "streams:\n camera1: val1\n", }, { name: "no new line", src: "streams:\n camera1: url1\nhomekit:\n camera1:\n name: dummy", path: []string{"homekit", "camera1", "pairings"}, value: []string{"val1"}, expect: "streams:\n camera1: url1\nhomekit:\n camera1:\n name: dummy\n pairings:\n - val1\n", }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b, err := Patch([]byte(tt.src), tt.path, tt.value) require.NoError(t, err) require.Equal(t, tt.expect, string(b)) }) } } ================================================ FILE: pkg/yandex/session.go ================================================ package yandex import ( "encoding/json" "errors" "io" "net/http" "net/http/cookiejar" "strings" "sync" "time" "github.com/AlexxIT/go2rtc/pkg/core" ) type Session struct { token string client *http.Client } var sessions = map[string]*Session{} var sessionsMu sync.Mutex func GetSession(token string) (*Session, error) { sessionsMu.Lock() defer sessionsMu.Unlock() if session, ok := sessions[token]; ok { return session, nil } session := &Session{token: token} if err := session.Login(); err != nil { return nil, err } sessions[token] = session return session, nil } func (s *Session) Login() error { req, err := http.NewRequest( "POST", "https://mobileproxy.passport.yandex.net/1/bundle/auth/x_token/", strings.NewReader("type=x-token&retpath=https%3A%2F%2Fwww.yandex.ru"), ) if err != nil { return err } req.Header.Set("Content-Type", "application/x-www-form-urlencoded") req.Header.Set("Ya-Consumer-Authorization", "OAuth "+s.token) res, err := http.DefaultClient.Do(req) if err != nil { return err } var auth struct { PassportHost string `json:"passport_host"` Status string `json:"status"` TrackId string `json:"track_id"` } if err = json.NewDecoder(res.Body).Decode(&auth); err != nil { return err } if auth.Status != "ok" { return errors.New("yandex: login error: " + auth.Status) } s.client = &http.Client{Timeout: 15 * time.Second} s.client.CheckRedirect = func(req *http.Request, via []*http.Request) error { return http.ErrUseLastResponse } s.client.Jar, _ = cookiejar.New(nil) res, err = s.client.Get(auth.PassportHost + "/auth/session/?track_id=" + auth.TrackId) if err != nil { return err } s.client.CheckRedirect = nil return nil } func (s *Session) Get(url string) (*http.Response, error) { return s.client.Get(url) } func (s *Session) GetCSRF() (string, error) { res, err := s.Get("https://yandex.ru/quasar") if err != nil { return "", err } body, err := io.ReadAll(res.Body) if err != nil { return "", err } token := core.Between(string(body), `"csrfToken2":"`, `"`) return token, nil } func (s *Session) GetCookieString(url string) string { req, err := http.NewRequest("GET", url, nil) if err != nil { return "" } for _, cookie := range s.client.Jar.Cookies(req.URL) { req.AddCookie(cookie) } return req.Header.Get("Cookie") } func (s *Session) GetDevices() ([]Device, error) { res, err := s.Get("https://iot.quasar.yandex.ru/m/v3/user/devices") if err != nil { return nil, err } var data struct { Households []struct { All []Device `json:"all"` } `json:"households"` } if err = json.NewDecoder(res.Body).Decode(&data); err != nil { return nil, err } var devices []Device for _, household := range data.Households { devices = append(devices, household.All...) } return devices, nil } func (s *Session) GetSnapshotURL(deviceID string) (string, error) { devices, err := s.GetDevices() if err != nil { return "", err } for _, device := range devices { if device.Id == deviceID { return device.Parameters.SnapshotUrl, nil } } return "", errors.New("yandex: can't get snapshot url for device: " + deviceID) } func (s *Session) WebrtcCreateRoom(deviceID string) (*Room, error) { csrf, err := s.GetCSRF() if err != nil { return nil, err } req, err := http.NewRequest( "POST", "https://iot.quasar.yandex.ru/m/v3/user/devices/"+deviceID+"/webrtc/create-room", strings.NewReader(`{"protocol":"whip"}`), ) if err != nil { return nil, err } req.Header.Add("Content-Type", "application/json") req.Header.Add("X-CSRF-Token", csrf) res, err := s.client.Do(req) if err != nil { return nil, err } var data struct { Result Room `json:"result"` } if err = json.NewDecoder(res.Body).Decode(&data); err != nil { return nil, err } return &data.Result, nil } type Device struct { Id string `json:"id"` Name string `json:"name"` Type string `json:"type"` Parameters struct { SnapshotUrl string `json:"snapshot_url,omitempty"` } `json:"parameters"` } type Room struct { ServiceUrl string `json:"service_url"` ServiceName string `json:"service_name"` RoomId string `json:"room_id"` ParticipantId string `json:"participant_id"` Credentials string `json:"jwt"` } ================================================ FILE: scripts/README.md ================================================ # Scripts This folder contains a script for building binaries for all platforms. The project has no `CGO` dependencies, so building is as simple as possible using the `go build` command. The project has to use the latest versions of go due to dependencies on third-party go libraries. Such as `pion/webrtc` or `golang.org/x`. Unfortunately, this breaks compatibility with older versions of operating systems. The project uses [UPX](https://github.com/upx/upx) to compress binaries for Linux. The project does not use compression for Windows due to false antivirus alarms. The project does not use compression for macOS due to broken result. ## Useful commands ``` go get -u go mod tidy go mod why github.com/pion/rtcp go list -deps .\cmd\go2rtc_rtsp\ ./goweight ``` ## Dependencies ``` - gopkg.in/yaml.v3 - github.com/kr/pretty - github.com/AlexxIT/go2rtc/pkg/hap - github.com/tadglines/go-pkgs - golang.org/x/crypto - github.com/AlexxIT/go2rtc/pkg/mdns - github.com/miekg/dns - github.com/AlexxIT/go2rtc/pkg/pcm - github.com/sigurn/crc16 - github.com/sigurn/crc8 - github.com/pion/ice/v2 - github.com/google/uuid - github.com/wlynxg/anet - github.com/rs/zerolog - github.com/mattn/go-colorable - github.com/mattn/go-isatty - github.com/stretchr/testify - github.com/davecgh/go-spew - github.com/pmezard/go-difflib - ??? - golang.org/x/mod - golang.org/x/net - golang.org/x/sys - golang.org/x/tools ``` ## Licenses - github.com/asticode/go-astits - MIT - github.com/eclipse/paho.mqtt.golang - EPL-2.0 - github.com/expr-lang/expr - MIT - github.com/gorilla/websocket - BSD-2 - github.com/mattn/go-isatty - MIT - github.com/miekg/dns - BSD-3 - github.com/pion/dtls - MIT - github.com/pion/ice - MIT - github.com/pion/interceptor - MIT - github.com/pion/rtcp - MIT - github.com/pion/rtp - MIT - github.com/pion/sdp - MIT - github.com/pion/srtp - MIT - github.com/pion/stun - MIT - github.com/pion/webrtc - MIT - github.com/rs/zerolog - MIT - github.com/sigurn/crc16 - MIT - github.com/sigurn/crc8 - MIT - github.com/stretchr/testify - MIT - github.com/tadglines/go-pkgs - Apache - golang.org/x/crypto - BSD-3 - gopkg.in/yaml.v3 - MIT and Apache - github.com/asticode/go-astikit - MIT - github.com/davecgh/go-spew - ISC (BSD/MIT like) - github.com/google/uuid - BSD-3 - github.com/kr/pretty - MIT - github.com/mattn/go-colorable - MIT - github.com/pion/datachannel - MIT - github.com/pion/logging - MIT - github.com/pion/mdns - MIT - github.com/pion/randutil - MIT - github.com/pion/sctp - MIT - github.com/pmezard/go-difflib - ??? - github.com/wlynxg/anet - BSD-3 - golang.org/x/mod - BSD-3 - golang.org/x/net - BSD-3 - golang.org/x/sync - BSD-3 - golang.org/x/sys - BSD-3 - golang.org/x/tools - BSD-3 ## Virus - https://go.dev/doc/faq#virus - https://groups.google.com/g/golang-nuts/c/lPwiWYaApSU ## Useful links - https://github.com/golang-standards/project-layout - https://github.com/micro/micro - https://github.com/golang/go/wiki/GoArm - https://gist.github.com/asukakenji/f15ba7e588ac42795f421b48b8aede63 - https://en.wikipedia.org/wiki/AArch64 - https://stackoverflow.com/questions/22267189/what-does-the-w-flag-mean-when-passed-in-via-the-ldflags-option-to-the-go-comman ================================================ FILE: scripts/build.cmd ================================================ @ECHO OFF @SET GOOS=windows @SET GOARCH=amd64 @SET FILENAME=go2rtc_win64.zip go build -ldflags "-s -w" -trimpath && 7z a -mx9 -sdel %FILENAME% go2rtc.exe @SET GOOS=windows @SET GOARCH=386 @SET FILENAME=go2rtc_win32.zip go build -ldflags "-s -w" -trimpath && 7z a -mx9 -sdel %FILENAME% go2rtc.exe @SET GOOS=windows @SET GOARCH=arm64 @SET FILENAME=go2rtc_win_arm64.zip go build -ldflags "-s -w" -trimpath && 7z a -mx9 -sdel %FILENAME% go2rtc.exe @SET GOOS=linux @SET GOARCH=amd64 @SET FILENAME=go2rtc_linux_amd64 go build -ldflags "-s -w" -trimpath -o %FILENAME% && upx --best --lzma %FILENAME% @SET GOOS=linux @SET GOARCH=386 @SET FILENAME=go2rtc_linux_i386 go build -ldflags "-s -w" -trimpath -o %FILENAME% && upx --best --lzma %FILENAME% @SET GOOS=linux @SET GOARCH=arm64 @SET FILENAME=go2rtc_linux_arm64 go build -ldflags "-s -w" -trimpath -o %FILENAME% && upx --best --lzma %FILENAME% @SET GOOS=linux @SET GOARCH=arm @SET GOARM=7 @SET FILENAME=go2rtc_linux_arm go build -ldflags "-s -w" -trimpath -o %FILENAME% && upx --best --lzma %FILENAME% @SET GOOS=linux @SET GOARCH=arm @SET GOARM=6 @SET FILENAME=go2rtc_linux_armv6 go build -ldflags "-s -w" -trimpath -o %FILENAME% && upx --best --lzma %FILENAME% @SET GOOS=linux @SET GOARCH=mipsle @SET FILENAME=go2rtc_linux_mipsel go build -ldflags "-s -w" -trimpath -o %FILENAME% && upx --best --lzma %FILENAME% @SET GOOS=darwin @SET GOARCH=amd64 @SET FILENAME=go2rtc_mac_amd64.zip go build -ldflags "-s -w" -trimpath && 7z a -mx9 -sdel %FILENAME% go2rtc @SET GOOS=darwin @SET GOARCH=arm64 @SET FILENAME=go2rtc_mac_arm64.zip go build -ldflags "-s -w" -trimpath && 7z a -mx9 -sdel %FILENAME% go2rtc @SET GOOS=freebsd @SET GOARCH=amd64 @SET FILENAME=go2rtc_freebsd_amd64.zip go build -ldflags "-s -w" -trimpath && 7z a -mx9 -sdel %FILENAME% go2rtc @SET GOOS=freebsd @SET GOARCH=arm64 @SET FILENAME=go2rtc_freebsd_arm64.zip go build -ldflags "-s -w" -trimpath && 7z a -mx9 -sdel %FILENAME% go2rtc ================================================ FILE: scripts/build.sh ================================================ #!/bin/sh set -e # Exit immediately if a command exits with a non-zero status. set -u # Treat unset variables as an error when substituting. check_command() { if ! command -v "$1" >/dev/null then echo "Error: $1 could not be found. Please install it." >&2 return 1 fi } build_zip() { go build -ldflags "-s -w" -trimpath -o $2 7z a -mx9 -sdel $1 $2 } build_upx() { go build -ldflags "-s -w" -trimpath -o $1 upx --best --lzma $1 } check_command go check_command 7z check_command upx export CGO_ENABLED=0 set -x # Print commands and their arguments as they are executed. GOOS=windows GOARCH=amd64 build_zip go2rtc_win64.zip go2rtc.exe GOOS=windows GOARCH=386 build_zip go2rtc_win32.zip go2rtc.exe GOOS=windows GOARCH=arm64 build_zip go2rtc_win_arm64.zip go2rtc.exe GOOS=linux GOARCH=amd64 build_upx go2rtc_linux_amd64 GOOS=linux GOARCH=386 build_upx go2rtc_linux_i386 GOOS=linux GOARCH=arm64 build_upx go2rtc_linux_arm64 GOOS=linux GOARCH=mipsle build_upx go2rtc_linux_mipsel GOOS=linux GOARCH=arm GOARM=7 build_upx go2rtc_linux_arm GOOS=linux GOARCH=arm GOARM=6 build_upx go2rtc_linux_armv6 GOOS=darwin GOARCH=amd64 build_zip go2rtc_mac_amd64.zip go2rtc GOOS=darwin GOARCH=arm64 build_zip go2rtc_mac_arm64.zip go2rtc GOOS=freebsd GOARCH=amd64 build_zip go2rtc_freebsd_amd64.zip go2rtc GOOS=freebsd GOARCH=arm64 build_zip go2rtc_freebsd_arm64.zip go2rtc ================================================ FILE: website/.vitepress/config.js ================================================ import {defineConfig} from 'vitepress'; function replace_link(md) { md.core.ruler.after('inline', 'replace-link', function (state) { for (const block of state.tokens) { if (block.type === 'inline' && block.children) { for (const token of block.children) { const href = token.attrGet('href'); if (href && href.indexOf('README.md') >= 0) { // token.attrJoin('style', 'color:red;'); token.attrSet('href', href.replace('README.md', 'index.md')); } } } } return true; }); } export default defineConfig({ title: 'go2rtc', description: 'Ultimate camera streaming application', head: [ // first line (green bold) of Telegram card, autodetect from hostname ['meta', { property: 'og:site_name', content: 'go2rtc.org' }], // second line of Telegram card (black bold), autodetect from site description ['meta', { property: 'og:title', content: 'go2rtc - Ultimate camera streaming application' }], // third line of Telegram card, autodetect from site description ['meta', { property: 'og:description', content: 'Support alsa, doorbird, dvrip, eseecloud, ffmpeg, gopro, hass, hls, homekit, mjpeg, mp4, mpegts, nest, onvif, ring, roborock, rtmp, rtsp, tapo, vigi, tuya, v4l2, webrtc, wyze, xiaomi.' }], ['meta', { property: 'og:url', content: 'https://go2rtc.org/' }], ['meta', { property: 'og:image', content: 'https://go2rtc.org/images/logo.png' }], // important for Telegram - the image will be at the bottom and large ['meta', { property: 'twitter:card', content: 'summary_large_image' }], ], sitemap: {hostname: 'https://go2rtc.org'}, themeConfig: { nav: [ {text: 'Home', link: '/'}, ], sidebar: [ { items: [ {text: 'Installation', link: '/#installation'}, {text: 'Configuration', link: '/#configuration'}, {text: 'Security', link: '/#security'}, ], }, { text: 'Features', items: [ {text: 'Streaming input', link: '/#streaming-input'}, {text: 'Streaming output', link: '/#streaming-output'}, {text: 'Streaming ingest', link: '/#streaming-ingest'}, {text: 'Two-way audio', link: '/#two-way-audio'}, {text: 'Stream to camera', link: '/#stream-to-camera'}, {text: 'Publish stream', link: '/#publish-stream'}, {text: 'Preload stream', link: '/#preload-stream'}, {text: 'Streaming stats', link: '/#streaming-stats'}, ], collapsed: false, }, { text: 'Codecs', items: [ {text: 'Codecs filters', link: '/#codecs-filters'}, {text: 'Codecs madness', link: '/#codecs-madness'}, {text: 'Built-in transcoding', link: '/#built-in-transcoding'}, {text: 'Codecs negotiation', link: '/#codecs-negotiation'}, ], collapsed: true, }, { text: 'Other', items: [ {text: 'Projects using go2rtc', link: '/#projects-using-go2rtc'}, {text: 'Camera experience', link: '/#camera-experience'}, {text: 'Tips', link: '/#tips'}, ], collapsed: true, }, { text: 'Core modules', items: [ {text: 'app', link: '/internal/app/'}, {text: 'api', link: '/internal/api/'}, {text: 'streams', link: '/internal/streams/'}, ], collapsed: false, }, { text: 'Main modules', items: [ {text: 'http', link: '/internal/http/'}, {text: 'mjpeg', link: '/internal/mjpeg/'}, {text: 'mp4', link: '/internal/mp4/'}, {text: 'rtsp', link: '/internal/rtsp/'}, {text: 'webrtc', link: '/internal/webrtc/'}, ], collapsed: false, }, { text: 'Other modules', items: [ {text: 'hls', link: '/internal/hls/'}, {text: 'homekit', link: '/internal/homekit/'}, {text: 'onvif', link: '/internal/onvif/'}, {text: 'rtmp', link: '/internal/rtmp/'}, {text: 'webtorrent', link: '/internal/webtorrent/'}, {text: 'wyoming', link: '/internal/wyoming/'}, ], collapsed: false, }, { text: 'Script sources', items: [ {text: 'echo', link: '/internal/echo/'}, {text: 'exec', link: '/internal/exec/'}, {text: 'expr', link: '/internal/expr/'}, {text: 'ffmpeg', link: '/internal/ffmpeg/'}, ], collapsed: false, }, { text: 'Other sources', items: [ {text: 'alsa', link: '/internal/alsa/'}, {text: 'bubble', link: '/internal/bubble/'}, {text: 'doorbird', link: '/internal/doorbird/'}, {text: 'dvrip', link: '/internal/dvrip/'}, {text: 'eseecloud', link: '/internal/eseecloud/'}, {text: 'flussonic', link: '/internal/flussonic/'}, {text: 'gopro', link: '/internal/gopro/'}, {text: 'hass', link: '/internal/hass/'}, {text: 'isapi', link: '/internal/isapi/'}, {text: 'ivideon', link: '/internal/ivideon/'}, {text: 'kasa', link: '/internal/kasa/'}, {text: 'mpeg', link: '/internal/mpeg/'}, {text: 'multitrans', link: '/internal/multitrans/'}, {text: 'nest', link: '/internal/nest/'}, {text: 'ring', link: '/internal/ring/'}, {text: 'roborock', link: '/internal/roborock/'}, {text: 'tapo', link: '/internal/tapo/'}, {text: 'tuya', link: '/internal/tuya/'}, {text: 'v4l2', link: '/internal/v4l2/'}, {text: 'wyze', link: '/internal/wyze/'}, {text: 'xiaomi', link: '/internal/xiaomi/'}, {text: 'yandex', link: '/internal/yandex/'}, ], collapsed: false, }, { text: 'Helper modules', items: [ {text: 'debug', link: '/internal/debug/'}, {text: 'ngrok', link: '/internal/ngrok/'}, {text: 'pinggy', link: '/internal/pinggy/'}, {text: 'srtp', link: '/internal/srtp/'}, ], collapsed: false, }, ], socialLinks: [ {icon: 'github', link: 'https://github.com/AlexxIT/go2rtc'} ], outline: [2, 3], search: {provider: 'local'}, }, rewrites(id) { // change file names return id.replace('README.md', 'index.md'); }, markdown: { config: (md) => { // change markdown links md.use(replace_link); } }, srcDir: '..', srcExclude: ['examples/', 'pkg/'], // cleanUrls: true, ignoreDeadLinks: true, }); ================================================ FILE: website/README.md ================================================ # WebSite These are the sources of the [go2rtc.org](https://go2rtc.org/) website. It's content published on GitHub Pages and is a mirror of [alexxit.github.io/go2rtc/](http://alexxit.github.io/go2rtc/). The site contains: - Project's documentation, which is compiled via [vitepress](https://github.com/vuejs/vitepress) from `README.md` files located in the root of the repository, as well as in the `internal` folder. - Project's API in OpenAPI format, and the [Redoc](https://github.com/Redocly/redoc) viewer - Project's assets (logo). ================================================ FILE: website/api/index.html ================================================ API | go2rtc ================================================ FILE: website/api/openapi.yaml ================================================ openapi: 3.1.0 info: title: go2rtc version: 1.9.13 license: { name: MIT,url: https://opensource.org/licenses/MIT } contact: { url: https://github.com/AlexxIT/go2rtc } description: | Ultimate camera streaming application with support RTSP, RTMP, HTTP-FLV, WebRTC, MSE, HLS, MP4, MJPEG, HomeKit, FFmpeg, etc. servers: - url: http://localhost:1984 tags: - name: Application description: "[Module: API](https://github.com/AlexxIT/go2rtc#module-api)" - name: Config description: "[Configuration](https://github.com/AlexxIT/go2rtc#configuration)" - name: Streams list description: "[Module: Streams](https://github.com/AlexxIT/go2rtc#module-streams)" - name: Consume stream - name: HLS - name: Snapshot - name: Produce stream - name: WebSocket description: "WebSocket API endpoint: `/api/ws` (see `api/README.md`)" - name: Discovery - name: HomeKit - name: ONVIF - name: RTSPtoWebRTC - name: WebTorrent description: "[Module: WebTorrent](https://github.com/AlexxIT/go2rtc#module-webtorrent)" - name: FFmpeg - name: Debug components: parameters: stream_src_path: name: src in: path description: Source stream name required: true schema: { type: string } example: camera1 stream_dst_path: name: dst in: path description: Destination stream name required: true schema: { type: string } example: camera1 stream_src_query: name: src in: query description: Source stream name required: true schema: { type: string } example: camera1 hls_session_id_path: name: id in: path description: HLS session ID (passed as query param `id`) required: true schema: { type: string } example: DvmHdd9w mp4_filter: name: mp4 in: query description: MP4 codecs filter required: false schema: type: string enum: [ "", flac, all ] example: flac video_filter: name: video in: query description: Video codecs filter schema: type: string enum: [ "", all, h264, h265, mjpeg ] example: h264,h265 audio_filter: name: audio in: query description: Audio codecs filter schema: type: string enum: [ "", all, aac, opus, pcm, pcmu, pcma ] example: aac responses: discovery: description: "" content: application/json: example: { streams: [ { "name": "Camera 1","url": "..." } ] } webtorrent: description: "" content: application/json: example: { share: AKDypPy4zz, pwd: H0Km1HLTTP } paths: /api: get: summary: Get application info tags: [ Application ] responses: "200": description: "" content: application/json: schema: type: object properties: config_path: { type: string, example: "/config/go2rtc.yaml" } host: { type: string, example: "192.168.1.123:1984" } rtsp: type: object properties: listen: { type: string, example: ":8554" } default_query: { type: string, example: "video&audio" } version: { type: string, example: "1.9.12" } /api/exit: post: summary: Close application tags: [ Application ] parameters: - name: code in: query description: Application exit code required: false schema: { type: integer } example: 100 responses: default: description: "" /api/restart: post: summary: Restart daemon description: Restarts the daemon. tags: [ Application ] responses: default: description: "" /api/log: get: summary: Get in-memory logs buffer description: | Returns current log output from the in-memory circular buffer. tags: [ Application ] responses: "200": description: OK content: application/jsonlines: example: | {"level":"info","version":"1.9.13","platform":"linux/amd64","revision":"dfe4755","time":1766841087331,"message":"go2rtc"} delete: summary: Clear in-memory logs buffer tags: [ Application ] responses: "200": description: "" content: text/plain: { example: "" } /api/config: get: summary: Get main config file content tags: [ Config ] responses: "200": description: "" content: application/yaml: { example: "streams:..." } "404": description: Config file not found post: summary: Rewrite main config file tags: [ Config ] requestBody: content: "*/*": { example: "streams:..." } responses: default: description: "" patch: summary: Merge changes to main config file tags: [ Config ] requestBody: content: "*/*": { example: "streams:..." } responses: default: description: "" /api/streams: get: summary: Get all streams info tags: [ Streams list ] responses: "200": description: "" content: application/json: schema: type: object additionalProperties: type: object properties: producers: type: array consumers: type: array put: summary: Create new stream tags: [ Streams list ] parameters: - name: src in: query description: Stream source (URI) required: true schema: { type: string } example: "rtsp://rtsp:12345678@192.168.1.123/av_stream/ch0" - name: name in: query description: Stream name required: false schema: { type: string } example: camera1 responses: default: description: "" patch: summary: Update stream source tags: [ Streams list ] parameters: - name: src in: query description: Stream source (URI) required: true schema: { type: string } example: "rtsp://rtsp:12345678@192.168.1.123/av_stream/ch0" - name: name in: query description: Stream name required: true schema: { type: string } example: camera1 responses: default: description: "" delete: summary: Delete stream tags: [ Streams list ] parameters: - name: src in: query description: Stream name required: true schema: { type: string } example: camera1 responses: default: description: "" post: summary: Send stream from source to destination description: "[Stream to camera](https://github.com/AlexxIT/go2rtc#stream-to-camera)" tags: [ Streams list ] parameters: - name: src in: query description: Stream source (URI) required: true schema: { type: string } example: "ffmpeg:http://example.com/song.mp3#audio=pcma#input=file" - name: dst in: query description: Destination stream name required: true schema: { type: string } example: camera1 responses: default: description: "" /api/streams.dot: get: summary: Get streams graph in Graphviz DOT format tags: [ Streams list ] parameters: - name: src in: query description: Stream name filter. Repeat `src` to include multiple streams. required: false schema: { type: string } example: camera1 responses: "200": description: OK content: text/vnd.graphviz: example: "digraph { ... }" /api/preload: get: summary: Get all preloaded streams tags: [ Streams list ] responses: "200": description: "" content: application/json: schema: type: object additionalProperties: type: object properties: consumer: type: object query: type: string example: "video&audio" put: summary: Preload new stream tags: [ Streams list ] parameters: - name: src in: query description: Stream source (name) required: true schema: { type: string } example: "camera1" - name: video in: query description: Video codecs filter required: false schema: { type: string } example: all,h264,h265,... - name: audio in: query description: Audio codecs filter required: false schema: { type: string } example: all,aac,opus,... - name: microphone in: query description: Microphone codecs filter required: false schema: { type: string } example: all,aac,opus,... responses: default: description: "" delete: summary: Delete preloaded stream tags: [ Streams list ] parameters: - name: src in: query description: Stream source (name) required: true schema: { type: string } example: "camera1" responses: default: description: "" /api/schemes: get: summary: Get supported source URL schemes tags: [ Streams list ] responses: "200": description: OK content: application/json: schema: type: array items: { type: string } example: [ rtsp, rtmp, webrtc, ffmpeg, hass ] /api/streams?src={src}: get: summary: Get stream info in JSON format tags: [ Consume stream ] parameters: - $ref: "#/components/parameters/stream_src_path" responses: "200": description: "" content: application/json: schema: type: object additionalProperties: type: object properties: producers: type: array items: { type: object } consumers: type: array items: { type: object } /api/webrtc?src={src}: post: summary: Get stream in WebRTC format (WHEP) description: "[Module: WebRTC](https://github.com/AlexxIT/go2rtc#module-webrtc)" tags: [ Consume stream ] parameters: - $ref: "#/components/parameters/stream_src_path" requestBody: description: | Support: - JSON format (`Content-Type: application/json`) - WHEP standard (`Content-Type: application/sdp`) - raw SDP (`Content-Type: anything`) required: true content: application/json: { example: { type: offer, sdp: "v=0..." } } "application/sdp": { example: "v=0..." } "*/*": { example: "v=0..." } responses: "200": description: "Response on JSON or raw SDP" content: application/json: { example: { type: answer, sdp: "v=0..." } } application/sdp: { example: "v=0..." } "201": description: "Response on `Content-Type: application/sdp`" content: application/sdp: { example: "v=0..." } /api/stream.mp4?src={src}: get: summary: Get stream in MP4 format (HTTP progressive) description: "[Module: MP4](https://github.com/AlexxIT/go2rtc#module-mp4)" tags: [ Consume stream ] parameters: - $ref: "#/components/parameters/stream_src_path" - name: duration in: query description: Limit the length of the stream in seconds required: false schema: { type: string } example: 15 - name: filename in: query description: Download as a file with this name required: false schema: { type: string } example: camera1.mp4 - name: rotate in: query description: "Rotate video (degrees). Supported values: 90, 180, 270." required: false schema: { type: integer, enum: [ 90, 180, 270 ] } - name: scale in: query description: Scale video in format `width:height` required: false schema: { type: string, example: "1280:720" } - $ref: "#/components/parameters/mp4_filter" - $ref: "#/components/parameters/video_filter" - $ref: "#/components/parameters/audio_filter" responses: 200: description: "" content: { video/mp4: { example: "" } } /api/stream.m3u8?src={src}: get: summary: Get stream in HLS format description: "[Module: HLS](https://github.com/AlexxIT/go2rtc#module-hls)" tags: [ Consume stream, HLS ] parameters: - $ref: "#/components/parameters/stream_src_path" - $ref: "#/components/parameters/mp4_filter" - $ref: "#/components/parameters/video_filter" - $ref: "#/components/parameters/audio_filter" responses: 200: description: "" content: { application/vnd.apple.mpegurl: { example: "" } } /api/hls/playlist.m3u8?id={id}: get: summary: Get HLS media playlist for an active session tags: [ HLS ] parameters: - $ref: "#/components/parameters/hls_session_id_path" responses: "200": description: OK content: application/vnd.apple.mpegurl: { example: "" } "404": description: Session not found /api/hls/segment.ts?id={id}: get: summary: Get HLS MPEG-TS segment for an active session tags: [ HLS ] parameters: - $ref: "#/components/parameters/hls_session_id_path" responses: "200": description: OK content: video/mp2t: { example: "" } "404": description: Segment or session not found /api/hls/init.mp4?id={id}: get: summary: Get HLS fMP4 init segment for an active session tags: [ HLS ] parameters: - $ref: "#/components/parameters/hls_session_id_path" responses: "200": description: OK content: video/mp4: { example: "" } "404": description: Segment or session not found /api/hls/segment.m4s?id={id}: get: summary: Get HLS fMP4 media segment for an active session tags: [ HLS ] parameters: - $ref: "#/components/parameters/hls_session_id_path" responses: "200": description: OK content: video/iso.segment: { example: "" } "404": description: Segment or session not found /api/stream.mjpeg?src={src}: get: summary: Get stream in MJPEG format description: "[Module: MJPEG](https://github.com/AlexxIT/go2rtc#module-mjpeg)" tags: [ Consume stream ] parameters: - $ref: "#/components/parameters/stream_src_path" responses: 200: description: "" content: { multipart/x-mixed-replace: { example: "" } } /api/stream.ascii?src={src}: get: summary: Get stream in ASCII-art format (ANSI escape codes) description: "[Module: MJPEG](https://github.com/AlexxIT/go2rtc#module-mjpeg)" tags: [ Consume stream ] parameters: - $ref: "#/components/parameters/stream_src_path" - name: color in: query description: Foreground mode (`8`, `256`, `rgb` or ANSI SGR code) required: false schema: { type: string } - name: back in: query description: Background mode (`8`, `256`, `rgb` or ANSI SGR code) required: false schema: { type: string } - name: text in: query description: Charset preset (empty/default, `block`) or custom characters required: false schema: { type: string } responses: "200": description: OK content: text/plain: { example: "" } "404": description: Stream not found /api/stream.y4m?src={src}: get: summary: Get stream in YUV4MPEG2 format (y4m) tags: [ Consume stream ] parameters: - $ref: "#/components/parameters/stream_src_path" responses: "200": description: OK content: application/octet-stream: { example: "" } "404": description: Stream not found /api/stream.ts?src={src}: get: summary: Get stream in MPEG-TS format tags: [ Consume stream ] parameters: - $ref: "#/components/parameters/stream_src_path" responses: "200": description: OK content: video/mp2t: { example: "" } "404": description: Stream not found /api/stream.aac?src={src}: get: summary: Get stream audio in AAC (ADTS) format tags: [ Consume stream ] parameters: - $ref: "#/components/parameters/stream_src_path" responses: "200": description: OK content: audio/aac: { example: "" } "404": description: Stream not found /api/stream.flv?src={src}: get: summary: Get stream in FLV format tags: [ Consume stream ] parameters: - $ref: "#/components/parameters/stream_src_path" responses: "200": description: OK content: video/x-flv: { example: "" } "404": description: Stream not found /api/frame.jpeg?src={src}: get: summary: Get snapshot in JPEG format description: "[Module: MJPEG](https://github.com/AlexxIT/go2rtc#module-mjpeg)" tags: [ Snapshot ] parameters: - $ref: "#/components/parameters/stream_src_path" - name: name in: query description: Optional stream name to create/update if `src` is a URL required: false schema: { type: string } - name: width in: query description: "Scale output width (alias: `w`)" required: false schema: { type: integer, minimum: 1 } - name: height in: query description: "Scale output height (alias: `h`)" required: false schema: { type: integer, minimum: 1 } - name: rotate in: query description: "Rotate output (degrees). Supported values: 90, 180, 270." required: false schema: { type: integer, enum: [ 90, 180, 270 ] } - name: hardware in: query description: "Hardware acceleration engine for FFmpeg snapshot transcoding (alias: `hw`)" required: false schema: { type: string } responses: "200": description: "" content: image/jpeg: { example: "" } /api/frame.mp4?src={src}: get: summary: Get snapshot in MP4 format description: "[Module: MP4](https://github.com/AlexxIT/go2rtc#module-mp4)" tags: [ Snapshot ] parameters: - $ref: "#/components/parameters/stream_src_path" - name: filename in: query description: Download as a file with this name required: false schema: { type: string } example: camera1.mp4 responses: 200: description: "" content: video/mp4: { example: "" } /api/webrtc?dst={dst}: post: summary: Post stream in WebRTC format (WHIP) description: "[Incoming: WebRTC/WHIP](https://github.com/AlexxIT/go2rtc#incoming-webrtcwhip)" tags: [ Produce stream ] parameters: - $ref: "#/components/parameters/stream_dst_path" responses: "201": description: Created headers: Location: description: Resource URL for session schema: { type: string } content: application/sdp: { example: "v=0..." } "404": description: Stream not found /api/stream?dst={dst}: post: summary: Post stream in auto-detected format description: | Incoming source with automatic format detection. Use for pushing a stream into an existing `dst` stream. tags: [ Produce stream ] parameters: - $ref: "#/components/parameters/stream_dst_path" responses: default: description: "" /api/stream.flv?dst={dst}: post: summary: Post stream in FLV format description: "[Incoming sources](https://github.com/AlexxIT/go2rtc#incoming-sources)" tags: [ Produce stream ] parameters: - $ref: "#/components/parameters/stream_dst_path" responses: default: description: "" /api/stream.ts?dst={dst}: post: summary: Post stream in MPEG-TS format description: "[Incoming sources](https://github.com/AlexxIT/go2rtc#incoming-sources)" tags: [ Produce stream ] parameters: - $ref: "#/components/parameters/stream_dst_path" responses: default: description: "" /api/stream.mjpeg?dst={dst}: post: summary: Post stream in MJPEG format description: "[Incoming sources](https://github.com/AlexxIT/go2rtc#incoming-sources)" tags: [ Produce stream ] parameters: - $ref: "#/components/parameters/stream_dst_path" responses: default: description: "" /api/ffmpeg: post: summary: Play file/live/TTS into a stream via FFmpeg description: | Helper endpoint for "stream to camera" scenarios. Exactly one of `file`, `live`, `text` should be provided. tags: [ FFmpeg ] parameters: - name: dst in: query description: Destination stream name required: true schema: { type: string } example: camera1 - name: file in: query description: Input URL to treat as file (`#input=file`) required: false schema: { type: string } example: "http://example.com/song.mp3" - name: live in: query description: Live input URL required: false schema: { type: string } example: "http://example.com/live.mp3" - name: text in: query description: Text-to-speech phrase required: false schema: { type: string } example: "Hello" - name: voice in: query description: Optional TTS voice (engine-dependent) required: false schema: { type: string } responses: "200": description: OK "400": description: Invalid parameters "404": description: Stream not found /api/dvrip: get: summary: DVRIP cameras discovery description: "[Source: DVRIP](https://github.com/AlexxIT/go2rtc#source-dvrip)" tags: [ Discovery ] responses: "200": { $ref: "#/components/responses/discovery" } /api/ffmpeg/devices: get: summary: FFmpeg USB devices discovery description: "[Source: FFmpeg Device](https://github.com/AlexxIT/go2rtc#source-ffmpeg-device)" tags: [ Discovery ] responses: "200": { $ref: "#/components/responses/discovery" } /api/ffmpeg/hardware: get: summary: FFmpeg hardware transcoding discovery description: "[Hardware acceleration](https://github.com/AlexxIT/go2rtc/wiki/Hardware-acceleration)" tags: [ Discovery ] responses: "200": { $ref: "#/components/responses/discovery" } /api/v4l2: get: summary: V4L2 video devices discovery (Linux) tags: [ Discovery ] responses: "200": { $ref: "#/components/responses/discovery" } /api/alsa: get: summary: ALSA audio devices discovery (Linux) tags: [ Discovery ] responses: "200": { $ref: "#/components/responses/discovery" } /api/gopro: get: summary: GoPro cameras discovery tags: [ Discovery ] responses: "200": { $ref: "#/components/responses/discovery" } /api/ring: get: summary: Ring cameras discovery description: | Provide either `email`/`password` (and optional `code` for 2FA) or `refresh_token`. If 2FA is required, returns a JSON prompt instead of sources. tags: [ Discovery ] parameters: - name: email in: query required: false schema: { type: string } - name: password in: query required: false schema: { type: string } - name: code in: query required: false schema: { type: string } - name: refresh_token in: query required: false schema: { type: string } responses: "200": description: OK content: application/json: { example: "" } /api/tuya: get: summary: Tuya cameras discovery tags: [ Discovery ] parameters: - name: region in: query description: Tuya API host (region) required: true schema: { type: string } example: "openapi.tuyaus.com" - name: email in: query required: true schema: { type: string } - name: password in: query required: true schema: { type: string } responses: "200": { $ref: "#/components/responses/discovery" } "400": description: Invalid parameters "404": description: No cameras found /api/hass: get: summary: Home Assistant cameras discovery description: "[Source: Hass](https://github.com/AlexxIT/go2rtc#source-hass)" tags: [ Discovery ] responses: "200": { $ref: "#/components/responses/discovery" } "404": { description: No Hass config } /api/discovery/homekit: get: summary: HomeKit cameras discovery description: "[Source: HomeKit](https://github.com/AlexxIT/go2rtc#source-homekit)" tags: [ Discovery ] responses: "200": { $ref: "#/components/responses/discovery" } /api/nest: get: summary: Nest cameras discovery tags: [ Discovery ] parameters: - name: client_id in: query required: true schema: { type: string } - name: client_secret in: query required: true schema: { type: string } - name: refresh_token in: query required: true schema: { type: string } - name: project_id in: query required: true schema: { type: string } responses: "200": { $ref: "#/components/responses/discovery" } /api/onvif: get: summary: ONVIF cameras discovery description: "[Source: ONVIF](https://github.com/AlexxIT/go2rtc#source-onvif)" tags: [ Discovery ] parameters: - name: src in: query description: Optional ONVIF device URL to enumerate profiles required: false schema: { type: string } example: "onvif://user:pass@192.168.1.50:80" responses: "200": { $ref: "#/components/responses/discovery" } /api/roborock: get: summary: Roborock vacuums discovery (requires prior auth) description: "[Source: Roborock](https://github.com/AlexxIT/go2rtc#source-roborock)" tags: [ Discovery ] responses: "200": { $ref: "#/components/responses/discovery" } "404": description: No auth post: summary: Roborock login and discovery tags: [ Discovery ] requestBody: required: true content: multipart/form-data: schema: type: object properties: username: { type: string } password: { type: string } required: [ username, password ] responses: "200": { $ref: "#/components/responses/discovery" } /api/homekit: get: summary: Get HomeKit servers state tags: [ HomeKit ] parameters: - name: id in: query description: Optional stream name (server ID) required: false schema: { type: string } responses: "200": description: OK content: application/json: { example: "" } "404": description: Server not found post: summary: Pair HomeKit camera and create/update stream tags: [ HomeKit ] parameters: - name: id in: query description: Stream name to create/update required: true schema: { type: string } - name: src in: query description: HomeKit URL (without pin) required: true schema: { type: string } - name: pin in: query description: HomeKit PIN required: true schema: { type: string } responses: "200": description: OK delete: summary: Unpair HomeKit camera and delete stream tags: [ HomeKit ] parameters: - name: id in: query description: Stream name / server ID required: true schema: { type: string } responses: "200": description: OK "404": description: Stream not found /api/homekit/accessories: get: summary: Get HomeKit accessories JSON for a stream tags: [ HomeKit ] parameters: - name: id in: query description: Stream name required: true schema: { type: string } responses: "200": description: OK content: application/json: { example: { } } "404": description: Stream not found /pair-setup: post: summary: HomeKit Pair Setup (HAP) description: HomeKit Accessory Protocol endpoint (TLV8). tags: [ HomeKit ] requestBody: required: true content: application/pairing+tlv8: { example: "" } responses: "200": description: OK content: application/pairing+tlv8: { example: "" } /pair-verify: post: summary: HomeKit Pair Verify (HAP) description: HomeKit Accessory Protocol endpoint (TLV8). tags: [ HomeKit ] requestBody: required: true content: application/pairing+tlv8: { example: "" } responses: "200": description: OK content: application/pairing+tlv8: { example: "" } /onvif/: get: summary: ONVIF server implementation description: Simple realisation of the ONVIF protocol. Accepts any suburl requests tags: [ ONVIF ] responses: default: description: "" /stream/: get: summary: RTSPtoWebRTC server implementation description: Simple API for support [RTSPtoWebRTC](https://www.home-assistant.io/integrations/rtsp_to_webrtc/) integration tags: [ RTSPtoWebRTC ] responses: default: description: "" /api/ws: get: summary: WebSocket endpoint description: | Upgrade to WebSocket and exchange JSON messages: - Request: `{ "type": "...", "value": ... }` - Response: `{ "type": "...", "value": ... }` Supported message types depend on enabled modules (see `api/README.md`). tags: [ WebSocket ] parameters: - name: src in: query description: Stream name (consumer) required: false schema: { type: string } - name: dst in: query description: Stream name (producer) required: false schema: { type: string } responses: "101": { description: Switching Protocols } /api/webtorrent?src={src}: get: summary: Get WebTorrent share info tags: [ WebTorrent ] parameters: - $ref: "#/components/parameters/stream_src_path" responses: 200: { $ref: "#/components/responses/webtorrent" } post: summary: Add WebTorrent share tags: [ WebTorrent ] parameters: - $ref: "#/components/parameters/stream_src_path" responses: 200: { $ref: "#/components/responses/webtorrent" } delete: summary: Delete WebTorrent share tags: [ WebTorrent ] parameters: - $ref: "#/components/parameters/stream_src_path" responses: default: { description: "" } /api/webtorrent: get: summary: Get all WebTorrent shares info tags: [ WebTorrent ] responses: 200: { $ref: "#/components/responses/discovery" } /api/stack: get: summary: Show list unknown goroutines tags: [ Debug ] responses: 200: description: "" content: { text/plain: { example: "" } } ================================================ FILE: website/manifest.json ================================================ { "name": "go2rtc", "icons": [ { "src": "https://go2rtc.org/icons/android-chrome-192x192.png", "sizes": "192x192", "type": "image/png" }, { "src": "https://go2rtc.org/icons/android-chrome-512x512.png", "sizes": "512x512", "type": "image/png" } ], "display": "standalone", "theme_color": "#000000", "background_color": "#000000" } ================================================ FILE: website/webtorrent/index.html ================================================ webtorrent - go2rtc
================================================ FILE: www/README.md ================================================ # www This folder contains static HTTP and JS content that is embedded into the application during build. An external developer can use it as a basis for integrating go2rtc into their project or for developing a custom web interface for go2rtc. ## HTTP API `www/stream.html` - universal viewer with support params in URL: - multiple streams on page `src=camera1&src=camera2...` - stream technology autoselection `mode=webrtc,webrtc/tcp,mse,hls,mp4,mjpeg` - stream technology comparison `src=camera1&mode=webrtc&mode=mse&mode=mp4` - player width setting in pixels `width=320px` or percents `width=50%` `www/webrtc.html` - WebRTC viewer with support two way audio and params in URL: - `media=video+audio` - simple viewer - `media=video+audio+microphone` - two way audio from camera - `media=camera+microphone` - stream from browser - `media=display+speaker` - stream from desktop ## JavaScript API - You can write your viewer from the scratch - You can extend the built-in viewer - `www/video-rtc.js` - Check example - `www/video-stream.js` - Check example - https://github.com/AlexxIT/WebRTC `video-rtc.js` features: - support technologies: - WebRTC over UDP or TCP - MSE or HLS or MP4 or MJPEG over WebSocket - automatic selection best technology according on: - codecs inside your stream - current browser capabilities - current network configuration - automatic stop stream while browser or page not active - automatic stop stream while player not inside page viewport - automatic reconnection Technology selection based on priorities: 1. Video and Audio better than just Video 2. H265 better than H264 3. WebRTC better than MSE, than HLS, than MJPEG ## Browser support [ECMAScript 2019 (ES10)](https://caniuse.com/?search=es10) supported by [iOS 12](https://en.wikipedia.org/wiki/IOS_12) (iPhone 5S, iPad Air, iPad Mini 2, etc.). But [ECMAScript 2017 (ES8)](https://caniuse.com/?search=es8) almost fine (`es6 + async`) and recommended for [React+TypeScript](https://github.com/typescript-cheatsheets/react). ## Known problems - Autoplay doesn't work for WebRTC in Safari [read more](https://developer.apple.com/documentation/webkit/delivering_video_content_for_safari/). ## Useful links - https://www.webrtc-experiment.com/DetectRTC/ - https://divtable.com/table-styler/ - https://www.chromium.org/audio-video/ - https://web.dev/i18n/en/fast-playback-with-preload/#manual_buffering - https://developer.mozilla.org/en-US/docs/Web/API/Media_Source_Extensions_API - https://chromium.googlesource.com/external/w3c/web-platform-tests/+/refs/heads/master/media-source/mediasource-is-type-supported.html - https://googlechrome.github.io/samples/media/sourcebuffer-changetype.html - https://chromestatus.com/feature/5100845653819392 - https://developer.apple.com/documentation/webkit/delivering_video_content_for_safari - https://dirask.com/posts/JavaScript-supported-Audio-Video-MIME-Types-by-MediaRecorder-Chrome-and-Firefox-jERn81 - https://privacycheck.sec.lrz.de/active/fp_cpt/fp_can_play_type.html ================================================ FILE: www/add.html ================================================ add - go2rtc

API Key required: Get your API Key

================================================ FILE: www/config.html ================================================ config - go2rtc
================================================ FILE: www/hls.html ================================================ hls - go2rtc ================================================ FILE: www/index.html ================================================ go2rtc
modes
online commands
================================================ FILE: www/links.html ================================================ links - go2rtc

Play audio


/ cameras with two way audio support

Publish stream

YouTube:  rtmps://xxx.rtmp.youtube.com/live2/xxxx-xxxx-xxxx-xxxx-xxxx
Telegram: rtmps://xxx-x.rtmp.t.me/s/xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxx
/ Telegram RTMPS server

WebRTC Magic


  • webrtc.html local WebRTC viewer
  • share link copy link delete external WebRTC viewer
  • ================================================ FILE: www/log.html ================================================ log - go2rtc
    Time Level Message
    ================================================ FILE: www/main.js ================================================ document.head.innerHTML += ` `; document.body.innerHTML = `
    ` + document.body.innerHTML; ================================================ FILE: www/net.html ================================================ net - go2rtc
    ================================================ FILE: www/schema.json ================================================ { "$schema": "http://json-schema.org/draft-07/schema#", "title": "go2rtc", "type": "object", "additionalProperties": false, "definitions": { "listen": { "type": "string", "anyOf": [ { "type": "string", "pattern": ":[0-9]{1,5}$" }, { "type": "string", "const": "" } ] }, "log_level": { "type": "string", "enum": [ "trace", "debug", "info", "warn", "error", "fatal", "panic", "disabled" ] }, "source": { "type": "string", "examples": [ "rtsp://username:password@192.168.1.123/cam/realmonitor?channel=1&subtype=0&unicast=true&proto=Onvif", "rtsp://username:password@192.168.1.123/stream1", "rtsp://username:password@192.168.1.123/h264Preview_01_main", "rtmp://192.168.1.123/bcs/channel0_main.bcs?channel=0&stream=0&user=username&password=password", "http://192.168.1.123/flv?port=1935&app=bcs&stream=channel0_main.bcs&user=username&password=password", "http://username:password@192.168.1.123/cgi-bin/snapshot.cgi?channel=1", "ffmpeg:media.mp4#video=h264#hardware#width=1920#height=1080#rotate=180#audio=copy", "ffmpeg:virtual?video=testsrc&size=4K#video=h264#hardware#bitrate=50M", "exec:ffmpeg -re -i media.mp4 -c copy -rtsp_transport tcp -f rtsp {output}", "onvif://username:password@192.168.1.123:80?subtype=0", "tapo://password@192.168.1.123:8800?channel=0&subtype=0" ] } }, "properties": { "api": { "type": "object", "properties": { "listen": { "type": "string", "default": ":1984", "examples": [ "127.0.0.1:1984" ] }, "username": { "description": "Basic auth for WebUI", "type": "string", "examples": [ "admin" ] }, "password": { "type": "string" }, "local_auth": { "description": "Enable auth check for localhost requests", "type": "boolean", "default": false }, "base_path": { "description": "API prefix for serving on suburl (/api => /rtc/api)", "type": "string", "examples": [ "/rtc" ] }, "static_dir": { "description": "Folder for static files (custom web interface)", "type": "string", "examples": [ "www" ] }, "origin": { "description": "Allow CORS requests (only * supported)", "type": "string", "enum": [ "*", "" ] }, "tls_listen": { "type": "string" }, "tls_cert": { "type": "string", "examples": [ "-----BEGIN CERTIFICATE-----", "/ssl/fullchain.pem" ] }, "tls_key": { "type": "string", "examples": [ "-----BEGIN PRIVATE KEY-----", "/ssl/privkey.pem" ] }, "unix_listen": { "type": "string", "examples": [ "/tmp/go2rtc.sock" ] }, "allow_paths": { "description": "Allow only these HTTP paths (full paths, including base_path)", "type": "array", "items": { "type": "string" }, "examples": [ [ "/api", "/api/streams", "/api/webrtc" ] ] } } }, "app": { "type": "object", "properties": { "modules": { "description": "Enable only these modules (empty / omitted means all)", "type": "array", "items": { "type": "string", "enum": [ "api", "ws", "http", "rtsp", "webrtc", "mp4", "hls", "mjpeg", "hass", "homekit", "onvif", "rtmp", "webtorrent", "wyoming", "echo", "exec", "expr", "ffmpeg", "alsa", "v4l2", "bubble", "doorbird", "dvrip", "eseecloud", "flussonic", "gopro", "isapi", "ivideon", "kasa", "mpeg", "nest", "ring", "roborock", "tapo", "tuya", "xiaomi", "yandex", "debug", "ngrok", "pinggy", "srtp" ] } } } }, "env": { "description": "Config variables that can be referenced as ${NAME} / ${NAME:default}", "type": "object", "additionalProperties": { "type": "string" } }, "echo": { "type": "object", "properties": { "allow_paths": { "description": "Allow only these binaries for echo: URLs (exact cmd name/path)", "type": "array", "items": { "type": "string" } } } }, "exec": { "type": "object", "properties": { "allow_paths": { "description": "Allow only these binaries for exec: URLs (exact cmd name/path)", "type": "array", "items": { "type": "string" }, "examples": [ [ "ffmpeg", "/usr/bin/ffmpeg" ] ] } } }, "ffmpeg": { "type": "object", "properties": { "bin": { "type": "string", "default": "ffmpeg" }, "global": { "type": "string", "default": "-hide_banner" }, "file": { "type": "string", "default": "-re -i {input}" }, "http": { "type": "string", "default": "-fflags nobuffer -flags low_delay -i {input}" }, "rtsp": { "type": "string", "default": "-fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -rtsp_flags prefer_tcp -i {input}" }, "rtsp/udp": { "type": "string", "default": "-fflags nobuffer -flags low_delay -timeout 5000000 -user_agent go2rtc/ffmpeg -i {input}" } }, "additionalProperties": { "description": "FFmpeg template", "type": "string" } }, "hass": { "type": "object", "properties": { "config": { "description": "Home Assistant config directory path", "type": "string", "examples": [ "/config" ] } } }, "homekit": { "type": "object", "additionalProperties": { "type": [ "object", "null" ], "properties": { "pin": { "description": "HomeKit pairing PIN", "type": "string", "default": "19550224", "anyOf": [ { "type": "string", "pattern": "^[0-9]{8}$" }, { "type": "string", "pattern": "^[0-9]{3}-[0-9]{2}-[0-9]{3}$" } ] }, "name": { "type": "string" }, "device_id": { "type": "string" }, "device_private": { "type": "string" }, "category_id": { "description": "Accessory category: `bridge`, `doorbell` or numeric ID", "type": "string", "default": "camera", "anyOf": [ { "type": "string", "enum": [ "bridge", "camera", "doorbell" ] }, { "type": "string", "pattern": "^[0-9]+$" }, { "type": "string", "const": "" } ] }, "pairings": { "type": "array", "items": { "type": "string" } } } } }, "log": { "type": "object", "properties": { "format": { "description": "Log format: color/json/text or empty for autodetect", "type": "string", "default": "color", "enum": [ "", "color", "json", "text" ] }, "level": { "description": "Defaul log level", "default": "info", "$ref": "#/definitions/log_level" }, "output": { "description": "Log output: stdout/stderr/file[:path] or empty (memory only)", "type": "string", "default": "stdout", "anyOf": [ { "type": "string", "enum": [ "", "stdout", "stderr" ] }, { "type": "string", "pattern": "^file(:.+)?$", "examples": [ "file", "file:go2rtc.log" ] } ] }, "time": { "type": "string", "default": "UNIXMS", "anyOf": [ { "type": "string", "enum": [ "", "UNIXMS", "UNIXMICRO", "UNIXNANO", "2006-01-02T15:04:05Z07:00", "2006-01-02T15:04:05.999999999Z07:00" ] }, { "type": "string" } ] }, "api": { "$ref": "#/definitions/log_level" }, "echo": { "$ref": "#/definitions/log_level" }, "exec": { "description": "Value `exec: debug` will print stderr", "$ref": "#/definitions/log_level" }, "expr": { "$ref": "#/definitions/log_level" }, "ffmpeg": { "description": "Will only be displayed with `exec: debug` setting", "default": "error", "$ref": "#/definitions/log_level" }, "hass": { "$ref": "#/definitions/log_level" }, "hls": { "$ref": "#/definitions/log_level" }, "homekit": { "$ref": "#/definitions/log_level" }, "mjpeg": { "$ref": "#/definitions/log_level" }, "mp4": { "$ref": "#/definitions/log_level" }, "ngrok": { "$ref": "#/definitions/log_level" }, "onvif": { "$ref": "#/definitions/log_level" }, "rtmp": { "$ref": "#/definitions/log_level" }, "rtsp": { "$ref": "#/definitions/log_level" }, "streams": { "$ref": "#/definitions/log_level" }, "webrtc": { "$ref": "#/definitions/log_level" }, "webtorrent": { "$ref": "#/definitions/log_level" }, "wyoming": { "$ref": "#/definitions/log_level" } } }, "ngrok": { "type": "object", "properties": { "command": { "type": "string", "examples": [ "ngrok tcp 8555 --authtoken xxx", "ngrok start --all --config ngrok.yaml" ] } } }, "pinggy": { "type": "object", "properties": { "tunnel": { "description": "Expose local address via Pinggy", "type": "string", "examples": [ "http://127.0.0.1:1984", "tcp://192.168.1.123:554" ] } } }, "preload": { "description": "Preload streams on startup (map stream name => probe query, default `video&audio`)", "type": "object", "additionalProperties": { "type": "string", "examples": [ "video&audio", "video" ] } }, "publish": { "type": "object", "additionalProperties": { "anyOf": [ { "type": "string", "examples": [ "rtmp://xxx.rtmp.youtube.com/live2/xxxx-xxxx-xxxx-xxxx-xxxx", "rtmps://xxx-x.rtmp.t.me/s/xxxxxxxxxx:xxxxxxxxxxxxxxxxxxxxxx" ] }, { "type": "array", "items": { "type": "string" } } ] } }, "rtmp": { "type": "object", "properties": { "listen": { "type": "string", "examples": [ ":1935" ] } } }, "rtsp": { "type": "object", "properties": { "listen": { "type": "string", "default": ":8554" }, "username": { "type": "string", "examples": [ "admin" ] }, "password": { "type": "string" }, "default_query": { "type": "string", "default": "video&audio" }, "pkt_size": { "type": "integer" } } }, "srtp": { "description": "SRTP server for HomeKit", "type": "object", "properties": { "listen": { "type": "string", "default": ":8443" } } }, "streams": { "type": "object", "additionalProperties": { "anyOf": [ { "$ref": "#/definitions/source" }, { "type": "array", "items": { "$ref": "#/definitions/source" } }, { "type": "null" } ] } }, "xiaomi": { "type": "object", "additionalProperties": { "type": "string" } }, "webrtc": { "type": "object", "properties": { "listen": { "type": "string", "default": ":8555", "examples": [ ":8555/udp" ] }, "candidates": { "type": "array", "items": { "type": "string", "examples": [ "216.58.210.174:8555", "stun:8555", "home.duckdns.org:8555" ] } }, "ice_servers": { "type": "array", "items": { "type": "object", "properties": { "urls": { "type": "array", "items": { "type": "string", "examples": [ "stun:stun.l.google.com:19302", "turn:123.123.123.123:3478" ] } }, "username": { "type": "string" }, "credential": { "type": "string" } } } }, "filters": { "type": "object", "properties": { "candidates": { "description": "Keep only these candidates", "type": "array", "items": { "type": "string" } }, "interfaces": { "description": "Keep only these interfaces", "type": "array", "items": { "type": "string" } }, "ips": { "description": "Keep only these IP-addresses", "type": "array", "items": { "type": "string" } }, "networks": { "description": "Use only these network types", "type": "array", "items": { "type": "string", "enum": [ "tcp4", "tcp6", "udp4", "udp6" ] } }, "udp_ports": { "description": "Use only these UDP ports range [min, max]", "type": "array", "items": { "type": "integer" }, "maxItems": 2, "minItems": 2 } } } } }, "webtorrent": { "type": "object", "properties": { "trackers": { "type": "array", "items": { "type": "string" } }, "shares": { "additionalProperties": { "type": "object", "properties": { "pwd": { "type": "string", "minLength": 4 }, "src": { "type": "string" } } } } } }, "wyoming": { "type": "object", "additionalProperties": { "type": "object", "properties": { "listen": { "description": "Listen address for Wyoming server", "type": "string" }, "name": { "description": "Optional satellite name (default: stream name)", "type": "string" }, "mode": { "description": "Optional mode: mic / snd / default", "type": "string", "enum": [ "", "mic", "snd" ] }, "event": { "description": "Event handlers (map event type => expr script)", "type": "object", "additionalProperties": { "type": "string" } }, "wake_uri": { "description": "Optional WAKE service URI (ex. tcp://host:port?name=...)", "type": "string", "examples": [ "tcp://192.168.1.23:10400" ] }, "vad_threshold": { "description": "Optional VAD threshold (0.1..3.5 typical)", "type": "number" } } } } } } ================================================ FILE: www/static.go ================================================ package www import "embed" //go:embed *.html //go:embed *.js //go:embed *.json var Static embed.FS ================================================ FILE: www/stream.html ================================================ stream - go2rtc ================================================ FILE: www/video-rtc.js ================================================ /** * VideoRTC v1.6.0 - Video player for go2rtc streaming application. * * All modern web technologies are supported in almost any browser except Apple Safari. * * Support: * - ECMAScript 2017 (ES8) = ES6 + async * - RTCPeerConnection for Safari iOS 11.0+ * - IntersectionObserver for Safari iOS 12.2+ * - ManagedMediaSource for Safari 17+ * * Doesn't support: * - MediaSource for Safari iOS * - Customized built-in elements (extends HTMLVideoElement) because Safari * - Autoplay for WebRTC in Safari */ export class VideoRTC extends HTMLElement { constructor() { super(); this.DISCONNECT_TIMEOUT = 5000; this.RECONNECT_TIMEOUT = 15000; this.CODECS = [ 'avc1.640029', // H.264 high 4.1 (Chromecast 1st and 2nd Gen) 'avc1.64002A', // H.264 high 4.2 (Chromecast 3rd Gen) 'avc1.640033', // H.264 high 5.1 (Chromecast with Google TV) 'hvc1.1.6.L153.B0', // H.265 main 5.1 (Chromecast Ultra) 'mp4a.40.2', // AAC LC 'mp4a.40.5', // AAC HE 'flac', // FLAC (PCM compatible) 'opus', // OPUS Chrome, Firefox ]; /** * [config] Supported modes (webrtc, webrtc/tcp, mse, hls, mp4, mjpeg). * @type {string} */ this.mode = 'webrtc,mse,hls,mjpeg'; /** * [Config] Requested medias (video, audio, microphone). * @type {string} */ this.media = 'video,audio'; /** * [config] Run stream when not displayed on the screen. Default `false`. * @type {boolean} */ this.background = false; /** * [config] Run stream only when player in the viewport. Stop when user scroll out player. * Value is percentage of visibility from `0` (not visible) to `1` (full visible). * Default `0` - disable; * @type {number} */ this.visibilityThreshold = 0; /** * [config] Run stream only when browser page on the screen. Stop when user change browser * tab or minimise browser windows. * @type {boolean} */ this.visibilityCheck = true; /** * [config] WebRTC configuration * @type {RTCConfiguration} */ this.pcConfig = { bundlePolicy: 'max-bundle', iceServers: [{urls: ['stun:stun.cloudflare.com:3478', 'stun:stun.l.google.com:19302']}], sdpSemantics: 'unified-plan', // important for Chromecast 1 }; /** * [info] WebSocket connection state. Values: CONNECTING, OPEN, CLOSED * @type {number} */ this.wsState = WebSocket.CLOSED; /** * [info] WebRTC connection state. * @type {number} */ this.pcState = WebSocket.CLOSED; /** * @type {HTMLVideoElement} */ this.video = null; /** * @type {WebSocket} */ this.ws = null; /** * @type {string|URL} */ this.wsURL = ''; /** * @type {RTCPeerConnection} */ this.pc = null; /** * @type {number} */ this.connectTS = 0; /** * @type {string} */ this.mseCodecs = ''; /** * [internal] Disconnect TimeoutID. * @type {number} */ this.disconnectTID = 0; /** * [internal] Reconnect TimeoutID. * @type {number} */ this.reconnectTID = 0; /** * [internal] Handler for receiving Binary from WebSocket. * @type {Function} */ this.ondata = null; /** * [internal] Handlers list for receiving JSON from WebSocket. * @type {Object.} */ this.onmessage = null; } /** * Set video source (WebSocket URL). Support relative path. * @param {string|URL} value */ set src(value) { if (typeof value !== 'string') value = value.toString(); if (value.startsWith('http')) { value = 'ws' + value.substring(4); } else if (value.startsWith('/')) { value = 'ws' + location.origin.substring(4) + value; } this.wsURL = value; this.onconnect(); } /** * Play video. Support automute when autoplay blocked. * https://developer.chrome.com/blog/autoplay/ */ play() { this.video.play().catch(() => { if (!this.video.muted) { this.video.muted = true; this.video.play().catch(er => { console.warn(er); }); } }); } /** * Send message to server via WebSocket * @param {Object} value */ send(value) { if (this.ws) this.ws.send(JSON.stringify(value)); } /** @param {Function} isSupported */ codecs(isSupported) { return this.CODECS .filter(codec => this.media.includes(codec.includes('vc1') ? 'video' : 'audio')) .filter(codec => isSupported(`video/mp4; codecs="${codec}"`)).join(); } /** * `CustomElement`. Invoked each time the custom element is appended into a * document-connected element. */ connectedCallback() { if (this.disconnectTID) { clearTimeout(this.disconnectTID); this.disconnectTID = 0; } // because video autopause on disconnected from DOM if (this.video) { const seek = this.video.seekable; if (seek.length > 0) { this.video.currentTime = seek.end(seek.length - 1); } this.play(); } else { this.oninit(); } this.onconnect(); } /** * `CustomElement`. Invoked each time the custom element is disconnected from the * document's DOM. */ disconnectedCallback() { if (this.background || this.disconnectTID) return; if (this.wsState === WebSocket.CLOSED && this.pcState === WebSocket.CLOSED) return; this.disconnectTID = setTimeout(() => { if (this.reconnectTID) { clearTimeout(this.reconnectTID); this.reconnectTID = 0; } this.disconnectTID = 0; this.ondisconnect(); }, this.DISCONNECT_TIMEOUT); } /** * Creates child DOM elements. Called automatically once on `connectedCallback`. */ oninit() { this.video = document.createElement('video'); this.video.controls = true; this.video.playsInline = true; this.video.preload = 'auto'; this.video.style.display = 'block'; // fix bottom margin 4px this.video.style.width = '100%'; this.video.style.height = '100%'; this.appendChild(this.video); this.video.addEventListener('error', ev => { const err = this.video.error; // https://developer.mozilla.org/en-US/docs/Web/API/MediaError/code const MEDIA_ERRORS = { 1: 'MEDIA_ERR_ABORTED', 2: 'MEDIA_ERR_NETWORK', 3: 'MEDIA_ERR_DECODE', 4: 'MEDIA_ERR_SRC_NOT_SUPPORTED' }; console.error('[VideoRTC] Video error:', { error: err ? MEDIA_ERRORS[err.code] : 'unknown', message: err ? err.message : 'unknown', codecs: this.mseCodecs || 'not set', readyState: this.video.readyState, networkState: this.video.networkState, currentTime: this.video.currentTime }); if (this.ws) this.ws.close(); // run reconnect for broken MSE stream }); // all Safari lies about supported audio codecs const m = window.navigator.userAgent.match(/Version\/(\d+).+Safari/); if (m) { // AAC from v13, FLAC from v14, OPUS - unsupported const skip = m[1] < '13' ? 'mp4a.40.2' : m[1] < '14' ? 'flac' : 'opus'; this.CODECS.splice(this.CODECS.indexOf(skip)); } if (this.background) return; if ('hidden' in document && this.visibilityCheck) { document.addEventListener('visibilitychange', () => { if (document.hidden) { this.disconnectedCallback(); } else if (this.isConnected) { this.connectedCallback(); } }); } if ('IntersectionObserver' in window && this.visibilityThreshold) { const observer = new IntersectionObserver(entries => { entries.forEach(entry => { if (!entry.isIntersecting) { this.disconnectedCallback(); } else if (this.isConnected) { this.connectedCallback(); } }); }, {threshold: this.visibilityThreshold}); observer.observe(this); } } /** * Connect to WebSocket. Called automatically on `connectedCallback`. * @return {boolean} true if the connection has started. */ onconnect() { if (!this.isConnected || !this.wsURL || this.ws || this.pc) return false; // CLOSED or CONNECTING => CONNECTING this.wsState = WebSocket.CONNECTING; this.connectTS = Date.now(); this.ws = new WebSocket(this.wsURL); this.ws.binaryType = 'arraybuffer'; this.ws.addEventListener('open', () => this.onopen()); this.ws.addEventListener('close', () => this.onclose()); return true; } ondisconnect() { this.wsState = WebSocket.CLOSED; if (this.ws) { this.ws.close(); this.ws = null; } this.pcState = WebSocket.CLOSED; if (this.pc) { this.pc.getSenders().forEach(sender => { if (sender.track) sender.track.stop(); }); this.pc.close(); this.pc = null; } this.video.src = ''; this.video.srcObject = null; } /** * @returns {Array.} of modes (mse, webrtc, etc.) */ onopen() { // CONNECTING => OPEN this.wsState = WebSocket.OPEN; this.ws.addEventListener('message', ev => { if (typeof ev.data === 'string') { const msg = JSON.parse(ev.data); for (const mode in this.onmessage) { this.onmessage[mode](msg); } } else { this.ondata(ev.data); } }); this.ondata = null; this.onmessage = {}; const modes = []; if (this.mode.includes('mse') && ('MediaSource' in window || 'ManagedMediaSource' in window)) { modes.push('mse'); this.onmse(); } else if (this.mode.includes('hls') && this.video.canPlayType('application/vnd.apple.mpegurl')) { modes.push('hls'); this.onhls(); } else if (this.mode.includes('mp4')) { modes.push('mp4'); this.onmp4(); } if (this.mode.includes('webrtc') && 'RTCPeerConnection' in window) { modes.push('webrtc'); this.onwebrtc(); } if (this.mode.includes('mjpeg')) { if (modes.length) { this.onmessage['mjpeg'] = msg => { if (msg.type !== 'error' || msg.value.indexOf(modes[0]) !== 0) return; this.onmjpeg(); }; } else { modes.push('mjpeg'); this.onmjpeg(); } } return modes; } /** * @return {boolean} true if reconnection has started. */ onclose() { if (this.wsState === WebSocket.CLOSED) return false; // CONNECTING, OPEN => CONNECTING this.wsState = WebSocket.CONNECTING; this.ws = null; // reconnect no more than once every X seconds const delay = Math.max(this.RECONNECT_TIMEOUT - (Date.now() - this.connectTS), 0); this.reconnectTID = setTimeout(() => { this.reconnectTID = 0; this.onconnect(); }, delay); return true; } onmse() { /** @type {MediaSource} */ let ms; if ('ManagedMediaSource' in window) { const MediaSource = window.ManagedMediaSource; ms = new MediaSource(); ms.addEventListener('sourceopen', () => { this.send({type: 'mse', value: this.codecs(MediaSource.isTypeSupported)}); }, {once: true}); this.video.disableRemotePlayback = true; this.video.srcObject = ms; } else { ms = new MediaSource(); ms.addEventListener('sourceopen', () => { URL.revokeObjectURL(this.video.src); this.send({type: 'mse', value: this.codecs(MediaSource.isTypeSupported)}); }, {once: true}); this.video.src = URL.createObjectURL(ms); this.video.srcObject = null; } this.play(); this.mseCodecs = ''; this.onmessage['mse'] = msg => { if (msg.type !== 'mse') return; this.mseCodecs = msg.value; const sb = ms.addSourceBuffer(msg.value); sb.mode = 'segments'; // segments or sequence sb.addEventListener('updateend', () => { if (!sb.updating && bufLen > 0) { try { const data = buf.slice(0, bufLen); sb.appendBuffer(data); bufLen = 0; } catch (e) { // console.debug(e); } } if (!sb.updating && sb.buffered && sb.buffered.length) { const end = sb.buffered.end(sb.buffered.length - 1); const start = end - 5; const start0 = sb.buffered.start(0); if (start > start0) { sb.remove(start0, start); ms.setLiveSeekableRange(start, end); } if (this.video.currentTime < start) { this.video.currentTime = start; } const gap = end - this.video.currentTime; this.video.playbackRate = gap > 0.1 ? gap : 0.1; // console.debug('VideoRTC.buffered', gap, this.video.playbackRate, this.video.readyState); } }); const buf = new Uint8Array(2 * 1024 * 1024); let bufLen = 0; this.ondata = data => { if (sb.updating || bufLen > 0) { const b = new Uint8Array(data); buf.set(b, bufLen); bufLen += b.byteLength; // console.debug('VideoRTC.buffer', b.byteLength, bufLen); } else { try { sb.appendBuffer(data); } catch (e) { // console.debug(e); } } }; }; } onwebrtc() { const pc = new RTCPeerConnection(this.pcConfig); pc.addEventListener('icecandidate', ev => { if (ev.candidate && this.mode.includes('webrtc/tcp') && ev.candidate.protocol === 'udp') return; const candidate = ev.candidate ? ev.candidate.toJSON().candidate : ''; this.send({type: 'webrtc/candidate', value: candidate}); }); pc.addEventListener('connectionstatechange', () => { if (pc.connectionState === 'connected') { const tracks = pc.getTransceivers() .filter(tr => tr.currentDirection === 'recvonly') // skip inactive .map(tr => tr.receiver.track); /** @type {HTMLVideoElement} */ const video2 = document.createElement('video'); video2.addEventListener('loadeddata', () => this.onpcvideo(video2), {once: true}); video2.srcObject = new MediaStream(tracks); } else if (pc.connectionState === 'failed' || pc.connectionState === 'disconnected') { pc.close(); // stop next events this.pcState = WebSocket.CLOSED; this.pc = null; this.onconnect(); } }); this.onmessage['webrtc'] = msg => { switch (msg.type) { case 'webrtc/candidate': if (this.mode.includes('webrtc/tcp') && msg.value.includes(' udp ')) return; pc.addIceCandidate({candidate: msg.value, sdpMid: '0'}).catch(er => { console.warn(er); }); break; case 'webrtc/answer': pc.setRemoteDescription({type: 'answer', sdp: msg.value}).catch(er => { console.warn(er); }); break; case 'error': if (!msg.value.includes('webrtc/offer')) return; pc.close(); } }; this.createOffer(pc).then(offer => { this.send({type: 'webrtc/offer', value: offer.sdp}); }); this.pcState = WebSocket.CONNECTING; this.pc = pc; } /** * @param pc {RTCPeerConnection} * @return {Promise} */ async createOffer(pc) { try { if (this.media.includes('microphone')) { const media = await navigator.mediaDevices.getUserMedia({audio: true}); media.getTracks().forEach(track => { pc.addTransceiver(track, {direction: 'sendonly'}); }); } } catch (e) { console.warn(e); } for (const kind of ['video', 'audio']) { if (this.media.includes(kind)) { pc.addTransceiver(kind, {direction: 'recvonly'}); } } const offer = await pc.createOffer(); await pc.setLocalDescription(offer); return offer; } /** * @param video2 {HTMLVideoElement} */ onpcvideo(video2) { if (this.pc) { // Video+Audio > Video, H265 > H264, Video > Audio, WebRTC > MSE let rtcPriority = 0, msePriority = 0; /** @type {MediaStream} */ const stream = video2.srcObject; if (stream.getVideoTracks().length > 0) { // not the best, but a pretty simple way to check a codec const isH265Supported = this.pc.remoteDescription.sdp.includes('H265/90000'); rtcPriority += isH265Supported ? 0x240 : 0x220; } if (stream.getAudioTracks().length > 0) rtcPriority += 0x102; if (this.mseCodecs.includes('hvc1.')) msePriority += 0x230; if (this.mseCodecs.includes('avc1.')) msePriority += 0x210; if (this.mseCodecs.includes('mp4a.')) msePriority += 0x101; if (rtcPriority >= msePriority) { this.video.srcObject = stream; this.play(); this.pcState = WebSocket.OPEN; this.wsState = WebSocket.CLOSED; if (this.ws) { this.ws.close(); this.ws = null; } } else { this.pcState = WebSocket.CLOSED; if (this.pc) { this.pc.close(); this.pc = null; } } } video2.srcObject = null; } onmjpeg() { this.ondata = data => { this.video.controls = false; this.video.poster = 'data:image/jpeg;base64,' + VideoRTC.btoa(data); }; this.send({type: 'mjpeg'}); } onhls() { this.onmessage['hls'] = msg => { if (msg.type !== 'hls') return; const url = 'http' + this.wsURL.substring(2, this.wsURL.indexOf('/ws')) + '/hls/'; const playlist = msg.value.replace('hls/', url); this.video.src = 'data:application/vnd.apple.mpegurl;base64,' + btoa(playlist); this.play(); }; this.send({type: 'hls', value: this.codecs(type => this.video.canPlayType(type))}); } onmp4() { /** @type {HTMLCanvasElement} **/ const canvas = document.createElement('canvas'); /** @type {CanvasRenderingContext2D} */ let context; /** @type {HTMLVideoElement} */ const video2 = document.createElement('video'); video2.autoplay = true; video2.playsInline = true; video2.muted = true; video2.addEventListener('loadeddata', () => { if (!context) { canvas.width = video2.videoWidth; canvas.height = video2.videoHeight; context = canvas.getContext('2d'); } context.drawImage(video2, 0, 0, canvas.width, canvas.height); this.video.controls = false; this.video.poster = canvas.toDataURL('image/jpeg'); }); this.ondata = data => { video2.src = 'data:video/mp4;base64,' + VideoRTC.btoa(data); }; this.send({type: 'mp4', value: this.codecs(this.video.canPlayType)}); } static btoa(buffer) { const bytes = new Uint8Array(buffer); const len = bytes.byteLength; let binary = ''; for (let i = 0; i < len; i++) { binary += String.fromCharCode(bytes[i]); } return window.btoa(binary); } } ================================================ FILE: www/video-stream.js ================================================ import {VideoRTC} from './video-rtc.js'; /** * This is example, how you can extend VideoRTC player for your app. * Also you can check this example: https://github.com/AlexxIT/WebRTC */ class VideoStream extends VideoRTC { set divMode(value) { this.querySelector('.mode').innerText = value; this.querySelector('.status').innerText = ''; } set divError(value) { const state = this.querySelector('.mode').innerText; if (state !== 'loading') return; this.querySelector('.mode').innerText = 'error'; this.querySelector('.status').innerText = value; } /** * Custom GUI */ oninit() { console.debug('stream.oninit'); super.oninit(); this.innerHTML = `
    `; const info = this.querySelector('.info'); this.insertBefore(this.video, info); } onconnect() { console.debug('stream.onconnect'); const result = super.onconnect(); if (result) this.divMode = 'loading'; return result; } ondisconnect() { console.debug('stream.ondisconnect'); super.ondisconnect(); } onopen() { console.debug('stream.onopen'); const result = super.onopen(); this.onmessage['stream'] = msg => { console.debug('stream.onmessge', msg); switch (msg.type) { case 'error': this.divError = msg.value; break; case 'mse': case 'hls': case 'mp4': case 'mjpeg': this.divMode = msg.type.toUpperCase(); break; } }; return result; } onclose() { console.debug('stream.onclose'); return super.onclose(); } onpcvideo(ev) { console.debug('stream.onpcvideo'); super.onpcvideo(ev); if (this.pcState !== WebSocket.CLOSED) { this.divMode = 'RTC'; } } } customElements.define('video-stream', VideoStream); ================================================ FILE: www/webrtc-sync.html ================================================ webrtc - go2rtc ================================================ FILE: www/webrtc.html ================================================ webrtc - go2rtc