From f520621508d13311cde0d7d91df458603a1b2d63 Mon Sep 17 00:00:00 2001 From: Stefan Sayer Date: Thu, 15 Apr 2010 13:01:00 +0000 Subject: [PATCH 12/12] create RTP stream (AmSession::rtp_str) only if really needed replace AmSession::rtp_str with AmSession::RTPStream() in your app. for example for pure signaling B2B calls, no RTP stream instance is created, which saves a lot of memory (especially because of the RTP receive buffer) ref r30371 r30372 git-svn-id: http://svn.berlios.de/svnroot/repos/sems/trunk@1784 8eb893ce-cfd4-0310-b710-fb5ebe64c474 --- apps/auth_b2b/AuthB2B.cpp | 12 +++--- apps/call_timer/CallTimer.cpp | 4 +- apps/callback/CallBack.cpp | 4 +- apps/conference/Conference.cpp | 6 +- apps/dsm/mods/mod_conference/ModConference.cpp | 6 +- apps/examples/b2b_connect/b2b_connect.cpp | 4 +- .../pinauthconference/PinAuthConference.cpp | 2 +- apps/examples/serviceline/ServiceLine.cpp | 2 +- .../simple_conference/SimpleConference.cpp | 2 +- apps/gateway/GWSession.cpp | 2 +- apps/sw_prepaid_sip/SWPrepaidSIP.cpp | 4 +- apps/webconference/WebConferenceDialog.cpp | 4 +- core/AmMediaProcessor.cpp | 18 ++++---- core/AmSession.cpp | 42 ++++++++++--------- core/AmSession.h | 19 +++++++-- core/plug-in/echo/Echo.cpp | 6 +- 16 files changed, 75 insertions(+), 62 deletions(-) diff --git a/apps/auth_b2b/AuthB2B.cpp b/apps/auth_b2b/AuthB2B.cpp index e415f9f..3cdd6ab 100644 --- a/apps/auth_b2b/AuthB2B.cpp +++ b/apps/auth_b2b/AuthB2B.cpp @@ -109,8 +109,8 @@ void AuthB2BDialog::onInvite(const AmSipRequest& req) // -> only in SessionFactory::onInvite they do. todo: move the logic to // session factory - setReceiving(false); - AmMediaProcessor::instance()->removeSession(this); + // this will prevent us from being added to media processor + setInOut(NULL,NULL); if (AuthB2BFactory::user.empty()) { string app_param = getHeader(req.hdrs, PARAM_HDR); @@ -131,10 +131,10 @@ void AuthB2BDialog::onInvite(const AmSipRequest& req) from = "sip:"+user+"@"+domain; to = "sip:"+req.user+"@"+domain; -// DBG("-----------------------------------------------------------------\n"); -// DBG("domain = %s, user = %s, pwd = %s, from = %s, to = %s;", -// domain.c_str(), user.c_str(), password.c_str(), from.c_str(), to.c_str()); -// DBG("-----------------------------------------------------------------\n"); + // DBG("-----------------------------------------------------------------\n"); + // DBG("domain = %s, user = %s, pwd = %s, from = %s, to = %s;", + // domain.c_str(), user.c_str(), password.c_str(), from.c_str(), to.c_str()); + // DBG("-----------------------------------------------------------------\n"); m_state = BB_Dialing; diff --git a/apps/call_timer/CallTimer.cpp b/apps/call_timer/CallTimer.cpp index 79a5bfe..24d6cd9 100644 --- a/apps/call_timer/CallTimer.cpp +++ b/apps/call_timer/CallTimer.cpp @@ -137,8 +137,8 @@ void CallTimerDialog::onInvite(const AmSipRequest& req) return; } - setReceiving(false); - AmMediaProcessor::instance()->removeSession(this); + // this will prevent us from being added to media processor + setInOut(NULL,NULL); m_state = BB_Dialing; diff --git a/apps/callback/CallBack.cpp b/apps/callback/CallBack.cpp index c03f01d..988a61b 100644 --- a/apps/callback/CallBack.cpp +++ b/apps/callback/CallBack.cpp @@ -228,7 +228,7 @@ CallBackDialog::CallBackDialog(AmPromptCollection& prompts, state(CBNone) { // set configured playout type - rtp_str.setPlayoutType(CallBackFactory::m_PlayoutType); + RTPStream()->setPlayoutType(CallBackFactory::m_PlayoutType); } CallBackDialog::~CallBackDialog() @@ -321,7 +321,7 @@ CallBackCalleeDialog::CallBackCalleeDialog(const string& other_tag, : AmB2ABCalleeSession(other_tag, connector), cred(cred) { // set configured playout type - rtp_str.setPlayoutType(CallBackFactory::m_PlayoutType); + RTPStream()->setPlayoutType(CallBackFactory::m_PlayoutType); setDtmfDetectionEnabled(false); } diff --git a/apps/conference/Conference.cpp b/apps/conference/Conference.cpp index 06ff807..4aad409 100644 --- a/apps/conference/Conference.cpp +++ b/apps/conference/Conference.cpp @@ -349,7 +349,7 @@ ConferenceDialog::ConferenceDialog(const string& conf_id, allow_dialout(false) { dialedout = this->dialout_channel.get() != 0; - rtp_str.setPlayoutType(ConferenceFactory::m_PlayoutType); + RTPStream()->setPlayoutType(ConferenceFactory::m_PlayoutType); #ifdef WITH_SAS_TTS tts_voice = register_cmu_us_kal(); #endif @@ -763,7 +763,7 @@ void ConferenceDialog::createDialoutParticipant(const string& uri_user) dialout_dlg.remote_uri = uri; string body; - int local_port = dialout_session->rtp_str.getLocalPort(); + int local_port = dialout_session->RTPStream()->getLocalPort(); dialout_session->sdp.genRequest(AmConfig::LocalIP,local_port,body); if (extra_headers.length() == 0) { @@ -855,7 +855,7 @@ void ConferenceDialog::onSipRequest(const AmSipRequest& req) DBG("ConferenceDialog::onSipRequest: remote_tag = %s\n",dlg.remote_tag.c_str()); string body; - int local_port = rtp_str.getLocalPort(); + int local_port = RTPStream()->getLocalPort(); sdp.genRequest(AmConfig::LocalIP,local_port,body); dlg.sendRequest("INVITE","application/sdp",body,""); diff --git a/apps/dsm/mods/mod_conference/ModConference.cpp b/apps/dsm/mods/mod_conference/ModConference.cpp index 24f7306..791cdbc 100644 --- a/apps/dsm/mods/mod_conference/ModConference.cpp +++ b/apps/dsm/mods/mod_conference/ModConference.cpp @@ -195,9 +195,9 @@ EXEC_ACTION_START(ConfRejoinAction) { EXEC_ACTION_START(ConfSetPlayoutTypeAction) { string playout_type = resolveVars(arg, sess, sc_sess, event_params); if (playout_type == "adaptive") - sess->rtp_str.setPlayoutType(ADAPTIVE_PLAYOUT); + sess->RTPStream()->setPlayoutType(ADAPTIVE_PLAYOUT); else if (playout_type == "jb") - sess->rtp_str.setPlayoutType(JB_PLAYOUT); + sess->RTPStream()->setPlayoutType(JB_PLAYOUT); else - sess->rtp_str.setPlayoutType(SIMPLE_PLAYOUT); + sess->RTPStream()->setPlayoutType(SIMPLE_PLAYOUT); } EXEC_ACTION_END; diff --git a/apps/examples/b2b_connect/b2b_connect.cpp b/apps/examples/b2b_connect/b2b_connect.cpp index 7c35c52..6c7ccfa 100644 --- a/apps/examples/b2b_connect/b2b_connect.cpp +++ b/apps/examples/b2b_connect/b2b_connect.cpp @@ -93,7 +93,7 @@ b2b_connectDialog::b2b_connectDialog() // AmDynInvoke* user_timer) AmB2ABCallerSession() { - rtp_str.setPlayoutType(ADAPTIVE_PLAYOUT); + RTPStream()->setPlayoutType(ADAPTIVE_PLAYOUT); } b2b_connectDialog::~b2b_connectDialog() @@ -274,7 +274,7 @@ b2b_connectCalleeSession::b2b_connectCalleeSession(const string& other_tag, : credentials("", user, pwd), // domain (realm) is unused in credentials AmB2ABCalleeSession(other_tag, connector) { - rtp_str.setPlayoutType(ADAPTIVE_PLAYOUT); + RTPStream()->setPlayoutType(ADAPTIVE_PLAYOUT); setDtmfDetectionEnabled(false); } diff --git a/apps/examples/pinauthconference/PinAuthConference.cpp b/apps/examples/pinauthconference/PinAuthConference.cpp index 521df25..d75e10d 100644 --- a/apps/examples/pinauthconference/PinAuthConference.cpp +++ b/apps/examples/pinauthconference/PinAuthConference.cpp @@ -101,7 +101,7 @@ PinAuthConferenceDialog::PinAuthConferenceDialog(AmPromptCollection& prompts) : play_list(this), separator(this, 0), prompts(prompts), state(None) { // set configured playout type - rtp_str.setPlayoutType(PinAuthConferenceFactory::m_PlayoutType); + RTPStream()->setPlayoutType(PinAuthConferenceFactory::m_PlayoutType); } PinAuthConferenceDialog::~PinAuthConferenceDialog() diff --git a/apps/examples/serviceline/ServiceLine.cpp b/apps/examples/serviceline/ServiceLine.cpp index a8c9b9e..427543d 100644 --- a/apps/examples/serviceline/ServiceLine.cpp +++ b/apps/examples/serviceline/ServiceLine.cpp @@ -204,7 +204,7 @@ ServiceLineCalleeDialog::ServiceLineCalleeDialog(const string& other_tag, ServiceLineFactory::GWAuthuser, ServiceLineFactory::GWAuthpwd) { - rtp_str.setPlayoutType(ADAPTIVE_PLAYOUT); + RTPStream()->setPlayoutType(ADAPTIVE_PLAYOUT); setDtmfDetectionEnabled(false); } diff --git a/apps/examples/simple_conference/SimpleConference.cpp b/apps/examples/simple_conference/SimpleConference.cpp index ccc0930..9a5c9f7 100644 --- a/apps/examples/simple_conference/SimpleConference.cpp +++ b/apps/examples/simple_conference/SimpleConference.cpp @@ -60,7 +60,7 @@ SimpleConferenceDialog::SimpleConferenceDialog() : play_list(this) { // use adaptive playout - its the best method around - rtp_str.setPlayoutType(ADAPTIVE_PLAYOUT); + RTPStream()->setPlayoutType(ADAPTIVE_PLAYOUT); } SimpleConferenceDialog::~SimpleConferenceDialog() diff --git a/apps/gateway/GWSession.cpp b/apps/gateway/GWSession.cpp index f7c3b00..e946839 100644 --- a/apps/gateway/GWSession.cpp +++ b/apps/gateway/GWSession.cpp @@ -26,7 +26,7 @@ void GWSession::setOtherLeg(AmAudio *otherleg) { } void GWSession::onInvite(const AmSipRequest& req) { DBG("GWSession::onInvite\n"); -// rtp_str.setMonitorRTPTimeout(false); +// RTPStream()->setMonitorRTPTimeout(false); invite_req=req; return; } diff --git a/apps/sw_prepaid_sip/SWPrepaidSIP.cpp b/apps/sw_prepaid_sip/SWPrepaidSIP.cpp index d2d7fb9..339d1d2 100644 --- a/apps/sw_prepaid_sip/SWPrepaidSIP.cpp +++ b/apps/sw_prepaid_sip/SWPrepaidSIP.cpp @@ -118,8 +118,8 @@ void SWPrepaidSIPDialog::onInvite(const AmSipRequest& req) // TODO: errors thrown as exception don't seem to trigger a reply? - setReceiving(false); - AmMediaProcessor::instance()->removeSession(this); + // this will prevent us from being added to media processor + setInOut(NULL,NULL); m_uuid = getHeader(req.hdrs,"P-Caller-Uuid"); if(!m_uuid.length()) { diff --git a/apps/webconference/WebConferenceDialog.cpp b/apps/webconference/WebConferenceDialog.cpp index d41d191..d95ac6f 100644 --- a/apps/webconference/WebConferenceDialog.cpp +++ b/apps/webconference/WebConferenceDialog.cpp @@ -40,7 +40,7 @@ WebConferenceDialog::WebConferenceDialog(AmPromptCollection& prompts, is_dialout = (cred != NULL); accept_early_session = is_dialout; // set configured playout type - rtp_str.setPlayoutType(WebConferenceFactory::m_PlayoutType); + RTPStream()->setPlayoutType(WebConferenceFactory::m_PlayoutType); } WebConferenceDialog::WebConferenceDialog(AmPromptCollection& prompts, @@ -54,7 +54,7 @@ WebConferenceDialog::WebConferenceDialog(AmPromptCollection& prompts, DBG("set conf_id to %s\n", conf_id.c_str()); is_dialout = false; // set configured playout type - rtp_str.setPlayoutType(WebConferenceFactory::m_PlayoutType); + RTPStream()->setPlayoutType(WebConferenceFactory::m_PlayoutType); } WebConferenceDialog::~WebConferenceDialog() diff --git a/core/AmMediaProcessor.cpp b/core/AmMediaProcessor.cpp index c59b930..312d5c8 100644 --- a/core/AmMediaProcessor.cpp +++ b/core/AmMediaProcessor.cpp @@ -284,10 +284,10 @@ void AmMediaProcessorThread::processAudio(unsigned int ts) AmSession* s = (*it); // todo: get frame size/checkInterval from local audio if local in+out (?) - unsigned int f_size = s->rtp_str.getFrameSize(); + unsigned int f_size = s->RTPStream()->getFrameSize(); // complete frame time reached? - if (s->rtp_str.checkInterval(ts, f_size)) { + if (s->RTPStream()->checkInterval(ts, f_size)) { s->lockAudio(); int got_audio = -1; @@ -295,8 +295,8 @@ void AmMediaProcessorThread::processAudio(unsigned int ts) // get/receive audio if (!s->getAudioLocal(AM_AUDIO_IN)) { // input is not local - receive from rtp stream - if (s->rtp_str.receiving || s->rtp_str.getPassiveMode()) { - int ret = s->rtp_str.receive(ts); + if (s->RTPStream()->receiving || s->RTPStream()->getPassiveMode()) { + int ret = s->RTPStream()->receive(ts); if(ret < 0){ switch(ret){ @@ -317,7 +317,7 @@ void AmMediaProcessorThread::processAudio(unsigned int ts) break; } } else { - got_audio = s->rtp_str.get(ts,buffer,f_size); + got_audio = s->RTPStream()->get(ts,buffer,f_size); if (s->isDtmfDetectionEnabled() && got_audio > 0) s->putDtmfAudio(buffer, got_audio, ts); @@ -355,9 +355,9 @@ void AmMediaProcessorThread::processAudio(unsigned int ts) s->lockAudio(); AmAudio* output = s->getOutput(); - if(output && s->rtp_str.sendIntReached()){ + if(output && s->RTPStream()->sendIntReached()){ - int size = output->get(ts,buffer,s->rtp_str.getFrameSize()); + int size = output->get(ts,buffer,s->RTPStream()->getFrameSize()); if(size <= 0){ DBG("output->get() returned: %i\n",size); postRequest(new SchedRequest(AmMediaProcessor::ClearSession,s)); @@ -365,8 +365,8 @@ void AmMediaProcessorThread::processAudio(unsigned int ts) else { if (!s->getAudioLocal(AM_AUDIO_OUT)) { // audio should go to RTP - if(!s->rtp_str.mute){ - if(s->rtp_str.put(ts,buffer,size)<0) + if(!s->RTPStream()->mute){ + if(s->RTPStream()->put(ts,buffer,size)<0) postRequest(new SchedRequest(AmMediaProcessor::ClearSession,s)); } } else { diff --git a/core/AmSession.cpp b/core/AmSession.cpp index e25caaf..38d23e3 100644 --- a/core/AmSession.cpp +++ b/core/AmSession.cpp @@ -60,7 +60,7 @@ AmSession::AmSession() : AmEventQueue(this), // AmDialogState(), dlg(this), detached(true), - sess_stopped(false),rtp_str(this),negotiate_onreply(false), + sess_stopped(false),negotiate_onreply(false), input(0), output(0), local_input(0), local_output(0), m_dtmfDetector(this), m_dtmfEventQueue(&m_dtmfDetector), m_dtmfDetectionEnabled(true), @@ -208,7 +208,7 @@ const vector& AmSession::getPayloads() int AmSession::getRPort() { - return rtp_str.getRPort(); + return RTPStream()->getRPort(); } AmPayloadProviderInterface* AmSession::getPayloadProvider() { @@ -262,7 +262,7 @@ void AmSession::negotiate(const string& sdp_body, telephone_event_payload->payload_type); lockAudio(); - rtp_str.setTelephoneEventPT(telephone_event_payload); + RTPStream()->setTelephoneEventPT(telephone_event_payload); unlockAudio(); } else { @@ -280,9 +280,9 @@ void AmSession::negotiate(const string& sdp_body, lockAudio(); try { - rtp_str.setLocalIP(AmConfig::LocalIP); - rtp_str.setPassiveMode(passive_mode); - rtp_str.setRAddr(r_host, r_port); + RTPStream()->setLocalIP(AmConfig::LocalIP); + RTPStream()->setPassiveMode(passive_mode); + RTPStream()->setRAddr(r_host, r_port); } catch (const string& err_str) { unlockAudio(); throw AmSession::Exception(400, err_str); @@ -293,7 +293,9 @@ void AmSession::negotiate(const string& sdp_body, unlockAudio(); if(sdp_reply) - sdp.genResponse(advertisedIP(), rtp_str.getLocalPort(), *sdp_reply, AmConfig::SingleCodecInOK); + sdp.genResponse(advertisedIP(), + RTPStream()->getLocalPort(), + *sdp_reply, AmConfig::SingleCodecInOK); } void AmSession::run() @@ -318,7 +320,7 @@ void AmSession::run() return; } - zrtp_audio = zrtp_attach_stream(zrtp_session, rtp_str.get_ssrc()); + zrtp_audio = zrtp_attach_stream(zrtp_session, RTPStream()->get_ssrc()); zrtp_audio->stream_usr_data = this; if (NULL == zrtp_audio) { @@ -648,7 +650,7 @@ void AmSession::onSipReply(const AmSipReply& reply) case AmSipDialog::Connected: try { - rtp_str.setMonitorRTPTimeout(true); + RTPStream()->setMonitorRTPTimeout(true); acceptAudio(reply.body,reply.hdrs); @@ -681,7 +683,7 @@ void AmSession::onSipReply(const AmSipReply& reply) onRinging(reply); - rtp_str.setMonitorRTPTimeout(false); + RTPStream()->setMonitorRTPTimeout(false); if(input || output || local_input || local_output) AmMediaProcessor::instance()->addSession(this, @@ -697,10 +699,10 @@ void AmSession::onSipReply(const AmSipReply& reply) onEarlySessionStart(reply); - rtp_str.setMonitorRTPTimeout(false); + RTPStream()->setMonitorRTPTimeout(false); // ping the other side to open fw/NAT/symmetric RTP - rtp_str.ping(); + RTPStream()->ping(); if(input || output || local_input || local_output) AmMediaProcessor::instance()->addSession(this, @@ -760,11 +762,11 @@ int AmSession::acceptAudio(const string& body, // enable RTP stream lockAudio(); - rtp_str.init(m_payloads); + RTPStream()->init(m_payloads); unlockAudio(); DBG("Sending Rtp data to %s/%i\n", - rtp_str.getRHost().c_str(),rtp_str.getRPort()); + RTPStream()->getRHost().c_str(),RTPStream()->getRPort()); return 0; } @@ -819,9 +821,9 @@ void AmSession::sendUpdate() void AmSession::sendReinvite(bool updateSDP, const string& headers) { if (updateSDP) { - rtp_str.setLocalIP(AmConfig::LocalIP); + RTPStream()->setLocalIP(AmConfig::LocalIP); string sdp_body; - sdp.genResponse(advertisedIP(), rtp_str.getLocalPort(), sdp_body); + sdp.genResponse(advertisedIP(), RTPStream()->getLocalPort(), sdp_body); dlg.reinvite(headers, "application/sdp", sdp_body); } else { dlg.reinvite(headers, "", ""); @@ -834,19 +836,19 @@ int AmSession::sendInvite(const string& headers) // Set local IP first, so that IP is set when // getLocalPort/setLocalPort may bind. - rtp_str.setLocalIP(AmConfig::LocalIP); + RTPStream()->setLocalIP(AmConfig::LocalIP); // Generate SDP. string sdp_body; - sdp.genRequest(advertisedIP(), rtp_str.getLocalPort(), sdp_body); + sdp.genRequest(advertisedIP(), RTPStream()->getLocalPort(), sdp_body); return dlg.invite(headers, "application/sdp", sdp_body); } void AmSession::setOnHold(bool hold) { lockAudio(); - bool old_hold = rtp_str.getOnHold(); - rtp_str.setOnHold(hold); + bool old_hold = RTPStream()->getOnHold(); + RTPStream()->setOnHold(hold); if (hold != old_hold) sendReinvite(); unlockAudio(); diff --git a/core/AmSession.h b/core/AmSession.h index 6fb4356..74c03bf 100644 --- a/core/AmSession.h +++ b/core/AmSession.h @@ -111,7 +111,9 @@ private: friend class AmMediaProcessorThread; friend class AmSessionContainer; friend class AmSessionFactory; - + + auto_ptr _rtp_str; + protected: AmSdp sdp; @@ -126,7 +128,7 @@ protected: public: - AmRtpAudio rtp_str; + AmRtpAudio* RTPStream(); #ifdef WITH_ZRTP zrtp_conn_ctx_t* zrtp_session; // ZRTP session @@ -253,10 +255,10 @@ public: void clearAudio(); /** setter for rtp_str->mute */ - void setMute(bool mute) { rtp_str.mute = mute; } + void setMute(bool mute) { RTPStream()->mute = mute; } /** setter for rtp_str->receiving */ - void setReceiving(bool receive) { rtp_str.receiving = receive; } + void setReceiving(bool receive) { RTPStream()->receiving = receive; } /** Gets the Session's call ID */ const string& getCallID() const; @@ -477,6 +479,15 @@ public: string advertisedIP(); }; +inline AmRtpAudio* AmSession::RTPStream() { + if (NULL == _rtp_str.get()) { + DBG("creating RTP stream instance for session [%p]\n", + this); + _rtp_str.reset(new AmRtpAudio(this)); + } + return _rtp_str.get(); +} + #endif // Local Variables: diff --git a/core/plug-in/echo/Echo.cpp b/core/plug-in/echo/Echo.cpp index 21a6a29..a90e051 100644 --- a/core/plug-in/echo/Echo.cpp +++ b/core/plug-in/echo/Echo.cpp @@ -104,13 +104,13 @@ EchoDialog::~EchoDialog() void EchoDialog::onSessionStart(const AmSipRequest& req) { - rtp_str.setPlayoutType(playout_type); + RTPStream()->setPlayoutType(playout_type); setInOut(&echo,&echo); } void EchoDialog::onSessionStart(const AmSipReply& req) { - rtp_str.setPlayoutType(playout_type); + RTPStream()->setPlayoutType(playout_type); setInOut(&echo,&echo); } @@ -134,7 +134,7 @@ void EchoDialog::onDtmf(int event, int duration) playout_type = SIMPLE_PLAYOUT; DBG("received *. set playout technique to %s.\n", pt); - rtp_str.setPlayoutType(playout_type); + RTPStream()->setPlayoutType(playout_type); } #endif } -- 1.7.2.1