2021-04-09 20:42:36 +08:00
/*
2023-12-09 16:23:51 +08:00
* Copyright ( c ) 2016 - present The ZLMediaKit project authors . All Rights Reserved .
2021-04-09 20:42:36 +08:00
*
2023-12-09 16:23:51 +08:00
* This file is part of ZLMediaKit ( https : //github.com/ZLMediaKit/ZLMediaKit).
2021-04-09 20:42:36 +08:00
*
2023-12-09 16:23:51 +08:00
* Use of this source code is governed by MIT - like license that can be found in the
2021-04-09 20:42:36 +08:00
* LICENSE file in the root of the source tree . All contributing project authors
* may be found in the AUTHORS file in the root of the source tree .
*/
2021-03-27 18:33:20 +08:00
# include "Sdp.h"
2021-04-02 18:28:01 +08:00
# include "Rtsp/Rtsp.h"
2022-11-29 11:07:13 +08:00
# include "Common/config.h"
2021-06-23 10:34:02 +08:00
# include <cinttypes>
2022-02-02 20:34:50 +08:00
using namespace std ;
using namespace toolkit ;
2021-03-28 09:49:34 +08:00
2022-09-18 21:03:05 +08:00
namespace mediakit {
namespace Rtc {
2021-11-15 10:32:54 +08:00
# define RTC_FIELD "rtc."
2024-03-23 23:08:10 +08:00
const string kPreferredCodecA = RTC_FIELD " preferredCodecA " ;
const string kPreferredCodecV = RTC_FIELD " preferredCodecV " ;
2021-11-15 10:32:54 +08:00
static onceToken token ( [ ] ( ) {
2024-03-22 20:30:48 +08:00
mINI : : Instance ( ) [ kPreferredCodecA ] = " PCMA,PCMU,opus,mpeg4-generic " ;
2022-11-18 16:47:02 +08:00
mINI : : Instance ( ) [ kPreferredCodecV ] = " H264,H265,AV1,VP9,VP8 " ;
2021-11-15 10:32:54 +08:00
} ) ;
2024-03-23 23:08:10 +08:00
} // namespace Rtc
2021-11-15 10:32:54 +08:00
2021-03-28 09:49:34 +08:00
using onCreateSdpItem = function < SdpItem : : Ptr ( const string & key , const string & value ) > ;
2021-03-30 09:53:58 +08:00
static map < string , onCreateSdpItem , StrCaseCompare > sdpItemCreator ;
2021-03-28 09:49:34 +08:00
template < typename Item >
2024-03-23 23:08:10 +08:00
void registerSdpItem ( ) {
2021-03-28 09:49:34 +08:00
onCreateSdpItem func = [ ] ( const string & key , const string & value ) {
auto ret = std : : make_shared < Item > ( ) ;
ret - > parse ( value ) ;
return ret ;
} ;
Item item ;
sdpItemCreator . emplace ( item . getKey ( ) , std : : move ( func ) ) ;
}
2021-03-28 17:32:53 +08:00
class DirectionInterface {
public :
2021-03-28 23:31:21 +08:00
virtual RtpDirection getDirection ( ) const = 0 ;
2021-03-28 17:32:53 +08:00
} ;
2024-03-23 23:08:10 +08:00
class SdpDirectionSendonly : public SdpItem , public DirectionInterface {
2021-03-28 17:32:53 +08:00
public :
2024-03-23 23:08:10 +08:00
const char * getKey ( ) const override { return getRtpDirectionString ( getDirection ( ) ) ; }
RtpDirection getDirection ( ) const override { return RtpDirection : : sendonly ; }
2021-03-28 17:32:53 +08:00
} ;
2024-03-23 23:08:10 +08:00
class SdpDirectionRecvonly : public SdpItem , public DirectionInterface {
2021-03-28 17:32:53 +08:00
public :
2024-03-23 23:08:10 +08:00
const char * getKey ( ) const override { return getRtpDirectionString ( getDirection ( ) ) ; }
RtpDirection getDirection ( ) const override { return RtpDirection : : recvonly ; }
2021-03-28 17:32:53 +08:00
} ;
2024-03-23 23:08:10 +08:00
class SdpDirectionSendrecv : public SdpItem , public DirectionInterface {
2021-03-28 17:32:53 +08:00
public :
2024-03-23 23:08:10 +08:00
const char * getKey ( ) const override { return getRtpDirectionString ( getDirection ( ) ) ; }
RtpDirection getDirection ( ) const override { return RtpDirection : : sendrecv ; }
2021-03-28 17:32:53 +08:00
} ;
2024-03-23 23:08:10 +08:00
class SdpDirectionInactive : public SdpItem , public DirectionInterface {
2021-03-28 17:32:53 +08:00
public :
2024-03-23 23:08:10 +08:00
const char * getKey ( ) const override { return getRtpDirectionString ( getDirection ( ) ) ; }
RtpDirection getDirection ( ) const override { return RtpDirection : : inactive ; }
2021-03-28 17:32:53 +08:00
} ;
2024-03-23 23:08:10 +08:00
class DirectionInterfaceImp : public SdpItem , public DirectionInterface {
2021-03-30 13:09:02 +08:00
public :
2024-03-23 23:08:10 +08:00
DirectionInterfaceImp ( RtpDirection direct ) { direction = direct ; }
const char * getKey ( ) const override { return getRtpDirectionString ( getDirection ( ) ) ; }
RtpDirection getDirection ( ) const override { return direction ; }
2021-03-30 13:09:02 +08:00
private :
RtpDirection direction ;
} ;
2024-03-23 23:08:10 +08:00
static bool registerAllItem ( ) {
registerSdpItem < SdpString < ' v ' > > ( ) ;
registerSdpItem < SdpString < ' s ' > > ( ) ;
registerSdpItem < SdpString < ' i ' > > ( ) ;
registerSdpItem < SdpString < ' u ' > > ( ) ;
registerSdpItem < SdpString < ' e ' > > ( ) ;
registerSdpItem < SdpString < ' p ' > > ( ) ;
registerSdpItem < SdpString < ' z ' > > ( ) ;
registerSdpItem < SdpString < ' k ' > > ( ) ;
registerSdpItem < SdpString < ' r ' > > ( ) ;
2021-03-28 09:49:34 +08:00
registerSdpItem < SdpTime > ( ) ;
registerSdpItem < SdpOrigin > ( ) ;
registerSdpItem < SdpConnection > ( ) ;
registerSdpItem < SdpBandwidth > ( ) ;
registerSdpItem < SdpMedia > ( ) ;
registerSdpItem < SdpAttr > ( ) ;
registerSdpItem < SdpAttrGroup > ( ) ;
registerSdpItem < SdpAttrMsidSemantic > ( ) ;
registerSdpItem < SdpAttrRtcp > ( ) ;
registerSdpItem < SdpAttrIceUfrag > ( ) ;
registerSdpItem < SdpAttrIcePwd > ( ) ;
2021-03-28 23:31:21 +08:00
registerSdpItem < SdpAttrIceOption > ( ) ;
2021-03-28 09:49:34 +08:00
registerSdpItem < SdpAttrFingerprint > ( ) ;
registerSdpItem < SdpAttrSetup > ( ) ;
registerSdpItem < SdpAttrMid > ( ) ;
registerSdpItem < SdpAttrExtmap > ( ) ;
registerSdpItem < SdpAttrRtpMap > ( ) ;
registerSdpItem < SdpAttrRtcpFb > ( ) ;
registerSdpItem < SdpAttrFmtp > ( ) ;
registerSdpItem < SdpAttrSSRC > ( ) ;
2021-03-28 23:31:21 +08:00
registerSdpItem < SdpAttrSSRCGroup > ( ) ;
2021-03-28 09:49:34 +08:00
registerSdpItem < SdpAttrSctpMap > ( ) ;
2021-03-28 17:32:53 +08:00
registerSdpItem < SdpAttrCandidate > ( ) ;
registerSdpItem < SdpDirectionSendonly > ( ) ;
registerSdpItem < SdpDirectionRecvonly > ( ) ;
registerSdpItem < SdpDirectionSendrecv > ( ) ;
registerSdpItem < SdpDirectionInactive > ( ) ;
2021-03-31 17:15:26 +08:00
registerSdpItem < SdpAttrMsid > ( ) ;
registerSdpItem < SdpAttrExtmapAllowMixed > ( ) ;
registerSdpItem < SdpAttrRid > ( ) ;
registerSdpItem < SdpAttrSimulcast > ( ) ;
2021-03-28 09:49:34 +08:00
return true ;
}
2021-03-30 09:53:58 +08:00
static map < string , DtlsRole , StrCaseCompare > dtls_role_map = {
{ " active " , DtlsRole : : active } ,
{ " passive " , DtlsRole : : passive } ,
{ " actpass " , DtlsRole : : actpass }
} ;
DtlsRole getDtlsRole ( const string & str ) {
auto it = dtls_role_map . find ( str ) ;
return it = = dtls_role_map . end ( ) ? DtlsRole : : invalid : it - > second ;
2021-03-28 17:32:53 +08:00
}
2024-03-23 23:08:10 +08:00
const char * getDtlsRoleString ( DtlsRole role ) {
2021-03-28 17:32:53 +08:00
switch ( role ) {
2024-03-23 23:08:10 +08:00
case DtlsRole : : active : return " active " ;
case DtlsRole : : passive : return " passive " ;
case DtlsRole : : actpass : return " actpass " ;
2021-03-28 17:32:53 +08:00
default : return " invalid " ;
}
}
2021-03-30 09:53:58 +08:00
static map < string , RtpDirection , StrCaseCompare > direction_map = {
{ " sendonly " , RtpDirection : : sendonly } ,
{ " recvonly " , RtpDirection : : recvonly } ,
{ " sendrecv " , RtpDirection : : sendrecv } ,
{ " inactive " , RtpDirection : : inactive }
} ;
RtpDirection getRtpDirection ( const string & str ) {
auto it = direction_map . find ( str ) ;
return it = = direction_map . end ( ) ? RtpDirection : : invalid : it - > second ;
2021-03-28 17:32:53 +08:00
}
2024-03-23 23:08:10 +08:00
const char * getRtpDirectionString ( RtpDirection val ) {
2021-03-28 17:32:53 +08:00
switch ( val ) {
2024-03-23 23:08:10 +08:00
case RtpDirection : : sendonly : return " sendonly " ;
case RtpDirection : : recvonly : return " recvonly " ;
case RtpDirection : : sendrecv : return " sendrecv " ;
case RtpDirection : : inactive : return " inactive " ;
2021-03-28 17:32:53 +08:00
default : return " invalid " ;
}
}
//////////////////////////////////////////////////////////////////////////////////////////
2021-03-29 11:39:58 +08:00
string RtcSdpBase : : toString ( ) const {
_StrPrinter printer ;
for ( auto & item : items ) {
printer < < item - > getKey ( ) < < " = " < < item - > toString ( ) < < " \r \n " ;
}
return std : : move ( printer ) ;
}
2024-03-23 23:08:10 +08:00
RtpDirection RtcSdpBase : : getDirection ( ) const {
2021-03-29 11:39:58 +08:00
for ( auto & item : items ) {
auto attr = dynamic_pointer_cast < SdpAttr > ( item ) ;
if ( attr ) {
auto dir = dynamic_pointer_cast < DirectionInterface > ( attr - > detail ) ;
if ( dir ) {
return dir - > getDirection ( ) ;
}
}
}
return RtpDirection : : invalid ;
}
2021-03-30 09:53:58 +08:00
SdpItem : : Ptr RtcSdpBase : : getItem ( char key_c , const char * attr_key ) const {
2022-05-18 09:56:53 +08:00
std : : string key ( 1 , key_c ) ;
2021-03-29 11:39:58 +08:00
for ( auto item : items ) {
2021-03-30 09:53:58 +08:00
if ( strcasecmp ( item - > getKey ( ) , key . data ( ) ) = = 0 ) {
2021-03-29 11:39:58 +08:00
if ( ! attr_key ) {
return item ;
}
auto attr = dynamic_pointer_cast < SdpAttr > ( item ) ;
2024-03-23 23:08:10 +08:00
if ( attr & & ! strcasecmp ( attr - > detail - > getKey ( ) , attr_key ) ) {
2021-03-29 12:00:42 +08:00
return attr - > detail ;
2021-03-29 11:39:58 +08:00
}
}
}
return SdpItem : : Ptr ( ) ;
}
2022-05-18 09:56:53 +08:00
//////////////////////////////////////////////////////////////////////////
int RtcSessionSdp : : getVersion ( ) const {
2021-03-29 11:39:58 +08:00
return atoi ( getStringItem ( ' v ' ) . data ( ) ) ;
}
2022-05-18 09:56:53 +08:00
SdpOrigin RtcSessionSdp : : getOrigin ( ) const {
2021-03-29 11:39:58 +08:00
return getItemClass < SdpOrigin > ( ' o ' ) ;
}
2022-05-18 09:56:53 +08:00
string RtcSessionSdp : : getSessionName ( ) const {
2021-03-29 11:39:58 +08:00
return getStringItem ( ' s ' ) ;
}
2022-05-18 09:56:53 +08:00
string RtcSessionSdp : : getSessionInfo ( ) const {
2021-03-29 11:39:58 +08:00
return getStringItem ( ' i ' ) ;
}
2024-03-23 23:08:10 +08:00
SdpTime RtcSessionSdp : : getSessionTime ( ) const {
2021-03-29 11:39:58 +08:00
return getItemClass < SdpTime > ( ' t ' ) ;
}
2022-05-18 09:56:53 +08:00
SdpConnection RtcSessionSdp : : getConnection ( ) const {
2021-03-29 11:39:58 +08:00
return getItemClass < SdpConnection > ( ' c ' ) ;
}
2022-05-18 09:56:53 +08:00
SdpBandwidth RtcSessionSdp : : getBandwidth ( ) const {
2021-03-29 11:39:58 +08:00
return getItemClass < SdpBandwidth > ( ' b ' ) ;
}
2022-05-18 09:56:53 +08:00
string RtcSessionSdp : : getUri ( ) const {
2021-03-29 11:39:58 +08:00
return getStringItem ( ' u ' ) ;
}
2022-05-18 09:56:53 +08:00
string RtcSessionSdp : : getEmail ( ) const {
2021-03-29 11:39:58 +08:00
return getStringItem ( ' e ' ) ;
}
2022-05-18 09:56:53 +08:00
string RtcSessionSdp : : getPhone ( ) const {
2021-03-29 11:39:58 +08:00
return getStringItem ( ' p ' ) ;
}
2022-05-18 09:56:53 +08:00
string RtcSessionSdp : : getTimeZone ( ) const {
2021-03-29 11:39:58 +08:00
return getStringItem ( ' z ' ) ;
}
2022-05-18 09:56:53 +08:00
string RtcSessionSdp : : getEncryptKey ( ) const {
2021-03-29 11:39:58 +08:00
return getStringItem ( ' k ' ) ;
}
2022-05-18 09:56:53 +08:00
string RtcSessionSdp : : getRepeatTimes ( ) const {
2021-03-29 11:39:58 +08:00
return getStringItem ( ' r ' ) ;
}
//////////////////////////////////////////////////////////////////////
2021-03-29 10:54:18 +08:00
void RtcSessionSdp : : parse ( const string & str ) {
2021-03-28 09:49:34 +08:00
static auto flag = registerAllItem ( ) ;
2021-03-29 11:39:58 +08:00
RtcSdpBase * media = nullptr ;
2021-03-28 09:49:34 +08:00
auto lines = split ( str , " \n " ) ;
2023-12-01 10:44:59 +08:00
std : : set < std : : string > line_set ;
for ( auto & line : lines ) {
2021-03-28 09:49:34 +08:00
trim ( line ) ;
2023-12-01 10:44:59 +08:00
if ( line . size ( ) < 3 | | line [ 1 ] ! = ' = ' ) {
2021-03-28 09:49:34 +08:00
continue ;
}
2023-12-01 10:44:59 +08:00
if ( ! line_set . emplace ( line ) . second ) {
continue ;
}
2021-03-28 09:49:34 +08:00
auto key = line . substr ( 0 , 1 ) ;
auto value = line . substr ( 2 ) ;
2021-03-30 09:58:53 +08:00
if ( ! strcasecmp ( key . data ( ) , " m " ) ) {
2021-03-29 11:39:58 +08:00
medias . emplace_back ( RtcSdpBase ( ) ) ;
2021-03-28 09:49:34 +08:00
media = & medias . back ( ) ;
2023-12-01 10:44:59 +08:00
line_set . clear ( ) ;
2021-03-28 09:49:34 +08:00
}
SdpItem : : Ptr item ;
auto it = sdpItemCreator . find ( key ) ;
if ( it ! = sdpItemCreator . end ( ) ) {
item = it - > second ( key , value ) ;
} else {
item = std : : make_shared < SdpCommon > ( key ) ;
item - > parse ( value ) ;
}
if ( media ) {
2022-05-20 22:14:21 +08:00
media - > addItem ( std : : move ( item ) ) ;
2021-03-28 09:49:34 +08:00
} else {
2022-05-20 22:14:21 +08:00
addItem ( std : : move ( item ) ) ;
2021-03-28 09:49:34 +08:00
}
}
}
2021-03-29 10:54:18 +08:00
string RtcSessionSdp : : toString ( ) const {
2021-03-28 17:32:53 +08:00
_StrPrinter printer ;
2021-03-29 11:39:58 +08:00
printer < < RtcSdpBase : : toString ( ) ;
2021-03-28 17:32:53 +08:00
for ( auto & media : medias ) {
printer < < media . toString ( ) ;
}
return std : : move ( printer ) ;
}
//////////////////////////////////////////////////////////////////////////////////////////
2021-10-16 16:46:05 +08:00
# define CHECK_SDP(exp) CHECK(exp, "解析sdp ", getKey(), " 字段失败:", str)
2021-03-28 23:31:21 +08:00
2021-03-28 17:32:53 +08:00
void SdpTime : : parse ( const string & str ) {
2021-10-16 16:46:05 +08:00
CHECK_SDP ( sscanf ( str . data ( ) , " % " SCNu64 " % " SCNu64 , & start , & stop ) = = 2 ) ;
2021-03-28 17:32:53 +08:00
}
string SdpTime : : toString ( ) const {
if ( value . empty ( ) ) {
value = to_string ( start ) + " " + to_string ( stop ) ;
}
return SdpItem : : toString ( ) ;
}
void SdpOrigin : : parse ( const string & str ) {
auto vec = split ( str , " " ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( vec . size ( ) = = 6 ) ;
2021-03-28 17:32:53 +08:00
username = vec [ 0 ] ;
session_id = vec [ 1 ] ;
session_version = vec [ 2 ] ;
nettype = vec [ 3 ] ;
addrtype = vec [ 4 ] ;
2021-03-29 09:53:54 +08:00
address = vec [ 5 ] ;
2021-03-28 17:32:53 +08:00
}
string SdpOrigin : : toString ( ) const {
if ( value . empty ( ) ) {
value = username + " " + session_id + " " + session_version + " " + nettype + " " + addrtype + " " + address ;
}
return SdpItem : : toString ( ) ;
}
void SdpConnection : : parse ( const string & str ) {
auto vec = split ( str , " " ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( vec . size ( ) = = 3 ) ;
2021-03-28 17:32:53 +08:00
nettype = vec [ 0 ] ;
addrtype = vec [ 1 ] ;
address = vec [ 2 ] ;
}
string SdpConnection : : toString ( ) const {
if ( value . empty ( ) ) {
value = nettype + " " + addrtype + " " + address ;
}
return SdpItem : : toString ( ) ;
}
void SdpBandwidth : : parse ( const string & str ) {
auto vec = split ( str , " : " ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( vec . size ( ) = = 2 ) ;
2021-03-28 17:32:53 +08:00
bwtype = vec [ 0 ] ;
bandwidth = atoi ( vec [ 1 ] . data ( ) ) ;
}
string SdpBandwidth : : toString ( ) const {
if ( value . empty ( ) ) {
value = bwtype + " : " + to_string ( bandwidth ) ;
}
return SdpItem : : toString ( ) ;
}
void SdpMedia : : parse ( const string & str ) {
auto vec = split ( str , " " ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( vec . size ( ) > = 4 ) ;
2021-03-28 17:32:53 +08:00
type = getTrackType ( vec [ 0 ] ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( type ! = TrackInvalid ) ;
2021-03-28 17:32:53 +08:00
port = atoi ( vec [ 1 ] . data ( ) ) ;
proto = vec [ 2 ] ;
2021-03-29 09:53:54 +08:00
for ( size_t i = 3 ; i < vec . size ( ) ; + + i ) {
2022-04-03 17:12:23 +08:00
fmts . emplace_back ( vec [ i ] ) ;
2021-03-28 17:32:53 +08:00
}
}
string SdpMedia : : toString ( ) const {
if ( value . empty ( ) ) {
value = string ( getTrackString ( type ) ) + " " + to_string ( port ) + " " + proto ;
for ( auto fmt : fmts ) {
value + = ' ' ;
2022-04-03 17:12:23 +08:00
value + = fmt ;
2021-03-28 17:32:53 +08:00
}
}
return SdpItem : : toString ( ) ;
2021-03-28 09:49:34 +08:00
}
void SdpAttr : : parse ( const string & str ) {
auto pos = str . find ( ' : ' ) ;
auto key = pos = = string : : npos ? str : str . substr ( 0 , pos ) ;
auto value = pos = = string : : npos ? string ( ) : str . substr ( pos + 1 ) ;
auto it = sdpItemCreator . find ( key ) ;
if ( it ! = sdpItemCreator . end ( ) ) {
detail = it - > second ( key , value ) ;
} else {
detail = std : : make_shared < SdpCommon > ( key ) ;
detail - > parse ( value ) ;
}
}
2021-03-28 17:32:53 +08:00
string SdpAttr : : toString ( ) const {
if ( value . empty ( ) ) {
auto detail_value = detail - > toString ( ) ;
if ( detail_value . empty ( ) ) {
value = detail - > getKey ( ) ;
} else {
value = string ( detail - > getKey ( ) ) + " : " + detail_value ;
}
}
return SdpItem : : toString ( ) ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrGroup : : parse ( const string & str ) {
2021-03-28 17:32:53 +08:00
auto vec = split ( str , " " ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( vec . size ( ) > = 2 ) ;
2021-03-28 17:32:53 +08:00
type = vec [ 0 ] ;
vec . erase ( vec . begin ( ) ) ;
mids = std : : move ( vec ) ;
}
2024-03-23 23:08:10 +08:00
string SdpAttrGroup : : toString ( ) const {
2021-03-28 17:32:53 +08:00
if ( value . empty ( ) ) {
value = type ;
for ( auto mid : mids ) {
value + = ' ' ;
value + = mid ;
}
}
return SdpItem : : toString ( ) ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrMsidSemantic : : parse ( const string & str ) {
2021-03-28 17:32:53 +08:00
auto vec = split ( str , " " ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( vec . size ( ) > = 1 ) ;
2021-03-28 17:32:53 +08:00
msid = vec [ 0 ] ;
token = vec . size ( ) > 1 ? vec [ 1 ] : " " ;
}
2024-03-23 23:08:10 +08:00
string SdpAttrMsidSemantic : : toString ( ) const {
2021-03-28 17:32:53 +08:00
if ( value . empty ( ) ) {
if ( token . empty ( ) ) {
value = string ( " " ) + msid ;
} else {
value = string ( " " ) + msid + " " + token ;
}
}
return SdpItem : : toString ( ) ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrRtcp : : parse ( const string & str ) {
2021-03-28 17:32:53 +08:00
auto vec = split ( str , " " ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( vec . size ( ) = = 4 ) ;
2021-03-28 17:32:53 +08:00
port = atoi ( vec [ 0 ] . data ( ) ) ;
nettype = vec [ 1 ] ;
addrtype = vec [ 2 ] ;
address = vec [ 3 ] ;
}
2024-03-23 23:08:10 +08:00
string SdpAttrRtcp : : toString ( ) const {
2021-03-28 17:32:53 +08:00
if ( value . empty ( ) ) {
value = to_string ( port ) + " " + nettype + " " + addrtype + " " + address ;
}
return SdpItem : : toString ( ) ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrIceOption : : parse ( const string & str ) {
2021-04-01 10:35:31 +08:00
auto vec = split ( str , " " ) ;
for ( auto & v : vec ) {
if ( ! strcasecmp ( v . data ( ) , " trickle " ) ) {
trickle = true ;
continue ;
}
if ( ! strcasecmp ( v . data ( ) , " renomination " ) ) {
renomination = true ;
continue ;
}
}
}
2024-03-23 23:08:10 +08:00
string SdpAttrIceOption : : toString ( ) const {
2021-04-01 10:35:31 +08:00
if ( value . empty ( ) ) {
if ( trickle & & renomination ) {
value = " trickle renomination " ;
} else if ( trickle ) {
value = " trickle " ;
} else if ( renomination ) {
value = " renomination " ;
}
}
return value ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrFingerprint : : parse ( const string & str ) {
2021-03-28 17:32:53 +08:00
auto vec = split ( str , " " ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( vec . size ( ) = = 2 ) ;
2021-03-28 17:32:53 +08:00
algorithm = vec [ 0 ] ;
hash = vec [ 1 ] ;
}
2024-03-23 23:08:10 +08:00
string SdpAttrFingerprint : : toString ( ) const {
2021-03-28 17:32:53 +08:00
if ( value . empty ( ) ) {
value = algorithm + " " + hash ;
}
return SdpItem : : toString ( ) ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrSetup : : parse ( const string & str ) {
2021-03-28 17:32:53 +08:00
role = getDtlsRole ( str ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( role ! = DtlsRole : : invalid ) ;
2021-03-28 17:32:53 +08:00
}
2024-03-23 23:08:10 +08:00
string SdpAttrSetup : : toString ( ) const {
2021-03-28 17:32:53 +08:00
if ( value . empty ( ) ) {
value = getDtlsRoleString ( role ) ;
}
return SdpItem : : toString ( ) ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrExtmap : : parse ( const string & str ) {
char buf [ 128 ] = { 0 } ;
char direction_buf [ 32 ] = { 0 } ;
2021-05-07 14:08:43 +08:00
if ( sscanf ( str . data ( ) , " % " SCNd8 " /%31[^ ] %127s " , & id , direction_buf , buf ) ! = 3 ) {
2021-10-16 16:46:05 +08:00
CHECK_SDP ( sscanf ( str . data ( ) , " % " SCNd8 " %127s " , & id , buf ) = = 2 ) ;
2021-03-30 11:51:39 +08:00
direction = RtpDirection : : sendrecv ;
2021-03-29 00:12:20 +08:00
} else {
direction = getRtpDirection ( direction_buf ) ;
2021-03-28 17:32:53 +08:00
}
ext = buf ;
}
2024-03-23 23:08:10 +08:00
string SdpAttrExtmap : : toString ( ) const {
2021-03-28 17:32:53 +08:00
if ( value . empty ( ) ) {
2024-03-23 23:08:10 +08:00
if ( direction = = RtpDirection : : invalid | | direction = = RtpDirection : : sendrecv ) {
2021-05-07 14:08:43 +08:00
value = to_string ( ( int ) id ) + " " + ext ;
2021-03-29 00:12:20 +08:00
} else {
2024-03-23 23:08:10 +08:00
value = to_string ( ( int ) id ) + " / " + getRtpDirectionString ( direction ) + " " + ext ;
2021-03-29 00:12:20 +08:00
}
2021-03-28 17:32:53 +08:00
}
return SdpItem : : toString ( ) ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrRtpMap : : parse ( const string & str ) {
char buf [ 32 ] = { 0 } ;
2021-03-29 09:53:54 +08:00
if ( sscanf ( str . data ( ) , " % " SCNu8 " %31[^/]/% " SCNd32 " /% " SCNd32 , & pt , buf , & sample_rate , & channel ) ! = 4 ) {
2021-10-16 16:46:05 +08:00
CHECK_SDP ( sscanf ( str . data ( ) , " % " SCNu8 " %31[^/]/% " SCNd32 , & pt , buf , & sample_rate ) = = 3 ) ;
2021-04-05 11:07:41 +08:00
if ( getTrackType ( getCodecId ( buf ) ) = = TrackAudio ) {
2024-09-19 14:53:50 +08:00
// 未指定通道数时, 且为音频时, 那么通道数默认为1 [AUTO-TRANSLATED:bd128fbd]
// If the number of channels is not specified and it is audio, the number of channels defaults to 1
2021-04-05 11:07:41 +08:00
channel = 1 ;
}
2021-03-28 17:32:53 +08:00
}
codec = buf ;
}
2024-03-23 23:08:10 +08:00
string SdpAttrRtpMap : : toString ( ) const {
2021-03-28 17:32:53 +08:00
if ( value . empty ( ) ) {
2022-08-18 19:13:22 +08:00
value = to_string ( ( int ) pt ) + " " + codec + " / " + to_string ( sample_rate ) ;
2021-03-28 17:32:53 +08:00
if ( channel ) {
value + = ' / ' ;
value + = to_string ( channel ) ;
}
}
return SdpItem : : toString ( ) ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrRtcpFb : : parse ( const string & str_in ) {
2021-03-29 23:03:55 +08:00
auto str = str_in + " \n " ;
2024-03-23 23:08:10 +08:00
char rtcp_type_buf [ 32 ] = { 0 } ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( sscanf ( str . data ( ) , " % " SCNu8 " %31[^ \n ] " , & pt , rtcp_type_buf ) = = 2 ) ;
2021-03-29 23:03:55 +08:00
rtcp_type = rtcp_type_buf ;
2021-03-28 17:32:53 +08:00
}
2024-03-23 23:08:10 +08:00
string SdpAttrRtcpFb : : toString ( ) const {
2021-03-28 17:32:53 +08:00
if ( value . empty ( ) ) {
2022-08-18 19:13:22 +08:00
value = to_string ( ( int ) pt ) + " " + rtcp_type ;
2021-03-28 17:32:53 +08:00
}
return SdpItem : : toString ( ) ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrFmtp : : parse ( const string & str ) {
2021-03-28 17:32:53 +08:00
auto pos = str . find ( ' ' ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( pos ! = string : : npos ) ;
2021-03-28 17:32:53 +08:00
pt = atoi ( str . substr ( 0 , pos ) . data ( ) ) ;
auto vec = split ( str . substr ( pos + 1 ) , " ; " ) ;
for ( auto & item : vec ) {
trim ( item ) ;
2021-04-02 18:28:01 +08:00
auto pos = item . find ( ' = ' ) ;
2024-03-23 23:08:10 +08:00
if ( pos = = string : : npos ) {
2021-04-15 19:35:25 +08:00
fmtp . emplace ( std : : make_pair ( item , " " ) ) ;
2024-03-23 23:08:10 +08:00
} else {
2021-04-15 19:35:25 +08:00
fmtp . emplace ( std : : make_pair ( item . substr ( 0 , pos ) , item . substr ( pos + 1 ) ) ) ;
2021-03-28 17:32:53 +08:00
}
}
2021-10-16 16:46:05 +08:00
CHECK_SDP ( ! fmtp . empty ( ) ) ;
2021-03-28 17:32:53 +08:00
}
2024-03-23 23:08:10 +08:00
string SdpAttrFmtp : : toString ( ) const {
2021-03-28 17:32:53 +08:00
if ( value . empty ( ) ) {
2022-08-18 19:13:22 +08:00
value = to_string ( ( int ) pt ) ;
2021-03-28 17:32:53 +08:00
int i = 0 ;
2021-04-15 19:35:25 +08:00
for ( auto & pr : fmtp ) {
2024-03-23 23:08:10 +08:00
value + = ( i + + ? ' ; ' : ' ' ) ;
2021-03-28 17:32:53 +08:00
value + = pr . first + " = " + pr . second ;
}
}
return SdpItem : : toString ( ) ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrSSRC : : parse ( const string & str_in ) {
2021-03-29 09:53:54 +08:00
auto str = str_in + ' \n ' ;
2024-03-23 23:08:10 +08:00
char attr_buf [ 32 ] = { 0 } ;
char attr_val_buf [ 128 ] = { 0 } ;
2021-03-29 09:53:54 +08:00
if ( 3 = = sscanf ( str . data ( ) , " % " SCNu32 " %31[^:]:%127[^ \n ] " , & ssrc , attr_buf , attr_val_buf ) ) {
2021-03-28 17:32:53 +08:00
attribute = attr_buf ;
attribute_value = attr_val_buf ;
2021-03-29 09:53:54 +08:00
} else if ( 2 = = sscanf ( str . data ( ) , " % " SCNu32 " %31s[^ \n ] " , & ssrc , attr_buf ) ) {
2021-03-28 17:32:53 +08:00
attribute = attr_buf ;
} else {
2021-10-16 16:46:05 +08:00
CHECK_SDP ( 0 ) ;
2021-03-28 17:32:53 +08:00
}
}
2024-03-23 23:08:10 +08:00
string SdpAttrSSRC : : toString ( ) const {
2021-03-28 17:32:53 +08:00
if ( value . empty ( ) ) {
value = to_string ( ssrc ) + ' ' ;
value + = attribute ;
if ( ! attribute_value . empty ( ) ) {
value + = ' : ' ;
value + = attribute_value ;
}
}
return SdpItem : : toString ( ) ;
}
2021-03-28 23:31:21 +08:00
void SdpAttrSSRCGroup : : parse ( const string & str ) {
auto vec = split ( str , " " ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( vec . size ( ) > = 3 ) ;
type = std : : move ( vec [ 0 ] ) ;
CHECK ( isFID ( ) | | isSIM ( ) ) ;
vec . erase ( vec . begin ( ) ) ;
for ( auto ssrc : vec ) {
2024-03-23 23:08:10 +08:00
ssrcs . emplace_back ( ( uint32_t ) atoll ( ssrc . data ( ) ) ) ;
2021-03-28 23:31:21 +08:00
}
}
2024-03-23 23:08:10 +08:00
string SdpAttrSSRCGroup : : toString ( ) const {
2021-03-28 23:31:21 +08:00
if ( value . empty ( ) ) {
2021-05-08 18:39:35 +08:00
value = type ;
2024-09-19 14:53:50 +08:00
// 最少要求2个ssrc [AUTO-TRANSLATED:968acb83]
// At least 2 SSRCs are required
2021-05-08 18:39:35 +08:00
CHECK ( ssrcs . size ( ) > = 2 ) ;
for ( auto & ssrc : ssrcs ) {
value + = ' ' ;
value + = to_string ( ssrc ) ;
2021-03-28 23:31:21 +08:00
}
}
return SdpItem : : toString ( ) ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrSctpMap : : parse ( const string & str ) {
char subtypes_buf [ 64 ] = { 0 } ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( 3 = = sscanf ( str . data ( ) , " % " SCNu16 " %63[^ ] % " SCNd32 , & port , subtypes_buf , & streams ) ) ;
subtypes = subtypes_buf ;
2021-03-28 17:32:53 +08:00
}
2024-03-23 23:08:10 +08:00
string SdpAttrSctpMap : : toString ( ) const {
2021-03-28 17:32:53 +08:00
if ( value . empty ( ) ) {
value = to_string ( port ) ;
value + = ' ' ;
value + = subtypes ;
value + = ' ' ;
value + = to_string ( streams ) ;
}
return SdpItem : : toString ( ) ;
}
2024-03-23 23:08:10 +08:00
void SdpAttrCandidate : : parse ( const string & str ) {
char foundation_buf [ 40 ] = { 0 } ;
char transport_buf [ 16 ] = { 0 } ;
char address_buf [ 64 ] = { 0 } ;
char type_buf [ 16 ] = { 0 } ;
2021-03-28 17:32:53 +08:00
2021-06-18 13:36:25 +08:00
// https://datatracker.ietf.org/doc/html/rfc5245#section-15.1
2023-03-13 17:38:08 +08:00
CHECK_SDP ( sscanf ( str . data ( ) , " %32[^ ] % " SCNu32 " %15[^ ] % " SCNu32 " %63[^ ] % " SCNu16 " typ %15[^ ] " ,
2021-10-16 16:46:05 +08:00
foundation_buf , & component , transport_buf , & priority , address_buf , & port , type_buf ) = = 7 ) ;
2021-03-29 12:28:47 +08:00
foundation = foundation_buf ;
2021-03-28 17:32:53 +08:00
transport = transport_buf ;
address = address_buf ;
type = type_buf ;
auto pos = str . find ( type ) ;
if ( pos ! = string : : npos ) {
auto remain = str . substr ( pos + type . size ( ) ) ;
trim ( remain ) ;
if ( ! remain . empty ( ) ) {
auto vec = split ( remain , " " ) ;
string key ;
for ( auto & item : vec ) {
if ( key . empty ( ) ) {
key = item ;
} else {
arr . emplace_back ( std : : make_pair ( std : : move ( key ) , std : : move ( item ) ) ) ;
}
}
}
}
}
2024-03-23 23:08:10 +08:00
string SdpAttrCandidate : : toString ( ) const {
2021-03-28 17:32:53 +08:00
if ( value . empty ( ) ) {
2024-03-23 23:08:10 +08:00
value = foundation + " " + to_string ( component ) + " " + transport + " " + to_string ( priority ) + " " + address + " " + to_string ( port ) + " typ " + type ;
2021-03-28 17:32:53 +08:00
for ( auto & pr : arr ) {
value + = ' ' ;
value + = pr . first ;
value + = ' ' ;
value + = pr . second ;
}
}
return SdpItem : : toString ( ) ;
}
2021-05-08 18:39:35 +08:00
void SdpAttrSimulcast : : parse ( const string & str ) {
2024-03-23 23:08:10 +08:00
// https://www.meetecho.com/blog/simulcast-janus-ssrc/
// a=simulcast:send/recv q;h;f
// a=simulcast:send/recv [rid=]q;h;f
// a=simulcast: recv h;m;l
2021-05-08 18:39:35 +08:00
//
auto vec = split ( str , " " ) ;
2021-10-16 16:46:05 +08:00
CHECK_SDP ( vec . size ( ) = = 2 ) ;
2021-05-08 18:39:35 +08:00
direction = vec [ 0 ] ;
2021-05-08 20:27:46 +08:00
rids = split ( vec [ 1 ] , " ; " ) ;
}
string SdpAttrSimulcast : : toString ( ) const {
if ( value . empty ( ) ) {
value = direction + " " ;
bool first = true ;
for ( auto & rid : rids ) {
if ( first ) {
first = false ;
} else {
value + = ' ; ' ;
}
value + = rid ;
2021-05-08 18:39:35 +08:00
}
}
2021-05-08 20:27:46 +08:00
return SdpItem : : toString ( ) ;
2021-05-08 18:39:35 +08:00
}
2021-05-08 20:27:46 +08:00
void SdpAttrRid : : parse ( const string & str ) {
auto vec = split ( str , " " ) ;
CHECK ( vec . size ( ) > = 2 ) ;
rid = vec [ 0 ] ;
direction = vec [ 1 ] ;
}
string SdpAttrRid : : toString ( ) const {
2021-05-08 18:39:35 +08:00
if ( value . empty ( ) ) {
2021-05-08 20:27:46 +08:00
value = rid + " " + direction ;
2021-05-08 18:39:35 +08:00
}
return SdpItem : : toString ( ) ;
}
2021-10-16 16:46:05 +08:00
void RtcSession : : loadFrom ( const string & str ) {
2021-03-29 12:00:42 +08:00
RtcSessionSdp sdp ;
sdp . parse ( str ) ;
version = sdp . getVersion ( ) ;
origin = sdp . getOrigin ( ) ;
session_name = sdp . getSessionName ( ) ;
session_info = sdp . getSessionInfo ( ) ;
connection = sdp . getConnection ( ) ;
2021-03-30 09:53:58 +08:00
time = sdp . getSessionTime ( ) ;
2021-03-29 23:03:55 +08:00
msid_semantic = sdp . getItemClass < SdpAttrMsidSemantic > ( ' a ' , " msid-semantic " ) ;
2021-03-29 12:00:42 +08:00
for ( auto & media : sdp . medias ) {
auto mline = media . getItemClass < SdpMedia > ( ' m ' ) ;
2021-03-29 23:03:55 +08:00
this - > media . emplace_back ( ) ;
auto & rtc_media = this - > media . back ( ) ;
2021-03-29 12:00:42 +08:00
rtc_media . mid = media . getStringItem ( ' a ' , " mid " ) ;
2021-03-29 18:31:56 +08:00
rtc_media . proto = mline . proto ;
rtc_media . type = mline . type ;
rtc_media . port = mline . port ;
2021-03-29 23:03:55 +08:00
rtc_media . addr = media . getItemClass < SdpConnection > ( ' c ' ) ;
2022-07-07 16:44:42 +08:00
rtc_media . bandwidth = media . getItemClass < SdpBandwidth > ( ' b ' ) ;
2021-03-29 23:03:55 +08:00
rtc_media . ice_ufrag = media . getStringItem ( ' a ' , " ice-ufrag " ) ;
rtc_media . ice_pwd = media . getStringItem ( ' a ' , " ice-pwd " ) ;
rtc_media . role = media . getItemClass < SdpAttrSetup > ( ' a ' , " setup " ) . role ;
2021-03-29 18:31:56 +08:00
rtc_media . fingerprint = media . getItemClass < SdpAttrFingerprint > ( ' a ' , " fingerprint " ) ;
2021-04-06 22:51:16 +08:00
if ( rtc_media . fingerprint . empty ( ) ) {
rtc_media . fingerprint = sdp . getItemClass < SdpAttrFingerprint > ( ' a ' , " fingerprint " ) ;
}
2021-03-29 23:03:55 +08:00
rtc_media . ice_lite = media . getItem ( ' a ' , " ice-lite " ) . operator bool ( ) ;
2021-04-01 10:35:31 +08:00
auto ice_options = media . getItemClass < SdpAttrIceOption > ( ' a ' , " ice-options " ) ;
rtc_media . ice_trickle = ice_options . trickle ;
rtc_media . ice_renomination = ice_options . renomination ;
2021-03-29 23:03:55 +08:00
rtc_media . candidate = media . getAllItem < SdpAttrCandidate > ( ' a ' , " candidate " ) ;
if ( mline . type = = TrackType : : TrackApplication ) {
rtc_media . sctp_port = atoi ( media . getStringItem ( ' a ' , " sctp-port " ) . data ( ) ) ;
rtc_media . sctpmap = media . getItemClass < SdpAttrSctpMap > ( ' a ' , " sctpmap " ) ;
continue ;
}
rtc_media . rtcp_addr = media . getItemClass < SdpAttrRtcp > ( ' a ' , " rtcp " ) ;
rtc_media . direction = media . getDirection ( ) ;
2021-05-10 10:14:42 +08:00
rtc_media . extmap = media . getAllItem < SdpAttrExtmap > ( ' a ' , " extmap " ) ;
2021-03-29 23:03:55 +08:00
rtc_media . rtcp_mux = media . getItem ( ' a ' , " rtcp-mux " ) . operator bool ( ) ;
rtc_media . rtcp_rsize = media . getItem ( ' a ' , " rtcp-rsize " ) . operator bool ( ) ;
map < uint32_t , RtcSSRC > rtc_ssrc_map ;
2021-05-08 20:27:46 +08:00
auto ssrc_attr = media . getAllItem < SdpAttrSSRC > ( ' a ' , " ssrc " ) ;
for ( auto & ssrc : ssrc_attr ) {
2021-03-29 23:03:55 +08:00
auto & rtc_ssrc = rtc_ssrc_map [ ssrc . ssrc ] ;
rtc_ssrc . ssrc = ssrc . ssrc ;
2021-03-30 09:58:53 +08:00
if ( ! strcasecmp ( ssrc . attribute . data ( ) , " cname " ) ) {
2021-03-29 23:03:55 +08:00
rtc_ssrc . cname = ssrc . attribute_value ;
continue ;
}
2021-03-30 09:58:53 +08:00
if ( ! strcasecmp ( ssrc . attribute . data ( ) , " msid " ) ) {
2021-03-29 23:03:55 +08:00
rtc_ssrc . msid = ssrc . attribute_value ;
continue ;
}
2021-03-30 09:58:53 +08:00
if ( ! strcasecmp ( ssrc . attribute . data ( ) , " mslabel " ) ) {
2021-03-29 23:03:55 +08:00
rtc_ssrc . mslabel = ssrc . attribute_value ;
continue ;
}
2021-03-30 09:58:53 +08:00
if ( ! strcasecmp ( ssrc . attribute . data ( ) , " label " ) ) {
2021-03-29 23:03:55 +08:00
rtc_ssrc . label = ssrc . attribute_value ;
continue ;
}
}
auto ssrc_groups = media . getAllItem < SdpAttrSSRCGroup > ( ' a ' , " ssrc-group " ) ;
2021-10-12 21:26:01 +08:00
bool have_rtx_ssrc = false ;
2021-05-08 18:39:35 +08:00
SdpAttrSSRCGroup * ssrc_group_sim = nullptr ;
2021-03-29 23:03:55 +08:00
for ( auto & group : ssrc_groups ) {
if ( group . isFID ( ) ) {
2021-10-12 21:26:01 +08:00
have_rtx_ssrc = true ;
2024-09-19 14:53:50 +08:00
// ssrc-group:FID字段必须包含rtp/rtx的ssrc [AUTO-TRANSLATED:3da97d7d]
// The ssrc-group:FID field must contain the SSRCs of rtp/rtx
2021-10-12 21:26:01 +08:00
CHECK ( group . ssrcs . size ( ) = = 2 ) ;
2024-09-19 14:53:50 +08:00
// 根据rtp ssrc找到对象 [AUTO-TRANSLATED:c0a56b42]
// Find the object based on the RTP SSRC
2021-10-12 21:26:01 +08:00
auto it = rtc_ssrc_map . find ( group . ssrcs [ 0 ] ) ;
CHECK ( it ! = rtc_ssrc_map . end ( ) ) ;
2024-09-19 14:53:50 +08:00
// 设置rtx ssrc [AUTO-TRANSLATED:422e2a55]
// Set the RTX SSRC
2021-10-12 21:26:01 +08:00
it - > second . rtx_ssrc = group . ssrcs [ 1 ] ;
rtc_media . rtp_rtx_ssrc . emplace_back ( it - > second ) ;
2021-03-29 23:03:55 +08:00
} else if ( group . isSIM ( ) ) {
2021-10-12 21:26:01 +08:00
CHECK ( ! ssrc_group_sim ) ;
2021-05-08 18:39:35 +08:00
ssrc_group_sim = & group ;
2021-03-29 23:03:55 +08:00
}
}
2021-10-12 21:26:01 +08:00
if ( ! have_rtx_ssrc ) {
2024-09-19 14:53:50 +08:00
// 按照sdp顺序依次添加ssrc [AUTO-TRANSLATED:0996ba7e]
// Add SSRCs in the order of SDP
2021-10-12 21:26:01 +08:00
for ( auto & attr : ssrc_attr ) {
if ( attr . attribute = = " cname " ) {
rtc_media . rtp_rtx_ssrc . emplace_back ( rtc_ssrc_map [ attr . ssrc ] ) ;
2021-05-08 18:39:35 +08:00
}
2021-03-29 23:03:55 +08:00
}
2021-10-12 21:26:01 +08:00
}
auto simulcast = media . getItemClass < SdpAttrSimulcast > ( ' a ' , " simulcast " ) ;
if ( ! simulcast . empty ( ) ) {
// a=rid:h send
// a=rid:m send
// a=rid:l send
// a=simulcast:send h;m;l
2024-09-19 14:53:50 +08:00
// 风格的simulcast [AUTO-TRANSLATED:94ac2d55]
// Style of simulcast
2021-10-12 21:26:01 +08:00
unordered_set < string > rid_map ;
for ( auto & rid : simulcast . rids ) {
rid_map . emplace ( rid ) ;
2021-04-06 18:29:14 +08:00
}
2021-10-12 21:26:01 +08:00
for ( auto & rid : media . getAllItem < SdpAttrRid > ( ' a ' , " rid " ) ) {
CHECK ( rid . direction = = simulcast . direction ) ;
CHECK ( rid_map . find ( rid . rid ) ! = rid_map . end ( ) ) ;
}
2024-09-19 14:53:50 +08:00
// simulcast最少要求2种方案 [AUTO-TRANSLATED:31732a7a]
// Simulcast requires at least 2 schemes
2021-10-12 21:26:01 +08:00
CHECK ( simulcast . rids . size ( ) > = 2 ) ;
rtc_media . rtp_rids = simulcast . rids ;
2021-03-29 23:03:55 +08:00
}
2021-05-08 18:39:35 +08:00
if ( ssrc_group_sim ) {
2024-09-19 14:53:50 +08:00
// 指定了a=ssrc-group:SIM [AUTO-TRANSLATED:5732661e]
// a=ssrc-group:SIM is specified
2021-05-08 18:39:35 +08:00
for ( auto ssrc : ssrc_group_sim - > ssrcs ) {
auto it = rtc_ssrc_map . find ( ssrc ) ;
2021-10-12 21:26:01 +08:00
CHECK ( it ! = rtc_ssrc_map . end ( ) ) ;
2021-05-08 18:39:35 +08:00
rtc_media . rtp_ssrc_sim . emplace_back ( it - > second ) ;
2021-03-29 23:03:55 +08:00
}
2021-05-08 20:27:46 +08:00
} else if ( ! rtc_media . rtp_rids . empty ( ) ) {
2024-09-19 14:53:50 +08:00
// 未指定a=ssrc-group:SIM, 但是指定了a=simulcast, 那么只能根据ssrc顺序来对应rid顺序 [AUTO-TRANSLATED:b198a817]
// a=ssrc-group:SIM is not specified, but a=simulcast is specified, so the RID order can only be matched according to the SSRC order
2021-10-12 21:26:01 +08:00
rtc_media . rtp_ssrc_sim = rtc_media . rtp_rtx_ssrc ;
}
if ( ! rtc_media . supportSimulcast ( ) ) {
2024-09-19 14:53:50 +08:00
// 不支持simulcast的情况下, 最多一组ssrc [AUTO-TRANSLATED:3ea8ed65]
// In the case of not supporting simulcast, there is at most one group of SSRCs
2021-10-12 21:26:01 +08:00
CHECK ( rtc_media . rtp_rtx_ssrc . size ( ) < = 1 ) ;
} else {
2024-09-19 14:53:50 +08:00
// simulcast的情况下, 要么没有指定ssrc, 要么指定的ssrc个数与rid个数一致 [AUTO-TRANSLATED:1d45ce03]
// In the case of simulcast, either no SSRC is specified or the number of specified SSRCs is consistent with the number of RIDs
2024-03-23 23:08:10 +08:00
// CHECK(rtc_media.rtp_ssrc_sim.empty() || rtc_media.rtp_ssrc_sim.size() == rtc_media.rtp_rids.size());
2021-03-29 23:03:55 +08:00
}
auto rtpmap_arr = media . getAllItem < SdpAttrRtpMap > ( ' a ' , " rtpmap " ) ;
auto rtcpfb_arr = media . getAllItem < SdpAttrRtcpFb > ( ' a ' , " rtcp-fb " ) ;
auto fmtp_aar = media . getAllItem < SdpAttrFmtp > ( ' a ' , " fmtp " ) ;
2024-09-19 14:53:50 +08:00
// 方便根据pt查找rtpmap,一个pt必有一条 [AUTO-TRANSLATED:c3673faa]
// Convenient to find rtpmap based on pt, one pt must have one
2021-03-29 23:03:55 +08:00
map < uint8_t , SdpAttrRtpMap & > rtpmap_map ;
2024-09-19 14:53:50 +08:00
// 方便根据pt查找rtcp-fb,一个pt可能有多条或0条 [AUTO-TRANSLATED:38361f68]
// Convenient to find rtcp-fb based on pt, one pt may have multiple or 0
2021-03-29 23:03:55 +08:00
multimap < uint8_t , SdpAttrRtcpFb & > rtcpfb_map ;
2024-09-19 14:53:50 +08:00
// 方便根据pt查找fmtp, 一个pt最多一条 [AUTO-TRANSLATED:be5d652d]
// Convenient to find fmtp based on pt, one pt has at most one
2021-03-29 23:03:55 +08:00
map < uint8_t , SdpAttrFmtp & > fmtp_map ;
for ( auto & rtpmap : rtpmap_arr ) {
2024-09-19 14:53:50 +08:00
// 添加失败,有多条 [AUTO-TRANSLATED:717782c0]
// Add failed, there are multiple
2021-10-16 16:46:05 +08:00
CHECK ( rtpmap_map . emplace ( rtpmap . pt , rtpmap ) . second , " 该pt存在多条a=rtpmap: " , ( int ) rtpmap . pt ) ;
2021-03-29 23:03:55 +08:00
}
for ( auto & rtpfb : rtcpfb_arr ) {
rtcpfb_map . emplace ( rtpfb . pt , rtpfb ) ;
}
for ( auto & fmtp : fmtp_aar ) {
2024-09-19 14:53:50 +08:00
// 添加失败,有多条 [AUTO-TRANSLATED:717782c0]
// Add failed, there are multiple
2021-10-16 16:46:05 +08:00
CHECK ( fmtp_map . emplace ( fmtp . pt , fmtp ) . second , " 该pt存在多条a=fmtp: " , ( int ) fmtp . pt ) ;
2021-03-29 23:03:55 +08:00
}
2022-04-03 17:12:23 +08:00
for ( auto & item : mline . fmts ) {
auto pt = atoi ( item . c_str ( ) ) ;
CHECK ( pt < 0xFF , " invalid payload type: " , item ) ;
2024-09-19 14:53:50 +08:00
// 遍历所有编码方案的pt [AUTO-TRANSLATED:40f2db36]
// Traverse the pt of all encoding schemes
2021-03-29 23:03:55 +08:00
rtc_media . plan . emplace_back ( ) ;
auto & plan = rtc_media . plan . back ( ) ;
auto rtpmap_it = rtpmap_map . find ( pt ) ;
if ( rtpmap_it = = rtpmap_map . end ( ) ) {
2021-04-02 18:28:01 +08:00
plan . pt = pt ;
2021-04-05 11:07:41 +08:00
plan . codec = RtpPayload : : getName ( pt ) ;
2021-04-02 18:28:01 +08:00
plan . sample_rate = RtpPayload : : getClockRate ( pt ) ;
plan . channel = RtpPayload : : getAudioChannel ( pt ) ;
} else {
plan . pt = rtpmap_it - > second . pt ;
plan . codec = rtpmap_it - > second . codec ;
plan . sample_rate = rtpmap_it - > second . sample_rate ;
plan . channel = rtpmap_it - > second . channel ;
2021-03-29 23:03:55 +08:00
}
2021-04-02 18:28:01 +08:00
2021-03-29 23:03:55 +08:00
auto fmtp_it = fmtp_map . find ( pt ) ;
if ( fmtp_it ! = fmtp_map . end ( ) ) {
2021-04-15 19:35:25 +08:00
plan . fmtp = fmtp_it - > second . fmtp ;
2021-03-29 23:03:55 +08:00
}
2024-03-23 23:08:10 +08:00
for ( auto rtpfb_it = rtcpfb_map . find ( pt ) ; rtpfb_it ! = rtcpfb_map . end ( ) & & rtpfb_it - > second . pt = = pt ; + + rtpfb_it ) {
2021-04-28 15:07:15 +08:00
plan . rtcp_fb . emplace ( rtpfb_it - > second . rtcp_type ) ;
2021-03-29 23:03:55 +08:00
}
}
2021-03-29 12:00:42 +08:00
}
2021-03-29 23:03:55 +08:00
group = sdp . getItemClass < SdpAttrGroup > ( ' a ' , " group " ) ;
2021-03-29 12:00:42 +08:00
}
2021-03-29 23:55:29 +08:00
2022-05-20 22:14:21 +08:00
void RtcSdpBase : : toRtsp ( ) {
2021-04-02 23:01:58 +08:00
for ( auto it = items . begin ( ) ; it ! = items . end ( ) ; ) {
switch ( ( * it ) - > getKey ( ) [ 0 ] ) {
case ' v ' :
case ' o ' :
case ' s ' :
case ' i ' :
case ' t ' :
case ' c ' :
2024-03-23 23:08:10 +08:00
case ' b ' : {
2021-04-02 23:01:58 +08:00
+ + it ;
break ;
}
case ' m ' : {
auto m = dynamic_pointer_cast < SdpMedia > ( * it ) ;
CHECK ( m ) ;
m - > proto = " RTP/AVP " ;
+ + it ;
break ;
}
case ' a ' : {
auto attr = dynamic_pointer_cast < SdpAttr > ( * it ) ;
CHECK ( attr ) ;
2024-03-23 23:08:10 +08:00
if ( ! strcasecmp ( attr - > detail - > getKey ( ) , " rtpmap " ) | | ! strcasecmp ( attr - > detail - > getKey ( ) , " fmtp " ) ) {
2021-04-02 23:01:58 +08:00
+ + it ;
break ;
}
}
default : {
it = items . erase ( it ) ;
break ;
}
}
}
}
2024-03-23 23:08:10 +08:00
string RtcSession : : toRtspSdp ( ) const {
2021-04-02 23:01:58 +08:00
RtcSession copy = * this ;
copy . media . clear ( ) ;
for ( auto & m : media ) {
switch ( m . type ) {
case TrackAudio :
case TrackVideo : {
2021-07-07 14:47:41 +08:00
if ( m . direction ! = RtpDirection : : inactive ) {
copy . media . emplace_back ( m ) ;
copy . media . back ( ) . plan . resize ( 1 ) ;
}
2021-04-02 23:01:58 +08:00
break ;
}
2021-07-07 14:47:41 +08:00
default : continue ;
2021-04-02 23:01:58 +08:00
}
}
2021-07-07 14:47:41 +08:00
CHECK ( ! copy . media . empty ( ) ) ;
2021-04-02 23:01:58 +08:00
auto sdp = copy . toRtcSessionSdp ( ) ;
2022-05-20 22:14:21 +08:00
sdp - > toRtsp ( ) ;
2021-04-02 23:01:58 +08:00
int i = 0 ;
for ( auto & m : sdp - > medias ) {
2022-05-20 22:14:21 +08:00
m . toRtsp ( ) ;
2022-05-18 09:56:53 +08:00
m . addAttr ( std : : make_shared < SdpCommon > ( " control " , string ( " trackID= " ) + to_string ( i + + ) ) ) ;
2021-04-02 23:01:58 +08:00
}
return sdp - > toString ( ) ;
}
2022-05-18 09:56:53 +08:00
void addSdpAttrSSRC ( const RtcSSRC & rtp_ssrc , RtcSdpBase & media , uint32_t ssrc_num ) {
2021-11-18 21:25:14 +08:00
assert ( ssrc_num ) ;
SdpAttrSSRC ssrc ;
ssrc . ssrc = ssrc_num ;
ssrc . attribute = " cname " ;
ssrc . attribute_value = rtp_ssrc . cname ;
2022-05-18 09:56:53 +08:00
media . addAttr ( std : : make_shared < SdpAttrSSRC > ( ssrc ) ) ;
2021-11-18 21:25:14 +08:00
if ( ! rtp_ssrc . msid . empty ( ) ) {
ssrc . attribute = " msid " ;
ssrc . attribute_value = rtp_ssrc . msid ;
2022-05-18 09:56:53 +08:00
media . addAttr ( std : : make_shared < SdpAttrSSRC > ( ssrc ) ) ;
2021-11-18 21:25:14 +08:00
}
if ( ! rtp_ssrc . mslabel . empty ( ) ) {
ssrc . attribute = " mslabel " ;
ssrc . attribute_value = rtp_ssrc . mslabel ;
2022-05-18 09:56:53 +08:00
media . addAttr ( std : : make_shared < SdpAttrSSRC > ( ssrc ) ) ;
2021-11-18 21:25:14 +08:00
}
if ( ! rtp_ssrc . label . empty ( ) ) {
ssrc . attribute = " label " ;
ssrc . attribute_value = rtp_ssrc . label ;
2022-05-18 09:56:53 +08:00
media . addAttr ( std : : make_shared < SdpAttrSSRC > ( ssrc ) ) ;
2021-11-18 21:25:14 +08:00
}
}
2021-11-18 20:55:18 +08:00
2024-03-23 23:08:10 +08:00
RtcSessionSdp : : Ptr RtcSession : : toRtcSessionSdp ( ) const {
2021-04-02 23:01:58 +08:00
RtcSessionSdp : : Ptr ret = std : : make_shared < RtcSessionSdp > ( ) ;
auto & sdp = * ret ;
2024-03-23 23:08:10 +08:00
sdp . addItem ( std : : make_shared < SdpString < ' v ' > > ( to_string ( version ) ) ) ;
2022-05-18 09:56:53 +08:00
sdp . addItem ( std : : make_shared < SdpOrigin > ( origin ) ) ;
2024-03-23 23:08:10 +08:00
sdp . addItem ( std : : make_shared < SdpString < ' s ' > > ( session_name ) ) ;
2021-03-30 13:09:02 +08:00
if ( ! session_info . empty ( ) ) {
2024-03-23 23:08:10 +08:00
sdp . addItem ( std : : make_shared < SdpString < ' i ' > > ( session_info ) ) ;
2021-03-30 13:09:02 +08:00
}
2022-05-18 09:56:53 +08:00
sdp . addItem ( std : : make_shared < SdpTime > ( time ) ) ;
2024-03-23 23:08:10 +08:00
if ( connection . empty ( ) ) {
2022-05-18 09:56:53 +08:00
sdp . addItem ( std : : make_shared < SdpConnection > ( connection ) ) ;
2021-04-01 11:02:09 +08:00
}
2022-05-18 09:56:53 +08:00
sdp . addAttr ( std : : make_shared < SdpAttrGroup > ( group ) ) ;
sdp . addAttr ( std : : make_shared < SdpAttrMsidSemantic > ( msid_semantic ) ) ;
2023-03-08 19:33:52 +08:00
bool ice_lite = false ;
2021-03-30 13:09:02 +08:00
for ( auto & m : media ) {
sdp . medias . emplace_back ( ) ;
auto & sdp_media = sdp . medias . back ( ) ;
auto mline = std : : make_shared < SdpMedia > ( ) ;
mline - > type = m . type ;
mline - > port = m . port ;
mline - > proto = m . proto ;
for ( auto & p : m . plan ) {
2022-08-18 19:13:22 +08:00
mline - > fmts . emplace_back ( to_string ( ( int ) p . pt ) ) ;
2021-03-30 13:09:02 +08:00
}
if ( m . type = = TrackApplication ) {
2022-04-03 17:12:23 +08:00
mline - > fmts . emplace_back ( " webrtc-datachannel " ) ;
2021-03-30 13:09:02 +08:00
}
2022-05-18 09:56:53 +08:00
sdp_media . addItem ( std : : move ( mline ) ) ;
sdp_media . addItem ( std : : make_shared < SdpConnection > ( m . addr ) ) ;
2022-07-07 16:44:42 +08:00
if ( ! m . bandwidth . empty ( ) & & m . type ! = TrackAudio ) {
sdp_media . addItem ( std : : make_shared < SdpBandwidth > ( m . bandwidth ) ) ;
}
2021-04-01 11:02:09 +08:00
if ( ! m . rtcp_addr . empty ( ) ) {
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : make_shared < SdpAttrRtcp > ( m . rtcp_addr ) ) ;
2021-04-01 11:02:09 +08:00
}
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : make_shared < SdpAttrIceUfrag > ( m . ice_ufrag ) ) ;
sdp_media . addAttr ( std : : make_shared < SdpAttrIcePwd > ( m . ice_pwd ) ) ;
2021-04-01 10:35:31 +08:00
if ( m . ice_trickle | | m . ice_renomination ) {
auto attr = std : : make_shared < SdpAttrIceOption > ( ) ;
attr - > trickle = m . ice_trickle ;
attr - > renomination = m . ice_renomination ;
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( attr ) ;
2021-03-30 13:09:02 +08:00
}
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : make_shared < SdpAttrFingerprint > ( m . fingerprint ) ) ;
sdp_media . addAttr ( std : : make_shared < SdpAttrSetup > ( m . role ) ) ;
sdp_media . addAttr ( std : : make_shared < SdpAttrMid > ( m . mid ) ) ;
2021-03-30 13:09:02 +08:00
if ( m . ice_lite ) {
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : make_shared < SdpCommon > ( " ice-lite " ) ) ;
2023-03-08 19:33:52 +08:00
ice_lite = true ;
2021-03-30 13:09:02 +08:00
}
2021-05-10 10:14:42 +08:00
for ( auto & ext : m . extmap ) {
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : make_shared < SdpAttrExtmap > ( ext ) ) ;
2021-03-30 13:09:02 +08:00
}
if ( m . direction ! = RtpDirection : : invalid ) {
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : make_shared < DirectionInterfaceImp > ( m . direction ) ) ;
2021-03-30 13:09:02 +08:00
}
if ( m . rtcp_mux ) {
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : make_shared < SdpCommon > ( " rtcp-mux " ) ) ;
2021-03-30 13:09:02 +08:00
}
if ( m . rtcp_rsize ) {
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : make_shared < SdpCommon > ( " rtcp-rsize " ) ) ;
2021-03-30 13:09:02 +08:00
}
2024-03-23 23:08:10 +08:00
if ( m . type ! = TrackApplication ) {
2021-03-30 13:09:02 +08:00
for ( auto & p : m . plan ) {
auto rtp_map = std : : make_shared < SdpAttrRtpMap > ( ) ;
rtp_map - > pt = p . pt ;
rtp_map - > codec = p . codec ;
rtp_map - > sample_rate = p . sample_rate ;
rtp_map - > channel = p . channel ;
2024-09-19 14:53:50 +08:00
// 添加a=rtpmap [AUTO-TRANSLATED:8bef5d64]
// Add a=rtpmap
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : move ( rtp_map ) ) ;
2021-03-30 13:09:02 +08:00
2024-03-23 23:08:10 +08:00
for ( auto & fb : p . rtcp_fb ) {
2021-03-30 13:09:02 +08:00
auto rtcp_fb = std : : make_shared < SdpAttrRtcpFb > ( ) ;
rtcp_fb - > pt = p . pt ;
rtcp_fb - > rtcp_type = fb ;
2024-09-19 14:53:50 +08:00
// 添加a=rtcp-fb [AUTO-TRANSLATED:11754b43]
// Add a=rtcp-fb
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : move ( rtcp_fb ) ) ;
2021-03-30 13:09:02 +08:00
}
if ( ! p . fmtp . empty ( ) ) {
auto fmtp = std : : make_shared < SdpAttrFmtp > ( ) ;
fmtp - > pt = p . pt ;
2021-04-15 19:35:25 +08:00
fmtp - > fmtp = p . fmtp ;
2024-09-19 14:53:50 +08:00
// 添加a=fmtp [AUTO-TRANSLATED:594a4425]
// Add a=fmtp
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : move ( fmtp ) ) ;
2021-03-30 13:09:02 +08:00
}
}
2021-05-08 18:39:35 +08:00
{
2024-09-19 14:53:50 +08:00
// 添加a=msid字段 [AUTO-TRANSLATED:cf2c1471]
// Add a=msid field
2021-05-08 18:39:35 +08:00
if ( ! m . rtp_rtx_ssrc . empty ( ) ) {
if ( ! m . rtp_rtx_ssrc [ 0 ] . msid . empty ( ) ) {
2022-03-12 19:29:58 +08:00
auto msid = std : : make_shared < SdpAttrMsid > ( ) ;
2021-05-08 18:39:35 +08:00
msid - > parse ( m . rtp_rtx_ssrc [ 0 ] . msid ) ;
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : move ( msid ) ) ;
2021-05-08 18:39:35 +08:00
}
2021-05-08 15:01:30 +08:00
}
2021-03-30 13:09:02 +08:00
}
2021-05-08 18:39:35 +08:00
{
for ( auto & ssrc : m . rtp_rtx_ssrc ) {
2024-09-19 14:53:50 +08:00
// 添加a=ssrc字段 [AUTO-TRANSLATED:75ca5225]
// Add a=ssrc field
2021-11-18 20:55:18 +08:00
CHECK ( ! ssrc . empty ( ) ) ;
2022-05-18 09:56:53 +08:00
addSdpAttrSSRC ( ssrc , sdp_media , ssrc . ssrc ) ;
2021-10-12 21:26:01 +08:00
if ( ssrc . rtx_ssrc ) {
2022-05-18 09:56:53 +08:00
addSdpAttrSSRC ( ssrc , sdp_media , ssrc . rtx_ssrc ) ;
2021-11-18 20:55:18 +08:00
2024-09-19 14:53:50 +08:00
// 生成a=ssrc-group:FID字段 [AUTO-TRANSLATED:22b1f966]
// Generate a=ssrc-group:FID field
// 有rtx ssrc [AUTO-TRANSLATED:fece8076]
// There is rtx ssrc
2021-10-12 21:26:01 +08:00
auto group = std : : make_shared < SdpAttrSSRCGroup > ( ) ;
group - > type = " FID " ;
group - > ssrcs . emplace_back ( ssrc . ssrc ) ;
group - > ssrcs . emplace_back ( ssrc . rtx_ssrc ) ;
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : move ( group ) ) ;
2021-10-12 21:26:01 +08:00
}
2021-05-08 18:39:35 +08:00
}
2021-03-30 13:09:02 +08:00
}
2021-05-08 18:39:35 +08:00
{
if ( m . rtp_ssrc_sim . size ( ) > = 2 ) {
2024-09-19 14:53:50 +08:00
// simulcast 要求 2~3路 [AUTO-TRANSLATED:3237ffca]
// Simulcast requires 2~3 channels
2021-05-08 18:39:35 +08:00
auto group = std : : make_shared < SdpAttrSSRCGroup > ( ) ;
2021-10-12 21:26:01 +08:00
for ( auto & ssrc : m . rtp_ssrc_sim ) {
2021-05-08 18:39:35 +08:00
group - > ssrcs . emplace_back ( ssrc . ssrc ) ;
}
2024-09-19 14:53:50 +08:00
// 添加a=ssrc-group:SIM字段 [AUTO-TRANSLATED:46b04aae]
// Add a=ssrc-group:SIM field
2021-05-08 18:39:35 +08:00
group - > type = " SIM " ;
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : move ( group ) ) ;
2021-05-08 18:39:35 +08:00
}
2021-05-08 20:27:46 +08:00
if ( m . rtp_rids . size ( ) > = 2 ) {
auto simulcast = std : : make_shared < SdpAttrSimulcast > ( ) ;
simulcast - > direction = " recv " ;
simulcast - > rids = m . rtp_rids ;
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : move ( simulcast ) ) ;
2021-05-08 20:27:46 +08:00
for ( auto & rid : m . rtp_rids ) {
auto attr_rid = std : : make_shared < SdpAttrRid > ( ) ;
attr_rid - > rid = rid ;
attr_rid - > direction = " recv " ;
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : move ( attr_rid ) ) ;
2021-05-08 20:27:46 +08:00
}
}
2021-03-30 13:09:02 +08:00
}
2021-05-08 20:27:46 +08:00
2021-03-30 13:09:02 +08:00
} else {
2022-04-03 17:12:23 +08:00
if ( ! m . sctpmap . empty ( ) ) {
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : make_shared < SdpAttrSctpMap > ( m . sctpmap ) ) ;
2022-04-03 17:12:23 +08:00
}
2022-05-18 09:56:53 +08:00
sdp_media . addAttr ( std : : make_shared < SdpCommon > ( " sctp-port " , to_string ( m . sctp_port ) ) ) ;
2021-03-30 13:09:02 +08:00
}
for ( auto & cand : m . candidate ) {
2022-11-19 09:46:04 +08:00
if ( cand . port ) {
2022-11-19 01:51:53 +08:00
sdp_media . addAttr ( std : : make_shared < SdpAttrCandidate > ( cand ) ) ;
}
2021-03-30 13:09:02 +08:00
}
}
2024-03-23 23:08:10 +08:00
if ( ice_lite ) {
2023-03-08 19:33:52 +08:00
sdp . addAttr ( std : : make_shared < SdpCommon > ( " ice-lite " ) ) ;
2024-03-23 23:08:10 +08:00
}
2021-04-02 23:01:58 +08:00
return ret ;
}
2024-03-23 23:08:10 +08:00
string RtcSession : : toString ( ) const {
2021-04-02 23:01:58 +08:00
return toRtcSessionSdp ( ) - > toString ( ) ;
2021-03-30 13:09:02 +08:00
}
2024-03-23 23:08:10 +08:00
string RtcCodecPlan : : getFmtp ( const char * key ) const {
2021-03-29 23:55:29 +08:00
for ( auto & item : fmtp ) {
2021-03-30 09:53:58 +08:00
if ( strcasecmp ( item . first . data ( ) , key ) = = 0 ) {
2021-03-29 23:55:29 +08:00
return item . second ;
}
}
return " " ;
}
2024-03-23 23:08:10 +08:00
const RtcCodecPlan * RtcMedia : : getPlan ( uint8_t pt ) const {
2021-03-29 23:55:29 +08:00
for ( auto & item : plan ) {
if ( item . pt = = pt ) {
return & item ;
}
}
return nullptr ;
}
2024-03-23 23:08:10 +08:00
const RtcCodecPlan * RtcMedia : : getPlan ( const char * codec ) const {
2021-03-29 23:55:29 +08:00
for ( auto & item : plan ) {
2021-03-30 09:53:58 +08:00
if ( strcasecmp ( item . codec . data ( ) , codec ) = = 0 ) {
return & item ;
2021-03-29 23:55:29 +08:00
}
}
2021-03-30 09:53:58 +08:00
return nullptr ;
}
2024-03-23 23:08:10 +08:00
const RtcCodecPlan * RtcMedia : : getRelatedRtxPlan ( uint8_t pt ) const {
2021-03-30 10:59:15 +08:00
for ( auto & item : plan ) {
if ( strcasecmp ( item . codec . data ( ) , " rtx " ) = = 0 ) {
auto apt = atoi ( item . getFmtp ( " apt " ) . data ( ) ) ;
if ( pt = = apt ) {
return & item ;
}
}
}
return nullptr ;
}
2021-05-16 12:28:50 +08:00
uint32_t RtcMedia : : getRtpSSRC ( ) const {
if ( rtp_rtx_ssrc . size ( ) ) {
return rtp_rtx_ssrc [ 0 ] . ssrc ;
}
return 0 ;
}
uint32_t RtcMedia : : getRtxSSRC ( ) const {
2021-10-12 21:26:01 +08:00
if ( rtp_rtx_ssrc . size ( ) ) {
return rtp_rtx_ssrc [ 0 ] . rtx_ssrc ;
2021-05-16 12:28:50 +08:00
}
return 0 ;
}
2021-10-12 21:26:01 +08:00
bool RtcMedia : : supportSimulcast ( ) const {
if ( ! rtp_rids . empty ( ) ) {
return true ;
}
if ( ! rtp_ssrc_sim . empty ( ) ) {
return true ;
}
return false ;
}
2024-03-23 23:08:10 +08:00
void RtcMedia : : checkValid ( ) const {
2021-03-30 09:53:58 +08:00
CHECK ( type ! = TrackInvalid ) ;
CHECK ( ! mid . empty ( ) ) ;
CHECK ( ! proto . empty ( ) ) ;
CHECK ( direction ! = RtpDirection : : invalid | | type = = TrackApplication ) ;
2021-10-16 16:46:05 +08:00
CHECK ( ! plan . empty ( ) | | type = = TrackApplication ) ;
CHECK ( type = = TrackApplication | | rtcp_mux , " 只支持rtcp-mux模式 " ) ;
2021-05-10 17:35:22 +08:00
2021-03-30 09:53:58 +08:00
bool send_rtp = ( direction = = RtpDirection : : sendonly | | direction = = RtpDirection : : sendrecv ) ;
2022-05-16 12:54:50 +08:00
if ( ! supportSimulcast ( ) ) {
2024-09-19 14:53:50 +08:00
// 非simulcast时, 检查有没有指定rtp ssrc [AUTO-TRANSLATED:e2d53f8a]
// When not simulcast, check if the RTP SSRC is specified
2021-05-08 20:27:46 +08:00
CHECK ( ! rtp_rtx_ssrc . empty ( ) | | ! send_rtp ) ;
}
2021-05-08 19:45:53 +08:00
#if 0
2024-09-19 14:53:50 +08:00
// todo 发现Firefox(88.0)在mac平台下, 开启rtx后没有指定ssrc [AUTO-TRANSLATED:9112d91a]
// todo Found that Firefox (88.0) on the mac platform does not specify ssrc when rtx is enabled
2021-03-30 09:53:58 +08:00
auto rtx_plan = getPlan ( " rtx " ) ;
if ( rtx_plan ) {
2024-09-19 14:53:50 +08:00
// 开启rtx后必须指定rtx_ssrc [AUTO-TRANSLATED:c527f68d]
// RTX must be specified after rtx_ssrc is enabled
2021-05-08 18:39:35 +08:00
CHECK ( rtp_rtx_ssrc . size ( ) > = 2 | | ! send_rtp ) ;
2021-03-30 09:53:58 +08:00
}
2021-05-08 19:45:53 +08:00
# endif
2021-03-29 23:55:29 +08:00
}
2024-03-23 23:08:10 +08:00
void RtcSession : : checkValid ( ) const {
2021-03-30 09:53:58 +08:00
CHECK ( version = = 0 ) ;
CHECK ( ! origin . empty ( ) ) ;
CHECK ( ! session_name . empty ( ) ) ;
CHECK ( ! msid_semantic . empty ( ) ) ;
CHECK ( ! media . empty ( ) ) ;
2021-10-16 16:46:05 +08:00
CHECK ( ! group . mids . empty ( ) & & group . mids . size ( ) < = media . size ( ) , " 只支持group BUNDLE模式 " ) ;
2021-03-30 11:51:39 +08:00
2021-10-16 16:46:05 +08:00
bool have_active_media = false ;
2021-05-10 17:35:22 +08:00
for ( auto & item : media ) {
2021-10-16 16:46:05 +08:00
item . checkValid ( ) ;
2022-08-20 10:28:19 +08:00
if ( TrackApplication = = item . type ) {
have_active_media = true ;
}
2021-10-16 16:46:05 +08:00
switch ( item . direction ) {
case RtpDirection : : sendrecv :
case RtpDirection : : sendonly :
case RtpDirection : : recvonly : have_active_media = true ; break ;
2024-03-23 23:08:10 +08:00
default : break ;
2021-10-16 16:46:05 +08:00
}
2021-05-10 17:35:22 +08:00
}
2021-10-16 16:46:05 +08:00
CHECK ( have_active_media , " 必须确保最少有一个活跃的track " ) ;
2021-05-10 17:35:22 +08:00
}
2024-03-23 23:08:10 +08:00
const RtcMedia * RtcSession : : getMedia ( TrackType type ) const {
for ( auto & m : media ) {
if ( m . type = = type ) {
2021-04-01 10:09:50 +08:00
return & m ;
}
}
return nullptr ;
}
2021-04-28 15:41:36 +08:00
bool RtcSession : : supportRtcpFb ( const string & name , TrackType type ) const {
auto media = getMedia ( type ) ;
if ( ! media ) {
return false ;
}
auto & ref = media - > plan [ 0 ] . rtcp_fb ;
return ref . find ( name ) ! = ref . end ( ) ;
}
2021-07-07 14:47:41 +08:00
bool RtcSession : : supportSimulcast ( ) const {
for ( auto & m : media ) {
2021-10-12 21:26:01 +08:00
if ( m . supportSimulcast ( ) ) {
2021-07-07 14:47:41 +08:00
return true ;
}
}
return false ;
}
2021-10-12 21:26:01 +08:00
2022-08-20 10:28:19 +08:00
bool RtcSession : : isOnlyDatachannel ( ) const {
return 1 = = media . size ( ) & & TrackApplication = = media [ 0 ] . type ;
}
2021-04-30 15:15:35 +08:00
string const SdpConst : : kTWCCRtcpFb = " transport-cc " ;
string const SdpConst : : kRembRtcpFb = " goog-remb " ;
2021-04-28 15:07:15 +08:00
2024-03-23 23:08:10 +08:00
void RtcConfigure : : RtcTrackConfigure : : enableTWCC ( bool enable ) {
2021-04-28 15:07:15 +08:00
if ( ! enable ) {
2021-04-30 15:15:35 +08:00
rtcp_fb . erase ( SdpConst : : kTWCCRtcpFb ) ;
2021-05-08 21:43:27 +08:00
extmap . erase ( RtpExtType : : transport_cc ) ;
2021-04-28 15:07:15 +08:00
} else {
2021-04-30 15:15:35 +08:00
rtcp_fb . emplace ( SdpConst : : kTWCCRtcpFb ) ;
2021-10-14 16:18:01 +08:00
extmap . emplace ( RtpExtType : : transport_cc , RtpDirection : : sendrecv ) ;
2021-04-28 15:07:15 +08:00
}
}
2024-03-23 23:08:10 +08:00
void RtcConfigure : : RtcTrackConfigure : : enableREMB ( bool enable ) {
2021-04-30 15:08:43 +08:00
if ( ! enable ) {
2021-04-30 15:15:35 +08:00
rtcp_fb . erase ( SdpConst : : kRembRtcpFb ) ;
2021-05-07 14:02:03 +08:00
extmap . erase ( RtpExtType : : abs_send_time ) ;
2021-04-30 15:08:43 +08:00
} else {
2021-04-30 15:15:35 +08:00
rtcp_fb . emplace ( SdpConst : : kRembRtcpFb ) ;
2021-10-14 16:18:01 +08:00
extmap . emplace ( RtpExtType : : abs_send_time , RtpDirection : : sendrecv ) ;
2021-04-30 15:08:43 +08:00
}
}
2024-03-23 23:08:10 +08:00
static vector < CodecId > toCodecArray ( const string & str ) {
2021-11-15 10:32:54 +08:00
vector < CodecId > ret ;
auto vec = split ( str , " , " ) ;
for ( auto & s : vec ) {
auto codec = getCodecId ( trim ( s ) ) ;
if ( codec ! = CodecInvalid ) {
ret . emplace_back ( codec ) ;
}
}
return ret ;
}
2024-03-23 23:08:10 +08:00
void RtcConfigure : : RtcTrackConfigure : : setDefaultSetting ( TrackType type ) {
2021-03-30 11:51:39 +08:00
rtcp_mux = true ;
2021-04-01 11:59:35 +08:00
rtcp_rsize = false ;
2021-03-30 11:51:39 +08:00
group_bundle = true ;
support_rtx = true ;
support_red = false ;
support_ulpfec = false ;
ice_lite = true ;
ice_trickle = true ;
ice_renomination = false ;
switch ( type ) {
2021-04-01 10:09:50 +08:00
case TrackAudio : {
2024-09-19 14:53:50 +08:00
// 此处调整偏好的编码格式优先级 [AUTO-TRANSLATED:b8719e66]
// Adjust the priority of preferred encoding formats here
2022-09-18 21:03:05 +08:00
GET_CONFIG_FUNC ( vector < CodecId > , s_preferred_codec , Rtc : : kPreferredCodecA , toCodecArray ) ;
2021-11-15 10:32:54 +08:00
CHECK ( ! s_preferred_codec . empty ( ) , " rtc音频偏好codec不能为空 " ) ;
preferred_codec = s_preferred_codec ;
2024-03-23 23:08:10 +08:00
rtcp_fb = { SdpConst : : kTWCCRtcpFb , SdpConst : : kRembRtcpFb } ;
extmap = { { RtpExtType : : ssrc_audio_level , RtpDirection : : sendrecv } ,
{ RtpExtType : : csrc_audio_level , RtpDirection : : sendrecv } ,
{ RtpExtType : : abs_send_time , RtpDirection : : sendrecv } ,
{ RtpExtType : : transport_cc , RtpDirection : : sendrecv } ,
2024-09-19 14:53:50 +08:00
// rtx重传rtp时, 忽略sdes_mid类型的rtp ext,实测发现Firefox在接收rtx时, 如果存在sdes_mid的ext,将导致无法播放 [AUTO-TRANSLATED:221df025]
// When rtx retransmits rtp, ignore the rtp ext of sdes_mid type. It is found that Firefox cannot play when receiving rtx if there is an ext of sdes_mid
2024-03-23 23:08:10 +08:00
//{RtpExtType::sdes_mid,RtpDirection::sendrecv},
{ RtpExtType : : sdes_rtp_stream_id , RtpDirection : : sendrecv } ,
{ RtpExtType : : sdes_repaired_rtp_stream_id , RtpDirection : : sendrecv } } ;
2021-03-30 11:51:39 +08:00
break ;
}
2021-04-01 10:09:50 +08:00
case TrackVideo : {
2024-09-19 14:53:50 +08:00
// 此处调整偏好的编码格式优先级 [AUTO-TRANSLATED:b8719e66]
// Adjust the priority of preferred encoding formats here
2022-09-18 21:03:05 +08:00
GET_CONFIG_FUNC ( vector < CodecId > , s_preferred_codec , Rtc : : kPreferredCodecV , toCodecArray ) ;
2021-11-15 10:32:54 +08:00
CHECK ( ! s_preferred_codec . empty ( ) , " rtc视频偏好codec不能为空 " ) ;
preferred_codec = s_preferred_codec ;
2024-03-23 23:08:10 +08:00
rtcp_fb = { SdpConst : : kTWCCRtcpFb , SdpConst : : kRembRtcpFb , " nack " , " ccm fir " , " nack pli " } ;
extmap = { { RtpExtType : : abs_send_time , RtpDirection : : sendrecv } ,
{ RtpExtType : : transport_cc , RtpDirection : : sendrecv } ,
2024-09-19 14:53:50 +08:00
// rtx重传rtp时, 忽略sdes_mid类型的rtp ext,实测发现Firefox在接收rtx时, 如果存在sdes_mid的ext,将导致无法播放 [AUTO-TRANSLATED:221df025]
// When rtx retransmits rtp, ignore the rtp ext of sdes_mid type. It is found that Firefox cannot play when receiving rtx if there is an ext of sdes_mid
2024-03-23 23:08:10 +08:00
//{RtpExtType::sdes_mid,RtpDirection::sendrecv},
{ RtpExtType : : sdes_rtp_stream_id , RtpDirection : : sendrecv } ,
{ RtpExtType : : sdes_repaired_rtp_stream_id , RtpDirection : : sendrecv } ,
{ RtpExtType : : video_timing , RtpDirection : : sendrecv } ,
{ RtpExtType : : color_space , RtpDirection : : sendrecv } ,
{ RtpExtType : : video_content_type , RtpDirection : : sendrecv } ,
{ RtpExtType : : playout_delay , RtpDirection : : sendrecv } ,
2024-09-19 14:53:50 +08:00
// 手机端推webrtc 会带有旋转角度, rtc协议能正常播放 其他协议拉流画面旋转 [AUTO-TRANSLATED:3f2f9e0e]
// Mobile push webrtc will have a rotation angle, rtc protocol can play normally, other protocols pull stream picture rotation
2024-03-23 23:08:10 +08:00
//{RtpExtType::video_orientation, RtpDirection::sendrecv},
{ RtpExtType : : toffset , RtpDirection : : sendrecv } ,
{ RtpExtType : : framemarking , RtpDirection : : sendrecv } } ;
2021-03-30 11:51:39 +08:00
break ;
}
case TrackApplication : {
break ;
}
default : break ;
}
}
2024-03-23 23:08:10 +08:00
void RtcConfigure : : setDefaultSetting ( string ice_ufrag , string ice_pwd , RtpDirection direction , const SdpAttrFingerprint & fingerprint ) {
2021-03-30 11:51:39 +08:00
video . setDefaultSetting ( TrackVideo ) ;
audio . setDefaultSetting ( TrackAudio ) ;
application . setDefaultSetting ( TrackApplication ) ;
2021-11-18 21:36:10 +08:00
video . ice_ufrag = audio . ice_ufrag = application . ice_ufrag = std : : move ( ice_ufrag ) ;
video . ice_pwd = audio . ice_pwd = application . ice_pwd = std : : move ( ice_pwd ) ;
2021-03-30 11:51:39 +08:00
video . direction = audio . direction = application . direction = direction ;
video . fingerprint = audio . fingerprint = application . fingerprint = fingerprint ;
}
void RtcConfigure : : addCandidate ( const SdpAttrCandidate & candidate , TrackType type ) {
switch ( type ) {
case TrackAudio : {
audio . candidate . emplace_back ( candidate ) ;
break ;
}
case TrackVideo : {
video . candidate . emplace_back ( candidate ) ;
break ;
}
case TrackApplication : {
application . candidate . emplace_back ( candidate ) ;
break ;
}
default : {
2021-03-30 13:53:45 +08:00
if ( audio . group_bundle ) {
audio . candidate . emplace_back ( candidate ) ;
}
if ( video . group_bundle ) {
video . candidate . emplace_back ( candidate ) ;
}
if ( application . group_bundle ) {
application . candidate . emplace_back ( candidate ) ;
}
2021-03-30 11:51:39 +08:00
break ;
}
}
2021-03-30 18:34:17 +08:00
}
2024-03-23 23:08:10 +08:00
void RtcConfigure : : enableTWCC ( bool enable , TrackType type ) {
2021-04-28 15:07:15 +08:00
switch ( type ) {
case TrackAudio : {
audio . enableTWCC ( enable ) ;
break ;
}
case TrackVideo : {
video . enableTWCC ( enable ) ;
break ;
}
default : {
audio . enableTWCC ( enable ) ;
video . enableTWCC ( enable ) ;
break ;
}
}
}
2024-03-23 23:08:10 +08:00
void RtcConfigure : : enableREMB ( bool enable , TrackType type ) {
2021-05-06 10:40:21 +08:00
switch ( type ) {
case TrackAudio : {
audio . enableREMB ( enable ) ;
break ;
}
case TrackVideo : {
video . enableREMB ( enable ) ;
break ;
}
default : {
audio . enableREMB ( enable ) ;
video . enableREMB ( enable ) ;
break ;
}
}
}
2022-06-11 12:26:31 +08:00
shared_ptr < RtcSession > RtcConfigure : : createAnswer ( const RtcSession & offer ) const {
2021-03-31 18:32:43 +08:00
shared_ptr < RtcSession > ret = std : : make_shared < RtcSession > ( ) ;
ret - > version = offer . version ;
2021-04-01 11:02:09 +08:00
ret - > origin = offer . origin ;
ret - > session_name = offer . session_name ;
ret - > msid_semantic = offer . msid_semantic ;
2022-06-11 12:26:31 +08:00
for ( auto & m : offer . media ) {
matchMedia ( ret , m ) ;
}
2024-09-19 14:53:50 +08:00
// 设置音视频端口复用 [AUTO-TRANSLATED:ffe27d17]
// Set audio and video port multiplexing
2021-04-02 17:08:11 +08:00
if ( ! offer . group . mids . empty ( ) ) {
for ( auto & m : ret - > media ) {
2024-03-23 23:08:10 +08:00
// The remote end has rejected (port 0) the m-section, so it should not be putting its mid in the group attribute.
2023-08-29 11:22:30 +08:00
if ( m . port ) {
ret - > group . mids . emplace_back ( m . mid ) ;
}
2021-04-02 17:08:11 +08:00
}
}
2021-03-31 18:32:43 +08:00
return ret ;
}
2024-03-23 23:08:10 +08:00
static RtpDirection matchDirection ( RtpDirection offer_direction , RtpDirection supported ) {
2021-10-14 16:18:01 +08:00
switch ( offer_direction ) {
2024-03-23 23:08:10 +08:00
case RtpDirection : : sendonly : {
2021-10-14 16:18:01 +08:00
if ( supported ! = RtpDirection : : recvonly & & supported ! = RtpDirection : : sendrecv ) {
2024-09-19 14:53:50 +08:00
// 我们不支持接收 [AUTO-TRANSLATED:e4ef4034]
// We do not support receiving
2021-10-14 16:18:01 +08:00
return RtpDirection : : inactive ;
}
return RtpDirection : : recvonly ;
}
2024-03-23 23:08:10 +08:00
case RtpDirection : : recvonly : {
2021-10-14 16:18:01 +08:00
if ( supported ! = RtpDirection : : sendonly & & supported ! = RtpDirection : : sendrecv ) {
2024-09-19 14:53:50 +08:00
// 我们不支持发送 [AUTO-TRANSLATED:6505a226]
// We do not support sending
2021-10-14 16:18:01 +08:00
return RtpDirection : : inactive ;
}
return RtpDirection : : sendonly ;
}
2024-09-19 14:53:50 +08:00
// 对方支持发送接收,那么最终能力根据配置来决定 [AUTO-TRANSLATED:d234d603]
// The other party supports sending and receiving, so the final capability is determined by the configuration
2024-03-23 23:08:10 +08:00
case RtpDirection : : sendrecv : return ( supported = = RtpDirection : : invalid ? RtpDirection : : inactive : supported ) ;
case RtpDirection : : inactive : return RtpDirection : : inactive ;
2021-10-14 16:18:01 +08:00
default : return RtpDirection : : invalid ;
}
}
2024-03-23 23:08:10 +08:00
static DtlsRole mathDtlsRole ( DtlsRole role ) {
2022-04-03 17:12:23 +08:00
switch ( role ) {
case DtlsRole : : actpass :
case DtlsRole : : active : return DtlsRole : : passive ;
case DtlsRole : : passive : return DtlsRole : : active ;
default : CHECK ( 0 , " invalid role: " , getDtlsRoleString ( role ) ) ; return DtlsRole : : passive ;
}
}
2024-03-23 23:08:10 +08:00
void RtcConfigure : : matchMedia ( const std : : shared_ptr < RtcSession > & ret , const RtcMedia & offer_media ) const {
2021-04-06 23:47:54 +08:00
bool check_profile = true ;
bool check_codec = true ;
2022-06-11 12:26:31 +08:00
const RtcTrackConfigure * cfg_ptr = nullptr ;
switch ( offer_media . type ) {
case TrackAudio : cfg_ptr = & audio ; break ;
case TrackVideo : cfg_ptr = & video ; break ;
case TrackApplication : cfg_ptr = & application ; break ;
default : return ;
}
auto & configure = * cfg_ptr ;
2021-04-04 23:58:59 +08:00
RETRY :
2022-06-11 12:26:31 +08:00
if ( offer_media . type = = TrackApplication ) {
RtcMedia answer_media = offer_media ;
answer_media . role = mathDtlsRole ( offer_media . role ) ;
2022-08-20 10:28:19 +08:00
answer_media . ice_ufrag = configure . ice_ufrag ;
answer_media . ice_pwd = configure . ice_pwd ;
answer_media . fingerprint = configure . fingerprint ;
answer_media . ice_lite = configure . ice_lite ;
2023-08-29 11:22:30 +08:00
# ifdef ENABLE_SCTP
answer_media . candidate = configure . candidate ;
2022-05-13 20:45:04 +08:00
# else
2023-08-29 11:22:30 +08:00
answer_media . port = 0 ;
WarnL < < " answer sdp忽略application mline, 请安装usrsctp后再测试datachannel功能 " ;
2022-05-13 20:45:04 +08:00
# endif
2022-06-11 12:26:31 +08:00
ret - > media . emplace_back ( answer_media ) ;
return ;
}
for ( auto & codec : configure . preferred_codec ) {
if ( offer_media . ice_lite & & configure . ice_lite ) {
WarnL < < " answer sdp配置为ice_lite模式, 与offer sdp中的ice_lite模式冲突 " ;
continue ;
2022-04-03 17:12:23 +08:00
}
2022-06-11 12:26:31 +08:00
const RtcCodecPlan * selected_plan = nullptr ;
for ( auto & plan : offer_media . plan ) {
2024-09-19 14:53:50 +08:00
// 先检查编码格式是否为偏好 [AUTO-TRANSLATED:b7fb32a0]
// First check if the encoding format is preferred
2022-06-11 12:26:31 +08:00
if ( check_codec & & getCodecId ( plan . codec ) ! = codec ) {
2021-03-31 18:32:43 +08:00
continue ;
}
2024-09-19 14:53:50 +08:00
// 命中偏好的编码格式,然后检查规格 [AUTO-TRANSLATED:a859c839]
// Hit the preferred encoding format, then check the specifications
2022-06-11 12:26:31 +08:00
if ( check_profile & & ! onCheckCodecProfile ( plan , codec ) ) {
2021-10-14 16:18:01 +08:00
continue ;
2021-03-31 18:32:43 +08:00
}
2024-09-19 14:53:50 +08:00
// 找到中意的codec [AUTO-TRANSLATED:4b5eebfd]
// Find the desired codec
2022-06-11 12:26:31 +08:00
selected_plan = & plan ;
break ;
}
if ( ! selected_plan ) {
2024-09-19 14:53:50 +08:00
// offer中该媒体的所有的codec都不支持 [AUTO-TRANSLATED:3b57b86f]
// All codecs for this media in the offer are not supported
2022-06-11 12:26:31 +08:00
continue ;
}
RtcMedia answer_media ;
answer_media . type = offer_media . type ;
answer_media . mid = offer_media . mid ;
answer_media . proto = offer_media . proto ;
answer_media . port = offer_media . port ;
answer_media . addr = offer_media . addr ;
2022-07-07 16:44:42 +08:00
answer_media . bandwidth = offer_media . bandwidth ;
2022-06-11 12:26:31 +08:00
answer_media . rtcp_addr = offer_media . rtcp_addr ;
answer_media . rtcp_mux = offer_media . rtcp_mux & & configure . rtcp_mux ;
answer_media . rtcp_rsize = offer_media . rtcp_rsize & & configure . rtcp_rsize ;
answer_media . ice_trickle = offer_media . ice_trickle & & configure . ice_trickle ;
answer_media . ice_renomination = offer_media . ice_renomination & & configure . ice_renomination ;
answer_media . ice_ufrag = configure . ice_ufrag ;
answer_media . ice_pwd = configure . ice_pwd ;
answer_media . fingerprint = configure . fingerprint ;
answer_media . ice_lite = configure . ice_lite ;
answer_media . candidate = configure . candidate ;
// copy simulicast setting
answer_media . rtp_rids = offer_media . rtp_rids ;
answer_media . rtp_ssrc_sim = offer_media . rtp_ssrc_sim ;
answer_media . role = mathDtlsRole ( offer_media . role ) ;
2024-09-19 14:53:50 +08:00
// 如果codec匹配失败, 那么禁用该track [AUTO-TRANSLATED:037de9a8]
// If the codec matching fails, then disable the track
2024-03-23 23:08:10 +08:00
answer_media . direction = check_codec ? matchDirection ( offer_media . direction , configure . direction ) : RtpDirection : : inactive ;
2022-06-11 12:26:31 +08:00
if ( answer_media . direction = = RtpDirection : : invalid ) {
continue ;
}
if ( answer_media . direction = = RtpDirection : : sendrecv ) {
2024-09-19 14:53:50 +08:00
// 如果是收发双向, 那么我们拷贝offer sdp的ssrc, 确保ssrc一致 [AUTO-TRANSLATED:d4a621f2]
// If it is bidirectional, then we copy the offer sdp ssrc to ensure ssrc consistency
2022-06-11 12:26:31 +08:00
answer_media . rtp_rtx_ssrc = offer_media . rtp_rtx_ssrc ;
}
2024-09-19 14:53:50 +08:00
// 添加媒体plan [AUTO-TRANSLATED:3f730050]
// Add media plan
2022-06-11 12:26:31 +08:00
answer_media . plan . emplace_back ( * selected_plan ) ;
onSelectPlan ( answer_media . plan . back ( ) , codec ) ;
2024-03-23 23:08:10 +08:00
set < uint8_t > pt_selected = { selected_plan - > pt } ;
2021-04-02 16:23:40 +08:00
2024-09-19 14:53:50 +08:00
// 添加rtx,red,ulpfec plan [AUTO-TRANSLATED:1abff0c1]
// Add rtx, red, ulpfec plan
2022-06-11 12:26:31 +08:00
if ( configure . support_red | | configure . support_rtx | | configure . support_ulpfec ) {
for ( auto & plan : offer_media . plan ) {
if ( ! strcasecmp ( plan . codec . data ( ) , " rtx " ) ) {
if ( configure . support_rtx & & atoi ( plan . getFmtp ( " apt " ) . data ( ) ) = = selected_plan - > pt ) {
answer_media . plan . emplace_back ( plan ) ;
pt_selected . emplace ( plan . pt ) ;
2021-04-01 10:09:50 +08:00
}
2022-06-11 12:26:31 +08:00
continue ;
}
if ( ! strcasecmp ( plan . codec . data ( ) , " red " ) ) {
if ( configure . support_red ) {
answer_media . plan . emplace_back ( plan ) ;
pt_selected . emplace ( plan . pt ) ;
2021-04-01 10:09:50 +08:00
}
2022-06-11 12:26:31 +08:00
continue ;
}
if ( ! strcasecmp ( plan . codec . data ( ) , " ulpfec " ) ) {
if ( configure . support_ulpfec ) {
answer_media . plan . emplace_back ( plan ) ;
pt_selected . emplace ( plan . pt ) ;
2021-04-01 10:09:50 +08:00
}
2022-06-11 12:26:31 +08:00
continue ;
2021-04-01 10:09:50 +08:00
}
}
2022-06-11 12:26:31 +08:00
}
2021-04-01 10:09:50 +08:00
2024-09-19 14:53:50 +08:00
// 对方和我方都支持的扩展,那么我们才支持 [AUTO-TRANSLATED:a6cd98b2]
// We only support extensions that are supported by both the other party and us
2022-06-11 12:26:31 +08:00
for ( auto & ext : offer_media . extmap ) {
auto it = configure . extmap . find ( RtpExt : : getExtType ( ext . ext ) ) ;
if ( it ! = configure . extmap . end ( ) ) {
auto new_dir = matchDirection ( ext . direction , it - > second ) ;
switch ( new_dir ) {
case RtpDirection : : invalid :
case RtpDirection : : inactive : continue ;
default : break ;
2021-04-01 10:09:50 +08:00
}
2022-06-11 12:26:31 +08:00
answer_media . extmap . emplace_back ( ext ) ;
answer_media . extmap . back ( ) . direction = new_dir ;
2021-04-01 10:09:50 +08:00
}
2022-06-11 12:26:31 +08:00
}
2021-04-28 15:07:15 +08:00
2022-06-11 12:26:31 +08:00
auto & rtcp_fb_ref = answer_media . plan [ 0 ] . rtcp_fb ;
rtcp_fb_ref . clear ( ) ;
2024-09-19 14:53:50 +08:00
// 对方和我方都支持的rtcpfb, 那么我们才支持 [AUTO-TRANSLATED:f10450bb]
// We only support rtcpfb that is supported by both the other party and us
2022-06-11 12:26:31 +08:00
for ( auto & fp : selected_plan - > rtcp_fb ) {
if ( configure . rtcp_fb . find ( fp ) ! = configure . rtcp_fb . end ( ) ) {
2024-09-19 14:53:50 +08:00
// 对方该rtcp被我们支持 [AUTO-TRANSLATED:3b16e666]
// The other party's rtcp is supported by us
2022-06-11 12:26:31 +08:00
rtcp_fb_ref . emplace ( fp ) ;
2021-04-01 10:09:50 +08:00
}
2022-06-11 12:26:31 +08:00
}
2021-05-08 14:50:19 +08:00
2021-05-08 15:05:13 +08:00
#if 0
2024-09-19 14:53:50 +08:00
// todo 此处为添加无效的plan, webrtc sdp通过调节plan pt顺序选择匹配的codec, 意味着后面的codec其实放在sdp中是无意义的 [AUTO-TRANSLATED:502d0cb2]
// todo This is to add an invalid plan. WebRTC sdp selects the matching codec by adjusting the plan pt order, which means that the subsequent codecs are actually meaningless in the sdp
2022-06-11 12:26:31 +08:00
for ( auto & plan : offer_media . plan ) {
if ( pt_selected . find ( plan . pt ) = = pt_selected . end ( ) ) {
answer_media . plan . emplace_back ( plan ) ;
2021-05-08 14:50:19 +08:00
}
2021-03-31 18:32:43 +08:00
}
2022-06-11 12:26:31 +08:00
# endif
ret - > media . emplace_back ( answer_media ) ;
return ;
2021-03-31 18:32:43 +08:00
}
2021-04-04 23:58:59 +08:00
2021-04-06 23:47:54 +08:00
if ( check_profile ) {
2024-09-19 14:53:50 +08:00
// 如果是由于检查profile导致匹配失败, 那么重试一次, 且不检查profile [AUTO-TRANSLATED:897fa4ae]
// If the matching fails due to profile check, retry once and do not check profile
2021-04-06 23:47:54 +08:00
check_profile = false ;
goto RETRY ;
}
if ( check_codec ) {
2024-09-19 14:53:50 +08:00
// 如果是由于检查codec导致匹配失败, 那么重试一次, 且不检查codec [AUTO-TRANSLATED:fbd85968]
// If the matching fails due to codec check, retry once and do not check codec
2021-04-06 23:47:54 +08:00
check_codec = false ;
2021-04-04 23:58:59 +08:00
goto RETRY ;
}
}
2024-03-23 23:08:10 +08:00
void RtcConfigure : : setPlayRtspInfo ( const string & sdp ) {
2021-04-04 23:58:59 +08:00
RtcSession session ;
2021-11-25 20:42:51 +08:00
video . direction = RtpDirection : : inactive ;
audio . direction = RtpDirection : : inactive ;
2021-10-16 16:46:05 +08:00
session . loadFrom ( sdp ) ;
2021-04-04 23:58:59 +08:00
for ( auto & m : session . media ) {
switch ( m . type ) {
2024-03-23 23:08:10 +08:00
case TrackVideo : {
2021-11-25 20:42:51 +08:00
video . direction = RtpDirection : : sendonly ;
2021-04-05 00:06:21 +08:00
_rtsp_video_plan = std : : make_shared < RtcCodecPlan > ( m . plan [ 0 ] ) ;
video . preferred_codec . clear ( ) ;
video . preferred_codec . emplace_back ( getCodecId ( _rtsp_video_plan - > codec ) ) ;
break ;
}
2024-03-23 23:08:10 +08:00
case TrackAudio : {
audio . direction = RtpDirection : : sendonly ;
2021-04-05 00:06:21 +08:00
_rtsp_audio_plan = std : : make_shared < RtcCodecPlan > ( m . plan [ 0 ] ) ;
audio . preferred_codec . clear ( ) ;
audio . preferred_codec . emplace_back ( getCodecId ( _rtsp_audio_plan - > codec ) ) ;
break ;
}
2021-04-04 23:58:59 +08:00
default : break ;
}
}
}
2024-03-23 23:08:10 +08:00
static const string kProfile { " profile-level-id " } ;
static const string kMode { " packetization-mode " } ;
2021-04-06 23:21:46 +08:00
2022-06-11 12:26:31 +08:00
bool RtcConfigure : : onCheckCodecProfile ( const RtcCodecPlan & plan , CodecId codec ) const {
2021-04-04 23:58:59 +08:00
if ( _rtsp_audio_plan & & codec = = getCodecId ( _rtsp_audio_plan - > codec ) ) {
if ( plan . sample_rate ! = _rtsp_audio_plan - > sample_rate | | plan . channel ! = _rtsp_audio_plan - > channel ) {
2024-09-19 14:53:50 +08:00
// 音频采样率和通道数必须相同 [AUTO-TRANSLATED:6e591932]
// Audio sampling rate and number of channels must be the same
2021-04-04 23:58:59 +08:00
return false ;
}
2021-04-06 23:21:46 +08:00
return true ;
2021-04-04 23:58:59 +08:00
}
2021-04-06 23:21:46 +08:00
if ( _rtsp_video_plan & & codec = = CodecH264 & & getCodecId ( _rtsp_video_plan - > codec ) = = CodecH264 ) {
2024-09-19 14:53:50 +08:00
// h264时, profile-level-id [AUTO-TRANSLATED:94a5f360]
// When h264, profile-level-id
2021-04-15 19:35:25 +08:00
if ( strcasecmp ( _rtsp_video_plan - > fmtp [ kProfile ] . data ( ) , const_cast < RtcCodecPlan & > ( plan ) . fmtp [ kProfile ] . data ( ) ) ) {
2024-09-19 14:53:50 +08:00
// profile-level-id 不匹配 [AUTO-TRANSLATED:814ec4c4]
// profile-level-id does not match
2021-04-06 23:21:46 +08:00
return false ;
}
return true ;
}
2021-04-04 23:58:59 +08:00
return true ;
2021-03-31 18:32:43 +08:00
}
2021-04-15 19:35:25 +08:00
2023-07-22 17:30:39 +08:00
/**
Single NAI Unit Mode = 0. // Single NAI mode (Only nals from 1-23 are allowed)
Non Interleaved Mode = 1 , // Non-interleaved Mode: 1-23, 24 (STAP-A), 28 (FU-A) are allowed
Interleaved Mode = 2 , // 25 (STAP-B), 26 (MTAP16), 27 (MTAP24), 28 (EU-A), and 29 (EU-B) are allowed.
2024-09-19 14:53:50 +08:00
Single NAI Unit Mode = 0. // Single NAI mode (Only nals from 1-23 are allowed)
Non Interleaved Mode = 1 , // Non-interleaved Mode: 1-23, 24 (STAP-A), 28 (FU-A) are allowed
Interleaved Mode = 2 , // 25 (STAP-B), 26 (MTAP16), 27 (MTAP24), 28 (EU-A), and 29 (EU-B) are allowed.
*
* [ AUTO - TRANSLATED : b1526114 ]
2023-07-22 17:30:39 +08:00
* */
2022-06-11 12:26:31 +08:00
void RtcConfigure : : onSelectPlan ( RtcCodecPlan & plan , CodecId codec ) const {
2021-04-15 19:35:25 +08:00
if ( _rtsp_video_plan & & codec = = CodecH264 & & getCodecId ( _rtsp_video_plan - > codec ) = = CodecH264 ) {
2024-09-19 14:53:50 +08:00
// h264时, 设置packetization-mod为一致 [AUTO-TRANSLATED:59a00889]
// When h264, set packetization-mod to be consistent
2021-04-15 19:35:25 +08:00
auto mode = _rtsp_video_plan - > fmtp [ kMode ] ;
2023-07-22 17:30:39 +08:00
GET_CONFIG ( bool , h264_stap_a , Rtp : : kH264StapA ) ;
plan . fmtp [ kMode ] = mode . empty ( ) ? std : : to_string ( h264_stap_a ) : mode ;
2021-04-15 19:35:25 +08:00
}
}
2022-09-18 21:03:05 +08:00
} // namespace mediakit