Revision 3af4c8d7 src/path_handlers.c

View differences:

src/path_handlers.c
28 28
#include<mongoose.h>
29 29
#include<streamer_creation_callback.h>
30 30

  
31
void mg_connection_remote_ip(char * ip, const struct mg_connection *nc)
32
{
33
	const struct sockaddr_in *sender;
34
	sender = (struct sockaddr_in *)&(nc->sa);
35
	inet_ntop(AF_INET, &(sender->sin_addr), ip, MAX_IPADDR_LENGTH);
36
}
37

  
38 31
char * mg_uri_field(struct http_message *hm, uint8_t pos)
39 32
{
40 33
	char * uri;
......
71 64
	free(channels);
72 65
}
73 66

  
67
int8_t source_streamer_creation_handler(struct mg_connection *nc, const struct pschannel_bucket *psb, const struct pstreamer * ps, int8_t ret)
68
{
69
	char * json = NULL;
70
	int8_t res = -1;
71

  
72
	info("Inside source creation handler\n");
73
	if (ps)
74
		json = pstreamer_to_json(ps);
75

  
76
	if (ret == 0 && json)
77
	{
78
		mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-type: application/json\r\n\r\n");
79
		mg_printf_http_chunk(nc, json);
80
		res = 0;
81
		info("Source room created and served\n");
82
	} else {
83
		mg_printf(nc, "%s", "HTTP/1.1 500 Internal server error\r\nTransfer-Encoding: chunked\r\n\r\n");
84
		// destroy ps?
85
		info("Stream room cannot be correctly created\n");
86
		if (ret)
87
			debug(json);
88
		else
89
			debug("PS does not exist");
90
	}
91
	if (json)
92
		free(json);
93
	mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
94

  
95
	return res;
96
}
97

  
74 98
int8_t streamer_creation_handler(struct mg_connection *nc, const struct pschannel_bucket *psb, const struct pstreamer * ps, int8_t ret)
75 99
{
76 100
	char * json = NULL;
......
125 149
	mg_get_http_var(&hm->body, "port", port, MAX_PORT_LENGTH);
126 150

  
127 151
	id = mg_uri_field(hm, 1);
128
	mg_connection_remote_ip(rtp_dst_ip, nc);
152
	mg_conn_addr_to_str(nc, rtp_dst_ip, MAX_IPADDR_LENGTH, MG_SOCK_STRINGIFY_IP|MG_SOCK_STRINGIFY_REMOTE);
129 153

  
130 154
	info("POST request for resource %s from %s\n", id, rtp_dst_ip);
131 155
	ch = pschannel_bucket_find(c->pb, ipaddr, port);
......
180 204

  
181 205
	free(id);
182 206
}
207

  
208
void source_index(struct mg_connection *nc, struct http_message *hm)
209
{
210
	char * channels;
211
	const struct context * c;
212

  
213
	c = (const struct context *) nc->user_data;
214

  
215
	info("GET request for source\n");
216
	channels = pstreamer_manager_sources_to_json(c->psm);
217
	debug("\t%s\n", channels);
218

  
219
	mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-type: application/json\r\n\r\n");
220
	mg_printf_http_chunk(nc, "%s", channels);
221
	mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
222

  
223
	free(channels);
224
}
225

  
226
void source_streamer_create(struct mg_connection *nc, struct http_message *hm)
227
{
228
	const struct context * c;
229
	char rtp_source_ip[MAX_IPADDR_LENGTH];
230
	char * id;
231
	const struct pstreamer * ps = NULL;
232
	const struct pschannel * ch = NULL;
233

  
234
	c = (const struct context *) nc->user_data;
235

  
236
	id = mg_uri_field(hm, 1);
237
	mg_conn_addr_to_str(nc, rtp_source_ip, MAX_IPADDR_LENGTH, MG_SOCK_STRINGIFY_IP);
238

  
239
	info("POST request for source resource %s from %s\n", id, rtp_source_ip);
240

  
241
	ps = pstreamer_manager_create_source_streamer(c->psm, id, rtp_source_ip, streamer_creation_callback_new(nc, c->pb, source_streamer_creation_handler)); 
242
	if(ps)
243
	{
244
		pstreamer_schedule_tasks((struct pstreamer*)ps, c->tm);
245
		info("Source streamer instance created\n");
246
	} else {
247
		info("Source streamer could not be launched\n");
248
		mg_printf(nc, "%s", "HTTP/1.1 409 Conflict\r\nTransfer-Encoding: chunked\r\n\r\n");
249
		mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
250
	}
251

  
252
	free(id);
253
}
254

  
255
void source_streamer_update(struct mg_connection *nc, struct http_message *hm)
256
{
257
	char * id, * json;
258
	char janus_user_id[MAX_JANUS_USERID_LENGTH+1];
259
	const struct pstreamer * ps;
260
	const struct context * c;
261

  
262
	c = (const struct context *) nc->user_data;
263
	id = mg_uri_field(hm, 1);
264

  
265
	ps = pstreamer_manager_get_streamer(c->psm, id);
266
	info("UPDATE request for source resource %s\n", id);
267
	if (ps)
268
	{
269
		mg_get_http_var(&hm->body, "participant_id", janus_user_id, MAX_JANUS_USERID_LENGTH);
270

  
271
		pstreamer_source_touch(c->psm, (struct pstreamer*) ps, atoll(janus_user_id));
272
		info("\tSource instance %s found and touched\n", id);
273
		json = pstreamer_to_json(ps);
274
		mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\nContent-type: application/json\r\n\r\n");
275
		mg_printf_http_chunk(nc, "%s", json);
276
		mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
277
		free(json);
278
	} else {
279
		info("\tInstance %s not found\n", id);
280
		mg_printf(nc, "%s", "HTTP/1.1 404 Not Found\r\nTransfer-Encoding: chunked\r\n\r\n");
281
		mg_send_http_chunk(nc, "", 0); /* Send empty chunk, the end of response */
282
	}
283
	free(id);
284
}
285

  

Also available in: Unified diff