Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

429 rindas
10KB

  1. #include "mp4-file-source.h"
  2. #include "mov-format.h"
  3. #include "rtp.h"
  4. #include "rtp-profile.h"
  5. #include "rtp-payload.h"
  6. #include "sys/system.h"
  7. #include "sys/path.h"
  8. #include <assert.h>
  9. extern "C" const struct mov_buffer_t* mov_file_buffer(void);
  10. MP4FileSource::MP4FileSource(const char *file)
  11. {
  12. m_speed = 1.0;
  13. m_status = 0;
  14. m_clock = 0;
  15. m_count = 0;
  16. m_dts = -1;
  17. m_fp = fopen(file, "rb");
  18. m_reader = mov_reader_create(mov_file_buffer(), m_fp);
  19. if (m_reader)
  20. {
  21. struct mov_reader_trackinfo_t info = { MP4OnVideo, MP4OnAudio };
  22. mov_reader_getinfo(m_reader, &info, this);
  23. }
  24. for (int i = 0; i < m_count; i++)
  25. {
  26. struct media_t* m = &m_media[i];
  27. rtp_set_info(m->rtp.rtp, "RTSPServer", path_basename(file));
  28. }
  29. }
  30. MP4FileSource::~MP4FileSource()
  31. {
  32. for (int i = 0; i < m_count; i++)
  33. {
  34. struct media_t* m = &m_media[i];
  35. rtp_sender_destroy(&m->rtp);
  36. if (m->pkts)
  37. {
  38. avpacket_queue_destroy(m->pkts);
  39. m->pkts = NULL;
  40. }
  41. }
  42. if (m_reader)
  43. {
  44. mov_reader_destroy(m_reader);
  45. m_reader = NULL;
  46. }
  47. if (m_fp)
  48. fclose(m_fp);
  49. }
  50. int MP4FileSource::SetTransport(const char* track, std::shared_ptr<IRTPTransport> transport)
  51. {
  52. int t = atoi(track + 5/*track*/);
  53. for (int i = 0; i < m_count; i++)
  54. {
  55. struct media_t* m = &m_media[i];
  56. if(t != m->track)
  57. continue;
  58. m->transport = transport;
  59. return 0;
  60. }
  61. return -1;
  62. }
  63. struct MP4FileSource::media_t* MP4FileSource::FetchNextPacket()
  64. {
  65. struct media_t* best = NULL;
  66. int64_t timestamp = 0;
  67. for (int i = 0; i < m_count; i++)
  68. {
  69. struct media_t* m = &m_media[i];
  70. while (0 == avpacket_queue_count(m->pkts))
  71. {
  72. int r = mov_reader_read(m_reader, m_packet, sizeof(m_packet), MP4OnRead, this);
  73. if (r == 0)
  74. {
  75. // 0-EOF
  76. break;
  77. }
  78. else if (r < 0)
  79. {
  80. // error
  81. return NULL;
  82. }
  83. }
  84. if(0 == avpacket_queue_count(m->pkts))
  85. continue;
  86. std::shared_ptr<struct avpacket_t> cur(avpacket_queue_front(m->pkts), avpacket_release);
  87. if (NULL == best || cur->dts < timestamp || cur->pts < timestamp)
  88. {
  89. best = m;
  90. timestamp = cur->dts < cur->pts ? cur->dts : cur->pts;
  91. }
  92. }
  93. //m_status = 3;
  94. //SendBye();
  95. return best;
  96. }
  97. int MP4FileSource::Play()
  98. {
  99. bool sendframe = false;
  100. if (3 == m_status)
  101. return 0;
  102. struct media_t* m = FetchNextPacket();
  103. if (NULL == m)
  104. {
  105. // 0-EOF
  106. m_status = 3;
  107. SendBye();
  108. return 0;
  109. }
  110. m_status = 1;
  111. int bytes = 0;
  112. uint64_t clock = system_clock();
  113. std::shared_ptr<struct avpacket_t> pkt(avpacket_queue_front(m->pkts), avpacket_release);
  114. int64_t dts = pkt->dts < pkt->pts ? pkt->dts : pkt->pts;
  115. SendRTCP(clock);
  116. if (0 == m_clock || m_clock > clock)
  117. m_clock = clock;
  118. if (-1 == m_dts)
  119. m_dts = dts;
  120. if (int64_t(clock - m_clock) + m_dts >= dts)
  121. {
  122. if (0 == strcmp("H264", m->rtp.encoding))
  123. {
  124. // AVC1 -> H.264 byte stream
  125. bytes = h264_mp4toannexb(&m_avc, pkt->data, pkt->size, m_packet, sizeof(m_packet));
  126. //printf("[V] pts: %lld, dts: %lld, clock: %llu\n", m_frame.pts, m_frame.dts, clock);
  127. }
  128. else if (0 == strcmp("H265", m->rtp.encoding))
  129. {
  130. // HVC1 -> H.264 byte stream
  131. bytes = h265_mp4toannexb(&m_hevc, pkt->data, pkt->size, m_packet, sizeof(m_packet));
  132. //printf("[V] pts: %lld, dts: %lld, clock: %llu\n", m_frame.pts, m_frame.dts, clock);
  133. }
  134. else if (0 == strcmp("MP4A-LATM", m->rtp.encoding) || 0 == strcmp("MPEG4-GENERIC", m->rtp.encoding))
  135. {
  136. // add ADTS header
  137. memcpy(m_packet, pkt->data, pkt->size);
  138. bytes = pkt->size;
  139. //printf("[A] pts: %lld, dts: %lld, clock: %llu\n", m_frame.pts, m_frame.dts, clock);
  140. }
  141. else
  142. {
  143. assert(0);
  144. }
  145. if (-1 == m->dts_first)
  146. m->dts_first = pkt->pts;
  147. m->dts_last = pkt->pts;
  148. uint32_t timestamp = m->rtp.timestamp + (uint32_t)((m->dts_last - m->dts_first) * (m->rtp.frequency / 1000) /*kHz*/);
  149. //printf("[%d] pts: %lld, dts: %lld, clock: %u\n", pkt->stream, pkt->pts, pkt->dts, timestamp);
  150. rtp_payload_encode_input(m->rtp.encoder, m_packet, bytes, timestamp);
  151. avpacket_queue_pop(m->pkts);
  152. sendframe = 1;
  153. // preload next packet
  154. FetchNextPacket();
  155. }
  156. return sendframe ? 1 : 0;
  157. }
  158. int MP4FileSource::Pause()
  159. {
  160. m_status = 2;
  161. m_clock = 0;
  162. m_dts = -1;
  163. return 0;
  164. }
  165. int MP4FileSource::Seek(int64_t pos)
  166. {
  167. // update timestamp
  168. for (int i = 0; i < m_count; i++)
  169. {
  170. //m_media[i].dts = -1;
  171. //m_media[i].timestamp += 1;
  172. if (-1 != m_media[i].dts_first)
  173. m_media[i].rtp.timestamp += (uint32_t)(m_media[i].dts_last - m_media[i].dts_first + 1);
  174. m_media[i].dts_first = -1;
  175. avpacket_queue_clear(m_media[i].pkts); // clear buffered frame
  176. }
  177. m_dts = pos;
  178. m_clock = 0;
  179. return mov_reader_seek(m_reader, &m_dts);
  180. }
  181. int MP4FileSource::SetSpeed(double speed)
  182. {
  183. m_speed = speed;
  184. return 0;
  185. }
  186. int MP4FileSource::GetDuration(int64_t& duration) const
  187. {
  188. if (m_reader)
  189. {
  190. duration = (int64_t)mov_reader_getduration(m_reader);
  191. return 0;
  192. }
  193. return -1;
  194. }
  195. int MP4FileSource::GetSDPMedia(std::string& sdp) const
  196. {
  197. sdp = m_sdp;
  198. return m_reader ? 0 : -1;
  199. }
  200. int MP4FileSource::GetRTPInfo(const char* uri, char *rtpinfo, size_t bytes) const
  201. {
  202. int n = 0;
  203. uint16_t seq;
  204. uint32_t timestamp;
  205. // RTP-Info: url=rtsp://foo.com/bar.avi/streamid=0;seq=45102,
  206. // url=rtsp://foo.com/bar.avi/streamid=1;seq=30211
  207. for (int i = 0; i < m_count; i++)
  208. {
  209. const struct media_t* m = &m_media[i];
  210. rtp_payload_encode_getinfo(m->rtp.encoder, &seq, &timestamp);
  211. if (i > 0)
  212. rtpinfo[n++] = ',';
  213. n += snprintf(rtpinfo + n, bytes - n, "url=%s/track%d;seq=%hu;rtptime=%u", uri, m->track, seq, (unsigned int)m->rtp.timestamp);
  214. }
  215. return 0;
  216. }
  217. void MP4FileSource::MP4OnVideo(void* param, uint32_t track, uint8_t object, int /*width*/, int /*height*/, const void* extra, size_t bytes)
  218. {
  219. assert(track > 0);
  220. int n = 0;
  221. MP4FileSource* self = (MP4FileSource*)param;
  222. struct media_t* m = &self->m_media[self->m_count++];
  223. memset(&m->rtp, 0, sizeof(m->rtp));
  224. m->pkts = avpacket_queue_create(100);
  225. m->track = track;
  226. m->rtcp_clock = 0;
  227. m->dts_first = -1;
  228. m->dts_last = -1;
  229. if (MOV_OBJECT_H264 == object)
  230. {
  231. mpeg4_avc_decoder_configuration_record_load((const uint8_t*)extra, bytes, &self->m_avc);
  232. n = rtp_sender_init_video(&m->rtp, "RTP/AVP", 0, RTP_PAYLOAD_H264, "H264", 90000, extra, bytes);
  233. }
  234. else if (MOV_OBJECT_HEVC == object)
  235. {
  236. mpeg4_hevc_decoder_configuration_record_load((const uint8_t*)extra, bytes, &self->m_hevc);
  237. n = rtp_sender_init_video(&m->rtp, "RTP/AVP", 0, RTP_PAYLOAD_H265, "H265", 90000, extra, bytes);
  238. }
  239. else if (MOV_OBJECT_MP4V == object)
  240. {
  241. n = rtp_sender_init_video(&m->rtp, "RTP/AVP", 0, RTP_PAYLOAD_MP4V, "MP4V-ES", 90000, extra, bytes);
  242. }
  243. else
  244. {
  245. assert(0);
  246. return;
  247. }
  248. m->rtp.onpacket = OnRTPPacket;
  249. m->rtp.param = m;
  250. n = snprintf((char*)self->m_packet, sizeof(self->m_packet), "%.*sa=control:track%d\n", n, m->rtp.buffer, m->track);
  251. self->m_sdp += (const char*)self->m_packet;
  252. }
  253. void MP4FileSource::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)
  254. {
  255. assert(track > 0);
  256. int n = 0;
  257. MP4FileSource* self = (MP4FileSource*)param;
  258. struct media_t* m = &self->m_media[self->m_count++];
  259. memset(&m->rtp, 0, sizeof(m->rtp));
  260. m->pkts = avpacket_queue_create(100);
  261. m->track = track;
  262. m->rtcp_clock = 0;
  263. m->dts_first = -1;
  264. m->dts_last = -1;
  265. if (MOV_OBJECT_AAC == object || MOV_OBJECT_AAC_LOW == object)
  266. {
  267. mpeg4_aac_audio_specific_config_load((const uint8_t*)extra, bytes, &self->m_aac);
  268. if (1)
  269. {
  270. // RFC 6416
  271. n = rtp_sender_init_audio(&m->rtp, "RTP/AVP", 0, RTP_PAYLOAD_LATM, "MP4A-LATM", sample_rate, self->m_aac.channel_configuration, extra, bytes);
  272. }
  273. else
  274. {
  275. // RFC 3640 3.3.1. General (p21)
  276. n = rtp_sender_init_audio(&m->rtp, "RTP/AVP", 0, RTP_PAYLOAD_MP4A, "MP4A-GENERIC", sample_rate, channel_count, extra, bytes);
  277. }
  278. }
  279. else if (MOV_OBJECT_OPUS == object)
  280. {
  281. // RFC7587 RTP Payload Format for the Opus Speech and Audio Codec
  282. n = rtp_sender_init_audio(&m->rtp, "RTP/AVP", 0, RTP_PAYLOAD_OPUS, "opus", sample_rate, channel_count, extra, bytes);
  283. }
  284. else if (MOV_OBJECT_G711u == object)
  285. {
  286. n = rtp_sender_init_audio(&m->rtp, "RTP/AVP", 0, RTP_PAYLOAD_PCMU, "PCMU", 8000, 1, extra, bytes);
  287. }
  288. else if (MOV_OBJECT_G711a == object)
  289. {
  290. n = rtp_sender_init_audio(&m->rtp, "RTP/AVP", 0, RTP_PAYLOAD_PCMA, "PCMA", 8000, 1, extra, bytes);
  291. }
  292. else
  293. {
  294. assert(0);
  295. return;
  296. }
  297. m->rtp.param = m;
  298. m->rtp.onpacket = OnRTPPacket;
  299. n = snprintf((char*)self->m_packet, sizeof(self->m_packet), "%.*sa=control:track%d\n", n, m->rtp.buffer, m->track);
  300. self->m_sdp += (const char*)self->m_packet;
  301. }
  302. void MP4FileSource::MP4OnRead(void* param, uint32_t track, const void* buffer, size_t bytes, int64_t pts, int64_t dts, int /*flags*/)
  303. {
  304. MP4FileSource *self = (MP4FileSource *)param;
  305. std::shared_ptr<struct avpacket_t> pkt(avpacket_alloc((int)bytes), avpacket_release);
  306. memcpy(pkt->data, buffer, bytes);
  307. //pkt->codecid = track;
  308. pkt->pts = pts;
  309. pkt->dts = dts;
  310. for (int i = 0; i < self->m_count; i++)
  311. {
  312. struct media_t* m = &self->m_media[i];
  313. if (m->track == (int)track)
  314. {
  315. // TODO: overflow
  316. assert(0 == avpacket_queue_push(m->pkts, pkt.get()));
  317. //assert(0 == avpacket_queue_push_wait(m->pkts, &pkt, 1000000));
  318. break;
  319. }
  320. }
  321. }
  322. void MP4FileSource::OnRTCPEvent(const struct rtcp_msg_t* msg)
  323. {
  324. msg;
  325. }
  326. void MP4FileSource::OnRTCPEvent(void* param, const struct rtcp_msg_t* msg)
  327. {
  328. MP4FileSource *self = (MP4FileSource *)param;
  329. self->OnRTCPEvent(msg);
  330. }
  331. int MP4FileSource::SendBye()
  332. {
  333. char rtcp[1024] = { 0 };
  334. for (int i = 0; i < m_count; i++)
  335. {
  336. struct media_t* m = &m_media[i];
  337. size_t n = rtp_rtcp_bye(m->rtp.rtp, rtcp, sizeof(rtcp));
  338. // send RTCP packet
  339. m->transport->Send(true, rtcp, n);
  340. }
  341. return 0;
  342. }
  343. int MP4FileSource::SendRTCP(uint64_t clock)
  344. {
  345. char rtcp[1024] = { 0 };
  346. // make sure have sent RTP packet
  347. for (int i = 0; i < m_count; i++)
  348. {
  349. struct media_t* m = &m_media[i];
  350. int interval = rtp_rtcp_interval(m->rtp.rtp);
  351. if (0 == m->rtcp_clock || m->rtcp_clock + interval < clock)
  352. {
  353. size_t n = rtp_rtcp_report(m->rtp.rtp, rtcp, sizeof(rtcp));
  354. // send RTCP packet
  355. m->transport->Send(true, rtcp, n);
  356. m->rtcp_clock = clock;
  357. }
  358. }
  359. return 0;
  360. }
  361. int MP4FileSource::OnRTPPacket(void* param, const void *packet, int bytes, uint32_t /*timestamp*/, int /*flags*/)
  362. {
  363. struct media_t* m = (struct media_t*)param;
  364. int r = m->transport->Send(false, packet, bytes);
  365. if (r != bytes)
  366. {
  367. assert(0);
  368. return -1;
  369. }
  370. return rtp_onsend(m->rtp.rtp, packet, bytes/*, time*/);
  371. }