视频分析2.0 多进程拆分仓库
zhangmeng
2019-05-08 19445d14e73f4bb96e218a65e126ae526f89537d
decoder/main.go
@@ -2,6 +2,7 @@
import (
   "context"
   "decoder/valib/goffmpeg"
   "decoder/valib/ipc"
   srv "decoder/work/service"
   "encoding/json"
@@ -34,7 +35,7 @@
   flag.BoolVar(&asServer, "server", false, "run ipc as server")
}
// cameraInfo camera info
// CameraInfo camera info
type CameraInfo struct {
   ID  string `json:"Cameraid"`
   URL string `json:"Rtsp"`
@@ -48,9 +49,6 @@
var (
   mapCameraInfo = make(map[string]CameraInfo)
   tcp  = `tcp://192.168.1.124:`
   port = 7001
)
func recvCameraInfoFromIPC(ctx context.Context, url string, ch chan<- CameraInfo) {
@@ -69,7 +67,7 @@
            ch <- c
            msgIpc := MsgIPC{"new decoder", port}
            msgIpc := MsgIPC{"new decoder", 0}
            if b, err := json.Marshal(msgIpc); err == nil {
               ipc.Send(b)
            }
@@ -84,15 +82,28 @@
func main() {
   flag.Parse()
   if testIt {
      test()
   }
   goffmpeg.InitFFmpeg()
   ctx, cancel := context.WithCancel(context.Background())
   if testIt {
      test(ctx)
   }
   ch := make(chan CameraInfo)
   // tcpURL := tcp + strconv.Itoa(port)
   // port++
   tcp := ``
   port := 7001
   if strings.Index(ipcURL, "tcp://") == 0 {
      i := strings.LastIndex(ipcURL, ":")
      tcp = ipcURL[0 : i+1]
      strPort := ipcURL[i+1:]
      port, _ = strconv.Atoi(strPort)
      port++
   }
   go recvCameraInfoFromIPC(ctx, ipcURL, ch)
   for {
@@ -108,25 +119,27 @@
            url := strings.TrimSpace(c.URL)
            id := strings.TrimSpace(c.ID)
            addr := strings.TrimSpace(ipcAddr)
            go runSender(id, url, addr)
            go runSender(ctx, id, url, addr)
         }
      }
   }
   goffmpeg.FreeFFmpeg()
   cancel()
}
func runSender(cameraID, rtspURL, ipcLabel string) {
   d := srv.NewSender(cameraID, rtspURL, ipcLabel)
func runSender(ctx context.Context, cameraID, rtspURL, ipcLabel string) {
   d := srv.NewSender(ctx, cameraID, rtspURL, ipcLabel)
   if asServer {
      d.RunAsServer()
   }
   d.RunAsClient()
}
func test() {
func test(ctx context.Context) {
   fmt.Println("start test")
   fmt.Println(picFolder)
   runSender("cameraid", streamURL, ipcURL)
   runSender(ctx, "cameraid", streamURL, ipcURL)
}