MediaContent.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango.org>
  5. *
  6. * This file is part of Open Source Doubango Framework.
  7. *
  8. * DOUBANGO is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * DOUBANGO is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with DOUBANGO.
  20. *
  21. */
  22. #include "MediaContent.h"
  23. MediaContent::MediaContent(tmedia_content_t* pContent)
  24. : m_pData(tsk_null)
  25. {
  26. m_pContent = (tmedia_content_t*)tsk_object_ref(pContent);
  27. }
  28. MediaContent::~MediaContent()
  29. {
  30. TSK_OBJECT_SAFE_FREE(m_pContent);
  31. TSK_OBJECT_SAFE_FREE(m_pData);
  32. }
  33. const char* MediaContent::getType()
  34. {
  35. if(m_pContent) {
  36. return m_pContent->type;
  37. }
  38. return tsk_null;
  39. }
  40. unsigned MediaContent::getDataLength()
  41. {
  42. if(!m_pContent) {
  43. TSK_DEBUG_ERROR("Invalid internal object");
  44. return 0;
  45. }
  46. if(!m_pData) {
  47. m_pData = tmedia_content_get_data(m_pContent);
  48. }
  49. return (m_pData ? m_pData->size : 0);
  50. }
  51. unsigned MediaContent::getData(void* pOutput, unsigned nMaxsize)
  52. {
  53. unsigned nRetsize = 0;
  54. if(!m_pContent) {
  55. TSK_DEBUG_ERROR("Invalid internal object");
  56. return 0;
  57. }
  58. if(!m_pData) {
  59. m_pData = tmedia_content_get_data(m_pContent);
  60. }
  61. if(pOutput && nMaxsize && m_pData) {
  62. nRetsize = (m_pData->size > nMaxsize) ? nMaxsize : m_pData->size;
  63. memcpy(pOutput, m_pData->data, nRetsize);
  64. }
  65. return nRetsize;
  66. }
  67. MediaContent* MediaContent::parse(const void* pData, unsigned nSize, const char* pType)
  68. {
  69. MediaContent* pMediaContent = tsk_null;
  70. tmedia_content_t* pContent = tmedia_content_parse(pData, (tsk_size_t)nSize, pType);
  71. if(pContent) {
  72. if(TMEDIA_CONTENT_IS_CPIM(pContent)) {
  73. pMediaContent = new MediaContentCPIM(pContent);
  74. }
  75. else if(TMEDIA_CONTENT_IS_DUMMY(pContent)) {
  76. // Todo
  77. }
  78. TSK_OBJECT_SAFE_FREE(pContent);
  79. }
  80. return pMediaContent;
  81. }
  82. MediaContentCPIM* MediaContent::parse(const void* pData, unsigned nSize)
  83. {
  84. MediaContent* pMediaContent;
  85. if((pMediaContent = MediaContent::parse(pData, nSize, TMEDIA_CONTENT_CPIM_TYPE))) {
  86. return dyn_cast<MediaContentCPIM*>(pMediaContent);
  87. }
  88. return tsk_null;
  89. }
  90. /* ============ message/CPIM ================= */
  91. MediaContentCPIM::MediaContentCPIM(tmedia_content_t* pContent)
  92. : MediaContent(pContent)
  93. {
  94. }
  95. MediaContentCPIM::~MediaContentCPIM()
  96. {
  97. }
  98. unsigned MediaContentCPIM::getPayloadLength()
  99. {
  100. if(!m_pContent || !TMEDIA_CONTENT_IS_CPIM(m_pContent)) {
  101. TSK_DEBUG_ERROR("Invalid internal object");
  102. return 0;
  103. }
  104. return (TMEDIA_CONTENT_CPIM(m_pContent)->e ? TMEDIA_CONTENT_CPIM(m_pContent)->e->size : 0);
  105. }
  106. unsigned MediaContentCPIM::getPayload(void* pOutput, unsigned nMaxsize)
  107. {
  108. unsigned nRetsize = 0;
  109. if(!m_pContent || !TMEDIA_CONTENT_IS_CPIM(m_pContent)) {
  110. TSK_DEBUG_ERROR("Invalid internal object");
  111. return 0;
  112. }
  113. if(pOutput && nMaxsize && TMEDIA_CONTENT_CPIM(m_pContent)->e) {
  114. nRetsize = (TMEDIA_CONTENT_CPIM(m_pContent)->e->size > nMaxsize) ? nMaxsize : TMEDIA_CONTENT_CPIM(m_pContent)->e->size;
  115. memcpy(pOutput, TMEDIA_CONTENT_CPIM(m_pContent)->e->data, nRetsize);
  116. }
  117. return nRetsize;
  118. }
  119. const void* MediaContentCPIM::getPayloadPtr()
  120. {
  121. if(!m_pContent || !TMEDIA_CONTENT_IS_CPIM(m_pContent)) {
  122. TSK_DEBUG_ERROR("Invalid internal object");
  123. return tsk_null;
  124. }
  125. return TMEDIA_CONTENT_CPIM(m_pContent)->e ? TMEDIA_CONTENT_CPIM(m_pContent)->e->data : tsk_null;
  126. }
  127. const char* MediaContentCPIM::getHeaderValue(const char* name)
  128. {
  129. const tmedia_content_cpim_t* cpim;
  130. const tsk_list_item_t* item;
  131. if(!m_pContent || !TMEDIA_CONTENT_IS_CPIM(m_pContent)) {
  132. TSK_DEBUG_ERROR("Invalid internal object");
  133. return tsk_null;
  134. }
  135. cpim = TMEDIA_CONTENT_CPIM(m_pContent);
  136. tsk_list_foreach(item, cpim->h_headers) {
  137. if(tsk_striequals(name, TMEDIA_CONTENT_HEADER(item->data)->name)) {
  138. return TMEDIA_CONTENT_HEADER(item->data)->value;
  139. }
  140. }
  141. tsk_list_foreach(item, cpim->m_headers) {
  142. if(tsk_striequals(name, TMEDIA_CONTENT_HEADER(item->data)->name)) {
  143. return TMEDIA_CONTENT_HEADER(item->data)->value;
  144. }
  145. }
  146. return tsk_null;
  147. }