| 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 |
#ifdef HAVE_CONFIG_H |
|---|
| 28 |
# include "config.h" |
|---|
| 29 |
#endif |
|---|
| 30 |
|
|---|
| 31 |
#include <vlc_common.h> |
|---|
| 32 |
#include <vlc_plugin.h> |
|---|
| 33 |
#include <vlc_sout.h> |
|---|
| 34 |
#include <vlc_vout.h> |
|---|
| 35 |
#include <vlc_filter.h> |
|---|
| 36 |
#include <vlc_osd.h> |
|---|
| 37 |
|
|---|
| 38 |
#include <ctype.h> |
|---|
| 39 |
#include <fcntl.h> |
|---|
| 40 |
|
|---|
| 41 |
#include "dynamicoverlay.h" |
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
|
|---|
| 45 |
|
|---|
| 46 |
static int Create( vlc_object_t * ); |
|---|
| 47 |
static void Destroy( vlc_object_t * ); |
|---|
| 48 |
static subpicture_t *Filter( filter_t *, mtime_t ); |
|---|
| 49 |
|
|---|
| 50 |
static int AdjustCallback( vlc_object_t *p_this, char const *psz_var, |
|---|
| 51 |
vlc_value_t oldval, vlc_value_t newval, |
|---|
| 52 |
void *p_data ); |
|---|
| 53 |
|
|---|
| 54 |
|
|---|
| 55 |
|
|---|
| 56 |
|
|---|
| 57 |
|
|---|
| 58 |
#define INPUT_TEXT N_("Input FIFO") |
|---|
| 59 |
#define INPUT_LONGTEXT N_("FIFO which will be read for commands") |
|---|
| 60 |
|
|---|
| 61 |
#define OUTPUT_TEXT N_("Output FIFO") |
|---|
| 62 |
#define OUTPUT_LONGTEXT N_("FIFO which will be written to for responses") |
|---|
| 63 |
|
|---|
| 64 |
vlc_module_begin(); |
|---|
| 65 |
set_description( N_("Dynamic video overlay") ); |
|---|
| 66 |
set_shortname( N_("Overlay" )); |
|---|
| 67 |
set_category( CAT_VIDEO ); |
|---|
| 68 |
set_subcategory( SUBCAT_VIDEO_VFILTER ); |
|---|
| 69 |
set_capability( "sub filter", 0 ); |
|---|
| 70 |
|
|---|
| 71 |
add_file( "overlay-input", NULL, NULL, INPUT_TEXT, INPUT_LONGTEXT, |
|---|
| 72 |
false ); |
|---|
| 73 |
add_file( "overlay-output", NULL, NULL, OUTPUT_TEXT, OUTPUT_LONGTEXT, |
|---|
| 74 |
false ); |
|---|
| 75 |
|
|---|
| 76 |
add_shortcut( "overlay" ); |
|---|
| 77 |
set_callbacks( Create, Destroy ); |
|---|
| 78 |
vlc_module_end(); |
|---|
| 79 |
|
|---|
| 80 |
static const char *const ppsz_filter_options[] = { |
|---|
| 81 |
"input", "output", NULL |
|---|
| 82 |
}; |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 |
|
|---|
| 89 |
static int Create( vlc_object_t *p_this ) |
|---|
| 90 |
{ |
|---|
| 91 |
filter_t *p_filter = (filter_t *)p_this; |
|---|
| 92 |
filter_sys_t *p_sys; |
|---|
| 93 |
|
|---|
| 94 |
|
|---|
| 95 |
p_filter->p_sys = malloc( sizeof( filter_sys_t ) ); |
|---|
| 96 |
if( p_filter->p_sys == NULL ) |
|---|
| 97 |
return VLC_ENOMEM; |
|---|
| 98 |
p_sys = p_filter->p_sys; |
|---|
| 99 |
|
|---|
| 100 |
BufferInit( &p_sys->input ); |
|---|
| 101 |
BufferInit( &p_sys->output ); |
|---|
| 102 |
QueueInit( &p_sys->atomic ); |
|---|
| 103 |
QueueInit( &p_sys->pending ); |
|---|
| 104 |
QueueInit( &p_sys->processed ); |
|---|
| 105 |
ListInit( &p_sys->overlays ); |
|---|
| 106 |
|
|---|
| 107 |
p_sys->i_inputfd = -1; |
|---|
| 108 |
p_sys->i_outputfd = -1; |
|---|
| 109 |
p_sys->b_updated = true; |
|---|
| 110 |
p_sys->b_atomic = false; |
|---|
| 111 |
|
|---|
| 112 |
p_filter->pf_sub_filter = Filter; |
|---|
| 113 |
|
|---|
| 114 |
config_ChainParse( p_filter, "overlay-", ppsz_filter_options, |
|---|
| 115 |
p_filter->p_cfg ); |
|---|
| 116 |
|
|---|
| 117 |
p_sys->psz_inputfile = var_CreateGetStringCommand( p_filter, |
|---|
| 118 |
"overlay-input" ); |
|---|
| 119 |
p_sys->psz_outputfile = var_CreateGetStringCommand( p_filter, |
|---|
| 120 |
"overlay-output" ); |
|---|
| 121 |
|
|---|
| 122 |
var_AddCallback( p_filter, "overlay-input", AdjustCallback, p_sys ); |
|---|
| 123 |
var_AddCallback( p_filter, "overlay-output", AdjustCallback, p_sys ); |
|---|
| 124 |
|
|---|
| 125 |
RegisterCommand( p_filter ); |
|---|
| 126 |
return VLC_SUCCESS; |
|---|
| 127 |
} |
|---|
| 128 |
|
|---|
| 129 |
|
|---|
| 130 |
|
|---|
| 131 |
|
|---|
| 132 |
|
|---|
| 133 |
|
|---|
| 134 |
static void Destroy( vlc_object_t *p_this ) |
|---|
| 135 |
{ |
|---|
| 136 |
filter_t *p_filter = (filter_t *)p_this; |
|---|
| 137 |
|
|---|
| 138 |
BufferDestroy( &p_filter->p_sys->input ); |
|---|
| 139 |
BufferDestroy( &p_filter->p_sys->output ); |
|---|
| 140 |
QueueDestroy( &p_filter->p_sys->atomic ); |
|---|
| 141 |
QueueDestroy( &p_filter->p_sys->pending ); |
|---|
| 142 |
QueueDestroy( &p_filter->p_sys->processed ); |
|---|
| 143 |
ListDestroy( &p_filter->p_sys->overlays ); |
|---|
| 144 |
UnregisterCommand( p_filter ); |
|---|
| 145 |
|
|---|
| 146 |
free( p_filter->p_sys->psz_inputfile ); |
|---|
| 147 |
free( p_filter->p_sys->psz_outputfile ); |
|---|
| 148 |
free( p_filter->p_sys ); |
|---|
| 149 |
} |
|---|
| 150 |
|
|---|
| 151 |
|
|---|
| 152 |
|
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
|
|---|
| 156 |
|
|---|
| 157 |
|
|---|
| 158 |
static subpicture_t *Filter( filter_t *p_filter, mtime_t date ) |
|---|
| 159 |
{ |
|---|
| 160 |
filter_sys_t *p_sys = p_filter->p_sys; |
|---|
| 161 |
|
|---|
| 162 |
|
|---|
| 163 |
if( p_sys->i_inputfd == -1 ) |
|---|
| 164 |
{ |
|---|
| 165 |
p_sys->i_inputfd = open( p_sys->psz_inputfile, O_RDONLY | O_NONBLOCK ); |
|---|
| 166 |
if( p_sys->i_inputfd == -1 ) |
|---|
| 167 |
{ |
|---|
| 168 |
msg_Warn( p_filter, "Failed to grab input file: %s (%s)", |
|---|
| 169 |
p_sys->psz_inputfile, strerror( errno ) ); |
|---|
| 170 |
} |
|---|
| 171 |
else |
|---|
| 172 |
{ |
|---|
| 173 |
msg_Info( p_filter, "Grabbed input file: %s", |
|---|
| 174 |
p_sys->psz_inputfile ); |
|---|
| 175 |
} |
|---|
| 176 |
} |
|---|
| 177 |
|
|---|
| 178 |
if( p_sys->i_outputfd == -1 ) |
|---|
| 179 |
{ |
|---|
| 180 |
p_sys->i_outputfd = open( p_sys->psz_outputfile, |
|---|
| 181 |
O_WRONLY | O_NONBLOCK ); |
|---|
| 182 |
if( p_sys->i_outputfd == -1 ) |
|---|
| 183 |
{ |
|---|
| 184 |
if( errno != ENXIO ) |
|---|
| 185 |
{ |
|---|
| 186 |
msg_Warn( p_filter, "Failed to grab output file: %s (%s)", |
|---|
| 187 |
p_sys->psz_outputfile, strerror( errno ) ); |
|---|
| 188 |
} |
|---|
| 189 |
} |
|---|
| 190 |
else |
|---|
| 191 |
{ |
|---|
| 192 |
msg_Info( p_filter, "Grabbed output file: %s", |
|---|
| 193 |
p_sys->psz_outputfile ); |
|---|
| 194 |
} |
|---|
| 195 |
} |
|---|
| 196 |
|
|---|
| 197 |
|
|---|
| 198 |
if( p_sys->i_inputfd != -1 ) |
|---|
| 199 |
{ |
|---|
| 200 |
char p_buffer[1024]; |
|---|
| 201 |
ssize_t i_len = read( p_sys->i_inputfd, p_buffer, 1024 ); |
|---|
| 202 |
if( i_len == -1 ) |
|---|
| 203 |
{ |
|---|
| 204 |
|
|---|
| 205 |
if( errno != EAGAIN ) |
|---|
| 206 |
{ |
|---|
| 207 |
msg_Warn( p_filter, "Error on input file: %s", |
|---|
| 208 |
strerror( errno ) ); |
|---|
| 209 |
close( p_sys->i_inputfd ); |
|---|
| 210 |
p_sys->i_inputfd = -1; |
|---|
| 211 |
} |
|---|
| 212 |
} |
|---|
| 213 |
else if( i_len == 0 ) |
|---|
| 214 |
{ |
|---|
| 215 |
|
|---|
| 216 |
} |
|---|
| 217 |
else |
|---|
| 218 |
{ |
|---|
| 219 |
BufferAdd( &p_sys->input, p_buffer, i_len ); |
|---|
| 220 |
} |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
|
|---|
| 224 |
char *p_end, *p_cmd; |
|---|
| 225 |
while( ( p_end = memchr( p_sys->input.p_begin, '\n', |
|---|
| 226 |
p_sys->input.i_length ) ) ) |
|---|
| 227 |
{ |
|---|
| 228 |
commanddesc_t *p_cur = NULL; |
|---|
| 229 |
bool b_found = false; |
|---|
| 230 |
size_t i_index = 0; |
|---|
| 231 |
|
|---|
| 232 |
*p_end = '\0'; |
|---|
| 233 |
p_cmd = BufferGetToken( &p_sys->input ); |
|---|
| 234 |
|
|---|
| 235 |
msg_Info( p_filter, "Search command: %s", p_cmd ); |
|---|
| 236 |
for( i_index = 0; i_index < p_sys->i_commands; i_index++ ) |
|---|
| 237 |
{ |
|---|
| 238 |
p_cur = p_sys->pp_commands[i_index]; |
|---|
| 239 |
if( !strncmp( p_cur->psz_command, p_cmd, strlen(p_cur->psz_command) ) ) |
|---|
| 240 |
{ |
|---|
| 241 |
p_cmd[strlen(p_cur->psz_command)] = '\0'; |
|---|
| 242 |
b_found = true; |
|---|
| 243 |
break; |
|---|
| 244 |
} |
|---|
| 245 |
} |
|---|
| 246 |
|
|---|
| 247 |
if( !b_found ) |
|---|
| 248 |
{ |
|---|
| 249 |
|
|---|
| 250 |
msg_Err( p_filter, "Got invalid command: %s", p_cmd ); |
|---|
| 251 |
BufferPrintf( &p_sys->output, "FAILURE: %d Invalid Command\n", VLC_EGENERIC ); |
|---|
| 252 |
} |
|---|
| 253 |
else |
|---|
| 254 |
{ |
|---|
| 255 |
msg_Info( p_filter, "Got valid command: %s", p_cmd ); |
|---|
| 256 |
|
|---|
| 257 |
command_t *p_cmddesc = malloc( sizeof( command_t ) ); |
|---|
| 258 |
if( !p_cmddesc ) |
|---|
| 259 |
return NULL; |
|---|
| 260 |
|
|---|
| 261 |
p_cmd = p_cmd + strlen(p_cur->psz_command) +1; |
|---|
| 262 |
p_cmddesc->p_command = p_cur; |
|---|
| 263 |
p_cmddesc->p_command->pf_parser( p_cmd, p_end, |
|---|
| 264 |
&p_cmddesc->params ); |
|---|
| 265 |
|
|---|
| 266 |
if( ( p_cmddesc->p_command->b_atomic == true ) && |
|---|
| 267 |
( p_sys->b_atomic == true ) ) |
|---|
| 268 |
QueueEnqueue( &p_sys->atomic, p_cmddesc ); |
|---|
| 269 |
else |
|---|
| 270 |
QueueEnqueue( &p_sys->pending, p_cmddesc ); |
|---|
| 271 |
} |
|---|
| 272 |
|
|---|
| 273 |
BufferDel( &p_sys->input, p_end - p_sys->input.p_begin + 1 ); |
|---|
| 274 |
} |
|---|
| 275 |
|
|---|
| 276 |
|
|---|
| 277 |
command_t *p_command = NULL; |
|---|
| 278 |
while( (p_command = QueueDequeue( &p_sys->pending )) ) |
|---|
| 279 |
{ |
|---|
| 280 |
p_command->i_status = |
|---|
| 281 |
p_command->p_command->pf_execute( p_filter, &p_command->params, |
|---|
| 282 |
&p_command->results ); |
|---|
| 283 |
QueueEnqueue( &p_sys->processed, p_command ); |
|---|
| 284 |
} |
|---|
| 285 |
|
|---|
| 286 |
|
|---|
| 287 |
while( (p_command = QueueDequeue( &p_sys->processed )) ) |
|---|
| 288 |
{ |
|---|
| 289 |
if( p_command->i_status == VLC_SUCCESS ) |
|---|
| 290 |
{ |
|---|
| 291 |
const char *psz_success = "SUCCESS:"; |
|---|
| 292 |
const char *psz_nl = "\n"; |
|---|
| 293 |
BufferAdd( &p_sys->output, psz_success, 8 ); |
|---|
| 294 |
p_command->p_command->pf_unparse( &p_command->results, |
|---|
| 295 |
&p_sys->output ); |
|---|
| 296 |
BufferAdd( &p_sys->output, psz_nl, 1 ); |
|---|
| 297 |
} |
|---|
| 298 |
else |
|---|
| 299 |
{ |
|---|
| 300 |
BufferPrintf( &p_sys->output, "FAILURE: %d\n", |
|---|
| 301 |
p_command->i_status ); |
|---|
| 302 |
} |
|---|
| 303 |
} |
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
if( p_sys->i_outputfd != -1 ) |
|---|
| 307 |
{ |
|---|
| 308 |
ssize_t i_len = write( p_sys->i_outputfd, p_sys->output.p_begin, |
|---|
| 309 |
p_sys->output.i_length ); |
|---|
| 310 |
if( i_len == -1 ) |
|---|
| 311 |
{ |
|---|
| 312 |
|
|---|
| 313 |
if( errno != EAGAIN ) |
|---|
| 314 |
{ |
|---|
| 315 |
msg_Warn( p_filter, "Error on output file: %s", |
|---|
| 316 |
strerror( errno ) ); |
|---|
| 317 |
close( p_sys->i_outputfd ); |
|---|
| 318 |
p_sys->i_outputfd = -1; |
|---|
| 319 |
} |
|---|
| 320 |
} |
|---|
| 321 |
else |
|---|
| 322 |
{ |
|---|
| 323 |
BufferDel( &p_sys->output, i_len ); |
|---|
| 324 |
} |
|---|
| 325 |
} |
|---|
| 326 |
|
|---|
| 327 |
if( p_sys->b_updated == false ) |
|---|
| 328 |
return NULL; |
|---|
| 329 |
|
|---|
| 330 |
subpicture_t *p_spu = NULL; |
|---|
| 331 |
overlay_t *p_overlay = NULL; |
|---|
| 332 |
|
|---|
| 333 |
p_spu = p_filter->pf_sub_buffer_new( p_filter ); |
|---|
| 334 |
if( !p_spu ) |
|---|
| 335 |
{ |
|---|
| 336 |
msg_Err( p_filter, "cannot allocate subpicture" ); |
|---|
| 337 |
return NULL; |
|---|
| 338 |
} |
|---|
| 339 |
|
|---|
| 340 |
p_spu->b_absolute = true; |
|---|
| 341 |
p_spu->i_start = date; |
|---|
| 342 |
p_spu->i_stop = 0; |
|---|
| 343 |
p_spu->b_ephemer = true; |
|---|
| 344 |
|
|---|
| 345 |
subpicture_region_t **pp_region = &p_spu->p_region; |
|---|
| 346 |
while( (p_overlay = ListWalk( &p_sys->overlays )) ) |
|---|
| 347 |
{ |
|---|
| 348 |
subpicture_region_t *p_region; |
|---|
| 349 |
|
|---|
| 350 |
*pp_region = p_region = subpicture_region_New( &p_overlay->format ); |
|---|
| 351 |
if( !p_region ) |
|---|
| 352 |
break; |
|---|
| 353 |
|
|---|
| 354 |
msg_Dbg( p_filter, "Displaying overlay: %4.4s, %d, %d, %d", |
|---|
| 355 |
(char*)&p_overlay->format.i_chroma, p_overlay->i_x, p_overlay->i_y, |
|---|
| 356 |
p_overlay->i_alpha ); |
|---|
| 357 |
|
|---|
| 358 |
if( p_overlay->format.i_chroma == VLC_FOURCC('T','E','X','T') ) |
|---|
| 359 |
{ |
|---|
| 360 |
p_region->psz_text = strdup( p_overlay->data.p_text ); |
|---|
| 361 |
p_region->p_style = malloc( sizeof(struct text_style_t) ); |
|---|
| 362 |
if( p_region->p_style ) |
|---|
| 363 |
*p_region->p_style = p_overlay->fontstyle; |
|---|
| 364 |
} |
|---|
| 365 |
else |
|---|
| 366 |
{ |
|---|
| 367 |
|
|---|
| 368 |
picture_Copy( p_region->p_picture, p_overlay->data.p_pic ); |
|---|
| 369 |
} |
|---|
| 370 |
p_region->i_x = p_overlay->i_x; |
|---|
| 371 |
p_region->i_y = p_overlay->i_y; |
|---|
| 372 |
p_region->i_align = OSD_ALIGN_LEFT | OSD_ALIGN_TOP; |
|---|
| 373 |
p_region->i_alpha = p_overlay->i_alpha; |
|---|
| 374 |
pp_region = &p_region->p_next; |
|---|
| 375 |
} |
|---|
| 376 |
|
|---|
| 377 |
p_sys->b_updated = false; |
|---|
| 378 |
return p_spu; |
|---|
| 379 |
} |
|---|
| 380 |
|
|---|
| 381 |
static int AdjustCallback( vlc_object_t *p_this, char const *psz_var, |
|---|
| 382 |
vlc_value_t oldval, vlc_value_t newval, |
|---|
| 383 |
void *p_data ) |
|---|
| 384 |
{ |
|---|
| 385 |
filter_sys_t *p_sys = (filter_sys_t *)p_data; |
|---|
| 386 |
VLC_UNUSED(p_this); VLC_UNUSED(oldval); |
|---|
| 387 |
|
|---|
| 388 |
if( !strncmp( psz_var, "overlay-input", 13 ) ) |
|---|
| 389 |
p_sys->psz_inputfile = newval.psz_string; |
|---|
| 390 |
else if( !strncmp( psz_var, "overlay-output", 14 ) ) |
|---|
| 391 |
p_sys->psz_outputfile = newval.psz_string; |
|---|
| 392 |
|
|---|
| 393 |
return VLC_EGENERIC; |
|---|
| 394 |
} |
|---|