gst-plugins-mythtv/src/gstmythtvsrc.c
author rosfran
Tue Nov 28 00:53:25 2006 +0000 (2006-11-28)
branchtrunk
changeset 123 253b9171a703
parent 114 04cead6ae39b
child 124 d2d5fe1c3997
permissions -rwxr-xr-x
[svn r124] Fixes to the newly added BackendInfo issues.
leo_sobral@2
     1
/* vim: set sw=2: -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2; c-indent-level: 2 -*- */
leo_sobral@2
     2
/* GStreamer MythTV Plug-in
leo_sobral@2
     3
 * Copyright (C) <2006> Rosfran Borges <rosfran.borges@indt.org.br>
leo_sobral@2
     4
 *
leo_sobral@2
     5
 * This library is free software; you can redistribute it and/or
rosfran@30
     6
 * modify it under the terms of the GNU Library Lesser General 
rosfran@30
     7
 * Public License as published by the Free Software Foundation; either
leo_sobral@2
     8
 * version 2 of the License, or (at your option) any later version.
leo_sobral@2
     9
 *
leo_sobral@2
    10
 * This library is distributed in the hope that it will be useful,
leo_sobral@2
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
leo_sobral@2
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
leo_sobral@2
    13
 * Library General Public License for more 
leo_sobral@2
    14
 */
leo_sobral@2
    15
leo_sobral@2
    16
#ifdef HAVE_CONFIG_H
leo_sobral@2
    17
#include "config.h"
leo_sobral@2
    18
#endif
leo_sobral@2
    19
leo_sobral@2
    20
#include "gstmythtvsrc.h"
rosfran@52
    21
#include <gmyth/gmyth_file_transfer.h>
rosfran@52
    22
#include <gmyth/gmyth_livetv.h>
leo_sobral@2
    23
leo_sobral@2
    24
#include <gmyth/gmyth_socket.h>
leo_sobral@2
    25
#include <gmyth/gmyth_tvchain.h>
leo_sobral@2
    26
leo_sobral@2
    27
#include <string.h>
leo_sobral@2
    28
#include <unistd.h>
leo_sobral@2
    29
leo_sobral@2
    30
GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
leo_sobral@2
    31
#define GST_CAT_DEFAULT mythtvsrc_debug
leo_sobral@2
    32
rosfran@95
    33
#define GST_GMYTHTV_ID_NUM			1
rosfran@70
    34
rosfran@70
    35
#define GST_GMYTHTV_CHANNEL_NUM			1000
leo_sobral@2
    36
rosfran@52
    37
#define GMYTHTV_VERSION_DEFAULT			30
rosfran@61
    38
 
rosfran@90
    39
#define GMYTHTV_TRANSFER_MAX_WAITS		100
leo_sobral@2
    40
rosfran@102
    41
#define GMYTHTV_TRANSFER_MAX_RESENDS	5
rosfran@102
    42
rosfran@90
    43
#define GMYTHTV_TRANSFER_MAX_BUFFER		128*1024
rosfran@30
    44
//( 32*1024  )
leo_sobral@2
    45
leo_sobral@2
    46
/* 4*1024 ??? */
rosfran@98
    47
#define MAX_READ_SIZE              		4*1024
rosfran@30
    48
//( 32*1024 )
leo_sobral@2
    49
rosfran@98
    50
#define GST_FLOW_ERROR_NO_DATA  			-101
rosfran@70
    51
rosfran@110
    52
#define INTERNAL_BUFFER_SIZE					40*1024
rosfran@90
    53
leo_sobral@2
    54
/* stablish a maximum iteration value to the IS_RECORDING message */
leo_sobral@2
    55
static guint wait_to_transfer = 0;
leo_sobral@2
    56
leo_sobral@2
    57
static const GstElementDetails gst_mythtv_src_details =
rosfran@30
    58
GST_ELEMENT_DETAILS ( "MythTV client source",
leo_sobral@2
    59
    "Source/Network",
leo_sobral@2
    60
    "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
rosfran@30
    61
    "Rosfran Borges <rosfran.borges@indt.org.br>" );
leo_sobral@2
    62
rosfran@81
    63
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ( "src",
leo_sobral@2
    64
    GST_PAD_SRC,
leo_sobral@2
    65
    GST_PAD_ALWAYS,
rosfran@30
    66
    GST_STATIC_CAPS ("video/x-nuv") );
rosfran@81
    67
    
leo_sobral@2
    68
enum
leo_sobral@2
    69
{
leo_sobral@2
    70
  PROP_0,
leo_sobral@2
    71
  PROP_LOCATION,
leo_sobral@2
    72
  PROP_URI,
leo_sobral@2
    73
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
    74
  PROP_GMYTHTV_DBG,
leo_sobral@2
    75
#endif
rosfran@40
    76
  PROP_GMYTHTV_VERSION,
rosfran@40
    77
  PROP_GMYTHTV_LIVE,
rosfran@40
    78
  PROP_GMYTHTV_LIVEID,
rosfran@52
    79
  PROP_GMYTHTV_LIVE_CHAINID,
rosfran@70
    80
  PROP_GMYTHTV_ENABLE_TIMING_POSITION,
rosfran@70
    81
  PROP_GMYTHTV_CHANNEL_NUM
leo_sobral@2
    82
};
leo_sobral@2
    83
leo_sobral@2
    84
static void gst_mythtv_src_finalize (GObject * gobject);
leo_sobral@2
    85
rosfran@100
    86
static GstFlowReturn gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf );
rosfran@52
    87
leo_sobral@2
    88
static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
leo_sobral@2
    89
static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
leo_sobral@2
    90
static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
rosfran@87
    91
static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
rosfran@87
    92
rosfran@106
    93
/*
rosfran@106
    94
static void gst_mythtv_src_get_times (GstBaseSrc * src, GstBuffer * buffer,
rosfran@106
    95
   GstClockTime * start, GstClockTime * end);
rosfran@106
    96
*/
rosfran@98
    97
rosfran@100
    98
static gboolean gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment );
rosfran@52
    99
rosfran@52
   100
static gboolean gst_mythtv_src_next_program_chain ( GstMythtvSrc *src );
rosfran@52
   101
rosfran@52
   102
static GstStateChangeReturn
rosfran@52
   103
gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
leo_sobral@2
   104
leo_sobral@2
   105
static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
leo_sobral@2
   106
    const GValue * value, GParamSpec * pspec);
leo_sobral@2
   107
static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
leo_sobral@2
   108
    GValue * value, GParamSpec * pspec);
leo_sobral@2
   109
rosfran@30
   110
static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
rosfran@30
   111
rosfran@87
   112
static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
rosfran@87
   113
rosfran@90
   114
static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
rosfran@30
   115
rosfran@102
   116
static gint do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr);
rosfran@90
   117
rosfran@90
   118
GStaticRecMutex th_mutex = G_STATIC_REC_MUTEX_INIT;
leo_sobral@2
   119
rosfran@52
   120
static void
leo_sobral@2
   121
_urihandler_init (GType type)
leo_sobral@2
   122
{
leo_sobral@2
   123
  static const GInterfaceInfo urihandler_info = {
leo_sobral@2
   124
    gst_mythtv_src_uri_handler_init,
leo_sobral@2
   125
    NULL,
leo_sobral@2
   126
    NULL
leo_sobral@2
   127
  };
leo_sobral@2
   128
leo_sobral@2
   129
  g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
leo_sobral@2
   130
leo_sobral@2
   131
  GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
leo_sobral@2
   132
      "MythTV src");
leo_sobral@2
   133
}
leo_sobral@2
   134
rosfran@100
   135
GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
rosfran@100
   136
    GST_TYPE_PUSH_SRC, _urihandler_init)
rosfran@30
   137
    
rosfran@87
   138
static void
leo_sobral@2
   139
gst_mythtv_src_base_init (gpointer g_class)
leo_sobral@2
   140
{
leo_sobral@2
   141
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
leo_sobral@2
   142
leo_sobral@2
   143
  gst_element_class_add_pad_template (element_class,
leo_sobral@2
   144
      gst_static_pad_template_get (&srctemplate));
rosfran@81
   145
      
leo_sobral@2
   146
  gst_element_class_set_details (element_class, &gst_mythtv_src_details);
rosfran@52
   147
  
rosfran@52
   148
  element_class->change_state = gst_mythtv_src_change_state;
rosfran@81
   149
  
leo_sobral@2
   150
}
leo_sobral@2
   151
rosfran@30
   152
static void
leo_sobral@2
   153
gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
leo_sobral@2
   154
{
rosfran@63
   155
  GObjectClass *gobject_class; 
rosfran@100
   156
  GstPushSrcClass *gstpushsrc_class;
leo_sobral@2
   157
  GstBaseSrcClass *gstbasesrc_class;
leo_sobral@2
   158
leo_sobral@2
   159
  gobject_class = (GObjectClass *) klass;
leo_sobral@2
   160
  gstbasesrc_class = (GstBaseSrcClass *) klass;
rosfran@100
   161
  gstpushsrc_class = (GstPushSrcClass *) klass;
leo_sobral@2
   162
leo_sobral@2
   163
  gobject_class->set_property = gst_mythtv_src_set_property;
leo_sobral@2
   164
  gobject_class->get_property = gst_mythtv_src_get_property;
leo_sobral@2
   165
  gobject_class->finalize = gst_mythtv_src_finalize;
leo_sobral@2
   166
leo_sobral@2
   167
  g_object_class_install_property
leo_sobral@2
   168
    (gobject_class, PROP_LOCATION,
leo_sobral@2
   169
     g_param_spec_string ("location", "Location",
leo_sobral@2
   170
       "The location. In the form:"
leo_sobral@2
   171
       "\n\t\t\tmyth://a.com/file.nuv"
leo_sobral@2
   172
       "\n\t\t\tmyth://a.com:23223/file.nuv "
leo_sobral@2
   173
       "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
leo_sobral@2
   174
       "", G_PARAM_READWRITE));
leo_sobral@2
   175
leo_sobral@2
   176
  g_object_class_install_property
leo_sobral@2
   177
    (gobject_class, PROP_URI,
leo_sobral@2
   178
     g_param_spec_string ("uri", "Uri",
leo_sobral@2
   179
       "The location in form of a URI (deprecated; use location)",
leo_sobral@2
   180
       "", G_PARAM_READWRITE));
leo_sobral@2
   181
leo_sobral@2
   182
  g_object_class_install_property
rosfran@40
   183
    (gobject_class, PROP_GMYTHTV_VERSION,
leo_sobral@2
   184
     g_param_spec_int ("mythtv-version", "mythtv-version",
rosfran@70
   185
       "Change MythTV version",
leo_sobral@2
   186
       26, 30, 26, G_PARAM_READWRITE));
leo_sobral@2
   187
leo_sobral@2
   188
  g_object_class_install_property
rosfran@40
   189
    (gobject_class, PROP_GMYTHTV_LIVEID,
leo_sobral@2
   190
     g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
rosfran@70
   191
       "Change MythTV version",
rosfran@40
   192
       0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
leo_sobral@2
   193
leo_sobral@2
   194
  g_object_class_install_property
rosfran@40
   195
    (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
leo_sobral@2
   196
     g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
rosfran@70
   197
       "Sets the MythTV chain ID (from TV Chain)",
leo_sobral@2
   198
       "", G_PARAM_READWRITE));
leo_sobral@2
   199
leo_sobral@2
   200
  g_object_class_install_property
rosfran@40
   201
    (gobject_class, PROP_GMYTHTV_LIVE,
leo_sobral@2
   202
     g_param_spec_boolean ("mythtv-live", "mythtv-live",
leo_sobral@2
   203
       "Enable MythTV Live TV content streaming",
leo_sobral@2
   204
       FALSE, G_PARAM_READWRITE));
leo_sobral@2
   205
rosfran@52
   206
  g_object_class_install_property
rosfran@52
   207
    (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
rosfran@52
   208
     g_param_spec_boolean ("mythtv-enable-timing-position", "mythtv-enable-timing-position",
rosfran@52
   209
       "Enable MythTV Live TV content size continuous updating",
rosfran@52
   210
       FALSE, G_PARAM_READWRITE));
rosfran@70
   211
       
rosfran@70
   212
  g_object_class_install_property
rosfran@70
   213
    (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
rosfran@70
   214
     g_param_spec_int ("mythtv-channel", "mythtv-channel",
rosfran@70
   215
       "Change MythTV channel number",
rosfran@71
   216
       0, 99999, GST_GMYTHTV_CHANNEL_NUM, G_PARAM_READWRITE));
rosfran@52
   217
leo_sobral@2
   218
#ifndef GST_DISABLE_GST_DEBUG
leo_sobral@2
   219
  g_object_class_install_property
rosfran@40
   220
    (gobject_class, PROP_GMYTHTV_DBG,
leo_sobral@2
   221
     g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
leo_sobral@2
   222
       "Enable MythTV debug messages",
leo_sobral@2
   223
       FALSE, G_PARAM_READWRITE));
leo_sobral@2
   224
#endif
leo_sobral@2
   225
leo_sobral@2
   226
  gstbasesrc_class->start = gst_mythtv_src_start;
leo_sobral@2
   227
  gstbasesrc_class->stop = gst_mythtv_src_stop;
leo_sobral@2
   228
  gstbasesrc_class->get_size = gst_mythtv_src_get_size;
rosfran@87
   229
  gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
rosfran@81
   230
  
rosfran@98
   231
  //gstbasesrc_class->get_times = gst_mythtv_src_get_times;
rosfran@98
   232
  
rosfran@100
   233
  gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
rosfran@100
   234
  gstpushsrc_class->create = gst_mythtv_src_create;
rosfran@52
   235
    
leo_sobral@2
   236
  GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
leo_sobral@2
   237
      "MythTV Client Source");
leo_sobral@2
   238
}
leo_sobral@2
   239
rosfran@30
   240
static void
leo_sobral@2
   241
gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
leo_sobral@2
   242
{
leo_sobral@2
   243
  this->file_transfer = NULL;
leo_sobral@2
   244
leo_sobral@2
   245
  this->unique_setup = FALSE;
leo_sobral@2
   246
rosfran@40
   247
  this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
rosfran@70
   248
  
rosfran@70
   249
  this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
leo_sobral@2
   250
leo_sobral@2
   251
  this->bytes_read = 0;
rosfran@63
   252
  
rosfran@63
   253
  this->prev_content_size = 0;
leo_sobral@2
   254
rosfran@52
   255
  this->content_size = 0;
leo_sobral@2
   256
  this->read_offset = 0;
leo_sobral@2
   257
rosfran@61
   258
  this->content_size_last = 0;
rosfran@61
   259
leo_sobral@2
   260
  this->live_tv = FALSE;
rosfran@52
   261
  
rosfran@52
   262
  this->enable_timing_position = FALSE;
rosfran@52
   263
  this->update_prog_chain = FALSE;    
leo_sobral@2
   264
leo_sobral@2
   265
  this->user_agent = g_strdup ("mythtvsrc");
rosfran@52
   266
  this->mythtv_caps = NULL;
rosfran@52
   267
  this->update_prog_chain = FALSE;
rosfran@52
   268
  
rosfran@70
   269
  this->channel_num = 0;
rosfran@70
   270
  
rosfran@52
   271
  this->eos = FALSE;
rosfran@30
   272
  
rosfran@98
   273
  this->bytes_queue = NULL;
rosfran@70
   274
  
rosfran@81
   275
  this->th_mutex = NULL;
rosfran@81
   276
  
rosfran@87
   277
  gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );  
leo_sobral@2
   278
rosfran@106
   279
  /* gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE ); */
rosfran@81
   280
  
rosfran@90
   281
  gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
rosfran@90
   282
      gst_mythtv_src_handle_event );
rosfran@87
   283
  gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
rosfran@87
   284
     gst_mythtv_src_handle_query );
rosfran@30
   285
leo_sobral@2
   286
}
leo_sobral@2
   287
rosfran@30
   288
static void
leo_sobral@2
   289
