gmyth/src/gmyth_recorder.c
author renatofilho
Thu Jun 14 18:19:52 2007 +0100 (2007-06-14)
branchtrunk
changeset 750 312d6bc514f3
parent 701 2f28edb4d804
child 754 cb885ee44618
permissions -rw-r--r--
[svn r756] fixed indent using GNU Style
rosfran@65
     1
/**
rosfran@65
     2
 * GMyth Library
rosfran@65
     3
 *
rosfran@420
     4
 * @file gmyth/gmyth_recorder.c
rosfran@65
     5
 * 
rosfran@420
     6
 * @brief <p> GMythRecorder defines functions for playing live tv.
rosfran@65
     7
 *
rosfran@65
     8
 * The remote encoder is used by gmyth_tvplayer to setup livetv. 
rosfran@65
     9
 *
rosfran@65
    10
 * Copyright (C) 2006 INdT - Instituto Nokia de Tecnologia.
rosfran@65
    11
 * @author Hallyson Luiz de Morais Melo <hallyson.melo@indt.org.br>
rosfran@420
    12
 * @author Rosfran Borges <rosfran.borges@indt.org.br>
rosfran@65
    13
 *
rosfran@701
    14
 * 
rosfran@701
    15
 * This program is free software; you can redistribute it and/or modify
rosfran@701
    16
 * it under the terms of the GNU Lesser General Public License as published by
rosfran@701
    17
 * the Free Software Foundation; either version 2 of the License, or
rosfran@701
    18
 * (at your option) any later version.
rosfran@701
    19
 *
rosfran@701
    20
 * This program is distributed in the hope that it will be useful,
rosfran@701
    21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rosfran@701
    22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
rosfran@701
    23
 * GNU General Public License for more details.
rosfran@701
    24
 *
rosfran@701
    25
 * You should have received a copy of the GNU Lesser General Public License
rosfran@701
    26
 * along with this program; if not, write to the Free Software
rosfran@701
    27
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
rosfran@701
    28
 */
rosfran@443
    29
leo_sobral@213
    30
#ifdef HAVE_CONFIG_H
leo_sobral@213
    31
#include "config.h"
leo_sobral@213
    32
#endif
rosfran@65
    33
rosfran@65
    34
#include "gmyth_recorder.h"
rosfran@65
    35
rosfran@65
    36
#include <assert.h>
rosfran@65
    37
rosfran@65
    38
#include "gmyth_stringlist.h"
rosfran@115
    39
#include "gmyth_util.h"
leo_sobral@446
    40
#include "gmyth_common.h"
renatofilho@131
    41
#include "gmyth_debug.h"
rosfran@115
    42
rosfran@115
    43
#define	 GMYTHTV_RECORDER_HEADER			"QUERY_RECORDER"
rosfran@65
    44
renatofilho@750
    45
static void gmyth_recorder_class_init(GMythRecorderClass * klass);
renatofilho@750
    46
static void gmyth_recorder_init(GMythRecorder * object);
rosfran@65
    47
renatofilho@750
    48
static void gmyth_recorder_dispose(GObject * object);
renatofilho@750
    49
static void gmyth_recorder_finalize(GObject * object);
rosfran@65
    50
renatofilho@750
    51
G_DEFINE_TYPE(GMythRecorder, gmyth_recorder, G_TYPE_OBJECT)
renatofilho@750
    52
	 static void gmyth_recorder_class_init(GMythRecorderClass * klass)
rosfran@65
    53
{
renatofilho@750
    54
  GObjectClass *gobject_class;
rosfran@65
    55
renatofilho@750
    56
  gobject_class = (GObjectClass *) klass;
rosfran@698
    57
renatofilho@750
    58
  gobject_class->dispose = gmyth_recorder_dispose;
renatofilho@750
    59
  gobject_class->finalize = gmyth_recorder_finalize;
rosfran@65
    60
}
rosfran@65
    61
rosfran@65
    62
static void
renatofilho@750
    63
gmyth_recorder_init(GMythRecorder * gmyth_remote_encoder)
leo_sobral@446
    64
{
rosfran@65
    65
}
rosfran@65
    66
rosfran@65
    67
static void
renatofilho@750
    68
gmyth_recorder_dispose(GObject * object)
leo_sobral@446
    69
{
renatofilho@750
    70
  GMythRecorder *recorder = GMYTH_RECORDER(object);
rosfran@443
    71
renatofilho@750
    72
  gmyth_recorder_close(recorder);
rosfran@443
    73
renatofilho@750
    74
  if (recorder->mutex != NULL)
renatofilho@750
    75
	{
renatofilho@750
    76
	  g_mutex_free(recorder->mutex);
renatofilho@750
    77
	  recorder->mutex = NULL;
renatofilho@750
    78
	}
rosfran@698
    79
renatofilho@750
    80
  if (recorder->myth_socket != NULL)
renatofilho@750
    81
	{
renatofilho@750
    82
	  g_object_unref(recorder->myth_socket);
renatofilho@750
    83
	  recorder->myth_socket = NULL;
renatofilho@750
    84
	}
rosfran@698
    85
renatofilho@750
    86
  if (recorder->progs_info_list != NULL)
renatofilho@750
    87
	gmyth_free_program_list(recorder->progs_info_list);
rosfran@698
    88
renatofilho@750
    89
  if (recorder->hostname != NULL)
renatofilho@750
    90
	g_string_free(recorder->hostname, TRUE);
leo_sobral@446
    91
renatofilho@750
    92
  G_OBJECT_CLASS(gmyth_recorder_parent_class)->dispose(object);
rosfran@65
    93
}
rosfran@65
    94
rosfran@443
    95
static void
renatofilho@750
    96
gmyth_recorder_finalize(GObject * object)
leo_sobral@446
    97
{
renatofilho@750
    98
  g_signal_handlers_destroy(object);
rosfran@65
    99
renatofilho@750
   100
  G_OBJECT_CLASS(gmyth_recorder_parent_class)->finalize(object);
rosfran@336
   101
}
rosfran@65
   102
rosfran@336
   103
void
renatofilho@750
   104
gmyth_recorder_close(GMythRecorder * recorder)
leo_sobral@446
   105
{
renatofilho@750
   106
  if (recorder != NULL && recorder->recorder_num != -1)
renatofilho@750
   107
	{
renatofilho@750
   108
	  g_mutex_lock(recorder->mutex);
rosfran@545
   109
renatofilho@750
   110
	  gmyth_recorder_stop_playing(recorder);
renatofilho@750
   111
	  gmyth_recorder_stop_livetv(recorder);
renatofilho@750
   112
	  gmyth_recorder_finish_recording(recorder);
renatofilho@750
   113
	  gmyth_recorder_free_tuner(recorder);
rosfran@545
   114
renatofilho@750
   115
	  g_mutex_unlock(recorder->mutex);
renatofilho@750
   116
	}
rosfran@65
   117
}
rosfran@65
   118
rosfran@65
   119
/** Creates a new instance of GMythRecorder.
rosfran@65
   120
 * 
rosfran@65
   121
 * @return a new instance of GMythRecorder.
rosfran@65
   122
 */
rosfran@698
   123
GMythRecorder *
renatofilho@750
   124
gmyth_recorder_new(int num, GString * hostname, gshort port)
leo_sobral@446
   125
{
renatofilho@750
   126
  GMythRecorder *encoder =
renatofilho@750
   127
	GMYTH_RECORDER(g_object_new(GMYTH_RECORDER_TYPE, FALSE));
rosfran@443
   128
renatofilho@750
   129
  encoder->recorder_num = num;
renatofilho@750
   130
  encoder->hostname = g_string_new(hostname->str);
renatofilho@750
   131
  encoder->port = port;
rosfran@698
   132
renatofilho@750
   133
  encoder->mutex = g_mutex_new();
rosfran@698
   134
renatofilho@750
   135
  encoder->progs_info_list = NULL;
rosfran@443
   136
renatofilho@750
   137
  return encoder;
rosfran@65
   138
}
rosfran@65
   139
rosfran@65
   140
/** Configures the remote encoder instance connecting it to Mythtv backend.
rosfran@65
   141
 * 
rosfran@65
   142
 * @param recorder the GMythRecorder instance.
rosfran@420
   143
 * 
rosfran@65
   144
 * @return TRUE if successfull, FALSE if any error happens.
rosfran@65
   145
 */
rosfran@65
   146
gboolean
renatofilho@750
   147
gmyth_recorder_setup(GMythRecorder * recorder)
leo_sobral@446
   148
{
renatofilho@750
   149
  assert(recorder);
renatofilho@750
   150
  gmyth_debug("[%s] Creating socket and connecting to backend", __FUNCTION__);
rosfran@65
   151
renatofilho@750
   152
  if (recorder->myth_socket == NULL)
renatofilho@750
   153
	{
renatofilho@750
   154
	  recorder->myth_socket = gmyth_socket_new();
rosfran@443
   155
renatofilho@750
   156
	  if (!gmyth_socket_connect_to_backend(recorder->myth_socket,
renatofilho@750
   157
										   recorder->hostname->str,
renatofilho@750
   158
										   recorder->port, TRUE))
renatofilho@750
   159
		{
renatofilho@750
   160
		  gmyth_debug("GMythRemoteEncoder: Connection to backend failed");
renatofilho@750
   161
		  return FALSE;
renatofilho@750
   162
		}
renatofilho@750
   163
	}
renatofilho@750
   164
  else
renatofilho@750
   165
	{
renatofilho@750
   166
	  gmyth_debug("Remote encoder socket already created\n");
renatofilho@750
   167
	}
rosfran@443
   168
renatofilho@750
   169
  return TRUE;
rosfran@65
   170
}
rosfran@65
   171
rosfran@65
   172
/** Sends the SPAWN_LIVETV command through Mythtv protocol. This command
rosfran@65
   173
 * requests the backend to start capturing TV content.
rosfran@65
   174
 * 
rosfran@65
   175
 * @param recorder The GMythRecorder instance.
rosfran@65
   176
 * @param tvchain_id The tvchain unique id.
rosfran@65
   177
 * @return true if success, false if any error happens.
rosfran@65
   178
 */
