-
Notifications
You must be signed in to change notification settings - Fork 4
/
ndi_recorder.cpp
299 lines (267 loc) · 10.1 KB
/
ndi_recorder.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
/*
* NDI Client to JACK (Jack Audio Connection Kit) Output
*
* This program can be used and distrubuted without resrictions
*/
#include <cassert>
#include <cstdio>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <math.h>
#include <signal.h>
#include <ctime>
#include <atomic>
#include <unistd.h>
#include <fstream> //for reading and writing preset file
#include <mongoose.h>
#include "mjson.h"
#include <thread>
#include <getopt.h>
#include <condition_variable>
#include <Processing.NDI.Lib.h>
NDIlib_find_create_t NDI_find_create_desc; /* Default settings for NDI find */
NDIlib_find_instance_t pNDI_find;
const NDIlib_source_t* p_sources = NULL;
struct mg_mgr mgr;
//Function Definitions
std::string convertToString(char* a);
static char *store_path;
struct recorder {
recorder(const char* source, const char* path); //constructor
~recorder(void); //destructor
private:
pid_t pid;
NDIlib_recv_instance_t m_pNDI_recv; // Create the receiver
NDIlib_framesync_instance_t m_pNDI_framesync; //NDI framesync
std::atomic<bool> m_exit; // Are we ready to exit
void receive(void); // This is called to receive frames
};
//Constructor
recorder::recorder(const char* source, const char* path): m_pNDI_recv(NULL), m_pNDI_framesync(NULL), m_exit(false){
printf("Starting Recorder for %s\n", source);
std::string store_path_string = path;
store_path_string += source;
const char *final_path = store_path_string.c_str();
printf("Storing at %s\n", path);
pid = fork();
if(pid == 0){
printf("child process, pid = %u\n",getpid());
execl("/opt/ndi_recorder/bin/ndi-record", "/opt/ndi_recorder/bin/ndi-record", "-i", source, "-o", final_path, NULL); //start the recorder process
}
}
// Destructor
recorder::~recorder(void){ // Wait for the thread to exit
m_exit = true;
kill(pid, SIGINT); //send INT signal to the ndi-record process
// Destroy the receiver
}
static const int no_receivers = 50; //max number of recorders
recorder* p_receivers[no_receivers] = { 0 };
std::string ndi_running_name[no_receivers] = { "" };
static void fn(struct mg_connection *c, int ev, void *ev_data, void *fn_data){
if(ev == MG_EV_WS_OPEN){
c->label[0] = 'W'; // Mark this connection as an established WS client
}
if (ev == MG_EV_HTTP_MSG){
struct mg_http_message *hm = (struct mg_http_message *) ev_data;
struct mg_connection *c2 = mgr.conns;
if(mg_http_match_uri(hm, "/ws")){ //upgrade to WebSocket
mg_ws_upgrade(c, hm, NULL);
}else if(mg_http_match_uri(hm, "/rest")) { //handle REST events
mg_http_reply(c, 200, "", "{\"result\": %d}\n", 123);
}else{ // Serve static files
struct mg_http_serve_opts opts = {.root_dir = "/opt/ndi_recorder/assets/"};
mg_http_serve_dir(c, (mg_http_message*)ev_data, &opts);
}
}else if (ev == MG_EV_WS_MSG){
// Got websocket frame. Received data is wm->data. Echo it back!
struct mg_ws_message *wm = (struct mg_ws_message *) ev_data;
//std::cout << "WebSocket: " << wm->data.ptr << std::endl;
char prefix_buf[100];
char action_buf[100];
mjson_get_string(wm->data.ptr, wm->data.len, "$.prefix", prefix_buf, sizeof(prefix_buf)); //get prefix
mjson_get_string(wm->data.ptr, wm->data.len, "$.action", action_buf, sizeof(action_buf)); //get action
std::string prefix_string = convertToString(prefix_buf);
std::string action_string = convertToString(action_buf);
if(prefix_string == "refresh"){
if(action_string == "refresh"){
uint32_t no_sources = 0;
p_sources = NDIlib_find_get_current_sources(pNDI_find, &no_sources);
std::string discover_json;
std::string source_json = "";
discover_json = "{\"prefix\":\"discover_source\",\"action\":\"display\",\"source_list\":{";
for(uint32_t i = 0; i < no_sources; i++){
std::string ndi_string = p_sources[i].p_ndi_name;
std::string url_string = p_sources[i].p_url_address;
std::string source_id = std::to_string(i);
int conflict = 0;
for(uint32_t i = 0; i < no_receivers; i++){ //check for conflicts
if((ndi_running_name[i] == ndi_string)&&(conflict == 0)){
conflict = 1; //found conflict with a name that is already stored - already running this receiver
}
}
if(conflict == 0){ //since this is not running on a receiver - display
//std::cout << "Source IP: " << p_sources[i].p_url_address << std::endl;
if(source_json == ""){
source_json += "\""+source_id + "\":{\"name\":\""+ndi_string+"\",\"url\":\""+url_string+"\"}";
}else{
source_json += ",\""+source_id + "\":{\"name\":\""+ndi_string+"\",\"url\":\""+url_string+"\"}";
}
}
}
discover_json += source_json;
discover_json += "}";
discover_json += "}";
const char* pub_json1 = discover_json.c_str();
for (struct mg_connection *c2 = mgr.conns; c2 != NULL; c2 = c2->next) { //traverse over all client connections
if (c2->label[0] == 'W'){ //make sure it is a websocket connection
mg_ws_send(c2, pub_json1, strlen(pub_json1), WEBSOCKET_OP_TEXT);
}
}
std::string connected_json;
source_json = "";
connected_json = "{\"prefix\":\"playing_source\",\"action\":\"display\",\"source_list\":{";
for(uint32_t i = 0; i < no_receivers; i++){
if(ndi_running_name[i] != ""){ //make sure receiver is not empty
std::string source_id = std::to_string(i);
if(source_json == ""){
source_json += "\""+source_id + "\":{\"name\":\""+ndi_running_name[i]+"\"}";
}else{
source_json += ",\""+source_id + "\":{\"name\":\""+ndi_running_name[i]+"\"}";
}
}
}
connected_json += source_json;
connected_json += "}";
connected_json += "}";
const char* pub_json2 = connected_json.c_str();
for (struct mg_connection *c2 = mgr.conns; c2 != NULL; c2 = c2->next) { //traverse over all client connections
if (c2->label[0] == 'W'){ //make sure it is a websocket connection
mg_ws_send(c2, pub_json2, strlen(pub_json2), WEBSOCKET_OP_TEXT);
}
}
}
}
if(prefix_string == "connect_source"){
int source_id = std::stoi(action_string);
int stored = 0;
int receiver_id = 0;
int conflict = 0;
std::string ndi_string = p_sources[source_id].p_ndi_name;
for(uint32_t i = 0; i < no_receivers; i++){ //check for conflicts
if((ndi_running_name[i] == ndi_string)&&(conflict == 0)){
conflict = 1; //found conflict with a name that is already stored - already running this receiver
}
}
if(conflict == 0){
for(uint32_t i = 0; i < no_receivers; i++){
if(stored == 0){
if(ndi_running_name[i] == ""){ //empty string array
ndi_running_name[i] = ndi_string;
std::cout << "ID: " << i << std::endl;
stored = 1;
receiver_id = i;
}
}
}
p_receivers[receiver_id] = new recorder(p_sources[source_id].p_ndi_name, store_path);
}else{
//std::cout << "Receiver already running for: " << p_sources[source_id].p_ndi_name << std::endl;
}
}
if(prefix_string == "disconnect_source"){
int source_id = std::stoi(action_string);
delete p_receivers[source_id]; //delete receiver
ndi_running_name[source_id] = ""; //update the running receiver
}
if(prefix_string == "save_streams"){
std::ofstream preset_file("/opt/ndi2jack/assets/presets.txt");
for(uint32_t i = 0; i < no_receivers; i++){
if(ndi_running_name[i] != ""){ //make sure a receiver is stored before trying to save in file
preset_file << ndi_running_name[i];
preset_file << std::endl;
}
}
preset_file.close();
}
}
}
static void usage(FILE *fp, int argc, char **argv){
fprintf(fp,
"Usage: NDI Recorder [options]\n\n"
"Version 1.0\n"
"Options:\n"
"-h | --help Print this message\n"
"-p | --path Specify storage path (e.g. /media/store/)\n"
"",
argv[0]);
}
static const char short_options[] = "p:";
static const struct option
long_options[] = {
{ "help", no_argument, NULL, 'h' },
{ "path", required_argument, NULL, 'p' },
{ 0, 0, 0, 0 }
};
int main (int argc, char *argv[]){
for (;;) {
int idx;
int c;
c = getopt_long(argc, argv,short_options, long_options, &idx);
if (-1 == c){
break;
}
switch(c){
case 'h':
usage(stdout, argc, argv);
exit(EXIT_SUCCESS);
case 'p':
store_path = optarg;
break;
default:
usage(stderr, argc, argv);
exit(EXIT_FAILURE);
}
}
if(!NDIlib_initialize()){
printf("Cannot run NDI."); // Cannot run NDI. Most likely because the CPU is not sufficient.
return 0;
}
// Create a NDI finder
NDI_find_create_desc.show_local_sources = (bool)false; //don't include local sources when searching for NDI
pNDI_find = NDIlib_find_create_v2(&NDI_find_create_desc);
if (!pNDI_find) return 0; //error out if the NDI finder can't be created
std::string output_text; //preset file is temporary stored in this variable
std::ifstream preset_file("/opt/ndi2jack/assets/presets.txt"); //open the presets file
while(getline(preset_file, output_text)){
int stored = 0;
int receiver_id = 0;
const char* ndi_name = output_text.c_str();;
std::string ndi_string = ndi_name;
for(uint32_t i = 0; i < no_receivers; i++){
if(stored == 0){
if(ndi_running_name[i] == ""){ //empty string array - make sure it is empty before trying to start receiver
ndi_running_name[i] = ndi_string;
stored = 1;
receiver_id = i;
}
}
}
p_receivers[receiver_id] = new recorder(ndi_name,store_path);
}
mg_mgr_init(&mgr);
mg_http_listen(&mgr, "ws://0.0.0.0:80", fn, NULL); // Create WebSocket and HTTP connection
for (;;) mg_mgr_poll(&mgr, 1000); // Block forever
/* keep running until the Ctrl+C */
while(1){
sleep(1);
}
exit (0);
}
std::string convertToString(char* a){
std::string s = a;
return s;
}