gst_mythtv_src_finalize (GObject * gobject)
leo_sobral@2
   290
{
leo_sobral@2
   291
  GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
rosfran@114
   292
rosfran@81
   293
  if ( this->th_read_ahead != NULL ) {
rosfran@114
   294
    gst_task_stop( this->th_read_ahead );
rosfran@114
   295
    this->th_read_ahead = NULL;
rosfran@81
   296
  }
leo_sobral@2
   297
leo_sobral@2
   298
  if (this->mythtv_caps) {
leo_sobral@2
   299
    gst_caps_unref (this->mythtv_caps);
leo_sobral@2
   300
    this->mythtv_caps = NULL;
leo_sobral@2
   301
  }
leo_sobral@2
   302
leo_sobral@2
   303
  if (this->file_transfer) {
leo_sobral@2
   304
    g_object_unref (this->file_transfer);
leo_sobral@2
   305
    this->file_transfer = NULL;
leo_sobral@2
   306
  }
leo_sobral@2
   307
rosfran@114
   308
  if (this->spawn_livetv) {
rosfran@70
   309
    g_object_unref (this->spawn_livetv);
rosfran@52
   310
    this->spawn_livetv = NULL;
rosfran@52
   311
  }
rosfran@118
   312
  
rosfran@118
   313
  if (this->backend_info) {
rosfran@118
   314
    g_object_unref (this->backend_info);
rosfran@118
   315
    this->backend_info = NULL;
rosfran@118
   316
  }
rosfran@52
   317
leo_sobral@2
   318
  if (this->uri_name) {
leo_sobral@2
   319
    g_free (this->uri_name);
leo_sobral@2
   320
  }
leo_sobral@2
   321
leo_sobral@2
   322
  if (this->user_agent) {
leo_sobral@2
   323
    g_free (this->user_agent);
leo_sobral@2
   324
  }
rosfran@114
   325
rosfran@100
   326
  if ( this->bytes_queue ) {
rosfran@114
   327
    g_byte_array_free( this->bytes_queue, TRUE );
rosfran@114
   328
    this->bytes_queue = NULL;
rosfran@100
   329
  }
leo_sobral@2
   330
leo_sobral@2
   331
  G_OBJECT_CLASS (parent_class)->finalize (gobject);
leo_sobral@2
   332
}
leo_sobral@2
   333
rosfran@37
   334
static gint
rosfran@102
   335
do_read_request_response (GstMythtvSrc * src, guint size, GByteArray *data_ptr)
leo_sobral@2
   336
{
rosfran@37
   337
  gint read = 0;
rosfran@52
   338
  guint sizetoread = size;
rosfran@102
   339
  guint max_iters = GMYTHTV_TRANSFER_MAX_RESENDS;
rosfran@102
   340
  
rosfran@106
   341
  GST_LOG_OBJECT( src, "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); 
leo_sobral@2
   342
rosfran@30
   343
  /* Loop sending the Myth File Transfer request:
leo_sobral@2
   344
   * Retry whilst authentication fails and we supply it. */
rosfran@37
   345
  gint len = 0;
rosfran@70
   346
  
rosfran@102
   347
  while ( sizetoread == size && --max_iters > 0) {
rosfran@61
   348
rosfran@61
   349
    len = gmyth_file_transfer_read( src->file_transfer,
rosfran@102
   350
				data_ptr, sizetoread, TRUE );
leo_sobral@2
   351
leo_sobral@2
   352
    if ( len > 0 ) {
rosfran@81
   353
      read += len;
leo_sobral@2
   354
      sizetoread -= len;
rosfran@52
   355
    } 
rosfran@90
   356
    else if ( len < 0 )
rosfran@52
   357
    {
rosfran@90
   358
      read = -1;
rosfran@61
   359
rosfran@52
   360
      if ( src->live_tv == FALSE ) 
rosfran@52
   361
      {
rosfran@90
   362
	goto eos;
rosfran@52
   363
      } 
rosfran@52
   364
      else  
rosfran@52
   365
      {
rosfran@90
   366
	if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */
rosfran@90
   367
	  src->update_prog_chain = TRUE;
rosfran@90
   368
	  goto done;	  	
rosfran@100
   369
	} 
rosfran@90
   370
	goto done;
rosfran@90
   371
      }
rosfran@30
   372
rosfran@61
   373
    }
rosfran@61
   374
rosfran@30
   375
    if ( read == sizetoread )
rosfran@90
   376
      goto done;
rosfran@100
   377
  }
rosfran@61
   378
rosfran@102
   379
  if ( ( read < 0 && !src->live_tv ) || max_iters == 0 )
rosfran@61
   380
    goto eos;
rosfran@61
   381
rosfran@30
   382
  goto done;
leo_sobral@2
   383
leo_sobral@2
   384
eos:
leo_sobral@2
   385
  src->eos = TRUE;
rosfran@61
   386
leo_sobral@2
   387
done:
leo_sobral@2
   388
  return read;
leo_sobral@2
   389
}
leo_sobral@2
   390
rosfran@30
   391
static GstFlowReturn
rosfran@81
   392
gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf )
rosfran@81
   393
{
rosfran@81
   394
  GstMythtvSrc *src;
rosfran@81
   395
  GstFlowReturn ret = GST_FLOW_OK;
rosfran@81
   396
  gint read = -1;
rosfran@90
   397
rosfran@81
   398
  src = GST_MYTHTV_SRC ( psrc );
rosfran@90
   399
rosfran@81
   400
  /* The caller should know the number of bytes and not read beyond EOS. */
rosfran@81
   401
  if (G_UNLIKELY (src->eos))
rosfran@81
   402
    goto eos;
rosfran@81
   403
  if ( G_UNLIKELY (src->update_prog_chain) )
rosfran@81
   404
    goto change_progchain;
rosfran@95
   405
rosfran@90
   406
  //g_static_rec_mutex_lock( &th_mutex );
rosfran@106
   407
  GST_DEBUG_OBJECT( src, "[%s] offset = %llu, size = %d...\n", __FUNCTION__, 
rosfran@100
   408
  		src->read_offset, MAX_READ_SIZE );
rosfran@95
   409
rosfran@118
   410
  GST_DEBUG_OBJECT ( src, "[%s]\t\tCreate: buffer_remain: %d, buffer_size = %d.\n", __FUNCTION__, 
rosfran@118
   411
      (gint) src->buffer_remain, src->bytes_queue->len );
rosfran@95
   412
rosfran@100
   413
  /* just get from the byte array, no network effort... */
rosfran@100
   414
  if ( ( src->buffer_remain = src->bytes_queue->len ) < MAX_READ_SIZE ) {
rosfran@118
   415
  //if ( src->buffer_remain  ) {
rosfran@102
   416
  	guint buffer_size_inter = INTERNAL_BUFFER_SIZE - src->buffer_remain;
rosfran@102
   417
  	GByteArray *buffer = g_byte_array_new();
rosfran@95
   418
rosfran@102
   419
    read = do_read_request_response( src, buffer_size_inter, buffer );
rosfran@81
   420
rosfran@95
   421
    if (G_UNLIKELY (read < 0)) {
rosfran@90
   422
      if ( src->live_tv )
rosfran@90
   423
	goto change_progchain;
rosfran@90
   424
      else
rosfran@95
   425
	goto read_error;
rosfran@90
   426
    }
rosfran@90
   427
rosfran@95
   428
    if ( G_UNLIKELY (src->update_prog_chain) )
rosfran@95
   429
      goto change_progchain;
rosfran@100
   430
      
rosfran@102
   431
    src->bytes_queue = g_byte_array_append( src->bytes_queue, g_memdup( buffer->data, read ), read );
rosfran@106
   432
    if ( read > buffer_size_inter )
rosfran@106
   433
    	GST_WARNING_OBJECT( src, "[%s] INCREASED buffer size! Backend sent more than we ask him... (%d)\n", 
rosfran@106
   434
    			__FUNCTION__, abs( read - buffer_size_inter ) );
rosfran@90
   435
rosfran@95
   436
    src->buffer_remain = src->buffer_remain + read;
rosfran@102
   437
    
rosfran@102
   438
    if ( buffer != NULL ) {
rosfran@102
   439
    	g_byte_array_free( buffer, TRUE );
rosfran@102
   440
    	buffer = NULL;    	
rosfran@102
   441
    }
rosfran@90
   442
rosfran@106
   443
    GST_DEBUG_OBJECT( src, "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
rosfran@95
   444
	"OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
rosfran@95
   445
	src->read_offset, src->content_size );
rosfran@100
   446
	
rosfran@81
   447
  }
rosfran@98
   448
  
rosfran@100
   449
  guint buffer_size = ( src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
rosfran@100
   450
  
rosfran@100
   451
  /* Create the buffer. */
rosfran@100
   452
  ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
rosfran@100
   453
      src->read_offset, buffer_size,
rosfran@100
   454
      GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );    
rosfran@81
   455
rosfran@100
   456
  if (G_UNLIKELY (ret != GST_FLOW_OK)) {
rosfran@100
   457
    if ( src->live_tv )
rosfran@100
   458
      goto change_progchain;
rosfran@100
   459
    else
rosfran@100
   460
      goto done;
rosfran@100
   461
  }
rosfran@100
   462
  
rosfran@100
   463
  /* gets the first buffer_size bytes from the byte array buffer variable */ 
rosfran@100
   464
  guint8 *buf = g_memdup( src->bytes_queue->data, buffer_size );
rosfran@95
   465
rosfran@106
   466
  GST_DEBUG_OBJECT( src, "[%s] read from network? %s!, buffer_remain = %d\n", __FUNCTION__, 
rosfran@100
   467
  	read == -1 ? "NO, got from buffer" : "YES, go see the backend's log file", src->buffer_remain );
rosfran@95
   468
rosfran@100
   469
  GST_BUFFER_SIZE (*outbuf) = buffer_size;
rosfran@95
   470
  GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
rosfran@95
   471
  GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
rosfran@100
   472
  g_memmove( GST_BUFFER_DATA( (*outbuf) ), buf, GST_BUFFER_SIZE(*outbuf) );
rosfran@95
   473
  GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
rosfran@95
   474
  GST_BUFFER_OFFSET_END (*outbuf) = src->read_offset + GST_BUFFER_SIZE (*outbuf);
rosfran@95
   475
rosfran@95
   476
  src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
rosfran@95
   477
rosfran@95
   478
  src->read_offset += GST_BUFFER_SIZE (*outbuf);
rosfran@95
   479
  src->bytes_read += GST_BUFFER_SIZE (*outbuf);
rosfran@106
   480
  GST_DEBUG_OBJECT( src, "[%s]\t\tBuffer output with size: %d\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf) );
rosfran@100
   481
  
rosfran@100
   482
  /* flushs the newly buffer got from byte array */
rosfran@100
   483
  src->bytes_queue = g_byte_array_remove_range( src->bytes_queue, 0, buffer_size );
rosfran@95
   484
rosfran@106
   485
  GST_DEBUG_OBJECT( src, "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
rosfran@95
   486
      "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
rosfran@95
   487
      GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
rosfran@95
   488
rosfran@90
   489
  return ret;
rosfran@90
   490
rosfran@81
   491
done:
rosfran@90
   492
  {
rosfran@81
   493
    const gchar *reason = gst_flow_get_name (ret);
rosfran@81
   494
rosfran@81
   495
    GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
rosfran@90
   496
    return ret;
rosfran@90
   497
  }
rosfran@81
   498
eos:
rosfran@81
   499
  {
rosfran@81
   500
    const gchar *reason = gst_flow_get_name (ret);
rosfran@81
   501
rosfran@81
   502
    GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
rosfran@81
   503
    return GST_FLOW_UNEXPECTED;
rosfran@81
   504
  }
rosfran@81
   505
  /* ERRORS */
rosfran@81
   506
read_error:
rosfran@81
   507
  {
rosfran@81
   508
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@81
   509
	(NULL), ("Could not read any bytes (%i, %s)", read,
rosfran@90
   510
		 src->uri_name));
rosfran@81
   511
    return GST_FLOW_ERROR;
rosfran@81
   512
  }
rosfran@81
   513
change_progchain:
rosfran@81
   514
  {
rosfran@81
   515
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@90
   516
	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
rosfran@90
   517
		 src->uri_name));
rosfran@90
   518
rosfran@90
   519
    gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
rosfran@90
   520
	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
rosfran@90
   521
    // go to the next program chain
rosfran@90
   522
    src->unique_setup = FALSE;
rosfran@90
   523
    src->update_prog_chain = TRUE;
rosfran@90
   524
rosfran@90
   525
    gst_mythtv_src_next_program_chain( src );
rosfran@90
   526
rosfran@70
   527
    return GST_FLOW_ERROR_NO_DATA;
rosfran@37
   528
  }