rosfran@65
   179
gboolean
renatofilho@750
   180
gmyth_recorder_spawntv(GMythRecorder * recorder, GString * tvchain_id)
leo_sobral@446
   181
{
renatofilho@750
   182
  GMythStringList *str_list;
renatofilho@750
   183
  GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
renatofilho@750
   184
  gboolean ret = TRUE;
rosfran@65
   185
renatofilho@750
   186
  gmyth_debug("[%s] Spawntv with tvchain_id = %s", __FUNCTION__,
renatofilho@750
   187
			  tvchain_id->str);
rosfran@443
   188
renatofilho@750
   189
  str_list = gmyth_string_list_new();
rosfran@443
   190
renatofilho@750
   191
  g_mutex_lock(recorder->mutex);
rosfran@698
   192
renatofilho@750
   193
  g_string_append_printf(tmp_str, " %d", recorder->recorder_num);
rosfran@443
   194
renatofilho@750
   195
  gmyth_string_list_append_string(str_list, tmp_str);
renatofilho@750
   196
  g_string_free(tmp_str, TRUE);
leo_sobral@446
   197
renatofilho@750
   198
  gmyth_string_list_append_char_array(str_list, "SPAWN_LIVETV");
leo_sobral@446
   199
renatofilho@750
   200
  gmyth_string_list_append_string(str_list, tvchain_id);
renatofilho@750
   201
  gmyth_string_list_append_int(str_list, 0);	// PIP = FALSE (0)
rosfran@443
   202
renatofilho@750
   203
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@443
   204
renatofilho@750
   205
  tmp_str = gmyth_string_list_get_string(str_list, 0);
rosfran@443
   206
renatofilho@750
   207
  if (tmp_str == NULL)
renatofilho@750
   208
	{
renatofilho@750
   209
	  gmyth_debug("[%s] Spawntv request returned %s", __FUNCTION__,
renatofilho@750
   210
				  tmp_str->str);
renatofilho@750
   211
	  ret = FALSE;
renatofilho@750
   212
	  goto cleanup;
renatofilho@750
   213
	}
rosfran@443
   214
renatofilho@750
   215
  if (g_ascii_strncasecmp(tmp_str->str, "ok", 2))
renatofilho@750
   216
	{
renatofilho@750
   217
	  gmyth_debug("[%s] Spawntv request returned %s", __FUNCTION__,
renatofilho@750
   218
				  tmp_str->str);
renatofilho@750
   219
	  ret = FALSE;
renatofilho@750
   220
	  goto cleanup;
renatofilho@750
   221
	}
rosfran@65
   222
renatofilho@750
   223
cleanup:
renatofilho@750
   224
  g_mutex_unlock(recorder->mutex);
rosfran@698
   225
renatofilho@750
   226
  g_string_free(tmp_str, TRUE);
renatofilho@750
   227
  g_object_unref(str_list);
rosfran@65
   228
renatofilho@750
   229
  return ret;
rosfran@65
   230
}
rosfran@65
   231
rosfran@310
   232
/** 
rosfran@310
   233
 * Sends the SPAWN_LIVETV command through Mythtv protocol. This command
rosfran@310
   234
 * requests the backend to start capturing TV content, but it doesn't need
rosfran@310
   235
 * the TV chain ID.
rosfran@310
   236
 * 
rosfran@310
   237
 * @param recorder The GMythRecorder instance.
rosfran@310
   238
 * @return true if success, false if any error happens.
rosfran@310
   239
 */
rosfran@310
   240
gboolean
renatofilho@750
   241
gmyth_recorder_spawntv_no_tvchain(GMythRecorder * recorder)
rosfran@698
   242
{
renatofilho@750
   243
  GMythStringList *str_list;
renatofilho@750
   244
  GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
renatofilho@750
   245
  gboolean ret = TRUE;
rosfran@310
   246
renatofilho@750
   247
  gmyth_debug("[%s] Spawntv, no TV chain!", __FUNCTION__);
rosfran@443
   248
renatofilho@750
   249
  str_list = gmyth_string_list_new();
rosfran@443
   250
renatofilho@750
   251
  g_mutex_lock(recorder->mutex);
rosfran@698
   252
renatofilho@750
   253
  g_string_append_printf(tmp_str, " %d", recorder->recorder_num);
rosfran@443
   254
renatofilho@750
   255
  gmyth_string_list_append_string(str_list, tmp_str);
renatofilho@750
   256
  g_string_free(tmp_str, TRUE);
leo_sobral@446
   257
renatofilho@750
   258
  gmyth_string_list_append_char_array(str_list, "SPAWN_LIVETV");
rosfran@443
   259
renatofilho@750
   260
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@443
   261
renatofilho@750
   262
  tmp_str = gmyth_string_list_get_string(str_list, 0);
rosfran@443
   263
renatofilho@750
   264
  if (tmp_str == NULL)
renatofilho@750
   265
	{
renatofilho@750
   266
	  gmyth_debug("[%s] Spawntv request returned %s", __FUNCTION__,
renatofilho@750
   267
				  tmp_str->str);
renatofilho@750
   268
	  ret = FALSE;
renatofilho@750
   269
	  goto cleanup;
renatofilho@750
   270
	}
rosfran@443
   271
renatofilho@750
   272
  if (g_ascii_strncasecmp(tmp_str->str, "ok", 2))
renatofilho@750
   273
	{
renatofilho@750
   274
	  gmyth_debug("[%s] Spawntv request returned %s", __FUNCTION__,
renatofilho@750
   275
				  tmp_str->str);
renatofilho@750
   276
	  ret = FALSE;
renatofilho@750
   277
	  goto cleanup;
renatofilho@750
   278
	}
rosfran@310
   279
renatofilho@750
   280
cleanup:
renatofilho@750
   281
  g_mutex_unlock(recorder->mutex);
rosfran@698
   282
renatofilho@750
   283
  g_string_free(tmp_str, TRUE);
renatofilho@750
   284
  g_object_unref(str_list);
rosfran@310
   285
renatofilho@750
   286
  return ret;
rosfran@310
   287
}
rosfran@310
   288
rosfran@65
   289
/** Sends the command STOP_LIVETV to Mythtv backend.
rosfran@65
   290
 * 
rosfran@65
   291
 * @param recorder the GMythRecorder instance.
rosfran@65
   292
 * @return true if success, false if any error happens.
rosfran@65
   293
 */
rosfran@65
   294
gboolean
renatofilho@750
   295
gmyth_recorder_stop_livetv(GMythRecorder * recorder)
rosfran@698
   296
{
renatofilho@750
   297
  GMythStringList *str_list;
renatofilho@750
   298
  GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
renatofilho@750
   299
  gboolean ret = TRUE;
rosfran@65
   300
renatofilho@750
   301
  gmyth_debug("[%s]", __FUNCTION__);
rosfran@65
   302
renatofilho@750
   303
  str_list = gmyth_string_list_new();
rosfran@698
   304
renatofilho@750
   305
  g_string_append_printf(tmp_str, " %d", recorder->recorder_num);
leo_sobral@446
   306
renatofilho@750
   307
  gmyth_string_list_append_string(str_list, tmp_str);
renatofilho@750
   308
  g_string_free(tmp_str, TRUE);
leo_sobral@446
   309
renatofilho@750
   310
  gmyth_string_list_append_char_array(str_list, "STOP_LIVETV");
rosfran@65
   311
renatofilho@750
   312
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@65
   313
renatofilho@750
   314
  tmp_str = gmyth_string_list_get_string(str_list, 0);
rosfran@65
   315
renatofilho@750
   316
  if (g_ascii_strncasecmp(tmp_str->str, "ok", 2))
renatofilho@750
   317
	{
renatofilho@750
   318
	  gmyth_debug("[%s] Stop livetv request returned %s", __FUNCTION__,
renatofilho@750
   319
				  tmp_str->str);
renatofilho@750
   320
	  ret = FALSE;
renatofilho@750
   321
	  goto cleanup;
renatofilho@750
   322
	}
rosfran@65
   323
renatofilho@750
   324
cleanup:
renatofilho@750
   325
  g_string_free(tmp_str, TRUE);
renatofilho@750
   326
  g_object_unref(str_list);
rosfran@65
   327
renatofilho@750
   328
  return ret;
rosfran@65
   329
}
rosfran@65
   330
rosfran@104
   331
/** Sends the FRONTEND_READY command through Mythtv protocol. This command
rosfran@104
   332
 * advertises the backend to start capturing TV content.
rosfran@104
   333
 * 
rosfran@104
   334
 * @param recorder The GMythRecorder instance.
rosfran@104
   335
 * @return TRUE if success, FALSE if any error happens.
rosfran@104
   336
 */
rosfran@104
   337
gboolean
renatofilho@750
   338
