1
0

rtphint.cpp 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343
  1. /*
  2. * The contents of this file are subject to the Mozilla Public
  3. * License Version 1.1 (the "License"); you may not use this file
  4. * except in compliance with the License. You may obtain a copy of
  5. * the License at http://www.mozilla.org/MPL/
  6. *
  7. * Software distributed under the License is distributed on an "AS
  8. * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9. * implied. See the License for the specific language governing
  10. * rights and limitations under the License.
  11. *
  12. * The Original Code is MPEG4IP.
  13. *
  14. * The Initial Developer of the Original Code is Cisco Systems Inc.
  15. * Portions created by Cisco Systems Inc. are
  16. * Copyright (C) Cisco Systems Inc. 2001. All Rights Reserved.
  17. *
  18. * Contributor(s):
  19. * Dave Mackie [email protected]
  20. */
  21. #include "mp4common.h"
  22. /* rtp hint track operations */
  23. MP4RtpHintTrack::MP4RtpHintTrack(MP4File* pFile, MP4Atom* pTrakAtom)
  24. : MP4Track(pFile, pTrakAtom)
  25. {
  26. m_pRefTrack = NULL;
  27. m_pRtpMapProperty = NULL;
  28. m_pPayloadNumberProperty = NULL;
  29. m_pMaxPacketSizeProperty = NULL;
  30. m_pSnroProperty = NULL;
  31. m_pTsroProperty = NULL;
  32. m_rtpSequenceStart = 0;
  33. m_rtpTimestampStart = 0;
  34. m_pReadHint = NULL;
  35. m_pReadHintSample = NULL;
  36. m_readHintSampleSize = 0;
  37. m_readHintTimestamp = 0;
  38. m_pWriteHint = NULL;
  39. m_writeHintId = MP4_INVALID_SAMPLE_ID;
  40. m_writePacketId = 0;
  41. m_pTrpy = NULL;
  42. m_pNump = NULL;
  43. m_pTpyl = NULL;
  44. m_pMaxr = NULL;
  45. m_pDmed = NULL;
  46. m_pDimm = NULL;
  47. m_pPmax = NULL;
  48. m_pDmax = NULL;
  49. m_pMaxPdu = NULL;
  50. m_pAvgPdu = NULL;
  51. m_pMaxBitRate = NULL;
  52. m_pAvgBitRate = NULL;
  53. m_thisSec = 0;
  54. m_bytesThisSec = 0;
  55. m_bytesThisHint = 0;
  56. m_bytesThisPacket = 0;
  57. }
  58. MP4RtpHintTrack::~MP4RtpHintTrack()
  59. {
  60. delete m_pReadHint;
  61. delete m_pReadHintSample;
  62. delete m_pWriteHint;
  63. }
  64. void MP4RtpHintTrack::InitRefTrack()
  65. {
  66. if (m_pRefTrack == NULL) {
  67. MP4Integer32Property* pRefTrackIdProperty = NULL;
  68. (void)m_pTrakAtom->FindProperty(
  69. "trak.tref.hint.entries[0].trackId",
  70. (MP4Property**)&pRefTrackIdProperty);
  71. ASSERT(pRefTrackIdProperty);
  72. m_pRefTrack = m_pFile->GetTrack(pRefTrackIdProperty->GetValue());
  73. }
  74. }
  75. void MP4RtpHintTrack::InitRtpStart()
  76. {
  77. struct timeval tv;
  78. (void)gettimeofday(&tv, NULL);
  79. srandom((tv.tv_usec << 12) | (tv.tv_sec & 0xFFF));
  80. ASSERT(m_pTrakAtom);
  81. (void)m_pTrakAtom->FindProperty(
  82. "trak.udta.hnti.rtp .snro.offset",
  83. (MP4Property**)&m_pSnroProperty);
  84. if (m_pSnroProperty) {
  85. m_rtpSequenceStart = m_pSnroProperty->GetValue();
  86. } else {
  87. m_rtpSequenceStart = random();
  88. }
  89. (void)m_pTrakAtom->FindProperty(
  90. "trak.udta.hnti.rtp .tsro.offset",
  91. (MP4Property**)&m_pTsroProperty);
  92. if (m_pTsroProperty) {
  93. m_rtpTimestampStart = m_pTsroProperty->GetValue();
  94. } else {
  95. m_rtpTimestampStart = random();
  96. }
  97. }
  98. void MP4RtpHintTrack::ReadHint(
  99. MP4SampleId hintSampleId,
  100. u_int16_t* pNumPackets)
  101. {
  102. if (m_pRefTrack == NULL) {
  103. InitRefTrack();
  104. InitRtpStart();
  105. }
  106. // dispose of any old hint
  107. delete m_pReadHint;
  108. m_pReadHint = NULL;
  109. delete m_pReadHintSample;
  110. m_pReadHintSample = NULL;
  111. m_readHintSampleSize = 0;
  112. // read the desired hint sample into memory
  113. ReadSample(
  114. hintSampleId,
  115. &m_pReadHintSample,
  116. &m_readHintSampleSize,
  117. &m_readHintTimestamp);
  118. m_pFile->EnableMemoryBuffer(m_pReadHintSample, m_readHintSampleSize);
  119. m_pReadHint = new MP4RtpHint(this);
  120. m_pReadHint->Read(m_pFile);
  121. m_pFile->DisableMemoryBuffer();
  122. if (pNumPackets) {
  123. *pNumPackets = GetHintNumberOfPackets();
  124. }
  125. }
  126. u_int16_t MP4RtpHintTrack::GetHintNumberOfPackets()
  127. {
  128. if (m_pReadHint == NULL) {
  129. throw new MP4Error("no hint has been read",
  130. "MP4GetRtpHintNumberOfPackets");
  131. }
  132. return m_pReadHint->GetNumberOfPackets();
  133. }
  134. bool MP4RtpHintTrack::GetPacketBFrame(u_int16_t packetIndex)
  135. {
  136. if (m_pReadHint == NULL) {
  137. throw new MP4Error("no hint has been read",
  138. "MP4GetRtpPacketBFrame");
  139. }
  140. MP4RtpPacket* pPacket =
  141. m_pReadHint->GetPacket(packetIndex);
  142. return pPacket->IsBFrame();
  143. }
  144. u_int16_t MP4RtpHintTrack::GetPacketTransmitOffset(u_int16_t packetIndex)
  145. {
  146. if (m_pReadHint == NULL) {
  147. throw new MP4Error("no hint has been read",
  148. "MP4GetRtpPacketTransmitOffset");
  149. }
  150. MP4RtpPacket* pPacket =
  151. m_pReadHint->GetPacket(packetIndex);
  152. return pPacket->GetTransmitOffset();
  153. }
  154. void MP4RtpHintTrack::ReadPacket(
  155. u_int16_t packetIndex,
  156. u_int8_t** ppBytes,
  157. u_int32_t* pNumBytes,
  158. u_int32_t ssrc,
  159. bool addHeader,
  160. bool addPayload)
  161. {
  162. if (m_pReadHint == NULL) {
  163. throw new MP4Error("no hint has been read",
  164. "MP4ReadRtpPacket");
  165. }
  166. if (!addHeader && !addPayload) {
  167. throw new MP4Error("no data requested",
  168. "MP4ReadRtpPacket");
  169. }
  170. MP4RtpPacket* pPacket =
  171. m_pReadHint->GetPacket(packetIndex);
  172. *pNumBytes = 0;
  173. if (addHeader) {
  174. *pNumBytes += 12;
  175. }
  176. if (addPayload) {
  177. *pNumBytes += pPacket->GetDataSize();
  178. }
  179. // if needed, allocate the packet memory
  180. bool buffer_malloc = false;
  181. if (*ppBytes == NULL) {
  182. *ppBytes = (u_int8_t*)MP4Malloc(*pNumBytes);
  183. buffer_malloc = true;
  184. }
  185. try {
  186. u_int8_t* pDest = *ppBytes;
  187. if (addHeader) {
  188. *pDest++ =
  189. 0x80 | (pPacket->GetPBit() << 5) | (pPacket->GetXBit() << 4);
  190. *pDest++ =
  191. (pPacket->GetMBit() << 7) | pPacket->GetPayload();
  192. *((u_int16_t*)pDest) =
  193. htons(m_rtpSequenceStart + pPacket->GetSequenceNumber());
  194. pDest += 2;
  195. *((u_int32_t*)pDest) =
  196. htonl(m_rtpTimestampStart + (u_int32_t)m_readHintTimestamp);
  197. pDest += 4;
  198. *((u_int32_t*)pDest) =
  199. htonl(ssrc);
  200. pDest += 4;
  201. }
  202. if (addPayload) {
  203. pPacket->GetData(pDest);
  204. }
  205. }
  206. catch (MP4Error* e) {
  207. if (buffer_malloc) {
  208. MP4Free(*ppBytes);
  209. *ppBytes = NULL;
  210. }
  211. throw e;
  212. }
  213. VERBOSE_READ_HINT(m_pFile->GetVerbosity(),
  214. printf("ReadPacket: %u ", packetIndex);
  215. MP4HexDump(*ppBytes, *pNumBytes););
  216. }
  217. MP4Timestamp MP4RtpHintTrack::GetRtpTimestampStart()
  218. {
  219. if (m_pRefTrack == NULL) {
  220. InitRefTrack();
  221. InitRtpStart();
  222. }
  223. return m_rtpTimestampStart;
  224. }
  225. void MP4RtpHintTrack::SetRtpTimestampStart(MP4Timestamp start)
  226. {
  227. if (!m_pTsroProperty) {
  228. MP4Atom* pTsroAtom =
  229. m_pFile->AddDescendantAtoms(m_pTrakAtom, "udta.hnti.rtp .tsro");
  230. ASSERT(pTsroAtom);
  231. (void)pTsroAtom->FindProperty("offset",
  232. (MP4Property**)&m_pTsroProperty);
  233. ASSERT(m_pTsroProperty);
  234. }
  235. m_pTsroProperty->SetValue(start);
  236. m_rtpTimestampStart = start;
  237. }
  238. void MP4RtpHintTrack::InitPayload()
  239. {
  240. ASSERT(m_pTrakAtom);
  241. if (m_pRtpMapProperty == NULL) {
  242. (void)m_pTrakAtom->FindProperty(
  243. "trak.udta.hinf.payt.rtpMap",
  244. (MP4Property**)&m_pRtpMapProperty);
  245. }
  246. if (m_pPayloadNumberProperty == NULL) {
  247. (void)m_pTrakAtom->FindProperty(
  248. "trak.udta.hinf.payt.payloadNumber",
  249. (MP4Property**)&m_pPayloadNumberProperty);
  250. }
  251. if (m_pMaxPacketSizeProperty == NULL) {
  252. (void)m_pTrakAtom->FindProperty(
  253. "trak.mdia.minf.stbl.stsd.rtp .maxPacketSize",
  254. (MP4Property**)&m_pMaxPacketSizeProperty);
  255. }
  256. }
  257. void MP4RtpHintTrack::GetPayload(
  258. char** ppPayloadName,
  259. u_int8_t* pPayloadNumber,
  260. u_int16_t* pMaxPayloadSize,
  261. char **ppEncodingParams)
  262. {
  263. const char* pRtpMap;
  264. const char* pSlash;
  265. u_int32_t length;
  266. InitPayload();
  267. if (ppPayloadName || ppEncodingParams) {
  268. if (ppPayloadName)
  269. *ppPayloadName = NULL;
  270. if (ppEncodingParams)
  271. *ppEncodingParams = NULL;
  272. if (m_pRtpMapProperty) {
  273. pRtpMap = m_pRtpMapProperty->GetValue();
  274. pSlash = strchr(pRtpMap, '/');
  275. if (pSlash) {
  276. length = pSlash - pRtpMap;
  277. } else {
  278. length = (u_int32_t)strlen(pRtpMap);
  279. }
  280. if (ppPayloadName) {
  281. *ppPayloadName = (char*)MP4Calloc(length + 1);
  282. strncpy(*ppPayloadName, pRtpMap, length);
  283. }
  284. if (pSlash && ppEncodingParams) {
  285. pSlash++;
  286. pSlash = strchr(pSlash, '/');
  287. if (pSlash != NULL) {
  288. pSlash++;
  289. if (pSlash != '\0') {
  290. length = (u_int32_t)strlen(pRtpMap) - (pSlash - pRtpMap);
  291. *ppEncodingParams = (char *)MP4Calloc(length + 1);
  292. strncpy(*ppEncodingParams, pSlash, length);
  293. }
  294. }
  295. }
  296. }
  297. }
  298. if (pPayloadNumber) {
  299. if (m_pPayloadNumberProperty) {
  300. *pPayloadNumber = m_pPayloadNumberProperty->GetValue();
  301. } else {
  302. *pPayloadNumber = 0;
  303. }
  304. }
  305. if (pMaxPayloadSize) {
  306. if (m_pMaxPacketSizeProperty) {
  307. *pMaxPayloadSize = m_pMaxPacketSizeProperty->GetValue();
  308. } else {
  309. *pMaxPayloadSize = 0;
  310. }
  311. }
  312. }
  313. void MP4RtpHintTrack::SetPayload(
  314. const char* payloadName,
  315. u_int8_t payloadNumber,
  316. u_int16_t maxPayloadSize,
  317. const char *encoding_parms,
  318. bool include_rtp_map,
  319. bool include_mpeg4_esid)
  320. {
  321. InitRefTrack();
  322. InitPayload();
  323. ASSERT(m_pRtpMapProperty);
  324. ASSERT(m_pPayloadNumberProperty);
  325. ASSERT(m_pMaxPacketSizeProperty);
  326. size_t len = strlen(payloadName) + 16;
  327. if (encoding_parms != NULL) {
  328. size_t temp = strlen(encoding_parms);
  329. if (temp == 0) {
  330. encoding_parms = NULL;
  331. } else {
  332. len += temp;
  333. }
  334. }
  335. char* rtpMapBuf = (char*)MP4Malloc(len);
  336. snprintf(rtpMapBuf, len, "%s/%u%c%s",
  337. payloadName,
  338. GetTimeScale(),
  339. encoding_parms != NULL ? '/' : '\0',
  340. encoding_parms == NULL ? "" : encoding_parms);
  341. m_pRtpMapProperty->SetValue(rtpMapBuf);
  342. m_pPayloadNumberProperty->SetValue(payloadNumber);
  343. if (maxPayloadSize == 0) {
  344. maxPayloadSize = 1460;
  345. }
  346. m_pMaxPacketSizeProperty->SetValue(maxPayloadSize);
  347. // set sdp media type
  348. const char* sdpMediaType;
  349. if (!strcmp(m_pRefTrack->GetType(), MP4_AUDIO_TRACK_TYPE)) {
  350. sdpMediaType = "audio";
  351. } else if (!strcmp(m_pRefTrack->GetType(), MP4_VIDEO_TRACK_TYPE)) {
  352. sdpMediaType = "video";
  353. } else if (!strcmp(m_pRefTrack->GetType(), MP4_CNTL_TRACK_TYPE)) {
  354. sdpMediaType = "control";
  355. } else {
  356. sdpMediaType = "application";
  357. }
  358. uint32_t maxlen =
  359. (u_int32_t)(strlen(sdpMediaType) + strlen(rtpMapBuf) + 256);
  360. char* sdpBuf = (char*)MP4Malloc(maxlen);
  361. uint32_t buflen;
  362. buflen = snprintf(sdpBuf, maxlen,
  363. "m=%s 0 RTP/AVP %u\015\012"
  364. "a=control:trackID=%u\015\012",
  365. sdpMediaType, payloadNumber,
  366. m_trackId);
  367. if (include_rtp_map) {
  368. buflen += snprintf(sdpBuf + buflen, maxlen - buflen,
  369. "a=rtpmap:%u %s\015\012",
  370. payloadNumber, rtpMapBuf);
  371. }
  372. if (include_mpeg4_esid) {
  373. snprintf(sdpBuf + buflen, maxlen - buflen,
  374. "a=mpeg4-esid:%u\015\012",
  375. m_pRefTrack->GetId());
  376. }
  377. MP4StringProperty* pSdpProperty = NULL;
  378. (void)m_pTrakAtom->FindProperty("trak.udta.hnti.sdp .sdpText",
  379. (MP4Property**)&pSdpProperty);
  380. ASSERT(pSdpProperty);
  381. pSdpProperty->SetValue(sdpBuf);
  382. // cleanup
  383. MP4Free(rtpMapBuf);
  384. MP4Free(sdpBuf);
  385. }
  386. void MP4RtpHintTrack::AddHint(bool isBFrame, u_int32_t timestampOffset)
  387. {
  388. // on first hint, need to lookup the reference track
  389. if (m_writeHintId == MP4_INVALID_SAMPLE_ID) {
  390. InitRefTrack();
  391. InitStats();
  392. }
  393. if (m_pWriteHint) {
  394. throw new MP4Error("unwritten hint is still pending", "MP4AddRtpHint");
  395. }
  396. m_pWriteHint = new MP4RtpHint(this);
  397. m_pWriteHint->SetBFrame(isBFrame);
  398. m_pWriteHint->SetTimestampOffset(timestampOffset);
  399. m_bytesThisHint = 0;
  400. m_writeHintId++;
  401. }
  402. void MP4RtpHintTrack::AddPacket(bool setMbit, int32_t transmitOffset)
  403. {
  404. if (m_pWriteHint == NULL) {
  405. throw new MP4Error("no hint pending", "MP4RtpAddPacket");
  406. }
  407. MP4RtpPacket* pPacket = m_pWriteHint->AddPacket();
  408. ASSERT(m_pPayloadNumberProperty);
  409. pPacket->Set(
  410. m_pPayloadNumberProperty->GetValue(),
  411. m_writePacketId++,
  412. setMbit);
  413. pPacket->SetTransmitOffset(transmitOffset);
  414. m_bytesThisHint += 12;
  415. if (m_bytesThisPacket > m_pPmax->GetValue()) {
  416. m_pPmax->SetValue(m_bytesThisPacket);
  417. }
  418. m_bytesThisPacket = 12;
  419. m_pNump->IncrementValue();
  420. m_pTrpy->IncrementValue(12); // RTP packet header size
  421. }
  422. void MP4RtpHintTrack::AddImmediateData(
  423. const u_int8_t* pBytes,
  424. u_int32_t numBytes)
  425. {
  426. if (m_pWriteHint == NULL) {
  427. throw new MP4Error("no hint pending", "MP4RtpAddImmediateData");
  428. }
  429. MP4RtpPacket* pPacket = m_pWriteHint->GetCurrentPacket();
  430. if (pPacket == NULL) {
  431. throw new MP4Error("no packet pending", "MP4RtpAddImmediateData");
  432. }
  433. if (pBytes == NULL || numBytes == 0) {
  434. throw new MP4Error("no data",
  435. "AddImmediateData");
  436. }
  437. if (numBytes > 14) {
  438. throw new MP4Error("data size is larger than 14 bytes",
  439. "AddImmediateData");
  440. }
  441. MP4RtpImmediateData* pData = new MP4RtpImmediateData(pPacket);
  442. pData->Set(pBytes, numBytes);
  443. pPacket->AddData(pData);
  444. m_bytesThisHint += numBytes;
  445. m_bytesThisPacket += numBytes;
  446. m_pDimm->IncrementValue(numBytes);
  447. m_pTpyl->IncrementValue(numBytes);
  448. m_pTrpy->IncrementValue(numBytes);
  449. }
  450. void MP4RtpHintTrack::AddSampleData(
  451. MP4SampleId sampleId,
  452. u_int32_t dataOffset,
  453. u_int32_t dataLength)
  454. {
  455. if (m_pWriteHint == NULL) {
  456. throw new MP4Error("no hint pending", "MP4RtpAddSampleData");
  457. }
  458. MP4RtpPacket* pPacket = m_pWriteHint->GetCurrentPacket();
  459. if (pPacket == NULL) {
  460. throw new MP4Error("no packet pending", "MP4RtpAddSampleData");
  461. }
  462. MP4RtpSampleData* pData = new MP4RtpSampleData(pPacket);
  463. pData->SetReferenceSample(sampleId, dataOffset, dataLength);
  464. pPacket->AddData(pData);
  465. m_bytesThisHint += dataLength;
  466. m_bytesThisPacket += dataLength;
  467. m_pDmed->IncrementValue(dataLength);
  468. m_pTpyl->IncrementValue(dataLength);
  469. m_pTrpy->IncrementValue(dataLength);
  470. }
  471. void MP4RtpHintTrack::AddESConfigurationPacket()
  472. {
  473. if (m_pWriteHint == NULL) {
  474. throw new MP4Error("no hint pending",
  475. "MP4RtpAddESConfigurationPacket");
  476. }
  477. u_int8_t* pConfig = NULL;
  478. u_int32_t configSize = 0;
  479. m_pFile->GetTrackESConfiguration(m_pRefTrack->GetId(),
  480. &pConfig, &configSize);
  481. if (pConfig == NULL) {
  482. return;
  483. }
  484. ASSERT(m_pMaxPacketSizeProperty);
  485. if (configSize > m_pMaxPacketSizeProperty->GetValue()) {
  486. throw new MP4Error("ES configuration is too large for RTP payload",
  487. "MP4RtpAddESConfigurationPacket");
  488. }
  489. AddPacket(false);
  490. MP4RtpPacket* pPacket = m_pWriteHint->GetCurrentPacket();
  491. ASSERT(pPacket);
  492. // This is ugly!
  493. // To get the ES configuration data somewhere known
  494. // we create a sample data reference that points to
  495. // this hint track (not the media track)
  496. // and this sample of the hint track
  497. // the offset into this sample is filled in during the write process
  498. MP4RtpSampleData* pData = new MP4RtpSampleData(pPacket);
  499. pData->SetEmbeddedImmediate(m_writeSampleId, pConfig, configSize);
  500. pPacket->AddData(pData);
  501. m_bytesThisHint += configSize;
  502. m_bytesThisPacket += configSize;
  503. m_pTpyl->IncrementValue(configSize);
  504. m_pTrpy->IncrementValue(configSize);
  505. }
  506. void MP4RtpHintTrack::WriteHint(MP4Duration duration, bool isSyncSample)
  507. {
  508. if (m_pWriteHint == NULL) {
  509. throw new MP4Error("no hint pending", "MP4WriteRtpHint");
  510. }
  511. u_int8_t* pBytes;
  512. u_int64_t numBytes;
  513. m_pFile->EnableMemoryBuffer();
  514. m_pWriteHint->Write(m_pFile);
  515. m_pFile->DisableMemoryBuffer(&pBytes, &numBytes);
  516. WriteSample(pBytes, numBytes, duration, 0, isSyncSample);
  517. MP4Free(pBytes);
  518. // update statistics
  519. if (m_bytesThisPacket > m_pPmax->GetValue()) {
  520. m_pPmax->SetValue(m_bytesThisPacket);
  521. }
  522. if (duration > m_pDmax->GetValue()) {
  523. m_pDmax->SetValue(duration);
  524. }
  525. MP4Timestamp startTime;
  526. GetSampleTimes(m_writeHintId, &startTime, NULL);
  527. if (startTime < m_thisSec + GetTimeScale()) {
  528. m_bytesThisSec += m_bytesThisHint;
  529. } else {
  530. if (m_bytesThisSec > m_pMaxr->GetValue()) {
  531. m_pMaxr->SetValue(m_bytesThisSec);
  532. }
  533. m_thisSec = startTime - (startTime % GetTimeScale());
  534. m_bytesThisSec = m_bytesThisHint;
  535. }
  536. // cleanup
  537. delete m_pWriteHint;
  538. m_pWriteHint = NULL;
  539. }
  540. void MP4RtpHintTrack::FinishWrite()
  541. {
  542. if (m_writeHintId != MP4_INVALID_SAMPLE_ID) {
  543. m_pMaxPdu->SetValue(m_pPmax->GetValue());
  544. if (m_pNump->GetValue()) {
  545. m_pAvgPdu->SetValue(m_pTrpy->GetValue() / m_pNump->GetValue());
  546. }
  547. m_pMaxBitRate->SetValue(m_pMaxr->GetValue() * 8);
  548. if (GetDuration()) {
  549. m_pAvgBitRate->SetValue(
  550. m_pTrpy->GetValue() * 8 * GetTimeScale() / GetDuration());
  551. }
  552. }
  553. MP4Track::FinishWrite();
  554. }
  555. void MP4RtpHintTrack::InitStats()
  556. {
  557. MP4Atom* pHinfAtom = m_pTrakAtom->FindAtomMP4("trak.udta.hinf");
  558. ASSERT(pHinfAtom);
  559. (void)pHinfAtom->FindProperty("hinf.trpy.bytes", (MP4Property**)&m_pTrpy);
  560. (void)pHinfAtom->FindProperty("hinf.nump.packets", (MP4Property**)&m_pNump);
  561. (void)pHinfAtom->FindProperty("hinf.tpyl.bytes", (MP4Property**)&m_pTpyl);
  562. (void)pHinfAtom->FindProperty("hinf.maxr.bytes", (MP4Property**)&m_pMaxr);
  563. (void)pHinfAtom->FindProperty("hinf.dmed.bytes", (MP4Property**)&m_pDmed);
  564. (void)pHinfAtom->FindProperty("hinf.dimm.bytes", (MP4Property**)&m_pDimm);
  565. (void)pHinfAtom->FindProperty("hinf.pmax.bytes", (MP4Property**)&m_pPmax);
  566. (void)pHinfAtom->FindProperty("hinf.dmax.milliSecs", (MP4Property**)&m_pDmax);
  567. MP4Atom* pHmhdAtom = m_pTrakAtom->FindAtomMP4("trak.mdia.minf.hmhd");
  568. ASSERT(pHmhdAtom);
  569. (void)pHmhdAtom->FindProperty("hmhd.maxPduSize", (MP4Property**)&m_pMaxPdu);
  570. (void)pHmhdAtom->FindProperty("hmhd.avgPduSize", (MP4Property**)&m_pAvgPdu);
  571. (void)pHmhdAtom->FindProperty("hmhd.maxBitRate", (MP4Property**)&m_pMaxBitRate);
  572. (void)pHmhdAtom->FindProperty("hmhd.avgBitRate", (MP4Property**)&m_pAvgBitRate);
  573. MP4Integer32Property* pMaxrPeriod = NULL;
  574. (void)pHinfAtom->FindProperty("hinf.maxr.granularity",
  575. (MP4Property**)&pMaxrPeriod);
  576. if (pMaxrPeriod) {
  577. pMaxrPeriod->SetValue(1000); // 1 second
  578. }
  579. }
  580. MP4RtpHint::MP4RtpHint(MP4RtpHintTrack* pTrack)
  581. {
  582. m_pTrack = pTrack;
  583. m_isBFrame = false;
  584. m_timestampOffset = 0;
  585. AddProperty( /* 0 */
  586. new MP4Integer16Property("packetCount"));
  587. AddProperty( /* 1 */
  588. new MP4Integer16Property("reserved"));
  589. }
  590. MP4RtpHint::~MP4RtpHint()
  591. {
  592. for (u_int32_t i = 0; i < m_rtpPackets.Size(); i++) {
  593. delete m_rtpPackets[i];
  594. }
  595. }
  596. MP4RtpPacket* MP4RtpHint::AddPacket()
  597. {
  598. MP4RtpPacket* pPacket = new MP4RtpPacket(this);
  599. m_rtpPackets.Add(pPacket);
  600. // packetCount property
  601. ((MP4Integer16Property*)m_pProperties[0])->IncrementValue();
  602. pPacket->SetBFrame(m_isBFrame);
  603. pPacket->SetTimestampOffset(m_timestampOffset);
  604. return pPacket;
  605. }
  606. void MP4RtpHint::Read(MP4File* pFile)
  607. {
  608. // call base class Read for required properties
  609. MP4Container::Read(pFile);
  610. u_int16_t numPackets =
  611. ((MP4Integer16Property*)m_pProperties[0])->GetValue();
  612. for (u_int16_t i = 0; i < numPackets; i++) {
  613. MP4RtpPacket* pPacket = new MP4RtpPacket(this);
  614. m_rtpPackets.Add(pPacket);
  615. pPacket->Read(pFile);
  616. }
  617. }
  618. void MP4RtpHint::Write(MP4File* pFile)
  619. {
  620. u_int64_t hintStartPos = pFile->GetPosition();
  621. MP4Container::Write(pFile);
  622. u_int64_t packetStartPos = pFile->GetPosition();
  623. u_int32_t i;
  624. // first write out packet (and data) entries
  625. for (i = 0; i < m_rtpPackets.Size(); i++) {
  626. m_rtpPackets[i]->Write(pFile);
  627. }
  628. // now let packets write their extra data into the hint sample
  629. for (i = 0; i < m_rtpPackets.Size(); i++) {
  630. m_rtpPackets[i]->WriteEmbeddedData(pFile, hintStartPos);
  631. }
  632. u_int64_t endPos = pFile->GetPosition();
  633. pFile->SetPosition(packetStartPos);
  634. // finally rewrite the packet and data entries
  635. // which now contain the correct offsets for the embedded data
  636. for (i = 0; i < m_rtpPackets.Size(); i++) {
  637. m_rtpPackets[i]->Write(pFile);
  638. }
  639. pFile->SetPosition(endPos);
  640. }
  641. MP4RtpPacket::MP4RtpPacket(MP4RtpHint* pHint)
  642. {
  643. m_pHint = pHint;
  644. AddProperty( /* 0 */
  645. new MP4Integer32Property("relativeXmitTime"));
  646. AddProperty( /* 1 */
  647. new MP4BitfieldProperty("reserved1", 2));
  648. AddProperty( /* 2 */
  649. new MP4BitfieldProperty("Pbit", 1));
  650. AddProperty( /* 3 */
  651. new MP4BitfieldProperty("Xbit", 1));
  652. AddProperty( /* 4 */
  653. new MP4BitfieldProperty("reserved2", 4));
  654. AddProperty( /* 5 */
  655. new MP4BitfieldProperty("Mbit", 1));
  656. AddProperty( /* 6 */
  657. new MP4BitfieldProperty("payloadType", 7));
  658. AddProperty( /* 7 */
  659. new MP4Integer16Property("sequenceNumber"));
  660. AddProperty( /* 8 */
  661. new MP4BitfieldProperty("reserved3", 13));
  662. AddProperty( /* 9 */
  663. new MP4BitfieldProperty("extraFlag", 1));
  664. AddProperty( /* 10 */
  665. new MP4BitfieldProperty("bFrameFlag", 1));
  666. AddProperty( /* 11 */
  667. new MP4BitfieldProperty("repeatFlag", 1));
  668. AddProperty( /* 12 */
  669. new MP4Integer16Property("entryCount"));
  670. }
  671. MP4RtpPacket::~MP4RtpPacket()
  672. {
  673. for (u_int32_t i = 0; i < m_rtpData.Size(); i++) {
  674. delete m_rtpData[i];
  675. }
  676. }
  677. void MP4RtpPacket::AddExtraProperties()
  678. {
  679. AddProperty( /* 13 */
  680. new MP4Integer32Property("extraInformationLength"));
  681. // This is a bit of a hack, since the tlv entries are really defined
  682. // as atoms but there is only one type defined now, rtpo, and getting
  683. // our atom code hooked up here would be a major pain with little gain
  684. AddProperty( /* 14 */
  685. new MP4Integer32Property("tlvLength"));
  686. AddProperty( /* 15 */
  687. new MP4StringProperty("tlvType"));
  688. AddProperty( /* 16 */
  689. new MP4Integer32Property("timestampOffset"));
  690. ((MP4Integer32Property*)m_pProperties[13])->SetValue(16);
  691. ((MP4Integer32Property*)m_pProperties[14])->SetValue(12);
  692. ((MP4StringProperty*)m_pProperties[15])->SetFixedLength(4);
  693. ((MP4StringProperty*)m_pProperties[15])->SetValue("rtpo");
  694. }
  695. void MP4RtpPacket::Read(MP4File* pFile)
  696. {
  697. // call base class Read for required properties
  698. MP4Container::Read(pFile);
  699. // read extra info if present
  700. // we only support the rtpo field!
  701. if (((MP4BitfieldProperty*)m_pProperties[9])->GetValue() == 1) {
  702. ReadExtra(pFile);
  703. }
  704. u_int16_t numDataEntries =
  705. ((MP4Integer16Property*)m_pProperties[12])->GetValue();
  706. // read data entries
  707. for (u_int16_t i = 0; i < numDataEntries; i++) {
  708. u_int8_t dataType;
  709. pFile->PeekBytes(&dataType, 1);
  710. MP4RtpData* pData;
  711. switch (dataType) {
  712. case 0:
  713. pData = new MP4RtpNullData(this);
  714. break;
  715. case 1:
  716. pData = new MP4RtpImmediateData(this);
  717. break;
  718. case 2:
  719. pData = new MP4RtpSampleData(this);
  720. break;
  721. case 3:
  722. pData = new MP4RtpSampleDescriptionData(this);
  723. break;
  724. default:
  725. throw new MP4Error("unknown packet data entry type",
  726. "MP4ReadHint");
  727. }
  728. m_rtpData.Add(pData);
  729. // read data entry's properties
  730. pData->Read(pFile);
  731. }
  732. }
  733. void MP4RtpPacket::ReadExtra(MP4File* pFile)
  734. {
  735. AddExtraProperties();
  736. int32_t extraLength = (int32_t)pFile->ReadUInt32();
  737. if (extraLength < 4) {
  738. throw new MP4Error("bad packet extra info length",
  739. "MP4RtpPacket::ReadExtra");
  740. }
  741. extraLength -= 4;
  742. while (extraLength > 0) {
  743. u_int32_t entryLength = pFile->ReadUInt32();
  744. u_int32_t entryTag = pFile->ReadUInt32();
  745. if (entryLength < 8) {
  746. throw new MP4Error("bad packet extra info entry length",
  747. "MP4RtpPacket::ReadExtra");
  748. }
  749. if (entryTag == STRTOINT32("rtpo") && entryLength == 12) {
  750. // read the rtp timestamp offset
  751. m_pProperties[16]->Read(pFile);
  752. } else {
  753. // ignore it, LATER carry it along
  754. pFile->SetPosition(pFile->GetPosition() + entryLength - 8);
  755. }
  756. extraLength -= entryLength;
  757. }
  758. if (extraLength < 0) {
  759. throw new MP4Error("invalid packet extra info length",
  760. "MP4RtpPacket::ReadExtra");
  761. }
  762. }
  763. void MP4RtpPacket::Set(u_int8_t payloadNumber,
  764. u_int32_t packetId, bool setMbit)
  765. {
  766. ((MP4BitfieldProperty*)m_pProperties[5])->SetValue(setMbit);
  767. ((MP4BitfieldProperty*)m_pProperties[6])->SetValue(payloadNumber);
  768. ((MP4Integer16Property*)m_pProperties[7])->SetValue(packetId);
  769. }
  770. int32_t MP4RtpPacket::GetTransmitOffset()
  771. {
  772. return ((MP4Integer32Property*)m_pProperties[0])->GetValue();
  773. }
  774. void MP4RtpPacket::SetTransmitOffset(int32_t transmitOffset)
  775. {
  776. ((MP4Integer32Property*)m_pProperties[0])->SetValue(transmitOffset);
  777. }
  778. bool MP4RtpPacket::GetPBit()
  779. {
  780. return ((MP4BitfieldProperty*)m_pProperties[2])->GetValue();
  781. }
  782. bool MP4RtpPacket::GetXBit()
  783. {
  784. return ((MP4BitfieldProperty*)m_pProperties[3])->GetValue();
  785. }
  786. bool MP4RtpPacket::GetMBit()
  787. {
  788. return ((MP4BitfieldProperty*)m_pProperties[5])->GetValue();
  789. }
  790. u_int8_t MP4RtpPacket::GetPayload()
  791. {
  792. return ((MP4BitfieldProperty*)m_pProperties[6])->GetValue();
  793. }
  794. u_int16_t MP4RtpPacket::GetSequenceNumber()
  795. {
  796. return ((MP4Integer16Property*)m_pProperties[7])->GetValue();
  797. }
  798. bool MP4RtpPacket::IsBFrame()
  799. {
  800. return ((MP4BitfieldProperty*)m_pProperties[10])->GetValue();
  801. }
  802. void MP4RtpPacket::SetBFrame(bool isBFrame)
  803. {
  804. ((MP4BitfieldProperty*)m_pProperties[10])->SetValue(isBFrame);
  805. }
  806. void MP4RtpPacket::SetTimestampOffset(u_int32_t timestampOffset)
  807. {
  808. if (timestampOffset == 0) {
  809. return;
  810. }
  811. ASSERT(((MP4BitfieldProperty*)m_pProperties[9])->GetValue() == 0);
  812. // set X bit
  813. ((MP4BitfieldProperty*)m_pProperties[9])->SetValue(1);
  814. AddExtraProperties();
  815. ((MP4Integer32Property*)m_pProperties[16])->SetValue(timestampOffset);
  816. }
  817. void MP4RtpPacket::AddData(MP4RtpData* pData)
  818. {
  819. m_rtpData.Add(pData);
  820. // increment entry count property
  821. ((MP4Integer16Property*)m_pProperties[12])->IncrementValue();
  822. }
  823. u_int32_t MP4RtpPacket::GetDataSize()
  824. {
  825. u_int32_t totalDataSize = 0;
  826. for (u_int32_t i = 0; i < m_rtpData.Size(); i++) {
  827. totalDataSize += m_rtpData[i]->GetDataSize();
  828. }
  829. return totalDataSize;
  830. }
  831. void MP4RtpPacket::GetData(u_int8_t* pDest)
  832. {
  833. for (u_int32_t i = 0; i < m_rtpData.Size(); i++) {
  834. m_rtpData[i]->GetData(pDest);
  835. pDest += m_rtpData[i]->GetDataSize();
  836. }
  837. }
  838. void MP4RtpPacket::Write(MP4File* pFile)
  839. {
  840. MP4Container::Write(pFile);
  841. for (u_int32_t i = 0; i < m_rtpData.Size(); i++) {
  842. m_rtpData[i]->Write(pFile);
  843. }
  844. }
  845. void MP4RtpPacket::WriteEmbeddedData(MP4File* pFile, u_int64_t startPos)
  846. {
  847. for (u_int32_t i = 0; i < m_rtpData.Size(); i++) {
  848. m_rtpData[i]->WriteEmbeddedData(pFile, startPos);
  849. }
  850. }
  851. MP4RtpData::MP4RtpData(MP4RtpPacket* pPacket)
  852. {
  853. m_pPacket = pPacket;
  854. AddProperty( /* 0 */
  855. new MP4Integer8Property("type"));
  856. }
  857. MP4Track* MP4RtpData::FindTrackFromRefIndex(u_int8_t refIndex)
  858. {
  859. MP4Track* pTrack;
  860. if (refIndex == (u_int8_t)-1) {
  861. // ourselves
  862. pTrack = GetPacket()->GetHint()->GetTrack();
  863. } else if (refIndex == 0) {
  864. // our reference track
  865. pTrack = GetPacket()->GetHint()->GetTrack()->GetRefTrack();
  866. } else {
  867. // some other track
  868. MP4RtpHintTrack* pHintTrack =
  869. GetPacket()->GetHint()->GetTrack();
  870. MP4Atom* pTrakAtom = pHintTrack->GetTrakAtom();
  871. ASSERT(pTrakAtom);
  872. MP4Integer32Property* pTrackIdProperty = NULL;
  873. (void)pTrakAtom->FindProperty(
  874. "trak.tref.hint.entries",
  875. (MP4Property**)&pTrackIdProperty);
  876. ASSERT(pTrackIdProperty);
  877. u_int32_t refTrackId =
  878. pTrackIdProperty->GetValue(refIndex - 1);
  879. pTrack = pHintTrack->GetFile()->GetTrack(refTrackId);
  880. }
  881. return pTrack;
  882. }
  883. MP4RtpNullData::MP4RtpNullData(MP4RtpPacket* pPacket)
  884. : MP4RtpData(pPacket)
  885. {
  886. ((MP4Integer8Property*)m_pProperties[0])->SetValue(0);
  887. AddProperty( /* 1 */
  888. new MP4BytesProperty("pad", 15));
  889. ((MP4BytesProperty*)m_pProperties[1])->SetFixedSize(15);
  890. }
  891. MP4RtpImmediateData::MP4RtpImmediateData(MP4RtpPacket* pPacket)
  892. : MP4RtpData(pPacket)
  893. {
  894. ((MP4Integer8Property*)m_pProperties[0])->SetValue(1);
  895. AddProperty( /* 1 */
  896. new MP4Integer8Property("count"));
  897. AddProperty( /* 2 */
  898. new MP4BytesProperty("data", 14));
  899. ((MP4BytesProperty*)m_pProperties[2])->SetFixedSize(14);
  900. }
  901. void MP4RtpImmediateData::Set(const u_int8_t* pBytes, u_int8_t numBytes)
  902. {
  903. ((MP4Integer8Property*)m_pProperties[1])->SetValue(numBytes);
  904. ((MP4BytesProperty*)m_pProperties[2])->SetValue(pBytes, numBytes);
  905. }
  906. u_int16_t MP4RtpImmediateData::GetDataSize()
  907. {
  908. return ((MP4Integer8Property*)m_pProperties[1])->GetValue();
  909. }
  910. void MP4RtpImmediateData::GetData(u_int8_t* pDest)
  911. {
  912. u_int8_t* pValue;
  913. u_int32_t valueSize;
  914. ((MP4BytesProperty*)m_pProperties[2])->GetValue(&pValue, &valueSize);
  915. memcpy(pDest, pValue, GetDataSize());
  916. MP4Free(pValue);
  917. }
  918. MP4RtpSampleData::MP4RtpSampleData(MP4RtpPacket* pPacket)
  919. : MP4RtpData(pPacket)
  920. {
  921. ((MP4Integer8Property*)m_pProperties[0])->SetValue(2);
  922. AddProperty( /* 1 */
  923. new MP4Integer8Property("trackRefIndex"));
  924. AddProperty( /* 2 */
  925. new MP4Integer16Property("length"));
  926. AddProperty( /* 3 */
  927. new MP4Integer32Property("sampleNumber"));
  928. AddProperty( /* 4 */
  929. new MP4Integer32Property("sampleOffset"));
  930. AddProperty( /* 5 */
  931. new MP4Integer16Property("bytesPerBlock"));
  932. AddProperty( /* 6 */
  933. new MP4Integer16Property("samplesPerBlock"));
  934. ((MP4Integer16Property*)m_pProperties[5])->SetValue(1);
  935. ((MP4Integer16Property*)m_pProperties[6])->SetValue(1);
  936. m_pRefData = NULL;
  937. m_pRefTrack = NULL;
  938. m_refSampleId = MP4_INVALID_SAMPLE_ID;
  939. m_refSampleOffset = 0;
  940. }
  941. void MP4RtpSampleData::SetEmbeddedImmediate(MP4SampleId sampleId,
  942. u_int8_t* pData, u_int16_t dataLength)
  943. {
  944. ((MP4Integer8Property*)m_pProperties[1])->SetValue((u_int8_t)-1);
  945. ((MP4Integer16Property*)m_pProperties[2])->SetValue(dataLength);
  946. ((MP4Integer32Property*)m_pProperties[3])->SetValue(sampleId);
  947. ((MP4Integer32Property*)m_pProperties[4])->SetValue(0);
  948. CHECK_AND_FREE(m_pRefData);
  949. m_pRefData = pData;
  950. }
  951. void MP4RtpSampleData::SetReferenceSample(
  952. MP4SampleId refSampleId, u_int32_t refSampleOffset,
  953. u_int16_t sampleLength)
  954. {
  955. ((MP4Integer8Property*)m_pProperties[1])->SetValue(0);
  956. ((MP4Integer16Property*)m_pProperties[2])->SetValue(sampleLength);
  957. ((MP4Integer32Property*)m_pProperties[3])->SetValue(refSampleId);
  958. ((MP4Integer32Property*)m_pProperties[4])->SetValue(refSampleOffset);
  959. }
  960. void MP4RtpSampleData::SetEmbeddedSample(
  961. MP4SampleId sampleId, MP4Track* pRefTrack,
  962. MP4SampleId refSampleId, u_int32_t refSampleOffset,
  963. u_int16_t sampleLength)
  964. {
  965. ((MP4Integer8Property*)m_pProperties[1])->SetValue((u_int8_t)-1);
  966. ((MP4Integer16Property*)m_pProperties[2])->SetValue(sampleLength);
  967. ((MP4Integer32Property*)m_pProperties[3])->SetValue(sampleId);
  968. ((MP4Integer32Property*)m_pProperties[4])->SetValue(0);
  969. m_pRefTrack = pRefTrack;
  970. m_refSampleId = refSampleId;
  971. m_refSampleOffset = refSampleOffset;
  972. }
  973. u_int16_t MP4RtpSampleData::GetDataSize()
  974. {
  975. return ((MP4Integer16Property*)m_pProperties[2])->GetValue();
  976. }
  977. void MP4RtpSampleData::GetData(u_int8_t* pDest)
  978. {
  979. u_int8_t trackRefIndex =
  980. ((MP4Integer8Property*)m_pProperties[1])->GetValue();
  981. MP4Track* pSampleTrack =
  982. FindTrackFromRefIndex(trackRefIndex);
  983. pSampleTrack->ReadSampleFragment(
  984. ((MP4Integer32Property*)m_pProperties[3])->GetValue(), // sampleId
  985. ((MP4Integer32Property*)m_pProperties[4])->GetValue(), // sampleOffset
  986. ((MP4Integer16Property*)m_pProperties[2])->GetValue(), // sampleLength
  987. pDest);
  988. }
  989. void MP4RtpSampleData::WriteEmbeddedData(MP4File* pFile, u_int64_t startPos)
  990. {
  991. // if not using embedded data, nothing to do
  992. if (((MP4Integer8Property*)m_pProperties[1])->GetValue() != (u_int8_t)-1) {
  993. return;
  994. }
  995. // figure out the offset within this hint sample for this embedded data
  996. u_int64_t offset = pFile->GetPosition() - startPos;
  997. ASSERT(offset <= 0xFFFFFFFF);
  998. ((MP4Integer32Property*)m_pProperties[4])->SetValue((u_int32_t)offset);
  999. u_int16_t length = ((MP4Integer16Property*)m_pProperties[2])->GetValue();
  1000. if (m_pRefData) {
  1001. pFile->WriteBytes(m_pRefData, length);
  1002. return;
  1003. }
  1004. if (m_refSampleId != MP4_INVALID_SAMPLE_ID) {
  1005. u_int8_t* pSample = NULL;
  1006. u_int32_t sampleSize = 0;
  1007. ASSERT(m_pRefTrack);
  1008. m_pRefTrack->ReadSample(m_refSampleId, &pSample, &sampleSize);
  1009. ASSERT(m_refSampleOffset + length <= sampleSize);
  1010. pFile->WriteBytes(&pSample[m_refSampleOffset], length);
  1011. MP4Free(pSample);
  1012. return;
  1013. }
  1014. }
  1015. MP4RtpSampleDescriptionData::MP4RtpSampleDescriptionData(MP4RtpPacket* pPacket)
  1016. : MP4RtpData(pPacket)
  1017. {
  1018. ((MP4Integer8Property*)m_pProperties[0])->SetValue(3);
  1019. AddProperty( /* 1 */
  1020. new MP4Integer8Property("trackRefIndex"));
  1021. AddProperty( /* 2 */
  1022. new MP4Integer16Property("length"));
  1023. AddProperty( /* 3 */
  1024. new MP4Integer32Property("sampleDescriptionIndex"));
  1025. AddProperty( /* 4 */
  1026. new MP4Integer32Property("sampleDescriptionOffset"));
  1027. AddProperty( /* 5 */
  1028. new MP4Integer32Property("reserved"));
  1029. }
  1030. void MP4RtpSampleDescriptionData::Set(u_int32_t sampleDescrIndex,
  1031. u_int32_t offset, u_int16_t length)
  1032. {
  1033. ((MP4Integer16Property*)m_pProperties[2])->SetValue(length);
  1034. ((MP4Integer32Property*)m_pProperties[3])->SetValue(sampleDescrIndex);
  1035. ((MP4Integer32Property*)m_pProperties[4])->SetValue(offset);
  1036. }
  1037. u_int16_t MP4RtpSampleDescriptionData::GetDataSize()
  1038. {
  1039. return ((MP4Integer16Property*)m_pProperties[2])->GetValue();
  1040. }
  1041. void MP4RtpSampleDescriptionData::GetData(u_int8_t* pDest)
  1042. {
  1043. // we start with the index into our track references
  1044. u_int8_t trackRefIndex =
  1045. ((MP4Integer8Property*)m_pProperties[1])->GetValue();
  1046. // from which we can find the track structure
  1047. MP4Track* pSampleTrack =
  1048. FindTrackFromRefIndex(trackRefIndex);
  1049. // next find the desired atom in the track's sample description table
  1050. u_int32_t sampleDescrIndex =
  1051. ((MP4Integer32Property*)m_pProperties[3])->GetValue();
  1052. MP4Atom* pTrakAtom =
  1053. pSampleTrack->GetTrakAtom();
  1054. char sdName[64];
  1055. snprintf(sdName, 64, "trak.mdia.minf.stbl.stsd.*[%u]", sampleDescrIndex);
  1056. MP4Atom* pSdAtom =
  1057. pTrakAtom->FindAtomMP4(sdName);
  1058. // bad reference
  1059. if (pSdAtom == NULL) {
  1060. throw new MP4Error("invalid sample description index",
  1061. "MP4RtpSampleDescriptionData::GetData");
  1062. }
  1063. // check validity of the upcoming copy
  1064. u_int16_t length =
  1065. ((MP4Integer16Property*)m_pProperties[2])->GetValue();
  1066. u_int32_t offset =
  1067. ((MP4Integer32Property*)m_pProperties[4])->GetValue();
  1068. if (offset + length > pSdAtom->GetSize()) {
  1069. throw new MP4Error("offset and/or length are too large",
  1070. "MP4RtpSampleDescriptionData::GetData");
  1071. }
  1072. // now we use the raw file to get the desired bytes
  1073. MP4File* pFile = GetPacket()->GetHint()->GetTrack()->GetFile();
  1074. u_int64_t orgPos = pFile->GetPosition();
  1075. // It's not entirely clear from the spec whether the offset is from
  1076. // the start of the sample descirption atom, or the start of the atom's
  1077. // data. I believe it is the former, but the commented out code will
  1078. // realize the latter interpretation if I turn out to be wrong.
  1079. u_int64_t dataPos = pSdAtom->GetStart();
  1080. //u_int64_t dataPos = pSdAtom->GetEnd() - pSdAtom->GetSize();
  1081. pFile->SetPosition(dataPos + offset);
  1082. pFile->ReadBytes(pDest, length);
  1083. pFile->SetPosition(orgPos);
  1084. }