视频分析2.0 多进程拆分仓库
zhangmeng
2019-05-06 e4f567f656cc8b5e1e8f96722ca78d0a09333eb8
decoder/main.go
@@ -1,13 +1,14 @@
package main
import (
   "bytes"
   "context"
   "decoder/demo"
   "decoder/valib/ipc"
   "encoding/gob"
   "encoding/json"
   "flag"
   "fmt"
   "strconv"
   "strings"
   // "videoServer/demo"
)
@@ -17,6 +18,8 @@
   ipcURL string
   proc   string
   testIt bool
)
func init() {
@@ -24,6 +27,82 @@
   flag.StringVar(&picFolder, "f", ".", "test pic folder")
   flag.StringVar(&ipcURL, "ipc", "ipc:///tmp/pic.ipc", "ipc label")
   flag.BoolVar(&testIt, "test", false, "use test")
}
// CameraInfo camera info
type CameraInfo struct {
   ID  string `json:"Cameraid"`
   URL string `json:"Rtsp"`
}
// MsgIPC msg for ipc
type MsgIPC struct {
   CMD  string `json:"Command"`
   Port int    `jsong:"PortIpc"`
}
var (
   mapCI = make(map[string]CameraInfo)
   port  = 7001
)
func recvFromIPC(ctx context.Context, url string, ch chan<- CameraInfo) {
   ipc := ipc.NewClient(ctx, url)
   for {
      msg := ipc.Recv()
      if msg != nil {
         fmt.Println(string(msg))
         var c CameraInfo
         if err := json.Unmarshal(msg, &c); err == nil {
            ch <- c
            fmt.Printf("recv camere info %+v\n", c)
         } else {
            fmt.Println(err)
         }
         msgIpc := MsgIPC{"new decoder", port}
         if b, err := json.Marshal(msgIpc); err == nil {
            ipc.Send(b)
         }
      }
   }
}
func main() {
   flag.Parse()
   if testIt {
      test()
   }
   ctx, cancel := context.WithCancel(context.Background())
   ch := make(chan CameraInfo)
   go recvFromIPC(ctx, "tcp://192.168.1.124:7000", ch)
   // demo.SendByIPC("rtsp://admin:a1234567@192.168.1.188:554/h264/ch1/main/av_stream", "cid0",
   //    "tcp://192.168.1.140:7000", false)
   for {
      select {
      case <-ctx.Done():
         return
      case c := <-ch:
         if _, ok := mapCI[c.ID]; !ok {
            mapCI[c.ID] = c
            ipc := "tcp://192.168.1.124:" + strconv.Itoa(port)
            port++
            fmt.Printf("create ipc %s for decode : %s, on camera id %s\n", ipc, c.URL, c.ID)
            url := strings.TrimSpace(c.URL)
            id := strings.TrimSpace(c.ID)
            i := strings.TrimSpace(ipc)
            go demo.SendByIPC(url, id, i, false)
         }
      }
   }
   cancel()
}
func test() {
@@ -31,41 +110,5 @@
   fmt.Println(picFolder)
   demo.SendByIPC(streamURL, "camera1", ipcURL)
}
type cameraInfo struct {
   cameraID string
   videoURL string
}
func recvFromIPC(ctx context.Context, url string) (cameraID, rtspURL string) {
   ipc := ipc.NewClient(ctx, url)
   for {
      msg := ipc.Recv()
      var buf bytes.Buffer
      buf.Write(msg)
      dec := gob.NewDecoder(&buf)
      var i cameraInfo
      if err := dec.Decode(&i); err != nil {
         fmt.Println("gob decode CameraImage error", err)
         continue
      }
      return i.cameraID, i.videoURL
   }
}
func main() {
   flag.Parse()
   ctx, cancel := context.WithCancel(context.Background())
   recvFromIPC(ctx, "tcp://192.168.1.156:7000")
   cancel()
   // test()
   demo.SendByIPC(streamURL, "camera1", ipcURL, false)
}