AlexxIT / go2rtc

Ultimate camera streaming application with support RTSP, RTMP, HTTP-FLV, WebRTC, MSE, HLS, MP4, MJPEG, HomeKit, FFmpeg, etc.
https://github.com/AlexxIT/Blog
MIT License
5.13k stars 409 forks source link

How do you generate an URL to view the webrtc stream ? #1307

Closed musapinar closed 2 months ago

musapinar commented 2 months ago

"Frigate Notification" integration (https://github.com/SgtBatten/HA_blueprints) allows you to send notifications on your mobile phone when a person is detected. Clicking on the notification can redirect you to the player. yaml configuration of such an automation looks like this :

- id: '1723502560176'
  alias: Porche
  description: ''
  use_blueprint:
    path: SgtBatten/Stable.yaml
    input:
      camera: camera.porche_2
      notify_device: 7ebed67d640b24ff8f3a9789d9b22094
      attachment: snapshot.jpg?bbox=1
      update_thumbnail: true
      tv: false
      base_url: https://hass.redacted.com
      tap_action: '{{base_url}}/api/camera_proxy_stream/camera.{{trigger.payload_json[''after''][''camera'']
        | lower | replace(''-'',''_'')}}?token={{state_attr( ''camera.'' ~ camera,
        ''access_token'')}}'

which will generate a link like : https://hass.redacted.com/api/camera_proxy_stream/camera.porche?token=6943b3017636a1d83527e238423ea55acbafa2c610970f6f7e7b087c23d313dc 2024-08-13_04h24_12

Problem : the "stream" it returns is actually just a jpeg image (a snapshot, not even an mjpeg) ? if I pull-to-refresh on my phone or press F5 on my desktop browser, it does refresh the image with a more recent snapshot.

Question : Having only the "token" as a relevant camera entity (which seems safe enough to me as it appears to be a "rolling" code refreshed every x minutes), how can I return the webrtc video player ?

Thank you.

AlexxIT commented 2 months ago

Your should use WebRTC Camera integration.

musapinar commented 2 months ago

@AlexxIT Isn't that a "custom component"/card integration ? How is that related to my request ? 😕

I already do have Go2rtc and Frigate set up an running fine. It does stream great through both Frigate and as a "Generic camera" as well, with subsecond latency, externally (outside the LAN).

I need an URL. How does the "WebRTC Camera integration" achieve this ?

Anyways, I went the long route and ended up implementing webtorrent. I copied your html/js at https://alexxit.github.io/go2rtc/#share=redacted&pwd=redacted&media=video+audio and put it locally within a folder /homeassistant/www/player/ as an index.html, that I now reach at https://hass.redacted.com/local/player/index.html#share=porche_sub&pwd=redacted&media=video+audio

Here is how it looks like in frigate.yaml

[...]

go2rtc:
  streams:
    Garage:
      - rtsp://redacted:redacted@192.168.1.28:554/Streaming/channels/401
      - ffmpeg:Garage#audio=opus
    Garage_sub:
      - rtsp://redacted:redacted@192.168.1.28:554/Streaming/channels/402
      - ffmpeg:Garage_sub#audio=opus
    Porche:
      - rtsp://redacted:redacted@192.168.1.28:554/Streaming/channels/301
      - ffmpeg:Porche#audio=opus
    Porche_sub:
      - rtsp://redacted:redacted@192.168.1.28:554/Streaming/channels/302
      - ffmpeg:Porche_sub#audio=opus
    Terrasse:
      - rtsp://redacted:redacted@192.168.1.28:554/Streaming/channels/101
      - ffmpeg:Terrasse#audio=opus
    Terrasse_sub:
      - rtsp://redacted:redacted@192.168.1.28:554/Streaming/channels/102
      - ffmpeg:Terrasse_sub#audio=opus
    Allee:
      - rtsp://redacted:redacted@192.168.1.28:554/Streaming/channels/201
      - ffmpeg:Allee#audio=opus
    Allee_sub:
      - rtsp://redacted:redacted@192.168.1.28:554/Streaming/channels/202
      - ffmpeg:Allee_sub#audio=opus
  webtorrent: # https://github.com/AlexxIT/go2rtc?tab=readme-ov-file#module-webtorrent
    shares:
      # /homeassistant/www/player/index.html => https://hass.redacted.com/local/player/index.html#share=porche_sub&pwd=redacted&media=video+audio
      porche_sub: # share name, should be unique among all go2rtc users!
        pwd: redacted
        src: Porche_sub # stream name from streams section
      garage_sub:
        pwd: redacted
        src: Garage_sub
      terrasse_sub:
        pwd: redacted
        src: Terrasse_sub
      allee_sub:
        pwd: redacted
        src: Allee_sub

[...]

pwd is identical for all 4 streams (32 chars).

So now I can set my tap_action such as :

      tap_action: '{{base_url}}/local/player/index.html#share={{trigger.payload_json[''after''][''camera'']
        | lower | replace(''-'',''_'')}}_sub&pwd=redacted&media=video+audio'

which does work as I expected.

How safe is torrenting ? I wish I did not have to rely on openwebtorrent. Can you elaborate about "WebRTC Camera integration" ?

AlexxIT commented 2 months ago

This component can "generate URL to WebRTC stream". You also can use WebTorrent technology. It have static URL, if you setup it via go2rtc config.

musapinar commented 2 months ago

OK. Found it : https://github.com/AlexxIT/WebRTC/wiki/Cast-or-share-camera-stream#html-page Thanks.