Skip to content

Commit

Permalink
upstream: bump version to v10.8.1
Browse files Browse the repository at this point in the history
Signed-off-by: qwq233 <qwq233@qwq2333.top>
  • Loading branch information
qwq233 committed Feb 24, 2024
2 parents bf0446a + 4a8efef commit 4dd4a74
Show file tree
Hide file tree
Showing 175 changed files with 9,144 additions and 1,862 deletions.
171 changes: 168 additions & 3 deletions TMessagesProj/jni/audio.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,19 @@ typedef struct {
int copy_comments;
} oe_enc_opt;

typedef struct {
ogg_int32_t _packetId;
opus_int64 bytes_written;
opus_int64 pages_out;
opus_int64 total_samples;
ogg_int64_t enc_granulepos;
int size_segments;
int last_segments;
ogg_int64_t last_granulepos;
opus_int32 min_bytes;
int max_frame_bytes;
} resume_data;

static int write_uint32(Packet *p, ogg_uint32_t val) {
if (p->pos > p->maxlen - 4) {
return 0;
Expand Down Expand Up @@ -233,6 +246,7 @@ ogg_int32_t _packetId;
OpusEncoder *_encoder = 0;
uint8_t *_packet = 0;
ogg_stream_state os;
const char *_filePath;
FILE *_fileOs = 0;
oe_enc_opt inopt;
OpusHeader header;
Expand Down Expand Up @@ -277,6 +291,10 @@ void cleanupRecorder() {
size_segments = 0;
last_segments = 0;
last_granulepos = 0;
if (_filePath) {
free(_filePath);
_filePath = 0;
}
memset(&os, 0, sizeof(ogg_stream_state));
memset(&inopt, 0, sizeof(oe_enc_opt));
memset(&header, 0, sizeof(OpusHeader));
Expand All @@ -294,7 +312,11 @@ int initRecorder(const char *path, opus_int32 sampleRate) {
LOGE("path is null");
return 0;
}


int length = strlen(path);
_filePath = (char*) malloc(length + 1);
strcpy(_filePath, path);

_fileOs = fopen(path, "w");
if (!_fileOs) {
LOGE("error cannot open file: %s", path);
Expand Down Expand Up @@ -416,6 +438,134 @@ int initRecorder(const char *path, opus_int32 sampleRate) {

return 1;
}

void saveResumeData() {
if (_filePath == NULL) {
return;
}
const char* ext = ".resume";
char* _resumeFilePath = (char*) malloc(strlen(_filePath) + strlen(ext) + 1);
strcpy(_resumeFilePath, _filePath);
strcat(_resumeFilePath, ext);

FILE* resumeFile = fopen(_resumeFilePath, "wb");
if (!resumeFile) {
LOGE("error cannot open resume file to write: %s", _resumeFilePath);
free(_resumeFilePath);
return;
}
resume_data data;
data._packetId = _packetId;
data.bytes_written = bytes_written;
data.pages_out = pages_out;
data.total_samples = total_samples;
data.enc_granulepos = enc_granulepos;
data.size_segments = size_segments;
data.last_segments = last_segments;
data.last_granulepos = last_granulepos;
data.min_bytes = min_bytes;
data.max_frame_bytes = max_frame_bytes;

if (fwrite(&data, sizeof(resume_data), 1, resumeFile) != 1) {
LOGE("error writing resume data to file: %s", _resumeFilePath);
}
fclose(resumeFile);

free(_resumeFilePath);
}

resume_data readResumeData(const char* filePath) {

const char* ext = ".resume";
char* _resumeFilePath = (char*) malloc(strlen(filePath) + strlen(ext) + 1);
strcpy(_resumeFilePath, filePath);
strcat(_resumeFilePath, ext);

resume_data data;

FILE* resumeFile = fopen(_resumeFilePath, "rb");
if (!resumeFile) {
LOGE("error cannot open resume file to read: %s", _resumeFilePath);
memset(&data, 0, sizeof(resume_data));
return data;
}

if (fread(&data, sizeof(resume_data), 1, resumeFile) != 1) {
LOGE("error cannot read resume file: %s", _resumeFilePath);
memset(&data, 0, sizeof(resume_data));
}

fclose(resumeFile);
free(_resumeFilePath);

return data;
}

int resumeRecorder(const char *path, opus_int32 sampleRate) {
cleanupRecorder();

coding_rate = sampleRate;
rate = sampleRate;

if (!path) {
LOGE("path is null");
return 0;
}

int length = strlen(path);
_filePath = (char*) malloc(length + 1);
strcpy(_filePath, path);

resume_data resumeData = readResumeData(path);
_packetId = resumeData._packetId;
bytes_written = resumeData.bytes_written;
pages_out = resumeData.pages_out;
total_samples = resumeData.total_samples;
enc_granulepos = resumeData.enc_granulepos;
size_segments = resumeData.size_segments;
last_segments = resumeData.last_segments;
last_granulepos = resumeData.last_granulepos;
min_bytes = resumeData.min_bytes;
max_frame_bytes = resumeData.max_frame_bytes;

_fileOs = fopen(path, "a");
if (!_fileOs) {
LOGE("error cannot open resume file: %s", path);
return 0;
}

int result = OPUS_OK;
_encoder = opus_encoder_create(coding_rate, 1, OPUS_APPLICATION_VOIP, &result);
if (result != OPUS_OK) {
LOGE("Error cannot create encoder: %s", opus_strerror(result));
return 0;
}

_packet = malloc(max_frame_bytes);

result = opus_encoder_ctl(_encoder, OPUS_SET_BITRATE(bitrate));
//result = opus_encoder_ctl(_encoder, OPUS_SET_COMPLEXITY(10));
if (result != OPUS_OK) {
LOGE("Error OPUS_SET_BITRATE returned: %s", opus_strerror(result));
return 0;
}

#ifdef OPUS_SET_LSB_DEPTH
result = opus_encoder_ctl(_encoder, OPUS_SET_LSB_DEPTH(MAX(8, MIN(24, 16))));
if (result != OPUS_OK) {
LOGE("Warning OPUS_SET_LSB_DEPTH returned: %s", opus_strerror(result));
}
#endif

if (ogg_stream_init(&os, rand()) == -1) {
LOGE("Error: stream init failed");
return 0;
}

return 1;
}


int writeFrame(uint8_t *framePcmBytes, uint32_t frameByteCount) {
size_t cur_frame_size = frame_size;
_packetId++;
Expand All @@ -433,7 +583,7 @@ int writeFrame(uint8_t *framePcmBytes, uint32_t frameByteCount) {
if (nb_samples != 0) {
uint8_t *paddedFrameBytes = framePcmBytes;
int freePaddedFrameBytes = 0;

if (nb_samples < cur_frame_size) {
paddedFrameBytes = malloc(cur_frame_size * 2);
freePaddedFrameBytes = 1;
Expand Down Expand Up @@ -503,6 +653,18 @@ JNIEXPORT jint Java_org_telegram_messenger_MediaController_startRecord(JNIEnv *e
const char *pathStr = (*env)->GetStringUTFChars(env, path, 0);

int32_t result = initRecorder(pathStr, sampleRate);

if (pathStr != 0) {
(*env)->ReleaseStringUTFChars(env, path, pathStr);
}

return result;
}

JNIEXPORT jint Java_org_telegram_messenger_MediaController_resumeRecord(JNIEnv *env, jclass class, jstring path, jint sampleRate) {
const char *pathStr = (*env)->GetStringUTFChars(env, path, 0);

int32_t result = resumeRecorder(pathStr, sampleRate);

if (pathStr != 0) {
(*env)->ReleaseStringUTFChars(env, path, pathStr);
Expand All @@ -516,7 +678,10 @@ JNIEXPORT jint Java_org_telegram_messenger_MediaController_writeFrame(JNIEnv *en
return writeFrame((uint8_t *) frameBytes, (uint32_t) len);
}

JNIEXPORT void Java_org_telegram_messenger_MediaController_stopRecord(JNIEnv *env, jclass class) {
JNIEXPORT void Java_org_telegram_messenger_MediaController_stopRecord(JNIEnv *env, jclass class, jboolean allowResuming) {
if (allowResuming && _filePath != NULL) {
saveResumeData();
}
cleanupRecorder();
}

Expand Down
2 changes: 2 additions & 0 deletions TMessagesProj/jni/opus/src/opus_encoder.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@
#include "tuning_parameters.h"
#ifdef FIXED_POINT
#include "fixed/structs_FIX.h"
#include "c_utils.h"

#else
#include "float/structs_FLP.h"
#endif
Expand Down
2 changes: 1 addition & 1 deletion TMessagesProj/jni/tgnet/Connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ void Connection::onReceivedData(NativeByteBuffer *buffer) {
parseLaterBuffer = buffer->hasRemaining() ? buffer : nullptr;
buffer = restOfTheData;
} else {
if (LOGS_ENABLED) DEBUG_D("connection(%p, account%u, dc%u, type %d) received packet size less(%u) then message size(%u)", this, currentDatacenter->instanceNum, currentDatacenter->getDatacenterId(), connectionType, restOfTheData->position(), lastPacketLength);
// if (LOGS_ENABLED) DEBUG_D("connection(%p, account%u, dc%u, type %d) received packet size less(%u) then message size(%u)", this, currentDatacenter->instanceNum, currentDatacenter->getDatacenterId(), connectionType, restOfTheData->position(), lastPacketLength);
return;
}
}
Expand Down
11 changes: 9 additions & 2 deletions TMessagesProj/jni/tgnet/ConnectionSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,12 @@ void ConnectionSocket::onEvent(uint32_t events) {
while (true) {
buffer->rewind();
readCount = recv(socketFd, buffer->bytes(), READ_BUFFER_SIZE, 0);
int err = errno;
// if (LOGS_ENABLED) DEBUG_D("connection(%p) recv resulted with %d, errno=%d", this, readCount, err);
if (readCount < 0) {
if (err == EAGAIN) {
break;
}
closeSocket(1, -1);
if (LOGS_ENABLED) DEBUG_E("connection(%p) recv failed", this);
return;
Expand Down Expand Up @@ -850,10 +855,12 @@ void ConnectionSocket::onEvent(uint32_t events) {
onReceivedData(buffer);
}
}
}
if (readCount != READ_BUFFER_SIZE) {
} else if (readCount == 0) {
break;
}
// if (readCount != READ_BUFFER_SIZE) {
// break;
// }
}
}
}
Expand Down
25 changes: 16 additions & 9 deletions TMessagesProj/jni/tgnet/ConnectionsManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -979,11 +979,13 @@ void ConnectionsManager::onConnectionDataReceived(Connection *connection, Native
sendMessagesToConnectionWithConfirmation(messages, connection, false);
}
} else {
if (LOGS_ENABLED) DEBUG_D("connection(%p, account%u, dc%u, type %d) received unparsed packet on 0x%" PRIx64, connection, instanceNum, datacenter->getDatacenterId(), connection->getConnectionType(), messageId);
if (delegate != nullptr) {
delegate->onUnparsedMessageReceived(messageId, data, connection->getConnectionType(), instanceNum);
}
}
} else {
if (LOGS_ENABLED) DEBUG_D("connection(%p, account%u, dc%u, type %d) received unprocessed packet on 0x%" PRIx64, connection, instanceNum, datacenter->getDatacenterId(), connection->getConnectionType(), messageId);
std::vector<std::unique_ptr<NetworkMessage>> messages;
sendMessagesToConnectionWithConfirmation(messages, connection, false);
}
Expand Down Expand Up @@ -1061,6 +1063,9 @@ TLObject *ConnectionsManager::TLdeserialize(TLObject *request, uint32_t bytes, N
}
}
} else {
if (constructor == 0x96a18d5) {
if (LOGS_ENABLED) DEBUG_D("not found file 0x%x", constructor);
}
if (LOGS_ENABLED) DEBUG_D("not found request to parse constructor 0x%x", constructor);
}
}
Expand Down Expand Up @@ -1630,6 +1635,7 @@ void ConnectionsManager::processServerResponse(TLObject *message, int64_t messag
processServerResponse(object, messageId, messageSeqNo, messageSalt, connection, innerMsgId, containerMessageId);
delete object;
} else {
if (LOGS_ENABLED) DEBUG_D("connection(%p, account%u, dc%u, type %d) received unparsed from gzip object on %0x" PRIx64, connection, instanceNum, datacenter->getDatacenterId(), connection->getConnectionType(), messageId);
if (delegate != nullptr) {
delegate->onUnparsedMessageReceived(messageId, data, connection->getConnectionType(), instanceNum);
}
Expand Down Expand Up @@ -1978,7 +1984,7 @@ bool ConnectionsManager::cancelRequestInternal(int32_t token, int64_t messageId,
Request *request = iter->get();
if ((token != 0 && request->requestToken == token) || (messageId != 0 && request->respondsToMessageId(messageId))) {
request->cancelled = true;
if (LOGS_ENABLED) DEBUG_D("cancelled queued rpc request %p - %s", request->rawRequest, typeid(*request->rawRequest).name());
if (LOGS_ENABLED) DEBUG_D("cancelled queued rpc request %p - %s of messageId 0x%" PRIx64, request->rawRequest, typeid(*request->rawRequest).name(), request->messageId);
requestsQueue.erase(iter);
if (removeFromClass) {
removeRequestFromGuid(token);
Expand All @@ -1991,7 +1997,7 @@ bool ConnectionsManager::cancelRequestInternal(int32_t token, int64_t messageId,
Request *request = iter->get();
if ((token != 0 && request->requestToken == token) || (messageId != 0 && request->respondsToMessageId(messageId))) {
request->cancelled = true;
if (LOGS_ENABLED) DEBUG_D("cancelled waiting login rpc request %p - %s", request->rawRequest, typeid(*request->rawRequest).name());
if (LOGS_ENABLED) DEBUG_D("cancelled waiting login rpc request %p - %s", request->rawRequest, typeid(*request->rawRequest).name());
waitingLoginRequests.erase(iter);
if (removeFromClass) {
removeRequestFromGuid(token);
Expand All @@ -2009,7 +2015,7 @@ bool ConnectionsManager::cancelRequestInternal(int32_t token, int64_t messageId,
sendRequest(dropAnswer, nullptr, nullptr, RequestFlagEnableUnauthorized | RequestFlagWithoutLogin | RequestFlagFailOnServerErrors | RequestFlagIsCancel, request->datacenterId, request->connectionType, true);
}
request->cancelled = true;
if (LOGS_ENABLED) DEBUG_D("cancelled running rpc request %p - %s", request->rawRequest, typeid(*request->rawRequest).name());
if (LOGS_ENABLED) DEBUG_D("cancelled running rpc request %p - %s, of messageId 0x%" PRIx64, request->rawRequest, typeid(*request->rawRequest).name(), request->messageId);
runningRequests.erase(iter);
if (removeFromClass) {
removeRequestFromGuid(token);
Expand Down Expand Up @@ -2361,12 +2367,13 @@ void ConnectionsManager::processRequestQueue(uint32_t connectionTypes, uint32_t
forceThisRequest = false;
}

if (forceThisRequest || (abs(currentTime - request->startTime) > maxTimeout &&
(currentTime >= request->minStartTime ||
(request->failedByFloodWait != 0 && (request->minStartTime - currentTime) > request->failedByFloodWait) ||
(request->failedByFloodWait == 0 && abs(currentTime - request->minStartTime) >= 60))
)
) {
if (forceThisRequest || (
abs(currentTime - request->startTime) > maxTimeout && (
currentTime >= request->minStartTime ||
(request->failedByFloodWait != 0 && (request->minStartTime - currentTime) > request->failedByFloodWait) ||
(request->failedByFloodWait == 0 && abs(currentTime - request->minStartTime) >= 60)
)
)) {
if (!forceThisRequest && request->connectionToken > 0) {
if ((request->connectionType & ConnectionTypeGeneric || request->connectionType & ConnectionTypeTemp) && request->connectionToken == connection->getConnectionToken()) {
if (LOGS_ENABLED) DEBUG_D("request token is valid, not retrying %s (%p)", typeInfo.name(), request->rawRequest);
Expand Down
1 change: 0 additions & 1 deletion TMessagesProj/jni/tgnet/ConnectionsManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,6 @@ class ConnectionsManager {
friend class Config;
friend class FileLog;
friend class Handshake;

};

#ifdef ANDROID
Expand Down
Loading

0 comments on commit 4dd4a74

Please sign in to comment.