/* vim: set sw=3 ts=3 sts=3 expandtab: */
#include "ecdb.h"

int transcode_data_cb(void *data, int type, void *event);

void
ecdb_audio_project_start(Ecdb_Audio_Project *proj)
{
   char cmd[PATH_MAX]; //<-- + 20ish?
   int i;

   /* Fork off the gstreamer program for every file to be added
    * Depending on the number of files, this can be pretty system intensive, so
    * is there a way to reduce/control this ?
    */
   for (i = 0; ECDB_BURN(proj)->files->children[i]; i++)
   {
      snprintf(cmd, PATH_MAX, "ecdb_transcode_helper %s",
                                    ECDB_BURN(proj)->files->children[i]->dst);
      ecore_exe_pipe_run(cmd, ECORE_EXE_PIPE_READ |
                                       ECORE_EXE_PIPE_READ_LINE_BUFFERED, NULL);
   }
   
   proj->num_tracks = i + 1;
   ecore_event_handler_add(ECORE_EXE_EVENT_DATA, transcode_data_cb, proj);
}

int
transcode_data_cb(void *data, int type, void *event)
{
   const char *rec;
   Ecore_Exe_Event_Data *ev = event;
   Ecdb_Audio_Project *proj = data;

   rec = ev->data;
   proj->num_transcode_complete++;

   EINA_ERROR_PDBG("Message: %s\n", rec);

   if (!strcmp(rec, "EOS"))
   {
      EINA_ERROR_PDBG("Transcode complete\n");
      proj->num_transcode_complete++;
   }
   else
   {
      EINA_ERROR_PWARN("Error!\n"); // How to handle these?
   }

   if (proj->num_tracks == proj->num_transcode_complete)
   {
      EINA_ERROR_PINFO("Hurrah, transcoding is done!\n");
      
      /* Change to another event later */
      ecore_event_add(ECORE_EVENT_SIGNAL_EXIT, NULL, NULL, NULL);
   }

   return 1;
}

