[xiph-commits] r12711 - in trunk/ezstream: . src

moritz at svn.xiph.org moritz at svn.xiph.org
Sat Mar 10 13:18:27 PST 2007


Author: moritz
Date: 2007-03-10 13:18:21 -0800 (Sat, 10 Mar 2007)
New Revision: 12711

Modified:
   trunk/ezstream/NEWS
   trunk/ezstream/src/ezstream.c
   trunk/ezstream/src/metadata.c
   trunk/ezstream/src/metadata.h
Log:
Include the total playing time of a song in the "real-time" output, if TagLib
supplies it.


Modified: trunk/ezstream/NEWS
===================================================================
--- trunk/ezstream/NEWS	2007-03-10 20:13:56 UTC (rev 12710)
+++ trunk/ezstream/NEWS	2007-03-10 21:18:21 UTC (rev 12711)
@@ -19,6 +19,8 @@
  * various:
    - [ADD]   Allow ezstream to use TagLib for reading metadata from media
              files. TagLib (libtag_c) is now an optional dependency.
+   - [ADD]   When built with TagLib support, include the song length in the
+             "real-time" information line, if available.
    - [ADD]   New <metadata_progname> configuration option, which causes
              metadata to be read from the output of an external program or
              script.

Modified: trunk/ezstream/src/ezstream.c
===================================================================
--- trunk/ezstream/src/ezstream.c	2007-03-10 20:13:56 UTC (rev 12710)
+++ trunk/ezstream/src/ezstream.c	2007-03-10 21:18:21 UTC (rev 12711)
@@ -110,14 +110,18 @@
 } ID3Tag;
 
 int		urlParse(const char *, char **, int *, char **);
-void		replaceString(const char *, char *, size_t, const char *, const char *);
+void		replaceString(const char *, char *, size_t, const char *,
+			      const char *);
 char *		buildCommandString(const char *, const char *, metadata_t *);
 char *		getMetadataString(const char *, metadata_t *);
 metadata_t *	getMetadata(const char *);
 int		setMetadata(shout_t *, metadata_t *, char **);
-FILE *		openResource(shout_t *, const char *, int *, char **, int *);
+FILE *		openResource(shout_t *, const char *, int *, char **, int *,
+			     int *);
 int		reconnectServer(shout_t *, int);
-int		sendStream(shout_t *, FILE *, const char *, int, void *);
+const char *	getTimeString(int);
+int		sendStream(shout_t *, FILE *, const char *, int, const char *,
+			   void *);
 int		streamFile(shout_t *, const char *);
 int		streamPlaylist(shout_t *, const char *);
 char *		getProgname(const char *);
@@ -505,7 +509,7 @@
 
 FILE *
 openResource(shout_t *shout, const char *fileName, int *popenFlag,
-	     char **metaCopy, int *isStdin)
+	     char **metaCopy, int *isStdin, int *songLen)
 {
 	FILE		*filep = NULL;
 	char		 extension[25];
@@ -561,6 +565,8 @@
 	}
 	if (metaCopy != NULL)
 		*metaCopy = metadata_assemble_string(mdata);
+	if (songLen != NULL)
+		*songLen = metadata_get_length(mdata);
 
 	*popenFlag = 0;
 	if (pezConfig->reencode) {
@@ -661,9 +667,28 @@
 	return (0);
 }
 
+const char *
+getTimeString(int seconds)
+{
+	static char	str[20];
+	int		secs, mins, hours;
+
+	if (seconds < 0)
+		return (NULL);
+
+	secs = seconds;
+	hours = secs / 3600;
+	secs %= 3600;
+	mins = secs / 60;
+	secs %= 60;
+
+	snprintf(str, sizeof(str), "%uh%02um%02us", hours, mins, secs);
+	return ((const char *)str);
+}
+
 int
 sendStream(shout_t *shout, FILE *filepstream, const char *fileName,
-	   int isStdin, void *tv)
+	   int isStdin, const char *songLenStr, void *tv)
 {
 	unsigned char	 buff[4096];
 	size_t		 read, total, oldTotal;
@@ -728,7 +753,6 @@
 #ifdef HAVE_GETTIMEOFDAY
 			struct timeval	tv;
 			double		oldTime, newTime;
-			unsigned int	hrs, mins, secs;
 #endif /* HAVE_GETTIMEOFDAY */
 
 			if (!isStdin && playlistMode) {
@@ -749,18 +773,19 @@
 			gettimeofday(&tv, NULL);
 			newTime = (double)tv.tv_sec
 				+ (double)tv.tv_usec / 1000000.0;
-			secs = tv.tv_sec - startTime->tv_sec;
-			hrs = secs / 3600;
-			secs %= 3600;
-			mins = secs / 60;
-			secs %= 60;
+			if (songLenStr == NULL)
+				printf("  [ %s]",
+				       getTimeString(tv.tv_sec - startTime->tv_sec));
+			else
+				printf("  [ %s/%s]",
+				       getTimeString(tv.tv_sec - startTime->tv_sec),
+				       songLenStr);
 			if (newTime - oldTime >= 1.0) {
 				kbps = (((double)(total - oldTotal) / (newTime - oldTime)) * 8.0) / 1000.0;
 				timeStamp.tv_sec = tv.tv_sec;
 				timeStamp.tv_usec = tv.tv_usec;
 				oldTotal = total;
 			}
-			printf("  [ %uh%02um%02us]", hrs, mins, secs);
 			if (kbps < 0)
 				printf("                 ");
 			else
@@ -788,15 +813,15 @@
 {
 	FILE		*filepstream = NULL;
 	int		 popenFlag = 0;
-	char		*metaData = NULL;
+	char		*metaData = NULL, *songLenStr = NULL;
 	int		 isStdin = 0;
-	int              ret, retval = 0;
+	int		 ret, retval = 0, songLen;
 #ifdef HAVE_GETTIMEOFDAY
 	struct timeval	 startTime;
 #endif
 
 	if ((filepstream = openResource(shout, fileName, &popenFlag,
-					&metaData, &isStdin))
+					&metaData, &isStdin, &songLen))
 	    == NULL) {
 		return (retval);
 	}
@@ -811,13 +836,15 @@
 	}
 
 #ifdef HAVE_GETTIMEOFDAY
