mp4container.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. MP4Container::~MP4Container()
  23. {
  24. for (u_int32_t i = 0; i < m_pProperties.Size(); i++) {
  25. delete m_pProperties[i];
  26. }
  27. }
  28. void MP4Container::AddProperty(MP4Property* pProperty)
  29. {
  30. ASSERT(pProperty);
  31. m_pProperties.Add(pProperty);
  32. }
  33. bool MP4Container::FindProperty(const char *name,
  34. MP4Property** ppProperty, u_int32_t* pIndex)
  35. {
  36. if (pIndex) {
  37. *pIndex = 0; // set the default answer for index
  38. }
  39. u_int32_t numProperties = m_pProperties.Size();
  40. for (u_int32_t i = 0; i < numProperties; i++) {
  41. if (m_pProperties[i]->FindProperty(name, ppProperty, pIndex)) {
  42. return true;
  43. }
  44. }
  45. return false;
  46. }
  47. void MP4Container::FindIntegerProperty(const char* name,
  48. MP4Property** ppProperty, u_int32_t* pIndex)
  49. {
  50. if (!FindProperty(name, ppProperty, pIndex)) {
  51. throw new MP4Error("no such property",
  52. "MP4Container::FindIntegerProperty");
  53. }
  54. switch ((*ppProperty)->GetType()) {
  55. case Integer8Property:
  56. case Integer16Property:
  57. case Integer24Property:
  58. case Integer32Property:
  59. case Integer64Property:
  60. break;
  61. default:
  62. throw new MP4Error("type mismatch",
  63. "MP4Container::FindIntegerProperty");
  64. }
  65. }
  66. u_int64_t MP4Container::GetIntegerProperty(const char* name)
  67. {
  68. MP4Property* pProperty;
  69. u_int32_t index;
  70. FindIntegerProperty(name, &pProperty, &index);
  71. return ((MP4IntegerProperty*)pProperty)->GetValue(index);
  72. }
  73. void MP4Container::SetIntegerProperty(const char* name, u_int64_t value)
  74. {
  75. MP4Property* pProperty = NULL;
  76. u_int32_t index = 0;
  77. FindIntegerProperty(name, &pProperty, &index);
  78. ((MP4IntegerProperty*)pProperty)->SetValue(value, index);
  79. }
  80. void MP4Container::FindFloatProperty(const char* name,
  81. MP4Property** ppProperty, u_int32_t* pIndex)
  82. {
  83. if (!FindProperty(name, ppProperty, pIndex)) {
  84. throw new MP4Error("no such property",
  85. "MP4Container::FindFloatProperty");
  86. }
  87. if ((*ppProperty)->GetType() != Float32Property) {
  88. throw new MP4Error("type mismatch",
  89. "MP4Container::FindFloatProperty");
  90. }
  91. }
  92. float MP4Container::GetFloatProperty(const char* name)
  93. {
  94. MP4Property* pProperty;
  95. u_int32_t index;
  96. FindFloatProperty(name, &pProperty, &index);
  97. return ((MP4Float32Property*)pProperty)->GetValue(index);
  98. }
  99. void MP4Container::SetFloatProperty(const char* name, float value)
  100. {
  101. MP4Property* pProperty;
  102. u_int32_t index;
  103. FindFloatProperty(name, &pProperty, &index);
  104. ((MP4Float32Property*)pProperty)->SetValue(value, index);
  105. }
  106. void MP4Container::FindStringProperty(const char* name,
  107. MP4Property** ppProperty, u_int32_t* pIndex)
  108. {
  109. if (!FindProperty(name, ppProperty, pIndex)) {
  110. throw new MP4Error("no such property",
  111. "MP4Container::FindStringProperty");
  112. }
  113. if ((*ppProperty)->GetType() != StringProperty) {
  114. throw new MP4Error("type mismatch",
  115. "MP4Container::FindStringProperty");
  116. }
  117. }
  118. const char* MP4Container::GetStringProperty(const char* name)
  119. {
  120. MP4Property* pProperty;
  121. u_int32_t index;
  122. FindStringProperty(name, &pProperty, &index);
  123. return ((MP4StringProperty*)pProperty)->GetValue(index);
  124. }
  125. void MP4Container::SetStringProperty(const char* name, const char* value)
  126. {
  127. MP4Property* pProperty;
  128. u_int32_t index;
  129. FindStringProperty(name, &pProperty, &index);
  130. ((MP4StringProperty*)pProperty)->SetValue(value, index);
  131. }
  132. void MP4Container::FindBytesProperty(const char* name,
  133. MP4Property** ppProperty, u_int32_t* pIndex)
  134. {
  135. if (!FindProperty(name, ppProperty, pIndex)) {
  136. throw new MP4Error("no such property",
  137. "MP4Container::FindBytesProperty");
  138. }
  139. if ((*ppProperty)->GetType() != BytesProperty) {
  140. throw new MP4Error("type mismatch",
  141. "MP4Container::FindBytesProperty");
  142. }
  143. }
  144. void MP4Container::GetBytesProperty(const char* name,
  145. u_int8_t** ppValue, u_int32_t* pValueSize)
  146. {
  147. MP4Property* pProperty;
  148. u_int32_t index;
  149. FindBytesProperty(name, &pProperty, &index);
  150. ((MP4BytesProperty*)pProperty)->GetValue(ppValue, pValueSize, index);
  151. }
  152. void MP4Container::SetBytesProperty(const char* name,
  153. const u_int8_t* pValue, u_int32_t valueSize)
  154. {
  155. MP4Property* pProperty;
  156. u_int32_t index;
  157. FindBytesProperty(name, &pProperty, &index);
  158. ((MP4BytesProperty*)pProperty)->SetValue(pValue, valueSize, index);
  159. }
  160. void MP4Container::Read(MP4File* pFile)
  161. {
  162. u_int32_t numProperties = m_pProperties.Size();
  163. for (u_int32_t i = 0; i < numProperties; i++) {
  164. m_pProperties[i]->Read(pFile);
  165. }
  166. }
  167. void MP4Container::Write(MP4File* pFile)
  168. {
  169. u_int32_t numProperties = m_pProperties.Size();
  170. if (numProperties == 0) {
  171. WARNING(numProperties == 0);
  172. return;
  173. }
  174. for (u_int32_t i = 0; i < numProperties; i++) {
  175. m_pProperties[i]->Write(pFile);
  176. }
  177. }