restful.go 2.5 KB

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