gmyth_recorder_send_frontend_ready_command(GMythRecorder * recorder)
rosfran@698
   339
{
renatofilho@750
   340
  GMythStringList *str_list;
renatofilho@750
   341
  GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
renatofilho@750
   342
  gboolean ret = TRUE;
rosfran@104
   343
renatofilho@750
   344
  gmyth_debug("[%s] FRONTEND_READY with recorder id = %d", __FUNCTION__,
renatofilho@750
   345
			  recorder->recorder_num);
rosfran@443
   346
renatofilho@750
   347
  str_list = gmyth_string_list_new();
rosfran@443
   348
renatofilho@750
   349
  g_mutex_lock(recorder->mutex);
rosfran@698
   350
renatofilho@750
   351
  g_string_append_printf(tmp_str, " %d", recorder->recorder_num);
rosfran@443
   352
renatofilho@750
   353
  gmyth_string_list_append_string(str_list, tmp_str);
renatofilho@750
   354
  g_string_free(tmp_str, TRUE);
leo_sobral@446
   355
renatofilho@750
   356
  gmyth_string_list_append_char_array(str_list, "FRONTEND_READY");
rosfran@443
   357
renatofilho@750
   358
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@443
   359
renatofilho@750
   360
  tmp_str = gmyth_string_list_get_string(str_list, 0);
rosfran@443
   361
renatofilho@750
   362
  if (tmp_str == NULL)
renatofilho@750
   363
	{
renatofilho@750
   364
	  gmyth_debug
renatofilho@750
   365
		("[%s] FRONTEND_READY command request couldn't returns, reason: %s",
renatofilho@750
   366
		 __FUNCTION__, tmp_str->str);
renatofilho@750
   367
	  ret = FALSE;
renatofilho@750
   368
	  goto cleanup;
renatofilho@750
   369
	}
rosfran@443
   370
renatofilho@750
   371
  if (g_ascii_strncasecmp(tmp_str->str, "ok", 2))
renatofilho@750
   372
	{
renatofilho@750
   373
	  gmyth_debug("[%s] FRONTEND_READY request returned %s", __FUNCTION__,
renatofilho@750
   374
				  tmp_str->str);
renatofilho@750
   375
	  ret = FALSE;
renatofilho@750
   376
	  goto cleanup;
renatofilho@750
   377
	}
rosfran@443
   378
renatofilho@750
   379
cleanup:
renatofilho@750
   380
  g_mutex_unlock(recorder->mutex);
renatofilho@750
   381
  g_string_free(tmp_str, TRUE);
renatofilho@750
   382
  g_object_unref(str_list);
rosfran@104
   383
renatofilho@750
   384
  return ret;
rosfran@104
   385
}
rosfran@104
   386
rosfran@65
   387
/** Send a CHECK_CHANNEL command request to the backend, in order to find if a 
rosfran@65
   388
 * certain channel actually exists.
rosfran@65
   389
 * 
rosfran@65
   390
 * @param recorder The GMythRecorder instance.
rosfran@307
   391
 * @param channel	 The new channel to be checked (string format).
rosfran@65
   392
 * @return true if success, false if any error happens.
rosfran@65
   393
 */
rosfran@65
   394
gboolean
renatofilho@750
   395
gmyth_recorder_check_channel_name(GMythRecorder * recorder, gchar * channel)
rosfran@698
   396
{
renatofilho@750
   397
  GMythStringList *str_list;
renatofilho@750
   398
  GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
renatofilho@750
   399
  gboolean ret = TRUE;
rosfran@65
   400
renatofilho@750
   401
  gmyth_debug("[%s] CHECK_CHANNEL with channel = %s", __FUNCTION__, channel);
rosfran@443
   402
renatofilho@750
   403
  str_list = gmyth_string_list_new();
rosfran@443
   404
renatofilho@750
   405
  g_mutex_lock(recorder->mutex);
rosfran@698
   406
renatofilho@750
   407
  g_string_append_printf(tmp_str, " %d", recorder->recorder_num);
rosfran@443
   408
renatofilho@750
   409
  gmyth_string_list_append_string(str_list, tmp_str);
renatofilho@750
   410
  g_string_free(tmp_str, TRUE);
leo_sobral@446
   411
renatofilho@750
   412
  gmyth_string_list_append_char_array(str_list, "CHECK_CHANNEL");
leo_sobral@446
   413
renatofilho@750
   414
  gmyth_string_list_append_char_array(str_list, channel);
rosfran@443
   415
renatofilho@750
   416
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@443
   417
renatofilho@750
   418
  tmp_str = gmyth_string_list_get_string(str_list, 0);
rosfran@443
   419
renatofilho@750
   420
  if (tmp_str == NULL)
renatofilho@750
   421
	{
renatofilho@750
   422
	  gmyth_debug("[%s] CHECK_CHANNEL request returned %s", __FUNCTION__,
renatofilho@750
   423
				  tmp_str->str);
renatofilho@750
   424
	  ret = FALSE;
renatofilho@750
   425
	  goto cleanup;
renatofilho@750
   426
	}
rosfran@443
   427
renatofilho@750
   428
  if (g_ascii_strncasecmp(tmp_str->str, "ok", 2) == 0
renatofilho@750
   429
	  || g_ascii_strncasecmp(tmp_str->str, "0", 1) == 0)
renatofilho@750
   430
	{
renatofilho@750
   431
	  gmyth_debug("[%s] CHECK_CHANNEL request returned %s", __FUNCTION__,
renatofilho@750
   432
				  tmp_str->str);
renatofilho@750
   433
	  ret = FALSE;
renatofilho@750
   434
	  goto cleanup;
renatofilho@750
   435
	}
rosfran@65
   436
renatofilho@750
   437
cleanup:
renatofilho@750
   438
  g_mutex_unlock(recorder->mutex);
renatofilho@750
   439
  g_string_free(tmp_str, TRUE);
renatofilho@750
   440
  g_object_unref(str_list);
rosfran@65
   441
renatofilho@750
   442
  return ret;
rosfran@65
   443
}
rosfran@65
   444
rosfran@307
   445
/** Send a CHECK_CHANNEL command request to the backend, in order to find if a 
rosfran@307
   446
 * certain channel actually exists.
rosfran@307
   447
 * 
rosfran@307
   448
 * @param recorder The GMythRecorder instance.
rosfran@307
   449
 * @param channel	 The new channel to be checked (decimal integer value).
rosfran@307
   450
 * @return true if success, false if any error happens.
rosfran@307
   451
 */
rosfran@307
   452
gboolean
renatofilho@750
   453
gmyth_recorder_check_channel(GMythRecorder * recorder, gint channel)
rosfran@698
   454
{
renatofilho@750
   455
  return gmyth_recorder_check_channel_name(recorder, g_strdup_printf("%d",
renatofilho@750
   456
																	 channel));
rosfran@307
   457
}
rosfran@307
   458
rosfran@65
   459
/** Send a SET_CHANNEL command request to the backend, to start streaming on another 
rosfran@65
   460
 * TV content channel.
rosfran@65
   461
 * 
rosfran@65
   462
 * @param recorder The GMythRecorder instance.
rosfran@65
   463
 * @param channel	 The new channel to be loaded.
rosfran@65
   464
 * @return true if success, false if any error happens.
rosfran@65
   465
 */
rosfran@65
   466
gboolean
renatofilho@750
   467
gmyth_recorder_set_channel(GMythRecorder * recorder, gint channel)
rosfran@698
   468
{
renatofilho@750
   469
  GMythStringList *str_list;
renatofilho@750
   470
  GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
renatofilho@750
   471
  gboolean ret = TRUE;
rosfran@65
   472
renatofilho@750
   473
  gmyth_debug("[%s] SET_CHANNEL with channel = %d", __FUNCTION__, channel);
rosfran@443
   474
renatofilho@750
   475
  str_list = gmyth_string_list_new();
rosfran@443
   476
renatofilho@750
   477
  g_mutex_lock(recorder->mutex);
rosfran@698
   478
renatofilho@750
   479
  g_string_append_printf(tmp_str, " %d", recorder->recorder_num);
rosfran@443
   480
renatofilho@750
   481
  gmyth_string_list_append_string(str_list, tmp_str);
renatofilho@750
   482
  g_string_free(tmp_str, TRUE);
leo_sobral@446
   483
renatofilho@750
   484
  gmyth_string_list_append_char_array(str_list, "SET_CHANNEL");
rosfran@698
   485
renatofilho@750
   486
  gmyth_string_list_append_int(str_list, channel);
rosfran@443
   487
renatofilho@750
   488
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@443
   489
renatofilho@750
   490
  tmp_str = gmyth_string_list_get_string(str_list, 0);
rosfran@443
   491
renatofilho@750
   492
  if (tmp_str == NULL)
renatofilho@750
   493
	{
renatofilho@750
   494
	  gmyth_debug("[%s] SET_CHANNEL request returned %s", __FUNCTION__,
renatofilho@750
   495
				  tmp_str->str);
renatofilho@750
   496
	  ret = FALSE;
renatofilho@750
   497
	  goto cleanup;
renatofilho@750
   498
	}
rosfran@443
   499
renatofilho@750
   500
  if (g_ascii_strncasecmp(tmp_str->str, "ok", 2))
renatofilho@750
   501
	{
renatofilho@750
   502
	  gmyth_debug("[%s] SET_CHANNEL request returned %s", __FUNCTION__,
renatofilho@750
   503
				  tmp_str->str);
renatofilho@750
   504
	  ret = FALSE;
renatofilho@750
   505
	  goto cleanup;
renatofilho@750
   506
	}
rosfran@65
   507
renatofilho@750
   508
cleanup:
renatofilho@750
   509
  g_mutex_unlock(recorder->mutex);
renatofilho@750
   510
  g_string_free(tmp_str, TRUE);
renatofilho@750
   511
  g_object_unref(str_list);
rosfran@65
   512
renatofilho@750
   513
  return ret;
rosfran@65
   514
}
rosfran@115
   515
rosfran@287
   516
/** Send a SET_CHANNEL command request to the backend, to start streaming on another 
rosfran@287
   517
 * TV content channel.
rosfran@287
   518
 * 
rosfran@287
   519
 * @param recorder The GMythRecorder instance.
rosfran@287
   520
 * @param channel	 The new channel to be loaded.
rosfran@287
   521
 * @return true if success, false if any error happens.
rosfran@287
   522
 */
rosfran@287
   523
gboolean
renatofilho@750
   524
gmyth_recorder_set_channel_name(GMythRecorder * recorder,
renatofilho@750
   525
								const gchar * channel)
