convert.go 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. package util
  2. import (
  3. "errors"
  4. "io"
  5. "strconv"
  6. "strings"
  7. )
  8. /*
  9. func ReadByteToUintX(r io.Reader, l int) (data uint64, err error) {
  10. if l%8 != 0 || l > 64 {
  11. return 0, errors.New("disable convert")
  12. }
  13. bb := make([]byte, l)
  14. if _, err := io.ReadFull(r, bb); err != nil {
  15. return 0, err
  16. }
  17. switch l / 8 {
  18. case 1:
  19. {
  20. return uint8(bb[0]), nil
  21. }
  22. case 2:
  23. {
  24. return BigEndian.Uint16(bb), nil
  25. }
  26. case 3:
  27. {
  28. return BigEndian.Uint24(bb), nil
  29. }
  30. case 4:
  31. {
  32. return BigEndian.Uint32(bb), nil
  33. }
  34. case 5:
  35. {
  36. //return BigEndian.Uint40(bb), nil
  37. return 0, errors.New("disable convert")
  38. }
  39. case 6:
  40. {
  41. return BigEndian.Uint48(bb), nil
  42. }
  43. case 7:
  44. {
  45. //return BigEndian.Uint56(bb), nil
  46. return 0, errors.New("disable convert")
  47. }
  48. case 8:
  49. {
  50. return BigEndian.Uint64(bb), nil
  51. }
  52. }
  53. return 0, errors.New("convert not exist")
  54. }
  55. */
  56. // // 千万注意大小端,RTMP是大端
  57. func ByteToUint32N(data []byte) (ret uint32, err error) {
  58. if len(data) > 4 {
  59. return 0, errors.New("ByteToUint32N error!")
  60. }
  61. for i := 0; i < len(data); i++ {
  62. ret <<= 8
  63. ret |= uint32(data[i])
  64. }
  65. return
  66. }
  67. // // 千万注意大小端,RTMP是大端
  68. func ByteToUint64N(data []byte) (ret uint64, err error) {
  69. if len(data) > 8 {
  70. return 0, errors.New("ByteToUint64N error!")
  71. }
  72. for i := 0; i < len(data); i++ {
  73. ret <<= 8
  74. ret |= uint64(data[i])
  75. }
  76. return
  77. }
  78. // 千万注意大小端,RTMP是大端
  79. func ByteToUint32(data []byte, bigEndian bool) (ret uint32, err error) {
  80. if bigEndian {
  81. return BigEndian.Uint32(data), nil
  82. } else {
  83. return LittleEndian.Uint32(data), nil
  84. }
  85. }
  86. func Uint32ToByte(data uint32, bigEndian bool) (ret []byte, err error) {
  87. if bigEndian {
  88. return BigEndian.ToUint32(data), nil
  89. } else {
  90. return LittleEndian.ToUint32(data), nil
  91. }
  92. }
  93. func ReadByteToUint8(r io.Reader) (data uint8, err error) {
  94. bb := make([]byte, 1)
  95. if _, err := io.ReadFull(r, bb); err != nil {
  96. return 0, err
  97. }
  98. return uint8(bb[0]), nil
  99. }
  100. func ReadByteToUint16(r io.Reader, bigEndian bool) (data uint16, err error) {
  101. bb := make([]byte, 2)
  102. if _, err := io.ReadFull(r, bb); err != nil {
  103. return 0, err
  104. }
  105. if bigEndian {
  106. return BigEndian.Uint16(bb), nil
  107. } else {
  108. return LittleEndian.Uint16(bb), nil
  109. }
  110. }
  111. func ReadByteToUint24(r io.Reader, bigEndian bool) (data uint32, err error) {
  112. bb := make([]byte, 3)
  113. if _, err := io.ReadFull(r, bb); err != nil {
  114. return 0, err
  115. }
  116. if bigEndian {
  117. return BigEndian.Uint24(bb), nil
  118. } else {
  119. return LittleEndian.Uint24(bb), nil
  120. }
  121. }
  122. func ReadByteToUint32(r io.Reader, bigEndian bool) (data uint32, err error) {
  123. bb := make([]byte, 4)
  124. if _, err := io.ReadFull(r, bb); err != nil {
  125. return 0, err
  126. }
  127. if bigEndian {
  128. return BigEndian.Uint32(bb), nil
  129. } else {
  130. return LittleEndian.Uint32(bb), nil
  131. }
  132. }
  133. func ReadByteToUint40(r io.Reader, bigEndian bool) (data uint64, err error) {
  134. bb := make([]byte, 5)
  135. if _, err := io.ReadFull(r, bb); err != nil {
  136. return 0, err
  137. }
  138. if bigEndian {
  139. return BigEndian.Uint40(bb), nil
  140. } else {
  141. return LittleEndian.Uint40(bb), nil
  142. }
  143. }
  144. func ReadByteToUint48(r io.Reader, bigEndian bool) (data uint64, err error) {
  145. bb := make([]byte, 6)
  146. if _, err := io.ReadFull(r, bb); err != nil {
  147. return 0, err
  148. }
  149. if bigEndian {
  150. return BigEndian.Uint48(bb), nil
  151. } else {
  152. return LittleEndian.Uint48(bb), nil
  153. }
  154. }
  155. /*
  156. func ReadByteToUint56(r io.Reader) (data uint64, err error) {
  157. bb := make([]byte, 7)
  158. if _, err := io.ReadFull(r, bb); err != nil {
  159. return 0, err
  160. }
  161. return uint8(bb[0]), nil
  162. }
  163. */
  164. func ReadByteToUint64(r io.Reader, bigEndian bool) (data uint64, err error) {
  165. bb := make([]byte, 8)
  166. if _, err := io.ReadFull(r, bb); err != nil {
  167. return 0, err
  168. }
  169. if bigEndian {
  170. return BigEndian.Uint64(bb), nil
  171. } else {
  172. return LittleEndian.Uint64(bb), nil
  173. }
  174. }
  175. func WriteUint8ToByte(w io.Writer, data uint8) error {
  176. bb := make([]byte, 8)
  177. bb[0] = byte(data)
  178. _, err := w.Write(bb[:1])
  179. if err != nil {
  180. return err
  181. }
  182. return nil
  183. }
  184. func WriteUint16ToByte(w io.Writer, data uint16, bigEndian bool) error {
  185. var bb []byte
  186. if bigEndian {
  187. bb = BigEndian.ToUint16(data)
  188. } else {
  189. bb = LittleEndian.ToUint16(data)
  190. }
  191. _, err := w.Write(bb)
  192. if err != nil {
  193. return err
  194. }
  195. return nil
  196. }
  197. func WriteUint24ToByte(w io.Writer, data uint32, bigEndian bool) error {
  198. var bb []byte
  199. if bigEndian {
  200. bb = BigEndian.ToUint24(data)
  201. } else {
  202. bb = LittleEndian.ToUint24(data)
  203. }
  204. _, err := w.Write(bb)
  205. if err != nil {
  206. return err
  207. }
  208. return nil
  209. }
  210. func WriteUint32ToByte(w io.Writer, data uint32, bigEndian bool) error {
  211. var bb []byte
  212. if bigEndian {
  213. bb = BigEndian.ToUint32(data)
  214. } else {
  215. bb = LittleEndian.ToUint32(data)
  216. }
  217. _, err := w.Write(bb)
  218. if err != nil {
  219. return err
  220. }
  221. return nil
  222. }
  223. func WriteUint40ToByte(w io.Writer, data uint64, bigEndian bool) error {
  224. var bb []byte
  225. if bigEndian {
  226. bb = BigEndian.ToUint40(data)
  227. } else {
  228. bb = LittleEndian.ToUint40(data)
  229. }
  230. _, err := w.Write(bb)
  231. if err != nil {
  232. return err
  233. }
  234. return nil
  235. }
  236. func WriteUint48ToByte(w io.Writer, data uint64, bigEndian bool) error {
  237. var bb []byte
  238. if bigEndian {
  239. bb = BigEndian.ToUint48(data)
  240. } else {
  241. bb = LittleEndian.ToUint48(data)
  242. }
  243. _, err := w.Write(bb)
  244. if err != nil {
  245. return err
  246. }
  247. return nil
  248. }
  249. func WriteUint64ToByte(w io.Writer, data uint64, bigEndian bool) error {
  250. var bb []byte
  251. if bigEndian {
  252. bb = BigEndian.ToUint64(data)
  253. } else {
  254. bb = LittleEndian.ToUint64(data)
  255. }
  256. _, err := w.Write(bb)
  257. if err != nil {
  258. return err
  259. }
  260. return nil
  261. }
  262. func GetPtsDts(v uint64) uint64 {
  263. // 4 + 3 + 1 + 15 + 1 + 15 + 1
  264. // 0011
  265. // 0010 + PTS[30-32] + marker_bit + PTS[29-15] + marker_bit + PTS[14-0] + marker_bit
  266. pts1 := ((v >> 33) & 0x7) << 30
  267. pts2 := ((v >> 17) & 0x7fff) << 15
  268. pts3 := ((v >> 1) & 0x7fff)
  269. return pts1 | pts2 | pts3
  270. }
  271. func PutPtsDts(v uint64) uint64 {
  272. // 4 + 3 + 1 + 15 + 1 + 15 + 1
  273. // 0011
  274. // 0010 + PTS[30-32] + marker_bit + PTS[29-15] + marker_bit + PTS[14-0] + marker_bit
  275. // 0x100010001
  276. // 0001 0000 0000 0000 0001 0000 0000 0000 0001
  277. // 3个 market_it
  278. pts1 := (v >> 30) & 0x7 << 33
  279. pts2 := (v >> 15) & 0x7fff << 17
  280. pts3 := (v & 0x7fff) << 1
  281. return pts1 | pts2 | pts3 | 0x100010001
  282. }
  283. func GetPCR(v uint64) uint64 {
  284. // program_clock_reference_base(33) + Reserved(6) + program_clock_reference_extension(9)
  285. base := v >> 15
  286. ext := v & 0x1ff
  287. return base*300 + ext
  288. }
  289. func PutPCR(pcr uint64) uint64 {
  290. base := pcr / 300
  291. ext := pcr % 300
  292. return base<<15 | 0x3f<<9 | ext
  293. }
  294. func GetFillBytes(data byte, n int) []byte {
  295. b := make([]byte, n)
  296. for i := range b {
  297. b[i] = data
  298. }
  299. return b
  300. }
  301. func ToFloat64(num interface{}) float64 {
  302. switch v := num.(type) {
  303. case uint:
  304. return float64(v)
  305. case int:
  306. return float64(v)
  307. case uint8:
  308. return float64(v)
  309. case uint16:
  310. return float64(v)
  311. case uint32:
  312. return float64(v)
  313. case uint64:
  314. return float64(v)
  315. case int8:
  316. return float64(v)
  317. case int16:
  318. return float64(v)
  319. case int32:
  320. return float64(v)
  321. case int64:
  322. return float64(v)
  323. case float64:
  324. return v
  325. case float32:
  326. return float64(v)
  327. }
  328. return 0
  329. }
  330. func Conf2Listener(conf string) (protocol string, ports []uint16) {
  331. var port string
  332. protocol, port, _ = strings.Cut(conf, ":")
  333. if r := strings.Split(port, "-"); len(r) == 2 {
  334. min, err := strconv.Atoi(r[0])
  335. if err != nil {
  336. return
  337. }
  338. max, err := strconv.Atoi(r[1])
  339. if err != nil {
  340. return
  341. }
  342. if min < max {
  343. ports = append(ports, uint16(min), uint16(max))
  344. }
  345. } else if p, err := strconv.Atoi(port); err == nil {
  346. ports = append(ports, uint16(p))
  347. }
  348. return
  349. }