Browse Source

fix: remove startHTTPOpera

pull/6/head
halwu(吴浩麟) 8 years ago
parent
commit
3e1b4b32f1
  1. 6
      README.md
  2. 106
      main.go
  3. 113
      protocol/httpopera/http_opera.go

6
README.md

@ -8,8 +8,8 @@ @@ -8,8 +8,8 @@
## Use
1. run `git clone `
2. run `go run main.go` to start livego server
3. push `RTMP` stream to `rtmp://localhost/live/movie`, eg use `ffmpeg -re -i demo.flv -c copy -f flv rtmp://localhost/live/movie`
3. push `RTMP` stream to `rtmp://localhost:1935/live/movie`, eg use `ffmpeg -re -i demo.flv -c copy -f flv rtmp://localhost:1935/live/movie`
4. play stream use [VLC](http://www.videolan.org/vlc/index.html) or other players
- play `RTMP` from `rtmp://localhost/live/movie`
- play `RTMP` from `rtmp://localhost:1935/live/movie`
- play `FLV` from `http://127.0.0.1:8081/live/movie.flv`
- play `HLS` from `http://127.0.0.1:8080/live/movie.m3u8`
- play `HLS` from `http://127.0.0.1:8082/live/movie.m3u8`

106
main.go

@ -3,71 +3,25 @@ package main @@ -3,71 +3,25 @@ package main
import (
"flag"
"net"
"os"
"os/signal"
"syscall"
"time"
"log"
"github.com/gwuhaolin/livego/protocol/rtmp"
"github.com/gwuhaolin/livego/protocol/hls"
"github.com/gwuhaolin/livego/protocol/httpflv"
"github.com/gwuhaolin/livego/protocol/httpopera"
"path/filepath"
"strings"
"io/ioutil"
"strconv"
"log"
)
var (
rtmpAddr = flag.String("rtmpAddr", ":1935", "The rtmp server address to bind.")
flvAddr = flag.String("flvAddr", ":8081", "the http-flv server address to bind.")
hlsAddr = flag.String("hlsAddr", ":8080", "the hls server address to bind.")
operaAddr = flag.String("operaAddr", "", "the http operation or config address to bind: 8082.")
CurDir string // save pid
rtmpAddr = flag.String("rtmpAddr", ":1935", "The rtmp server address to bind.")
operaAddr = flag.String("operaAddr", ":8080", "the http operation or config address to bind.")
flvAddr = flag.String("flvAddr", ":8081", "the http-flv server address to bind.")
hlsAddr = flag.String("hlsAddr", ":8082", "the hls server address to bind.")
)
func getParentDirectory(dirctory string) string {
return substr(dirctory, 0, strings.LastIndex(dirctory, "/"))
}
func getCurrentDirectory() string {
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
if err != nil {
log.Fatal(err)
}
return strings.Replace(dir, "\\", "/", -1)
}
func substr(s string, pos, length int) string {
runes := []rune(s)
l := pos + length
if l > len(runes) {
l = len(runes)
}
return string(runes[pos:l])
}
func SavePid() error {
pidFilename := CurDir + "/pid/" + filepath.Base(os.Args[0]) + ".pid"
pid := os.Getpid()
return ioutil.WriteFile(pidFilename, []byte(strconv.Itoa(pid)), 0755)
}
func init() {
CurDir = getParentDirectory(getCurrentDirectory())
flag.Usage = func() {
flag.PrintDefaults()
}
flag.Parse()
}
func catchSignal() {
sig := make(chan os.Signal)
signal.Notify(sig, syscall.SIGSTOP, syscall.SIGTERM)
<-sig
log.Println("recieved signal!")
}
func startHls() *hls.Server {
hlsListen, err := net.Listen("tcp", *hlsAddr)
if err != nil {
@ -78,29 +32,29 @@ func startHls() *hls.Server { @@ -78,29 +32,29 @@ func startHls() *hls.Server {
go func() {
defer func() {
if r := recover(); r != nil {
log.Println("hls server panic: ", r)
log.Println("HLS server panic: ", r)
}
}()
log.Println("HLS Listen On", *hlsAddr)
hlsServer.Serve(hlsListen)
}()
return hlsServer
}
func startRtmp(stream *rtmp.RtmpStream, hlsServer *hls.Server) {
rtmplisten, err := net.Listen("tcp", *rtmpAddr)
rtmpListen, err := net.Listen("tcp", *rtmpAddr)
if err != nil {
log.Fatal(err)
}
rtmpServer := rtmp.NewRtmpServer(stream, hlsServer)
go func() {
defer func() {
if r := recover(); r != nil {
log.Println("hls server panic: ", r)
}
}()
rtmpServer.Serve(rtmplisten)
defer func() {
if r := recover(); r != nil {
log.Println("RTMP server panic: ", r)
}
}()
log.Println("RTMP Listen On", *rtmpAddr)
rtmpServer.Serve(rtmpListen)
}
func startHTTPFlv(stream *rtmp.RtmpStream) {
@ -113,9 +67,10 @@ func startHTTPFlv(stream *rtmp.RtmpStream) { @@ -113,9 +67,10 @@ func startHTTPFlv(stream *rtmp.RtmpStream) {
go func() {
defer func() {
if r := recover(); r != nil {
log.Println("hls server panic: ", r)
log.Println("HTTP-FLV server panic: ", r)
}
}()
log.Println("HTTP-FLV Listen On", *flvAddr)
hdlServer.Serve(flvListen)
}()
}
@ -130,43 +85,26 @@ func startHTTPOpera(stream *rtmp.RtmpStream) { @@ -130,43 +85,26 @@ func startHTTPOpera(stream *rtmp.RtmpStream) {
go func() {
defer func() {
if r := recover(); r != nil {
log.Println("hls server panic: ", r)
log.Println("HTTP-Operation server panic: ", r)
}
}()
log.Println("HTTP-Operation Listen On", *operaAddr)
opServer.Serve(opListen)
}()
}
}
func startLog() {
log.Println("RTMP Listen On", *rtmpAddr)
log.Println("HLS Listen On", *hlsAddr)
log.Println("HTTP-FLV Listen On", *flvAddr)
if *operaAddr != "" {
log.Println("HTTP-Operation Listen On", *operaAddr)
}
SavePid()
}
func main() {
defer func() {
if r := recover(); r != nil {
log.Println("main panic: ", r)
log.Println("livego panic: ", r)
time.Sleep(1 * time.Second)
}
}()
stream := rtmp.NewRtmpStream()
// hls
h := startHls()
// rtmp
startRtmp(stream, h)
// http-flv
hlsServer := startHls()
startHTTPFlv(stream)
// http-opera
startHTTPOpera(stream)
// my log
startLog()
// block
catchSignal()
//startHTTPOpera(stream)
startRtmp(stream, hlsServer)
}

113
protocol/httpopera/http_opera.go

@ -5,11 +5,7 @@ import ( @@ -5,11 +5,7 @@ import (
"io/ioutil"
"net"
"net/http"
"net/url"
"strings"
"github.com/golang/glog"
"github.com/gwuhaolin/livego/utils/uid"
"github.com/gwuhaolin/livego/av"
"log"
"github.com/gwuhaolin/livego/protocol/rtmp"
@ -55,9 +51,6 @@ func (s *Server) Serve(l net.Listener) error { @@ -55,9 +51,6 @@ func (s *Server) Serve(l net.Listener) error {
mux.HandleFunc("/rtmp/operation", func(w http.ResponseWriter, r *http.Request) {
s.handleOpera(w, r)
})
// mux.HandleFunc("/rtmp/operation/change", func(w http.ResponseWriter, r *http.Request) {
// s.handleOperaChange(w, r)
// })
http.Serve(l, mux)
return nil
}
@ -93,7 +86,7 @@ func (s *Server) handleOpera(w http.ResponseWriter, r *http.Request) { @@ -93,7 +86,7 @@ func (s *Server) handleOpera(w http.ResponseWriter, r *http.Request) {
return
}
r.Body.Close()
glog.Infof("post body: %s\n", result)
log.Println("post body", result)
var op Operation
err = json.Unmarshal(result, &op)
@ -120,113 +113,9 @@ func (s *Server) handleOpera(w http.ResponseWriter, r *http.Request) { @@ -120,113 +113,9 @@ func (s *Server) handleOpera(w http.ResponseWriter, r *http.Request) {
func (s *Server) Push(uri string, stop bool) error {
rtmpClient := rtmp.NewRtmpClient(s.handler, nil)
return rtmpClient.Dial(uri, av.PUBLISH)
// return nil
}
func (s *Server) Pull(uri string, stop bool) error {
rtmpClient := rtmp.NewRtmpClient(s.handler, nil)
return rtmpClient.Dial(uri, av.PLAY)
// return nil
}
// TODO:
// handleOperaChange, 拉流和推流的http api,支持自定义路径
// @Path: /rtmp/operation/change
// @Method: POST
// @Param: json
// method string, "push" or "pull"
// url string
// stop bool
// @Example,
// curl -v -H "Content-Type: application/json" -X POST --data \
// '{"method":"pull","url":"rtmp://127.0.0.1:1935/live/test"}' \
// http://localhost:8087/rtmp/operation
// func (s *Server) handleOperaChange(w http.ResponseWriter, r *http.Request) {
// rep := &Response{
// w: w,
// }
// if r.Method != "POST" {
// rep.Status = 14000
// rep.Message = "bad request method"
// rep.SendJson()
// return
// } else {
// result, err := ioutil.ReadAll(r.Body)
// if err != nil {
// rep.Status = 15000
// rep.Message = "read request body error"
// rep.SendJson()
// return
// }
// r.Body.Close()
// glog.Infof("post body: %s\n", result)
// var op OperationChange
// err = json.Unmarshal(result, &op)
// if err != nil {
// rep.Status = 12000
// rep.Message = "parse json body failed"
// rep.SendJson()
// return
// }
// switch op.Method {
// case "push":
// s.PushChange(op.SourceURL, op.TargetURL, op.Stop)
// case "pull":
// s.PullChange(op.SourceURL, op.TargetURL, op.Stop)
// }
// rep.Status = 10000
// rep.Message = op.Method + " from" + op.SourceURL + "to " + op.TargetURL + " success"
// rep.SendJson()
// }
// }
// pushChange suri to turi
// func (s *Server) PushChange(suri, turi string, stop bool) error {
// if !stop {
// sinfo := parseURL(suri)
// tinfo := parseURL(turi)
// rtmpClient := rtmp.NewRtmpClient(s.handler, nil)
// return rtmpClient.Dial(turi, av.PUBLISH)
// } else {
// sinfo := parseURL(suri)
// tinfo := parseURL(turi)
// s.delStream(sinfo.Key, true)
// return nil
// }
// return nil
// }
// pullChange
// func (s *Server) PullChange(suri, turi string, stop bool) error {
// if !stop {
// rtmpStreams, ok := s.handler.(*rtmp.RtmpStream)
// if ok {
// streams := rtmpStreams.GetStreams()
// rtmpClient := rtmp.NewRtmpClient(s.handler, nil)
// return rtmpClient.Dial(turi, av.PLAY)
// }
// } else {
// info := parseURL(suri)
// s.delStream(info.Key, false)
// return nil
// }
// return nil
// }
func parseURL(URL string) (ret av.Info) {
ret.UID = uid.NEWID()
ret.URL = URL
_url, err := url.Parse(URL)
if err != nil {
log.Println(err)
}
ret.Key = strings.TrimLeft(_url.Path, "/")
ret.Inter = true
return
}

Loading…
Cancel
Save