Merge "app_stasis: Don't hang up if app is not registered"
[asterisk/asterisk.git] / addons / ooh323cDriver.c
index dd127a4..3096d1e 100644 (file)
@@ -121,7 +121,7 @@ void* ooh323c_call_thread(void* dummy)
 
  close(mycthread->thePipe[0]);
  close(mycthread->thePipe[1]);
- free(mycthread);
+ ast_free(mycthread);
  ast_module_unref(myself);
  ast_update_use_count();
  return NULL;
@@ -155,7 +155,7 @@ int ooh323c_start_call_thread(ooCallData *call) {
        ast_module_ref(myself);
        if ((socketpair(PF_LOCAL, SOCK_STREAM, 0, cur->thePipe)) == -1) {
                ast_log(LOG_ERROR, "Can't create thread pipe for call %s\n", call->callToken);
-               free(cur);
+               ast_free(cur);
                return -1;
        }
        cur->inUse = TRUE;
@@ -173,7 +173,7 @@ int ooh323c_start_call_thread(ooCallData *call) {
                ast_mutex_destroy(&cur->lock);
                close(cur->thePipe[0]);
                close(cur->thePipe[1]);
-               free(cur);
+               ast_free(cur);
                return -1;
        }
 
@@ -225,17 +225,17 @@ int ooh323c_stop_stack_thread(void)
 }
 
 int ooh323c_set_capability
-   (struct ast_codec_pref *prefs, struct ast_format_cap *cap, int dtmf, int dtmfcodec)
+   (struct ast_format_cap *cap, int dtmf, int dtmfcodec)
 {
    int ret = 0, x;
-   struct ast_format tmpfmt;
    if (gH323Debug) {
      ast_verb(0, "\tAdding capabilities to H323 endpoint\n");
    }
 
-   for(x=0; ast_codec_pref_index(prefs, x, &tmpfmt); x++)
+   for(x=0; x<ast_format_cap_count(cap); x++)
    {
-      if(tmpfmt.id == AST_FORMAT_ULAW)
+    struct ast_format *format = ast_format_cap_get_format(cap, x);
+      if(ast_format_cmp(format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g711 ulaw capability to H323 endpoint\n");
@@ -246,7 +246,7 @@ int ooh323c_set_capability
                                      &ooh323c_stop_receive_channel, 
                                      &ooh323c_stop_transmit_channel);
       }
-      if(tmpfmt.id == AST_FORMAT_ALAW)
+      if(ast_format_cmp(format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g711 alaw capability to H323 endpoint\n");
@@ -258,7 +258,7 @@ int ooh323c_set_capability
                                      &ooh323c_stop_transmit_channel);
       }
 
-      if(tmpfmt.id == AST_FORMAT_G729A)
+      if(ast_format_cmp(format, ast_format_g729) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
            ast_verb(0, "\tAdding g729A capability to H323 endpoint\n");
@@ -287,7 +287,7 @@ int ooh323c_set_capability
                                      &ooh323c_stop_transmit_channel);
       }
 
-      if(tmpfmt.id == AST_FORMAT_G723_1)
+      if(ast_format_cmp(format, ast_format_g723) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g7231 capability to H323 endpoint\n");
@@ -300,7 +300,7 @@ int ooh323c_set_capability
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_G726)
+      if(ast_format_cmp(format, ast_format_g726) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g726 capability to H323 endpoint\n");
@@ -313,7 +313,7 @@ int ooh323c_set_capability
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_G726_AAL2)
+      if(ast_format_cmp(format, ast_format_g726_aal2) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g726aal2 capability to H323 endpoint\n");
@@ -326,7 +326,7 @@ int ooh323c_set_capability
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_H263)
+      if(ast_format_cmp(format, ast_format_h263) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding h263 capability to H323 endpoint\n");
@@ -339,7 +339,7 @@ int ooh323c_set_capability
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_GSM)
+      if(ast_format_cmp(format, ast_format_gsm) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding gsm capability to H323 endpoint\n");
@@ -351,24 +351,8 @@ int ooh323c_set_capability
                                      &ooh323c_stop_transmit_channel);
 
       }
