Exemple #1
0
func InitHTTP() error {
	http.HandleFunc("/live", func(w http.ResponseWriter, r *http.Request) {
		glog.Info("http: get an request.", r.RequestURI, r.Method)
		if r.Method != "GET" {
			return
		}
		// get live source.
		// TODO: should map source's http request and source key.
		key := "/live/123" // for test.
		consumer, err := source.NewConsumer(key)
		if err != nil {
			glog.Info("<<<<<<<<<< can not get source >>>>>>>>>>>>>", err)
			return
		}
		defer consumer.Close()

		glog.Info("<<<<<<<<<<<<<<<<<<<<<<<<<got source>>>>>>>>>>>>>>>>>>>>>>>>>")
		// set flv live stream http head.
		// TODO: let browser not cache sources.
		w.Header().Add("Content-Type", "video/x-flv")
		if err := consumer.Live(w); err != nil {
			glog.Info("Live get an client error:", err)
		}
	})
	http.Handle("/", http.FileServer(http.Dir(".")))
	return nil
}
Exemple #2
0
func InitHTTPFlv(mux *http.ServeMux, app string, sources *source.SourceManage, cb callback.FlvCallback) {
	prefix := path.Join("/", app) + "/"
	mux.HandleFunc(prefix, func(w http.ResponseWriter, r *http.Request) {
		glog.Infof("http: get an request: %v", r.RequestURI)
		if r.Method != "GET" {
			return
		}
		r.ParseForm()
		// access check.
		if !cb.FlvAccessCheck(r.RemoteAddr, r.RequestURI, r.URL.Path, r.Form, r.Cookies()) {
			w.WriteHeader(http.StatusForbidden)
			return
		}
		// get path.
		_, file := path.Split(r.URL.Path)
		var key string
		key = file[:strings.Index(file, ".flv")]

		// get live source.
		consumer, err := source.NewConsumer(sources, key)
		if err != nil {
			glog.Info("can not get source", err)
			return
		}
		glog.Info("get source")
		defer consumer.Close()
		// set flv live stream http head.
		// TODO: let browser not cache sources.
		w.Header().Add("Content-Type", "video/x-flv")
		if err := consumer.Live(w); err != nil {
			glog.Info("Live get an client error:", err)
		}
	})
}