rosfran@698
   526
{
renatofilho@750
   527
  GMythStringList *str_list;
renatofilho@750
   528
  GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
renatofilho@750
   529
  gboolean ret = TRUE;
rosfran@287
   530
renatofilho@750
   531
  gmyth_debug("[%s] SET_CHANNEL with channel name = %s", __FUNCTION__,
renatofilho@750
   532
			  channel);
rosfran@443
   533
renatofilho@750
   534
  str_list = gmyth_string_list_new();
rosfran@443
   535
renatofilho@750
   536
  g_mutex_lock(recorder->mutex);
rosfran@698
   537
renatofilho@750
   538
  g_string_append_printf(tmp_str, " %d", recorder->recorder_num);
rosfran@443
   539
renatofilho@750
   540
  gmyth_string_list_append_string(str_list, tmp_str);
renatofilho@750
   541
  g_string_free(tmp_str, TRUE);
leo_sobral@446
   542
renatofilho@750
   543
  gmyth_string_list_append_char_array(str_list, "SET_CHANNEL");
renatofilho@750
   544
  gmyth_string_list_append_char_array(str_list, channel);
rosfran@443
   545
renatofilho@750
   546
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@443
   547
renatofilho@750
   548
  tmp_str = gmyth_string_list_get_string(str_list, 0);
rosfran@443
   549
renatofilho@750
   550
  if (tmp_str == NULL)
renatofilho@750
   551
	{
renatofilho@750
   552
	  gmyth_debug("[%s] SET_CHANNEL name request returned NULL!",
renatofilho@750
   553
				  __FUNCTION__);
renatofilho@750
   554
	  ret = FALSE;
renatofilho@750
   555
	  goto cleanup;
renatofilho@750
   556
	}
rosfran@443
   557
renatofilho@750
   558
  if (tmp_str != NULL && g_ascii_strncasecmp(tmp_str->str, "ok", 2)
renatofilho@750
   559
	 /*|| g_ascii_strtoull( tmp_str->str, NULL, 10 ) == 0 */
renatofilho@750
   560
	)
renatofilho@750
   561
	{
renatofilho@750
   562
	  g_warning("[%s] SET_CHANNEL name request returned not ok",
renatofilho@750
   563
				__FUNCTION__);
renatofilho@750
   564
	  ret = FALSE;
renatofilho@750
   565
	  goto cleanup;
renatofilho@750
   566
	}
rosfran@287
   567
renatofilho@750
   568
cleanup:
renatofilho@750
   569
  g_mutex_unlock(recorder->mutex);
renatofilho@750
   570
  g_string_free(tmp_str, TRUE);
renatofilho@750
   571
  g_object_unref(str_list);
rosfran@287
   572
renatofilho@750
   573
  return ret;
rosfran@287
   574
}
rosfran@287
   575
rosfran@309
   576
/**
rosfran@309
   577
 * Changes the channel of the actual Recorder.
rosfran@309
   578
 * 
rosfran@309
   579
 * CHANNEL_DIRECTION_UP       - Go up one channel in the listing
rosfran@309
   580
 *
rosfran@309
   581
 * CHANNEL_DIRECTION_DOWN     - Go down one channel in the listing
rosfran@309
   582
 *
rosfran@309
   583
 * CHANNEL_DIRECTION_FAVORITE - Go to the next favorite channel
rosfran@309
   584
 *
rosfran@309
   585
 * CHANNEL_DIRECTION_SAME     - Stay
rosfran@309
   586
 * 
rosfran@309
   587
 * @param recorder 	 The GMythRecorder instance.
rosfran@309
   588
 * @param direction	 The new channel direction where to move to.
rosfran@309
   589
 * @return true if success, false if any error happens.
rosfran@309
   590
 */
rosfran@309
   591
gboolean
renatofilho@750
   592
gmyth_recorder_change_channel(GMythRecorder * recorder,
renatofilho@750
   593
							  const GMythRecorderChannelChangeDirection
renatofilho@750
   594
							  direction)
rosfran@698
   595
{
renatofilho@750
   596
  GMythStringList *str_list;
renatofilho@750
   597
  GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
renatofilho@750
   598
  gboolean ret = TRUE;
rosfran@309
   599
renatofilho@750
   600
  gmyth_debug("[%s] CHANGE_CHANNEL to the channel direction = %u",
renatofilho@750
   601
			  __FUNCTION__, direction);
rosfran@443
   602
renatofilho@750
   603
  str_list = gmyth_string_list_new();
rosfran@443
   604
renatofilho@750
   605
  g_mutex_lock(recorder->mutex);
rosfran@698
   606
renatofilho@750
   607
  g_string_append_printf(tmp_str, " %d", recorder->recorder_num);
rosfran@443
   608
renatofilho@750
   609
  gmyth_string_list_append_string(str_list, tmp_str);
renatofilho@750
   610
  g_string_free(tmp_str, TRUE);
leo_sobral@446
   611
renatofilho@750
   612
  gmyth_string_list_append_char_array(str_list, "CHANGE_CHANNEL");
renatofilho@750
   613
  gmyth_string_list_append_int(str_list, direction);
rosfran@443
   614
renatofilho@750
   615
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@443
   616
renatofilho@750
   617
  tmp_str = gmyth_string_list_get_string(str_list, 0);
rosfran@443
   618
renatofilho@750
   619
  if (tmp_str == NULL)
renatofilho@750
   620
	{
renatofilho@750
   621
	  gmyth_debug("[%s] CHANGE_CHANNEL name request returned %s",
renatofilho@750
   622
				  __FUNCTION__, tmp_str->str);
renatofilho@750
   623
	  ret = FALSE;
renatofilho@750
   624
	  goto cleanup;
renatofilho@750
   625
	}
rosfran@443
   626
renatofilho@750
   627
  if (g_ascii_strncasecmp(tmp_str->str, "ok", 2)
renatofilho@750
   628
	  || g_ascii_strtoull(tmp_str->str, NULL, 10) == 0)
renatofilho@750
   629
	{
renatofilho@750
   630
	  gmyth_debug("[%s] CHANGE_CHANNEL name request returned %s",
renatofilho@750
   631
				  __FUNCTION__, tmp_str->str);
renatofilho@750
   632
	  ret = FALSE;
renatofilho@750
   633
	  goto cleanup;
renatofilho@750
   634
	}
rosfran@309
   635
renatofilho@750
   636
cleanup:
renatofilho@750
   637
  g_mutex_unlock(recorder->mutex);
renatofilho@750
   638
  g_string_free(tmp_str, TRUE);
renatofilho@750
   639
  g_object_unref(str_list);
rosfran@309
   640
renatofilho@750
   641
  return ret;
rosfran@309
   642
}
rosfran@309
   643
rosfran@472
   644
/** 
rosfran@472
   645
 * Gets the channel's list from the MythTV backend server.
rosfran@472
   646
 * 
rosfran@472
   647
 * @param recorder The GMythRecorder instance.
rosfran@472
   648
 * 
rosfran@472
   649
 * @return a GList* instance with all the channel names.
rosfran@472
   650
 */
rosfran@698
   651
GList *
renatofilho@750
   652
gmyth_recorder_get_channel_list(GMythRecorder * recorder)
rosfran@698
   653
{
rosfran@698
   654
renatofilho@750
   655
  GList *channel_list = NULL;
renatofilho@750
   656
  gchar *channel = NULL;
renatofilho@750
   657
  guint i;
rosfran@698
   658
renatofilho@750
   659
  for (i = 0; i < 1000; i++)
renatofilho@750
   660
	{
renatofilho@750
   661
	  channel = g_strdup_printf("%u", i);
rosfran@698
   662
renatofilho@750
   663
	  if (gmyth_recorder_check_channel_name(recorder, channel))
renatofilho@750
   664
		{
renatofilho@750
   665
		  channel_list = g_list_append(channel_list, g_strdup(channel));
renatofilho@750
   666
		}
rosfran@698
   667
renatofilho@750
   668
	}							/* for - channel list */
rosfran@698
   669
renatofilho@750
   670
  g_free(channel);
rosfran@698
   671
renatofilho@750
   672
  return channel_list;
rosfran@698
   673
rosfran@472
   674
}
rosfran@472
   675
rosfran@287
   676
/** Send a PAUSE command request to the backend, to pause streaming on another 
rosfran@287
   677
 * TV content channel.
rosfran@287
   678
 * 
rosfran@287
   679
 * @param recorder The GMythRecorder instance.
rosfran@287
   680
 * @return true if success, false if any error happens.
rosfran@287
   681
 */
rosfran@287
   682
gboolean
renatofilho@750
   683
gmyth_recorder_pause_recording(GMythRecorder * recorder)
rosfran@698
   684
{
renatofilho@750
   685
  GMythStringList *str_list;
renatofilho@750
   686
  GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
renatofilho@750
   687
  gboolean ret = TRUE;
rosfran@287
   688
renatofilho@750
   689
  gmyth_debug("[%s] PAUSE", __FUNCTION__);
rosfran@443
   690
renatofilho@750
   691
  str_list = gmyth_string_list_new();
rosfran@443
   692
renatofilho@750
   693
  g_mutex_lock(recorder->mutex);
rosfran@698
   694
renatofilho@750
   695
  g_string_append_printf(tmp_str, " %d", recorder->recorder_num);
rosfran@443
   696
renatofilho@750
   697
  gmyth_string_list_append_string(str_list, tmp_str);
renatofilho@750
   698
  g_string_free(tmp_str, TRUE);
leo_sobral@446
   699
renatofilho@750
   700
  gmyth_string_list_append_char_array(str_list, "PAUSE");
rosfran@443
   701
renatofilho@750
   702
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@443
   703
renatofilho@750
   704
  tmp_str = gmyth_string_list_get_string(str_list, 0);
rosfran@443
   705
renatofilho@750
   706
  if (tmp_str == NULL)
renatofilho@750
   707
	{
renatofilho@750
   708
	  gmyth_debug("[%s] PAUSE name request returned %s", __FUNCTION__,
renatofilho@750
   709
				  tmp_str->str);
renatofilho@750
   710
	  ret = FALSE;
renatofilho@750
   711
	  goto cleanup;
renatofilho@750
   712
	}
rosfran@443
   713
renatofilho@750
   714
  if (g_ascii_strncasecmp(tmp_str->str, "ok", 2))
renatofilho@750
   715
	{
renatofilho@750
   716
	  gmyth_debug("[%s] PAUSE name request returned %s", __FUNCTION__,
renatofilho@750
   717
				  tmp_str->str);
renatofilho@750
   718
	  ret = FALSE;
renatofilho@750
   719
	  goto cleanup;
renatofilho@750
   720
	}
rosfran@287
   721
renatofilho@750
   722
cleanup:
renatofilho@750
   723
  g_mutex_unlock(recorder->mutex);
renatofilho@750
   724
  g_string_free(tmp_str, TRUE);
renatofilho@750
   725
  g_object_unref(str_list);
rosfran@287
   726
renatofilho@750
   727
  return ret;
rosfran@287
   728
}
rosfran@287
   729
