gst-plugins-mythtv/src/gstmythtvsrc.c
author melunko
Mon Oct 23 14:41:30 2006 +0100 (2006-10-23)
branchtrunk
changeset 38 d5f5855e7800
parent 34 c71d37b93734
child 40 ba5dc9bff3a1
permissions -rwxr-xr-x
[svn r39] Added mythtv gnomevfs module based on gmyth
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"
leo_sobral@2
    21
#include "myth_file_transfer.h"
leo_sobral@2
    22
#include "myth_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
leo_sobral@2
    33
#define GST_MYTHTV_ID_NUM		1
leo_sobral@2
    34
leo_sobral@2
    35
#define MYTHTV_VERSION_DEFAULT		30
leo_sobral@2
    36
leo_sobral@2
    37
#define MYTHTV_TRANSFER_MAX_WAITS	100
leo_sobral@2
    38
rosfran@34
    39
#define MYTHTV_TRANSFER_MAX_BUFFER	1024*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
rosfran@34
    46
#define ENABLE_TIMING_POSITION		0
leo_sobral@2
    47
leo_sobral@2
    48
/* stablish a maximum iteration value to the IS_RECORDING message */
leo_sobral@2
    49
static guint wait_to_transfer = 0;
leo_sobral@2
    50
leo_sobral@2
    51
static const GstElementDetails gst_mythtv_src_details =
rosfran@30
    52
GST_ELEMENT_DETAILS ( "MythTV client source",
leo_sobral@2
    53
    "Source/Network",
leo_sobral@2
    54
    "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
rosfran@30
    55
    "Rosfran Borges <rosfran.borges@indt.org.br>" );
leo_sobral@2
    56
leo_sobral@2
    57
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
leo_sobral@2
    58
    GST_PAD_SRC,
leo_sobral@2
    59
    GST_PAD_ALWAYS,
rosfran@30
    60
    GST_STATIC_CAPS ("video/x-nuv") );
rosfran@30
    61
    //GST_STATIC_CAPS_ANY);
rosfran@30
    62
rosfran@30
    63
static GThread *update_size_task = NULL;
rosfran@30
    64
rosfran@30
    65
static GStaticMutex update_size_mutex = G_STATIC_MUTEX_INIT;
leo_sobral@2
    66
leo_sobral@2
    67
enum
leo_sobral@2
    68
{
leo_sobral@2
    69
  PROP_0,
leo_sobral@2
    70
  PROP_LOCATION,
leo_sobral@2
    71
  PROP_URI,
leo_sobral@2
    72
#ifndef GST_DISABLE_GST_DEBUG
leo_sobral@2
    73
  PROP_MYTHTV_DBG,
leo_sobral@2
    74
#endif
leo_sobral@2
    75
  PROP_MYTHTV_VERSION,
leo_sobral@2
    76
  PROP_MYTHTV_LIVE,
leo_sobral@2
    77
  PROP_MYTHTV_LIVEID,
leo_sobral@2
    78
  PROP_MYTHTV_LIVE_CHAINID
leo_sobral@2
    79
};
leo_sobral@2
    80
leo_sobral@2
    81
static void gst_mythtv_src_finalize (GObject * gobject);
leo_sobral@2
    82
rosfran@30
    83
static GstFlowReturn gst_mythtv_src_create (GstBaseSrc * psrc, guint64 offset, 
rosfran@30
    84
	guint size, GstBuffer ** outbuf);
rosfran@30
    85
//static GstFlowReturn gst_mythtv_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
leo_sobral@2
    86
static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
leo_sobral@2
    87
static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
leo_sobral@2
    88
static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
rosfran@30
    89
static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
rosfran@30
    90
//static gboolean gst_mythtv_new_segment ( GstBaseSrc * psrc );
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 GstFlowReturn gst_mythtv_src_chain (GstPad * pad, GstBuffer * buf);
leo_sobral@2
    98
rosfran@30
    99
static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
rosfran@30
   100
rosfran@31
   101
static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
rosfran@30
   102
//static gboolean gst_mythtv_src_query ( GstPad * pad, GstQuery * query );
rosfran@30
   103
rosfran@37
   104
static gint do_read_request_response (GstMythtvSrc *src, guint64 offset, 
rosfran@30
   105
		guint size, GstBuffer **outbuf);
rosfran@30
   106
//static gboolean gst_mythtv_src_sink_activate_pull (GstPad * srcpad, gboolean active);
leo_sobral@2
   107
leo_sobral@2
   108
  static void
leo_sobral@2
   109
_urihandler_init (GType type)
leo_sobral@2
   110
{
leo_sobral@2
   111
  static const GInterfaceInfo urihandler_info = {
leo_sobral@2
   112
    gst_mythtv_src_uri_handler_init,
leo_sobral@2
   113
    NULL,
leo_sobral@2
   114
    NULL
leo_sobral@2
   115
  };
leo_sobral@2
   116
leo_sobral@2
   117
  g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
leo_sobral@2
   118
leo_sobral@2
   119
  GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
leo_sobral@2
   120
      "MythTV src");
leo_sobral@2
   121
}
leo_sobral@2
   122
leo_sobral@2
   123
GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstBaseSrc,
rosfran@30
   124
    GST_TYPE_BASE_SRC, _urihandler_init)
rosfran@30
   125
    
rosfran@30
   126
//GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
rosfran@30
   127
//    GST_TYPE_PUSH_SRC, _urihandler_init)
rosfran@30
   128
    
leo_sobral@2
   129
  static void
leo_sobral@2
   130
