michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "CSFLog.h" michael@0: #include "nspr.h" michael@0: michael@0: // For rtcp-fb constants michael@0: #include "ccsdp.h" michael@0: michael@0: #include "VideoConduit.h" michael@0: #include "AudioConduit.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "LoadManager.h" michael@0: #include "YuvStamper.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsIPrefService.h" michael@0: #include "nsIPrefBranch.h" michael@0: michael@0: #include "webrtc/common_video/interface/native_handle.h" michael@0: #include "webrtc/video_engine/include/vie_errors.h" michael@0: michael@0: #ifdef MOZ_WIDGET_ANDROID michael@0: #include "AndroidJNIWrapper.h" michael@0: #endif michael@0: michael@0: #include michael@0: #include michael@0: michael@0: namespace mozilla { michael@0: michael@0: static const char* logTag ="WebrtcVideoSessionConduit"; michael@0: michael@0: // 32 bytes is what WebRTC CodecInst expects michael@0: const unsigned int WebrtcVideoConduit::CODEC_PLNAME_SIZE = 32; michael@0: michael@0: /** michael@0: * Factory Method for VideoConduit michael@0: */ michael@0: mozilla::RefPtr VideoSessionConduit::Create(VideoSessionConduit *aOther) michael@0: { michael@0: #ifdef MOZILLA_INTERNAL_API michael@0: // unit tests create their own "main thread" michael@0: NS_ASSERTION(NS_IsMainThread(), "Only call on main thread"); michael@0: #endif michael@0: CSFLogDebug(logTag, "%s ", __FUNCTION__); michael@0: michael@0: WebrtcVideoConduit* obj = new WebrtcVideoConduit(); michael@0: if(obj->Init(static_cast(aOther)) != kMediaConduitNoError) michael@0: { michael@0: CSFLogError(logTag, "%s VideoConduit Init Failed ", __FUNCTION__); michael@0: delete obj; michael@0: return nullptr; michael@0: } michael@0: CSFLogDebug(logTag, "%s Successfully created VideoConduit ", __FUNCTION__); michael@0: return obj; michael@0: } michael@0: michael@0: WebrtcVideoConduit::~WebrtcVideoConduit() michael@0: { michael@0: #ifdef MOZILLA_INTERNAL_API michael@0: // unit tests create their own "main thread" michael@0: NS_ASSERTION(NS_IsMainThread(), "Only call on main thread"); michael@0: #endif michael@0: CSFLogDebug(logTag, "%s ", __FUNCTION__); michael@0: michael@0: for(std::vector::size_type i=0;i < mRecvCodecList.size();i++) michael@0: { michael@0: delete mRecvCodecList[i]; michael@0: } michael@0: michael@0: delete mCurSendCodecConfig; michael@0: michael@0: // The first one of a pair to be deleted shuts down media for both michael@0: //Deal with External Capturer michael@0: if(mPtrViECapture) michael@0: { michael@0: if (!mShutDown) { michael@0: mPtrViECapture->DisconnectCaptureDevice(mCapId); michael@0: mPtrViECapture->ReleaseCaptureDevice(mCapId); michael@0: mPtrExtCapture = nullptr; michael@0: if (mOtherDirection) michael@0: mOtherDirection->mPtrExtCapture = nullptr; michael@0: } michael@0: } michael@0: michael@0: //Deal with External Renderer michael@0: if(mPtrViERender) michael@0: { michael@0: if (!mShutDown) { michael@0: if(mRenderer) { michael@0: mPtrViERender->StopRender(mChannel); michael@0: } michael@0: mPtrViERender->RemoveRenderer(mChannel); michael@0: } michael@0: } michael@0: michael@0: //Deal with the transport michael@0: if(mPtrViENetwork) michael@0: { michael@0: if (!mShutDown) { michael@0: mPtrViENetwork->DeregisterSendTransport(mChannel); michael@0: } michael@0: } michael@0: michael@0: if(mPtrViEBase) michael@0: { michael@0: if (!mShutDown) { michael@0: mPtrViEBase->StopSend(mChannel); michael@0: mPtrViEBase->StopReceive(mChannel); michael@0: SyncTo(nullptr); michael@0: mPtrViEBase->DeleteChannel(mChannel); michael@0: } michael@0: } michael@0: michael@0: if (mOtherDirection) michael@0: { michael@0: // mOtherDirection owns these now! michael@0: mOtherDirection->mOtherDirection = nullptr; michael@0: // let other side we terminated the channel michael@0: mOtherDirection->mShutDown = true; michael@0: mVideoEngine = nullptr; michael@0: } else { michael@0: // We can't delete the VideoEngine until all these are released! michael@0: // And we can't use a Scoped ptr, since the order is arbitrary michael@0: mPtrViEBase = nullptr; michael@0: mPtrViECapture = nullptr; michael@0: mPtrViECodec = nullptr; michael@0: mPtrViENetwork = nullptr; michael@0: mPtrViERender = nullptr; michael@0: mPtrRTP = nullptr; michael@0: mPtrExtCodec = nullptr; michael@0: michael@0: // only one opener can call Delete. Have it be the last to close. michael@0: if(mVideoEngine) michael@0: { michael@0: webrtc::VideoEngine::Delete(mVideoEngine); michael@0: } michael@0: } michael@0: } michael@0: michael@0: bool WebrtcVideoConduit::GetLocalSSRC(unsigned int* ssrc) { michael@0: return !mPtrRTP->GetLocalSSRC(mChannel, *ssrc); michael@0: } michael@0: michael@0: bool WebrtcVideoConduit::GetRemoteSSRC(unsigned int* ssrc) { michael@0: return !mPtrRTP->GetRemoteSSRC(mChannel, *ssrc); michael@0: } michael@0: michael@0: bool WebrtcVideoConduit::GetAVStats(int32_t* jitterBufferDelayMs, michael@0: int32_t* playoutBufferDelayMs, michael@0: int32_t* avSyncOffsetMs) { michael@0: return false; michael@0: } michael@0: michael@0: bool WebrtcVideoConduit::GetRTPStats(unsigned int* jitterMs, michael@0: unsigned int* cumulativeLost) { michael@0: unsigned short fractionLost; michael@0: unsigned extendedMax; michael@0: int rttMs; michael@0: // GetReceivedRTCPStatistics is a poorly named GetRTPStatistics variant michael@0: return !mPtrRTP->GetReceivedRTCPStatistics(mChannel, fractionLost, michael@0: *cumulativeLost, michael@0: extendedMax, michael@0: *jitterMs, michael@0: rttMs); michael@0: } michael@0: michael@0: bool WebrtcVideoConduit::GetRTCPReceiverReport(DOMHighResTimeStamp* timestamp, michael@0: uint32_t* jitterMs, michael@0: uint32_t* packetsReceived, michael@0: uint64_t* bytesReceived, michael@0: uint32_t* cumulativeLost, michael@0: int32_t* rttMs) { michael@0: uint32_t ntpHigh, ntpLow; michael@0: uint16_t fractionLost; michael@0: bool result = !mPtrRTP->GetRemoteRTCPReceiverInfo(mChannel, ntpHigh, ntpLow, michael@0: *packetsReceived, michael@0: *bytesReceived, michael@0: jitterMs, michael@0: &fractionLost, michael@0: cumulativeLost, michael@0: rttMs); michael@0: if (result) { michael@0: *timestamp = NTPtoDOMHighResTimeStamp(ntpHigh, ntpLow); michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: bool WebrtcVideoConduit::GetRTCPSenderReport(DOMHighResTimeStamp* timestamp, michael@0: unsigned int* packetsSent, michael@0: uint64_t* bytesSent) { michael@0: struct webrtc::SenderInfo senderInfo; michael@0: bool result = !mPtrRTP->GetRemoteRTCPSenderInfo(mChannel, &senderInfo); michael@0: if (result) { michael@0: *timestamp = NTPtoDOMHighResTimeStamp(senderInfo.NTP_timestamp_high, michael@0: senderInfo.NTP_timestamp_low); michael@0: *packetsSent = senderInfo.sender_packet_count; michael@0: *bytesSent = senderInfo.sender_octet_count; michael@0: } michael@0: return result; michael@0: } michael@0: michael@0: /** michael@0: * Peforms intialization of the MANDATORY components of the Video Engine michael@0: */ michael@0: MediaConduitErrorCode WebrtcVideoConduit::Init(WebrtcVideoConduit *other) michael@0: { michael@0: CSFLogDebug(logTag, "%s this=%p other=%p", __FUNCTION__, this, other); michael@0: michael@0: if (other) { michael@0: MOZ_ASSERT(!other->mOtherDirection); michael@0: other->mOtherDirection = this; michael@0: mOtherDirection = other; michael@0: michael@0: // only one can call ::Create()/GetVideoEngine() michael@0: MOZ_ASSERT(other->mVideoEngine); michael@0: mVideoEngine = other->mVideoEngine; michael@0: } else { michael@0: michael@0: #ifdef MOZ_WIDGET_ANDROID michael@0: jobject context = jsjni_GetGlobalContextRef(); michael@0: michael@0: // get the JVM michael@0: JavaVM *jvm = jsjni_GetVM(); michael@0: michael@0: if (webrtc::VideoEngine::SetAndroidObjects(jvm, (void*)context) != 0) { michael@0: CSFLogError(logTag, "%s: could not set Android objects", __FUNCTION__); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: #endif michael@0: michael@0: // Per WebRTC APIs below function calls return nullptr on failure michael@0: if( !(mVideoEngine = webrtc::VideoEngine::Create()) ) michael@0: { michael@0: CSFLogError(logTag, "%s Unable to create video engine ", __FUNCTION__); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: michael@0: PRLogModuleInfo *logs = GetWebRTCLogInfo(); michael@0: if (!gWebrtcTraceLoggingOn && logs && logs->level > 0) { michael@0: // no need to a critical section or lock here michael@0: gWebrtcTraceLoggingOn = 1; michael@0: michael@0: const char *file = PR_GetEnv("WEBRTC_TRACE_FILE"); michael@0: if (!file) { michael@0: file = "WebRTC.log"; michael@0: } michael@0: CSFLogDebug(logTag, "%s Logging webrtc to %s level %d", __FUNCTION__, michael@0: file, logs->level); michael@0: mVideoEngine->SetTraceFilter(logs->level); michael@0: mVideoEngine->SetTraceFile(file); michael@0: } michael@0: } michael@0: michael@0: if( !(mPtrViEBase = ViEBase::GetInterface(mVideoEngine))) michael@0: { michael@0: CSFLogError(logTag, "%s Unable to get video base interface ", __FUNCTION__); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: michael@0: if( !(mPtrViECapture = ViECapture::GetInterface(mVideoEngine))) michael@0: { michael@0: CSFLogError(logTag, "%s Unable to get video capture interface", __FUNCTION__); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: michael@0: if( !(mPtrViECodec = ViECodec::GetInterface(mVideoEngine))) michael@0: { michael@0: CSFLogError(logTag, "%s Unable to get video codec interface ", __FUNCTION__); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: michael@0: if( !(mPtrViENetwork = ViENetwork::GetInterface(mVideoEngine))) michael@0: { michael@0: CSFLogError(logTag, "%s Unable to get video network interface ", __FUNCTION__); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: michael@0: if( !(mPtrViERender = ViERender::GetInterface(mVideoEngine))) michael@0: { michael@0: CSFLogError(logTag, "%s Unable to get video render interface ", __FUNCTION__); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: michael@0: if( !(mPtrRTP = webrtc::ViERTP_RTCP::GetInterface(mVideoEngine))) michael@0: { michael@0: CSFLogError(logTag, "%s Unable to get video RTCP interface ", __FUNCTION__); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: michael@0: if ( !(mPtrExtCodec = webrtc::ViEExternalCodec::GetInterface(mVideoEngine))) michael@0: { michael@0: CSFLogError(logTag, "%s Unable to get external codec interface %d ", michael@0: __FUNCTION__, mPtrViEBase->LastError()); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: michael@0: if (other) { michael@0: mChannel = other->mChannel; michael@0: mPtrExtCapture = other->mPtrExtCapture; michael@0: mCapId = other->mCapId; michael@0: } else { michael@0: CSFLogDebug(logTag, "%s Engine Created: Init'ng the interfaces ",__FUNCTION__); michael@0: michael@0: if(mPtrViEBase->Init() == -1) michael@0: { michael@0: CSFLogError(logTag, " %s Video Engine Init Failed %d ",__FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: michael@0: if(mPtrViEBase->CreateChannel(mChannel) == -1) michael@0: { michael@0: CSFLogError(logTag, " %s Channel creation Failed %d ",__FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitChannelError; michael@0: } michael@0: michael@0: if(mPtrViENetwork->RegisterSendTransport(mChannel, *this) == -1) michael@0: { michael@0: CSFLogError(logTag, "%s ViENetwork Failed %d ", __FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitTransportRegistrationFail; michael@0: } michael@0: michael@0: if(mPtrViECapture->AllocateExternalCaptureDevice(mCapId, michael@0: mPtrExtCapture) == -1) michael@0: { michael@0: CSFLogError(logTag, "%s Unable to Allocate capture module: %d ", michael@0: __FUNCTION__, mPtrViEBase->LastError()); michael@0: return kMediaConduitCaptureError; michael@0: } michael@0: michael@0: if(mPtrViECapture->ConnectCaptureDevice(mCapId,mChannel) == -1) michael@0: { michael@0: CSFLogError(logTag, "%s Unable to Connect capture module: %d ", michael@0: __FUNCTION__,mPtrViEBase->LastError()); michael@0: return kMediaConduitCaptureError; michael@0: } michael@0: michael@0: if(mPtrViERender->AddRenderer(mChannel, michael@0: webrtc::kVideoI420, michael@0: (webrtc::ExternalRenderer*) this) == -1) michael@0: { michael@0: CSFLogError(logTag, "%s Failed to added external renderer ", __FUNCTION__); michael@0: return kMediaConduitInvalidRenderer; michael@0: } michael@0: // Set up some parameters, per juberti. Set MTU. michael@0: if(mPtrViENetwork->SetMTU(mChannel, 1200) != 0) michael@0: { michael@0: CSFLogError(logTag, "%s MTU Failed %d ", __FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitMTUError; michael@0: } michael@0: // Turn on RTCP and loss feedback reporting. michael@0: if(mPtrRTP->SetRTCPStatus(mChannel, webrtc::kRtcpCompound_RFC4585) != 0) michael@0: { michael@0: CSFLogError(logTag, "%s RTCPStatus Failed %d ", __FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitRTCPStatusError; michael@0: } michael@0: } michael@0: michael@0: CSFLogError(logTag, "%s Initialization Done", __FUNCTION__); michael@0: return kMediaConduitNoError; michael@0: } michael@0: michael@0: void michael@0: WebrtcVideoConduit::SyncTo(WebrtcAudioConduit *aConduit) michael@0: { michael@0: CSFLogDebug(logTag, "%s Synced to %p", __FUNCTION__, aConduit); michael@0: michael@0: // SyncTo(value) syncs to the AudioConduit, and if already synced replaces michael@0: // the current sync target. SyncTo(nullptr) cancels any existing sync and michael@0: // releases the strong ref to AudioConduit. michael@0: if (aConduit) { michael@0: mPtrViEBase->SetVoiceEngine(aConduit->GetVoiceEngine()); michael@0: mPtrViEBase->ConnectAudioChannel(mChannel, aConduit->GetChannel()); michael@0: // NOTE: this means the VideoConduit will keep the AudioConduit alive! michael@0: } else if ((mOtherDirection && mOtherDirection->mSyncedTo) || mSyncedTo) { michael@0: mPtrViEBase->DisconnectAudioChannel(mChannel); michael@0: mPtrViEBase->SetVoiceEngine(nullptr); michael@0: } michael@0: michael@0: // Now manage the shared sync reference (ugly) michael@0: if (mSyncedTo || !mOtherDirection ) { michael@0: mSyncedTo = aConduit; michael@0: } else { michael@0: mOtherDirection->mSyncedTo = aConduit; michael@0: } michael@0: } michael@0: michael@0: MediaConduitErrorCode michael@0: WebrtcVideoConduit::AttachRenderer(mozilla::RefPtr aVideoRenderer) michael@0: { michael@0: CSFLogDebug(logTag, "%s ", __FUNCTION__); michael@0: michael@0: //null renderer michael@0: if(!aVideoRenderer) michael@0: { michael@0: CSFLogError(logTag, "%s NULL Renderer", __FUNCTION__); michael@0: MOZ_ASSERT(PR_FALSE); michael@0: return kMediaConduitInvalidRenderer; michael@0: } michael@0: michael@0: //Start Rendering if we haven't already michael@0: if(!mRenderer) michael@0: { michael@0: mRenderer = aVideoRenderer; // must be done before StartRender() michael@0: michael@0: if(mPtrViERender->StartRender(mChannel) == -1) michael@0: { michael@0: CSFLogError(logTag, "%s Starting the Renderer Failed %d ", __FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: mRenderer = nullptr; michael@0: return kMediaConduitRendererFail; michael@0: } michael@0: } else { michael@0: //Assign the new renderer - overwrites if there is already one michael@0: mRenderer = aVideoRenderer; michael@0: } michael@0: michael@0: return kMediaConduitNoError; michael@0: } michael@0: michael@0: void michael@0: WebrtcVideoConduit::DetachRenderer() michael@0: { michael@0: if(mRenderer) michael@0: { michael@0: mPtrViERender->StopRender(mChannel); michael@0: mRenderer = nullptr; michael@0: } michael@0: } michael@0: michael@0: MediaConduitErrorCode michael@0: WebrtcVideoConduit::AttachTransport(mozilla::RefPtr aTransport) michael@0: { michael@0: CSFLogDebug(logTag, "%s ", __FUNCTION__); michael@0: if(!aTransport) michael@0: { michael@0: CSFLogError(logTag, "%s NULL Transport", __FUNCTION__); michael@0: return kMediaConduitInvalidTransport; michael@0: } michael@0: // set the transport michael@0: mTransport = aTransport; michael@0: return kMediaConduitNoError; michael@0: } michael@0: michael@0: /** michael@0: * Note: Setting the send-codec on the Video Engine will restart the encoder, michael@0: * sets up new SSRC and reset RTP_RTCP module with the new codec setting. michael@0: */ michael@0: MediaConduitErrorCode michael@0: WebrtcVideoConduit::ConfigureSendMediaCodec(const VideoCodecConfig* codecConfig) michael@0: { michael@0: CSFLogDebug(logTag, "%s ", __FUNCTION__); michael@0: bool codecFound = false; michael@0: MediaConduitErrorCode condError = kMediaConduitNoError; michael@0: int error = 0; //webrtc engine errors michael@0: webrtc::VideoCodec video_codec; michael@0: std::string payloadName; michael@0: michael@0: //validate basic params michael@0: if((condError = ValidateCodecConfig(codecConfig,true)) != kMediaConduitNoError) michael@0: { michael@0: return condError; michael@0: } michael@0: michael@0: //Check if we have same codec already applied michael@0: if(CheckCodecsForMatch(mCurSendCodecConfig, codecConfig)) michael@0: { michael@0: CSFLogDebug(logTag, "%s Codec has been applied already ", __FUNCTION__); michael@0: return kMediaConduitCodecInUse; michael@0: } michael@0: michael@0: //transmitting already ? michael@0: if(mEngineTransmitting) michael@0: { michael@0: CSFLogDebug(logTag, "%s Engine Already Sending. Attemping to Stop ", __FUNCTION__); michael@0: if(mPtrViEBase->StopSend(mChannel) == -1) michael@0: { michael@0: CSFLogError(logTag, "%s StopSend() Failed %d ",__FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitUnknownError; michael@0: } michael@0: } michael@0: michael@0: mEngineTransmitting = false; michael@0: michael@0: if (codecConfig->mLoadManager) { michael@0: mPtrViEBase->RegisterCpuOveruseObserver(mChannel, codecConfig->mLoadManager); michael@0: mPtrViEBase->SetLoadManager(codecConfig->mLoadManager); michael@0: } michael@0: michael@0: // we should be good here to set the new codec. michael@0: for(int idx=0; idx < mPtrViECodec->NumberOfCodecs(); idx++) michael@0: { michael@0: if(0 == mPtrViECodec->GetCodec(idx, video_codec)) michael@0: { michael@0: payloadName = video_codec.plName; michael@0: if(codecConfig->mName.compare(payloadName) == 0) michael@0: { michael@0: CodecConfigToWebRTCCodec(codecConfig, video_codec); michael@0: codecFound = true; michael@0: break; michael@0: } michael@0: } michael@0: }//for michael@0: michael@0: if(codecFound == false) michael@0: { michael@0: CSFLogError(logTag, "%s Codec Mismatch ", __FUNCTION__); michael@0: return kMediaConduitInvalidSendCodec; michael@0: } michael@0: michael@0: if(mPtrViECodec->SetSendCodec(mChannel, video_codec) == -1) michael@0: { michael@0: error = mPtrViEBase->LastError(); michael@0: if(error == kViECodecInvalidCodec) michael@0: { michael@0: CSFLogError(logTag, "%s Invalid Send Codec", __FUNCTION__); michael@0: return kMediaConduitInvalidSendCodec; michael@0: } michael@0: CSFLogError(logTag, "%s SetSendCodec Failed %d ", __FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitUnknownError; michael@0: } michael@0: mSendingWidth = 0; michael@0: mSendingHeight = 0; michael@0: michael@0: if(codecConfig->RtcpFbIsSet(SDP_RTCP_FB_NACK_BASIC)) { michael@0: CSFLogDebug(logTag, "Enabling NACK (send) for video stream\n"); michael@0: if (mPtrRTP->SetNACKStatus(mChannel, true) != 0) michael@0: { michael@0: CSFLogError(logTag, "%s NACKStatus Failed %d ", __FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitNACKStatusError; michael@0: } michael@0: } michael@0: michael@0: if(mPtrViEBase->StartSend(mChannel) == -1) michael@0: { michael@0: CSFLogError(logTag, "%s Start Send Error %d ", __FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitUnknownError; michael@0: } michael@0: michael@0: //Copy the applied config for future reference. michael@0: delete mCurSendCodecConfig; michael@0: michael@0: mCurSendCodecConfig = new VideoCodecConfig(*codecConfig); michael@0: michael@0: mPtrRTP->SetRembStatus(mChannel, true, false); michael@0: michael@0: // by now we should be successfully started the transmission michael@0: mEngineTransmitting = true; michael@0: return kMediaConduitNoError; michael@0: } michael@0: michael@0: MediaConduitErrorCode michael@0: WebrtcVideoConduit::ConfigureRecvMediaCodecs( michael@0: const std::vector& codecConfigList) michael@0: { michael@0: CSFLogDebug(logTag, "%s ", __FUNCTION__); michael@0: MediaConduitErrorCode condError = kMediaConduitNoError; michael@0: int error = 0; //webrtc engine errors michael@0: bool success = false; michael@0: std::string payloadName; michael@0: michael@0: // are we receiving already? If so, stop receiving and playout michael@0: // since we can't apply new recv codec when the engine is playing. michael@0: if(mEngineReceiving) michael@0: { michael@0: CSFLogDebug(logTag, "%s Engine Already Receiving . Attemping to Stop ", __FUNCTION__); michael@0: if(mPtrViEBase->StopReceive(mChannel) == -1) michael@0: { michael@0: error = mPtrViEBase->LastError(); michael@0: if(error == kViEBaseUnknownError) michael@0: { michael@0: CSFLogDebug(logTag, "%s StopReceive() Success ", __FUNCTION__); michael@0: mEngineReceiving = false; michael@0: } else { michael@0: CSFLogError(logTag, "%s StopReceive() Failed %d ", __FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitUnknownError; michael@0: } michael@0: } michael@0: } michael@0: michael@0: mEngineReceiving = false; michael@0: michael@0: if(codecConfigList.empty()) michael@0: { michael@0: CSFLogError(logTag, "%s Zero number of codecs to configure", __FUNCTION__); michael@0: return kMediaConduitMalformedArgument; michael@0: } michael@0: michael@0: webrtc::ViEKeyFrameRequestMethod kf_request = webrtc::kViEKeyFrameRequestNone; michael@0: bool use_nack_basic = false; michael@0: michael@0: //Try Applying the codecs in the list michael@0: // we treat as success if atleast one codec was applied and reception was michael@0: // started successfully. michael@0: for(std::vector::size_type i=0;i < codecConfigList.size();i++) michael@0: { michael@0: //if the codec param is invalid or diplicate, return error michael@0: if((condError = ValidateCodecConfig(codecConfigList[i],false)) != kMediaConduitNoError) michael@0: { michael@0: return condError; michael@0: } michael@0: michael@0: // Check for the keyframe request type: PLI is preferred michael@0: // over FIR, and FIR is preferred over none. michael@0: if (codecConfigList[i]->RtcpFbIsSet(SDP_RTCP_FB_NACK_PLI)) michael@0: { michael@0: kf_request = webrtc::kViEKeyFrameRequestPliRtcp; michael@0: } else if(kf_request == webrtc::kViEKeyFrameRequestNone && michael@0: codecConfigList[i]->RtcpFbIsSet(SDP_RTCP_FB_CCM_FIR)) michael@0: { michael@0: kf_request = webrtc::kViEKeyFrameRequestFirRtcp; michael@0: } michael@0: michael@0: // Check whether NACK is requested michael@0: if(codecConfigList[i]->RtcpFbIsSet(SDP_RTCP_FB_NACK_BASIC)) michael@0: { michael@0: use_nack_basic = true; michael@0: } michael@0: michael@0: webrtc::VideoCodec video_codec; michael@0: michael@0: mEngineReceiving = false; michael@0: memset(&video_codec, 0, sizeof(webrtc::VideoCodec)); michael@0: //Retrieve pre-populated codec structure for our codec. michael@0: for(int idx=0; idx < mPtrViECodec->NumberOfCodecs(); idx++) michael@0: { michael@0: if(mPtrViECodec->GetCodec(idx, video_codec) == 0) michael@0: { michael@0: payloadName = video_codec.plName; michael@0: if(codecConfigList[i]->mName.compare(payloadName) == 0) michael@0: { michael@0: CodecConfigToWebRTCCodec(codecConfigList[i], video_codec); michael@0: if(mPtrViECodec->SetReceiveCodec(mChannel,video_codec) == -1) michael@0: { michael@0: CSFLogError(logTag, "%s Invalid Receive Codec %d ", __FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: } else { michael@0: CSFLogError(logTag, "%s Successfully Set the codec %s", __FUNCTION__, michael@0: codecConfigList[i]->mName.c_str()); michael@0: if(CopyCodecToDB(codecConfigList[i])) michael@0: { michael@0: success = true; michael@0: } else { michael@0: CSFLogError(logTag,"%s Unable to updated Codec Database", __FUNCTION__); michael@0: return kMediaConduitUnknownError; michael@0: } michael@0: } michael@0: break; //we found a match michael@0: } michael@0: } michael@0: }//end for codeclist michael@0: michael@0: }//end for michael@0: michael@0: if(!success) michael@0: { michael@0: CSFLogError(logTag, "%s Setting Receive Codec Failed ", __FUNCTION__); michael@0: return kMediaConduitInvalidReceiveCodec; michael@0: } michael@0: michael@0: // XXX Currently, we gather up all of the feedback types that the remote michael@0: // party indicated it supports for all video codecs and configure the entire michael@0: // conduit based on those capabilities. This is technically out of spec, michael@0: // as these values should be configured on a per-codec basis. However, michael@0: // the video engine only provides this API on a per-conduit basis, so that's michael@0: // how we have to do it. The approach of considering the remote capablities michael@0: // for the entire conduit to be a union of all remote codec capabilities michael@0: // (rather than the more conservative approach of using an intersection) michael@0: // is made to provide as many feedback mechanisms as are likely to be michael@0: // processed by the remote party (and should be relatively safe, since the michael@0: // remote party is required to ignore feedback types that it does not michael@0: // understand). michael@0: // michael@0: // Note that our configuration uses this union of remote capabilites as michael@0: // input to the configuration. It is not isomorphic to the configuration. michael@0: // For example, it only makes sense to have one frame request mechanism michael@0: // active at a time; so, if the remote party indicates more than one michael@0: // supported mechanism, we're only configuring the one we most prefer. michael@0: // michael@0: // See http://code.google.com/p/webrtc/issues/detail?id=2331 michael@0: michael@0: if (kf_request != webrtc::kViEKeyFrameRequestNone) michael@0: { michael@0: CSFLogDebug(logTag, "Enabling %s frame requests for video stream\n", michael@0: (kf_request == webrtc::kViEKeyFrameRequestPliRtcp ? michael@0: "PLI" : "FIR")); michael@0: if(mPtrRTP->SetKeyFrameRequestMethod(mChannel, kf_request) != 0) michael@0: { michael@0: CSFLogError(logTag, "%s KeyFrameRequest Failed %d ", __FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitKeyFrameRequestError; michael@0: } michael@0: } michael@0: michael@0: switch (kf_request) { michael@0: case webrtc::kViEKeyFrameRequestNone: michael@0: mFrameRequestMethod = FrameRequestNone; michael@0: break; michael@0: case webrtc::kViEKeyFrameRequestPliRtcp: michael@0: mFrameRequestMethod = FrameRequestPli; michael@0: break; michael@0: case webrtc::kViEKeyFrameRequestFirRtcp: michael@0: mFrameRequestMethod = FrameRequestFir; michael@0: break; michael@0: default: michael@0: MOZ_ASSERT(PR_FALSE); michael@0: mFrameRequestMethod = FrameRequestUnknown; michael@0: } michael@0: michael@0: if(use_nack_basic) michael@0: { michael@0: CSFLogDebug(logTag, "Enabling NACK (recv) for video stream\n"); michael@0: if (mPtrRTP->SetNACKStatus(mChannel, true) != 0) michael@0: { michael@0: CSFLogError(logTag, "%s NACKStatus Failed %d ", __FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitNACKStatusError; michael@0: } michael@0: } michael@0: mUsingNackBasic = use_nack_basic; michael@0: michael@0: //Start Receive on the video engine michael@0: if(mPtrViEBase->StartReceive(mChannel) == -1) michael@0: { michael@0: error = mPtrViEBase->LastError(); michael@0: CSFLogError(logTag, "%s Start Receive Error %d ", __FUNCTION__, error); michael@0: michael@0: michael@0: return kMediaConduitUnknownError; michael@0: } michael@0: michael@0: #ifdef MOZILLA_INTERNAL_API michael@0: if (NS_IsMainThread()) { michael@0: nsresult rv; michael@0: nsCOMPtr prefs = do_GetService("@mozilla.org/preferences-service;1", &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: nsCOMPtr branch = do_QueryInterface(prefs); michael@0: michael@0: if (branch) { michael@0: branch->GetBoolPref("media.video.test_latency", &mVideoLatencyTestEnable); michael@0: } michael@0: } michael@0: } michael@0: #endif michael@0: michael@0: // by now we should be successfully started the reception michael@0: mPtrRTP->SetRembStatus(mChannel, false, true); michael@0: mEngineReceiving = true; michael@0: DumpCodecDB(); michael@0: return kMediaConduitNoError; michael@0: } michael@0: michael@0: // XXX we need to figure out how to feed back changes in preferred capture michael@0: // resolution to the getUserMedia source michael@0: bool michael@0: WebrtcVideoConduit::SelectSendResolution(unsigned short width, michael@0: unsigned short height) michael@0: { michael@0: // XXX This will do bandwidth-resolution adaptation as well - bug 877954 michael@0: michael@0: // Limit resolution to max-fs while keeping same aspect ratio as the michael@0: // incoming image. michael@0: if (mCurSendCodecConfig && mCurSendCodecConfig->mMaxFrameSize) michael@0: { michael@0: unsigned int cur_fs, max_width, max_height, mb_width, mb_height, mb_max; michael@0: michael@0: mb_width = (width + 15) >> 4; michael@0: mb_height = (height + 15) >> 4; michael@0: michael@0: cur_fs = mb_width * mb_height; michael@0: michael@0: // Limit resolution to max_fs, but don't scale up. michael@0: if (cur_fs > mCurSendCodecConfig->mMaxFrameSize) michael@0: { michael@0: double scale_ratio; michael@0: michael@0: scale_ratio = sqrt((double) mCurSendCodecConfig->mMaxFrameSize / michael@0: (double) cur_fs); michael@0: michael@0: mb_width = mb_width * scale_ratio; michael@0: mb_height = mb_height * scale_ratio; michael@0: michael@0: // Adjust mb_width and mb_height if they were truncated to zero. michael@0: if (mb_width == 0) { michael@0: mb_width = 1; michael@0: mb_height = std::min(mb_height, mCurSendCodecConfig->mMaxFrameSize); michael@0: } michael@0: if (mb_height == 0) { michael@0: mb_height = 1; michael@0: mb_width = std::min(mb_width, mCurSendCodecConfig->mMaxFrameSize); michael@0: } michael@0: } michael@0: michael@0: // Limit width/height seperately to limit effect of extreme aspect ratios. michael@0: mb_max = (unsigned) sqrt(8 * (double) mCurSendCodecConfig->mMaxFrameSize); michael@0: michael@0: max_width = 16 * std::min(mb_width, mb_max); michael@0: max_height = 16 * std::min(mb_height, mb_max); michael@0: michael@0: if (width * max_height > max_width * height) michael@0: { michael@0: if (width > max_width) michael@0: { michael@0: // Due to the value is truncated to integer here and forced to even michael@0: // value later, adding 1 to improve accuracy. michael@0: height = max_width * height / width + 1; michael@0: width = max_width; michael@0: } michael@0: } michael@0: else michael@0: { michael@0: if (height > max_height) michael@0: { michael@0: // Due to the value is truncated to integer here and forced to even michael@0: // value later, adding 1 to improve accuracy. michael@0: width = max_height * width / height + 1; michael@0: height = max_height; michael@0: } michael@0: } michael@0: michael@0: // Favor even multiples of pixels for width and height. michael@0: width = std::max(width & ~1, 2); michael@0: height = std::max(height & ~1, 2); michael@0: } michael@0: michael@0: // Adapt to getUserMedia resolution changes michael@0: // check if we need to reconfigure the sending resolution michael@0: if (mSendingWidth != width || mSendingHeight != height) michael@0: { michael@0: // This will avoid us continually retrying this operation if it fails. michael@0: // If the resolution changes, we'll try again. In the meantime, we'll michael@0: // keep using the old size in the encoder. michael@0: mSendingWidth = width; michael@0: mSendingHeight = height; michael@0: michael@0: // Get current vie codec. michael@0: webrtc::VideoCodec vie_codec; michael@0: int32_t err; michael@0: michael@0: if ((err = mPtrViECodec->GetSendCodec(mChannel, vie_codec)) != 0) michael@0: { michael@0: CSFLogError(logTag, "%s: GetSendCodec failed, err %d", __FUNCTION__, err); michael@0: return false; michael@0: } michael@0: if (vie_codec.width != width || vie_codec.height != height) michael@0: { michael@0: vie_codec.width = width; michael@0: vie_codec.height = height; michael@0: michael@0: if ((err = mPtrViECodec->SetSendCodec(mChannel, vie_codec)) != 0) michael@0: { michael@0: CSFLogError(logTag, "%s: SetSendCodec(%ux%u) failed, err %d", michael@0: __FUNCTION__, width, height, err); michael@0: return false; michael@0: } michael@0: CSFLogDebug(logTag, "%s: Encoder resolution changed to %ux%u", michael@0: __FUNCTION__, width, height); michael@0: } // else no change; mSendingWidth likely was 0 michael@0: } michael@0: return true; michael@0: } michael@0: michael@0: MediaConduitErrorCode michael@0: WebrtcVideoConduit::SetExternalSendCodec(int pltype, michael@0: VideoEncoder* encoder) { michael@0: int ret = mPtrExtCodec->RegisterExternalSendCodec(mChannel, michael@0: pltype, michael@0: static_cast(encoder), michael@0: false); michael@0: return ret ? kMediaConduitInvalidSendCodec : kMediaConduitNoError; michael@0: } michael@0: michael@0: MediaConduitErrorCode michael@0: WebrtcVideoConduit::SetExternalRecvCodec(int pltype, michael@0: VideoDecoder* decoder) { michael@0: int ret = mPtrExtCodec->RegisterExternalReceiveCodec(mChannel, michael@0: pltype, michael@0: static_cast(decoder)); michael@0: return ret ? kMediaConduitInvalidReceiveCodec : kMediaConduitNoError; michael@0: } michael@0: michael@0: MediaConduitErrorCode michael@0: WebrtcVideoConduit::SendVideoFrame(unsigned char* video_frame, michael@0: unsigned int video_frame_length, michael@0: unsigned short width, michael@0: unsigned short height, michael@0: VideoType video_type, michael@0: uint64_t capture_time) michael@0: { michael@0: CSFLogDebug(logTag, "%s ", __FUNCTION__); michael@0: michael@0: //check for the parameters sanity michael@0: if(!video_frame || video_frame_length == 0 || michael@0: width == 0 || height == 0) michael@0: { michael@0: CSFLogError(logTag, "%s Invalid Parameters ",__FUNCTION__); michael@0: MOZ_ASSERT(PR_FALSE); michael@0: return kMediaConduitMalformedArgument; michael@0: } michael@0: michael@0: webrtc::RawVideoType type; michael@0: switch (video_type) { michael@0: case kVideoI420: michael@0: type = webrtc::kVideoI420; michael@0: break; michael@0: case kVideoNV21: michael@0: type = webrtc::kVideoNV21; michael@0: break; michael@0: default: michael@0: CSFLogError(logTag, "%s VideoType Invalid. Only 1420 and NV21 Supported",__FUNCTION__); michael@0: MOZ_ASSERT(PR_FALSE); michael@0: return kMediaConduitMalformedArgument; michael@0: } michael@0: //Transmission should be enabled before we insert any frames. michael@0: if(!mEngineTransmitting) michael@0: { michael@0: CSFLogError(logTag, "%s Engine not transmitting ", __FUNCTION__); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: michael@0: // enforce even width/height (paranoia) michael@0: MOZ_ASSERT(!(width & 1)); michael@0: MOZ_ASSERT(!(height & 1)); michael@0: michael@0: if (!SelectSendResolution(width, height)) michael@0: { michael@0: return kMediaConduitCaptureError; michael@0: } michael@0: michael@0: //insert the frame to video engine in I420 format only michael@0: MOZ_ASSERT(mPtrExtCapture); michael@0: if(mPtrExtCapture->IncomingFrame(video_frame, michael@0: video_frame_length, michael@0: width, height, michael@0: type, michael@0: (unsigned long long)capture_time) == -1) michael@0: { michael@0: CSFLogError(logTag, "%s IncomingFrame Failed %d ", __FUNCTION__, michael@0: mPtrViEBase->LastError()); michael@0: return kMediaConduitCaptureError; michael@0: } michael@0: michael@0: CSFLogDebug(logTag, "%s Inserted a frame", __FUNCTION__); michael@0: return kMediaConduitNoError; michael@0: } michael@0: michael@0: // Transport Layer Callbacks michael@0: MediaConduitErrorCode michael@0: WebrtcVideoConduit::ReceivedRTPPacket(const void *data, int len) michael@0: { michael@0: CSFLogDebug(logTag, "%s: Channel %d, Len %d ", __FUNCTION__, mChannel, len); michael@0: michael@0: // Media Engine should be receiving already. michael@0: if(mEngineReceiving) michael@0: { michael@0: // let the engine know of a RTP packet to decode michael@0: if(mPtrViENetwork->ReceivedRTPPacket(mChannel,data,len) == -1) michael@0: { michael@0: int error = mPtrViEBase->LastError(); michael@0: CSFLogError(logTag, "%s RTP Processing Failed %d ", __FUNCTION__, error); michael@0: if(error >= kViERtpRtcpInvalidChannelId && error <= kViERtpRtcpRtcpDisabled) michael@0: { michael@0: return kMediaConduitRTPProcessingFailed; michael@0: } michael@0: return kMediaConduitRTPRTCPModuleError; michael@0: } michael@0: } else { michael@0: CSFLogError(logTag, "Error: %s when not receiving", __FUNCTION__); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: michael@0: return kMediaConduitNoError; michael@0: } michael@0: michael@0: MediaConduitErrorCode michael@0: WebrtcVideoConduit::ReceivedRTCPPacket(const void *data, int len) michael@0: { michael@0: CSFLogDebug(logTag, " %s Channel %d, Len %d ", __FUNCTION__, mChannel, len); michael@0: michael@0: //Media Engine should be receiving already michael@0: if(mEngineTransmitting) michael@0: { michael@0: if(mPtrViENetwork->ReceivedRTCPPacket(mChannel,data,len) == -1) michael@0: { michael@0: int error = mPtrViEBase->LastError(); michael@0: CSFLogError(logTag, "%s RTP Processing Failed %d", __FUNCTION__, error); michael@0: if(error >= kViERtpRtcpInvalidChannelId && error <= kViERtpRtcpRtcpDisabled) michael@0: { michael@0: return kMediaConduitRTPProcessingFailed; michael@0: } michael@0: return kMediaConduitRTPRTCPModuleError; michael@0: } michael@0: } else { michael@0: CSFLogError(logTag, "Error: %s when not receiving", __FUNCTION__); michael@0: return kMediaConduitSessionNotInited; michael@0: } michael@0: return kMediaConduitNoError; michael@0: } michael@0: michael@0: //WebRTC::RTP Callback Implementation michael@0: int WebrtcVideoConduit::SendPacket(int channel, const void* data, int len) michael@0: { michael@0: CSFLogDebug(logTag, "%s : channel %d len %d %s", __FUNCTION__, channel, len, michael@0: (mEngineReceiving && mOtherDirection) ? "(using mOtherDirection)" : ""); michael@0: michael@0: if (mEngineReceiving) michael@0: { michael@0: if (mOtherDirection) michael@0: { michael@0: return mOtherDirection->SendPacket(channel, data, len); michael@0: } michael@0: CSFLogDebug(logTag, "%s : Asked to send RTP without an RTP sender on channel %d", michael@0: __FUNCTION__, channel); michael@0: return -1; michael@0: } else { michael@0: if(mTransport && (mTransport->SendRtpPacket(data, len) == NS_OK)) michael@0: { michael@0: CSFLogDebug(logTag, "%s Sent RTP Packet ", __FUNCTION__); michael@0: return len; michael@0: } else { michael@0: CSFLogError(logTag, "%s RTP Packet Send Failed ", __FUNCTION__); michael@0: return -1; michael@0: } michael@0: } michael@0: } michael@0: michael@0: int WebrtcVideoConduit::SendRTCPPacket(int channel, const void* data, int len) michael@0: { michael@0: CSFLogDebug(logTag, "%s : channel %d , len %d ", __FUNCTION__, channel,len); michael@0: michael@0: if (mEngineTransmitting) michael@0: { michael@0: if (mOtherDirection) michael@0: { michael@0: return mOtherDirection->SendRTCPPacket(channel, data, len); michael@0: } michael@0: } michael@0: michael@0: // We come here if we have only one pipeline/conduit setup, michael@0: // such as for unidirectional streams. michael@0: // We also end up here if we are receiving michael@0: if(mTransport && mTransport->SendRtcpPacket(data, len) == NS_OK) michael@0: { michael@0: CSFLogDebug(logTag, "%s Sent RTCP Packet ", __FUNCTION__); michael@0: return len; michael@0: } else { michael@0: CSFLogError(logTag, "%s RTCP Packet Send Failed ", __FUNCTION__); michael@0: return -1; michael@0: } michael@0: } michael@0: michael@0: // WebRTC::ExternalMedia Implementation michael@0: int michael@0: WebrtcVideoConduit::FrameSizeChange(unsigned int width, michael@0: unsigned int height, michael@0: unsigned int numStreams) michael@0: { michael@0: CSFLogDebug(logTag, "%s ", __FUNCTION__); michael@0: michael@0: michael@0: mReceivingWidth = width; michael@0: mReceivingHeight = height; michael@0: michael@0: if(mRenderer) michael@0: { michael@0: mRenderer->FrameSizeChange(width, height, numStreams); michael@0: return 0; michael@0: } michael@0: michael@0: CSFLogError(logTag, "%s Renderer is NULL ", __FUNCTION__); michael@0: return -1; michael@0: } michael@0: michael@0: int michael@0: WebrtcVideoConduit::DeliverFrame(unsigned char* buffer, michael@0: int buffer_size, michael@0: uint32_t time_stamp, michael@0: int64_t render_time, michael@0: void *handle) michael@0: { michael@0: CSFLogDebug(logTag, "%s Buffer Size %d", __FUNCTION__, buffer_size); michael@0: michael@0: if(mRenderer) michael@0: { michael@0: layers::Image* img = nullptr; michael@0: // |handle| should be a webrtc::NativeHandle if available. michael@0: if (handle) { michael@0: webrtc::NativeHandle* native_h = static_cast(handle); michael@0: // In the handle, there should be a layers::Image. michael@0: img = static_cast(native_h->GetHandle()); michael@0: } michael@0: michael@0: if (mVideoLatencyTestEnable && mReceivingWidth && mReceivingHeight) { michael@0: uint64_t now = PR_Now(); michael@0: uint64_t timestamp = 0; michael@0: bool ok = YuvStamper::Decode(mReceivingWidth, mReceivingHeight, mReceivingWidth, michael@0: buffer, michael@0: reinterpret_cast(×tamp), michael@0: sizeof(timestamp), 0, 0); michael@0: if (ok) { michael@0: VideoLatencyUpdate(now - timestamp); michael@0: } michael@0: } michael@0: michael@0: const ImageHandle img_h(img); michael@0: mRenderer->RenderVideoFrame(buffer, buffer_size, time_stamp, render_time, michael@0: img_h); michael@0: return 0; michael@0: } michael@0: michael@0: CSFLogError(logTag, "%s Renderer is NULL ", __FUNCTION__); michael@0: return -1; michael@0: } michael@0: michael@0: /** michael@0: * Copy the codec passed into Conduit's database michael@0: */ michael@0: michael@0: void michael@0: WebrtcVideoConduit::CodecConfigToWebRTCCodec(const VideoCodecConfig* codecInfo, michael@0: webrtc::VideoCodec& cinst) michael@0: { michael@0: cinst.plType = codecInfo->mType; michael@0: // leave width/height alone; they'll be overridden on the first frame michael@0: if (codecInfo->mMaxFrameRate > 0) michael@0: { michael@0: cinst.maxFramerate = codecInfo->mMaxFrameRate; michael@0: } michael@0: cinst.minBitrate = 200; michael@0: cinst.startBitrate = 300; michael@0: cinst.maxBitrate = 2000; michael@0: } michael@0: michael@0: //Copy the codec passed into Conduit's database michael@0: bool michael@0: WebrtcVideoConduit::CopyCodecToDB(const VideoCodecConfig* codecInfo) michael@0: { michael@0: VideoCodecConfig* cdcConfig = new VideoCodecConfig(*codecInfo); michael@0: mRecvCodecList.push_back(cdcConfig); michael@0: return true; michael@0: } michael@0: michael@0: bool michael@0: WebrtcVideoConduit::CheckCodecsForMatch(const VideoCodecConfig* curCodecConfig, michael@0: const VideoCodecConfig* codecInfo) const michael@0: { michael@0: if(!curCodecConfig) michael@0: { michael@0: return false; michael@0: } michael@0: michael@0: if(curCodecConfig->mType == codecInfo->mType && michael@0: curCodecConfig->mName.compare(codecInfo->mName) == 0 && michael@0: curCodecConfig->mMaxFrameSize == codecInfo->mMaxFrameSize && michael@0: curCodecConfig->mMaxFrameRate == codecInfo->mMaxFrameRate) michael@0: { michael@0: return true; michael@0: } michael@0: michael@0: return false; michael@0: } michael@0: michael@0: /** michael@0: * Checks if the codec is already in Conduit's database michael@0: */ michael@0: bool michael@0: WebrtcVideoConduit::CheckCodecForMatch(const VideoCodecConfig* codecInfo) const michael@0: { michael@0: //the db should have atleast one codec michael@0: for(std::vector::size_type i=0;i < mRecvCodecList.size();i++) michael@0: { michael@0: if(CheckCodecsForMatch(mRecvCodecList[i],codecInfo)) michael@0: { michael@0: //match michael@0: return true; michael@0: } michael@0: } michael@0: //no match or empty local db michael@0: return false; michael@0: } michael@0: michael@0: /** michael@0: * Perform validation on the codecConfig to be applied michael@0: * Verifies if the codec is already applied. michael@0: */ michael@0: MediaConduitErrorCode michael@0: WebrtcVideoConduit::ValidateCodecConfig(const VideoCodecConfig* codecInfo, michael@0: bool send) const michael@0: { michael@0: bool codecAppliedAlready = false; michael@0: michael@0: if(!codecInfo) michael@0: { michael@0: CSFLogError(logTag, "%s Null CodecConfig ", __FUNCTION__); michael@0: return kMediaConduitMalformedArgument; michael@0: } michael@0: michael@0: if((codecInfo->mName.empty()) || michael@0: (codecInfo->mName.length() >= CODEC_PLNAME_SIZE)) michael@0: { michael@0: CSFLogError(logTag, "%s Invalid Payload Name Length ", __FUNCTION__); michael@0: return kMediaConduitMalformedArgument; michael@0: } michael@0: michael@0: //check if we have the same codec already applied michael@0: if(send) michael@0: { michael@0: codecAppliedAlready = CheckCodecsForMatch(mCurSendCodecConfig,codecInfo); michael@0: } else { michael@0: codecAppliedAlready = CheckCodecForMatch(codecInfo); michael@0: } michael@0: michael@0: if(codecAppliedAlready) michael@0: { michael@0: CSFLogDebug(logTag, "%s Codec %s Already Applied ", __FUNCTION__, codecInfo->mName.c_str()); michael@0: return kMediaConduitCodecInUse; michael@0: } michael@0: return kMediaConduitNoError; michael@0: } michael@0: michael@0: void michael@0: WebrtcVideoConduit::DumpCodecDB() const michael@0: { michael@0: for(std::vector::size_type i=0;imName.c_str()); michael@0: CSFLogDebug(logTag,"Payload Type: %d", mRecvCodecList[i]->mType); michael@0: CSFLogDebug(logTag,"Payload Max Frame Size: %d", mRecvCodecList[i]->mMaxFrameSize); michael@0: CSFLogDebug(logTag,"Payload Max Frame Rate: %d", mRecvCodecList[i]->mMaxFrameRate); michael@0: } michael@0: } michael@0: michael@0: void michael@0: WebrtcVideoConduit::VideoLatencyUpdate(uint64_t newSample) michael@0: { michael@0: mVideoLatencyAvg = (sRoundingPadding * newSample + sAlphaNum * mVideoLatencyAvg) / sAlphaDen; michael@0: } michael@0: michael@0: uint64_t michael@0: WebrtcVideoConduit::MozVideoLatencyAvg() michael@0: { michael@0: return mVideoLatencyAvg / sRoundingPadding; michael@0: } michael@0: michael@0: }// end namespace