·您当前的位置:首页 > 技术教程 > FMS教程 >

[FMS]FLV动态控制FMS直播的实现参考代码

时间:2013-09-27 17:03cuplayer.com
[FMS]FLV动态控制FMS直播的实现参考代码,fms视频,as3视频直播,FMS直播

[FMS]FLV动态控制FMS直播的实现参考代码

  1. //main.asc 
  2.  
  3. load("NetServices.asc");     // for Flash remoting 
  4. //load("WebServices.asc");     // for SOAP web services 
  5. load("JSON.asc");     // for JSON 
  6. trace('load json.asc'); 
  7. trace(typeof JSON); 
  8. isLoadCab=0;// 
  9. isLoadVideo=0;//0,-1,1,-2 
  10. Cablist=[];//台列表 
  11. flvCurr=[]; 
  12. videoList=[]; 
  13. Data_array2=[]; 
  14. xmlFile ="http://www.cuplayer.com/x.xml"; 
  15. gateway = "http://www.cuplayer.com/member/amfphp/gateway.php"; 
  16. application.playStream = [];//new Object(); 
  17. /* 
  18. * application.onAppStart: 
  19. *                    is called when application load. It contains Live (out of the box) 
  20. * application specific initializations. 
  21. */ 
  22.  
  23. application.onAppStart = function() 
  24. { 
  25.     // Logging 
  26.     trace("Starting Live Service..."); 
  27.     //  Turning on the Authentication by default 
  28.     this.HTMLDomainsAuth =    true; 
  29.     this.SWFDomainsAuth =    true; 
  30.      
  31.     // Populating the list of domains which are allowed to host HTML file 
  32.     // which in turn may embed a SWF that connects to this application 
  33.     thisthis.allowedHTMLDomains = this.readValidDomains("allowedHTMLdomains.txt","HTMLDomains"); 
  34.  
  35.     // Populating the list of domains which are allowed to host a SWF file 
  36.     // which may connect to this application 
  37.     thisthis.allowedSWFDomains = this.readValidDomains("allowedSWFdomains.txt","SWFDomains"); 
  38.      
  39.     // Logging 
  40.     if(this.HTMLDomainsAuth){ 
  41.         trace("Authentication of HTML page URL domains is enabled"); 
  42.     } 
  43.     if(this.SWFDomainsAuth){ 
  44.         trace("Authentication of SWF URL domains is enabled"); 
  45.     } 
  46.     trace("...loading completed."); 
  47.      
  48.     this.onTime(); 
  49.     //setInterval(onTime,1000); 
  50.  
  51. } 
  52. application.loadCabList = function(){ 
  53.     isLoadCab = -1; 
  54.     var myCablist = new  NetConnection(); 
  55.     myCablist.connect(gateway); 
  56.     var myCablistResponse = new Object(); 
  57.     myCablistResponse.onResult = function(returnData){ 
  58.      //"[{"id":"1","cabname":"\u767e\u5bb6\u4e5001","cabtype":"1","order":"1"}]"     
  59.       Cablist = JSON.parse(returnData); 
  60.       isLoadCab=1; 
  61.     } 
  62.     myCablistResponse.onError = function(error) { 
  63.         trace(error.description); 
  64.         isLoadCab=-2    ; 
  65.     } 
  66.  
  67.     myCablist.call("HelloWorld.cablist",myCablistResponse,"");     
  68.      
  69. } 
  70.  
  71. application.loadVideoList = function(){ 
  72.     isLoadVideo = -1; 
  73.     var myVideolist = new  NetConnection(); 
  74.     myVideolist.connect(gateway); 
  75.     var myVideolistResponse = new Object(); 
  76.     myVideolistResponse.onResult = function(returnData){ 
  77.       videoList = JSON.parse(returnData); 
  78.       trace("videoList:"); 
  79.       trace(outarr(videoList)); 
  80.       isLoadVideo=1; 
  81.     } 
  82.     myVideolistResponse.onError = function(error) { 
  83.         trace(error.description); 
  84.         isLoadVideo=-2    ; 
  85.     } 
  86.  
  87.     myVideolist.call("HelloWorld.videolist",myVideolistResponse,"",videoList,"");     
  88.      
  89. } 
  90. application.playlist = function(CabKey){ 
  91.     //添加一个虚拟的流 
  92.     trace('--------- '+CabKey); 
  93.     //trace(CabKey);    trace(videoList);    trace(flvCurr); 
  94.      
  95.      
  96.     if(!flvCurr[CabKey]){ 
  97.         flvCurr[CabKey]=[]; 
  98.         flvCurr[CabKey]['key']=0; 
  99.     } 
  100.     if(!videoList[CabKey]){ 
  101.         trace('videoList['+CabKey+'] is undefined'); 
  102.         return false; 
  103.     } 
  104.     if( videoList[CabKey].length >0 && (flvCurr[CabKey]['key'] < videoList[CabKey].length) ){ 
  105.         //trace(outarr(videoList[CabKey])); 
  106.          
  107.         if(!application.playStream[CabKey]){//第一次播放 
  108.             trace(CabKey+":第一次播放:添加一个虚拟的流:"+"fo"+CabKey); 
  109.             application.playStream[CabKey] = Stream.get("fo"+CabKey);     
  110.              
  111.             //var flv = videoList[CabKey].shift(); 
  112.             var flv = videoList[CabKey][0]; 
  113.              
  114.             flvCurr[CabKey]['key'] = 0;         
  115.             flvCurr[CabKey]['name'] = flv.flvname;//当前的文件名 
  116.             flvCurr[CabKey]['time'] = 0;//当前的文件已经播放时间 
  117.             flvCurr[CabKey]['starttime'] = 0;//当前文件从总时间开始播放 
  118.             flvCurr[CabKey]['len'] = Stream.length("flv:"+flv.flvname); 
  119.             if(flvCurr[CabKey]['len']==0){//文件错误 
  120.                 videoList[CabKey][0]['status']=-1; 
  121.             }else{ 
  122.                 application.playStream[CabKey].play("flv:"+flv.flvname, 0, false);     
  123.             } 
  124.             trace(outarr(flvCurr)); 
  125.              
  126.         }else{//后面             
  127.             //接近播放结束时,开始播放下一个 
  128.             flvCurr[CabKey]['time'] = application.playStream[CabKey].time - flvCurr[CabKey]['starttime']; 
  129.             if( flvCurr[CabKey]['len'] -  flvCurr[CabKey]['time'] < 1 ){//当前文件马上要播放完了 
  130.                 trace(CabKey+":第N次播放:播放下一条"); 
  131.                 var videoKey = flvCurr[CabKey]['key']; 
  132.                 var flv = videoList[CabKey][videoKey+1]; 
  133.                 flvCurr[CabKey]['key'] = videoKey+1; 
  134.                 flvCurr[CabKey]['name'] = flv.flvname;//当前的文件名 
  135.                 flvCurr[CabKey]['time'] = 0;//当前的文件已经播放时间 
  136.                 flvCurr[CabKey]['starttime'] = application.playStream.time;//当前文件从总时间开始播放 
  137.                 flvCurr[CabKey]['len'] = Stream.length("flv:"+flv.flvname); 
  138.                  
  139.                  
  140.                 if(flvCurr[CabKey]['len']==0){//文件错误 
  141.                     videoList[CabKey][videoKey+1]['status']=-1; 
  142.                 }else{ 
  143.                     application.playStream[CabKey].play("flv:"+flv.flvname, 0, false); 
  144.                     videoList[CabKey][videoKey+1]['status']=1; 
  145.                 } 
  146.                 //将上一条标志为播放完毕 
  147.                 if(videoList[CabKey][videoKey]['status']==1) videoList[CabKey][videoKey]['status']=2; 
  148.                  
  149.                 //将本条视频标志为正在播放 
  150.  
  151.             }else{//当前文件播放中... 
  152.                 trace(CabKey+":第N次播放:当前文件播放中:"); 
  153.                 trace(outarr(flvCurr[CabKey])); 
  154.             } 
  155.          
  156.              
  157.         }         
  158.     }else{ 
  159.         //无播放列表 
  160.         trace(CabKey+":无播放列表"); 
  161.     } 
  162.     //trace('my_video:'+Stream.length("flv:my_video")); 
  163.     //trace('my_video2:'+Stream.length("flv:my_video2")); 
  164.      
  165.      
  166.     //application.playStream.pause(); 
  167.     /* 
  168. array( 
  169.   "name"=>"foo2" 
  170.   "type"=>"flv" 
  171.   "bufferTime"=>"0" 
  172.   "time"=>"0" 
  173.   "publishQueryString"=>"" 
  174.   "maxQueueDelay"=>"500" 
  175.   "maxQueueSize"=>"4096" 
  176.   "syncWrite"=>"false" 
  177. ) 
  178.      
  179.      
  180.     */ 
  181.     //playStream.addEventListener(fl.video.VideoEvent.COMPLETE,Flvpcomplete); 
  182.     //playStream.addEventListener(fl.video.MetadataEvent.CUE_POINT,Flvpvuepoint); 
  183.      
  184.  
  185. } 
  186. application.onTime = function(){ 
  187.     clearInterval(application.s); 
  188.     switch(isLoadCab){ 
  189.         case 0: 
  190.             application.loadCabList(); 
  191.             break; 
  192.         case -1:         
  193.             trace('LoadCab...');     
  194.             break; 
  195.         case 1://载入正常 
  196.             switch(isLoadVideo){ 
  197.                 case 0: 
  198.                     //this.LoadXML(xmlFile); 
  199.                     application.loadVideoList(); 
  200.                     break; 
  201.                 case -1: 
  202.                     trace(videoList); 
  203.                     trace('LoadVideo...'); 
  204.                     break; 
  205.                 case 1: 
  206.                     trace("outarr(Cablist):"); 
  207.                     trace(JSON.stringify(Cablist)); 
  208.                     for(i in Cablist){ 
  209.                         application.playlist(Cablist[i].id); 
  210.                     } 
  211.                     break;     
  212.                 case -2://读取xml失败         
  213.                     isLoadVideo=0;     
  214.                     break;     
  215.                 default:             
  216.                     break;     
  217.                      
  218.             } 
  219.              
  220.             break; 
  221.         case -2: 
  222.             isLoadCab=0; 
  223.             break; 
  224.         default:break; 
  225.     } 
  226.      
  227. /*    if(isLoadVideo==0){ 
  228.         application.LoadXML(xmlFile); 
  229.     } 
  230.     if(isLoadVideo==-1){ 
  231.         //还在读取xml 
  232.     }     
  233.     if(isLoadVideo==1){ 
  234.         application.playlist(); 
  235.         //isLoadVideo=0; 
  236.     } 
  237.     if(isLoadVideo==-2){ 
  238.          
  239.         isLoadVideo=0; 
  240.     }*/ 
  241.     application.s = setInterval(application.onTime,500); 
  242.     //setInterval(onTime,1000); 
  243.     //setInterval(onTime,1000); 
  244. } 
  245.  
  246. /* 
  247. *  application.validate: 
  248. *                 function to validate a given URL by matching through a list of 
  249. * allowed patterns. 
  250. * 
  251. * Parameters: 
  252. *     url:        contains the input url string. 
  253. *     patterns:    Array; an array of permmited url patterns. 
  254. * 
  255. * return value: 
  256. *             true; when 'url domain" contains a listed domains as a suffix. 
  257. *              false; otherwise. 
  258. */ 
  259.  
  260. application.validate = function( url, patterns ) 
  261. { 
  262.     // Convert to lower case 
  263.     urlurl = url.toLowerCase(); 
  264.     var domainStartPos = 0; // domain start position in the URL 
  265.     var domainEndPos = 0; // domain end position in the URL 
  266.      
  267.     switch (url.indexOf( "://" )) 
  268.     { 
  269.         case 4: 
  270.             if(url.indexOf( "http://" ) ==0) 
  271.                 domainStartPos = 7; 
  272.             break; 
  273.         case 5: 
  274.             if(url.indexOf( "https://" ) ==0) 
  275.                 domainStartPos = 8; 
  276.             break; 
  277.     } 
  278.     if(domainStartPos == 0) 
  279.     { 
  280.         // URL must be HTTP or HTTPS protocol based 
  281.         return false; 
  282.     } 
  283.     domainEndPos = url.indexOf("/", domainStartPos); 
  284.     if(domainEndPos>0) 
  285.     { 
  286.         colonPos = url.indexOf(":", domainStartPos); 
  287.         if( (colonPos>0) && (domainEndPos > colonPos)) 
  288.         { 
  289.             // probably URL contains a port number 
  290.             domainEndPos = colonPos; // truncate the port number in the URL 
  291.         } 
  292.     } 
  293.     for ( var i = 0; i < patterns.length; i++ ) 
  294.     { 
  295.         var pos = url.lastIndexOf( patterns[i]); 
  296.         if ( (pos > 0) && (pos  < domainEndPos) && (domainEndPos == (pos + patterns[i].length)) ) 
  297.             return true; 
  298.     } 
  299.     return false; 
  300. } 
  301.  
  302.  
  303. /* 
  304. *     application.onConnect: 
  305. *                 Implementation of the onConnect interface function (optional). 
  306. *  it is invoked whenever a client connection request connection. Live app uses this 
  307. *  function to authenticate the domain of connection and authorizes only 
  308. *  for a subscriber request. 
  309. */ 
  310.  
  311.  
  312. application.onConnect = function( p_client, p_autoSenseBW ) 
  313. { 
  314.     // Check if pageUrl is from a domain we know.     
  315.     // Check pageurl 
  316.  
  317.     // A request from Flash Media Encoder is not checked for authentication 
  318.     if( (p_client.agent.indexOf("FME")==-1) && (p_client.agent.indexOf("FMLE")==-1)) 
  319.     { 
  320.  
  321.         // Authenticating HTML file's domain for the request : 
  322.         // Don't call validate() when the request is from localhost 
  323.         // or HTML Domains Authentication is off. 
  324.         if ((p_client.ip != "127.0.0.1") && application.HTMLDomainsAuth 
  325.                 &&  !this.validate( p_client.pageUrl, this.allowedHTMLDomains ) ) 
  326.         { 
  327.             trace("Authentication failed for pageurl: " + p_client.pageUrl + ", rejecting connection from "+p_client.ip); 
  328.             return false; 
  329.         } 
  330.      
  331.         // Authenticating the SWF file's domain for the request : 
  332.         // Don't call validate() when the request is from localhost 
  333.         // or SWF Domains Authentication is off. 
  334.         if ((p_client.ip != "127.0.0.1") && application.SWFDomainsAuth 
  335.                 &&  !this.validate( p_client.referrer, this.allowedSWFDomains ) ) 
  336.         { 
  337.             trace("Authentication failed for referrer: " + p_client.referrer + ", rejecting connection from "+p_client.ip); 
  338.             return false; 
  339.         } 
  340.             // Logging 
  341.         trace("Accepted a connection from IP:"+ p_client.ip 
  342.                         + ", referrer: "+ p_client.referrer 
  343.                         + ", pageurl: "+ p_client.pageUrl); 
  344.  
  345.     }else{ 
  346.         // Logging 
  347.         trace("Adobe Flash Media Encoder connected from "+p_client.ip); 
  348.     } 
  349.      
  350.  
  351.     // As default, all clients are disabled to access raw audio and video and data bytes in a stream 
  352.     // through the use of BitmapData.draw() and SoundMixer.computeSpectrum()., Please refer 
  353.     // Stream Data Access doccumentations to know flash player version requirement to support this restriction 
  354.     // Access permissions can be allowed for all by uncommenting the following statements 
  355.      
  356.     //p_client.audioSampleAccess = "/"; 
  357.      //p_client.videoSampleAccess = "/";     
  358.  
  359.     this.acceptConnection(p_client); 
  360.          
  361.     // A connection from Flash 8 & 9 FLV Playback component based client 
  362.     // requires the following code. 
  363.  
  364.     if (p_autoSenseBW) 
  365.         p_client.checkBandwidth(); 
  366.     else 
  367.         p_client.call("onBWDone"); 
  368.          
  369. } 
  370.  
  371. /* 
  372. * Client.prototype.getPageUrl 
  373. *                 Public API to return URL of the HTML page.                 
  374. * 
  375. */ 
  376.  
  377. Client.prototype.getPageUrl = function() { 
  378.     return this.pageUrl; 
  379. } 
  380.  
  381. /* 
  382. * Client.prototype.getReferrer 
  383. *                 Public API to return Domain URL of the client SWF file.                 
  384. * 
  385. */ 
  386. Client.prototype.getReferrer = function() { 
  387.     return this.referrer; 
  388. } 
  389.  
  390. /* 
  391. * FCPublish : 
  392. * FME calls FCPublish with the name of the stream whenever a new stream 
  393. * is published. This notification can be used by server-side action script 
  394. * to maintain list of all streams or also to force FME to stop publishing. 
  395. * To stop publishing, call "onFCPublish" with an info object with status 
  396. * code set to "NetStream.Publish.BadName". 
  397. */ 
  398.  
  399. Client.prototype.FCPublish = function( streamname ) 
  400. { 
  401.   
  402.     // setup your stream and check if you want to allow this stream to be published 
  403.     if ( true) // do some validation here 
  404.     {      // this is optional. 
  405.         this.call("onFCPublish", null, {code:"NetStream.Publish.Start", description:streamname}); 
  406.     } 
  407.     else 
  408.     { 
  409.         this.call("onFCPublish", null, {code:"NetStream.Publish.BadName", description:streamname}); 
  410.     } 
  411.          
  412. } 
  413.  
  414. /* 
  415. * FCUnpublish : 
  416. * FME notifies server script when a stream is unpublished. 
  417. */ 
  418.  
  419. Client.prototype.FCUnpublish = function( streamname ) 
  420. { 
  421.     // perform your clean  up 
  422.     this.call("onFCUnpublish", null, {code:"NetStream.Unpublish.Success", description:streamname}); 
  423. } 
  424.  
  425. /* 
  426. * releaseStream : 
  427. * When FME connection to FMS drops during a publishing session it will 
  428. * try and republish the stream when connection is restored. On certain 
  429. * occasions FMS will reject the new stream because server is still 
  430. * unaware of the connection drop, sometimes this can take a few minutes. 
  431. * FME calls "releaseStream" method with the stream name and this can be 
  432. * used to forcibly clear the stream. 
  433. */ 
  434. Client.prototype.releaseStream = function(streamname) 
  435. { 
  436.      s = Stream.get(streamname); 
  437.      s.play(false); 
  438. } 
  439.  
  440.  
  441. /* 
  442. * application.readValidDomains 
  443. *             Function to read Allowed domain file 
  444. * Parameters: 
  445. *         fileName: 
  446. *             name of the file in the application directory 
  447. * which contains one valid domain name per line. This file can contain 
  448. * comments followed by a '#' as the very first charector in that line. 
  449. * a non-comment entry with a space is considered as an error case. 
  450. *   
  451. * returns 
  452. *         an array in which each entry contains a domain name 
  453. * listed in the file. 
  454. */ 
  455.  
  456. application.readValidDomains = function( fileName , domainsType ) 
  457. { 
  458.     var domainFile = new File(fileName); 
  459.     var domainsArray = new Array(); 
  460.     var index = 0; 
  461.     var lineCount = 0; 
  462.     var tempLine; 
  463.     domainFile.open("text", "read"); 
  464.      
  465.     // Read the file line-by-line and fill the domainsArray 
  466.     // with valid entries 
  467.     while (domainFile.isOpen && ! domainFile.eof() ) 
  468.     { 
  469.          
  470.         tempLine = domainFile.readln(); 
  471.         lineCount++; 
  472.         if( !tempLine  || tempLine.indexOf("#") == 0) 
  473.         { 
  474.             continue; 
  475.         } 
  476.         tempLinetempLine = tempLine.trim(); 
  477.         if(tempLine.indexOf(" ")!=-1) 
  478.         { 
  479.             trace("undesired <space>, domain entry ignored. "+fileName+":"+(lineCount+1)); 
  480.         } 
  481.         else 
  482.         { 
  483.             domainsArray[index] =  tempLine.toLowerCase(); 
  484.             index++; 
  485.              
  486.             if(tempLine == "*") 
  487.             { 
  488.                 switch (domainsType){ 
  489.                      
  490.                     case "HTMLDomains": 
  491.                         trace ("Found wildcard (*) entry: disabling authentication for HTML file domains ")    ; 
  492.                         application.HTMLDomainsAuth =    false;         
  493.                         break; 
  494.                      
  495.                     case "SWFDomains": 
  496.                         trace ("Found wildcard (*) entry: disabling authentication for SWF file domains ")    ; 
  497.                         this.SWFDomainsAuth =    false;         
  498.                         break; 
  499.                          
  500.                     default: 
  501.                         // Do nothing 
  502.                         break;     
  503.                 } 
  504.             } 
  505.         } 
  506.     } // End while 
  507.      
  508.     // Something is wrong! the domains file must be accessible. 
  509.     if( !domainFile.isOpen){ 
  510.         trace("Error: could not open '"+fileName+"', rejecting all clients except localhost. "); 
  511.          
  512.     } 
  513.     else 
  514.     { 
  515.         domainFile.close(); 
  516.     } 
  517.  
  518.     return domainsArray; 
  519. } 
  520.  
  521.  
  522. application.LoadXML = function(xmlFile){ 
  523.     trace('LoadXML:' + xmlFile); 
  524.     isLoadVideo = -1; 
  525.     var _loc6 = new XML(); 
  526.     trace('Load XML'); 
  527.     _loc6.load(xmlFile); 
  528.     _loc6._parent = this; 
  529.     _loc6.ignoreWhite = true; 
  530.     _loc6.getBytesLoaded(); 
  531.     _loc6.onLoad = function(e) 
  532.     { 
  533.         trace('Load'); 
  534.         if (e) 
  535.         { 
  536.             trace(e); 
  537.             //var _loc5 = new XMLNode(); 
  538.             //trace(_loc5); 
  539.             trace(this); 
  540.             _loc5 = this.childNodes; 
  541.             trace(_loc5); 
  542.             trace(_loc5[0].childNodes.length); 
  543.  
  544.             Data_array2 = new Array(); 
  545.             for (var _loc3 = 0; _loc3 < _loc5[0].childNodes.length; ++_loc3) 
  546.             { 
  547.  
  548.                 Data_array2[_loc3] = new Object();                 
  549.                 Data_array2[_loc3].flvname = _loc5[0].childNodes[_loc3].attributes.flvname 
  550.                 Data_array2[_loc3].start = _loc5[0].childNodes[_loc3].attributes.start; 
  551.             }// end of for 
  552.             trace('isLoadVideo:xml'); 
  553.             //trace(outarr(Data_array2)); 
  554.             videoList = Data_array2; 
  555.          
  556.             isLoadVideo = 1; 
  557.         } 
  558.         else 
  559.         { 
  560.             isLoadVideo = -2; 
  561.             trace("Load Error"); 
  562.         }// end else if 
  563.     }; 
  564. }// End of the function 
  565.  
  566. outarr  = function(arr, level) 
  567. { 
  568.  
  569.     var astring = ''; 
  570.     if (level == undefined || isNaN(level)) 
  571.     { 
  572.         level = 0; 
  573.     } 
  574.  
  575.     for (a = 0; a < level; a++) 
  576.     { 
  577.         astring += '  '; 
  578.     } 
  579.     //trace('astring:' + astring); 
  580.  
  581.     out = 'array(\r'; 
  582.     for (i in arr) 
  583.     { 
  584.         if (typeof (arr[i]) == 'object') 
  585.         { 
  586.             out += astring + '  "' + i + '"=>' + outarr(arr[i], level + 1); 
  587.         } 
  588.         else 
  589.         { 
  590.             out += astring + '  "' + i + '"=>"' + arr[i] + '"\r'; 
  591.         } 
  592.     } 
  593.     out += astring + ')\r'; 
  594.     return out; 
  595. } 
  596.  
  597. /** 
  598. * String.prototype.trim: 
  599. *             Function to trim spaces in start an end of an input string. 
  600. * returns: 
  601. *         a trimmed string without any leading & ending spaces. 
  602. *          
  603. */ 
  604. String.prototype.trim = function () { 
  605.      
  606.     return this.replace(/^\s*/, "").replace(/\s*$/, ""); 
  607. } 

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器