-
Notifications
You must be signed in to change notification settings - Fork 1
/
transcode.c
360 lines (323 loc) · 7.81 KB
/
transcode.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
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
/* MiniDLNA media server
* Copyright (C) 2012 Lukas Jirkovsky
*
* This file is part of MiniDLNA.
*
* MiniDLNA is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* MiniDLNA 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with MiniDLNA. If not, see <http://www.gnu.org/licenses/>.
*/
#include <unistd.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <errno.h>
#include <string.h>
#include "config.h"
#include "libav.h"
#include "upnpglobalvars.h"
#include "minidlnatypes.h"
#include "transcode.h"
#include "utils.h"
#include "log.h"
#define READ 0
#define WRITE 1
pid_t
popenvp(const char* file, char * const argv[], int *pipehandle)
{
int fildes[2];
pid_t pid;
/* create a pipe */
if(pipe(fildes)<0)
{
DPRINTF(E_ERROR, L_TRANSCODE, "Cannot create pipe (%s)\n", strerror(errno));
return -1;
}
/* Invoke processs */
pid=fork();
if (pid < 0)
{
DPRINTF(E_ERROR, L_TRANSCODE, "Fork failed (%s)\n", strerror(errno));
close(fildes[WRITE]);
close(fildes[READ]);
return pid;
}
/* child */
if(pid == 0)
{
close(fildes[READ]);
dup2(fildes[WRITE], WRITE);
if (execvp(file, argv) < 0) {
DPRINTF(E_ERROR, L_TRANSCODE, "Exec failed (%s)\n", strerror(errno));
close(fildes[WRITE]);
exit(1);
}
}
else
{
close(fildes[WRITE]);
}
*pipehandle = fildes[READ];
return pid;
}
/* NOTE: Partially based on Hiero's code */
pid_t
exec_transcode(char *transcoder, char *source_path, int offset, int end_offset, int *pipehandle)
{
pid_t pid;
int retval;
char position[12], duration[12];
static struct sigaction sa;
char * args[5];
sprintf(position, "%d.%03d", offset/1000, offset%1000);
sprintf(duration, "%d.%03d", (end_offset - offset + 1)/1000, (end_offset - offset + 1)%1000);
args[0] = transcoder;
args[1] = source_path;
args[2] = position;
args[3] = duration;
args[4] = NULL;
/* Invoke processs */
pid = popenvp(args[0], args, pipehandle);
return pid;
}
pid_t
exec_transcode_img(char *transcoder, char *source_path, char *dest_path)
{
pid_t pid;
int status, retval;
static struct sigaction sa;
char * args[4];
args[0] = transcoder;
args[1] = source_path;
args[2] = dest_path;
args[3] = NULL;
/* Invoke processs */
pid=fork();
if (pid < 0)
{
DPRINTF(E_ERROR, L_TRANSCODE, "Fork failed (%s)\n", strerror(errno));
return pid;
}
/* child */
if(pid == 0)
{
if (execvp(args[0], args) < 0) {
DPRINTF(E_ERROR, L_TRANSCODE, "Exec failed (%s)\n", strerror(errno));
exit(1);
}
}
return pid;
}
int
needs_transcode_image(const char* path, enum client_types client)
{
int i;
struct transcode_list_format_s *transcode_format_it = NULL;
struct transcode_info_s *clients_info[] = {client_types[0].transcode_info, client_types[client].transcode_info};
for ( i = 0; i < 2; i++ )
{
if (clients_info[i])
{
transcode_format_it = clients_info[i]->image_formats;
/* test for the reserved value "all" */
if ( transcode_format_it && strcmp(transcode_format_it->value, "all") == 0 )
{
return 1;
}
while( transcode_format_it )
{
if ( ends_with(path, transcode_format_it->value) )
{
return 1;
}
transcode_format_it = transcode_format_it->next;
}
}
}
return 0;
}
int
needs_transcode_audio(const char* path, enum client_types client)
{
int ret;
AVFormatContext *ctx = NULL;
struct AVCodecParameters *ac = NULL;
int i;
struct transcode_list_format_s *transcode_format_it = NULL;
const struct AVCodec *codec = NULL;
struct transcode_info_s *clients_info[] = {client_types[0].transcode_info, client_types[client].transcode_info};
/* prepare ffmpeg */
ret = lav_open(&ctx, path);
if( ret != 0 )
{
char err[128];
av_strerror(ret, err, sizeof(err));
DPRINTF(E_ERROR, L_TRANSCODE, "Opening %s failed! [%s]\n", path, err);
return -1;
}
for( i=0; i<ctx->nb_streams; i++)
{
if( ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO )
{
ac = ctx->streams[i]->codecpar;
break;
}
}
if ( ac )
{
codec = avcodec_find_decoder(ac->codec_id);
for ( i = 0; i < 2; i++ )
{
if (clients_info[i])
{
transcode_format_it = clients_info[i]->audio_codecs;
/* test for the reserved value "all" */
if ( transcode_format_it && strcmp(transcode_format_it->value, "all") == 0 )
{
lav_close(ctx);
return 1;
}
while( transcode_format_it )
{
if ( strcmp(codec->name, transcode_format_it->value) == 0 )
{
lav_close(ctx);
return 1;
}
transcode_format_it = transcode_format_it->next;
}
}
}
}
lav_close(ctx);
return 0;
}
int
needs_transcode_video(const char* path, enum client_types client)
{
int ret;
AVFormatContext *ctx = NULL;
const struct AVCodec *codec = NULL;
int audio_stream = -1, video_stream = -1;
struct AVCodecParameters *ac = NULL;
struct AVCodecParameters *vc = NULL;
int i;
struct transcode_list_format_s *transcode_format_it = NULL;
struct transcode_info_s *clients_info[] = {client_types[0].transcode_info, client_types[client].transcode_info};
/* prepare ffmpeg */
ret = lav_open(&ctx, path);
if( ret != 0 )
{
char err[128];
av_strerror(ret, err, sizeof(err));
DPRINTF(E_ERROR, L_TRANSCODE, "Opening %s failed! [%s]\n", path, err);
return -1;
}
for( i=0; i<ctx->nb_streams; i++)
{
if( audio_stream == -1 &&
ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO )
{
audio_stream = i;
ac = ctx->streams[audio_stream]->codecpar;
continue;
}
else if( video_stream == -1 &&
ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO )
{
video_stream = i;
vc = ctx->streams[video_stream]->codecpar;
continue;
}
}
if ( vc == NULL )
{
/* This must not be a video file. */
DPRINTF(E_ERROR, L_TRANSCODE, "File does not contain a video stream.\n");
lav_close(ctx);
return 0;
}
/* check if the file needs to be transcoded */
/* check the container */
for ( i = 0; i < 2; i++ )
{
if (clients_info[i])
{
transcode_format_it = clients_info[i]->video_containers;
/* test for the reserved value "all" */
if ( transcode_format_it && strcmp(transcode_format_it->value, "all") == 0 )
{
lav_close(ctx);
return 1;
}
while( transcode_format_it )
{
if ( strcmp(ctx->iformat->name, transcode_format_it->value) == 0 )
{
lav_close(ctx);
return 1;
}
transcode_format_it = transcode_format_it->next;
}
}
}
/* check the video codec */
for ( i = 0; i < 2; i++ )
{
if (clients_info[i])
{
transcode_format_it = clients_info[i]->video_codecs;
/* test for the reserved value "all" */
if ( transcode_format_it && strcmp(transcode_format_it->value, "all") == 0 )
{
lav_close(ctx);
return 1;
}
codec = avcodec_find_decoder(vc->codec_id);
while( transcode_format_it )
{
if ( strcmp(codec->name, transcode_format_it->value) == 0 )
{
lav_close(ctx);
return 1;
}
transcode_format_it = transcode_format_it->next;
}
}
}
/* check the audio codec */
for ( i = 0; i < 2; i++ )
{
if (clients_info[i])
{
transcode_format_it = clients_info[i]->audio_codecs;
/* test for the reserved value "all" */
if ( transcode_format_it && strcmp(transcode_format_it->value, "all") == 0 )
{
lav_close(ctx);
return 1;
}
codec = avcodec_find_decoder(ac->codec_id);
while( transcode_format_it )
{
if ( strcmp(codec->name, transcode_format_it->value) == 0 )
{
lav_close(ctx);
return 1;
}
transcode_format_it = transcode_format_it->next;
}
}
}
lav_close(ctx);
return 0;
}