-
Notifications
You must be signed in to change notification settings - Fork 0
/
tox_ngc_videoplayer.c
3192 lines (2855 loc) · 121 KB
/
tox_ngc_videoplayer.c
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
/**
*
* tox_ngc_videoplayer
* (C)Zoff <[email protected]> in 2023
*
* https://github.com/zoff99/tox_ngc_videoplayer
*
*
*/
/*
* Copyright © 2023 Zoff <[email protected]>
*
* tox_videoplayer is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* tox_videoplayer is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; If not, see <https://www.gnu.org/licenses/>.
*/
/*
*
ASAN compile:
gcc -O3 -g -fno-omit-frame-pointer -fsanitize=address -static-libasan -fPIC -Wno-discarded-qualifiers tox_ngc_videoplayer.c $(pkg-config --cflags --libs x11 libsodium libswresample opus vpx libavcodec libswscale libavformat libavdevice libavutil x264) -pthread -o tox_ngc_videoplayer
NORMAL compile:
gcc -O3 -flto -fomit-frame-pointer -g -march=native -fPIC -Wno-discarded-qualifiers tox_ngc_videoplayer.c $(pkg-config --cflags --libs x11 libsodium libswresample opus vpx libavcodec libswscale libavformat libavdevice libavutil x264) -pthread -o tox_ngc_videoplayer
MOSTLY STATIC compile:
gcc -O3 -fomit-frame-pointer -Wno-discarded-qualifiers -fPIC tox_ngc_videoplayer.c \
-Wl,-Bstatic \
$(pkg-config --cflags --libs libsodium x264) \
-Wl,-Bdynamic \
$(pkg-config --cflags --libs x11 libswresample opus vpx libavcodec libswscale libavformat libavdevice libavutil ) \
-pthread -o tox_ngc_videoplayer
*
*/
#define _GNU_SOURCE // NOLINT(bugprone-reserved-identifier)
#define _FILE_OFFSET_BITS 64 // NOLINT(bugprone-reserved-identifier)
#define _LARGEFILE_SOURCE // NOLINT(bugprone-reserved-identifier)
#include <ctype.h>
#include <getopt.h>
#include <pthread.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>
#include <libavcodec/avcodec.h>
#include <libavdevice/avdevice.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/imgutils.h>
#include <libavutil/time.h>
#include <libswresample/swresample.h>
#include <libswscale/swscale.h>
#include <X11/Xlib.h>
#include <sodium/utils.h>
// define this before including toxcore amalgamation -------
#define MIN_LOGGER_LEVEL LOGGER_LEVEL_WARNING // LOGGER_LEVEL_WARNING // LOGGER_LEVEL_DEBUG
#define HW_CODEC_CONFIG_UTOX_UB81
// #define HW_CODEC_CONFIG_TBW_LINNVENC
#define HW_CODEC_CONFIG_HIGHVBITRATE
// define this before including toxcore amalgamation -------
// include toxcore amalgamation with ToxAV --------
#include "toxcore_amalgamation.c"
// include toxcore amalgamation with ToxAV --------
// ----------- version -----------
// ----------- version -----------
#define TOX_VPLAYER_VERSION_MAJOR 0
#define TOX_VPLAYER_VERSION_MINOR 99
#define TOX_VPLAYER_VERSION_PATCH 2
static const char global_tox_vplayer_version_string[] = "0.99.2";
// ----------- version -----------
// ----------- version -----------
#define DEFAULT_GLOBAL_AUD_BITRATE 128 // kbit/sec
#define DEFAULT_GLOBAL_VID_BITRATE 8000 // kbit/sec
int GLOBAL_VID_BITRATE_var = DEFAULT_GLOBAL_VID_BITRATE;
const int audio_frame_size_ms = 120;
int vbr = 0;
int max_video_bitrate = 8000;
int max_video_bitrate_set = 0;
#define DEFAULT_SCREEN_CAPTURE_FPS "30" // 30 fps desktop screen capture
#define DEFAULT_SCREEN_CAPTURE_PULSE_DEVICE "default" // default pulse device is called "default"
#define NGC_VIDEO_OUT_WIDTH 640
#define NGC_VIDEO_OUT_HEIGHT 480
#define ___MAX_GC_PACKET_CHUNK_SIZE 1372
static int self_online = 0;
static int friend_online = 0;
static int friend_in_call = 0;
static int switch_tcponly = 0;
#define PROXY_PORT_TOR_DEFAULT 9050
static int use_tor = 0;
static pthread_t ffmpeg_thread_video;
static int ffmpeg_thread_video_stop = 1;
static pthread_t ffmpeg_thread_audio;
static int ffmpeg_thread_audio_stop = 1;
static pthread_t thread_key;
static int thread_key_stop = 1;
static pthread_t thread_time;
static int thread_time_stop = 1;
static const char *savedata_filename = "./savedata.tox";
static const char *savedata_tmp_filename = "./savedata.tox.tmp";
static char *global_decoder_string = "";
static char *global_encoder_string = "";
static ToxAV *toxav = NULL;
static const int global_friend_num = 0; // we always only use the first friend
char *global_pulse_inputdevice_name = NULL;
char *global_desktop_capture_fps = NULL;
char* global_desktop_display_num_str = NULL;
int global_osd_message_toggle = 0;
int global_hdmifreq_toggle = 60;
static bool main_loop_running;
int need_free_global_pulse_inputdevice_name = 0;
int need_free_global_desktop_display_num_str = 0;
int need_free_global_desktop_capture_fps = 0;
AVRational time_base_audio = (AVRational) {0, 0};
AVRational time_base_video = (AVRational) {0, 0};
int64_t audio_start_time = 0;
int global_audio_delay_factor = 0;
int global_video_delay_factor = 0; // default video delay of x * 50 (= YY ms) to audio
pthread_mutex_t time___mutex;
pthread_mutex_t vscale___mutex;
pthread_mutex_t vsend___mutex;
#define VIDEO_SEND_THREAD_COUNT_MAX 10
int vsend_thread_count = 0;
#define PLAY_PAUSED 0
#define PLAY_PLAYING 1
static int global_need_video_seek = 0;
static int global_need_audio_seek = 0;
static int global_play_status = PLAY_PAUSED;
static int64_t global_pts = 0;
int64_t global_time_cur_ms = 0;
int64_t global_time_old_mono_ts = 0;
const int seek_delta_ms = 30 * 1000; // seek X seconds
const int seek_delta_ms_faster = 5 * 60 * 1000; // seek X minutes
struct termios oldt;
struct termios newt;
uint8_t *encoded_vframe = NULL;
uint32_t encoded_frame_size_bytes = 0;
void* tox_av_ngc_vcoders_global = NULL;
void* tox_av_ngc_acoders_global = NULL;
uint64_t global_last_sent_video_frame = 0;
//
#define NGC_VIDEO_QUANTIZER_HIGH 38 // 40 // "high" here means "high quality" and therfore low q value
#define NGC_VIDEO_BITRATE_HIGH 300
//
#define NGC_VIDEO_QUANTIZER_LOW 51
#define NGC_VIDEO_BITRATE_LOW 95
//
int global_ngc_video_bitrate = NGC_VIDEO_BITRATE_LOW;
int global_ngc_video_max_quantizer = NGC_VIDEO_QUANTIZER_LOW;
int global_ngc_video_fps_delta_ms = 50; // 40ms =~ 25fps 100ms =~ 10fps
int sws_scale_algo = SWS_SINC; // SWS_SINC SWS_LANCZOS
int global_ngc_audio_bitrate = 10000; // 10 kbit/s
int global_ngc_audio_sampling_rate = 48000; // 48 kHz
int global_ngc_audio_channel_count = 1; // mono
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
struct Node1 {
char *ip;
char *key;
uint16_t udp_port;
uint16_t tcp_port;
} nodes1[] = {
{ "2604:a880:1:20::32f:1001", "BEF0CFB37AF874BD17B9A8F9FE64C75521DB95A37D33C5BDB00E9CF58659C04F", 33445, 33445 },
{ "46.101.197.175", "CD133B521159541FB1D326DE9850F5E56A6C724B5B8E5EB5CD8D950408E95707", 33445, 3389 },
{ "144.217.167.73","7E5668E0EE09E19F320AD47902419331FFEE147BB3606769CFBE921A2A2FD34C",33445,33445},
{ "tox1.mf-net.eu", "B3E5FA80DC8EBD1149AD2AB35ED8B85BD546DEDE261CA593234C619249419506", 33445, 3389 },
{ "bg.tox.dcntrlzd.network", "20AD2A54D70E827302CDF5F11D7C43FA0EC987042C36628E64B2B721A1426E36", 33445, 33445 },
{"91.219.59.156","8E7D0B859922EF569298B4D261A8CCB5FEA14FB91ED412A7603A585A25698832",33445,33445},
{"85.143.221.42","DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43",33445,33445},
{"tox.initramfs.io","3F0A45A268367C1BEA652F258C85F4A66DA76BCAA667A49E770BCC4917AB6A25",33445,33445},
{"144.217.167.73","7E5668E0EE09E19F320AD47902419331FFEE147BB3606769CFBE921A2A2FD34C",33445,33445},
{"tox.abilinski.com","10C00EB250C3233E343E2AEBA07115A5C28920E9C8D29492F6D00B29049EDC7E",33445,33445},
{"tox.novg.net","D527E5847F8330D628DAB1814F0A422F6DC9D0A300E6C357634EE2DA88C35463",33445,33445},
{"198.199.98.108","BEF0CFB37AF874BD17B9A8F9FE64C75521DB95A37D33C5BDB00E9CF58659C04F",33445,33445},
{"tox.kurnevsky.net","82EF82BA33445A1F91A7DB27189ECFC0C013E06E3DA71F588ED692BED625EC23",33445,33445},
{"81.169.136.229","E0DB78116AC6500398DDBA2AEEF3220BB116384CAB714C5D1FCD61EA2B69D75E",33445,33445},
{"205.185.115.131","3091C6BEB2A993F1C6300C16549FABA67098FF3D62C6D253828B531470B53D68",53,53},
{"bg.tox.dcntrlzd.network","20AD2A54D70E827302CDF5F11D7C43FA0EC987042C36628E64B2B721A1426E36",33445,33445},
{"46.101.197.175","CD133B521159541FB1D326DE9850F5E56A6C724B5B8E5EB5CD8D950408E95707",33445,33445},
{"tox1.mf-net.eu","B3E5FA80DC8EBD1149AD2AB35ED8B85BD546DEDE261CA593234C619249419506",33445,33445},
{"tox2.mf-net.eu","70EA214FDE161E7432530605213F18F7427DC773E276B3E317A07531F548545F",33445,33445},
{"195.201.7.101","B84E865125B4EC4C368CD047C72BCE447644A2DC31EF75BD2CDA345BFD310107",33445,33445},
{"tox4.plastiras.org","836D1DA2BE12FE0E669334E437BE3FB02806F1528C2B2782113E0910C7711409",33445,33445},
{"gt.sot-te.ch","F4F4856F1A311049E0262E9E0A160610284B434F46299988A9CB42BD3D494618",33445,33445},
{"188.225.9.167","1911341A83E02503AB1FD6561BD64AF3A9D6C3F12B5FBB656976B2E678644A67",33445,33445},
{"122.116.39.151","5716530A10D362867C8E87EE1CD5362A233BAFBBA4CF47FA73B7CAD368BD5E6E",33445,33445},
{"195.123.208.139","534A589BA7427C631773D13083570F529238211893640C99D1507300F055FE73",33445,33445},
{"tox3.plastiras.org","4B031C96673B6FF123269FF18F2847E1909A8A04642BBECD0189AC8AEEADAF64",33445,33445},
{"104.225.141.59","933BA20B2E258B4C0D475B6DECE90C7E827FE83EFA9655414E7841251B19A72C",43334,43334},
{"139.162.110.188","F76A11284547163889DDC89A7738CF271797BF5E5E220643E97AD3C7E7903D55",33445,33445},
{"198.98.49.206","28DB44A3CEEE69146469855DFFE5F54DA567F5D65E03EFB1D38BBAEFF2553255",33445,33445},
{"172.105.109.31","D46E97CF995DC1820B92B7D899E152A217D36ABE22730FEA4B6BF1BFC06C617C",33445,33445},
{"ru.tox.dcntrlzd.network","DBB2E896990ECC383DA2E68A01CA148105E34F9B3B9356F2FE2B5096FDB62762",33445,33445},
{"91.146.66.26","B5E7DAC610DBDE55F359C7F8690B294C8E4FCEC4385DE9525DBFA5523EAD9D53",33445,33445},
{"tox01.ky0uraku.xyz","FD04EB03ABC5FC5266A93D37B4D6D6171C9931176DC68736629552D8EF0DE174",33445,33445},
{"tox02.ky0uraku.xyz","D3D6D7C0C7009FC75406B0A49E475996C8C4F8BCE1E6FC5967DE427F8F600527",33445,33445},
{"tox.plastiras.org","8E8B63299B3D520FB377FE5100E65E3322F7AE5B20A0ACED2981769FC5B43725",33445,33445},
{"kusoneko.moe","BE7ED53CD924813507BA711FD40386062E6DC6F790EFA122C78F7CDEEE4B6D1B",33445,33445},
{"tox2.plastiras.org","B6626D386BE7E3ACA107B46F48A5C4D522D29281750D44A0CBA6A2721E79C951",33445,33445},
{"172.104.215.182","DA2BD927E01CD05EBCC2574EBE5BEBB10FF59AE0B2105A7D1E2B40E49BB20239",33445,33445},
{ NULL, NULL, 0, 0 }
};
/**
* @brief Converts a hexadecimal string to binary format
*
* @param hex_string The hexadecimal string to be converted, must be NULL terminated
* @param output Pointer to the binary format output buffer
*/
static void hex_string_to_bin2(const char *hex_string, uint8_t *output)
{
size_t len = strlen(hex_string) / 2;
size_t i = len;
if (!output)
{
return;
}
const char *pos = hex_string;
for (i = 0; i < len; ++i, pos += 2)
{
sscanf(pos, "%2hhx", &output[i]);
}
}
/**
* @brief Converts binary data to uppercase hexadecimal string using libsodium
*
* @param bin Pointer to binary data
* @param bin_size Size of binary data
* @param hex Pointer to hexadecimal string
* @param hex_size Size of hexadecimal string
*/
static void bin2upHex(const uint8_t *bin, uint32_t bin_size, char *hex, uint32_t hex_size)
{
sodium_bin2hex(hex, hex_size, bin, bin_size);
for (size_t i = 0; i < hex_size - 1; i++) {
hex[i] = toupper(hex[i]);
}
}
/**
* @brief This function returns a counter value that increases every millisecond using the CLOCK_MONOTONIC clock.
*
* @return uint64_t The current counter value in milliseconds.
*/
static uint64_t current_time_monotonic_default2(void)
{
struct timespec clock_mono;
clock_gettime(CLOCK_MONOTONIC, &clock_mono);
uint64_t time = 1000ULL * clock_mono.tv_sec + (clock_mono.tv_nsec / 1000000ULL);
return time;
}
/**
* @brief Delays the execution of the current thread for a specified number of milliseconds.
*
* @param ms The number of milliseconds to delay the execution of the current thread.
*/
static void yieldcpu(uint32_t ms)
{
usleep(1000 * ms);
}
/**
* @brief Get the current Unix time.
*
* @return The time as the number of seconds since the Epoch.
*/
static time_t get_unix_time(void)
{
return time(NULL);
}
/**
* @brief Calculates the percentage of the current value relative to the maximum value.
*
* @param current_value The current value.
* @param max_value The maximum value.
* @return The percentage of the current value relative to the maximum value.
*/
static float calculate_percentage(int64_t current_value, int64_t max_value)
{
float percentage = ((float)current_value / (float)max_value) * 100.0;
return percentage;
}
/**
* @brief Convert presentation timestamp (pts) to milliseconds (ms)
*
* @param pts Presentation timestamp
* @param time_base Time base
* @return Converted value in milliseconds
*/
static int64_t pts_to_ms(int64_t pts, AVRational time_base)
{
return av_rescale_q(pts, time_base, AV_TIME_BASE_Q) / 1000;
}
/**
* @brief Changes the terminal settings to disable canonical mode and echo.
*
*/
static void change_term()
{
printf("change_term\n");
/* Get the terminal settings */
tcgetattr(STDIN_FILENO, &oldt);
newt = oldt;
/* Disable canonical mode and echo */
newt.c_lflag &= ~(ICANON | ECHO);
/* Set the new terminal settings */
tcsetattr(STDIN_FILENO, TCSANOW, &newt);
}
/**
* @brief Restores the old terminal settings
*
*/
static void restore_term()
{
printf("restore_term\n");
/* Restore the old terminal settings */
tcsetattr(STDIN_FILENO, TCSANOW, &oldt);
}
// ############## FIFO ##############
// ############## FIFO ##############
typedef struct {
void* data;
size_t size;
size_t head;
size_t tail;
} fifo_buffer_t;
fifo_buffer_t* fifo_buffer_create(size_t size) {
fifo_buffer_t* buffer = calloc(1, sizeof(fifo_buffer_t));
buffer->data = calloc(1, size);
buffer->size = size;
buffer->head = 0;
buffer->tail = 0;
return buffer;
}
void fifo_buffer_destroy(fifo_buffer_t* buffer) {
free(buffer->data);
free(buffer);
}
size_t fifo_buffer_free(fifo_buffer_t* buffer) {
if ((buffer->tail == buffer->head) && (buffer->head == 0)){
return (buffer->size - 1);
} else {
if (buffer->tail > 0)
{
if (buffer->tail < buffer->head)
{
// move data to beginning of the buffer
memmove(buffer->data, buffer->data + buffer->tail, buffer->head - buffer->tail);
}
buffer->head -= buffer->tail;
buffer->tail = (size_t)0;
}
return buffer->size - buffer->head - 1;
}
}
size_t fifo_buffer_data_available(fifo_buffer_t* buffer) {
if ((buffer->tail == buffer->head) && (buffer->head == (size_t)0)) {
return (size_t)0;
} else {
return buffer->head - buffer->tail;
}
}
size_t fifo_buffer_write(fifo_buffer_t* buffer, const void* data, size_t size) {
size_t available = fifo_buffer_free(buffer);
if (size > available) {
size = available;
}
memcpy(buffer->data + buffer->head, data, size);
buffer->head += size;
return size;
}
size_t fifo_buffer_read(fifo_buffer_t* buffer, void* data, size_t size) {
size_t available = fifo_buffer_data_available(buffer);
if (size > available) {
size = available;
}
memcpy(data, buffer->data + buffer->tail, size);
buffer->tail += size;
return size;
}
// ############## FIFO ##############
// ############## FIFO ##############
static void tox_log_cb__custom(Tox *tox, TOX_LOG_LEVEL level, const char *file, uint32_t line, const char *func,
const char *message, void *user_data)
{
printf("C-TOXCORE:1:%d:%s:%d:%s:%s\n", (int)level, file, (int)line, func, message);
}
static void updateToxSavedata(const Tox *tox)
{
size_t size = tox_get_savedata_size(tox);
uint8_t *savedata = calloc(1, size);
tox_get_savedata(tox, savedata);
FILE *f = fopen(savedata_tmp_filename, "wb");
fwrite(savedata, size, 1, f);
fclose(f);
rename(savedata_tmp_filename, savedata_filename);
free(savedata);
}
static void self_connection_change_callback(Tox *tox, TOX_CONNECTION status, void *userdata)
{
switch (status) {
case TOX_CONNECTION_NONE:
printf("Lost connection to the Tox network.\n");
self_online = 0;
break;
case TOX_CONNECTION_TCP:
printf("Connected using TCP.\n");
self_online = 1;
break;
case TOX_CONNECTION_UDP:
printf("Connected using UDP.\n");
self_online = 2;
break;
}
}
static void friendlist_onConnectionChange(Tox *tox, uint32_t friend_number, TOX_CONNECTION status, void *user_data)
{
switch (status) {
case TOX_CONNECTION_NONE:
printf("Lost connection to friend %d.\n", friend_number);
//friend_online = 0;
break;
case TOX_CONNECTION_TCP:
printf("Connected to friend %d using TCP.\n", friend_number);
//friend_online = 1;
break;
case TOX_CONNECTION_UDP:
printf("Connected to friend %d using UDP.\n", friend_number);
//friend_online = 2;
break;
}
}
static void friend_request_cb(Tox *tox, const uint8_t *public_key, const uint8_t *message, size_t length, void *user_data)
{
tox_friend_add_norequest(tox, public_key, NULL);
updateToxSavedata(tox);
}
static void group_invite_cb(Tox *tox, uint32_t friend_number, const uint8_t *invite_data, size_t length,
const uint8_t *group_name, size_t group_name_length, void *userdata)
{
Tox_Err_Group_Invite_Accept error;
tox_group_invite_accept(tox, friend_number, invite_data, length,
(const uint8_t *)"ToxNGCVideoplayer", strlen("ToxNGCVideoplayer"),
NULL, 0,
&error);
printf("tox_group_invite_accept:%d\n", error);
}
static void group_self_join_cb(Tox *tox, uint32_t group_number, void *userdata)
{
printf("You joined group %d\n", group_number);
}
/**
*
* 8x8 monochrome bitmap fonts for rendering
* Author: Daniel Hepper <[email protected]>
*
* https://github.com/dhepper/font8x8
*
* License: Public Domain
*
**/
// Constant: font8x8_basic
// Contains an 8x8 font map for unicode points U+0000 - U+007F (basic latin)
char font8x8_basic[128][8] =
{
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0000 (nul)
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0001
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0002
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0003
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0004
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0005
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0006
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0007
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0008
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0009
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000A
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000B
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000C
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000D
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000E
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+000F
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0010
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0011
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0012
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0013
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0014
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0015
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0016
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0017
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0018
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0019
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001A
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001B
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001C
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001D
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001E
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+001F
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0020 (space)
{ 0x18, 0x3C, 0x3C, 0x18, 0x18, 0x00, 0x18, 0x00}, // U+0021 (!)
{ 0x36, 0x36, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0022 (")
{ 0x36, 0x36, 0x7F, 0x36, 0x7F, 0x36, 0x36, 0x00}, // U+0023 (#)
{ 0x0C, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x0C, 0x00}, // U+0024 ($)
{ 0x00, 0x63, 0x33, 0x18, 0x0C, 0x66, 0x63, 0x00}, // U+0025 (%)
{ 0x1C, 0x36, 0x1C, 0x6E, 0x3B, 0x33, 0x6E, 0x00}, // U+0026 (&)
{ 0x06, 0x06, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0027 (')
{ 0x18, 0x0C, 0x06, 0x06, 0x06, 0x0C, 0x18, 0x00}, // U+0028 (()
{ 0x06, 0x0C, 0x18, 0x18, 0x18, 0x0C, 0x06, 0x00}, // U+0029 ())
{ 0x00, 0x66, 0x3C, 0xFF, 0x3C, 0x66, 0x00, 0x00}, // U+002A (*)
{ 0x00, 0x0C, 0x0C, 0x3F, 0x0C, 0x0C, 0x00, 0x00}, // U+002B (+)
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x06}, // U+002C (,)
{ 0x00, 0x00, 0x00, 0x3F, 0x00, 0x00, 0x00, 0x00}, // U+002D (-)
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x0C, 0x0C, 0x00}, // U+002E (.)
{ 0x60, 0x30, 0x18, 0x0C, 0x06, 0x03, 0x01, 0x00}, // U+002F (/)
{ 0x3E, 0x63, 0x73, 0x7B, 0x6F, 0x67, 0x3E, 0x00}, // U+0030 (0)
{ 0x0C, 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x3F, 0x00}, // U+0031 (1)
{ 0x1E, 0x33, 0x30, 0x1C, 0x06, 0x33, 0x3F, 0x00}, // U+0032 (2)
{ 0x1E, 0x33, 0x30, 0x1C, 0x30, 0x33, 0x1E, 0x00}, // U+0033 (3)
{ 0x38, 0x3C, 0x36, 0x33, 0x7F, 0x30, 0x78, 0x00}, // U+0034 (4)
{ 0x3F, 0x03, 0x1F, 0x30, 0x30, 0x33, 0x1E, 0x00}, // U+0035 (5)
{ 0x1C, 0x06, 0x03, 0x1F, 0x33, 0x33, 0x1E, 0x00}, // U+0036 (6)
{ 0x3F, 0x33, 0x30, 0x18, 0x0C, 0x0C, 0x0C, 0x00}, // U+0037 (7)
{ 0x1E, 0x33, 0x33, 0x1E, 0x33, 0x33, 0x1E, 0x00}, // U+0038 (8)
{ 0x1E, 0x33, 0x33, 0x3E, 0x30, 0x18, 0x0E, 0x00}, // U+0039 (9)
{ 0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x00}, // U+003A (:)
{ 0x00, 0x0C, 0x0C, 0x00, 0x00, 0x0C, 0x0C, 0x06}, // U+003B (//)
{ 0x18, 0x0C, 0x06, 0x03, 0x06, 0x0C, 0x18, 0x00}, // U+003C (<)
{ 0x00, 0x00, 0x3F, 0x00, 0x00, 0x3F, 0x00, 0x00}, // U+003D (=)
{ 0x06, 0x0C, 0x18, 0x30, 0x18, 0x0C, 0x06, 0x00}, // U+003E (>)
{ 0x1E, 0x33, 0x30, 0x18, 0x0C, 0x00, 0x0C, 0x00}, // U+003F (?)
{ 0x3E, 0x63, 0x7B, 0x7B, 0x7B, 0x03, 0x1E, 0x00}, // U+0040 (@)
{ 0x0C, 0x1E, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x00}, // U+0041 (A)
{ 0x3F, 0x66, 0x66, 0x3E, 0x66, 0x66, 0x3F, 0x00}, // U+0042 (B)
{ 0x3C, 0x66, 0x03, 0x03, 0x03, 0x66, 0x3C, 0x00}, // U+0043 (C)
{ 0x1F, 0x36, 0x66, 0x66, 0x66, 0x36, 0x1F, 0x00}, // U+0044 (D)
{ 0x7F, 0x46, 0x16, 0x1E, 0x16, 0x46, 0x7F, 0x00}, // U+0045 (E)
{ 0x7F, 0x46, 0x16, 0x1E, 0x16, 0x06, 0x0F, 0x00}, // U+0046 (F)
{ 0x3C, 0x66, 0x03, 0x03, 0x73, 0x66, 0x7C, 0x00}, // U+0047 (G)
{ 0x33, 0x33, 0x33, 0x3F, 0x33, 0x33, 0x33, 0x00}, // U+0048 (H)
{ 0x1E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0049 (I)
{ 0x78, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E, 0x00}, // U+004A (J)
{ 0x67, 0x66, 0x36, 0x1E, 0x36, 0x66, 0x67, 0x00}, // U+004B (K)
{ 0x0F, 0x06, 0x06, 0x06, 0x46, 0x66, 0x7F, 0x00}, // U+004C (L)
{ 0x63, 0x77, 0x7F, 0x7F, 0x6B, 0x63, 0x63, 0x00}, // U+004D (M)
{ 0x63, 0x67, 0x6F, 0x7B, 0x73, 0x63, 0x63, 0x00}, // U+004E (N)
{ 0x1C, 0x36, 0x63, 0x63, 0x63, 0x36, 0x1C, 0x00}, // U+004F (O)
{ 0x3F, 0x66, 0x66, 0x3E, 0x06, 0x06, 0x0F, 0x00}, // U+0050 (P)
{ 0x1E, 0x33, 0x33, 0x33, 0x3B, 0x1E, 0x38, 0x00}, // U+0051 (Q)
{ 0x3F, 0x66, 0x66, 0x3E, 0x36, 0x66, 0x67, 0x00}, // U+0052 (R)
{ 0x1E, 0x33, 0x07, 0x0E, 0x38, 0x33, 0x1E, 0x00}, // U+0053 (S)
{ 0x3F, 0x2D, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0054 (T)
{ 0x33, 0x33, 0x33, 0x33, 0x33, 0x33, 0x3F, 0x00}, // U+0055 (U)
{ 0x33, 0x33, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00}, // U+0056 (V)
{ 0x63, 0x63, 0x63, 0x6B, 0x7F, 0x77, 0x63, 0x00}, // U+0057 (W)
{ 0x63, 0x63, 0x36, 0x1C, 0x1C, 0x36, 0x63, 0x00}, // U+0058 (X)
{ 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x0C, 0x1E, 0x00}, // U+0059 (Y)
{ 0x7F, 0x63, 0x31, 0x18, 0x4C, 0x66, 0x7F, 0x00}, // U+005A (Z)
{ 0x1E, 0x06, 0x06, 0x06, 0x06, 0x06, 0x1E, 0x00}, // U+005B ([)
{ 0x03, 0x06, 0x0C, 0x18, 0x30, 0x60, 0x40, 0x00}, // U+005C (\)
{ 0x1E, 0x18, 0x18, 0x18, 0x18, 0x18, 0x1E, 0x00}, // U+005D (])
{ 0x08, 0x1C, 0x36, 0x63, 0x00, 0x00, 0x00, 0x00}, // U+005E (^)
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xFF}, // U+005F (_)
{ 0x0C, 0x0C, 0x18, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+0060 (`)
{ 0x00, 0x00, 0x1E, 0x30, 0x3E, 0x33, 0x6E, 0x00}, // U+0061 (a)
{ 0x07, 0x06, 0x06, 0x3E, 0x66, 0x66, 0x3B, 0x00}, // U+0062 (b)
{ 0x00, 0x00, 0x1E, 0x33, 0x03, 0x33, 0x1E, 0x00}, // U+0063 (c)
{ 0x38, 0x30, 0x30, 0x3e, 0x33, 0x33, 0x6E, 0x00}, // U+0064 (d)
{ 0x00, 0x00, 0x1E, 0x33, 0x3f, 0x03, 0x1E, 0x00}, // U+0065 (e)
{ 0x1C, 0x36, 0x06, 0x0f, 0x06, 0x06, 0x0F, 0x00}, // U+0066 (f)
{ 0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x1F}, // U+0067 (g)
{ 0x07, 0x06, 0x36, 0x6E, 0x66, 0x66, 0x67, 0x00}, // U+0068 (h)
{ 0x0C, 0x00, 0x0E, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+0069 (i)
{ 0x30, 0x00, 0x30, 0x30, 0x30, 0x33, 0x33, 0x1E}, // U+006A (j)
{ 0x07, 0x06, 0x66, 0x36, 0x1E, 0x36, 0x67, 0x00}, // U+006B (k)
{ 0x0E, 0x0C, 0x0C, 0x0C, 0x0C, 0x0C, 0x1E, 0x00}, // U+006C (l)
{ 0x00, 0x00, 0x33, 0x7F, 0x7F, 0x6B, 0x63, 0x00}, // U+006D (m)
{ 0x00, 0x00, 0x1F, 0x33, 0x33, 0x33, 0x33, 0x00}, // U+006E (n)
{ 0x00, 0x00, 0x1E, 0x33, 0x33, 0x33, 0x1E, 0x00}, // U+006F (o)
{ 0x00, 0x00, 0x3B, 0x66, 0x66, 0x3E, 0x06, 0x0F}, // U+0070 (p)
{ 0x00, 0x00, 0x6E, 0x33, 0x33, 0x3E, 0x30, 0x78}, // U+0071 (q)
{ 0x00, 0x00, 0x3B, 0x6E, 0x66, 0x06, 0x0F, 0x00}, // U+0072 (r)
{ 0x00, 0x00, 0x3E, 0x03, 0x1E, 0x30, 0x1F, 0x00}, // U+0073 (s)
{ 0x08, 0x0C, 0x3E, 0x0C, 0x0C, 0x2C, 0x18, 0x00}, // U+0074 (t)
{ 0x00, 0x00, 0x33, 0x33, 0x33, 0x33, 0x6E, 0x00}, // U+0075 (u)
{ 0x00, 0x00, 0x33, 0x33, 0x33, 0x1E, 0x0C, 0x00}, // U+0076 (v)
{ 0x00, 0x00, 0x63, 0x6B, 0x7F, 0x7F, 0x36, 0x00}, // U+0077 (w)
{ 0x00, 0x00, 0x63, 0x36, 0x1C, 0x36, 0x63, 0x00}, // U+0078 (x)
{ 0x00, 0x00, 0x33, 0x33, 0x33, 0x3E, 0x30, 0x1F}, // U+0079 (y)
{ 0x00, 0x00, 0x3F, 0x19, 0x0C, 0x26, 0x3F, 0x00}, // U+007A (z)
{ 0x38, 0x0C, 0x0C, 0x07, 0x0C, 0x0C, 0x38, 0x00}, // U+007B ({)
{ 0x18, 0x18, 0x18, 0x00, 0x18, 0x18, 0x18, 0x00}, // U+007C (|)
{ 0x07, 0x0C, 0x0C, 0x38, 0x0C, 0x0C, 0x07, 0x00}, // U+007D (})
{ 0x6E, 0x3B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, // U+007E (~)
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00} // U+007F
};
// "0" -> [48]
// "9" -> [57]
// ":" -> [58]
/**
* @brief Get the bitmap of a character from a font
*
* @param font_char_num The character number to get the bitmap from
* @return char* The bitmap of the character
*/
static char *get_bitmap_from_font(int font_char_num)
{
char *ret_bitmap = font8x8_basic[0x3F]; // fallback: "?"
if ((font_char_num >= 0) && (font_char_num <= 0x7F))
{
ret_bitmap = font8x8_basic[font_char_num];
}
#if 0
else if ((font_char_num >= 0xA0) && (font_char_num <= 0xFF))
{
ret_bitmap = font8x8_ext_latin[font_char_num - 0xA0];
}
#endif
return ret_bitmap;
}
/**
* @brief Prints a character from a font to a given location in a Y-plane buffer of a YUV display
*
* @param start_x_pix Starting x-coordinate of the character on the display
* @param start_y_pix Starting y-coordinate of the character on the display
* @param font_char_num The character number in the font to be printed
* @param col_value The color value to be used for the character
* @param yy Pointer to the y-plane of the display
* @param w Width of the display
*/
void print_font_char_ptr(int start_x_pix, int start_y_pix, int font_char_num,
uint8_t col_value, uint8_t *yy, int w)
{
int font_w = 8;
int font_h = 8;
uint8_t *y_plane = yy;
// uint8_t col_value = 0; // black
char *bitmap = get_bitmap_from_font(font_char_num);
int k;
int j;
int offset = 0;
int set = 0;
for (k = 0; k < font_h; k++)
{
y_plane = yy + ((start_y_pix + k) * w);
y_plane = y_plane + start_x_pix;
for (j = 0; j < font_w; j++)
{
set = bitmap[k] & 1 << j;
if (set)
{
*y_plane = col_value; // set luma value
}
y_plane = y_plane + 1;
}
}
}
#define UTF8_EXTENDED_OFFSET 64
static void text_on_yuf_frame_xy_ptr(int start_x_pix, int start_y_pix, const char *text, uint8_t col_value, uint8_t *yy,
int w, int h)
{
int carriage = 0;
const int letter_width = 8;
const int letter_spacing = 1;
int block_needed_width = 2 + 2 + (strlen(text) * (letter_width + letter_spacing));
int looper;
bool lat_ext = false;
for (looper = 0; (int)looper < (int)strlen(text); looper++)
{
uint8_t c = text[looper];
if (((c > 0) && (c < 127)) || (lat_ext == true))
{
if (lat_ext == true)
{
print_font_char_ptr((2 + start_x_pix + ((letter_width + letter_spacing) * carriage)),
2 + start_y_pix, c + UTF8_EXTENDED_OFFSET, letter_width, yy, w);
}
else
{
print_font_char_ptr((2 + start_x_pix + ((letter_width + letter_spacing) * carriage)),
2 + start_y_pix, c, col_value, yy, w);
}
lat_ext = false;
}
else
{
// leave a blank
if (c == 0xC3)
{
// UTF-8 latin extended
lat_ext = true;
}
else
{
lat_ext = false;
}
carriage--;
}
carriage++;
}
}
static void set_ngc_audio_header(uint8_t *buf)
{
/*
| what | Length in bytes| Contents |
|------ |-------- |------------------ |
| magic | 6 | 0x667788113435 |
| version | 1 | 0x01 |
| pkt id | 1 | 0x31 |
| audio channels| 1 | uint8_t always 1 (for MONO) |
| sampling freq | 1 | uint8_t always 48 (for 48kHz) |
| data |[1, 1363] | *uint8_t bytes, zero not allowed! |
*/
*buf = 0x66;
buf++;
*buf = 0x77;
buf++;
*buf = 0x88;
buf++;
*buf = 0x11;
buf++;
*buf = 0x34;
buf++;
*buf = 0x35;
buf++;
*buf = 0x01;
buf++;
*buf = 0x31;
buf++;
*buf = 1;
buf++;
*buf = 48;
buf++;
}
#define CRC_POLYNOM 0x9c
#define CRC_PRESET 0xFF
static uint8_t crc_8(const uint8_t *buf, const uint32_t num_bytes)
{
uint8_t crc = CRC_PRESET;
for(uint32_t i = 0; i < num_bytes; i++) {
crc ^= *buf;
buf++;
for(uint32_t j = 0; j < 8; j++) {
if(crc & 0x01) {
crc = (crc >> 1) ^ CRC_POLYNOM;
} else {
crc = (crc >> 1);
}
}
}
return crc;
}
static void set_ngc_video_header(uint8_t *buf, uint16_t seqnum, uint8_t chksum)
{
/*
| what | Length in bytes| Contents |
|------ |-------- |------------------ |
| magic | 6 | 0x667788113435 |
| version | 1 | 0x02 |
| pkt id | 1 | 0x21 |
| video width | 1 | uint8_t always 224 (8bit of 480) |
| video height | 1 | uint8_t always 128 (8bit of 640) |
| video codec | 1 | uint8_t always 1 (1 -> H264) |
| seq number | 2 | uint16_t video frame sequence number (rolls over) in little endian |
| chksum | 1 | uint8_t 8 bit CRC checksum over data |
| data |[1, 36986] | *uint8_t bytes, zero not allowed! |
*/
*buf = 0x66;
buf++;
*buf = 0x77;
buf++;
*buf = 0x88;
buf++;
*buf = 0x11;
buf++;
*buf = 0x34;
buf++;
*buf = 0x35;
buf++;
*buf = 0x02;
buf++;
*buf = 0x21;
buf++;
// warning: unsigned conversion from ‘int’ to ‘uint8_t’ {aka ‘unsigned char’} changes value from ‘480’ to ‘224’
*buf = 224; // 480; // LOL
buf++;
// warning: unsigned conversion from ‘int’ to ‘uint8_t’ {aka ‘unsigned char’} changes value from ‘640’ to ‘128’
*buf = 128; // 640; // LOL
buf++;
*buf = 0x01;
buf++;
// seq num
const uint8_t lowerByte = (uint8_t)(seqnum & 0xFF);
const uint8_t upperByte = (seqnum >> 8) & 0xFF;
*buf = lowerByte;
buf++;
*buf = upperByte;
buf++;
*buf = chksum;
buf++;
}
static void rotateYUV420Buffer(uint8_t* in, uint8_t* out, uint32_t in_w, uint32_t in_stride, uint32_t in_h)
{
for (int i = 0; i < in_h; i++) {
for (int j = 0; j < in_w; j++) {
const int d = (j * in_h) + (in_h - i - 1);
const int s = (i * in_stride) + j;
out[d] = in[s];
}
}
}
static uint64_t tta;
static bool ngc_toxav_audio_send_frame_age(ToxAV *av, uint32_t friend_number, const int16_t *pcm, size_t sample_count,
uint8_t channels, uint32_t sampling_rate, TOXAV_ERR_SEND_FRAME *error, int32_t age_ms)
{
Tox *tox_local = toxav_get_tox(av);
if (!tox_local)
{
return true;
}
uint8_t *encoded_frame_bytes = calloc(1, 6000);
if (encoded_frame_bytes) {
uint8_t *encoded_frame_bytes_data = encoded_frame_bytes + 10; // HINT: ngc group audio header is 10 bytes
set_ngc_audio_header(encoded_frame_bytes);
uint32_t encoded_frame_size_bytes = 0;
bool res = toxav_ngc_audio_encode(tox_av_ngc_acoders_global,
pcm, sample_count, encoded_frame_bytes_data, &encoded_frame_size_bytes);
encoded_frame_size_bytes = encoded_frame_size_bytes + 10;
bool lossless = true;
if (encoded_frame_size_bytes <= ___MAX_GC_PACKET_CHUNK_SIZE) {
lossless = false;
}
Tox_Err_Group_Send_Custom_Packet err_send;
bool res_send = tox_group_send_custom_packet(tox_local, 0,
lossless, encoded_frame_bytes,
encoded_frame_size_bytes, &err_send);
const int delta_a = (int)(current_time_monotonic_default2() - tta);
//printf("ngc_toxav_audio_send_frame_age:delta=%d:encoded_frame_size_bytes=%d sample_count=%d L=%d\n",
// delta_a, encoded_frame_size_bytes, (int)sample_count, (int)lossless);
tta = current_time_monotonic_default2();
}
free(encoded_frame_bytes);
return true;
}
static uint64_t ttv;
static uint16_t video_seqnum = 0;
static bool ngc_toxav_video_send_frame_age(ToxAV *av, uint32_t friend_number,
uint16_t output_width, uint16_t output_height,
const uint16_t y_stride, const uint16_t u_stride, const uint16_t v_stride,
const uint8_t *y, const uint8_t *u, const uint8_t *v,
TOXAV_ERR_SEND_FRAME *error, int32_t age_ms)
{
// HINT: send only about ~X fps
if ((global_last_sent_video_frame + global_ngc_video_fps_delta_ms) > current_time_monotonic_default2())
{
return true;
}
global_last_sent_video_frame = current_time_monotonic_default2();
if (!av)
{
return true;
}
int y_size = output_width * output_height;
int u_size = (output_width/2) * (output_height/2);
int v_size = (output_width/2) * (output_height/2);
uint8_t *y_buf_temp2 = malloc(y_size );
uint8_t *u_buf_temp2 = malloc(u_size);
uint8_t *v_buf_temp2 = malloc(v_size);
rotateYUV420Buffer(y, y_buf_temp2,
output_width, y_stride, output_height);
rotateYUV420Buffer(u, u_buf_temp2,
(output_width/2), u_stride, (output_height/2));
rotateYUV420Buffer(v, v_buf_temp2,
(output_width/2), v_stride, (output_height/2));
uint8_t *encoded_vframe_data = encoded_vframe + 14; // HINT: ngc group video header is 14 bytes
bool res_enc = toxav_ngc_video_encode(tox_av_ngc_vcoders_global,
global_ngc_video_bitrate,
global_ngc_video_max_quantizer,
output_height, output_width,
y_buf_temp2, u_buf_temp2, v_buf_temp2,
encoded_vframe_data, &encoded_frame_size_bytes);
uint8_t chskum_crc_8 = crc_8(encoded_vframe_data, encoded_frame_size_bytes);
set_ngc_video_header(encoded_vframe, video_seqnum, chskum_crc_8);
// printf("ngc_toxav_video_send_frame_age:encoded size=%d res=%d\n", encoded_frame_size_bytes, res_enc);
free(y_buf_temp2);
free(u_buf_temp2);
free(v_buf_temp2);
Tox *tox_local = toxav_get_tox(av);
if (!tox_local)
{
return true;
}
bool lossless = true;
if ((encoded_frame_size_bytes + 14) <= ___MAX_GC_PACKET_CHUNK_SIZE) {
lossless = false;
}
Tox_Err_Group_Send_Custom_Packet err_send;
bool res_send = tox_group_send_custom_packet(tox_local, 0, lossless, encoded_vframe, encoded_frame_size_bytes + 14, &err_send);
if (err_send != TOX_ERR_GROUP_SEND_CUSTOM_PACKET_OK)
{
int tries;
for(tries=0;tries<5;tries++) {
res_send = tox_group_send_custom_packet(tox_local, 0, lossless, encoded_vframe, encoded_frame_size_bytes + 14, &err_send);
if (err_send == TOX_ERR_GROUP_SEND_CUSTOM_PACKET_OK) {
break;
}
}