Web直播系列5——nginx-rtmp-module实现推流,播放计数统计

前提:

电脑的系统为64位Ubuntu16.04TLS及系列文章2中所实现的简易直播平台https://blog.csdn.net/zzhang_12/article/details

目的:

  • ffmpeg推流rtmp://10.0.0.118:1935/live/test,数据库在地址test的推流number+1,停止推流,自动-1。
  • 某直播地址(rtmp://10.0.0.118:1935/live/test)的rtmp流被播放,数据库播放number+1,停止播放,如关掉浏览器,自动-1。

Ubuntu下的搭建方法:

1)nginx服务器的配置

  • 配置文件的添加
    rtmp {
        server {
            listen 1935;
            chunk_size 4000;
            application live {
    				live on;
    				on_play http://10.0.0.118/camera_monitor/on_play_servlet;
    				on_play_done http://10.0.0.118/camera_monitor/on_play_done_servlet;
    				on_publish http://10.0.0.118/camera_monitor/on_publish_servlet;
    				on_publish_done http://10.0.0.118/camera_monitor/on_publish_done_servlet;       
            }
        }
    }
    
    http {
        #include      /nginx/conf/naxsi_core.rules;
        include       mime.types;
        default_type  application/octet-stream;
    
         upstream 10.0.0.118{
             server 10.0.0.118:8080 weight=1;
             server 10.0.0.119:8080 weight=1;
             server 10.0.0.120:8080 weight=1;
         }
    
        sendfile        off;
        #tcp_nopush     on;
    
        server_names_hash_bucket_size 128;
    
        client_body_timeout   10;
        client_header_timeout 10;
        keepalive_timeout     30;
        send_timeout          10;
        keepalive_requests    10;
    
        server {
            listen       80;
            server_name  10.0.0.118;
    
         location / {
            root   html;
            index  index.html index.htm;
            proxy_pass http://10.0.0.118;   
            }
        
         location camera_monitor/on_play_servlet {
             return 202;
            }
    
         location camera_monitor/on_play_done_servlet {
             return 202;
            }
    
         location camera_monitor/on_publish_servlet {
             return 202; 
            }
    
         location camera_monitor/on_publish_done_servlet{ 
            return 202; 
            }   
        }
    }
  • 参数解释
  1. on_play —— 客户端播放
  2. on_play_done —— 客户端播放结束
  3. on_publish —— 推流开始
  4. on_publish_done —— 推流结束
  5. 参数说明见官方文档https://github.com/arut/nginx-rtmp-module/wiki/Directives#on_publish
     

2)servlet的内容及注意事项

  • on_play_servlet的具体内容
    import java.io.IOException;
    import java.sql.SQLException;
    import javax.servlet.ServletException;
    import javax.servlet.annotation.WebServlet;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    
    @WebServlet("/on_play_servlet")
    public class on_play_servlet extends HttpServlet
    {
    	/**
    	 * 
    	 */
    	private static final long serialVersionUID = 1L;
    
    	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    	{
    		// TODO Auto-generated method stub
    		doPost(request, response);
    	}
    
    	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    	{
    		String vehicleid_direction = request.getParameter("name");#依据上面的配置,此处获得的vehicleid_direction 为test
    	}
    }
  • 其他参数使用与on_play_servlet相似
  • 注意:在使用on_play参数时,如果已成功推流但视频由于延时及播放器等原因未显示时,数据库的播放记录可能仍会+1.

参考:

http://www.ptbird.cn/rtmp-nginx-module-status-and-people.html

猜你喜欢

转载自blog.csdn.net/zzhang_12/article/details/79827316