From 870c861273473f00ebc9641c6f1cde0081fc8cfc Mon Sep 17 00:00:00 2001 From: doe300 Date: Fri, 5 Feb 2016 10:45:06 +0100 Subject: [PATCH] Moved some globals into scopes, rewrote to constexpr Signed-off-by:doe300 --- include/AudioProcessor.h | 10 +++--- include/Parameters.h | 8 ++--- include/Statistics.h | 32 +++++++++--------- include/rtp/ProcessorRTP.h | 3 +- include/rtp/RTCPHandler.h | 3 +- include/rtp/RTCPPackageHandler.h | 6 ++-- include/rtp/RTPHeader.h | 39 ++++++++++----------- include/sip/SDPMessageHandler.h | 42 +++++++++++------------ include/sip/SIPConfiguration.h | 2 +- include/sip/SIPHandler.h | 20 +++++------ include/sip/STUNClient.h | 58 ++++++++++++++++---------------- include/sip/SupportedFormats.h | 8 +++-- src/RTAudioWrapper.cpp | 4 +-- src/rtp/ProcessorRTP.cpp | 5 +-- src/rtp/RTCPHandler.cpp | 3 -- src/rtp/RTPListener.cpp | 6 ++-- src/rtp/RTPPackageHandler.cpp | 18 +++++----- src/sip/SDPMessageHandler.cpp | 17 +++++++--- src/sip/SIPConfiguration.cpp | 2 +- src/sip/SIPHandler.cpp | 15 +++++---- src/sip/STUNClient.cpp | 6 ---- src/sip/SupportedFormats.cpp | 5 ++- test/TestConfigurationModes.cpp | 2 +- test/sip/TestSIPHandler.cpp | 2 +- 24 files changed, 161 insertions(+), 155 deletions(-) diff --git a/include/AudioProcessor.h b/include/AudioProcessor.h index 5c19e7f..568a98e 100644 --- a/include/AudioProcessor.h +++ b/include/AudioProcessor.h @@ -10,11 +10,6 @@ #include "ConfigurationMode.h" #include "rtp/RTPHeader.h" -/*! - * This AudioProcessor supports any buffer-length - */ -const int BUFFER_SIZE_ANY = -1; - /*! * Information about the stream to be passed to the process-methods of AudioProcessor. * Details of the values are specified by the used AudioIO-implementation @@ -54,6 +49,11 @@ struct StreamData class AudioProcessor { public: + /*! + * This AudioProcessor supports any buffer-length + */ + static constexpr int BUFFER_SIZE_ANY{1}; + AudioProcessor(const std::string name); virtual ~AudioProcessor() diff --git a/include/Parameters.h b/include/Parameters.h index 2e8f21e..aa391ad 100644 --- a/include/Parameters.h +++ b/include/Parameters.h @@ -32,11 +32,11 @@ enum class ParameterCategory : char struct Parameter { //flag determining whether this parameter is required - const static unsigned short FLAG_REQUIRED = 0x1; + static constexpr unsigned short FLAG_REQUIRED = 0x1; //flag determining whether this parameter has a value - const static unsigned short FLAG_HAS_VALUE = 0x2; + static constexpr unsigned short FLAG_HAS_VALUE = 0x2; //flag determining whether this parameter calls another configuration-mode - const static unsigned short FLAG_CONFIGURATION_MODE = 0x4; + static constexpr unsigned short FLAG_CONFIGURATION_MODE = 0x4; //The category of this parameter const ParameterCategory category; @@ -224,7 +224,7 @@ class Parameters // A list of all available parameters //we use list for easier sorting static std::list availableParameters; - static const unsigned int tabSize{5}; + static constexpr unsigned int tabSize{5}; //Prints a help-line for a single parameter void printParameterHelp(const Parameter& param) const; diff --git a/include/Statistics.h b/include/Statistics.h index c4a1a92..72ec03d 100644 --- a/include/Statistics.h +++ b/include/Statistics.h @@ -20,22 +20,22 @@ class Statistics { //TODO RTCP-statistics # packages sent/received, # bytes sent/received public: - static const int COUNTER_PACKAGES_SENT{0}; - static const int COUNTER_PACKAGES_RECEIVED{1}; - static const int COUNTER_PACKAGES_LOST{2}; - static const int COUNTER_FRAMES_SENT{3}; - static const int COUNTER_FRAMES_RECORDED{4}; - static const int COUNTER_FRAMES_RECEIVED{5}; - static const int COUNTER_FRAMES_OUTPUT{6}; - static const int COUNTER_HEADER_BYTES_SENT{7}; - static const int COUNTER_HEADER_BYTES_RECEIVED{8}; - static const int COUNTER_PAYLOAD_BYTES_SENT{9}; - static const int COUNTER_PAYLOAD_BYTES_RECORDED{10}; - static const int COUNTER_PAYLOAD_BYTES_RECEIVED{11}; - static const int COUNTER_PAYLOAD_BYTES_OUTPUT{12}; - static const int TOTAL_ELAPSED_MILLISECONDS{13}; - static const int RTP_BUFFER_MAXIMUM_USAGE{14}; - static const int RTP_BUFFER_LIMIT{15}; + static constexpr int COUNTER_PACKAGES_SENT{0}; + static constexpr int COUNTER_PACKAGES_RECEIVED{1}; + static constexpr int COUNTER_PACKAGES_LOST{2}; + static constexpr int COUNTER_FRAMES_SENT{3}; + static constexpr int COUNTER_FRAMES_RECORDED{4}; + static constexpr int COUNTER_FRAMES_RECEIVED{5}; + static constexpr int COUNTER_FRAMES_OUTPUT{6}; + static constexpr int COUNTER_HEADER_BYTES_SENT{7}; + static constexpr int COUNTER_HEADER_BYTES_RECEIVED{8}; + static constexpr int COUNTER_PAYLOAD_BYTES_SENT{9}; + static constexpr int COUNTER_PAYLOAD_BYTES_RECORDED{10}; + static constexpr int COUNTER_PAYLOAD_BYTES_RECEIVED{11}; + static constexpr int COUNTER_PAYLOAD_BYTES_OUTPUT{12}; + static constexpr int TOTAL_ELAPSED_MILLISECONDS{13}; + static constexpr int RTP_BUFFER_MAXIMUM_USAGE{14}; + static constexpr int RTP_BUFFER_LIMIT{15}; /*! * Increments the given counter by the value provided diff --git a/include/rtp/ProcessorRTP.h b/include/rtp/ProcessorRTP.h index ef5ef41..9d4482b 100644 --- a/include/rtp/ProcessorRTP.h +++ b/include/rtp/ProcessorRTP.h @@ -42,7 +42,8 @@ class ProcessorRTP : public AudioProcessor bool cleanUp(); private: //! Delay until treating input as silence - static const unsigned short SILENCE_DELAY; + //!Treat as silence after 500ms of no input + static constexpr unsigned short SILENCE_DELAY{500}; std::shared_ptr networkObject; std::unique_ptr rtpPackage; std::shared_ptr rtpBuffer; diff --git a/include/rtp/RTCPHandler.h b/include/rtp/RTCPHandler.h index 9e10c93..24c1242 100644 --- a/include/rtp/RTCPHandler.h +++ b/include/rtp/RTCPHandler.h @@ -68,7 +68,8 @@ class RTCPHandler : public PlaybackListener bool threadRunning = false; //send SR every X seconds - static const std::chrono::seconds sendSRInterval; + //standard-conform minimum interval of 5 seconds (no need to be adaptive, as long as we only have one remote) (RFC3550 Section 6.2) + static constexpr std::chrono::seconds sendSRInterval{5}; std::chrono::system_clock::time_point lastSRReceived; std::chrono::system_clock::time_point lastSRSent; std::vector sourceDescriptions; diff --git a/include/rtp/RTCPPackageHandler.h b/include/rtp/RTCPPackageHandler.h index 998517c..f6ab9e2 100644 --- a/include/rtp/RTCPPackageHandler.h +++ b/include/rtp/RTCPPackageHandler.h @@ -23,9 +23,9 @@ class RTCPPackageHandler { public: - static const uint8_t RTCP_HEADER_SIZE = sizeof(RTCPHeader); - static const uint8_t RTCP_SENDER_INFO_SIZE = sizeof(SenderInformation); - static const uint8_t RTCP_RECEPTION_REPORT_SIZE = sizeof(ReceptionReport); + static constexpr uint8_t RTCP_HEADER_SIZE = sizeof(RTCPHeader); + static constexpr uint8_t RTCP_SENDER_INFO_SIZE = sizeof(SenderInformation); + static constexpr uint8_t RTCP_RECEPTION_REPORT_SIZE = sizeof(ReceptionReport); RTCPPackageHandler(); diff --git a/include/rtp/RTPHeader.h b/include/rtp/RTPHeader.h index 1ad2f2a..742088e 100644 --- a/include/rtp/RTPHeader.h +++ b/include/rtp/RTPHeader.h @@ -15,20 +15,6 @@ #include #endif -/*! - * Minimum size of a RTP-Header in bytes, without any CSRCs set - */ -static const unsigned int RTP_HEADER_MIN_SIZE = 12; -/*! - * Maximum size of a RTP-Header with all CSRCs set. - */ -static const unsigned int RTP_HEADER_MAX_SIZE = 72; - -/*! - * Minimum size for RTP header-extensions, 4 Byte - */ -static const unsigned int RTP_HEADER_EXTENSION_MIN_SIZE = 4; - /*! * A RTP header extension has the following format: * @@ -65,6 +51,11 @@ struct RTPHeaderExtension uint32_t* extensions; public: + /*! + * Minimum size for RTP header-extensions, 4 Byte + */ + static constexpr unsigned int MIN_EXTENSION_SIZE{4}; + RTPHeaderExtension(uint16_t length) : profile_field(0), length(length) { if(length > 0) @@ -290,17 +281,27 @@ struct RTPHeader //also watch out for byte-order and order of fields //uint32_t csrc_list[15]; - static const unsigned int shiftVersion = 6; - static const unsigned int shiftPadding = 5; - static const unsigned int shiftExtension = 4; - static const unsigned int shiftMarker = 7; + static constexpr unsigned int shiftVersion = 6; + static constexpr unsigned int shiftPadding = 5; + static constexpr unsigned int shiftExtension = 4; + static constexpr unsigned int shiftMarker = 7; public: + /*! + * Minimum size of a RTP-Header in bytes, without any CSRCs set + */ + static constexpr unsigned int MIN_HEADER_SIZE{12}; + + /*! + * Maximum size of a RTP-Header with all CSRCs set. + */ + static constexpr unsigned int MAX_HEADER_SIZE{72}; + /*! * The version-flag of a RTP package is always 2 */ - static const unsigned int VERSION = 2; + static constexpr unsigned int VERSION{2}; RTPHeader() : data{0} { diff --git a/include/sip/SDPMessageHandler.h b/include/sip/SDPMessageHandler.h index a532eed..df89809 100644 --- a/include/sip/SDPMessageHandler.h +++ b/include/sip/SDPMessageHandler.h @@ -13,27 +13,6 @@ #include "rtp/RTPHeader.h" #include "sip/SupportedFormats.h" -/*! - * The MIME-type for SDP: application/sdp - */ -const std::string MIME_SDP="application/sdp"; - -const char SDP_VERSION='v'; -const char SDP_ORIGIN='c'; -const char SDP_SESSION_NAME='s'; -const char SDP_CONNECTION='c'; -const char SDP_TIMING='t'; -const char SDP_MEDIA='m'; -const char SDP_ATTRIBUTE='a'; - -const std::string SDP_ATTRIBUTE_RTPMAP("rtpmap"); -const std::string SDP_ATTRIBUTE_FMTP("fmtp"); -//specifies RTCP-port, if not consecutive to RTP-port, see RFC 3605 -const std::string SDP_ATTRIBUTE_RTCP("rtcp"); - -const std::string SDP_MEDIA_RTP("RTP/AVP"); -const std::string SDP_MEDIA_SRTP("RTP/SAVP"); - struct SessionKey : public KeyValuePair { SessionKey() : KeyValuePair() @@ -97,6 +76,22 @@ struct MediaDescription struct SessionDescription : public KeyValuePairs { + static constexpr char SDP_VERSION='v'; + static constexpr char SDP_ORIGIN='c'; + static constexpr char SDP_SESSION_NAME='s'; + static constexpr char SDP_CONNECTION='c'; + static constexpr char SDP_TIMING='t'; + static constexpr char SDP_MEDIA='m'; + static constexpr char SDP_ATTRIBUTE='a'; + + static const std::string SDP_ATTRIBUTE_RTPMAP; + static const std::string SDP_ATTRIBUTE_FMTP; + //specifies RTCP-port, if not consecutive to RTP-port, see RFC 3605 + static const std::string SDP_ATTRIBUTE_RTCP; + + static const std::string SDP_MEDIA_RTP; + static const std::string SDP_MEDIA_SRTP; + /*! * \param tag The attribute-tag to search for * \param firstValue The (optional) first part of the attribute-value @@ -140,6 +135,11 @@ struct SessionDescription : public KeyValuePairs class SDPMessageHandler { public: + + /*! + * The MIME-type for SDP: application/sdp + */ + static const std::string MIME_SDP; SDPMessageHandler(); /*! diff --git a/include/sip/SIPConfiguration.h b/include/sip/SIPConfiguration.h index 4697b58..af23e05 100644 --- a/include/sip/SIPConfiguration.h +++ b/include/sip/SIPConfiguration.h @@ -42,7 +42,7 @@ class SIPConfiguration : public ConfigurationMode void setConfig(const MediaDescription& media, const NetworkConfiguration& rtpConfig, const NetworkConfiguration& customRTCPConfig); //Wait a maximum of 30 secs - static const int MAX_WAIT_TIME{30000}; + static constexpr int MAX_WAIT_TIME{30000}; }; #endif /* SIPCONFIGURATION_H */ diff --git a/include/sip/SIPHandler.h b/include/sip/SIPHandler.h index c1bae41..86198a9 100644 --- a/include/sip/SIPHandler.h +++ b/include/sip/SIPHandler.h @@ -18,20 +18,19 @@ #include "SDPMessageHandler.h" #include "SIPPackageHandler.h" -#define SIP_BUFFER_SIZE 2048 - -//A list of all allowed SIP-methods -const std::string SIP_ALLOW_METHODS = Utility::joinStrings({SIP_REQUEST_INVITE, SIP_REQUEST_ACK, SIP_REQUEST_BYE, SIP_REQUEST_CANCEL}, " "); - -//A list of all accepted MIME-types -const std::string SIP_ACCEPT_TYPES = Utility::joinStrings({MIME_SDP, MIME_MULTIPART_MIXED, MIME_MULTIPART_ALTERNATIVE}, ", "); - -const unsigned short SIP_DEFAULT_PORT =5060; - class SIPHandler { public: + //A list of all allowed SIP-methods + static const std::string SIP_ALLOW_METHODS; + + //A list of all accepted MIME-types + static const std::string SIP_ACCEPT_TYPES; + + //The default port for SIP, as of RFC 3261 + static constexpr unsigned short SIP_DEFAULT_PORT{5060}; + SIPHandler(const NetworkConfiguration& sipConfig, const std::string& remoteUser, const std::function configFunction); SIPHandler(const NetworkConfiguration& sipConfig, const std::string& localUser, const std::string& localHostName, const std::string& remoteUser, const std::string& callID); @@ -56,6 +55,7 @@ class SIPHandler static std::string generateCallID(const std::string& host); private: + static constexpr unsigned short SIP_BUFFER_SIZE{2048}; enum class SessionState { /*! diff --git a/include/sip/STUNClient.h b/include/sip/STUNClient.h index 0d6e1a1..478b9a9 100644 --- a/include/sip/STUNClient.h +++ b/include/sip/STUNClient.h @@ -15,29 +15,6 @@ #include "Utility.h" -typedef uint16_t STUNMessageType; - -static const STUNMessageType STUN_BINDING_REQUEST = 0x0001; -static const STUNMessageType STUN_BINDING_RESPONSE = 0x0101; -static const STUNMessageType STUN_BINDING_ERROR_RESPONSE = 0x0111; -//list is not exhaustive (see: https://www.iana.org/assignments/stun-parameters/stun-parameters.txt) - -typedef uint16_t STUNAttributeType; - -static const STUNAttributeType STUN_INVALID_ATTRIBUTE = 0x0000; -static const STUNAttributeType STUN_MAPPED_ADDRESS = 0x0001; -static const STUNAttributeType STUN_CHANGE_REQUEST = 0x0003; -static const STUNAttributeType STUN_USERNAME = 0x0006; -static const STUNAttributeType STUN_MESSAGE_INTEGRITY = 0x0008; -static const STUNAttributeType STUN_ERROR_CODE = 0x0009; -static const STUNAttributeType STUN_UNKNOWN_ATTRIBUTES = 0x000A; -static const STUNAttributeType STUN_XOR_MAPPED_ADDRESS = 0x8020; -static const STUNAttributeType STUN_ALTERNATE_SERVER = 0x8023; -//list is not exhaustive (see: https://www.iana.org/assignments/stun-parameters/stun-parameters.txt) - -static const uint16_t MAPPED_ADDRESS_IPv4 = 0x1; -static const uint16_t MAPPED_ADDRESS_IPv6 = 0x2; - /*! * STUN client for RFC 5389 * @@ -64,14 +41,37 @@ class STUNClient const std::tuple retrieveSIPInfo(); private: + typedef uint16_t STUNMessageType; + + static constexpr STUNMessageType STUN_BINDING_REQUEST = 0x0001; + static constexpr STUNMessageType STUN_BINDING_RESPONSE = 0x0101; + static constexpr STUNMessageType STUN_BINDING_ERROR_RESPONSE = 0x0111; + //list is not exhaustive (see: https://www.iana.org/assignments/stun-parameters/stun-parameters.txt) + + typedef uint16_t STUNAttributeType; + + static constexpr STUNAttributeType STUN_INVALID_ATTRIBUTE = 0x0000; + static constexpr STUNAttributeType STUN_MAPPED_ADDRESS = 0x0001; + static constexpr STUNAttributeType STUN_CHANGE_REQUEST = 0x0003; + static constexpr STUNAttributeType STUN_USERNAME = 0x0006; + static constexpr STUNAttributeType STUN_MESSAGE_INTEGRITY = 0x0008; + static constexpr STUNAttributeType STUN_ERROR_CODE = 0x0009; + static constexpr STUNAttributeType STUN_UNKNOWN_ATTRIBUTES = 0x000A; + static constexpr STUNAttributeType STUN_XOR_MAPPED_ADDRESS = 0x8020; + static constexpr STUNAttributeType STUN_ALTERNATE_SERVER = 0x8023; + //list is not exhaustive (see: https://www.iana.org/assignments/stun-parameters/stun-parameters.txt) + + static constexpr uint16_t MAPPED_ADDRESS_IPv4 = 0x1; + static constexpr uint16_t MAPPED_ADDRESS_IPv6 = 0x2; + static const std::vector STUN_SERVERS; - static const unsigned short DEFAULT_STUN_PORT; + static constexpr unsigned short DEFAULT_STUN_PORT{3478}; static const std::string DEFAULT_SOURCE_IP; - static const unsigned short LOCAL_PORT; - static const uint32_t MAGIC_COOKIE; - static const unsigned short BUFFER_SIZE; - static const uint8_t STUN_HEADER_SIZE; - static const uint8_t MAX_RETRIES; + static constexpr unsigned short LOCAL_PORT{54320}; + static constexpr uint32_t MAGIC_COOKIE{0x2112A442}; + static constexpr unsigned short BUFFER_SIZE{2048}; + static constexpr uint8_t STUN_HEADER_SIZE{20}; + static constexpr uint8_t MAX_RETRIES{8}; char buffer[2048]; std::string transactionID; diff --git a/include/sip/SupportedFormats.h b/include/sip/SupportedFormats.h index 1731873..d517528 100644 --- a/include/sip/SupportedFormats.h +++ b/include/sip/SupportedFormats.h @@ -11,9 +11,6 @@ #include #include -const std::string FORMAT_OPUS_DTX("usedtx"); -const std::string FORMAT_OPUS_FEC("useinbandfec"); - struct SupportedFormat { // media name for LPCM samples @@ -23,6 +20,11 @@ struct SupportedFormat //media name for G.711 mu-law samples static const std::string MEDIA_PCMU; + //parameter name for opus DTX + static const std::string FORMAT_OPUS_DTX; + //parameter name for opus FEC + static const std::string FORMAT_OPUS_FEC; + const unsigned int payloadType; const std::string encoding; const unsigned int sampleRate; diff --git a/src/RTAudioWrapper.cpp b/src/RTAudioWrapper.cpp index 7667bd5..ee427d2 100644 --- a/src/RTAudioWrapper.cpp +++ b/src/RTAudioWrapper.cpp @@ -405,7 +405,7 @@ unsigned int RtAudioWrapper::findOptimalBufferSize(unsigned int defaultBufferSiz { continue; } - if(processorBufferSizes[procIndex][sizeIndex] == BUFFER_SIZE_ANY) + if(processorBufferSizes[procIndex][sizeIndex] == AudioProcessor::BUFFER_SIZE_ANY) { processorSupportAll[procIndex] = true; continue; @@ -420,7 +420,7 @@ unsigned int RtAudioWrapper::findOptimalBufferSize(unsigned int defaultBufferSiz //we already checked all sizes below sizeIndex for(unsigned int checkSizeIndex = sizeIndex; checkSizeIndex < processorBufferSizes[checkProcIndex].size(); checkSizeIndex ++) { - if(processorBufferSizes[checkProcIndex][checkSizeIndex] == suggestedBufferSize || processorBufferSizes[checkProcIndex][checkSizeIndex] == BUFFER_SIZE_ANY) + if(processorBufferSizes[checkProcIndex][checkSizeIndex] == suggestedBufferSize || processorBufferSizes[checkProcIndex][checkSizeIndex] == AudioProcessor::BUFFER_SIZE_ANY) { singleProcessorAccepted = true; //break; diff --git a/src/rtp/ProcessorRTP.cpp b/src/rtp/ProcessorRTP.cpp index d29ddd5..ad04299 100644 --- a/src/rtp/ProcessorRTP.cpp +++ b/src/rtp/ProcessorRTP.cpp @@ -4,9 +4,6 @@ Participant ParticipantDatabase::participants[ParticipantDatabase::MAX_PARTICIPANTS] = {{},{}}; -//!Treat as silence after 500ms of no input -const unsigned short ProcessorRTP::SILENCE_DELAY = 500; - ProcessorRTP::ProcessorRTP(const std::string name, std::shared_ptr networkwrapper, std::shared_ptr buffer, const PayloadType payloadType) : AudioProcessor(name), payloadType(payloadType), lastPackageWasSilent(false) { @@ -75,7 +72,7 @@ unsigned int ProcessorRTP::processInputData(void *inputBuffer, const unsigned in ParticipantDatabase::self().extendedHighestSequenceNumber += 1; Statistics::incrementCounter(Statistics::COUNTER_FRAMES_SENT, userData->nBufferFrames); Statistics::incrementCounter(Statistics::COUNTER_PACKAGES_SENT, 1); - Statistics::incrementCounter(Statistics::COUNTER_HEADER_BYTES_SENT, RTP_HEADER_MIN_SIZE); + Statistics::incrementCounter(Statistics::COUNTER_HEADER_BYTES_SENT, RTPHeader::MIN_HEADER_SIZE); Statistics::incrementCounter(Statistics::COUNTER_PAYLOAD_BYTES_SENT, inputBufferByteSize); //no changes in buffer-size diff --git a/src/rtp/RTCPHandler.cpp b/src/rtp/RTCPHandler.cpp index a70734f..3b96f92 100644 --- a/src/rtp/RTCPHandler.cpp +++ b/src/rtp/RTCPHandler.cpp @@ -13,9 +13,6 @@ #include "Parameters.h" #include "Utility.h" -//standard-conform minimum interval of 5 seconds (no need to be adaptive, as long as we only have one remote) (RFC3550 Section 6.2) -const std::chrono::seconds RTCPHandler::sendSRInterval{5}; - RTCPHandler::RTCPHandler(std::unique_ptr&& networkWrapper, const std::shared_ptr configMode, const std::function startCallback): wrapper(std::move(networkWrapper)), configMode(configMode), startAudioCallback(startCallback), rtcpHandler(), diff --git a/src/rtp/RTPListener.cpp b/src/rtp/RTPListener.cpp index 7049370..9777365 100644 --- a/src/rtp/RTPListener.cpp +++ b/src/rtp/RTPListener.cpp @@ -56,7 +56,7 @@ void RTPListener::runThread() else if(threadRunning && RTPPackageHandler::isRTPPackage(rtpHandler.getWorkBuffer(), (unsigned int)receivedSize)) { //2. write package to buffer - auto result = buffer->addPackage(rtpHandler, receivedSize - RTP_HEADER_MIN_SIZE); + auto result = buffer->addPackage(rtpHandler, receivedSize - RTPHeader::MIN_HEADER_SIZE); if (result == RTPBufferStatus::RTP_BUFFER_INPUT_OVERFLOW) { //TODO some handling or simply discard? @@ -82,8 +82,8 @@ void RTPListener::runThread() calculateInterarrivalJitter(rtpHandler.getRTPPackageHeader()->getTimestamp(), rtpHandler.getCurrentRTPTimestamp()); ParticipantDatabase::remote().ssrc = rtpHandler.getRTPPackageHeader()->getSSRC(); Statistics::incrementCounter(Statistics::COUNTER_PACKAGES_RECEIVED, 1); - Statistics::incrementCounter(Statistics::COUNTER_HEADER_BYTES_RECEIVED, RTP_HEADER_MIN_SIZE); - Statistics::incrementCounter(Statistics::COUNTER_PAYLOAD_BYTES_RECEIVED, receivedSize - RTP_HEADER_MIN_SIZE); + Statistics::incrementCounter(Statistics::COUNTER_HEADER_BYTES_RECEIVED, RTPHeader::MIN_HEADER_SIZE); + Statistics::incrementCounter(Statistics::COUNTER_PAYLOAD_BYTES_RECEIVED, receivedSize - RTPHeader::MIN_HEADER_SIZE); } } } diff --git a/src/rtp/RTPPackageHandler.cpp b/src/rtp/RTPPackageHandler.cpp index 65ee5db..7571164 100644 --- a/src/rtp/RTPPackageHandler.cpp +++ b/src/rtp/RTPPackageHandler.cpp @@ -10,7 +10,7 @@ RTPPackageHandler::RTPPackageHandler(unsigned int maximumPayloadSize, PayloadType payloadType) { this->maximumPayloadSize = maximumPayloadSize; - this->maximumBufferSize = maximumPayloadSize + RTP_HEADER_MAX_SIZE; + this->maximumBufferSize = maximumPayloadSize + RTPHeader::MAX_HEADER_SIZE; this->payloadType = payloadType; workBuffer = new char[maximumBufferSize]; @@ -39,8 +39,8 @@ const void* RTPPackageHandler::createNewRTPPackage(const void* audioData, unsign newRTPHeader.setSSRC(this->ssrc); // Copy RTPHeader and Audiodata in the buffer - memcpy((char*)workBuffer, &newRTPHeader, RTP_HEADER_MIN_SIZE); - memcpy((char*)(workBuffer)+ RTP_HEADER_MIN_SIZE, audioData, payloadSize); + memcpy((char*)workBuffer, &newRTPHeader, RTPHeader::MIN_HEADER_SIZE); + memcpy((char*)(workBuffer)+ RTPHeader::MIN_HEADER_SIZE, audioData, payloadSize); actualPayloadSize = payloadSize; return workBuffer; @@ -68,7 +68,7 @@ const RTPHeaderExtension RTPPackageHandler::getRTPHeaderExtension() const const RTPHeaderExtension* readEx = (RTPHeaderExtension*)((char*)(workBuffer) + getRTPHeaderSize()); RTPHeaderExtension ex(readEx->getLength()); ex.setProfile(readEx->getProfile()); - memcpy(ex.getExtension(), ((char*)(workBuffer) + getRTPHeaderSize() + RTP_HEADER_EXTENSION_MIN_SIZE), readEx->getLength()); + memcpy(ex.getExtension(), ((char*)(workBuffer) + getRTPHeaderSize() + RTPHeaderExtension::MIN_EXTENSION_SIZE), readEx->getLength()); return ex; } @@ -91,7 +91,7 @@ unsigned int RTPPackageHandler::getAudioSourceId() unsigned int RTPPackageHandler::getRTPHeaderSize() const { - return RTP_HEADER_MIN_SIZE + getRTPPackageHeader()->getCSRCCount() * sizeof(uint32_t); + return RTPHeader::MIN_HEADER_SIZE + getRTPPackageHeader()->getCSRCCount() * sizeof(uint32_t); } unsigned int RTPPackageHandler::getRTPHeaderExtensionSize() const @@ -99,7 +99,7 @@ unsigned int RTPPackageHandler::getRTPHeaderExtensionSize() const if(((RTPHeader*)workBuffer)->hasExtension()) { const RTPHeaderExtension* readEx = (RTPHeaderExtension*)((char*)(workBuffer) + getRTPHeaderSize()); - return RTP_HEADER_EXTENSION_MIN_SIZE + readEx->getLength() * sizeof(uint32_t); + return RTPHeaderExtension::MIN_EXTENSION_SIZE + readEx->getLength() * sizeof(uint32_t); } else { @@ -136,8 +136,8 @@ void* RTPPackageHandler::getWorkBuffer() void RTPPackageHandler::createSilencePackage() { RTPHeader silenceHeader; - memcpy(workBuffer, &silenceHeader, RTP_HEADER_MIN_SIZE); - memset((char *)(workBuffer) + RTP_HEADER_MIN_SIZE, 0, maximumPayloadSize); + memcpy(workBuffer, &silenceHeader, RTPHeader::MIN_HEADER_SIZE); + memset((char *)(workBuffer) + RTPHeader::MIN_HEADER_SIZE, 0, maximumPayloadSize); actualPayloadSize = maximumPayloadSize; } @@ -157,7 +157,7 @@ uint32_t RTPPackageHandler::getCurrentRTPTimestamp() const bool RTPPackageHandler::isRTPPackage(const void* packageBuffer, unsigned int packageLength) { //1. check for package size, if large enough - if(packageLength < RTP_HEADER_MIN_SIZE) + if(packageLength < RTPHeader::MIN_HEADER_SIZE) { return false; } diff --git a/src/sip/SDPMessageHandler.cpp b/src/sip/SDPMessageHandler.cpp index 8c69b25..5675309 100644 --- a/src/sip/SDPMessageHandler.cpp +++ b/src/sip/SDPMessageHandler.cpp @@ -13,6 +13,13 @@ SIPUserAgent sipUserAgents[2] = {{},{}}; +const std::string SessionDescription::SDP_ATTRIBUTE_RTPMAP("rtpmap"); +const std::string SessionDescription::SDP_ATTRIBUTE_FMTP("fmtp"); +const std::string SessionDescription::SDP_ATTRIBUTE_RTCP("rtcp"); +const std::string SessionDescription::SDP_MEDIA_RTP("RTP/AVP"); +const std::string SessionDescription::SDP_MEDIA_SRTP("RTP/SAVP"); +const std::string SDPMessageHandler::MIME_SDP="application/sdp"; + SDPMessageHandler::SDPMessageHandler() { } @@ -246,7 +253,7 @@ std::vector SDPMessageHandler::readMediaDescriptions(const Ses { std::cout << "SDP: Reading media descriptions..." << std::endl; std::vector results; - const std::vector mediaFields = sdp.getFieldValues(SDP_MEDIA); + const std::vector mediaFields = sdp.getFieldValues(SessionDescription::SDP_MEDIA); for(const std::string& mediaField : mediaFields) { //m= ... @@ -259,7 +266,7 @@ std::vector SDPMessageHandler::readMediaDescriptions(const Ses const int port = atoi(mediaField.substr(index, mediaField.find(' ', index)- index).data()); index = mediaField.find(' ', index) + 1; const std::string protocol = mediaField.substr(index, mediaField.find(' ', index) - index); - if(SDP_MEDIA_RTP.compare(protocol) != 0 && SDP_MEDIA_SRTP.compare(protocol) != 0) + if(SessionDescription::SDP_MEDIA_RTP.compare(protocol) != 0 && SessionDescription::SDP_MEDIA_SRTP.compare(protocol) != 0) { //skip non-(S)RTP continue; @@ -313,7 +320,7 @@ NetworkConfiguration SDPMessageHandler::readRTCPAttribute(const SessionDescripti NetworkConfiguration config{0}; config.remoteIPAddress = ""; //a=rtcp: [ ] - const std::string rtcpAttribute = sdp.getAttribute(SDP_ATTRIBUTE_RTCP); + const std::string rtcpAttribute = sdp.getAttribute(SessionDescription::SDP_ATTRIBUTE_RTCP); if(rtcpAttribute.empty()) { //no custom RTCP-config set @@ -338,7 +345,7 @@ NetworkConfiguration SDPMessageHandler::readRTCPAttribute(const SessionDescripti MediaDescription SDPMessageHandler::getRTPMap(const SessionDescription& sdp, const unsigned int payloadType) { //a=rtpmap: / [/] - const std::string rtpMap = sdp.getAttribute(SDP_ATTRIBUTE_RTPMAP, std::to_string(payloadType)); + const std::string rtpMap = sdp.getAttribute(SessionDescription::SDP_ATTRIBUTE_RTPMAP, std::to_string(payloadType)); if(rtpMap.empty()) { return std::move(MediaDescription{}); @@ -360,7 +367,7 @@ MediaDescription SDPMessageHandler::getRTPMap(const SessionDescription& sdp, con void SDPMessageHandler::readFormatParameters(MediaDescription& descr, const SessionDescription& sdp, const unsigned int payloadType) { - const std::string fmtParams = sdp.getAttribute(SDP_ATTRIBUTE_FMTP, std::to_string(payloadType)); + const std::string fmtParams = sdp.getAttribute(SessionDescription::SDP_ATTRIBUTE_FMTP, std::to_string(payloadType)); if(fmtParams.empty()) { return; diff --git a/src/sip/SIPConfiguration.cpp b/src/sip/SIPConfiguration.cpp index 569f74b..989c0e7 100644 --- a/src/sip/SIPConfiguration.cpp +++ b/src/sip/SIPConfiguration.cpp @@ -138,7 +138,7 @@ void SIPConfiguration::setConfig(const MediaDescription& media, const NetworkCon SupportedFormat format = media.getFormat(); for(const KeyValuePair& param : media.formatParams.fields) { - if(FORMAT_OPUS_DTX.compare(param.key) == 0 && param.value.compare("1") == 0) + if(SupportedFormat::FORMAT_OPUS_DTX.compare(param.key) == 0 && param.value.compare("1") == 0) { //if the other side supports DTX, we do too //add "Gain Control" to calculate the silence-threshold diff --git a/src/sip/SIPHandler.cpp b/src/sip/SIPHandler.cpp index 6465c6c..dcb8e26 100644 --- a/src/sip/SIPHandler.cpp +++ b/src/sip/SIPHandler.cpp @@ -15,6 +15,9 @@ const std::string SIPUserAgent::getSIPURI() const return SIPPackageHandler::createSIPURI(userName, hostName.empty() ? ipAddress : hostName, port); } +const std::string SIPHandler::SIP_ALLOW_METHODS = Utility::joinStrings({SIP_REQUEST_INVITE, SIP_REQUEST_ACK, SIP_REQUEST_BYE, SIP_REQUEST_CANCEL}, " "); +const std::string SIPHandler::SIP_ACCEPT_TYPES = Utility::joinStrings({SDPMessageHandler::MIME_SDP, MIME_MULTIPART_MIXED, MIME_MULTIPART_ALTERNATIVE}, ", "); + SIPHandler::SIPHandler(const NetworkConfiguration& sipConfig, const std::string& remoteUser, const std::function configFunction) : network(new UDPWrapper(sipConfig)), sipConfig(sipConfig), configFunction(configFunction), callID(SIPHandler::generateCallID(Utility::getDomainName())), sequenceNumber(0), buffer(SIP_BUFFER_SIZE), lastBranch(), state(SessionState::UNKNOWN) { @@ -136,7 +139,7 @@ void SIPHandler::sendInviteRequest() //header-fields header[SIP_HEADER_ALLOW] = SIP_ALLOW_METHODS; header[SIP_HEADER_ACCEPT] = SIP_ACCEPT_TYPES; - header[SIP_HEADER_CONTENT_TYPE] = MIME_SDP; + header[SIP_HEADER_CONTENT_TYPE] = SDPMessageHandler::MIME_SDP; NetworkConfiguration rtpConfig = sipConfig; rtpConfig.localPort = DEFAULT_NETWORK_PORT; @@ -237,13 +240,13 @@ void SIPHandler::handleSIPRequest(const void* buffer, unsigned int packageLength sendResponse(SIP_RESPONSE_BUSY_CODE, SIP_RESPONSE_BUSY, &requestHeader); std::cout << "SIP: Received INVITE, but communication already active, answering: " << SIP_RESPONSE_BUSY << std::endl; } - else if (requestHeader[SIP_HEADER_CONTENT_TYPE].compare(MIME_SDP) == 0 || SIPPackageHandler::hasMultipartBody(requestHeader)) + else if (requestHeader[SIP_HEADER_CONTENT_TYPE].compare(SDPMessageHandler::MIME_SDP) == 0 || SIPPackageHandler::hasMultipartBody(requestHeader)) { //adds support for multipart containing SDP (RFC 5621) if(SIPPackageHandler::hasMultipartBody(requestHeader)) { //we only need the SDP part of the body - requestBody = SIPPackageHandler::readMultipartBody(requestHeader, requestBody)[MIME_SDP]; + requestBody = SIPPackageHandler::readMultipartBody(requestHeader, requestBody)[SDPMessageHandler::MIME_SDP]; if(requestBody.empty()) { std::cout << "SIP: Received multi-part INVITE without SDP body!" << std::endl; @@ -266,7 +269,7 @@ void SIPHandler::handleSIPRequest(const void* buffer, unsigned int packageLength //2.2 send ok to start communication (with selected media-format) SIPResponseHeader responseHeader(SIP_RESPONSE_OK_CODE, SIP_RESPONSE_OK); initializeHeaderFields(SIP_REQUEST_INVITE, responseHeader, &requestHeader); - responseHeader[SIP_HEADER_CONTENT_TYPE] = MIME_SDP; + responseHeader[SIP_HEADER_CONTENT_TYPE] = SDPMessageHandler::MIME_SDP; NetworkConfiguration rtpConfig; rtpConfig.remoteIPAddress = sdp.getConnectionAddress(); rtpConfig.localPort = DEFAULT_NETWORK_PORT; @@ -349,13 +352,13 @@ void SIPHandler::handleSIPResponse(const void* buffer, unsigned int packageLengt sendAckRequest(); //action depending on request const std::string requestCommand = responseHeader.getRequestCommand(); - if (SIP_REQUEST_INVITE.compare(requestCommand) == 0 && (responseHeader[SIP_HEADER_CONTENT_TYPE].compare(MIME_SDP) == 0 || SIPPackageHandler::hasMultipartBody(responseHeader))) + if (SIP_REQUEST_INVITE.compare(requestCommand) == 0 && (responseHeader[SIP_HEADER_CONTENT_TYPE].compare(SDPMessageHandler::MIME_SDP) == 0 || SIPPackageHandler::hasMultipartBody(responseHeader))) { //adds support for multipart containing SDP (RFC 5621) if(SIPPackageHandler::hasMultipartBody(responseHeader)) { //we only need the SDP part of the body - responseBody = SIPPackageHandler::readMultipartBody(responseHeader, responseBody)[MIME_SDP]; + responseBody = SIPPackageHandler::readMultipartBody(responseHeader, responseBody)[SDPMessageHandler::MIME_SDP]; if(responseBody.empty()) { std::cout << "SIP: Received multi-part OK without SDP body!" << std::endl; diff --git a/src/sip/STUNClient.cpp b/src/sip/STUNClient.cpp index 782a0b3..d50e35d 100644 --- a/src/sip/STUNClient.cpp +++ b/src/sip/STUNClient.cpp @@ -18,13 +18,7 @@ const std::vector STUNClient::STUN_SERVERS = { "stunserver.org" }; -const unsigned short STUNClient::DEFAULT_STUN_PORT{3478}; const std::string STUNClient::DEFAULT_SOURCE_IP{"0.0.0.0"}; -const unsigned short STUNClient::LOCAL_PORT{54320}; -const uint32_t STUNClient::MAGIC_COOKIE{0x2112A442}; -const unsigned short STUNClient::BUFFER_SIZE{2048}; -const uint8_t STUNClient::STUN_HEADER_SIZE{20}; -const uint8_t STUNClient::MAX_RETRIES{8}; STUNClient::STUNClient() { diff --git a/src/sip/SupportedFormats.cpp b/src/sip/SupportedFormats.cpp index 80b90f3..a89dc1f 100644 --- a/src/sip/SupportedFormats.cpp +++ b/src/sip/SupportedFormats.cpp @@ -8,9 +8,12 @@ const std::string SupportedFormat::MEDIA_LPCM("LPCM"); const std::string SupportedFormat::MEDIA_PCMA("PCMA"); const std::string SupportedFormat::MEDIA_PCMU("PCMU"); +const std::string SupportedFormat::FORMAT_OPUS_DTX("usedtx"); +const std::string SupportedFormat::FORMAT_OPUS_FEC("useinbandfec"); + std::vector SupportedFormats::availableFormats = {}; -const SupportedFormat* SupportedFormats::OPUS_48000 = SupportedFormats::registerFormat(SupportedFormat(PayloadType::OPUS, "opus", 48000, 2, AudioProcessorFactory::OPUS_CODEC, false, std::string(FORMAT_OPUS_DTX).append("=1; ").append(FORMAT_OPUS_FEC).append("=0"))); +const SupportedFormat* SupportedFormats::OPUS_48000 = SupportedFormats::registerFormat(SupportedFormat(PayloadType::OPUS, "opus", 48000, 2, AudioProcessorFactory::OPUS_CODEC, false, std::string(SupportedFormat::FORMAT_OPUS_DTX).append("=1; ").append(SupportedFormat::FORMAT_OPUS_FEC).append("=0"))); const SupportedFormat* SupportedFormats::G711_PCMA = SupportedFormats::registerFormat(SupportedFormat(PayloadType::PCMA, SupportedFormat::MEDIA_PCMA, 8000, 1, AudioProcessorFactory::G711_PCMA, true)); const SupportedFormat* SupportedFormats::G711_PCMU = SupportedFormats::registerFormat(SupportedFormat(PayloadType::PCMU, SupportedFormat::MEDIA_PCMU, 8000, 1, AudioProcessorFactory::G711_PCMU, true)); const SupportedFormat* SupportedFormats::L16_2_44100 = SupportedFormats::registerFormat(SupportedFormat(PayloadType::L16_2, SupportedFormat::MEDIA_LPCM, 44100, 2, "", true)); diff --git a/test/TestConfigurationModes.cpp b/test/TestConfigurationModes.cpp index a7eb926..f58cc29 100644 --- a/test/TestConfigurationModes.cpp +++ b/test/TestConfigurationModes.cpp @@ -179,7 +179,7 @@ void TestConfigurationModes::testFileConfiguration() void TestConfigurationModes::testSIPConfiguration() { - ConfigurationMode* config = new SIPConfiguration({54321, "127.0.0.1", SIP_DEFAULT_PORT}); + ConfigurationMode* config = new SIPConfiguration({54321, "127.0.0.1", SIPHandler::SIP_DEFAULT_PORT}); TEST_ASSERT_EQUALS(false, config->isConfigured()); TEST_ASSERT(config->runConfiguration()); TEST_ASSERT(config->isConfigured()); diff --git a/test/sip/TestSIPHandler.cpp b/test/sip/TestSIPHandler.cpp index 3a6dfb1..e05514b 100644 --- a/test/sip/TestSIPHandler.cpp +++ b/test/sip/TestSIPHandler.cpp @@ -8,7 +8,7 @@ #include "TestSIPHandler.h" #include "UDPWrapper.h" -TestSIPHandler::TestSIPHandler() : Suite(), handler({2060, "192.168.178.123", SIP_DEFAULT_PORT}, "dummy", [this](const MediaDescription, const NetworkConfiguration, const NetworkConfiguration){TEST_FAIL("Wrong configuration accepted!");}) +TestSIPHandler::TestSIPHandler() : Suite(), handler({2060, "192.168.178.123", SIPHandler::SIP_DEFAULT_PORT}, "dummy", [this](const MediaDescription, const NetworkConfiguration, const NetworkConfiguration){TEST_FAIL("Wrong configuration accepted!");}) { // TEST_ADD(TestSIPHandler::testSIPThread); #ifdef EXTENDED_SIP_TEST