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.

575 lines
16KB

  1. #include "rtsp-demuxer.h"
  2. #include "rtp-demuxer.h"
  3. #include "rtp-profile.h"
  4. #include "rtcp-header.h"
  5. #include "sdp-a-fmtp.h"
  6. #include "mpeg-ps.h"
  7. #include "mpeg-ts.h"
  8. #include "avbsf.h"
  9. #include "avpbs.h" // https://github.com/ireader/avcodec #avbsf/include
  10. #include "mpeg4-aac.h"
  11. #include "rtsp-payloads.h"
  12. #include <stdlib.h>
  13. #include <string.h>
  14. #include <assert.h>
  15. #include <stdio.h>
  16. #include <errno.h>
  17. #define RTSP_STREAM_MAX 4
  18. #define RTSP_PAYLOAD_MAX 8
  19. #define RTSP_PAYLOAD_MAX_SIZE (10 * 1024 * 1024)
  20. #if defined(OS_WINDOWS)
  21. #if !defined(strcasecmp)
  22. #define strcasecmp _stricmp
  23. #endif
  24. #endif
  25. struct rtsp_demuxer_t;
  26. struct rtp_payload_info_t
  27. {
  28. struct rtsp_demuxer_t* ctx;
  29. int frequency;
  30. int payload;
  31. char encoding[64];
  32. uint8_t* extra;
  33. int extra_bytes;
  34. uint16_t seq; // RTP-Info seq
  35. uint32_t base; // RTP-Info timestamp
  36. uint32_t last; // last rtp packet timestamp
  37. int64_t timestamp; // rtp timestamp extend to 64bit
  38. int64_t pts; // last mapped rtp packet timestamp
  39. struct rtp_demuxer_t* rtp;
  40. void* ts, * ps; // only one
  41. union
  42. {
  43. struct sdp_a_fmtp_h264_t h264;
  44. struct sdp_a_fmtp_h265_t h265;
  45. struct sdp_a_fmtp_mpeg4_t mpeg4;
  46. struct mpeg4_aac_t aac;
  47. } fmtp;
  48. struct avpbs_t* bs;
  49. void* filter;
  50. // 264/265 only
  51. struct avbsf_t* avbsf;
  52. void* h2645;
  53. // ps/ts only
  54. struct
  55. {
  56. AVPACKET_CODEC_ID codec;
  57. struct avpbs_t* bs;
  58. void* filter;
  59. int pid; // ts/ps only
  60. } tracks[RTSP_STREAM_MAX];
  61. // ps only
  62. struct {
  63. uint8_t* ptr;
  64. int len, cap;
  65. } ptr;
  66. };
  67. struct rtsp_demuxer_t
  68. {
  69. void* param;
  70. rtsp_demuxer_onpacket onpacket;
  71. int stream;
  72. int jitter;
  73. struct rtp_payload_info_t pt[RTSP_PAYLOAD_MAX];
  74. int count; // payload type count
  75. int idx; // last payload index
  76. uint8_t ptr[8 * 1024];
  77. int off;
  78. };
  79. int sdp_aac_latm_load(uint8_t* data, int bytes, const char* config);
  80. int sdp_aac_mpeg4_load(uint8_t* data, int bytes, const char* config);
  81. int sdp_h264_load(uint8_t* data, int bytes, const char* config);
  82. int sdp_h265_load(uint8_t* data, int bytes, const char* vps, const char* sps, const char* pps, const char* sei);
  83. static int rtsp_demuxer_avpbs_onpacket(void* param, struct avpacket_t* pkt)
  84. {
  85. struct rtp_payload_info_t* pt;
  86. pt = (struct rtp_payload_info_t*)param;
  87. return pt->ctx->onpacket(pt->ctx->param, pkt);
  88. }
  89. static int rtsp_demuxer_mpeg2_fetch_stream(struct rtp_payload_info_t* pt, int pid, int codecid)
  90. {
  91. int i, mpeg2;
  92. mpeg2 = avpayload_find_by_mpeg2((uint8_t)codecid);
  93. if (mpeg2 < 0)
  94. return -EPROTONOSUPPORT;
  95. for (i = 0; i < sizeof(pt->tracks) / sizeof(pt->tracks[0]); i++)
  96. {
  97. if (0 == pt->tracks[i].pid || pt->tracks[i].pid == pid)
  98. break;
  99. }
  100. if (i >= sizeof(pt->tracks) / sizeof(pt->tracks[0]))
  101. return -E2BIG;
  102. if(pt->tracks[i].codec != s_payloads[mpeg2].codecid && pt->tracks[i].bs && pt->tracks[i].filter)
  103. pt->tracks[i].bs->destroy(&pt->tracks[i].filter);
  104. if (!pt->tracks[i].bs || !pt->tracks[i].filter)
  105. {
  106. pt->tracks[i].codec = s_payloads[mpeg2].codecid;
  107. pt->tracks[i].bs = avpbs_find(s_payloads[mpeg2].codecid);
  108. if(pt->tracks[i].bs)
  109. pt->tracks[i].filter = pt->tracks[i].bs->create(i, s_payloads[mpeg2].codecid, NULL, 0, rtsp_demuxer_avpbs_onpacket, pt);
  110. }
  111. pt->tracks[i].pid = pid;
  112. return pt->tracks[i].filter ? i : -1;
  113. }
  114. static inline int rtsp_demuxer_mpegts_onpacket(void* param, int program, int track, int codecid, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes)
  115. {
  116. int i;
  117. struct rtp_payload_info_t* pt;
  118. #if defined(FIX_DAHUA_AAC_FROM_G711)
  119. if ((codecid == PSI_STREAM_AUDIO_G711A || codecid == PSI_STREAM_AUDIO_G711U)
  120. && bytes > 7 && 0xFF == ((uint8_t*)data)[0] && 0xF0 == (((uint8_t*)data)[1] & 0xF0))
  121. {
  122. int n = 7; // fix n = 0
  123. for (i = 0; i + 7 < bytes && n >= 7; i += n)
  124. n = mpeg4_aac_adts_frame_length((const uint8_t*)data + i, bytes - i);
  125. codecid = i == bytes ? PSI_STREAM_AAC : codecid; // fix it
  126. }
  127. #endif
  128. pt = (struct rtp_payload_info_t*)param;
  129. i = rtsp_demuxer_mpeg2_fetch_stream(pt, track, codecid);
  130. if (i >= 0)
  131. {
  132. flags = flags ? AVPACKET_FLAG_KEY : 0;
  133. return pt->tracks[i].bs->input(pt->tracks[i].filter, pts / 90, dts / 90, (const uint8_t*)data, (int)bytes, flags);
  134. }
  135. if (0xbd == codecid || 0 == codecid)
  136. return 0; // ignore HIK private stream
  137. (void)program; //ignore
  138. assert(0);
  139. return -1; // discard
  140. }
  141. static inline int rtsp_demuxer_mpegps_onpacket(void* param, int track, int codecid, int flags, int64_t pts, int64_t dts, const void* data, size_t bytes)
  142. {
  143. //struct rtp_payload_info_t* pt;
  144. //pt = (struct rtp_payload_info_t*)param;
  145. (void)param;
  146. return rtsp_demuxer_mpegts_onpacket(param, 0, track, codecid, flags, pts, dts, data, bytes);
  147. }
  148. static inline int rtsp_demuxer_ontspacket(void* param, const void* packet, int bytes, uint32_t timestamp, int flags)
  149. {
  150. int r;
  151. struct rtp_payload_info_t* pt;
  152. pt = (struct rtp_payload_info_t*)param;
  153. r = 0;
  154. while (0 == r && bytes >= 188)
  155. {
  156. r = (int)ts_demuxer_input(pt->ts, packet, 188);
  157. assert(0 == r);
  158. bytes -= 188;
  159. packet = (const uint8_t*)packet + 188;
  160. }
  161. (void)timestamp, (void)flags; //ignore
  162. return r;
  163. }
  164. static inline int rtsp_demuxer_merge_ps_buffer(struct rtp_payload_info_t* pt, const uint8_t* packet, int bytes)
  165. {
  166. int n;
  167. uint8_t* ptr;
  168. if (bytes < 0)
  169. return -EINVAL;
  170. if (pt->ptr.len + bytes > pt->ptr.cap)
  171. {
  172. if (pt->ptr.len + bytes > RTSP_PAYLOAD_MAX_SIZE)
  173. {
  174. pt->ptr.len = 0; // clear
  175. return -E2BIG;
  176. }
  177. n = pt->ptr.len + bytes + 64 * 1024;
  178. ptr = (uint8_t*)realloc(pt->ptr.ptr, n);
  179. if (!ptr)
  180. {
  181. pt->ptr.len = 0; // clear
  182. return -ENOMEM;
  183. }
  184. pt->ptr.ptr = ptr;
  185. pt->ptr.cap = n;
  186. }
  187. if (bytes > 0)
  188. {
  189. memmove(pt->ptr.ptr + pt->ptr.len, packet, bytes);
  190. pt->ptr.len += bytes;
  191. }
  192. return 0;
  193. }
  194. static inline int rtsp_demuxer_onpspacket(void* param, const void* packet, int bytes, uint32_t timestamp, int flags)
  195. {
  196. int r, n;
  197. struct rtp_payload_info_t* pt;
  198. pt = (struct rtp_payload_info_t*)param;
  199. #if 1
  200. if (pt->ptr.len > 0)
  201. {
  202. r = rtsp_demuxer_merge_ps_buffer(pt, packet, bytes);
  203. if (0 != r)
  204. return r;
  205. packet = pt->ptr.ptr;
  206. bytes = pt->ptr.len;
  207. pt->ptr.len = 0;
  208. }
  209. n = (int)ps_demuxer_input(pt->ps, packet, bytes);
  210. assert(n <= bytes);
  211. if (n >= 0 && n < bytes)
  212. r = rtsp_demuxer_merge_ps_buffer(pt, (const uint8_t*)packet + n, bytes - n);
  213. else
  214. r = n < 0 ? n : 0;
  215. (void)timestamp, (void)flags; //ignore
  216. return r;
  217. #else
  218. if (!pt->tracks[0].bs || !pt->tracks[0].filter)
  219. {
  220. pt->tracks[0].codec = AVCODEC_DATA_MP2P;
  221. pt->tracks[0].bs = avpbs_find(AVCODEC_DATA_MP2P);
  222. if (pt->tracks[0].bs)
  223. pt->tracks[0].filter = pt->tracks[0].bs->create(0, AVCODEC_DATA_MP2P, NULL, 0, rtsp_demuxer_avpbs_onpacket, pt);
  224. }
  225. // RTP timestamp => PTS/DTS
  226. if (0 == pt->last && INT64_MIN == pt->pts)
  227. pt->timestamp = timestamp;
  228. else
  229. pt->timestamp += (int32_t)(timestamp - pt->last);
  230. pt->last = timestamp;
  231. pt->pts = pt->timestamp * 1000 / pt->frequency;
  232. flags = flags ? AVPACKET_FLAG_KEY : 0;
  233. return pt->tracks[0].bs->input(pt->tracks[0].filter, pt->pts, pt->pts, (const uint8_t*)packet, (int)bytes, flags);
  234. #endif
  235. }
  236. static inline int rtsp_demuxer_onrtppacket(void* param, const void* data, int bytes, uint32_t timestamp, int flags)
  237. {
  238. int r;
  239. struct rtp_payload_info_t* pt;
  240. pt = (struct rtp_payload_info_t*)param;
  241. // RTP timestamp => PTS/DTS
  242. if (0 == pt->last && INT64_MIN == pt->pts)
  243. pt->timestamp = timestamp;
  244. else
  245. pt->timestamp += (int32_t)(timestamp - pt->last);
  246. pt->last = timestamp;
  247. pt->pts = pt->timestamp * 1000 / pt->frequency;
  248. r = -1;
  249. if (pt->bs && pt->filter)
  250. {
  251. //flags = 0;
  252. r = pt->bs->input(pt->filter, pt->pts, pt->pts, (const uint8_t*)data, bytes, flags);
  253. }
  254. //else
  255. // r = pt->ctx->onpacket(pt->ctx->param, 0, pt->payload, pt->encoding, pt->pts, pt->pts, data, bytes, flags);
  256. return r;
  257. }
  258. static inline int rtsp_demuxer_onh2645nalu(void* param, const void* data, int bytes, uint32_t timestamp, int flags)
  259. {
  260. struct rtp_payload_info_t* pt;
  261. pt = (struct rtp_payload_info_t*)param;
  262. // RTP timestamp => PTS/DTS
  263. if (0 == pt->last && INT64_MIN == pt->pts)
  264. pt->timestamp = timestamp;
  265. else
  266. pt->timestamp += (int32_t)(timestamp - pt->last);
  267. pt->last = timestamp;
  268. pt->pts = pt->timestamp * 1000 / pt->frequency;
  269. assert(pt->avbsf && pt->h2645);
  270. if (pt->avbsf && pt->h2645)
  271. {
  272. //flags = 0;
  273. return pt->avbsf->input(pt->h2645, pt->pts, pt->pts, (const uint8_t*)data, bytes);
  274. }
  275. (void)flags; // ignore
  276. return -1;
  277. }
  278. static int rtsp_demuxer_onh2645packet(void* param, int64_t pts, int64_t dts, const uint8_t* data, int bytes, int flags)
  279. {
  280. struct rtp_payload_info_t* pt;
  281. pt = (struct rtp_payload_info_t*)param;
  282. if (pt->bs && pt->filter)
  283. {
  284. //flags = 0;
  285. return pt->bs->input(pt->filter, pts, dts, (const uint8_t*)data, bytes, flags);
  286. }
  287. return -1;
  288. }
  289. static int rtsp_demuxer_payload_close(struct rtp_payload_info_t* pt)
  290. {
  291. if (pt->ptr.ptr)
  292. {
  293. assert(pt->ptr.cap > 0);
  294. free(pt->ptr.ptr);
  295. pt->ptr.ptr = NULL;
  296. }
  297. if (pt->bs && pt->filter)
  298. {
  299. pt->bs->destroy(&pt->filter);
  300. pt->bs = NULL;
  301. }
  302. if (pt->avbsf && pt->h2645)
  303. {
  304. pt->avbsf->destroy(&pt->h2645);
  305. pt->avbsf = NULL;
  306. }
  307. if (pt->ts)
  308. {
  309. ts_demuxer_destroy(pt->ts);
  310. pt->ts = NULL;
  311. }
  312. if (pt->ps)
  313. {
  314. ps_demuxer_destroy(pt->ps);
  315. pt->ps = NULL;
  316. }
  317. if (pt->rtp)
  318. {
  319. rtp_demuxer_destroy(&pt->rtp);
  320. pt->rtp = NULL;
  321. }
  322. return 0;
  323. }
  324. int rtsp_demuxer_add_payload(struct rtsp_demuxer_t* demuxer, int frequency, int payload, const char* encoding, const char* fmtp)
  325. {
  326. int avp, len;
  327. AVPACKET_CODEC_ID codec;
  328. struct rtp_payload_info_t* pt;
  329. int (*onpacket)(void* param, const void* data, int bytes, uint32_t timestamp, int flags);
  330. if (demuxer->count >= sizeof(demuxer->pt) / sizeof(demuxer->pt[0]))
  331. return -E2BIG; // too many payload type
  332. avp = avpayload_find_by_rtp((uint8_t)payload, encoding);
  333. // fixme: ffmpeg g711u sample rate 0
  334. frequency = frequency ? frequency : (-1!=avp?s_payloads[avp].frequency:90000);
  335. pt = &demuxer->pt[demuxer->count];
  336. memset(pt, 0, sizeof(*pt));
  337. snprintf(pt->encoding, sizeof(pt->encoding) - 1, "%s", encoding);
  338. pt->frequency = frequency;
  339. pt->payload = payload;
  340. pt->extra = demuxer->ptr + demuxer->off;
  341. pt->extra_bytes = 0;
  342. pt->ctx = demuxer;
  343. pt->pts = INT64_MIN;
  344. if (RTP_PAYLOAD_MP2T == payload)
  345. {
  346. onpacket = rtsp_demuxer_ontspacket;
  347. pt->ts = ts_demuxer_create(rtsp_demuxer_mpegts_onpacket, pt);
  348. }
  349. else if (0 == strcasecmp(encoding, "MP2P") || 0 == strcasecmp(encoding, "PS"))
  350. {
  351. onpacket = rtsp_demuxer_onpspacket;
  352. pt->ps = ps_demuxer_create(rtsp_demuxer_mpegps_onpacket, pt);
  353. }
  354. else
  355. {
  356. onpacket = rtsp_demuxer_onrtppacket;
  357. assert(demuxer->off <= sizeof(demuxer->ptr));
  358. len = sizeof(demuxer->ptr) - demuxer->off;
  359. codec = -1 != avp ? s_payloads[avp].codecid : AVCODEC_NONE;
  360. switch (codec)
  361. {
  362. case AVCODEC_VIDEO_H264:
  363. if (fmtp && *fmtp && 0 == sdp_a_fmtp_h264(fmtp, &payload, &pt->fmtp.h264))
  364. pt->extra_bytes = sdp_h264_load(pt->extra, len, pt->fmtp.h264.sprop_parameter_sets);
  365. pt->avbsf = avbsf_h264();
  366. pt->h2645 = pt->avbsf->create(pt->extra, pt->extra_bytes, rtsp_demuxer_onh2645packet, pt);
  367. onpacket = rtsp_demuxer_onh2645nalu;
  368. break;
  369. case AVCODEC_VIDEO_H265:
  370. if (fmtp && *fmtp && 0 == sdp_a_fmtp_h265(fmtp, &payload, &pt->fmtp.h265))
  371. pt->extra_bytes = sdp_h265_load(pt->extra, len, pt->fmtp.h265.sprop_vps, pt->fmtp.h265.sprop_sps, pt->fmtp.h265.sprop_pps, pt->fmtp.h265.sprop_sei);
  372. pt->avbsf = avbsf_h265();
  373. pt->h2645 = pt->avbsf->create(pt->extra, pt->extra_bytes, rtsp_demuxer_onh2645packet, pt);
  374. onpacket = rtsp_demuxer_onh2645nalu;
  375. break;
  376. case AVCODEC_AUDIO_AAC:
  377. if (0 == strcasecmp(encoding, "MPEG4-GENERIC"))
  378. {
  379. if (fmtp && *fmtp && 0 == sdp_a_fmtp_mpeg4(fmtp, &payload, &pt->fmtp.mpeg4))
  380. pt->extra_bytes = sdp_aac_mpeg4_load(pt->extra, len, pt->fmtp.mpeg4.config);
  381. }
  382. else if (0 == strcasecmp(encoding, "MP4A-LATM"))
  383. {
  384. if (fmtp && *fmtp && 0 == sdp_a_fmtp_mpeg4(fmtp, &payload, &pt->fmtp.mpeg4))
  385. pt->extra_bytes = sdp_aac_latm_load(pt->extra, len, pt->fmtp.mpeg4.config);
  386. }
  387. else
  388. {
  389. }
  390. break;
  391. // TODO:
  392. case AVCODEC_VIDEO_AV1:
  393. case AVCODEC_VIDEO_VP8:
  394. case AVCODEC_VIDEO_VP9:
  395. case AVCODEC_AUDIO_MP3:
  396. case AVCODEC_AUDIO_OPUS:
  397. default:// common
  398. break;
  399. }
  400. pt->bs = avpbs_find(codec);
  401. if (pt->bs)
  402. pt->filter = pt->bs->create(demuxer->stream+demuxer->count, codec, pt->extra, pt->extra_bytes > 0 ? pt->extra_bytes : 0, rtsp_demuxer_avpbs_onpacket, pt);
  403. }
  404. pt->rtp = rtp_demuxer_create(demuxer->jitter, frequency, pt->payload, encoding, onpacket, pt);
  405. if (!pt->rtp || (pt->bs && !pt->filter))
  406. {
  407. rtsp_demuxer_payload_close(pt);
  408. return -ENOMEM;
  409. }
  410. demuxer->count++;
  411. demuxer->off += pt->extra_bytes;
  412. assert(demuxer->off <= sizeof(demuxer->ptr));
  413. return 0;
  414. }
  415. struct rtsp_demuxer_t* rtsp_demuxer_create(int stream, int jitter, rtsp_demuxer_onpacket onpkt, void* param)
  416. {
  417. struct rtsp_demuxer_t* demuxer;
  418. demuxer = calloc(1, sizeof(*demuxer));
  419. if (!demuxer) return NULL;
  420. demuxer->stream = stream;
  421. demuxer->jitter = jitter;
  422. demuxer->param = param;
  423. demuxer->onpacket = onpkt;
  424. return demuxer;
  425. }
  426. int rtsp_demuxer_destroy(struct rtsp_demuxer_t* demuxer)
  427. {
  428. int i;
  429. if (demuxer)
  430. {
  431. for (i = 0; i < demuxer->count; i++)
  432. rtsp_demuxer_payload_close(&demuxer->pt[i]);
  433. free(demuxer);
  434. }
  435. return 0;
  436. }
  437. int rtsp_demuxer_rtpinfo(struct rtsp_demuxer_t* demuxer, uint16_t seq, uint32_t timestamp)
  438. {
  439. struct rtp_payload_info_t* pt;
  440. if (demuxer->idx >= demuxer->count || demuxer->idx < 0)
  441. {
  442. assert(0);
  443. return -ENOENT;
  444. }
  445. pt = &demuxer->pt[demuxer->idx];
  446. pt->seq = seq;
  447. pt->base = timestamp;
  448. // fixme
  449. //if (0 == pt->last)
  450. // pt->last = timestamp;
  451. //pt->pts = ((int64_t)(int32_t)(pt->last - timestamp)) * 1000 / pt->frequency;
  452. return 0;
  453. }
  454. int rtsp_demuxer_input(struct rtsp_demuxer_t* demuxer, const void* data, int bytes)
  455. {
  456. int i;
  457. uint8_t id;
  458. struct rtp_payload_info_t* pt;
  459. id = bytes > 1 ? ((const uint8_t*)data)[1] : 255;
  460. for (i = demuxer->idx; i < demuxer->count + demuxer->idx; i++)
  461. {
  462. pt = &demuxer->pt[i % demuxer->count];
  463. // RFC7983 SRTP: https://tools.ietf.org/html/draft-ietf-avtcore-rfc5764-mux-fixes
  464. assert(((const uint8_t*)data)[0] > 127 && ((const uint8_t*)data)[0] < 192);
  465. // http://www.iana.org/assignments/rtp-parameters/rtp-parameters.xhtml#rtp-parameters-4
  466. // RFC 5761 (RTCP-mux) states this range for secure RTCP/RTP detection.
  467. // RTCP packet types in the ranges 1-191 and 224-254 SHOULD only be used when other values have been exhausted.
  468. if ( (id & 0x7F) == pt->payload || (192 <= id && id <= 223) )
  469. {
  470. demuxer->idx = i % demuxer->count;
  471. return rtp_demuxer_input(pt->rtp, data, bytes);
  472. }
  473. }
  474. //assert(0);
  475. //return -1;
  476. return 0;
  477. }
  478. int rtsp_demuxer_rtcp(struct rtsp_demuxer_t* demuxer, void* buf, int len)
  479. {
  480. if (demuxer->idx >= demuxer->count || demuxer->idx < 0)
  481. {
  482. assert(0);
  483. return -ENOENT;
  484. }
  485. return rtp_demuxer_rtcp(demuxer->pt[demuxer->idx].rtp, buf, len);
  486. }