raw.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package record
  2. import (
  3. "go.uber.org/zap"
  4. . "m7s.live/engine/v4"
  5. "m7s.live/engine/v4/codec"
  6. "m7s.live/engine/v4/track"
  7. )
  8. type RawRecorder struct {
  9. Recorder
  10. IsAudio bool
  11. }
  12. func NewRawRecorder() (r *RawRecorder) {
  13. r = &RawRecorder{}
  14. r.Record = RecordPluginConfig.Raw
  15. return r
  16. }
  17. func NewRawAudioRecorder() (r *RawRecorder) {
  18. r = &RawRecorder{IsAudio: true}
  19. r.Record = RecordPluginConfig.RawAudio
  20. return r
  21. }
  22. func (r *RawRecorder) Start(streamPath string) error {
  23. r.ID = streamPath + "/raw"
  24. if r.IsAudio {
  25. r.ID += "_audio"
  26. }
  27. return r.start(r, streamPath, SUBTYPE_RAW)
  28. }
  29. func (r *RawRecorder) Close() (err error) {
  30. if r.File != nil {
  31. err = r.File.Close()
  32. }
  33. return
  34. }
  35. func (r *RawRecorder) OnEvent(event any) {
  36. switch v := event.(type) {
  37. case FileWr:
  38. r.SetIO(v)
  39. case *RawRecorder:
  40. r.Recorder.OnEvent(event)
  41. case *track.Video:
  42. if r.IsAudio {
  43. break
  44. }
  45. if r.Ext == "." {
  46. if v.CodecID == codec.CodecID_H264 {
  47. r.Ext = ".h264"
  48. } else {
  49. r.Ext = ".h265"
  50. }
  51. }
  52. r.AddTrack(v)
  53. case *track.Audio:
  54. if !r.IsAudio {
  55. break
  56. }
  57. if r.Ext == "." {
  58. switch v.CodecID {
  59. case codec.CodecID_AAC:
  60. r.Ext = ".aac"
  61. case codec.CodecID_PCMA:
  62. r.Ext = ".pcma"
  63. case codec.CodecID_PCMU:
  64. r.Ext = ".pcmu"
  65. }
  66. }
  67. r.AddTrack(v)
  68. case AudioFrame:
  69. r.Recorder.OnEvent(event)
  70. if _, err := v.WriteRawTo(r); err != nil {
  71. r.Stop(zap.Error(err))
  72. }
  73. case VideoFrame:
  74. r.Recorder.OnEvent(event)
  75. if _, err := v.WriteAnnexBTo(r); err != nil {
  76. r.Stop(zap.Error(err))
  77. }
  78. default:
  79. r.IO.OnEvent(v)
  80. }
  81. }