| 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 |
#ifdef HAVE_CONFIG_H |
|---|
| 27 |
# include "config.h" |
|---|
| 28 |
#endif |
|---|
| 29 |
|
|---|
| 30 |
#include <vlc_common.h> |
|---|
| 31 |
#include <vlc_plugin.h> |
|---|
| 32 |
#include <vlc_sout.h> |
|---|
| 33 |
|
|---|
| 34 |
#include <vlc_network.h> |
|---|
| 35 |
#include <vlc_url.h> |
|---|
| 36 |
#include <vlc_block.h> |
|---|
| 37 |
|
|---|
| 38 |
#include "../access/rtmp/rtmp_amf_flv.h" |
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
|
|---|
| 43 |
|
|---|
| 44 |
#define RTMP_CONNECT_TEXT N_( "Active TCP connection" ) |
|---|
| 45 |
#define RTMP_CONNECT_LONGTEXT N_( \ |
|---|
| 46 |
"If enabled, VLC will connect to a remote destination instead of " \ |
|---|
| 47 |
"waiting for an incoming connection." ) |
|---|
| 48 |
|
|---|
| 49 |
static int Open ( vlc_object_t * ); |
|---|
| 50 |
static void Close( vlc_object_t * ); |
|---|
| 51 |
|
|---|
| 52 |
#define SOUT_CFG_PREFIX "sout-rtmp-" |
|---|
| 53 |
|
|---|
| 54 |
vlc_module_begin(); |
|---|
| 55 |
set_description( N_("RTMP stream output") ); |
|---|
| 56 |
set_shortname( N_("RTMP" ) ); |
|---|
| 57 |
set_capability( "sout access", 50 ); |
|---|
| 58 |
set_category( CAT_SOUT ); |
|---|
| 59 |
set_subcategory( SUBCAT_SOUT_STREAM ); |
|---|
| 60 |
add_shortcut( "rtmp" ); |
|---|
| 61 |
set_callbacks( Open, Close ); |
|---|
| 62 |
add_bool( "rtmp-connect", false, NULL, RTMP_CONNECT_TEXT, |
|---|
| 63 |
RTMP_CONNECT_LONGTEXT, false ); |
|---|
| 64 |
vlc_module_end(); |
|---|
| 65 |
|
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
|
|---|
| 69 |
static ssize_t Write( sout_access_out_t *, block_t * ); |
|---|
| 70 |
static int Seek ( sout_access_out_t *, off_t ); |
|---|
| 71 |
static void* ThreadControl( vlc_object_t * ); |
|---|
| 72 |
|
|---|
| 73 |
struct sout_access_out_sys_t |
|---|
| 74 |
{ |
|---|
| 75 |
int active; |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
rtmp_control_thread_t *p_thread; |
|---|
| 79 |
}; |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
|
|---|
| 83 |
|
|---|
| 84 |
static int Open( vlc_object_t *p_this ) |
|---|
| 85 |
{ |
|---|
| 86 |
sout_access_out_t *p_access = (sout_access_out_t *) p_this; |
|---|
| 87 |
sout_access_out_sys_t *p_sys; |
|---|
| 88 |
char *psz, *p; |
|---|
| 89 |
int length_path, length_media_name; |
|---|
| 90 |
int i; |
|---|
| 91 |
|
|---|
| 92 |
if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) ) |
|---|
| 93 |
{ |
|---|
| 94 |
msg_Err( p_access, "not enough memory" ); |
|---|
| 95 |
return VLC_ENOMEM; |
|---|
| 96 |
} |
|---|
| 97 |
p_access->p_sys = p_sys; |
|---|
| 98 |
|
|---|
| 99 |
p_sys->p_thread = |
|---|
| 100 |
vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) ); |
|---|
| 101 |
if( !p_sys->p_thread ) |
|---|
| 102 |
{ |
|---|
| 103 |
msg_Err( p_access, "out of memory" ); |
|---|
| 104 |
return VLC_ENOMEM; |
|---|
| 105 |
} |
|---|
| 106 |
vlc_object_attach( p_sys->p_thread, p_access ); |
|---|
| 107 |
|
|---|
| 108 |
|
|---|
| 109 |
p = psz = strdup( p_access->psz_path ); |
|---|
| 110 |
while( ( p = strchr( p, ' ' ) ) != NULL ) |
|---|
| 111 |
*p = '+'; |
|---|
| 112 |
vlc_UrlParse( &p_sys->p_thread->url, psz, 0 ); |
|---|
| 113 |
free( psz ); |
|---|
| 114 |
|
|---|
| 115 |
if( p_sys->p_thread->url.psz_host == NULL |
|---|
| 116 |
|| *p_sys->p_thread->url.psz_host == '\0' ) |
|---|
| 117 |
{ |
|---|
| 118 |
msg_Warn( p_access, "invalid host" ); |
|---|
| 119 |
goto error; |
|---|
| 120 |
} |
|---|
| 121 |
|
|---|
| 122 |
if( p_sys->p_thread->url.i_port <= 0 ) |
|---|
| 123 |
p_sys->p_thread->url.i_port = 1935; |
|---|
| 124 |
|
|---|
| 125 |
if ( p_sys->p_thread->url.psz_path == NULL ) |
|---|
| 126 |
{ |
|---|
| 127 |
msg_Warn( p_access, "invalid path" ); |
|---|
| 128 |
goto error; |
|---|
| 129 |
} |
|---|
| 130 |
|
|---|
| 131 |
length_path = strlen( p_sys->p_thread->url.psz_path ); |
|---|
| 132 |
length_media_name = strlen( strrchr( p_sys->p_thread->url.psz_path, '/' ) ) - 1; |
|---|
| 133 |
|
|---|
| 134 |
p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 ); |
|---|
| 135 |
p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) ); |
|---|
| 136 |
|
|---|
| 137 |
msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'", |
|---|
| 138 |
p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path ); |
|---|
| 139 |
|
|---|
| 140 |
if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username ) |
|---|
| 141 |
{ |
|---|
| 142 |
msg_Dbg( p_access, " user='%s', pwd='%s'", |
|---|
| 143 |
p_sys->p_thread->url.psz_username, p_sys->p_thread->url.psz_password ); |
|---|
| 144 |
} |
|---|
| 145 |
|
|---|
| 146 |
|
|---|
| 147 |
p_sys->p_thread->b_die = 0; |
|---|
| 148 |
p_sys->p_thread->b_error= 0; |
|---|
| 149 |
p_sys->p_thread->p_fifo_input = block_FifoNew(); |
|---|
| 150 |
p_sys->p_thread->p_empty_blocks = block_FifoNew(); |
|---|
| 151 |
p_sys->p_thread->has_audio = 0; |
|---|
| 152 |
p_sys->p_thread->has_video = 0; |
|---|
| 153 |
p_sys->p_thread->metadata_received = 0; |
|---|
| 154 |
p_sys->p_thread->first_media_packet = 1; |
|---|
| 155 |
p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; |
|---|
| 156 |
|
|---|
| 157 |
p_sys->p_thread->flv_body = rtmp_body_new( -1 ); |
|---|
| 158 |
p_sys->p_thread->flv_length_body = 0; |
|---|
| 159 |
|
|---|
| 160 |
p_sys->p_thread->chunk_size_recv = 128; |
|---|
| 161 |
p_sys->p_thread->chunk_size_send = 128; |
|---|
| 162 |
for(i = 0; i < 64; i++) |
|---|
| 163 |
{ |
|---|
| 164 |
memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) ); |
|---|
| 165 |
p_sys->p_thread->rtmp_headers_send[i].length_header = -1; |
|---|
| 166 |
p_sys->p_thread->rtmp_headers_send[i].stream_index = -1; |
|---|
| 167 |
p_sys->p_thread->rtmp_headers_send[i].timestamp = -1; |
|---|
| 168 |
p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1; |
|---|
| 169 |
p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1; |
|---|
| 170 |
p_sys->p_thread->rtmp_headers_send[i].length_body = -1; |
|---|
| 171 |
p_sys->p_thread->rtmp_headers_send[i].content_type = -1; |
|---|
| 172 |
p_sys->p_thread->rtmp_headers_send[i].src_dst = -1; |
|---|
| 173 |
p_sys->p_thread->rtmp_headers_send[i].body = NULL; |
|---|
| 174 |
} |
|---|
| 175 |
|
|---|
| 176 |
vlc_cond_init( &p_sys->p_thread->wait ); |
|---|
| 177 |
vlc_mutex_init( &p_sys->p_thread->lock ); |
|---|
| 178 |
|
|---|
| 179 |
p_sys->p_thread->result_connect = 1; |
|---|
| 180 |
|
|---|
| 181 |
p_sys->p_thread->result_play = 1; |
|---|
| 182 |
p_sys->p_thread->result_stop = 0; |
|---|
| 183 |
p_sys->p_thread->fd = -1; |
|---|
| 184 |
|
|---|
| 185 |
|
|---|
| 186 |
if( var_CreateGetBool( p_access, "rtmp-connect" ) > 0 ) |
|---|
| 187 |
{ |
|---|
| 188 |
#if 0 |
|---|
| 189 |
p_sys->p_thread->fd = net_ConnectTCP( p_access, |
|---|
| 190 |
p_sys->p_thread->url.psz_host, |
|---|
| 191 |
p_sys->p_thread->url.i_port ); |
|---|
| 192 |
#endif |
|---|
| 193 |
msg_Err( p_access, "to be implemented" ); |
|---|
| 194 |
goto error2; |
|---|
| 195 |
} |
|---|
| 196 |
else |
|---|
| 197 |
{ |
|---|
| 198 |
int *p_fd_listen; |
|---|
| 199 |
|
|---|
| 200 |
p_sys->active = 0; |
|---|
| 201 |
p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host, |
|---|
| 202 |
p_sys->p_thread->url.i_port ); |
|---|
| 203 |
if( p_fd_listen == NULL ) |
|---|
| 204 |
{ |
|---|
| 205 |
msg_Warn( p_access, "cannot listen to %s port %i", |
|---|
| 206 |
p_sys->p_thread->url.psz_host, |
|---|
| 207 |
p_sys->p_thread->url.i_port ); |
|---|
| 208 |
goto error2; |
|---|
| 209 |
} |
|---|
| 210 |
|
|---|
| 211 |
do |
|---|
| 212 |
p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen, -1 ); |
|---|
| 213 |
while( p_sys->p_thread->fd == -1 ); |
|---|
| 214 |
net_ListenClose( p_fd_listen ); |
|---|
| 215 |
|
|---|
| 216 |
if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 ) |
|---|
| 217 |
{ |
|---|
| 218 |
msg_Err( p_access, "handshake passive failed"); |
|---|
| 219 |
goto error2; |
|---|
| 220 |
} |
|---|
| 221 |
} |
|---|
| 222 |
|
|---|
| 223 |
if( vlc_thread_create( p_sys->p_thread, "rtmp control thread", ThreadControl, |
|---|
| 224 |
VLC_THREAD_PRIORITY_INPUT, false ) ) |
|---|
| 225 |
{ |
|---|
| 226 |
msg_Err( p_access, "cannot spawn rtmp control thread" ); |
|---|
| 227 |
goto error2; |
|---|
| 228 |
} |
|---|
| 229 |
|
|---|
| 230 |
if( !p_sys->active ) |
|---|
| 231 |
{ |
|---|
| 232 |
if( rtmp_connect_passive( p_sys->p_thread ) < 0 ) |
|---|
| 233 |
{ |
|---|
| 234 |
msg_Err( p_access, "connect passive failed"); |
|---|
| 235 |
goto error2; |
|---|
| 236 |
} |
|---|
| 237 |
} |
|---|
| 238 |
|
|---|
| 239 |
p_access->pf_write = Write; |
|---|
| 240 |
p_access->pf_seek = Seek; |
|---|
| 241 |
|
|---|
| 242 |
return VLC_SUCCESS; |
|---|
| 243 |
|
|---|
| 244 |
error2: |
|---|
| 245 |
vlc_cond_destroy( &p_sys->p_thread->wait ); |
|---|
| 246 |
vlc_mutex_destroy( &p_sys->p_thread->lock ); |
|---|
| 247 |
|
|---|
| 248 |
free( p_sys->p_thread->psz_application ); |
|---|
| 249 |
free( p_sys->p_thread->psz_media ); |
|---|
| 250 |
|
|---|
| 251 |
if( p_sys->p_thread->fd != -1 ) |
|---|
| 252 |
net_Close( p_sys->p_thread->fd ); |
|---|
| 253 |
error: |
|---|
| 254 |
vlc_object_detach( p_sys->p_thread ); |
|---|
| 255 |
vlc_object_release( p_sys->p_thread ); |
|---|
| 256 |
|
|---|
| 257 |
vlc_UrlClean( &p_sys->p_thread->url ); |
|---|
| 258 |
free( p_sys ); |
|---|
| 259 |
|
|---|
| 260 |
return VLC_EGENERIC; |
|---|
| 261 |
} |
|---|
| 262 |
|
|---|
| 263 |
|
|---|
| 264 |
|
|---|
| 265 |
|
|---|
| 266 |
static void Close( vlc_object_t * p_this ) |
|---|
| 267 |
{ |
|---|
| 268 |
sout_access_out_t *p_access = (sout_access_out_t *) p_this; |
|---|
| 269 |
sout_access_out_sys_t *p_sys = p_access->p_sys; |
|---|
| 270 |
int i; |
|---|
| 271 |
|
|---|
| 272 |
|
|---|
| 273 |
vlc_object_kill( p_sys->p_thread ); |
|---|
| 274 |
block_FifoWake( p_sys->p_thread->p_fifo_input ); |
|---|
| 275 |
|
|---|
| 276 |
vlc_thread_join( p_sys->p_thread ); |
|---|
| 277 |
|
|---|
| 278 |
vlc_cond_destroy( &p_sys->p_thread->wait ); |
|---|
| 279 |
vlc_mutex_destroy( &p_sys->p_thread->lock ); |
|---|
| 280 |
|
|---|
| 281 |
block_FifoRelease( p_sys->p_thread->p_fifo_input ); |
|---|
| 282 |
block_FifoRelease( p_sys->p_thread->p_empty_blocks ); |
|---|
| 283 |
|
|---|
| 284 |
for( i = 0; i < 64; i++ ) |
|---|
| 285 |
{ |
|---|
| 286 |
if( p_sys->p_thread->rtmp_headers_recv[i].body != NULL ) |
|---|
| 287 |
{ |
|---|
| 288 |
free( p_sys->p_thread->rtmp_headers_recv[i].body->body ); |
|---|
| 289 |
free( p_sys->p_thread->rtmp_headers_recv[i].body ); |
|---|
| 290 |
} |
|---|
| 291 |
} |
|---|
| 292 |
|
|---|
| 293 |
net_Close( p_sys->p_thread->fd ); |
|---|
| 294 |
|
|---|
| 295 |
vlc_object_detach( p_sys->p_thread ); |
|---|
| 296 |
vlc_object_release( p_sys->p_thread ); |
|---|
| 297 |
|
|---|
| 298 |
vlc_UrlClean( &p_sys->p_thread->url ); |
|---|
| 299 |
free( p_sys->p_thread->psz_application ); |
|---|
| 300 |
free( p_sys->p_thread->psz_media ); |
|---|
| 301 |
free( p_sys ); |
|---|
| 302 |
} |
|---|
| 303 |
|
|---|
| 304 |
|
|---|
| 305 |
|
|---|
| 306 |
|
|---|
| 307 |
static ssize_t Write( sout_access_out_t *p_access, block_t *p_buffer ) |
|---|
| 308 |
{ |
|---|
| 309 |
rtmp_packet_t *rtmp_packet; |
|---|
| 310 |
uint8_t *tmp_buffer; |
|---|
| 311 |
ssize_t i_ret; |
|---|
| 312 |
ssize_t i_write = 0; |
|---|
| 313 |
|
|---|
| 314 |
if( p_access->p_sys->p_thread->first_media_packet ) |
|---|
| 315 |
{ |
|---|
| 316 |
|
|---|
| 317 |
memmove( p_buffer->p_buffer, p_buffer->p_buffer + 13, p_buffer->i_buffer - 13 ); |
|---|
| 318 |
p_buffer = block_Realloc( p_buffer, 0, p_buffer->i_buffer - 13 ); |
|---|
| 319 |
|
|---|
| 320 |
p_access->p_sys->p_thread->first_media_packet = 0; |
|---|
| 321 |
} |
|---|
| 322 |
|
|---|
| 323 |
while( p_buffer ) |
|---|
| 324 |
{ |
|---|
| 325 |
block_t *p_next = p_buffer->p_next; |
|---|
| 326 |
|
|---|
| 327 |
|
|---|
| 328 |
|
|---|
| 329 |
|
|---|
| 330 |
|
|---|
| 331 |
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 |
|
|---|
| 335 |
|
|---|
| 336 |
msg_Warn(p_access, "rtmp.c:360 i_dts %"PRIu64" i_pts %"PRIu64, |
|---|
| 337 |
p_buffer->i_dts, p_buffer->i_pts); |
|---|
| 338 |
rtmp_packet = rtmp_build_flv_over_rtmp( p_access->p_sys->p_thread, p_buffer ); |
|---|
| 339 |
|
|---|
| 340 |
if( rtmp_packet ) |
|---|
| 341 |
{ |
|---|
| 342 |
tmp_buffer = rtmp_encode_packet( p_access->p_sys->p_thread, rtmp_packet ); |
|---|
| 343 |
|
|---|
| 344 |
i_ret = net_Write( p_access->p_sys->p_thread, p_access->p_sys->p_thread->fd, NULL, tmp_buffer, rtmp_packet->length_encoded ); |
|---|
| 345 |
if( i_ret != rtmp_packet->length_encoded ) |
|---|
| 346 |
{ |
|---|
| 347 |
free( rtmp_packet->body->body ); |
|---|
| 348 |
free( rtmp_packet->body ); |
|---|
| 349 |
free( rtmp_packet ); |
|---|
| 350 |
free( tmp_buffer ); |
|---|
| 351 |
msg_Err( p_access->p_sys->p_thread, "failed send flv packet" ); |
|---|
| 352 |
return -1; |
|---|
| 353 |
} |
|---|
| 354 |
free( rtmp_packet->body->body ); |
|---|
| 355 |
free( rtmp_packet->body ); |
|---|
| 356 |
free( rtmp_packet ); |
|---|
| 357 |
free( tmp_buffer ); |
|---|
| 358 |
} |
|---|
| 359 |
|
|---|
| 360 |
i_write += p_buffer->i_buffer; |
|---|
| 361 |
|
|---|
| 362 |
p_buffer = p_next; |
|---|
| 363 |
} |
|---|
| 364 |
|
|---|
| 365 |
return i_write; |
|---|
| 366 |
} |
|---|
| 367 |
|
|---|
| 368 |
|
|---|
| 369 |
|
|---|
| 370 |
|
|---|
| 371 |
static int Seek( sout_access_out_t *p_access, off_t i_pos ) |
|---|
| 372 |
{ |
|---|
| 373 |
(void)i_pos; |
|---|
| 374 |
msg_Err( p_access, "RTMP sout access cannot seek" ); |
|---|
| 375 |
return -1; |
|---|
| 376 |
} |
|---|
| 377 |
|
|---|
| 378 |
|
|---|
| 379 |
|
|---|
| 380 |
|
|---|
| 381 |
static void* ThreadControl( vlc_object_t *p_this ) |
|---|
| 382 |
{ |
|---|
| 383 |
rtmp_control_thread_t *p_thread = (rtmp_control_thread_t *) p_this; |
|---|
| 384 |
rtmp_packet_t *rtmp_packet; |
|---|
| 385 |
int canc = vlc_savecancel (); |
|---|
| 386 |
|
|---|
| 387 |
rtmp_init_handler( p_thread->rtmp_handler ); |
|---|
| 388 |
|
|---|
| 389 |
while( vlc_object_alive (p_thread) ) |
|---|
| 390 |
{ |
|---|
| 391 |
rtmp_packet = rtmp_read_net_packet( p_thread ); |
|---|
| 392 |
if( rtmp_packet != NULL ) |
|---|
| 393 |
{ |
|---|
| 394 |
if( rtmp_packet->content_type < 0x01 |
|---|
| 395 |
|| rtmp_packet->content_type > 0x14 ) |
|---|
| 396 |
{ |
|---|
| 397 |
free( rtmp_packet->body->body ); |
|---|
| 398 |
free( rtmp_packet->body ); |
|---|
| 399 |
free( rtmp_packet ); |
|---|
| 400 |
|
|---|
| 401 |
msg_Warn( p_thread, "unknown content type received" ); |
|---|
| 402 |
} |
|---|
| 403 |
else |
|---|
| 404 |
p_thread->rtmp_handler[rtmp_packet->content_type]( p_thread, rtmp_packet ); |
|---|
| 405 |
} |
|---|
| 406 |
else |
|---|
| 407 |
{ |
|---|
| 408 |
|
|---|
| 409 |
if( p_thread->result_connect ) |
|---|
| 410 |
{ |
|---|
| 411 |
vlc_mutex_lock( &p_thread->lock ); |
|---|
| 412 |
vlc_cond_signal( &p_thread->wait ); |
|---|
| 413 |
vlc_mutex_unlock( &p_thread->lock ); |
|---|
| 414 |
} |
|---|
| 415 |
|
|---|
| 416 |
p_thread->b_die = 1; |
|---|
| 417 |
} |
|---|
| 418 |
} |
|---|
| 419 |
vlc_restorecancel (canc); |
|---|
| 420 |
return NULL; |
|---|
| 421 |
} |
|---|