[FMS]FLV动态控制FMS直播的实现参考代码
[FMS]FLV动态控制FMS直播的实现参考代码,fms视频,as3视频直播,FMS直播
[FMS]FLV动态控制FMS直播的实现参考代码
- //main.asc
- load("NetServices.asc"); // for Flash remoting
- //load("WebServices.asc"); // for SOAP web services
- load("JSON.asc"); // for JSON
- trace('load json.asc');
- trace(typeof JSON);
- isLoadCab=0;//
- isLoadVideo=0;//0,-1,1,-2
- Cablist=[];//台列表
- flvCurr=[];
- videoList=[];
- Data_array2=[];
- xmlFile ="http://www.cuplayer.com/x.xml";
- gateway = "http://www.cuplayer.com/member/amfphp/gateway.php";
- application.playStream = [];//new Object();
- /*
- * application.onAppStart:
- * is called when application load. It contains Live (out of the box)
- * application specific initializations.
- */
- application.onAppStart = function()
- {
- // Logging
- trace("Starting Live Service...");
- // Turning on the Authentication by default
- this.HTMLDomainsAuth = true;
- this.SWFDomainsAuth = true;
- // Populating the list of domains which are allowed to host HTML file
- // which in turn may embed a SWF that connects to this application
- thisthis.allowedHTMLDomains = this.readValidDomains("allowedHTMLdomains.txt","HTMLDomains");
- // Populating the list of domains which are allowed to host a SWF file
- // which may connect to this application
- thisthis.allowedSWFDomains = this.readValidDomains("allowedSWFdomains.txt","SWFDomains");
- // Logging
- if(this.HTMLDomainsAuth){
- trace("Authentication of HTML page URL domains is enabled");
- }
- if(this.SWFDomainsAuth){
- trace("Authentication of SWF URL domains is enabled");
- }
- trace("...loading completed.");
- this.onTime();
- //setInterval(onTime,1000);
- }
- application.loadCabList = function(){
- isLoadCab = -1;
- var myCablist = new NetConnection();
- myCablist.connect(gateway);
- var myCablistResponse = new Object();
- myCablistResponse.onResult = function(returnData){
- //"[{"id":"1","cabname":"\u767e\u5bb6\u4e5001","cabtype":"1","order":"1"}]"
- Cablist = JSON.parse(returnData);
- isLoadCab=1;
- }
- myCablistResponse.onError = function(error) {
- trace(error.description);
- isLoadCab=-2 ;
- }
- myCablist.call("HelloWorld.cablist",myCablistResponse,"");
- }
- application.loadVideoList = function(){
- isLoadVideo = -1;
- var myVideolist = new NetConnection();
- myVideolist.connect(gateway);
- var myVideolistResponse = new Object();
- myVideolistResponse.onResult = function(returnData){
- videoList = JSON.parse(returnData);
- trace("videoList:");
- trace(outarr(videoList));
- isLoadVideo=1;
- }
- myVideolistResponse.onError = function(error) {
- trace(error.description);
- isLoadVideo=-2 ;
- }
- myVideolist.call("HelloWorld.videolist",myVideolistResponse,"",videoList,"");
- }
- application.playlist = function(CabKey){
- //添加一个虚拟的流
- trace('--------- '+CabKey);
- //trace(CabKey); trace(videoList); trace(flvCurr);
- if(!flvCurr[CabKey]){
- flvCurr[CabKey]=[];
- flvCurr[CabKey]['key']=0;
- }
- if(!videoList[CabKey]){
- trace('videoList['+CabKey+'] is undefined');
- return false;
- }
- if( videoList[CabKey].length >0 && (flvCurr[CabKey]['key'] < videoList[CabKey].length) ){
- //trace(outarr(videoList[CabKey]));
- if(!application.playStream[CabKey]){//第一次播放
- trace(CabKey+":第一次播放:添加一个虚拟的流:"+"fo"+CabKey);
- application.playStream[CabKey] = Stream.get("fo"+CabKey);
- //var flv = videoList[CabKey].shift();
- var flv = videoList[CabKey][0];
- flvCurr[CabKey]['key'] = 0;
- flvCurr[CabKey]['name'] = flv.flvname;//当前的文件名
- flvCurr[CabKey]['time'] = 0;//当前的文件已经播放时间
- flvCurr[CabKey]['starttime'] = 0;//当前文件从总时间开始播放
- flvCurr[CabKey]['len'] = Stream.length("flv:"+flv.flvname);
- if(flvCurr[CabKey]['len']==0){//文件错误
- videoList[CabKey][0]['status']=-1;
- }else{
- application.playStream[CabKey].play("flv:"+flv.flvname, 0, false);
- }
- trace(outarr(flvCurr));
- }else{//后面
- //接近播放结束时,开始播放下一个
- flvCurr[CabKey]['time'] = application.playStream[CabKey].time - flvCurr[CabKey]['starttime'];
- if( flvCurr[CabKey]['len'] - flvCurr[CabKey]['time'] < 1 ){//当前文件马上要播放完了
- trace(CabKey+":第N次播放:播放下一条");
- var videoKey = flvCurr[CabKey]['key'];
- var flv = videoList[CabKey][videoKey+1];
- flvCurr[CabKey]['key'] = videoKey+1;
- flvCurr[CabKey]['name'] = flv.flvname;//当前的文件名
- flvCurr[CabKey]['time'] = 0;//当前的文件已经播放时间
- flvCurr[CabKey]['starttime'] = application.playStream.time;//当前文件从总时间开始播放
- flvCurr[CabKey]['len'] = Stream.length("flv:"+flv.flvname);
- if(flvCurr[CabKey]['len']==0){//文件错误
- videoList[CabKey][videoKey+1]['status']=-1;
- }else{
- application.playStream[CabKey].play("flv:"+flv.flvname, 0, false);
- videoList[CabKey][videoKey+1]['status']=1;
- }
- //将上一条标志为播放完毕
- if(videoList[CabKey][videoKey]['status']==1) videoList[CabKey][videoKey]['status']=2;
- //将本条视频标志为正在播放
- }else{//当前文件播放中...
- trace(CabKey+":第N次播放:当前文件播放中:");
- trace(outarr(flvCurr[CabKey]));
- }
- }
- }else{
- //无播放列表
- trace(CabKey+":无播放列表");
- }
- //trace('my_video:'+Stream.length("flv:my_video"));
- //trace('my_video2:'+Stream.length("flv:my_video2"));
- //application.playStream.pause();
- /*
- array(
- "name"=>"foo2"
- "type"=>"flv"
- "bufferTime"=>"0"
- "time"=>"0"
- "publishQueryString"=>""
- "maxQueueDelay"=>"500"
- "maxQueueSize"=>"4096"
- "syncWrite"=>"false"
- )
- */
- //playStream.addEventListener(fl.video.VideoEvent.COMPLETE,Flvpcomplete);
- //playStream.addEventListener(fl.video.MetadataEvent.CUE_POINT,Flvpvuepoint);
- }
- application.onTime = function(){
- clearInterval(application.s);
- switch(isLoadCab){
- case 0:
- application.loadCabList();
- break;
- case -1:
- trace('LoadCab...');
- break;
- case 1://载入正常
- switch(isLoadVideo){
- case 0:
- //this.LoadXML(xmlFile);
- application.loadVideoList();
- break;
- case -1:
- trace(videoList);
- trace('LoadVideo...');
- break;
- case 1:
- trace("outarr(Cablist):");
- trace(JSON.stringify(Cablist));
- for(i in Cablist){
- application.playlist(Cablist[i].id);
- }
- break;
- case -2://读取xml失败
- isLoadVideo=0;
- break;
- default:
- break;
- }
- break;
- case -2:
- isLoadCab=0;
- break;
- default:break;
- }
- /* if(isLoadVideo==0){
- application.LoadXML(xmlFile);
- }
- if(isLoadVideo==-1){
- //还在读取xml
- }
- if(isLoadVideo==1){
- application.playlist();
- //isLoadVideo=0;
- }
- if(isLoadVideo==-2){
- isLoadVideo=0;
- }*/
- application.s = setInterval(application.onTime,500);
- //setInterval(onTime,1000);
- //setInterval(onTime,1000);
- }
- /*
- * application.validate:
- * function to validate a given URL by matching through a list of
- * allowed patterns.
- *
- * Parameters:
- * url: contains the input url string.
- * patterns: Array; an array of permmited url patterns.
- *
- * return value:
- * true; when 'url domain" contains a listed domains as a suffix.
- * false; otherwise.
- */
- application.validate = function( url, patterns )
- {
- // Convert to lower case
- urlurl = url.toLowerCase();
- var domainStartPos = 0; // domain start position in the URL
- var domainEndPos = 0; // domain end position in the URL
- switch (url.indexOf( "://" ))
- {
- case 4:
- if(url.indexOf( "http://" ) ==0)
- domainStartPos = 7;
- break;
- case 5:
- if(url.indexOf( "https://" ) ==0)
- domainStartPos = 8;
- break;
- }
- if(domainStartPos == 0)
- {
- // URL must be HTTP or HTTPS protocol based
- return false;
- }
- domainEndPos = url.indexOf("/", domainStartPos);
- if(domainEndPos>0)
- {
- colonPos = url.indexOf(":", domainStartPos);
- if( (colonPos>0) && (domainEndPos > colonPos))
- {
- // probably URL contains a port number
- domainEndPos = colonPos; // truncate the port number in the URL
- }
- }
- for ( var i = 0; i < patterns.length; i++ )
- {
- var pos = url.lastIndexOf( patterns[i]);
- if ( (pos > 0) && (pos < domainEndPos) && (domainEndPos == (pos + patterns[i].length)) )
- return true;
- }
- return false;
- }
- /*
- * application.onConnect:
- * Implementation of the onConnect interface function (optional).
- * it is invoked whenever a client connection request connection. Live app uses this
- * function to authenticate the domain of connection and authorizes only
- * for a subscriber request.
- */
- application.onConnect = function( p_client, p_autoSenseBW )
- {
- // Check if pageUrl is from a domain we know.
- // Check pageurl
- // A request from Flash Media Encoder is not checked for authentication
- if( (p_client.agent.indexOf("FME")==-1) && (p_client.agent.indexOf("FMLE")==-1))
- {
- // Authenticating HTML file's domain for the request :
- // Don't call validate() when the request is from localhost
- // or HTML Domains Authentication is off.
- if ((p_client.ip != "127.0.0.1") && application.HTMLDomainsAuth
- && !this.validate( p_client.pageUrl, this.allowedHTMLDomains ) )
- {
- trace("Authentication failed for pageurl: " + p_client.pageUrl + ", rejecting connection from "+p_client.ip);
- return false;
- }
- // Authenticating the SWF file's domain for the request :
- // Don't call validate() when the request is from localhost
- // or SWF Domains Authentication is off.
- if ((p_client.ip != "127.0.0.1") && application.SWFDomainsAuth
- && !this.validate( p_client.referrer, this.allowedSWFDomains ) )
- {
- trace("Authentication failed for referrer: " + p_client.referrer + ", rejecting connection from "+p_client.ip);
- return false;
- }
- // Logging
- trace("Accepted a connection from IP:"+ p_client.ip
- + ", referrer: "+ p_client.referrer
- + ", pageurl: "+ p_client.pageUrl);
- }else{
- // Logging
- trace("Adobe Flash Media Encoder connected from "+p_client.ip);
- }
- // As default, all clients are disabled to access raw audio and video and data bytes in a stream
- // through the use of BitmapData.draw() and SoundMixer.computeSpectrum()., Please refer
- // Stream Data Access doccumentations to know flash player version requirement to support this restriction
- // Access permissions can be allowed for all by uncommenting the following statements
- //p_client.audioSampleAccess = "/";
- //p_client.videoSampleAccess = "/";
- this.acceptConnection(p_client);
- // A connection from Flash 8 & 9 FLV Playback component based client
- // requires the following code.
- if (p_autoSenseBW)
- p_client.checkBandwidth();
- else
- p_client.call("onBWDone");
- }
- /*
- * Client.prototype.getPageUrl
- * Public API to return URL of the HTML page.
- *
- */
- Client.prototype.getPageUrl = function() {
- return this.pageUrl;
- }
- /*
- * Client.prototype.getReferrer
- * Public API to return Domain URL of the client SWF file.
- *
- */
- Client.prototype.getReferrer = function() {
- return this.referrer;
- }
- /*
- * FCPublish :
- * FME calls FCPublish with the name of the stream whenever a new stream
- * is published. This notification can be used by server-side action script
- * to maintain list of all streams or also to force FME to stop publishing.
- * To stop publishing, call "onFCPublish" with an info object with status
- * code set to "NetStream.Publish.BadName".
- */
- Client.prototype.FCPublish = function( streamname )
- {
- // setup your stream and check if you want to allow this stream to be published
- if ( true) // do some validation here
- { // this is optional.
- this.call("onFCPublish", null, {code:"NetStream.Publish.Start", description:streamname});
- }
- else
- {
- this.call("onFCPublish", null, {code:"NetStream.Publish.BadName", description:streamname});
- }
- }
- /*
- * FCUnpublish :
- * FME notifies server script when a stream is unpublished.
- */
- Client.prototype.FCUnpublish = function( streamname )
- {
- // perform your clean up
- this.call("onFCUnpublish", null, {code:"NetStream.Unpublish.Success", description:streamname});
- }
- /*
- * releaseStream :
- * When FME connection to FMS drops during a publishing session it will
- * try and republish the stream when connection is restored. On certain
- * occasions FMS will reject the new stream because server is still
- * unaware of the connection drop, sometimes this can take a few minutes.
- * FME calls "releaseStream" method with the stream name and this can be
- * used to forcibly clear the stream.
- */
- Client.prototype.releaseStream = function(streamname)
- {
- s = Stream.get(streamname);
- s.play(false);
- }
- /*
- * application.readValidDomains
- * Function to read Allowed domain file
- * Parameters:
- * fileName:
- * name of the file in the application directory
- * which contains one valid domain name per line. This file can contain
- * comments followed by a '#' as the very first charector in that line.
- * a non-comment entry with a space is considered as an error case.
- *
- * returns
- * an array in which each entry contains a domain name
- * listed in the file.
- */
- application.readValidDomains = function( fileName , domainsType )
- {
- var domainFile = new File(fileName);
- var domainsArray = new Array();
- var index = 0;
- var lineCount = 0;
- var tempLine;
- domainFile.open("text", "read");
- // Read the file line-by-line and fill the domainsArray
- // with valid entries
- while (domainFile.isOpen && ! domainFile.eof() )
- {
- tempLine = domainFile.readln();
- lineCount++;
- if( !tempLine || tempLine.indexOf("#") == 0)
- {
- continue;
- }
- tempLinetempLine = tempLine.trim();
- if(tempLine.indexOf(" ")!=-1)
- {
- trace("undesired <space>, domain entry ignored. "+fileName+":"+(lineCount+1));
- }
- else
- {
- domainsArray[index] = tempLine.toLowerCase();
- index++;
- if(tempLine == "*")
- {
- switch (domainsType){
- case "HTMLDomains":
- trace ("Found wildcard (*) entry: disabling authentication for HTML file domains ") ;
- application.HTMLDomainsAuth = false;
- break;
- case "SWFDomains":
- trace ("Found wildcard (*) entry: disabling authentication for SWF file domains ") ;
- this.SWFDomainsAuth = false;
- break;
- default:
- // Do nothing
- break;
- }
- }
- }
- } // End while
- // Something is wrong! the domains file must be accessible.
- if( !domainFile.isOpen){
- trace("Error: could not open '"+fileName+"', rejecting all clients except localhost. ");
- }
- else
- {
- domainFile.close();
- }
- return domainsArray;
- }
- application.LoadXML = function(xmlFile){
- trace('LoadXML:' + xmlFile);
- isLoadVideo = -1;
- var _loc6 = new XML();
- trace('Load XML');
- _loc6.load(xmlFile);
- _loc6._parent = this;
- _loc6.ignoreWhite = true;
- _loc6.getBytesLoaded();
- _loc6.onLoad = function(e)
- {
- trace('Load');
- if (e)
- {
- trace(e);
- //var _loc5 = new XMLNode();
- //trace(_loc5);
- trace(this);
- _loc5 = this.childNodes;
- trace(_loc5);
- trace(_loc5[0].childNodes.length);
- Data_array2 = new Array();
- for (var _loc3 = 0; _loc3 < _loc5[0].childNodes.length; ++_loc3)
- {
- Data_array2[_loc3] = new Object();
- Data_array2[_loc3].flvname = _loc5[0].childNodes[_loc3].attributes.flvname
- Data_array2[_loc3].start = _loc5[0].childNodes[_loc3].attributes.start;
- }// end of for
- trace('isLoadVideo:xml');
- //trace(outarr(Data_array2));
- videoList = Data_array2;
- isLoadVideo = 1;
- }
- else
- {
- isLoadVideo = -2;
- trace("Load Error");
- }// end else if
- };
- }// End of the function
- outarr = function(arr, level)
- {
- var astring = '';
- if (level == undefined || isNaN(level))
- {
- level = 0;
- }
- for (a = 0; a < level; a++)
- {
- astring += ' ';
- }
- //trace('astring:' + astring);
- out = 'array(\r';
- for (i in arr)
- {
- if (typeof (arr[i]) == 'object')
- {
- out += astring + ' "' + i + '"=>' + outarr(arr[i], level + 1);
- }
- else
- {
- out += astring + ' "' + i + '"=>"' + arr[i] + '"\r';
- }
- }
- out += astring + ')\r';
- return out;
- }
- /**
- * String.prototype.trim:
- * Function to trim spaces in start an end of an input string.
- * returns:
- * a trimmed string without any leading & ending spaces.
- *
- */
- String.prototype.trim = function () {
- return this.replace(/^\s*/, "").replace(/\s*$/, "");
- }
热门文章推荐
- [FMS]adobe FMS配置实现(FMS/HLS/HDS)功能经验
- [HLS]HTTP Live Streaming流与TS流比较
- [FMS]adobe FMS(flash media server )服务器安装过程
- [FMS]fms5.0的hls(HTTP Live Streaming)技术实现跨平台点播与
- [FMS]FMS之HTTP Live Streaming (HLS)视频直播技术
- [hls]m3u8视频如何处理成加密?
- [海康]视频监控环境中配置组播模式
- [FMS]基于fms搭建在线hls跨平台直播环境(组图)
请稍候...