Friday, January 10, 2014

Redirecting streams in version 1.1.1

In version 1.1.1 of nginx-rtmp-module stream redirect feature is added. Now you can change currently played or published stream in realtime through control request. The following call changes subscriber stream name to newname. The subscriber is found in myapp application by the IP address 127.0.0.1.
http://server.com/control/redirect/subscriber?app=myapp&addr=127.0.0.1
&newname=newname
The above example changes stream for a single client. To change it for all clients use pull and change its source end. You can pull certain streams from VOD applications as well.
application myapp {
  live on;
  hls on;
  hls_path /var/hls;
  pull rtmp://localhost/src/default name=myapp static;
}

application src {
  live on; 
  pull rtmp://localhost/vod/title.mp4 name=default;
  pull rtmp://localhost/vod/ad.mp4 name=ad;
}

application vod {
  play /var/videos;
}
The myapp stream starts with title.mp4. Now switch to cam1 (which should be published to src application)
http://server.com/control/redirect/subscriber?app=src&addr=127.0.0.1
&newname=cam1
Now show ad.mp4
http://server.com/control/redirect/subscriber?app=src&addr=127.0.0.1
&newname=ad
Back to cam1
http://server.com/control/redirect/subscriber?app=src&addr=127.0.0.1
&newname=cam1
Now show cam2
http://server.com/control/redirect/subscriber?app=src&addr=127.0.0.1
&newname=cam2
The new stream starts immediately in RTMP. In HLS the stream is usually slightly delayed due to the nature of HLS.

Limitations:
  • The feature only works in single-worker mode. You can easily create a streaming backend with a single worker to pull from.
  • MPEG-DASH engine cannot handle stream discontinuities so the feature will not work properly with DASH

5 comments:

  1. This is very nice. I've been wanting to do dynamic redirection for quite some time.

    What kind of error handling does this offer? For instance if the "cam1" stream isn't connected and you try to redirect to it, what happens? Would there be a means of setting up and error handling redirect for an app to automatically redirect to if the stream doesn't exist, or if the stream cuts out for some reason?

    ReplyDelete
    Replies
    1. Error handling is not implemented now. Connecting to an idle stream is not an error by default.

      Delete
    2. Dear Roman - you are doing an excellent job with the RTM module!
      I am trying to use this feature and have difficulties. I appreciate if you'll be able to help me.

      Here is what I have in my config file:

      application myapp {
      live on;
      hls on;
      hls_path /data/hls;
      pull rtmp://localhost:1935/src/default name=myapp static;
      }

      application src {
      live on;

      pull rtmp://localhost:1935/vod/videoclip.mp4 name=default;
      pull rtmp://localhost:1935/vod/ad.mp4 name=ad;
      pull rtmp://SOMEIP:1935 app=1tv name=1tv1 static live=1 ;

      }

      in VLC I am typing rtmp://SERVERIP:1935/myapp/myapp - plays videoclip.mp4 I am typing in the browser:

      http://SERVERIP/control/redirect/subscriber?app=myapp&addr=127.0.0.1&newname=1tv1

      or

      http://SERVERIP/control/redirect/subscriber?app=myapp&addr=127.0.0.1&newname=ad

      and I don't see any switching. Each stream works fine, like rtmp://SERVERIP:1935/src/ad and rtmp://SERVERIP:1935/src/1tv1

      Can you tell me please what am I doing wrong.

      Thank you,
      George

      Delete
    3. I've just fixed a typo in the post. Instead of "app=myapp" you should use "app=src" in the control request.

      Delete
  2. sorry dono if my last post worked but http://server:8080/control/redirect/subscriber?app=live&addr=thepersonsip&name=oldchan&newname=newchan should work?

    ReplyDelete