-      
-#ifdef AST_FORMAT_AMRNB
-      if(tmpfmt.id == AST_FORMAT_AMRNB)
-      {
-         if (gH323Debug) {
-            ast_verb(0, "\tAdding amr nb capability to H323 endpoint\n");
-        }
-         ret = ooH323EpAddAMRNBCapability(OO_AMRNB, 4, 4, FALSE, 
-                                     OORXANDTX, &ooh323c_start_receive_channel,
-                                     &ooh323c_start_transmit_channel,
-                                     &ooh323c_stop_receive_channel, 
-                                     &ooh323c_stop_transmit_channel);
-
-      }
-#endif
 
-#ifdef AST_FORMAT_SPEEX
-      if(tmpfmt.id == AST_FORMAT_SPEEX)
+      if(ast_format_cmp(format, ast_format_speex) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding speex capability to H323 endpoint\n");
@@ -380,8 +364,8 @@ int ooh323c_set_capability
                                      &ooh323c_stop_transmit_channel);
 
       }
-#endif
-      
+
+    ao2_ref(format, -1);      
    }
    
    if(dtmf & H323_DTMF_CISCO)
@@ -397,11 +381,10 @@ int ooh323c_set_capability
 }
 
 int ooh323c_set_capability_for_call
-   (ooCallData *call, struct ast_codec_pref *prefs, struct ast_format_cap *cap, int dtmf, int dtmfcodec,
+   (ooCallData *call, struct ast_format_cap *cap, int dtmf, int dtmfcodec,
                 int t38support, int g729onlyA)
 {
    int ret = 0, x, txframes;
-   struct ast_format tmpfmt;
    if (gH323Debug) {
      ast_verb(0, "\tAdding capabilities to call(%s, %s)\n", call->callType, 
                                                             call->callToken);
@@ -423,15 +406,16 @@ int ooh323c_set_capability_for_call
                                        &ooh323c_stop_transmit_datachannel,
                                        0);
 
-   for(x=0; ast_codec_pref_index(prefs, x, &tmpfmt); x++)
+   for(x=0; x<ast_format_cap_count(cap); x++)
    {
-      if(tmpfmt.id == AST_FORMAT_ULAW)
+    struct ast_format *format = ast_format_cap_get_format(cap, x);
+      if(ast_format_cmp(format, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g711 ulaw capability to call(%s, %s)\n", 
                                               call->callType, call->callToken);
         }
-        txframes = prefs->framing[x];
+        txframes = ast_format_cap_get_format_framing(cap, format);
          ret= ooCallAddG711Capability(call, OO_G711ULAW64K, txframes, 
                                       txframes, OORXANDTX, 
                                       &ooh323c_start_receive_channel,
@@ -439,13 +423,13 @@ int ooh323c_set_capability_for_call
                                       &ooh323c_stop_receive_channel, 
                                       &ooh323c_stop_transmit_channel);
       }
-      if(tmpfmt.id == AST_FORMAT_ALAW)
+      if(ast_format_cmp(format, ast_format_alaw) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g711 alaw capability to call(%s, %s)\n",
                                             call->callType, call->callToken);
         }
-         txframes = prefs->framing[x];
+         txframes = ast_format_cap_get_format_framing(cap, format);
          ret= ooCallAddG711Capability(call, OO_G711ALAW64K, txframes, 
                                      txframes, OORXANDTX, 
                                      &ooh323c_start_receive_channel,
@@ -454,13 +438,13 @@ int ooh323c_set_capability_for_call
                                      &ooh323c_stop_transmit_channel);
       }
 
-      if(tmpfmt.id == AST_FORMAT_G726)
+      if(ast_format_cmp(format, ast_format_g726) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g726 capability to call (%s, %s)\n",
                                            call->callType, call->callToken);
         }
-        txframes = prefs->framing[x];
+        txframes = ast_format_cap_get_format_framing(cap, format);
          ret = ooCallAddG726Capability(call, OO_G726, txframes, grxframes, FALSE,
                                      OORXANDTX, &ooh323c_start_receive_channel,
                                      &ooh323c_start_transmit_channel,
@@ -469,13 +453,13 @@ int ooh323c_set_capability_for_call
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_G726_AAL2)
+      if(ast_format_cmp(format, ast_format_g726_aal2) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g726aal2 capability to call (%s, %s)\n",
                                            call->callType, call->callToken);
         }
