atom_rtp.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. MP4RtpAtom::MP4RtpAtom()
  23. : MP4Atom("rtp ")
  24. {
  25. // The atom type "rtp " is used in two complete unrelated ways
  26. // i.e. it's real two atoms with the same name
  27. // To handle that we need to postpone property creation until
  28. // we know who our parent atom is (stsd or hnti) which gives us
  29. // the context info we need to know who we are
  30. }
  31. void MP4RtpAtom::AddPropertiesStsdType()
  32. {
  33. AddReserved("reserved1", 6); /* 0 */
  34. AddProperty( /* 1 */
  35. new MP4Integer16Property("dataReferenceIndex"));
  36. AddProperty( /* 2 */
  37. new MP4Integer16Property("hintTrackVersion"));
  38. AddProperty( /* 3 */
  39. new MP4Integer16Property("highestCompatibleVersion"));
  40. AddProperty( /* 4 */
  41. new MP4Integer32Property("maxPacketSize"));
  42. ExpectChildAtom("tims", Required, OnlyOne);
  43. ExpectChildAtom("tsro", Optional, OnlyOne);
  44. ExpectChildAtom("snro", Optional, OnlyOne);
  45. }
  46. void MP4RtpAtom::AddPropertiesHntiType()
  47. {
  48. MP4StringProperty* pProp =
  49. new MP4StringProperty("descriptionFormat");
  50. pProp->SetFixedLength(4);
  51. AddProperty(pProp); /* 0 */
  52. AddProperty( /* 1 */
  53. new MP4StringProperty("sdpText"));
  54. }
  55. void MP4RtpAtom::Generate()
  56. {
  57. if (!strcmp(m_pParentAtom->GetType(), "stsd")) {
  58. AddPropertiesStsdType();
  59. GenerateStsdType();
  60. } else if (!strcmp(m_pParentAtom->GetType(), "hnti")) {
  61. AddPropertiesHntiType();
  62. GenerateHntiType();
  63. } else {
  64. VERBOSE_WARNING(m_pFile->GetVerbosity(),
  65. printf("Warning: rtp atom in unexpected context, can not generate"));
  66. }
  67. }
  68. void MP4RtpAtom::GenerateStsdType()
  69. {
  70. // generate children
  71. MP4Atom::Generate();
  72. ((MP4Integer16Property*)m_pProperties[1])->SetValue(1);
  73. ((MP4Integer16Property*)m_pProperties[2])->SetValue(1);
  74. ((MP4Integer16Property*)m_pProperties[3])->SetValue(1);
  75. }
  76. void MP4RtpAtom::GenerateHntiType()
  77. {
  78. MP4Atom::Generate();
  79. ((MP4StringProperty*)m_pProperties[0])->SetValue("sdp ");
  80. }
  81. void MP4RtpAtom::Read()
  82. {
  83. if (!strcmp(m_pParentAtom->GetType(), "stsd")) {
  84. AddPropertiesStsdType();
  85. ReadStsdType();
  86. } else if (!strcmp(m_pParentAtom->GetType(), "hnti")) {
  87. AddPropertiesHntiType();
  88. ReadHntiType();
  89. } else {
  90. VERBOSE_READ(m_pFile->GetVerbosity(),
  91. printf("rtp atom in unexpected context, can not read"));
  92. }
  93. Skip(); // to end of atom
  94. }
  95. void MP4RtpAtom::ReadStsdType()
  96. {
  97. MP4Atom::Read();
  98. }
  99. void MP4RtpAtom::ReadHntiType()
  100. {
  101. ReadProperties(0, 1);
  102. // read sdp string, length is implicit in size of atom
  103. u_int64_t size = GetEnd() - m_pFile->GetPosition();
  104. char* data = (char*)MP4Malloc(size + 1);
  105. ASSERT(data != NULL);
  106. m_pFile->ReadBytes((u_int8_t*)data, size);
  107. data[size] = '\0';
  108. ((MP4StringProperty*)m_pProperties[1])->SetValue(data);
  109. MP4Free(data);
  110. }
  111. void MP4RtpAtom::Write()
  112. {
  113. if (!strcmp(m_pParentAtom->GetType(), "hnti")) {
  114. WriteHntiType();
  115. } else {
  116. MP4Atom::Write();
  117. }
  118. }
  119. void MP4RtpAtom::WriteHntiType()
  120. {
  121. // since length of string is implicit in size of atom
  122. // we need to handle this specially, and not write the terminating \0
  123. MP4StringProperty* pSdp = (MP4StringProperty*)m_pProperties[1];
  124. pSdp->SetFixedLength((u_int32_t)strlen(pSdp->GetValue()));
  125. MP4Atom::Write();
  126. pSdp->SetFixedLength(0);
  127. }