invite.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (C) 2009 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 "invite.h"
  23. #include "tinymsrp.h"
  24. extern ctx_t* ctx;
  25. extern const session_t* session_handle_cmd(cmd_type_t , const opts_L_t*);
  26. static int invite_msrp_cb(const tmsrp_event_t* _event);
  27. int invite_handle_event(const tsip_event_t *_event)
  28. {
  29. const tsip_invite_event_t* inv_event = TSIP_INVITE_EVENT(_event);
  30. const session_t* session;
  31. tsip_ssession_id_t sid;
  32. int ret = 0;
  33. /* Find associated session */
  34. sid = tsip_ssession_get_id(_event->ss);
  35. if(!(session = session_get_by_sid(ctx->sessions, sid))) {
  36. if(inv_event->type == tsip_i_newcall) {
  37. /* It's a new incoming call */
  38. if(!tsip_ssession_have_ownership(_event->ss)) {
  39. session_t* _session;
  40. if((_session = session_server_create(st_invite, _event->ss)) && (session = _session)) {
  41. tsk_list_push_back_data(ctx->sessions, (void**)&_session);
  42. }
  43. else {
  44. TSK_DEBUG_ERROR("Failed to create \"sever-side-session\".");
  45. ret = -3;
  46. goto bail;
  47. }
  48. }
  49. }
  50. else {
  51. /* it's or own session and we fail to match it ==> should never happen */
  52. TSK_DEBUG_ERROR("Failed to match session event.");
  53. ret = -2;
  54. goto bail;
  55. }
  56. }
  57. if(!session || !session->handle) {
  58. /* guard ==> should never happen */
  59. TSK_DEBUG_ERROR("Null session.");
  60. goto bail;
  61. }
  62. switch(inv_event->type) {
  63. // ============================
  64. // Sip Events
  65. //
  66. case tsip_i_newcall: {
  67. /* New call */
  68. tmedia_type_t media_type = tsip_ssession_get_mediatype(session);
  69. tsip_api_common_accept(session->handle,
  70. TSIP_ACTION_SET_NULL());
  71. /*tsip_api_common_reject(session->handle,
  72. TSIP_ACTION_SET_NULL());*/
  73. break;
  74. }
  75. case tsip_i_request:
  76. TSK_DEBUG_INFO("invite_handle_event(tsip_i_request)");
  77. break;
  78. case tsip_ao_request:
  79. TSK_DEBUG_INFO("invite_handle_event(tsip_ao_request)");
  80. break;
  81. /* Explicit Call Transfer (ECT) */
  82. case tsip_o_ect_trying:
  83. case tsip_o_ect_accepted:
  84. case tsip_o_ect_completed:
  85. case tsip_o_ect_failed:
  86. case tsip_o_ect_notify:
  87. case tsip_i_ect_requested:
  88. case tsip_i_ect_newcall:
  89. case tsip_i_ect_completed:
  90. case tsip_i_ect_failed:
  91. case tsip_i_ect_notify:
  92. TSK_DEBUG_INFO("ECT event");
  93. break;
  94. // ============================
  95. // Media Events
  96. //
  97. /* Early Media started */
  98. case tsip_m_early_media:
  99. TSK_DEBUG_INFO("invite_handle_event(tsip_m_early_media)");
  100. break;
  101. /* 3GPP TS 24.610: Communication Hold */
  102. case tsip_m_local_hold_ok:
  103. TSK_DEBUG_INFO("invite_handle_event(tsip_m_local_hold_ok)");
  104. break;
  105. case tsip_m_local_hold_nok:
  106. TSK_DEBUG_INFO("invite_handle_event(tsip_m_local_hold_nok)");
  107. break;
  108. case tsip_m_local_resume_ok:
  109. TSK_DEBUG_INFO("invite_handle_event(tsip_m_local_resume_ok)");
  110. break;
  111. case tsip_m_local_resume_nok:
  112. TSK_DEBUG_INFO("invite_handle_event(tsip_m_local_resume_nok)");
  113. break;
  114. case tsip_m_remote_hold:
  115. TSK_DEBUG_INFO("invite_handle_event(tsip_m_remote_hold)");
  116. break;
  117. case tsip_m_remote_resume:
  118. TSK_DEBUG_INFO("invite_handle_event(tsip_m_remote_resume)");
  119. break;
  120. default:
  121. break;
  122. }
  123. bail:
  124. return ret;
  125. }
  126. tsip_ssession_id_t invite_handle_cmd(cmd_type_t cmd, const opts_L_t* opts)
  127. {
  128. const session_t* session = tsk_null;
  129. tsip_ssession_id_t id = TSIP_SSESSION_INVALID_ID;
  130. if(!(session = session_handle_cmd(cmd, opts))) {
  131. goto bail;
  132. }
  133. else {
  134. id = tsip_ssession_get_id(session->handle);
  135. }
  136. switch(cmd) {
  137. case cmd_audio:
  138. case cmd_audiovideo: {
  139. /* Make Audio/Video call */
  140. tsip_action_handle_t* action_config = action_get_config(opts);
  141. tsip_api_invite_send_invite(session->handle, (cmd == cmd_audio) ? tmedia_audio : (tmedia_audio|tmedia_video),
  142. TSIP_ACTION_SET_CONFIG(action_config),
  143. /* Any other TSIP_ACTION_SET_*() macros */
  144. TSIP_ACTION_SET_NULL());
  145. TSK_OBJECT_SAFE_FREE(action_config);
  146. break;
  147. }
  148. case cmd_file: {
  149. /* Send file using MSRP protocol */
  150. tsip_action_handle_t* action_config = action_get_config(opts);
  151. const opt_t* opt = opt_get_by_type(opts, opt_path); // existance already checked
  152. /* Set Callback function */
  153. tsip_ssession_set(session->handle,
  154. TSIP_SSESSION_SET_MEDIA(
  155. TSIP_MSESSION_SET_MSRP_CB(invite_msrp_cb),
  156. TSIP_MSESSION_SET_NULL()
  157. ),
  158. TSIP_SSESSION_SET_NULL());
  159. /* Send INVITE */
  160. tsip_api_invite_send_invite(session->handle, tmedia_msrp,
  161. TSIP_ACTION_SET_CONFIG(action_config),
  162. TSIP_ACTION_SET_MEDIA(
  163. TMEDIA_SESSION_MSRP_SET_STR("file-path", opt->value),
  164. TMEDIA_SESSION_MSRP_SET_STR("accept-types", "application/octet-stream"),
  165. //TMEDIA_SESSION_MSRP_SET_STR("accept-wrapped-types", "application/octet-stream"),
  166. //TMEDIA_SESSION_MSRP_SET_STR("file-selector", "name:\"test.sn\" type:application/octet-stream size:3740 hash:sha-1:27:D0:AE:39:48:77:37:1D:FD:39:7E:2D:78:2F:BC:7B:94:48:29:81"),
  167. //TMEDIA_SESSION_MSRP_SET_STR("file-disposition", "attachment"),
  168. //TMEDIA_SESSION_MSRP_SET_STR("file-date", "creation:2010-02-13T17:50:31.763Z"),
  169. //TMEDIA_SESSION_MSRP_SET_STR("file-icon", "cid:test@doubango.org"),
  170. TMEDIA_SESSION_SET_NULL()
  171. ),
  172. TSIP_ACTION_SET_NULL());
  173. TSK_OBJECT_SAFE_FREE(action_config);
  174. break;
  175. }
  176. case cmd_dtmf: {
  177. const opt_t* opt = opt_get_by_type(opts, opt_event); // existance already checked
  178. tsip_action_handle_t* action_config = action_get_config(opts);
  179. tsip_api_invite_send_dtmf(session->handle, atoi(opt->value),
  180. TSIP_ACTION_SET_CONFIG(action_config),
  181. /* Any other TSIP_ACTION_SET_*() macros */
  182. TSIP_ACTION_SET_NULL());
  183. TSK_OBJECT_SAFE_FREE(action_config);
  184. break;
  185. }
  186. case cmd_ect: {
  187. /* Explict Call Transfer */
  188. const opt_t* opt = opt_get_by_type(opts, opt_to); // existance already checked
  189. tsip_action_handle_t* action_config = action_get_config(opts);
  190. tsip_api_invite_send_ect(session->handle, opt? opt->value : "sip:anonymous@example.com",
  191. TSIP_ACTION_SET_CONFIG(action_config),
  192. /* Any other TSIP_ACTION_SET_*() macros */
  193. TSIP_ACTION_SET_NULL());
  194. TSK_OBJECT_SAFE_FREE(action_config);
  195. break;
  196. }
  197. case cmd_hold: {
  198. /* Put the session on hold state */
  199. tsip_action_handle_t* action_config = action_get_config(opts);
  200. tsip_api_invite_send_hold(session->handle, tmedia_all,
  201. TSIP_ACTION_SET_CONFIG(action_config),
  202. /* Any other TSIP_ACTION_SET_*() macros */
  203. TSIP_ACTION_SET_NULL());
  204. TSK_OBJECT_SAFE_FREE(action_config);
  205. break;
  206. }
  207. case cmd_resume: {
  208. /* Put the session on hold state */
  209. tsip_action_handle_t* action_config = action_get_config(opts);
  210. tsip_api_invite_send_resume(session->handle, tmedia_all,
  211. TSIP_ACTION_SET_CONFIG(action_config),
  212. /* Any other TSIP_ACTION_SET_*() macros */
  213. TSIP_ACTION_SET_NULL());
  214. TSK_OBJECT_SAFE_FREE(action_config);
  215. break;
  216. }
  217. default:
  218. /* already handled by session_handle_cmd() */
  219. break;
  220. }
  221. bail:
  222. return id;
  223. }
  224. int invite_msrp_cb(const tmsrp_event_t* _event)
  225. {
  226. const session_t* session = _event->callback_data;
  227. if(_event->message) {
  228. if(_event->message->end_line.cflag == '$') {
  229. TSK_DEBUG_INFO("Last Chunck");
  230. }
  231. switch(_event->message->type) {
  232. case tmsrp_request: {
  233. /* MSRP Request */
  234. break;
  235. }
  236. case tmsrp_response: {
  237. /* MSRP Response */
  238. if(_event->message->ByteRange) {
  239. TSK_DEBUG_INFO("MSRP Response code=%hi %lld-%lld/%lld", TMSRP_RESPONSE_CODE(_event->message),
  240. _event->message->ByteRange->start, _event->message->ByteRange->end, _event->message->ByteRange->total);
  241. }
  242. break;
  243. }
  244. default:
  245. TSK_DEBUG_ERROR("Invalid MSRP message");
  246. break;
  247. }
  248. }
  249. return 0;
  250. }