-        txframes = prefs->framing[x];
+        txframes = ast_format_cap_get_format_framing(cap, format);
          ret = ooCallAddG726Capability(call, OO_G726AAL2, txframes, grxframes, FALSE,
                                      OORXANDTX, &ooh323c_start_receive_channel,
                                      &ooh323c_start_transmit_channel,
@@ -484,10 +468,10 @@ int ooh323c_set_capability_for_call
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_G729A)
+      if(ast_format_cmp(format, ast_format_g729) == AST_FORMAT_CMP_EQUAL)
       {
       
-         txframes = (prefs->framing[x])/10;
+         txframes = (ast_format_cap_get_format_framing(cap, format))/10;
          if (gH323Debug) {
             ast_verb(0, "\tAdding g729A capability to call(%s, %s)\n",
                                             call->callType, call->callToken);
@@ -520,7 +504,7 @@ int ooh323c_set_capability_for_call
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_G723_1)
+      if(ast_format_cmp(format, ast_format_g723) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding g7231 capability to call (%s, %s)\n",
@@ -534,7 +518,7 @@ int ooh323c_set_capability_for_call
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_H263)
+      if(ast_format_cmp(format, ast_format_h263) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding h263 capability to call (%s, %s)\n",
@@ -548,7 +532,7 @@ int ooh323c_set_capability_for_call
 
       }
 
-      if(tmpfmt.id == AST_FORMAT_GSM)
+      if(ast_format_cmp(format, ast_format_gsm) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding gsm capability to call(%s, %s)\n", 
@@ -561,22 +545,7 @@ int ooh323c_set_capability_for_call
                                      &ooh323c_stop_transmit_channel);
       }
 
-#ifdef AST_FORMAT_AMRNB
-      if(tmpfmt.id == AST_FORMAT_AMRNB)
-      {
-         if (gH323Debug) {
-            ast_verb(0, "\tAdding AMR capability to call(%s, %s)\n", 
-                                             call->callType, call->callToken);
-        }
-         ret = ooCallAddAMRNBCapability(call, OO_AMRNB, 4, 4, FALSE, 
-                                     OORXANDTX, &ooh323c_start_receive_channel,
-                                     &ooh323c_start_transmit_channel,
-                                     &ooh323c_stop_receive_channel, 
-                                     &ooh323c_stop_transmit_channel);
-      }
-#endif
-#ifdef AST_FORMAT_SPEEX
-      if(tmpfmt.id == AST_FORMAT_SPEEX)
+      if(ast_format_cmp(format, ast_format_speex) == AST_FORMAT_CMP_EQUAL)
       {
          if (gH323Debug) {
             ast_verb(0, "\tAdding Speex capability to call(%s, %s)\n", 
@@ -588,7 +557,8 @@ int ooh323c_set_capability_for_call
                                      &ooh323c_stop_receive_channel, 
                                      &ooh323c_stop_transmit_channel);
       }
-#endif
+
+      ao2_ref(format, -1);
    }
    return ret;
 }
