From dc913a843a824e1d9e7edc0c83ba252b25e28325 Mon Sep 17 00:00:00 2001 From: Grishka Date: Mon, 21 Aug 2017 19:02:37 +0300 Subject: [PATCH] Added signal bars & fixed OSX project --- VoIPController.cpp | 48 ++++++++++++++++++++++++- VoIPController.h | 13 +++++++ client/android/tg_voip_jni.cpp | 23 ++++++++++++ libtgvoip_osx.xcodeproj/project.pbxproj | 28 +++++++++++++-- os/darwin/TGLogWrapper.m | 2 +- 5 files changed, 110 insertions(+), 4 deletions(-) diff --git a/VoIPController.cpp b/VoIPController.cpp index 78e9e4b..fd1a16d 100644 --- a/VoIPController.cpp +++ b/VoIPController.cpp @@ -74,6 +74,9 @@ #define TLID_UDP_REFLECTOR_SELF_INFO 0xc01572c7 #define PAD4(x) (4-(x+(x<=253 ? 1 : 0))%4) +#define MAX(a,b) (a>b ? a : b) +#define MIN(a,b) (a10.0 && rttHistory[8]>10.0 && (networkType==NET_TYPE_EDGE || networkType==NET_TYPE_GPRS)){ waitingForAcks=true; + signalBarCount=1; }else{ waitingForAcks=false; } @@ -1453,6 +1464,11 @@ void VoIPController::RunTickThread(){ }else{ encoder->SetPacketLoss(15); } + + if(encoder->GetPacketLoss()>30) + signalBarCount=MIN(signalBarCount, 2); + else if(encoder->GetPacketLoss()>20) + signalBarCount=MIN(signalBarCount, 3); } } @@ -1521,8 +1537,24 @@ void VoIPController::RunTickThread(){ } unlock_mutex(queuedPacketsMutex); - if(jitterBuffer) + if(jitterBuffer){ jitterBuffer->Tick(); + double avgDelay=jitterBuffer->GetAverageDelay(); + double avgLateCount[3]; + jitterBuffer->GetAverageLateCount(avgLateCount); + if(avgDelay>=5) + signalBarCount=1; + else if(avgDelay>=4) + signalBarCount=MIN(signalBarCount, 2); + else if(avgDelay>=3) + signalBarCount=MIN(signalBarCount, 3); + + if(avgLateCount[2]>=0.2) + signalBarCount=1; + else if(avgLateCount[2]>=0.1) + signalBarCount=MIN(signalBarCount, 2); + + } lock_mutex(endpointsMutex); if(state==STATE_ESTABLISHED || state==STATE_RECONNECTING){ @@ -1662,6 +1694,12 @@ void VoIPController::RunTickThread(){ setEstablishedAt=0; } + if(signalBarCount!=prevSignalBarCount){ + LOGD("SIGNAL BAR COUNT CHANGED: %d", signalBarCount); + if(signalBarCountCallback) + signalBarCountCallback(this, signalBarCount); + } + if(statsDump){ //fprintf(statsDump, "Time\tRTT\tLISeq\tLASeq\tCWnd\tBitrate\tJitter\tJDelay\tAJDelay\n"); @@ -2404,6 +2442,14 @@ void VoIPController::SendUdpPing(Endpoint *endpoint){ udpSocket->Send(&pkt); } +int VoIPController::GetSignalBarsCount(){ + return signalBarCount; +} + +void VoIPController::SetSignalBarsCountCallback(void (*f)(VoIPController *, int)){ + signalBarCountCallback=f; +} + Endpoint::Endpoint(int64_t id, uint16_t port, IPv4Address& _address, IPv6Address& _v6address, char type, unsigned char peerTag[16]) : address(_address), v6address(_v6address){ this->id=id; this->port=port; diff --git a/VoIPController.h b/VoIPController.h index 6237814..40feb58 100644 --- a/VoIPController.h +++ b/VoIPController.h @@ -324,6 +324,16 @@ public: * @param password Password; empty string if none */ void SetProxy(int protocol, std::string address, uint16_t port, std::string username, std::string password); + /** + * Get the number of signal bars to display in the client UI. + * @return the number of signal bars, from 1 to 4 + */ + int GetSignalBarsCount(); + /** + * Set the callback to be called when the signal bar count changes. + * @param f + */ + void SetSignalBarsCountCallback(void (*f)(VoIPController*, int)); private: struct PendingOutgoingPacket{ @@ -453,6 +463,9 @@ private: std::string proxyUsername; std::string proxyPassword; IPv4Address* resolvedProxyAddress; + + int signalBarCount; + void (*signalBarCountCallback)(VoIPController*, int); /*** server config values ***/ uint32_t maxAudioBitrate; diff --git a/client/android/tg_voip_jni.cpp b/client/android/tg_voip_jni.cpp index f97f3d4..dcb18df 100644 --- a/client/android/tg_voip_jni.cpp +++ b/client/android/tg_voip_jni.cpp @@ -21,6 +21,7 @@ JavaVM* sharedJVM; jfieldID audioRecordInstanceFld=NULL; jfieldID audioTrackInstanceFld=NULL; jmethodID setStateMethod=NULL; +jmethodID setSignalBarsMethod=NULL; struct impl_data_android_t{ jobject javaObject; @@ -49,6 +50,26 @@ void updateConnectionState(VoIPController* cntrlr, int state){ } } +void updateSignalBarCount(VoIPController* cntrlr, int count){ + impl_data_android_t* impl=(impl_data_android_t*) cntrlr->implData; + if(!impl->javaObject) + return; + JNIEnv* env=NULL; + bool didAttach=false; + sharedJVM->GetEnv((void**) &env, JNI_VERSION_1_6); + if(!env){ + sharedJVM->AttachCurrentThread(&env, NULL); + didAttach=true; + } + + if(setSignalBarsMethod) + env->CallVoidMethod(impl->javaObject, setSignalBarsMethod, count); + + if(didAttach){ + sharedJVM->DetachCurrentThread(); + } +} + extern "C" JNIEXPORT jlong Java_org_telegram_messenger_voip_VoIPController_nativeInit(JNIEnv* env, jobject thiz, jint systemVersion){ AudioOutputAndroid::systemVersion=systemVersion; @@ -70,12 +91,14 @@ extern "C" JNIEXPORT jlong Java_org_telegram_messenger_voip_VoIPController_nativ } setStateMethod=env->GetMethodID(env->GetObjectClass(thiz), "handleStateChange", "(I)V"); + setSignalBarsMethod=env->GetMethodID(env->GetObjectClass(thiz), "handleSignalBarsChange", "(I)V"); impl_data_android_t* impl=(impl_data_android_t*) malloc(sizeof(impl_data_android_t)); impl->javaObject=env->NewGlobalRef(thiz); VoIPController* cntrlr=new VoIPController(); cntrlr->implData=impl; cntrlr->SetStateCallback(updateConnectionState); + cntrlr->SetSignalBarsCountCallback(updateSignalBarCount); return (jlong)(intptr_t)cntrlr; } diff --git a/libtgvoip_osx.xcodeproj/project.pbxproj b/libtgvoip_osx.xcodeproj/project.pbxproj index d1a7764..1280d5d 100644 --- a/libtgvoip_osx.xcodeproj/project.pbxproj +++ b/libtgvoip_osx.xcodeproj/project.pbxproj @@ -48,6 +48,12 @@ 692AB9211E675F7000706ACC /* CoreAudio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 692AB91E1E675F7000706ACC /* CoreAudio.framework */; }; 695B20621EBD39FF00E31757 /* DarwinSpecific.h in Headers */ = {isa = PBXBuildFile; fileRef = 695B20601EBD39FF00E31757 /* DarwinSpecific.h */; }; 695B20631EBD39FF00E31757 /* DarwinSpecific.mm in Sources */ = {isa = PBXBuildFile; fileRef = 695B20611EBD39FF00E31757 /* DarwinSpecific.mm */; }; + 698848411F4B39F700076DF0 /* AudioInputAudioUnit.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6988483B1F4B39F700076DF0 /* AudioInputAudioUnit.cpp */; }; + 698848421F4B39F700076DF0 /* AudioInputAudioUnit.h in Headers */ = {isa = PBXBuildFile; fileRef = 6988483C1F4B39F700076DF0 /* AudioInputAudioUnit.h */; }; + 698848431F4B39F700076DF0 /* AudioOutputAudioUnit.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6988483D1F4B39F700076DF0 /* AudioOutputAudioUnit.cpp */; }; + 698848441F4B39F700076DF0 /* AudioOutputAudioUnit.h in Headers */ = {isa = PBXBuildFile; fileRef = 6988483E1F4B39F700076DF0 /* AudioOutputAudioUnit.h */; }; + 698848451F4B39F700076DF0 /* AudioUnitIO.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6988483F1F4B39F700076DF0 /* AudioUnitIO.cpp */; }; + 698848461F4B39F700076DF0 /* AudioUnitIO.h in Headers */ = {isa = PBXBuildFile; fileRef = 698848401F4B39F700076DF0 /* AudioUnitIO.h */; }; 69A6DEB91E96149300000E69 /* array_view.h in Headers */ = {isa = PBXBuildFile; fileRef = 69A6DE231E96149300000E69 /* array_view.h */; }; 69A6DEBA1E96149300000E69 /* atomicops.h in Headers */ = {isa = PBXBuildFile; fileRef = 69A6DE241E96149300000E69 /* atomicops.h */; }; 69A6DEBB1E96149300000E69 /* basictypes.h in Headers */ = {isa = PBXBuildFile; fileRef = 69A6DE251E96149300000E69 /* basictypes.h */; }; @@ -271,8 +277,14 @@ 692AB91C1E675F7000706ACC /* AudioToolbox.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioToolbox.framework; path = System/Library/Frameworks/AudioToolbox.framework; sourceTree = SDKROOT; }; 692AB91D1E675F7000706ACC /* AudioUnit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = AudioUnit.framework; path = System/Library/Frameworks/AudioUnit.framework; sourceTree = SDKROOT; }; 692AB91E1E675F7000706ACC /* CoreAudio.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreAudio.framework; path = System/Library/Frameworks/CoreAudio.framework; sourceTree = SDKROOT; }; - 695B20601EBD39FF00E31757 /* DarwinSpecific.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = DarwinSpecific.h; path = ../../../../../TDesktop/TBuild/tdesktop/third_party/libtgvoip/os/darwin/DarwinSpecific.h; sourceTree = ""; }; - 695B20611EBD39FF00E31757 /* DarwinSpecific.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = DarwinSpecific.mm; path = ../../../../../TDesktop/TBuild/tdesktop/third_party/libtgvoip/os/darwin/DarwinSpecific.mm; sourceTree = ""; }; + 695B20601EBD39FF00E31757 /* DarwinSpecific.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DarwinSpecific.h; sourceTree = ""; }; + 695B20611EBD39FF00E31757 /* DarwinSpecific.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; name = DarwinSpecific.mm; path = ../../../../../libtgvoip/os/darwin/DarwinSpecific.mm; sourceTree = ""; }; + 6988483B1F4B39F700076DF0 /* AudioInputAudioUnit.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = AudioInputAudioUnit.cpp; path = ../../../../../libtgvoip/os/darwin/AudioInputAudioUnit.cpp; sourceTree = ""; }; + 6988483C1F4B39F700076DF0 /* AudioInputAudioUnit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AudioInputAudioUnit.h; path = ../../../../../libtgvoip/os/darwin/AudioInputAudioUnit.h; sourceTree = ""; }; + 6988483D1F4B39F700076DF0 /* AudioOutputAudioUnit.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = AudioOutputAudioUnit.cpp; path = ../../../../../libtgvoip/os/darwin/AudioOutputAudioUnit.cpp; sourceTree = ""; }; + 6988483E1F4B39F700076DF0 /* AudioOutputAudioUnit.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AudioOutputAudioUnit.h; path = ../../../../../libtgvoip/os/darwin/AudioOutputAudioUnit.h; sourceTree = ""; }; + 6988483F1F4B39F700076DF0 /* AudioUnitIO.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = AudioUnitIO.cpp; path = ../../../../../libtgvoip/os/darwin/AudioUnitIO.cpp; sourceTree = ""; }; + 698848401F4B39F700076DF0 /* AudioUnitIO.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = AudioUnitIO.h; path = ../../../../../libtgvoip/os/darwin/AudioUnitIO.h; sourceTree = ""; }; 69A6DE231E96149300000E69 /* array_view.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = array_view.h; sourceTree = ""; }; 69A6DE241E96149300000E69 /* atomicops.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = atomicops.h; sourceTree = ""; }; 69A6DE251E96149300000E69 /* basictypes.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = basictypes.h; sourceTree = ""; }; @@ -497,6 +509,12 @@ 692AB8BD1E6759DD00706ACC /* darwin */ = { isa = PBXGroup; children = ( + 6988483B1F4B39F700076DF0 /* AudioInputAudioUnit.cpp */, + 6988483C1F4B39F700076DF0 /* AudioInputAudioUnit.h */, + 6988483D1F4B39F700076DF0 /* AudioOutputAudioUnit.cpp */, + 6988483E1F4B39F700076DF0 /* AudioOutputAudioUnit.h */, + 6988483F1F4B39F700076DF0 /* AudioUnitIO.cpp */, + 698848401F4B39F700076DF0 /* AudioUnitIO.h */, 69A6DF3F1E9614B700000E69 /* AudioInputAudioUnitOSX.cpp */, 69A6DF401E9614B700000E69 /* AudioInputAudioUnitOSX.h */, 69A6DF411E9614B700000E69 /* AudioOutputAudioUnitOSX.cpp */, @@ -840,6 +858,7 @@ 69A6DF181E96149300000E69 /* gain_control.h in Headers */, 69A6DF231E96149300000E69 /* nsx_core.h in Headers */, 692AB9011E6759DD00706ACC /* threading.h in Headers */, + 698848461F4B39F700076DF0 /* AudioUnitIO.h in Headers */, 69A6DF2B1E96149300000E69 /* three_band_filter_bank.h in Headers */, 692AB8EA1E6759DD00706ACC /* MediaStreamItf.h in Headers */, 69A6DF3E1E96149300000E69 /* typedefs.h in Headers */, @@ -900,6 +919,7 @@ 69A6DF3C1E96149300000E69 /* metrics.h in Headers */, 692AB8D61E6759DD00706ACC /* BufferPool.h in Headers */, 69A6DF0E1E96149300000E69 /* aecm_core.h in Headers */, + 698848421F4B39F700076DF0 /* AudioInputAudioUnit.h in Headers */, 69A6DF2F1E96149300000E69 /* delay_estimator.h in Headers */, 69A6DEC41E96149300000E69 /* stringutils.h in Headers */, 69A6DF1F1E96149300000E69 /* noise_suppression_x.h in Headers */, @@ -914,6 +934,7 @@ 69A6DEBB1E96149300000E69 /* basictypes.h in Headers */, 69A6DEE71E96149300000E69 /* spl_inl_mips.h in Headers */, 69A6DF261E96149300000E69 /* nsx_defines.h in Headers */, + 698848441F4B39F700076DF0 /* AudioOutputAudioUnit.h in Headers */, 69A6DEC11E96149300000E69 /* safe_conversions_impl.h in Headers */, 69A6DEC01E96149300000E69 /* safe_conversions.h in Headers */, ); @@ -1081,6 +1102,7 @@ 692AB8EB1E6759DD00706ACC /* OpusDecoder.cpp in Sources */, 69A6DED81E96149300000E69 /* dot_product_with_scale.c in Sources */, 69A6DF331E96149300000E69 /* ooura_fft.cc in Sources */, + 698848451F4B39F700076DF0 /* AudioUnitIO.cpp in Sources */, 69A6DEF11E96149300000E69 /* resample_by_2.c in Sources */, 69A6DEEC1E96149300000E69 /* randomization_functions.c in Sources */, 69A6DEEE1E96149300000E69 /* refl_coef_to_lpc.c in Sources */, @@ -1091,6 +1113,7 @@ 69A6DF161E96149300000E69 /* digital_agc.c in Sources */, 69A6DF061E96149300000E69 /* aec_core_neon.cc in Sources */, 69A6DF201E96149300000E69 /* ns_core.c in Sources */, + 698848431F4B39F700076DF0 /* AudioOutputAudioUnit.cpp in Sources */, 69A6DF091E96149300000E69 /* aec_resampler.cc in Sources */, 692AB8D11E6759DD00706ACC /* BufferInputStream.cpp in Sources */, 692AB8E91E6759DD00706ACC /* MediaStreamItf.cpp in Sources */, @@ -1123,6 +1146,7 @@ 69A6DF251E96149300000E69 /* nsx_core_neon.c in Sources */, 69A6DF081E96149300000E69 /* aec_core_sse2.cc in Sources */, 69A6DEEA1E96149300000E69 /* min_max_operations.c in Sources */, + 698848411F4B39F700076DF0 /* AudioInputAudioUnit.cpp in Sources */, 69A6DF361E96149300000E69 /* ooura_fft_sse2.cc in Sources */, 69A6DED51E96149300000E69 /* cross_correlation.c in Sources */, 69A6DF3D1E96149300000E69 /* cpu_features.cc in Sources */, diff --git a/os/darwin/TGLogWrapper.m b/os/darwin/TGLogWrapper.m index 6affe3d..be239b5 100644 --- a/os/darwin/TGLogWrapper.m +++ b/os/darwin/TGLogWrapper.m @@ -1,6 +1,6 @@ #import -#import +extern void TGLogv(NSString *format, va_list args); void __tgvoip_call_tglog(const char* format, ...){ va_list args;