当前位置:   article > 正文

Liblinphone 3.9.1中文--Modules--Tutorials:

liblinphone

Tutorials(教程)


 1.Basic buddy status notification (基本的好友状态通知)

    这个程序是一个非常简单的liblinphone用例,演示如何发起SIP订阅和接收来自sip uri标识(从命令行传递的)的通知.

    参数必须像 sip:jehan@sip.linphone.org        

    例如 budy_list sip:jehan@sip.linphone.org

    订阅在SIGINT上清除


  1. /*
  2. buddy_status
  3. Copyright (C) 2010 Belledonne Communications SARL
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #ifdef IN_LINPHONE
  17. #include "linphonecore.h"
  18. #else
  19. #include "linphone/linphonecore.h"
  20. #endif
  21. #include <signal.h>
  22. static bool_t running=TRUE;
  23. static void stop(int signum){
  24. running=FALSE;
  25. }
  26. static void notify_presence_recv_updated (LinphoneCore *lc, LinphoneFriend *friend) {
  27. const LinphonePresenceModel* model = linphone_friend_get_presence_model(friend);
  28. const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
  29. LinphonePresenceActivity *activity = linphone_presence_model_get_activity(model);
  30. char *activity_str = linphone_presence_activity_to_string(activity);
  31. printf("New state state [%s] for user id [%s] \n"
  32. ,activity_str
  33. ,linphone_address_as_string (friend_address));
  34. }
  35. static void new_subscription_requested (LinphoneCore *lc, LinphoneFriend *friend, const char* url) {
  36. const LinphoneAddress* friend_address = linphone_friend_get_address(friend);
  37. printf(" [%s] wants to see your status, accepting\n"
  38. ,linphone_address_as_string (friend_address));
  39. linphone_friend_edit(friend); /* start editing friend */
  40. linphone_friend_set_inc_subscribe_policy(friend,LinphoneSPAccept); /* Accept incoming subscription request for this friend*/
  41. linphone_friend_done(friend); /*commit change*/
  42. linphone_core_add_friend(lc,friend); /* add this new friend to the buddy list*/
  43. }
  44. static void registration_state_changed(struct _LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const char *message){
  45. printf("New registration state %s for user id [%s] at proxy [%s]"
  46. ,linphone_registration_state_to_string(cstate)
  47. ,linphone_proxy_config_get_identity(cfg)
  48. ,linphone_proxy_config_get_addr(cfg));
  49. }
  50. LinphoneCore *lc;
  51. int main(int argc, char *argv[]){
  52. LinphoneCoreVTable vtable={0};
  53. char* dest_friend=NULL;
  54. char* identity=NULL;
  55. char* password=NULL;
  56. LinphoneFriend* my_friend=NULL;
  57. /* takes sip uri identity from the command line arguments */
  58. if (argc>1){
  59. dest_friend=argv[1];
  60. }
  61. /* takes sip uri identity from the command line arguments */
  62. if (argc>2){
  63. identity=argv[2];
  64. }
  65. /* takes password from the command line arguments */
  66. if (argc>3){
  67. password=argv[3];
  68. }
  69. signal(SIGINT,stop);
  70. //#define DEBUG
  71. #ifdef DEBUG
  72. linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
  73. #endif
  74. /*
  75. Fill the LinphoneCoreVTable with application callbacks.
  76. All are optional. Here we only use the both notify_presence_received and new_subscription_requested callbacks
  77. in order to get notifications about friend status.
  78. */
  79. vtable.notify_presence_received=notify_presence_recv_updated;
  80. vtable.new_subscription_requested=new_subscription_requested;
  81. vtable.registration_state_changed=registration_state_changed; /*just in case sip proxy is used*/
  82. /*
  83. Instantiate a LinphoneCore object given the LinphoneCoreVTable
  84. */
  85. lc=linphone_core_new(&vtable,NULL,NULL,NULL);
  86. /*sip proxy might be requested*/
  87. if (identity != NULL) {
  88. /*create proxy config*/
  89. LinphoneProxyConfig* proxy_cfg = linphone_proxy_config_new();
  90. /*parse identity*/
  91. LinphoneAddress *from = linphone_address_new(identity);
  92. LinphoneAuthInfo *info;
  93. if (from==NULL){
  94. printf("%s not a valid sip uri, must be like sip:toto@sip.linphone.org \n",identity);
  95. goto end;
  96. }
  97. if (password!=NULL){
  98. info=linphone_auth_info_new(linphone_address_get_username(from),NULL,password,NULL,NULL,NULL); /*create authentication structure from identity*/
  99. linphone_core_add_auth_info(lc,info); /*add authentication info to LinphoneCore*/
  100. }
  101. // configure proxy entries
  102. linphone_proxy_config_set_identity(proxy_cfg,identity); /*set identity with user name and domain*/
  103. linphone_proxy_config_set_server_addr(proxy_cfg,linphone_address_get_domain(from)); /* we assume domain = proxy server address*/
  104. linphone_proxy_config_enable_register(proxy_cfg,TRUE); /*activate registration for this proxy config*/
  105. linphone_proxy_config_enable_publish(proxy_cfg,TRUE); /* enable presence satus publication for this proxy*/
  106. linphone_address_destroy(from); /*release resource*/
  107. linphone_core_add_proxy_config(lc,proxy_cfg); /*add proxy config to linphone core*/
  108. linphone_core_set_default_proxy(lc,proxy_cfg); /*set to default proxy*/
  109. /* Loop until registration status is available */
  110. do {
  111. linphone_core_iterate(lc); /* first iterate initiates registration */
  112. ms_usleep(100000);
  113. }
  114. while( running && linphone_proxy_config_get_state(proxy_cfg) == LinphoneRegistrationProgress);
  115. }
  116. if (dest_friend) {
  117. my_friend = linphone_core_create_friend_with_address(lc, dest_friend); /*creates friend object from dest*/
  118. if (my_friend == NULL) {
  119. printf("bad destination uri for friend [%s]\n",dest_friend);
  120. goto end;
  121. }
  122. linphone_friend_enable_subscribes(my_friend,TRUE); /*configure this friend to emit SUBSCRIBE message after being added to LinphoneCore*/
  123. linphone_friend_set_inc_subscribe_policy(my_friend,LinphoneSPAccept); /* Accept incoming subscription request for this friend*/
  124. linphone_core_add_friend(lc,my_friend); /* add my friend to the buddy list, initiate SUBSCRIBE message*/
  125. }
  126. /*set my status to online*/
  127. linphone_core_set_presence_model(lc, linphone_presence_model_new_with_activity(LinphonePresenceActivityOnline, NULL));
  128. /* main loop for receiving notifications and doing background linphone core work: */
  129. while(running){
  130. linphone_core_iterate(lc); /* first iterate initiates subscription */
  131. ms_usleep(50000);
  132. }
  133. /* change my presence status to offline*/
  134. linphone_core_set_presence_model(lc, linphone_presence_model_new_with_activity(LinphonePresenceActivityOffline, NULL));
  135. linphone_core_iterate(lc); /* just to make sure new status is initiate message is issued */
  136. linphone_friend_edit(my_friend); /* start editing friend */
  137. linphone_friend_enable_subscribes(my_friend,FALSE); /*disable subscription for this friend*/
  138. linphone_friend_done(my_friend); /*commit changes triggering an UNSUBSCRIBE message*/
  139. linphone_core_iterate(lc); /* just to make sure unsubscribe message is issued */
  140. end:
  141. printf("Shutting down...\n");
  142. linphone_core_destroy(lc);
  143. printf("Exited\n");
  144. return 0;
  145. }



 2.Chat room and messaging (聊天室和消息传递)

    这个程序是一个liblinphone的一个非常简单的用例,演示如何发送/接收来自一个sip uri标识的SIP消息.

    参数必须像 sip:jehan@sip.linphone.org.

    例如 chatroom sip:jehan@sip.linphone.org

    

  1. /*
  2. linphone
  3. Copyright (C) 2010 Belledonne Communications SARL
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #ifdef IN_LINPHONE
  17. #include "linphonecore.h"
  18. #else
  19. #include "linphone/linphonecore.h"
  20. #endif
  21. #include <signal.h>
  22. static bool_t running=TRUE;
  23. static void stop(int signum){
  24. running=FALSE;
  25. }
  26. void text_received(LinphoneCore *lc, LinphoneChatRoom *room, const LinphoneAddress *from, const char *message) {
  27. printf(" Message [%s] received from [%s] \n",message,linphone_address_as_string (from));
  28. }
  29. LinphoneCore *lc;
  30. int main(int argc, char *argv[]){
  31. LinphoneCoreVTable vtable={0};
  32. char* dest_friend=NULL;
  33. LinphoneChatRoom* chat_room;
  34. /* takes sip uri identity from the command line arguments */
  35. if (argc>1){
  36. dest_friend=argv[1];
  37. }
  38. signal(SIGINT,stop);
  39. //#define DEBUG
  40. #ifdef DEBUG
  41. linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
  42. #endif
  43. /*
  44. Fill the LinphoneCoreVTable with application callbacks.
  45. All are optional. Here we only use the text_received callback
  46. in order to get notifications about incoming message.
  47. */
  48. vtable.text_received=text_received;
  49. /*
  50. Instantiate a LinphoneCore object given the LinphoneCoreVTable
  51. */
  52. lc=linphone_core_new(&vtable,NULL,NULL,NULL);
  53. /*Next step is to create a chat root*/
  54. chat_room = linphone_core_get_chat_room_from_uri(lc,dest_friend);
  55. linphone_chat_room_send_message(chat_room,"Hello world"); /*sending message*/
  56. /* main loop for receiving incoming messages and doing background linphone core work: */
  57. while(running){
  58. linphone_core_iterate(lc);
  59. ms_usleep(50000);
  60. }
  61. printf("Shutting down...\n");
  62. linphone_core_destroy(lc);
  63. printf("Exited\n");
  64. return 0;
  65. }

 3.Basic call(基本的通话)

   这个程序是一个非常简单的liblinphone用例.它只是将sip uri作为第一个参数并试图调用它.

  1. /*
  2. linphone
  3. Copyright (C) 2010 Belledonne Communications SARL
  4. (simon.morlat@linphone.org)
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. */
  17. #ifdef IN_LINPHONE
  18. #include "linphonecore.h"
  19. #else
  20. #include "linphone/linphonecore.h"
  21. #endif
  22. #include <signal.h>
  23. static bool_t running=TRUE;
  24. static void stop(int signum){
  25. running=FALSE;
  26. }
  27. /*
  28. * Call state notification callback
  29. */
  30. static void call_state_changed(LinphoneCore *lc, LinphoneCall *call, LinphoneCallState cstate, const char *msg){
  31. switch(cstate){
  32. case LinphoneCallOutgoingRinging:
  33. printf("It is now ringing remotely !\n");
  34. break;
  35. case LinphoneCallOutgoingEarlyMedia:
  36. printf("Receiving some early media\n");
  37. break;
  38. case LinphoneCallConnected:
  39. printf("We are connected !\n");
  40. break;
  41. case LinphoneCallStreamsRunning:
  42. printf("Media streams established !\n");
  43. break;
  44. case LinphoneCallEnd:
  45. printf("Call is terminated.\n");
  46. break;
  47. case LinphoneCallError:
  48. printf("Call failure !");
  49. break;
  50. default:
  51. printf("Unhandled notification %i\n",cstate);
  52. }
  53. }
  54. int main(int argc, char *argv[]){
  55. LinphoneCoreVTable vtable={0};
  56. LinphoneCore *lc;
  57. LinphoneCall *call=NULL;
  58. const char *dest=NULL;
  59. /* take the destination sip uri from the command line arguments */
  60. if (argc>1){
  61. dest=argv[1];
  62. }
  63. signal(SIGINT,stop);
  64. #ifdef DEBUG
  65. linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
  66. #endif
  67. /*
  68. Fill the LinphoneCoreVTable with application callbacks.
  69. All are optional. Here we only use the call_state_changed callbacks
  70. in order to get notifications about the progress of the call.
  71. */
  72. vtable.call_state_changed=call_state_changed;
  73. /*
  74. Instanciate a LinphoneCore object given the LinphoneCoreVTable
  75. */
  76. lc=linphone_core_new(&vtable,NULL,NULL,NULL);
  77. if (dest){
  78. /*
  79. Place an outgoing call
  80. */
  81. call=linphone_core_invite(lc,dest);
  82. if (call==NULL){
  83. printf("Could not place call to %s\n",dest);
  84. goto end;
  85. }else printf("Call to %s is in progress...",dest);
  86. linphone_call_ref(call);
  87. }
  88. /* main loop for receiving notifications and doing background linphonecore work: */
  89. while(running){
  90. linphone_core_iterate(lc);
  91. ms_usleep(50000);
  92. }
  93. if (call && linphone_call_get_state(call)!=LinphoneCallEnd){
  94. /* terminate the call */
  95. printf("Terminating the call...\n");
  96. linphone_core_terminate_call(lc,call);
  97. /*at this stage we don't need the call object */
  98. linphone_call_unref(call);
  99. }
  100. end:
  101. printf("Shutting down...\n");
  102. linphone_core_destroy(lc);
  103. printf("Exited\n");
  104. return 0;
  105. }



