/
srs_app_threads.cpp
1313 lines (1049 loc) · 35 KB
/
srs_app_threads.cpp
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
/**
* The MIT License (MIT)
*
* Copyright (c) 2013-2020 Winlin
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <srs_app_threads.hpp>
#include <srs_kernel_error.hpp>
#include <srs_app_config.hpp>
#include <srs_app_log.hpp>
#include <srs_core_autofree.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_app_utility.hpp>
#include <unistd.h>
#ifdef SRS_OSX
pid_t gettid() {
return 0;
}
#else
#if __GLIBC__ == 2 && __GLIBC_MINOR__ < 30
#include <sys/syscall.h>
#define gettid() syscall(SYS_gettid)
#endif
#endif
using namespace std;
#include <srs_protocol_kbps.hpp>
extern SrsPps* _srs_pps_rloss;
extern SrsPps* _srs_pps_aloss;
extern SrsPps* _srs_pps_snack2;
extern SrsPps* _srs_pps_snack3;
extern SrsPps* _srs_pps_snack4;
SrsPps* _srs_thread_sync_10us = new SrsPps();
SrsPps* _srs_thread_sync_100us = new SrsPps();
SrsPps* _srs_thread_sync_1000us = new SrsPps();
SrsPps* _srs_thread_sync_plus = new SrsPps();
uint64_t srs_covert_cpuset(cpu_set_t v)
{
#ifdef SRS_OSX
return v;
#else
uint64_t iv = 0;
for (int i = 0; i <= 63; i++) {
if (CPU_ISSET(i, &v)) {
iv |= uint64_t(1) << i;
}
}
return iv;
#endif
}
SrsThreadMutex::SrsThreadMutex()
{
// https://man7.org/linux/man-pages/man3/pthread_mutexattr_init.3.html
int r0 = pthread_mutexattr_init(&attr_);
srs_assert(!r0);
// https://man7.org/linux/man-pages/man3/pthread_mutexattr_gettype.3p.html
r0 = pthread_mutexattr_settype(&attr_, PTHREAD_MUTEX_ERRORCHECK);
srs_assert(!r0);
// https://michaelkerrisk.com/linux/man-pages/man3/pthread_mutex_init.3p.html
r0 = pthread_mutex_init(&lock_, &attr_);
srs_assert(!r0);
}
SrsThreadMutex::~SrsThreadMutex()
{
int r0 = pthread_mutex_destroy(&lock_);
srs_assert(!r0);
r0 = pthread_mutexattr_destroy(&attr_);
srs_assert(!r0);
}
void SrsThreadMutex::lock()
{
// https://man7.org/linux/man-pages/man3/pthread_mutex_lock.3p.html
// EDEADLK
// The mutex type is PTHREAD_MUTEX_ERRORCHECK and the current
// thread already owns the mutex.
int r0 = pthread_mutex_lock(&lock_);
srs_assert(!r0);
}
void SrsThreadMutex::unlock()
{
int r0 = pthread_mutex_unlock(&lock_);
srs_assert(!r0);
}
SrsThreadEntry::SrsThreadEntry()
{
pool = NULL;
start = NULL;
arg = NULL;
num = 0;
tid = 0;
err = srs_success;
// Set affinity mask to include CPUs 0 to 7
CPU_ZERO(&cpuset);
CPU_ZERO(&cpuset2);
cpuset_ok = false;
stat = new SrsProcSelfStat();
}
SrsThreadEntry::~SrsThreadEntry()
{
srs_freep(stat);
srs_freep(err);
// TODO: FIXME: Should dispose trd.
}
SrsThreadPool::SrsThreadPool()
{
entry_ = NULL;
lock_ = new SrsThreadMutex();
hybrid_ = NULL;
hybrid_high_water_level_ = 0;
hybrid_critical_water_level_ = 0;
trd_ = new SrsFastCoroutine("pool", this);
high_threshold_ = 0;
high_pulse_ = 0;
critical_threshold_ = 0;
critical_pulse_ = 0;
// Add primordial thread, current thread itself.
SrsThreadEntry* entry = new SrsThreadEntry();
threads_.push_back(entry);
entry_ = entry;
entry->pool = this;
entry->label = "primordial";
entry->start = NULL;
entry->arg = NULL;
entry->num = 1;
entry->trd = pthread_self();
entry->tid = gettid();
char buf[256];
snprintf(buf, sizeof(buf), "srs-master-%d", entry->num);
entry->name = buf;
}
// TODO: FIMXE: If free the pool, we should stop all threads.
SrsThreadPool::~SrsThreadPool()
{
srs_freep(trd_);
srs_freep(lock_);
}
bool SrsThreadPool::hybrid_high_water_level()
{
return hybrid_critical_water_level_ || hybrid_high_water_level_;
}
bool SrsThreadPool::hybrid_critical_water_level()
{
return hybrid_critical_water_level_;
}
// Thread local objects.
extern const int LOG_MAX_SIZE;
extern __thread char* _srs_log_data;
// Setup the thread-local variables, MUST call when each thread starting.
void SrsThreadPool::setup()
{
// Initialize the log shared buffer for threads.
srs_assert(!_srs_log_data);
_srs_log_data = new char[LOG_MAX_SIZE];
}
srs_error_t SrsThreadPool::initialize()
{
srs_error_t err = srs_success;
// TODO: FIXME: Should init ST for each thread.
if ((err = srs_st_init()) != srs_success) {
return srs_error_wrap(err, "initialize st failed");
}
SrsThreadEntry* entry = (SrsThreadEntry*)entry_;
#ifndef SRS_OSX
// Load CPU affinity from config.
int cpu_start = 0, cpu_end = 0;
entry->cpuset_ok = _srs_config->get_threads_cpu_affinity("master", &cpu_start, &cpu_end);
for (int i = cpu_start; entry->cpuset_ok && i <= cpu_end; i++) {
CPU_SET(i, &entry->cpuset);
}
#endif
int r0 = 0, r1 = 0;
#ifndef SRS_OSX
if (entry->cpuset_ok) {
r0 = pthread_setaffinity_np(pthread_self(), sizeof(entry->cpuset), &entry->cpuset);
}
r1 = pthread_getaffinity_np(pthread_self(), sizeof(entry->cpuset2), &entry->cpuset2);
#endif
interval_ = _srs_config->get_threads_interval();
high_threshold_ = _srs_config->get_high_threshold();
high_pulse_ = _srs_config->get_high_pulse();
critical_threshold_ = _srs_config->get_critical_threshold();
critical_pulse_ = _srs_config->get_critical_pulse();
bool async_srtp = _srs_config->get_threads_async_srtp();
int recv_queue = _srs_config->get_threads_max_recv_queue();
_srs_async_recv->set_max_recv_queue(recv_queue);
bool async_send = _srs_config->get_threads_async_send();
_srs_async_send->set_enabled(async_send);
srs_trace("Thread #%d(%s): init name=%s, interval=%dms, async_srtp=%d, cpuset=%d/%d-0x%" PRIx64 "/%d-0x%" PRIx64 ", water_level=%dx%d,%dx%d, recvQ=%d, aSend=%d",
entry->num, entry->label.c_str(), entry->name.c_str(), srsu2msi(interval_), async_srtp,
entry->cpuset_ok, r0, srs_covert_cpuset(entry->cpuset), r1, srs_covert_cpuset(entry->cpuset2),
high_pulse_, high_threshold_, critical_pulse_, critical_threshold_, recv_queue, async_send);
return err;
}
srs_error_t SrsThreadPool::execute(string label, srs_error_t (*start)(void* arg), void* arg)
{
srs_error_t err = srs_success;
SrsThreadEntry* entry = new SrsThreadEntry();
// Update the hybrid thread entry for circuit breaker.
if (label == "hybrid") {
hybrid_ = entry;
}
// To protect the threads_ for executing thread-safe.
if (true) {
SrsThreadLocker(lock_);
threads_.push_back(entry);
}
entry->pool = this;
entry->label = label;
entry->start = start;
entry->arg = arg;
// The id of thread, should equal to the debugger thread id.
// For gdb, it's: info threads
// For lldb, it's: thread list
static int num = entry_->num + 1;
entry->num = num++;
char buf[256];
snprintf(buf, sizeof(buf), "srs-%s-%d", entry->label.c_str(), entry->num);
entry->name = buf;
#ifndef SRS_OSX
// Load CPU affinity from config.
int cpu_start = 0, cpu_end = 0;
entry->cpuset_ok = _srs_config->get_threads_cpu_affinity(label, &cpu_start, &cpu_end);
for (int i = cpu_start; entry->cpuset_ok && i <= cpu_end; i++) {
CPU_SET(i, &entry->cpuset);
}
#endif
// https://man7.org/linux/man-pages/man3/pthread_create.3.html
pthread_t trd;
int r0 = pthread_create(&trd, NULL, SrsThreadPool::start, entry);
if (r0 != 0) {
entry->err = srs_error_new(ERROR_THREAD_CREATE, "create thread %s, r0=%d", label.c_str(), r0);
return srs_error_copy(entry->err);
}
entry->trd = trd;
return err;
}
srs_error_t SrsThreadPool::run()
{
srs_error_t err = srs_success;
while (true) {
vector<SrsThreadEntry*> threads;
if (true) {
SrsThreadLocker(lock_);
threads = threads_;
}
// Check the threads status fastly.
int loops = (int)(interval_ / SRS_UTIME_SECONDS);
for (int i = 0; i < loops; i++) {
for (int i = 0; i < (int)threads.size(); i++) {
SrsThreadEntry* entry = threads.at(i);
if (entry->err != srs_success) {
err = srs_error_wrap(entry->err, "thread #%d(%s)", entry->num, entry->label.c_str());
return srs_error_copy(err);
}
}
// For Circuit-Breaker to update the SNMP, ASAP.
srs_update_udp_snmp_statistic();
_srs_pps_aloss->update();
// Update thread CPUs per 1s.
for (int i = 0; i < (int)threads.size(); i++) {
SrsThreadEntry* entry = threads.at(i);
if (!entry->tid) {
continue;
}
srs_update_thread_proc_stat(entry->stat, entry->tid);
}
// Update the Circuit-Breaker by water-level.
if (hybrid_ && hybrid_->stat) {
// Reset the high water-level when CPU is low for N times.
if (hybrid_->stat->percent * 100 > high_threshold_) {
hybrid_high_water_level_ = high_pulse_;
} else if (hybrid_high_water_level_ > 0) {
hybrid_high_water_level_--;
}
// Reset the critical water-level when CPU is low for N times.
if (hybrid_->stat->percent * 100 > critical_threshold_) {
hybrid_critical_water_level_ = critical_pulse_;
} else if (hybrid_critical_water_level_ > 0) {
hybrid_critical_water_level_--;
}
}
sleep(1);
}
// In normal state, gather status and log it.
static char buf[128];
string async_logs = _srs_async_log->description();
string queue_desc;
if (true) {
snprintf(buf, sizeof(buf), ", queue=%d,%d,%d", _srs_async_recv->size(), _srs_async_srtp->size(), _srs_async_srtp->cooked_size());
queue_desc = buf;
}
string sync_desc;
_srs_thread_sync_10us->update(); _srs_thread_sync_100us->update();
_srs_thread_sync_1000us->update(); _srs_thread_sync_plus->update();
if (_srs_thread_sync_10us->r10s() || _srs_thread_sync_100us->r10s() || _srs_thread_sync_1000us->r10s() || _srs_thread_sync_plus->r10s()) {
snprintf(buf, sizeof(buf), ", sync=%d,%d,%d,%d", _srs_thread_sync_10us->r10s(), _srs_thread_sync_100us->r10s(), _srs_thread_sync_1000us->r10s(), _srs_thread_sync_plus->r10s());
sync_desc = buf;
}
// Show statistics for RTC server.
SrsProcSelfStat* u = srs_get_self_proc_stat();
// Resident Set Size: number of pages the process has in real memory.
int memory = (int)(u->rss * 4 / 1024);
// The hybrid thread cpu and memory.
float thread_percent = 0.0f, top_percent = 0.0f;
if (hybrid_ && hybrid_->stat) {
thread_percent = hybrid_->stat->percent * 100;
}
for (int i = 0; i < (int)threads.size(); i++) {
SrsThreadEntry* entry = threads.at(i);
if (!entry->stat || entry->stat->percent <= 0) {
continue;
}
top_percent = srs_max(top_percent, entry->stat->percent * 100);
}
string circuit_breaker;
if (hybrid_high_water_level() || hybrid_critical_water_level() || _srs_pps_aloss->r1s() || _srs_pps_rloss->r1s() || _srs_pps_snack2->r10s()) {
snprintf(buf, sizeof(buf), ", break=%d,%d, cond=%d,%d,%.2f%%, snk=%d,%d,%d",
hybrid_high_water_level(), hybrid_critical_water_level(), // Whether Circuit-Break is enable.
_srs_pps_rloss->r1s(), _srs_pps_aloss->r1s(), thread_percent, // The conditions to enable Circuit-Breaker.
_srs_pps_snack2->r10s(), _srs_pps_snack3->r10s(), // NACK packet,seqs sent.
_srs_pps_snack4->r10s() // NACK drop by Circuit-Break.
);
circuit_breaker = buf;
}
srs_trace("Process: cpu=%.2f%%,%dMB, threads=%d,%.2f%%,%.2f%%%s%s%s%s",
u->percent * 100, memory, (int)threads_.size(), top_percent, thread_percent,
async_logs.c_str(), sync_desc.c_str(), queue_desc.c_str(), circuit_breaker.c_str());
}
return err;
}
void SrsThreadPool::stop()
{
// TODO: FIXME: Should notify other threads to do cleanup and quit.
}
void* SrsThreadPool::start(void* arg)
{
// Initialize thread-local variables.
SrsThreadPool::setup();
srs_error_t err = srs_success;
SrsThreadEntry* entry = (SrsThreadEntry*)arg;
// Set the thread local fields.
entry->tid = gettid();
int r0 = 0, r1 = 0;
#ifndef SRS_OSX
// https://man7.org/linux/man-pages/man3/pthread_setname_np.3.html
pthread_setname_np(pthread_self(), entry->name.c_str());
if (entry->cpuset_ok) {
r0 = pthread_setaffinity_np(pthread_self(), sizeof(entry->cpuset), &entry->cpuset);
}
r1 = pthread_getaffinity_np(pthread_self(), sizeof(entry->cpuset2), &entry->cpuset2);
#else
pthread_setname_np(entry->name.c_str());
#endif
srs_trace("Thread #%d: run with tid=%d, entry=%p, label=%s, name=%s, cpuset=%d/%d-0x%" PRIx64 "/%d-0x%" PRIx64,
entry->num, (int)entry->tid, entry, entry->label.c_str(), entry->name.c_str(), entry->cpuset_ok,
r0, srs_covert_cpuset(entry->cpuset), r1, srs_covert_cpuset(entry->cpuset2));
if ((err = entry->start(entry->arg)) != srs_success) {
entry->err = err;
}
// We do not use the return value, the err has been set to entry->err.
return NULL;
}
srs_error_t SrsThreadPool::consume()
{
srs_error_t err = srs_success;
if ((err = trd_->start()) != srs_success) {
return srs_error_wrap(err, "start");
}
return err;
}
srs_error_t SrsThreadPool::cycle()
{
srs_error_t err = srs_success;
while (true) {
int consumed = 0;
// Check error before consume packets.
if ((err = trd_->pull()) != srs_success) {
return srs_error_wrap(err, "pull");
}
if ((err = _srs_async_recv->consume(&consumed)) != srs_success) {
srs_error_reset(err); // Ignore any error.
}
// Check error before consume packets.
if ((err = trd_->pull()) != srs_success) {
return srs_error_wrap(err, "pull");
}
if ((err = _srs_async_srtp->consume(&consumed)) != srs_success) {
srs_error_reset(err); // Ignore any error.
}
if (!consumed) {
srs_usleep(20 * SRS_UTIME_MILLISECONDS);
continue;
}
}
return err;
}
// TODO: FIXME: It should be thread-local or thread-safe.
SrsThreadPool* _srs_thread_pool = new SrsThreadPool();
SrsAsyncFileWriter::SrsAsyncFileWriter(std::string p)
{
filename_ = p;
writer_ = new SrsFileWriter();
chunks_ = new SrsThreadQueue<SrsSharedPtrMessage>();
}
// TODO: FIXME: Before free the writer, we must remove it from the manager.
SrsAsyncFileWriter::~SrsAsyncFileWriter()
{
// TODO: FIXME: Should we flush dirty logs?
srs_freep(writer_);
srs_freep(chunks_);
}
srs_error_t SrsAsyncFileWriter::open()
{
return writer_->open(filename_);
}
srs_error_t SrsAsyncFileWriter::open_append()
{
return writer_->open_append(filename_);
}
void SrsAsyncFileWriter::close()
{
writer_->close();
}
srs_error_t SrsAsyncFileWriter::write(void* buf, size_t count, ssize_t* pnwrite)
{
srs_error_t err = srs_success;
if (count <= 0) {
return err;
}
char* cp = new char[count];
memcpy(cp, buf, count);
SrsSharedPtrMessage* msg = new SrsSharedPtrMessage();
msg->wrap(cp, count);
chunks_->push_back(msg);
if (pnwrite) {
*pnwrite = count;
}
return err;
}
srs_error_t SrsAsyncFileWriter::writev(const iovec* iov, int iovcnt, ssize_t* pnwrite)
{
srs_error_t err = srs_success;
for (int i = 0; i < iovcnt; i++) {
const iovec* p = iov + i;
ssize_t nn = 0;
if ((err = write(p->iov_base, p->iov_len, &nn)) != srs_success) {
return srs_error_wrap(err, "write %d iov %d bytes", i, p->iov_len);
}
if (pnwrite) {
*pnwrite += nn;
}
}
return err;
}
srs_error_t SrsAsyncFileWriter::flush()
{
srs_error_t err = srs_success;
// The time to wait here, is the time to wait there, because they wait for the same lock
// at queue to push_back or swap all messages.
srs_utime_t now = srs_update_system_time();
vector<SrsSharedPtrMessage*> flying_chunks;
if (true) {
chunks_->swap(flying_chunks);
}
// Stat the sync wait of locks.
srs_utime_t elapsed = srs_update_system_time() - now;
if (elapsed <= 10) {
++_srs_thread_sync_10us->sugar;
} else if (elapsed <= 100) {
++_srs_thread_sync_100us->sugar;
} else if (elapsed <= 1000) {
++_srs_thread_sync_1000us->sugar;
} else {
++_srs_thread_sync_plus->sugar;
}
// Flush the chunks to disk.
for (int i = 0; i < (int)flying_chunks.size(); i++) {
SrsSharedPtrMessage* msg = flying_chunks.at(i);
srs_error_t r0 = writer_->write(msg->payload, msg->size, NULL);
// Choose a random error to return.
if (err == srs_success) {
err = r0;
} else {
srs_freep(r0);
}
srs_freep(msg);
}
return err;
}
SrsAsyncLogManager::SrsAsyncLogManager()
{
interval_ = 0;
reopen_ = false;
lock_ = new SrsThreadMutex();
}
// TODO: FIXME: We should stop the thread first, then free the manager.
SrsAsyncLogManager::~SrsAsyncLogManager()
{
srs_freep(lock_);
for (int i = 0; i < (int)writers_.size(); i++) {
SrsAsyncFileWriter* writer = writers_.at(i);
srs_freep(writer);
}
}
// @remark Note that we should never write logs, because log is not ready not.
srs_error_t SrsAsyncLogManager::initialize()
{
srs_error_t err = srs_success;
interval_ = _srs_config->srs_log_flush_interval();
if (interval_ <= 0) {
return srs_error_new(ERROR_SYSTEM_LOGFILE, "invalid interval=%dms", srsu2msi(interval_));
}
return err;
}
// @remark Now, log is ready, and we can print logs.
srs_error_t SrsAsyncLogManager::start(void* arg)
{
SrsAsyncLogManager* log = (SrsAsyncLogManager*)arg;
return log->do_start();
}
srs_error_t SrsAsyncLogManager::create_writer(std::string filename, SrsAsyncFileWriter** ppwriter)
{
srs_error_t err = srs_success;
SrsAsyncFileWriter* writer = new SrsAsyncFileWriter(filename);
if (true) {
SrsThreadLocker(lock_);
writers_.push_back(writer);
}
if ((err = writer->open()) != srs_success) {
return srs_error_wrap(err, "open file %s fail", filename.c_str());
}
*ppwriter = writer;
return err;
}
void SrsAsyncLogManager::reopen()
{
SrsThreadLocker(lock_);
reopen_ = true;
}
std::string SrsAsyncLogManager::description()
{
SrsThreadLocker(lock_);
int nn_logs = 0;
int max_logs = 0;
for (int i = 0; i < (int)writers_.size(); i++) {
SrsAsyncFileWriter* writer = writers_.at(i);
int nn = (int)writer->chunks_->size();
nn_logs += nn;
max_logs = srs_max(max_logs, nn);
}
static char buf[128];
snprintf(buf, sizeof(buf), ", logs=%d/%d/%d", (int)writers_.size(), nn_logs, max_logs);
return buf;
}
srs_error_t SrsAsyncLogManager::do_start()
{
srs_error_t err = srs_success;
// Never quit for this thread.
while (true) {
// Reopen all log files.
if (reopen_) {
SrsThreadLocker(lock_);
reopen_ = false;
for (int i = 0; i < (int)writers_.size(); i++) {
SrsAsyncFileWriter* writer = writers_.at(i);
writer->close();
if ((err = writer->open()) != srs_success) {
srs_error_reset(err); // Ignore any error for reopen logs.
}
}
}
// Flush all logs from cache to disk.
if (true) {
SrsThreadLocker(lock_);
for (int i = 0; i < (int)writers_.size(); i++) {
SrsAsyncFileWriter* writer = writers_.at(i);
if ((err = writer->flush()) != srs_success) {
srs_error_reset(err); // Ignore any error for flushing logs.
}
}
}
// We use the system primordial sleep, not the ST sleep, because
// this is a system thread, not a coroutine.
timespec tv = {0};
tv.tv_sec = interval_ / SRS_UTIME_SECONDS;
tv.tv_nsec = (interval_ % SRS_UTIME_SECONDS) * 1000;
nanosleep(&tv, NULL);
}
return err;
}
// TODO: FIXME: It should be thread-local or thread-safe.
SrsAsyncLogManager* _srs_async_log = new SrsAsyncLogManager();
SrsAsyncSRTP::SrsAsyncSRTP(SrsSecurityTransport* transport)
{
task_ = NULL;
transport_ = transport;
}
SrsAsyncSRTP::~SrsAsyncSRTP()
{
_srs_async_srtp->on_srtp_codec_destroy(task_);
}
srs_error_t SrsAsyncSRTP::initialize(std::string recv_key, std::string send_key)
{
srs_error_t err = srs_success;
srs_assert(!task_);
task_ = new SrsAsyncSRTPTask(this);
_srs_async_srtp->register_task(task_);
if ((err = task_->initialize(recv_key, send_key)) != srs_success) {
return srs_error_wrap(err, "init async srtp");
}
// TODO: FIMXE: Remove it.
return SrsSRTP::initialize(recv_key, send_key);
}
srs_error_t SrsAsyncSRTP::protect_rtp(void* packet, int* nb_cipher)
{
if (!task_) {
return srs_error_new(ERROR_RTC_SRTP_UNPROTECT, "not ready");
}
// TODO: FIMXE: Remove it.
return SrsSRTP::protect_rtp(packet, nb_cipher);
}
srs_error_t SrsAsyncSRTP::protect_rtcp(void* packet, int* nb_cipher)
{
if (!task_) {
return srs_error_new(ERROR_RTC_SRTP_UNPROTECT, "not ready");
}
// TODO: FIMXE: Remove it.
return SrsSRTP::protect_rtcp(packet, nb_cipher);
}
srs_error_t SrsAsyncSRTP::unprotect_rtp(void* packet, int* nb_plaintext)
{
if (!task_) {
return srs_error_new(ERROR_RTC_SRTP_UNPROTECT, "not ready");
}
int nb_cipher = *nb_plaintext;
char* buf = new char[nb_cipher];
memcpy(buf, packet, nb_cipher);
SrsAsyncSRTPPacket* pkt = new SrsAsyncSRTPPacket(task_);
pkt->msg_->wrap(buf, nb_cipher);
pkt->is_rtp_ = true;
pkt->do_decrypt_ = true;
_srs_async_srtp->add_packet(pkt);
// Do the job asynchronously.
if (nb_plaintext) {
*nb_plaintext = 0;
}
return srs_success;
}
srs_error_t SrsAsyncSRTP::unprotect_rtcp(void* packet, int* nb_plaintext)
{
if (!task_) {
return srs_error_new(ERROR_RTC_SRTP_UNPROTECT, "not ready");
}
int nb_cipher = *nb_plaintext;
char* buf = new char[nb_cipher];
memcpy(buf, packet, nb_cipher);
SrsAsyncSRTPPacket* pkt = new SrsAsyncSRTPPacket(task_);
pkt->msg_->wrap(buf, nb_cipher);
pkt->is_rtp_ = false;
pkt->do_decrypt_ = true;
_srs_async_srtp->add_packet(pkt);
// Do the job asynchronously.
if (nb_plaintext) {
*nb_plaintext = 0;
}
return srs_success;
}
SrsAsyncSRTPTask::SrsAsyncSRTPTask(SrsAsyncSRTP* codec)
{
codec_ = codec;
impl_ = new SrsSRTP();
disposing_ = false;
}
SrsAsyncSRTPTask::~SrsAsyncSRTPTask()
{
srs_freep(impl_);
}
srs_error_t SrsAsyncSRTPTask::initialize(std::string recv_key, std::string send_key)
{
srs_error_t err = srs_success;
if ((err = impl_->initialize(recv_key, send_key)) != srs_success) {
return srs_error_wrap(err, "init srtp impl");
}
return err;
}
void SrsAsyncSRTPTask::dispose()
{
// TODO: FIXME: Do cleanup in future.
// TODO: FIXME: Memory leak here, use lazy free to avoid lock for each packet.
disposing_ = true;
// It's safe to set the codec to NULl, because it has been freed.
codec_ = NULL;
}
srs_error_t SrsAsyncSRTPTask::cook(SrsAsyncSRTPPacket* pkt)
{
srs_error_t err = srs_success;
// It's safe, because here we do not use the codec.
if (disposing_) {
return err;
}
pkt->nb_consumed_ = pkt->msg_->size;
if (pkt->do_decrypt_) {
if (pkt->is_rtp_) {
err = impl_->unprotect_rtp(pkt->msg_->payload, &pkt->nb_consumed_);
} else {
err = impl_->unprotect_rtcp(pkt->msg_->payload, &pkt->nb_consumed_);
}
}
if (err != srs_success) {
return err;
}
return err;
}
srs_error_t SrsAsyncSRTPTask::consume(SrsAsyncSRTPPacket* pkt)
{
srs_error_t err = srs_success;
// It's safe, because the dispose and consume are in the same thread hybrid.
if (disposing_) {
return err;
}
char* payload = pkt->msg_->payload;
if (pkt->do_decrypt_) {
if (pkt->is_rtp_) {
err = codec_->transport_->on_rtp_plaintext(payload, pkt->nb_consumed_);
} else {
err = codec_->transport_->on_rtcp_plaintext(payload, pkt->nb_consumed_);
}
}
return err;
}
SrsAsyncSRTPPacket::SrsAsyncSRTPPacket(SrsAsyncSRTPTask* task)
{
srs_assert(task);
task_ = task;
msg_ = new SrsSharedPtrMessage();
is_rtp_ = false;
do_decrypt_ = false;
nb_consumed_ = 0;
}
SrsAsyncSRTPPacket::~SrsAsyncSRTPPacket()
{
srs_freep(msg_);
}
SrsAsyncSRTPManager::SrsAsyncSRTPManager()
{
lock_ = new SrsThreadMutex();
srtp_packets_ = new SrsThreadQueue<SrsAsyncSRTPPacket>();
cooked_packets_ = new SrsThreadQueue<SrsAsyncSRTPPacket>();
}
// TODO: FIXME: We should stop the thread first, then free the manager.
SrsAsyncSRTPManager::~SrsAsyncSRTPManager()
{
srs_freep(lock_);
srs_freep(srtp_packets_);
srs_freep(cooked_packets_);
vector<SrsAsyncSRTPTask*>::iterator it;
for (it = tasks_.begin(); it != tasks_.end(); ++it) {
SrsAsyncSRTPTask* task = *it;
srs_freep(task);
}
}
void SrsAsyncSRTPManager::register_task(SrsAsyncSRTPTask* task)
{
if (!task) {
return;
}
SrsThreadLocker(lock_);
tasks_.push_back(task);
}
void SrsAsyncSRTPManager::on_srtp_codec_destroy(SrsAsyncSRTPTask* task)
{
if (!task) {
return;
}
SrsThreadLocker(lock_);
vector<SrsAsyncSRTPTask*>::iterator it;
if ((it = std::find(tasks_.begin(), tasks_.end(), task)) != tasks_.end()) {
tasks_.erase(it);
// TODO: FIXME: Do cleanup in future.
task->dispose();
}
}
// TODO: FIXME: We could use a coroutine queue, then cook all packet in RTC server timer.
void SrsAsyncSRTPManager::add_packet(SrsAsyncSRTPPacket* pkt)
{
srtp_packets_->push_back(pkt);
}
int SrsAsyncSRTPManager::size()
{
return srtp_packets_->size();