00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include <stdlib.h>
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <string.h>
00032 #include <errno.h>
00033 #include <fcntl.h>
00034
00035 #include <vlc/vlc.h>
00036 #include <vlc/sout.h>
00037
00038 #ifdef HAVE_UNISTD_H
00039 # include <unistd.h>
00040 #endif
00041
00042 #ifdef WIN32
00043 # include <winsock2.h>
00044 # include <ws2tcpip.h>
00045 # ifndef IN_MULTICAST
00046 # define IN_MULTICAST(a) IN_CLASSD(a)
00047 # endif
00048 #else
00049 # include <sys/socket.h>
00050 #endif
00051
00052 #include "network.h"
00053
00054 #define MAX_EMPTY_BLOCKS 200
00055
00056 #if defined(WIN32) || defined(UNDER_CE)
00057 # define WINSOCK_STRERROR_SIZE 20
00058 static const char *winsock_strerror( char *buf )
00059 {
00060 snprintf( buf, WINSOCK_STRERROR_SIZE, "Winsock error %d",
00061 WSAGetLastError( ) );
00062 buf[WINSOCK_STRERROR_SIZE - 1] = '\0';
00063 return buf;
00064 }
00065 #endif
00066
00067
00068
00069
00070 static int Open ( vlc_object_t * );
00071 static void Close( vlc_object_t * );
00072
00073 #define SOUT_CFG_PREFIX "sout-udp-"
00074
00075 #define CACHING_TEXT N_("Caching value (ms)")
00076 #define CACHING_LONGTEXT N_( \
00077 "Allows you to modify the default caching value for UDP streams. This " \
00078 "value should be set in millisecond units." )
00079
00080 #define TTL_TEXT N_("Time To Live")
00081 #define TTL_LONGTEXT N_("Allows you to define the time to live of the " \
00082 "outgoing stream.")
00083
00084 #define GROUP_TEXT N_("Group packets")
00085 #define GROUP_LONGTEXT N_("Packets can be sent one by one at the right time " \
00086 "or by groups. This allows you to give the number " \
00087 "of packets that will be sent at a time. It " \
00088 "helps reducing the scheduling load on " \
00089 "heavily-loaded systems." )
00090 #define RAW_TEXT N_("Raw write")
00091 #define RAW_LONGTEXT N_("If you enable this option, packets will be sent " \
00092 "directly, without trying to fill the MTU (ie, " \
00093 "without trying to make the biggest possible packets " \
00094 "in order to improve streaming)." )
00095
00096 vlc_module_begin();
00097 set_description( _("UDP stream output") );
00098 set_shortname( N_( "UDP" ) );
00099 set_category( CAT_SOUT );
00100 set_subcategory( SUBCAT_SOUT_ACO );
00101 add_integer( SOUT_CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
00102 add_integer( SOUT_CFG_PREFIX "ttl", 0, NULL,TTL_TEXT, TTL_LONGTEXT,
00103 VLC_TRUE );
00104 add_integer( SOUT_CFG_PREFIX "group", 1, NULL, GROUP_TEXT, GROUP_LONGTEXT,
00105 VLC_TRUE );
00106 add_suppressed_integer( SOUT_CFG_PREFIX "late" );
00107 add_bool( SOUT_CFG_PREFIX "raw", 0, NULL, RAW_TEXT, RAW_LONGTEXT,
00108 VLC_TRUE );
00109
00110 set_capability( "sout access", 100 );
00111 add_shortcut( "udp" );
00112 add_shortcut( "rtp" );
00113 set_callbacks( Open, Close );
00114 vlc_module_end();
00115
00116
00117
00118
00119
00120 static const char *ppsz_sout_options[] = {
00121 "caching",
00122 "ttl",
00123 "group",
00124 "raw",
00125 NULL
00126 };
00127
00128 static int Write ( sout_access_out_t *, block_t * );
00129 static int WriteRaw( sout_access_out_t *, block_t * );
00130 static int Seek ( sout_access_out_t *, off_t );
00131
00132 static void ThreadWrite( vlc_object_t * );
00133 static block_t *NewUDPPacket( sout_access_out_t *, mtime_t );
00134
00135 typedef struct sout_access_thread_t
00136 {
00137 VLC_COMMON_MEMBERS
00138
00139 sout_instance_t *p_sout;
00140
00141 block_fifo_t *p_fifo;
00142
00143 int i_handle;
00144
00145 int64_t i_caching;
00146 int i_group;
00147
00148 vlc_mutex_t blocks_lock;
00149 block_t *p_empty_blocks;
00150 int i_empty_depth;
00151
00152 } sout_access_thread_t;
00153
00154 struct sout_access_out_sys_t
00155 {
00156 int b_rtpts;
00157 uint16_t i_sequence_number;
00158 uint32_t i_ssrc;
00159
00160 int i_mtu;
00161
00162 block_t *p_buffer;
00163
00164 sout_access_thread_t *p_thread;
00165
00166 vlc_bool_t b_mtu_warning;
00167 };
00168
00169 #define DEFAULT_PORT 1234
00170
00171
00172
00173
00174 static int Open( vlc_object_t *p_this )
00175 {
00176 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
00177 sout_access_out_sys_t *p_sys;
00178
00179 char *psz_parser;
00180 char *psz_dst_addr;
00181 int i_dst_port;
00182
00183 module_t *p_network;
00184 network_socket_t socket_desc;
00185
00186 vlc_value_t val;
00187
00188 sout_CfgParse( p_access, SOUT_CFG_PREFIX,
00189 ppsz_sout_options, p_access->p_cfg );
00190
00191 if( !( p_sys = malloc( sizeof( sout_access_out_sys_t ) ) ) )
00192 {
00193 msg_Err( p_access, "not enough memory" );
00194 return VLC_EGENERIC;
00195 }
00196 memset( p_sys, 0, sizeof(sout_access_out_sys_t) );
00197 p_access->p_sys = p_sys;
00198
00199 if( p_access->psz_access != NULL &&
00200 !strcmp( p_access->psz_access, "rtp" ) )
00201 {
00202 p_sys->b_rtpts = 1;
00203 }
00204 else
00205 {
00206 p_sys->b_rtpts = 0;
00207 }
00208
00209 psz_parser = strdup( p_access->psz_name );
00210
00211 psz_dst_addr = psz_parser;
00212 i_dst_port = 0;
00213
00214 if ( *psz_parser == '[' )
00215 {
00216 while( *psz_parser && *psz_parser != ']' )
00217 {
00218 psz_parser++;
00219 }
00220 }
00221 while( *psz_parser && *psz_parser != ':' )
00222 {
00223 psz_parser++;
00224 }
00225 if( *psz_parser == ':' )
00226 {
00227 *psz_parser = '\0';
00228 psz_parser++;
00229 i_dst_port = atoi( psz_parser );
00230 }
00231 if( i_dst_port <= 0 )
00232 {
00233 i_dst_port = DEFAULT_PORT;
00234 }
00235
00236 p_sys->p_thread =
00237 vlc_object_create( p_access, sizeof( sout_access_thread_t ) );
00238 if( !p_sys->p_thread )
00239 {
00240 msg_Err( p_access, "out of memory" );
00241 return VLC_EGENERIC;
00242 }
00243
00244 p_sys->p_thread->p_sout = p_access->p_sout;
00245 p_sys->p_thread->b_die = 0;
00246 p_sys->p_thread->b_error= 0;
00247 p_sys->p_thread->p_fifo = block_FifoNew( p_access );
00248 p_sys->p_thread->p_empty_blocks = NULL;
00249 p_sys->p_thread->i_empty_depth = 0;
00250 vlc_mutex_init( p_access, &p_sys->p_thread->blocks_lock );
00251
00252
00253 socket_desc.psz_server_addr = psz_dst_addr;
00254 socket_desc.i_server_port = i_dst_port;
00255 socket_desc.psz_bind_addr = "";
00256 socket_desc.i_bind_port = 0;
00257 socket_desc.i_handle = -1;
00258 socket_desc.v6only = 0;
00259
00260 var_Get( p_access, SOUT_CFG_PREFIX "ttl", &val );
00261 socket_desc.i_ttl = val.i_int;
00262
00263 p_sys->p_thread->p_private = (void*)&socket_desc;
00264 p_network = module_Need( p_sys->p_thread, "network", "ipv4", VLC_TRUE );
00265 if( p_network != NULL )
00266 module_Unneed( p_sys->p_thread, p_network );
00267
00268 if( socket_desc.i_handle == -1 )
00269 {
00270 p_network = module_Need( p_sys->p_thread, "network", "ipv6", VLC_TRUE );
00271 if( p_network != NULL )
00272 module_Unneed( p_sys->p_thread, p_network );
00273
00274 if( socket_desc.i_handle == -1 )
00275 {
00276 msg_Err( p_access, "failed to open a connection (udp)" );
00277 return VLC_EGENERIC;
00278 }
00279 }
00280
00281 p_sys->p_thread->i_handle = socket_desc.i_handle;
00282 net_StopRecv( socket_desc.i_handle );
00283
00284 var_Get( p_access, SOUT_CFG_PREFIX "caching", &val );
00285 p_sys->p_thread->i_caching = (int64_t)val.i_int * 1000;
00286
00287 var_Get( p_access, SOUT_CFG_PREFIX "group", &val );
00288 p_sys->p_thread->i_group = val.i_int;
00289
00290 p_sys->i_mtu = socket_desc.i_mtu;
00291
00292 if( vlc_thread_create( p_sys->p_thread, "sout write thread", ThreadWrite,
00293 VLC_THREAD_PRIORITY_HIGHEST, VLC_FALSE ) )
00294 {
00295 msg_Err( p_access->p_sout, "cannot spawn sout access thread" );
00296 vlc_object_destroy( p_sys->p_thread );
00297 return VLC_EGENERIC;
00298 }
00299
00300 srand( (uint32_t)mdate());
00301 p_sys->p_buffer = NULL;
00302 p_sys->i_sequence_number = rand()&0xffff;
00303 p_sys->i_ssrc = rand()&0xffffffff;
00304
00305 var_Get( p_access, SOUT_CFG_PREFIX "raw", &val );
00306 if( val.b_bool ) p_access->pf_write = WriteRaw;
00307 else p_access->pf_write = Write;
00308
00309 p_access->pf_seek = Seek;
00310
00311 msg_Dbg( p_access, "udp access output opened(%s:%d)",
00312 psz_dst_addr, i_dst_port );
00313
00314 free( psz_dst_addr );
00315
00316
00317 p_access->p_sout->i_out_pace_nocontrol++;
00318
00319 return VLC_SUCCESS;
00320 }
00321
00322
00323
00324
00325 static void Close( vlc_object_t * p_this )
00326 {
00327 sout_access_out_t *p_access = (sout_access_out_t*)p_this;
00328 sout_access_out_sys_t *p_sys = p_access->p_sys;
00329 int i;
00330
00331 p_sys->p_thread->b_die = 1;
00332 for( i = 0; i < 10; i++ )
00333 {
00334 block_t *p_dummy = block_New( p_access, p_sys->i_mtu );
00335 p_dummy->i_dts = 0;
00336 p_dummy->i_pts = 0;
00337 p_dummy->i_length = 0;
00338 memset( p_dummy->p_buffer, 0, p_dummy->i_buffer );
00339 block_FifoPut( p_sys->p_thread->p_fifo, p_dummy );
00340 }
00341 vlc_thread_join( p_sys->p_thread );
00342
00343 block_FifoRelease( p_sys->p_thread->p_fifo );
00344
00345 if( p_sys->p_buffer ) block_Release( p_sys->p_buffer );
00346 while ( p_sys->p_thread->p_empty_blocks != NULL )
00347 {
00348 block_t *p_next = p_sys->p_thread->p_empty_blocks->p_next;
00349 block_Release( p_sys->p_thread->p_empty_blocks );
00350 p_sys->p_thread->p_empty_blocks = p_next;
00351 }
00352 vlc_mutex_destroy( &p_sys->p_thread->blocks_lock );
00353
00354 net_Close( p_sys->p_thread->i_handle );
00355
00356
00357 p_access->p_sout->i_out_pace_nocontrol--;
00358
00359 msg_Dbg( p_access, "udp access output closed" );
00360 free( p_sys );
00361 }
00362
00363
00364
00365
00366 static int Write( sout_access_out_t *p_access, block_t *p_buffer )
00367 {
00368 sout_access_out_sys_t *p_sys = p_access->p_sys;
00369
00370 while( p_buffer )
00371 {
00372 block_t *p_next;
00373 int i_packets = 0;
00374
00375 if( !p_sys->b_mtu_warning && p_buffer->i_buffer > p_sys->i_mtu )
00376 {
00377 msg_Warn( p_access, "packet size > MTU, you should probably "
00378 "increase the MTU" );
00379 p_sys->b_mtu_warning = VLC_TRUE;
00380 }
00381
00382
00383 if( p_sys->p_buffer &&
00384 p_sys->p_buffer->i_buffer + p_buffer->i_buffer > p_sys->i_mtu )
00385 {
00386 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching < mdate() )
00387 {
00388 msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
00389 mdate() - p_sys->p_buffer->i_dts
00390 - p_sys->p_thread->i_caching );
00391 }
00392 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
00393 p_sys->p_buffer = NULL;
00394 }
00395
00396 while( p_buffer->i_buffer )
00397 {
00398 int i_write = __MIN( p_buffer->i_buffer, p_sys->i_mtu );
00399
00400 i_packets++;
00401
00402 if( !p_sys->p_buffer )
00403 {
00404 p_sys->p_buffer = NewUDPPacket( p_access, p_buffer->i_dts );
00405 if( !p_sys->p_buffer ) break;
00406 }
00407
00408 memcpy( p_sys->p_buffer->p_buffer + p_sys->p_buffer->i_buffer,
00409 p_buffer->p_buffer, i_write );
00410
00411 p_sys->p_buffer->i_buffer += i_write;
00412 p_buffer->p_buffer += i_write;
00413 p_buffer->i_buffer -= i_write;
00414 if ( p_buffer->i_flags & BLOCK_FLAG_CLOCK )
00415 {
00416 if ( p_sys->p_buffer->i_flags & BLOCK_FLAG_CLOCK )
00417 msg_Warn( p_access, "putting two PCRs at once" );
00418 p_sys->p_buffer->i_flags |= BLOCK_FLAG_CLOCK;
00419 }
00420
00421 if( p_sys->p_buffer->i_buffer == p_sys->i_mtu || i_packets > 1 )
00422 {
00423
00424 if( p_sys->p_buffer->i_dts + p_sys->p_thread->i_caching
00425 < mdate() )
00426 {
00427 msg_Dbg( p_access, "late packet for udp input (" I64Fd ")",
00428 mdate() - p_sys->p_buffer->i_dts
00429 - p_sys->p_thread->i_caching );
00430 }
00431 block_FifoPut( p_sys->p_thread->p_fifo, p_sys->p_buffer );
00432 p_sys->p_buffer = NULL;
00433 }
00434 }
00435
00436 p_next = p_buffer->p_next;
00437 block_Release( p_buffer );
00438 p_buffer = p_next;
00439 }
00440
00441 return( p_sys->p_thread->b_error ? -1 : 0 );
00442 }
00443
00444
00445
00446
00447 static int WriteRaw( sout_access_out_t *p_access, block_t *p_buffer )
00448 {
00449 sout_access_out_sys_t *p_sys = p_access->p_sys;
00450 block_t *p_buf;
00451
00452 vlc_mutex_lock( &p_sys->p_thread->blocks_lock );
00453 while ( p_sys->p_thread->i_empty_depth >= MAX_EMPTY_BLOCKS )
00454 {
00455 p_buf = p_sys->p_thread->p_empty_blocks;
00456 p_sys->p_thread->p_empty_blocks =
00457 p_sys->p_thread->p_empty_blocks->p_next;
00458 p_sys->p_thread->i_empty_depth--;
00459 vlc_mutex_unlock( &p_sys->p_thread->blocks_lock );
00460 block_Release( p_buf );
00461 vlc_mutex_lock( &p_sys->p_thread->blocks_lock );
00462 }
00463 vlc_mutex_unlock( &p_sys->p_thread->blocks_lock );
00464
00465 block_FifoPut( p_sys->p_thread->p_fifo, p_buffer );
00466
00467 return( p_sys->p_thread->b_error ? -1 : 0 );
00468 }
00469
00470
00471
00472
00473 static int Seek( sout_access_out_t *p_access, off_t i_pos )
00474 {
00475 msg_Err( p_access, "UDP sout access cannot seek" );
00476 return -1;
00477 }
00478
00479
00480
00481
00482 static block_t *NewUDPPacket( sout_access_out_t *p_access, mtime_t i_dts)
00483 {
00484 sout_access_out_sys_t *p_sys = p_access->p_sys;
00485 block_t *p_buffer;
00486
00487 vlc_mutex_lock( &p_sys->p_thread->blocks_lock );
00488 while ( p_sys->p_thread->i_empty_depth > MAX_EMPTY_BLOCKS )
00489 {
00490 p_buffer = p_sys->p_thread->p_empty_blocks;
00491 p_sys->p_thread->p_empty_blocks =
00492 p_sys->p_thread->p_empty_blocks->p_next;
00493 p_sys->p_thread->i_empty_depth--;
00494 vlc_mutex_unlock( &p_sys->p_thread->blocks_lock );
00495 block_Release( p_buffer );
00496 vlc_mutex_lock( &p_sys->p_thread->blocks_lock );
00497 }
00498 p_buffer = p_sys->p_thread->p_empty_blocks;
00499 if ( p_buffer != NULL )
00500 {
00501 p_sys->p_thread->p_empty_blocks =
00502 p_sys->p_thread->p_empty_blocks->p_next;
00503 p_sys->p_thread->i_empty_depth--;
00504 vlc_mutex_unlock( &p_sys->p_thread->blocks_lock );
00505 p_buffer->p_next = NULL;
00506 p_buffer->i_flags = 0;
00507 p_buffer = block_Realloc( p_buffer, 0, p_sys->i_mtu );
00508 }
00509 else
00510 {
00511 vlc_mutex_unlock( &p_sys->p_thread->blocks_lock );
00512 p_buffer = block_New( p_access->p_sout, p_sys->i_mtu );
00513 }
00514
00515 p_buffer->i_dts = i_dts;
00516 p_buffer->i_buffer = 0;
00517
00518 if( p_sys->b_rtpts )
00519 {
00520 mtime_t i_timestamp = p_buffer->i_dts * 9 / 100;
00521
00522
00523 p_buffer->p_buffer[0] = 0x80;
00524 p_buffer->p_buffer[1] = 0x21;
00525
00526 p_buffer->p_buffer[2] = ( p_sys->i_sequence_number >> 8 )&0xff;
00527 p_buffer->p_buffer[3] = p_sys->i_sequence_number&0xff;
00528 p_sys->i_sequence_number++;
00529
00530 p_buffer->p_buffer[4] = ( i_timestamp >> 24 )&0xff;
00531 p_buffer->p_buffer[5] = ( i_timestamp >> 16 )&0xff;
00532 p_buffer->p_buffer[6] = ( i_timestamp >> 8 )&0xff;
00533 p_buffer->p_buffer[7] = i_timestamp&0xff;
00534
00535 p_buffer->p_buffer[ 8] = ( p_sys->i_ssrc >> 24 )&0xff;
00536 p_buffer->p_buffer[ 9] = ( p_sys->i_ssrc >> 16 )&0xff;
00537 p_buffer->p_buffer[10] = ( p_sys->i_ssrc >> 8 )&0xff;
00538 p_buffer->p_buffer[11] = p_sys->i_ssrc&0xff;
00539
00540 p_buffer->i_buffer = 12;
00541 }
00542
00543 return p_buffer;
00544 }
00545
00546
00547
00548
00549 static void ThreadWrite( vlc_object_t *p_this )
00550 {
00551 sout_access_thread_t *p_thread = (sout_access_thread_t*)p_this;
00552 mtime_t i_date_last = -1;
00553 mtime_t i_to_send = p_thread->i_group;
00554 int i_dropped_packets = 0;
00555 #if defined(WIN32) || defined(UNDER_CE)
00556 char strerror_buf[WINSOCK_STRERROR_SIZE];
00557 # define strerror( x ) winsock_strerror( strerror_buf )
00558 #endif
00559
00560 while( !p_thread->b_die )
00561 {
00562 block_t *p_pk;
00563 mtime_t i_date, i_sent;
00564
00565 p_pk = block_FifoGet( p_thread->p_fifo );
00566
00567 i_date = p_thread->i_caching + p_pk->i_dts;
00568 if( i_date_last > 0 )
00569 {
00570 if( i_date - i_date_last > 2000000 )
00571 {
00572 if( !i_dropped_packets )
00573 msg_Dbg( p_thread, "mmh, hole ("I64Fd" > 2s) -> drop",
00574 i_date - i_date_last );
00575
00576 vlc_mutex_lock( &p_thread->blocks_lock );
00577 p_pk->p_next = p_thread->p_empty_blocks;
00578 p_thread->p_empty_blocks = p_pk;
00579 p_thread->i_empty_depth++;
00580 vlc_mutex_unlock( &p_thread->blocks_lock );
00581
00582 i_date_last = i_date;
00583 i_dropped_packets++;
00584 continue;
00585 }
00586 else if( i_date - i_date_last < -1000 )
00587 {
00588 if( !i_dropped_packets )
00589 msg_Dbg( p_thread, "mmh, packets in the past ("I64Fd")",
00590 i_date_last - i_date );
00591 }
00592 }
00593
00594 i_to_send--;
00595 if( !i_to_send || (p_pk->i_flags & BLOCK_FLAG_CLOCK) )
00596 {
00597 mwait( i_date );
00598 i_to_send = p_thread->i_group;
00599 }
00600 if( send( p_thread->i_handle, p_pk->p_buffer, p_pk->i_buffer, 0 )
00601 == -1 )
00602 {
00603 msg_Warn( p_thread, "send error: %s", strerror(errno) );
00604 }
00605
00606 if( i_dropped_packets )
00607 {
00608 msg_Dbg( p_thread, "dropped %i packets", i_dropped_packets );
00609 i_dropped_packets = 0;
00610 }
00611
00612 #if 1
00613 i_sent = mdate();
00614 if ( i_sent > i_date + 20000 )
00615 {
00616 msg_Dbg( p_thread, "packet has been sent too late (" I64Fd ")",
00617 i_sent - i_date );
00618 }
00619 #endif
00620
00621 vlc_mutex_lock( &p_thread->blocks_lock );
00622 p_pk->p_next = p_thread->p_empty_blocks;
00623 p_thread->p_empty_blocks = p_pk;
00624 p_thread->i_empty_depth++;
00625 vlc_mutex_unlock( &p_thread->blocks_lock );
00626
00627 i_date_last = i_date;
00628 }
00629 }