-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1029 lines (913 loc) · 40.6 KB
/
Copy pathmain.cpp
File metadata and controls
1029 lines (913 loc) · 40.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Telegram Echo — accepts incoming calls, plays prompt, echoes caller audio, records to MP3
// Uses TDLib for signaling + tgcalls for audio
#include <td/telegram/Client.h>
#include <td/telegram/td_api.h>
#include <td/telegram/td_api.hpp>
#include "tgcalls/Instance.h"
#include "tgcalls/InstanceImpl.h"
#include "tgcalls/v2/InstanceV2Impl.h"
#include "tgcalls/v2/InstanceV2ReferenceImpl.h"
#include "tgcalls/FakeAudioDeviceModule.h"
#include "tgcalls/StaticThreads.h"
#include "tgcalls/VideoCaptureInterface.h"
#include "api/video/i420_buffer.h"
#include "api/video/video_frame.h"
#include "api/video/video_sink_interface.h"
#include <libyuv.h>
#include <lame/lame.h>
#include <csignal>
#include <termios.h>
#include <unistd.h>
#include <algorithm>
#include <atomic>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
namespace td_api = td::td_api;
static volatile std::sig_atomic_t g_quit = 0;
// -- MP3 audio recorder (writes caller audio to file) --
class FileRecorder : public tgcalls::FakeAudioDeviceModule::Renderer {
public:
explicit FileRecorder(const std::string &path, int sample_rate = 48000, int num_channels = 2)
: path_(path), num_channels_(num_channels) {
file_.open(path, std::ios::binary);
if (!file_) {
std::cerr << "Failed to open recording file: " << path << std::endl;
return;
}
lame_ = lame_init();
if (!lame_) {
std::cerr << "Failed to initialize LAME encoder" << std::endl;
file_.close();
return;
}
lame_set_in_samplerate(lame_, sample_rate);
lame_set_num_channels(lame_, num_channels);
lame_set_quality(lame_, 2); // high quality
lame_set_VBR(lame_, vbr_default);
if (lame_init_params(lame_) < 0) {
std::cerr << "Failed to set LAME parameters" << std::endl;
lame_close(lame_);
lame_ = nullptr;
file_.close();
}
}
bool Render(const tgcalls::AudioFrame &frame) override {
if (!file_ || !lame_ || frame.num_samples == 0 || !frame.audio_samples)
return true;
int samples_per_channel = static_cast<int>(frame.num_samples / num_channels_);
// Ensure MP3 buffer is large enough (worst case: 1.25 * samples + 7200)
size_t mp3_buf_size = static_cast<size_t>(1.25 * samples_per_channel) + 7200;
if (mp3_buffer_.size() < mp3_buf_size)
mp3_buffer_.resize(mp3_buf_size);
int mp3_bytes;
if (num_channels_ == 2) {
mp3_bytes = lame_encode_buffer_interleaved(
lame_, const_cast<short *>(frame.audio_samples),
samples_per_channel, mp3_buffer_.data(),
static_cast<int>(mp3_buffer_.size()));
} else {
mp3_bytes = lame_encode_buffer(
lame_, frame.audio_samples, nullptr,
samples_per_channel, mp3_buffer_.data(),
static_cast<int>(mp3_buffer_.size()));
}
if (mp3_bytes > 0) {
file_.write(reinterpret_cast<const char *>(mp3_buffer_.data()), mp3_bytes);
total_bytes_ += mp3_bytes;
}
return true;
}
~FileRecorder() override {
if (lame_ && file_) {
// Flush remaining MP3 frames
if (mp3_buffer_.size() < 7200)
mp3_buffer_.resize(7200);
int flush_bytes = lame_encode_flush(lame_, mp3_buffer_.data(),
static_cast<int>(mp3_buffer_.size()));
if (flush_bytes > 0) {
file_.write(reinterpret_cast<const char *>(mp3_buffer_.data()), flush_bytes);
total_bytes_ += flush_bytes;
}
}
if (lame_)
lame_close(lame_);
if (file_) {
file_.close();
std::cout << "Recording saved: " << path_ << " (" << total_bytes_ << " bytes)" << std::endl;
}
}
private:
std::string path_;
std::ofstream file_;
lame_global_flags *lame_ = nullptr;
int num_channels_;
std::vector<unsigned char> mp3_buffer_;
size_t total_bytes_ = 0;
};
// -- Y4M video recorder: writes the caller's incoming video to a .y4m file --
//
// Incoming frames arrive already decoded as I420. We lock onto the first frame's
// resolution and scale any differently-sized frames to match (WebRTC may change the
// resolution mid-call). Output is raw YUV4MPEG2 — no muxer needed; plays in ffmpeg/mpv.
// The file is created lazily on the first frame, so a call with no incoming video
// leaves no file behind.
class VideoRecorder : public rtc::VideoSinkInterface<webrtc::VideoFrame> {
public:
explicit VideoRecorder(const std::string &path) : path_(path) {}
void OnFrame(const webrtc::VideoFrame &frame) override {
auto i420 = frame.video_frame_buffer()->ToI420();
if (!i420)
return;
if (!file_.is_open()) {
width_ = i420->width();
height_ = i420->height();
file_.open(path_, std::ios::binary);
if (!file_) {
std::cerr << "Failed to open video recording file: " << path_ << std::endl;
return;
}
// Stream header. Nominal 30fps (F30:1) — the real incoming rate varies.
file_ << "YUV4MPEG2 W" << width_ << " H" << height_ << " F30:1 Ip A1:1 C420\n";
}
if (!file_)
return;
// Scale to the locked resolution if this frame differs.
webrtc::scoped_refptr<webrtc::I420BufferInterface> out = i420;
if (i420->width() != width_ || i420->height() != height_) {
auto scaled = webrtc::I420Buffer::Create(width_, height_);
libyuv::I420Scale(
i420->DataY(), i420->StrideY(), i420->DataU(), i420->StrideU(),
i420->DataV(), i420->StrideV(), i420->width(), i420->height(),
scaled->MutableDataY(), scaled->StrideY(), scaled->MutableDataU(), scaled->StrideU(),
scaled->MutableDataV(), scaled->StrideV(), width_, height_,
libyuv::kFilterBilinear);
out = scaled;
}
file_ << "FRAME\n";
write_plane(out->DataY(), out->StrideY(), width_, height_);
write_plane(out->DataU(), out->StrideU(), (width_ + 1) / 2, (height_ + 1) / 2);
write_plane(out->DataV(), out->StrideV(), (width_ + 1) / 2, (height_ + 1) / 2);
frames_++;
}
~VideoRecorder() override {
if (file_.is_open()) {
file_.close();
std::cout << "Video recording saved: " << path_ << " (" << frames_ << " frames, "
<< width_ << "x" << height_ << ")" << std::endl;
}
}
private:
void write_plane(const uint8_t *data, int stride, int w, int h) {
for (int row = 0; row < h; row++)
file_.write(reinterpret_cast<const char *>(data + row * stride), w);
}
std::string path_;
std::ofstream file_;
int width_ = 0;
int height_ = 0;
uint64_t frames_ = 0;
};
// -- Echo player: plays prompt once, then echoes caller audio with delay --
class EchoPlayer : public tgcalls::FakeAudioDeviceModule::Recorder,
public tgcalls::FakeAudioDeviceModule::Renderer {
public:
static constexpr size_t kSamplesPerFrame = 480; // 10ms at 48kHz
static constexpr size_t kChannels = 2;
static constexpr size_t kFrameInt16s = kSamplesPerFrame * kChannels;
static constexpr size_t kFrameBytes = kFrameInt16s * sizeof(int16_t);
EchoPlayer(const std::string &prompt_path, int delay_ms,
std::shared_ptr<FileRecorder> file_recorder)
: file_recorder_(std::move(file_recorder)),
delay_frames_(std::max(1, delay_ms / 10)),
out_buf_(kFrameInt16s, 0) {
// Load MP3 prompt and decode to PCM
prompt_data_ = load_mp3_as_pcm(prompt_path);
if (!prompt_data_.empty()) {
std::cout << "Loaded prompt: " << prompt_path << " (" << prompt_data_.size() << " bytes PCM)" << std::endl;
}
// Allocate ring buffer for echo delay
ring_.resize(delay_frames_);
for (auto &f : ring_)
f.resize(kFrameInt16s, 0);
std::cout << "Echo delay: " << delay_ms << " ms (" << delay_frames_ << " frames)" << std::endl;
}
void set_enabled(bool enabled) { enabled_.store(enabled); }
// Renderer: receives caller's audio (render thread)
bool Render(const tgcalls::AudioFrame &frame) override {
if (!enabled_.load()) return true;
if (file_recorder_)
file_recorder_->Render(frame);
if (frame.num_samples == 0 || !frame.audio_samples)
return true;
// Store in ring buffer for echo
std::lock_guard<std::mutex> lock(echo_mutex_);
std::memcpy(ring_[write_frame_].data(), frame.audio_samples,
kFrameInt16s * sizeof(int16_t));
write_frame_ = (write_frame_ + 1) % delay_frames_;
frames_written_++;
return true;
}
// Recorder: provides audio to send (record thread)
tgcalls::AudioFrame Record() override {
tgcalls::AudioFrame frame{};
frame.audio_samples = out_buf_.data();
frame.num_samples = kSamplesPerFrame;
frame.bytes_per_sample = sizeof(int16_t);
frame.num_channels = kChannels;
frame.samples_per_sec = 48000;
if (!enabled_.load()) {
std::memset(out_buf_.data(), 0, kFrameInt16s * sizeof(int16_t));
return frame;
}
// Phase 1: play prompt once
if (!prompt_done_) {
if (!prompt_data_.empty() && prompt_pos_ < prompt_data_.size()) {
size_t bytes_copied = 0;
while (bytes_copied < kFrameBytes && prompt_pos_ < prompt_data_.size()) {
size_t to_copy = std::min(kFrameBytes - bytes_copied,
prompt_data_.size() - prompt_pos_);
std::memcpy(reinterpret_cast<char *>(out_buf_.data()) + bytes_copied,
prompt_data_.data() + prompt_pos_, to_copy);
bytes_copied += to_copy;
prompt_pos_ += to_copy;
}
if (bytes_copied < kFrameBytes) {
std::memset(reinterpret_cast<char *>(out_buf_.data()) + bytes_copied,
0, kFrameBytes - bytes_copied);
}
if (prompt_pos_ >= prompt_data_.size()) {
prompt_done_ = true;
std::cout << "Prompt finished, switching to echo mode" << std::endl;
}
return frame;
}
prompt_done_ = true;
std::cout << "No prompt data, switching to echo mode" << std::endl;
}
// Phase 2: echo with delay
{
std::lock_guard<std::mutex> lock(echo_mutex_);
if (frames_written_ < delay_frames_) {
// Not enough audio buffered yet — send silence
std::memset(out_buf_.data(), 0, kFrameInt16s * sizeof(int16_t));
} else {
// write_frame_ points to the oldest frame (delay_frames_ behind latest)
std::memcpy(out_buf_.data(), ring_[write_frame_].data(),
kFrameInt16s * sizeof(int16_t));
}
}
return frame;
}
private:
std::shared_ptr<FileRecorder> file_recorder_;
const size_t delay_frames_;
// Prompt state (only accessed from record thread)
std::vector<uint8_t> prompt_data_;
size_t prompt_pos_ = 0;
bool prompt_done_ = false;
// Echo ring buffer (shared between threads, protected by mutex)
std::mutex echo_mutex_;
std::vector<std::vector<int16_t>> ring_;
size_t write_frame_ = 0;
size_t frames_written_ = 0;
// Output buffer (only accessed from record thread)
std::vector<int16_t> out_buf_;
std::atomic<bool> enabled_{false};
// Decode MP3 file to raw PCM bytes (48kHz stereo s16le)
static std::vector<uint8_t> load_mp3_as_pcm(const std::string &path) {
std::ifstream file(path, std::ios::binary | std::ios::ate);
if (!file) {
std::cerr << "Failed to open MP3 prompt: " << path << std::endl;
return {};
}
auto size = file.tellg();
file.seekg(0);
std::vector<unsigned char> mp3_data(size);
file.read(reinterpret_cast<char *>(mp3_data.data()), size);
hip_t hip = hip_decode_init();
if (!hip) {
std::cerr << "Failed to initialize MP3 decoder" << std::endl;
return {};
}
// Feed all MP3 data at once, then drain decoded frames one at a time
constexpr int kMaxSamples = 1152;
int16_t pcm_l[kMaxSamples], pcm_r[kMaxSamples];
std::vector<uint8_t> result;
// Feed entire MP3 buffer — hip buffers it internally
int samples = hip_decode1(hip, mp3_data.data(),
static_cast<int>(mp3_data.size()),
pcm_l, pcm_r);
auto append_samples = [&](int n) {
for (int i = 0; i < n; i++) {
auto l = pcm_l[i], r = pcm_r[i];
result.push_back(static_cast<uint8_t>(l & 0xFF));
result.push_back(static_cast<uint8_t>((l >> 8) & 0xFF));
result.push_back(static_cast<uint8_t>(r & 0xFF));
result.push_back(static_cast<uint8_t>((r >> 8) & 0xFF));
}
};
if (samples > 0)
append_samples(samples);
// Drain remaining decoded frames (pass size=0 to pull buffered data)
while ((samples = hip_decode1(hip, mp3_data.data(), 0,
pcm_l, pcm_r)) > 0) {
append_samples(samples);
}
hip_decode_exit(hip);
return result;
}
};
// -- Utility --
static std::string hex_string(const std::string &bytes) {
static const char hex[] = "0123456789abcdef";
std::string result;
result.reserve(bytes.size() * 2);
for (unsigned char c : bytes) {
result.push_back(hex[c >> 4]);
result.push_back(hex[c & 0xf]);
}
return result;
}
static std::string timestamp_string() {
auto now = std::chrono::system_clock::now();
auto epoch = now.time_since_epoch();
auto seconds = std::chrono::duration_cast<std::chrono::seconds>(epoch).count();
return std::to_string(seconds);
}
// -- TDLib service base class --
class TdService {
public:
using Object = td_api::object_ptr<td_api::Object>;
using AuthHandler = std::function<void(Object)>;
TdService(int32_t api_id, const std::string &api_hash)
: api_id_(api_id), api_hash_(api_hash) {
td::ClientManager::execute(td_api::make_object<td_api::setLogVerbosityLevel>(1));
client_manager_ = std::make_unique<td::ClientManager>();
client_id_ = client_manager_->create_client_id();
send_query(td_api::make_object<td_api::getOption>("version"), {});
}
virtual ~TdService() = default;
void run() {
while (!quit_ && !g_quit) {
auto response = client_manager_->receive(1.0);
if (response.object) {
process_response(std::move(response));
}
}
}
protected:
bool quit_ = false;
void send_query(td_api::object_ptr<td_api::Function> f, std::function<void(Object)> handler) {
auto id = ++query_id_;
if (handler) handlers_.emplace(id, std::move(handler));
client_manager_->send(client_id_, id, std::move(f));
}
virtual void on_authorized() = 0;
virtual void on_auth_challenge(td_api::object_ptr<td_api::AuthorizationState> state, AuthHandler handler) {
std::cerr << "Not authenticated — run `tg-echo auth` first" << std::endl;
quit_ = true;
}
virtual void on_update(Object update) {}
private:
int32_t api_id_;
std::string api_hash_;
bool authorized_ = false;
std::unique_ptr<td::ClientManager> client_manager_;
td::ClientManager::ClientId client_id_{0};
uint64_t query_id_{0};
uint64_t auth_query_id_{0};
std::map<uint64_t, std::function<void(Object)>> handlers_;
void process_response(td::ClientManager::Response response) {
if (!response.object) return;
if (response.request_id == 0) {
process_update_internal(std::move(response.object));
} else {
auto it = handlers_.find(response.request_id);
if (it != handlers_.end()) {
it->second(std::move(response.object));
handlers_.erase(it);
}
}
}
void process_update_internal(Object update) {
if (update->get_id() == td_api::updateAuthorizationState::ID) {
auto &u = static_cast<td_api::updateAuthorizationState &>(*update);
on_auth_state(std::move(u.authorization_state_));
} else {
on_update(std::move(update));
}
}
void on_auth_state(td_api::object_ptr<td_api::AuthorizationState> state) {
auth_query_id_++;
auto handler = [this, expected = auth_query_id_](Object obj) {
if (expected != auth_query_id_) return;
if (obj->get_id() == td_api::error::ID) {
auto &err = static_cast<td_api::error &>(*obj);
std::cerr << "Auth error: " << err.code_ << " " << err.message_ << std::endl;
}
};
switch (state->get_id()) {
case td_api::authorizationStateWaitTdlibParameters::ID: {
auto req = td_api::make_object<td_api::setTdlibParameters>();
req->database_directory_ = "tdlib_db";
req->use_message_database_ = false;
req->use_secret_chats_ = false;
req->api_id_ = api_id_;
req->api_hash_ = api_hash_;
req->system_language_code_ = "en";
req->device_model_ = "CallService";
req->application_version_ = "1.0";
send_query(std::move(req), handler);
break;
}
case td_api::authorizationStateWaitPhoneNumber::ID:
case td_api::authorizationStateWaitCode::ID:
case td_api::authorizationStateWaitPassword::ID:
on_auth_challenge(std::move(state), handler);
break;
case td_api::authorizationStateReady::ID:
authorized_ = true;
on_authorized();
break;
case td_api::authorizationStateClosed::ID:
std::cout << "Session closed." << std::endl;
quit_ = true;
break;
default:
break;
}
}
};
// -- Auth service: interactive auth, exits after success --
class AuthService : public TdService {
public:
using TdService::TdService;
protected:
void on_authorized() override {
std::cout << "Authorized." << std::endl;
quit_ = true;
}
void on_auth_challenge(td_api::object_ptr<td_api::AuthorizationState> state, AuthHandler handler) override {
switch (state->get_id()) {
case td_api::authorizationStateWaitPhoneNumber::ID: {
std::string phone;
if (!read_input("Enter phone number: ", phone)) return;
send_query(td_api::make_object<td_api::setAuthenticationPhoneNumber>(phone, nullptr), handler);
break;
}
case td_api::authorizationStateWaitCode::ID: {
std::string code;
if (!read_input("Enter auth code: ", code)) return;
send_query(td_api::make_object<td_api::checkAuthenticationCode>(code), handler);
break;
}
case td_api::authorizationStateWaitPassword::ID: {
std::string password;
if (!read_password("Enter 2FA password: ", password)) return;
send_query(td_api::make_object<td_api::checkAuthenticationPassword>(password), handler);
break;
}
default:
break;
}
}
private:
bool read_input(const char *prompt, std::string &out) {
std::cout << prompt << std::flush;
if (!std::getline(std::cin, out)) {
std::cerr << "No stdin — run the auth service first (podman compose run auth)" << std::endl;
quit_ = true;
return false;
}
return true;
}
bool read_password(const char *prompt, std::string &out) {
std::cout << prompt << std::flush;
termios oldt{};
bool is_tty = isatty(STDIN_FILENO);
if (is_tty) {
tcgetattr(STDIN_FILENO, &oldt);
termios newt = oldt;
newt.c_lflag &= ~ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
}
bool ok = !!std::getline(std::cin, out);
if (is_tty) {
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
std::cout << std::endl;
}
if (!ok) {
std::cerr << "No stdin — run the auth service first (podman compose run auth)" << std::endl;
quit_ = true;
}
return ok;
}
};
// -- Call service: accepts calls, plays prompt, echoes audio, records --
class CallService : public TdService {
public:
CallService(int32_t api_id, const std::string &api_hash,
const std::string &prompt_file, const std::string &recordings_dir,
int echo_delay_ms = 1000, bool video_enabled = true)
: TdService(api_id, api_hash),
prompt_file_(prompt_file), recordings_dir_(recordings_dir),
echo_delay_ms_(echo_delay_ms), video_enabled_(video_enabled) {
tgcalls::Register<tgcalls::InstanceImpl>();
tgcalls::Register<tgcalls::InstanceV2Impl>();
tgcalls::Register<tgcalls::InstanceV2ReferenceImpl>();
auto versions = tgcalls::Meta::Versions();
std::cout << "tgcalls versions:";
for (const auto &v : versions) std::cout << " " << v;
std::cout << std::endl;
}
protected:
void on_authorized() override {
send_query(td_api::make_object<td_api::setOption>(
"online", td_api::make_object<td_api::optionValueBoolean>(true)), {});
send_query(td_api::make_object<td_api::getMe>(), [](Object obj) {
if (obj->get_id() == td_api::error::ID) {
auto &err = static_cast<td_api::error &>(*obj);
std::cerr << "getMe failed: " << err.code_ << " " << err.message_ << std::endl;
return;
}
auto &user = static_cast<td_api::user &>(*obj);
std::string name = user.first_name_;
if (!user.last_name_.empty()) name += " " + user.last_name_;
std::string handle;
if (user.usernames_) {
if (!user.usernames_->editable_username_.empty()) {
handle = user.usernames_->editable_username_;
} else if (!user.usernames_->active_usernames_.empty()) {
handle = user.usernames_->active_usernames_.front();
}
}
std::cout << "Logged in as " << name << " (id=" << user.id_;
if (!handle.empty()) std::cout << ", @" << handle;
if (!user.phone_number_.empty()) std::cout << ", +" << user.phone_number_;
std::cout << ")" << std::endl;
});
std::cout << "Authorized. Waiting for incoming calls..." << std::endl;
}
void on_update(Object update) override {
auto id = update->get_id();
if (id == td_api::updateCall::ID) {
auto &u = static_cast<td_api::updateCall &>(*update);
on_call_update(std::move(u.call_));
} else if (id == td_api::updateNewCallSignalingData::ID) {
auto &u = static_cast<td_api::updateNewCallSignalingData &>(*update);
on_signaling_data(u.call_id_, u.data_);
} else if (id == td_api::updateConnectionState::ID) {
auto &u = static_cast<td_api::updateConnectionState &>(*update);
const char *name = "unknown";
switch (u.state_->get_id()) {
case td_api::connectionStateWaitingForNetwork::ID: name = "WaitingForNetwork"; break;
case td_api::connectionStateConnectingToProxy::ID: name = "ConnectingToProxy"; break;
case td_api::connectionStateConnecting::ID: name = "Connecting"; break;
case td_api::connectionStateUpdating::ID: name = "Updating"; break;
case td_api::connectionStateReady::ID: name = "Ready"; break;
}
std::cout << "connection state: " << name << std::endl;
}
}
private:
std::string prompt_file_;
std::string recordings_dir_;
int echo_delay_ms_;
bool video_enabled_;
// Active call state
int32_t active_call_id_ = 0;
int64_t caller_user_id_ = 0;
bool active_call_outgoing_ = false;
std::unique_ptr<tgcalls::Instance> tgcalls_instance_;
tgcalls::State tgcalls_state_ = tgcalls::State::WaitInit;
bool voice_established_ = false;
std::shared_ptr<EchoPlayer> echo_player_;
std::shared_ptr<FileRecorder> file_recorder_;
std::string recording_path_;
std::shared_ptr<tgcalls::VideoCaptureInterface> video_capture_;
std::shared_ptr<VideoRecorder> video_recorder_;
std::string video_recording_path_;
void on_call_update(td_api::object_ptr<td_api::call> call) {
auto call_id = call->id_;
auto state_id = call->state_->get_id();
if (state_id == td_api::callStatePending::ID) {
if (!call->is_outgoing_ && active_call_id_ == 0) {
// Incoming call — accept it
active_call_id_ = call_id;
caller_user_id_ = call->user_id_;
active_call_outgoing_ = false;
std::cout << "Incoming call #" << call_id << " from user " << call->user_id_ << " — accepting" << std::endl;
auto versions = tgcalls::Meta::Versions();
auto protocol = td_api::make_object<td_api::callProtocol>(
true, true, 65, 92, std::move(versions));
send_query(td_api::make_object<td_api::acceptCall>(call_id, std::move(protocol)), {});
} else if (!call->is_outgoing_ && active_call_id_ != 0) {
std::cout << "Rejecting call #" << call_id << " — already in a call" << std::endl;
send_query(td_api::make_object<td_api::discardCall>(call_id, false, "", 0, false, 0), {});
}
} else if (state_id == td_api::callStateReady::ID) {
if (call_id == active_call_id_) {
auto &ready = static_cast<td_api::callStateReady &>(*call->state_);
start_tgcalls(call_id, call->is_outgoing_, ready);
}
} else if (state_id == td_api::callStateDiscarded::ID ||
state_id == td_api::callStateError::ID ||
state_id == td_api::callStateHangingUp::ID) {
if (call_id == active_call_id_) {
std::cout << "Call #" << call_id << " ended" << std::endl;
stop_tgcalls();
}
}
}
void on_signaling_data(int32_t call_id, const std::string &data) {
if (call_id == active_call_id_ && tgcalls_instance_) {
std::vector<uint8_t> bytes(data.begin(), data.end());
tgcalls_instance_->receiveSignalingData(bytes);
}
}
void start_tgcalls(int32_t call_id, bool is_outgoing, td_api::callStateReady &ready) {
std::cout << "Call #" << call_id << " ready — starting tgcalls" << std::endl;
std::cout << " servers: " << ready.servers_.size() << std::endl;
std::cout << " encryption_key: " << ready.encryption_key_.size() << " bytes" << std::endl;
std::cout << " allow_p2p: " << ready.allow_p2p_ << std::endl;
// Pick version
std::string version;
if (ready.protocol_ && !ready.protocol_->library_versions_.empty()) {
// Use the best matching version
auto our_versions = tgcalls::Meta::Versions();
for (const auto &theirs : ready.protocol_->library_versions_) {
for (const auto &ours : our_versions) {
if (theirs == ours) {
version = ours;
break;
}
}
if (!version.empty()) break;
}
if (version.empty()) {
version = our_versions.back();
}
} else {
version = tgcalls::Meta::Versions().back();
}
std::cout << " using tgcalls version: " << version << std::endl;
// Map servers
std::vector<tgcalls::Endpoint> endpoints;
std::vector<tgcalls::RtcServer> rtc_servers;
std::vector<int64_t> reflector_ids;
for (const auto &srv : ready.servers_) {
auto type_id = srv->type_->get_id();
if (type_id == td_api::callServerTypeTelegramReflector::ID) {
const auto &ref = static_cast<const td_api::callServerTypeTelegramReflector &>(*srv->type_);
tgcalls::Endpoint ep{};
ep.endpointId = srv->id_;
ep.host = {srv->ip_address_, srv->ipv6_address_};
ep.port = static_cast<uint16_t>(srv->port_);
ep.type = ref.is_tcp_ ? tgcalls::EndpointType::TcpRelay : tgcalls::EndpointType::UdpRelay;
if (ref.peer_tag_.size() >= 16) {
std::memcpy(ep.peerTag, ref.peer_tag_.data(), 16);
}
endpoints.push_back(ep);
reflector_ids.push_back(srv->id_);
} else if (type_id == td_api::callServerTypeWebrtc::ID) {
const auto &webrtc = static_cast<const td_api::callServerTypeWebrtc &>(*srv->type_);
tgcalls::RtcServer rtc{};
rtc.id = static_cast<uint8_t>(srv->id_);
rtc.host = srv->ip_address_.empty() ? srv->ipv6_address_ : srv->ip_address_;
rtc.port = static_cast<uint16_t>(srv->port_);
rtc.login = webrtc.username_;
rtc.password = webrtc.password_;
rtc.isTurn = webrtc.supports_turn_;
rtc_servers.push_back(rtc);
}
}
// Add reflectors as RtcServers too (like Telegram-X does)
std::sort(reflector_ids.begin(), reflector_ids.end());
for (const auto &srv : ready.servers_) {
if (srv->type_->get_id() == td_api::callServerTypeTelegramReflector::ID) {
const auto &ref = static_cast<const td_api::callServerTypeTelegramReflector &>(*srv->type_);
auto itr = std::find(reflector_ids.begin(), reflector_ids.end(), srv->id_);
size_t reflector_id = itr - reflector_ids.begin() + 1;
tgcalls::RtcServer rtc{};
rtc.id = static_cast<uint8_t>(reflector_id);
rtc.host = srv->ip_address_.empty() ? srv->ipv6_address_ : srv->ip_address_;
rtc.port = static_cast<uint16_t>(srv->port_);
rtc.login = "reflector";
rtc.password = hex_string(ref.peer_tag_);
rtc.isTurn = true;
rtc.isTcp = ref.is_tcp_;
rtc_servers.push_back(rtc);
}
}
// Encryption key
auto key = std::make_shared<std::array<uint8_t, tgcalls::EncryptionKey::kSize>>();
if (ready.encryption_key_.size() >= tgcalls::EncryptionKey::kSize) {
std::memcpy(key->data(), ready.encryption_key_.data(), tgcalls::EncryptionKey::kSize);
}
// Audio: echo player + MP3 recorder
std::string ts = timestamp_string();
recording_path_ = recordings_dir_ + "/recording_" + ts + ".mp3";
file_recorder_ = std::make_shared<FileRecorder>(recording_path_);
echo_player_ = std::make_shared<EchoPlayer>(prompt_file_, echo_delay_ms_, file_recorder_);
// Video: animated outgoing prompt (sent for the whole call) + incoming recorder.
// The capture's source self-feeds a looping animation; the recorder is attached
// to the instance below via setIncomingVideoOutput().
if (video_enabled_) {
video_capture_ = tgcalls::VideoCaptureInterface::Create(tgcalls::StaticThreads::getThreads());
video_recording_path_ = recordings_dir_ + "/recording_" + ts + ".y4m";
video_recorder_ = std::make_shared<VideoRecorder>(video_recording_path_);
}
int max_layer = ready.protocol_ ? ready.protocol_->max_layer_ : 92;
// Build descriptor
tgcalls::Descriptor descriptor{
.version = version,
.config = tgcalls::Config{
.initializationTimeout = 30.0,
.receiveTimeout = 30.0,
.dataSaving = tgcalls::DataSaving::Never,
.enableP2P = ready.allow_p2p_,
.allowTCP = false,
.enableStunMarking = false,
.enableAEC = false,
.enableNS = false,
.enableAGC = false,
.enableCallUpgrade = false,
.enableVolumeControl = false,
.maxApiLayer = max_layer,
.enableHighBitrateVideo = false,
.customParameters = ready.custom_parameters_
},
.endpoints = std::move(endpoints),
.rtcServers = std::move(rtc_servers),
.initialNetworkType = tgcalls::NetworkType::WiFi,
.encryptionKey = tgcalls::EncryptionKey(std::move(key), is_outgoing),
.videoCapture = video_capture_,
.stateUpdated = [this, call_id](tgcalls::State state) {
const char *names[] = {"WaitInit", "WaitInitAck", "Established", "Failed", "Reconnecting"};
if (state == tgcalls_state_) return;
tgcalls_state_ = state;
std::cout << "tgcalls state: " << names[static_cast<int>(state)] << std::endl;
if (state == tgcalls::State::Established) {
voice_established_ = true;
if (echo_player_) echo_player_->set_enabled(true);
} else if (state == tgcalls::State::Failed) {
if (echo_player_) echo_player_->set_enabled(false);
std::cout << "tgcalls failed — discarding call #" << call_id << std::endl;
send_query(td_api::make_object<td_api::discardCall>(call_id, true, "", 0, false, 0), {});
}
},
.signalingDataEmitted = [this, call_id](const std::vector<uint8_t> &data) {
std::string bytes(data.begin(), data.end());
send_query(td_api::make_object<td_api::sendCallSignalingData>(call_id, std::move(bytes)), {});
},
.createAudioDeviceModule = tgcalls::FakeAudioDeviceModule::Creator(
std::shared_ptr<tgcalls::FakeAudioDeviceModule::Renderer>(echo_player_, echo_player_.get()),
std::shared_ptr<tgcalls::FakeAudioDeviceModule::Recorder>(echo_player_, echo_player_.get()),
tgcalls::FakeAudioDeviceModule::Options{
.samples_per_sec = 48000,
.num_channels = 2
}
),
};
tgcalls_instance_ = tgcalls::Meta::Create(version, std::move(descriptor));
if (tgcalls_instance_) {
tgcalls_instance_->setNetworkType(tgcalls::NetworkType::WiFi);
tgcalls_instance_->setMuteMicrophone(false);
if (video_recorder_)
tgcalls_instance_->setIncomingVideoOutput(video_recorder_);
std::cout << "tgcalls instance created" << std::endl;
} else {
std::cerr << "Failed to create tgcalls instance for version: " << version << std::endl;
}
}
void stop_tgcalls() {
if (tgcalls_instance_) {
tgcalls_instance_->stop([](tgcalls::FinalState) {});
tgcalls_instance_.reset();
}
tgcalls_state_ = tgcalls::State::WaitInit;
echo_player_.reset();
file_recorder_.reset();
video_capture_.reset(); // stops the animation thread
video_recorder_.reset(); // flushes + closes the .y4m file (logs if any frames)
video_recording_path_.clear();
if (caller_user_id_ != 0 && !recording_path_.empty() && voice_established_) {
send_voice_message(caller_user_id_, recording_path_);
} else if (!recording_path_.empty()) {
std::cout << "Voice never established — discarding recording " << recording_path_ << std::endl;
std::remove(recording_path_.c_str());
}
voice_established_ = false;
active_call_id_ = 0;
caller_user_id_ = 0;
recording_path_.clear();
std::cout << "Waiting for next call..." << std::endl;
}
void send_voice_message(int64_t user_id, const std::string &path) {
std::cout << "Sending voice message to user " << user_id << ": " << path << std::endl;
send_query(td_api::make_object<td_api::createPrivateChat>(user_id, false),
[this, path](Object response) {
if (response->get_id() == td_api::error::ID) {
auto &err = static_cast<td_api::error &>(*response);
std::cerr << "Failed to create chat: " << err.message_ << std::endl;
return;
}
auto &chat = static_cast<td_api::chat &>(*response);
send_query(td_api::make_object<td_api::sendMessage>(
chat.id_, nullptr, nullptr, nullptr, nullptr,
td_api::make_object<td_api::inputMessageVoiceNote>(
td_api::make_object<td_api::inputFileLocal>(path),
0, "", nullptr, nullptr
)
), [path](Object response) {
if (response->get_id() == td_api::error::ID) {
auto &err = static_cast<td_api::error &>(*response);
std::cerr << "Failed to send voice message: " << err.message_ << std::endl;
} else {
std::cout << "Voice message sent: " << path << std::endl;
}
});
});
}
};
// -- Main --
int main(int argc, char *argv[]) {
// Defaults
int32_t api_id = 0;
std::string api_hash;
std::string prompt_file = "prompt.mp3";
std::string recordings_dir = "recordings";
int echo_delay_ms = 1000;
bool video_enabled = true;
// Check for subcommand
bool auth_mode = argc >= 2 && std::string(argv[1]) == "auth";
if (auth_mode) {
argv[1] = argv[0];
argv++;
argc--;
}
// Parse args
for (int i = 1; i < argc; i++) {
std::string arg = argv[i];
if (arg == "--api-id" && i + 1 < argc) {
api_id = std::stoi(argv[++i]);
} else if (arg == "--api-hash" && i + 1 < argc) {
api_hash = argv[++i];
} else if (arg == "--prompt" && i + 1 < argc) {
prompt_file = argv[++i];
} else if (arg == "--recordings-dir" && i + 1 < argc) {
recordings_dir = argv[++i];
} else if (arg == "--echo-delay" && i + 1 < argc) {
echo_delay_ms = std::stoi(argv[++i]);
} else if (arg == "--video") {
video_enabled = true;
} else if (arg == "--no-video") {
video_enabled = false;
} else if (arg == "--help") {
std::cout << "Usage: " << argv[0] << " [auth] [--api-id ID] [--api-hash HASH] [--prompt FILE] [--recordings-dir DIR] [--echo-delay MS] [--video|--no-video]" << std::endl;
return 0;
}
}
// Env var fallback
if (api_id == 0) {
const char *env = std::getenv("API_ID");
if (env) api_id = std::stoi(env);
}
if (api_hash.empty()) {
const char *env = std::getenv("API_HASH");