]> sjero.net Git - linphone/blobdiff - coreapi/chat.c
Add fmtp parameters to opus payload to enable FEC and DTX
[linphone] / coreapi / chat.c
index b9894ea8fabbf7750b23b14e7921ae238a771604..c12bb33988a984f29c5d3b8491c24cac257aafb3 100644 (file)
@@ -62,12 +62,15 @@ void linphone_chat_room_destroy(LinphoneChatRoom *cr){
        ms_free(cr->peer);
 }
 
+
+
 static void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatMessage* msg){
        const char *route=NULL;
        const char *identity=linphone_core_find_best_identity(cr->lc,cr->peer_url,&route);
        SalOp *op=NULL;
        LinphoneCall *call;
        char* content_type;
+       time_t t=time(NULL);
        
        if (lp_config_get_int(cr->lc->config,"sip","chat_use_call_dialogs",0)){
                if((call = linphone_core_get_call_by_remote_address(cr->lc,cr->peer))!=NULL){
@@ -82,6 +85,7 @@ static void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatM
                        }
                }
        }
+       msg->time=t;
        if (op==NULL){
                /*sending out of calls*/
                op = sal_op_new(cr->lc->sal);
@@ -99,6 +103,9 @@ static void _linphone_chat_room_send_message(LinphoneChatRoom *cr, LinphoneChatM
        } else {
                sal_text_send(op, identity, cr->peer,msg->message);
        }
+       msg->dir=LinphoneChatMessageOutgoing;
+       msg->from=linphone_address_new(identity);
+       linphone_chat_message_store(msg);
 }
 
 /**
@@ -125,31 +132,55 @@ void linphone_chat_room_message_received(LinphoneChatRoom *cr, LinphoneCore *lc,
        
 }
 
-void linphone_core_message_received(LinphoneCore *lc, SalOp *op, const SalMessage *sal_msg){
+/**
+ * Retrieve an existing chat room whose peer is the supplied address, if exists.
+ * @param lc the linphone core
+ * @param add a linphone address.
+ * @returns the matching chatroom, or NULL if no such chatroom exists.
+**/
+LinphoneChatRoom *linphone_core_get_chat_room(LinphoneCore *lc, const LinphoneAddress *addr){
+       LinphoneChatRoom *cr=NULL;
        MSList *elem;
+       for(elem=lc->chatrooms;elem!=NULL;elem=ms_list_next(elem)){
+               cr=(LinphoneChatRoom*)elem->data;
+               if (linphone_chat_room_matches(cr,addr)){
+                       break;
+               }
+               cr=NULL;
+       }
+       return cr;
+}
+
+void linphone_core_message_received(LinphoneCore *lc, SalOp *op, const SalMessage *sal_msg){
+       
        LinphoneChatRoom *cr=NULL;
        LinphoneAddress *addr;
        char *cleanfrom;
+       char *from;
        LinphoneChatMessage* msg;
        const SalCustomHeader *ch;
        
        addr=linphone_address_new(sal_msg->from);
        linphone_address_clean(addr);
-       for(elem=lc->chatrooms;elem!=NULL;elem=ms_list_next(elem)){
-               cr=(LinphoneChatRoom*)elem->data;
-               if (linphone_chat_room_matches(cr,addr)){
-                       break;
-               }
-               cr=NULL;
-       }
+       cr=linphone_core_get_chat_room(lc,addr);
        cleanfrom=linphone_address_as_string(addr);
+       from=linphone_address_as_string_uri_only(addr);
        if (cr==NULL){
                /* create a new chat room */
                cr=linphone_core_create_chat_room(lc,cleanfrom);
        }
        msg = linphone_chat_room_create_message(cr, sal_msg->text);
        linphone_chat_message_set_from(msg, cr->peer_url);
+       
+       {
+               LinphoneAddress *to;
+               to=sal_op_get_to(op) ? linphone_address_new(sal_op_get_to(op)) : linphone_address_new(linphone_core_get_identity(lc));
+               msg->to=to;
+       }
+       
        msg->time=sal_msg->time;
+       msg->state=LinphoneChatMessageStateDelivered;
+       msg->is_read=FALSE;
        ch=sal_op_get_custom_header(op);
        if (ch) msg->custom_headers=sal_custom_header_clone(ch);
        
@@ -157,8 +188,10 @@ void linphone_core_message_received(LinphoneCore *lc, SalOp *op, const SalMessag
                linphone_chat_message_set_external_body_url(msg, sal_msg->url);
        }
        linphone_address_destroy(addr);
+       linphone_chat_message_store(msg);
        linphone_chat_room_message_received(cr,lc,msg);
        ms_free(cleanfrom);
+       ms_free(from);
 }
 
 /**
@@ -201,6 +234,7 @@ LinphoneChatMessage* linphone_chat_room_create_message(LinphoneChatRoom *cr, con
        LinphoneChatMessage* msg = ms_new0(LinphoneChatMessage,1);
        msg->chat_room=(LinphoneChatRoom*)cr;
        msg->message=message?ms_strdup(message):NULL;
+       msg->is_read=TRUE;
        return msg;
 }
 
@@ -215,6 +249,7 @@ LinphoneChatMessage* linphone_chat_room_create_message(LinphoneChatRoom *cr, con
 void linphone_chat_room_send_message2(LinphoneChatRoom *cr, LinphoneChatMessage* msg,LinphoneChatMessageStateChangeCb status_cb, void* ud) {
        msg->cb=status_cb;
        msg->cb_ud=ud;
+       msg->state=LinphoneChatMessageStateInProgress;
        _linphone_chat_room_send_message(cr, msg);
 }
 
@@ -297,10 +332,32 @@ void linphone_chat_message_set_from(LinphoneChatMessage* message, const Linphone
  *@param message #LinphoneChatMessage obj
  *@return #LinphoneAddress
  */
