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