Module: sems
Branch: master
Commit: 3a6ff9a573035a0dddaf3e2516ef1b64c39e497b
URL:    
http://git.sip-router.org/cgi-bin/gitweb.cgi/sems/?a=commit;h=3a6ff9a573035a0dddaf3e2516ef1b64c39e497b

Author: Raphael Coeffic <[email protected]>
Committer: Raphael Coeffic <[email protected]>
Date:   Wed Sep 21 16:25:56 2011 +0200

rtmp: added implicit registration.

---

 apps/rtmp/Rtmp.cpp           |   53 +++++++++++++++++++++++++++++------------
 apps/rtmp/Rtmp.h             |   18 ++++++++++----
 apps/rtmp/RtmpConnection.cpp |   48 ++++++++++++++++++++++++++++++++++---
 apps/rtmp/RtmpConnection.h   |   15 +++++++++++-
 4 files changed, 108 insertions(+), 26 deletions(-)

diff --git a/apps/rtmp/Rtmp.cpp b/apps/rtmp/Rtmp.cpp
index 144fc99..c7538bc 100644
--- a/apps/rtmp/Rtmp.cpp
+++ b/apps/rtmp/Rtmp.cpp
@@ -29,13 +29,16 @@
 #include "RtmpSession.h"
 #include "RtmpConnection.h"
 #include "AmConfigReader.h"
+#include "AmEventDispatcher.h"
+#include "ampi/SIPRegistrarClientAPI.h"
 
 RtmpConfig::RtmpConfig()
-  : FromName("RTMP Gateway"),
+  : ListenAddress("0.0.0.0"),
+    ListenPort(DEFAULT_RTMP_PORT),
+    FromName("RTMP Gateway"),
     FromDomain(),
     AllowExternalRegister(false),
-    ListenAddress("0.0.0.0"),
-    ListenPort(DEFAULT_RTMP_PORT)
+    ImplicitRegistrar()
 {
 }
 
@@ -45,7 +48,8 @@ extern "C" void* FACTORY_SESSION_EXPORT()
 }
 
 RtmpFactory::RtmpFactory()
-  : AmSessionFactory(MOD_NAME)
+  : AmSessionFactory(MOD_NAME),
+    di_reg_client(NULL)
 {
 }
 
@@ -62,6 +66,20 @@ int RtmpFactory::onLoad()
   }
   else {
 
+    if(cfg_file.hasParameter("listen_address")){
+      cfg.ListenAddress = fixIface2IP(cfg_file.getParameter("listen_address"));
+    }
+
+    if(cfg_file.hasParameter("listen_port")){
+      string listen_port_str = cfg_file.getParameter("listen_port");
+      if(sscanf(listen_port_str.c_str(),"%u",
+               &(cfg.ListenPort)) != 1){
+       ERROR("listen_port: invalid RTMP port specified (%s), using default\n",
+             listen_port_str.c_str());
+       cfg.ListenPort = DEFAULT_RTMP_PORT;
+      }
+    }
+
     if(cfg_file.hasParameter("from_name")){
       cfg.FromName = cfg_file.getParameter("from_name");
     }
@@ -75,18 +93,8 @@ int RtmpFactory::onLoad()
        cfg_file.getParameter("allow_external_register") == string("yes");
     }
 
-    if(cfg_file.hasParameter("listen_address")){
-      cfg.ListenAddress = cfg_file.getParameter("listen_address");
-    }
-
-    if(cfg_file.hasParameter("listen_port")){
-      string listen_port_str = cfg_file.getParameter("listen_port");
-      if(sscanf(listen_port_str.c_str(),"%u",
-               &(cfg.ListenPort)) != 1){
-       ERROR("listen_port: invalid RTMP port specified (%s), using default\n",
-             listen_port_str.c_str());
-       cfg.ListenPort = DEFAULT_RTMP_PORT;
-      }
+    if(cfg_file.hasParameter("implicit_registrar")){
+      cfg.ImplicitRegistrar = cfg_file.getParameter("implicit_registrar");
     }
   }
 
@@ -100,6 +108,19 @@ int RtmpFactory::onLoad()
   }
   rtmp_server->start();
   
+  AmDynInvokeFactory* di_reg_client_f = AmPlugIn::instance()->
+    getFactory4Di("registrar_client");
+  if(di_reg_client_f)
+    di_reg_client = di_reg_client_f->getInstance();
+
+  if(di_reg_client) {
+    // start the event processing
+    AmEventDispatcher::instance()->addEventQueue(FACTORY_Q_NAME,this);
+    start(); 
+  }
+  else {
+    INFO("'registrar_client' not found: registration disabled.\n");
+  }
 
   return 0;
 }
diff --git a/apps/rtmp/Rtmp.h b/apps/rtmp/Rtmp.h
index aa53b00..c9cca23 100644
--- a/apps/rtmp/Rtmp.h
+++ b/apps/rtmp/Rtmp.h
@@ -39,11 +39,17 @@ class RtmpConnection;
 
 struct RtmpConfig
 {
+  // RTMP server params
+  string ListenAddress;
+  unsigned int ListenPort;
+
+  // Outbound call params
   string FromName;
   string FromDomain;
+
+  // Registration related params
   bool   AllowExternalRegister;
-  string ListenAddress;
-  unsigned int ListenPort;
+  string ImplicitRegistrar;
 
   RtmpConfig();
 };
@@ -59,10 +65,11 @@ class RtmpFactory
   // to enable inbound calls to RTMP clients
   map<string,RtmpConnection*> connections;
   AmMutex                     m_connections;
+  
+  // registrar_client instance pointer
+  AmDynInvoke* di_reg_client;
 
 protected:
