ht1-callcontrol.dox 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. /**
  2. * @ingroup libeXosip2 The eXtented eXosip stack
  3. * @defgroup howto_callcontrol How-To initiate, modify or terminate calls.
  4. eXosip2 offers a flexible API to help you controling calls.
  5. <H2>Initiate a call</H2>
  6. To start an outgoing call, you typically need a few headers which
  7. will be used by eXosip2 to build a default SIP INVITE request. The
  8. code below is used to start a call:
  9. ~~~~~~~{.c}
  10. osip_message_t *invite;
  11. int cid;
  12. int i;
  13. i = eXosip_call_build_initial_invite (ctx, &invite, "<sip:to@antisip.com>",
  14. "<sip:from@antisip.com>",
  15. NULL, // optional route header
  16. "This is a call for a conversation");
  17. if (i != 0)
  18. {
  19. return -1;
  20. }
  21. osip_message_set_supported (invite, "100rel");
  22. {
  23. char tmp[4096];
  24. char localip[128];
  25. eXosip_guess_localip (ctx, AF_INET, localip, 128);
  26. snprintf (tmp, 4096,
  27. "v=0\r\n"
  28. "o=jack 0 0 IN IP4 %s\r\n"
  29. "s=conversation\r\n"
  30. "c=IN IP4 %s\r\n"
  31. "t=0 0\r\n"
  32. "m=audio %s RTP/AVP 0 8 101\r\n"
  33. "a=rtpmap:0 PCMU/8000\r\n"
  34. "a=rtpmap:8 PCMA/8000\r\n"
  35. "a=rtpmap:101 telephone-event/8000\r\n"
  36. "a=fmtp:101 0-11\r\n", localip, localip, port);
  37. osip_message_set_body (invite, tmp, strlen (tmp));
  38. osip_message_set_content_type (invite, "application/sdp");
  39. }
  40. eXosip_lock (ctx);
  41. cid = eXosip_call_send_initial_invite (ctx, invite);
  42. if (cid > 0)
  43. {
  44. eXosip_call_set_reference (ctx, i, reference);
  45. }
  46. eXosip_unlock (ctx);
  47. return i;
  48. ~~~~~~~
  49. The above code is using eXosip_call_build_initial_invite to build
  50. a default SIP INVITE request for a new call. You have to insert
  51. a SDP body announcing your audio parameter for the RTP stream.
  52. The above code also show the flexibility of the eXosip2 API which
  53. allow you to insert additionnal headers such as "Supported: 100rel"
  54. (announcing support for a SIP extension). Thus you can enterely
  55. control the creation of SIP requests.
  56. The returned element of eXosip_call_send_initial_invite is the
  57. cid (call identifier) that you can use to send a CANCEL. In future
  58. events other than 100 Trying, you'll also get the did (dialog identifier)
  59. that will also be needed to control established calls.
  60. eXosip_call_set_reference is also a mean to attach one of your
  61. own context to a call so that you'll get your pointer back in
  62. eXosip_event.
  63. <H2>Answer a call</H2>
  64. The code below is another example that teach you how to answer
  65. an incoming call.
  66. You'll usually need to send a "180 Ringing" SIP answer when
  67. receiving a SIP INVITE:
  68. ~~~~~~~{.c}
  69. eXosip_lock (ctx);
  70. eXosip_call_send_answer (ctx, evt->tid, 180, NULL);
  71. eXosip_unlock (ctx);
  72. ~~~~~~~
  73. <b>Note</b>: The above code also shows that the stack is sometimes able to
  74. build and send a default SIP messages with only one API call
  75. Then, when the user wants to answer the call, you'll need to send a
  76. 200 ok and insert a SDP body in your SIP answer:
  77. ~~~~~~~{.c}
  78. osip_message_t *answer = NULL;
  79. eXosip_lock (ctx);
  80. i = eXosip_call_build_answer (ctx, evt->tid, 200, &answer);
  81. if (i != 0)
  82. {
  83. eXosip_call_send_answer (ctx, evt->tid, 400, NULL);
  84. }
  85. else
  86. {
  87. i = sdp_complete_200ok (evt->did, answer);
  88. if (i != 0)
  89. {
  90. osip_message_free (answer);
  91. eXosip_call_send_answer (ctx, evt->tid, 415, NULL);
  92. }
  93. else
  94. eXosip_call_send_answer (ctx, evt->tid, 200, answer);
  95. }
  96. eXosip_unlock (ctx);
  97. ~~~~~~~
  98. <b>Note</b>: In the above code, you can note that to send a response
  99. to a request, you have to use the tid (transaction identifier) and not a
  100. cid (call identifier) or a did (dialog identifier).
  101. <b>Note2</b>: For sending a 200ok, you'll usually need to insert
  102. a SDP body in the answer and before this, to negotiate the parameters
  103. and codecs that you want to support. This is left to you! Once
  104. you have created the SDP, you add it in the answer using the following
  105. code:
  106. ~~~~~~~{.c}
  107. osip_message_set_body (answer, tmp, strlen (tmp));
  108. osip_message_set_content_type (answer, "application/sdp");
  109. ~~~~~~~
  110. <H2>Terminate a Call</H2>
  111. Simple API, no much to say about it! You can use it when you
  112. want: it will either send a CANCEL, a negative answer or
  113. a BYE depending on the call state.
  114. ~~~~~~~{.c}
  115. eXosip_lock (ctx);
  116. eXosip_call_terminate (ctx, cid, did);
  117. eXosip_unlock (ctx);
  118. ~~~~~~~
  119. <b>Note</b>: You can't stop a call where no 100 Trying has been
  120. received. In that case, you need to wait before sending a CANCEL
  121. or a BYE... This is per rfc3261.
  122. <H2>Sending INFO, REFER, UPDATE, NOTIFY, OPTIONS request</H2>
  123. The call control API allows you to send and receive REFER, UPDATE, INFO,
  124. OPTIONS, NOTIFY and INVITEs whitin calls.
  125. Here you have a code sample to send an INFO requests used to
  126. send an out of band dtmf within the signalling layer. (not
  127. standard, but still used on some system!)
  128. ~~~~~~~{.c}
  129. osip_message_t *info;
  130. char dtmf_body[1000];
  131. int i;
  132. eXosip_lock (ctx);
  133. i = eXosip_call_build_info (ctx, evt->did, &info);
  134. if (i == 0)
  135. {
  136. snprintf (dtmf_body, 999, "Signal=%c\r\nDuration=250\r\n", c);
  137. osip_message_set_content_type (info, "application/dtmf-relay");
  138. osip_message_set_body (info, dtmf_body, strlen (dtmf_body));
  139. i = eXosip_call_send_request (ctx, evt->did, info);
  140. }
  141. eXosip_unlock (ctx);
  142. ~~~~~~~
  143. <H2>Sending any other request, with any header</H2>
  144. You can in fact, send any kind of other request using eXosip2 API.
  145. You will find many other API to build any kind of sip message.
  146. Using osip API, you can add any header or body in those message.
  147. eXosip2 will always prepare the minimal and technical stuff you need.
  148. ~~~~~~~{.c}
  149. osip_message_t *message;
  150. char body[1000];
  151. int i;
  152. eXosip_lock (ctx);
  153. i = eXosip_call_build_request (ctx, evt->did, "PRIVATECOMMAND", &message);
  154. if (i == 0)
  155. {
  156. snprintf (body, 999, "room=1;light=on\r\nroom=2;light=off\r\n");
  157. osip_message_set_content_type (message, "application/antisip-domotic");
  158. osip_message_set_body (message, body, strlen (body));
  159. osip_message_set_header (invite, "P-MyCommand", "option=value");
  160. i = eXosip_call_send_request (ctx, evt->did, message);
  161. }
  162. eXosip_unlock (ctx);
  163. ~~~~~~~
  164. */