The most detailed webrtc-streamer H265 solution in history

Table of contents

foreword

opening nonsense

Software Environment

the whole idea

Detailed plan

     Download mediaMtx

      Configure mediamtx.yml

 start up

API use

Query all paths: http://localhost:9997/v2/paths/list

Add a channel path named 111 (without transcoding): http://192.168.1.227:9997/v2/config/paths/add/111

 Add a channel path (transcoding) named 222: http://192.168.1.227:9997/v2/config/paths/add/222


foreword

I admire Musk's words the most, I don't care about patents, patents will only inhibit the development of technology. I have searched all the tutorials but there is no solution about webrtc-streamer can play H265 rtsp video stream. I found it and I will share it. No matter what you think, I hope you read my post and if you benefit, I hope You can also be like me, knowledge can only be shared to make progress together!

opening nonsense

The front end uses webrtc-streamer for rtsp streaming playback. The current webrtc-streamer only supports H264 video, which is applicable in most occasions, but in some special occasions, the camera must use the H265 encoding format to reduce storage usage. At this time, we either wait for the open source author of webrtc-streamer to release a version that supports 265, or consider transcoding to 264 for playback. I belong to the latter.

Software Environment

the whole idea

  1. Configure the rtsp stream of the camera through the mediaMtx streaming media built-in command, and use ffmpeg to transcode the rtsp stream and push it to the mediaMtx streaming media.
  2. After transcoding, use mediaMtx to pull the stream. You can use webrtc-streamer to play it, or use vlc or other methods to pull the stream.
  3. mediaMtx streaming media can choose transcoding or non-transcoding, and mediaMtx can be selected as streaming media for unified stream distribution address and centralized management.

Detailed plan

     Download mediaMtx

        The directory structure is as follows: 

      Configure mediamtx.yml

          Open mediamtx.yml for configuration. Since there are many configuration items, I only intercept the key configuration:

        Open the api, because we need to configure it through code, so we need to open the mediaMtx API

        API description document address: MediaMTX API (bluenviron.github.io)

         Turn off rtsp: rtspDisable: no Note here that no is turned off means it is turned on. Other ports can be configured by themselves according to the situation

         Key configuration:

        If we do not use the api to configure the paths, we can configure them here. If we use the api to configure them, it is enough to leave a default of all here. All means that when no matching key is found in the path, all are configured as all.

        I use this configuration when I test, in actual use I use api

        Transcoding, when we need to transcode, we need to configure runOnInit, where we configure the command that needs to be transcoded, that is, the command to call ffmpeg for transcoding.

  •         Configure runOnInit: program starts loading transcoding
  •         Configure runOnDemand: load on demand, load transcoding when streaming

I provide a command: "runOnInit": "ffmpeg -rtsp_transport tcp -i \"rtsp://admin:[email protected]:554/h264/ch1/main/av_stream\" -vcodec libx264 -preset:v ultrafast - r 25 -threads 4 -b:v 4096k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH"

The FFMPEG transcoding command does not explain, and Baidu itself, roughly means to transcode an rtsp stream to H264 and output a new rtsp stream

 start up

API use

The code layer solves it by itself, I use postman for demonstration here

Query all paths: http://localhost:9997/v2/paths/list

 The default return value is empty, indicating that the streaming media has not configured a channel path.

Add a channel path named 111 (without transcoding)

View mediaMtx dos window:

 My environment is a bit packet loss, normally you can see [path 111] [rtsp source] started, it means the streaming media pull stream is successful

 Query all paths again: only focus on the source when not transcoding.

{
    "itemCount": 1,
    "pageCount": 1,
    "items": [
        {
            "name": "111",
            "confName": "111",
            "conf": {
                "source": "rtsp://admin:[email protected]:554/h264/ch1/main/av_stream",
                "sourceFingerprint": "",
                "sourceOnDemand": false,
                "sourceOnDemandStartTimeout": "10s",
                "sourceOnDemandCloseAfter": "10s",
                "publishUser": "",
                "publishPass": "",
                "publishIPs": [],
                "readUser": "",
                "readPass": "",
                "readIPs": [],
                "disablePublisherOverride": false,
                "fallback": "",
                "sourceProtocol": "automatic",
                "sourceAnyPortEnable": false,
                "rtspRangeType": "",
                "rtspRangeStart": "",
                "sourceRedirect": "",
                "rpiCameraCamID": 0,
                "rpiCameraWidth": 1920,
                "rpiCameraHeight": 1080,
                "rpiCameraHFlip": false,
                "rpiCameraVFlip": false,
                "rpiCameraBrightness": 0,
                "rpiCameraContrast": 1,
                "rpiCameraSaturation": 1,
                "rpiCameraSharpness": 1,
                "rpiCameraExposure": "",
                "rpiCameraAWB": "",
                "rpiCameraDenoise": "",
                "rpiCameraShutter": 0,
                "rpiCameraMetering": "",
                "rpiCameraGain": 0,
                "rpiCameraEV": 0,
                "rpiCameraROI": "",
                "rpiCameraTuningFile": "",
                "rpiCameraMode": "",
                "rpiCameraFPS": 30,
                "rpiCameraIDRPeriod": 60,
                "rpiCameraBitrate": 1000000,
                "rpiCameraProfile": "main",
                "rpiCameraLevel": "4.1",
                "rpiCameraAfMode": "",
                "rpiCameraAfRange": "",
                "rpiCameraAfSpeed": "",
                "rpiCameraLensPosition": 0,
                "rpiCameraAfWindow": "",
                "rpiCameraTextOverlayEnable": false,
                "rpiCameraTextOverlay": "%Y-%m-%d %H:%M:%S - MediaMTX",
                "runOnInit": "",
                "runOnInitRestart": false,
                "runOnDemand": "",
                "runOnDemandRestart": false,
                "runOnDemandStartTimeout": "10s",
                "runOnDemandCloseAfter": "10s",
                "runOnReady": "",
                "runOnReadyRestart": false,
                "runOnRead": "",
                "runOnReadRestart": false
            },
            "source": {
                "type": "rtspSource",
                "id": ""
            },
            "sourceReady": false,
            "tracks": [],
            "bytesReceived": 0,
            "readers": []
        }
    ]
}

 At this point, you can use the mediaMtx address plus the path name to preview and play

 I won't show the playback screen, it's inconvenient.