4.Generic subscribe/notify example(通用的订阅/通知例子)

   这个程序是一个非常简单的liblinphone用例.它演示了如何监听一个SIP订阅.然后定期发送通知请求.第一个参数必须像 sip:jehan@sip.linphone.org,第二个参数必须是密码.

   例如 registration sip:jehan@sip.linphone.org 密码

   注册在SIGINT上清除

  1. /*
  2. linphone
  3. Copyright (C) 2010 Belledonne Communications SARL
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #ifdef IN_LINPHONE
  17. #include "linphonecore.h"
  18. #else
  19. #include "linphone/linphonecore.h"
  20. #endif
  21. #include <signal.h>
  22. static bool_t running=TRUE;
  23. static void stop(int signum){
  24. running=FALSE;
  25. }
  26. static void registration_state_changed(struct _LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const char *message){
  27. printf("New registration state %s for user id [%s] at proxy [%s]\n"
  28. ,linphone_registration_state_to_string(cstate)
  29. ,linphone_proxy_config_get_identity(cfg)
  30. ,linphone_proxy_config_get_addr(cfg));
  31. }
  32. LinphoneCore *lc;
  33. int main(int argc, char *argv[]){
  34. LinphoneCoreVTable vtable={0};
  35. LinphoneProxyConfig* proxy_cfg;
  36. LinphoneAddress *from;
  37. LinphoneAuthInfo *info;
  38. char* identity=NULL;
  39. char* password=NULL;
  40. const char* server_addr;
  41. /* takes sip uri identity from the command line arguments */
  42. if (argc>1){
  43. identity=argv[1];
  44. }
  45. /* takes password from the command line arguments */
  46. if (argc>2){
  47. password=argv[2];
  48. }
  49. signal(SIGINT,stop);
  50. #ifdef DEBUG
  51. linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
  52. #endif
  53. /*
  54. Fill the LinphoneCoreVTable with application callbacks.
  55. All are optional. Here we only use the registration_state_changed callbacks
  56. in order to get notifications about the progress of the registration.
  57. */
  58. vtable.registration_state_changed=registration_state_changed;
  59. /*
  60. Instanciate a LinphoneCore object given the LinphoneCoreVTable
  61. */
  62. lc=linphone_core_new(&vtable,NULL,NULL,NULL);
  63. /*create proxy config*/
  64. proxy_cfg = linphone_proxy_config_new();
  65. /*parse identity*/
  66. from = linphone_address_new(identity);
  67. if (from==NULL){
  68. printf("%s not a valid sip uri, must be like sip:toto@sip.linphone.org \n",identity);
  69. goto end;
  70. }
  71. if (password!=NULL){
  72. info=linphone_auth_info_new(linphone_address_get_username(from),NULL,password,NULL,NULL,NULL); /*create authentication structure from identity*/
  73. linphone_core_add_auth_info(lc,info); /*add authentication info to LinphoneCore*/
  74. }
  75. // configure proxy entries
  76. linphone_proxy_config_set_identity(proxy_cfg,identity); /*set identity with user name and domain*/
  77. server_addr = linphone_address_get_domain(from); /*extract domain address from identity*/
  78. linphone_proxy_config_set_server_addr(proxy_cfg,server_addr); /* we assume domain = proxy server address*/
  79. linphone_proxy_config_enable_register(proxy_cfg,TRUE); /*activate registration for this proxy config*/
  80. linphone_address_destroy(from); /*release resource*/
  81. linphone_core_add_proxy_config(lc,proxy_cfg); /*add proxy config to linphone core*/
  82. linphone_core_set_default_proxy(lc,proxy_cfg); /*set to default proxy*/
  83. /* main loop for receiving notifications and doing background linphonecore work: */
  84. while(running){
  85. linphone_core_iterate(lc); /* first iterate initiates registration */
  86. ms_usleep(50000);
  87. }
  88. proxy_cfg = linphone_core_get_default_proxy_config(lc); /* get default proxy config*/
  89. linphone_proxy_config_edit(proxy_cfg); /*start editing proxy configuration*/
  90. linphone_proxy_config_enable_register(proxy_cfg,FALSE); /*de-activate registration for this proxy config*/
  91. linphone_proxy_config_done(proxy_cfg); /*initiate REGISTER with expire = 0*/
  92. while(linphone_proxy_config_get_state(proxy_cfg) != LinphoneRegistrationCleared){
  93. linphone_core_iterate(lc); /*to make sure we receive call backs before shutting down*/
  94. ms_usleep(50000);
  95. }
  96. end:
  97. printf("Shutting down...\n");
  98. linphone_core_destroy(lc);
  99. printf("Exited\n");
  100. return 0;
  101. }