gst_mythtv_src_base_init (gpointer g_class)
leo_sobral@2
   131
{
leo_sobral@2
   132
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
leo_sobral@2
   133
leo_sobral@2
   134
  gst_element_class_add_pad_template (element_class,
leo_sobral@2
   135
      gst_static_pad_template_get (&srctemplate));
leo_sobral@2
   136
leo_sobral@2
   137
  gst_element_class_set_details (element_class, &gst_mythtv_src_details);
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
{
leo_sobral@2
   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;
rosfran@30
   149
  //gstpushsrc_class = (GstPushSrcClass *) klass;
leo_sobral@2
   150
leo_sobral@2
   151
  gobject_class->set_property = gst_mythtv_src_set_property;
leo_sobral@2
   152
  gobject_class->get_property = gst_mythtv_src_get_property;
leo_sobral@2
   153
  gobject_class->finalize = gst_mythtv_src_finalize;
leo_sobral@2
   154
leo_sobral@2
   155
  g_object_class_install_property
leo_sobral@2
   156
    (gobject_class, PROP_LOCATION,
leo_sobral@2
   157
     g_param_spec_string ("location", "Location",
leo_sobral@2
   158
       "The location. In the form:"
leo_sobral@2
   159
       "\n\t\t\tmyth://a.com/file.nuv"
leo_sobral@2
   160
       "\n\t\t\tmyth://a.com:23223/file.nuv "
leo_sobral@2
   161
       "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
leo_sobral@2
   162
       "", G_PARAM_READWRITE));
leo_sobral@2
   163
leo_sobral@2
   164
  g_object_class_install_property
leo_sobral@2
   165
    (gobject_class, PROP_URI,
leo_sobral@2
   166
     g_param_spec_string ("uri", "Uri",
leo_sobral@2
   167
       "The location in form of a URI (deprecated; use location)",
leo_sobral@2
   168
       "", G_PARAM_READWRITE));
leo_sobral@2
   169
leo_sobral@2
   170
  g_object_class_install_property
leo_sobral@2
   171
    (gobject_class, PROP_MYTHTV_VERSION,
leo_sobral@2
   172
     g_param_spec_int ("mythtv-version", "mythtv-version",
leo_sobral@2
   173
       "Change Myth TV version",
leo_sobral@2
   174
       26, 30, 26, G_PARAM_READWRITE));
leo_sobral@2
   175
leo_sobral@2
   176
  g_object_class_install_property
leo_sobral@2
   177
    (gobject_class, PROP_MYTHTV_LIVEID,
leo_sobral@2
   178
     g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
leo_sobral@2
   179
       "Change Myth TV version",
leo_sobral@2
   180
       0, 200, GST_MYTHTV_ID_NUM, G_PARAM_READWRITE));
leo_sobral@2
   181
leo_sobral@2
   182
  g_object_class_install_property
leo_sobral@2
   183
    (gobject_class, PROP_MYTHTV_LIVE_CHAINID,
leo_sobral@2
   184
     g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
leo_sobral@2
   185
       "Sets the Myth TV chain ID (from TV Chain)",
leo_sobral@2
   186
       "", G_PARAM_READWRITE));
leo_sobral@2
   187
leo_sobral@2
   188
  g_object_class_install_property
leo_sobral@2
   189
    (gobject_class, PROP_MYTHTV_LIVE,
leo_sobral@2
   190
     g_param_spec_boolean ("mythtv-live", "mythtv-live",
leo_sobral@2
   191
       "Enable MythTV Live TV content streaming",
leo_sobral@2
   192
       FALSE, G_PARAM_READWRITE));
leo_sobral@2
   193
leo_sobral@2
   194
#ifndef GST_DISABLE_GST_DEBUG
leo_sobral@2
   195
  g_object_class_install_property
leo_sobral@2
   196
    (gobject_class, PROP_MYTHTV_DBG,
leo_sobral@2
   197
     g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
leo_sobral@2
   198
       "Enable MythTV debug messages",
leo_sobral@2
   199
       FALSE, G_PARAM_READWRITE));
leo_sobral@2
   200
#endif
leo_sobral@2
   201
leo_sobral@2
   202
  gstbasesrc_class->start = gst_mythtv_src_start;
leo_sobral@2
   203
  gstbasesrc_class->stop = gst_mythtv_src_stop;
leo_sobral@2
   204
  gstbasesrc_class->get_size = gst_mythtv_src_get_size;
leo_sobral@2
   205
  gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
rosfran@30
   206
  //gstpushsrc_class->newsegment = gst_mythtv_src_new_segment;
leo_sobral@2
   207
leo_sobral@2
   208
  gstbasesrc_class->create = gst_mythtv_src_create;
rosfran@30
   209
  
leo_sobral@2
   210
  GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
leo_sobral@2
   211
      "MythTV Client Source");
leo_sobral@2
   212
}
leo_sobral@2
   213
rosfran@30
   214
static void
leo_sobral@2
   215
gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
leo_sobral@2
   216
{
leo_sobral@2
   217
  this->file_transfer = NULL;
leo_sobral@2
   218
leo_sobral@2
   219
  this->unique_setup = FALSE;
leo_sobral@2
   220
leo_sobral@2
   221
  this->mythtv_version = MYTHTV_VERSION_DEFAULT;
leo_sobral@2
   222
leo_sobral@2
   223
  this->bytes_read = 0;
leo_sobral@2
   224
leo_sobral@2
   225
  this->content_size = -1;
leo_sobral@2
   226
  this->read_offset = 0;
leo_sobral@2
   227
leo_sobral@2
   228
  this->live_tv = FALSE;
leo_sobral@2
   229
leo_sobral@2
   230
  this->user_agent = g_strdup ("mythtvsrc");
rosfran@30
   231
  this->mythtv_caps = NULL;  
rosfran@30
   232
  
rosfran@30
   233
  gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );  
leo_sobral@2
   234
rosfran@30
   235
  gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE );
rosfran@30
   236
rosfran@30
   237
  //gst_pad_set_chain_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
rosfran@30
   238
  //    gst_mythtv_src_chain );
rosfran@30
   239
rosfran@30
   240
  gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
rosfran@30
   241
      gst_mythtv_src_handle_event );
rosfran@31
   242
/*
rosfran@30
   243
  gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
rosfran@30
   244
      gst_mythtv_src_query );
rosfran@30
   245
*/
rosfran@30
   246
  //gst_pad_set_activatepull_function( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
rosfran@30
   247
  //    gst_mythtv_src_sink_activate_pull );
rosfran@30
   248
leo_sobral@2
   249
}
leo_sobral@2
   250
rosfran@30
   251
static void
leo_sobral@2
   252
gst_mythtv_src_finalize (GObject * gobject)
leo_sobral@2
   253
{
leo_sobral@2
   254
  GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
leo_sobral@2
   255
leo_sobral@2
   256
  g_free (this->user_agent);
leo_sobral@2
   257
leo_sobral@2
   258
  if (this->mythtv_caps) {
leo_sobral@2
   259
    gst_caps_unref (this->mythtv_caps);
leo_sobral@2
   260
    this->mythtv_caps = NULL;
leo_sobral@2
   261
  }
leo_sobral@2
   262
leo_sobral@2
   263
  if (this->file_transfer) {
leo_sobral@2
   264
    g_object_unref (this->file_transfer);
leo_sobral@2
   265
    this->file_transfer = NULL;
leo_sobral@2
   266
  }
leo_sobral@2
   267
leo_sobral@2
   268
  if (this->uri_name) {
leo_sobral@2
   269
    g_free (this->uri_name);
leo_sobral@2
   270
  }
leo_sobral@2
   271
leo_sobral@2
   272
  if (this->user_agent) {
leo_sobral@2
   273
    g_free (this->user_agent);
leo_sobral@2
   274
  }
leo_sobral@2
   275
leo_sobral@2
   276
  G_OBJECT_CLASS (parent_class)->finalize (gobject);
leo_sobral@2
   277
}
leo_sobral@2
   278
