flv.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. package codec
  2. import (
  3. "errors"
  4. "io"
  5. "net"
  6. "m7s.live/engine/v4/util"
  7. )
  8. const (
  9. // FLV Tag Type
  10. FLV_TAG_TYPE_AUDIO = 0x08
  11. FLV_TAG_TYPE_VIDEO = 0x09
  12. FLV_TAG_TYPE_SCRIPT = 0x12
  13. )
  14. const (
  15. PacketTypeSequenceStart = iota
  16. PacketTypeCodedFrames
  17. PacketTypeSequenceEnd
  18. PacketTypeCodedFramesX
  19. PacketTypeMetadata
  20. PacketTypeMPEG2TSSequenceStart
  21. )
  22. var (
  23. Codec2SoundFormat = map[string]byte{
  24. "aac": 10,
  25. "pcma": 7,
  26. "pcmu": 8,
  27. }
  28. // 音频格式. 4 bit
  29. SoundFormat = map[byte]string{
  30. 0: "Linear PCM, platform endian",
  31. 1: "ADPCM",
  32. 2: "MP3",
  33. 3: "Linear PCM, little endian",
  34. 4: "Nellymoser 16kHz mono",
  35. 5: "Nellymoser 8kHz mono",
  36. 6: "Nellymoser",
  37. 7: "PCMA",
  38. 8: "PCMU",
  39. 9: "reserved",
  40. 10: "AAC",
  41. 11: "Speex",
  42. 14: "MP3 8Khz",
  43. 15: "Device-specific sound"}
  44. // 采样频率. 2 bit
  45. SoundRate = map[byte]int{
  46. 0: 5500,
  47. 1: 11000,
  48. 2: 22000,
  49. 3: 44000}
  50. // 量化精度. 1 bit
  51. SoundSize = map[byte]string{
  52. 0: "8Bit",
  53. 1: "16Bit"}
  54. // 音频类型. 1bit
  55. SoundType = map[byte]string{
  56. 0: "Mono",
  57. 1: "Stereo"}
  58. // 视频帧类型. 4bit
  59. FrameType = map[byte]string{
  60. 1: "keyframe (for AVC, a seekable frame)",
  61. 2: "inter frame (for AVC, a non-seekable frame)",
  62. 3: "disposable inter frame (H.263 only)",
  63. 4: "generated keyframe (reserved for server use only)",
  64. 5: "video info/command frame"}
  65. // 视频编码类型. 4bit
  66. CodecID = map[byte]string{
  67. 1: "JPEG (currently unused)",
  68. 2: "Sorenson H.263",
  69. 3: "Screen video",
  70. 4: "On2 VP6",
  71. 5: "On2 VP6 with alpha channel",
  72. 6: "Screen video version 2",
  73. 7: "H264",
  74. 12: "H265"}
  75. )
  76. var ErrInvalidFLV = errors.New("invalid flv")
  77. var FLVHeader = []byte{'F', 'L', 'V', 0x01, 0x05, 0, 0, 0, 9, 0, 0, 0, 0}
  78. func WriteFLVTag(w io.Writer, t byte, timestamp uint32, payload []byte) (err error) {
  79. buffers := AVCC2FLV(t, timestamp, payload)
  80. _, err = buffers.WriteTo(w)
  81. return
  82. }
  83. func ReadFLVTag(r io.Reader) (t byte, timestamp uint32, payload []byte, err error) {
  84. head := make([]byte, 11)
  85. if _, err = io.ReadFull(r, head); err != nil {
  86. return
  87. }
  88. t = head[0]
  89. dataSize := util.ReadBE[int](head[1:4])
  90. timestamp = (uint32(head[7]) << 24) | (uint32(head[4]) << 16) | (uint32(head[5]) << 8) | uint32(head[6])
  91. payload = make([]byte, dataSize)
  92. if _, err = io.ReadFull(r, payload); err == nil {
  93. _, err = io.ReadFull(r, head[:4])
  94. }
  95. return
  96. }
  97. func AudioAVCC2FLV(ts uint32, avcc ...[]byte) net.Buffers {
  98. return AVCC2FLV(FLV_TAG_TYPE_AUDIO, ts, avcc...)
  99. }
  100. func VideoAVCC2FLV(ts uint32, avcc ...[]byte) net.Buffers {
  101. return AVCC2FLV(FLV_TAG_TYPE_VIDEO, ts, avcc...)
  102. }
  103. func AVCC2FLV(t byte, ts uint32, avcc ...[]byte) (flv net.Buffers) {
  104. b := util.Buffer(make([]byte, 0, 15))
  105. b.WriteByte(t)
  106. dataSize := util.SizeOfBuffers(avcc)
  107. b.WriteUint24(uint32(dataSize))
  108. b.WriteUint24(ts)
  109. b.WriteByte(byte(ts >> 24))
  110. b.WriteUint24(0)
  111. return append(append(append(flv, b), avcc...), util.PutBE(b.Malloc(4), dataSize+11))
  112. }