gmyth/src/gmyth_transcoder.c
author renatofilho
Tue Aug 07 16:00:49 2007 +0100 (2007-08-07)
branchtrunk
changeset 797 7563dc4ed8d5
parent 750 312d6bc514f3
permissions -rw-r--r--
[svn r803] copyright fixed
     1 /**
     2  * GMyth Library
     3  * 
     4  * @file gmyth/gmyth_transcoder.c
     5  * 
     6  * @brief <p> This file contains the transcoder class.
     7  *
     8  * Copyright (C) 2007 INdT - Instituto Nokia de Tecnologia.
     9  * @author Artur Duque de Souza <artur.souza@indt.org.br>
    10  *
    11  * 
    12  * This program is free software; you can redistribute it and/or modify
    13  * it under the terms of the GNU Lesser General Public License as published by
    14  * the Free Software Foundation; either version 2 of the License, or
    15  * (at your option) any later version.
    16  *
    17  * This program is distributed in the hope that it will be useful,
    18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    20  * GNU General Public License for more details.
    21  *
    22  * You should have received a copy of the GNU Lesser General Public License
    23  * along with this program; if not, write to the Free Software
    24  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
    25  */
    26 
    27 
    28 #ifdef HAVE_CONFIG_H
    29 #include "config.h"
    30 #endif
    31 
    32 #include <stdlib.h>
    33 #include <string.h>
    34 #include <assert.h>
    35 
    36 #include "gmyth_util.h"
    37 #include "gmyth_debug.h"
    38 
    39 #include "gmyth_jobqueue.h"
    40 #include "gmyth_transcoder.h"
    41 
    42 static void     gmyth_transcoder_class_init(GMythTranscoderClass * klass);
    43 static void     gmyth_transcoder_init(GMythTranscoder * object);
    44 
    45 static void     gmyth_transcoder_dispose(GObject * object);
    46 static void     gmyth_transcoder_finalize(GObject * object);
    47 
    48 G_DEFINE_TYPE(GMythTranscoder, gmyth_transcoder, G_TYPE_OBJECT)
    49     static void     gmyth_transcoder_class_init(GMythTranscoderClass *
    50                                                 klass)
    51 {
    52     GObjectClass   *gobject_class = G_OBJECT_CLASS(klass);
    53 
    54     gobject_class->dispose = gmyth_transcoder_dispose;
    55     gobject_class->finalize = gmyth_transcoder_finalize;
    56 }
    57 
    58 static void
    59 gmyth_transcoder_init(GMythTranscoder * transcoder)
    60 {
    61     transcoder->started = FALSE;
    62 }
    63 
    64 static void
    65 gmyth_transcoder_dispose(GObject * object)
    66 {
    67     GMythTranscoder *transcoder = GMYTH_TRANSCODER(object);
    68 
    69     g_free(transcoder->output_filename);
    70     g_free(transcoder->filename);
    71     g_free(transcoder->profile);
    72     g_free(transcoder->starttime);
    73 
    74     if (transcoder->backend_info)
    75         g_object_unref(transcoder->backend_info);
    76 
    77     G_OBJECT_CLASS(gmyth_transcoder_parent_class)->dispose(object);
    78 }
    79 
    80 static void
    81 gmyth_transcoder_finalize(GObject * object)
    82 {
    83     g_signal_handlers_destroy(object);
    84     G_OBJECT_CLASS(gmyth_transcoder_parent_class)->finalize(object);
    85 }
    86 
    87 /**
    88  * Creates a new instance of GMythTranscoder.
    89  * 
    90  * @return a new instance of GMythTranscoder.
    91  **/
    92 GMythTranscoder *
    93 gmyth_transcoder_new(GMythBackendInfo * backend_info)
    94 {
    95     GMythTranscoder *transcoder = GMYTH_TRANSCODER
    96         (g_object_new(GMYTH_TRANSCODER_TYPE, NULL));
    97 
    98     if (backend_info != NULL) {
    99         g_object_ref(backend_info);
   100         transcoder->backend_info = backend_info;
   101     }
   102 
   103     return transcoder;
   104 }
   105 
   106 /**
   107  *
   108  * gmyth_transcoder_date_change_format
   109  * @brief converts a string like YYYY-MM-DDTHH:MM:SS into YYYYMMDDHHMMSS (vice versa)
   110  * @param date_s gchar*
   111  * @return gchar* with file or iso format
   112  *
   113  **/
   114 static gchar   *
   115 gmyth_transcoder_date_change_format(gchar * date_s, int format)
   116 {
   117     if (date_s != NULL) {
   118         gint            length = strlen(date_s);
   119 
   120         // create the right date format
   121         gchar          *src = (gchar *) g_malloc0(sizeof(gchar) * length);
   122 
   123         strncpy(src, date_s, length);
   124 
   125         gchar          *dst;
   126 
   127         if (format == DATE_FILE) {
   128             dst = (gchar *) g_malloc0(sizeof(gchar) * 16);
   129             snprintf(dst, 16, "%.4s%.2s%.2s%.2s%.2s%.2s", src, src + 5,
   130                      src + 7, src + 9, src + 11, src + 13);
   131             dst[15] = '\0';
   132         } else if (format == DATE_ISO) {
   133             dst = (gchar *) g_malloc0(sizeof(gchar) * 20);
   134             snprintf(dst, 20, "%.4s-%.2s-%.2sT%.2s:%.2s:%.2s", src,
   135                      src + 4, src + 6, src + 8, src + 10, src + 12);
   136             dst[19] = '\0';
   137         }
   138 
   139         gchar          *ret = g_strdup(dst);
   140 
   141         g_free(src);
   142         g_free(dst);
   143 
   144         return ret;
   145     } else
   146         return NULL;
   147 }
   148 
   149 /**
   150  * gmyth_transcoder_set_output
   151  * @brief set transcoder to use output
   152  * @param value gboolean
   153  * @param outfile filename of output
   154  * @return void set's up the var to value
   155  *
   156  **/
   157 void
   158 gmyth_transcoder_set_output(GMythTranscoder * transcoder,
   159                             gboolean value, const gchar * outputfile)
   160 {
   161     transcoder->output = value;
   162     transcoder->output_filename = g_strdup(outputfile);
   163 }
   164 
   165 /**
   166  * gmyth_transcoder_set_file
   167  * @brief set the file to transcoder
   168  * @param file filename
   169  * @return void set's up the var to value
   170  *
   171  **/
   172 void
   173 gmyth_transcoder_set_filename(GMythTranscoder * transcoder,
   174                               const gchar * file)
   175 {
   176     // fixme: if this method is called twice, memory will not be
   177     // dealocated
   178     // one transcoder can be used only for one file request?
   179     if (file != NULL) {
   180         gchar         **splited = g_strsplit(file, "_", 2);
   181 
   182         // Get chanid
   183         sscanf(splited[0], "%d", &(transcoder->chanid));
   184 
   185         // Get starttime
   186         gchar         **date = g_strsplit(splited[1], ".", 2);
   187 
   188         transcoder->starttime =
   189             gmyth_transcoder_date_change_format(date[0], DATE_ISO);
   190 
   191         transcoder->filename = g_strdup(file);
   192     }
   193 }
   194 
   195 
   196 /**
   197  *
   198  * gmyth_transcoder_set_profile
   199  * @brief set transcoder's profile
   200  * @param rec GMythTranscoder*
   201  * @param value the value
   202  * @return gint representing the result
   203  *
   204  **/
   205 gint
   206 gmyth_transcoder_set_profile(GMythTranscoder * trans, const gchar * value)
   207 {
   208     g_return_val_if_fail(value != NULL, -1);
   209 
   210     trans->profile = g_strndup(value, strlen(value));
   211 
   212     return 0;
   213 }
   214 
   215 gboolean
   216 gmyth_transcoder_start(GMythTranscoder * trans)
   217 {
   218     g_return_val_if_fail(trans != NULL, FALSE);
   219     g_return_val_if_fail(trans->backend_info != NULL, FALSE);
   220     g_return_val_if_fail(trans->filename != NULL, FALSE);
   221 
   222     if (trans->started == FALSE) {  // not started yet
   223         if (!gmyth_util_file_exists(trans->backend_info, trans->filename)) {
   224             gmyth_debug("File %s does not exist", trans->filename);
   225         }
   226         trans->started = gmyth_jobqueue_add_job(trans, "JOB_TRANSCODE");
   227         if (trans->started == FALSE)
   228             gmyth_debug("Error while starting GMythTranscoder to file: %s",
   229                         trans->output_filename);
   230     } else {
   231         gmyth_debug("GMythTransfer already started!");
   232     }
   233 
   234     return trans->started;
   235 }
   236 
   237 gboolean
   238 gmyth_transcoder_pause(GMythTranscoder * trans)
   239 {
   240     g_return_val_if_fail(trans != NULL, FALSE);
   241     g_return_val_if_fail(trans->started == TRUE, FALSE);
   242 
   243     return gmyth_jobqueue_change_cmd(trans, "PAUSE", "JOB_TRANSCODE");
   244 }
   245 
   246 gboolean
   247 gmyth_transcoder_resume(GMythTranscoder * trans)
   248 {
   249     g_return_val_if_fail(trans != NULL, FALSE);
   250 
   251     return gmyth_jobqueue_change_cmd(trans, "RESUME", "JOB_TRANSCODE");
   252 }
   253 
   254 gboolean
   255 gmyth_transcoder_cancel(GMythTranscoder * trans)
   256 {
   257     g_return_val_if_fail(trans != NULL, FALSE);
   258     g_return_val_if_fail(trans->started == TRUE, FALSE);
   259 
   260     trans->started = FALSE;
   261 
   262     return gmyth_jobqueue_change_cmd(trans, "STOP", "JOB_TRANSCODE");
   263 }
   264 
   265 // fixme: implement this method
   266 gint
   267 gmyth_transcoder_get_progress(GMythTranscoder * trans)
   268 {
   269     static int      fixme = 0;
   270 
   271     return (fixme++) % 101;
   272 }