You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

mk_recorder.cpp 3.1KB

8 months ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright (c) 2016 The ZLMediaKit project authors. All Rights Reserved.
  3. *
  4. * This file is part of ZLMediaKit(https://github.com/xia-chu/ZLMediaKit).
  5. *
  6. * Use of this source code is governed by MIT license that can be found in the
  7. * LICENSE file in the root of the source tree. All contributing project authors
  8. * may be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include "mk_recorder.h"
  11. #include "Rtmp/FlvMuxer.h"
  12. #include "Record/Recorder.h"
  13. using namespace std;
  14. using namespace toolkit;
  15. using namespace mediakit;
  16. API_EXPORT mk_flv_recorder API_CALL mk_flv_recorder_create(){
  17. FlvRecorder::Ptr *ret = new FlvRecorder::Ptr(new FlvRecorder);
  18. return ret;
  19. }
  20. API_EXPORT void API_CALL mk_flv_recorder_release(mk_flv_recorder ctx){
  21. assert(ctx);
  22. FlvRecorder::Ptr *record = (FlvRecorder::Ptr *)(ctx);
  23. delete record;
  24. }
  25. API_EXPORT int API_CALL mk_flv_recorder_start(mk_flv_recorder ctx, const char *vhost, const char *app, const char *stream, const char *file_path){
  26. assert(ctx && vhost && app && stream && file_path);
  27. try {
  28. FlvRecorder::Ptr *record = (FlvRecorder::Ptr *)(ctx);
  29. (*record)->startRecord(EventPollerPool::Instance().getPoller(),vhost,app,stream,file_path);
  30. return 0;
  31. }catch (std::exception &ex){
  32. WarnL << ex.what();
  33. return -1;
  34. }
  35. }
  36. ///////////////////////////////////////////hls/mp4录制/////////////////////////////////////////////
  37. static inline bool isRecording(Recorder::type type, const string &vhost, const string &app, const string &stream_id){
  38. auto src = MediaSource::find(vhost, app, stream_id);
  39. if(!src){
  40. return false;
  41. }
  42. return src->isRecording(type);
  43. }
  44. static inline bool startRecord(Recorder::type type, const string &vhost, const string &app, const string &stream_id,const string &customized_path, size_t max_second){
  45. auto src = MediaSource::find(vhost, app, stream_id);
  46. if (!src) {
  47. WarnL << "未找到相关的MediaSource,startRecord失败:" << vhost << "/" << app << "/" << stream_id;
  48. return false;
  49. }
  50. return src->setupRecord(type, true, customized_path, max_second);
  51. }
  52. static inline bool stopRecord(Recorder::type type, const string &vhost, const string &app, const string &stream_id){
  53. auto src = MediaSource::find(vhost, app, stream_id);
  54. if(!src){
  55. return false;
  56. }
  57. return src->setupRecord(type, false, "", 0);
  58. }
  59. API_EXPORT int API_CALL mk_recorder_is_recording(int type, const char *vhost, const char *app, const char *stream){
  60. assert(vhost && app && stream);
  61. return isRecording((Recorder::type)type,vhost,app,stream);
  62. }
  63. API_EXPORT int API_CALL mk_recorder_start(int type, const char *vhost, const char *app, const char *stream,const char *customized_path, size_t max_second){
  64. assert(vhost && app && stream);
  65. return startRecord((Recorder::type)type,vhost,app,stream,customized_path ? customized_path : "", max_second);
  66. }
  67. API_EXPORT int API_CALL mk_recorder_stop(int type, const char *vhost, const char *app, const char *stream){
  68. assert(vhost && app && stream);
  69. return stopRecord((Recorder::type)type,vhost,app,stream);
  70. }