5.Real Time Text Receiver(实时文本接收器)

   这个程序是可以实时地从端口5060上接收到聊天消息.使用realtimetext_sender来生成聊天信息.用法:./realtimetext_receiver

   

  1. /*
  2. linphone
  3. Copyright (C) 2015 Belledonne Communications SARL
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #ifdef IN_LINPHONE
  17. #include "linphonecore.h"
  18. #else
  19. #include "linphone/linphonecore.h"
  20. #endif
  21. #include <signal.h>
  22. static bool_t running=TRUE;
  23. static void stop(int signum){
  24. running=FALSE;
  25. }
  26. int main(int argc, char *argv[]){
  27. LinphoneCoreVTable vtable={0};
  28. LinphoneCore *lc;
  29. LinphoneCall *call=NULL;
  30. LinphoneChatRoom *chat_room;
  31. LinphoneChatMessage *chat_message=NULL;
  32. const char *dest=NULL;
  33. LCSipTransports tp;
  34. tp.udp_port=LC_SIP_TRANSPORT_RANDOM;
  35. tp.tcp_port=LC_SIP_TRANSPORT_RANDOM;
  36. tp.tls_port=LC_SIP_TRANSPORT_RANDOM;
  37. /* take the destination sip uri from the command line arguments */
  38. if (argc>1){
  39. dest=argv[1];
  40. }
  41. signal(SIGINT,stop);
  42. #ifdef DEBUG
  43. linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
  44. #endif
  45. /*
  46. Instanciate a LinphoneCore object given the LinphoneCoreVTable
  47. */
  48. lc=linphone_core_new(&vtable,NULL,NULL,NULL);
  49. linphone_core_set_sip_transports(lc,&tp); /*to avoid port colliding with receiver*/
  50. if (dest){
  51. /*
  52. Place an outgoing call with rtt enabled
  53. */
  54. LinphoneCallParams *cp = linphone_core_create_call_params(lc, NULL);
  55. linphone_call_params_enable_realtime_text(cp,TRUE); /*enable real time text*/
  56. call=linphone_core_invite_with_params(lc,dest,cp);
  57. linphone_call_params_destroy(cp);
  58. if (call==NULL){
  59. printf("Could not place call to %s\n",dest);
  60. goto end;
  61. }else printf("Call to %s is in progress...",dest);
  62. linphone_call_ref(call);
  63. }
  64. /*wait for call to be established*/
  65. while (running && (linphone_call_get_state(call) == LinphoneCallOutgoingProgress
  66. || linphone_call_get_state(call) == LinphoneCallOutgoingInit)) {
  67. linphone_core_iterate(lc);
  68. ms_usleep(50000);
  69. }
  70. /*check if call is established*/
  71. switch (linphone_call_get_state(call)) {
  72. case LinphoneCallError:
  73. case LinphoneCallReleased:
  74. case LinphoneCallEnd:
  75. printf("Could not place call to %s\n",dest);
  76. goto end;
  77. break;
  78. default:
  79. break;
  80. /*continue*/
  81. }
  82. chat_room=linphone_call_get_chat_room(call); /*create a chat room associated to this call*/
  83. /* main loop for sending message and doing background linphonecore work: */
  84. while(running){
  85. char character;
  86. /*to disable terminal buffering*/
  87. if (system ("/bin/stty raw") == -1){
  88. ms_error("/bin/stty error");
  89. }
  90. character = getchar();
  91. if (system("/bin/stty cooked") == -1){
  92. ms_error("/bin/stty error");
  93. }
  94. if (character==0x03) {/*CTRL C*/
  95. running=0;
  96. break;
  97. }
  98. if (character != EOF) {
  99. /* user has typed something*/
  100. if (chat_message == NULL) {
  101. /*create a new message*/
  102. chat_message = linphone_chat_room_create_message(chat_room,""); /*create an empty message*/
  103. }
  104. if (character == '\r') {
  105. /*new line, committing message*/
  106. linphone_chat_room_send_chat_message(chat_room,chat_message);
  107. chat_message = NULL; /*reset message*/
  108. } else {
  109. linphone_chat_message_put_char(chat_message,character); /*send char in realtime*/
  110. }
  111. }
  112. linphone_core_iterate(lc);
  113. ms_usleep(50000);
  114. }
  115. if (call && linphone_call_get_state(call)!=LinphoneCallEnd){
  116. /* terminate the call */
  117. printf("Terminating the call...\n");
  118. linphone_core_terminate_call(lc,call);
  119. /*at this stage we don't need the call object */
  120. linphone_call_unref(call);
  121. }
  122. end:
  123. printf("Shutting down...\n");
  124. linphone_core_destroy(lc);
  125. printf("Exited\n");
  126. return 0;
  127. }


