4 * @file gmyth/gmyth_transcoder.c
6 * @brief <p> This file contains the transcoder class.
8 * Copyright (C) 2007 INdT - Instituto Nokia de Tecnologia.
9 * @author Artur Duque de Souza <artur.souza@indt.org.br>
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.
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.
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
36 #include "gmyth_util.h"
37 #include "gmyth_debug.h"
39 #include "gmyth_jobqueue.h"
40 #include "gmyth_transcoder.h"
42 static void gmyth_transcoder_class_init(GMythTranscoderClass * klass);
43 static void gmyth_transcoder_init(GMythTranscoder * object);
45 static void gmyth_transcoder_dispose(GObject * object);
46 static void gmyth_transcoder_finalize(GObject * object);
48 G_DEFINE_TYPE(GMythTranscoder, gmyth_transcoder, G_TYPE_OBJECT)
49 static void gmyth_transcoder_class_init(GMythTranscoderClass *
52 GObjectClass *gobject_class = G_OBJECT_CLASS(klass);
54 gobject_class->dispose = gmyth_transcoder_dispose;
55 gobject_class->finalize = gmyth_transcoder_finalize;
59 gmyth_transcoder_init(GMythTranscoder * transcoder)
61 transcoder->started = FALSE;
65 gmyth_transcoder_dispose(GObject * object)
67 GMythTranscoder *transcoder = GMYTH_TRANSCODER(object);
69 g_free(transcoder->output_filename);
70 g_free(transcoder->filename);
71 g_free(transcoder->profile);
72 g_free(transcoder->starttime);
74 if (transcoder->backend_info)
75 g_object_unref(transcoder->backend_info);
77 G_OBJECT_CLASS(gmyth_transcoder_parent_class)->dispose(object);
81 gmyth_transcoder_finalize(GObject * object)
83 g_signal_handlers_destroy(object);
84 G_OBJECT_CLASS(gmyth_transcoder_parent_class)->finalize(object);
88 * Creates a new instance of GMythTranscoder.
90 * @return a new instance of GMythTranscoder.
93 gmyth_transcoder_new(GMythBackendInfo * backend_info)
95 GMythTranscoder *transcoder = GMYTH_TRANSCODER
96 (g_object_new(GMYTH_TRANSCODER_TYPE, NULL));
98 if (backend_info != NULL) {
99 g_object_ref(backend_info);
100 transcoder->backend_info = backend_info;
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
115 gmyth_transcoder_date_change_format(gchar * date_s, int format)
117 if (date_s != NULL) {
118 gint length = strlen(date_s);
120 // create the right date format
121 gchar *src = (gchar *) g_malloc0(sizeof(gchar) * length);
123 strncpy(src, date_s, length);
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);
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);
139 gchar *ret = g_strdup(dst);
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
158 gmyth_transcoder_set_output(GMythTranscoder * transcoder,
159 gboolean value, const gchar * outputfile)
161 transcoder->output = value;
162 transcoder->output_filename = g_strdup(outputfile);
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
173 gmyth_transcoder_set_filename(GMythTranscoder * transcoder,
176 // fixme: if this method is called twice, memory will not be
178 // one transcoder can be used only for one file request?
180 gchar **splited = g_strsplit(file, "_", 2);
183 sscanf(splited[0], "%d", &(transcoder->chanid));
186 gchar **date = g_strsplit(splited[1], ".", 2);
188 transcoder->starttime =
189 gmyth_transcoder_date_change_format(date[0], DATE_ISO);
191 transcoder->filename = g_strdup(file);
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
206 gmyth_transcoder_set_profile(GMythTranscoder * trans, const gchar * value)
208 g_return_val_if_fail(value != NULL, -1);
210 trans->profile = g_strndup(value, strlen(value));
216 gmyth_transcoder_start(GMythTranscoder * trans)
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);
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);
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);
231 gmyth_debug("GMythTransfer already started!");
234 return trans->started;
238 gmyth_transcoder_pause(GMythTranscoder * trans)
240 g_return_val_if_fail(trans != NULL, FALSE);
241 g_return_val_if_fail(trans->started == TRUE, FALSE);
243 return gmyth_jobqueue_change_cmd(trans, "PAUSE", "JOB_TRANSCODE");
247 gmyth_transcoder_resume(GMythTranscoder * trans)
249 g_return_val_if_fail(trans != NULL, FALSE);
251 return gmyth_jobqueue_change_cmd(trans, "RESUME", "JOB_TRANSCODE");
255 gmyth_transcoder_cancel(GMythTranscoder * trans)
257 g_return_val_if_fail(trans != NULL, FALSE);
258 g_return_val_if_fail(trans->started == TRUE, FALSE);
260 trans->started = FALSE;
262 return gmyth_jobqueue_change_cmd(trans, "STOP", "JOB_TRANSCODE");
265 // fixme: implement this method
267 gmyth_transcoder_get_progress(GMythTranscoder * trans)
269 static int fixme = 0;
271 return (fixme++) % 101;