tmedia_params.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * Copyright (C) 2010-2011 Mamadou Diop.
  3. *
  4. * Contact: Mamadou Diop <diopmamadou(at)doubango[dot]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. /**@file tmedia_params.c
  23. * @brief Media parameters used to configure any session or plugin.
  24. *
  25. * @author Mamadou Diop <diopmamadou(at)doubango[dot]org>
  26. *
  27. */
  28. #include "tinymedia/tmedia_params.h"
  29. #include "tinymedia/tmedia_session.h"
  30. #include "tsk_params.h"
  31. #include "tsk_debug.h"
  32. #include "tsk_memory.h"
  33. tmedia_param_t* tmedia_param_create(tmedia_param_access_type_t access_type,
  34. tmedia_type_t media_type,
  35. tmedia_param_plugin_type_t plugin_type,
  36. tmedia_param_value_type_t value_type,
  37. const char* key,
  38. void* value)
  39. {
  40. tmedia_param_t* param;
  41. if(!key || (!value && (value_type != tmedia_pvt_pobject && value_type != tmedia_pvt_pchar))) {
  42. TSK_DEBUG_ERROR("Invalid parameter");
  43. return tsk_null;
  44. }
  45. if((param = tsk_object_new(tmedia_param_def_t))) {
  46. param->access_type = access_type;
  47. param->media_type = media_type;
  48. param->plugin_type = plugin_type;
  49. param->value_type = value_type;
  50. param->key = tsk_strdup(key);
  51. if(access_type == tmedia_pat_get) {
  52. param->value = (value);
  53. }
  54. else if(access_type == tmedia_pat_set) {
  55. switch(value_type) {
  56. case tmedia_pvt_int32:
  57. if((param->value = tsk_calloc(1, sizeof(int32_t)))) {
  58. memcpy(param->value, value, sizeof(int32_t));
  59. }
  60. break;
  61. case tmedia_pvt_pobject:
  62. param->value = tsk_object_ref(value);
  63. break;
  64. case tmedia_pvt_pchar:
  65. param->value = tsk_strdup(value);
  66. break;
  67. case tmedia_pvt_int64:
  68. if((param->value = tsk_calloc(1, sizeof(int64_t)))) {
  69. memcpy(param->value, value, sizeof(int64_t));
  70. }
  71. break;
  72. }
  73. }
  74. }
  75. else {
  76. TSK_DEBUG_ERROR("Failed to create media parameter");
  77. }
  78. return param;
  79. }
  80. tmedia_params_L_t* tmedia_params_create_2(va_list *app)
  81. {
  82. tmedia_session_param_type_t curr;
  83. tmedia_params_L_t* params;
  84. if(!app) {
  85. TSK_DEBUG_ERROR("Invalid parameter");
  86. return tsk_null;
  87. }
  88. params = tmedia_params_create();
  89. while((curr = va_arg(*app, tmedia_session_param_type_t)) != tmedia_sptype_null) {
  90. switch(curr) {
  91. case tmedia_sptype_set:
  92. case tmedia_sptype_get: {
  93. /* (tmedia_type_t)MEDIA_TYPE_ENUM, (tmedia_param_plugin_type_t)PLUGIN_TYPE_ENUM, (tmedia_param_value_type_t)VALUE_TYPE_ENUM \
  94. (const char*)KEY_STR, (void*)&VALUE */
  95. /* IMPORTANT: do not pass va_arg() directly into the function */
  96. tmedia_type_t media_type = va_arg(*app, tmedia_type_t);
  97. tmedia_param_plugin_type_t plugin_type = va_arg(*app, tmedia_param_plugin_type_t);
  98. tmedia_param_value_type_t value_type = va_arg(*app, tmedia_param_value_type_t);
  99. const char* key = va_arg(*app, const char*);
  100. void* value = va_arg(*app, void*);
  101. tmedia_params_add_param(&params, (curr == tmedia_sptype_set) ? tmedia_pat_set : tmedia_pat_get,
  102. media_type, plugin_type, value_type, key, value);
  103. break;
  104. }
  105. default: {
  106. /* va_list will be unsafe => exit */
  107. TSK_DEBUG_ERROR("%d NOT a valid pname", curr);
  108. break;
  109. }
  110. }/* switch */
  111. }/* while */
  112. return params;
  113. }
  114. int tmedia_params_add_param(tmedia_params_L_t **self,
  115. tmedia_param_access_type_t access_type,
  116. tmedia_type_t media_type,
  117. tmedia_param_plugin_type_t plugin_type,
  118. tmedia_param_value_type_t value_type,
  119. const char* key,
  120. void* value)
  121. {
  122. tmedia_param_t *param;
  123. if(!self) {
  124. TSK_DEBUG_ERROR("Invalid parameter");
  125. return -1;
  126. }
  127. if(!*self) {
  128. *self = tmedia_params_create();
  129. }
  130. if((param = tmedia_param_create(access_type, media_type, plugin_type, value_type, key, value))) {
  131. tsk_list_push_back_data(*self, (void**)&param);
  132. }
  133. return 0;
  134. }
  135. //=================================================================================================
  136. // param object definition
  137. //
  138. static tsk_object_t* tmedia_param_ctor(tsk_object_t* self, va_list * app)
  139. {
  140. tmedia_param_t *param = self;
  141. if(param) {
  142. }
  143. return self;
  144. }
  145. static tsk_object_t* tmedia_param_dtor(tsk_object_t* self)
  146. {
  147. tmedia_param_t *param = self;
  148. if(param) {
  149. TSK_FREE(param->key);
  150. if(param->access_type == tmedia_pat_set) {
  151. switch(param->value_type) {
  152. case tmedia_pvt_pobject:
  153. TSK_OBJECT_SAFE_FREE(param->value);
  154. break;
  155. case tmedia_pvt_pchar:
  156. case tmedia_pvt_int64:
  157. case tmedia_pvt_int32:
  158. TSK_FREE(param->value);
  159. break;
  160. }
  161. }
  162. }
  163. return self;
  164. }
  165. static const tsk_object_def_t tmedia_param_def_s = {
  166. sizeof(tmedia_param_t),
  167. tmedia_param_ctor,
  168. tmedia_param_dtor,
  169. tsk_null,
  170. };
  171. const tsk_object_def_t *tmedia_param_def_t = &tmedia_param_def_s;