rosfran@37
   279
static gint
rosfran@30
   280
do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, GstBuffer **outbuf)
leo_sobral@2
   281
{
rosfran@37
   282
  gint read = 0;
leo_sobral@2
   283
  guint sizetoread = size; //GST_BUFFER_SIZE (outbuf);
rosfran@31
   284
  //GstBuffer *buffer = gst_buffer_new_and_alloc( size ); 
leo_sobral@2
   285
rosfran@30
   286
  g_print( "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); 
leo_sobral@2
   287
rosfran@30
   288
  /* Loop sending the Myth File Transfer request:
leo_sobral@2
   289
   * Retry whilst authentication fails and we supply it. */
rosfran@37
   290
  gint len = 0;
leo_sobral@2
   291
rosfran@37
   292
  GST_OBJECT_LOCK(src);
leo_sobral@2
   293
leo_sobral@2
   294
  while ( sizetoread > 0 ) {
rosfran@31
   295
  	
rosfran@31
   296
  	len = myth_file_transfer_read( src->file_transfer,
rosfran@31
   297
			GST_BUFFER_DATA( *outbuf ) + read, sizetoread, TRUE );
leo_sobral@2
   298
leo_sobral@2
   299
    if ( len > 0 ) {
rosfran@34
   300
      read += len;      
leo_sobral@2
   301
      sizetoread -= len;
rosfran@30
   302
    } else if ( len <= 0 ) {
rosfran@30
   303
    	
rosfran@30
   304
      if ( src->live_tv == FALSE ) {
rosfran@30
   305
		goto done;
rosfran@34
   306
      } else if ( /*src->content_size >= src->read_offset && 
rosfran@34
   307
      			abs ( src->content_size - src->read_offset ) <= 1024 ) ||*/
rosfran@34
   308
      			( src->content_size <= ( src->read_offset + size + MYTHTV_TRANSFER_MAX_BUFFER ) ) )
rosfran@34
   309
  {
rosfran@34
   310
#if ENABLE_TIMING_POSITION == 1
rosfran@34
   311
    guint64 size_tmp = 0;
rosfran@34
   312
    if (src->live_tv == TRUE) {
rosfran@34
   313
get_file_pos:
rosfran@34
   314
      size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
rosfran@34
   315
      if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@34
   316
	src->content_size = size_tmp;
rosfran@34
   317
      else
rosfran@34
   318
	goto get_file_pos;
rosfran@34
   319
      g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
rosfran@34
   320
	  __FUNCTION__, size_tmp );
rosfran@34
   321
    }
rosfran@34
   322
#else
rosfran@34
   323
  	guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer );
rosfran@34
   324
  	if ( src->content_size < new_offset ) {
rosfran@34
   325
  	  src->content_size = new_offset;
rosfran@34
   326
  	}
rosfran@34
   327
#endif
rosfran@34
   328
	goto done;
rosfran@34
   329
  }
rosfran@30
   330
leo_sobral@2
   331
    }
rosfran@31
   332
    
rosfran@30
   333
    if ( read == sizetoread )
leo_sobral@2
   334
      break;
leo_sobral@2
   335
  }
rosfran@30
   336
  
leo_sobral@2
   337
  if ( read > 0 ) {
rosfran@34
   338
  	src->read_offset += read;
leo_sobral@2
   339
    src->bytes_read += read;
rosfran@31
   340
  
leo_sobral@2
   341
  g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
leo_sobral@2
   342
      "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
leo_sobral@2
   343
      src->read_offset, src->content_size );
rosfran@31
   344
      
rosfran@31
   345
  //GST_BUFFER_TIMESTAMP( buffer ) = GST_BUFFER_TIMESTAMP ( *outbuf );
rosfran@31
   346
  GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
rosfran@31
   347
  GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
rosfran@31
   348
  GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
rosfran@31
   349
  //memcpy( GST_BUFFER_DATA( *outbuf ), GST_BUFFER_DATA( buffer ), read );
rosfran@30
   350
     
rosfran@30
   351
  g_print( "Stopping: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
rosfran@30
   352
      "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
rosfran@30
   353
      GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
rosfran@31
   354
      
rosfran@34
   355
  } else if ( !src->live_tv )
rosfran@31
   356
  	goto eos;
rosfran@31
   357
  
rosfran@30
   358
  goto done;
leo_sobral@2
   359
leo_sobral@2
   360
eos:
leo_sobral@2
   361
  src->eos = TRUE;
rosfran@30
   362
  
leo_sobral@2
   363
done:
rosfran@37
   364
  GST_OBJECT_UNLOCK(src);
leo_sobral@2
   365
leo_sobral@2
   366
  return read;
leo_sobral@2
   367
}
leo_sobral@2
   368
rosfran@30
   369
static GstFlowReturn
rosfran@30
   370
gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
leo_sobral@2
   371
{
leo_sobral@2
   372
  GstMythtvSrc *src;
leo_sobral@2
   373
  GstFlowReturn ret = GST_FLOW_OK;
rosfran@37
   374
  gint read = -1;
rosfran@30
   375
  
rosfran@30
   376
  src = GST_MYTHTV_SRC (psrc);
rosfran@31
   377
  /* The caller should know the number of bytes and not read beyond EOS. */
rosfran@31
   378
  if (G_UNLIKELY (src->eos))
rosfran@31
   379
    goto eos;
rosfran@30
   380
  
rosfran@31
   381
  //GST_OBJECT_LOCK(src);
leo_sobral@2
   382
rosfran@30
   383
  if (G_UNLIKELY (src->read_offset != offset)) {
rosfran@30
   384
    guint64 new_offset = myth_file_transfer_seek(src->file_transfer, offset, SEEK_SET);
rosfran@34
   385
    g_print( "[%s] SRC Offset = %llu, NEW actual backend SEEK Offset = %llu.\n",
rosfran@34
   386
    	__FUNCTION__, src->read_offset, new_offset );
rosfran@34
   387
    if (G_UNLIKELY (new_offset < 0 ) )//|| new_offset != src->read_offset)) {
rosfran@34
   388
    {
rosfran@31
   389
      //GST_OBJECT_UNLOCK(src);
rosfran@37
   390
      goto seek_failed;
rosfran@31
   391
    }
leo_sobral@2
   392
rosfran@30
   393
    src->read_offset = offset;
rosfran@30
   394
  }
rosfran@31
   395
  //GST_OBJECT_UNLOCK(src);
rosfran@30
   396
  
rosfran@31
   397
  /* Create the buffer. */
rosfran@31
   398
  ret = gst_pad_alloc_buffer (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
rosfran@31
   399
      src->read_offset, size,
rosfran@31
   400
      //src->icy_caps ? src->icy_caps :
rosfran@31
   401
      GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf);
leo_sobral@2
   402
rosfran@31
   403
  if (G_UNLIKELY (ret != GST_FLOW_OK))
rosfran@31
   404
    goto done;
rosfran@30
   405
  
rosfran@31
   406
  //g_static_mutex_lock( &update_size_mutex );
rosfran@31
   407
  read = do_read_request_response ( src, src->read_offset, size, outbuf );
rosfran@31
   408
  //g_static_mutex_unlock( &update_size_mutex );  
leo_sobral@2
   409
rosfran@31
   410
  if (G_UNLIKELY (read < 0) || *outbuf == NULL) {
rosfran@31
   411
  	//if ( src->live_tv )
rosfran@31
   412
    //	goto done;
rosfran@31
   413
    //else
leo_sobral@2
   414
    goto read_error;
rosfran@31
   415
  }
leo_sobral@2
   416
rosfran@31
   417
/* 
leo_sobral@2
   418
  if (G_UNLIKELY(src->eos))
leo_sobral@2
   419
    goto eos;
rosfran@30
   420
  else
rosfran@30
   421
    goto done;
rosfran@31
   422
*/
leo_sobral@2
   423
leo_sobral@2
   424
done:
rosfran@31
   425
 {
rosfran@31
   426
    const gchar *reason = gst_flow_get_name (ret);
rosfran@31
   427
rosfran@31
   428
    GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
rosfran@31
   429
  	return ret;
rosfran@31
   430
 }
leo_sobral@2
   431
eos:
leo_sobral@2
   432
  {
rosfran@30
   433
    const gchar *reason = gst_flow_get_name (ret);
rosfran@30
   434
rosfran@30
   435
    GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
leo_sobral@2
   436
    return GST_FLOW_UNEXPECTED;
leo_sobral@2
   437
  }
leo_sobral@2
   438
  /* ERRORS */
leo_sobral@2
   439
read_error:
leo_sobral@2
   440
  {
leo_sobral@2
   441
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
leo_sobral@2
   442
	(NULL), ("Could not read any bytes (%i, %s)", read,
leo_sobral@2
   443
	  src->uri_name));
leo_sobral@2
   444
    return GST_FLOW_ERROR;
leo_sobral@2
   445
  }
rosfran@37
   446
seek_failed:
rosfran@37
   447
  {
rosfran@37
   448
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@37
   449
	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
rosfran@37
   450
	  src->uri_name));
rosfran@37
   451
	// go to the next program chain
rosfran@37
   452
	src->unique_setup = FALSE;
rosfran@37
   453
	gst_mythtv_src_start( psrc );
rosfran@37
   454
  
rosfran@37
   455
    return GST_FLOW_OK;
rosfran@37
   456
  }
rosfran@30
   457
leo_sobral@2
   458
}
leo_sobral@2
   459
rosfran@30
   460
void
rosfran@30
   461
update_size_func( void *mythtv_data ) 
leo_sobral@2
   462
{
rosfran@30
   463
  GstMythtvSrc *src;
leo_sobral@2
   464
rosfran@30
   465
  g_return_if_fail( mythtv_data != NULL );
leo_sobral@2
   466
rosfran@30
   467
  src = GST_MYTHTV_SRC ( mythtv_data );
rosfran@30
   468
rosfran@30
   469
  g_static_mutex_lock( &update_size_mutex );
rosfran@30
   470
rosfran@30
   471
  if ( src->do_start ) {
rosfran@30
   472
#if ENABLE_TIMING_POSITION == 1
rosfran@30
   473
    guint64 size_tmp = 0;
rosfran@30
   474
    if (src->live_tv == TRUE) {
rosfran@30
   475
get_file_pos:
rosfran@30
   476
      g_usleep( 50 );
rosfran@30
   477
      size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
rosfran@30
   478
      if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@30
   479
	src->content_size = size_tmp;
rosfran@30
   480
      else
rosfran@30
   481
	goto get_file_pos;
rosfran@30
   482
      g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
rosfran@30
   483
	  __FUNCTION__, size_tmp );
rosfran@30
   484
    }
rosfran@30
   485
#endif
leo_sobral@2
   486
  }
rosfran@30
   487
  g_static_mutex_unlock( &update_size_mutex );
leo_sobral@2
   488
leo_sobral@2
   489
}
leo_sobral@2
   490
rosfran@30
   491
guint64
rosfran@30
   492
gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
leo_sobral@2
   493
{
rosfran@30
   494
rosfran@30
   495
  if ( src->do_start ) {
rosfran@30
   496
#if ENABLE_TIMING_POSITION == 1
rosfran@30
   497
    guint64 size_tmp = 0;
rosfran@30
   498
    if (src->live_tv == TRUE) {
rosfran@30
   499
get_file_pos:
rosfran@30
   500
      g_usleep( 50 );
rosfran@30
   501
      size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
rosfran@30
   502
      if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@30
   503
		src->content_size = size_tmp;
rosfran@30
   504
      else
rosfran@30
   505
	goto get_file_pos;
rosfran@30
   506
      g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
rosfran@30
   507
	  __FUNCTION__, size_tmp );
rosfran@30
   508
    }
rosfran@30
   509
#endif
rosfran@30
   510
  }
rosfran@30
   511
  
rosfran@30
   512
  return src->content_size;	
rosfran@30
   513
leo_sobral@2
   514
}
leo_sobral@2
   515
leo_sobral@2
   516
/* create a socket for connecting to remote server */
rosfran@30
   517
static gboolean
leo_sobral@2
   518
