Sunday, April 14, 2013

RTMP notify redirects in 0.9.14

Recently I have committed a new feature in nginx-rtmp. The feature is called notify-redirects. HTTP notifications have been available in ngnx-rtmp for a long time. Unsuccessful (non-2xx) HTTP result code used to terminate RTMP connection.

Since version 0.9.14 notification HTTP result code 3xx redirects RTMP connection to another application/stream. Application redirect is done in case of on_connect, stream redirect is done in case of on_play and on_publish. Application/stream name should be returned in Location field of HTTP response header.

In this example fallback is returned as new application name instead of the one provided by RTMP client.

curl -v http://localhost:8080/on_connect?...
...
< HTTP/1.1 301 Moved Permanently
< Server: nginx/1.2.4
< Date: Sun, 14 Apr 2013 04:41:18 GMT
< Content-Type: text/html
< Content-Length: 184
< Connection: keep-alive
< Location: fallback
...


Redirects can be easily implemented in nginx. The following example implements on_connect handler checking for RTMP flashVer equal to my_secret_flashver and redirecting client to fallback application in case of failure.

http {
server {
listen 8080;
server_name localhost;
location /on_connect {
if ($arg_flashver != "my_secret_flashver") {
rewrite ^.*$ fallback? permanent;
}
return 200;
}
}
}


Rtmp config for this case.

rtmp {
server {
listen 1935;
notify_method get;
on_connect http://localhost:8080/on_connect;
application myapp {
# Live stream for good clients
live on;
}
application fallback {
# VOD video for bad clients
live on;
play /var/fallback;
}
}
}


The same way you can redirect streams with on_play and on_publish handlers.

PS: Several important issues have been fixed in HLS engine as well. Please make sure you use the latest code. Moreover new updated HLS engine is under development.

8 comments:

  1. Nice features, can it redirect to a different server?
    Thank you.

    ReplyDelete
  2. No. It's like nginx's "rewrite" directive. You can redirect to a different application with pull enabled or you can enable pull for a particular stream within the same application and redirect to it.

    ReplyDelete
  3. Thank Arut. I'm looking to redirect RTMP traffic by IP, do you know how to implement features like Nginx Geo modules on RTMP?

    ReplyDelete
  4. Thats a nice feature. This can be very useful to check the user in case we want to server authenticated video. One thing i want to confirm in redirect do we need to send stream of we can send any other content like html formatted page or josn messages.

    I would also like to know, whether we can get the stream position which this module is reading and sending. We have implemented a video solution on Wowza which uses seek lock. Basically reads the seek position and when we want the user not to seek in case of seek request it just goes back to the position it was playing the last.

    Thanks for wonderful module i m testing it in our current senario.

    ReplyDelete
  5. 1. you need to send stream, not anything else
    2. you can get stream position when handling on_play command (coming with that command), but you can't change it

    ReplyDelete
  6. Thanks,
    I find there are on_update callback which can be used to suit my need. Can you elaborate on on_update please?
    Is this linked with ping and it response.

    ReplyDelete
  7. Hi Roman,

    Thanks for the nice module. I have a question about how to pass my on_connect args to another page so I can implement some other logic separate from the config file.

    Right now under location on_connect I have a working example like
    if ($arg_myarg = "1234") {
    return 200;} return 400;

    How can I pass myarg to my defined php page?
    if (auth.php?v1=$arg_myarg) { return 200;}

    ReplyDelete
  8. Use proxy_pass directive like this

    proxy_pass http://example.com/process.php?myarg=$arg_myarg;

    ReplyDelete