6.Real Time Text Sender

    这个程序仅实时地发送聊天消息到目标URI.用realtimetext_receiver来接收消息.用法:./realtimetext_sender sip:localhost:5060

  1. /*
  2. linphone
  3. Copyright (C) 2015 Belledonne Communications SARL
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #ifdef IN_LINPHONE
  17. #include "linphonecore.h"
  18. #else
  19. #include "linphone/linphonecore.h"
  20. #endif
  21. #include <signal.h>
  22. static bool_t running=TRUE;
  23. static void stop(int signum){
  24. running=FALSE;
  25. }
  26. int main(int argc, char *argv[]){
  27. LinphoneCoreVTable vtable={0};
  28. LinphoneCore *lc;
  29. LinphoneCall *call=NULL;
  30. LinphoneChatRoom *chat_room;
  31. LinphoneChatMessage *chat_message=NULL;
  32. const char *dest=NULL;
  33. LCSipTransports tp;
  34. tp.udp_port=LC_SIP_TRANSPORT_RANDOM;
  35. tp.tcp_port=LC_SIP_TRANSPORT_RANDOM;
  36. tp.tls_port=LC_SIP_TRANSPORT_RANDOM;
  37. /* take the destination sip uri from the command line arguments */
  38. if (argc>1){
  39. dest=argv[1];
  40. }
  41. signal(SIGINT,stop);
  42. #ifdef DEBUG
  43. linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
  44. #endif
  45. /*
  46. Instanciate a LinphoneCore object given the LinphoneCoreVTable
  47. */
  48. lc=linphone_core_new(&vtable,NULL,NULL,NULL);
  49. linphone_core_set_sip_transports(lc,&tp); /*to avoid port colliding with receiver*/
  50. if (dest){
  51. /*
  52. Place an outgoing call with rtt enabled
  53. */
  54. LinphoneCallParams *cp = linphone_core_create_call_params(lc, NULL);
  55. linphone_call_params_enable_realtime_text(cp,TRUE); /*enable real time text*/
  56. call=linphone_core_invite_with_params(lc,dest,cp);
  57. linphone_call_params_destroy(cp);
  58. if (call==NULL){
  59. printf("Could not place call to %s\n",dest);
  60. goto end;
  61. }else printf("Call to %s is in progress...",dest);
  62. linphone_call_ref(call);
  63. }
  64. /*wait for call to be established*/
  65. while (running && (linphone_call_get_state(call) == LinphoneCallOutgoingProgress
  66. || linphone_call_get_state(call) == LinphoneCallOutgoingInit)) {
  67. linphone_core_iterate(lc);
  68. ms_usleep(50000);
  69. }
  70. /*check if call is established*/
  71. switch (linphone_call_get_state(call)) {
  72. case LinphoneCallError:
  73. case LinphoneCallReleased:
  74. case LinphoneCallEnd:
  75. printf("Could not place call to %s\n",dest);
  76. goto end;
  77. break;
  78. default:
  79. break;
  80. /*continue*/
  81. }
  82. chat_room=linphone_call_get_chat_room(call); /*create a chat room associated to this call*/
  83. /* main loop for sending message and doing background linphonecore work: */
  84. while(running){
  85. char character;
  86. /*to disable terminal buffering*/
  87. if (system ("/bin/stty raw") == -1){
  88. ms_error("/bin/stty error");
  89. }
  90. character = getchar();
  91. if (system("/bin/stty cooked") == -1){
  92. ms_error("/bin/stty error");
  93. }
  94. if (character==0x03) {/*CTRL C*/
  95. running=0;
  96. break;
  97. }
  98. if (character != EOF) {
  99. /* user has typed something*/
  100. if (chat_message == NULL) {
  101. /*create a new message*/
  102. chat_message = linphone_chat_room_create_message(chat_room,""); /*create an empty message*/
  103. }
  104. if (character == '\r') {
  105. /*new line, committing message*/
  106. linphone_chat_room_send_chat_message(chat_room,chat_message);
  107. chat_message = NULL; /*reset message*/
  108. } else {
  109. linphone_chat_message_put_char(chat_message,character); /*send char in realtime*/
  110. }
  111. }
  112. linphone_core_iterate(lc);
  113. ms_usleep(50000);
  114. }
  115. if (call && linphone_call_get_state(call)!=LinphoneCallEnd){
  116. /* terminate the call */
  117. printf("Terminating the call...\n");
  118. linphone_core_terminate_call(lc,call);
  119. /*at this stage we don't need the call object */
  120. linphone_call_unref(call);
  121. }
  122. end:
  123. printf("Shutting down...\n");
  124. linphone_core_destroy(lc);
  125. printf("Exited\n");
  126. return 0;
  127. }