when using vlc for streaming

There will be the following print information under dos, and mediaMtx will create a connection. 

 Add a channel path named 222 (transcoding)

query all paths

{
    "itemCount": 1,
    "pageCount": 1,
    "items": [
        {
            "name": "222",
            "confName": "222",
            "conf": {
                "source": "publisher",
                "sourceFingerprint": "",
                "sourceOnDemand": false,
                "sourceOnDemandStartTimeout": "10s",
                "sourceOnDemandCloseAfter": "10s",
                "publishUser": "",
                "publishPass": "",
                "publishIPs": [],
                "readUser": "",
                "readPass": "",
                "readIPs": [],
                "disablePublisherOverride": false,
                "fallback": "",
                "sourceProtocol": "automatic",
                "sourceAnyPortEnable": false,
                "rtspRangeType": "",
                "rtspRangeStart": "",
                "sourceRedirect": "",
                "rpiCameraCamID": 0,
                "rpiCameraWidth": 1920,
                "rpiCameraHeight": 1080,
                "rpiCameraHFlip": false,
                "rpiCameraVFlip": false,
                "rpiCameraBrightness": 0,
                "rpiCameraContrast": 1,
                "rpiCameraSaturation": 1,
                "rpiCameraSharpness": 1,
                "rpiCameraExposure": "",
                "rpiCameraAWB": "",
                "rpiCameraDenoise": "",
                "rpiCameraShutter": 0,
                "rpiCameraMetering": "",
                "rpiCameraGain": 0,
                "rpiCameraEV": 0,
                "rpiCameraROI": "",
                "rpiCameraTuningFile": "",
                "rpiCameraMode": "",
                "rpiCameraFPS": 30,
                "rpiCameraIDRPeriod": 60,
                "rpiCameraBitrate": 1000000,
                "rpiCameraProfile": "main",
                "rpiCameraLevel": "4.1",
                "rpiCameraAfMode": "",
                "rpiCameraAfRange": "",
                "rpiCameraAfSpeed": "",
                "rpiCameraLensPosition": 0,
                "rpiCameraAfWindow": "",
                "rpiCameraTextOverlayEnable": false,
                "rpiCameraTextOverlay": "%Y-%m-%d %H:%M:%S - MediaMTX",
                "runOnInit": "ffmpeg -rtsp_transport tcp -i rtsp://admin:[email protected]:554/h264/ch1/main/av_stream -vcodec libx264 -preset:v ultrafast -r 25 -threads 4 -b:v 4096k -f rtsp rtsp://localhost:$RTSP_PORT/$MTX_PATH",
                "runOnInitRestart": true,
                "runOnDemand": "",
                "runOnDemandRestart": false,
                "runOnDemandStartTimeout": "10s",
                "runOnDemandCloseAfter": "10s",
                "runOnReady": "",
                "runOnReadyRestart": false,
                "runOnRead": "",
                "runOnReadRestart": false
            },
            "source": null,
            "sourceReady": false,
            "tracks": [],
            "bytesReceived": 20054902,
            "readers": []
        }
    ]
}

The mediaMtx DOS page can be seen to start calling ffmpeg for transcoding

So far use rtsp://192.168.1.227:8554/222, you can preview.

You can use vlc to preview. Of course, the same is true when you use webrtc-streamer, but webrtc-streamer cannot directly use the rtsp address of the camera.

Here is a detail that I forgot to mention, ffmpeg needs to configure environment variables, here you can Baidu yourself. Otherwise, mediaMtx commands are invalid.

Guess you like

Origin blog.csdn.net/qq_20937557/article/details/132271507