gst_mythtv_src_start ( GstBaseSrc * bsrc )
leo_sobral@2
   519
{
leo_sobral@2
   520
  GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
leo_sobral@2
   521
leo_sobral@2
   522
  GString *chain_id_local = NULL;
leo_sobral@2
   523
leo_sobral@2
   524
  gboolean ret = TRUE;
rosfran@30
   525
leo_sobral@2
   526
  if (src->unique_setup == FALSE) {
leo_sobral@2
   527
    src->unique_setup = TRUE;
leo_sobral@2
   528
  } else {
leo_sobral@2
   529
    goto done;
leo_sobral@2
   530
  }
leo_sobral@2
   531
leo_sobral@2
   532
  GST_OBJECT_LOCK(src);
leo_sobral@2
   533
leo_sobral@2
   534
  if ( src->live_tv ) {
leo_sobral@2
   535
    src->spawn_livetv = myth_livetv_new( );
leo_sobral@2
   536
    if ( myth_livetv_setup( src->spawn_livetv ) == FALSE ) {
rosfran@30
   537
      ret = FALSE;
rosfran@30
   538
      goto init_failed;
leo_sobral@2
   539
    }
leo_sobral@2
   540
    /* set up the uri variable */
leo_sobral@2
   541
    src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
leo_sobral@2
   542
    chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
leo_sobral@2
   543
    if ( chain_id_local != NULL ) {
leo_sobral@2
   544
      src->live_chain_id = g_strdup( chain_id_local->str );
leo_sobral@2
   545
      g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
leo_sobral@2
   546
    }
leo_sobral@2
   547
    src->live_tv_id = src->spawn_livetv->remote_encoder->recorder_num;
leo_sobral@2
   548
    g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
leo_sobral@2
   549
  }
leo_sobral@2
   550
leo_sobral@2
   551
  src->file_transfer = myth_file_transfer_new( src->live_tv_id, 
leo_sobral@2
   552
      g_string_new( src->uri_name ), -1, src->mythtv_version );
leo_sobral@2
   553
leo_sobral@2
   554
  if ( src->file_transfer == NULL ) {
leo_sobral@2
   555
    GST_OBJECT_UNLOCK(src);
leo_sobral@2
   556
leo_sobral@2
   557
    goto init_failed;
leo_sobral@2
   558
  }
leo_sobral@2
   559
leo_sobral@2
   560
  /* sets the Playback monitor connection */
leo_sobral@2
   561
  ret = myth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
leo_sobral@2
   562
leo_sobral@2
   563
  if ( src->live_tv == TRUE && ret == TRUE ) {
leo_sobral@2
   564
    /* loop finished, set the max tries variable to zero again... */
leo_sobral@2
   565
    wait_to_transfer = 0;
leo_sobral@2
   566
leo_sobral@2
   567
    while ( wait_to_transfer++ < MYTHTV_TRANSFER_MAX_WAITS && ( myth_file_transfer_is_recording( src->file_transfer ) == FALSE 
leo_sobral@2
   568
	  /*|| ( myth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
leo_sobral@2
   569
      g_usleep( 100 );
leo_sobral@2
   570
  }
leo_sobral@2
   571
leo_sobral@2
   572
  /* sets the FileTransfer instance connection (video/audio download) */
leo_sobral@2
   573
  ret = myth_file_transfer_setup( &(src->file_transfer), src->live_tv );
leo_sobral@2
   574
leo_sobral@2
   575
  if ( ret == FALSE ) {
leo_sobral@2
   576
    GST_OBJECT_UNLOCK(src);
leo_sobral@2
   577
#ifndef GST_DISABLE_GST_DEBUG  
leo_sobral@2
   578
    if ( src->mythtv_msgs_dbg )
leo_sobral@2
   579
      g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
leo_sobral@2
   580
#endif
leo_sobral@2
   581
    goto begin_req_failed;
leo_sobral@2
   582
  }
leo_sobral@2
   583
leo_sobral@2
   584
  src->content_size = src->file_transfer->filesize;
leo_sobral@2
   585
leo_sobral@2
   586
  GST_OBJECT_UNLOCK(src);
rosfran@30
   587
  if ( src->live_tv ) {
rosfran@30
   588
rosfran@30
   589
    //GError* error;
rosfran@30
   590
    //update_size_task = g_thread_create( (GThreadFunc)update_size_func, src, FALSE, &error );
rosfran@30
   591
    g_print( "[%s] Update Size task = %s\n", __FUNCTION__, update_size_task != NULL ?  "OK !" : "ERROR!!!" );
rosfran@30
   592
rosfran@30
   593
  }
rosfran@30
   594
  src->do_start = TRUE;  
rosfran@30
   595
rosfran@30
   596
done:
rosfran@30
   597
  return TRUE;
rosfran@30
   598
rosfran@30
   599
  /* ERRORS */
rosfran@30
   600
init_failed:
rosfran@30
   601
  {
rosfran@30
   602
    if (src->spawn_livetv != NULL )
rosfran@30
   603
      g_object_unref( src->spawn_livetv );
rosfran@30
   604
rosfran@30
   605
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@30
   606
	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
rosfran@30
   607
    return FALSE;
rosfran@30
   608
  }
rosfran@30
   609
begin_req_failed:
rosfran@30
   610
  {
rosfran@30
   611
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@30
   612
	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
rosfran@30
   613
    return FALSE;
rosfran@30
   614
  }
rosfran@30
   615
}
leo_sobral@2
   616
leo_sobral@2
   617
#if 0
rosfran@30
   618
/* handles queries for location in the stream in the requested format */
rosfran@30
   619
static gboolean
rosfran@30
   620
gst_mythtv_src_query ( GstPad * pad, GstQuery * query )
rosfran@30
   621
{
rosfran@30
   622
  gboolean res = TRUE;
rosfran@30
   623
  GstMythtvSrc *mythtv;
leo_sobral@2
   624
rosfran@30
   625
  guint64 size = 0;
rosfran@30
   626
rosfran@30
   627
  mythtv = GST_MYTHTV_SRC( GST_PAD_PARENT (pad) );
rosfran@30
   628
rosfran@30
   629
  size = gst_mythtv_src_get_position (mythtv);
rosfran@30
   630
rosfran@30
   631
  switch (GST_QUERY_TYPE (query)) {
rosfran@30
   632
rosfran@30
   633
    case GST_QUERY_POSITION:
rosfran@30
   634
      {
rosfran@30
   635
rosfran@30
   636
	//GstFormat format;
rosfran@30
   637
	gint64 cur = 0;
rosfran@30
   638
rosfran@30
   639
	/* save requested format */
rosfran@30
   640
	gst_query_parse_position (query, NULL, &cur);
rosfran@30
   641
rosfran@30
   642
	/* query peer for current position in time */
rosfran@30
   643
	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
rosfran@30
   644
	    ( size > cur ) ? "greater" : "lower", size, cur );
rosfran@30
   645
	gst_query_set_position (query, GST_FORMAT_BYTES, size);
rosfran@30
   646
	if ( size < cur )
rosfran@30
   647
	  goto error;
rosfran@30
   648
rosfran@30
   649
	break;
leo_sobral@2
   650
      }
rosfran@30
   651
     #if 0
rosfran@30
   652
    case GST_QUERY_DURATION:
rosfran@30
   653
      {
rosfran@30
   654
	//GstFormat format;
rosfran@30
   655
	gint64 cur = 0;
rosfran@30
   656
rosfran@30
   657
	/* save requested format */
rosfran@30
   658
	gst_query_parse_position (query, NULL, &cur);
rosfran@30
   659
rosfran@30
   660
	/* query peer for current position in time */
rosfran@30
   661
	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
rosfran@30
   662
	    ( size * GST_SECOND > cur * GST_SECOND ) ? "greater" : "lower", size * GST_SECOND, 
rosfran@30
   663
	    cur * GST_SECOND );
rosfran@30
   664
	gst_query_set_position (query, GST_FORMAT_TIME, size * GST_SECOND );
rosfran@30
   665
rosfran@30
   666
	if ( size * GST_SECOND < cur * GST_SECOND )
rosfran@30
   667
	  goto error;
rosfran@30
   668
rosfran@30
   669
	break;
rosfran@30
   670
      }
rosfran@30
   671
	#endif
rosfran@30
   672
    default:
rosfran@30
   673
      res = FALSE;
rosfran@30
   674
      break;
rosfran@30
   675
  }
rosfran@30
   676
rosfran@30
   677
  return res;
rosfran@30
   678
rosfran@30
   679
error:
rosfran@30
   680
rosfran@30
   681
  return FALSE;
rosfran@30
   682
}
leo_sobral@2
   683
#endif
leo_sobral@2
   684
rosfran@30
   685
static gboolean
leo_sobral@2
   686
gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
leo_sobral@2
   687
{
rosfran@31
   688
  GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
rosfran@30
   689
  gboolean ret = TRUE;
leo_sobral@2
   690
rosfran@30
   691
  if (src->content_size <= 0) {
rosfran@30
   692
    ret= FALSE;
rosfran@34
   693
  } else if ( abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) {
rosfran@34
   694
    //g_static_mutex_lock( &update_size_mutex );
rosfran@37
   695
    GST_OBJECT_LOCK(src);
rosfran@30
   696
rosfran@31
   697
  	guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer );
rosfran@31
   698
  	if ( src->content_size < new_offset ) {
rosfran@31
   699
  	  src->content_size = new_offset;
rosfran@31
   700
  	}
rosfran@31
   701
 
leo_sobral@2
   702
#if ENABLE_TIMING_POSITION == 1
rosfran@30
   703
    guint64 size_tmp = 0;
rosfran@30
   704
    if (src->live_tv == TRUE) {
leo_sobral@2
   705
get_file_pos:
rosfran@31
   706
      g_usleep( 5 );
rosfran@30
   707
      size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
rosfran@30
   708
      if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@30
   709
	src->content_size = size_tmp;
rosfran@30
   710
      else
rosfran@30
   711
	goto get_file_pos;
rosfran@30
   712
      g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
rosfran@30
   713
	  __FUNCTION__, size_tmp );
rosfran@30
   714
    }
rosfran@30
   715
#endif
rosfran@37
   716
	GST_OBJECT_UNLOCK(src);
rosfran@34
   717
    //g_static_mutex_unlock( &update_size_mutex );
rosfran@30
   718
leo_sobral@2
   719
  }