rosfran@698
   730
static gboolean
renatofilho@750
   731
gmyth_recorder_find_if_program_exists(GMythRecorder * recorder,
renatofilho@750
   732
									  GMythProgramInfo * prog)
rosfran@443
   733
{
renatofilho@750
   734
  GList *lst = NULL;
rosfran@698
   735
renatofilho@750
   736
  g_return_val_if_fail(recorder != NULL
renatofilho@750
   737
					   && recorder->progs_info_list != NULL, FALSE);
rosfran@698
   738
renatofilho@750
   739
  for (lst = recorder->progs_info_list; lst != NULL; lst = g_list_next(lst))
renatofilho@750
   740
	{
renatofilho@750
   741
	  gmyth_debug("Got program info from list = [%s]",
renatofilho@750
   742
				  gmyth_program_info_to_string((GMythProgramInfo *)
renatofilho@750
   743
											   lst->data));
renatofilho@750
   744
	  if (gmyth_program_info_is_equals(prog, (GMythProgramInfo *) lst->data))
renatofilho@750
   745
		return TRUE;
renatofilho@750
   746
	}
rosfran@698
   747
renatofilho@750
   748
  return FALSE;
rosfran@443
   749
}
rosfran@443
   750
rosfran@291
   751
/**
rosfran@291
   752
 * Requests the actual program info from the MythTV backend server.
rosfran@291
   753
 * 
rosfran@291
   754
 * @param recorder The GMythRecorder instance.
rosfran@291
   755
 * @return The actual program info.
rosfran@291
   756
 */
rosfran@291
   757
GMythProgramInfo *
renatofilho@750
   758
gmyth_recorder_get_current_program_info(GMythRecorder * recorder)
leo_sobral@446
   759
{
renatofilho@750
   760
  GMythStringList *str_list = NULL;
renatofilho@750
   761
  GMythProgramInfo *program_info = NULL;
renatofilho@750
   762
  GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
rosfran@291
   763
renatofilho@750
   764
  str_list = gmyth_string_list_new();
rosfran@291
   765
renatofilho@750
   766
  g_mutex_lock(recorder->mutex);
rosfran@291
   767
renatofilho@750
   768
  g_string_append_printf(tmp_str, " %d", recorder->recorder_num);
rosfran@291
   769
renatofilho@750
   770
  gmyth_string_list_append_string(str_list, tmp_str);
rosfran@698
   771
renatofilho@750
   772
  if (recorder->myth_socket->mythtv_version >= 26)
renatofilho@750
   773
	gmyth_string_list_append_char_array(str_list, "GET_CURRENT_RECORDING");
renatofilho@750
   774
  else
renatofilho@750
   775
	gmyth_string_list_append_char_array(str_list, "GET_PROGRAM_INFO");
rosfran@291
   776
renatofilho@750
   777
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@443
   778
renatofilho@750
   779
  if (str_list == NULL)
renatofilho@750
   780
	{
renatofilho@750
   781
	  gmyth_debug
renatofilho@750
   782
		("[%s] GET_PROGRAM_INFO request returned. Error getting program info, string list equals to NULL!",
renatofilho@750
   783
		 __FUNCTION__);
renatofilho@750
   784
	  goto cleanup;
renatofilho@750
   785
	}
rosfran@443
   786
renatofilho@750
   787
  program_info = gmyth_program_info_from_string_list(str_list);
rosfran@443
   788
renatofilho@750
   789
  if (NULL == program_info || NULL == program_info->pathname
renatofilho@750
   790
	  || program_info->pathname->len <= 0)
renatofilho@750
   791
	{
renatofilho@750
   792
	  gmyth_debug
renatofilho@750
   793
		("GET_PROGRAM_INFO request returned. Error getting program info, it is equals to NULL!!!");
rosfran@698
   794
renatofilho@750
   795
	  if (program_info)
renatofilho@750
   796
		g_object_unref(program_info);
leo_sobral@446
   797
renatofilho@750
   798
	  program_info = NULL;
leo_sobral@446
   799
renatofilho@750
   800
	  goto cleanup;
renatofilho@750
   801
	}
rosfran@698
   802
renatofilho@750
   803
  if (!gmyth_recorder_find_if_program_exists(recorder, program_info))
renatofilho@750
   804
	recorder->progs_info_list = g_list_append(recorder->progs_info_list,
renatofilho@750
   805
											  g_object_ref(program_info));
renatofilho@750
   806
cleanup:
renatofilho@750
   807
  g_mutex_unlock(recorder->mutex);
renatofilho@750
   808
  g_string_free(tmp_str, TRUE);
renatofilho@750
   809
  g_object_unref(str_list);
rosfran@443
   810
renatofilho@750
   811
  return program_info;
rosfran@291
   812
}
rosfran@291
   813
rosfran@321
   814
/**
rosfran@321
   815
 * Requests the actual program info from the MythTV backend server.
rosfran@321
   816
 * 
rosfran@321
   817
 * @param rec_id The GMythRecorder record number.
rosfran@321
   818
 * @return The GMythRecorder instance.
rosfran@321
   819
 */
rosfran@321
   820
GMythRecorder *
renatofilho@750
   821
gmyth_recorder_get_recorder_from_num(gint rec_id)
leo_sobral@446
   822
{
renatofilho@750
   823
  GMythRecorder *recorder = NULL;
renatofilho@750
   824
  GMythStringList *str_list;
renatofilho@750
   825
  GString *tmp_str = g_string_new("GET_RECORDER_FROM_NUM");
renatofilho@750
   826
  gint command_size = 0;
rosfran@321
   827
renatofilho@750
   828
  gchar *recorder_host = NULL;
renatofilho@750
   829
  gint recorder_port;
rosfran@321
   830
renatofilho@750
   831
  str_list = gmyth_string_list_new();
rosfran@321
   832
renatofilho@750
   833
 /* g_string_append_printf ( tmp_str, " %d", recorder->recorder_num ); */
rosfran@321
   834
renatofilho@750
   835
  g_mutex_lock(recorder->mutex);
leo_sobral@446
   836
renatofilho@750
   837
  gmyth_string_list_append_string(str_list, tmp_str);
rosfran@443
   838
renatofilho@750
   839
  gmyth_string_list_append_int(str_list, rec_id);
rosfran@698
   840
renatofilho@750
   841
  command_size = gmyth_socket_sendreceive_stringlist(recorder->myth_socket,
renatofilho@750
   842
													 str_list);
rosfran@443
   843
renatofilho@750
   844
  if (str_list == NULL)
renatofilho@750
   845
	{
renatofilho@750
   846
	  gmyth_debug
renatofilho@750
   847
		("[%s] GET_RECORDER_FROM_NUM request returned. Error getting recorder number %d, it is equals to NULL!!!",
renatofilho@750
   848
		 __FUNCTION__, rec_id);
renatofilho@750
   849
	  return NULL;
renatofilho@750
   850
	}
rosfran@443
   851
renatofilho@750
   852
  if (command_size > 0)
renatofilho@750
   853
	{
renatofilho@750
   854
	  recorder_host = gmyth_string_list_get_char_array(str_list, 0);
renatofilho@750
   855
	  recorder_port = gmyth_string_list_get_int(str_list, 1);
rosfran@443
   856
renatofilho@750
   857
	  if (g_strstr_len(recorder_host, strlen(recorder_host), "nohost")
renatofilho@750
   858
		  != NULL)
renatofilho@750
   859
		{
renatofilho@750
   860
		  gmyth_debug
renatofilho@750
   861
			("No available recorder with the recorder ID number %d!", rec_id);
renatofilho@750
   862
		}
renatofilho@750
   863
	  else
renatofilho@750
   864
		{
rosfran@443
   865
renatofilho@750
   866
		  recorder = gmyth_recorder_new(rec_id,
renatofilho@750
   867
										g_string_new(recorder_host),
renatofilho@750
   868
										(gshort) recorder_port);
rosfran@443
   869
renatofilho@750
   870
		  if (NULL == recorder)
renatofilho@750
   871
			{
renatofilho@750
   872
			  gmyth_debug
renatofilho@750
   873
				("[%s] GET_RECORDER_FROM_NUM request returned. Error getting recorder number %d, it is equals to NULL!!!",
renatofilho@750
   874
				 __FUNCTION__, rec_id);
renatofilho@750
   875
			  g_object_unref(recorder);
renatofilho@750
   876
			  return NULL;
renatofilho@750
   877
			}
rosfran@443
   878
renatofilho@750
   879
		}
rosfran@443
   880
renatofilho@750
   881
	}
renatofilho@750
   882
  else
renatofilho@750
   883
	{
renatofilho@750
   884
	  gmyth_debug
renatofilho@750
   885
		("Cannot find a valuable recorder with the recorder ID number %d, backend server error!",
renatofilho@750
   886
		 rec_id);
renatofilho@750
   887
	}
rosfran@443
   888
renatofilho@750
   889
  g_mutex_unlock(recorder->mutex);
rosfran@443
   890
renatofilho@750
   891
  g_object_unref(str_list);
leo_sobral@446
   892
renatofilho@750
   893
  g_string_free(tmp_str, TRUE);
rosfran@698
   894
renatofilho@750
   895
  g_free(recorder_host);
rosfran@443
   896
renatofilho@750
   897
  return recorder;
rosfran@321
   898
rosfran@321
   899
}
rosfran@321
   900