rosfran@30
   529
leo_sobral@2
   530
}
rosfran@98
   531
rosfran@52
   532
gint64
rosfran@30
   533
gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
leo_sobral@2
   534
{
rosfran@30
   535
rosfran@52
   536
  gint64 size_tmp = 0;
rosfran@71
   537
  guint max_tries = 2;
rosfran@71
   538
  if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) < 
rosfran@71
   539
		GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
rosfran@71
   540
rosfran@30
   541
get_file_pos:
rosfran@61
   542
    g_usleep( 10 );
rosfran@114
   543
    size_tmp = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
rosfran@52
   544
    if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@61
   545
      src->content_size = size_tmp;
rosfran@61
   546
    else if ( size_tmp > 0 && --max_tries > 0 )
rosfran@61
   547
      goto get_file_pos;
rosfran@106
   548
    GST_LOG_OBJECT( src, "\t[%s]\tGET_POSITION: file_position = %lld\n",
rosfran@61
   549
	__FUNCTION__, size_tmp );
rosfran@71
   550
    /* sets the last content size amount before it can be updated */
rosfran@71
   551
    src->prev_content_size = src->content_size;
rosfran@30
   552
  }
rosfran@52
   553
rosfran@30
   554
  return src->content_size;	
rosfran@30
   555
leo_sobral@2
   556
}
leo_sobral@2
   557
rosfran@87
   558
static gboolean
rosfran@87
   559
gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment )
rosfran@87
   560
{
rosfran@87
   561
  GstMythtvSrc *src = GST_MYTHTV_SRC( base );
rosfran@90
   562
  gint64 new_offset = -1;
rosfran@90
   563
  gint64 actual_seek = segment->start;
rosfran@87
   564
  gboolean ret = TRUE;
rosfran@87
   565
rosfran@106
   566
  GST_LOG_OBJECT( src, "[%s]DO Seek called! (start = %lld, stop = %lld)\n", __FUNCTION__, 
rosfran@106
   567
  		segment->start, segment->stop );
rosfran@90
   568
rosfran@90
   569
  if ( segment->format == GST_FORMAT_TIME ) 
rosfran@90
   570
  {
rosfran@90
   571
    goto done; 
rosfran@90
   572
    //actual_seek = ( ( segment->start / 1000 ) * 28 ) * 4000;
rosfran@90
   573
  }
rosfran@106
   574
  GST_LOG_OBJECT( src, "[%s]Trying to seek at the value (actual_seek = %lld, read_offset = %lld)\n", __FUNCTION__, actual_seek, src->read_offset );
rosfran@87
   575
  /* verify if it needs to seek */
rosfran@90
   576
  if ( src->read_offset != actual_seek )
rosfran@87
   577
  {
rosfran@87
   578
rosfran@87
   579
    new_offset = gmyth_file_transfer_seek( src->file_transfer, segment->start, SEEK_SET );
rosfran@87
   580
rosfran@106
   581
    GST_LOG_OBJECT( src, "[%s] Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
rosfran@106
   582
			__FUNCTION__, segment->start, src->read_offset, new_offset );
rosfran@87
   583
    if ( G_UNLIKELY (new_offset < 0 ) )
rosfran@87
   584
    {
rosfran@87
   585
      ret = FALSE;
rosfran@87
   586
      if ( src->live_tv )
rosfran@87
   587
	goto change_progchain;
rosfran@87
   588
      else
rosfran@87
   589
	goto eos;
rosfran@87
   590
    }
rosfran@87
   591
rosfran@87
   592
    src->read_offset = new_offset;
rosfran@87
   593
rosfran@90
   594
    if ( ret == FALSE ) {
rosfran@106
   595
      GST_INFO_OBJECT( src, "[%s] Failed to set the SEEK on segment!\n", __FUNCTION__ );
rosfran@90
   596
    }
rosfran@90
   597
rosfran@87
   598
  }
rosfran@90
   599
rosfran@90
   600
done:
rosfran@87
   601
   return ret;
rosfran@87
   602
rosfran@87
   603
eos:
rosfran@87
   604
  {
rosfran@87
   605
rosfran@87
   606
    GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
rosfran@87
   607
    return FALSE;
rosfran@87
   608
  }
rosfran@87
   609
change_progchain:
rosfran@87
   610
  {
rosfran@87
   611
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@87
   612
	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
rosfran@87
   613
		 src->uri_name));
rosfran@87
   614
rosfran@87
   615
    gst_pad_push_event ( GST_BASE_SRC_PAD (base),
rosfran@87
   616
	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
rosfran@87
   617
    /* go to the next program chain */
rosfran@87
   618
    src->unique_setup = FALSE;
rosfran@87
   619
    src->update_prog_chain = TRUE;
rosfran@87
   620
rosfran@87
   621
    gst_mythtv_src_next_program_chain( src );
rosfran@87
   622
rosfran@87
   623
    return TRUE;
rosfran@87
   624
  }
rosfran@87
   625
rosfran@87
   626
}
rosfran@87
   627
leo_sobral@2
   628
/* create a socket for connecting to remote server */
rosfran@30
   629
static gboolean
leo_sobral@2
   630
