s/sout_stream_id_t/sout_stream_id_sys_t/
[vlc.git] / modules / stream_out / stats.c
blobc8c03d24f0bead7d4570cafbb0e296c49ae002d2
1 /*****************************************************************************
2 * delay.c: delay a stream
3 *****************************************************************************
4 * Copyright © 2014 VLC authors and VideoLAN
6 * Authors: Ilkka Ollakka <ileoo at videolan dot org>
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
23 /*****************************************************************************
24 * Preamble
25 *****************************************************************************/
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_sout.h>
33 #include <vlc_block.h>
34 #include <vlc_md5.h>
35 #include <vlc_fs.h>
37 /*****************************************************************************
38 * Module descriptor
39 *****************************************************************************/
40 #define OUTPUT_TEXT N_("Output file")
41 #define OUTPUT_LONGTEXT N_( \
42 "Writes stats to file instead of stdout" )
43 #define PREFIX_TEXT N_("Prefix to show on output line")
45 static int Open ( vlc_object_t * );
46 static void Close ( vlc_object_t * );
48 #define SOUT_CFG_PREFIX "sout-stats-"
50 vlc_module_begin()
51 set_shortname( N_("Stats"))
52 set_description( N_("Writes statistic info about stream"))
53 set_capability( "sout stream", 0 )
54 add_shortcut( "stats" )
55 set_category( CAT_SOUT )
56 set_subcategory( SUBCAT_SOUT_STREAM )
57 set_callbacks( Open, Close )
58 add_string( SOUT_CFG_PREFIX "output", "", OUTPUT_TEXT,OUTPUT_LONGTEXT, false );
59 add_string( SOUT_CFG_PREFIX "prefix", "stats", PREFIX_TEXT,PREFIX_TEXT, false );
60 vlc_module_end()
63 /*****************************************************************************
64 * Local prototypes
65 *****************************************************************************/
66 static const char *ppsz_sout_options[] = {
67 "output", "prefix", NULL
70 static sout_stream_id_sys_t *Add ( sout_stream_t *, es_format_t * );
71 static int Del ( sout_stream_t *, sout_stream_id_sys_t * );
72 static int Send ( sout_stream_t *, sout_stream_id_sys_t *, block_t * );
74 struct sout_stream_sys_t
76 FILE *output;
77 char *prefix;
80 struct sout_stream_id_sys_t
82 int id;
83 uint64_t segment_number;
84 void *next_id;
85 const char *type;
86 mtime_t previous_dts;
87 struct md5_s hash;
90 /*****************************************************************************
91 * Open:
92 *****************************************************************************/
93 static int Open( vlc_object_t *p_this )
95 sout_stream_t *p_stream = (sout_stream_t*)p_this;
96 sout_stream_sys_t *p_sys;
97 char *outputFile;
99 p_sys = calloc( 1, sizeof( sout_stream_sys_t ) );
100 if( !p_sys )
101 return VLC_ENOMEM;
104 config_ChainParse( p_stream, SOUT_CFG_PREFIX, ppsz_sout_options,
105 p_stream->p_cfg );
108 outputFile = var_InheritString( p_stream, SOUT_CFG_PREFIX "output" );
110 if( outputFile )
112 p_sys->output = vlc_fopen( outputFile, "wt" );
113 if( !p_sys->output )
115 msg_Err( p_stream, "Unable to open file '%s' for writing", outputFile );
116 free( p_sys );
117 free( outputFile );
118 return VLC_EGENERIC;
120 free( outputFile );
122 p_sys->prefix = var_InheritString( p_stream, SOUT_CFG_PREFIX "prefix" );
124 p_stream->p_sys = p_sys;
126 p_stream->pf_add = Add;
127 p_stream->pf_del = Del;
128 p_stream->pf_send = Send;
131 return VLC_SUCCESS;
134 /*****************************************************************************
135 * Close:
136 *****************************************************************************/
137 static void Close( vlc_object_t * p_this )
139 sout_stream_t *p_stream = (sout_stream_t*)p_this;
140 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
142 if( p_sys->output )
143 fclose( p_sys->output );
145 free( p_sys->prefix );
146 free( p_sys );
149 static sout_stream_id_sys_t * Add( sout_stream_t *p_stream, es_format_t *p_fmt )
151 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
152 sout_stream_id_sys_t *id;
154 id = malloc( sizeof( sout_stream_id_sys_t ) );
155 if( unlikely( !id ) )
156 return NULL;
158 id->id = p_fmt->i_id;
159 switch( p_fmt->i_cat)
161 case VIDEO_ES:
162 id->type = "Video";
163 break;
164 case AUDIO_ES:
165 id->type = "Audio";
166 break;
167 case SPU_ES:
168 id->type = "SPU";
169 break;
170 default:
171 id->type = "Data";
172 break;
174 id->next_id = NULL;
175 id->segment_number = 0;
176 id->previous_dts = VLC_TS_INVALID;
177 InitMD5( &id->hash );
179 msg_Dbg( p_stream, "%s: Adding track type:%s id:%d", p_sys->prefix, id->type, id->id);
181 if( p_stream->p_next )
182 id->next_id = sout_StreamIdAdd( p_stream->p_next, p_fmt );
184 return id;
187 static int Del( sout_stream_t *p_stream, sout_stream_id_sys_t *id )
189 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
191 EndMD5( &id->hash );
192 char *outputhash = psz_md5_hash( &id->hash );
193 msg_Dbg( p_stream, "%s: Removing track type:%s id:%d", p_sys->prefix, id->type, id->id );
194 if( p_sys->output )
196 fprintf( p_sys->output,"%s: final type:%s id:%d segments:%"PRIu64" md5:%16s\n",
197 p_sys->prefix, id->type, id->id, id->segment_number, outputhash );
198 } else {
199 msg_Info( p_stream, "%s: final type:%s id:%d segments:%"PRIu64" md5:%16s",
200 p_sys->prefix, id->type, id->id, id->segment_number, outputhash );
202 free( outputhash );
203 if( id->next_id ) sout_StreamIdDel( p_stream->p_next, id->next_id );
204 free( id );
206 return VLC_SUCCESS;
209 static int Send( sout_stream_t *p_stream, sout_stream_id_sys_t *id,
210 block_t *p_buffer )
212 sout_stream_sys_t *p_sys = (sout_stream_sys_t *)p_stream->p_sys;
213 struct md5_s hash;
215 block_t *p_block = p_buffer;
216 while ( p_block != NULL )
218 InitMD5( &hash );
219 AddMD5( &hash, p_block->p_buffer, p_block->i_buffer );
220 AddMD5( &id->hash, p_block->p_buffer, p_block->i_buffer );
221 EndMD5( &hash );
222 char *outputhash = psz_md5_hash( &hash );
224 /* We could just set p_sys->output to stdout and remove user of msg_Dbg
225 * if we don't need ability to output info to gui modules (like qt4 messages window
227 if( p_sys->output )
229 fprintf( p_sys->output, "%s: track:%d type:%s segment_number:%"PRIu64" dts_difference:%"PRId64" length:%"PRId64" md5:%16s\n",
230 p_sys->prefix, id->id, id->type, ++id->segment_number, p_block->i_dts - id->previous_dts,
231 p_block->i_length, outputhash );
233 } else {
234 msg_Dbg( p_stream, "%s: track:%d type:%s segment_number:%"PRIu64" dts_difference:%"PRId64" length:%"PRId64" md5:%16s",
235 p_sys->prefix, id->id, id->type, ++id->segment_number, p_block->i_dts - id->previous_dts,
236 p_block->i_length, outputhash );
238 free( outputhash );
239 id->previous_dts = p_block->i_dts;
240 p_block = p_block->p_next;
243 if( p_stream->p_next )
244 return sout_StreamIdSend( p_stream->p_next, id->next_id, p_buffer );
245 else
246 block_Release( p_buffer );
247 return VLC_SUCCESS;