leo_sobral@2
   720
leo_sobral@2
   721
  *size = src->content_size;
rosfran@30
   722
  g_print( "[%s] Content size = %llu\n", __FUNCTION__, src->content_size );
leo_sobral@2
   723
rosfran@30
   724
  return ret;
rosfran@30
   725
leo_sobral@2
   726
}
leo_sobral@2
   727
leo_sobral@2
   728
/* close the socket and associated resources
leo_sobral@2
   729
 * used both to recover from errors and go to NULL state */
leo_sobral@2
   730
  static gboolean
leo_sobral@2
   731
gst_mythtv_src_stop (GstBaseSrc * bsrc)
leo_sobral@2
   732
{
leo_sobral@2
   733
  GstMythtvSrc *src;
leo_sobral@2
   734
leo_sobral@2
   735
  src = GST_MYTHTV_SRC (bsrc);
leo_sobral@2
   736
leo_sobral@2
   737
  if (src->uri_name) {
leo_sobral@2
   738
    g_free (src->uri_name);
leo_sobral@2
   739
    src->uri_name = NULL;
leo_sobral@2
   740
  }
leo_sobral@2
   741
leo_sobral@2
   742
  if (src->mythtv_caps) {
leo_sobral@2
   743
    gst_caps_unref (src->mythtv_caps);
leo_sobral@2
   744
    src->mythtv_caps = NULL;
leo_sobral@2
   745
  }
leo_sobral@2
   746
leo_sobral@2
   747
  src->eos = FALSE;
leo_sobral@2
   748
leo_sobral@2
   749
  return TRUE;
leo_sobral@2
   750
}
leo_sobral@2
   751
rosfran@30
   752
static gboolean
leo_sobral@2
   753
gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
leo_sobral@2
   754
{
leo_sobral@2
   755
  GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
leo_sobral@2
   756
leo_sobral@2
   757
  switch (GST_EVENT_TYPE (event)) {
rosfran@30
   758
#if 0
leo_sobral@2
   759
    case GST_EVENT_FLUSH_START:
leo_sobral@2
   760
      src->eos = FALSE;
leo_sobral@2
   761
      break;
leo_sobral@2
   762
      //return TRUE;
leo_sobral@2
   763
    case GST_EVENT_FLUSH_STOP:
leo_sobral@2
   764
      src->do_start = TRUE;
leo_sobral@2
   765
      src->eos = FALSE;
leo_sobral@2
   766
      gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
rosfran@30
   767
      //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
rosfran@30
   768
      break;
rosfran@30
   769
#endif
rosfran@30
   770
    case GST_EVENT_EOS:
rosfran@30
   771
      g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
rosfran@31
   772
	  guint64 cont_size = gst_mythtv_src_get_position (src);
rosfran@34
   773
	  if ( !src->live_tv ) {
rosfran@34
   774
		  if ( cont_size > src->content_size ) {
rosfran@34
   775
		  	src->content_size = cont_size;
rosfran@34
   776
		  	src->eos = FALSE;
rosfran@34
   777
	  	  } else {
rosfran@34
   778
	      	src->eos = TRUE;
rosfran@34
   779
	      	gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
rosfran@34
   780
	      	gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
rosfran@34
   781
	  	  }
rosfran@34
   782
	  } else 
rosfran@34
   783
	  	src->eos = TRUE;
rosfran@30
   784
      break;
rosfran@30
   785
#if 0
rosfran@30
   786
    case GST_EVENT_NEWSEGMENT:
rosfran@30
   787
      g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
rosfran@30
   788
      src->eos = FALSE;
leo_sobral@2
   789
      break;
leo_sobral@2
   790
    case GST_EVENT_SEEK:  	  
leo_sobral@2
   791
      {
rosfran@30
   792
	g_print( "[%s] Got EVENT_SEEK!!!\n", __FUNCTION__ );
leo_sobral@2
   793
	gdouble rate;
leo_sobral@2
   794
	//gboolean update = TRUE;
leo_sobral@2
   795
	GstFormat format;
leo_sobral@2
   796
	GstSeekType cur_type, stop_type;
leo_sobral@2
   797
	GstSeekFlags flags;
leo_sobral@2
   798
	gint64 cur = 0, stop = 0;
leo_sobral@2
   799
	gst_event_parse_seek ( event, &rate, &format,
leo_sobral@2
   800
	    &flags, &cur_type, &cur,
leo_sobral@2
   801
	    &stop_type, &stop );
leo_sobral@2
   802
leo_sobral@2
   803
	g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
leo_sobral@2
   804
	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
leo_sobral@2
   805
	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
leo_sobral@2
   806
	}
leo_sobral@2
   807
	//gboolean ret = gst_event_parse_new_segment ( event,
leo_sobral@2
   808
	//    &update, &rate, &format, &start, &stop,
leo_sobral@2
   809
	//    &position );
leo_sobral@2
   810
	//GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ), 
leo_sobral@2
   811
	//			cur, stop - cur + 1, GstBuffer)
rosfran@30
   812
      }
rosfran@30
   813
#endif
leo_sobral@2
   814
    default:
leo_sobral@2
   815
      return gst_pad_event_default (pad, event);
leo_sobral@2
   816
  }
leo_sobral@2
   817
leo_sobral@2
   818
  return gst_pad_event_default (pad, event);
leo_sobral@2
   819
}
leo_sobral@2
   820
rosfran@30
   821
static gboolean
rosfran@30
   822
gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
leo_sobral@2
   823
{
leo_sobral@2
   824
  return TRUE;
leo_sobral@2
   825
}
leo_sobral@2
   826
rosfran@30
   827
static void
leo_sobral@2
   828
gst_mythtv_src_set_property (GObject * object, guint prop_id,
leo_sobral@2
   829
    const GValue * value, GParamSpec * pspec)
leo_sobral@2
   830
{
leo_sobral@2
   831
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
   832
leo_sobral@2
   833
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
   834
  switch (prop_id) {
leo_sobral@2
   835
    case PROP_URI:
leo_sobral@2
   836
    case PROP_LOCATION:
leo_sobral@2
   837
      {
leo_sobral@2
   838
	if (!g_value_get_string (value)) {
leo_sobral@2
   839
	  GST_WARNING ("location property cannot be NULL");
leo_sobral@2
   840
	  goto done;
leo_sobral@2
   841
	}
leo_sobral@2
   842
leo_sobral@2
   843
	if (mythtvsrc->uri_name != NULL) {
leo_sobral@2
   844
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
   845
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
   846
	}
leo_sobral@2
   847
	mythtvsrc->uri_name = g_value_dup_string (value);
leo_sobral@2
   848
leo_sobral@2
   849
	break;
leo_sobral@2
   850
      }
leo_sobral@2
   851
#ifndef GST_DISABLE_GST_DEBUG
leo_sobral@2
   852
    case PROP_MYTHTV_DBG:
leo_sobral@2
   853
      {
leo_sobral@2
   854
	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
leo_sobral@2
   855
	break;
leo_sobral@2
   856
      }
leo_sobral@2
   857
#endif
leo_sobral@2
   858
    case PROP_MYTHTV_VERSION:
leo_sobral@2
   859
      {
leo_sobral@2
   860
	mythtvsrc->mythtv_version = g_value_get_int (value);
leo_sobral@2
   861
	break;
leo_sobral@2
   862
      }
leo_sobral@2
   863
    case PROP_MYTHTV_LIVEID:
leo_sobral@2
   864
      {
leo_sobral@2
   865
	mythtvsrc->live_tv_id = g_value_get_int (value);
leo_sobral@2
   866
	break;
leo_sobral@2
   867
      }
leo_sobral@2
   868
    case PROP_MYTHTV_LIVE:
leo_sobral@2
   869
      {
leo_sobral@2
   870
	mythtvsrc->live_tv = g_value_get_boolean (value);
leo_sobral@2
   871
	break;
leo_sobral@2
   872
      }
leo_sobral@2
   873
    case PROP_MYTHTV_LIVE_CHAINID:
leo_sobral@2
   874
      {
leo_sobral@2
   875
	if (!g_value_get_string (value)) {
leo_sobral@2
   876
	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
leo_sobral@2
   877
	  goto done;
leo_sobral@2
   878
	}
leo_sobral@2
   879
leo_sobral@2
   880
	if (mythtvsrc->live_chain_id != NULL) {
leo_sobral@2
   881
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
   882
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
   883
	}
leo_sobral@2
   884
	mythtvsrc->live_chain_id = g_value_dup_string (value);
leo_sobral@2
   885
leo_sobral@2
   886
	break;
leo_sobral@2
   887
      }
leo_sobral@2
   888
leo_sobral@2
   889
    default:
leo_sobral@2
   890
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
   891
      break;
leo_sobral@2
   892
  }
leo_sobral@2
   893
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
   894
done:
leo_sobral@2
   895
  return;
leo_sobral@2
   896
}
leo_sobral@2
   897
leo_sobral@2
   898
  static void
leo_sobral@2
   899
gst_mythtv_src_get_property (GObject * object, guint prop_id,
leo_sobral@2
   900
    GValue * value, GParamSpec * pspec)