gst_mythtv_src_start ( GstBaseSrc * bsrc )
leo_sobral@2
   631
{
leo_sobral@2
   632
  GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
leo_sobral@2
   633
leo_sobral@2
   634
  GString *chain_id_local = NULL;
leo_sobral@2
   635
leo_sobral@2
   636
  gboolean ret = TRUE;
rosfran@90
   637
rosfran@71
   638
  if ( G_UNLIKELY (src->update_prog_chain) )
rosfran@71
   639
    goto change_progchain;
rosfran@30
   640
leo_sobral@2
   641
  if (src->unique_setup == FALSE) {
leo_sobral@2
   642
    src->unique_setup = TRUE;
leo_sobral@2
   643
  } else {
leo_sobral@2
   644
    goto done;
leo_sobral@2
   645
  }
leo_sobral@2
   646
leo_sobral@2
   647
  if ( src->live_tv ) {
rosfran@118
   648
  	src->backend_info = gmyth_backend_info_new_with_uri( src->uri_name );
rosfran@40
   649
    src->spawn_livetv = gmyth_livetv_new( );
rosfran@118
   650
    if ( gmyth_livetv_setup( src->spawn_livetv, src->backend_info ) == FALSE ) {
rosfran@30
   651
      ret = FALSE;
rosfran@30
   652
      goto init_failed;
rosfran@70
   653
    }    
rosfran@52
   654
leo_sobral@2
   655
    /* set up the uri variable */
leo_sobral@2
   656
    src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
leo_sobral@2
   657
    chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
leo_sobral@2
   658
    if ( chain_id_local != NULL ) {
leo_sobral@2
   659
      src->live_chain_id = g_strdup( chain_id_local->str );
rosfran@114
   660
      GST_INFO_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );      
leo_sobral@2
   661
    }
rosfran@118
   662
    //if ( src->uri_name == NULL || strlen( src->uri_name ) <= 0 )
rosfran@118
   663
    //	gmyth_context_initialize();
rosfran@114
   664
    	
rosfran@70
   665
    src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
rosfran@106
   666
    GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name ); 
leo_sobral@2
   667
  }
leo_sobral@2
   668
rosfran@114
   669
  //src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
rosfran@114
   670
  //    g_string_new( src->uri_name ), -1, src->mythtv_version );
rosfran@114
   671
	src->file_transfer = gmyth_file_transfer_new( );
leo_sobral@2
   672
leo_sobral@2
   673
  if ( src->file_transfer == NULL ) {
leo_sobral@2
   674
    goto init_failed;
leo_sobral@2
   675
  }
leo_sobral@2
   676
rosfran@114
   677
	ret = gmyth_file_transfer_open( src->file_transfer, g_string_new( src->uri_name ) );
rosfran@114
   678
leo_sobral@2
   679
  /* sets the Playback monitor connection */
rosfran@114
   680
  //ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
leo_sobral@2
   681
leo_sobral@2
   682
  if ( src->live_tv == TRUE && ret == TRUE ) {
leo_sobral@2
   683
    /* loop finished, set the max tries variable to zero again... */
leo_sobral@2
   684
    wait_to_transfer = 0;
leo_sobral@2
   685
rosfran@70
   686
    while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
rosfran@108
   687
				( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
rosfran@110
   688
      g_usleep( 500 );
rosfran@110
   689
      
rosfran@110
   690
    /* IS_RECORDING again, just like the MythTV backend does... */
rosfran@110
   691
	  gmyth_livetv_is_recording( src->spawn_livetv );
rosfran@110
   692
	  
rosfran@114
   693
	  sleep( 4 );  
rosfran@110
   694
leo_sobral@2
   695
  }
leo_sobral@2
   696
  /* sets the FileTransfer instance connection (video/audio download) */
rosfran@114
   697
  //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
leo_sobral@2
   698
leo_sobral@2
   699
  if ( ret == FALSE ) {
leo_sobral@2
   700
#ifndef GST_DISABLE_GST_DEBUG  
leo_sobral@2
   701
    if ( src->mythtv_msgs_dbg )
rosfran@106
   702
      GST_INFO_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
leo_sobral@2
   703
#endif
leo_sobral@2
   704
    goto begin_req_failed;
leo_sobral@2
   705
  }
leo_sobral@2
   706
leo_sobral@2
   707
  src->content_size = src->file_transfer->filesize;
leo_sobral@2
   708
rosfran@71
   709
  src->do_start = FALSE;
rosfran@114
   710
  
rosfran@114
   711
  /* this is used for the buffer cache */
rosfran@100
   712
  src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
rosfran@90
   713
  src->buffer_remain = 0;
rosfran@114
   714
  
rosfran@114
   715
  //gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
rosfran@114
   716
	//		gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
rosfran@90
   717
rosfran@30
   718
done:
rosfran@30
   719
  return TRUE;
rosfran@30
   720
rosfran@30
   721
  /* ERRORS */
rosfran@30
   722
init_failed:
rosfran@30
   723
  {
rosfran@30
   724
    if (src->spawn_livetv != NULL )
rosfran@30
   725
      g_object_unref( src->spawn_livetv );
rosfran@30
   726
rosfran@30
   727
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@30
   728
	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
rosfran@30
   729
    return FALSE;
rosfran@30
   730
  }
rosfran@30
   731
begin_req_failed:
rosfran@30
   732
  {
rosfran@30
   733
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@30
   734
	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
rosfran@30
   735
    return FALSE;
rosfran@30
   736
  }
rosfran@71
   737
change_progchain:
rosfran@71
   738
  {
rosfran@71
   739
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@90
   740
	(NULL), ("Seek failed, go to the next program info... (%s)",
rosfran@90
   741
		 src->uri_name));
rosfran@90
   742
rosfran@90
   743
    gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
rosfran@90
   744
	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
rosfran@90
   745
rosfran@90
   746
    // go to the next program chain
rosfran@90
   747
    src->unique_setup = FALSE;
rosfran@90
   748
    src->update_prog_chain = TRUE;
rosfran@90
   749
rosfran@90
   750
    gst_mythtv_src_next_program_chain( src );
rosfran@90
   751
rosfran@71
   752
    return TRUE;
rosfran@71
   753
  }
rosfran@30
   754
}
leo_sobral@2
   755
rosfran@52
   756
/* create a new socket for connecting to the next program chain */
rosfran@52
   757
static gboolean
rosfran@52
   758
gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
rosfran@52
   759
{
rosfran@52
   760
  GString *chain_id_local = NULL;
rosfran@52
   761
rosfran@52
   762
  gboolean ret = TRUE;
rosfran@61
   763
rosfran@52
   764
  if ( !src->live_tv )
rosfran@61
   765
    goto init_failed;
rosfran@70
   766
    
rosfran@70
   767
  if (src->unique_setup == FALSE) {
rosfran@70
   768
    src->unique_setup = TRUE;
rosfran@70
   769
  } else {
rosfran@70
   770
    goto done;
rosfran@70
   771
  }
rosfran@81
   772
  
rosfran@81
   773
	GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
rosfran@61
   774
rosfran@52
   775
  if (src->file_transfer) {
rosfran@52
   776
    g_object_unref (src->file_transfer);
rosfran@52
   777
    src->file_transfer = NULL;
rosfran@52
   778
  }
rosfran@61
   779
rosfran@52
   780
  if (src->uri_name) {
rosfran@52
   781
    g_free (src->uri_name);
rosfran@52
   782
  }
rosfran@61
   783
rosfran@52
   784
  if ( src->live_tv ) {
rosfran@61
   785
    if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
rosfran@106
   786
    	GST_INFO_OBJECT( src, "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
rosfran@61
   787
      ret = FALSE;
rosfran@61
   788
      goto init_failed;
rosfran@52
   789
    }
rosfran@52
   790
    /* set up the uri variable */
rosfran@52
   791
    src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
rosfran@52
   792
    chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
rosfran@52
   793
    if ( chain_id_local != NULL ) {
rosfran@52
   794
      src->live_chain_id = g_strdup( chain_id_local->str );
rosfran@106
   795
      GST_DEBUG_OBJECT( src, "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
rosfran@52
   796
    }
rosfran@70
   797
    src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
rosfran@106
   798
    GST_LOG_OBJECT( src, "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
rosfran@52
   799
  }
rosfran@52
   800
rosfran@114
   801
  src->file_transfer = gmyth_file_transfer_new();
rosfran@52
   802
rosfran@52
   803
  if ( src->file_transfer == NULL ) {
rosfran@52
   804
    goto init_failed;
rosfran@52
   805
  }
rosfran@114
   806
  
rosfran@114
   807
	ret = gmyth_file_transfer_open( src->file_transfer, g_string_new( src->uri_name ) );
rosfran@52
   808
rosfran@52
   809
  /* sets the Playback monitor connection */
rosfran@114
   810
  //ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
rosfran@52
   811
rosfran@52
   812
  if ( src->live_tv == TRUE && ret == TRUE ) {
rosfran@52
   813
    /* loop finished, set the max tries variable to zero again... */
rosfran@52
   814
    wait_to_transfer = 0;
rosfran@61
   815
rosfran@61
   816
    g_usleep( 200 );
rosfran@52
   817
rosfran@52
   818
    while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && 
rosfran@108
   819
				( gmyth_livetv_is_recording( src->spawn_livetv ) == FALSE ) )
rosfran@61
   820
      g_usleep( 1000 );
rosfran@52
   821
  }
rosfran@52
   822
rosfran@52
   823
  /* sets the FileTransfer instance connection (video/audio download) */
rosfran@114
   824
  //ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
rosfran@52
   825
rosfran@52
   826
  if ( ret == FALSE ) {
rosfran@52
   827
#ifndef GST_DISABLE_GST_DEBUG  
rosfran@52
   828
    if ( src->mythtv_msgs_dbg )
rosfran@106
   829
      GST_ERROR_OBJECT( src, "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
rosfran@52
   830
#endif
rosfran@52
   831
    goto begin_req_failed;
rosfran@52
   832
  }
rosfran@61
   833
  src->content_size_last = src->content_size;
rosfran@61
   834
rosfran@61
   835
  src->content_size = src->file_transfer->filesize;
rosfran@87
   836
  if ( src->live_tv ) {
rosfran@87
   837
  	wait_to_transfer = 0;
rosfran@87
   838
	  while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
rosfran@87
   839
	    src->content_size = gst_mythtv_src_get_position( src );
rosfran@87
   840
  }
rosfran@63
   841
rosfran@81
   842
  src->read_offset = 0;  
rosfran@70
   843
  
rosfran@100
   844
  if ( src->bytes_queue != NULL ) {
rosfran@100
   845
  	g_byte_array_free( src->bytes_queue, TRUE );
rosfran@100
   846
  }
rosfran@100
   847
  
rosfran@100
   848
  src->bytes_queue = g_byte_array_sized_new( INTERNAL_BUFFER_SIZE );
rosfran@100
   849
  
rosfran@114
   850
  //gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
rosfran@114
   851
	//		gst_event_new_new_segment ( TRUE, 1.0, GST_FORMAT_TIME, 0, src->content_size, 0 ) );
rosfran@114
   852
  
rosfran@70
   853
done:
rosfran@81
   854
	src->update_prog_chain = FALSE;
rosfran@81
   855
	
rosfran@81
   856
	GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
rosfran@81
   857
	
rosfran@52
   858
  return TRUE;
rosfran@52
   859
rosfran@52
   860
  /* ERRORS */
rosfran@52
   861
init_failed:
rosfran@52
   862
  {
rosfran@52
   863
    if (src->spawn_livetv != NULL )
rosfran@52
   864
      g_object_unref( src->spawn_livetv );
rosfran@52
   865
rosfran@52
   866
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@52
   867
	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
rosfran@52
   868
    return FALSE;
rosfran@52
   869
  }
rosfran@52
   870
begin_req_failed:
rosfran@52
   871
  {
rosfran@52
   872
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@52
   873
	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
rosfran@52
   874
    return FALSE;
rosfran@52
   875
  }
rosfran@61
   876
rosfran@52
   877
}
rosfran@52
   878
rosfran@30
   879
static gboolean
leo_sobral@2
   880
gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
leo_sobral@2
   881
{
rosfran@31
   882
  GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
rosfran@30
   883
  gboolean ret = TRUE;
rosfran@106
   884
  GST_LOG_OBJECT( src, "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__, 
rosfran@63
   885
  			abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
rosfran@98
   886
  			
rosfran@100
   887
  if ( src->live_tv ) {
rosfran@98
   888
  	ret = FALSE;
rosfran@100
   889
	} else if ( src->live_tv && src->enable_timing_position && ( abs( src->content_size - src->bytes_read ) < 
rosfran@70
   890
				GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
rosfran@70
   891
    
rosfran@114
   892
    gint64 new_offset = gmyth_recorder_get_file_position( src->spawn_livetv->recorder );
rosfran@70
   893
    if ( new_offset > 0 && new_offset > src->content_size ) {
rosfran@70
   894
			src->content_size = new_offset;
rosfran@70
   895
    } else if ( new_offset < src->content_size ) {
rosfran@70
   896
			src->update_prog_chain = TRUE;
rosfran@70
   897
	  }
rosfran@61
   898
leo_sobral@2
   899
  }
leo_sobral@2
   900
leo_sobral@2
   901
  *size = src->content_size;
rosfran@106
   902
  GST_LOG_OBJECT( src, "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
rosfran@87
   903
  
rosfran@30
   904
  return ret;
rosfran@30
   905
leo_sobral@2
   906
}
leo_sobral@2
   907
leo_sobral@2
   908
/* close the socket and associated resources
leo_sobral@2
   909
 * used both to recover from errors and go to NULL state */
rosfran@52
   910
static gboolean
leo_sobral@2
   911
gst_mythtv_src_stop (GstBaseSrc * bsrc)
leo_sobral@2
   912
{
leo_sobral@2
   913
  GstMythtvSrc *src;
leo_sobral@2
   914
leo_sobral@2
   915
  src = GST_MYTHTV_SRC (bsrc);
leo_sobral@2
   916
leo_sobral@2
   917
  if (src->uri_name) {
leo_sobral@2
   918
    g_free (src->uri_name);
leo_sobral@2
   919
    src->uri_name = NULL;
leo_sobral@2
   920
  }
leo_sobral@2
   921
leo_sobral@2
   922
  if (src->mythtv_caps) {
leo_sobral@2
   923
    gst_caps_unref (src->mythtv_caps);
leo_sobral@2
   924
    src->mythtv_caps = NULL;
leo_sobral@2
   925
  }
leo_sobral@2
   926
leo_sobral@2
   927
  src->eos = FALSE;
leo_sobral@2
   928
leo_sobral@2
   929
  return TRUE;
leo_sobral@2
   930
}
leo_sobral@2
   931
rosfran@30
   932
static gboolean
leo_sobral@2
   933
gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
leo_sobral@2
   934
{
leo_sobral@2
   935
  GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
rosfran@52
   936
  gint64 cont_size = 0;
rosfran@90
   937
  gboolean ret = FALSE;
leo_sobral@2
   938
leo_sobral@2
   939
  switch (GST_EVENT_TYPE (event)) {
rosfran@30
   940
    case GST_EVENT_EOS:
rosfran@106
   941
      GST_WARNING_OBJECT( src, "[%s] Got EOS event!!!\n", __FUNCTION__ );
rosfran@61
   942
rosfran@61
   943
      if ( src->live_tv ) {
rosfran@61
   944
	cont_size = gst_mythtv_src_get_position (src);
rosfran@61
   945
	if ( cont_size > src->content_size ) {
rosfran@61
   946
	  src->content_size = cont_size;
rosfran@61
   947
	  src->eos = FALSE;
rosfran@61
   948
	} else {
rosfran@61
   949
	  src->eos = TRUE;
rosfran@61
   950
	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
rosfran@61
   951
	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
rosfran@61
   952
	}
rosfran@114
   953
      }
rosfran@30
   954
      break;
leo_sobral@2
   955
    default:
rosfran@90
   956
      ret = gst_pad_event_default (pad, event);
leo_sobral@2
   957
  }
leo_sobral@2
   958
rosfran@90
   959
  return ret;
leo_sobral@2
   960
}
leo_sobral@2
   961
rosfran@30
   962
static gboolean
rosfran@30
   963
gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
leo_sobral@2
   964
{
rosfran@100
   965
  return TRUE;
leo_sobral@2
   966
}
rosfran@87
   967
rosfran@87
   968
static gboolean
rosfran@87
   969
gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
rosfran@87
   970
{
rosfran@87
   971
  gboolean res = FALSE;
rosfran@87
   972
  GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
rosfran@114
   973
  GstFormat formt;
rosfran@87
   974
rosfran@87
   975
  switch (GST_QUERY_TYPE (query)) {
rosfran@87
   976
    case GST_QUERY_POSITION:
rosfran@114
   977
    {
rosfran@114
   978
    	gint64 pos = -1;
rosfran@114
   979
      gst_query_parse_position (query, &formt, &pos );
rosfran@87
   980
      res = TRUE;
rosfran@114
   981
      if ( formt == GST_FORMAT_BYTES )
rosfran@114
   982
      	GST_DEBUG_OBJECT (myth, "POS %lld (BYTES).\n", pos );
rosfran@114
   983
      else if ( formt == GST_FORMAT_TIME )
rosfran@114
   984
      	GST_DEBUG_OBJECT (myth, "POS %lld (TIME).\n", pos );
rosfran@87
   985
      break;
rosfran@114
   986
    }
rosfran@87
   987
    case GST_QUERY_DURATION:
rosfran@114
   988
    {
rosfran@87
   989
#if 0
rosfran@87
   990
      if (myth->duration != 0) {
rosfran@87
   991
	gint64 total;
rosfran@87
   992
	gint64 fps;
rosfran@87
   993
rosfran@87
   994
	fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
rosfran@87
   995
	total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
rosfran@114
   996
      }
rosfran@81
   997
#endif
rosfran@114
   998
	gint64 dur = -1;
rosfran@114
   999
	gst_query_parse_duration( query, &formt, &dur );
rosfran@87
  1000
	//gst_query_set_duration (query, GST_FORMAT_TIME, myth->content_size);
rosfran@114
  1001
  if ( formt == GST_FORMAT_BYTES )
rosfran@114
  1002
  	GST_DEBUG_OBJECT (myth, "DURATION %lld (BYTES).\n", dur );
rosfran@114
  1003
  else if ( formt == GST_FORMAT_TIME )
rosfran@114
  1004
  	GST_DEBUG_OBJECT (myth, "DURATION %lld (TIME).\n", dur );
rosfran@114
  1005
	res = TRUE;
rosfran@70
  1006
      break;
rosfran@114
  1007
    }
rosfran@70
  1008
    default:
rosfran@114
  1009
    {
rosfran@87
  1010
      res = FALSE;
rosfran@87
  1011
      break;
rosfran@114
  1012
    }
rosfran@70
  1013
  }
rosfran@61
  1014
rosfran@87
  1015
  gst_object_unref (myth);
rosfran@61
  1016
rosfran@70
  1017
  return res;
rosfran@61
  1018
}
rosfran@61
  1019
rosfran@52
  1020
static GstStateChangeReturn
rosfran@52
  1021
gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
rosfran@52
  1022
{
rosfran@87
  1023
  GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
rosfran@52
  1024
  GstMythtvSrc *src = GST_MYTHTV_SRC (element);
rosfran@52
  1025
rosfran@52
  1026
  switch (transition) {
rosfran@52
  1027
    case GST_STATE_CHANGE_NULL_TO_READY:
rosfran@87
  1028
      //src->do_start = TRUE;
rosfran@87
  1029
      //src->unique_setup = FALSE;
rosfran@52
  1030
      break;
rosfran@52
  1031
    case GST_STATE_CHANGE_READY_TO_PAUSED:
rosfran@52
  1032
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
rosfran@87
  1033
      //src->eos = FALSE;
rosfran@52
  1034
      break;
rosfran@52
  1035
    default:
rosfran@52
  1036
      break;
rosfran@52
  1037
  }
rosfran@61
  1038
rosfran@61
  1039
  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
rosfran@52
  1040
  if (ret == GST_STATE_CHANGE_FAILURE)
rosfran@52
  1041
    return ret;
rosfran@52
  1042
rosfran@52
  1043
  switch (transition) {
rosfran@52
  1044
    case GST_STATE_CHANGE_READY_TO_NULL:
rosfran@106
  1045
      GST_INFO_OBJECT( src, "[%s] READY to NULL called!\n", __FUNCTION__ );
rosfran@52
  1046
      break;
rosfran@61
  1047
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
rosfran@106
  1048
      GST_INFO_OBJECT( src, "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
rosfran@52
  1049
    case GST_STATE_CHANGE_PAUSED_TO_READY:
rosfran@106
  1050
      GST_INFO_OBJECT( src, "[%s] PAUSED to READY called!\n", __FUNCTION__ );
rosfran@61
  1051
      if ( src->live_tv && src->update_prog_chain ) {
rosfran@110
  1052
	      if ( !gmyth_recorder_send_frontend_ready_command( src->spawn_livetv->recorder ) )
rosfran@110
  1053
	      	GST_WARNING_OBJECT( src, "[%s] Couldn't send the FRONTEND_READY message to the backend!", __FUNCTION__ );
rosfran@110
  1054
	      else
rosfran@110
  1055
	      	GST_DEBUG_OBJECT( src, "[%s] Message FRONTEND_READY was sent to the backend!", __FUNCTION__ );
rosfran@81
  1056
      	
rosfran@98
  1057
      /*
rosfran@98
  1058
      	
rosfran@81
  1059
  			gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
rosfran@81
  1060
      			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
rosfran@81
  1061
rosfran@70
  1062
				src->read_offset = 0;
rosfran@70
  1063
				src->bytes_read = 0;
rosfran@81
  1064
				src->unique_setup = FALSE;				
rosfran@70
  1065
				gst_mythtv_src_next_program_chain( src );
rosfran@98
  1066
				*/
rosfran@61
  1067
      }
rosfran@98
  1068
      
rosfran@61
  1069
      break;
rosfran@52
  1070
    default:
rosfran@52
  1071
      break;
rosfran@52
  1072
  }
rosfran@52
  1073
rosfran@52
  1074
  return ret;
rosfran@52
  1075
}
rosfran@52
  1076
rosfran@30
  1077
static void
leo_sobral@2
  1078
gst_mythtv_src_set_property (GObject * object, guint prop_id,
leo_sobral@2
  1079
    const GValue * value, GParamSpec * pspec)
leo_sobral@2
  1080
{
leo_sobral@2
  1081
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
  1082
leo_sobral@2
  1083
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
  1084
  switch (prop_id) {
leo_sobral@2
  1085
    case PROP_URI:
leo_sobral@2
  1086
    case PROP_LOCATION:
leo_sobral@2
  1087
      {
leo_sobral@2
  1088
	if (!g_value_get_string (value)) {
leo_sobral@2
  1089
	  GST_WARNING ("location property cannot be NULL");
leo_sobral@2
  1090
	  goto done;
leo_sobral@2
  1091
	}
leo_sobral@2
  1092
leo_sobral@2
  1093
	if (mythtvsrc->uri_name != NULL) {
leo_sobral@2
  1094
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
  1095
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
  1096
	}
leo_sobral@2
  1097
	mythtvsrc->uri_name = g_value_dup_string (value);
leo_sobral@2
  1098
leo_sobral@2
  1099
	break;
leo_sobral@2
  1100
      }
leo_sobral@2
  1101
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
  1102
    case PROP_GMYTHTV_DBG:
leo_sobral@2
  1103
      {
leo_sobral@2
  1104
	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
leo_sobral@2
  1105
	break;
leo_sobral@2
  1106
      }
leo_sobral@2
  1107
#endif
rosfran@40
  1108
    case PROP_GMYTHTV_VERSION:
leo_sobral@2
  1109
      {
leo_sobral@2
  1110
	mythtvsrc->mythtv_version = g_value_get_int (value);
leo_sobral@2
  1111
	break;
leo_sobral@2
  1112
      }
rosfran@40
  1113
    case PROP_GMYTHTV_LIVEID:
leo_sobral@2
  1114
      {
leo_sobral@2
  1115
	mythtvsrc->live_tv_id = g_value_get_int (value);
leo_sobral@2
  1116
	break;
leo_sobral@2
  1117
      }
rosfran@40
  1118
    case PROP_GMYTHTV_LIVE:
leo_sobral@2
  1119
      {
leo_sobral@2
  1120
	mythtvsrc->live_tv = g_value_get_boolean (value);
leo_sobral@2
  1121
	break;
leo_sobral@2
  1122
      }
rosfran@52
  1123
    case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
rosfran@52
  1124
      {
rosfran@52
  1125
	mythtvsrc->enable_timing_position = g_value_get_boolean (value);
rosfran@52
  1126
	break;
rosfran@52
  1127
      }      
rosfran@40
  1128
    case PROP_GMYTHTV_LIVE_CHAINID:
leo_sobral@2
  1129
      {
leo_sobral@2
  1130
	if (!g_value_get_string (value)) {
leo_sobral@2
  1131
	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
leo_sobral@2
  1132
	  goto done;
leo_sobral@2
  1133
	}
leo_sobral@2
  1134
leo_sobral@2
  1135
	if (mythtvsrc->live_chain_id != NULL) {
leo_sobral@2
  1136
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
  1137
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
  1138
	}
leo_sobral@2
  1139
	mythtvsrc->live_chain_id = g_value_dup_string (value);
leo_sobral@2
  1140
	break;
leo_sobral@2
  1141
      }
rosfran@70
  1142
    case PROP_GMYTHTV_CHANNEL_NUM:
rosfran@70
  1143
      {
rosfran@70
  1144
	mythtvsrc->channel_num = g_value_get_int (value);
rosfran@70
  1145
	break;
rosfran@70
  1146
      }
leo_sobral@2
  1147
    default:
leo_sobral@2
  1148
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
  1149
      break;
leo_sobral@2
  1150
  }
leo_sobral@2
  1151
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
  1152
done:
leo_sobral@2
  1153
  return;
leo_sobral@2
  1154
}
leo_sobral@2
  1155
rosfran@100
  1156
static void
leo_sobral@2
  1157
gst_mythtv_src_get_property (GObject * object, guint prop_id,
leo_sobral@2
  1158
    GValue * value, GParamSpec * pspec)
leo_sobral@2
  1159
{
leo_sobral@2
  1160
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
  1161
leo_sobral@2
  1162
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
  1163
  switch (prop_id) {
leo_sobral@2
  1164
    case PROP_URI:
leo_sobral@2
  1165
    case PROP_LOCATION:
leo_sobral@2
  1166
      {
leo_sobral@2
  1167
	gchar *str = g_strdup( "" );
leo_sobral@2
  1168
leo_sobral@2
  1169
	if ( mythtvsrc->uri_name == NULL ) {
leo_sobral@2
  1170
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
  1171
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
  1172
	} else {
leo_sobral@2
  1173
	  str = g_strdup( mythtvsrc->uri_name );
leo_sobral@2
  1174
	}
leo_sobral@2
  1175
	g_value_set_string ( value, str );
leo_sobral@2
  1176
	break;
leo_sobral@2
  1177
      }
leo_sobral@2
  1178
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
  1179
    case PROP_GMYTHTV_DBG:
leo_sobral@2
  1180
      g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
leo_sobral@2
  1181
      break;
leo_sobral@2
  1182
#endif
rosfran@40
  1183
    case PROP_GMYTHTV_VERSION:
leo_sobral@2
  1184
      {
leo_sobral@2
  1185
	g_value_set_int ( value, mythtvsrc->mythtv_version );
leo_sobral@2
  1186
	break;
leo_sobral@2
  1187
      }
rosfran@40
  1188
    case PROP_GMYTHTV_LIVEID:
leo_sobral@2
  1189
      {
leo_sobral@2
  1190
	g_value_set_int ( value, mythtvsrc->live_tv_id );
leo_sobral@2
  1191
	break;
leo_sobral@2
  1192
      }
rosfran@40
  1193
    case PROP_GMYTHTV_LIVE:
leo_sobral@2
  1194
      g_value_set_boolean ( value, mythtvsrc->live_tv );
leo_sobral@2
  1195
      break;
rosfran@52
  1196
    case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
rosfran@52
  1197
      g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
rosfran@52
  1198
      break;
rosfran@40
  1199
    case PROP_GMYTHTV_LIVE_CHAINID:
leo_sobral@2
  1200
      {
leo_sobral@2
  1201
	gchar *str = g_strdup( "" );
leo_sobral@2
  1202
leo_sobral@2
  1203
	if ( mythtvsrc->live_chain_id == NULL ) {
leo_sobral@2
  1204
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
  1205
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
  1206
	} else {
leo_sobral@2
  1207
	  str = g_strdup( mythtvsrc->live_chain_id );
leo_sobral@2
  1208
	}
leo_sobral@2
  1209
	g_value_set_string ( value, str );
leo_sobral@2
  1210
	break;
leo_sobral@2
  1211
      }
rosfran@70
  1212
    case PROP_GMYTHTV_CHANNEL_NUM:
rosfran@70
  1213
      {
rosfran@70
  1214
	g_value_set_int ( value, mythtvsrc->channel_num );
rosfran@70
  1215
	break;
rosfran@70
  1216
      }
leo_sobral@2
  1217
    default:
leo_sobral@2
  1218
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
  1219
      break;
leo_sobral@2
  1220
  }
leo_sobral@2
  1221
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
  1222
}
leo_sobral@2
  1223
leo_sobral@2
  1224
/* entry point to initialize the plug-in
leo_sobral@2
  1225
 * initialize the plug-in itself
leo_sobral@2
  1226
 * register the element factories and pad templates
leo_sobral@2
  1227
 * register the features
leo_sobral@2
  1228
 */
rosfran@40
  1229
static gboolean
leo_sobral@2
  1230
plugin_init (GstPlugin * plugin)
leo_sobral@2
  1231
{
leo_sobral@2
  1232
  return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
leo_sobral@2
  1233
      GST_TYPE_MYTHTV_SRC);
leo_sobral@2
  1234
}
leo_sobral@2
  1235
leo_sobral@2
  1236
/* this is the structure that gst-register looks for
leo_sobral@2
  1237
 * so keep the name plugin_desc, or you cannot get your plug-in registered */
leo_sobral@2
  1238
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
leo_sobral@2
  1239
    GST_VERSION_MINOR,
leo_sobral@2
  1240
    "mythtv",
leo_sobral@2
  1241
    "lib MythTV src",
leo_sobral@2
  1242
    plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
leo_sobral@2
  1243
leo_sobral@2
  1244
leo_sobral@2
  1245
/*** GSTURIHANDLER INTERFACE *************************************************/
leo_sobral@2
  1246
  static guint 
leo_sobral@2
  1247
gst_mythtv_src_uri_get_type (void)
leo_sobral@2
  1248
{
leo_sobral@2
  1249
  return GST_URI_SRC;
leo_sobral@2
  1250
}
leo_sobral@2
  1251
leo_sobral@2
  1252
  static gchar **
leo_sobral@2
  1253
gst_mythtv_src_uri_get_protocols (void)
leo_sobral@2
  1254
{
leo_sobral@2
  1255
  static gchar *protocols[] = { "myth", "myths", NULL };
leo_sobral@2
  1256
leo_sobral@2
  1257
  return protocols;
leo_sobral@2
  1258
}
leo_sobral@2
  1259
leo_sobral@2
  1260
  static const gchar *
leo_sobral@2
  1261
gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
leo_sobral@2
  1262
{
leo_sobral@2
  1263
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
  1264
leo_sobral@2
  1265
  return src->uri_name;
leo_sobral@2
  1266
}
leo_sobral@2
  1267
leo_sobral@2
  1268
  static gboolean
leo_sobral@2
  1269
gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
leo_sobral@2
  1270
{
leo_sobral@2
  1271
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
  1272
leo_sobral@2
  1273
  gchar *protocol;
leo_sobral@2
  1274
leo_sobral@2
  1275
  protocol = gst_uri_get_protocol (uri);
leo_sobral@2
  1276
  if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
leo_sobral@2
  1277
    g_free (protocol);
leo_sobral@2
  1278
    return FALSE;
leo_sobral@2
  1279
  }
leo_sobral@2
  1280
  g_free (protocol);
leo_sobral@2
  1281
  g_object_set (src, "location", uri, NULL);
leo_sobral@2
  1282
leo_sobral@2
  1283
  return TRUE;
leo_sobral@2
  1284
}
leo_sobral@2
  1285
rosfran@87
  1286
 static void
leo_sobral@2
  1287
gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
leo_sobral@2
  1288
{
leo_sobral@2
  1289
  GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
leo_sobral@2
  1290
leo_sobral@2
  1291
  iface->get_type = gst_mythtv_src_uri_get_type;
leo_sobral@2
  1292
  iface->get_protocols = gst_mythtv_src_uri_get_protocols;
leo_sobral@2
  1293
  iface->get_uri = gst_mythtv_src_uri_get_uri;
leo_sobral@2
  1294
  iface->set_uri = gst_mythtv_src_uri_set_uri;
leo_sobral@2
  1295
}
leo_sobral@2
  1296
leo_sobral@2
  1297
  void
leo_sobral@2
  1298
size_header_handler (void *userdata, const char *value)
leo_sobral@2
  1299
{
leo_sobral@2
  1300
  GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
leo_sobral@2
  1301
leo_sobral@2
  1302
  //src->content_size = g_ascii_strtoull (value, NULL, 10);
leo_sobral@2
  1303
leo_sobral@2
  1304
  GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
leo_sobral@2
  1305
}