+	if (songLen >= 0)
+		songLenStr = xstrdup(getTimeString(songLen));
 	gettimeofday(&startTime, NULL);
 	do {
 		ret = sendStream(shout, filepstream, fileName, isStdin,
-				 (void *)&startTime);
+				 songLenStr, (void *)&startTime);
 #else
 	do {
-		ret = sendStream(shout, filepstream, fileName, isStdin, NULL);
+		ret = sendStream(shout, filepstream, fileName, isStdin, NULL, NULL);
 #endif
 		if (ret != STREAM_DONE) {
 			if ((skipTrack && rereadPlaylist) ||
@@ -875,6 +902,9 @@
 	else
 		fclose(filepstream);
 
+	if (songLenStr != NULL)
+		xfree(songLenStr);
+
 	return (retval);
 }
 

Modified: trunk/ezstream/src/metadata.c
===================================================================
--- trunk/ezstream/src/metadata.c	2007-03-10 20:13:56 UTC (rev 12710)
+++ trunk/ezstream/src/metadata.c	2007-03-10 21:18:21 UTC (rev 12711)
@@ -53,6 +53,7 @@
 	char	*string;
 	char	*artist;
 	char	*title;
+	int	 songLen;
 	int	 program;
 };
 
@@ -81,6 +82,7 @@
 
 	md = xcalloc(1, sizeof(metadata_t));
 	md->filename = xstrdup(filename);
+	md->songLen = -1;
 
 	return (md);
 }
@@ -89,9 +91,10 @@
 metadata_use_taglib(metadata_t *md, FILE **filep)
 #ifdef HAVE_TAGLIB
 {
-	TagLib_File	*tf;
-	TagLib_Tag	*tt;
-	char		*str;
+	TagLib_File		*tf;
+	TagLib_Tag		*tt;
+	TagLib_AudioProperties	*ta;
+	char			*str;
 
 	if (md == NULL || md->filename == NULL) {
 		printf("%s: metadata_use_taglib(): Internal error: Bad arguments\n",
@@ -113,7 +116,9 @@
 		md->string = metadata_get_name(md->filename);
 		return;
 	}
+
 	tt = taglib_file_tag(tf);
+	ta = taglib_file_audioproperties(tf);
 
 	str = taglib_tag_artist(tt);
 	if (str != NULL) {
@@ -129,6 +134,8 @@
 		xfree(str);
 	}
 
+	md->songLen = taglib_audioproperties_length(ta);
+
 	taglib_file_free(tf);
 }
 #else
@@ -602,6 +609,18 @@
 		return (md->filename);
 }
 
+int
+metadata_get_length(metadata_t *md)
+{
+	if (md == NULL) {
+		printf("%s: metadata_get_length(): Internal error: Bad arguments\n",
+		       __progname);
+		abort();
+	}
+
+	return (md->songLen);
+}
+
 char *
 metadata_assemble_string(metadata_t *md)
 {

Modified: trunk/ezstream/src/metadata.h
===================================================================
--- trunk/ezstream/src/metadata.h	2007-03-10 20:13:56 UTC (rev 12710)
+++ trunk/ezstream/src/metadata.h	2007-03-10 21:18:21 UTC (rev 12711)
@@ -97,6 +97,12 @@
 const char *	metadata_get_filename(metadata_t *);
 
 /*
+ * Returns the length of the song, in seconds, or -1 if the information is not
+ * available.
+ */
+int		metadata_get_length(metadata_t *);
+
+/*
  * Allocates and returns a meaningful string based on a metadata handle's
  * content. The result is what metadata_get_string() defaults to if an external
  * program is not used.



More information about the commits mailing list