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.

преди 6 месеца
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. #ifndef FFMPEG_SOURCE_H
  11. #define FFMPEG_SOURCE_H
  12. #include <mutex>
  13. #include <memory>
  14. #include <functional>
  15. #include "Process.h"
  16. #include "Util/TimeTicker.h"
  17. #include "Common/MediaSource.h"
  18. namespace FFmpeg {
  19. extern const std::string kSnap;
  20. }
  21. class FFmpegSnap {
  22. public:
  23. using onSnap = std::function<void(bool success, const std::string &err_msg)>;
  24. /// 创建截图
  25. /// \param play_url 播放url地址,只要FFmpeg支持即可
  26. /// \param save_path 截图jpeg文件保存路径
  27. /// \param timeout_sec 生成截图超时时间(防止阻塞太久)
  28. /// \param cb 生成截图成功与否回调
  29. static void makeSnap(const std::string &play_url, const std::string &save_path, float timeout_sec, const onSnap &cb);
  30. private:
  31. FFmpegSnap() = delete;
  32. ~FFmpegSnap() = delete;
  33. };
  34. class FFmpegSource : public std::enable_shared_from_this<FFmpegSource> , public mediakit::MediaSourceEventInterceptor{
  35. public:
  36. using Ptr = std::shared_ptr<FFmpegSource>;
  37. using onPlay = std::function<void(const toolkit::SockException &ex)>;
  38. FFmpegSource();
  39. ~FFmpegSource();
  40. /**
  41. * 设置主动关闭回调
  42. */
  43. void setOnClose(const std::function<void()> &cb);
  44. /**
  45. * 开始播放url
  46. * @param ffmpeg_cmd_key FFmpeg拉流命令配置项key,用户可以在配置文件中同时设置多个命令参数模板
  47. * @param src_url FFmpeg拉流地址
  48. * @param dst_url FFmpeg推流地址
  49. * @param timeout_ms 等待结果超时时间,单位毫秒
  50. * @param cb 成功与否回调
  51. */
  52. void play(const std::string &ffmpeg_cmd_key, const std::string &src_url, const std::string &dst_url, int timeout_ms, const onPlay &cb);
  53. /**
  54. * 设置录制
  55. * @param enable_hls 是否开启hls直播或录制
  56. * @param enable_mp4 是否录制mp4
  57. */
  58. void setupRecordFlag(bool enable_hls, bool enable_mp4);
  59. private:
  60. void findAsync(int maxWaitMS ,const std::function<void(const mediakit::MediaSource::Ptr &src)> &cb);
  61. void startTimer(int timeout_ms);
  62. void onGetMediaSource(const mediakit::MediaSource::Ptr &src);
  63. ///////MediaSourceEvent override///////
  64. // 关闭
  65. bool close(mediakit::MediaSource &sender) override;
  66. // 获取媒体源类型
  67. mediakit::MediaOriginType getOriginType(mediakit::MediaSource &sender) const override;
  68. //获取媒体源url或者文件路径
  69. std::string getOriginUrl(mediakit::MediaSource &sender) const override;
  70. // 获取媒体源客户端相关信息
  71. std::shared_ptr<toolkit::SockInfo> getOriginSock(mediakit::MediaSource &sender) const override;
  72. private:
  73. bool _enable_hls = false;
  74. bool _enable_mp4 = false;
  75. Process _process;
  76. toolkit::Timer::Ptr _timer;
  77. toolkit::EventPoller::Ptr _poller;
  78. mediakit::MediaInfo _media_info;
  79. std::string _src_url;
  80. std::string _dst_url;
  81. std::string _ffmpeg_cmd_key;
  82. std::function<void()> _onClose;
  83. toolkit::Ticker _replay_ticker;
  84. };
  85. #endif //FFMPEG_SOURCE_H