`
137459045
  • 浏览: 22441 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Wow录制接口开发

 
阅读更多

基于WOWZA的录制开发和rtsp流的发布:

public class WowzaLiveStreamRecord extends HTTProvider2Base {
    private static final Class<WowzaLiveStreamRecord> CLASS = WowzaLiveStreamRecord.class;
 public void onHTTPRequest(IVHost vhost, IHTTPRequest req, IHTTPResponse resp) {
  if (!doHTTPAuthentication(vhost, req, resp))
   return;
  
  IApplication application=vhost.getApplication("live");
  IApplicationInstance appInstance=application.getAppInstance("_definst_");
  OutputStream out = resp.getOutputStream();
  
  String action=req.getParameter("action");
  String fileformat=req.getParameter("fileformat");
  String filepath=req.getParameter("filepath");
  String parameters=req.getParameter("parameters");
  StringBuffer resstr=new StringBuffer();
  
  WowzaDaoImpl wowza=new WowzaDaoImpl();
  try{
      if(action==null || action.equals("")){       
       Common.setRetInfo(resstr,"","fail","action is required");
       out.write(resstr.toString().getBytes());
       resstr=null;       
       return;
      }
      if(action.equals("startRecording") || action.equals("stopRecording") || action.equals("streamInfo")){
       if(parameters==null || parameters.equals("")){
        Common.setRetInfo(resstr,"","fail","parameters is required");
        out.write(resstr.toString().getBytes());
        resstr=null;  
        return;
       }
       List l=Common.jsonToList(parameters,Response.class);
    for(int i=0;i<l.size();i++){
     Response p=(Response)l.get(i);
     String streamname=p.getStreamname();
        if(streamname==null || streamname.equals("")){         
         Common.setRetInfo(resstr,"","fail","streamname is required");
         out.write(resstr.toString().getBytes());
         resstr=null; 
         return;
        }
    }
    for(int j=0;j<l.size();j++){
     Response p=(Response)l.get(j);
     String streamname=p.getStreamname();
     //开始录制
     if(action.equals("startRecording")){
      String filename=p.getFilename();
      if(filename==null || filename.equals("")) filename=Common.getFileName();    
      StreamRecorderParameters recordParams = new StreamRecorderParameters(appInstance);
      
      if(fileformat==null ||fileformat.equals("")) fileformat="1";
      if(filepath!=null && !filepath.equals("")){
       recordParams.outputPath=filepath;
       //若文件路径不存在,自行建立
             File fDir = new File(filepath);
       if (!fDir.exists()) {
        fDir.mkdir();
       }
       File file = new File(filepath);
       if(!file.exists()) file.mkdirs();       
      }
      
      if(fileformat.equals("1")) recordParams.fileFormat=IStreamRecorderConstants.FORMAT_FLV;
      
      if(fileformat.equals("2")) recordParams.fileFormat=IStreamRecorderConstants.FORMAT_MP4;
      
      recordParams.outputFile=filename;
      
      recordParams.segmentationType = IStreamRecorderConstants.SEGMENT_NONE;
      
      recordParams.versioningOption = IStreamRecorderConstants.OVERWRITE_FILE;
      IStreamRecorder  rec=vhost.getLiveStreamRecordManager().getRecorder(appInstance,streamname);
      if(rec==null){
          vhost.getLiveStreamRecordManager().startRecording(appInstance,streamname,recordParams);
          Common.setRetInfo(resstr,streamname,"succ","");
      }else{
       Common.setRetInfo(resstr,streamname,"fail","record stream exist");
      }
      
      resstr.append(",");
       
     }
     
     //结束录制
     if(action.equals("stopRecording")){
      IStreamRecorder  rec=vhost.getLiveStreamRecordManager().getRecorder(appInstance,streamname);
      if(rec==null){//判断是否有录制流
          Common.setRetStreamInfo(resstr,streamname, "","record stream not exist", "", "");
          resstr.append(",");
          continue;
      }
      wowza.streamInfo(rec,resstr);      
         resstr.append(",");
         vhost.getLiveStreamRecordManager().stopRecording(appInstance,streamname); 
     }
     
     //流信息查询
     if(action.equals("streamInfo")){
      IStreamRecorder  rec=vhost.getLiveStreamRecordManager().getRecorder(appInstance,streamname);
      if(rec==null){//判断是否有录制流
          Common.setRetStreamInfo(resstr,streamname, "","record stream not exist", "", "");
          resstr.append(",");
          continue;
      }
      wowza.streamInfo(rec,resstr);      
         resstr.append(",");
     }   
    }
    String resstrs=resstr.toString();
       if(!resstrs.toString().equals("")) resstrs=resstrs.substring(0,resstrs.length()-1);
    out.write(resstrs.getBytes());
       resstr=null; 
      }
 
   //录制流列表
   if(action.equals("streamRecordList")){
    List  lx=vhost.getLiveStreamRecordManager().getRecordersList(appInstance);
    
    for(int i=0;i<lx.size();i++){
     IStreamRecorder  rec=(IStreamRecorder)lx.get(i);
     
     if(rec.getStream()==null){//判断是否有录制流
         Common.setRetStreamInfo(resstr,rec.getStreamName(), "","record stream not exist", "", "");
         resstr.append(",");
         continue;
     }
     wowza.streamInfo(rec,resstr);  
        resstr.append(",");
    }
    String resstrs=resstr.toString();
       if(!resstrs.toString().equals("")) resstrs=resstrs.substring(0,resstrs.length()-1);
    out.write(resstrs.getBytes());
       resstr=null;  
   }
   
   // 添加流文件,0文件已存在,1添加成功
   if(action.toLowerCase().equals("addstreamfile")){
    String filesName = req.getParameter("filesName");
    String url = req.getParameter("url");
    String streamFilePath = new File(appInstance.getStreamStoragePath()).getAbsolutePath() + "/" + filesName;
    File streamFile = new File(streamFilePath);
    if(streamFile.exists()) {// 流文件存在
     out.write("0".getBytes());
    } else {
     BufferedWriter bos = new BufferedWriter(new FileWriter(streamFile));
     bos.write(url);
     bos.flush();
     bos.close();
     out.write("1".getBytes());
    }
   }
   // 删除流文件,0流文件不存在,1删除成功
   if(action.toLowerCase().equals("delstreamfile")){
    String filesName = req.getParameter("filesName");
    appInstance.stopMediaCasterStream(filesName);
    String streamFilePath = new File(appInstance.getStreamStoragePath()).getAbsolutePath() + "/" + filesName;
    File streamFile = new File(streamFilePath);
    if(!streamFile.exists()) {// 流文件不存在
     out.write("0".getBytes());
    } else {
     streamFile.delete();
     out.write("1".getBytes());
    }
   }
   // 发布流文件,0发布失败,1发布成功
   if(action.toLowerCase().equals("startstreamfile")){
    String filesName = req.getParameter("filesName");
    String streamFilePath = new File(appInstance.getStreamStoragePath()).getAbsolutePath() + "/" + filesName;
    File streamFile = new File(streamFilePath);
    if(!streamFile.exists()) {// 流文件存在
     out.write("0".getBytes());
    } else {
     appInstance.startMediaCasterStream(filesName, "rtp");
     out.write("1".getBytes());
    }
    com.wowza.wms.vhost.StartupStream stream = new com.wowza.wms.vhost.StartupStream();
    stream.setApplicationName(application.getName() + "/" + appInstance.getName());
    stream.setMediaCasterType("rtp");
    stream.setStreamName(filesName);
    vhost.addStartupStream(stream);
   }
     }catch(Exception e){
   StringWriter sw=new StringWriter();
   PrintWriter pw=new PrintWriter(sw);
   e.printStackTrace(pw);
   try {
       Common.setRetInfo(resstr,"","exception",sw.toString());
       out.write(resstr.toString().getBytes());
       resstr=null;  
   } catch (IOException ex) {
    ex.printStackTrace();
   }
   WMSLoggerFactory.getLogger(CLASS).info("WowzaLiveStreamRecord:"+sw.toString());
     }
 }
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics