基于JavaCV技术实现RTSP推流和拉流功能

RTSP简介

RTSP(Real Time Streaming Protocol)是由Real Network和Netscape共同提出的如何有效地在IP网络上传输流媒体数据的应用层协议。RTSP对流媒体提供了诸如暂停,快进等控制,而它本身并不传输数据,RTSP的作用相当于流媒体服务器的远程控制。服务器端可以自行选择使用TCP或UDP来传送串流内容,它的语法和运作跟HTTP 1.1类似,但并不特别强调时间同步,所以比较能容忍网络延迟。而且允许同时多个串流需求控制(Multicast),除了可以降低服务器端的网络用量,还可以支持多方视频会议(Video onference)。 因为与HTTP1.1的运作方式相似,所以代理服务器《Proxy》的快取功能《Cache》也同样适用于RTSP,并因RTSP具有重新导向功能,可视实际负载情况来转换提供服务的服务器,以避免过大的负载集中于同一服务器而造成延迟。

推流过程

  1. 首先为了将来可以扩展多种协议(TRSP、RTMP)的视频推流功能,我们创建BStreamer基类。
/**
 1. 基础视频流
 */
public class BStreamer {
    private int width = 640;
    private int height = 480;
    private String url;

    public BStreamer(String url) {
        this.url = url;
    }

    public BStreamer(String url, int w, int h) {
        this.url = url;
        if (w > 0 && h > 0) {
            this.width = w;
            this.height = h;
        }
    }
    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}
  1. 根据上面BStreamer基类,我们扩展实现RTSP协议的视频推流器,由于本实例是采用FFMPEG为推流手段,所以我们下面创建FFmpegPusher实体类。
public class FFmpegPusher extends BStreamer {
    public FFmpegPusher(String url) {
        super(url);
    }

    public FFmpegPusher(String url, int w, int h) {
        super(url, w, h);
    }
    private Process process;

    public void push(){
        String basePath="F:\\ffmpeg-4.2.1-win64-shared\\bin";
        String videoPath=String.format("%s\\test.mp4",basePath);
        String ffmpegPath=String.format("%s\\ffmpeg",basePath);
       try {
            // 视频切换时,先销毁进程,全局变量Process process,方便进程销毁重启,即切换推流视频
            if(process != null){
                process.destroy();
                System.out.println(">>>>>>>>>>推流视频切换<<<<<<<<<<");
            }
           // ffmpeg开头,-re代表按照帧率发送,在推流时必须有
           // 指定要推送的视频
           // 指定推送服务器,-f:指定格式
           String command =String.format("%s -re -i %s -f rtsp %s",
                   ffmpegPath,videoPath,getUrl());
            System.out.println("ffmpeg推流命令:" + command);
            // 运行cmd命令,获取其进程
            process = Runtime.getRuntime().exec(command);
            // 输出ffmpeg推流日志
            BufferedReader br= new BufferedReader(new InputStreamReader(process.getErrorStream()));
            String line = "";
            while ((line = br.readLine()) != null) {
                System.out.println("视频推流信息[" + line + "]");
            }
            process.waitFor();
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
  1. 创建单元测试
public class TestRTSPFFmpegPusher {
    public static void main(String[] args) throws Exception {
        String url="rtsp://192.168.56.1/test";
        FFmpegPusher pusher=new FFmpegPusher(url);
        pusher.push();
    }
}
  1. 运行以上测试程序得到如下结果
    基于JavaCV技术实现RTSP推流和拉流功能_第1张图片

  2. 在服务端查看推流结果(服务下载地址:http://www.easydarwin.org/)
    基于JavaCV技术实现RTSP推流和拉流功能_第2张图片

  3. 客户端测试拉取上面的视频流进行直播显示
    基于JavaCV技术实现RTSP推流和拉流功能_第3张图片
    以上是采用RTSP协议进行推流测试,实例比较简单,主要起到抛砖引玉的功能。

你可能感兴趣的:(流媒体技术)