Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

426 wiersze
11KB

  1. #include "rtsp-muxer.h"
  2. #include "rtp-profile.h"
  3. #include "rtp-payload.h"
  4. #include "rtcp-header.h"
  5. #include "rtp.h"
  6. #include "sdp-a-fmtp.h"
  7. #include "mpeg-ps.h"
  8. #include "mpeg-ts.h"
  9. #include "avbsf.h" // https://github.com/ireader/avcodec
  10. #include "mpeg4-aac.h"
  11. #include "rtp-sender.h"
  12. #include "rtsp-payloads.h"
  13. #include <errno.h>
  14. #include <stdio.h>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <assert.h>
  18. #include <stdint.h>
  19. #include <inttypes.h>
  20. #if defined(OS_WINDOWS)
  21. #if !defined(strcasecmp)
  22. #define strcasecmp _stricmp
  23. #endif
  24. #endif
  25. uint64_t rtpclock(void);
  26. struct rtp_muxer_media_t;
  27. struct rtp_muxer_payload_t;
  28. typedef int (*media_input)(struct rtp_muxer_media_t* m, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes);
  29. struct rtp_muxer_payload_t
  30. {
  31. struct rtsp_muxer_t* muxer;
  32. struct rtp_sender_t rtp;
  33. int pid;
  34. int port;
  35. uint64_t clock; // rtcp clock
  36. struct ps_muxer_t* ps;
  37. void* ts; // ts muxer
  38. int64_t timestamp; // ps/ts only
  39. const char* sdp;
  40. int len; // sdp size
  41. };
  42. struct rtp_muxer_media_t
  43. {
  44. int codec;
  45. int stream; // ps/ts only
  46. struct rtp_muxer_payload_t* pt;
  47. media_input input;
  48. struct avbsf_t* bsf;
  49. void* filter;
  50. };
  51. struct rtsp_muxer_t
  52. {
  53. struct rtp_muxer_payload_t payloads[3];
  54. int payload_capacity;
  55. int payload_count;
  56. struct rtp_muxer_media_t medias[4];
  57. int media_capacity;
  58. int media_count;
  59. rtsp_muxer_onpacket onpacket;
  60. void* param;
  61. uint8_t ptr[1024 * 1024];
  62. int off;
  63. };
  64. static void* rtsp_muxer_ts_alloc(void* param, size_t bytes)
  65. {
  66. struct rtp_muxer_payload_t* pt;
  67. pt = (struct rtp_muxer_payload_t*)param;
  68. if (bytes > sizeof(pt->muxer->ptr) - pt->muxer->off)
  69. {
  70. assert(0);
  71. return NULL;
  72. }
  73. return pt->muxer->ptr + pt->muxer->off;
  74. }
  75. static void rtsp_muxer_ts_free(void* param, void* packet)
  76. {
  77. (void)param, (void)packet;
  78. }
  79. static int rtsp_muxer_ts_write(void* param, const void* packet, size_t bytes)
  80. {
  81. struct rtp_muxer_payload_t* pt;
  82. pt = (struct rtp_muxer_payload_t*)param;
  83. return rtp_payload_encode_input(pt->rtp.encoder, packet, (int)bytes, (uint32_t)pt->timestamp);
  84. }
  85. static int rtsp_muxer_ps_write(void* param, int stream, void* packet, size_t bytes)
  86. {
  87. struct rtp_muxer_payload_t* pt;
  88. (void)stream;
  89. pt = (struct rtp_muxer_payload_t*)param;
  90. return rtp_payload_encode_input(pt->rtp.encoder, packet, (int)bytes, (uint32_t)pt->timestamp);
  91. }
  92. static int rtsp_muxer_rtp_encode_packet(void* param, const void* packet, int bytes, uint32_t timestamp, int flags)
  93. {
  94. struct rtp_muxer_payload_t* pt;
  95. pt = (struct rtp_muxer_payload_t*)param;
  96. return pt->muxer->onpacket(pt->muxer->param, pt->pid, packet, bytes, timestamp, flags);
  97. }
  98. static int rtsp_muxer_ts_input(struct rtp_muxer_media_t* m, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes)
  99. {
  100. m->pt->timestamp = pts * 90; // last dts
  101. return mpeg_ts_write(m->pt->ts, m->stream, flags ? 0x01 : 0, pts * 90, dts * 90, data, bytes);
  102. }
  103. static int rtsp_muxer_ps_input(struct rtp_muxer_media_t* m, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes)
  104. {
  105. m->pt->timestamp = pts * 90; // last dts
  106. return ps_muxer_input(m->pt->ps, m->stream, flags ? 0x01 : 0, pts * 90, dts * 90, data, bytes);
  107. }
  108. static int rtsp_muxer_av_input(struct rtp_muxer_media_t* m, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes)
  109. {
  110. (void)flags, (void)dts; // TODO: rtp timestamp map PTS
  111. return rtp_payload_encode_input(m->pt->rtp.encoder, data, (int)bytes, (uint32_t)(pts * m->pt->rtp.frequency / 1000));
  112. }
  113. static int rtsp_muxer_bsf_onpacket(void* param, int64_t pts, int64_t dts, const uint8_t* data, int bytes, int flags)
  114. {
  115. struct rtp_muxer_media_t* m;
  116. m = (struct rtp_muxer_media_t*)param;
  117. return m->input(m, flags, pts, dts, data, bytes);
  118. }
  119. static int rtsp_muxer_payload_close(struct rtp_muxer_payload_t* pt)
  120. {
  121. if (pt->ps)
  122. {
  123. ps_muxer_destroy(pt->ps);
  124. pt->ps = NULL;
  125. }
  126. if (pt->ts)
  127. {
  128. mpeg_ts_destroy(pt->ts);
  129. pt->ts = NULL;
  130. }
  131. rtp_sender_destroy(&pt->rtp);
  132. return 0;
  133. }
  134. struct rtsp_muxer_t* rtsp_muxer_create(rtsp_muxer_onpacket onpacket, void* param)
  135. {
  136. struct rtsp_muxer_t* muxer;
  137. muxer = calloc(1, sizeof(*muxer));
  138. if (!muxer)
  139. return NULL;
  140. muxer->payload_capacity = sizeof(muxer->payloads) / sizeof(muxer->payloads[0]);
  141. muxer->media_capacity = sizeof(muxer->medias) / sizeof(muxer->medias[0]);
  142. muxer->onpacket = onpacket;
  143. muxer->param = param;
  144. return muxer;
  145. }
  146. int rtsp_muxer_destroy(struct rtsp_muxer_t* muxer)
  147. {
  148. int i;
  149. struct rtp_muxer_media_t* m;
  150. struct rtp_muxer_payload_t* pt;
  151. if (muxer)
  152. {
  153. for (i = 0; i < muxer->media_count; i++)
  154. {
  155. m = &muxer->medias[i];
  156. if (m->bsf && m->filter)
  157. m->bsf->destroy(&m->filter);
  158. }
  159. for (i = 0; i < muxer->payload_count; i++)
  160. {
  161. pt = &muxer->payloads[i];
  162. rtsp_muxer_payload_close(pt);
  163. }
  164. free(muxer);
  165. }
  166. return 0;
  167. }
  168. int rtsp_muxer_add_payload(struct rtsp_muxer_t* muxer, const char* proto, int frequence, int payload, const char* encoding, uint16_t seq, uint32_t ssrc, uint16_t port, const void* extra, int size)
  169. {
  170. int r = 0;
  171. struct rtp_muxer_payload_t* pt;
  172. if (muxer->payload_count >= muxer->payload_capacity)
  173. return -E2BIG;
  174. pt = &muxer->payloads[muxer->payload_count];
  175. memset(pt, 0, sizeof(*pt));
  176. pt->port = port;
  177. pt->pid = muxer->payload_count;
  178. pt->muxer = muxer;
  179. pt->rtp.seq = seq;
  180. pt->rtp.ssrc = ssrc;
  181. pt->rtp.onpacket = rtsp_muxer_rtp_encode_packet;
  182. pt->rtp.param = pt;
  183. if (RTP_PAYLOAD_MP2T == payload)
  184. {
  185. struct mpeg_ts_func_t h;
  186. h.alloc = rtsp_muxer_ts_alloc;
  187. h.write = rtsp_muxer_ts_write;
  188. h.free = rtsp_muxer_ts_free;
  189. for (r = 0; r < muxer->payload_count; r++)
  190. {
  191. if (muxer->payloads[r].ts)
  192. return muxer->payloads[r].pid; // exist
  193. }
  194. pt->ts = mpeg_ts_create(&h, pt);
  195. r = rtp_sender_init_video(&pt->rtp, proto, port, payload, encoding, 90000, extra, size);
  196. }
  197. else if (0 == strcasecmp(encoding, "MP2P") || 0 == strcasecmp(encoding, "PS"))
  198. {
  199. struct ps_muxer_func_t h;
  200. h.alloc = rtsp_muxer_ts_alloc;
  201. h.write = rtsp_muxer_ps_write;
  202. h.free = rtsp_muxer_ts_free;
  203. for (r = 0; r < muxer->payload_count; r++)
  204. {
  205. if (muxer->payloads[r].ps)
  206. return muxer->payloads[r].pid; // exist
  207. }
  208. pt->ps = ps_muxer_create(&h, pt);
  209. r = rtp_sender_init_video(&pt->rtp, proto, port, payload, encoding, 90000, extra, size);
  210. }
  211. else
  212. {
  213. if (0 == strcasecmp(encoding, "H264")
  214. || 0 == strcasecmp(encoding, "H265") || 0 == strcasecmp(encoding, "HEVC")
  215. || 0 == strcasecmp(encoding, "VP8") || 0 == strcasecmp(encoding, "VP9")
  216. || 0 == strcasecmp(encoding, "AV1")
  217. || 0 == strcasecmp(encoding, "MP4V-ES"))
  218. {
  219. r = rtp_sender_init_video(&pt->rtp, proto, port, payload, encoding, frequence ? frequence : 90000, extra, size);
  220. }
  221. else if (RTP_PAYLOAD_PCMU == payload || RTP_PAYLOAD_PCMA == payload || 0 == strcasecmp(encoding, "MP4A-LATM") || 0 == strcasecmp(encoding, "MPEG4-GENERIC") || 0 == strcasecmp(encoding, "opus"))
  222. {
  223. r = rtp_sender_init_audio(&pt->rtp, proto, port, payload, encoding, frequence, 0 /*from extra*/, extra, size);
  224. }
  225. else
  226. {
  227. assert(0);
  228. return -EPROTONOSUPPORT;
  229. }
  230. }
  231. if (r < 0 || r > (int)sizeof(muxer->ptr) - muxer->off)
  232. {
  233. rtsp_muxer_payload_close(pt);
  234. return -1;
  235. }
  236. // copy sdp
  237. pt->len = r;
  238. pt->sdp = (const char*)muxer->ptr + muxer->off;
  239. memcpy(muxer->ptr + muxer->off, pt->rtp.buffer, r);
  240. muxer->payload_count++;
  241. muxer->off += r;
  242. return pt->pid;
  243. }
  244. int rtsp_muxer_add_media(struct rtsp_muxer_t* muxer, int pid, int codec, const void* extra, int size)
  245. {
  246. int mpeg2;
  247. struct rtp_muxer_media_t* m;
  248. if (muxer->media_count >= muxer->media_capacity)
  249. return -E2BIG;
  250. if (pid < 0 || pid >= muxer->payload_count)
  251. return -ENOENT;
  252. mpeg2 = avpayload_find_by_payload((uint8_t)codec);
  253. if (mpeg2 < 0)
  254. return -EPROTONOSUPPORT;
  255. m = &muxer->medias[muxer->media_count];
  256. memset(m, 0, sizeof(*m));
  257. m->pt = &muxer->payloads[pid];
  258. m->codec = codec;
  259. switch (codec)
  260. {
  261. case RTP_PAYLOAD_H264:
  262. //m->bsf = avbsf_h264();
  263. break;
  264. case RTP_PAYLOAD_H265:
  265. //m->bsf = avbsf_h265();
  266. break;
  267. case RTP_PAYLOAD_MP4A:
  268. //m->bsf = avbsf_aac();
  269. break;
  270. default:
  271. // TODO: opus/g711
  272. ;
  273. }
  274. if (m->bsf)
  275. m->filter = m->bsf->create(extra, size, rtsp_muxer_bsf_onpacket, m);
  276. if (m->bsf && !m->filter)
  277. {
  278. assert(0);
  279. return -1;
  280. }
  281. if (RTP_PAYLOAD_MP2T == m->pt->rtp.payload)
  282. {
  283. m->stream = mpeg_ts_add_stream(m->pt->ts, s_payloads[mpeg2].mpeg2, NULL, 0);
  284. m->input = rtsp_muxer_ts_input;
  285. }
  286. else if (0 == strcasecmp(m->pt->rtp.encoding, "MP2P") || 0 == strcasecmp(m->pt->rtp.encoding, "PS"))
  287. {
  288. m->stream = ps_muxer_add_stream(m->pt->ps, s_payloads[mpeg2].mpeg2, NULL, 0);
  289. m->input = rtsp_muxer_ps_input;
  290. }
  291. else
  292. {
  293. m->stream = 0; // unuse
  294. m->input = rtsp_muxer_av_input;
  295. }
  296. if (m->stream < 0)
  297. {
  298. assert(0);
  299. return -1;
  300. }
  301. return muxer->media_count++;
  302. }
  303. int rtsp_muxer_getinfo(struct rtsp_muxer_t* muxer, int pid, uint16_t* seq, uint32_t* timestamp, const char** sdp, int* size)
  304. {
  305. struct rtp_muxer_payload_t* pt;
  306. if (pid < 0 || pid >= muxer->payload_count)
  307. return -ENOENT;
  308. pt = &muxer->payloads[pid];
  309. *sdp = pt->sdp;
  310. *size = pt->len;
  311. rtp_payload_encode_getinfo(pt->rtp.rtp, seq, timestamp);
  312. return 0;
  313. }
  314. int rtsp_muxer_input(struct rtsp_muxer_t* muxer, int mid, int64_t pts, int64_t dts, const void* data, int bytes, int flags)
  315. {
  316. int r;
  317. struct rtp_muxer_media_t* m;
  318. if (mid < 0 || mid >= muxer->media_count)
  319. return -ENOENT;
  320. m = &muxer->medias[mid];
  321. if (m->bsf && m->filter)
  322. r = m->bsf->input(m->filter, pts, dts, (const uint8_t*)data, bytes);
  323. else
  324. r = m->input(m, flags, pts, dts, data, bytes);
  325. return r;
  326. }
  327. int rtsp_muxer_rtcp(struct rtsp_muxer_t* muxer, int pid, void* buf, int len)
  328. {
  329. int r;
  330. int interval;
  331. uint64_t clock;
  332. struct rtp_muxer_payload_t* pt;
  333. if (pid < 0 || pid >= muxer->payload_count)
  334. return -ENOENT;
  335. pt = &muxer->payloads[pid];
  336. r = 0;
  337. clock = rtpclock();
  338. interval = rtp_rtcp_interval(pt->rtp.rtp);
  339. if (pt->clock + (uint64_t)interval * 1000 < clock)
  340. {
  341. // RTCP report
  342. r = rtp_rtcp_report(pt->rtp.rtp, buf, len);
  343. pt->clock = clock;
  344. }
  345. return r;
  346. }
  347. int rtsp_muxer_onrtcp(struct rtsp_muxer_t* muxer, int pid, const void* buf, int len)
  348. {
  349. struct rtp_muxer_payload_t* pt;
  350. if (pid < 0 || pid >= muxer->payload_count)
  351. return -ENOENT;
  352. pt = &muxer->payloads[pid];
  353. return rtp_onreceived_rtcp(pt->rtp.rtp, buf, len);
  354. }