📄 sipconnection.cpp
字号:
break; default: // Its a SIP Response if(sipMsg->isResponse()) { // Ignore it and do not create a connection createConnection = FALSE; } // Its a SIP Request else { createConnection = SipConnection::requestShouldCreateConnection(sipMsg, sipUa, codecFactory); } break; } if(!createConnection) { UtlString msgBytes; int numBytes; sipMsg->getBytes(&msgBytes, &numBytes); msgBytes.insert(0, "SipConnection::shouldCreateConnection: FALSE\n");#ifdef TEST_PRINT osPrintf("%s\n", msgBytes.data());#endif }#ifdef TEST_PRINT else { osPrintf("Create a SIP connection\n"); }#endif } return(createConnection);}// Select a compatible contact type given the request URLCONTACT_TYPE SipConnection::selectCompatibleContactType(const SipMessage& request){ CONTACT_TYPE contactType = mContactType ; char szAdapter[256]; UtlString localAddress; getLocalAddress(&localAddress); getContactAdapterName(szAdapter, localAddress.data()); UtlString requestUriHost ; int requestUriPort ; UtlString strUri ; request.getRequestUri(&strUri) ; Url requestUri(strUri) ; requestUri.getHostAddress(requestUriHost) ; requestUriPort = requestUri.getHostPort() ; if (!portIsValid(requestUriPort)) { requestUriPort = 5060 ; } CONTACT_ADDRESS config_contact; CONTACT_ADDRESS stun_contact; CONTACT_ADDRESS local_contact; if (sipUserAgent->getContactDb().getRecordForAdapter(config_contact, szAdapter, CONFIG) && (strcmp(config_contact.cIpAddress, requestUriHost) == 0) && (requestUriPort == (!portIsValid(config_contact.iPort) ? 5060 : config_contact.iPort))) { mContactId = config_contact.id; contactType = CONFIG ; } else if (sipUserAgent->getContactDb().getRecordForAdapter(stun_contact, szAdapter, NAT_MAPPED) && (strcmp(stun_contact.cIpAddress, requestUriHost) == 0) && (requestUriPort == (!portIsValid(stun_contact.iPort) ? 5060 : stun_contact.iPort))) { mContactId = stun_contact.id; contactType = NAT_MAPPED ; } else if (sipUserAgent->getContactDb().getRecordForAdapter(local_contact, szAdapter, LOCAL_CONTACT) && (strcmp(local_contact.cIpAddress, requestUriHost) == 0) && (requestUriPort == (!portIsValid(local_contact.iPort) ? 5060 : local_contact.iPort))) { mContactId = local_contact.id; contactType = LOCAL_CONTACT ; } return contactType ;}void SipConnection::updateContact(Url* pContactUrl, CONTACT_TYPE eType){ UtlString useIp ; if ((mContactId == 0) && inviteMsg) { CONTACT_TYPE cType; cType = selectCompatibleContactType(*inviteMsg); mContactType = cType; } // get the Contact DB id that was set during // selectCompatibleContacts CONTACT_ADDRESS* pContact = sipUserAgent->getContactDb().find(mContactId); if (pContact == NULL) { if ((eType == AUTO) || (eType == NAT_MAPPED) || (eType == RELAY)) { pContact = sipUserAgent->getContactDb().findByType(NAT_MAPPED); } if (pContact == NULL) { pContact = sipUserAgent->getContactDb().findByType(LOCAL_CONTACT); } } if (pContact) { pContactUrl->setHostAddress(pContact->cIpAddress) ; pContactUrl->setHostPort(pContact->iPort) ; }}void SipConnection::buildLocalContact(Url fromUrl, UtlString& localContact) { UtlString contactHostPort; UtlString address; sipUserAgent->getContactUri(&contactHostPort); Url hostPort(contactHostPort); hostPort.getHostAddress(address); int port = hostPort.getHostPort(); UtlString displayName; UtlString userId; fromUrl.getDisplayName(displayName); fromUrl.getUserId(userId); Url contactUrl(mLocalContact, FALSE); contactUrl.setUserId(userId.data()); contactUrl.setDisplayName(displayName); contactUrl.setHostAddress(address); contactUrl.setHostPort(port); contactUrl.includeAngleBrackets(); updateContact(&contactUrl, mContactType) ; contactUrl.toString(localContact);}void SipConnection::buildLocalContact(UtlString& localContact){ UtlString contactHostPort; UtlString address; sipUserAgent->getContactUri(&contactHostPort); Url hostPort(contactHostPort); hostPort.getHostAddress(address); int port = hostPort.getHostPort(); Url contactUrl(mLocalContact, FALSE); contactUrl.setHostAddress(address); contactUrl.setHostPort(port); contactUrl.includeAngleBrackets(); updateContact(&contactUrl, mContactType) ; contactUrl.toString(localContact);}UtlBoolean SipConnection::dial(const char* dialString, const char* localLineAddress, const char* callId, const char* callController, const char* originalCallConnection, UtlBoolean requestQueuedCall, const void* pDisplay){ UtlBoolean dialOk = FALSE; SipMessage sipInvite; const char* callerDisplayName = NULL; int receiveRtpPort; int receiveRtcpPort; int receiveVideoRtpPort; int receiveVideoRtcpPort; SdpSrtpParameters srtpParams; UtlString rtpAddress; UtlString dummyFrom; UtlString fromAddress; UtlString goodToAddress; int cause = CONNECTION_CAUSE_NORMAL; if(getState() == CONNECTION_IDLE && mpMediaInterface != NULL) { // Create a new connection in the media flowgraph mpMediaInterface->createConnection(mConnectionId, (void*)pDisplay); mpMediaInterface->setContactType(mConnectionId, mContactType) ; SdpCodecFactory supportedCodecs; mpMediaInterface->getCapabilities(mConnectionId, rtpAddress, receiveRtpPort, receiveRtcpPort, receiveVideoRtpPort, receiveVideoRtcpPort, supportedCodecs, srtpParams); mRemoteIsCallee = TRUE; setCallId(callId); lastLocalSequenceNumber++; buildFromToAddresses(dialString, "xxxx", callerDisplayName, dummyFrom, goodToAddress); // The local address is always set mFromUrl.toString(fromAddress);#ifdef TEST_PRINT osPrintf("Using To address: \"%s\"\n", goodToAddress.data());#endif { //memory scope // Get the codecs int numCodecs; SdpCodec** rtpCodecsArray = NULL; supportedCodecs.getCodecs(numCodecs, rtpCodecsArray);#ifdef TEST_PRINT UtlString codecsString; supportedCodecs.toString(codecsString); osPrintf("SipConnection::dial codecs:\n%s\n", codecsString.data());#endif // Prepare to receive the codecs mpMediaInterface->startRtpReceive(mConnectionId, numCodecs, rtpCodecsArray, srtpParams); // Create a contact using the host & port from the // SipUserAgent and the display name and userId from // the from URL. // Create and send an INVITE sipInvite.setInviteData(fromAddress.data(), goodToAddress.data(), NULL, mLocalContact.data(), callId, rtpAddress.data(), receiveRtpPort, receiveRtcpPort, receiveVideoRtpPort, receiveVideoRtcpPort, &srtpParams, lastLocalSequenceNumber, numCodecs, rtpCodecsArray, mDefaultSessionReinviteTimer); // Set the asserted Identity if provided if(!mLocalPAssertedIdentity.isNull()) { sipInvite.addPAssertedIdentityField(mLocalPAssertedIdentity); } // Free up the codecs and the array for(int codecIndex = 0; codecIndex < numCodecs; codecIndex++) { delete rtpCodecsArray[codecIndex]; rtpCodecsArray[codecIndex] = NULL; } delete[] rtpCodecsArray; rtpCodecsArray = NULL; } if (callController && callController[0] != '\0') { fireSipXEvent(CALLSTATE_NEWCALL, CALLSTATE_NEW_CALL_TRANSFER) ; } // Set caller preference if caller wants queueing or campon if(requestQueuedCall) { sipInvite.addRequestDisposition(SIP_DISPOSITION_QUEUE); } // Set the requested by field (BYE Also style transfer) if(callController && originalCallConnection == NULL) { UtlString requestedByField(callController); const char* alsoTags = strchr(dialString, '>'); int uriIndex = requestedByField.index('<'); if(uriIndex < 0) { requestedByField.insert(0, '<'); requestedByField.append('>'); } if(alsoTags) { alsoTags++; requestedByField.append(alsoTags); } sipInvite.setRequestedByField(requestedByField.data()); cause = CONNECTION_CAUSE_TRANSFER; } // Set the header fields for REFER style transfer INVITE /*else*/ if(callController && originalCallConnection) { mOriginalCallConnectionAddress = originalCallConnection; sipInvite.setReferredByField(callController); cause = CONNECTION_CAUSE_TRANSFER; } // Save a copy of the invite inviteMsg = new SipMessage(sipInvite); inviteFromThisSide = TRUE; setCallerId(); setState(Connection::CONNECTION_ESTABLISHED, Connection::CONNECTION_LOCAL); if(!goodToAddress.isNull() && send(sipInvite)) { setState(CONNECTION_INITIATED, CONNECTION_REMOTE, cause);#ifdef TEST_PRINT osPrintf("INVITE sent successfully\n");#endif setState(CONNECTION_OFFERING, CONNECTION_REMOTE, cause); dialOk = TRUE; fireSipXEvent(CALLSTATE_REMOTE_OFFERING, CALLSTATE_REMOTE_OFFERING_NORMAL) ; } else {#ifdef TEST_PRINT osPrintf("INVITE send failed\n");#endif setState(CONNECTION_FAILED, CONNECTION_REMOTE, CONNECTION_CAUSE_DEST_NOT_OBTAINABLE); fireSipXEvent(CALLSTATE_DISCONNECTED, CALLSTATE_DISCONNECTED_BADADDRESS) ; // Failed to send a message for transfer if(callController && !goodToAddress.isNull()) { // Send back a status to the original call UtlString originalCallId; mpCall->getOriginalCallId(originalCallId); CpMultiStringMessage transfereeStatus(CallManager::CP_TRANSFEREE_CONNECTION_STATUS, originalCallId.data(), mOriginalCallConnectionAddress.data(), NULL, NULL, NULL, CONNECTION_FAILED, SIP_REQUEST_TIMEOUT_CODE);#ifdef TEST_PRINT osPrintf("SipConnection::dial posting CP_TRANSFEREE_CONNECTION_STATUS to call: %s\n", originalCallId.data());#endif mpCallManager->postMessage(transfereeStatus); } } } return(dialOk);}UtlBoolean SipConnection::sendInfo(UtlString contentType, UtlString sContent){ bool bRet = false; SipMessage sipInfoMessage; UtlString fromAddress; UtlString toAddress; UtlString callId; UtlString uri ; mToUrl.getUri(uri) ; getFromField(&fromAddress); getRemoteAddress(&toAddress); getCallId(&callId); sipInfoMessage.setRequestData(SIP_INFO_METHOD, uri, fromAddress, toAddress, callId); sipInfoMessage.setContactField(mLocalContact.data()); sipInfoMessage.setContentType(contentType.data()); sipInfoMessage.setContentLength(sContent.length()); HttpBody* pBody = new HttpBody(sContent.data(), sContent.length()); sipInfoMessage.setBody(pBody); sipInfoMessage.setCSeqField(lastLocalSequenceNumber++, SIP_INFO_METHOD); if(send(sipInfoMessage, sipUserAgent->getMessageQueue())) { bRet = true; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -