| | |
| | | |
| | | import ( |
| | | "context" |
| | | "decoder/valib/goffmpeg" |
| | | "decoder/valib/ipc" |
| | | srv "decoder/work/service" |
| | | "encoding/json" |
| | |
| | | proc string |
| | | |
| | | testIt bool |
| | | |
| | | asServer bool |
| | | ) |
| | | |
| | | func init() { |
| | |
| | | flag.StringVar(&ipcURL, "ipc", "ipc:///tmp/pic.ipc", "ipc label") |
| | | |
| | | flag.BoolVar(&testIt, "test", false, "use test") |
| | | |
| | | flag.BoolVar(&asServer, "server", false, "run ipc as server") |
| | | } |
| | | |
| | | // CameraInfo camera info |
| | |
| | | } |
| | | |
| | | var ( |
| | | mapCI = make(map[string]CameraInfo) |
| | | port = 7001 |
| | | mapCameraInfo = make(map[string]CameraInfo) |
| | | ) |
| | | |
| | | func recvFromIPC(ctx context.Context, url string, ch chan<- CameraInfo) { |
| | | func recvCameraInfoFromIPC(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 { |
| | | |
| | | if _, ok := mapCameraInfo[c.ID]; ok { |
| | | continue |
| | | } |
| | | |
| | | ch <- c |
| | | fmt.Printf("recv camere info %+v\n", c) |
| | | |
| | | msgIpc := MsgIPC{"new decoder", 0} |
| | | if b, err := json.Marshal(msgIpc); err == nil { |
| | | ipc.Send(b) |
| | | } |
| | | } 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() |
| | | } |
| | | goffmpeg.InitFFmpeg() |
| | | |
| | | ctx, cancel := context.WithCancel(context.Background()) |
| | | |
| | | ch := make(chan CameraInfo) |
| | | go recvFromIPC(ctx, "tcp://192.168.1.124:7000", ch) |
| | | if testIt { |
| | | test(ctx) |
| | | } |
| | | |
| | | // demo.SendByIPC("rtsp://admin:a1234567@192.168.1.188:554/h264/ch1/main/av_stream", "cid0", |
| | | // "tcp://192.168.1.140:7000", false) |
| | | ch := make(chan CameraInfo) |
| | | |
| | | 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 { |
| | | 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) |
| | | if _, ok := mapCameraInfo[c.ID]; !ok { |
| | | mapCameraInfo[c.ID] = c |
| | | ipcAddr := tcp + 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 runSender(id, url, i) |
| | | addr := strings.TrimSpace(ipcAddr) |
| | | 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) |
| | | } |