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