bluenviron / mediamtx

Ready-to-use SRT / WebRTC / RTSP / RTMP / LL-HLS media server and media proxy that allows to read, publish, proxy, record and playback video and audio streams.
MIT License
12.27k stars 1.53k forks source link

Compression via ffmpeg only on demand #1541

Closed forabi closed 1 year ago

forabi commented 1 year ago

Describe the feature

Description The README has instructions on how to do a compressed stream via runOnReady. I assume this means ffmpeg will unconditionally run whether there are consumers for the compressed stream or not. This starts to slowdown my Pi 4 with just two stream (I have a bunch of other stuff running, of course).

My question/request is: can I start and kill the compressed stream based on whether it's being used or not?

raypanggh commented 1 year ago

Look at runOnDemand. It's explained in the config file

forabi commented 1 year ago

Run on demand does not work with runOnReady

forabi commented 1 year ago

Even if it did, the compressed stream would be started when the raw stream is accessed. This is not efficient. I want the compressed stream to only start when it's actually requested and shut down when it's not used.

On Wed, Mar 15, 2023, 12:35 AM raypanggh @.***> wrote:

Look at runOnDemand. It's explained in the config file

— Reply to this email directly, view it on GitHub https://github.com/aler9/rtsp-simple-server/issues/1541#issuecomment-1469006641, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAHXIGOZMYMJSPI2DJVDM43W4D6DNANCNFSM6AAAAAAVTTT6QM . You are receiving this because you authored the thread.Message ID: @.***>

raypanggh commented 1 year ago

Not following the requirement.

I don't see any reference to startOnReady, only runOnReady.

Why not use runOnDemand? Ffmpeg will only start when the stream is requested. And it will be shut down when there are no consumers.

forabi commented 1 year ago

Ffmpeg will start compressing the stream when the raw stream is requested. I don't want to compress the stream if the compressed version is not being used.

raypanggh commented 1 year ago

Can you share your config so I can understand the use case?

forabi commented 1 year ago

This is an example of one stream that I need a compressed version of. As you can see, runOnReady is started when the uncompressed stream is ready. If I combine sourceOnDemand with runOnReady, the compressed stream won't start unless the raw stream is requested. That does not make sense. Also, we cannot control ffmpeg to only compress on demand because it's started from the raw stream, not from the compressed path.

paths:
 all:
   publishUser: <redacted>
   publishPass: <redacted>
 1/unicast/compressed:
  readUser: <redacted>
  readPass: <redacted>
 1/unicast:
   readUser: <redacted>
   readPass: <redacted>
   source: rtsp://<redacted>:<redacted>@<ip>:8554/unicast
   sourceOnDemand: no
   sourceProtocol: udp
   runOnReady: ffmpeg -i rtsp://<redacted>:<redacted>@localhost:$RTSP_PORT/$RTSP_PATH -c:v h264_v4l2m2m -b:v 600k -max_muxing_queue_size 1024 -f rtsp rtsp://localhost:$RTSP_PORT/$RTSP_PATH/compressed
   runOnReadyRestart: yes
raypanggh commented 1 year ago

Could you not make 1/unicast simply an RTSP proxy for rtsp://root:redacted>@<ip:8554/unicast, and then have 1/unicast/compressed call ffmpeg via runOnDemand?

I'm imagining this:

1/unicast/compressed:
  runOnDemand: ffmpeg -i rtsp://<redacted>:<redacted>@localhost:$RTSP_PORT/1/unicast -c:v h264_v4l2m2m -b:v 600k -max_muxing_queue_size 1024 -f rtsp rtsp://localhost:$RTSP_PORT/$RTSP_PATH
  runOnDemandRestart: yes
1/unicast:
   source: rtsp://root:<redacted>@<ip>:8554/unicast
   sourceOnDemand: no # or yes if you don't want the bandwidth consumption if nobody is using the uncompressed source
   sourceProtocol: udp

This way the 1/unicast (the uncompressed stream) is always available, and only if 1/unicast/compressed is requested does ffmpeg kick in.

forabi commented 1 year ago

Tried it, but it didn't work:

Mar 15 16:57:50 raspberrypi rtsp-simple-server[5540]: 2023/03/15 16:57:50 INF [RTSP] [conn 192.168.0.168:56752] closed (no one is publishing to path '3/unicast/compressed')
forabi commented 1 year ago
 3/unicast/compressed:
  readUser: <redacted>
  readPass: <redacted>
  runOnReady: ffmpeg -i rtsp://<redacted>:<redacted>@localhost:$RTSP_PORT/3/unicast -c:v h264_v4l2m2m -b:v 600k -max_muxing_queue_size 1024 -f rtsp rtsp://localhost:$RTSP_PORT/$RTSP_PATH
  runOnReadyRestart: yes
 3/unicast:
   readUser: <redacted>
   readPass: <redacted>
   source: rtsp://<redacted>:<redacted>@<ip>:8554/unicast
   sourceOnDemand: yes
   sourceProtocol: udp

Am I doing something wrong?

raypanggh commented 1 year ago

Hmm, it looks alright.

Try sourceOnDemand: no.

I suspect that 3/unicast isn't working because nothing is requesting it. As a result, 3/unicast/compressed won't work, as that relies on 3/unicast to be running.

Also you want runOnDemand, not runOnReady. runOnReady will leave you with the compressed stream running all the time.

If this doesn't work, I'll have a play with such a setup on my side and see if I can get it working.

raypanggh commented 1 year ago

Oh also you'll probably need to specify the credentials for publishing to 3/unicast/compressed, if you've got publishUser/publishPass configured.

-f rtsp rtsp://<publishUser>:<publishPass>@localhost:$RTSP_PORT/$RTSP_PATH

Try turning off all your authentication temporarily to eliminate that from the problem.

forabi commented 1 year ago

It works with your suggestions (but I also had to change ffmpeg params to the same ones in the README as mine were causing an empty stream)

github-actions[bot] commented 1 year ago

This issue is being locked automatically because it has been closed for more than 6 months. Please open a new issue in case you encounter a similar problem.