Skip to content

Commit

Permalink
rtmp input added
Browse files Browse the repository at this point in the history
  • Loading branch information
amukhsimov committed Feb 22, 2021
1 parent e0eae90 commit 148f90d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 5 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ sudo apt update && sudo apt install -y tclsh pkg-config cmake libssl-dev build-e
https://github.com/ALLATRA-IT/srt-server/releases

## Usage
srt-server [PORT_RECEIVE PORT_SEND]
srt-server [PORT_RECEIVE PORT_SEND [--rtmp]]

*if PORT_RECEIVE and PORT_SEND are not specified, by default server receives on port 9000 and sends on 9001*
*if PORT_RECEIVE and PORT_SEND are not specified, by default server receives on port 9000 and sends on 9001.*

*if **rtmp** is specified, rtmp stream is received on rtmp://0.0.0.0/rtmp/rtmp2srt*

***ARGUMENTS ARE POSITION SENSITIVE***
2 changes: 2 additions & 0 deletions headers/main.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ uint64_t get_current_ms();

SRTSOCKET create_starter_socket(string *service);

void *begin_rtmp(void *opinfo);

void *handle_data_transfer(void *opinfo);

void *connections_handler(void *ptr);
Expand Down
42 changes: 39 additions & 3 deletions source/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,27 @@ int main(int argc, char *argv[]) {
srt_startup();
srt_setloglevel(srt_logging::LogLevel::fatal);

bool in_rtmp = false;
pthread_t rtmp_thread;

int yes = 1, no = 0;

service_rcv = string("9000");
service_snd = string("9001");

if (argc == 3) {
if (argc >= 3) {
if (strcmp(argv[1], argv[2]) == 0) {
cout << "Argument 1 cannot be equal to argument 2" << endl;
return 1;
}
service_rcv = argv[1];
service_snd = argv[2];
if (argc > 3) {
if (strcmp(argv[3], "rtmp") == 0) {
cout << "RTMP input mode enabled" << endl;
}
in_rtmp = true;
}
}

// Open sockets
Expand Down Expand Up @@ -121,7 +130,7 @@ int main(int argc, char *argv[]) {
.thread_info=&transferthread_infos[0]
};

if (pthread_create(&transferthread_infos[0].thread, NULL, handle_data_transfer,
if (pthread_create(&transferthread_infos[0].thread, nullptr, handle_data_transfer,
(void *) (&transferthread_0_info)) != 0) {
cout << "cannot create transfer thread 0: " << strerror(errno) << endl;
return 7;
Expand All @@ -131,6 +140,17 @@ int main(int argc, char *argv[]) {
return 8;
}

if (in_rtmp) {
if (pthread_create(&rtmp_thread, nullptr, begin_rtmp, nullptr) != 0) {
cout << "cannot create rtmp thread" << strerror(errno) << endl;
return 7;
}
if (pthread_detach(rtmp_thread) != 0) {
cout << "cannot detach rtmp thread: " << strerror(errno) << endl;
return 8;
}
}

transferthread_infos[0].is_alive = true;

log(LOG_INFO, (char *) "Server was successfully initialized\n");
Expand All @@ -145,7 +165,7 @@ int main(int argc, char *argv[]) {

pthread_mutex_lock(&stat_lock);

float timedelta = (float)(get_current_ms() - last_ms) / 1000;
float timedelta = (float) (get_current_ms() - last_ms) / 1000;
total_rcv_bytes += temp_rcv_bytes;
total_send_bytes += temp_send_bytes;
print_stats(timedelta);
Expand Down Expand Up @@ -254,6 +274,22 @@ int main(int argc, char *argv[]) {
return 0;
}

void *begin_rtmp(void *opinfo) {
char command[1000];
snprintf(command, 1000,
"ffmpeg -f flv -listen 1 -i rtmp://0.0.0.0:1935/rtmp/rtmp2srt "
"-c copy -f mpegts srt://127.0.0.1:%s?pkt_size=1316",
service_rcv.c_str());

while (true) {
cout << "Invoke cmd" << endl;
system(command);
cout << "RTMP error, restarting..." << endl;
}

return 0;
}

SRTSOCKET create_starter_socket(string *service) {
addrinfo hints;
addrinfo *res;
Expand Down

0 comments on commit 148f90d

Please sign in to comment.