7.Basic registration(基本的注册)

    这个程序是一个非常简单的liblinphone用例.演示如何从一个sip URI(由命令行传递的)发起一个SIP注册.第一个参数必须像 sip:jehan@sip.linphone.org, 第二个参数必须为密码.

   例如 registration sip:jehan@sip.linphone.org 密码

   注册在SIGINT上被清除.

   

  1. /*
  2. linphone
  3. Copyright (C) 2010 Belledonne Communications SARL
  4. This program is free software; you can redistribute it and/or
  5. modify it under the terms of the GNU General Public License
  6. as published by the Free Software Foundation; either version 2
  7. of the License, or (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  15. */
  16. #ifdef IN_LINPHONE
  17. #include "linphonecore.h"
  18. #else
  19. #include "linphone/linphonecore.h"
  20. #endif
  21. #include <signal.h>
  22. static bool_t running=TRUE;
  23. static void stop(int signum){
  24. running=FALSE;
  25. }
  26. static void registration_state_changed(struct _LinphoneCore *lc, LinphoneProxyConfig *cfg, LinphoneRegistrationState cstate, const char *message){
  27. printf("New registration state %s for user id [%s] at proxy [%s]\n"
  28. ,linphone_registration_state_to_string(cstate)
  29. ,linphone_proxy_config_get_identity(cfg)
  30. ,linphone_proxy_config_get_addr(cfg));
  31. }
  32. LinphoneCore *lc;
  33. int main(int argc, char *argv[]){
  34. LinphoneCoreVTable vtable={0};
  35. LinphoneProxyConfig* proxy_cfg;
  36. LinphoneAddress *from;
  37. LinphoneAuthInfo *info;
  38. char* identity=NULL;
  39. char* password=NULL;
  40. const char* server_addr;
  41. /* takes sip uri identity from the command line arguments */
  42. if (argc>1){
  43. identity=argv[1];
  44. }
  45. /* takes password from the command line arguments */
  46. if (argc>2){
  47. password=argv[2];
  48. }
  49. signal(SIGINT,stop);
  50. #ifdef DEBUG
  51. linphone_core_enable_logs(NULL); /*enable liblinphone logs.*/
  52. #endif
  53. /*
  54. Fill the LinphoneCoreVTable with application callbacks.
  55. All are optional. Here we only use the registration_state_changed callbacks
  56. in order to get notifications about the progress of the registration.
  57. */
  58. vtable.registration_state_changed=registration_state_changed;
  59. /*
  60. Instanciate a LinphoneCore object given the LinphoneCoreVTable
  61. */
  62. lc=linphone_core_new(&vtable,NULL,NULL,NULL);
  63. /*create proxy config*/
  64. proxy_cfg = linphone_proxy_config_new();
  65. /*parse identity*/
  66. from = linphone_address_new(identity);
  67. if (from==NULL){
  68. printf("%s not a valid sip uri, must be like sip:toto@sip.linphone.org \n",identity);
  69. goto end;
  70. }
  71. if (password!=NULL){
  72. info=linphone_auth_info_new(linphone_address_get_username(from),NULL,password,NULL,NULL,NULL); /*create authentication structure from identity*/
  73. linphone_core_add_auth_info(lc,info); /*add authentication info to LinphoneCore*/
  74. }
  75. // configure proxy entries
  76. linphone_proxy_config_set_identity(proxy_cfg,identity); /*set identity with user name and domain*/
  77. server_addr = linphone_address_get_domain(from); /*extract domain address from identity*/
  78. linphone_proxy_config_set_server_addr(proxy_cfg,server_addr); /* we assume domain = proxy server address*/
  79. linphone_proxy_config_enable_register(proxy_cfg,TRUE); /*activate registration for this proxy config*/
  80. linphone_address_destroy(from); /*release resource*/
  81. linphone_core_add_proxy_config(lc,proxy_cfg); /*add proxy config to linphone core*/
  82. linphone_core_set_default_proxy(lc,proxy_cfg); /*set to default proxy*/
  83. /* main loop for receiving notifications and doing background linphonecore work: */
  84. while(running){
  85. linphone_core_iterate(lc); /* first iterate initiates registration */
  86. ms_usleep(50000);
  87. }
  88. proxy_cfg = linphone_core_get_default_proxy_config(lc); /* get default proxy config*/
  89. linphone_proxy_config_edit(proxy_cfg); /*start editing proxy configuration*/
  90. linphone_proxy_config_enable_register(proxy_cfg,FALSE); /*de-activate registration for this proxy config*/
  91. linphone_proxy_config_done(proxy_cfg); /*initiate REGISTER with expire = 0*/
  92. while(linphone_proxy_config_get_state(proxy_cfg) != LinphoneRegistrationCleared){
  93. linphone_core_iterate(lc); /*to make sure we receive call backs before shutting down*/
  94. ms_usleep(50000);
  95. }
  96. end:
  97. printf("Shutting down...\n");
  98. linphone_core_destroy(lc);
  99. printf("Exited\n");
  100. return 0;
  101. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Monodyee/article/detail/128738
推荐阅读
相关标签
  

闽ICP备14008679号