[89ff87]: ogg123 / status.c Maximize Restore History

Download this file

status.c    466 lines (343 with data), 10.9 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
/********************************************************************
* *
* THIS FILE IS PART OF THE OggVorbis SOFTWARE CODEC SOURCE CODE. *
* USE, DISTRIBUTION AND REPRODUCTION OF THIS SOURCE IS GOVERNED BY *
* THE GNU PUBLIC LICENSE 2, WHICH IS INCLUDED WITH THIS SOURCE. *
* PLEASE READ THESE TERMS BEFORE DISTRIBUTING. *
* *
* THE Ogg123 SOURCE CODE IS (C) COPYRIGHT 2000-2001 *
* by Kenneth C. Arnold <ogg@arnoldnet.net> AND OTHER CONTRIBUTORS *
* http://www.xiph.org/ *
* *
********************************************************************
last mod: $Id: status.c,v 1.8 2002/03/19 09:50:09 msmith Exp $
********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include "status.h"
#include "i18n.h"
char temp_buffer[200];
int last_line_len = 0;
int max_verbosity = 0;
pthread_mutex_t output_lock = PTHREAD_MUTEX_INITIALIZER;
/* ------------------- Private functions ------------------ */
void unlock_output_lock (void *arg)
{
pthread_mutex_unlock(&output_lock);
}
void write_buffer_state_string (char *dest, buffer_stats_t *buf_stats)
{
char *cur = dest;
char *comma = ", ";
char *sep = "(";
if (buf_stats->prebuffering) {
cur += sprintf (cur, _("%sPrebuf to %.1f%%"), sep,
100.0f * buf_stats->prebuffer_fill);
sep = comma;
}
if (buf_stats->paused) {
cur += sprintf (cur, _("%sPaused"), sep);
sep = comma;
}
if (buf_stats->eos) {
cur += sprintf (cur, _("%sEOS"), sep);
sep = comma;
}
if (cur != dest)
cur += sprintf (cur, ")");
else
*cur = '\0';
}
/* Write a min:sec.msec style string to dest corresponding to time.
The time parameter is in seconds. Returns the number of characters
written */
int write_time_string (char *dest, double time)
{
long min = (long) time / (long) 60;
double sec = time - 60.0f * min;
return sprintf (dest, "%02li:%05.2f", min, sec);
}
void clear_line (int len)
{
fputc('\r', stderr);
while (len > 0) {
fputc (' ', stderr);
len--;
}
fputc ('\r', stderr);
}
int sprintf_clear_line(int len, char *buf)
{
int i = 0;
buf[i] = '\r';
i++;
while (len > 0) {
buf[i] = ' ';
len--;
i++;
}
buf[i] = '\r';
i++;
/* Null terminate just in case */
buf[i] = '\0';
return i;
}
int print_statistics_line (stat_format_t stats[])
{
int len = 0;
char *str = temp_buffer;
if (max_verbosity == 0)
return 0;
/* Put the clear line text into the same string buffer so that the
line is cleared and redrawn all at once. This reduces
flickering. Don't count characters used to clear line in len */
str += sprintf_clear_line(last_line_len, str);
while (stats->formatstr != NULL) {
if (stats->verbosity > max_verbosity || !stats->enabled) {
stats++;
continue;
}
if (len != 0)
len += sprintf(str+len, " ");
switch (stats->type) {
case stat_noarg:
len += sprintf(str+len, stats->formatstr);
break;
case stat_intarg:
len += sprintf(str+len, stats->formatstr, stats->arg.intarg);
break;
case stat_stringarg:
len += sprintf(str+len, stats->formatstr, stats->arg.stringarg);
break;
case stat_floatarg:
len += sprintf(str+len, stats->formatstr, stats->arg.floatarg);
break;
case stat_doublearg:
len += sprintf(str+len, stats->formatstr, stats->arg.doublearg);
break;
}
stats++;
}
len += sprintf(str+len, "\r");
fprintf(stderr, "%s", temp_buffer);
return len;
}
void vstatus_print_nolock (const char *fmt, va_list ap)
{
if (last_line_len != 0)
fputc ('\n', stderr);
vfprintf (stderr, fmt, ap);
fputc ('\n', stderr);
last_line_len = 0;
}
/* ------------------- Public interface -------------------- */
#define TIME_STR_SIZE 20
#define STATE_STR_SIZE 25
#define NUM_STATS 10
stat_format_t *stat_format_create ()
{
stat_format_t *stats;
stat_format_t *cur;
stats = calloc(NUM_STATS + 1, sizeof(stat_format_t)); /* One extra for end flag */
if (stats == NULL) {
fprintf(stderr, _("Memory allocation error in stats_init()\n"));
exit(1);
}
cur = stats + 0; /* currently playing file / stream */
cur->verbosity = 3;
cur->enabled = 0;
cur->formatstr = _("File: %s");
cur->type = stat_stringarg;
cur = stats + 1; /* current playback time (preformatted) */
cur->verbosity = 1;
cur->enabled = 1;
cur->formatstr = _("Time: %s");
cur->type = stat_stringarg;
cur->arg.stringarg = calloc(TIME_STR_SIZE, sizeof(char));
if (cur->arg.stringarg == NULL) {
fprintf(stderr, _("Memory allocation error in stats_init()\n"));
exit(1);
}
write_time_string(cur->arg.stringarg, 0.0);
cur = stats + 2; /* remaining playback time (preformatted) */
cur->verbosity = 1;
cur->enabled = 1;
cur->formatstr = "[%s]";
cur->type = stat_stringarg;
cur->arg.stringarg = calloc(TIME_STR_SIZE, sizeof(char));
if (cur->arg.stringarg == NULL) {
fprintf(stderr, _("Memory allocation error in stats_init()\n"));
exit(1);
}
write_time_string(cur->arg.stringarg, 0.0);
cur = stats + 3; /* total playback time (preformatted) */
cur->verbosity = 1;
cur->enabled = 1;
cur->formatstr = _("of %s");
cur->type = stat_stringarg;
cur->arg.stringarg = calloc(TIME_STR_SIZE, sizeof(char));
if (cur->arg.stringarg == NULL) {
fprintf(stderr, _("Memory allocation error in stats_init()\n"));
exit(1);
}
write_time_string(cur->arg.stringarg, 0.0);
cur = stats + 4; /* instantaneous bitrate */
cur->verbosity = 2;
cur->enabled = 1;
cur->formatstr = " (%5.1f kbps)";
cur->type = stat_doublearg;
cur = stats + 5; /* average bitrate (not yet implemented) */
cur->verbosity = 2;
cur->enabled = 0;
cur->formatstr = _("Avg bitrate: %5.1f");
cur->type = stat_doublearg;
cur = stats + 6; /* input buffer fill % */
cur->verbosity = 2;
cur->enabled = 0;
cur->formatstr = _(" Input Buffer %5.1f%%");
cur->type = stat_doublearg;
cur = stats + 7; /* input buffer status */
cur->verbosity = 2;
cur->enabled = 0;
cur->formatstr = "%s";
cur->type = stat_stringarg;
cur->arg.stringarg = calloc(STATE_STR_SIZE, sizeof(char));
if (cur->arg.stringarg == NULL) {
fprintf(stderr, _("Memory allocation error in stats_init()\n"));
exit(1);
}
cur = stats + 8; /* output buffer fill % */
cur->verbosity = 2;
cur->enabled = 0;
cur->formatstr = _(" Output Buffer %5.1f%%");
cur->type = stat_doublearg;
cur = stats + 9; /* output buffer status */
cur->verbosity = 1;
cur->enabled = 0;
cur->formatstr = "%s";
cur->type = stat_stringarg;
cur->arg.stringarg = calloc(STATE_STR_SIZE, sizeof(char));
if (cur->arg.stringarg == NULL) {
fprintf(stderr, _("Memory allocation error in stats_init()\n"));
exit(1);
}
cur = stats + 10; /* End flag */
cur->formatstr = NULL;
return stats;
}
void stat_format_cleanup (stat_format_t *stats)
{
free(stats[1].arg.stringarg);
free(stats[2].arg.stringarg);
free(stats[3].arg.stringarg);
free(stats[7].arg.stringarg);
free(stats[9].arg.stringarg);
free(stats);
}
void status_set_verbosity (int verbosity)
{
max_verbosity = verbosity;
}
void status_reset_output_lock ()
{
pthread_mutex_unlock(&output_lock);
}
void status_clear_line ()
{
pthread_cleanup_push(unlock_output_lock, NULL);
pthread_mutex_lock(&output_lock);
clear_line(last_line_len);
pthread_mutex_unlock(&output_lock);
pthread_cleanup_pop(0);
}
void status_print_statistics (stat_format_t *stats,
buffer_stats_t *audio_statistics,
data_source_stats_t *transport_statistics,
decoder_stats_t *decoder_statistics)
{
pthread_cleanup_push(unlock_output_lock, NULL);
/* Updating statistics is not critical. If another thread is
already doing output, we skip it. */
if (pthread_mutex_trylock(&output_lock) == 0) {
if (decoder_statistics != NULL) {
/* Current playback time */
write_time_string(stats[1].arg.stringarg,
decoder_statistics->current_time);
/* Remaining playback time */
write_time_string(stats[2].arg.stringarg,
decoder_statistics->total_time -
decoder_statistics->current_time);
/* Total playback time */
write_time_string(stats[3].arg.stringarg,
decoder_statistics->total_time);
/* Instantaneous bitrate */
stats[4].arg.doublearg = decoder_statistics->instant_bitrate / 1000.0f;
/* Instantaneous bitrate */
stats[5].arg.doublearg = decoder_statistics->avg_bitrate / 1000.0f;
}
if (transport_statistics != NULL &&
transport_statistics->input_buffer_used) {
/* Input buffer fill % */
stats[6].arg.doublearg = transport_statistics->input_buffer.fill;
/* Input buffer state */
write_buffer_state_string(stats[7].arg.stringarg,
&transport_statistics->input_buffer);
}
if (audio_statistics != NULL) {
/* Output buffer fill % */
stats[8].arg.doublearg = audio_statistics->fill;
/* Output buffer state */
write_buffer_state_string(stats[9].arg.stringarg, audio_statistics);
}
last_line_len = print_statistics_line(stats);
pthread_mutex_unlock(&output_lock);
}
pthread_cleanup_pop(0);
}
void status_message (int verbosity, const char *fmt, ...)
{
va_list ap;
if (verbosity > max_verbosity)
return;
pthread_cleanup_push(unlock_output_lock, NULL);
pthread_mutex_lock(&output_lock);
clear_line(last_line_len);
va_start (ap, fmt);
vstatus_print_nolock(fmt, ap);
va_end (ap);
pthread_mutex_unlock(&output_lock);
pthread_cleanup_pop(0);
}
void vstatus_message (int verbosity, const char *fmt, va_list ap)
{
if (verbosity > max_verbosity)
return;
pthread_cleanup_push(unlock_output_lock, NULL);
pthread_mutex_lock(&output_lock);
clear_line(last_line_len);
vstatus_print_nolock(fmt, ap);
pthread_mutex_unlock(&output_lock);
pthread_cleanup_pop(0);
}
void status_error (const char *fmt, ...)
{
va_list ap;
pthread_cleanup_push(unlock_output_lock, NULL);
pthread_mutex_lock(&output_lock);
va_start (ap, fmt);
clear_line(last_line_len);
vstatus_print_nolock (fmt, ap);
va_end (ap);
pthread_mutex_unlock(&output_lock);
pthread_cleanup_pop(0);
}
void vstatus_error (const char *fmt, va_list ap)
{
pthread_cleanup_push(unlock_output_lock, NULL);
pthread_mutex_lock(&output_lock);
clear_line(last_line_len);
vstatus_print_nolock (fmt, ap);
pthread_mutex_unlock(&output_lock);
pthread_cleanup_pop(0);
}