package common import ( "encoding/json" "fmt" "io/ioutil" "strconv" "basic.com/pubsub/protomsg.git" "basic.com/valib/deliver.git" "github.com/gogo/protobuf/proto" ) const mode = deliver.PushPull // MsgRS msg recv and snd type MsgRS struct { Msg protomsg.SdkMessage } // GetIpcAddress get ipc func GetIpcAddress(shm bool, id string) string { if shm { return id } return `ipc:///tmp/` + id + `.ipc` } // SubConfig sub type SubConfig struct { SoFile string `json:"so_file_path"` Env string `json:"runtime"` Param map[string]string `json:"param"` } // SdkConfig sdk type SdkConfig struct { SoFile string `json:"so_file_path"` Env string `json:"runtime"` Param map[string]string `json:"param"` Sub *SubConfig `json:"sub"` } // ReadConfig conf func ReadConfig(file string) (SdkConfig, error) { data, err := ioutil.ReadFile(file) if err != nil { return SdkConfig{}, fmt.Errorf("READ SDK CONFIG FILE %s ERROR", file) } //读取的数据为json格式,需要进行解码 var v SdkConfig err = json.Unmarshal(data, &v) return v, err } // Atoi atoi func Atoi(s string) int { i, _ := strconv.Atoi(s) return i } // EjectResult eject func EjectResult(res []byte, msg MsgRS, out chan<- MsgRS) { if res == nil { out <- msg return } index := int(msg.Msg.Tasklab.Index) if index >= len(msg.Msg.Tasklab.Sdkinfos) { return } msg.Msg.Tasklab.Sdkinfos[index].Sdkdata = res out <- msg } ///////////////////////////////////////////////////////////// // ValidRemoteMessage valid or not func ValidRemoteMessage(msg MsgRS, fnName string, fn func(...interface{})) bool { if msg.Msg.Tasklab == nil { fn(fnName, " recieve msg nil") return false } sdkLen := len(msg.Msg.Tasklab.Sdkinfos) if sdkLen == 0 { fn(fnName, " has no sdk info") return false } curIndex := int(msg.Msg.Tasklab.Index) if curIndex < 0 || curIndex >= sdkLen { fn(fnName, " tasklab index ", curIndex, " error") return false } if msg.Msg.Tasklab.Sdkinfos[curIndex].Sdktype != fnName { fn(fnName, " is different from ", msg.Msg.Tasklab.Sdkinfos[curIndex].Sdktype) return false } return true } // UnpackImage unpack func UnpackImage(msg MsgRS, fnName string, fn func(...interface{})) *protomsg.Image { // 反序列化数据得到sdk入参 i := &protomsg.Image{} err := proto.Unmarshal(msg.Msg.Data, i) if err != nil { fn(fnName, " protobuf decode CameraImage error: ", err.Error()) return nil } if i.Data == nil { fn(fnName, " protomsg.Image data null") return nil } return i }