123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802 |
- /*
- * Asterisk -- An open source telephony toolkit.
- *
- * Copyright (C) 1999 - 2008, Digium, Inc.
- *
- * Joshua Colp <jcolp@digium.com>
- *
- * See http://www.asterisk.org for more information about
- * the Asterisk project. Please do not directly contact
- * any of the maintainers of this project for assistance;
- * the project provides a web site, mailing lists and IRC
- * channels for your use.
- *
- * This program is free software, distributed under the terms of
- * the GNU General Public License Version 2. See the LICENSE file
- * at the top of the source tree.
- */
- /*! \file
- *
- * \brief Pluggable RTP Architecture
- *
- * \author Joshua Colp <jcolp@digium.com>
- */
- /*** MODULEINFO
- <support_level>core</support_level>
- ***/
- /*** DOCUMENTATION
- <managerEvent language="en_US" name="RTCPSent">
- <managerEventInstance class="EVENT_FLAG_REPORTING">
- <synopsis>Raised when an RTCP packet is sent.</synopsis>
- <syntax>
- <channel_snapshot/>
- <parameter name="SSRC">
- <para>The SSRC identifier for our stream</para>
- </parameter>
- <parameter name="PT">
- <para>The type of packet for this RTCP report.</para>
- <enumlist>
- <enum name="200(SR)"/>
- <enum name="201(RR)"/>
- </enumlist>
- </parameter>
- <parameter name="To">
- <para>The address the report is sent to.</para>
- </parameter>
- <parameter name="ReportCount">
- <para>The number of reports that were sent.</para>
- <para>The report count determines the number of ReportX headers in
- the message. The X for each set of report headers will range from 0 to
- <literal>ReportCount - 1</literal>.</para>
- </parameter>
- <parameter name="SentNTP" required="false">
- <para>The time the sender generated the report. Only valid when
- PT is <literal>200(SR)</literal>.</para>
- </parameter>
- <parameter name="SentRTP" required="false">
- <para>The sender's last RTP timestamp. Only valid when PT is
- <literal>200(SR)</literal>.</para>
- </parameter>
- <parameter name="SentPackets" required="false">
- <para>The number of packets the sender has sent. Only valid when PT
- is <literal>200(SR)</literal>.</para>
- </parameter>
- <parameter name="SentOctets" required="false">
- <para>The number of bytes the sender has sent. Only valid when PT is
- <literal>200(SR)</literal>.</para>
- </parameter>
- <parameter name="ReportXSourceSSRC">
- <para>The SSRC for the source of this report block.</para>
- </parameter>
- <parameter name="ReportXFractionLost">
- <para>The fraction of RTP data packets from <literal>ReportXSourceSSRC</literal>
- lost since the previous SR or RR report was sent.</para>
- </parameter>
- <parameter name="ReportXCumulativeLost">
- <para>The total number of RTP data packets from <literal>ReportXSourceSSRC</literal>
- lost since the beginning of reception.</para>
- </parameter>
- <parameter name="ReportXHighestSequence">
- <para>The highest sequence number received in an RTP data packet from
- <literal>ReportXSourceSSRC</literal>.</para>
- </parameter>
- <parameter name="ReportXSequenceNumberCycles">
- <para>The number of sequence number cycles seen for the RTP data
- received from <literal>ReportXSourceSSRC</literal>.</para>
- </parameter>
- <parameter name="ReportXIAJitter">
- <para>An estimate of the statistical variance of the RTP data packet
- interarrival time, measured in timestamp units.</para>
- </parameter>
- <parameter name="ReportXLSR">
- <para>The last SR timestamp received from <literal>ReportXSourceSSRC</literal>.
- If no SR has been received from <literal>ReportXSourceSSRC</literal>,
- then 0.</para>
- </parameter>
- <parameter name="ReportXDLSR">
- <para>The delay, expressed in units of 1/65536 seconds, between
- receiving the last SR packet from <literal>ReportXSourceSSRC</literal>
- and sending this report.</para>
- </parameter>
- </syntax>
- <see-also>
- <ref type="managerEvent">RTCPReceived</ref>
- </see-also>
- </managerEventInstance>
- </managerEvent>
- <managerEvent language="en_US" name="RTCPReceived">
- <managerEventInstance class="EVENT_FLAG_REPORTING">
- <synopsis>Raised when an RTCP packet is received.</synopsis>
- <syntax>
- <channel_snapshot/>
- <parameter name="SSRC">
- <para>The SSRC identifier for the remote system</para>
- </parameter>
- <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='PT'])" />
- <parameter name="From">
- <para>The address the report was received from.</para>
- </parameter>
- <parameter name="RTT">
- <para>Calculated Round-Trip Time in seconds</para>
- </parameter>
- <parameter name="ReportCount">
- <para>The number of reports that were received.</para>
- <para>The report count determines the number of ReportX headers in
- the message. The X for each set of report headers will range from 0 to
- <literal>ReportCount - 1</literal>.</para>
- </parameter>
- <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentNTP'])" />
- <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentRTP'])" />
- <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentPackets'])" />
- <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[@name='SentOctets'])" />
- <xi:include xpointer="xpointer(/docs/managerEvent[@name='RTCPSent']/managerEventInstance/syntax/parameter[contains(@name, 'ReportX')])" />
- </syntax>
- <see-also>
- <ref type="managerEvent">RTCPSent</ref>
- </see-also>
- </managerEventInstance>
- </managerEvent>
- ***/
- #include "asterisk.h"
- ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
- #include <math.h> /* for sqrt, MAX */
- #include <sched.h> /* for sched_yield */
- #include <sys/time.h> /* for timeval */
- #include <time.h> /* for time_t */
- #include "asterisk/_private.h" /* for ast_rtp_engine_init prototype */
- #include "asterisk/astobj2.h" /* for ao2_cleanup, ao2_ref, etc */
- #include "asterisk/channel.h" /* for ast_channel_name, etc */
- #include "asterisk/codec.h" /* for ast_codec_media_type2str, etc */
- #include "asterisk/format.h" /* for ast_format_cmp, etc */
- #include "asterisk/format_cache.h" /* for ast_format_adpcm, etc */
- #include "asterisk/format_cap.h" /* for ast_format_cap_alloc, etc */
- #include "asterisk/json.h" /* for ast_json_ref, etc */
- #include "asterisk/linkedlists.h" /* for ast_rtp_engine::<anonymous>, etc */
- #include "asterisk/lock.h" /* for ast_rwlock_unlock, etc */
- #include "asterisk/logger.h" /* for ast_log, ast_debug, etc */
- #include "asterisk/manager.h"
- #include "asterisk/module.h" /* for ast_module_unref, etc */
- #include "asterisk/netsock2.h" /* for ast_sockaddr_copy, etc */
- #include "asterisk/options.h" /* for ast_option_rtpptdynamic */
- #include "asterisk/pbx.h" /* for pbx_builtin_setvar_helper */
- #include "asterisk/res_srtp.h" /* for ast_srtp_res */
- #include "asterisk/rtp_engine.h" /* for ast_rtp_codecs, etc */
- #include "asterisk/stasis.h" /* for stasis_message_data, etc */
- #include "asterisk/stasis_channels.h" /* for ast_channel_stage_snapshot, etc */
- #include "asterisk/strings.h" /* for ast_str_append, etc */
- #include "asterisk/time.h" /* for ast_tvdiff_ms, ast_tvnow */
- #include "asterisk/translate.h" /* for ast_translate_available_formats */
- #include "asterisk/utils.h" /* for ast_free, ast_strdup, etc */
- #include "asterisk/vector.h" /* for AST_VECTOR_GET, etc */
- struct ast_srtp_res *res_srtp = NULL;
- struct ast_srtp_policy_res *res_srtp_policy = NULL;
- /*! Structure that represents an RTP session (instance) */
- struct ast_rtp_instance {
- /*! Engine that is handling this RTP instance */
- struct ast_rtp_engine *engine;
- /*! Data unique to the RTP engine */
- void *data;
- /*! RTP properties that have been set and their value */
- int properties[AST_RTP_PROPERTY_MAX];
- /*! Address that we are expecting RTP to come in to */
- struct ast_sockaddr local_address;
- /*! The original source address */
- struct ast_sockaddr requested_target_address;
- /*! Address that we are sending RTP to */
- struct ast_sockaddr incoming_source_address;
- /*! Instance that we are bridged to if doing remote or local bridging */
- struct ast_rtp_instance *bridged;
- /*! Payload and packetization information */
- struct ast_rtp_codecs codecs;
- /*! RTP timeout time (negative or zero means disabled, negative value means temporarily disabled) */
- int timeout;
- /*! RTP timeout when on hold (negative or zero means disabled, negative value means temporarily disabled). */
- int holdtimeout;
- /*! RTP keepalive interval */
- int keepalive;
- /*! Glue currently in use */
- struct ast_rtp_glue *glue;
- /*! SRTP info associated with the instance */
- struct ast_srtp *srtp;
- /*! SRTP info dedicated for RTCP associated with the instance */
- struct ast_srtp *rtcp_srtp;
- /*! Channel unique ID */
- char channel_uniqueid[AST_MAX_UNIQUEID];
- /*! Time of last packet sent */
- time_t last_tx;
- /*! Time of last packet received */
- time_t last_rx;
- };
- /*! List of RTP engines that are currently registered */
- static AST_RWLIST_HEAD_STATIC(engines, ast_rtp_engine);
- /*! List of RTP glues */
- static AST_RWLIST_HEAD_STATIC(glues, ast_rtp_glue);
- #define MAX_RTP_MIME_TYPES 128
- /*! The following array defines the MIME Media type (and subtype) for each
- of our codecs, or RTP-specific data type. */
- static struct ast_rtp_mime_type {
- /*! \brief A mapping object between the Asterisk codec and this RTP payload */
- struct ast_rtp_payload_type payload_type;
- /*! \brief The media type */
- char type[16];
- /*! \brief The format type */
- char subtype[64];
- /*! \brief Expected sample rate of the /c subtype */
- unsigned int sample_rate;
- } ast_rtp_mime_types[128]; /* This will Likely not need to grow any time soon. */
- static ast_rwlock_t mime_types_lock;
- static int mime_types_len = 0;
- /*!
- * \brief Mapping between Asterisk codecs and rtp payload types
- *
- * Static (i.e., well-known) RTP payload types for our "AST_FORMAT..."s:
- * also, our own choices for dynamic payload types. This is our master
- * table for transmission
- *
- * See http://www.iana.org/assignments/rtp-parameters for a list of
- * assigned values
- */
- static struct ast_rtp_payload_type *static_RTP_PT[AST_RTP_MAX_PT];
- static ast_rwlock_t static_RTP_PT_lock;
- /*! \brief \ref stasis topic for RTP related messages */
- static struct stasis_topic *rtp_topic;
- /*!
- * \internal
- * \brief Destructor for \c ast_rtp_payload_type
- */
- static void rtp_payload_type_dtor(void *obj)
- {
- struct ast_rtp_payload_type *payload = obj;
- ao2_cleanup(payload->format);
- }
- struct ast_rtp_payload_type *ast_rtp_engine_alloc_payload_type(void)
- {
- struct ast_rtp_payload_type *payload;
- payload = ao2_alloc_options(sizeof(*payload), rtp_payload_type_dtor,
- AO2_ALLOC_OPT_LOCK_NOLOCK);
- return payload;
- }
- int ast_rtp_engine_register2(struct ast_rtp_engine *engine, struct ast_module *module)
- {
- struct ast_rtp_engine *current_engine;
- /* Perform a sanity check on the engine structure to make sure it has the basics */
- if (ast_strlen_zero(engine->name) || !engine->new || !engine->destroy || !engine->write || !engine->read) {
- ast_log(LOG_WARNING, "RTP Engine '%s' failed sanity check so it was not registered.\n", !ast_strlen_zero(engine->name) ? engine->name : "Unknown");
- return -1;
- }
- /* Link owner module to the RTP engine for reference counting purposes */
- engine->mod = module;
- AST_RWLIST_WRLOCK(&engines);
- /* Ensure that no two modules with the same name are registered at the same time */
- AST_RWLIST_TRAVERSE(&engines, current_engine, entry) {
- if (!strcmp(current_engine->name, engine->name)) {
- ast_log(LOG_WARNING, "An RTP engine with the name '%s' has already been registered.\n", engine->name);
- AST_RWLIST_UNLOCK(&engines);
- return -1;
- }
- }
- /* The engine survived our critique. Off to the list it goes to be used */
- AST_RWLIST_INSERT_TAIL(&engines, engine, entry);
- AST_RWLIST_UNLOCK(&engines);
- ast_verb(2, "Registered RTP engine '%s'\n", engine->name);
- return 0;
- }
- int ast_rtp_engine_unregister(struct ast_rtp_engine *engine)
- {
- struct ast_rtp_engine *current_engine = NULL;
- AST_RWLIST_WRLOCK(&engines);
- if ((current_engine = AST_RWLIST_REMOVE(&engines, engine, entry))) {
- ast_verb(2, "Unregistered RTP engine '%s'\n", engine->name);
- }
- AST_RWLIST_UNLOCK(&engines);
- return current_engine ? 0 : -1;
- }
- int ast_rtp_glue_register2(struct ast_rtp_glue *glue, struct ast_module *module)
- {
- struct ast_rtp_glue *current_glue = NULL;
- if (ast_strlen_zero(glue->type)) {
- return -1;
- }
- glue->mod = module;
- AST_RWLIST_WRLOCK(&glues);
- AST_RWLIST_TRAVERSE(&glues, current_glue, entry) {
- if (!strcasecmp(current_glue->type, glue->type)) {
- ast_log(LOG_WARNING, "RTP glue with the name '%s' has already been registered.\n", glue->type);
- AST_RWLIST_UNLOCK(&glues);
- return -1;
- }
- }
- AST_RWLIST_INSERT_TAIL(&glues, glue, entry);
- AST_RWLIST_UNLOCK(&glues);
- ast_verb(2, "Registered RTP glue '%s'\n", glue->type);
- return 0;
- }
- int ast_rtp_glue_unregister(struct ast_rtp_glue *glue)
- {
- struct ast_rtp_glue *current_glue = NULL;
- AST_RWLIST_WRLOCK(&glues);
- if ((current_glue = AST_RWLIST_REMOVE(&glues, glue, entry))) {
- ast_verb(2, "Unregistered RTP glue '%s'\n", glue->type);
- }
- AST_RWLIST_UNLOCK(&glues);
- return current_glue ? 0 : -1;
- }
- static void instance_destructor(void *obj)
- {
- struct ast_rtp_instance *instance = obj;
- /* Pass us off to the engine to destroy */
- if (instance->data) {
- /*
- * Lock in case the RTP engine has other threads that
- * need synchronization with the destruction.
- */
- ao2_lock(instance);
- instance->engine->destroy(instance);
- ao2_unlock(instance);
- }
- if (instance->srtp) {
- res_srtp->destroy(instance->srtp);
- }
- if (instance->rtcp_srtp) {
- res_srtp->destroy(instance->rtcp_srtp);
- }
- ast_rtp_codecs_payloads_destroy(&instance->codecs);
- /* Drop our engine reference */
- ast_module_unref(instance->engine->mod);
- ast_debug(1, "Destroyed RTP instance '%p'\n", instance);
- }
- int ast_rtp_instance_destroy(struct ast_rtp_instance *instance)
- {
- ao2_ref(instance, -1);
- return 0;
- }
- struct ast_rtp_instance *ast_rtp_instance_new(const char *engine_name,
- struct ast_sched_context *sched, const struct ast_sockaddr *sa,
- void *data)
- {
- struct ast_sockaddr address = {{0,}};
- struct ast_rtp_instance *instance = NULL;
- struct ast_rtp_engine *engine = NULL;
- AST_RWLIST_RDLOCK(&engines);
- /* If an engine name was specified try to use it or otherwise use the first one registered */
- if (!ast_strlen_zero(engine_name)) {
- AST_RWLIST_TRAVERSE(&engines, engine, entry) {
- if (!strcmp(engine->name, engine_name)) {
- break;
- }
- }
- } else {
- engine = AST_RWLIST_FIRST(&engines);
- }
- /* If no engine was actually found bail out now */
- if (!engine) {
- ast_log(LOG_ERROR, "No RTP engine was found. Do you have one loaded?\n");
- AST_RWLIST_UNLOCK(&engines);
- return NULL;
- }
- /* Bump up the reference count before we return so the module can not be unloaded */
- ast_module_ref(engine->mod);
- AST_RWLIST_UNLOCK(&engines);
- /* Allocate a new RTP instance */
- if (!(instance = ao2_alloc(sizeof(*instance), instance_destructor))) {
- ast_module_unref(engine->mod);
- return NULL;
- }
- instance->engine = engine;
- ast_sockaddr_copy(&instance->local_address, sa);
- ast_sockaddr_copy(&address, sa);
- if (ast_rtp_codecs_payloads_initialize(&instance->codecs)) {
- ao2_ref(instance, -1);
- return NULL;
- }
- ast_debug(1, "Using engine '%s' for RTP instance '%p'\n", engine->name, instance);
- /*
- * And pass it off to the engine to setup
- *
- * Lock in case the RTP engine has other threads that
- * need synchronization with the construction.
- */
- ao2_lock(instance);
- if (instance->engine->new(instance, sched, &address, data)) {
- ast_debug(1, "Engine '%s' failed to setup RTP instance '%p'\n", engine->name, instance);
- ao2_unlock(instance);
- ao2_ref(instance, -1);
- return NULL;
- }
- ao2_unlock(instance);
- ast_debug(1, "RTP instance '%p' is setup and ready to go\n", instance);
- return instance;
- }
- const char *ast_rtp_instance_get_channel_id(struct ast_rtp_instance *instance)
- {
- return instance->channel_uniqueid;
- }
- void ast_rtp_instance_set_channel_id(struct ast_rtp_instance *instance, const char *uniqueid)
- {
- ast_copy_string(instance->channel_uniqueid, uniqueid, sizeof(instance->channel_uniqueid));
- }
- void ast_rtp_instance_set_data(struct ast_rtp_instance *instance, void *data)
- {
- instance->data = data;
- }
- void *ast_rtp_instance_get_data(struct ast_rtp_instance *instance)
- {
- return instance->data;
- }
- int ast_rtp_instance_write(struct ast_rtp_instance *instance, struct ast_frame *frame)
- {
- int res;
- ao2_lock(instance);
- res = instance->engine->write(instance, frame);
- ao2_unlock(instance);
- return res;
- }
- struct ast_frame *ast_rtp_instance_read(struct ast_rtp_instance *instance, int rtcp)
- {
- struct ast_frame *frame;
- ao2_lock(instance);
- frame = instance->engine->read(instance, rtcp);
- ao2_unlock(instance);
- return frame;
- }
- int ast_rtp_instance_set_local_address(struct ast_rtp_instance *instance,
- const struct ast_sockaddr *address)
- {
- ao2_lock(instance);
- ast_sockaddr_copy(&instance->local_address, address);
- ao2_unlock(instance);
- return 0;
- }
- static void rtp_instance_set_incoming_source_address_nolock(struct ast_rtp_instance *instance,
- const struct ast_sockaddr *address)
- {
- ast_sockaddr_copy(&instance->incoming_source_address, address);
- if (instance->engine->remote_address_set) {
- instance->engine->remote_address_set(instance, &instance->incoming_source_address);
- }
- }
- int ast_rtp_instance_set_incoming_source_address(struct ast_rtp_instance *instance,
- const struct ast_sockaddr *address)
- {
- ao2_lock(instance);
- rtp_instance_set_incoming_source_address_nolock(instance, address);
- ao2_unlock(instance);
- return 0;
- }
- int ast_rtp_instance_set_requested_target_address(struct ast_rtp_instance *instance,
- const struct ast_sockaddr *address)
- {
- ao2_lock(instance);
- ast_sockaddr_copy(&instance->requested_target_address, address);
- rtp_instance_set_incoming_source_address_nolock(instance, address);
- ao2_unlock(instance);
- return 0;
- }
- int ast_rtp_instance_get_and_cmp_local_address(struct ast_rtp_instance *instance,
- struct ast_sockaddr *address)
- {
- ao2_lock(instance);
- if (ast_sockaddr_cmp(address, &instance->local_address) != 0) {
- ast_sockaddr_copy(address, &instance->local_address);
- ao2_unlock(instance);
- return 1;
- }
- ao2_unlock(instance);
- return 0;
- }
- void ast_rtp_instance_get_local_address(struct ast_rtp_instance *instance,
- struct ast_sockaddr *address)
- {
- ao2_lock(instance);
- ast_sockaddr_copy(address, &instance->local_address);
- ao2_unlock(instance);
- }
- int ast_rtp_instance_get_and_cmp_requested_target_address(struct ast_rtp_instance *instance,
- struct ast_sockaddr *address)
- {
- ao2_lock(instance);
- if (ast_sockaddr_cmp(address, &instance->requested_target_address) != 0) {
- ast_sockaddr_copy(address, &instance->requested_target_address);
- ao2_unlock(instance);
- return 1;
- }
- ao2_unlock(instance);
- return 0;
- }
- void ast_rtp_instance_get_incoming_source_address(struct ast_rtp_instance *instance,
- struct ast_sockaddr *address)
- {
- ao2_lock(instance);
- ast_sockaddr_copy(address, &instance->incoming_source_address);
- ao2_unlock(instance);
- }
- void ast_rtp_instance_get_requested_target_address(struct ast_rtp_instance *instance,
- struct ast_sockaddr *address)
- {
- ao2_lock(instance);
- ast_sockaddr_copy(address, &instance->requested_target_address);
- ao2_unlock(instance);
- }
- void ast_rtp_instance_set_extended_prop(struct ast_rtp_instance *instance, int property, void *value)
- {
- if (instance->engine->extended_prop_set) {
- ao2_lock(instance);
- instance->engine->extended_prop_set(instance, property, value);
- ao2_unlock(instance);
- }
- }
- void *ast_rtp_instance_get_extended_prop(struct ast_rtp_instance *instance, int property)
- {
- void *prop;
- if (instance->engine->extended_prop_get) {
- ao2_lock(instance);
- prop = instance->engine->extended_prop_get(instance, property);
- ao2_unlock(instance);
- } else {
- prop = NULL;
- }
- return prop;
- }
- void ast_rtp_instance_set_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property, int value)
- {
- ao2_lock(instance);
- instance->properties[property] = value;
- if (instance->engine->prop_set) {
- instance->engine->prop_set(instance, property, value);
- }
- ao2_unlock(instance);
- }
- int ast_rtp_instance_get_prop(struct ast_rtp_instance *instance, enum ast_rtp_property property)
- {
- int prop;
- ao2_lock(instance);
- prop = instance->properties[property];
- ao2_unlock(instance);
- return prop;
- }
- struct ast_rtp_codecs *ast_rtp_instance_get_codecs(struct ast_rtp_instance *instance)
- {
- return &instance->codecs;
- }
- int ast_rtp_codecs_payloads_initialize(struct ast_rtp_codecs *codecs)
- {
- int res;
- codecs->framing = 0;
- ast_rwlock_init(&codecs->codecs_lock);
- res = AST_VECTOR_INIT(&codecs->payloads, AST_RTP_MAX_PT);
- return res;
- }
- void ast_rtp_codecs_payloads_destroy(struct ast_rtp_codecs *codecs)
- {
- int i;
- for (i = 0; i < AST_VECTOR_SIZE(&codecs->payloads); i++) {
- struct ast_rtp_payload_type *type;
- type = AST_VECTOR_GET(&codecs->payloads, i);
- ao2_t_cleanup(type, "destroying ast_rtp_codec");
- }
- AST_VECTOR_FREE(&codecs->payloads);
- ast_rwlock_destroy(&codecs->codecs_lock);
- }
- void ast_rtp_codecs_payloads_clear(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance)
- {
- ast_rtp_codecs_payloads_destroy(codecs);
- ast_rtp_codecs_payloads_initialize(codecs);
- if (instance && instance->engine && instance->engine->payload_set) {
- int i;
- ao2_lock(instance);
- for (i = 0; i < AST_RTP_MAX_PT; i++) {
- instance->engine->payload_set(instance, i, 0, NULL, 0);
- }
- ao2_unlock(instance);
- }
- }
- void ast_rtp_codecs_payloads_copy(struct ast_rtp_codecs *src, struct ast_rtp_codecs *dest, struct ast_rtp_instance *instance)
- {
- int i;
- ast_rwlock_wrlock(&dest->codecs_lock);
- /* Deadlock avoidance because of held write lock. */
- while (ast_rwlock_tryrdlock(&src->codecs_lock)) {
- ast_rwlock_unlock(&dest->codecs_lock);
- sched_yield();
- ast_rwlock_wrlock(&dest->codecs_lock);
- }
- for (i = 0; i < AST_VECTOR_SIZE(&src->payloads); i++) {
- struct ast_rtp_payload_type *type;
- type = AST_VECTOR_GET(&src->payloads, i);
- if (!type) {
- continue;
- }
- if (i < AST_VECTOR_SIZE(&dest->payloads)) {
- ao2_t_cleanup(AST_VECTOR_GET(&dest->payloads, i), "cleaning up vector element about to be replaced");
- }
- ast_debug(2, "Copying payload %d (%p) from %p to %p\n", i, type, src, dest);
- ao2_bump(type);
- if (AST_VECTOR_REPLACE(&dest->payloads, i, type)) {
- ao2_cleanup(type);
- } else if (instance && instance->engine && instance->engine->payload_set) {
- ao2_lock(instance);
- instance->engine->payload_set(instance, i, type->asterisk_format, type->format, type->rtp_code);
- ao2_unlock(instance);
- }
- }
- dest->framing = src->framing;
- ast_rwlock_unlock(&src->codecs_lock);
- ast_rwlock_unlock(&dest->codecs_lock);
- }
- void ast_rtp_codecs_payloads_set_m_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
- {
- struct ast_rtp_payload_type *new_type;
- if (payload < 0 || payload >= AST_RTP_MAX_PT) {
- return;
- }
- ast_rwlock_rdlock(&static_RTP_PT_lock);
- new_type = ao2_bump(static_RTP_PT[payload]);
- ast_rwlock_unlock(&static_RTP_PT_lock);
- if (!new_type) {
- ast_debug(1, "Don't have a default tx payload type %d format for m type on %p\n",
- payload, codecs);
- return;
- }
- ast_debug(1, "Setting tx payload type %d based on m type on %p\n",
- payload, codecs);
- ast_rwlock_wrlock(&codecs->codecs_lock);
- if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
- ao2_t_cleanup(AST_VECTOR_GET(&codecs->payloads, payload), "cleaning up replaced payload type");
- }
- if (AST_VECTOR_REPLACE(&codecs->payloads, payload, new_type)) {
- ao2_ref(new_type, -1);
- } else if (instance && instance->engine && instance->engine->payload_set) {
- ao2_lock(instance);
- instance->engine->payload_set(instance, payload, new_type->asterisk_format, new_type->format, new_type->rtp_code);
- ao2_unlock(instance);
- }
- ast_rwlock_unlock(&codecs->codecs_lock);
- }
- int ast_rtp_codecs_payloads_set_rtpmap_type_rate(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int pt,
- char *mimetype, char *mimesubtype,
- enum ast_rtp_options options,
- unsigned int sample_rate)
- {
- unsigned int i;
- int found = 0;
- if (pt < 0 || pt >= AST_RTP_MAX_PT) {
- return -1; /* bogus payload type */
- }
- ast_rwlock_rdlock(&mime_types_lock);
- ast_rwlock_wrlock(&codecs->codecs_lock);
- for (i = 0; i < mime_types_len; ++i) {
- const struct ast_rtp_mime_type *t = &ast_rtp_mime_types[i];
- struct ast_rtp_payload_type *new_type;
- if (strcasecmp(mimesubtype, t->subtype)) {
- continue;
- }
- if (strcasecmp(mimetype, t->type)) {
- continue;
- }
- /* if both sample rates have been supplied, and they don't match,
- * then this not a match; if one has not been supplied, then the
- * rates are not compared */
- if (sample_rate && t->sample_rate &&
- (sample_rate != t->sample_rate)) {
- continue;
- }
- found = 1;
- new_type = ast_rtp_engine_alloc_payload_type();
- if (!new_type) {
- continue;
- }
- if (pt < AST_VECTOR_SIZE(&codecs->payloads)) {
- ao2_t_cleanup(AST_VECTOR_GET(&codecs->payloads, pt), "cleaning up replaced payload type");
- }
- new_type->payload = pt;
- new_type->asterisk_format = t->payload_type.asterisk_format;
- new_type->rtp_code = t->payload_type.rtp_code;
- if ((ast_format_cmp(t->payload_type.format, ast_format_g726) == AST_FORMAT_CMP_EQUAL) &&
- t->payload_type.asterisk_format && (options & AST_RTP_OPT_G726_NONSTANDARD)) {
- new_type->format = ast_format_g726_aal2;
- } else {
- new_type->format = t->payload_type.format;
- }
- if (new_type->format) {
- /* SDP parsing automatically increases the reference count */
- new_type->format = ast_format_parse_sdp_fmtp(new_type->format, "");
- }
- if (AST_VECTOR_REPLACE(&codecs->payloads, pt, new_type)) {
- ao2_ref(new_type, -1);
- } else if (instance && instance->engine && instance->engine->payload_set) {
- ao2_lock(instance);
- instance->engine->payload_set(instance, pt, new_type->asterisk_format, new_type->format, new_type->rtp_code);
- ao2_unlock(instance);
- }
- break;
- }
- ast_rwlock_unlock(&codecs->codecs_lock);
- ast_rwlock_unlock(&mime_types_lock);
- return (found ? 0 : -2);
- }
- int ast_rtp_codecs_payloads_set_rtpmap_type(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload, char *mimetype, char *mimesubtype, enum ast_rtp_options options)
- {
- return ast_rtp_codecs_payloads_set_rtpmap_type_rate(codecs, instance, payload, mimetype, mimesubtype, options, 0);
- }
- void ast_rtp_codecs_payloads_unset(struct ast_rtp_codecs *codecs, struct ast_rtp_instance *instance, int payload)
- {
- struct ast_rtp_payload_type *type;
- if (payload < 0 || payload >= AST_RTP_MAX_PT) {
- return;
- }
- ast_debug(2, "Unsetting payload %d on %p\n", payload, codecs);
- ast_rwlock_wrlock(&codecs->codecs_lock);
- if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
- type = AST_VECTOR_GET(&codecs->payloads, payload);
- ao2_cleanup(type);
- AST_VECTOR_REPLACE(&codecs->payloads, payload, NULL);
- }
- if (instance && instance->engine && instance->engine->payload_set) {
- ao2_lock(instance);
- instance->engine->payload_set(instance, payload, 0, NULL, 0);
- ao2_unlock(instance);
- }
- ast_rwlock_unlock(&codecs->codecs_lock);
- }
- enum ast_media_type ast_rtp_codecs_get_stream_type(struct ast_rtp_codecs *codecs)
- {
- enum ast_media_type stream_type = AST_MEDIA_TYPE_UNKNOWN;
- int payload;
- struct ast_rtp_payload_type *type;
- ast_rwlock_rdlock(&codecs->codecs_lock);
- for (payload = 0; payload < AST_VECTOR_SIZE(&codecs->payloads); ++payload) {
- type = AST_VECTOR_GET(&codecs->payloads, payload);
- if (type && type->asterisk_format) {
- stream_type = ast_format_get_type(type->format);
- break;
- }
- }
- ast_rwlock_unlock(&codecs->codecs_lock);
- return stream_type;
- }
- struct ast_rtp_payload_type *ast_rtp_codecs_get_payload(struct ast_rtp_codecs *codecs, int payload)
- {
- struct ast_rtp_payload_type *type = NULL;
- if (payload < 0 || payload >= AST_RTP_MAX_PT) {
- return NULL;
- }
- ast_rwlock_rdlock(&codecs->codecs_lock);
- if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
- type = AST_VECTOR_GET(&codecs->payloads, payload);
- ao2_bump(type);
- }
- ast_rwlock_unlock(&codecs->codecs_lock);
- if (!type) {
- ast_rwlock_rdlock(&static_RTP_PT_lock);
- type = ao2_bump(static_RTP_PT[payload]);
- ast_rwlock_unlock(&static_RTP_PT_lock);
- }
- return type;
- }
- int ast_rtp_codecs_payload_replace_format(struct ast_rtp_codecs *codecs, int payload, struct ast_format *format)
- {
- struct ast_rtp_payload_type *type;
- if (payload < 0 || payload >= AST_RTP_MAX_PT || !format) {
- return -1;
- }
- type = ast_rtp_engine_alloc_payload_type();
- if (!type) {
- return -1;
- }
- ao2_ref(format, +1);
- type->format = format;
- type->asterisk_format = 1;
- type->payload = payload;
- ast_rwlock_wrlock(&codecs->codecs_lock);
- if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
- ao2_cleanup(AST_VECTOR_GET(&codecs->payloads, payload));
- }
- if (AST_VECTOR_REPLACE(&codecs->payloads, payload, type)) {
- ao2_ref(type, -1);
- ast_rwlock_unlock(&codecs->codecs_lock);
- return -1;
- }
- ast_rwlock_unlock(&codecs->codecs_lock);
- return 0;
- }
- struct ast_format *ast_rtp_codecs_get_payload_format(struct ast_rtp_codecs *codecs, int payload)
- {
- struct ast_rtp_payload_type *type;
- struct ast_format *format = NULL;
- if (payload < 0 || payload >= AST_RTP_MAX_PT) {
- return NULL;
- }
- ast_rwlock_rdlock(&codecs->codecs_lock);
- if (payload < AST_VECTOR_SIZE(&codecs->payloads)) {
- type = AST_VECTOR_GET(&codecs->payloads, payload);
- if (type && type->asterisk_format) {
- format = ao2_bump(type->format);
- }
- }
- ast_rwlock_unlock(&codecs->codecs_lock);
- return format;
- }
- void ast_rtp_codecs_set_framing(struct ast_rtp_codecs *codecs, unsigned int framing)
- {
- if (!framing) {
- return;
- }
- ast_rwlock_wrlock(&codecs->codecs_lock);
- codecs->framing = framing;
- ast_rwlock_unlock(&codecs->codecs_lock);
- }
- unsigned int ast_rtp_codecs_get_framing(struct ast_rtp_codecs *codecs)
- {
- unsigned int framing;
- ast_rwlock_rdlock(&codecs->codecs_lock);
- framing = codecs->framing;
- ast_rwlock_unlock(&codecs->codecs_lock);
- return framing;
- }
- void ast_rtp_codecs_payload_formats(struct ast_rtp_codecs *codecs, struct ast_format_cap *astformats, int *nonastformats)
- {
- int i;
- ast_format_cap_remove_by_type(astformats, AST_MEDIA_TYPE_UNKNOWN);
- *nonastformats = 0;
- ast_rwlock_rdlock(&codecs->codecs_lock);
- for (i = 0; i < AST_VECTOR_SIZE(&codecs->payloads); i++) {
- struct ast_rtp_payload_type *type;
- type = AST_VECTOR_GET(&codecs->payloads, i);
- if (!type) {
- continue;
- }
- if (type->asterisk_format) {
- ast_format_cap_append(astformats, type->format, 0);
- } else {
- *nonastformats |= type->rtp_code;
- }
- }
- if (codecs->framing) {
- ast_format_cap_set_framing(astformats, codecs->framing);
- }
- ast_rwlock_unlock(&codecs->codecs_lock);
- }
- int ast_rtp_codecs_payload_code(struct ast_rtp_codecs *codecs, int asterisk_format, const struct ast_format *format, int code)
- {
- struct ast_rtp_payload_type *type;
- int i;
- int payload = -1;
- ast_rwlock_rdlock(&codecs->codecs_lock);
- for (i = 0; i < AST_VECTOR_SIZE(&codecs->payloads); i++) {
- type = AST_VECTOR_GET(&codecs->payloads, i);
- if (!type) {
- continue;
- }
- if ((asterisk_format && format && ast_format_cmp(format, type->format) == AST_FORMAT_CMP_EQUAL)
- || (!asterisk_format && type->rtp_code == code)) {
- payload = i;
- break;
- }
- }
- ast_rwlock_unlock(&codecs->codecs_lock);
- if (payload < 0) {
- ast_rwlock_rdlock(&static_RTP_PT_lock);
- for (i = 0; i < AST_RTP_MAX_PT; i++) {
- if (!static_RTP_PT[i]) {
- continue;
- }
- if (static_RTP_PT[i]->asterisk_format && asterisk_format && format &&
- (ast_format_cmp(format, static_RTP_PT[i]->format) != AST_FORMAT_CMP_NOT_EQUAL)) {
- payload = i;
- break;
- } else if (!static_RTP_PT[i]->asterisk_format && !asterisk_format &&
- (static_RTP_PT[i]->rtp_code == code)) {
- payload = i;
- break;
- }
- }
- ast_rwlock_unlock(&static_RTP_PT_lock);
- }
- return payload;
- }
- int ast_rtp_codecs_find_payload_code(struct ast_rtp_codecs *codecs, int code)
- {
- struct ast_rtp_payload_type *type;
- int res = -1;
- ast_rwlock_rdlock(&codecs->codecs_lock);
- if (code < AST_VECTOR_SIZE(&codecs->payloads)) {
- type = AST_VECTOR_GET(&codecs->payloads, code);
- if (type) {
- res = type->payload;
- }
- }
- ast_rwlock_unlock(&codecs->codecs_lock);
- return res;
- }
- const char *ast_rtp_lookup_mime_subtype2(const int asterisk_format, struct ast_format *format, int code, enum ast_rtp_options options)
- {
- int i;
- const char *res = "";
- ast_rwlock_rdlock(&mime_types_lock);
- for (i = 0; i < mime_types_len; i++) {
- if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
- (ast_format_cmp(format, ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
- if ((ast_format_cmp(format, ast_format_g726_aal2) == AST_FORMAT_CMP_EQUAL) &&
- (options & AST_RTP_OPT_G726_NONSTANDARD)) {
- res = "G726-32";
- break;
- } else {
- res = ast_rtp_mime_types[i].subtype;
- break;
- }
- } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
- ast_rtp_mime_types[i].payload_type.rtp_code == code) {
- res = ast_rtp_mime_types[i].subtype;
- break;
- }
- }
- ast_rwlock_unlock(&mime_types_lock);
- return res;
- }
- unsigned int ast_rtp_lookup_sample_rate2(int asterisk_format, struct ast_format *format, int code)
- {
- unsigned int i;
- unsigned int res = 0;
- ast_rwlock_rdlock(&mime_types_lock);
- for (i = 0; i < mime_types_len; ++i) {
- if (ast_rtp_mime_types[i].payload_type.asterisk_format && asterisk_format && format &&
- (ast_format_cmp(format, ast_rtp_mime_types[i].payload_type.format) != AST_FORMAT_CMP_NOT_EQUAL)) {
- res = ast_rtp_mime_types[i].sample_rate;
- break;
- } else if (!ast_rtp_mime_types[i].payload_type.asterisk_format && !asterisk_format &&
- ast_rtp_mime_types[i].payload_type.rtp_code == code) {
- res = ast_rtp_mime_types[i].sample_rate;
- break;
- }
- }
- ast_rwlock_unlock(&mime_types_lock);
- return res;
- }
- char *ast_rtp_lookup_mime_multiple2(struct ast_str *buf, struct ast_format_cap *ast_format_capability, int rtp_capability, const int asterisk_format, enum ast_rtp_options options)
- {
- int found = 0;
- const char *name;
- if (!buf) {
- return NULL;
- }
- if (asterisk_format) {
- int x;
- struct ast_format *tmp_fmt;
- for (x = 0; x < ast_format_cap_count(ast_format_capability); x++) {
- tmp_fmt = ast_format_cap_get_format(ast_format_capability, x);
- name = ast_rtp_lookup_mime_subtype2(asterisk_format, tmp_fmt, 0, options);
- ao2_ref(tmp_fmt, -1);
- ast_str_append(&buf, 0, "%s|", name);
- found = 1;
- }
- } else {
- int x;
- ast_str_append(&buf, 0, "0x%x (", (unsigned int) rtp_capability);
- for (x = 1; x <= AST_RTP_MAX; x <<= 1) {
- if (rtp_capability & x) {
- name = ast_rtp_lookup_mime_subtype2(asterisk_format, NULL, x, options);
- ast_str_append(&buf, 0, "%s|", name);
- found = 1;
- }
- }
- }
- ast_str_append(&buf, 0, "%s", found ? ")" : "nothing)");
- return ast_str_buffer(buf);
- }
- int ast_rtp_instance_dtmf_begin(struct ast_rtp_instance *instance, char digit)
- {
- int res;
- if (instance->engine->dtmf_begin) {
- ao2_lock(instance);
- res = instance->engine->dtmf_begin(instance, digit);
- ao2_unlock(instance);
- } else {
- res = -1;
- }
- return res;
- }
- int ast_rtp_instance_dtmf_end(struct ast_rtp_instance *instance, char digit)
- {
- int res;
- if (instance->engine->dtmf_end) {
- ao2_lock(instance);
- res = instance->engine->dtmf_end(instance, digit);
- ao2_unlock(instance);
- } else {
- res = -1;
- }
- return res;
- }
- int ast_rtp_instance_dtmf_end_with_duration(struct ast_rtp_instance *instance, char digit, unsigned int duration)
- {
- int res;
- if (instance->engine->dtmf_end_with_duration) {
- ao2_lock(instance);
- res = instance->engine->dtmf_end_with_duration(instance, digit, duration);
- ao2_unlock(instance);
- } else {
- res = -1;
- }
- return res;
- }
- int ast_rtp_instance_dtmf_mode_set(struct ast_rtp_instance *instance, enum ast_rtp_dtmf_mode dtmf_mode)
- {
- int res;
- if (instance->engine->dtmf_mode_set) {
- ao2_lock(instance);
- res = instance->engine->dtmf_mode_set(instance, dtmf_mode);
- ao2_unlock(instance);
- } else {
- res = -1;
- }
- return res;
- }
- enum ast_rtp_dtmf_mode ast_rtp_instance_dtmf_mode_get(struct ast_rtp_instance *instance)
- {
- int res;
- if (instance->engine->dtmf_mode_get) {
- ao2_lock(instance);
- res = instance->engine->dtmf_mode_get(instance);
- ao2_unlock(instance);
- } else {
- res = 0;
- }
- return res;
- }
- void ast_rtp_instance_update_source(struct ast_rtp_instance *instance)
- {
- if (instance->engine->update_source) {
- ao2_lock(instance);
- instance->engine->update_source(instance);
- ao2_unlock(instance);
- }
- }
- void ast_rtp_instance_change_source(struct ast_rtp_instance *instance)
- {
- if (instance->engine->change_source) {
- ao2_lock(instance);
- instance->engine->change_source(instance);
- ao2_unlock(instance);
- }
- }
- int ast_rtp_instance_set_qos(struct ast_rtp_instance *instance, int tos, int cos, const char *desc)
- {
- int res;
- if (instance->engine->qos) {
- ao2_lock(instance);
- res = instance->engine->qos(instance, tos, cos, desc);
- ao2_unlock(instance);
- } else {
- res = -1;
- }
- return res;
- }
- void ast_rtp_instance_stop(struct ast_rtp_instance *instance)
- {
- if (instance->engine->stop) {
- ao2_lock(instance);
- instance->engine->stop(instance);
- ao2_unlock(instance);
- }
- }
- int ast_rtp_instance_fd(struct ast_rtp_instance *instance, int rtcp)
- {
- int res;
- if (instance->engine->fd) {
- ao2_lock(instance);
- res = instance->engine->fd(instance, rtcp);
- ao2_unlock(instance);
- } else {
- res = -1;
- }
- return res;
- }
- struct ast_rtp_glue *ast_rtp_instance_get_glue(const char *type)
- {
- struct ast_rtp_glue *glue = NULL;
- AST_RWLIST_RDLOCK(&glues);
- AST_RWLIST_TRAVERSE(&glues, glue, entry) {
- if (!strcasecmp(glue->type, type)) {
- break;
- }
- }
- AST_RWLIST_UNLOCK(&glues);
- return glue;
- }
- /*!
- * \brief Conditionally unref an rtp instance
- */
- static void unref_instance_cond(struct ast_rtp_instance **instance)
- {
- if (*instance) {
- ao2_ref(*instance, -1);
- *instance = NULL;
- }
- }
- struct ast_rtp_instance *ast_rtp_instance_get_bridged(struct ast_rtp_instance *instance)
- {
- struct ast_rtp_instance *bridged;
- ao2_lock(instance);
- bridged = instance->bridged;
- ao2_unlock(instance);
- return bridged;
- }
- void ast_rtp_instance_set_bridged(struct ast_rtp_instance *instance, struct ast_rtp_instance *bridged)
- {
- ao2_lock(instance);
- instance->bridged = bridged;
- ao2_unlock(instance);
- }
- void ast_rtp_instance_early_bridge_make_compatible(struct ast_channel *c_dst, struct ast_channel *c_src)
- {
- struct ast_rtp_instance *instance_dst = NULL, *instance_src = NULL,
- *vinstance_dst = NULL, *vinstance_src = NULL,
- *tinstance_dst = NULL, *tinstance_src = NULL;
- struct ast_rtp_glue *glue_dst, *glue_src;
- enum ast_rtp_glue_result audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
- enum ast_rtp_glue_result audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID, video_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
- struct ast_format_cap *cap_dst = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- struct ast_format_cap *cap_src = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- /* Lock both channels so we can look for the glue that binds them together */
- ast_channel_lock_both(c_dst, c_src);
- if (!cap_src || !cap_dst) {
- goto done;
- }
- /* Grab glue that binds each channel to something using the RTP engine */
- if (!(glue_dst = ast_rtp_instance_get_glue(ast_channel_tech(c_dst)->type)) || !(glue_src = ast_rtp_instance_get_glue(ast_channel_tech(c_src)->type))) {
- ast_debug(1, "Can't find native functions for channel '%s'\n", glue_dst ? ast_channel_name(c_src) : ast_channel_name(c_dst));
- goto done;
- }
- audio_glue_dst_res = glue_dst->get_rtp_info(c_dst, &instance_dst);
- video_glue_dst_res = glue_dst->get_vrtp_info ? glue_dst->get_vrtp_info(c_dst, &vinstance_dst) : AST_RTP_GLUE_RESULT_FORBID;
- audio_glue_src_res = glue_src->get_rtp_info(c_src, &instance_src);
- video_glue_src_res = glue_src->get_vrtp_info ? glue_src->get_vrtp_info(c_src, &vinstance_src) : AST_RTP_GLUE_RESULT_FORBID;
- /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
- if (video_glue_dst_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE)) {
- audio_glue_dst_res = AST_RTP_GLUE_RESULT_FORBID;
- }
- if (video_glue_src_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE)) {
- audio_glue_src_res = AST_RTP_GLUE_RESULT_FORBID;
- }
- if (audio_glue_dst_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue_dst_res == AST_RTP_GLUE_RESULT_FORBID || video_glue_dst_res == AST_RTP_GLUE_RESULT_REMOTE) && glue_dst->get_codec) {
- glue_dst->get_codec(c_dst, cap_dst);
- }
- if (audio_glue_src_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue_src_res == AST_RTP_GLUE_RESULT_FORBID || video_glue_src_res == AST_RTP_GLUE_RESULT_REMOTE) && glue_src->get_codec) {
- glue_src->get_codec(c_src, cap_src);
- }
- /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
- if (audio_glue_dst_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue_src_res != AST_RTP_GLUE_RESULT_REMOTE) {
- goto done;
- }
- /* Make sure we have matching codecs */
- if (!ast_format_cap_iscompatible(cap_dst, cap_src)) {
- goto done;
- }
- ast_rtp_codecs_payloads_copy(&instance_src->codecs, &instance_dst->codecs, instance_dst);
- if (vinstance_dst && vinstance_src) {
- ast_rtp_codecs_payloads_copy(&vinstance_src->codecs, &vinstance_dst->codecs, vinstance_dst);
- }
- if (tinstance_dst && tinstance_src) {
- ast_rtp_codecs_payloads_copy(&tinstance_src->codecs, &tinstance_dst->codecs, tinstance_dst);
- }
- if (glue_dst->update_peer(c_dst, instance_src, vinstance_src, tinstance_src, cap_src, 0)) {
- ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n",
- ast_channel_name(c_dst), ast_channel_name(c_src));
- } else {
- ast_debug(1, "Seeded SDP of '%s' with that of '%s'\n",
- ast_channel_name(c_dst), ast_channel_name(c_src));
- }
- done:
- ast_channel_unlock(c_dst);
- ast_channel_unlock(c_src);
- ao2_cleanup(cap_dst);
- ao2_cleanup(cap_src);
- unref_instance_cond(&instance_dst);
- unref_instance_cond(&instance_src);
- unref_instance_cond(&vinstance_dst);
- unref_instance_cond(&vinstance_src);
- unref_instance_cond(&tinstance_dst);
- unref_instance_cond(&tinstance_src);
- }
- int ast_rtp_instance_early_bridge(struct ast_channel *c0, struct ast_channel *c1)
- {
- struct ast_rtp_instance *instance0 = NULL, *instance1 = NULL,
- *vinstance0 = NULL, *vinstance1 = NULL,
- *tinstance0 = NULL, *tinstance1 = NULL;
- struct ast_rtp_glue *glue0, *glue1;
- enum ast_rtp_glue_result audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID, video_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
- enum ast_rtp_glue_result audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID, video_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
- struct ast_format_cap *cap0 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- struct ast_format_cap *cap1 = ast_format_cap_alloc(AST_FORMAT_CAP_FLAG_DEFAULT);
- /* If there is no second channel just immediately bail out, we are of no use in that scenario */
- if (!c1 || !cap1 || !cap0) {
- ao2_cleanup(cap0);
- ao2_cleanup(cap1);
- return -1;
- }
- /* Lock both channels so we can look for the glue that binds them together */
- ast_channel_lock_both(c0, c1);
- /* Grab glue that binds each channel to something using the RTP engine */
- if (!(glue0 = ast_rtp_instance_get_glue(ast_channel_tech(c0)->type)) || !(glue1 = ast_rtp_instance_get_glue(ast_channel_tech(c1)->type))) {
- ast_log(LOG_WARNING, "Can't find native functions for channel '%s'\n", glue0 ? ast_channel_name(c1) : ast_channel_name(c0));
- goto done;
- }
- audio_glue0_res = glue0->get_rtp_info(c0, &instance0);
- video_glue0_res = glue0->get_vrtp_info ? glue0->get_vrtp_info(c0, &vinstance0) : AST_RTP_GLUE_RESULT_FORBID;
- audio_glue1_res = glue1->get_rtp_info(c1, &instance1);
- video_glue1_res = glue1->get_vrtp_info ? glue1->get_vrtp_info(c1, &vinstance1) : AST_RTP_GLUE_RESULT_FORBID;
- /* If we are carrying video, and both sides are not going to remotely bridge... fail the native bridge */
- if (video_glue0_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue0_res != AST_RTP_GLUE_RESULT_REMOTE)) {
- audio_glue0_res = AST_RTP_GLUE_RESULT_FORBID;
- }
- if (video_glue1_res != AST_RTP_GLUE_RESULT_FORBID && (audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE || video_glue1_res != AST_RTP_GLUE_RESULT_REMOTE)) {
- audio_glue1_res = AST_RTP_GLUE_RESULT_FORBID;
- }
- if (audio_glue0_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue0_res == AST_RTP_GLUE_RESULT_FORBID || video_glue0_res == AST_RTP_GLUE_RESULT_REMOTE) && glue0->get_codec) {
- glue0->get_codec(c0, cap0);
- }
- if (audio_glue1_res == AST_RTP_GLUE_RESULT_REMOTE && (video_glue1_res == AST_RTP_GLUE_RESULT_FORBID || video_glue1_res == AST_RTP_GLUE_RESULT_REMOTE) && glue1->get_codec) {
- glue1->get_codec(c1, cap1);
- }
- /* If any sort of bridge is forbidden just completely bail out and go back to generic bridging */
- if (audio_glue0_res != AST_RTP_GLUE_RESULT_REMOTE || audio_glue1_res != AST_RTP_GLUE_RESULT_REMOTE) {
- goto done;
- }
- /* Make sure we have matching codecs */
- if (!ast_format_cap_iscompatible(cap0, cap1)) {
- goto done;
- }
- /* Bridge media early */
- if (glue0->update_peer(c0, instance1, vinstance1, tinstance1, cap1, 0)) {
- ast_log(LOG_WARNING, "Channel '%s' failed to setup early bridge to '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
- }
- done:
- ast_channel_unlock(c0);
- ast_channel_unlock(c1);
- ao2_cleanup(cap0);
- ao2_cleanup(cap1);
- unref_instance_cond(&instance0);
- unref_instance_cond(&instance1);
- unref_instance_cond(&vinstance0);
- unref_instance_cond(&vinstance1);
- unref_instance_cond(&tinstance0);
- unref_instance_cond(&tinstance1);
- ast_debug(1, "Setting early bridge SDP of '%s' with that of '%s'\n", ast_channel_name(c0), c1 ? ast_channel_name(c1) : "<unspecified>");
- return 0;
- }
- int ast_rtp_red_init(struct ast_rtp_instance *instance, int buffer_time, int *payloads, int generations)
- {
- int res;
- if (instance->engine->red_init) {
- ao2_lock(instance);
- res = instance->engine->red_init(instance, buffer_time, payloads, generations);
- ao2_unlock(instance);
- } else {
- res = -1;
- }
- return res;
- }
- int ast_rtp_red_buffer(struct ast_rtp_instance *instance, struct ast_frame *frame)
- {
- int res;
- if (instance->engine->red_buffer) {
- ao2_lock(instance);
- res = instance->engine->red_buffer(instance, frame);
- ao2_unlock(instance);
- } else {
- res = -1;
- }
- return res;
- }
- int ast_rtp_instance_get_stats(struct ast_rtp_instance *instance, struct ast_rtp_instance_stats *stats, enum ast_rtp_instance_stat stat)
- {
- int res;
- if (instance->engine->get_stat) {
- ao2_lock(instance);
- res = instance->engine->get_stat(instance, stats, stat);
- ao2_unlock(instance);
- } else {
- res = -1;
- }
- return res;
- }
- char *ast_rtp_instance_get_quality(struct ast_rtp_instance *instance, enum ast_rtp_instance_stat_field field, char *buf, size_t size)
- {
- struct ast_rtp_instance_stats stats = { 0, };
- enum ast_rtp_instance_stat stat;
- /* Determine what statistics we will need to retrieve based on field passed in */
- if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
- stat = AST_RTP_INSTANCE_STAT_ALL;
- } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
- stat = AST_RTP_INSTANCE_STAT_COMBINED_JITTER;
- } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
- stat = AST_RTP_INSTANCE_STAT_COMBINED_LOSS;
- } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
- stat = AST_RTP_INSTANCE_STAT_COMBINED_RTT;
- } else {
- return NULL;
- }
- /* Attempt to actually retrieve the statistics we need to generate the quality string */
- if (ast_rtp_instance_get_stats(instance, &stats, stat)) {
- return NULL;
- }
- /* Now actually fill the buffer with the good information */
- if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY) {
- snprintf(buf, size, "ssrc=%u;themssrc=%u;lp=%u;rxjitter=%f;rxcount=%u;txjitter=%f;txcount=%u;rlp=%u;rtt=%f",
- stats.local_ssrc, stats.remote_ssrc, stats.rxploss, stats.rxjitter, stats.rxcount, stats.txjitter, stats.txcount, stats.txploss, stats.rtt);
- } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER) {
- snprintf(buf, size, "minrxjitter=%f;maxrxjitter=%f;avgrxjitter=%f;stdevrxjitter=%f;reported_minjitter=%f;reported_maxjitter=%f;reported_avgjitter=%f;reported_stdevjitter=%f;",
- stats.local_minjitter, stats.local_maxjitter, stats.local_normdevjitter, sqrt(stats.local_stdevjitter), stats.remote_minjitter, stats.remote_maxjitter, stats.remote_normdevjitter, sqrt(stats.remote_stdevjitter));
- } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS) {
- snprintf(buf, size, "minrxlost=%f;maxrxlost=%f;avgrxlost=%f;stdevrxlost=%f;reported_minlost=%f;reported_maxlost=%f;reported_avglost=%f;reported_stdevlost=%f;",
- stats.local_minrxploss, stats.local_maxrxploss, stats.local_normdevrxploss, sqrt(stats.local_stdevrxploss), stats.remote_minrxploss, stats.remote_maxrxploss, stats.remote_normdevrxploss, sqrt(stats.remote_stdevrxploss));
- } else if (field == AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT) {
- snprintf(buf, size, "minrtt=%f;maxrtt=%f;avgrtt=%f;stdevrtt=%f;", stats.minrtt, stats.maxrtt, stats.normdevrtt, stats.stdevrtt);
- }
- return buf;
- }
- void ast_rtp_instance_set_stats_vars(struct ast_channel *chan, struct ast_rtp_instance *instance)
- {
- char quality_buf[AST_MAX_USER_FIELD];
- char *quality;
- struct ast_channel *bridge;
- bridge = ast_channel_bridge_peer(chan);
- if (bridge) {
- ast_channel_lock_both(chan, bridge);
- ast_channel_stage_snapshot(bridge);
- } else {
- ast_channel_lock(chan);
- }
- ast_channel_stage_snapshot(chan);
- quality = ast_rtp_instance_get_quality(instance, AST_RTP_INSTANCE_STAT_FIELD_QUALITY,
- quality_buf, sizeof(quality_buf));
- if (quality) {
- pbx_builtin_setvar_helper(chan, "RTPAUDIOQOS", quality);
- if (bridge) {
- pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSBRIDGED", quality);
- }
- }
- quality = ast_rtp_instance_get_quality(instance,
- AST_RTP_INSTANCE_STAT_FIELD_QUALITY_JITTER, quality_buf, sizeof(quality_buf));
- if (quality) {
- pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSJITTER", quality);
- if (bridge) {
- pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSJITTERBRIDGED", quality);
- }
- }
- quality = ast_rtp_instance_get_quality(instance,
- AST_RTP_INSTANCE_STAT_FIELD_QUALITY_LOSS, quality_buf, sizeof(quality_buf));
- if (quality) {
- pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSLOSS", quality);
- if (bridge) {
- pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSLOSSBRIDGED", quality);
- }
- }
- quality = ast_rtp_instance_get_quality(instance,
- AST_RTP_INSTANCE_STAT_FIELD_QUALITY_RTT, quality_buf, sizeof(quality_buf));
- if (quality) {
- pbx_builtin_setvar_helper(chan, "RTPAUDIOQOSRTT", quality);
- if (bridge) {
- pbx_builtin_setvar_helper(bridge, "RTPAUDIOQOSRTTBRIDGED", quality);
- }
- }
- ast_channel_stage_snapshot_done(chan);
- ast_channel_unlock(chan);
- if (bridge) {
- ast_channel_stage_snapshot_done(bridge);
- ast_channel_unlock(bridge);
- ast_channel_unref(bridge);
- }
- }
- int ast_rtp_instance_set_read_format(struct ast_rtp_instance *instance, struct ast_format *format)
- {
- int res;
- if (instance->engine->set_read_format) {
- ao2_lock(instance);
- res = instance->engine->set_read_format(instance, format);
- ao2_unlock(instance);
- } else {
- res = -1;
- }
- return res;
- }
- int ast_rtp_instance_set_write_format(struct ast_rtp_instance *instance, struct ast_format *format)
- {
- int res;
- if (instance->engine->set_read_format) {
- ao2_lock(instance);
- res = instance->engine->set_write_format(instance, format);
- ao2_unlock(instance);
- } else {
- res = -1;
- }
- return res;
- }
- /* XXX Nothing calls this */
- int ast_rtp_instance_make_compatible(struct ast_channel *chan, struct ast_rtp_instance *instance, struct ast_channel *peer)
- {
- struct ast_rtp_glue *glue;
- struct ast_rtp_instance *peer_instance = NULL;
- int res = -1;
- if (!instance->engine->make_compatible) {
- return -1;
- }
- ast_channel_lock(peer);
- if (!(glue = ast_rtp_instance_get_glue(ast_channel_tech(peer)->type))) {
- ast_channel_unlock(peer);
- return -1;
- }
- glue->get_rtp_info(peer, &peer_instance);
- if (!peer_instance) {
- ast_log(LOG_ERROR, "Unable to get_rtp_info for peer type %s\n", glue->type);
- ast_channel_unlock(peer);
- return -1;
- }
- if (peer_instance->engine != instance->engine) {
- ast_log(LOG_ERROR, "Peer engine mismatch for type %s\n", glue->type);
- ast_channel_unlock(peer);
- ao2_ref(peer_instance, -1);
- return -1;
- }
- /*
- * XXX Good thing nothing calls this function because we would need
- * deadlock avoidance to get the two instance locks.
- */
- res = instance->engine->make_compatible(chan, instance, peer, peer_instance);
- ast_channel_unlock(peer);
- ao2_ref(peer_instance, -1);
- peer_instance = NULL;
- return res;
- }
- void ast_rtp_instance_available_formats(struct ast_rtp_instance *instance, struct ast_format_cap *to_endpoint, struct ast_format_cap *to_asterisk, struct ast_format_cap *result)
- {
- if (instance->engine->available_formats) {
- ao2_lock(instance);
- instance->engine->available_formats(instance, to_endpoint, to_asterisk, result);
- ao2_unlock(instance);
- if (ast_format_cap_count(result)) {
- return;
- }
- }
- ast_translate_available_formats(to_endpoint, to_asterisk, result);
- }
- int ast_rtp_instance_activate(struct ast_rtp_instance *instance)
- {
- int res;
- if (instance->engine->activate) {
- ao2_lock(instance);
- res = instance->engine->activate(instance);
- ao2_unlock(instance);
- } else {
- res = 0;
- }
- return res;
- }
- void ast_rtp_instance_stun_request(struct ast_rtp_instance *instance,
- struct ast_sockaddr *suggestion,
- const char *username)
- {
- if (instance->engine->stun_request) {
- instance->engine->stun_request(instance, suggestion, username);
- }
- }
- void ast_rtp_instance_set_timeout(struct ast_rtp_instance *instance, int timeout)
- {
- instance->timeout = timeout;
- }
- void ast_rtp_instance_set_hold_timeout(struct ast_rtp_instance *instance, int timeout)
- {
- instance->holdtimeout = timeout;
- }
- void ast_rtp_instance_set_keepalive(struct ast_rtp_instance *instance, int interval)
- {
- instance->keepalive = interval;
- }
- int ast_rtp_instance_get_timeout(struct ast_rtp_instance *instance)
- {
- return instance->timeout;
- }
- int ast_rtp_instance_get_hold_timeout(struct ast_rtp_instance *instance)
- {
- return instance->holdtimeout;
- }
- int ast_rtp_instance_get_keepalive(struct ast_rtp_instance *instance)
- {
- return instance->keepalive;
- }
- struct ast_rtp_engine *ast_rtp_instance_get_engine(struct ast_rtp_instance *instance)
- {
- return instance->engine;
- }
- struct ast_rtp_glue *ast_rtp_instance_get_active_glue(struct ast_rtp_instance *instance)
- {
- return instance->glue;
- }
- int ast_rtp_engine_register_srtp(struct ast_srtp_res *srtp_res, struct ast_srtp_policy_res *policy_res)
- {
- if (res_srtp || res_srtp_policy) {
- return -1;
- }
- if (!srtp_res || !policy_res) {
- return -1;
- }
- res_srtp = srtp_res;
- res_srtp_policy = policy_res;
- return 0;
- }
- void ast_rtp_engine_unregister_srtp(void)
- {
- res_srtp = NULL;
- res_srtp_policy = NULL;
- }
- int ast_rtp_engine_srtp_is_registered(void)
- {
- return res_srtp && res_srtp_policy;
- }
- int ast_rtp_instance_add_srtp_policy(struct ast_rtp_instance *instance, struct ast_srtp_policy *remote_policy, struct ast_srtp_policy *local_policy, int rtcp)
- {
- int res = 0;
- struct ast_srtp **srtp;
- if (!res_srtp) {
- return -1;
- }
- srtp = rtcp ? &instance->rtcp_srtp : &instance->srtp;
- if (!*srtp) {
- res = res_srtp->create(srtp, instance, remote_policy);
- } else {
- res = res_srtp->replace(srtp, instance, remote_policy);
- }
- if (!res) {
- res = res_srtp->add_stream(*srtp, local_policy);
- }
- return res;
- }
- struct ast_srtp *ast_rtp_instance_get_srtp(struct ast_rtp_instance *instance, int rtcp)
- {
- if (rtcp && instance->rtcp_srtp) {
- return instance->rtcp_srtp;
- } else {
- return instance->srtp;
- }
- }
- int ast_rtp_instance_sendcng(struct ast_rtp_instance *instance, int level)
- {
- int res;
- if (instance->engine->sendcng) {
- ao2_lock(instance);
- res = instance->engine->sendcng(instance, level);
- ao2_unlock(instance);
- } else {
- res = -1;
- }
- return res;
- }
- static void rtp_ice_wrap_set_authentication(struct ast_rtp_instance *instance, const char *ufrag, const char *password)
- {
- ao2_lock(instance);
- instance->engine->ice->set_authentication(instance, ufrag, password);
- ao2_unlock(instance);
- }
- static void rtp_ice_wrap_add_remote_candidate(struct ast_rtp_instance *instance, const struct ast_rtp_engine_ice_candidate *candidate)
- {
- ao2_lock(instance);
- instance->engine->ice->add_remote_candidate(instance, candidate);
- ao2_unlock(instance);
- }
- static void rtp_ice_wrap_start(struct ast_rtp_instance *instance)
- {
- ao2_lock(instance);
- instance->engine->ice->start(instance);
- ao2_unlock(instance);
- }
- static void rtp_ice_wrap_stop(struct ast_rtp_instance *instance)
- {
- ao2_lock(instance);
- instance->engine->ice->stop(instance);
- ao2_unlock(instance);
- }
- static const char *rtp_ice_wrap_get_ufrag(struct ast_rtp_instance *instance)
- {
- const char *ufrag;
- ao2_lock(instance);
- ufrag = instance->engine->ice->get_ufrag(instance);
- ao2_unlock(instance);
- return ufrag;
- }
- static const char *rtp_ice_wrap_get_password(struct ast_rtp_instance *instance)
- {
- const char *password;
- ao2_lock(instance);
- password = instance->engine->ice->get_password(instance);
- ao2_unlock(instance);
- return password;
- }
- static struct ao2_container *rtp_ice_wrap_get_local_candidates(struct ast_rtp_instance *instance)
- {
- struct ao2_container *local_candidates;
- ao2_lock(instance);
- local_candidates = instance->engine->ice->get_local_candidates(instance);
- ao2_unlock(instance);
- return local_candidates;
- }
- static void rtp_ice_wrap_ice_lite(struct ast_rtp_instance *instance)
- {
- ao2_lock(instance);
- instance->engine->ice->ice_lite(instance);
- ao2_unlock(instance);
- }
- static void rtp_ice_wrap_set_role(struct ast_rtp_instance *instance,
- enum ast_rtp_ice_role role)
- {
- ao2_lock(instance);
- instance->engine->ice->set_role(instance, role);
- ao2_unlock(instance);
- }
- static void rtp_ice_wrap_turn_request(struct ast_rtp_instance *instance,
- enum ast_rtp_ice_component_type component, enum ast_transport transport,
- const char *server, unsigned int port, const char *username, const char *password)
- {
- ao2_lock(instance);
- instance->engine->ice->turn_request(instance, component, transport, server, port,
- username, password);
- ao2_unlock(instance);
- }
- static void rtp_ice_wrap_change_components(struct ast_rtp_instance *instance,
- int num_components)
- {
- ao2_lock(instance);
- instance->engine->ice->change_components(instance, num_components);
- ao2_unlock(instance);
- }
- static struct ast_rtp_engine_ice rtp_ice_wrappers = {
- .set_authentication = rtp_ice_wrap_set_authentication,
- .add_remote_candidate = rtp_ice_wrap_add_remote_candidate,
- .start = rtp_ice_wrap_start,
- .stop = rtp_ice_wrap_stop,
- .get_ufrag = rtp_ice_wrap_get_ufrag,
- .get_password = rtp_ice_wrap_get_password,
- .get_local_candidates = rtp_ice_wrap_get_local_candidates,
- .ice_lite = rtp_ice_wrap_ice_lite,
- .set_role = rtp_ice_wrap_set_role,
- .turn_request = rtp_ice_wrap_turn_request,
- .change_components = rtp_ice_wrap_change_components,
- };
- struct ast_rtp_engine_ice *ast_rtp_instance_get_ice(struct ast_rtp_instance *instance)
- {
- if (instance->engine->ice) {
- return &rtp_ice_wrappers;
- }
- /* ICE not available */
- return NULL;
- }
- static int rtp_dtls_wrap_set_configuration(struct ast_rtp_instance *instance,
- const struct ast_rtp_dtls_cfg *dtls_cfg)
- {
- int set_configuration;
- ao2_lock(instance);
- set_configuration = instance->engine->dtls->set_configuration(instance, dtls_cfg);
- ao2_unlock(instance);
- return set_configuration;
- }
- static int rtp_dtls_wrap_active(struct ast_rtp_instance *instance)
- {
- int active;
- ao2_lock(instance);
- active = instance->engine->dtls->active(instance);
- ao2_unlock(instance);
- return active;
- }
- static void rtp_dtls_wrap_stop(struct ast_rtp_instance *instance)
- {
- ao2_lock(instance);
- instance->engine->dtls->stop(instance);
- ao2_unlock(instance);
- }
- static void rtp_dtls_wrap_reset(struct ast_rtp_instance *instance)
- {
- ao2_lock(instance);
- instance->engine->dtls->reset(instance);
- ao2_unlock(instance);
- }
- static enum ast_rtp_dtls_connection rtp_dtls_wrap_get_connection(struct ast_rtp_instance *instance)
- {
- enum ast_rtp_dtls_connection get_connection;
- ao2_lock(instance);
- get_connection = instance->engine->dtls->get_connection(instance);
- ao2_unlock(instance);
- return get_connection;
- }
- static enum ast_rtp_dtls_setup rtp_dtls_wrap_get_setup(struct ast_rtp_instance *instance)
- {
- enum ast_rtp_dtls_setup get_setup;
- ao2_lock(instance);
- get_setup = instance->engine->dtls->get_setup(instance);
- ao2_unlock(instance);
- return get_setup;
- }
- static void rtp_dtls_wrap_set_setup(struct ast_rtp_instance *instance,
- enum ast_rtp_dtls_setup setup)
- {
- ao2_lock(instance);
- instance->engine->dtls->set_setup(instance, setup);
- ao2_unlock(instance);
- }
- static void rtp_dtls_wrap_set_fingerprint(struct ast_rtp_instance *instance,
- enum ast_rtp_dtls_hash hash, const char *fingerprint)
- {
- ao2_lock(instance);
- instance->engine->dtls->set_fingerprint(instance, hash, fingerprint);
- ao2_unlock(instance);
- }
- static enum ast_rtp_dtls_hash rtp_dtls_wrap_get_fingerprint_hash(struct ast_rtp_instance *instance)
- {
- enum ast_rtp_dtls_hash get_fingerprint_hash;
- ao2_lock(instance);
- get_fingerprint_hash = instance->engine->dtls->get_fingerprint_hash(instance);
- ao2_unlock(instance);
- return get_fingerprint_hash;
- }
- static const char *rtp_dtls_wrap_get_fingerprint(struct ast_rtp_instance *instance)
- {
- const char *get_fingerprint;
- ao2_lock(instance);
- get_fingerprint = instance->engine->dtls->get_fingerprint(instance);
- ao2_unlock(instance);
- return get_fingerprint;
- }
- static struct ast_rtp_engine_dtls rtp_dtls_wrappers = {
- .set_configuration = rtp_dtls_wrap_set_configuration,
- .active = rtp_dtls_wrap_active,
- .stop = rtp_dtls_wrap_stop,
- .reset = rtp_dtls_wrap_reset,
- .get_connection = rtp_dtls_wrap_get_connection,
- .get_setup = rtp_dtls_wrap_get_setup,
- .set_setup = rtp_dtls_wrap_set_setup,
- .set_fingerprint = rtp_dtls_wrap_set_fingerprint,
- .get_fingerprint_hash = rtp_dtls_wrap_get_fingerprint_hash,
- .get_fingerprint = rtp_dtls_wrap_get_fingerprint,
- };
- struct ast_rtp_engine_dtls *ast_rtp_instance_get_dtls(struct ast_rtp_instance *instance)
- {
- if (instance->engine->dtls) {
- return &rtp_dtls_wrappers;
- }
- /* DTLS not available */
- return NULL;
- }
- int ast_rtp_dtls_cfg_parse(struct ast_rtp_dtls_cfg *dtls_cfg, const char *name, const char *value)
- {
- if (!strcasecmp(name, "dtlsenable")) {
- dtls_cfg->enabled = ast_true(value) ? 1 : 0;
- } else if (!strcasecmp(name, "dtlsverify")) {
- if (!strcasecmp(value, "yes")) {
- dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT | AST_RTP_DTLS_VERIFY_CERTIFICATE;
- } else if (!strcasecmp(value, "fingerprint")) {
- dtls_cfg->verify = AST_RTP_DTLS_VERIFY_FINGERPRINT;
- } else if (!strcasecmp(value, "certificate")) {
- dtls_cfg->verify = AST_RTP_DTLS_VERIFY_CERTIFICATE;
- } else if (!strcasecmp(value, "no")) {
- dtls_cfg->verify = AST_RTP_DTLS_VERIFY_NONE;
- } else {
- return -1;
- }
- } else if (!strcasecmp(name, "dtlsrekey")) {
- if (sscanf(value, "%30u", &dtls_cfg->rekey) != 1) {
- return -1;
- }
- } else if (!strcasecmp(name, "dtlscertfile")) {
- if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
- ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
- return -1;
- }
- ast_free(dtls_cfg->certfile);
- dtls_cfg->certfile = ast_strdup(value);
- } else if (!strcasecmp(name, "dtlsprivatekey")) {
- if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
- ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
- return -1;
- }
- ast_free(dtls_cfg->pvtfile);
- dtls_cfg->pvtfile = ast_strdup(value);
- } else if (!strcasecmp(name, "dtlscipher")) {
- ast_free(dtls_cfg->cipher);
- dtls_cfg->cipher = ast_strdup(value);
- } else if (!strcasecmp(name, "dtlscafile")) {
- if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
- ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
- return -1;
- }
- ast_free(dtls_cfg->cafile);
- dtls_cfg->cafile = ast_strdup(value);
- } else if (!strcasecmp(name, "dtlscapath") || !strcasecmp(name, "dtlscadir")) {
- if (!ast_strlen_zero(value) && !ast_file_is_readable(value)) {
- ast_log(LOG_ERROR, "%s file %s does not exist or is not readable\n", name, value);
- return -1;
- }
- ast_free(dtls_cfg->capath);
- dtls_cfg->capath = ast_strdup(value);
- } else if (!strcasecmp(name, "dtlssetup")) {
- if (!strcasecmp(value, "active")) {
- dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTIVE;
- } else if (!strcasecmp(value, "passive")) {
- dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_PASSIVE;
- } else if (!strcasecmp(value, "actpass")) {
- dtls_cfg->default_setup = AST_RTP_DTLS_SETUP_ACTPASS;
- }
- } else if (!strcasecmp(name, "dtlsfingerprint")) {
- if (!strcasecmp(value, "sha-256")) {
- dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA256;
- } else if (!strcasecmp(value, "sha-1")) {
- dtls_cfg->hash = AST_RTP_DTLS_HASH_SHA1;
- }
- } else {
- return -1;
- }
- return 0;
- }
- void ast_rtp_dtls_cfg_copy(const struct ast_rtp_dtls_cfg *src_cfg, struct ast_rtp_dtls_cfg *dst_cfg)
- {
- ast_rtp_dtls_cfg_free(dst_cfg); /* Prevent a double-call leaking memory via ast_strdup */
- dst_cfg->enabled = src_cfg->enabled;
- dst_cfg->verify = src_cfg->verify;
- dst_cfg->rekey = src_cfg->rekey;
- dst_cfg->suite = src_cfg->suite;
- dst_cfg->hash = src_cfg->hash;
- dst_cfg->certfile = ast_strdup(src_cfg->certfile);
- dst_cfg->pvtfile = ast_strdup(src_cfg->pvtfile);
- dst_cfg->cipher = ast_strdup(src_cfg->cipher);
- dst_cfg->cafile = ast_strdup(src_cfg->cafile);
- dst_cfg->capath = ast_strdup(src_cfg->capath);
- dst_cfg->default_setup = src_cfg->default_setup;
- }
- void ast_rtp_dtls_cfg_free(struct ast_rtp_dtls_cfg *dtls_cfg)
- {
- ast_free(dtls_cfg->certfile);
- dtls_cfg->certfile = NULL;
- ast_free(dtls_cfg->pvtfile);
- dtls_cfg->pvtfile = NULL;
- ast_free(dtls_cfg->cipher);
- dtls_cfg->cipher = NULL;
- ast_free(dtls_cfg->cafile);
- dtls_cfg->cafile = NULL;
- ast_free(dtls_cfg->capath);
- dtls_cfg->capath = NULL;
- }
- /*! \internal
- * \brief Small helper routine that cleans up entry i in
- * \c ast_rtp_mime_types.
- */
- static void rtp_engine_mime_type_cleanup(int i)
- {
- ao2_cleanup(ast_rtp_mime_types[i].payload_type.format);
- memset(&ast_rtp_mime_types[i], 0, sizeof(struct ast_rtp_mime_type));
- }
- static void set_next_mime_type(struct ast_format *format, int rtp_code, const char *type, const char *subtype, unsigned int sample_rate)
- {
- int x;
- ast_rwlock_wrlock(&mime_types_lock);
- x = mime_types_len;
- if (ARRAY_LEN(ast_rtp_mime_types) <= x) {
- ast_rwlock_unlock(&mime_types_lock);
- return;
- }
- /* Make sure any previous value in ast_rtp_mime_types is cleaned up */
- memset(&ast_rtp_mime_types[x], 0, sizeof(struct ast_rtp_mime_type));
- if (format) {
- ast_rtp_mime_types[x].payload_type.asterisk_format = 1;
- ast_rtp_mime_types[x].payload_type.format = ao2_bump(format);
- } else {
- ast_rtp_mime_types[x].payload_type.rtp_code = rtp_code;
- }
- ast_copy_string(ast_rtp_mime_types[x].type, type, sizeof(ast_rtp_mime_types[x].type));
- ast_copy_string(ast_rtp_mime_types[x].subtype, subtype, sizeof(ast_rtp_mime_types[x].subtype));
- ast_rtp_mime_types[x].sample_rate = sample_rate;
- mime_types_len++;
- ast_rwlock_unlock(&mime_types_lock);
- }
- static void add_static_payload(int map, struct ast_format *format, int rtp_code)
- {
- int x;
- struct ast_rtp_payload_type *type;
- /*
- * ARRAY_LEN's result is cast to an int so 'map' is not autocast to a size_t,
- * which if negative would cause an assertion.
- */
- ast_assert(map < (int)ARRAY_LEN(static_RTP_PT));
- ast_rwlock_wrlock(&static_RTP_PT_lock);
- if (map < 0) {
- /* find next available dynamic payload slot */
- for (x = AST_RTP_PT_FIRST_DYNAMIC; x < AST_RTP_MAX_PT; ++x) {
- if (!static_RTP_PT[x]) {
- map = x;
- break;
- }
- }
- /* http://www.iana.org/assignments/rtp-parameters
- * RFC 3551, Section 3: "[...] applications which need to define more
- * than 32 dynamic payload types MAY bind codes below 96, in which case
- * it is RECOMMENDED that unassigned payload type numbers be used
- * first". Updated by RFC 5761, Section 4: "[...] values in the range
- * 64-95 MUST NOT be used [to avoid conflicts with RTCP]". Summaries:
- * https://tools.ietf.org/html/draft-roach-mmusic-unified-plan#section-3.2.1.2
- * https://tools.ietf.org/html/draft-wu-avtcore-dynamic-pt-usage#section-3
- */
- if (map < 0) {
- for (x = MAX(ast_option_rtpptdynamic, 35); x <= AST_RTP_PT_LAST_REASSIGN; ++x) {
- if (!static_RTP_PT[x]) {
- map = x;
- break;
- }
- }
- }
- /* Yet, reusing mappings below 35 is not supported in Asterisk because
- * when Compact Headers are activated, no rtpmap is send for those below
- * 35. If you want to use 35 and below
- * A) do not use Compact Headers,
- * B) remove that code in chan_sip/res_pjsip, or
- * C) add a flag that this RTP Payload Type got reassigned dynamically
- * and requires a rtpmap even with Compact Headers enabled.
- */
- if (map < 0) {
- for (x = MAX(ast_option_rtpptdynamic, 20); x < 35; ++x) {
- if (!static_RTP_PT[x]) {
- map = x;
- break;
- }
- }
- }
- if (map < 0) {
- for (x = MAX(ast_option_rtpptdynamic, 0); x < 20; ++x) {
- if (!static_RTP_PT[x]) {
- map = x;
- break;
- }
- }
- }
- if (map < 0) {
- if (format) {
- ast_log(LOG_WARNING, "No Dynamic RTP mapping available for format %s\n",
- ast_format_get_name(format));
- } else {
- ast_log(LOG_WARNING, "No Dynamic RTP mapping available for RTP code %d\n",
- rtp_code);
- }
- ast_rwlock_unlock(&static_RTP_PT_lock);
- return;
- }
- }
- type = ast_rtp_engine_alloc_payload_type();
- if (type) {
- if (format) {
- ao2_ref(format, +1);
- type->format = format;
- type->asterisk_format = 1;
- } else {
- type->rtp_code = rtp_code;
- }
- type->payload = map;
- ao2_cleanup(static_RTP_PT[map]);
- static_RTP_PT[map] = type;
- }
- ast_rwlock_unlock(&static_RTP_PT_lock);
- }
- int ast_rtp_engine_load_format(struct ast_format *format)
- {
- char *codec_name = ast_strdupa(ast_format_get_codec_name(format));
- codec_name = ast_str_to_upper(codec_name);
- set_next_mime_type(format,
- 0,
- ast_codec_media_type2str(ast_format_get_type(format)),
- codec_name,
- ast_format_get_sample_rate(format));
- add_static_payload(-1, format, 0);
- return 0;
- }
- int ast_rtp_engine_unload_format(struct ast_format *format)
- {
- int x;
- int y = 0;
- ast_rwlock_wrlock(&static_RTP_PT_lock);
- /* remove everything pertaining to this format id from the lists */
- for (x = 0; x < AST_RTP_MAX_PT; x++) {
- if (static_RTP_PT[x]
- && ast_format_cmp(static_RTP_PT[x]->format, format) == AST_FORMAT_CMP_EQUAL) {
- ao2_ref(static_RTP_PT[x], -1);
- static_RTP_PT[x] = NULL;
- }
- }
- ast_rwlock_unlock(&static_RTP_PT_lock);
- ast_rwlock_wrlock(&mime_types_lock);
- /* rebuild the list skipping the items matching this id */
- for (x = 0; x < mime_types_len; x++) {
- if (ast_format_cmp(ast_rtp_mime_types[x].payload_type.format, format) == AST_FORMAT_CMP_EQUAL) {
- rtp_engine_mime_type_cleanup(x);
- continue;
- }
- if (x != y) {
- ast_rtp_mime_types[y] = ast_rtp_mime_types[x];
- }
- y++;
- }
- mime_types_len = y;
- ast_rwlock_unlock(&mime_types_lock);
- return 0;
- }
- /*!
- * \internal
- * \brief \ref stasis message payload for RTCP messages
- */
- struct rtcp_message_payload {
- struct ast_channel_snapshot *snapshot; /*< The channel snapshot, if available */
- struct ast_rtp_rtcp_report *report; /*< The RTCP report */
- struct ast_json *blob; /*< Extra JSON data to publish */
- };
- static void rtcp_message_payload_dtor(void *obj)
- {
- struct rtcp_message_payload *payload = obj;
- ao2_cleanup(payload->report);
- ao2_cleanup(payload->snapshot);
- ast_json_unref(payload->blob);
- }
- static struct ast_manager_event_blob *rtcp_report_to_ami(struct stasis_message *msg)
- {
- struct rtcp_message_payload *payload = stasis_message_data(msg);
- RAII_VAR(struct ast_str *, channel_string, NULL, ast_free);
- RAII_VAR(struct ast_str *, packet_string, ast_str_create(512), ast_free);
- unsigned int ssrc = payload->report->ssrc;
- unsigned int type = payload->report->type;
- unsigned int report_count = payload->report->reception_report_count;
- int i;
- if (!packet_string) {
- return NULL;
- }
- if (payload->snapshot) {
- channel_string = ast_manager_build_channel_state_string(payload->snapshot);
- if (!channel_string) {
- return NULL;
- }
- }
- if (payload->blob) {
- /* Optional data */
- struct ast_json *to = ast_json_object_get(payload->blob, "to");
- struct ast_json *from = ast_json_object_get(payload->blob, "from");
- struct ast_json *rtt = ast_json_object_get(payload->blob, "rtt");
- if (to) {
- ast_str_append(&packet_string, 0, "To: %s\r\n", ast_json_string_get(to));
- }
- if (from) {
- ast_str_append(&packet_string, 0, "From: %s\r\n", ast_json_string_get(from));
- }
- if (rtt) {
- ast_str_append(&packet_string, 0, "RTT: %4.4f\r\n", ast_json_real_get(rtt));
- }
- }
- ast_str_append(&packet_string, 0, "SSRC: 0x%.8x\r\n", ssrc);
- ast_str_append(&packet_string, 0, "PT: %u(%s)\r\n", type, type== AST_RTP_RTCP_SR ? "SR" : "RR");
- ast_str_append(&packet_string, 0, "ReportCount: %u\r\n", report_count);
- if (type == AST_RTP_RTCP_SR) {
- ast_str_append(&packet_string, 0, "SentNTP: %lu.%06lu\r\n",
- (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec,
- (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec);
- ast_str_append(&packet_string, 0, "SentRTP: %u\r\n",
- payload->report->sender_information.rtp_timestamp);
- ast_str_append(&packet_string, 0, "SentPackets: %u\r\n",
- payload->report->sender_information.packet_count);
- ast_str_append(&packet_string, 0, "SentOctets: %u\r\n",
- payload->report->sender_information.octet_count);
- }
- for (i = 0; i < report_count; i++) {
- RAII_VAR(struct ast_str *, report_string, NULL, ast_free);
- if (!payload->report->report_block[i]) {
- break;
- }
- report_string = ast_str_create(256);
- if (!report_string) {
- return NULL;
- }
- ast_str_append(&report_string, 0, "Report%dSourceSSRC: 0x%.8x\r\n",
- i, payload->report->report_block[i]->source_ssrc);
- ast_str_append(&report_string, 0, "Report%dFractionLost: %d\r\n",
- i, payload->report->report_block[i]->lost_count.fraction);
- ast_str_append(&report_string, 0, "Report%dCumulativeLost: %u\r\n",
- i, payload->report->report_block[i]->lost_count.packets);
- ast_str_append(&report_string, 0, "Report%dHighestSequence: %u\r\n",
- i, payload->report->report_block[i]->highest_seq_no & 0xffff);
- ast_str_append(&report_string, 0, "Report%dSequenceNumberCycles: %u\r\n",
- i, payload->report->report_block[i]->highest_seq_no >> 16);
- ast_str_append(&report_string, 0, "Report%dIAJitter: %u\r\n",
- i, payload->report->report_block[i]->ia_jitter);
- ast_str_append(&report_string, 0, "Report%dLSR: %u\r\n",
- i, payload->report->report_block[i]->lsr);
- ast_str_append(&report_string, 0, "Report%dDLSR: %4.4f\r\n",
- i, ((double)payload->report->report_block[i]->dlsr) / 65536);
- ast_str_append(&packet_string, 0, "%s", ast_str_buffer(report_string));
- }
- return ast_manager_event_blob_create(EVENT_FLAG_REPORTING,
- stasis_message_type(msg) == ast_rtp_rtcp_received_type() ? "RTCPReceived" : "RTCPSent",
- "%s%s",
- AS_OR(channel_string, ""),
- ast_str_buffer(packet_string));
- }
- static struct ast_json *rtcp_report_to_json(struct stasis_message *msg,
- const struct stasis_message_sanitizer *sanitize)
- {
- struct rtcp_message_payload *payload = stasis_message_data(msg);
- struct ast_json *json_rtcp_report = NULL;
- struct ast_json *json_rtcp_report_blocks;
- struct ast_json *json_rtcp_sender_info = NULL;
- struct ast_json *json_channel = NULL;
- int i;
- json_rtcp_report_blocks = ast_json_array_create();
- if (!json_rtcp_report_blocks) {
- return NULL;
- }
- for (i = 0; i < payload->report->reception_report_count && payload->report->report_block[i]; i++) {
- struct ast_json *json_report_block;
- char str_lsr[32];
- snprintf(str_lsr, sizeof(str_lsr), "%u", payload->report->report_block[i]->lsr);
- json_report_block = ast_json_pack("{s: I, s: i, s: i, s: i, s: i, s: s, s: i}",
- "source_ssrc", (ast_json_int_t)payload->report->report_block[i]->source_ssrc,
- "fraction_lost", payload->report->report_block[i]->lost_count.fraction,
- "packets_lost", payload->report->report_block[i]->lost_count.packets,
- "highest_seq_no", payload->report->report_block[i]->highest_seq_no,
- "ia_jitter", payload->report->report_block[i]->ia_jitter,
- "lsr", str_lsr,
- "dlsr", payload->report->report_block[i]->dlsr);
- if (!json_report_block
- || ast_json_array_append(json_rtcp_report_blocks, json_report_block)) {
- ast_json_unref(json_rtcp_report_blocks);
- return NULL;
- }
- }
- if (payload->report->type == AST_RTP_RTCP_SR) {
- char sec[32];
- char usec[32];
- snprintf(sec, sizeof(sec), "%lu", (unsigned long)payload->report->sender_information.ntp_timestamp.tv_sec);
- snprintf(usec, sizeof(usec), "%lu", (unsigned long)payload->report->sender_information.ntp_timestamp.tv_usec);
- json_rtcp_sender_info = ast_json_pack("{s: s, s: s, s: i, s: i, s: i}",
- "ntp_timestamp_sec", sec,
- "ntp_timestamp_usec", usec,
- "rtp_timestamp", payload->report->sender_information.rtp_timestamp,
- "packets", payload->report->sender_information.packet_count,
- "octets", payload->report->sender_information.octet_count);
- if (!json_rtcp_sender_info) {
- ast_json_unref(json_rtcp_report_blocks);
- return NULL;
- }
- }
- json_rtcp_report = ast_json_pack("{s: I, s: i, s: i, s: o, s: o}",
- "ssrc", (ast_json_int_t)payload->report->ssrc,
- "type", payload->report->type,
- "report_count", payload->report->reception_report_count,
- "sender_information", json_rtcp_sender_info ?: ast_json_null(),
- "report_blocks", json_rtcp_report_blocks);
- if (!json_rtcp_report) {
- return NULL;
- }
- if (payload->snapshot) {
- json_channel = ast_channel_snapshot_to_json(payload->snapshot, sanitize);
- if (!json_channel) {
- ast_json_unref(json_rtcp_report);
- return NULL;
- }
- }
- return ast_json_pack("{s: o, s: o, s: o}",
- "channel", payload->snapshot ? json_channel : ast_json_null(),
- "rtcp_report", json_rtcp_report,
- "blob", ast_json_deep_copy(payload->blob) ?: ast_json_null());
- }
- static void rtp_rtcp_report_dtor(void *obj)
- {
- int i;
- struct ast_rtp_rtcp_report *rtcp_report = obj;
- for (i = 0; i < rtcp_report->reception_report_count; i++) {
- ast_free(rtcp_report->report_block[i]);
- }
- }
- struct ast_rtp_rtcp_report *ast_rtp_rtcp_report_alloc(unsigned int report_blocks)
- {
- struct ast_rtp_rtcp_report *rtcp_report;
- /* Size of object is sizeof the report + the number of report_blocks * sizeof pointer */
- rtcp_report = ao2_alloc((sizeof(*rtcp_report) + report_blocks * sizeof(struct ast_rtp_rtcp_report_block *)),
- rtp_rtcp_report_dtor);
- return rtcp_report;
- }
- void ast_rtp_publish_rtcp_message(struct ast_rtp_instance *rtp,
- struct stasis_message_type *message_type,
- struct ast_rtp_rtcp_report *report,
- struct ast_json *blob)
- {
- RAII_VAR(struct rtcp_message_payload *, payload, NULL, ao2_cleanup);
- RAII_VAR(struct stasis_message *, message, NULL, ao2_cleanup);
- if (!message_type) {
- return;
- }
- payload = ao2_alloc(sizeof(*payload), rtcp_message_payload_dtor);
- if (!payload || !report) {
- return;
- }
- if (!ast_strlen_zero(rtp->channel_uniqueid)) {
- payload->snapshot = ast_channel_snapshot_get_latest(rtp->channel_uniqueid);
- }
- if (blob) {
- payload->blob = blob;
- ast_json_ref(blob);
- }
- ao2_ref(report, +1);
- payload->report = report;
- message = stasis_message_create(message_type, payload);
- if (!message) {
- return;
- }
- stasis_publish(ast_rtp_topic(), message);
- }
- /*!
- * @{ \brief Define RTCP/RTP message types.
- */
- STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_sent_type,
- .to_ami = rtcp_report_to_ami,
- .to_json = rtcp_report_to_json,);
- STASIS_MESSAGE_TYPE_DEFN(ast_rtp_rtcp_received_type,
- .to_ami = rtcp_report_to_ami,
- .to_json = rtcp_report_to_json,);
- /*! @} */
- struct stasis_topic *ast_rtp_topic(void)
- {
- return rtp_topic;
- }
- static void rtp_engine_shutdown(void)
- {
- int x;
- ao2_cleanup(rtp_topic);
- rtp_topic = NULL;
- STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_received_type);
- STASIS_MESSAGE_TYPE_CLEANUP(ast_rtp_rtcp_sent_type);
- ast_rwlock_wrlock(&static_RTP_PT_lock);
- for (x = 0; x < AST_RTP_MAX_PT; x++) {
- ao2_cleanup(static_RTP_PT[x]);
- static_RTP_PT[x] = NULL;
- }
- ast_rwlock_unlock(&static_RTP_PT_lock);
- ast_rwlock_wrlock(&mime_types_lock);
- for (x = 0; x < mime_types_len; x++) {
- if (ast_rtp_mime_types[x].payload_type.format) {
- rtp_engine_mime_type_cleanup(x);
- }
- }
- mime_types_len = 0;
- ast_rwlock_unlock(&mime_types_lock);
- }
- int ast_rtp_engine_init(void)
- {
- ast_rwlock_init(&mime_types_lock);
- ast_rwlock_init(&static_RTP_PT_lock);
- rtp_topic = stasis_topic_create("rtp_topic");
- if (!rtp_topic) {
- return -1;
- }
- STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_sent_type);
- STASIS_MESSAGE_TYPE_INIT(ast_rtp_rtcp_received_type);
- ast_register_cleanup(rtp_engine_shutdown);
- /* Define all the RTP mime types available */
- set_next_mime_type(ast_format_g723, 0, "audio", "G723", 8000);
- set_next_mime_type(ast_format_gsm, 0, "audio", "GSM", 8000);
- set_next_mime_type(ast_format_ulaw, 0, "audio", "PCMU", 8000);
- set_next_mime_type(ast_format_ulaw, 0, "audio", "G711U", 8000);
- set_next_mime_type(ast_format_alaw, 0, "audio", "PCMA", 8000);
- set_next_mime_type(ast_format_alaw, 0, "audio", "G711A", 8000);
- set_next_mime_type(ast_format_g726, 0, "audio", "G726-32", 8000);
- set_next_mime_type(ast_format_adpcm, 0, "audio", "DVI4", 8000);
- set_next_mime_type(ast_format_slin, 0, "audio", "L16", 8000);
- set_next_mime_type(ast_format_slin16, 0, "audio", "L16", 16000);
- set_next_mime_type(ast_format_slin16, 0, "audio", "L16-256", 16000);
- set_next_mime_type(ast_format_lpc10, 0, "audio", "LPC", 8000);
- set_next_mime_type(ast_format_g729, 0, "audio", "G729", 8000);
- set_next_mime_type(ast_format_g729, 0, "audio", "G729A", 8000);
- set_next_mime_type(ast_format_g729, 0, "audio", "G.729", 8000);
- set_next_mime_type(ast_format_speex, 0, "audio", "speex", 8000);
- set_next_mime_type(ast_format_speex16, 0, "audio", "speex", 16000);
- set_next_mime_type(ast_format_speex32, 0, "audio", "speex", 32000);
- set_next_mime_type(ast_format_ilbc, 0, "audio", "iLBC", 8000);
- /* this is the sample rate listed in the RTP profile for the G.722 codec, *NOT* the actual sample rate of the media stream */
- set_next_mime_type(ast_format_g722, 0, "audio", "G722", 8000);
- set_next_mime_type(ast_format_g726_aal2, 0, "audio", "AAL2-G726-32", 8000);
- set_next_mime_type(NULL, AST_RTP_DTMF, "audio", "telephone-event", 8000);
- set_next_mime_type(NULL, AST_RTP_CISCO_DTMF, "audio", "cisco-telephone-event", 8000);
- set_next_mime_type(NULL, AST_RTP_CN, "audio", "CN", 8000);
- set_next_mime_type(ast_format_jpeg, 0, "video", "JPEG", 90000);
- set_next_mime_type(ast_format_png, 0, "video", "PNG", 90000);
- set_next_mime_type(ast_format_h261, 0, "video", "H261", 90000);
- set_next_mime_type(ast_format_h263, 0, "video", "H263", 90000);
- set_next_mime_type(ast_format_h263p, 0, "video", "h263-1998", 90000);
- set_next_mime_type(ast_format_h264, 0, "video", "H264", 90000);
- set_next_mime_type(ast_format_mp4, 0, "video", "MP4V-ES", 90000);
- set_next_mime_type(ast_format_t140_red, 0, "text", "RED", 1000);
- set_next_mime_type(ast_format_t140, 0, "text", "T140", 1000);
- set_next_mime_type(ast_format_siren7, 0, "audio", "G7221", 16000);
- set_next_mime_type(ast_format_siren14, 0, "audio", "G7221", 32000);
- set_next_mime_type(ast_format_g719, 0, "audio", "G719", 48000);
- /* Opus, VP8, and VP9 */
- set_next_mime_type(ast_format_opus, 0, "audio", "opus", 48000);
- set_next_mime_type(ast_format_vp8, 0, "video", "VP8", 90000);
- set_next_mime_type(ast_format_vp9, 0, "video", "VP9", 90000);
- /* Define the static rtp payload mappings */
- add_static_payload(0, ast_format_ulaw, 0);
- #ifdef USE_DEPRECATED_G726
- add_static_payload(2, ast_format_g726, 0);/* Technically this is G.721, but if Cisco can do it, so can we... */
- #endif
- add_static_payload(3, ast_format_gsm, 0);
- add_static_payload(4, ast_format_g723, 0);
- add_static_payload(5, ast_format_adpcm, 0);/* 8 kHz */
- add_static_payload(6, ast_format_adpcm, 0); /* 16 kHz */
- add_static_payload(7, ast_format_lpc10, 0);
- add_static_payload(8, ast_format_alaw, 0);
- add_static_payload(9, ast_format_g722, 0);
- add_static_payload(10, ast_format_slin, 0); /* 2 channels */
- add_static_payload(11, ast_format_slin, 0); /* 1 channel */
- add_static_payload(13, NULL, AST_RTP_CN);
- add_static_payload(16, ast_format_adpcm, 0); /* 11.025 kHz */
- add_static_payload(17, ast_format_adpcm, 0); /* 22.050 kHz */
- add_static_payload(18, ast_format_g729, 0);
- add_static_payload(19, NULL, AST_RTP_CN); /* Also used for CN */
- add_static_payload(26, ast_format_jpeg, 0);
- add_static_payload(31, ast_format_h261, 0);
- add_static_payload(34, ast_format_h263, 0);
- add_static_payload(97, ast_format_ilbc, 0);
- add_static_payload(99, ast_format_h264, 0);
- add_static_payload(101, NULL, AST_RTP_DTMF);
- add_static_payload(102, ast_format_siren7, 0);
- add_static_payload(103, ast_format_h263p, 0);
- add_static_payload(104, ast_format_mp4, 0);
- add_static_payload(105, ast_format_t140_red, 0); /* Real time text chat (with redundancy encoding) */
- add_static_payload(106, ast_format_t140, 0); /* Real time text chat */
- add_static_payload(108, ast_format_vp9, 0);
- add_static_payload(110, ast_format_speex, 0);
- add_static_payload(111, ast_format_g726, 0);
- add_static_payload(112, ast_format_g726_aal2, 0);
- add_static_payload(115, ast_format_siren14, 0);
- add_static_payload(116, ast_format_g719, 0);
- add_static_payload(117, ast_format_speex16, 0);
- add_static_payload(118, ast_format_slin16, 0); /* 16 Khz signed linear */
- add_static_payload(119, ast_format_speex32, 0);
- add_static_payload(121, NULL, AST_RTP_CISCO_DTMF); /* Must be type 121 */
- /* Opus and VP8 */
- add_static_payload(100, ast_format_vp8, 0);
- add_static_payload(107, ast_format_opus, 0);
- return 0;
- }
- time_t ast_rtp_instance_get_last_tx(const struct ast_rtp_instance *rtp)
- {
- return rtp->last_tx;
- }
- void ast_rtp_instance_set_last_tx(struct ast_rtp_instance *rtp, time_t time)
- {
- rtp->last_tx = time;
- }
- time_t ast_rtp_instance_get_last_rx(const struct ast_rtp_instance *rtp)
- {
- return rtp->last_rx;
- }
- void ast_rtp_instance_set_last_rx(struct ast_rtp_instance *rtp, time_t time)
- {
- rtp->last_rx = time;
- }
|