mirror of
https://github.com/mpv-player/mpv
synced 2024-08-04 14:59:58 +02:00
commands: clean up get_metadata() and related code
Code in get_metadata() allocated too small a buffer for the text it wrote (noticed by Clément Bœsch). Make the code cleaner and more robust by changing it to use talloc_asprintf(). Also make it always return non-NULL and remove checks on caller side.
This commit is contained in:
parent
2bc2399797
commit
835d3deea2
110
command.c
110
command.c
@ -3278,126 +3278,100 @@ void run_command(MPContext *mpctx, mp_cmd_t *cmd)
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_FILENAME:{
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_FILENAME='%s'\n",
|
||||
get_metadata(mpctx, META_NAME));
|
||||
char *inf = get_metadata(mpctx, META_NAME);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_FILENAME='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_VIDEO_CODEC:{
|
||||
char *inf = get_metadata(mpctx, META_VIDEO_CODEC);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_VIDEO_CODEC='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_VIDEO_CODEC);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_VIDEO_CODEC='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_VIDEO_BITRATE:{
|
||||
char *inf = get_metadata(mpctx, META_VIDEO_BITRATE);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_VIDEO_BITRATE='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_VIDEO_BITRATE);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_VIDEO_BITRATE='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_VIDEO_RESOLUTION:{
|
||||
char *inf = get_metadata(mpctx, META_VIDEO_RESOLUTION);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO,
|
||||
"ANS_VIDEO_RESOLUTION='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_VIDEO_RESOLUTION);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_VIDEO_RESOLUTION='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_AUDIO_CODEC:{
|
||||
char *inf = get_metadata(mpctx, META_AUDIO_CODEC);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_AUDIO_CODEC='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_AUDIO_CODEC);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_AUDIO_CODEC='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_AUDIO_BITRATE:{
|
||||
char *inf = get_metadata(mpctx, META_AUDIO_BITRATE);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_AUDIO_BITRATE='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_AUDIO_BITRATE);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_AUDIO_BITRATE='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_AUDIO_SAMPLES:{
|
||||
char *inf = get_metadata(mpctx, META_AUDIO_SAMPLES);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_AUDIO_SAMPLES='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_AUDIO_SAMPLES);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_AUDIO_SAMPLES='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_META_TITLE:{
|
||||
char *inf = get_metadata(mpctx, META_INFO_TITLE);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_TITLE='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_INFO_TITLE);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_TITLE='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_META_ARTIST:{
|
||||
char *inf = get_metadata(mpctx, META_INFO_ARTIST);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_ARTIST='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_INFO_ARTIST);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_ARTIST='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_META_ALBUM:{
|
||||
char *inf = get_metadata(mpctx, META_INFO_ALBUM);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_ALBUM='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_INFO_ALBUM);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_ALBUM='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_META_YEAR:{
|
||||
char *inf = get_metadata(mpctx, META_INFO_YEAR);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_YEAR='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_INFO_YEAR);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_YEAR='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_META_COMMENT:{
|
||||
char *inf = get_metadata(mpctx, META_INFO_COMMENT);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_COMMENT='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_INFO_COMMENT);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_COMMENT='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_META_TRACK:{
|
||||
char *inf = get_metadata(mpctx, META_INFO_TRACK);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_TRACK='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_INFO_TRACK);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_TRACK='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
case MP_CMD_GET_META_GENRE:{
|
||||
char *inf = get_metadata(mpctx, META_INFO_GENRE);
|
||||
if (!inf)
|
||||
inf = strdup("");
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_GENRE='%s'\n", inf);
|
||||
free(inf);
|
||||
char *inf = get_metadata(mpctx, META_INFO_GENRE);
|
||||
mp_msg(MSGT_GLOBAL, MSGL_INFO, "ANS_META_GENRE='%s'\n", inf);
|
||||
talloc_free(inf);
|
||||
}
|
||||
break;
|
||||
|
||||
|
111
mplayer.c
111
mplayer.c
@ -453,88 +453,55 @@ static char *get_demuxer_info(struct MPContext *mpctx, char *tag) {
|
||||
int n;
|
||||
|
||||
if (!info || !tag)
|
||||
return NULL;
|
||||
return talloc_strdup(NULL, "");
|
||||
|
||||
for (n = 0; info[2*n] != NULL ; n++)
|
||||
if (!strcasecmp (info[2*n], tag))
|
||||
break;
|
||||
|
||||
return info[2*n+1] ? strdup (info[2*n+1]) : NULL;
|
||||
return talloc_strdup(NULL, info[2*n+1] ? info[2*n+1] : "");
|
||||
}
|
||||
|
||||
char *get_metadata(struct MPContext *mpctx, metadata_t type)
|
||||
{
|
||||
char *meta = NULL;
|
||||
sh_audio_t * const sh_audio = mpctx->sh_audio;
|
||||
sh_video_t * const sh_video = mpctx->sh_video;
|
||||
sh_audio_t * const sh_audio = mpctx->sh_audio;
|
||||
sh_video_t * const sh_video = mpctx->sh_video;
|
||||
|
||||
if (!is_valid_metadata_type(mpctx, type))
|
||||
return NULL;
|
||||
if (!is_valid_metadata_type(mpctx, type))
|
||||
return NULL;
|
||||
|
||||
switch (type)
|
||||
{
|
||||
case META_NAME:
|
||||
{
|
||||
return strdup(mp_basename(mpctx->filename));
|
||||
}
|
||||
|
||||
case META_VIDEO_CODEC:
|
||||
{
|
||||
if (sh_video->format == 0x10000001)
|
||||
meta = strdup ("mpeg1");
|
||||
else if (sh_video->format == 0x10000002)
|
||||
meta = strdup ("mpeg2");
|
||||
else if (sh_video->format == 0x10000004)
|
||||
meta = strdup ("mpeg4");
|
||||
else if (sh_video->format == 0x10000005)
|
||||
meta = strdup ("h264");
|
||||
else if (sh_video->format >= 0x20202020)
|
||||
{
|
||||
meta = malloc (8);
|
||||
sprintf (meta, "%.4s", (char *) &sh_video->format);
|
||||
}
|
||||
else
|
||||
{
|
||||
meta = malloc (8);
|
||||
sprintf (meta, "0x%08X", sh_video->format);
|
||||
}
|
||||
return meta;
|
||||
}
|
||||
|
||||
case META_VIDEO_BITRATE:
|
||||
{
|
||||
meta = malloc (16);
|
||||
sprintf (meta, "%d kbps", (int) (sh_video->i_bps * 8 / 1024));
|
||||
return meta;
|
||||
}
|
||||
|
||||
case META_VIDEO_RESOLUTION:
|
||||
{
|
||||
meta = malloc (16);
|
||||
sprintf (meta, "%d x %d", sh_video->disp_w, sh_video->disp_h);
|
||||
return meta;
|
||||
}
|
||||
|
||||
case META_AUDIO_CODEC:
|
||||
{
|
||||
if (sh_audio->codec && sh_audio->codec->name)
|
||||
meta = strdup (sh_audio->codec->name);
|
||||
return meta;
|
||||
}
|
||||
|
||||
case META_AUDIO_BITRATE:
|
||||
{
|
||||
meta = malloc (16);
|
||||
sprintf (meta, "%d kbps", (int) (sh_audio->i_bps * 8/1000));
|
||||
return meta;
|
||||
}
|
||||
|
||||
case META_AUDIO_SAMPLES:
|
||||
{
|
||||
meta = malloc (16);
|
||||
sprintf (meta, "%d Hz, %d ch.", sh_audio->samplerate, sh_audio->channels);
|
||||
return meta;
|
||||
}
|
||||
switch (type) {
|
||||
case META_NAME:
|
||||
return talloc_strdup(NULL, mp_basename(mpctx->filename));
|
||||
case META_VIDEO_CODEC:
|
||||
if (sh_video->format == 0x10000001)
|
||||
return talloc_strdup(NULL, "mpeg1");
|
||||
else if (sh_video->format == 0x10000002)
|
||||
return talloc_strdup(NULL, "mpeg2");
|
||||
else if (sh_video->format == 0x10000004)
|
||||
return talloc_strdup(NULL, "mpeg4");
|
||||
else if (sh_video->format == 0x10000005)
|
||||
return talloc_strdup(NULL, "h264");
|
||||
else if (sh_video->format >= 0x20202020)
|
||||
return talloc_asprintf(NULL, "%.4s", (char *) &sh_video->format);
|
||||
else
|
||||
return talloc_asprintf(NULL, "0x%08X", sh_video->format);
|
||||
case META_VIDEO_BITRATE:
|
||||
return talloc_asprintf(NULL, "%d kbps",
|
||||
(int) (sh_video->i_bps * 8 / 1024));
|
||||
case META_VIDEO_RESOLUTION:
|
||||
return talloc_asprintf(NULL, "%d x %d", sh_video->disp_w,
|
||||
sh_video->disp_h);
|
||||
case META_AUDIO_CODEC:
|
||||
if (sh_audio->codec && sh_audio->codec->name)
|
||||
return talloc_strdup(NULL, sh_audio->codec->name);
|
||||
return talloc_strdup(NULL, "");
|
||||
case META_AUDIO_BITRATE:
|
||||
return talloc_asprintf(NULL, "%d kbps",
|
||||
(int) (sh_audio->i_bps * 8/1000));
|
||||
case META_AUDIO_SAMPLES:
|
||||
return talloc_asprintf(NULL, "%d Hz, %d ch.", sh_audio->samplerate,
|
||||
sh_audio->channels);
|
||||
|
||||
/* check for valid demuxer */
|
||||
case META_INFO_TITLE:
|
||||
@ -562,7 +529,7 @@ char *get_metadata(struct MPContext *mpctx, metadata_t type)
|
||||
break;
|
||||
}
|
||||
|
||||
return meta;
|
||||
return talloc_strdup(NULL, "");
|
||||
}
|
||||
|
||||
static void print_file_properties(struct MPContext *mpctx, const char *filename)
|
||||
|
Loading…
Reference in New Issue
Block a user