tcp-rtp.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package ps
  2. import (
  3. "bufio"
  4. "encoding/binary"
  5. "io"
  6. "net"
  7. "m7s.live/engine/v4/util"
  8. )
  9. type TCPRTP struct {
  10. net.Conn
  11. }
  12. func (t *TCPRTP) Start(onRTP func(util.Buffer) error) (err error) {
  13. reader := bufio.NewReader(t.Conn)
  14. rtpLenBuf := make([]byte, 4)
  15. buffer := make(util.Buffer, 1024)
  16. for err == nil {
  17. if _, err = io.ReadFull(reader, rtpLenBuf); err != nil {
  18. return
  19. }
  20. rtpLen := int(binary.BigEndian.Uint16(rtpLenBuf[:2]))
  21. if rtpLenBuf[2]>>6 != 2 || rtpLenBuf[2]&0x0f > 15 || rtpLenBuf[3]&0x7f > 127 { //长度后面正常紧跟 rtp 头,如果不是,说明长度不对,此处并非长度,而是可能之前的 rtp 包不完整导致的,需要往前查找
  22. buffer.Write(rtpLenBuf) //已读的数据先写入缓存
  23. for i := 12; i < buffer.Len()-2; i++ { // 缓存中 rtp 头就不用判断了,跳过 12 字节往后寻找
  24. if buffer[i]>>6 != 2 || buffer[i]&0x0f > 15 || buffer[i+1]&0x7f > 127 { // 一直找到 rtp 头为止
  25. continue
  26. }
  27. rtpLen = int(binary.BigEndian.Uint16(buffer[i-2 : i])) // rtp 头前面两个字节是长度
  28. if remain := buffer.Len() - i; remain < rtpLen { // 缓存中的数据不够一个 rtp 包,继续读取
  29. copy(buffer, buffer[i:])
  30. buffer.Relloc(rtpLen)
  31. if _, err = io.ReadFull(reader, buffer[remain:]); err != nil {
  32. return
  33. }
  34. err = onRTP(buffer)
  35. break
  36. } else {
  37. err = onRTP(buffer.SubBuf(i, rtpLen))
  38. if err != nil {
  39. return
  40. }
  41. i += rtpLen
  42. if buffer.Len() > i+1 {
  43. i += 2
  44. } else if buffer.Len() > i {
  45. reader.UnreadByte()
  46. break
  47. } else {
  48. break
  49. }
  50. i--
  51. }
  52. }
  53. } else {
  54. buffer.Relloc(rtpLen)
  55. copy(buffer, rtpLenBuf[2:])
  56. if _, err = io.ReadFull(reader, buffer[2:]); err != nil {
  57. return
  58. }
  59. err = onRTP(buffer)
  60. }
  61. }
  62. return
  63. }