@@ -622,9 +592,9 @@ int ooh323c_set_aliases(ooAliases * aliases)
    
 int ooh323c_start_receive_channel(ooCallData *call, ooLogicalChannel *pChannel)
 {
-   struct ast_format tmpfmt;
-   convertH323CapToAsteriskCap(pChannel->chanCap->cap, &tmpfmt);
-   if(tmpfmt.id) {
+   struct ast_format *tmpfmt = NULL;
+   tmpfmt = convertH323CapToAsteriskCap(pChannel->chanCap->cap);
+   if(tmpfmt) {
       /* ooh323_set_read_format(call, fmt); */
    }else{
      ast_log(LOG_ERROR, "Invalid capability type for receive channel %s\n",
@@ -636,19 +606,17 @@ int ooh323c_start_receive_channel(ooCallData *call, ooLogicalChannel *pChannel)
 
 int ooh323c_start_transmit_channel(ooCallData *call, ooLogicalChannel *pChannel)
 {
-   struct ast_format tmpfmt;
-   convertH323CapToAsteriskCap(pChannel->chanCap->cap, &tmpfmt);
-   if(tmpfmt.id) {
-      switch (tmpfmt.id) {
-      case AST_FORMAT_ALAW:
-      case AST_FORMAT_ULAW:
-       ooh323_set_write_format(call, &tmpfmt, ((OOCapParams *)(pChannel->chanCap->params))->txframes);
-       break;
-      case AST_FORMAT_G729A:
-       ooh323_set_write_format(call, &tmpfmt, ((OOCapParams *)(pChannel->chanCap->params))->txframes*10);
-       break;
-      default:
-       ooh323_set_write_format(call, &tmpfmt, 0);
+   struct ast_format *tmpfmt = NULL;
+   tmpfmt = convertH323CapToAsteriskCap(pChannel->chanCap->cap);
+
+   if (tmpfmt) {
+    if ((ast_format_cmp(tmpfmt, ast_format_alaw) == AST_FORMAT_CMP_EQUAL) ||
+      (ast_format_cmp(tmpfmt, ast_format_ulaw) == AST_FORMAT_CMP_EQUAL)) {
+       ooh323_set_write_format(call, tmpfmt, ((OOCapParams *)(pChannel->chanCap->params))->txframes);
+    } else if (ast_format_cmp(tmpfmt, ast_format_g729) == AST_FORMAT_CMP_EQUAL) {
+        ooh323_set_write_format(call, tmpfmt, ((OOCapParams *)(pChannel->chanCap->params))->txframes*10);
+  } else {
+       ooh323_set_write_format(call, tmpfmt, 0);
       }
    }else{
       ast_log(LOG_ERROR, "Invalid capability type for receive channel %s\n",
@@ -693,41 +661,32 @@ int ooh323c_stop_transmit_datachannel(ooCallData *call, ooLogicalChannel *pChann
    return 1;
 }
 
-struct ast_format *convertH323CapToAsteriskCap(int cap, struct ast_format *result)
+struct ast_format *convertH323CapToAsteriskCap(int cap)
 {
-   ast_format_clear(result);
    switch(cap)
    {
       case OO_G711ULAW64K:
-         return ast_format_set(result, AST_FORMAT_ULAW, 0);
+         return ast_format_ulaw;
       case OO_G711ALAW64K:
-         return ast_format_set(result, AST_FORMAT_ALAW, 0);
+         return ast_format_alaw;
       case OO_GSMFULLRATE:
-         return ast_format_set(result, AST_FORMAT_GSM, 0);
-
-#ifdef AST_FORMAT_AMRNB
-      case OO_AMRNB:
-         return ast_format_set(result, AST_FORMAT_AMRNB, 0);
-#endif
-#ifdef AST_FORMAT_SPEEX
+         return ast_format_gsm;
       case OO_SPEEX:
-         return ast_format_set(result, AST_FORMAT_SPEEX, 0);
-#endif
-
+         return ast_format_speex;
       case OO_G729:
-         return ast_format_set(result, AST_FORMAT_G729A, 0);
+         return ast_format_g729;
       case OO_G729A:
-         return ast_format_set(result, AST_FORMAT_G729A, 0);
+         return ast_format_g729;
       case OO_G729B:
-         return ast_format_set(result, AST_FORMAT_G729A, 0);
+         return ast_format_g729;
       case OO_G7231:
-         return ast_format_set(result, AST_FORMAT_G723_1, 0);
+         return ast_format_g723;
       case OO_G726:
-         return ast_format_set(result, AST_FORMAT_G726, 0);
+         return ast_format_g726;
       case OO_G726AAL2:
-         return ast_format_set(result, AST_FORMAT_G726_AAL2, 0);
+         return ast_format_g726_aal2;
       case OO_H263VIDEO:
-         return ast_format_set(result, AST_FORMAT_H263, 0);
+         return ast_format_h263;
       default:
          ast_debug(1, "Cap %d is not supported by driver yet\n", cap);
          return NULL;