SRS 6.0-b0 源码 + danmaku 后端(排除 objs/3rdparty/测试媒体)

This commit is contained in:
RRRosmontis
2026-08-27 21:46:20 +08:00
commit 41b5da7c7e
578 changed files with 237858 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
udp-server
udp-client
cost
cost.log
thread-join
+161
View File
@@ -0,0 +1,161 @@
/*
g++ -g -O0 cost.cpp ../../objs/st/libst.a -I../../objs/st -o cost && ./cost | grep COST
*/
#include <stdio.h>
#include <sys/time.h>
#include <fcntl.h>
#include <unistd.h>
#include <st.h>
#define SRS_UTIME_MILLISECONDS 1000
#define srsu2i(us) ((int)(us))
#define srsu2ms(us) ((us) / SRS_UTIME_MILLISECONDS)
#define srsu2msi(us) int((us) / SRS_UTIME_MILLISECONDS)
int64_t srs_update_system_time()
{
timeval now;
::gettimeofday(&now, NULL);
return ((int64_t)now.tv_sec) * 1000 * 1000 + (int64_t)now.tv_usec;
}
int main(int argc, char** argv)
{
// The cost for srs_update_system_time() itself.
if (true) {
int64_t start = srs_update_system_time();
int64_t ts_gettimeofday = srs_update_system_time();
printf("[COST] gettimeofday=%dus\n", srsu2i(ts_gettimeofday - start));
}
// The cost for allocate 1MB memory.
if (true) {
int64_t start = srs_update_system_time();
int size = 1024 * 1024;
char* p = new char[size];
int64_t ts_allocate = srs_update_system_time();
for (int i = 0; i < size; i++) {
p[i] = 0x0F;
}
int64_t ts_init = srs_update_system_time();
printf("[COST] new[%d]=%dus, init=%dus\n",
size,
srsu2i(ts_allocate - start),
srsu2i(ts_init - ts_allocate)
);
}
// The cost for loop.
if (true) {
int64_t start = srs_update_system_time();
for (long long i = 0; i < 1000000LL; i++);
int64_t ts_loop = srs_update_system_time();
for (long long i = 0; i < 10000000LL; i++);
int64_t ts_loop2 = srs_update_system_time();
printf("[COST] loop 100w=%dus, 1000w=%dus\n", srsu2i(ts_loop - start), srsu2i(ts_loop2 - ts_loop));
}
// The cost for printf.
if (true) {
int64_t start = srs_update_system_time();
printf("TEST: OK\n");
int64_t ts_printf = srs_update_system_time();
printf("TEST: OK OK\n");
int64_t ts_printf2 = srs_update_system_time();
printf("TEST: OK OK %s\n", "OK");
int64_t ts_printf3 = srs_update_system_time();
printf("[COST] printf=%dus %dus %dus\n",
srsu2i(ts_printf - start),
srsu2i(ts_printf2 - ts_printf),
srsu2i(ts_printf3 - ts_printf2)
);
}
// The cost for file open or close.
if (true) {
int64_t start = srs_update_system_time();
int fd = ::open("cost.log", O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
int64_t ts_open = srs_update_system_time();
::close(fd);
int64_t ts_close = srs_update_system_time();
printf("[COST] open=%dus, close=%dus\n",
srsu2i(ts_open - start),
srsu2i(ts_close - ts_open)
);
}
// The cost for file writing.
if (true) {
int64_t start = srs_update_system_time();
int fd = ::open("cost.log", O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
int64_t ts_open = srs_update_system_time();
::write(fd, "Hello\n", 6);
int64_t ts_write = srs_update_system_time();
::write(fd, "HelloHello\n", 12);
int64_t ts_write2 = srs_update_system_time();
::close(fd);
int64_t ts_close = srs_update_system_time();
printf("[COST] write=%dus %dus\n",
srsu2i(ts_write - ts_open),
srsu2i(ts_write2 - ts_write)
);
}
// The cost for file reading.
if (true) {
char buf[128];
int64_t start = srs_update_system_time();
int fd = ::open("cost.log", O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
int64_t ts_open = srs_update_system_time();
::read(fd, buf, 6);
int64_t ts_read = srs_update_system_time();
::read(fd, buf, 6);
int64_t ts_read2 = srs_update_system_time();
::close(fd);
int64_t ts_close = srs_update_system_time();
printf("[COST] read=%dus %dus\n",
srsu2i(ts_read - ts_open),
srsu2i(ts_read2 - ts_read)
);
}
// The cost for ST timer.
st_set_eventsys(ST_EVENTSYS_ALT);
st_init();
for (;;) {
int64_t start = srs_update_system_time();
st_usleep(20 * 1000);
int64_t cost = srs_update_system_time() - start;
if (cost > (20 + 10) * 1000) {
printf("[COST] timer=%dms\n", (int)(cost / 1000));
}
}
return 0;
}
+29
View File
@@ -0,0 +1,29 @@
/*
# !!! ST does not support C++ exceptions on cygwin !!!
g++ exceptions.cpp ../../objs/st/libst.a -g -O0 -o exceptions && ./exceptions
*/
#include <stdio.h>
#include <exception>
#include "../../objs/st/st.h"
int handle_exception() {
try {
throw 3;
} catch (...) {
return 5;
}
}
void* foo(void* arg) {
int r0 = handle_exception();
printf("r0=%d\n", r0);
return NULL;
}
int main(int argc, char** argv) {
st_init();
st_thread_create(foo, NULL, 0, 0);
st_thread_exit(NULL);
return 0;
}
+16
View File
@@ -0,0 +1,16 @@
/*
g++ hello-st.cpp ../../objs/st/libst.a -g -O0 -o hello-st && ./hello-st
*/
#include <stdio.h>
#include "../../objs/st/st.h"
void foo() {
st_init();
st_sleep(1);
printf("Hello World, ST!\n");
}
int main() {
foo();
return 0;
}
+19
View File
@@ -0,0 +1,19 @@
/*
g++ hello-world.cpp ../../objs/st/libst.a -g -O0 -o hello-world && ./hello-world
*/
#include <stdio.h>
#include "../../objs/st/st.h"
void foo() {
st_init();
for (int i = 0; ; i++) {
st_sleep(1);
printf("#%d: main: working\n", i);
}
}
int main() {
foo();
return 0;
}
+11
View File
@@ -0,0 +1,11 @@
/*
g++ hello.cpp -g -O0 -o hello && ./hello
*/
void foo() {
}
int main(int argc, char** argv) {
foo();
return 0;
}
+41
View File
@@ -0,0 +1,41 @@
/*
g++ huge-threads.cpp ../../objs/st/libst.a -g -O0 -o huge-threads && ./huge-threads 60000
*/
#include <stdio.h>
#include <stdlib.h>
#include "../../objs/st/st.h"
void* pfn(void* arg) {
char v[32*1024]; // 32KB in stack.
for (;;) {
v[0] = v[sizeof(v) - 1] = 0xf;
st_usleep(1000 * 1000);
}
return NULL;
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("Usage: %s nn_coroutines [verbose]\n", argv[0]);
exit(-1);
}
st_init();
int nn = ::atoi(argv[1]);
printf("pid=%d, create %d coroutines\n", ::getpid(), nn);
for (int i = 0; i < nn; i++) {
st_thread_t thread = st_thread_create(pfn, NULL, 1, 0);
if (!thread) {
printf("create thread fail, i=%d\n", i);
return -1;
}
if (argc > 2) {
printf("thread #%d: %p\n", i, thread);
}
}
printf("done\n");
st_thread_exit(NULL);
return 0;
}
+36
View File
@@ -0,0 +1,36 @@
/*
Directly compile c++ source and execute:
g++ pthreads.cpp ../../objs/st/libst.a -g -O0 -o pthreads && ./pthreads
*/
#include <stdio.h>
#include <pthread.h>
#include "../../objs/st/st.h"
void* foo(void* arg) {
while (true) {
printf("Hello, child thread\n");
st_sleep(1);
}
return NULL;
}
void* pfn(void* arg) {
st_init();
st_thread_create(foo, NULL, 0, 0);
st_thread_exit(NULL);
return NULL;
}
int main(int argc, char** argv) {
st_init();
pthread_t trd;
pthread_create(&trd, NULL, pfn, NULL);
while (true) {
printf("Hello, main thread\n");
st_sleep(1);
}
return 0;
}
+36
View File
@@ -0,0 +1,36 @@
/*
g++ thread-join.cpp ../../objs/st/libst.a -g -O0 -o thread-join && ./thread-join
*/
#include <stdio.h>
#include <stdlib.h>
#include "../../objs/st/st.h"
void* pfn(void* arg) {
printf("pid=%d, coroutine is ok\n", ::getpid());
return NULL;
}
int main(int argc, char** argv) {
st_init();
printf("pid=%d, create coroutine #1\n", ::getpid());
st_thread_t thread = st_thread_create(pfn, NULL, 1, 0);
st_thread_join(thread, NULL);
st_usleep(100 * 1000);
printf("pid=%d, create coroutine #2\n", ::getpid());
thread = st_thread_create(pfn, NULL, 1, 0);
st_thread_join(thread, NULL);
st_usleep(100 * 1000);
printf("pid=%d, create coroutine #3\n", ::getpid());
thread = st_thread_create(pfn, NULL, 1, 0);
st_thread_join(thread, NULL);
printf("done\n");
st_thread_exit(NULL);
return 0;
}
+62
View File
@@ -0,0 +1,62 @@
/*
g++ udp-client.cpp ../../objs/st/libst.a -g -O0 -o udp-client && ./udp-client 127.0.0.1 8000 3
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "../../objs/st/st.h"
#include <sys/socket.h>
#include <arpa/inet.h>
void* pfn(void* arg) {
sockaddr_in addr = *(sockaddr_in*)arg;
char* ip = inet_ntoa(addr.sin_addr);
int port = ntohs(addr.sin_port);
int fd = ::socket(AF_INET, SOCK_DGRAM, 0);
st_netfd_t stfd = st_netfd_open_socket(fd);
printf("connect to %s:%d, fd=%d ok\n", ip, port, fd);
while (true) {
char data[] = "Hello world!";
int r0 = st_sendto(stfd, data, sizeof(data), (sockaddr*)&addr, sizeof(sockaddr_in), ST_UTIME_NO_TIMEOUT);
printf("fd #%d, send %dB %s, r0=%d\n", fd, (int)sizeof(data), data, r0);
if (r0 != (int)sizeof(data)) {
break;
}
st_usleep(800 * 1000);
}
st_netfd_close(stfd);
return NULL;
}
int main(int argc, char** argv) {
if (argc < 4) {
printf("Usage: %s ip port workers\n", argv[0]);
exit(-1);
}
st_init();
const char* ip = argv[1];
int port = ::atoi(argv[2]);
int workers = ::atoi(argv[3]);
printf("Start %d workers, to %s:%d\n", workers, ip, port);
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip);
addr.sin_port = htons(port);
for (int i = 0; i < workers; i++) {
st_thread_t thread = st_thread_create(pfn, &addr, 1, 0);
assert(thread);
}
st_thread_exit(NULL);
return 0;
}
+80
View File
@@ -0,0 +1,80 @@
/*
g++ udp-server.cpp ../../objs/st/libst.a -g -O0 -o udp-server && ./udp-server 127.0.0.1 8000 3
*/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "../../objs/st/st.h"
#include <sys/socket.h>
#include <arpa/inet.h>
void* pfn(void* arg) {
sockaddr_in addr = *(sockaddr_in*)arg;
char* ip = inet_ntoa(addr.sin_addr);
int port = ntohs(addr.sin_port);
int fd = ::socket(AF_INET, SOCK_DGRAM, 0);
assert(fd > 0);
if (true) {
int v = 1;
int r0 = setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &v, sizeof(int));
assert(r0 != -1);
}
if (true) {
int v = 1;
int r0 = setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &v, sizeof(int));
assert(r0 != -1);
}
if (true) {
int r0 = bind(fd, (sockaddr*)&addr, sizeof(sockaddr_in));
assert(!r0);
}
st_netfd_t stfd = st_netfd_open_socket(fd);
printf("listen at %s:%d, fd=%d ok\n", ip, port, fd);
while (true) {
char buf[1600] = {0};
sockaddr_in from;
int nb_from = sizeof(from);
int r0 = st_recvfrom(stfd, buf, sizeof(buf), (sockaddr*)&from, &nb_from, ST_UTIME_NO_TIMEOUT);
printf("fd #%d, peer %s:%d, got %dB, %s\n", fd, inet_ntoa(from.sin_addr), ntohs(from.sin_port), r0, buf);
if (r0 <= 0) {
break;
}
st_usleep(10 * 1000);
}
st_netfd_close(stfd);
return NULL;
}
int main(int argc, char** argv) {
if (argc < 4) {
printf("Usage: %s ip port workers\n", argv[0]);
exit(-1);
}
st_init();
const char* ip = argv[1];
int port = ::atoi(argv[2]);
int workers = ::atoi(argv[3]);
printf("Start %d workers, at %s:%d\n", workers, ip, port);
sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = inet_addr(ip);
addr.sin_port = htons(port);
for (int i = 0; i < workers; i++) {
st_thread_t thread = st_thread_create(pfn, &addr, 1, 0);
assert(thread);
}
st_thread_exit(NULL);
return 0;
}