github.com/cnotch/ipchub@v1.1.0/media/cid.go (about) 1 // Copyright (c) 2019,CAOHONGJU All rights reserved. 2 // Use of this source code is governed by a MIT-style 3 // license that can be found in the LICENSE file. 4 5 package media 6 7 import ( 8 "sync/atomic" 9 ) 10 11 // PacketType 消费媒体包类型 12 type PacketType uint32 13 14 // 预定义消费媒体包类型 15 const ( 16 RTPPacket PacketType = iota // 根据 RTP 协议打包的媒体 17 FLVPacket 18 19 maxConsumerSequence = 0x3fff_ffff 20 ) 21 22 // CID consumer ID 23 // type(2bits)+sequence(30bits) 24 type CID uint32 25 26 // String 类型的字串表示 27 func (t PacketType) String() string { 28 switch t { 29 case RTPPacket: 30 return "RTP" 31 case FLVPacket: 32 return "FLV" 33 default: 34 return "Unknown" 35 } 36 } 37 38 // NewCID 创建新的流消费ID 39 func NewCID(packetType PacketType, consumerSequenceSeed *uint32) CID { 40 localid := atomic.AddUint32(consumerSequenceSeed, 1) 41 if localid >= maxConsumerSequence { 42 localid = 1 43 atomic.StoreUint32(consumerSequenceSeed, localid) 44 } 45 return CID(packetType<<30) | CID(localid&maxConsumerSequence) 46 } 47 48 // Type 获取消费者类型 49 func (id CID) Type() PacketType { 50 return PacketType((id >> 30) & 0x3) 51 } 52 53 // Sequence 获取消费者序号 54 func (id CID) Sequence() uint32 { 55 return uint32(id & CID(maxConsumerSequence)) 56 }