-  // @see AmEventProcessingThread
-  void onEvent(AmEvent* ev);
 
 public:
   RtmpFactory();
@@ -75,7 +82,8 @@ public:
   AmSession* onInvite(const AmSipRequest& req, const string& app_name,
                      const map<string,string>& app_params);
 
-  const RtmpConfig& getConfig() { return cfg; }
+  const RtmpConfig* getConfig() { return &cfg; }
+  AmDynInvoke* getRegClient() { return di_reg_client; }
   
   int addConnection(const string& ident, RtmpConnection*);
   void removeConnection(const string& ident);
diff --git a/apps/rtmp/RtmpConnection.cpp b/apps/rtmp/RtmpConnection.cpp
index 13cbd51..4d3465a 100644
--- a/apps/rtmp/RtmpConnection.cpp
+++ b/apps/rtmp/RtmpConnection.cpp
@@ -116,12 +116,16 @@ RtmpConnection::RtmpConnection(int fd)
     sender(NULL),
     session(NULL),
     registered(false),
-    rtmp_cfg(RtmpFactory_impl::instance()->getConfig())
+    di_reg_client(NULL),
+    rtmp_cfg(NULL)
 {
   memset(&rtmp,0,sizeof(RTMP));
   RTMP_Init(&rtmp);
   rtmp.m_sb.sb_socket = fd;
+
   ident = AmSession::getNewId();
+  di_reg_client = RtmpFactory_impl::instance()->getRegClient();
+  rtmp_cfg = RtmpFactory_impl::instance()->getConfig();
 }
 
 RtmpConnection::~RtmpConnection()
@@ -181,6 +185,7 @@ void RtmpConnection::run()
 
   if(registered){
     RtmpFactory_impl::instance()->removeConnection(ident);
+    removeRegistration();
     registered = false;
   }
   detachSession();
@@ -507,6 +512,12 @@ RtmpConnection::invoke(RTMPPacket *packet, unsigned int 
offset)
        registered = true;
        DBG("RTMP connection registered (ident='%s')\n",ident.c_str());
        SendRegisterResult(txn,ident.c_str());
+       
+       if(di_reg_client &&
+          !rtmp_cfg->ImplicitRegistrar.empty()) {
+         createRegistration(rtmp_cfg->ImplicitRegistrar,
+                            ident,rtmp_cfg->FromName);
+       }
       }
 
     }
@@ -1103,11 +1114,11 @@ RtmpSession* RtmpConnection::startSession(const char* 
uri)
   dialout_dlg.remote_party = "<" + string(uri) + ">";
   dialout_dlg.remote_uri   = uri;
 
-  dialout_dlg.local_party  = "\"" + rtmp_cfg.FromName + "\" "
+  dialout_dlg.local_party  = "\"" + rtmp_cfg->FromName + "\" "
     "<sip:" + ident + "@";
 
-  if(!rtmp_cfg.FromDomain.empty()){
-    dialout_dlg.local_party += rtmp_cfg.FromDomain;
+  if(!rtmp_cfg->FromDomain.empty()){
+    dialout_dlg.local_party += rtmp_cfg->FromDomain;
   }
   else {
     int out_if = dialout_dlg.getOutboundIf();
@@ -1165,6 +1176,35 @@ void RtmpConnection::disconnectSession()
   m_session.unlock();
 }
 
+void RtmpConnection::createRegistration(const string& domain,
+                                       const string& user,
+                                       const string& display_name,
+                                       const string& auth_user,
+                                       const string& passwd)
+{
+  if(!di_reg_client) return;
+
+  AmArg di_args,ret;
+  di_args.push(domain.c_str());
+  di_args.push(user.c_str());
+  di_args.push(display_name.c_str());  // display name
+  di_args.push(auth_user.c_str());     // auth_user
+  di_args.push(passwd.c_str());        // pwd
+  di_args.push(FACTORY_Q_NAME);
+  
+  di_reg_client->invoke("createRegistration", di_args, ret);
+  reg_handle = ret.get(0).asCStr();
+}
+
+void RtmpConnection::removeRegistration()
+{
+  if(!di_reg_client || reg_handle.empty()) return;
+  AmArg di_args,ret;
+  di_args.push(reg_handle.c_str());
+  di_reg_client->invoke("removeRegistration", di_args, ret);
+  reg_handle.clear();
+}
+
 void RtmpConnection::stopStream(unsigned int stream_id)
 {
   if(stream_id == play_stream_id){
diff --git a/apps/rtmp/RtmpConnection.h b/apps/rtmp/RtmpConnection.h
index 4533761..b6d823b 100644
--- a/apps/rtmp/RtmpConnection.h
+++ b/apps/rtmp/RtmpConnection.h
@@ -91,7 +91,12 @@ class RtmpConnection
   // Is the connection registered in RtmpFactory?
   bool         registered;
 
-  const RtmpConfig& rtmp_cfg;
+  // registrar_client instance
+  AmDynInvoke* di_reg_client;
+  // handle from registrar_client
+  string       reg_handle;
+
+  const RtmpConfig* rtmp_cfg;
 
 public:
   RtmpConnection(int fd);
@@ -117,6 +122,14 @@ private:
   void disconnectSession();
   void detachSession();
 
+  void createRegistration(const string& domain,
+                         const string& user,
+                         const string& display_name,
+                         const string& auth_user = "",
+                         const string& passwd = "");
+  void removeRegistration();
+  
+
   void stopStream(unsigned int stream_id);
 
   int processPacket(RTMPPacket *packet);

_______________________________________________
Semsdev mailing list
[email protected]
http://lists.iptel.org/mailman/listinfo/semsdev

Reply via email to