func (pu *PUDevice) readTask() { defer func() { if r := recover(); r != nil { mylog.GetErrorLogger().Println("readTask panic") return } }() for { proto := new(base.Proto) err := proto.ReadBinaryProto(pu.connSocket) if err != nil { pu.handleError(err) break } cmdID := proto.RD.BaseHD.CommandId & 0x7f if cmdID == base.HEART_CMD { mylog.GetErrorLogger().Println("Recv And Send Heart", proto.RD.BaseHD.CommandId) heartProto := new(base.Proto) heartProto.RD.BaseHD.CommandId = 0x80 | cmdID pu.rwChan <- heartProto } else { mylog.GetErrorLogger().Println(string(proto.BD.Data[4:])) if cmdID == base.OPEN_RESOURCE_CMD { go pu.openSource(proto.RD.HD.ClientIdent) } else if cmdID == base.CLOSE_RESOURCE_CMD { go pu.closeSource(proto.RD.HD.ClientIdent) } else if cmdID == base.REGISTER_RESOURCE { var reponseJson base.ResponseJson resErr := json.Unmarshal(proto.BD.Data[4:], &reponseJson) if resErr != nil { pu.handleError(resErr) return } if reponseJson.Error != base.OK { pu.handleError(base.DTerror{"Resoure Fail"}) fmt.Println(reponseJson.Error) return } stat.GetLocalStatistInst().RegisterRes() } } } }
func (cu *CUClient) readTask() { defer func() { if r := recover(); r != nil { cu.handleError(base.DTerror{}) } }() for { proto := new(base.Proto) err := proto.ReadBinaryProto(cu.connSocket) if err != nil { cu.handleError(err) return } cmdID := proto.RD.BaseHD.CommandId & 0x7f //fmt.Println("Recv CommandID ", cmdID) if cmdID == base.HEART_CMD { isRequest := proto.RD.BaseHD.CommandId & 0x80 if isRequest == 0 { heartHdr := new(base.BaseHeader) heartHdr.CommandId = proto.RD.BaseHD.CommandId | 0x80 msg := heartHdr.Encode() _, err := cu.connSocket.Write(msg.Bytes()) if err != nil { cu.handleError(base.DTerror{"Send Error " + err.Error()}) } //fmt.Println("Send Heart") } else { cu.heartChn <- true //fmt.Println("Recv Heart") } } else if cmdID == base.DATA_STREAM { // stat.GetLocalStatistInst().RecvData(uint64(proto.RD.HD.BodyLen)) var frameType uint16 = 0 var err error = nil err = binary.Read(bytes.NewBuffer(proto.BD.Data[10:12]), binary.LittleEndian, &frameType) if err != nil { fmt.Println(err) } // 由于我们公司的h264数据的sps中video_format没有指定yuv格式 // 所以在这里直接指定为2,即yuv420p if frameType == 1 { cu.frameCount++ } if cu.frameCount == 1 { //proto.BD.Data[32] = 0x5D proto.BD.Data[42] = 0x90 result := frameRateChange(proto.BD.Data[33:38]) for i := 0; i < 5; i++ { proto.BD.Data[33+i] = result[i] } backSPS := bytes.NewBuffer(proto.BD.Data[20:]) if cu.ffmpegChn != nil { cu.ffmpegChn.Write(backSPS.Bytes()) } else if cu.hlsHandler != nil { cu.hlsHandler.Write(backSPS.Bytes()) } else if cu.debugChn != nil { cu.debugChn.Write(backSPS.Bytes()) } continue } else if cu.frameCount == 3 { cu.frameCount = 0 } if cu.ffmpegChn != nil { cu.ffmpegChn.Write(proto.BD.Data[20:]) } else if cu.hlsHandler != nil { cu.hlsHandler.Write(proto.BD.Data[20:]) } else if cu.debugChn != nil { cu.debugChn.Write(proto.BD.Data[20:]) } } else if cmdID == base.OPEN_RESOURCE_CMD { var responseJson base.ResponseJson fmt.Println("Open Response :", string(proto.BD.Data[4:])) unerr := json.Unmarshal(proto.BD.Data[4:], &responseJson) if unerr != nil { cu.handleError(unerr) break } if !bytes.Equal([]byte("ok"), []byte(responseJson.Error)) { cu.handleError(unerr) break } //stat.GetLocalStatistInst().OpenRes() } else if cmdID == base.CLOSE_RESOURCE_CMD { var responseJson base.ResponseJson //stat.GetLocalStatistInst().CloseRes() fmt.Println("Close Response :", string(proto.BD.Data[4:])) unerr := json.Unmarshal(proto.BD.Data[4:], &responseJson) if unerr != nil { cu.handleError(unerr) break } } else if cmdID == base.LEAVE_NOTIFY { atomic.AddUint32(&cu.clientID, 1) // stat.GetLocalStatistInst().CloseRes() go cu.openSource(cu.clientID, cu.sn) break } } }