Beispiel #1
0
func (cu *CUClient) heartTask() {
	defer func() {
		if r := recover(); r != nil {
			cu.handleError(base.DTerror{"heartTask Panic"})
		}
	}()

	baseHD := new(base.BaseHeader)
	baseHD.CommandId = base.HEART_CMD
	msg := baseHD.Encode()

	var failCount int = 0

	for {

		if atomic.CompareAndSwapUint32(&cu.Valid, 0, 0) {
			break
		}
		cu.connSocket.Write(msg.Bytes())
		select {
		case <-cu.heartChn:
			failCount = 0
		case <-time.After(10 * time.Second):
			failCount++
		}
		if failCount >= 3 {
			cu.handleError(base.DTerror{"CU Heart"})
			break
		}
		time.Sleep(10 * time.Second)
	}
}
Beispiel #2
0
func (chn *Channel) handleWriteSocket(proto *base.Proto) {

	if atomic.CompareAndSwapUint32(&chn.valid, 0, 0) {
		return
	}
	cmdID := proto.RD.BaseHD.CommandId & 0x7f

	if cmdID == base.HEART_CMD {
		heartHdr := new(base.BaseHeader)
		heartHdr.CommandId = proto.RD.BaseHD.CommandId
		msg := heartHdr.Encode()
		_, err := chn.connSocket.Write(msg.Bytes())

		if err != nil {
			chn.handleError(base.DTerror{"Send Error"})
		}
		return
	}
	fmt.Println("handleWriteSocket ", cmdID)
	msg := proto.EncodeHdr()
	_, err := chn.connSocket.Write(msg.Bytes())
	if err != nil {
		chn.handleError(base.DTerror{"Send Error"})

	}
	_, dErr := chn.connSocket.Write(proto.BD.Data)
	if dErr != nil {
		chn.handleError(base.DTerror{"Send Error"})
	}
	base.PutProto(proto)

}
Beispiel #3
0
func (pu *PUDevice) handleWriteSocket(proto *base.Proto) {

	if atomic.CompareAndSwapUint32(&pu.Valid, 0, 0) {
		return
	}
	cmdID := proto.RD.BaseHD.CommandId & 0x7f

	if cmdID == base.HEART_CMD {
		heartHdr := new(base.BaseHeader)
		heartHdr.CommandId = proto.RD.BaseHD.CommandId
		msg := heartHdr.Encode()
		_, err := pu.connSocket.Write(msg.Bytes())

		if err != nil {
			pu.handleError(base.DTerror{"Send Error " + err.Error()})

		}
		return
	}
	msg := proto.EncodeHdr()
	_, err := pu.connSocket.Write(msg.Bytes())
	if err != nil {
		pu.handleError(base.DTerror{"Send Error " + err.Error()})
		return
	}
	_, dErr := pu.connSocket.Write(proto.BD.Data)
	if dErr != nil {
		pu.handleError(base.DTerror{"Send Error" + err.Error()})
		return
	}

}
Beispiel #4
0
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
		}

	}
}