leo_sobral@2
   901
{
leo_sobral@2
   902
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
   903
leo_sobral@2
   904
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
   905
  switch (prop_id) {
leo_sobral@2
   906
    case PROP_URI:
leo_sobral@2
   907
    case PROP_LOCATION:
leo_sobral@2
   908
      {
leo_sobral@2
   909
	gchar *str = g_strdup( "" );
leo_sobral@2
   910
leo_sobral@2
   911
	if ( mythtvsrc->uri_name == NULL ) {
leo_sobral@2
   912
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
   913
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
   914
	} else {
leo_sobral@2
   915
	  str = g_strdup( mythtvsrc->uri_name );
leo_sobral@2
   916
	}
leo_sobral@2
   917
	g_value_set_string ( value, str );
leo_sobral@2
   918
	break;
leo_sobral@2
   919
      }
leo_sobral@2
   920
#ifndef GST_DISABLE_GST_DEBUG
leo_sobral@2
   921
    case PROP_MYTHTV_DBG:
leo_sobral@2
   922
      g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
leo_sobral@2
   923
      break;
leo_sobral@2
   924
#endif
leo_sobral@2
   925
    case PROP_MYTHTV_VERSION:
leo_sobral@2
   926
      {
leo_sobral@2
   927
	g_value_set_int ( value, mythtvsrc->mythtv_version );
leo_sobral@2
   928
	break;
leo_sobral@2
   929
      }
leo_sobral@2
   930
    case PROP_MYTHTV_LIVEID:
leo_sobral@2
   931
      {
leo_sobral@2
   932
	g_value_set_int ( value, mythtvsrc->live_tv_id );
leo_sobral@2
   933
	break;
leo_sobral@2
   934
      }
leo_sobral@2
   935
    case PROP_MYTHTV_LIVE:
leo_sobral@2
   936
      g_value_set_boolean ( value, mythtvsrc->live_tv );
leo_sobral@2
   937
      break;
leo_sobral@2
   938
    case PROP_MYTHTV_LIVE_CHAINID:
leo_sobral@2
   939
      {
leo_sobral@2
   940
	gchar *str = g_strdup( "" );
leo_sobral@2
   941
leo_sobral@2
   942
	if ( mythtvsrc->live_chain_id == NULL ) {
leo_sobral@2
   943
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
   944
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
   945
	} else {
leo_sobral@2
   946
	  str = g_strdup( mythtvsrc->live_chain_id );
leo_sobral@2
   947
	}
leo_sobral@2
   948
	g_value_set_string ( value, str );
leo_sobral@2
   949
	break;
leo_sobral@2
   950
      }
leo_sobral@2
   951
    default:
leo_sobral@2
   952
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
   953
      break;
leo_sobral@2
   954
  }
leo_sobral@2
   955
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
   956
}
leo_sobral@2
   957
leo_sobral@2
   958
/* entry point to initialize the plug-in
leo_sobral@2
   959
 * initialize the plug-in itself
leo_sobral@2
   960
 * register the element factories and pad templates
leo_sobral@2
   961
 * register the features
leo_sobral@2
   962
 */
leo_sobral@2
   963
  static gboolean
leo_sobral@2
   964
plugin_init (GstPlugin * plugin)
leo_sobral@2
   965
{
leo_sobral@2
   966
  return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
leo_sobral@2
   967
      GST_TYPE_MYTHTV_SRC);
leo_sobral@2
   968
}
leo_sobral@2
   969
leo_sobral@2
   970
/* this is the structure that gst-register looks for
leo_sobral@2
   971
 * so keep the name plugin_desc, or you cannot get your plug-in registered */
leo_sobral@2
   972
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
leo_sobral@2
   973
    GST_VERSION_MINOR,
leo_sobral@2
   974
    "mythtv",
leo_sobral@2
   975
    "lib MythTV src",
leo_sobral@2
   976
    plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
leo_sobral@2
   977
leo_sobral@2
   978
leo_sobral@2
   979
/*** GSTURIHANDLER INTERFACE *************************************************/
leo_sobral@2
   980
  static guint 
leo_sobral@2
   981
gst_mythtv_src_uri_get_type (void)
leo_sobral@2
   982
{
leo_sobral@2
   983
  return GST_URI_SRC;
leo_sobral@2
   984
}
leo_sobral@2
   985
leo_sobral@2
   986
  static gchar **
leo_sobral@2
   987
gst_mythtv_src_uri_get_protocols (void)
leo_sobral@2
   988
{
leo_sobral@2
   989
  static gchar *protocols[] = { "myth", "myths", NULL };
leo_sobral@2
   990
leo_sobral@2
   991
  return protocols;
leo_sobral@2
   992
}
leo_sobral@2
   993
leo_sobral@2
   994
  static const gchar *
leo_sobral@2
   995
gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
leo_sobral@2
   996
{
leo_sobral@2
   997
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
   998
leo_sobral@2
   999
  return src->uri_name;
leo_sobral@2
  1000
}
leo_sobral@2
  1001
leo_sobral@2
  1002
  static gboolean
leo_sobral@2
  1003
gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
leo_sobral@2
  1004
{
leo_sobral@2
  1005
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
  1006
leo_sobral@2
  1007
  gchar *protocol;
leo_sobral@2
  1008
leo_sobral@2
  1009
  protocol = gst_uri_get_protocol (uri);
leo_sobral@2
  1010
  if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
leo_sobral@2
  1011
    g_free (protocol);
leo_sobral@2
  1012
    return FALSE;
leo_sobral@2
  1013
  }
leo_sobral@2
  1014
  g_free (protocol);
leo_sobral@2
  1015
  g_object_set (src, "location", uri, NULL);
leo_sobral@2
  1016
leo_sobral@2
  1017
  return TRUE;
leo_sobral@2
  1018
}
leo_sobral@2
  1019
leo_sobral@2
  1020
  static void
leo_sobral@2
  1021
gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
leo_sobral@2
  1022
{
leo_sobral@2
  1023
  GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
leo_sobral@2
  1024
leo_sobral@2
  1025
  iface->get_type = gst_mythtv_src_uri_get_type;
leo_sobral@2
  1026
  iface->get_protocols = gst_mythtv_src_uri_get_protocols;
leo_sobral@2
  1027
  iface->get_uri = gst_mythtv_src_uri_get_uri;
leo_sobral@2
  1028
  iface->set_uri = gst_mythtv_src_uri_set_uri;
leo_sobral@2
  1029
}
leo_sobral@2
  1030
leo_sobral@2
  1031
  void
leo_sobral@2
  1032
size_header_handler (void *userdata, const char *value)
leo_sobral@2
  1033
{
leo_sobral@2
  1034
  GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
leo_sobral@2
  1035
leo_sobral@2
  1036
  //src->content_size = g_ascii_strtoull (value, NULL, 10);
leo_sobral@2
  1037
leo_sobral@2
  1038
  GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
leo_sobral@2
  1039
}