Skip to content

Commit

Permalink
Update to 7.4.0 (2221)
Browse files Browse the repository at this point in the history
  • Loading branch information
DrKLO committed Jan 28, 2021
1 parent d52de1a commit 77bbe5b
Show file tree
Hide file tree
Showing 232 changed files with 16,265 additions and 2,677 deletions.
6 changes: 3 additions & 3 deletions TMessagesProj/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ dependencies {
implementation 'androidx.exifinterface:exifinterface:1.3.2'
implementation 'androidx.dynamicanimation:dynamicanimation:1.0.0'
implementation 'androidx.multidex:multidex:2.0.1'
implementation "androidx.sharetarget:sharetarget:1.0.0"
implementation "androidx.sharetarget:sharetarget:1.1.0"

compileOnly 'org.checkerframework:checker-qual:2.5.2'
compileOnly 'org.checkerframework:checker-compat-qual:2.5.0'
Expand Down Expand Up @@ -290,7 +290,7 @@ android {
}
}

defaultConfig.versionCode = 2206
defaultConfig.versionCode = 2221

applicationVariants.all { variant ->
variant.outputs.all { output ->
Expand All @@ -309,7 +309,7 @@ android {
defaultConfig {
minSdkVersion 16
targetSdkVersion 29
versionName "7.3.1"
versionName "7.4.0"

vectorDrawables.generatedDensities = ['mdpi', 'hdpi', 'xhdpi', 'xxhdpi']

Expand Down
22 changes: 22 additions & 0 deletions TMessagesProj/jni/voip/org_telegram_messenger_voip_Instance.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,21 @@ JNIEXPORT jlong JNICALL Java_org_telegram_messenger_voip_NativeInstance_makeNati
env->CallVoidMethod(globalRef, env->GetMethodID(NativeInstanceClass, "onSignalBarsUpdated", "(I)V"), count);
});
},
.audioLevelUpdated = [platformContext](float level) {
tgvoip::jni::DoWithJNI([platformContext, level](JNIEnv *env) {
jintArray intArray = nullptr;
jfloatArray floatArray = env->NewFloatArray(1);
jbooleanArray boolArray = nullptr;

jfloat floatFill[1];
floatFill[0] = level;
env->SetFloatArrayRegion(floatArray, 0, 1, floatFill);

jobject globalRef = ((AndroidContext *) platformContext.get())->getJavaInstance();
env->CallVoidMethod(globalRef, env->GetMethodID(NativeInstanceClass, "onAudioLevelsUpdated", "([I[F[Z)V"), intArray, floatArray, boolArray);
env->DeleteLocalRef(floatArray);
});
},
.remoteMediaStateUpdated = [platformContext](AudioState audioState, VideoState videoState) {
jobject globalRef = ((AndroidContext *) platformContext.get())->getJavaInstance();
tgvoip::jni::DoWithJNI([globalRef, audioState, videoState](JNIEnv *env) {
Expand Down Expand Up @@ -521,6 +536,13 @@ JNIEXPORT void JNICALL Java_org_telegram_messenger_voip_NativeInstance_setMuteMi
}
}

JNIEXPORT void JNICALL Java_org_telegram_messenger_voip_NativeInstance_setVolume(JNIEnv *env, jobject obj, jint ssrc, jdouble volume) {
InstanceHolder *instance = getInstanceHolder(env, obj);
if (instance->groupNativeInstance != nullptr) {
instance->groupNativeInstance->setVolume(ssrc, volume);
}
}

JNIEXPORT void JNICALL Java_org_telegram_messenger_voip_NativeInstance_setAudioOutputGainControlEnabled(JNIEnv *env, jobject obj, jboolean enabled) {
InstanceHolder *instance = getInstanceHolder(env, obj);
if (instance->nativeInstance == nullptr) {
Expand Down
99 changes: 83 additions & 16 deletions TMessagesProj/jni/voip/tgcalls/MediaManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "modules/rtp_rtcp/source/rtp_utility.h"
#include "api/call/audio_sink.h"
#include "modules/audio_processing/audio_buffer.h"
#include "modules/audio_device/include/audio_device_factory.h"

namespace tgcalls {
namespace {
Expand All @@ -45,6 +46,31 @@ VideoCaptureInterfaceObject *GetVideoCaptureAssumingSameThread(VideoCaptureInter
: nullptr;
}

class AudioCaptureAnalyzer : public webrtc::CustomAudioAnalyzer {
private:
void Initialize(int sample_rate_hz, int num_channels) override {

}
// Analyzes the given capture or render signal.
void Analyze(const webrtc::AudioBuffer* audio) override {
_analyze(audio);
}
// Returns a string representation of the module state.
std::string ToString() const override {
return "analyzing";
}

std::function<void(const webrtc::AudioBuffer*)> _analyze;

public:
AudioCaptureAnalyzer(std::function<void(const webrtc::AudioBuffer*)> analyze) :
_analyze(analyze) {
}

virtual ~AudioCaptureAnalyzer() = default;
};


} // namespace

class VideoSinkInterfaceProxyImpl : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
Expand Down Expand Up @@ -87,7 +113,7 @@ class VideoSinkInterfaceProxyImpl : public rtc::VideoSinkInterface<webrtc::Video
class AudioTrackSinkInterfaceImpl: public webrtc::AudioSinkInterface {
private:
std::function<void(float)> _update;

int _peakCount = 0;
uint16_t _peak = 0;

Expand All @@ -103,7 +129,7 @@ class AudioTrackSinkInterfaceImpl: public webrtc::AudioSinkInterface {
if (audio.channels == 1) {
int16_t *samples = (int16_t *)audio.data;
int numberOfSamplesInFrame = (int)audio.samples_per_channel;

for (int i = 0; i < numberOfSamplesInFrame; i++) {
int16_t sample = samples[i];
if (sample < 0) {
Expand All @@ -114,7 +140,7 @@ class AudioTrackSinkInterfaceImpl: public webrtc::AudioSinkInterface {
}
_peakCount += 1;
}

if (_peakCount >= 1200) {
float level = ((float)(_peak)) / 4000.0f;
_peak = 0;
Expand Down Expand Up @@ -206,7 +232,49 @@ _platformContext(platformContext) {
preferredCodecs,
_platformContext);

mediaDeps.audio_processing = webrtc::AudioProcessingBuilder().Create();
// [this] should outlive the analyzer
auto analyzer = new AudioCaptureAnalyzer([this](const webrtc::AudioBuffer* buffer) {
if (!buffer) {
return;
}
if (buffer->num_channels() != 1) {
return;
}

float peak = 0;
int peakCount = 0;
const float *samples = buffer->channels_const()[0];
for (int i = 0; i < buffer->num_frames(); i++) {
float sample = samples[i];
if (sample < 0) {
sample = -sample;
}
if (peak < sample) {
peak = sample;
}
peakCount += 1;
}

this->_thread->PostTask(RTC_FROM_HERE, [this, peak, peakCount](){
auto strong = this;

strong->_myAudioLevelPeakCount += peakCount;
if (strong->_myAudioLevelPeak < peak) {
strong->_myAudioLevelPeak = peak;
}
if (strong->_myAudioLevelPeakCount >= 1200) {
float level = strong->_myAudioLevelPeak / 4000.0f;
strong->_myAudioLevelPeak = 0;
strong->_myAudioLevelPeakCount = 0;
strong->_currentMyAudioLevel = level;
}
});
});

webrtc::AudioProcessingBuilder builder;
builder.SetCaptureAnalyzer(std::unique_ptr<AudioCaptureAnalyzer>(analyzer));

mediaDeps.audio_processing = builder.Create();

/*_audioDeviceModule = createAudioDeviceModule();
if (!_audioDeviceModule) {
Expand Down Expand Up @@ -298,6 +366,13 @@ rtc::scoped_refptr<webrtc::AudioDeviceModule> MediaManager::createAudioDeviceMod
_taskQueueFactory.get());
return (result && (result->Init() == 0)) ? result : nullptr;
};
#ifdef WEBRTC_WIN
if (auto result = webrtc::CreateWindowsCoreAudioAudioDeviceModule(_taskQueueFactory.get())) {
if (result->Init() == 0) {
return result;
}
}
#endif // WEBRTC_WIN
if (auto result = check(webrtc::AudioDeviceModule::kPlatformDefaultAudio)) {
return result;
#ifdef WEBRTC_LINUX
Expand All @@ -310,7 +385,7 @@ rtc::scoped_refptr<webrtc::AudioDeviceModule> MediaManager::createAudioDeviceMod

void MediaManager::start() {
const auto weak = std::weak_ptr<MediaManager>(shared_from_this());

// Here we hope that thread outlives the sink
rtc::Thread *thread = _thread;
std::unique_ptr<AudioTrackSinkInterfaceImpl> incomingSink(new AudioTrackSinkInterfaceImpl([weak, thread](float level) {
Expand All @@ -320,24 +395,16 @@ void MediaManager::start() {
}
});
}));
std::unique_ptr<AudioTrackSinkInterfaceImpl> outgoingSink(new AudioTrackSinkInterfaceImpl([weak, thread](float level) {
thread->PostTask(RTC_FROM_HERE, [weak, level] {
if (const auto strong = weak.lock()) {
strong->_currentMyAudioLevel = level;
}
});
}));
_audioChannel->SetRawAudioSink(_ssrcAudio.incoming, std::move(incomingSink));
_audioChannel->SetRawAudioSink(_ssrcAudio.outgoing, std::move(outgoingSink));

_sendSignalingMessage({ _myVideoFormats });

_sendSignalingMessage({ _myVideoFormats });

if (_videoCapture != nullptr) {
setSendVideo(_videoCapture);
}

beginStatsTimer(3000);
if (_audioLevelUpdated != nullptr) {
if (_audioLevelUpdated != nullptr) {
beginLevelsTimer(50);
}
}
Expand Down
2 changes: 2 additions & 0 deletions TMessagesProj/jni/voip/tgcalls/MediaManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,8 @@ class MediaManager : public sigslot::has_slots<>, public std::enable_shared_from

float _currentAudioLevel = 0.0f;
float _currentMyAudioLevel = 0.0f;
int _myAudioLevelPeakCount = 0;
int _myAudioLevelPeak = 0;

std::unique_ptr<MediaManager::NetworkInterfaceImpl> _audioNetworkInterface;
std::unique_ptr<MediaManager::NetworkInterfaceImpl> _videoNetworkInterface;
Expand Down
52 changes: 49 additions & 3 deletions TMessagesProj/jni/voip/tgcalls/group/GroupInstanceImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "system_wrappers/include/field_trial.h"
#include "api/stats/rtcstats_objects.h"
#include "modules/audio_processing/audio_buffer.h"
#include "modules/audio_device/include/audio_device_factory.h"
#include "common_audio/include/audio_util.h"
#include "common_audio/vad/include/webrtc_vad.h"
#include "modules/audio_processing/agc2/vad_with_level.h"
Expand Down Expand Up @@ -1091,6 +1092,14 @@ class GroupInstanceManager : public std::enable_shared_from_this<GroupInstanceMa
dependencies.task_queue_factory.get());
return (result && (result->Init() == 0)) ? result : nullptr;
};
#ifdef WEBRTC_WIN
if (auto result = webrtc::CreateWindowsCoreAudioAudioDeviceModule(dependencies.task_queue_factory.get())) {
if (result->Init() == 0) {
_adm_use_withAudioDeviceModule = new rtc::RefCountedObject<WrappedAudioDeviceModule>(result);
return;
}
}
#endif // WEBRTC_WIN
if (auto result = check(webrtc::AudioDeviceModule::kPlatformDefaultAudio)) {
_adm_use_withAudioDeviceModule = new rtc::RefCountedObject<WrappedAudioDeviceModule>(result);
#ifdef WEBRTC_LINUX
Expand Down Expand Up @@ -1326,7 +1335,7 @@ class GroupInstanceManager : public std::enable_shared_from_this<GroupInstanceMa
adm->EnableBuiltInAEC(false);
#endif // WEBRTC_WIN

if (adm->InitPlayout()) {
if (adm->InitPlayout() == 0) {
adm->StartPlayout();
} else {
getMediaThread()->PostDelayedTask(RTC_FROM_HERE, [weak](){
Expand All @@ -1335,7 +1344,7 @@ class GroupInstanceManager : public std::enable_shared_from_this<GroupInstanceMa
return;
}
strong->withAudioDeviceModule([](webrtc::AudioDeviceModule *adm) {
if (adm->InitPlayout()) {
if (adm->InitPlayout() == 0) {
adm->StartPlayout();
}
});
Expand Down Expand Up @@ -1443,6 +1452,27 @@ class GroupInstanceManager : public std::enable_shared_from_this<GroupInstanceMa
});
#endif
}

void setVolume(uint32_t ssrc, double volume) {
auto current = _audioTrackVolumes.find(ssrc);
bool updated = false;
if (current != _audioTrackVolumes.end()) {
if (abs(current->second - volume) > 0.001) {
updated = true;
}
} else {
if (volume < 1.0 - 0.001) {
updated = true;
}
}
if (updated) {
_audioTrackVolumes[ssrc] = volume;
auto track = _audioTracks.find(ssrc);
if (track != _audioTracks.end()) {
track->second->GetSource()->SetVolume(volume);
}
}
}

void updateIsConnected(bool isConnected) {
_isConnected = isConnected;
Expand Down Expand Up @@ -1792,7 +1822,14 @@ class GroupInstanceManager : public std::enable_shared_from_this<GroupInstanceMa
uint32_t ssrc = 0;
iss >> ssrc;

auto remoteAudioTrack = static_cast<webrtc::AudioTrackInterface *>(transceiver->receiver()->track().get());
rtc::scoped_refptr<webrtc::AudioTrackInterface> remoteAudioTrack(static_cast<webrtc::AudioTrackInterface *>(transceiver->receiver()->track().get()));
if (_audioTracks.find(ssrc) == _audioTracks.end()) {
_audioTracks.insert(std::make_pair(ssrc, remoteAudioTrack));
}
auto currentVolume = _audioTrackVolumes.find(ssrc);
if (currentVolume != _audioTrackVolumes.end()) {
remoteAudioTrack->GetSource()->SetVolume(currentVolume->second);
}
if (_audioTrackSinks.find(ssrc) == _audioTrackSinks.end()) {
const auto weak = std::weak_ptr<GroupInstanceManager>(shared_from_this());
std::shared_ptr<AudioTrackSinkInterfaceImpl> sink(new AudioTrackSinkInterfaceImpl([weak, ssrc](float level, bool hasSpeech) {
Expand Down Expand Up @@ -1821,6 +1858,7 @@ class GroupInstanceManager : public std::enable_shared_from_this<GroupInstanceMa
}));
_audioTrackSinks[ssrc] = sink;
remoteAudioTrack->AddSink(sink.get());
//remoteAudioTrack->GetSource()->SetVolume(0.01);
}
}
}
Expand Down Expand Up @@ -2048,8 +2086,10 @@ class GroupInstanceManager : public std::enable_shared_from_this<GroupInstanceMa
rtc::Thread *_adm_thread = nullptr;
rtc::scoped_refptr<webrtc::AudioDeviceModule> _adm_use_withAudioDeviceModule;

std::map<uint32_t, rtc::scoped_refptr<webrtc::AudioTrackInterface>> _audioTracks;
std::map<uint32_t, std::shared_ptr<AudioTrackSinkInterfaceImpl>> _audioTrackSinks;
std::map<uint32_t, GroupLevelValue> _audioLevels;
std::map<uint32_t, double> _audioTrackVolumes;

std::shared_ptr<PlatformContext> _platformContext;
};
Expand Down Expand Up @@ -2122,4 +2162,10 @@ void GroupInstanceImpl::setAudioOutputDevice(std::string id) {
});
}

void GroupInstanceImpl::setVolume(uint32_t ssrc, double volume) {
_manager->perform(RTC_FROM_HERE, [ssrc, volume](GroupInstanceManager *manager) {
manager->setVolume(ssrc, volume);
});
}

} // namespace tgcalls
2 changes: 2 additions & 0 deletions TMessagesProj/jni/voip/tgcalls/group/GroupInstanceImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ class GroupInstanceImpl final {
void setIsMuted(bool isMuted);
void setAudioOutputDevice(std::string id);
void setAudioInputDevice(std::string id);

void setVolume(uint32_t ssrc, double volume);

struct AudioDevice {
enum class Type {Input, Output};
Expand Down
4 changes: 2 additions & 2 deletions TMessagesProj/jni/voip/tgcalls/legacy/InstanceImplLegacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ void InstanceImplLegacy::setOutputVolume(float level) {
}

void InstanceImplLegacy::setAudioOutputDuckingEnabled(bool enabled) {
#if defined(__APPLE__) && defined(TARGET_OS_OSX)
#if defined(__APPLE__) && TARGET_OS_OSX
controller_->SetAudioOutputDuckingEnabled(enabled);
#endif
#endif // TARGET_OS_OSX
}

void InstanceImplLegacy::setIsLowBatteryLevel(bool isLowBatteryLevel) {
Expand Down
Loading

0 comments on commit 77bbe5b

Please sign in to comment.