forked from ossrs/srs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsrs_app_encoder.cpp
More file actions
364 lines (301 loc) · 10.4 KB
/
Copy pathsrs_app_encoder.cpp
File metadata and controls
364 lines (301 loc) · 10.4 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
/**
* The MIT License (MIT)
*
* Copyright (c) 2013-2017 OSSRS(winlin)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <srs_app_encoder.hpp>
#include <algorithm>
using namespace std;
#include <srs_kernel_error.hpp>
#include <srs_kernel_log.hpp>
#include <srs_app_config.hpp>
#include <srs_rtmp_stack.hpp>
#include <srs_app_pithy_print.hpp>
#include <srs_app_ffmpeg.hpp>
#include <srs_kernel_utility.hpp>
#ifdef SRS_AUTO_TRANSCODE
// for encoder to detect the dead loop
static std::vector<std::string> _transcoded_url;
SrsEncoder::SrsEncoder()
{
trd = new SrsDummyCoroutine();
pprint = SrsPithyPrint::create_encoder();
}
SrsEncoder::~SrsEncoder()
{
on_unpublish();
srs_freep(trd);
srs_freep(pprint);
}
int SrsEncoder::on_publish(SrsRequest* req)
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
// parse the transcode engines for vhost and app and stream.
ret = parse_scope_engines(req);
// ignore the loop encoder
// if got a loop, donot transcode the whole stream.
if (ret == ERROR_ENCODER_LOOP) {
clear_engines();
ret = ERROR_SUCCESS;
}
// return for error or no engine.
if (ret != ERROR_SUCCESS || ffmpegs.empty()) {
return ret;
}
// start thread to run all encoding engines.
srs_freep(trd);
trd = new SrsSTCoroutine("encoder", this, _srs_context->get_id());
if ((err = trd->start()) != srs_success) {
// TODO: FIXME: Use error
ret = srs_error_code(err);
srs_freep(err);
return ret;
}
return ret;
}
void SrsEncoder::on_unpublish()
{
trd->stop();
clear_engines();
}
// when error, encoder sleep for a while and retry.
#define SRS_RTMP_ENCODER_CIMS (3000)
srs_error_t SrsEncoder::cycle()
{
srs_error_t err = srs_success;
while (true) {
if ((err = do_cycle()) != srs_success) {
srs_warn("Encoder: Ignore error, %s", srs_error_desc(err).c_str());
srs_freep(err);
}
if ((err = trd->pull()) != srs_success) {
return srs_error_wrap(err, "encoder");
}
srs_usleep(SRS_RTMP_ENCODER_CIMS * 1000);
}
// kill ffmpeg when finished and it alive
std::vector<SrsFFMPEG*>::iterator it;
for (it = ffmpegs.begin(); it != ffmpegs.end(); ++it) {
SrsFFMPEG* ffmpeg = *it;
ffmpeg->stop();
}
return err;
}
srs_error_t SrsEncoder::do_cycle()
{
int ret = ERROR_SUCCESS;
srs_error_t err = srs_success;
std::vector<SrsFFMPEG*>::iterator it;
for (it = ffmpegs.begin(); it != ffmpegs.end(); ++it) {
SrsFFMPEG* ffmpeg = *it;
// start all ffmpegs.
if ((ret = ffmpeg->start()) != ERROR_SUCCESS) {
return srs_error_new(ret, "ffmpeg start");
}
// check ffmpeg status.
if ((ret = ffmpeg->cycle()) != ERROR_SUCCESS) {
return srs_error_new(ret, "ffmpeg cycle");
}
}
// pithy print
show_encode_log_message();
return err;
}
void SrsEncoder::clear_engines()
{
std::vector<SrsFFMPEG*>::iterator it;
for (it = ffmpegs.begin(); it != ffmpegs.end(); ++it) {
SrsFFMPEG* ffmpeg = *it;
std::string output = ffmpeg->output();
std::vector<std::string>::iterator tu_it;
tu_it = std::find(_transcoded_url.begin(), _transcoded_url.end(), output);
if (tu_it != _transcoded_url.end()) {
_transcoded_url.erase(tu_it);
}
srs_freep(ffmpeg);
}
ffmpegs.clear();
}
SrsFFMPEG* SrsEncoder::at(int index)
{
return ffmpegs[index];
}
int SrsEncoder::parse_scope_engines(SrsRequest* req)
{
int ret = ERROR_SUCCESS;
// parse all transcode engines.
SrsConfDirective* conf = NULL;
// parse vhost scope engines
std::string scope = "";
if ((conf = _srs_config->get_transcode(req->vhost, scope)) != NULL) {
if ((ret = parse_ffmpeg(req, conf)) != ERROR_SUCCESS) {
if (ret != ERROR_ENCODER_LOOP) {
srs_error("parse vhost scope=%s transcode engines failed. "
"ret=%d", scope.c_str(), ret);
}
return ret;
}
}
// parse app scope engines
scope = req->app;
if ((conf = _srs_config->get_transcode(req->vhost, scope)) != NULL) {
if ((ret = parse_ffmpeg(req, conf)) != ERROR_SUCCESS) {
if (ret != ERROR_ENCODER_LOOP) {
srs_error("parse app scope=%s transcode engines failed. "
"ret=%d", scope.c_str(), ret);
}
return ret;
}
}
// parse stream scope engines
scope += "/";
scope += req->stream;
if ((conf = _srs_config->get_transcode(req->vhost, scope)) != NULL) {
if ((ret = parse_ffmpeg(req, conf)) != ERROR_SUCCESS) {
if (ret != ERROR_ENCODER_LOOP) {
srs_error("parse stream scope=%s transcode engines failed. "
"ret=%d", scope.c_str(), ret);
}
return ret;
}
}
return ret;
}
int SrsEncoder::parse_ffmpeg(SrsRequest* req, SrsConfDirective* conf)
{
int ret = ERROR_SUCCESS;
srs_assert(conf);
// enabled
if (!_srs_config->get_transcode_enabled(conf)) {
srs_trace("ignore the disabled transcode: %s", conf->arg0().c_str());
return ret;
}
// ffmpeg
std::string ffmpeg_bin = _srs_config->get_transcode_ffmpeg(conf);
if (ffmpeg_bin.empty()) {
srs_trace("ignore the empty ffmpeg transcode: %s",
conf->arg0().c_str());
return ret;
}
// get all engines.
std::vector<SrsConfDirective*> engines = _srs_config->get_transcode_engines(conf);
if (engines.empty()) {
srs_trace("ignore the empty transcode engine: %s",
conf->arg0().c_str());
return ret;
}
// create engine
for (int i = 0; i < (int)engines.size(); i++) {
SrsConfDirective* engine = engines[i];
if (!_srs_config->get_engine_enabled(engine)) {
srs_trace("ignore the diabled transcode engine: %s %s",
conf->arg0().c_str(), engine->arg0().c_str());
continue;
}
SrsFFMPEG* ffmpeg = new SrsFFMPEG(ffmpeg_bin);
if ((ret = initialize_ffmpeg(ffmpeg, req, engine)) != ERROR_SUCCESS) {
srs_freep(ffmpeg);
if (ret != ERROR_ENCODER_LOOP) {
srs_error("invalid transcode engine: %s %s", conf->arg0().c_str(), engine->arg0().c_str());
}
return ret;
}
ffmpegs.push_back(ffmpeg);
}
return ret;
}
int SrsEncoder::initialize_ffmpeg(SrsFFMPEG* ffmpeg, SrsRequest* req, SrsConfDirective* engine)
{
int ret = ERROR_SUCCESS;
std::string input;
// input stream, from local.
// ie. rtmp://localhost:1935/live/livestream
input = "rtmp://";
input += SRS_CONSTS_LOCALHOST;
input += ":";
input += srs_int2str(req->port);
input += "/";
input += req->app;
input += "?vhost=";
input += req->vhost;
input += "/";
input += req->stream;
// stream name: vhost/app/stream for print
input_stream_name = req->vhost;
input_stream_name += "/";
input_stream_name += req->app;
input_stream_name += "/";
input_stream_name += req->stream;
std::string output = _srs_config->get_engine_output(engine);
// output stream, to other/self server
// ie. rtmp://localhost:1935/live/livestream_sd
output = srs_string_replace(output, "[vhost]", req->vhost);
output = srs_string_replace(output, "[port]", srs_int2str(req->port));
output = srs_string_replace(output, "[app]", req->app);
output = srs_string_replace(output, "[stream]", req->stream);
output = srs_string_replace(output, "[engine]", engine->arg0());
std::string log_file = SRS_CONSTS_NULL_FILE; // disabled
// write ffmpeg info to log file.
if (_srs_config->get_ffmpeg_log_enabled()) {
log_file = _srs_config->get_ffmpeg_log_dir();
log_file += "/";
log_file += "ffmpeg-encoder";
log_file += "-";
log_file += req->vhost;
log_file += "-";
log_file += req->app;
log_file += "-";
log_file += req->stream;
if (!engine->args.empty()) {
log_file += "-";
log_file += engine->arg0();
}
log_file += ".log";
}
// important: loop check, donot transcode again.
std::vector<std::string>::iterator it;
it = std::find(_transcoded_url.begin(), _transcoded_url.end(), input);
if (it != _transcoded_url.end()) {
ret = ERROR_ENCODER_LOOP;
srs_trace("detect a loop cycle, input=%s, output=%s, ignore it. ret=%d",
input.c_str(), output.c_str(), ret);
return ret;
}
_transcoded_url.push_back(output);
if ((ret = ffmpeg->initialize(input, output, log_file)) != ERROR_SUCCESS) {
return ret;
}
if ((ret = ffmpeg->initialize_transcode(engine)) != ERROR_SUCCESS) {
return ret;
}
return ret;
}
void SrsEncoder::show_encode_log_message()
{
pprint->elapse();
// reportable
if (pprint->can_print()) {
// TODO: FIXME: show more info.
srs_trace("-> " SRS_CONSTS_LOG_ENCODER " time=%" PRId64 ", encoders=%d, input=%s",
pprint->age(), (int)ffmpegs.size(), input_stream_name.c_str());
}
}
#endif