Example #1
0
func (cu *CUClient) closeSource(clientID uint32, id string) {

	var err error
	proto := new(base.Proto)
	proto.RD.BaseHD.CommandId = base.CLOSE_RESOURCE_CMD
	proto.RD.HD.ClientIdent = clientID
	proto.RD.HD.ContextType = base.CONTEXT_JSON
	proto.RD.HD.TransferType = base.TRANSFER_RAW

	resourceJson := base.CloseResParamJson{id}

	requestJson := base.RequestJson{"", "ns", "close", resourceJson}
	b, err := json.Marshal(requestJson)
	if err != nil {
		cu.handleError(err)
		return
	}

	proto.EncodeBody(b)
	result := proto.EncodeHdr()
	n, err := cu.connSocket.Write(result.Bytes())
	if err != nil {
		cu.handleError(err)
		return
	}
	n = n

	_, bErr := cu.connSocket.Write(proto.BD.Data)
	if err != nil {
		cu.handleError(bErr)
	}
	return
}
Example #2
0
func (pu *PUDevice) closeSource(clientID uint32) error {
	defer func() {
		if r := recover(); r != nil {
			pu.handleError(base.DTerror{"closeSource Panic"})
		}
	}()
	atomic.CompareAndSwapUint32(&pu.SendFlag, 1, 0)

	proto := new(base.Proto)
	proto.RD.BaseHD.CommandId = 0x80 | base.CLOSE_RESOURCE_CMD
	proto.RD.HD.ClientIdent = clientID
	proto.RD.HD.TransferType = base.TRANSFER_RAW
	proto.RD.HD.ContextType = base.CONTEXT_JSON
	responseJson := base.ResponseJson{"ns", "close", "", "ok"}
	b, err := json.Marshal(responseJson)
	if err != nil {
		pu.handleError(err)
		return err
	}

	proto.EncodeBody(b)
	pu.rwChan <- proto

	stat.GetLocalStatistInst().CloseRes()
	return err
}
Example #3
0
func (cu *CUClient) openSource(clientID uint32, id string) error {

	proto := new(base.Proto)
	proto.RD.BaseHD.CommandId = base.OPEN_RESOURCE_CMD
	proto.RD.HD.ClientIdent = clientID
	proto.RD.HD.ContextType = base.CONTEXT_JSON
	proto.RD.HD.TransferType = base.TRANSFER_RAW

	resourceJson := base.OpenResParamJson{id, 10}

	requestJson := base.RequestJson{cu.token, "ns", "open", resourceJson}
	b, err := json.Marshal(requestJson)
	if err != nil {
		cu.handleError(err)
		return err
	}

	proto.EncodeBody(b)
	result := proto.EncodeHdr()
	n, err := cu.connSocket.Write(result.Bytes())
	if err != nil {
		cu.handleError(err)
		return err
	}
	n = n

	_, bErr := cu.connSocket.Write(proto.BD.Data)
	if err != nil {
		cu.handleError(bErr)
		return err
	}
	go cu.timerClear()
	return err
}
Example #4
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)

}
Example #5
0
func (pu *PUDevice) openSource(clientID uint32) error {

	proto := new(base.Proto)
	proto.RD.BaseHD.CommandId = 0x80 | base.OPEN_RESOURCE_CMD
	proto.RD.HD.ClientIdent = clientID
	proto.RD.HD.TransferType = base.TRANSFER_RAW
	proto.RD.HD.ContextType = base.CONTEXT_JSON

	//resourceJson := command.ResourceDes{id}

	//res, e := json.Marshal(resourceJson)
	//if e != nil {
	//	return e
	//}
	atomic.CompareAndSwapUint32(&pu.SendFlag, 0, 1)

	responseJson := base.ResponseJson{"ns", "open", "", "ok"}
	b, err := json.Marshal(responseJson)
	if err != nil {
		pu.handleError(err)
		return err
	}

	proto.EncodeBody(b)

	pu.rwChan <- proto

	go pu.handleSource(clientID)
	stat.GetLocalStatistInst().OpenRes()
	return err
}
Example #6
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
	}

}
Example #7
0
func (chn *Channel) handleOtherCMD(proto *base.Proto) {

	cmdID := proto.RD.BaseHD.CommandId
	mylog.GetErrorLogger().Println("handleOtherCMD", cmdID)
	error := base.NOSUPPORT501
	responseJson := base.ResponseJson{"", "", "", error}
	b, err := json.Marshal(responseJson)
	if err != nil {
		mylog.GetErrorLogger().Println("handleOtherCMD : ", err)
	}

	proto.EncodeBody(b)
	chn.Send(proto)
}
Example #8
0
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()
			}
		}

	}
}
Example #9
0
func (pu *PUDevice) handleSource(clientID uint32) {

	defer func() {
		if r := recover(); r != nil {
			pu.handleError(base.DTerror{"handleSource Panic"})
		}
	}()
	file := new(record.RecordFile)
	resourceFile := new(bytes.Buffer)
	resourceFile.WriteString(oneEnvParam.ResourceDir)
	resourceFile.WriteString("\\")
	resourceFile.WriteString(oneEnvParam.ResourceFile)
	fmt.Println("Open File ", resourceFile)
	if b := file.Open("C:\\1.hmv"); !b {
		return
	}
	defer file.Close()
	file.GetHeader()
	for {
		if !atomic.CompareAndSwapUint32(&pu.SendFlag, 1, 1) {
			break
		}
		if atomic.CompareAndSwapUint32(&pu.Valid, 0, 0) {
			break
		}

		proto := new(base.Proto)
		proto.RD.BaseHD.CommandId = 0x80 | base.DATA_STREAM
		proto.RD.HD.TransferType = base.TRANSFER_RAW
		proto.RD.HD.ContextType = base.CONTEXT_VIDEO
		proto.RD.HD.ClientIdent = clientID

		b := file.GetNextFrame()
		if b == nil {
			file.Seek()
			continue
		}
		proto.EncodeBody(b)
		pu.rwChan <- proto

		stat.GetLocalStatistInst().SendData(uint64(len(proto.BD.Data)))

		time.Sleep(100 * time.Millisecond)

	}
}
Example #10
0
func (pu *PUDevice) Register() {
	// 注册资源
	proto := new(base.Proto)
	proto.RD.BaseHD.CommandId = base.REGISTER_RESOURCE
	proto.RD.HD.TransferType = base.TRANSFER_RAW
	proto.RD.HD.ContextType = base.CONTEXT_JSON

	regsterJson := base.RegisterResParamJson{pu.SN, 1, false}
	requestJson := base.RequestJson{"", "ns", "register", regsterJson}

	b, err := json.Marshal(requestJson)
	if err != nil {
		pu.handleError(err)
		return
	}

	proto.EncodeBody(b)
	pu.rwChan <- proto

}
Example #11
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
		}

	}
}
Example #12
0
func (chn *Channel) handleReqOpenRes(clientID uint32, data []byte) {

	defer func() {
		if r := recover(); r != nil {
			chn.handleError(base.DTerror{"handleReqOpenResr Panic"})
		}
	}()

	var responseErr string = ""
	var resourcer *Resourcer = nil
	var timeOut uint32 = 20 // 默认超时20s

	request, token, err := chn.getReqParam(clientID, data[4:])
	if err != nil {
		chn.sendProto(clientID,
			base.OPEN_RESOURCE_CMD,
			"", err.Error(),
			"huamail.defualt")
		return
	}

	//获取资源
	param := request.Param.(map[string]interface{})
	openResParam := base.OpenResParamJson{param["resourceid"].(string),
		uint32(param["timeout"].(float64))}

	if chn.CheckResIsOpen(clientID) {
		chn.sendProto(clientID,
			base.OPEN_RESOURCE_CMD,
			"", base.OPEN303,
			"huamail.defualt")
		return
	}

	mylog.GetErrorLogger().Println("Chn Open Resource ID ", openResParam.ID)

	if openResParam.TimeOut < 120 { //当超时时间大于120s,设置超时为默认

		timeOut = openResParam.TimeOut
	}

	for delayWait := uint32(0); delayWait < timeOut; delayWait++ {

		resourcer = GetResourcerByID(openResParam.ID)
		if resourcer != nil {
			break
		}

		time.Sleep(1 * time.Second)
	}

	if resourcer == nil {
		mylog.GetErrorLogger().Println("No Found Source ", openResParam.ID)

		chn.sendProto(clientID, base.OPEN_RESOURCE_CMD, "", base.NOFOUNF404, request.Ns)
		ReleaseResourcer(resourcer)

		return
	}

	clientDataID := new(ResourceClient)
	clientDataID.ClientInf = chn
	clientDataID.ClientID = clientID

	_, responseErr = resourcer.Open(clientDataID, request, token)
	responseJson := base.ResponseJson{request.Ns, request.Method, "", responseErr}
	b, err := json.Marshal(responseJson)
	if err != nil {
		mylog.GetErrorLogger().Println("handleOpenResource : ", err)
		ReleaseResourcer(resourcer)
		return
	}

	proto := new(base.Proto)
	proto.RD.BaseHD.CommandId = 0x80 | base.OPEN_RESOURCE_CMD
	proto.RD.HD.ClientIdent = clientID

	proto.EncodeBody(b)

	if !chn.Send(proto) {
		ReleaseResourcer(resourcer)
		return
	}

	if !strings.EqualFold(responseErr, base.OK) {
		ReleaseResourcer(resourcer)
		return
	}

	{
		chn.resMutex.Lock()
		defer chn.resMutex.Unlock()
		chn.openRes[clientID] = resourcer
	}
	stat.GetLocalStatistInst().OpenRes()

}