rosfran@321
   901
/**
rosfran@321
   902
 * Requests the actual program info from the MythTV backend server.
rosfran@321
   903
 * 
rosfran@321
   904
 * @param recorder The GMythRecorder instance.
rosfran@568
   905
 * @param direction The direction to move based on the current channel (forward, backward,
rosfran@568
   906
 *            up, down).
rosfran@568
   907
 * 
rosfran@321
   908
 * @return The GMythProgramInfo next program info instance.
rosfran@321
   909
 */
rosfran@321
   910
GMythProgramInfo *
renatofilho@750
   911
gmyth_recorder_get_next_program_info(GMythRecorder * recorder,
renatofilho@750
   912
									 const GMythRecorderBrowseDirection
renatofilho@750
   913
									 direction)
rosfran@698
   914
{
renatofilho@750
   915
  GMythProgramInfo *actual_proginfo = NULL;
renatofilho@750
   916
  GMythProgramInfo *program_info = NULL;
renatofilho@750
   917
  GMythStringList *str_list;
renatofilho@750
   918
  GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
rosfran@321
   919
renatofilho@750
   920
  gchar *date = NULL;
renatofilho@750
   921
  struct tm *tm = NULL;
renatofilho@750
   922
  time_t t;
rosfran@321
   923
renatofilho@750
   924
  actual_proginfo = gmyth_recorder_get_current_program_info(recorder);
rosfran@321
   925
renatofilho@750
   926
  str_list = gmyth_string_list_new();
rosfran@443
   927
renatofilho@750
   928
  g_mutex_lock(recorder->mutex);
rosfran@443
   929
renatofilho@750
   930
  g_string_append_printf(tmp_str, " %d", recorder->recorder_num);
rosfran@443
   931
renatofilho@750
   932
  t = time(NULL);
renatofilho@750
   933
  tm = localtime(&t);
renatofilho@750
   934
  date = g_strdup_printf("%.4d%.2d%.2d%.2d%.2d%.2d", tm->tm_year + 1900,
renatofilho@750
   935
						 tm->tm_mon + 1, tm->tm_mday, tm->tm_hour,
renatofilho@750
   936
						 tm->tm_min, tm->tm_sec);
rosfran@443
   937
renatofilho@750
   938
  gmyth_string_list_append_string(str_list, tmp_str);
renatofilho@750
   939
  gmyth_string_list_append_char_array(str_list, "GET_NEXT_PROGRAM_INFO");
renatofilho@750
   940
  gmyth_string_list_append_string(str_list, actual_proginfo->channame);
renatofilho@750
   941
  gmyth_string_list_append_string(str_list, actual_proginfo->chanid);
renatofilho@750
   942
  gmyth_string_list_append_int(str_list, direction);
renatofilho@750
   943
  gmyth_string_list_append_char_array(str_list, date);
rosfran@698
   944
renatofilho@750
   945
  if (gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list)
renatofilho@750
   946
	  > 0)
renatofilho@750
   947
	{
rosfran@443
   948
renatofilho@750
   949
	  if (str_list == NULL)
renatofilho@750
   950
		{
renatofilho@750
   951
		  gmyth_debug
renatofilho@750
   952
			("[%s] GET_NEXT_PROGRAM_INFO request returned. Error getting program info, it is equals to NULL!!!",
renatofilho@750
   953
			 __FUNCTION__);
renatofilho@750
   954
		  goto done;
renatofilho@750
   955
		}
renatofilho@750
   956
	  program_info = gmyth_program_info_from_string_list_next_prog(str_list);
rosfran@443
   957
renatofilho@750
   958
	  if (NULL == program_info)
renatofilho@750
   959
		{
renatofilho@750
   960
		  gmyth_debug
renatofilho@750
   961
			("[%s] GET_NEXT_PROGRAM_INFO request returned. Error getting next program info, it is equals to NULL!!!",
renatofilho@750
   962
			 __FUNCTION__);
renatofilho@750
   963
		  g_object_unref(program_info);
renatofilho@750
   964
		  goto done;
renatofilho@750
   965
		}
rosfran@443
   966
renatofilho@750
   967
	  if (						/*( program_info->chanid != NULL && strlen( program_info->chanid->str ) > 0  ) && */
renatofilho@750
   968
		   (program_info->chansign != NULL
renatofilho@750
   969
			&& strlen(program_info->chansign->str) > 0))
renatofilho@750
   970
		{
renatofilho@750
   971
		  gmyth_debug("OK!!! Got the next program info... [%s].",
renatofilho@750
   972
					  program_info->chansign->str);
renatofilho@750
   973
		}
renatofilho@750
   974
	  else
renatofilho@750
   975
		{
renatofilho@750
   976
		  gmyth_debug
renatofilho@750
   977
			("GET_NEXT_PROGRAM_INFO request returned. Error getting next program info, it is equals to NULL!!!");
renatofilho@750
   978
		  g_object_unref(program_info);
renatofilho@750
   979
		  program_info = NULL;
renatofilho@750
   980
		}
rosfran@443
   981
renatofilho@750
   982
	}
renatofilho@750
   983
 /* if */
renatofilho@750
   984
done:
rosfran@443
   985
renatofilho@750
   986
  g_mutex_unlock(recorder->mutex);
rosfran@443
   987
renatofilho@750
   988
  if (actual_proginfo != NULL)
renatofilho@750
   989
	g_object_unref(actual_proginfo);
rosfran@698
   990
renatofilho@750
   991
  if (str_list != NULL)
renatofilho@750
   992
	g_object_unref(str_list);
rosfran@698
   993
renatofilho@750
   994
  if (tmp_str != NULL)
renatofilho@750
   995
	g_string_free(tmp_str, TRUE);
rosfran@698
   996
renatofilho@750
   997
  if (date != NULL)
renatofilho@750
   998
	g_free(date);
renatofilho@750
   999
 //if ( tm != NULL)
renatofilho@750
  1000
 //    g_free (tm);
leo_sobral@446
  1001
renatofilho@750
  1002
  return program_info;
rosfran@568
  1003
}
rosfran@568
  1004
rosfran@568
  1005
/**
rosfran@568
  1006
 * Requests the program info from the MythTV backend server, based on its 
rosfran@568
  1007
 * channel name.
rosfran@568
  1008
 * 
rosfran@568
  1009
 * @param recorder The GMythRecorder instance.
rosfran@568
  1010
 * @return The GMythProgramInfo next program info instance.
rosfran@568
  1011
 */
rosfran@568
  1012
GMythProgramInfo *
renatofilho@750
  1013
gmyth_recorder_get_program_info_from_channel_name(GMythRecorder * recorder,
renatofilho@750
  1014
												  const gchar * channel)
rosfran@698
  1015
{
renatofilho@750
  1016
 //GMythProgramInfo* actual_proginfo=  NULL;
renatofilho@750
  1017
  GMythProgramInfo *program_info = NULL;
renatofilho@750
  1018
  GMythStringList *str_list;
renatofilho@750
  1019
  GString *tmp_str = g_string_new(GMYTHTV_RECORDER_HEADER);
rosfran@568
  1020
renatofilho@750
  1021
 /*
renatofilho@750
  1022
    gchar *date =  NULL;
renatofilho@750
  1023
    struct tm *tm = NULL;
renatofilho@750
  1024
    time_t t;
rosfran@568
  1025
renatofilho@750
  1026
    actual_proginfo = gmyth_recorder_get_current_program_info(recorder);
renatofilho@750
  1027
  */
rosfran@698
  1028
renatofilho@750
  1029
  str_list = gmyth_string_list_new();
rosfran@698
  1030
renatofilho@750
  1031
  g_mutex_lock(recorder->mutex);
rosfran@698
  1032
renatofilho@750
  1033
  g_string_append_printf(tmp_str, " %d", recorder->recorder_num);
rosfran@698
  1034
renatofilho@750
  1035
 /*
renatofilho@750
  1036
    t = time(NULL);
renatofilho@750
  1037
    tm = localtime(&t);
renatofilho@750
  1038
    date = g_strdup_printf("%.4d%.2d%.2d%.2d%.2d%.2d", tm->tm_year + 1900,
renatofilho@750
  1039
    tm->tm_mon + 1, tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
renatofilho@750
  1040
  */
rosfran@698
  1041
renatofilho@750
  1042
  gmyth_string_list_append_string(str_list, tmp_str);
renatofilho@750
  1043
  gmyth_string_list_append_char_array(str_list, "GET_NEXT_PROGRAM_INFO");
renatofilho@750
  1044
  gmyth_string_list_append_char_array(str_list, channel);
renatofilho@750
  1045
  gmyth_string_list_append_char_array(str_list, "0");
renatofilho@750
  1046
  gmyth_string_list_append_int(str_list, BROWSE_DIRECTION_UP);
renatofilho@750
  1047
  gmyth_string_list_append_char_array(str_list, "0");
rosfran@698
  1048
renatofilho@750
  1049
  do
renatofilho@750
  1050
	{
rosfran@568
  1051
renatofilho@750
  1052
	  if (str_list != NULL &&
renatofilho@750
  1053
		  gmyth_socket_sendreceive_stringlist(recorder->myth_socket,
renatofilho@750
  1054
											  str_list) > 0)
renatofilho@750
  1055
		{
rosfran@568
  1056
renatofilho@750
  1057
		  if (str_list == NULL)
renatofilho@750
  1058
			{
renatofilho@750
  1059
			  gmyth_debug
renatofilho@750
  1060
				("[%s] GET_NEXT_PROGRAM_INFO request returned. Error getting program info, it is equals to NULL!!!",
renatofilho@750
  1061
				 __FUNCTION__);
renatofilho@750
  1062
			  goto done;
renatofilho@750
  1063
			}
renatofilho@750
  1064
		  program_info =
renatofilho@750
  1065
			gmyth_program_info_from_string_list_next_prog(str_list);
rosfran@698
  1066
renatofilho@750
  1067
		  if (NULL == program_info)
renatofilho@750
  1068
			{
renatofilho@750
  1069
			  gmyth_debug
renatofilho@750
  1070
				("[%s] GET_NEXT_PROGRAM_INFO request returned. Error getting next program info, it is equals to NULL!!!",
renatofilho@750
  1071
				 __FUNCTION__);
renatofilho@750
  1072
			  g_object_unref(program_info);
renatofilho@750
  1073
			  goto done;
renatofilho@750
  1074
			}
rosfran@698
  1075
renatofilho@750
  1076
		  if (					/*( program_info->chanid != NULL && strlen( program_info->chanid->str ) > 0  ) && */
renatofilho@750
  1077
			   (program_info->chansign != NULL
renatofilho@750
  1078
				&& strlen(program_info->chansign->str) > 0))
renatofilho@750
  1079
			{
renatofilho@750
  1080
			  gmyth_debug("OK!!! Got the next program info... [%s].",
renatofilho@750
  1081
						  program_info->chansign->str);
renatofilho@750
  1082
			}
renatofilho@750
  1083
		  else
renatofilho@750
  1084
			{
renatofilho@750
  1085
			  gmyth_debug
renatofilho@750
  1086
				("GET_NEXT_PROGRAM_INFO request returned. Error getting "
renatofilho@750
  1087
				 "next program info, it is equals to NULL!!!");
renatofilho@750
  1088
			  g_object_unref(program_info);
renatofilho@750
  1089
			  program_info = NULL;
renatofilho@750
  1090
			}
rosfran@698
  1091
renatofilho@750
  1092
		}
renatofilho@750
  1093
	 /* if */
renatofilho@750
  1094
	}
renatofilho@750
  1095
  while (str_list != NULL);
rosfran@698
  1096
renatofilho@750
  1097
done:
rosfran@698
  1098
renatofilho@750
  1099
  g_mutex_unlock(recorder->mutex);
rosfran@698
  1100
renatofilho@750
  1101
  if (str_list != NULL)
renatofilho@750
  1102
	g_object_unref(str_list);
rosfran@698
  1103
renatofilho@750
  1104
  if (tmp_str != NULL)
renatofilho@750
  1105
	g_string_free(tmp_str, TRUE);
rosfran@443
  1106
renatofilho@750
  1107
  return program_info;
rosfran@321
  1108
}
rosfran@321
  1109
