“This is the 18th day of my participation in the August Gwen Challenge.

1. HTTP callback

HttpCallBack

  • Event: The specified HTTP address is called back when this event occurs
  • HTTP address: Multiple HTTP addresses can be supported, separated by Spaces. SRS calls back these interfaces in turn
  • Data transfer: SRS posts data to the HTTP interface

Modifying a Configuration File

The configuration file needs to have http_hooks enabled:

listen 1935; max_connections 1000; vhost __defaultVhost__ { http_hooks { enabled on; On_connect http://127.0.0.1:8631/api/hook/index; On_close http://127.0.0.1:8631/api/hook/index; On_publish http://127.0.0.1:8631/api/hook/index; On_unpublish http://127.0.0.1:8631/api/hook/index; On_play http://127.0.0.1:8631/api/hook/index; On_stop http://127.0.0.1:8631/api/hook/index; On_dvr http://127.0.0.1:8631/api/hook/index; On_hls_notify http://127.0.0.1:8631/api/hook/index; }}Copy the code

Callback event POST data

The event data instructions
on_connect {” action “:” on_connect “, “client_id:” 1985, “IP” : “192.168.1.10”, “vhost” : “video.test.com”, “app” : “live”, “tcUrl” : “rtmp://video.test.com/live?key=d2fa801d08e3f90ed1e1670e6e52651a”,”pageUrl”: “www.test.com/live.html”} When the client connects to the specified vhost and app
on_close {” action “:” on_close “, “client_id:” 1985, “IP” : “192.168.1.10”, “vhost” : “video.test.com”, “app” : “live”, “send_bytes” : 10240, “recv_bytes”: 10240} When the client closes the connection, or the SRS actively closes the connection
on_publish {” action “:” on_publish “, “client_id:” 1985, “IP” : “192.168.1.10”, “vhost” : “video.test.com”, “app” : “live”,”tcUrl” => “rtmp://video.test.com/live?token=xxx&salt=yyy”,”stream”: “livestream”, “param”:”?token=xxx&salt=yyy”} When the client publishes a stream, for example flash/FMLE pushes the stream to the server
on_unpublish {” action “:” on_unpublish “, “client_id:” 1985, “IP” : “192.168.1.10”, “vhost” : “video.test.com”, “app” : “live”, “stream” : “livestream”, “param”:”?token=xxx&salt=yyy”} When the client stops publishing the stream
on_play {” action “:” on_play “, “client_id:” 1985, “IP” : “192.168.1.10”, “vhost” : “video.test.com”, “app” : “live”, “stream” : “livestream”, “param”:”?token=xxx&salt=yyy”,”pageUrl”: “www.test.com/live.html”} When the client starts playing the stream
on_stop {” action “:” on_stop “, “client_id:” 1985, “IP” : “192.168.1.10”, “vhost” : “video.test.com”, “app” : “live”, “stream” : “livestream”, “param”:”?token=xxx&salt=yyy”} When the client stops playing. Note: Stopping playing may not close the connection and can continue playing.
on_dvr {” action “:” on_dvr “, “client_id:” 1985, “IP” : “192.168.1.10”, “vhost” : “video.test.com”, “app” : “live”, “stream” : “livestream”, “param”:”?token=xxx&salt=yyy”,”cwd”: “/usr/local/srs”,”file”: “./objs/nginx/html/live/livestream.1420254068776.flv”} When DVR records close a file
on_hls_notify HTTP GET and use the variable following:app, stream, param, ts_URL used to push file to cdn network

Push flow authorization

On_publish – When publishing an RTMP stream, security checks are required

Push stream address:

RTMP: / / 192.168.123.22 / live? key=d33c3468&token=5ip0xBNk/livestream

HTTP callback:

The printed POST data is as follows:

(' action '= >' on_publish ', 'client_id' = > 15694, 'IP' = > '192.168.123.100', 'vhost' = > '__defaultVhost__', 'app' = > 'live', 'tcUrl' = > 'RTMP: / / 192.168.123.22 / live? key=d33c3468&token=5ip0xBNk', 'stream' => 'livestream', 'param' => '? key=bd990b32&token=8PJTEelH', )Copy the code

In this case, param or tcUrl parameters can be parsed to obtain key and token parameters transmitted during the push flow, and then authorization can be determined based on the service logic.

If valid, the callback interface must return HTTP Code 200 with an integer error Code (0 indicates success). Other error codes will disconnect the client. To achieve the purpose of publishing RTMP stream security verification.


2, statistics

SRS API

The HTTP interface of the SRS follows the following simple principles:

  • Only JSON data format interfaces are provided, requiring both request and response data to be JSON, and cross-domain support is supported
  • Srs-console Provides a management background by accessing the SRS API
  • When an error occurs, HTTP error codes or CODE error codes in JSON are supported

Modifying a Configuration File

Configuration files need to be http-API enabled:

# the config for srs to delivery RTMP
# @see https://github.com/ossrs/srs/wiki/v1_CN_SampleRTMP
# @see full.conf for detail config.

listen              1935;
max_connections     1000;

#stats
#the http api, for instance, /api/v1/summaries will show these data.
stats {
    # the index of device ip
    network         0;
    # ignore the device of /proc/diskstats if not configed.
    disk            sda sdb xvda xvdb;
}

#http api
#api of srs
http_api {
    enabled         on;
    listen          1985;
    crossdomain     on;
}

vhost __defaultVhost__ {
	
}
Copy the code

The main interface

The address is: http://192.168.123.22:1985/api/v1, main contains a child of the API:

API Example Description
server 4481 Server id
versions /api/v1/versions Obtain the server version information
summaries /api/v1/summaries Get summary information for the server
rusages /api/v1/rusages Get server resource usage information
self_proc_stats /api/v1/self_proc_stats Obtain server process information
system_proc_stats /api/v1/system_proc_stats Gets information about all server processes
meminfos /api/v1/meminfos Gets the server memory usage
authors /api/v1/authors Obtain author, copyright, and License information
features /api/v1/features Obtain the list of functions supported by the system
requests /api/v1/requests Gets information about the request, that is, details about the currently initiated request
vhosts /api/v1/vhosts Obtain vhosts information on the server
streams /api/v1/streams Get streams information for the server
clients /api/v1/clients Get the server’s clients information (default start is 0, count is 10, i.e. the first 10 clients).
configs /api/v1/configs CUID configuration, RAW API

Additional API parameters can access interface, such as http://192.168.123.22:1985/api/v1/summaries

Management console

Srs-console Provides background management services by accessing the SRS API. Enter the server IP address and API port (based on the configuration file) and click Connect to SRS to manage SRS monitoring, video streams, and clients online