-LinphoneAddress* linphone_chat_message_get_from(const LinphoneChatMessage* message) {
+const LinphoneAddress* linphone_chat_message_get_from(const LinphoneChatMessage* message) {
        return message->from;
 }
 
+/**
+ * Get destination of the message 
+ *@param message #LinphoneChatMessage obj
+ *@return #LinphoneAddress
+ */
+const LinphoneAddress* linphone_chat_message_get_to(const LinphoneChatMessage* message){
+       if (message->to) return message->to;
+       if (message->dir==LinphoneChatMessageOutgoing){
+               return message->chat_room->peer_url;
+       }
+       return NULL;
+}
+
+/**
+ * Returns the origin address of a message if it was a outgoing message, or the destination address if it was an incoming message.
+ *@param message #LinphoneChatMessage obj
+ *@return #LinphoneAddress
+ */
+LinphoneAddress *linphone_chat_message_get_local_address(const LinphoneChatMessage* message){
+       return message->dir==LinphoneChatMessageOutgoing ? message->from : message->to;
+}
+
 /**
  * Get the time the message was sent.
  */
@@ -308,6 +365,15 @@ time_t linphone_chat_message_get_time(const LinphoneChatMessage* message) {
        return message->time;
 }
 
+/**
+ * Get the state of the message
+ *@param message #LinphoneChatMessage obj
+ *@return #LinphoneChatMessageState
+ */
+LinphoneChatMessageState linphone_chat_message_get_state(const LinphoneChatMessage* message) {
+       return message->state;
+}
+
 /**
  * Get text part of this message
  * @return text or NULL if no text.
@@ -347,6 +413,9 @@ LinphoneChatMessage* linphone_chat_message_clone(const LinphoneChatMessage* msg)
         void* message_userdata;
         char* external_body_url;
         LinphoneAddress* from;
+        time_t time;
+        SalCustomHeader *custom_headers;
+        LinphoneChatMessageState state;
         };*/
        LinphoneChatMessage* new_message = linphone_chat_room_create_message(msg->chat_room,msg->message);
        if (msg->external_body_url) new_message->external_body_url=ms_strdup(msg->external_body_url);
@@ -354,6 +423,8 @@ LinphoneChatMessage* linphone_chat_message_clone(const LinphoneChatMessage* msg)
        new_message->cb_ud=msg->cb_ud;
        new_message->message_userdata=msg->message_userdata;
        new_message->cb=msg->cb;
+       new_message->time=msg->time;
+       new_message->state=msg->state;
        if (msg->from) new_message->from=linphone_address_clone(msg->from);
        return new_message;
 }
@@ -365,6 +436,7 @@ void linphone_chat_message_destroy(LinphoneChatMessage* msg) {
        if (msg->message) ms_free(msg->message);
        if (msg->external_body_url) ms_free(msg->external_body_url);
        if (msg->from) linphone_address_destroy(msg->from);
+       if (msg->to) linphone_address_destroy(msg->to);
        if (msg->custom_headers) sal_custom_header_free(msg->custom_headers);
        ms_free(msg);
 }