winlin

add cherrypy api server

@@ -15,7 +15,7 @@ step 1: build srs <br/> @@ -15,7 +15,7 @@ step 1: build srs <br/>
15 <pre> 15 <pre>
16 tar xf simple-rtmp-server-*.*.tar.gz 16 tar xf simple-rtmp-server-*.*.tar.gz
17 cd simple-rtmp-server-*.*/trunk 17 cd simple-rtmp-server-*.*/trunk
18 -./configure --with-ssl --with-hls --with-ffmpeg 18 +./configure --with-ssl --with-hls --with-ffmpeg --with-http
19 make 19 make
20 </pre> 20 </pre>
21 step 2: start srs <br/> 21 step 2: start srs <br/>
@@ -30,6 +30,10 @@ step 4(optional): start nginx for HLS <br/> @@ -30,6 +30,10 @@ step 4(optional): start nginx for HLS <br/>
30 <pre> 30 <pre>
31 sudo ./objs/nginx/sbin/nginx 31 sudo ./objs/nginx/sbin/nginx
32 </pre> 32 </pre>
  33 +step 5(optional): start http hooks for srs callback <br/>
  34 +<pre>
  35 +python ./research/api-server/server.py 8085
  36 +</pre>
33 step 5: publish live stream <br/> 37 step 5: publish live stream <br/>
34 <pre> 38 <pre>
35 FMS URL: rtmp://127.0.0.1:1935/live 39 FMS URL: rtmp://127.0.0.1:1935/live
@@ -87,6 +87,12 @@ vhost dev { @@ -87,6 +87,12 @@ vhost dev {
87 hls_window 30; 87 hls_window 30;
88 #forward 127.0.0.1:19350; 88 #forward 127.0.0.1:19350;
89 #forward 127.0.0.1:1936; 89 #forward 127.0.0.1:1936;
  90 + on_connect http://127.0.0.1:8085/api/v1/clients;
  91 + on_close http://127.0.0.1:8085/api/v1/clients;
  92 + on_publish http://127.0.0.1:8085/api/v1/streams;
  93 + on_unpublish http://127.0.0.1:8085/api/v1/streams;
  94 + on_play http://127.0.0.1:8085/api/v1/sessions;
  95 + on_stop http://127.0.0.1:8085/api/v1/sessions;
90 transcode { 96 transcode {
91 enabled off; 97 enabled off;
92 ffmpeg ./objs/ffmpeg/bin/ffmpeg; 98 ffmpeg ./objs/ffmpeg/bin/ffmpeg;
@@ -174,4 +174,7 @@ fi @@ -174,4 +174,7 @@ fi
174 if [ $SRS_FFMPEG = YES ]; then 174 if [ $SRS_FFMPEG = YES ]; then
175 echo -e "\" ./objs/ffmpeg/bin/ffmpeg \" is used for live stream transcoding" 175 echo -e "\" ./objs/ffmpeg/bin/ffmpeg \" is used for live stream transcoding"
176 fi 176 fi
  177 +if [ $SRS_HTTP = YES ]; then
  178 + echo -e "\" python ./research/api-server/server.py 8085 \" to start the api-server"
  179 +fi
177 echo "\" ./objs/simple_rtmp_server -c conf/srs.conf \" to start the srs live server" 180 echo "\" ./objs/simple_rtmp_server -c conf/srs.conf \" to start the srs live server"
  1 +#!/usr/bin/python
  2 +'''
  3 +The MIT License (MIT)
  4 +
  5 +Copyright (c) 2013 winlin
  6 +
  7 +Permission is hereby granted, free of charge, to any person obtaining a copy of
  8 +this software and associated documentation files (the "Software"), to deal in
  9 +the Software without restriction, including without limitation the rights to
  10 +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  11 +the Software, and to permit persons to whom the Software is furnished to do so,
  12 +subject to the following conditions:
  13 +
  14 +The above copyright notice and this permission notice shall be included in all
  15 +copies or substantial portions of the Software.
  16 +
  17 +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  19 +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  20 +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  21 +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  22 +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  23 +'''
  24 +
  25 +"""
  26 +the api-server is a default demo server for srs to call
  27 +when srs get some event, for example, when client connect
  28 +to srs, srs can invoke the http api of the api-server
  29 +"""
  30 +
  31 +import sys;
  32 +# reload sys model to enable the getdefaultencoding method.
  33 +reload(sys);
  34 +# set the default encoding to utf-8
  35 +# using exec to set the encoding, to avoid error in IDE.
  36 +exec("sys.setdefaultencoding('utf-8')");
  37 +assert sys.getdefaultencoding().lower() == "utf-8";
  38 +
  39 +if __name__ != "__main__":
  40 + raise Exception("embed not support");
  41 +
  42 +if len(sys.argv) <= 1:
  43 + print "SRS api callback server, Copyright (c) 2013 winlin"
  44 + print "Usage: python %s <port>"%(sys.argv[0])
  45 + print " port: the port to listen at."
  46 + print "For example:"
  47 + print " python %s 8085"%(sys.argv[0])
  48 + print ""
  49 + print "See also: https://github.com/winlinvip/simple-rtmp-server"
  50 + sys.exit(1)
  51 +
  52 +import datetime, cherrypy
  53 +
  54 +def trace(msg):
  55 + date = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  56 + print "[%s][trace] %s"%(date, msg)
  57 +
  58 +class RESTClients(object):
  59 + pass;
  60 +
  61 +class Root(object):
  62 + def __init__(self):
  63 + self.api = Api()
  64 +
  65 +class Api(object):
  66 + def __init__(self):
  67 + self.v1 = V1()
  68 +
  69 +class V1(object):
  70 + def __init__(self):
  71 + self.clients = RESTClients()
  72 +
  73 +port = int(sys.argv[1])
  74 +trace("api server listen at port: %s"%(port))
  75 +
  76 +conf = {
  77 + 'global': {
  78 + 'server.shutdown_timeout': 1,
  79 + 'server.socket_host': '0.0.0.0',
  80 + 'server.socket_port': port,
  81 + 'tools.encode.on': True,
  82 + 'tools.encode.encoding': "utf-8"
  83 + },
  84 + '/': {
  85 + 'request.dispatch': cherrypy.dispatch.MethodDispatcher()
  86 + }
  87 +}
  88 +
  89 +trace("start cherrypy server")
  90 +cherrypy.quickstart(Root(), '/', conf)