refine srs player to play hls.

pull/609/head
winlin 9 years ago
parent 46a31f4884
commit b2bd63e833

@ -0,0 +1,30 @@
package main
import (
"fmt"
"os"
"io"
"net/http"
)
func main() {
fmt.Println("FileWriter web server, post to /api/v1/flv to temp.flv")
http.HandleFunc("/api/v1/flv", func(w http.ResponseWriter, r *http.Request){
defer r.Body.Close()
var f *os.File
var err error
if f,err = os.OpenFile("temp.flv", os.O_RDWR|os.O_APPEND|os.O_CREATE, os.ModePerm); err != nil {
return
}
var written int64
written,err = io.Copy(f, r.Body)
fmt.Println(fmt.Sprintf("write to file temp.flv, %v bytes", written))
if err != nil {
return
}
})
_ = http.ListenAndServe(fmt.Sprintf(":8088"), nil)
}

@ -564,7 +564,7 @@
$("#main_modal").on("hide", function(){ $("#main_modal").on("hide", function(){
if (srs_player) { if (srs_player) {
// report the log to backend. // report the log to backend.
console.log(srs_player.dump_log()); //console.log(srs_player.dump_log());
srs_player.stop(); srs_player.stop();
srs_player = null; srs_player = null;

@ -0,0 +1,11 @@
package
{
public class Consts
{
// refresh every ts_fragment_seconds*M3u8RefreshRatio
public static var M3u8RefreshRatio:Number = 0.5;
// parse ts every this ms.
public static var TsParseAsyncInterval:Number = 80;
}
}

@ -155,7 +155,7 @@ package
nb_parsed += each_parse; nb_parsed += each_parse;
if (nb_parsed < ts_packets) { if (nb_parsed < ts_packets) {
flash.utils.setTimeout(aysncParse, 80); flash.utils.setTimeout(aysncParse, Consts.TsParseAsyncInterval);
return; return;
} }

@ -16,7 +16,10 @@ package
import flash.net.NetConnection; import flash.net.NetConnection;
import flash.net.NetStream; import flash.net.NetStream;
import flash.net.NetStreamAppendBytesAction; import flash.net.NetStreamAppendBytesAction;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest; import flash.net.URLRequest;
import flash.net.URLRequestMethod;
import flash.net.URLStream; import flash.net.URLStream;
import flash.net.URLVariables; import flash.net.URLVariables;
import flash.system.Security; import flash.system.Security;
@ -46,12 +49,8 @@ package
private var hls:Hls = null; // parse m3u8 and ts private var hls:Hls = null; // parse m3u8 and ts
// callback for hls. // callback for hls.
private var shok:Boolean = false;
public var flvHeader:ByteArray = null; public var flvHeader:ByteArray = null;
public function onSequenceHeader():void { public function onSequenceHeader():void {
if (shok) {
return;
}
if (!media_stream) { if (!media_stream) {
setTimeout(onSequenceHeader, 1000); setTimeout(onSequenceHeader, 1000);
return; return;
@ -61,16 +60,30 @@ package
s.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN); s.appendBytesAction(NetStreamAppendBytesAction.RESET_BEGIN);
s.appendBytes(flvHeader); s.appendBytes(flvHeader);
log("FLV: sps/pps " + flvHeader.length + " bytes"); log("FLV: sps/pps " + flvHeader.length + " bytes");
shok = true;
writeFlv(flvHeader);
} }
public function onFlvBody(flv:ByteArray):void { public function onFlvBody(uri:String, flv:ByteArray):void {
if (!media_stream) { if (!media_stream) {
return; return;
} }
var s:NetStream = media_stream; var s:NetStream = media_stream;
s.appendBytes(flv); s.appendBytes(flv);
log("FLV: AV " + flv.length + " bytes"); log("FLV: ts " + uri + " parsed to flv " + flv.length + " bytes");
writeFlv(flv);
}
private function writeFlv(data:ByteArray):void {
return;
var r:URLRequest = new URLRequest("http://192.168.1.117:8088/api/v1/flv");
r.method = URLRequestMethod.POST;
r.data = data;
var pf:URLLoader = new URLLoader();
pf.dataFormat = URLLoaderDataFormat.BINARY;
pf.load(r);
} }
public function M3u8Player(o:srs_player) { public function M3u8Player(o:srs_player) {
@ -178,7 +191,7 @@ package
if (parsed_ts_seq_no >= hls.seq_no + hls.tsCount) { if (parsed_ts_seq_no >= hls.seq_no + hls.tsCount) {
var to:Number = 1000; var to:Number = 1000;
if (hls.tsCount > 0) { if (hls.tsCount > 0) {
to = hls.duration * 1000 / hls.tsCount * 0.5; to = hls.duration * 1000 / hls.tsCount * Consts.M3u8RefreshRatio;
} }
setTimeout(refresh_m3u8, to); setTimeout(refresh_m3u8, to);
log("m3u8 not changed, retry after " + to.toFixed(2) + "ms"); log("m3u8 not changed, retry after " + to.toFixed(2) + "ms");
@ -208,8 +221,6 @@ package
download(uri, function(stream:ByteArray):void{ download(uri, function(stream:ByteArray):void{
log("got ts seqno=" + parsed_ts_seq_no + ", " + stream.length + " bytes"); log("got ts seqno=" + parsed_ts_seq_no + ", " + stream.length + " bytes");
// reset and start to parse this ts.
hls.reset();
var flv:FlvPiece = new FlvPiece(parsed_ts_seq_no); var flv:FlvPiece = new FlvPiece(parsed_ts_seq_no);
var body:ByteArray = new ByteArray(); var body:ByteArray = new ByteArray();
@ -217,7 +228,7 @@ package
hls.parseBodyAsync(flv, stream, body, function():void{ hls.parseBodyAsync(flv, stream, body, function():void{
body.position = 0; body.position = 0;
//log("ts parsed, seqno=" + parsed_ts_seq_no + ", flv=" + body.length + "B"); //log("ts parsed, seqno=" + parsed_ts_seq_no + ", flv=" + body.length + "B");
onFlvBody(body); onFlvBody(uri, body);
parsed_ts_seq_no++; parsed_ts_seq_no++;
setTimeout(refresh_ts, 0); setTimeout(refresh_ts, 0);

Loading…
Cancel
Save