Monday, May 13, 2013

Dynamic pull and push in 0.9.19

Since 0.9.19 version of nginx-rtmp-module you can dynamically pull or push streams with on_play and on_publish. 3xx HTTP result code used to redirect current stream. Now if the new stream name is started with rtmp:// then it's supposed to be RTMP URL and relay (pull/push) is created for the current client.


http {
...
location /local_redirect {
rewrite ^.*$ newname? permanent;
}
location /remote_redirect {
# no domain name here, only ip
rewrite ^.*$ rtmp://192.168.1.123/someapp/somename? permanent;
}
...
}

rtmp {
...
application myapp1 {
live on;
# stream will be redirected to 'newname'
on_play http://localhost:8080/local_redirect;
}
application myapp2 {
live on;
# stream will be pulled from remote location
# requires nginx >= 1.3.10
on_play http://localhost:8080/remote_redirect;
}
...
}


In the example above myapp1 uses old redirect behavior, myapp2 shows the new feature.

In myapp2 application source stream name is not changed and pull is created for the source stream name. Later clients connecting to this stream will see the video pulled by the first client. That's identical to usual pull behavior. You can change it however by specifying notify_relay_redirect on. This will redirect pulled stream to a new (long url-like) local name. So the streams will be grouped on rtmp url.

on_publish does the same for push operation.

The feature has 2 limitations

  • RTMP URL should not contain domains names, only IP addresses

  • nginx version>=1.3.10 is required

5 comments:

  1. Hi Arut,
    Nginx_rtmp modules day as many interesting features.
    Do you implement real redirect in nginx_rtmp?
    I saw JW support for that: http://developer.longtailvideo.com/trac/browser/trunk/fl5/src/com/longtailvideo/jwplayer/media/RTMPMediaProvider.as#L594

    Thank you.

    ReplyDelete
  2. Christopher ZemanMay 19, 2013 at 5:53 AM

    I may be misunderstanding this, but it almost sounds like this would fit a need I have. Here's what I want to do:

    Stream 1: Always Live (being distributed via web and via ffmpeg to other servers)
    Stream 2: Special Event (I'd like this to supersede Stream 1 on publish. In other words, anyone viewing (ffmpeg included) Stream 1 would see Stream 2 automatically.

    Basically, I'm looking to perform dynamic stream switching.

    ReplyDelete
  3. Dynamic pull/push operates only on stream play/publish start when on_play/on_publish is handled. "Dynamic" means you can push and pull any remote stream, it should not be statically written in config. However it cannot be changed while stream is playing.

    If you want your clients to switch from one stream to another when the second stream appears, it's not supported.

    ReplyDelete
  4. Dynamic pull and push is a great feature!

    I wonder if there is any tutorial to test this functionality. Specifically, after adding the above config in the config file, which url should we use in the client to start the stream.

    For ex: If my url is rtmp://mydomain.com/live, then what should we use in the client?

    ReplyDelete
  5. You should connect to the application (APP) where you set that on_play directive.
    Stream name (STREAM) does not make big sense but it is sent to your on_play handler,
    you can use it to make up the redirect url.

    rtmp://server.com/APP/STREAM

    ReplyDelete