rosfran@420
  1110
/**
rosfran@420
  1111
 * Requests the actual remote file position on a LiveTV instance.
rosfran@420
  1112
 * 
rosfran@420
  1113
 * @param recorder The GMythRecorder instance.
rosfran@420
  1114
 * 
rosfran@420
  1115
 * @return The position, in bytes, of the offset to the read header.
rosfran@420
  1116
 */
rosfran@115
  1117
gint64
renatofilho@750
  1118
gmyth_recorder_get_file_position(GMythRecorder * recorder)
rosfran@698
  1119
{
renatofilho@750
  1120
  gint64 pos = 0;
renatofilho@750
  1121
  GString *query = g_string_new(GMYTHTV_RECORDER_HEADER);
rosfran@115
  1122
renatofilho@750
  1123
  GMythStringList *str_list = gmyth_string_list_new();
rosfran@115
  1124
renatofilho@750
  1125
  g_mutex_lock(recorder->mutex);
rosfran@115
  1126
renatofilho@750
  1127
  g_string_append_printf(query, " %d", recorder->recorder_num);
rosfran@115
  1128
renatofilho@750
  1129
  gmyth_string_list_append_string(str_list, query);
renatofilho@750
  1130
  gmyth_string_list_append_char_array(str_list, "GET_FILE_POSITION");
rosfran@115
  1131
renatofilho@750
  1132
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@698
  1133
renatofilho@750
  1134
  if (str_list != NULL && gmyth_string_list_length(str_list) > 0)
renatofilho@750
  1135
	{
renatofilho@750
  1136
	  GString *str = NULL;
rosfran@698
  1137
renatofilho@750
  1138
	  if ((str = gmyth_string_list_get_string(str_list, 0)) != NULL
renatofilho@750
  1139
		  && strstr(str->str, "bad") == NULL)
renatofilho@750
  1140
		pos = gmyth_string_list_get_int64(str_list, 0);
renatofilho@750
  1141
	  g_string_free(str, TRUE);
renatofilho@750
  1142
	}
rosfran@698
  1143
#ifndef GMYTHTV_ENABLE_DEBUG
renatofilho@750
  1144
  gmyth_debug("[%s] Got file position = %lld\n", __FUNCTION__, pos);
rosfran@698
  1145
#endif
rosfran@115
  1146
renatofilho@750
  1147
  g_mutex_unlock(recorder->mutex);
leo_sobral@446
  1148
renatofilho@750
  1149
  if (str_list != NULL)
renatofilho@750
  1150
	g_object_unref(str_list);
rosfran@115
  1151
renatofilho@750
  1152
  g_string_free(query, TRUE);
leo_sobral@446
  1153
renatofilho@750
  1154
  return pos;
rosfran@115
  1155
}
rosfran@115
  1156
rosfran@420
  1157
/**
rosfran@420
  1158
 * Asks MythTV backend server about if it started to record the remote file.
rosfran@420
  1159
 * 
rosfran@420
  1160
 * @param recorder The GMythRecorder instance.
rosfran@420
  1161
 * 
rosfran@420
  1162
 * @return <code>true</code>, if the actual remote file is bein recorded.
rosfran@420
  1163
 */
rosfran@292
  1164
gboolean
renatofilho@750
  1165
gmyth_recorder_is_recording(GMythRecorder * recorder)
rosfran@698
  1166
{
renatofilho@750
  1167
  gboolean ret = TRUE;
rosfran@292
  1168
renatofilho@750
  1169
  g_return_val_if_fail(recorder != NULL, FALSE);
rosfran@292
  1170
renatofilho@750
  1171
  GMythStringList *str_list = gmyth_string_list_new();
renatofilho@750
  1172
  GString *message = g_string_new("");
rosfran@292
  1173
renatofilho@750
  1174
  g_mutex_lock(recorder->mutex);
rosfran@292
  1175
renatofilho@750
  1176
  g_string_printf(message, "%s %d", GMYTHTV_RECORDER_HEADER,
renatofilho@750
  1177
				  recorder->recorder_num);
renatofilho@750
  1178
  gmyth_string_list_append_string(str_list, message);
renatofilho@750
  1179
  gmyth_string_list_append_char_array(str_list, "IS_RECORDING");
rosfran@443
  1180
renatofilho@750
  1181
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@698
  1182
renatofilho@750
  1183
  if (str_list != NULL && gmyth_string_list_length(str_list) > 0)
renatofilho@750
  1184
	{
renatofilho@750
  1185
	  GString *str = NULL;
rosfran@698
  1186
renatofilho@750
  1187
	  if ((str = gmyth_string_list_get_string(str_list, 0)) != NULL
renatofilho@750
  1188
		  && strcmp(str->str, "bad") != 0)
renatofilho@750
  1189
		{
renatofilho@750
  1190
		  gint is_rec = gmyth_string_list_get_int(str_list, 0);
rosfran@698
  1191
renatofilho@750
  1192
		  if (is_rec != 0)
renatofilho@750
  1193
			ret = TRUE;
renatofilho@750
  1194
		  else
renatofilho@750
  1195
			ret = FALSE;
renatofilho@750
  1196
		}
renatofilho@750
  1197
	  g_string_free(str, TRUE);
renatofilho@750
  1198
	}
leo_sobral@446
  1199
renatofilho@750
  1200
  gmyth_debug("%s, stream is %s being recorded!\n", ret ? "YES" : "NO",
renatofilho@750
  1201
			  ret ? "" : "NOT");
renatofilho@750
  1202
 //g_static_mutex_unlock (&mutex);
rosfran@292
  1203
renatofilho@750
  1204
  g_mutex_unlock(recorder->mutex);
rosfran@698
  1205
renatofilho@750
  1206
  if (str_list != NULL)
renatofilho@750
  1207
	g_object_unref(str_list);
leo_sobral@446
  1208
renatofilho@750
  1209
  g_string_free(message, TRUE);
rosfran@292
  1210
renatofilho@750
  1211
  return ret;
rosfran@292
  1212
rosfran@292
  1213
}
rosfran@356
  1214
rosfran@420
  1215
/**
rosfran@420
  1216
 * Finish remote file recording process.
rosfran@420
  1217
 * 
rosfran@420
  1218
 * @param recorder The GMythRecorder instance.
rosfran@420
  1219
 * 
rosfran@420
  1220
 * @return <code>true</code>, if the recording had been actually closed.
rosfran@420
  1221
 */
rosfran@356
  1222
gboolean
renatofilho@750
  1223
