選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

554 行
15KB

  1. #if defined(_HAVE_FFMPEG_)
  2. #include "ffmpeg-file-source.h"
  3. #include "rtp-profile.h"
  4. #include "rtp-payload.h"
  5. #include "mov-format.h"
  6. #include "mpeg4-avc.h"
  7. #include "mpeg4-aac.h"
  8. #include "sys/system.h"
  9. #include "sys/path.h"
  10. #include "base64.h"
  11. #include "rtp.h"
  12. #include <assert.h>
  13. extern "C" uint32_t rtp_ssrc(void);
  14. extern "C" const struct mov_buffer_t* mov_file_buffer(void);
  15. extern "C" int sdp_h264(uint8_t *data, int bytes, const char* proto, unsigned short port, int payload, int frequence, const void* extra, int extra_size);
  16. extern "C" int sdp_h265(uint8_t *data, int bytes, const char* proto, unsigned short port, int payload, int frequence, const void* extra, int extra_size);
  17. extern "C" int sdp_mpeg4_es(uint8_t *data, int bytes, const char* proto, unsigned short port, int payload, int frequence, const void* extra, int extra_size);
  18. extern "C" int sdp_aac_latm(uint8_t *data, int bytes, const char* proto, unsigned short port, int payload, int sample_rate, int channel_count, const void* extra, int extra_size);
  19. extern "C" int sdp_aac_generic(uint8_t *data, int bytes, const char* proto, unsigned short port, int payload, int sample_rate, int channel_count, const void* extra, int extra_size);
  20. extern "C" int sdp_opus(uint8_t *data, int bytes, const char* proto, unsigned short port, int payload, int sample_rate, int channel_count, const void* extra, int extra_size);
  21. extern "C" int sdp_g711u(uint8_t *data, int bytes, const char* proto, unsigned short port);
  22. extern "C" int sdp_g711a(uint8_t *data, int bytes, const char* proto, unsigned short port);
  23. inline uint8_t ffmpeg_codec_id_2_mp4_object(AVCodecID codecid)
  24. {
  25. switch (codecid)
  26. {
  27. case AV_CODEC_ID_MPEG4:
  28. return MOV_OBJECT_MP4V;
  29. case AV_CODEC_ID_H264:
  30. return MOV_OBJECT_H264;
  31. case AV_CODEC_ID_HEVC:
  32. return MOV_OBJECT_HEVC;
  33. case AV_CODEC_ID_AAC:
  34. return MOV_OBJECT_AAC;
  35. case AV_CODEC_ID_OPUS:
  36. return MOV_OBJECT_OPUS;
  37. default:
  38. return 0;
  39. }
  40. }
  41. FFFileSource::FFFileSource(const char *file)
  42. {
  43. static int s_init = 0;
  44. if(0 == s_init)
  45. {
  46. s_init = 1;
  47. avformat_network_init();
  48. }
  49. m_speed = 1.0;
  50. m_status = 0;
  51. m_clock = 0;
  52. m_count = 0;
  53. m_dts = -1;
  54. av_init_packet(&m_pkt);
  55. if (0 == Open(file))
  56. {
  57. for (unsigned int i = 0; i < m_ic->nb_streams; i++)
  58. {
  59. AVCodecParameters* codecpar = m_ic->streams[i]->codecpar;
  60. uint8_t object = ffmpeg_codec_id_2_mp4_object(codecpar->codec_id);
  61. if (0 == object)
  62. {
  63. // assert(0);
  64. continue;
  65. }
  66. if (AVMEDIA_TYPE_VIDEO == codecpar->codec_type)
  67. {
  68. MP4OnVideo(this, i, object, codecpar->width, codecpar->height, codecpar->extradata, codecpar->extradata_size);
  69. }
  70. else if (AVMEDIA_TYPE_AUDIO == codecpar->codec_type)
  71. {
  72. MP4OnAudio(this, i, object, codecpar->channels, codecpar->bits_per_raw_sample, codecpar->sample_rate, codecpar->extradata, codecpar->extradata_size);
  73. }
  74. }
  75. }
  76. for (int i = 0; i < m_count; i++)
  77. {
  78. struct media_t* m = &m_media[i];
  79. rtp_set_info(m->rtp, "RTSPServer", path_basename(file));
  80. }
  81. }
  82. FFFileSource::~FFFileSource()
  83. {
  84. for (int i = 0; i < m_count; i++)
  85. {
  86. struct media_t* m = &m_media[i];
  87. if (m->rtp)
  88. {
  89. rtp_destroy(m->rtp);
  90. m->rtp = NULL;
  91. }
  92. if (m->packer)
  93. {
  94. rtp_payload_encode_destroy(m->packer);
  95. m->packer = NULL;
  96. }
  97. }
  98. if (m_ic)
  99. {
  100. avformat_close_input(&m_ic);
  101. avformat_free_context(m_ic);
  102. }
  103. }
  104. int FFFileSource::Open(const char* file)
  105. {
  106. int r;
  107. AVDictionary* opt = NULL;
  108. m_ic = avformat_alloc_context();
  109. if (NULL == m_ic)
  110. {
  111. printf("%s(%s): avformat_alloc_context failed.\n", __FUNCTION__, file);
  112. return -ENOMEM;
  113. }
  114. //if (!av_dict_get(ff->opt, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE)) {
  115. // av_dict_set(&ff->opt, "scan_all_pmts", "1", AV_DICT_DONT_OVERWRITE);
  116. // scan_all_pmts_set = 1;
  117. //}
  118. r = avformat_open_input(&m_ic, file, NULL, NULL/*&opt*/);
  119. if (0 != r)
  120. {
  121. printf("%s: avformat_open_input(%s) => %d\n", __FUNCTION__, file, r);
  122. return r;
  123. }
  124. //if (scan_all_pmts_set)
  125. // av_dict_set(&format_opts, "scan_all_pmts", NULL, AV_DICT_MATCH_CASE);
  126. //ff->ic->probesize = 100 * 1024;
  127. //ff->ic->max_analyze_duration = 5 * AV_TIME_BASE;
  128. /* If not enough info to get the stream parameters, we decode the
  129. first frames to get it. (used in mpeg case for example) */
  130. r = avformat_find_stream_info(m_ic, NULL/*&opt*/);
  131. if (r < 0) {
  132. printf("%s(%s): could not find codec parameters\n", __FUNCTION__, file);
  133. return r;
  134. }
  135. av_dict_free(&opt);
  136. return 0;
  137. }
  138. int FFFileSource::SetTransport(const char* track, std::shared_ptr<IRTPTransport> transport)
  139. {
  140. int t = atoi(track + 5/*track*/);
  141. for (int i = 0; i < m_count; i++)
  142. {
  143. struct media_t* m = &m_media[i];
  144. if (t != m->track)
  145. continue;
  146. m->transport = transport;
  147. return 0;
  148. }
  149. return -1;
  150. }
  151. int FFFileSource::Play()
  152. {
  153. bool sendframe = false;
  154. if (3 == m_status)
  155. return 0;
  156. SEND_PACKET:
  157. if (0 == m_pkt.buf)
  158. {
  159. int r = av_read_frame(m_ic, &m_pkt);
  160. if (r == AVERROR_EOF)
  161. {
  162. // 0-EOF
  163. m_status = 3;
  164. SendBye();
  165. return 0;
  166. }
  167. else if (r < 0)
  168. {
  169. // error
  170. return r;
  171. }
  172. for (r = 0; r < m_count; r++)
  173. {
  174. struct media_t* m = &m_media[r];
  175. if (m->track == m_pkt.stream_index)
  176. break;
  177. }
  178. if (r == m_count)
  179. {
  180. av_packet_unref(&m_pkt); // send flag
  181. sendframe = 1;
  182. goto SEND_PACKET;
  183. }
  184. AVRational time_base = { 1, 1000/*ms*/ };
  185. m_pkt.dts = (AV_NOPTS_VALUE == m_pkt.dts ? m_pkt.pts : m_pkt.dts);
  186. m_pkt.pts = (AV_NOPTS_VALUE == m_pkt.pts ? m_pkt.dts : m_pkt.pts);
  187. m_pkt.dts = av_rescale_q(m_pkt.dts, m_ic->streams[m_pkt.stream_index]->time_base, time_base);
  188. m_pkt.pts = av_rescale_q(m_pkt.pts, m_ic->streams[m_pkt.stream_index]->time_base, time_base);
  189. }
  190. m_status = 1;
  191. uint64_t clock = system_clock();
  192. for (int i = 0; i < m_count; i++)
  193. {
  194. struct media_t* m = &m_media[i];
  195. if (m->track != m_pkt.stream_index)
  196. continue;
  197. if (0 == m_clock || m_clock > clock)
  198. m_clock = clock;
  199. if (-1 == m_dts)
  200. m_dts = m_pkt.dts;
  201. if (int64_t(clock - m_clock) + m_dts >= m_pkt.dts)
  202. {
  203. if (0 == strcmp("H264", m->name) || 0 == strcmp("H265", m->name))
  204. {
  205. // MPEG4 -> H.264 byte stream
  206. uint8_t* p = m_pkt.data;
  207. size_t bytes = m_pkt.size;
  208. while (bytes > 0)
  209. {
  210. // nalu size -> start code
  211. assert(bytes > 4);
  212. uint32_t n = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
  213. p[0] = 0;
  214. p[1] = 0;
  215. p[2] = 0;
  216. p[3] = 1;
  217. bytes -= n + 4;
  218. p += n + 4;
  219. }
  220. //printf("[V] pts: %lld, dts: %lld, clock: %llu\n", m_pkt.pts, m_pkt.dts, clock);
  221. }
  222. else if (0 == strcmp("MP4A-LATM", m->name) || 0 == strcmp("MPEG4-GENERIC", m->name))
  223. {
  224. // add ADTS header
  225. //printf("[A] pts: %lld, dts: %lld, clock: %llu\n", m_pkt.pts, m_pkt.dts, clock);
  226. }
  227. else
  228. {
  229. assert(0);
  230. }
  231. if (-1 == m->dts_first)
  232. m->dts_first = m_pkt.pts;
  233. m->dts_last = m_pkt.pts;
  234. uint32_t timestamp = m->timestamp + m->dts_last - m->dts_first;
  235. rtp_payload_encode_input(m->packer, m_pkt.data, m_pkt.size, (uint32_t)(timestamp * (m->frequency / 1000) /*kHz*/));
  236. av_packet_unref(&m_pkt); // send flag
  237. sendframe = 1;
  238. goto SEND_PACKET;
  239. }
  240. break;
  241. }
  242. return sendframe ? 1 : 0;
  243. }
  244. int FFFileSource::Pause()
  245. {
  246. m_status = 2;
  247. m_clock = 0;
  248. m_dts = -1;
  249. return 0;
  250. }
  251. int FFFileSource::Seek(int64_t pos)
  252. {
  253. // update timestamp
  254. for (int i = 0; i < m_count; i++)
  255. {
  256. if (-1 != m_media[i].dts_first)
  257. m_media[i].timestamp += m_media[i].dts_last - m_media[i].dts_first + 1;
  258. m_media[i].dts_first = -1;
  259. //SendRTCP(&m_media[i], system_clock());
  260. }
  261. m_dts = -1;
  262. m_clock = 0;
  263. av_packet_unref(&m_pkt); // clear buffered frame
  264. AVRational time_base = { 1, 1000/*ms*/ };
  265. pos = av_rescale_q(pos, time_base, m_ic->streams[0]->time_base);
  266. return av_seek_frame(m_ic, 0, pos, 0);
  267. }
  268. int FFFileSource::SetSpeed(double speed)
  269. {
  270. m_speed = speed;
  271. return 0;
  272. }
  273. int FFFileSource::GetDuration(int64_t& duration) const
  274. {
  275. if (m_ic)
  276. {
  277. duration = m_ic->duration / 1000;
  278. return 0;
  279. }
  280. return -1;
  281. }
  282. int FFFileSource::GetSDPMedia(std::string& sdp) const
  283. {
  284. sdp = m_sdp;
  285. return m_ic ? 0 : -1;
  286. }
  287. int FFFileSource::GetRTPInfo(const char* uri, char *rtpinfo, size_t bytes) const
  288. {
  289. int n = 0;
  290. uint16_t seq;
  291. uint32_t timestamp;
  292. // RTP-Info: url=rtsp://foo.com/bar.avi/streamid=0;seq=45102,
  293. // url=rtsp://foo.com/bar.avi/streamid=1;seq=30211
  294. for (int i = 0; i < m_count; i++)
  295. {
  296. const struct media_t* m = &m_media[i];
  297. rtp_payload_encode_getinfo(m->packer, &seq, &timestamp);
  298. if (i > 0)
  299. rtpinfo[n++] = ',';
  300. n += snprintf(rtpinfo + n, bytes - n, "url=%s/track%d;seq=%hu;rtptime=%u", uri, m->track, seq, (unsigned int)(m->timestamp * (m->frequency / 1000) /*kHz*/));
  301. }
  302. return 0;
  303. }
  304. void FFFileSource::MP4OnVideo(void* param, uint32_t track, uint8_t object, int /*width*/, int /*height*/, const void* extra, size_t bytes)
  305. {
  306. int n = 0;
  307. uint8_t buffer[8 * 1024];
  308. FFFileSource* self = (FFFileSource*)param;
  309. struct media_t* m = &self->m_media[self->m_count++];
  310. m->track = track;
  311. m->rtcp_clock = 0;
  312. m->ssrc = rtp_ssrc();
  313. m->timestamp = rtp_ssrc();
  314. m->bandwidth = 4 * 1024 * 1024;
  315. m->dts_last = m->dts_first = -1;
  316. if (MOV_OBJECT_H264 == object)
  317. {
  318. m->frequency = 90000;
  319. m->payload = RTP_PAYLOAD_H264;
  320. snprintf(m->name, sizeof(m->name), "%s", "H264");
  321. n = sdp_h264(buffer, sizeof(buffer), "RTP/AVP", 0, RTP_PAYLOAD_H264, 90000, extra, bytes);
  322. }
  323. else if (MOV_OBJECT_HEVC == object)
  324. {
  325. m->frequency = 90000;
  326. m->payload = RTP_PAYLOAD_H265;
  327. snprintf(m->name, sizeof(m->name), "%s", "H265");
  328. n = sdp_h265(buffer, sizeof(buffer), "RTP/AVP", 0, RTP_PAYLOAD_H265, 90000, extra, bytes);
  329. }
  330. else
  331. {
  332. assert(0);
  333. return;
  334. }
  335. struct rtp_payload_t rtpfunc = {
  336. FFFileSource::RTPAlloc,
  337. FFFileSource::RTPFree,
  338. FFFileSource::RTPPacket,
  339. };
  340. m->packer = rtp_payload_encode_create(m->payload, m->name, (uint16_t)m->timestamp, m->ssrc, &rtpfunc, m);
  341. struct rtp_event_t event;
  342. event.on_rtcp = OnRTCPEvent;
  343. m->rtp = rtp_create(&event, self, m->ssrc, m->timestamp, m->frequency, m->bandwidth, 1);
  344. n += snprintf((char*)buffer + n, sizeof(buffer) - n, "a=control:track%d\n", m->track);
  345. self->m_sdp += (const char*)buffer;
  346. }
  347. void FFFileSource::MP4OnAudio(void* param, uint32_t track, uint8_t object, int channel_count, int /*bit_per_sample*/, int sample_rate, const void* extra, size_t bytes)
  348. {
  349. int n = 0;
  350. uint8_t buffer[2 * 1024];
  351. FFFileSource* self = (FFFileSource*)param;
  352. struct media_t* m = &self->m_media[self->m_count++];
  353. m->track = track;
  354. m->rtcp_clock = 0;
  355. m->ssrc = rtp_ssrc();
  356. m->timestamp = rtp_ssrc();
  357. m->bandwidth = 128 * 1024;
  358. m->dts_last = m->dts_first = -1;
  359. if (MOV_OBJECT_AAC == object)
  360. {
  361. struct mpeg4_aac_t aac;
  362. //aac.profile = MPEG4_AAC_LC;
  363. //aac.channel_configuration = (uint8_t)channel_count;
  364. //aac.sampling_frequency_index = (uint8_t)mpeg4_aac_audio_frequency_from(sample_rate);
  365. mpeg4_aac_audio_specific_config_load((const uint8_t*)extra, bytes, &aac);
  366. //assert(aac.sampling_frequency_index == (uint8_t)mpeg4_aac_audio_frequency_from(sample_rate));
  367. //assert(aac.channel_configuration == channel_count);
  368. if (0)
  369. {
  370. // RFC 6416
  371. // In the presence of SBR, the sampling rates for the core encoder/
  372. // decoder and the SBR tool are different in most cases. Therefore,
  373. // this parameter SHALL NOT be considered as the definitive sampling rate.
  374. m->frequency = sample_rate;
  375. m->payload = RTP_PAYLOAD_LATM;
  376. snprintf(m->name, sizeof(m->name), "%s", "MP4A-LATM");
  377. n = sdp_aac_latm(buffer, sizeof(buffer), "RTP/AVP", 0, RTP_PAYLOAD_LATM, sample_rate, channel_count, extra, bytes);
  378. }
  379. else
  380. {
  381. // RFC 3640 3.3.1. General (p21)
  382. // a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encoding parameters > ]
  383. // For audio streams, <encoding parameters> specifies the number of audio channels
  384. // streamType: AudioStream
  385. // When using SDP, the clock rate of the RTP time stamp MUST be expressed using the "rtpmap" attribute.
  386. // If an MPEG-4 audio stream is transported, the rate SHOULD be set to the same value as the sampling rate of the audio stream.
  387. // If an MPEG-4 video stream transported, it is RECOMMENDED that the rate be set to 90 kHz.
  388. m->frequency = sample_rate;
  389. m->payload = RTP_PAYLOAD_MP4A;
  390. snprintf(m->name, sizeof(m->name), "%s", "MPEG4-GENERIC");
  391. n = sdp_aac_generic(buffer, sizeof(buffer), "RTP/AVP", 0, RTP_PAYLOAD_MP4A, sample_rate, channel_count, extra, bytes);
  392. }
  393. }
  394. else if (MOV_OBJECT_OPUS == object)
  395. {
  396. // RFC7587 RTP Payload Format for the Opus Speech and Audio Codec
  397. m->frequency = sample_rate;
  398. m->payload = RTP_PAYLOAD_OPUS;
  399. snprintf(m->name, sizeof(m->name), "%s", "opus");
  400. n = sdp_opus(buffer, sizeof(buffer), "RTP/AVP", 0, RTP_PAYLOAD_OPUS, sample_rate, channel_count, extra, bytes);
  401. }
  402. else if (MOV_OBJECT_G711u == object)
  403. {
  404. m->frequency = sample_rate;
  405. m->payload = RTP_PAYLOAD_PCMU;
  406. snprintf(m->name, sizeof(m->name), "%s", "PCMU");
  407. n = sdp_g711u(buffer, sizeof(buffer), "RTP/AVP", 0);
  408. }
  409. else
  410. {
  411. assert(0);
  412. return;
  413. }
  414. struct rtp_payload_t rtpfunc = {
  415. FFFileSource::RTPAlloc,
  416. FFFileSource::RTPFree,
  417. FFFileSource::RTPPacket,
  418. };
  419. m->packer = rtp_payload_encode_create(m->payload, m->name, (uint16_t)m->timestamp, m->ssrc, &rtpfunc, m);
  420. struct rtp_event_t event;
  421. event.on_rtcp = OnRTCPEvent;
  422. m->rtp = rtp_create(&event, self, m->ssrc, m->timestamp, m->frequency, m->bandwidth, 1);
  423. n += snprintf((char*)buffer + n, sizeof(buffer) - n, "a=control:track%d\n", m->track);
  424. self->m_sdp += (const char*)buffer;
  425. }
  426. void FFFileSource::OnRTCPEvent(const struct rtcp_msg_t* msg)
  427. {
  428. msg;
  429. }
  430. void FFFileSource::OnRTCPEvent(void* param, const struct rtcp_msg_t* msg)
  431. {
  432. FFFileSource *self = (FFFileSource *)param;
  433. self->OnRTCPEvent(msg);
  434. }
  435. int FFFileSource::SendBye()
  436. {
  437. char rtcp[1024] = { 0 };
  438. for (int i = 0; i < m_count; i++)
  439. {
  440. struct media_t* m = &m_media[i];
  441. size_t n = rtp_rtcp_bye(m->rtp, rtcp, sizeof(rtcp));
  442. // send RTCP packet
  443. m->transport->Send(true, rtcp, n);
  444. }
  445. return 0;
  446. }
  447. int FFFileSource::SendRTCP(struct media_t* m, uint64_t clock)
  448. {
  449. // make sure have sent RTP packet
  450. int interval = rtp_rtcp_interval(m->rtp);
  451. if (0 == m->rtcp_clock || m->rtcp_clock + interval < clock)
  452. {
  453. char rtcp[1024] = { 0 };
  454. size_t n = rtp_rtcp_report(m->rtp, rtcp, sizeof(rtcp));
  455. // send RTCP packet
  456. m->transport->Send(true, rtcp, n);
  457. m->rtcp_clock = clock;
  458. }
  459. return 0;
  460. }
  461. void* FFFileSource::RTPAlloc(void* param, int bytes)
  462. {
  463. struct media_t* m = (struct media_t*)param;
  464. assert(bytes <= sizeof(m->packet));
  465. return m->packet;
  466. }
  467. void FFFileSource::RTPFree(void* param, void *packet)
  468. {
  469. struct media_t* m = (struct media_t*)param;
  470. assert(m->packet == packet);
  471. }
  472. int FFFileSource::RTPPacket(void* param, const void *packet, int bytes, uint32_t /*timestamp*/, int /*flags*/)
  473. {
  474. struct media_t* m = (struct media_t*)param;
  475. assert(m->packet == packet);
  476. // Hack: Send an initial RTCP "SR" packet, before the initial RTP packet,
  477. // so that receivers will (likely) be able to get RTCP-synchronized presentation times immediately:
  478. rtp_onsend(m->rtp, packet, bytes/*, time*/);
  479. SendRTCP(m, system_clock());
  480. int r = m->transport->Send(false, packet, bytes);
  481. assert(r == (int)bytes);
  482. return 0;
  483. }
  484. #endif