forked from klaxa/ffserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlavfhttpd.c
163 lines (144 loc) · 5.27 KB
/
lavfhttpd.c
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
/*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef LAVFHTTPD_H
#define LAVFHTTPD_H
#include "httpd.h"
#include <libavutil/opt.h>
#include <libavformat/avformat.h>
int lavfhttpd_init(void **server, struct HTTPDConfig config)
{
char out_uri[1024];
int ret;
AVDictionary *opts = NULL;
AVIOContext *server_ctx = NULL;
snprintf(out_uri, 1024, "http://%s:%d", config.bind_address, config.port);
avformat_network_init();
if ((ret = av_dict_set(&opts, "listen", "2", 0)) < 0) {
av_log(opts, AV_LOG_ERROR, "Failed to set listen mode for server: %s\n", av_err2str(ret));
av_free(opts);
return -1;
}
if ((ret = av_dict_set_int(&opts, "listen_timeout", config.accept_timeout, 0)) < 0) {
av_log(opts, AV_LOG_ERROR, "Failed to set listen_timeout for server: %s\n", av_err2str(ret));
av_free(opts);
return -1;
}
if ((ret = avio_open2(&server_ctx, out_uri, AVIO_FLAG_WRITE, NULL, &opts)) < 0) {
av_log(server, AV_LOG_ERROR, "Failed to open server: %s\n", av_err2str(ret));
av_free(opts);
return -1;
}
av_free(opts);
*server = server_ctx;
return 0;
}
int lavfhttpd_accept(void *server, struct HTTPClient **client, const char **valid_files)
{
AVIOContext *server_ctx = (AVIOContext*) server;
AVIOContext *client_ctx = NULL;
struct HTTPClient *client_http = NULL;
int ret, ret2, handshake;
int reply_code2 = 200; // = reply_code;
char *method, *resource;
if ((ret = avio_accept(server_ctx, &client_ctx)) < 0) {
if (ret == AVERROR(ETIMEDOUT)) {
return HTTPD_LISTEN_TIMEOUT;
} else {
if (client_ctx)
avio_context_free(&client_ctx);
return HTTPD_OTHER_ERROR;
}
}
client_ctx->seekable = 0;
ret2 = HTTPD_OK;
client_http = av_malloc(sizeof(*client_http));
if (!client_http) {
av_log(server, AV_LOG_ERROR, "Could not allocate http client.\n");
return HTTPD_OTHER_ERROR;
}
client_http->method = NULL;
client_http->resource = NULL;
client_http->httpd_data = client_ctx;
while ((handshake = avio_handshake(client_ctx)) > 0) {
av_opt_get(client_ctx, "method", AV_OPT_SEARCH_CHILDREN, (uint8_t**) &method);
av_opt_get(client_ctx, "resource", AV_OPT_SEARCH_CHILDREN, (uint8_t**) &resource);
av_log(client_ctx, AV_LOG_DEBUG, "method: %s resource: %s\n", method, resource);
if (method && strlen(method) && strncmp("GET", method, 3)) {
ret2 = HTTPD_CLIENT_ERROR;
reply_code2 = 400;
if ((ret = av_opt_set_int(client_ctx, "reply_code", reply_code2, AV_OPT_SEARCH_CHILDREN)) < 0) {
av_log(client_ctx, AV_LOG_WARNING, "Failed to set reply_code: %s.\n", av_err2str(ret));
}
}
av_free(client_http->method);
av_free(client_http->resource);
client_http->method = av_strdup(method);
client_http->resource = av_strdup(resource);
av_free(method);
av_free(resource);
}
if (handshake < 0) {
ret2 = HTTPD_CLIENT_ERROR;
reply_code2 = 400;
}
if ((ret = av_opt_set_int(client_ctx, "reply_code", reply_code2, AV_OPT_SEARCH_CHILDREN)) < 0) {
av_log(client_ctx, AV_LOG_WARNING, "Failed to set reply_code: %s.\n", av_err2str(ret));
}
*client = client_http;
return ret2;
}
int lavfhttpd_write(void *server, struct HTTPClient *client, const unsigned char *buf, int size)
{
AVIOContext *client_ctx = (AVIOContext*) client->httpd_data;
int64_t old_written = client_ctx->written;
int64_t actual_written;
avio_write(client_ctx, buf, size);
avio_flush(client_ctx);
actual_written = client_ctx->written - old_written;
if (actual_written < size) {
return AVERROR_EOF;
}
return size;
}
int lavfhttpd_read(void *server, struct HTTPClient *client, unsigned char *buf, int size)
{
AVIOContext *client_ctx = (AVIOContext*) client->httpd_data;
return avio_read(client_ctx, buf, size);
}
void lavfhttpd_close(void *server, struct HTTPClient *client)
{
AVIOContext *client_ctx = (AVIOContext*) client->httpd_data;
avio_close(client_ctx);
av_free(client->method);
av_free(client->resource);
av_free(client);
}
void lavfhttpd_shutdown(void *server)
{
AVIOContext *server_ctx = (AVIOContext*) server;
avio_close(server_ctx);
avformat_network_deinit();
}
struct HTTPDInterface lavfhttpd = {
.init = lavfhttpd_init,
.accept = lavfhttpd_accept,
.write = lavfhttpd_write,
.close = lavfhttpd_close,
.shutdown = lavfhttpd_shutdown,
};
#endif