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