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