restful.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package record
  2. import (
  3. "encoding/json"
  4. "net/http"
  5. "time"
  6. "go.uber.org/zap"
  7. . "m7s.live/engine/v4"
  8. "m7s.live/engine/v4/util"
  9. )
  10. func (conf *RecordConfig) API_list(w http.ResponseWriter, r *http.Request) {
  11. query := r.URL.Query()
  12. t := query.Get("type")
  13. var files []*VideoFileInfo
  14. var err error
  15. recorder := conf.getRecorderConfigByType(t)
  16. if recorder == nil {
  17. for _, t = range []string{"flv", "mp4", "fmp4", "hls", "raw", "raw_audio"} {
  18. recorder = conf.getRecorderConfigByType(t)
  19. var fs []*VideoFileInfo
  20. if fs, err = recorder.Tree(recorder.Path, 0); err == nil {
  21. files = append(files, fs...)
  22. }
  23. }
  24. } else {
  25. files, err = recorder.Tree(recorder.Path, 0)
  26. }
  27. if err == nil {
  28. var bytes []byte
  29. if bytes, err = json.Marshal(files); err == nil {
  30. w.Write(bytes)
  31. }
  32. }
  33. if err != nil {
  34. http.Error(w, err.Error(), http.StatusInternalServerError)
  35. }
  36. }
  37. func (conf *RecordConfig) API_start(w http.ResponseWriter, r *http.Request) {
  38. query := r.URL.Query()
  39. streamPath := query.Get("streamPath")
  40. //recognitionId := query.Get("recognitionId")
  41. fileName := query.Get("fileName")
  42. fragment := query.Get("fragment")
  43. if streamPath == "" {
  44. http.Error(w, "no streamPath", http.StatusBadRequest)
  45. return
  46. }
  47. t := query.Get("type")
  48. var id string
  49. var err error
  50. var irecorder IRecorder
  51. switch t {
  52. case "":
  53. t = "flv"
  54. fallthrough
  55. case "flv":
  56. irecorder = NewFLVRecorder()
  57. case "mp4":
  58. mp4Recorder := NewMP4Recorder()
  59. //mp4Recorder.recognitionId = recognitionId
  60. irecorder = mp4Recorder
  61. case "fmp4":
  62. irecorder = NewFMP4Recorder()
  63. case "hls":
  64. irecorder = NewHLSRecorder()
  65. case "raw":
  66. irecorder = NewRawRecorder()
  67. case "raw_audio":
  68. irecorder = NewRawAudioRecorder()
  69. default:
  70. http.Error(w, "type not supported", http.StatusBadRequest)
  71. return
  72. }
  73. recorder := irecorder.GetRecorder()
  74. if fragment != "" {
  75. if f, err := time.ParseDuration(fragment); err == nil {
  76. recorder.Fragment = f
  77. }
  78. }
  79. recorder.FileName = fileName
  80. recorder.append = query.Get("append") != ""
  81. err = irecorder.Start(streamPath)
  82. id = recorder.ID
  83. if err != nil {
  84. util.ReturnError(util.APIErrorInternal, err.Error(), w, r)
  85. return
  86. }
  87. util.ReturnError(util.APIErrorNone, id, w, r)
  88. }
  89. func (conf *RecordConfig) API_list_recording(w http.ResponseWriter, r *http.Request) {
  90. util.ReturnFetchValue(func() (recordings []any) {
  91. conf.recordings.Range(func(key, value any) bool {
  92. recordings = append(recordings, value)
  93. return true
  94. })
  95. return
  96. }, w, r)
  97. }
  98. func (conf *RecordConfig) API_stop(w http.ResponseWriter, r *http.Request) {
  99. if recorder, ok := conf.recordings.Load(r.URL.Query().Get("id")); ok {
  100. recorder.(ISubscriber).Stop(zap.String("reason", "api"))
  101. util.ReturnOK(w, r)
  102. return
  103. }
  104. util.ReturnError(util.APIErrorNotFound, "no such recorder", w, r)
  105. }