static int Activate(vlc_object_t *p_this) { int i_port = 21178; const char* psz_host = "127.0.0.1"; intf_thread_t *p_intf = (intf_thread_t*)(p_this); intf_sys_t *p_sys; p_sys = malloc(sizeof(intf_sys_t)); if (!p_sys) return VLC_ENOMEM; if (pipe(p_sys->i_wakeup) < 0) { free(p_sys); return VLC_EGENERIC; } p_sys->pi_socket = net_ListenTCP(p_this, psz_host, i_port); if (p_sys->pi_socket == NULL) { free(p_sys); msg_Err(p_intf, "can't listen to %s port %i", psz_host, i_port); return VLC_EGENERIC; } p_sys->i_socket = -1; vlc_mutex_init(&p_sys->o_write_lock); p_sys->i_read_offset = 0; p_sys->i_write_offset = 0; p_sys->i_write_length = 0; p_intf->p_sys = p_sys; p_intf->pf_run = Run; return VLC_SUCCESS; }
static int vlclua_net_listen_tcp( lua_State *L ) { vlc_object_t *p_this = vlclua_get_this( L ); const char *psz_host = luaL_checkstring( L, 1 ); int i_port = luaL_checkint( L, 2 ); int *pi_fd = net_ListenTCP( p_this, psz_host, i_port ); if( pi_fd == NULL ) return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port ); for( unsigned i = 0; pi_fd[i] != -1; i++ ) if( vlclua_fd_map( L, pi_fd[i] ) == -1 ) { while( i > 0 ) vlclua_fd_unmap( L, vlclua_fd_get_lua( L, pi_fd[--i] ) ); net_ListenClose( pi_fd ); return luaL_error( L, "Cannot listen on %s:%d", psz_host, i_port ); } int **ppi_fd = lua_newuserdata( L, sizeof( int * ) ); *ppi_fd = pi_fd; if( luaL_newmetatable( L, "net_listen" ) ) { lua_newtable( L ); luaL_register( L, NULL, vlclua_net_listen_reg ); lua_setfield( L, -2, "__index" ); lua_pushcfunction( L, vlclua_net_listen_close ); lua_setfield( L, -2, "__gc" ); } lua_setmetatable( L, -2 ); return 1; }
/***************************************************************************** * Open: initialize dummy interface *****************************************************************************/ static int Open( vlc_object_t *p_this ) { intf_thread_t *p_intf = (intf_thread_t*) p_this; vlm_t *mediatheque; char *psz_address; vlc_url_t url; int i_telnetport; if( !(mediatheque = vlm_New( p_intf )) ) { msg_Err( p_intf, "cannot start VLM" ); return VLC_EGENERIC; } msg_Info( p_intf, "using the VLM interface plugin..." ); i_telnetport = config_GetInt( p_intf, "telnet-port" ); psz_address = config_GetPsz( p_intf, "telnet-host" ); vlc_UrlParse(&url, psz_address, 0); // There might be two ports given, resolve any potentially // conflict url.i_port = getPort(p_intf, url, i_telnetport); p_intf->p_sys = malloc( sizeof( intf_sys_t ) ); if( ( p_intf->p_sys->pi_fd = net_ListenTCP( p_intf, url.psz_host, url.i_port ) ) == NULL ) { msg_Err( p_intf, "cannot listen for telnet" ); vlc_UrlClean(&url); free( psz_address ); free( p_intf->p_sys ); return VLC_EGENERIC; } msg_Info( p_intf, "telnet interface started on interface %s %d", url.psz_host, url.i_port ); p_intf->p_sys->i_clients = 0; p_intf->p_sys->clients = NULL; p_intf->p_sys->mediatheque = mediatheque; p_intf->pf_run = Run; vlc_UrlClean(&url); free( psz_address ); return VLC_SUCCESS; }
/***************************************************************************** * Activate: initialize and create stuff *****************************************************************************/ static int Activate( vlc_object_t *p_this ) { intf_thread_t *p_intf = (intf_thread_t*)p_this; char *psz_host, *psz_unix_path; int i_socket = -1; #if defined(HAVE_ISATTY) && !defined(WIN32) /* Check that stdin is a TTY */ if( !config_GetInt( p_intf, "rtci-fake-tty" ) && !isatty( 0 ) ) { msg_Warn( p_intf, "fd 0 is not a TTY" ); return VLC_EGENERIC; } #endif psz_unix_path = config_GetPsz( p_intf, "rtci-unix" ); if( psz_unix_path ) { #ifndef PF_LOCAL msg_Warn( p_intf, "your OS doesn't support filesystem sockets" ); free( psz_unix_path ); return VLC_EGENERIC; #else struct sockaddr_un addr; int i_ret; memset( &addr, 0, sizeof(struct sockaddr_un) ); msg_Dbg( p_intf, "trying UNIX socket" ); if( (i_socket = socket( PF_LOCAL, SOCK_STREAM, 0 ) ) < 0 ) { msg_Warn( p_intf, "can't open socket: %s", strerror(errno) ); free( psz_unix_path ); return VLC_EGENERIC; } addr.sun_family = AF_LOCAL; strncpy( addr.sun_path, psz_unix_path, sizeof( addr.sun_path ) ); addr.sun_path[sizeof( addr.sun_path ) - 1] = '\0'; if( (i_ret = bind( i_socket, (struct sockaddr*)&addr, sizeof(struct sockaddr_un) ) ) < 0 ) { msg_Warn( p_intf, "couldn't bind socket to address: %s", strerror(errno) ); free( psz_unix_path ); net_Close( i_socket ); return VLC_EGENERIC; } if( ( i_ret = listen( i_socket, 1 ) ) < 0 ) { msg_Warn( p_intf, "can't listen on socket: %s", strerror(errno)); free( psz_unix_path ); net_Close( i_socket ); return VLC_EGENERIC; } #endif } if( ( i_socket == -1) && ( psz_host = config_GetPsz( p_intf, "rtci-host" ) ) != NULL ) { vlc_url_t url; vlc_UrlParse( &url, psz_host, 0 ); msg_Dbg( p_intf, "base %s port %d", url.psz_host, url.i_port ); if( (i_socket = net_ListenTCP(p_this, url.psz_host, url.i_port)) == -1) { msg_Warn( p_intf, "can't listen to %s port %i", url.psz_host, url.i_port ); vlc_UrlClean( &url ); free( psz_host ); return VLC_EGENERIC; } vlc_UrlClean( &url ); free( psz_host ); } p_intf->p_sys = malloc( sizeof( intf_sys_t ) ); if( !p_intf->p_sys ) { msg_Err( p_intf, "no memory" ); return VLC_ENOMEM; } p_intf->p_sys->i_socket_listen = i_socket; p_intf->p_sys->i_socket = -1; p_intf->p_sys->psz_unix_path = psz_unix_path; /* Non-buffered stdout */ setvbuf( stdout, (char *)NULL, _IOLBF, 0 ); p_intf->pf_run = Run; #ifdef WIN32 if( !config_GetInt( p_intf, "rtci-quiet" ) ) { CONSOLE_INTRO_MSG; } #else CONSOLE_INTRO_MSG; #endif msg_rtci( _("Real time control interface initialized, `h' for help\n") ); return VLC_SUCCESS; }
/***************************************************************************** * Open: open the rtmp connection *****************************************************************************/ static int Open( vlc_object_t *p_this ) { sout_access_out_t *p_access = (sout_access_out_t *) p_this; sout_access_out_sys_t *p_sys; char *psz, *p; int length_path, length_media_name; int i; if( !( p_sys = calloc ( 1, sizeof( sout_access_out_sys_t ) ) ) ) return VLC_ENOMEM; p_access->p_sys = p_sys; p_sys->p_thread = vlc_object_create( p_access, sizeof( rtmp_control_thread_t ) ); if( !p_sys->p_thread ) { free( p_sys ); return VLC_ENOMEM; } vlc_object_attach( p_sys->p_thread, p_access ); /* Parse URI - remove spaces */ p = psz = strdup( p_access->psz_path ); while( ( p = strchr( p, ' ' ) ) != NULL ) *p = '+'; vlc_UrlParse( &p_sys->p_thread->url, psz, 0 ); free( psz ); if( p_sys->p_thread->url.psz_host == NULL || *p_sys->p_thread->url.psz_host == '\0' ) { msg_Warn( p_access, "invalid host" ); goto error; } if( p_sys->p_thread->url.i_port <= 0 ) p_sys->p_thread->url.i_port = 1935; if ( p_sys->p_thread->url.psz_path == NULL ) { msg_Warn( p_access, "invalid path" ); goto error; } length_path = strlen( p_sys->p_thread->url.psz_path ); char* psz_tmp = strrchr( p_sys->p_thread->url.psz_path, '/' ); if( !psz_tmp ) goto error; length_media_name = strlen( psz_tmp ) - 1; p_sys->p_thread->psz_application = strndup( p_sys->p_thread->url.psz_path + 1, length_path - length_media_name - 2 ); p_sys->p_thread->psz_media = strdup( p_sys->p_thread->url.psz_path + ( length_path - length_media_name ) ); msg_Dbg( p_access, "rtmp: host='%s' port=%d path='%s'", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port, p_sys->p_thread->url.psz_path ); if( p_sys->p_thread->url.psz_username && *p_sys->p_thread->url.psz_username ) { msg_Dbg( p_access, " user='******'", p_sys->p_thread->url.psz_username ); } /* Initialize thread variables */ p_sys->p_thread->b_error= 0; p_sys->p_thread->p_fifo_input = block_FifoNew(); p_sys->p_thread->p_empty_blocks = block_FifoNew(); p_sys->p_thread->has_audio = 0; p_sys->p_thread->has_video = 0; p_sys->p_thread->metadata_received = 0; p_sys->p_thread->first_media_packet = 1; p_sys->p_thread->flv_tag_previous_tag_size = 0x00000000; /* FLV_TAG_FIRST_PREVIOUS_TAG_SIZE */ p_sys->p_thread->flv_body = rtmp_body_new( -1 ); p_sys->p_thread->flv_length_body = 0; p_sys->p_thread->chunk_size_recv = 128; /* RTMP_DEFAULT_CHUNK_SIZE */ p_sys->p_thread->chunk_size_send = 128; /* RTMP_DEFAULT_CHUNK_SIZE */ for(i = 0; i < 64; i++) { memset( &p_sys->p_thread->rtmp_headers_recv[i], 0, sizeof( rtmp_packet_t ) ); p_sys->p_thread->rtmp_headers_send[i].length_header = -1; p_sys->p_thread->rtmp_headers_send[i].stream_index = -1; p_sys->p_thread->rtmp_headers_send[i].timestamp = -1; p_sys->p_thread->rtmp_headers_send[i].timestamp_relative = -1; p_sys->p_thread->rtmp_headers_send[i].length_encoded = -1; p_sys->p_thread->rtmp_headers_send[i].length_body = -1; p_sys->p_thread->rtmp_headers_send[i].content_type = -1; p_sys->p_thread->rtmp_headers_send[i].src_dst = -1; p_sys->p_thread->rtmp_headers_send[i].body = NULL; } vlc_cond_init( &p_sys->p_thread->wait ); vlc_mutex_init( &p_sys->p_thread->lock ); p_sys->p_thread->result_connect = 1; /* p_sys->p_thread->result_publish = only used on access */ p_sys->p_thread->result_play = 1; p_sys->p_thread->result_stop = 0; p_sys->p_thread->fd = -1; /* Open connection */ if( var_CreateGetBool( p_access, "rtmp-connect" ) > 0 ) { #if 0 p_sys->p_thread->fd = net_ConnectTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port ); #endif msg_Err( p_access, "to be implemented" ); goto error2; } else { int *p_fd_listen; p_sys->active = 0; p_fd_listen = net_ListenTCP( p_access, p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port ); if( p_fd_listen == NULL ) { msg_Warn( p_access, "cannot listen to %s port %i", p_sys->p_thread->url.psz_host, p_sys->p_thread->url.i_port ); goto error2; } do p_sys->p_thread->fd = net_Accept( p_access, p_fd_listen ); while( p_sys->p_thread->fd == -1 ); net_ListenClose( p_fd_listen ); if( rtmp_handshake_passive( p_this, p_sys->p_thread->fd ) < 0 ) { msg_Err( p_access, "handshake passive failed"); goto error2; } } if( vlc_clone( &p_sys->p_thread->thread, ThreadControl, p_sys->p_thread, VLC_THREAD_PRIORITY_INPUT ) ) { msg_Err( p_access, "cannot spawn rtmp control thread" ); goto error2; } if( !p_sys->active ) { if( rtmp_connect_passive( p_sys->p_thread ) < 0 ) { msg_Err( p_access, "connect passive failed"); vlc_cancel( p_sys->p_thread->thread ); vlc_join( p_sys->p_thread->thread, NULL ); goto error2; } } p_access->pf_write = Write; p_access->pf_seek = Seek; return VLC_SUCCESS; error2: vlc_cond_destroy( &p_sys->p_thread->wait ); vlc_mutex_destroy( &p_sys->p_thread->lock ); free( p_sys->p_thread->psz_application ); free( p_sys->p_thread->psz_media ); if( p_sys->p_thread->fd != -1 ) net_Close( p_sys->p_thread->fd ); error: vlc_UrlClean( &p_sys->p_thread->url ); vlc_object_release( p_sys->p_thread ); free( p_sys ); return VLC_EGENERIC; }