[xiph-commits] r8839 - icecast/branches/ices0/pipe/src

brendan at motherfish-iii.xiph.org brendan at motherfish-iii.xiph.org
Sat Feb 5 10:09:22 PST 2005


Author: brendan
Date: 2005-02-05 10:09:20 -0800 (Sat, 05 Feb 2005)
New Revision: 8839

Added:
   icecast/branches/ices0/pipe/src/decode.c
   icecast/branches/ices0/pipe/src/decode.h
   icecast/branches/ices0/pipe/src/pipe.c
   icecast/branches/ices0/pipe/src/pipe.h
Modified:
   icecast/branches/ices0/pipe/src/Makefile.am
   icecast/branches/ices0/pipe/src/encode_pipe.c
   icecast/branches/ices0/pipe/src/icestypes.h
   icecast/branches/ices0/pipe/src/signals.c
   icecast/branches/ices0/pipe/src/util.h
Log:
More noodling on the pipe branch (nothing done yet).


Modified: icecast/branches/ices0/pipe/src/Makefile.am
===================================================================
--- icecast/branches/ices0/pipe/src/Makefile.am	2005-02-05 08:16:49 UTC (rev 8838)
+++ icecast/branches/ices0/pipe/src/Makefile.am	2005-02-05 18:09:20 UTC (rev 8839)
@@ -5,11 +5,11 @@
 bin_PROGRAMS = ices
 
 noinst_HEADERS = icestypes.h definitions.h setup.h log.h stream.h util.h \
-	cue.h metadata.h in_vorbis.h mp3.h in_mp4.h in_flac.h id3.h signals.h \
-	reencode.h ices_config.h
+	cue.h metadata.h in_vorbis.h mp3.h in_mp4.h in_flac.h id3.h pipe.h \
+        signals.h reencode.h ices_config.h
 
 ices_SOURCES = crossfade.c cue.c encode_pipe.c ices.c id3.c log.c metadata.c \
-	mp3.c setup.c signals.c stream.c util.c
+	mp3.c pipe.c setup.c signals.c stream.c util.c
 
 EXTRA_ices_SOURCES = ices_config.c reencode.c in_vorbis.c in_mp4.c in_flac.c
 

Added: icecast/branches/ices0/pipe/src/decode.c
===================================================================
--- icecast/branches/ices0/pipe/src/decode.c	2005-02-05 08:16:49 UTC (rev 8838)
+++ icecast/branches/ices0/pipe/src/decode.c	2005-02-05 18:09:20 UTC (rev 8839)
@@ -0,0 +1,52 @@
+/* decode.c
+ * Decode source audio to WAV via pipe
+ * Copyright (c) 2005 Brendan Cully
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * $Id$
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+typedef struct {
+  int pid;
+  int rfd;
+  int wfd;
+} decoder_t;
+
+/* Open a pipe to cmd for decoding. Returns decoder handle, or NULL on
+ * failure. */
+void* decode_init(const char* cmd) {
+  decoder_t* decoder;
+
+  decoder = (decoder_t*)calloc(1, sizeof(decoder_t));
+  if (!decoder) {
+    ices_log_error("Could not allocate decoder");
+    return NULL;
+  }
+  
+  if ((decoder->pid = ices_pipe(cmd, &rfd, &wfd)) < 0) {
+    return NULL;
+  }
+
+  return decoder;
+}
+
+int decode_shutdown(void* decoder) {
+  free(decoder);
+}
\ No newline at end of file


Property changes on: icecast/branches/ices0/pipe/src/decode.c
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: icecast/branches/ices0/pipe/src/decode.h
===================================================================
--- icecast/branches/ices0/pipe/src/decode.h	2005-02-05 08:16:49 UTC (rev 8838)
+++ icecast/branches/ices0/pipe/src/decode.h	2005-02-05 18:09:20 UTC (rev 8839)
@@ -0,0 +1,23 @@
+/* decode.h
+ * Decode source audio to WAV via pipe
+ * Copyright (c) 2005 Brendan Cully
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * $Id$
+ */
+
+void* decode_init(const char* cmd);
+int decode_shutdown(void* decoder);


Property changes on: icecast/branches/ices0/pipe/src/decode.h
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: icecast/branches/ices0/pipe/src/encode_pipe.c
===================================================================
--- icecast/branches/ices0/pipe/src/encode_pipe.c	2005-02-05 08:16:49 UTC (rev 8838)
+++ icecast/branches/ices0/pipe/src/encode_pipe.c	2005-02-05 18:09:20 UTC (rev 8839)
@@ -23,6 +23,7 @@
 #endif
 
 #include "definitions.h"
+#include "pipe.h"
 
 #include <sys/select.h>
 #include <unistd.h>
@@ -33,9 +34,9 @@
 static int pipe_new_source(ices_stream_t* stream, input_stream_t* source);
 static int pipe_encode(ices_stream_t* stream, int samples, int16_t* left,
                        int16_t* right, unsigned char* out, size_t olen);
