qq_18980559
qq_18980559
采纳率100%
2020-07-20 10:41 阅读 667

java海康sdk二次开发,视频预览回调如何显示到jsp中。

问题描述:最近用javaweb做海康sdk预览回放,到显示到页面中出现错误,已实现保存到文件中,可以播放。用servlet输出,然后用vediojs播放vedio/x-flv
回调代码

public class REALDATACALLBACK implements FRealDataCallBack_V30{

        // 用于记录当前回调端口号
        public  NativeLongByReference lPort = null;
        // 回调输出文件
        //public  File outputFile = null;

        public  BufferedOutputStream bos = null ;
        @Override
        public void invoke(NativeLong lRealHandle, int dwDataType, ByteByReference pBuffer, int dwBufSize,
                Pointer pUser) {
         //--===============================begin

            /*System.out.println("CallBack:" +lRealHandle.intValue() + "," + dwDataType + "," + dwBufSize +"," +
                    (lPort!=null?lPort.getValue().intValue():-2));*/
            try {
                switch (dwDataType){
                   case  NET_DVR_SYSHEAD://系统头
                       if (!SdkUtil.playCtrl.PlayM4_GetPort(lPort)){//获取播放库未使用的通道号
                           System.out.println("PlayM4_GetPort fail:" + SdkUtil.playCtrl.PlayM4_GetLastError(lPort.getValue()));
                           break;
                       }else{
                           System.out.println("PlayM4_GetPort success:" + lPort.getValue().intValue());
                       }
                       ////第一次回调的是系统头,将获取的播放库 port 号赋值给全局 port,下次回调数据时即使用此 port 号播放
                       if (dwBufSize > 0){
                           if (!SdkUtil.playCtrl.PlayM4_SetStreamOpenMode(lPort.getValue(), PlayCtrl.INSTANCE.STREAME_REALTIME)){ //设置实时流播放模式
                               System.out.println("PlayM4_SetStreamOpenMode fail:" + SdkUtil.playCtrl.PlayM4_GetLastError(lPort.getValue()));
                               break;
                           }
                           if (!SdkUtil.playCtrl.PlayM4_OpenStream(lPort.getValue(), pBuffer, dwBufSize, 1024*1024)){ //打开流接口
                               System.out.println("PlayM4_OpenStream fail:" + SdkUtil.playCtrl.PlayM4_GetLastError(lPort.getValue()));
                               break;
                           }
                           if (!SdkUtil.playCtrl.PlayM4_Play(lPort.getValue(), null)){ //播放开始
                               System.out.println("PlayM4_Play fail:" + SdkUtil.playCtrl.PlayM4_GetLastError(lPort.getValue()));
                               break;
                           }
                       }
                       break;
                   case NET_DVR_STREAMDATA: //码流数据
                       if ((dwBufSize > 0) && (lPort.getValue().intValue() != -1)){
                           if (!SdkUtil.playCtrl.PlayM4_InputData(lPort.getValue(), pBuffer, dwBufSize)){  //输入流数据
                               System.out.println("PlayM4_InputData fail:" + SdkUtil.playCtrl.PlayM4_GetLastError(lPort.getValue()));
                               int errorNo = SdkUtil.playCtrl.PlayM4_GetLastError(lPort.getValue());
                               if(errorNo==11){//缓冲区满,输入流失败,需要重复送入数据
                                   Thread.sleep(2000);
                               }else{
                                   break;
                               }
                           }else{
                               //System.out.println("PlayM4_InputData success:" +dwBufSize );
                               if(bos!=null){
                                   byte[] outputData = pBuffer.getPointer().getByteArray(0, dwBufSize);
                                   bos.write(outputData);
                               }
                           }
                       }else{
                           System.out.println("CallBack:4:"+lPort.getValue()+","+ dwBufSize);
                       }
                }
            } catch (Exception e) {
                System.out.println("error:" + e.getMessage());
            }
        }
    }

servlet代码

response.setContentType("video/x-flv");
                response.setHeader("Content-Disposition", "attachment;filename=".concat("abc.flv"));//虚拟文件名
try {
                    g_RealDataCallBack_V30.bos = new BufferedOutputStream(response.getOutputStream(),1024*60);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
                while(SdkUtil.handleMap.containsKey(resultKeyBuffer.toString()) && g_RealDataCallBack_V30!=null){

                    try {
                        Thread.sleep(300);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }

vediojs代码

<!DOCTYPE html>
<html lang="en">

<head>
    <title>flv文件播放(不兼容)</title>
    <!-- Video.js -->
    <link href="https://unpkg.com/video.js/dist/video-js.css" rel="stylesheet">
    <script src="https://unpkg.com/video.js/dist/video.min.js"></script>
    <script src="https://unpkg.com/flv.js/dist/flv.min.js"></script>
    <script src="https://unpkg.com/videojs-flvjs/dist/videojs-flvjs.min.js"></script>
</head>

<body>
    <div>
        <video id="videojs-flvjs-player" class="video-js vjs-default-skin vjs-big-play-centered"  width="1024" height="768"> </video>
    </div>
</body>

</html>
<script>
var flvUrl = "/sdk_modify_web/ViewServlet";
var player = videojs('videojs-flvjs-player', {
    techOrder: ['html5', 'flvjs'],
    flvjs: {
        mediaDataSource: {
            isLive: false,
            cors: true,
            withCredentials: false,
        },
    },
    sources: [{
        src: flvUrl,
        type: 'video/mp4'
    }],
    controls: true,
    preload: "none"
}, function onPlayerReady() {
    console.log('player ready')

    player.on('error', (err) => {
        console.log('first source load fail')

        player.src({
            src: flvUrl,
            type: 'video/x-flv'
        });

        player.ready(function() {
            console.log('player ready')
            player.load();
            player.play();
        });
    })
});
</script>

目前情况:单独用servelt输出文件可以播放,回调保存文件也可播放
回调函数报错信息:java.io.IOException: 断开的管道
文件格式为:应该为flv格式
问:如何才能正确播放回调函数返回视频数据?请各位大神解答下,不胜感激涕零!!!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

相关推荐