func publish(stream rtmp.ClientStream) { var err error flvFile, err = flv.OpenFile(*flvFileName) if err != nil { log.Println("Open FLV dump file error:", err) return } defer flvFile.Close() startTs := uint32(0) startAt := time.Now().UnixNano() preTs := uint32(0) for status == rtmp.CLIENT_CONN_STATUS_CREATE_STREAM_OK { if flvFile.IsFinished() { log.Println("@@@@@@@@@@@@@@File finished") flvFile.LoopBack() startAt = time.Now().UnixNano() startTs = uint32(0) preTs = uint32(0) } header, data, err := flvFile.ReadTag() if err != nil { log.Println("flvFile.ReadTag() error:", err) break } switch header.TagType { case flv.VIDEO_TAG: videoDataSize += int64(len(data)) case flv.AUDIO_TAG: audioDataSize += int64(len(data)) } if startTs == uint32(0) { startTs = header.Timestamp } diff1 := uint32(0) // deltaTs := uint32(0) if header.Timestamp >= startTs { diff1 = header.Timestamp - startTs } else { log.Printf("tag header timestamp (%d) < start timestamp (%d)", header.Timestamp, startTs) } if diff1 > preTs { // deltaTs = diff1 - preTs preTs = diff1 } if err = stream.PublishData(header.TagType, data, diff1); err != nil { log.Println("PublishData() error:", err) break } diff2 := uint32((time.Now().UnixNano() - startAt) / 1000000) // log.Printf("diff1: %d, diff2: %d\n", diff1, diff2) if diff1 > diff2+100 { // log.Printf("header.Timestamp: %d, now: %d\n", header.Timestamp, time.Now().UnixNano()) time.Sleep(time.Millisecond * time.Duration(diff1-diff2)) } } }
func (handler *TestClientConnHandler) OnStreamCreated(conn rtmp.ClientConn, stream rtmp.ClientStream) { log.Printf("Stream created: %d\n", stream.ID()) createStreamChan <- stream }
func (handler *PublishingClientConnHandler) OnStreamCreated(conn rtmp.ClientConn, stream rtmp.ClientStream) { log.Printf("OnStreamCreated, id = %d", stream.ID()) createStreamChan <- stream }
func (handler *PlayingClientConnHandler) OnStreamCreated(conn rtmp.ClientConn, stream rtmp.ClientStream) { log.Printf("play:OnStreamCreated, id = %d", stream.ID()) playCreateStreamChan <- stream }