Statistics
| Branch: | Revision:

peerstreamer-src / src / sdpfile.c @ 58fb2cdc

History | View | Annotate | Download (4.22 KB)

1 c039490c Luca Baldesi
/*******************************************************************
2
* PeerStreamer-ng is a P2P video streaming application exposing a ReST
3
* interface.
4
* Copyright (C) 2015 Luca Baldesi <luca.baldesi@unitn.it>
5
*
6
* This program is free software: you can redistribute it and/or modify
7
* it under the terms of the GNU Affero General Public License as published by
8
* the Free Software Foundation, either version 3 of the License, or
9
* (at your option) any later version.
10
*
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
* GNU Affero General Public License for more details.
15
*
16
* You should have received a copy of the GNU Affero General Public License
17
* along with this program.  If not, see <http://www.gnu.org/licenses/>.
18
*******************************************************************/
19
20
#include<stdint.h>
21
#include<stdio.h>
22
#include<sdpfile.h>
23
#include<debug.h>
24
#include<tokens.h>
25
26
27
#ifndef MAX
28
#define MAX(a, b) ((a) > (b) ? (a) : (b))
29
#endif
30
31
struct pssdpfile {
32
        const struct pstreamer * ps;
33
        char path[MAX_PATH_LENGTH];
34 91e9f1a7 Luca Baldesi
        char dst_ip[MAX_IPADDR_LENGTH];
35 c039490c Luca Baldesi
};
36
37 91e9f1a7 Luca Baldesi
void sdpfile_dump(const char * filename, const char * sdpdesc, const struct pstreamer * ps, const char * dst_ip)
38 c039490c Luca Baldesi
{
39
        FILE * fp;
40
        char ** lines;
41
        char ** records;
42
        uint32_t n_lines, n_records, l, r, i;
43
        uint16_t audio_port, video_port;
44
45 c2bc5145 Luca Baldesi
        audio_port = pstreamer_base_port(ps) + 1;
46 fc82e6c5 Luca Baldesi
        video_port = audio_port + 2;
47 c039490c Luca Baldesi
48
        debug("Saving sdpfile to %s\n", filename);
49
        lines = tokens_create((char *) sdpdesc, '\n', &n_lines);
50
51
        fp = fopen(filename, "w");
52
        for(l = 0; l < n_lines; l++)
53
        {
54
                records = tokens_create(lines[l], ' ', &n_records);
55
                if ((r = tokens_check(records, n_records, "m=audio")) + 1 > 0)
56
                {
57
                        for(i = 0; i < r+1; i++)
58
                                fprintf(fp, "%s ", records[i]);
59
                        fprintf(fp, "%d ", audio_port);
60
                        for(i = r+2; i < n_records-1; i++)
61
                                fprintf(fp, "%s ", records[i]);
62
                        fprintf(fp, "%s\n", records[i]);
63
                }
64
                else if ((r = tokens_check(records, n_records, "m=video")) + 1 > 0)
65
                {
66
                        for(i = 0; i < r+1; i++)
67
                                fprintf(fp, "%s ", records[i]);
68
                        fprintf(fp, "%d ", video_port);
69
                        for(i = r+2; i < n_records-1; i++)
70
                                fprintf(fp, "%s ", records[i]);
71
                        fprintf(fp, "%s\n", records[i]);
72
                }
73 91e9f1a7 Luca Baldesi
                else if ((r = tokens_check(records, n_records, "c=IN")) + 1 > 0)
74
                {
75
                        for(i = 0; i < r+2; i++)
76
                                fprintf(fp, "%s ", records[i]);
77
                        fprintf(fp, "%s ", dst_ip);
78
                        for(i = r+3; i < n_records; i++)
79
                                fprintf(fp, "%s ", records[i]);
80
                        fprintf(fp, "\n");
81
                }
82 c039490c Luca Baldesi
                else
83
                        if (lines[l])
84
                                fprintf(fp, "%s\n", lines[l]);
85
                tokens_destroy(&records, n_records);
86
        }
87
        fclose(fp);
88
89
        tokens_destroy(&lines, n_lines);
90
}
91
92
void sdpfile_handler(struct mg_connection *nc, int ev, void *ev_data)
93
{
94
        struct http_message *hm = (struct http_message *) ev_data;
95
        struct pssdpfile * psdp;
96
        char * sdpdesc;
97
98
        psdp = nc->user_data;
99
100
        switch (ev) {
101
                case MG_EV_CONNECT:
102
                        if (*(int *) ev_data != 0)
103
                                debug("SDPFILE retrieval failure\n");
104
                        break;
105
                case MG_EV_HTTP_REPLY:
106
                        switch (hm->resp_code) {
107
                                case 200:
108
                                        sdpdesc = malloc(sizeof(char) * (hm->body.len + 1));
109
                                        strncpy(sdpdesc, hm->body.p, hm->body.len);
110
                                        sdpdesc[hm->body.len] = '\0';  // make sure string terminates
111 91e9f1a7 Luca Baldesi
                                        sdpfile_dump(psdp->path, sdpdesc, psdp->ps, psdp->dst_ip);
112 c039490c Luca Baldesi
                                        free(sdpdesc);
113
                                default:
114
                                        debug("SDPFILE server answers: %d\n", hm->resp_code);
115
                        }
116 4d6f8fd5 Luca Baldesi
                        nc->flags |= MG_F_SEND_AND_CLOSE;
117 c039490c Luca Baldesi
                        free(psdp);
118
                        break;
119
                case MG_EV_CLOSE:
120
                        debug("SDPFILE server closed connection\n");
121
                        break;
122
                default:
123
                        break;
124
        }
125
}
126
127
128 91e9f1a7 Luca Baldesi
char * sdpfile_create(const struct context * c, const struct pschannel * ch, const struct pstreamer * ps, const char * rtp_dst_ip)
129 c039490c Luca Baldesi
{
130
        struct mg_connection * conn;
131
        struct pssdpfile * psdp;
132 0bfdcc3a Luca Baldesi
        char sdpfile[MAX_SDPFILENAME_LENGTH];
133 c039490c Luca Baldesi
134
        psdp = malloc(sizeof(struct pssdpfile));
135
        psdp->ps = ps;
136
        snprintf(psdp->path, MAX_PATH_LENGTH, "%s/%s.sdp", c->http_opts.document_root, pstreamer_id(ps));
137 91e9f1a7 Luca Baldesi
        snprintf(psdp->dst_ip, MAX_IPADDR_LENGTH, "%s", rtp_dst_ip);
138 0bfdcc3a Luca Baldesi
        snprintf(sdpfile, MAX_SDPFILENAME_LENGTH, "%s.sdp", pstreamer_id(ps));
139 c039490c Luca Baldesi
140
        conn = mg_connect_http(c->mongoose_srv, sdpfile_handler, ch->sdpfile, NULL, NULL);
141 fc82e6c5 Luca Baldesi
        if (conn)
142
        {
143
                conn->user_data = (void *) psdp;
144
                return strdup(sdpfile);
145
        } else
146
                return NULL;
147 c039490c Luca Baldesi
}