+static void pipe_close(ices_stream_t* stream);
+static void pipe_shutdown(ices_stream_t* stream);
 
-static void pipe_shutdown(void);
-
 static ices_encoder_t PipeEncoder = {
   pipe_init,
   pipe_new_source,
@@ -44,6 +45,7 @@
 };
 
 typedef struct {
+  int pid;
   int rfd;
   int wfd;
   int16_t* buf;
@@ -72,66 +74,20 @@
 
 static int pipe_new_source(ices_stream_t* stream, input_stream_t* source) {
   encoder_state_t* encoder;
-  int pin[2], pout[2];
-  int pid;
+  const char* cmd = "lame -b64 - -";
 
   encoder = (encoder_state_t*)stream->enc2_state;
-    
-  if (encoder->rfd) {
-    close(encoder->rfd);
-    close(encoder->wfd);
-    encoder->rfd = 0;
-    encoder->wfd = 0;
-  }
+
+  pipe_close(stream);
   if (encoder->blen) {
     free (encoder->buf);
     encoder->blen = 0;
   }
 
-  if (pipe (pin) == -1) {
-    ices_log_error("Error opening encoder read pipe");
-    return 0;
+  if ((encoder->pid = ices_pipe(cmd, &encoder->rfd, &encoder->wfd)) < 0) {
+    return -1;
   }
-  if (pipe (pout) == -1) {
-    ices_log_error("Error opening encoder write pipe");
-    return 0;
-  }
-  
-  if ((pid = fork()) == 0) {
-    if (dup2 (pout[0], STDIN_FILENO) < 0 || dup2(pin[1], STDOUT_FILENO) < 0)
-      _exit(127);
-    close(pin[0]);
-    close(pin[1]);
-    close(pout[0]);
-    close(pout[1]);
-    close(STDERR_FILENO);
-    
-    setsid();
-    
-    execl("/bin/sh", "sh", "-c", "lame -b64 - -", NULL);
-    _exit(127);
-  }
-  
-  if (pid < 0) {
-    close(pin[0]);
-    close(pin[1]);
-    close(pout[0]);
-    close(pout[1]);
-    ices_log_error("Error forking encoder");
-    return 0;
-  }
-  
-  close(pin[1]);
-  close(pout[0]);
-  
-  encoder->rfd = pin[0];
-  encoder->wfd = pout[1];
 
-  fcntl(encoder->rfd, F_SETFD, FD_CLOEXEC);
-  fcntl(encoder->wfd, F_SETFD, FD_CLOEXEC);
-
-  ices_log_debug("Encoder pipe opened");
-
   return 1;
 }
 
@@ -167,9 +123,13 @@
     select(FD_SETSIZE, &rfds, &wfds, NULL, NULL);
     if (FD_ISSET(encoder->wfd, &wfds)) {
       rc = write(encoder->wfd, ((char*)encoder->buf)+i, samples*4 - i);
-      if (rc > 0) {
-        i += rc;
+      if (rc < 0) {
+        ices_log_debug("error writing to pipe: %s", strerror(errno));
+        pipe_close(stream);
+        return -2;
       }
+
+      i += rc;
     }
 
     if (FD_ISSET(encoder->rfd, &rfds)) {
@@ -185,6 +145,31 @@
   return bytesread;
 }
 
-static void pipe_shutdown(void) {
+static void pipe_close(ices_stream_t* stream) {
+  encoder_state_t* encoder;
+  
+  encoder = (encoder_state_t*)stream->enc2_state;
+  
+  if (encoder->rfd) {
+    close(encoder->rfd);
+    close(encoder->wfd);
+    encoder->rfd = 0;
+    encoder->wfd = 0;
+  }
+}
+
+static void pipe_shutdown(ices_stream_t* stream) {
+  encoder_state_t* encoder;
+  
+  encoder = (encoder_state_t*)stream->enc2_state;
+
   ices_log_debug("Encoder pipe shutting down");
+
+  pipe_close(stream);
+  
+  if (encoder->blen)
+    free(encoder->buf);
+  
+  free(encoder);
+  stream->enc2_state = NULL;
 }
\ No newline at end of file

Modified: icecast/branches/ices0/pipe/src/icestypes.h
===================================================================
--- icecast/branches/ices0/pipe/src/icestypes.h	2005-02-05 08:16:49 UTC (rev 8838)
+++ icecast/branches/ices0/pipe/src/icestypes.h	2005-02-05 18:09:20 UTC (rev 8839)
@@ -115,7 +115,7 @@
   int (*new_source)(ices_stream_t* stream, input_stream_t* source);
   int (*encode)(ices_stream_t* stream, int samples, int16_t* left,
                 int16_t* right, unsigned char* out, size_t olen);
-  void (*shutdown)(void);
+  void (*shutdown)(ices_stream_t* stream);
 };
 
 typedef struct _ices_plugin {

Added: icecast/branches/ices0/pipe/src/pipe.c
===================================================================
--- icecast/branches/ices0/pipe/src/pipe.c	2005-02-05 08:16:49 UTC (rev 8838)
+++ icecast/branches/ices0/pipe/src/pipe.c	2005-02-05 18:09:20 UTC (rev 8839)
@@ -0,0 +1,86 @@
+/* pipe.c
+ * two-way pipe abstraction
+ * Copyright (c) 2005 Brendan Cully
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * $Id$
+ */
+
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include "log.h"
+#include "util.h"
+
+#include <errno.h>
+#include <unistd.h>
+#include <stdio.h>
+#include <string.h>
+#include <fcntl.h>
+
+/* open a pipe to cmd, and store read and write file descriptors into
+ *  rfd and wfd respectively. Returns pid of pipe process, or -1 on failure */
+int ices_pipe(const char* cmd, int* rfd, int* wfd) {
+  int pin[2], pout[2];
+  int pid;
+  
+  if (pipe (pin) == -1) {
+    ices_log_error("Error creating read pipe: %s", strerror(errno));
+    return -1;
+  }
+  if (pipe (pout) == -1) {
+    ices_log_error("Error creating write pipe: %s", strerror(errno));
+    return -1;
+  }
+  
+  if ((pid = fork()) == 0) {
+    if (dup2 (pout[0], STDIN_FILENO) < 0 || dup2(pin[1], STDOUT_FILENO) < 0)
+      _exit(127);
+    close(pin[0]);
+    close(pin[1]);
+    close(pout[0]);
+    close(pout[1]);
+    close(STDERR_FILENO);
+    
+    setsid();
+    
+    execl("/bin/sh", "sh", "-c", cmd, NULL);
+    _exit(127);
+  }
+  
+  if (pid < 0) {
+    close(pin[0]);
+    close(pin[1]);
+    close(pout[0]);
+    close(pout[1]);
+    ices_log_error("Error spawning pipe");
+    return -1;
+  }
+  
+  close(pin[1]);
+  close(pout[0]);
+  
+  *rfd = pin[0];
+  *wfd = pout[1];
+  
+  fcntl(*rfd, F_SETFD, FD_CLOEXEC);
+  fcntl(*wfd, F_SETFD, FD_CLOEXEC);
+  
+  ices_log_debug("Opened pipe (pid %d) to %s", pid, cmd);
+  
+  return pid;  
+}


Property changes on: icecast/branches/ices0/pipe/src/pipe.c
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Added: icecast/branches/ices0/pipe/src/pipe.h
===================================================================
--- icecast/branches/ices0/pipe/src/pipe.h	2005-02-05 08:16:49 UTC (rev 8838)
+++ icecast/branches/ices0/pipe/src/pipe.h	2005-02-05 18:09:20 UTC (rev 8839)
@@ -0,0 +1,22 @@
+/* pipe.h
+ * two-way pipe abstraction
+ * Copyright (c) 2005 Brendan Cully
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ * 
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
+ *
+ * $Id$
+ */
+
+int ices_pipe(const char* cmd, int* rfd, int* wfd);


Property changes on: icecast/branches/ices0/pipe/src/pipe.h
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: icecast/branches/ices0/pipe/src/signals.c
===================================================================
--- icecast/branches/ices0/pipe/src/signals.c	2005-02-05 08:16:49 UTC (rev 8838)
+++ icecast/branches/ices0/pipe/src/signals.c	2005-02-05 18:09:20 UTC (rev 8839)
@@ -28,6 +28,8 @@
 #include <sys/types.h>
 #endif
 
+#include <sys/wait.h>
+
 #ifdef HAVE_SIGNAL_H
 #include <signal.h>
 #endif
@@ -84,8 +86,17 @@
 signals_child (const int sig)
 {
   int stat;
+  int pid;
 
-  wait (&stat);
+  pid = wait (&stat);
+  
+  if (WIFEXITED(stat)) {
+    if (WEXITSTATUS(stat))
+      ices_log_debug("Child %d exited with status %d", pid, WEXITSTATUS(stat));
+  } else if (WIFSIGNALED(stat))
+    ices_log_debug("Child %d received signal %d", pid, WTERMSIG(stat));
+  else
+    ices_log_debug("Child %d died (unknown cause)");
 }
 
 /* SIGINT, ok, let's be nice and just drop dead */

Modified: icecast/branches/ices0/pipe/src/util.h
===================================================================
--- icecast/branches/ices0/pipe/src/util.h	2005-02-05 08:16:49 UTC (rev 8838)
+++ icecast/branches/ices0/pipe/src/util.h	2005-02-05 18:09:20 UTC (rev 8839)
@@ -18,6 +18,8 @@
  *
  */
 
+#include <stdio.h>
+
 /* Public function declarations */
 char **ices_util_get_argv (void);
 int ices_util_get_argc (void);



More information about the commits mailing list