Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

284 строки
9.6KB

  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_media.h"
  11. #include "Util/logger.h"
  12. #include "Common/Device.h"
  13. using namespace std;
  14. using namespace toolkit;
  15. using namespace mediakit;
  16. class MediaHelper : public MediaSourceEvent , public std::enable_shared_from_this<MediaHelper> {
  17. public:
  18. using Ptr = std::shared_ptr<MediaHelper>;
  19. template<typename ...ArgsType>
  20. MediaHelper(ArgsType &&...args){
  21. _channel = std::make_shared<DevChannel>(std::forward<ArgsType>(args)...);
  22. }
  23. ~MediaHelper(){}
  24. void attachEvent(){
  25. _channel->setMediaListener(shared_from_this());
  26. }
  27. DevChannel::Ptr &getChannel(){
  28. return _channel;
  29. }
  30. void setOnClose(on_mk_media_close cb, void *user_data){
  31. _on_close = cb;
  32. _on_close_data = user_data;
  33. }
  34. void setOnSeek(on_mk_media_seek cb, void *user_data){
  35. _on_seek = cb;
  36. _on_seek_data = user_data;
  37. }
  38. void setOnPause(on_mk_media_pause cb, void* user_data) {
  39. _on_pause = cb;
  40. _on_pause_data = user_data;
  41. }
  42. void setOnSpeed(on_mk_media_speed cb, void* user_data) {
  43. _on_speed = cb;
  44. _on_speed_data = user_data;
  45. }
  46. void setOnRegist(on_mk_media_source_regist cb, void *user_data){
  47. _on_regist = cb;
  48. _on_regist_data = user_data;
  49. }
  50. protected:
  51. // 通知其停止推流
  52. bool close(MediaSource &sender) override {
  53. if (!_on_close) {
  54. //未设置回调,没法关闭
  55. WarnL << "请使用mk_media_set_on_close函数设置回调函数!";
  56. return false;
  57. }
  58. //请在回调中调用mk_media_release函数释放资源,否则MediaSource::close()操作不会生效
  59. _on_close(_on_close_data);
  60. WarnL << "close media: " << sender.getUrl();
  61. return true;
  62. }
  63. bool seekTo(MediaSource &sender, uint32_t stamp) override {
  64. if (!_on_seek) {
  65. return false;
  66. }
  67. return _on_seek(_on_seek_data, stamp);
  68. }
  69. // 通知暂停或恢复
  70. bool pause(MediaSource &sender, bool pause) override {
  71. if (!_on_pause) {
  72. return false;
  73. }
  74. return _on_pause(_on_pause_data, pause);
  75. }
  76. //通知倍数播放
  77. bool speed(MediaSource &sender, float speed) override {
  78. if (!_on_speed) {
  79. return false;
  80. }
  81. return _on_speed(_on_speed_data, speed);
  82. }
  83. void onRegist(MediaSource &sender, bool regist) override{
  84. if (_on_regist) {
  85. _on_regist(_on_regist_data, &sender, regist);
  86. }
  87. }
  88. private:
  89. DevChannel::Ptr _channel;
  90. on_mk_media_close _on_close = nullptr;
  91. on_mk_media_seek _on_seek = nullptr;
  92. on_mk_media_pause _on_pause = nullptr;
  93. on_mk_media_speed _on_speed = nullptr;
  94. on_mk_media_source_regist _on_regist = nullptr;
  95. void* _on_seek_data;
  96. void* _on_pause_data;
  97. void* _on_speed_data;
  98. void *_on_close_data;
  99. void *_on_regist_data;
  100. };
  101. API_EXPORT void API_CALL mk_media_set_on_close(mk_media ctx, on_mk_media_close cb, void *user_data){
  102. assert(ctx);
  103. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  104. (*obj)->setOnClose(cb, user_data);
  105. }
  106. API_EXPORT void API_CALL mk_media_set_on_seek(mk_media ctx, on_mk_media_seek cb, void *user_data) {
  107. assert(ctx);
  108. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  109. (*obj)->setOnSeek(cb, user_data);
  110. }
  111. API_EXPORT void API_CALL mk_media_set_on_pause(mk_media ctx, on_mk_media_pause cb, void *user_data) {
  112. assert(ctx);
  113. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  114. (*obj)->setOnPause(cb, user_data);
  115. }
  116. API_EXPORT void API_CALL mk_media_set_on_speed(mk_media ctx, on_mk_media_speed cb, void *user_data) {
  117. assert(ctx);
  118. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  119. (*obj)->setOnSpeed(cb, user_data);
  120. }
  121. API_EXPORT void API_CALL mk_media_set_on_regist(mk_media ctx, on_mk_media_source_regist cb, void *user_data){
  122. assert(ctx);
  123. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  124. (*obj)->setOnRegist(cb, user_data);
  125. }
  126. API_EXPORT int API_CALL mk_media_total_reader_count(mk_media ctx){
  127. assert(ctx);
  128. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  129. return (*obj)->getChannel()->totalReaderCount();
  130. }
  131. API_EXPORT mk_media API_CALL mk_media_create(const char *vhost, const char *app, const char *stream,
  132. float duration, int hls_enabled, int mp4_enabled) {
  133. assert(vhost && app && stream);
  134. ProtocolOption option;
  135. option.enable_hls = hls_enabled;
  136. option.enable_mp4 = mp4_enabled;
  137. MediaHelper::Ptr *obj(new MediaHelper::Ptr(new MediaHelper(vhost, app, stream, duration, option)));
  138. (*obj)->attachEvent();
  139. return (mk_media) obj;
  140. }
  141. API_EXPORT void API_CALL mk_media_release(mk_media ctx) {
  142. assert(ctx);
  143. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  144. delete obj;
  145. }
  146. API_EXPORT int API_CALL mk_media_init_video(mk_media ctx, int codec_id, int width, int height, float fps, int bit_rate){
  147. assert(ctx);
  148. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  149. VideoInfo info;
  150. info.codecId = (CodecId)codec_id;
  151. info.iFrameRate = fps;
  152. info.iWidth = width;
  153. info.iHeight = height;
  154. info.iBitRate = bit_rate;
  155. return (*obj)->getChannel()->initVideo(info);
  156. }
  157. API_EXPORT int API_CALL mk_media_init_audio(mk_media ctx, int codec_id, int sample_rate, int channels, int sample_bit){
  158. assert(ctx);
  159. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  160. AudioInfo info;
  161. info.codecId = (CodecId)codec_id;
  162. info.iSampleRate = sample_rate;
  163. info.iChannel = channels;
  164. info.iSampleBit = sample_bit;
  165. return (*obj)->getChannel()->initAudio(info);
  166. }
  167. API_EXPORT void API_CALL mk_media_init_track(mk_media ctx, mk_track track){
  168. assert(ctx && track);
  169. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  170. (*obj)->getChannel()->addTrack(*((Track::Ptr *) track));
  171. }
  172. API_EXPORT void API_CALL mk_media_init_complete(mk_media ctx){
  173. assert(ctx);
  174. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  175. (*obj)->getChannel()->addTrackCompleted();
  176. }
  177. API_EXPORT int API_CALL mk_media_input_frame(mk_media ctx, mk_frame frame){
  178. assert(ctx && frame);
  179. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  180. return (*obj)->getChannel()->inputFrame(*((Frame::Ptr *) frame));
  181. }
  182. API_EXPORT int API_CALL mk_media_input_h264(mk_media ctx, const void *data, int len, uint64_t dts, uint64_t pts) {
  183. assert(ctx && data && len > 0);
  184. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  185. return (*obj)->getChannel()->inputH264((const char *) data, len, dts, pts);
  186. }
  187. API_EXPORT int API_CALL mk_media_input_h265(mk_media ctx, const void *data, int len, uint64_t dts, uint64_t pts) {
  188. assert(ctx && data && len > 0);
  189. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  190. return (*obj)->getChannel()->inputH265((const char *) data, len, dts, pts);
  191. }
  192. API_EXPORT void API_CALL mk_media_input_yuv(mk_media ctx, const char *yuv[3], int linesize[3], uint64_t cts) {
  193. assert(ctx && yuv && linesize);
  194. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  195. (*obj)->getChannel()->inputYUV((char **) yuv, linesize, cts);
  196. }
  197. API_EXPORT int API_CALL mk_media_input_aac(mk_media ctx, const void *data, int len, uint64_t dts, void *adts) {
  198. assert(ctx && data && len > 0 && adts);
  199. MediaHelper::Ptr *obj = (MediaHelper::Ptr *) ctx;
  200. return (*obj)->getChannel()->inputAAC((const char *) data, len, dts, (char *) adts);
  201. }
  202. API_EXPORT int API_CALL mk_media_input_pcm(mk_media ctx, void *data , int len, uint64_t pts){
  203. assert(ctx && data && len > 0);
  204. MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
  205. return (*obj)->getChannel()->inputPCM((char*)data, len, pts);
  206. }
  207. API_EXPORT int API_CALL mk_media_input_audio(mk_media ctx, const void* data, int len, uint64_t dts){
  208. assert(ctx && data && len > 0);
  209. MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
  210. return (*obj)->getChannel()->inputAudio((const char*)data, len, dts);
  211. }
  212. API_EXPORT void API_CALL mk_media_start_send_rtp(mk_media ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_send_rtp_result cb, void *user_data){
  213. assert(ctx && dst_url && ssrc);
  214. MediaHelper::Ptr* obj = (MediaHelper::Ptr*) ctx;
  215. MediaSourceEvent::SendRtpArgs args;
  216. args.dst_url = dst_url;
  217. args.dst_port = dst_port;
  218. args.ssrc = ssrc;
  219. args.is_udp = is_udp;
  220. // sender参数无用
  221. auto ref = *obj;
  222. (*obj)->getChannel()->getOwnerPoller(MediaSource::NullMediaSource())->async([args, ref, cb, user_data]() {
  223. ref->getChannel()->startSendRtp(MediaSource::NullMediaSource(), args, [cb, user_data](uint16_t local_port, const SockException &ex) {
  224. if (cb) {
  225. cb(user_data, local_port, ex.getErrCode(), ex.what());
  226. }
  227. });
  228. });
  229. }
  230. API_EXPORT void API_CALL mk_media_stop_send_rtp(mk_media ctx, const char *ssrc){
  231. assert(ctx);
  232. MediaHelper::Ptr *obj = (MediaHelper::Ptr *)ctx;
  233. // sender参数无用
  234. auto ref = *obj;
  235. string ssrc_str = ssrc ? ssrc : "";
  236. (*obj)->getChannel()->getOwnerPoller(MediaSource::NullMediaSource())->async([ref, ssrc_str]() {
  237. ref->getChannel()->stopSendRtp(MediaSource::NullMediaSource(), ssrc_str);
  238. });
  239. }
  240. API_EXPORT mk_thread API_CALL mk_media_get_owner_thread(mk_media ctx) {
  241. MediaHelper::Ptr *obj = (MediaHelper::Ptr *)ctx;
  242. return (*obj)->getChannel()->getOwnerPoller(MediaSource::NullMediaSource()).get();
  243. }