gmyth_recorder_finish_recording(GMythRecorder * recorder)
rosfran@698
  1224
{
renatofilho@750
  1225
  gboolean ret = TRUE;
rosfran@356
  1226
renatofilho@750
  1227
  g_return_val_if_fail(recorder != NULL, FALSE);
rosfran@356
  1228
renatofilho@750
  1229
  GMythStringList *str_list = gmyth_string_list_new();
renatofilho@750
  1230
  GString *message = g_string_new("");
rosfran@356
  1231
renatofilho@750
  1232
  g_string_printf(message, "%s %d", GMYTHTV_RECORDER_HEADER,
renatofilho@750
  1233
				  recorder->recorder_num);
renatofilho@750
  1234
  gmyth_string_list_append_string(str_list, message);
renatofilho@750
  1235
  gmyth_string_list_append_char_array(str_list, "FINISH_RECORDING");
rosfran@443
  1236
renatofilho@750
  1237
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@698
  1238
renatofilho@750
  1239
  if (str_list != NULL && gmyth_string_list_length(str_list) > 0)
renatofilho@750
  1240
	{
renatofilho@750
  1241
	  GString *str = NULL;
rosfran@698
  1242
renatofilho@750
  1243
	  if ((str = gmyth_string_list_get_string(str_list, 0)) != NULL &&
renatofilho@750
  1244
		  strcmp(str->str, "ok") != 0)
renatofilho@750
  1245
		{
renatofilho@750
  1246
		  gint is_rec = gmyth_string_list_get_int(str_list, 0);
rosfran@698
  1247
renatofilho@750
  1248
		  if (is_rec != 0)
renatofilho@750
  1249
			ret = TRUE;
renatofilho@750
  1250
		  else
renatofilho@750
  1251
			ret = FALSE;
renatofilho@750
  1252
		}
renatofilho@750
  1253
	  g_string_free(str, TRUE);
renatofilho@750
  1254
	}
leo_sobral@446
  1255
renatofilho@750
  1256
  gmyth_debug("%s, stream is %s finished!\n", ret ? "YES" : "NO",
renatofilho@750
  1257
			  ret ? "" : "NOT");
renatofilho@750
  1258
 //g_static_mutex_unlock (&mutex);
rosfran@356
  1259
renatofilho@750
  1260
  if (str_list != NULL)
renatofilho@750
  1261
	g_object_unref(str_list);
rosfran@698
  1262
renatofilho@750
  1263
  g_string_free(message, TRUE);
rosfran@356
  1264
renatofilho@750
  1265
  return ret;
rosfran@356
  1266
}
rosfran@462
  1267
rosfran@462
  1268
rosfran@462
  1269
/**
rosfran@462
  1270
 * Stops playing the remote file.
rosfran@462
  1271
 * 
rosfran@462
  1272
 * @param recorder The GMythRecorder instance.
rosfran@462
  1273
 * 
rosfran@462
  1274
 * @return <code>true</code>, if the recording had been actually stopped.
rosfran@462
  1275
 */
rosfran@462
  1276
gboolean
renatofilho@750
  1277
gmyth_recorder_stop_playing(GMythRecorder * recorder)
rosfran@698
  1278
{
renatofilho@750
  1279
  gboolean ret = TRUE;
rosfran@462
  1280
renatofilho@750
  1281
  g_return_val_if_fail(recorder != NULL, FALSE);
rosfran@462
  1282
renatofilho@750
  1283
  GMythStringList *str_list = gmyth_string_list_new();
renatofilho@750
  1284
  GString *message = g_string_new("");
rosfran@462
  1285
renatofilho@750
  1286
  g_string_printf(message, "%s %d", GMYTHTV_RECORDER_HEADER,
renatofilho@750
  1287
				  recorder->recorder_num);
renatofilho@750
  1288
  gmyth_string_list_append_string(str_list, message);
renatofilho@750
  1289
  gmyth_string_list_append_char_array(str_list, "STOP_PLAYING");
rosfran@462
  1290
renatofilho@750
  1291
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@698
  1292
renatofilho@750
  1293
  if (str_list != NULL && gmyth_string_list_length(str_list) > 0)
renatofilho@750
  1294
	{
renatofilho@750
  1295
	  GString *str = NULL;
rosfran@698
  1296
renatofilho@750
  1297
	  if ((str = gmyth_string_list_get_string(str_list, 0)) != NULL &&
renatofilho@750
  1298
		  strcmp(str->str, "ok") != 0)
renatofilho@750
  1299
		{
renatofilho@750
  1300
		  gint is_rec = gmyth_string_list_get_int(str_list, 0);
rosfran@698
  1301
renatofilho@750
  1302
		  if (is_rec != 0)
renatofilho@750
  1303
			ret = TRUE;
renatofilho@750
  1304
		  else
renatofilho@750
  1305
			ret = FALSE;
renatofilho@750
  1306
		}
renatofilho@750
  1307
	  g_string_free(str, TRUE);
renatofilho@750
  1308
	}
rosfran@462
  1309
renatofilho@750
  1310
  gmyth_debug("%s, stream is %s stopped!\n", ret ? "YES" : "NO",
renatofilho@750
  1311
			  ret ? "" : "NOT");
rosfran@462
  1312
renatofilho@750
  1313
  if (str_list != NULL)
renatofilho@750
  1314
	g_object_unref(str_list);
rosfran@698
  1315
renatofilho@750
  1316
  g_string_free(message, TRUE);
rosfran@462
  1317
renatofilho@750
  1318
  return ret;
rosfran@462
  1319
}
rosfran@463
  1320
rosfran@463
  1321
/**
rosfran@463
  1322
 * Free the tuner responsible for recording this channel.
rosfran@463
  1323
 * 
rosfran@463
  1324
 * @param recorder The GMythRecorder instance.
rosfran@463
  1325
 * 
rosfran@463
  1326
 * @return <code>true</code>, if the tuner had been freed.
rosfran@463
  1327
 */
rosfran@463
  1328
gboolean
renatofilho@750
  1329
gmyth_recorder_free_tuner(GMythRecorder * recorder)
rosfran@698
  1330
{
renatofilho@750
  1331
  gboolean ret = TRUE;
rosfran@463
  1332
renatofilho@750
  1333
  g_return_val_if_fail(recorder != NULL, FALSE);
rosfran@463
  1334
renatofilho@750
  1335
  GMythStringList *str_list = gmyth_string_list_new();
renatofilho@750
  1336
  GString *message = g_string_new("");
rosfran@463
  1337
renatofilho@750
  1338
  g_string_printf(message, "%s %d", "FREE_TUNER", recorder->recorder_num);
renatofilho@750
  1339
  gmyth_string_list_append_string(str_list, message);
rosfran@463
  1340
renatofilho@750
  1341
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@698
  1342
renatofilho@750
  1343
  if (str_list != NULL && gmyth_string_list_length(str_list) > 0)
renatofilho@750
  1344
	{
renatofilho@750
  1345
	  GString *str = NULL;
rosfran@698
  1346
renatofilho@750
  1347
	  if ((str = gmyth_string_list_get_string(str_list, 0)) != NULL &&
renatofilho@750
  1348
		  g_ascii_strncasecmp(str->str, "ok", 2) != 0)
renatofilho@750
  1349
		{
renatofilho@750
  1350
		  gint is_rec = gmyth_string_list_get_int(str_list, 0);
rosfran@698
  1351
renatofilho@750
  1352
		  if (is_rec != 0)
renatofilho@750
  1353
			ret = TRUE;
renatofilho@750
  1354
		  else
renatofilho@750
  1355
			ret = FALSE;
renatofilho@750
  1356
		}
renatofilho@750
  1357
	  g_string_free(str, TRUE);
renatofilho@750
  1358
	}
rosfran@463
  1359
renatofilho@750
  1360
  gmyth_debug("%s, tuner is %s freed!\n", ret ? "YES" : "NO",
renatofilho@750
  1361
			  ret ? "" : "NOT");
rosfran@463
  1362
renatofilho@750
  1363
  if (str_list != NULL)
renatofilho@750
  1364
	g_object_unref(str_list);
rosfran@698
  1365
renatofilho@750
  1366
  g_string_free(message, TRUE);
rosfran@463
  1367
renatofilho@750
  1368
  return ret;
rosfran@463
  1369
}
rosfran@493
  1370
rosfran@493
  1371
/**
rosfran@493
  1372
 * Asks the MythTV backend server about the frame rate 
rosfran@493
  1373
 * of this LiveTV instance.
rosfran@493
  1374
 * 
rosfran@493
  1375
 * @param recorder The GMythRecorder instance.
rosfran@493
  1376
 * 
rosfran@493
  1377
 * @return The framerate (double value) of the current video.
rosfran@493
  1378
 */
rosfran@493
  1379
gdouble
renatofilho@750
  1380
gmyth_recorder_get_framerate(GMythRecorder * recorder)
renatofilho@500
  1381
{
renatofilho@750
  1382
  gdouble fr = 0.0f;
renatofilho@750
  1383
  GString *query = g_string_new(GMYTHTV_RECORDER_HEADER);
rosfran@493
  1384
renatofilho@750
  1385
  GMythStringList *str_list = gmyth_string_list_new();
rosfran@493
  1386
renatofilho@750
  1387
  g_mutex_lock(recorder->mutex);
rosfran@493
  1388
renatofilho@750
  1389
  g_string_append_printf(query, " %d", recorder->recorder_num);
rosfran@493
  1390
renatofilho@750
  1391
  gmyth_string_list_append_string(str_list, query);
renatofilho@750
  1392
  gmyth_string_list_append_char_array(str_list, "GET_FRAMERATE");
rosfran@493
  1393
renatofilho@750
  1394
  gmyth_socket_sendreceive_stringlist(recorder->myth_socket, str_list);
rosfran@698
  1395
renatofilho@750
  1396
  if (str_list != NULL && gmyth_string_list_length(str_list) > 0)
renatofilho@750
  1397
	{
renatofilho@750
  1398
	  GString *str = NULL;
rosfran@698
  1399
renatofilho@750
  1400
	  if ((str = gmyth_string_list_get_string(str_list, 0)) != NULL
renatofilho@750
  1401
		  && strstr(str->str, "bad") == NULL)
renatofilho@750
  1402
		fr = g_ascii_strtod(str->str, NULL);
rosfran@698
  1403
renatofilho@750
  1404
	  g_string_free(str, TRUE);
renatofilho@750
  1405
	}
rosfran@698
  1406
#ifndef GMYTHTV_ENABLE_DEBUG
renatofilho@750
  1407
  gmyth_debug("[%s] Got file position = %f\n", __FUNCTION__, fr);
rosfran@698
  1408
#endif
rosfran@493
  1409
renatofilho@750
  1410
  g_mutex_unlock(recorder->mutex);
rosfran@493
  1411
renatofilho@750
  1412
  if (str_list != NULL)
renatofilho@750
  1413
	g_object_unref(str_list);
rosfran@493
  1414
renatofilho@750
  1415
  g_string_free(query, TRUE);
rosfran@493
  1416
renatofilho@750
  1417
  return fr;
rosfran@529
  1418
renatofilho@500
  1419
}