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; }
static int vlclua_net_listen_close( lua_State *L ) { int **ppi_fd = (int**)luaL_checkudata( L, 1, "net_listen" ); int *pi_fd = *ppi_fd; for( unsigned i = 0; pi_fd[i] != -1; i++ ) vlclua_fd_unmap( L, vlclua_fd_get_lua( L, pi_fd[i] ) ); net_ListenClose( pi_fd ); return 0; }
static void Deactivate(vlc_object_t *p_this) { intf_thread_t *p_intf = (intf_thread_t*)p_this; intf_sys_t *p_sys = p_intf->p_sys; net_ListenClose(p_sys->pi_socket); if(p_sys->i_socket != -1) net_Close(p_sys->i_socket); vlc_mutex_destroy(&p_sys->o_write_lock); close(p_sys->i_wakeup[0]); close(p_sys->i_wakeup[1]); free(p_sys); }
/***************************************************************************** * Close: *****************************************************************************/ static void Close( vlc_object_t *p_this ) { intf_thread_t *p_intf = (intf_thread_t*)p_this; intf_sys_t *p_sys = p_intf->p_sys; int i; for( i = 0; i < p_sys->i_clients; i++ ) { telnet_client_t *cl = p_sys->clients[i]; net_Close( cl->fd ); free( cl ); } if( p_sys->clients != NULL ) free( p_sys->clients ); net_ListenClose( p_sys->pi_fd ); vlm_Delete( p_sys->mediatheque ); free( p_sys ); }
/***************************************************************************** * 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; }