static int fixAccessMux( sout_stream_t *p_stream, char **ppsz_mux, char **ppsz_access, const char *psz_url ) { char *psz_mux = *ppsz_mux; char *psz_access = *ppsz_access; if( !psz_mux ) { const char *psz_ext = psz_url ? strrchr( psz_url, '.' ) : NULL; if( psz_ext ) psz_ext++; /* use extension */ const char *psz_mux_byext = getMuxFromAlias( psz_ext ); if( !psz_access ) { if( !psz_mux_byext ) { msg_Err( p_stream, "no access _and_ no muxer" ); return 1; } msg_Warn( p_stream, "no access _and_ no muxer, extension gives file/%s", psz_mux_byext ); *ppsz_access = strdup("file"); *ppsz_mux = strdup(psz_mux_byext); } else { if( !strncmp( psz_access, "mmsh", 4 ) ) *ppsz_mux = strdup("asfh"); else if (!strcmp (psz_access, "udp")) *ppsz_mux = strdup("ts"); else if( psz_mux_byext ) *ppsz_mux = strdup(psz_mux_byext); else { msg_Err( p_stream, "no mux specified or found by extension" ); return 1; } } } else if( !psz_access ) { if( !strncmp( psz_mux, "asfh", 4 ) ) *ppsz_access = strdup("mmsh"); else /* default file */ *ppsz_access = strdup("file"); } return 0; }
/***************************************************************************** * Open: *****************************************************************************/ static int Open( vlc_object_t *p_this ) { sout_stream_t *p_stream = (sout_stream_t*)p_this; sout_instance_t *p_sout = p_stream->p_sout; sout_stream_sys_t *p_sys; char *psz_mux, *psz_access, *psz_url; sout_access_out_t *p_access; int ret = VLC_EGENERIC; config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options, p_stream->p_cfg ); psz_mux = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "mux" ); psz_access = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "access" ); if( !psz_access ) { if( !strcmp( p_stream->psz_name, "http" ) ) psz_access = strdup("http"); else if (!strcmp (p_stream->psz_name, "udp")) psz_access = strdup("udp"); else if (!strcmp (p_stream->psz_name, "file")) psz_access = strdup("file"); } psz_url = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "dst" ); if (!psz_url) { char *psz_bind = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "bind" ); if( psz_bind ) { char *psz_path = var_GetNonEmptyString( p_stream, SOUT_CFG_PREFIX "path" ); if( psz_path ) { if( asprintf( &psz_url, "%s/%s", psz_bind, psz_path ) == -1 ) psz_url = NULL; free(psz_bind); free( psz_path ); } else psz_url = psz_bind; } } p_sys = p_stream->p_sys = malloc( sizeof( sout_stream_sys_t) ); if( !p_sys ) { ret = VLC_ENOMEM; goto end; } p_sys->p_session = NULL; if( fixAccessMux( p_stream, &psz_mux, &psz_access, psz_url ) ) goto end; checkAccessMux( p_stream, psz_access, psz_mux ); p_access = sout_AccessOutNew( p_sout, psz_access, psz_url ); if( p_access == NULL ) { msg_Err( p_stream, "no suitable sout access module for `%s/%s://%s'", psz_access, psz_mux, psz_url ); goto end; } p_sys->p_mux = sout_MuxNew( p_sout, psz_mux, p_access ); if( !p_sys->p_mux ) { const char *psz_mux_guess = getMuxFromAlias( psz_mux ); if( psz_mux_guess && strcmp( psz_mux_guess, psz_mux ) ) { msg_Dbg( p_stream, "Couldn't open mux `%s', trying `%s' instead", psz_mux, psz_mux_guess ); p_sys->p_mux = sout_MuxNew( p_sout, psz_mux_guess, p_access ); } if( !p_sys->p_mux ) { msg_Err( p_stream, "no suitable sout mux module for `%s/%s://%s'", psz_access, psz_mux, psz_url ); sout_AccessOutDelete( p_access ); goto end; } } if( var_GetBool( p_stream, SOUT_CFG_PREFIX"sap" ) ) create_SDP( p_stream, p_access ); if( !sout_AccessOutCanControlPace( p_access ) ) p_sout->i_out_pace_nocontrol++; p_stream->pf_add = Add; p_stream->pf_del = Del; p_stream->pf_send = Send; ret = VLC_SUCCESS; msg_Dbg( p_this, "using `%s/%s://%s'", psz_access, psz_mux, psz_url ); end: if( ret != VLC_SUCCESS ) free( p_sys ); free( psz_access ); free( psz_mux ); free( psz_url ); return ret; }
static void create_SDP(sout_stream_t *p_stream, sout_access_out_t *p_access) { sout_stream_sys_t *p_sys = p_stream->p_sys; static const struct addrinfo hints = { .ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM, .ai_protocol = 0, .ai_flags = AI_NUMERICHOST | AI_NUMERICSERV }; char *shost = var_GetNonEmptyString (p_access, "src-addr"); char *dhost = var_GetNonEmptyString (p_access, "dst-addr"); int sport = var_GetInteger (p_access, "src-port"); int dport = var_GetInteger (p_access, "dst-port"); struct sockaddr_storage src, dst; socklen_t srclen = 0, dstlen = 0; struct addrinfo *res; if (!vlc_getaddrinfo ( VLC_OBJECT(p_stream), dhost, dport, &hints, &res)) { memcpy (&dst, res->ai_addr, dstlen = res->ai_addrlen); freeaddrinfo (res); } if (!vlc_getaddrinfo ( VLC_OBJECT(p_stream), shost, sport, &hints, &res)) { memcpy (&src, res->ai_addr, srclen = res->ai_addrlen); freeaddrinfo (res); } char *head = vlc_sdp_Start (VLC_OBJECT (p_stream), SOUT_CFG_PREFIX, (struct sockaddr *)&src, srclen, (struct sockaddr *)&dst, dstlen); free (shost); if (head != NULL) { char *psz_sdp = NULL; if (asprintf (&psz_sdp, "%s" "m=video %d udp mpeg\r\n", head, dport) == -1) psz_sdp = NULL; free (head); /* Register the SDP with the SAP thread */ if (psz_sdp) { msg_Dbg (p_stream, "Generated SDP:\n%s", psz_sdp); p_sys->p_session = sout_AnnounceRegisterSDP (p_stream->p_sout, psz_sdp, dhost); free( psz_sdp ); } } free (dhost); } static const char *getMuxFromAlias( const char *psz_alias ) { static struct { const char alias[6]; const char mux[32]; } mux_alias[] = { { "avi", "avi" }, { "ogg", "ogg" }, { "ogm", "ogg" }, { "ogv", "ogg" }, { "flac","raw" }, { "mp3", "raw" }, { "mp4", "mp4" }, { "mov", "mov" }, { "moov","mov" }, { "asf", "asf" }, { "wma", "asf" }, { "wmv", "asf" }, { "trp", "ts" }, { "ts", "ts" }, { "mpg", "ps" }, { "mpeg","ps" }, { "ps", "ps" }, { "mpeg1","mpeg1" }, { "wav", "wav" }, { "flv", "ffmpeg{mux=flv}" }, { "mkv", "ffmpeg{mux=matroska}"}, { "webm", "ffmpeg{mux=webm}"}, }; if( !psz_alias ) return NULL; for( size_t i = 0; i < sizeof mux_alias / sizeof *mux_alias; i++ ) if( !strcasecmp( psz_alias, mux_alias[i].alias ) ) return mux_alias[i].mux; return NULL; } static int fixAccessMux( sout_stream_t *p_stream, char **ppsz_mux, char **ppsz_access, const char *psz_url ) { char *psz_mux = *ppsz_mux; char *psz_access = *ppsz_access; if( !psz_mux ) { const char *psz_ext = psz_url ? strrchr( psz_url, '.' ) : NULL; if( psz_ext ) psz_ext++; /* use extension */ const char *psz_mux_byext = getMuxFromAlias( psz_ext ); if( !psz_access ) { if( !psz_mux_byext ) { msg_Err( p_stream, "no access _and_ no muxer" ); return 1; } msg_Warn( p_stream, "no access _and_ no muxer, extension gives file/%s", psz_mux_byext ); *ppsz_access = strdup("file"); *ppsz_mux = strdup(psz_mux_byext); } else { if( !strncmp( psz_access, "mmsh", 4 ) ) *ppsz_mux = strdup("asfh"); else if (!strcmp (psz_access, "udp")) *ppsz_mux = strdup("ts"); else if( psz_mux_byext ) *ppsz_mux = strdup(psz_mux_byext); else { msg_Err( p_stream, "no mux specified or found by extension" ); return 1; } } } else if( !psz_access ) { if( !strncmp( psz_mux, "asfh", 4 ) ) *ppsz_access = strdup("mmsh"); else /* default file */ *ppsz_access = strdup("file"); } return 0; }