25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

447 lines
17KB

  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 <assert.h>
  11. #include "mk_events_objects.h"
  12. #include "Common/config.h"
  13. #include "Record/MP4Recorder.h"
  14. #include "Http/HttpSession.h"
  15. #include "Http/HttpBody.h"
  16. #include "Http/HttpClient.h"
  17. #include "Rtsp/RtspSession.h"
  18. using namespace toolkit;
  19. using namespace mediakit;
  20. ///////////////////////////////////////////RecordInfo/////////////////////////////////////////////
  21. API_EXPORT uint64_t API_CALL mk_mp4_info_get_start_time(const mk_mp4_info ctx){
  22. assert(ctx);
  23. RecordInfo *info = (RecordInfo *)ctx;
  24. return info->start_time;
  25. }
  26. API_EXPORT float API_CALL mk_mp4_info_get_time_len(const mk_mp4_info ctx){
  27. assert(ctx);
  28. RecordInfo *info = (RecordInfo *)ctx;
  29. return info->time_len;
  30. }
  31. API_EXPORT size_t API_CALL mk_mp4_info_get_file_size(const mk_mp4_info ctx){
  32. assert(ctx);
  33. RecordInfo *info = (RecordInfo *)ctx;
  34. return info->file_size;
  35. }
  36. API_EXPORT const char* API_CALL mk_mp4_info_get_file_path(const mk_mp4_info ctx){
  37. assert(ctx);
  38. RecordInfo *info = (RecordInfo *)ctx;
  39. return info->file_path.c_str();
  40. }
  41. API_EXPORT const char* API_CALL mk_mp4_info_get_file_name(const mk_mp4_info ctx){
  42. assert(ctx);
  43. RecordInfo *info = (RecordInfo *)ctx;
  44. return info->file_name.c_str();
  45. }
  46. API_EXPORT const char* API_CALL mk_mp4_info_get_folder(const mk_mp4_info ctx){
  47. assert(ctx);
  48. RecordInfo *info = (RecordInfo *)ctx;
  49. return info->folder.c_str();
  50. }
  51. API_EXPORT const char* API_CALL mk_mp4_info_get_url(const mk_mp4_info ctx){
  52. assert(ctx);
  53. RecordInfo *info = (RecordInfo *)ctx;
  54. return info->url.c_str();
  55. }
  56. API_EXPORT const char* API_CALL mk_mp4_info_get_vhost(const mk_mp4_info ctx){
  57. assert(ctx);
  58. RecordInfo *info = (RecordInfo *)ctx;
  59. return info->vhost.c_str();
  60. }
  61. API_EXPORT const char* API_CALL mk_mp4_info_get_app(const mk_mp4_info ctx){
  62. assert(ctx);
  63. RecordInfo *info = (RecordInfo *)ctx;
  64. return info->app.c_str();
  65. }
  66. API_EXPORT const char* API_CALL mk_mp4_info_get_stream(const mk_mp4_info ctx){
  67. assert(ctx);
  68. RecordInfo *info = (RecordInfo *)ctx;
  69. return info->stream.c_str();
  70. }
  71. ///////////////////////////////////////////Parser/////////////////////////////////////////////
  72. API_EXPORT const char* API_CALL mk_parser_get_method(const mk_parser ctx){
  73. assert(ctx);
  74. Parser *parser = (Parser *)ctx;
  75. return parser->Method().c_str();
  76. }
  77. API_EXPORT const char* API_CALL mk_parser_get_url(const mk_parser ctx){
  78. assert(ctx);
  79. Parser *parser = (Parser *)ctx;
  80. return parser->Url().c_str();
  81. }
  82. API_EXPORT const char* API_CALL mk_parser_get_url_params(const mk_parser ctx){
  83. assert(ctx);
  84. Parser *parser = (Parser *)ctx;
  85. return parser->Params().c_str();
  86. }
  87. API_EXPORT const char* API_CALL mk_parser_get_url_param(const mk_parser ctx,const char *key){
  88. assert(ctx && key);
  89. Parser *parser = (Parser *)ctx;
  90. return parser->getUrlArgs()[key].c_str();
  91. }
  92. API_EXPORT const char* API_CALL mk_parser_get_tail(const mk_parser ctx){
  93. assert(ctx);
  94. Parser *parser = (Parser *)ctx;
  95. return parser->Tail().c_str();
  96. }
  97. API_EXPORT const char* API_CALL mk_parser_get_header(const mk_parser ctx,const char *key){
  98. assert(ctx && key);
  99. Parser *parser = (Parser *)ctx;
  100. return parser->getHeader()[key].c_str();
  101. }
  102. API_EXPORT const char* API_CALL mk_parser_get_content(const mk_parser ctx, size_t *length){
  103. assert(ctx);
  104. Parser *parser = (Parser *)ctx;
  105. if(length){
  106. *length = parser->Content().size();
  107. }
  108. return parser->Content().c_str();
  109. }
  110. ///////////////////////////////////////////MediaInfo/////////////////////////////////////////////
  111. API_EXPORT const char* API_CALL mk_media_info_get_params(const mk_media_info ctx){
  112. assert(ctx);
  113. MediaInfo *info = (MediaInfo *)ctx;
  114. return info->_param_strs.c_str();
  115. }
  116. API_EXPORT const char* API_CALL mk_media_info_get_schema(const mk_media_info ctx){
  117. assert(ctx);
  118. MediaInfo *info = (MediaInfo *)ctx;
  119. return info->_schema.c_str();
  120. }
  121. API_EXPORT const char* API_CALL mk_media_info_get_vhost(const mk_media_info ctx){
  122. assert(ctx);
  123. MediaInfo *info = (MediaInfo *)ctx;
  124. return info->_vhost.c_str();
  125. }
  126. API_EXPORT const char* API_CALL mk_media_info_get_host(const mk_media_info ctx){
  127. assert(ctx);
  128. MediaInfo *info = (MediaInfo *)ctx;
  129. return info->_host.c_str();
  130. }
  131. API_EXPORT uint16_t API_CALL mk_media_info_get_port(const mk_media_info ctx){
  132. assert(ctx);
  133. MediaInfo *info = (MediaInfo *)ctx;
  134. return info->_port;
  135. }
  136. API_EXPORT const char* API_CALL mk_media_info_get_app(const mk_media_info ctx){
  137. assert(ctx);
  138. MediaInfo *info = (MediaInfo *)ctx;
  139. return info->_app.c_str();
  140. }
  141. API_EXPORT const char* API_CALL mk_media_info_get_stream(const mk_media_info ctx){
  142. assert(ctx);
  143. MediaInfo *info = (MediaInfo *)ctx;
  144. return info->_streamid.c_str();
  145. }
  146. ///////////////////////////////////////////MediaSource/////////////////////////////////////////////
  147. API_EXPORT const char* API_CALL mk_media_source_get_schema(const mk_media_source ctx){
  148. assert(ctx);
  149. MediaSource *src = (MediaSource *)ctx;
  150. return src->getSchema().c_str();
  151. }
  152. API_EXPORT const char* API_CALL mk_media_source_get_vhost(const mk_media_source ctx){
  153. assert(ctx);
  154. MediaSource *src = (MediaSource *)ctx;
  155. return src->getVhost().c_str();
  156. }
  157. API_EXPORT const char* API_CALL mk_media_source_get_app(const mk_media_source ctx){
  158. assert(ctx);
  159. MediaSource *src = (MediaSource *)ctx;
  160. return src->getApp().c_str();
  161. }
  162. API_EXPORT const char* API_CALL mk_media_source_get_stream(const mk_media_source ctx){
  163. assert(ctx);
  164. MediaSource *src = (MediaSource *)ctx;
  165. return src->getId().c_str();
  166. }
  167. API_EXPORT int API_CALL mk_media_source_get_reader_count(const mk_media_source ctx){
  168. assert(ctx);
  169. MediaSource *src = (MediaSource *)ctx;
  170. return src->readerCount();
  171. }
  172. API_EXPORT int API_CALL mk_media_source_get_total_reader_count(const mk_media_source ctx){
  173. assert(ctx);
  174. MediaSource *src = (MediaSource *)ctx;
  175. return src->totalReaderCount();
  176. }
  177. API_EXPORT int API_CALL mk_media_source_close(const mk_media_source ctx,int force){
  178. assert(ctx);
  179. MediaSource *src = (MediaSource *)ctx;
  180. return src->close(force);
  181. }
  182. API_EXPORT int API_CALL mk_media_source_seek_to(const mk_media_source ctx,uint32_t stamp){
  183. assert(ctx);
  184. MediaSource *src = (MediaSource *)ctx;
  185. return src->seekTo(stamp);
  186. }
  187. API_EXPORT void API_CALL mk_media_source_start_send_rtp(const mk_media_source ctx, const char *dst_url, uint16_t dst_port, const char *ssrc, int is_udp, on_mk_media_source_send_rtp_result cb, void *user_data){
  188. assert(ctx && dst_url && ssrc);
  189. MediaSource *src = (MediaSource *)ctx;
  190. MediaSourceEvent::SendRtpArgs args;
  191. args.dst_url = dst_url;
  192. args.dst_port = dst_port;
  193. args.ssrc = ssrc;
  194. args.is_udp = is_udp;
  195. src->startSendRtp(args, [cb, user_data](uint16_t local_port, const SockException &ex){
  196. if (cb) {
  197. cb(user_data, local_port, ex.getErrCode(), ex.what());
  198. }
  199. });
  200. }
  201. API_EXPORT int API_CALL mk_media_source_stop_send_rtp(const mk_media_source ctx){
  202. assert(ctx);
  203. MediaSource *src = (MediaSource *) ctx;
  204. return src->stopSendRtp("");
  205. }
  206. API_EXPORT void API_CALL mk_media_source_find(const char *schema,
  207. const char *vhost,
  208. const char *app,
  209. const char *stream,
  210. int from_mp4,
  211. void *user_data,
  212. on_mk_media_source_find_cb cb) {
  213. assert(schema && vhost && app && stream && cb);
  214. auto src = MediaSource::find(schema, vhost, app, stream, from_mp4);
  215. cb(user_data, src.get());
  216. }
  217. API_EXPORT void API_CALL mk_media_source_for_each(void *user_data, on_mk_media_source_find_cb cb, const char *schema,
  218. const char *vhost, const char *app, const char *stream) {
  219. assert(cb);
  220. MediaSource::for_each_media([&](const MediaSource::Ptr &src) {
  221. cb(user_data, src.get());
  222. }, schema ? schema : "", vhost ? vhost : "", app ? app : "", stream ? stream : "");
  223. }
  224. ///////////////////////////////////////////HttpBody/////////////////////////////////////////////
  225. API_EXPORT mk_http_body API_CALL mk_http_body_from_string(const char *str, size_t len){
  226. assert(str);
  227. if(!len){
  228. len = strlen(str);
  229. }
  230. return new HttpBody::Ptr(new HttpStringBody(std::string(str, len)));
  231. }
  232. API_EXPORT mk_http_body API_CALL mk_http_body_from_file(const char *file_path){
  233. assert(file_path);
  234. return new HttpBody::Ptr(new HttpFileBody(file_path));
  235. }
  236. template <typename C = StrCaseMap>
  237. static C get_http_header( const char *response_header[]){
  238. C header;
  239. for (int i = 0; response_header[i] != NULL;) {
  240. auto key = response_header[i];
  241. auto value = response_header[i + 1];
  242. if (key && value) {
  243. i += 2;
  244. header.emplace(key,value);
  245. continue;
  246. }
  247. break;
  248. }
  249. return header;
  250. }
  251. API_EXPORT mk_http_body API_CALL mk_http_body_from_multi_form(const char *key_val[],const char *file_path){
  252. assert(key_val && file_path);
  253. return new HttpBody::Ptr(new HttpMultiFormBody(get_http_header<HttpArgs>(key_val),file_path));
  254. }
  255. API_EXPORT void API_CALL mk_http_body_release(mk_http_body ctx){
  256. assert(ctx);
  257. HttpBody::Ptr *ptr = (HttpBody::Ptr *)ctx;
  258. delete ptr;
  259. }
  260. ///////////////////////////////////////////HttpResponseInvoker/////////////////////////////////////////////
  261. API_EXPORT void API_CALL mk_http_response_invoker_do_string(const mk_http_response_invoker ctx,
  262. int response_code,
  263. const char **response_header,
  264. const char *response_content){
  265. assert(ctx && response_code && response_header && response_content);
  266. auto header = get_http_header(response_header);
  267. HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx;
  268. (*invoker)(response_code,header,response_content);
  269. }
  270. API_EXPORT void API_CALL mk_http_response_invoker_do_file(const mk_http_response_invoker ctx,
  271. const mk_parser request_parser,
  272. const char *response_header[],
  273. const char *response_file_path){
  274. assert(ctx && request_parser && response_header && response_file_path);
  275. auto header = get_http_header(response_header);
  276. HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx;
  277. (*invoker).responseFile(((Parser *) (request_parser))->getHeader(), header, response_file_path);
  278. }
  279. API_EXPORT void API_CALL mk_http_response_invoker_do(const mk_http_response_invoker ctx,
  280. int response_code,
  281. const char **response_header,
  282. const mk_http_body response_body){
  283. assert(ctx && response_code && response_header && response_body);
  284. auto header = get_http_header(response_header);
  285. HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx;
  286. HttpBody::Ptr *body = (HttpBody::Ptr*) response_body;
  287. (*invoker)(response_code,header,*body);
  288. }
  289. API_EXPORT mk_http_response_invoker API_CALL mk_http_response_invoker_clone(const mk_http_response_invoker ctx){
  290. assert(ctx);
  291. HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx;
  292. return new HttpSession::HttpResponseInvoker (*invoker);
  293. }
  294. API_EXPORT void API_CALL mk_http_response_invoker_clone_release(const mk_http_response_invoker ctx){
  295. assert(ctx);
  296. HttpSession::HttpResponseInvoker *invoker = (HttpSession::HttpResponseInvoker *)ctx;
  297. delete invoker;
  298. }
  299. ///////////////////////////////////////////HttpAccessPathInvoker/////////////////////////////////////////////
  300. API_EXPORT void API_CALL mk_http_access_path_invoker_do(const mk_http_access_path_invoker ctx,
  301. const char *err_msg,
  302. const char *access_path,
  303. int cookie_life_second){
  304. assert(ctx);
  305. HttpSession::HttpAccessPathInvoker *invoker = (HttpSession::HttpAccessPathInvoker *)ctx;
  306. (*invoker)(err_msg ? err_msg : "",
  307. access_path? access_path : "",
  308. cookie_life_second);
  309. }
  310. API_EXPORT mk_http_access_path_invoker API_CALL mk_http_access_path_invoker_clone(const mk_http_access_path_invoker ctx){
  311. assert(ctx);
  312. HttpSession::HttpAccessPathInvoker *invoker = (HttpSession::HttpAccessPathInvoker *)ctx;
  313. return new HttpSession::HttpAccessPathInvoker(*invoker);
  314. }
  315. API_EXPORT void API_CALL mk_http_access_path_invoker_clone_release(const mk_http_access_path_invoker ctx){
  316. assert(ctx);
  317. HttpSession::HttpAccessPathInvoker *invoker = (HttpSession::HttpAccessPathInvoker *)ctx;
  318. delete invoker;
  319. }
  320. ///////////////////////////////////////////RtspSession::onGetRealm/////////////////////////////////////////////
  321. API_EXPORT void API_CALL mk_rtsp_get_realm_invoker_do(const mk_rtsp_get_realm_invoker ctx,
  322. const char *realm){
  323. assert(ctx);
  324. RtspSession::onGetRealm *invoker = (RtspSession::onGetRealm *)ctx;
  325. (*invoker)(realm ? realm : "");
  326. }
  327. API_EXPORT mk_rtsp_get_realm_invoker API_CALL mk_rtsp_get_realm_invoker_clone(const mk_rtsp_get_realm_invoker ctx){
  328. assert(ctx);
  329. RtspSession::onGetRealm *invoker = (RtspSession::onGetRealm *)ctx;
  330. return new RtspSession::onGetRealm (*invoker);
  331. }
  332. API_EXPORT void API_CALL mk_rtsp_get_realm_invoker_clone_release(const mk_rtsp_get_realm_invoker ctx){
  333. assert(ctx);
  334. RtspSession::onGetRealm *invoker = (RtspSession::onGetRealm *)ctx;
  335. delete invoker;
  336. }
  337. ///////////////////////////////////////////RtspSession::onAuth/////////////////////////////////////////////
  338. API_EXPORT void API_CALL mk_rtsp_auth_invoker_do(const mk_rtsp_auth_invoker ctx,
  339. int encrypted,
  340. const char *pwd_or_md5){
  341. assert(ctx);
  342. RtspSession::onAuth *invoker = (RtspSession::onAuth *)ctx;
  343. (*invoker)(encrypted, pwd_or_md5 ? pwd_or_md5 : "");
  344. }
  345. API_EXPORT mk_rtsp_auth_invoker API_CALL mk_rtsp_auth_invoker_clone(const mk_rtsp_auth_invoker ctx){
  346. assert(ctx);
  347. RtspSession::onAuth *invoker = (RtspSession::onAuth *)ctx;
  348. return new RtspSession::onAuth(*invoker);
  349. }
  350. API_EXPORT void API_CALL mk_rtsp_auth_invoker_clone_release(const mk_rtsp_auth_invoker ctx){
  351. assert(ctx);
  352. RtspSession::onAuth *invoker = (RtspSession::onAuth *)ctx;
  353. delete invoker;
  354. }
  355. ///////////////////////////////////////////Broadcast::PublishAuthInvoker/////////////////////////////////////////////
  356. API_EXPORT void API_CALL mk_publish_auth_invoker_do(const mk_publish_auth_invoker ctx,
  357. const char *err_msg,
  358. int enable_hls,
  359. int enable_mp4){
  360. assert(ctx);
  361. Broadcast::PublishAuthInvoker *invoker = (Broadcast::PublishAuthInvoker *)ctx;
  362. ProtocolOption option;
  363. option.enable_hls = enable_hls;
  364. option.enable_mp4 = enable_mp4;
  365. (*invoker)(err_msg ? err_msg : "", option);
  366. }
  367. API_EXPORT mk_publish_auth_invoker API_CALL mk_publish_auth_invoker_clone(const mk_publish_auth_invoker ctx){
  368. assert(ctx);
  369. Broadcast::PublishAuthInvoker *invoker = (Broadcast::PublishAuthInvoker *)ctx;
  370. return new Broadcast::PublishAuthInvoker(*invoker);
  371. }
  372. API_EXPORT void API_CALL mk_publish_auth_invoker_clone_release(const mk_publish_auth_invoker ctx){
  373. assert(ctx);
  374. Broadcast::PublishAuthInvoker *invoker = (Broadcast::PublishAuthInvoker *)ctx;
  375. delete invoker;
  376. }
  377. ///////////////////////////////////////////Broadcast::AuthInvoker/////////////////////////////////////////////
  378. API_EXPORT void API_CALL mk_auth_invoker_do(const mk_auth_invoker ctx, const char *err_msg){
  379. assert(ctx);
  380. Broadcast::AuthInvoker *invoker = (Broadcast::AuthInvoker *)ctx;
  381. (*invoker)(err_msg ? err_msg : "");
  382. }
  383. API_EXPORT mk_auth_invoker API_CALL mk_auth_invoker_clone(const mk_auth_invoker ctx){
  384. assert(ctx);
  385. Broadcast::AuthInvoker *invoker = (Broadcast::AuthInvoker *)ctx;
  386. return new Broadcast::AuthInvoker(*invoker);
  387. }
  388. API_EXPORT void API_CALL mk_auth_invoker_clone_release(const mk_auth_invoker ctx){
  389. assert(ctx);
  390. Broadcast::AuthInvoker *invoker = (Broadcast::AuthInvoker *)ctx;
  391. delete invoker;
  392. }