gst-plugins-mythtv/src/gstmythtvsrc.c
author rosfran
Wed Oct 25 00:26:33 2006 +0100 (2006-10-25)
branchtrunk
changeset 51 3e490ac963ce
parent 40 ba5dc9bff3a1
child 52 67e72eadeef2
permissions -rwxr-xr-x
[svn r52] Added reference to the GSTINTERFACES_LIBS (gst_x_overlay* functions).
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@40
    21
#include "gmyth_file_transfer.h"
rosfran@40
    22
#include "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@40
    35
#define GMYTHTV_VERSION_DEFAULT		30
leo_sobral@2
    36
rosfran@40
    37
#define GMYTHTV_TRANSFER_MAX_WAITS	100
leo_sobral@2
    38
rosfran@40
    39
#define GMYTHTV_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
rosfran@40
    73
  PROP_GMYTHTV_DBG,
leo_sobral@2
    74
#endif
rosfran@40
    75
  PROP_GMYTHTV_VERSION,
rosfran@40
    76
  PROP_GMYTHTV_LIVE,
rosfran@40
    77
  PROP_GMYTHTV_LIVEID,
rosfran@40
    78
  PROP_GMYTHTV_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
rosfran@40
   171
    (gobject_class, PROP_GMYTHTV_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
rosfran@40
   177
    (gobject_class, PROP_GMYTHTV_LIVEID,
leo_sobral@2
   178
     g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
leo_sobral@2
   179
       "Change Myth TV version",
rosfran@40
   180
       0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
leo_sobral@2
   181
leo_sobral@2
   182
  g_object_class_install_property
rosfran@40
   183
    (gobject_class, PROP_GMYTHTV_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
rosfran@40
   189
    (gobject_class, PROP_GMYTHTV_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
rosfran@40
   196
    (gobject_class, PROP_GMYTHTV_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
rosfran@40
   221
  this->mythtv_version = GMYTHTV_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@40
   296
  	len = gmyth_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@44
   306
      } else if ( len == 0 ) {
rosfran@44
   307
      	src->update_prog_chain = TRUE;
rosfran@44
   308
      	goto done;
rosfran@34
   309
      } else if ( /*src->content_size >= src->read_offset && 
rosfran@34
   310
      			abs ( src->content_size - src->read_offset ) <= 1024 ) ||*/
rosfran@40
   311
      			( src->content_size <= ( src->read_offset + size + GMYTHTV_TRANSFER_MAX_BUFFER ) ) )
rosfran@34
   312
  {
rosfran@34
   313
#if ENABLE_TIMING_POSITION == 1
rosfran@40
   314
    gint64 size_tmp = 0;
rosfran@34
   315
    if (src->live_tv == TRUE) {
rosfran@34
   316
get_file_pos:
rosfran@40
   317
      size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@40
   318
      if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@34
   319
	src->content_size = size_tmp;
rosfran@40
   320
      else if ( size_tmp > 0 )
rosfran@34
   321
	goto get_file_pos;
rosfran@40
   322
      g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
rosfran@34
   323
	  __FUNCTION__, size_tmp );
rosfran@34
   324
    }
rosfran@34
   325
#else
rosfran@40
   326
  	gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@40
   327
	if ( new_offset > 0 ) {
rosfran@40
   328
	  if ( src->content_size < new_offset ) {
rosfran@40
   329
	    src->content_size = new_offset;
rosfran@40
   330
	  }
rosfran@40
   331
	} else {
rosfran@40
   332
	  src->update_prog_chain = TRUE;
rosfran@40
   333
	}
rosfran@34
   334
#endif
rosfran@34
   335
	goto done;
rosfran@34
   336
  }
rosfran@30
   337
leo_sobral@2
   338
    }
rosfran@31
   339
    
rosfran@30
   340
    if ( read == sizetoread )
leo_sobral@2
   341
      break;
leo_sobral@2
   342
  }
rosfran@30
   343
  
leo_sobral@2
   344
  if ( read > 0 ) {
rosfran@34
   345
  	src->read_offset += read;
leo_sobral@2
   346
    src->bytes_read += read;
rosfran@31
   347
  
leo_sobral@2
   348
  g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
leo_sobral@2
   349
      "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
leo_sobral@2
   350
      src->read_offset, src->content_size );
rosfran@31
   351
      
rosfran@31
   352
  //GST_BUFFER_TIMESTAMP( buffer ) = GST_BUFFER_TIMESTAMP ( *outbuf );
rosfran@31
   353
  GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
rosfran@31
   354
  GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
rosfran@31
   355
  GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
rosfran@31
   356
  //memcpy( GST_BUFFER_DATA( *outbuf ), GST_BUFFER_DATA( buffer ), read );
rosfran@30
   357
     
rosfran@30
   358
  g_print( "Stopping: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
rosfran@30
   359
      "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
rosfran@30
   360
      GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
rosfran@31
   361
      
rosfran@34
   362
  } else if ( !src->live_tv )
rosfran@31
   363
  	goto eos;
rosfran@31
   364
  
rosfran@30
   365
  goto done;
leo_sobral@2
   366
leo_sobral@2
   367
eos:
leo_sobral@2
   368
  src->eos = TRUE;
rosfran@30
   369
  
leo_sobral@2
   370
done:
rosfran@37
   371
  GST_OBJECT_UNLOCK(src);
leo_sobral@2
   372
leo_sobral@2
   373
  return read;
leo_sobral@2
   374
}
leo_sobral@2
   375
rosfran@30
   376
static GstFlowReturn
rosfran@30
   377
gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
leo_sobral@2
   378
{
leo_sobral@2
   379
  GstMythtvSrc *src;
leo_sobral@2
   380
  GstFlowReturn ret = GST_FLOW_OK;
rosfran@37
   381
  gint read = -1;
rosfran@30
   382
  
rosfran@30
   383
  src = GST_MYTHTV_SRC (psrc);
rosfran@31
   384
  /* The caller should know the number of bytes and not read beyond EOS. */
rosfran@31
   385
  if (G_UNLIKELY (src->eos))
rosfran@31
   386
    goto eos;
rosfran@40
   387
  if ( G_UNLIKELY (src->update_prog_chain) )
rosfran@40
   388
    goto change_progchain;
rosfran@30
   389
  
rosfran@31
   390
  //GST_OBJECT_LOCK(src);
leo_sobral@2
   391
rosfran@30
   392
  if (G_UNLIKELY (src->read_offset != offset)) {
rosfran@40
   393
    gint64 new_offset = gmyth_file_transfer_seek(src->file_transfer, offset, SEEK_SET);
rosfran@40
   394
    g_print( "[%s] SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
rosfran@34
   395
    	__FUNCTION__, src->read_offset, new_offset );
rosfran@34
   396
    if (G_UNLIKELY (new_offset < 0 ) )//|| new_offset != src->read_offset)) {
rosfran@34
   397
    {
rosfran@31
   398
      //GST_OBJECT_UNLOCK(src);
rosfran@40
   399
      goto change_progchain;
rosfran@31
   400
    }
leo_sobral@2
   401
rosfran@30
   402
    src->read_offset = offset;
rosfran@30
   403
  }
rosfran@31
   404
  //GST_OBJECT_UNLOCK(src);
rosfran@30
   405
  
rosfran@31
   406
  /* Create the buffer. */
rosfran@31
   407
  ret = gst_pad_alloc_buffer (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
rosfran@31
   408
      src->read_offset, size,
rosfran@31
   409
      //src->icy_caps ? src->icy_caps :
rosfran@31
   410
      GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf);
leo_sobral@2
   411
rosfran@31
   412
  if (G_UNLIKELY (ret != GST_FLOW_OK))
rosfran@31
   413
    goto done;
rosfran@30
   414
  
rosfran@31
   415
  //g_static_mutex_lock( &update_size_mutex );
rosfran@31
   416
  read = do_read_request_response ( src, src->read_offset, size, outbuf );
rosfran@31
   417
  //g_static_mutex_unlock( &update_size_mutex );  
leo_sobral@2
   418
rosfran@40
   419
  if (G_UNLIKELY (src->update_prog_chain) )
rosfran@40
   420
    goto change_progchain;
rosfran@40
   421
rosfran@31
   422
  if (G_UNLIKELY (read < 0) || *outbuf == NULL) {
rosfran@31
   423
  	//if ( src->live_tv )
rosfran@31
   424
    //	goto done;
rosfran@31
   425
    //else
leo_sobral@2
   426
    goto read_error;
rosfran@31
   427
  }
leo_sobral@2
   428
rosfran@31
   429
/* 
leo_sobral@2
   430
  if (G_UNLIKELY(src->eos))
leo_sobral@2
   431
    goto eos;
rosfran@30
   432
  else
rosfran@30
   433
    goto done;
rosfran@31
   434
*/
leo_sobral@2
   435
leo_sobral@2
   436
done:
rosfran@31
   437
 {
rosfran@31
   438
    const gchar *reason = gst_flow_get_name (ret);
rosfran@31
   439
rosfran@31
   440
    GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
rosfran@31
   441
  	return ret;
rosfran@31
   442
 }
leo_sobral@2
   443
eos:
leo_sobral@2
   444
  {
rosfran@30
   445
    const gchar *reason = gst_flow_get_name (ret);
rosfran@30
   446
rosfran@30
   447
    GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
leo_sobral@2
   448
    return GST_FLOW_UNEXPECTED;
leo_sobral@2
   449
  }
leo_sobral@2
   450
  /* ERRORS */
leo_sobral@2
   451
read_error:
leo_sobral@2
   452
  {
leo_sobral@2
   453
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
leo_sobral@2
   454
	(NULL), ("Could not read any bytes (%i, %s)", read,
leo_sobral@2
   455
	  src->uri_name));
leo_sobral@2
   456
    return GST_FLOW_ERROR;
leo_sobral@2
   457
  }
rosfran@40
   458
change_progchain:
rosfran@37
   459
  {
rosfran@37
   460
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@37
   461
	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
rosfran@37
   462
	  src->uri_name));
rosfran@37
   463
	// go to the next program chain
rosfran@37
   464
	src->unique_setup = FALSE;
rosfran@37
   465
	gst_mythtv_src_start( psrc );
rosfran@37
   466
  
rosfran@37
   467
    return GST_FLOW_OK;
rosfran@37
   468
  }
rosfran@30
   469
leo_sobral@2
   470
}
leo_sobral@2
   471
rosfran@30
   472
void
rosfran@30
   473
update_size_func( void *mythtv_data ) 
leo_sobral@2
   474
{
rosfran@30
   475
  GstMythtvSrc *src;
leo_sobral@2
   476
rosfran@30
   477
  g_return_if_fail( mythtv_data != NULL );
leo_sobral@2
   478
rosfran@30
   479
  src = GST_MYTHTV_SRC ( mythtv_data );
rosfran@30
   480
rosfran@30
   481
  g_static_mutex_lock( &update_size_mutex );
rosfran@30
   482
rosfran@30
   483
  if ( src->do_start ) {
rosfran@30
   484
#if ENABLE_TIMING_POSITION == 1
rosfran@40
   485
    gint64 size_tmp = 0;
rosfran@30
   486
    if (src->live_tv == TRUE) {
rosfran@30
   487
get_file_pos:
rosfran@30
   488
      g_usleep( 50 );
rosfran@40
   489
      size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@40
   490
      if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@30
   491
	src->content_size = size_tmp;
rosfran@40
   492
      else if ( size_tmp > 0 )
rosfran@30
   493
	goto get_file_pos;
rosfran@40
   494
      g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
rosfran@30
   495
	  __FUNCTION__, size_tmp );
rosfran@30
   496
    }
rosfran@30
   497
#endif
leo_sobral@2
   498
  }
rosfran@30
   499
  g_static_mutex_unlock( &update_size_mutex );
leo_sobral@2
   500
leo_sobral@2
   501
}
leo_sobral@2
   502
rosfran@30
   503
guint64
rosfran@30
   504
gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
leo_sobral@2
   505
{
rosfran@30
   506
rosfran@30
   507
  if ( src->do_start ) {
rosfran@30
   508
#if ENABLE_TIMING_POSITION == 1
rosfran@40
   509
    gint64 size_tmp = 0;
rosfran@30
   510
    if (src->live_tv == TRUE) {
rosfran@30
   511
get_file_pos:
rosfran@30
   512
      g_usleep( 50 );
rosfran@40
   513
      size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@40
   514
      if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@30
   515
		src->content_size = size_tmp;
rosfran@40
   516
      else if ( size_tmp > 0 )
rosfran@30
   517
	goto get_file_pos;
rosfran@40
   518
      g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
rosfran@30
   519
	  __FUNCTION__, size_tmp );
rosfran@30
   520
    }
rosfran@30
   521
#endif
rosfran@30
   522
  }
rosfran@30
   523
  
rosfran@30
   524
  return src->content_size;	
rosfran@30
   525
leo_sobral@2
   526
}
leo_sobral@2
   527
leo_sobral@2
   528
/* create a socket for connecting to remote server */
rosfran@30
   529
static gboolean
leo_sobral@2
   530
gst_mythtv_src_start ( GstBaseSrc * bsrc )
leo_sobral@2
   531
{
leo_sobral@2
   532
  GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
leo_sobral@2
   533
leo_sobral@2
   534
  GString *chain_id_local = NULL;
leo_sobral@2
   535
leo_sobral@2
   536
  gboolean ret = TRUE;
rosfran@30
   537
leo_sobral@2
   538
  if (src->unique_setup == FALSE) {
leo_sobral@2
   539
    src->unique_setup = TRUE;
leo_sobral@2
   540
  } else {
leo_sobral@2
   541
    goto done;
leo_sobral@2
   542
  }
leo_sobral@2
   543
leo_sobral@2
   544
  GST_OBJECT_LOCK(src);
leo_sobral@2
   545
leo_sobral@2
   546
  if ( src->live_tv ) {
rosfran@40
   547
    src->spawn_livetv = gmyth_livetv_new( );
rosfran@40
   548
    if ( gmyth_livetv_setup( src->spawn_livetv ) == FALSE ) {
rosfran@30
   549
      ret = FALSE;
rosfran@30
   550
      goto init_failed;
leo_sobral@2
   551
    }
leo_sobral@2
   552
    /* set up the uri variable */
leo_sobral@2
   553
    src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
leo_sobral@2
   554
    chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
leo_sobral@2
   555
    if ( chain_id_local != NULL ) {
leo_sobral@2
   556
      src->live_chain_id = g_strdup( chain_id_local->str );
leo_sobral@2
   557
      g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
leo_sobral@2
   558
    }
leo_sobral@2
   559
    src->live_tv_id = src->spawn_livetv->remote_encoder->recorder_num;
leo_sobral@2
   560
    g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
leo_sobral@2
   561
  }
leo_sobral@2
   562
rosfran@40
   563
  src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
leo_sobral@2
   564
      g_string_new( src->uri_name ), -1, src->mythtv_version );
leo_sobral@2
   565
leo_sobral@2
   566
  if ( src->file_transfer == NULL ) {
leo_sobral@2
   567
    GST_OBJECT_UNLOCK(src);
leo_sobral@2
   568
leo_sobral@2
   569
    goto init_failed;
leo_sobral@2
   570
  }
leo_sobral@2
   571
leo_sobral@2
   572
  /* sets the Playback monitor connection */
rosfran@40
   573
  ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
leo_sobral@2
   574
leo_sobral@2
   575
  if ( src->live_tv == TRUE && ret == TRUE ) {
leo_sobral@2
   576
    /* loop finished, set the max tries variable to zero again... */
leo_sobral@2
   577
    wait_to_transfer = 0;
leo_sobral@2
   578
rosfran@40
   579
    while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && ( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE 
rosfran@40
   580
	  /*|| ( gmyth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
leo_sobral@2
   581
      g_usleep( 100 );
leo_sobral@2
   582
  }
leo_sobral@2
   583
leo_sobral@2
   584
  /* sets the FileTransfer instance connection (video/audio download) */
rosfran@40
   585
  ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
leo_sobral@2
   586
leo_sobral@2
   587
  if ( ret == FALSE ) {
leo_sobral@2
   588
    GST_OBJECT_UNLOCK(src);
leo_sobral@2
   589
#ifndef GST_DISABLE_GST_DEBUG  
leo_sobral@2
   590
    if ( src->mythtv_msgs_dbg )
leo_sobral@2
   591
      g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
leo_sobral@2
   592
#endif
leo_sobral@2
   593
    goto begin_req_failed;
leo_sobral@2
   594
  }
leo_sobral@2
   595
leo_sobral@2
   596
  src->content_size = src->file_transfer->filesize;
leo_sobral@2
   597
leo_sobral@2
   598
  GST_OBJECT_UNLOCK(src);
rosfran@30
   599
  if ( src->live_tv ) {
rosfran@30
   600
rosfran@30
   601
    //GError* error;
rosfran@30
   602
    //update_size_task = g_thread_create( (GThreadFunc)update_size_func, src, FALSE, &error );
rosfran@30
   603
    g_print( "[%s] Update Size task = %s\n", __FUNCTION__, update_size_task != NULL ?  "OK !" : "ERROR!!!" );
rosfran@30
   604
rosfran@30
   605
  }
rosfran@30
   606
  src->do_start = TRUE;  
rosfran@30
   607
rosfran@30
   608
done:
rosfran@30
   609
  return TRUE;
rosfran@30
   610
rosfran@30
   611
  /* ERRORS */
rosfran@30
   612
init_failed:
rosfran@30
   613
  {
rosfran@30
   614
    if (src->spawn_livetv != NULL )
rosfran@30
   615
      g_object_unref( src->spawn_livetv );
rosfran@30
   616
rosfran@30
   617
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@30
   618
	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
rosfran@30
   619
    return FALSE;
rosfran@30
   620
  }
rosfran@30
   621
begin_req_failed:
rosfran@30
   622
  {
rosfran@30
   623
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@30
   624
	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
rosfran@30
   625
    return FALSE;
rosfran@30
   626
  }
rosfran@30
   627
}
leo_sobral@2
   628
leo_sobral@2
   629
#if 0
rosfran@30
   630
/* handles queries for location in the stream in the requested format */
rosfran@30
   631
static gboolean
rosfran@30
   632
gst_mythtv_src_query ( GstPad * pad, GstQuery * query )
rosfran@30
   633
{
rosfran@30
   634
  gboolean res = TRUE;
rosfran@30
   635
  GstMythtvSrc *mythtv;
leo_sobral@2
   636
rosfran@30
   637
  guint64 size = 0;
rosfran@30
   638
rosfran@40
   639
  mythtv = GST_GMYTHTV_SRC( GST_PAD_PARENT (pad) );
rosfran@30
   640
rosfran@30
   641
  size = gst_mythtv_src_get_position (mythtv);
rosfran@30
   642
rosfran@30
   643
  switch (GST_QUERY_TYPE (query)) {
rosfran@30
   644
rosfran@30
   645
    case GST_QUERY_POSITION:
rosfran@30
   646
      {
rosfran@30
   647
rosfran@30
   648
	//GstFormat format;
rosfran@30
   649
	gint64 cur = 0;
rosfran@30
   650
rosfran@30
   651
	/* save requested format */
rosfran@30
   652
	gst_query_parse_position (query, NULL, &cur);
rosfran@30
   653
rosfran@30
   654
	/* query peer for current position in time */
rosfran@30
   655
	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
rosfran@30
   656
	    ( size > cur ) ? "greater" : "lower", size, cur );
rosfran@30
   657
	gst_query_set_position (query, GST_FORMAT_BYTES, size);
rosfran@30
   658
	if ( size < cur )
rosfran@30
   659
	  goto error;
rosfran@30
   660
rosfran@30
   661
	break;
leo_sobral@2
   662
      }
rosfran@30
   663
     #if 0
rosfran@30
   664
    case GST_QUERY_DURATION:
rosfran@30
   665
      {
rosfran@30
   666
	//GstFormat format;
rosfran@30
   667
	gint64 cur = 0;
rosfran@30
   668
rosfran@30
   669
	/* save requested format */
rosfran@30
   670
	gst_query_parse_position (query, NULL, &cur);
rosfran@30
   671
rosfran@30
   672
	/* query peer for current position in time */
rosfran@30
   673
	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
rosfran@30
   674
	    ( size * GST_SECOND > cur * GST_SECOND ) ? "greater" : "lower", size * GST_SECOND, 
rosfran@30
   675
	    cur * GST_SECOND );
rosfran@30
   676
	gst_query_set_position (query, GST_FORMAT_TIME, size * GST_SECOND );
rosfran@30
   677
rosfran@30
   678
	if ( size * GST_SECOND < cur * GST_SECOND )
rosfran@30
   679
	  goto error;
rosfran@30
   680
rosfran@30
   681
	break;
rosfran@30
   682
      }
rosfran@30
   683
	#endif
rosfran@30
   684
    default:
rosfran@30
   685
      res = FALSE;
rosfran@30
   686
      break;
rosfran@30
   687
  }
rosfran@30
   688
rosfran@30
   689
  return res;
rosfran@30
   690
rosfran@30
   691
error:
rosfran@30
   692
rosfran@30
   693
  return FALSE;
rosfran@30
   694
}
leo_sobral@2
   695
#endif
leo_sobral@2
   696
rosfran@30
   697
static gboolean
leo_sobral@2
   698
gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
leo_sobral@2
   699
{
rosfran@31
   700
  GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
rosfran@30
   701
  gboolean ret = TRUE;
leo_sobral@2
   702
rosfran@30
   703
  if (src->content_size <= 0) {
rosfran@30
   704
    ret= FALSE;
rosfran@40
   705
  } else if ( abs ( src->content_size - src->read_offset ) <= GMYTHTV_TRANSFER_MAX_BUFFER ) {
rosfran@34
   706
    //g_static_mutex_lock( &update_size_mutex );
rosfran@37
   707
    GST_OBJECT_LOCK(src);
rosfran@30
   708
rosfran@40
   709
  	gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@40
   710
	if ( new_offset > 0 ) {
rosfran@40
   711
	  if ( src->content_size < new_offset ) {
rosfran@40
   712
	    src->content_size = new_offset;
rosfran@40
   713
	  }
rosfran@40
   714
	} else {
rosfran@40
   715
	  src->update_prog_chain = TRUE;
rosfran@40
   716
	  src->content_size = 0;
rosfran@40
   717
	}
rosfran@31
   718
 
leo_sobral@2
   719
#if ENABLE_TIMING_POSITION == 1
rosfran@40
   720
    gint64 size_tmp = 0;
rosfran@30
   721
    if (src->live_tv == TRUE) {
leo_sobral@2
   722
get_file_pos:
rosfran@31
   723
      g_usleep( 5 );
rosfran@40
   724
      size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@40
   725
      if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@30
   726
	src->content_size = size_tmp;
rosfran@40
   727
      else if ( size_tmp > 0  )
rosfran@30
   728
	goto get_file_pos;
rosfran@40
   729
      g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
rosfran@30
   730
	  __FUNCTION__, size_tmp );
rosfran@30
   731
    }
rosfran@30
   732
#endif
rosfran@37
   733
	GST_OBJECT_UNLOCK(src);
rosfran@34
   734
    //g_static_mutex_unlock( &update_size_mutex );
rosfran@30
   735
leo_sobral@2
   736
  }
leo_sobral@2
   737
leo_sobral@2
   738
  *size = src->content_size;
rosfran@30
   739
  g_print( "[%s] Content size = %llu\n", __FUNCTION__, src->content_size );
leo_sobral@2
   740
rosfran@30
   741
  return ret;
rosfran@30
   742
leo_sobral@2
   743
}
leo_sobral@2
   744
leo_sobral@2
   745
/* close the socket and associated resources
leo_sobral@2
   746
 * used both to recover from errors and go to NULL state */
leo_sobral@2
   747
  static gboolean
leo_sobral@2
   748
gst_mythtv_src_stop (GstBaseSrc * bsrc)
leo_sobral@2
   749
{
leo_sobral@2
   750
  GstMythtvSrc *src;
leo_sobral@2
   751
leo_sobral@2
   752
  src = GST_MYTHTV_SRC (bsrc);
leo_sobral@2
   753
leo_sobral@2
   754
  if (src->uri_name) {
leo_sobral@2
   755
    g_free (src->uri_name);
leo_sobral@2
   756
    src->uri_name = NULL;
leo_sobral@2
   757
  }
leo_sobral@2
   758
leo_sobral@2
   759
  if (src->mythtv_caps) {
leo_sobral@2
   760
    gst_caps_unref (src->mythtv_caps);
leo_sobral@2
   761
    src->mythtv_caps = NULL;
leo_sobral@2
   762
  }
leo_sobral@2
   763
leo_sobral@2
   764
  src->eos = FALSE;
leo_sobral@2
   765
leo_sobral@2
   766
  return TRUE;
leo_sobral@2
   767
}
leo_sobral@2
   768
rosfran@30
   769
static gboolean
leo_sobral@2
   770
gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
leo_sobral@2
   771
{
leo_sobral@2
   772
  GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
leo_sobral@2
   773
leo_sobral@2
   774
  switch (GST_EVENT_TYPE (event)) {
rosfran@30
   775
#if 0
leo_sobral@2
   776
    case GST_EVENT_FLUSH_START:
leo_sobral@2
   777
      src->eos = FALSE;
leo_sobral@2
   778
      break;
leo_sobral@2
   779
      //return TRUE;
leo_sobral@2
   780
    case GST_EVENT_FLUSH_STOP:
leo_sobral@2
   781
      src->do_start = TRUE;
leo_sobral@2
   782
      src->eos = FALSE;
leo_sobral@2
   783
      gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
rosfran@30
   784
      //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
rosfran@30
   785
      break;
rosfran@30
   786
#endif
rosfran@30
   787
    case GST_EVENT_EOS:
rosfran@30
   788
      g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
rosfran@31
   789
	  guint64 cont_size = gst_mythtv_src_get_position (src);
rosfran@34
   790
	  if ( !src->live_tv ) {
rosfran@34
   791
		  if ( cont_size > src->content_size ) {
rosfran@34
   792
		  	src->content_size = cont_size;
rosfran@34
   793
		  	src->eos = FALSE;
rosfran@34
   794
	  	  } else {
rosfran@34
   795
	      	src->eos = TRUE;
rosfran@34
   796
	      	gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
rosfran@34
   797
	      	gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
rosfran@34
   798
	  	  }
rosfran@34
   799
	  } else 
rosfran@34
   800
	  	src->eos = TRUE;
rosfran@30
   801
      break;
rosfran@30
   802
#if 0
rosfran@30
   803
    case GST_EVENT_NEWSEGMENT:
rosfran@30
   804
      g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
rosfran@30
   805
      src->eos = FALSE;
leo_sobral@2
   806
      break;
leo_sobral@2
   807
    case GST_EVENT_SEEK:  	  
leo_sobral@2
   808
      {
rosfran@30
   809
	g_print( "[%s] Got EVENT_SEEK!!!\n", __FUNCTION__ );
leo_sobral@2
   810
	gdouble rate;
leo_sobral@2
   811
	//gboolean update = TRUE;
leo_sobral@2
   812
	GstFormat format;
leo_sobral@2
   813
	GstSeekType cur_type, stop_type;
leo_sobral@2
   814
	GstSeekFlags flags;
leo_sobral@2
   815
	gint64 cur = 0, stop = 0;
leo_sobral@2
   816
	gst_event_parse_seek ( event, &rate, &format,
leo_sobral@2
   817
	    &flags, &cur_type, &cur,
leo_sobral@2
   818
	    &stop_type, &stop );
leo_sobral@2
   819
leo_sobral@2
   820
	g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
leo_sobral@2
   821
	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
leo_sobral@2
   822
	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
leo_sobral@2
   823
	}
leo_sobral@2
   824
	//gboolean ret = gst_event_parse_new_segment ( event,
leo_sobral@2
   825
	//    &update, &rate, &format, &start, &stop,
leo_sobral@2
   826
	//    &position );
leo_sobral@2
   827
	//GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ), 
leo_sobral@2
   828
	//			cur, stop - cur + 1, GstBuffer)
rosfran@30
   829
      }
rosfran@30
   830
#endif
leo_sobral@2
   831
    default:
leo_sobral@2
   832
      return gst_pad_event_default (pad, event);
leo_sobral@2
   833
  }
leo_sobral@2
   834
leo_sobral@2
   835
  return gst_pad_event_default (pad, event);
leo_sobral@2
   836
}
leo_sobral@2
   837
rosfran@30
   838
static gboolean
rosfran@30
   839
gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
leo_sobral@2
   840
{
leo_sobral@2
   841
  return TRUE;
leo_sobral@2
   842
}
leo_sobral@2
   843
rosfran@30
   844
static void
leo_sobral@2
   845
gst_mythtv_src_set_property (GObject * object, guint prop_id,
leo_sobral@2
   846
    const GValue * value, GParamSpec * pspec)
leo_sobral@2
   847
{
leo_sobral@2
   848
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
   849
leo_sobral@2
   850
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
   851
  switch (prop_id) {
leo_sobral@2
   852
    case PROP_URI:
leo_sobral@2
   853
    case PROP_LOCATION:
leo_sobral@2
   854
      {
leo_sobral@2
   855
	if (!g_value_get_string (value)) {
leo_sobral@2
   856
	  GST_WARNING ("location property cannot be NULL");
leo_sobral@2
   857
	  goto done;
leo_sobral@2
   858
	}
leo_sobral@2
   859
leo_sobral@2
   860
	if (mythtvsrc->uri_name != NULL) {
leo_sobral@2
   861
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
   862
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
   863
	}
leo_sobral@2
   864
	mythtvsrc->uri_name = g_value_dup_string (value);
leo_sobral@2
   865
leo_sobral@2
   866
	break;
leo_sobral@2
   867
      }
leo_sobral@2
   868
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
   869
    case PROP_GMYTHTV_DBG:
leo_sobral@2
   870
      {
leo_sobral@2
   871
	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
leo_sobral@2
   872
	break;
leo_sobral@2
   873
      }
leo_sobral@2
   874
#endif
rosfran@40
   875
    case PROP_GMYTHTV_VERSION:
leo_sobral@2
   876
      {
leo_sobral@2
   877
	mythtvsrc->mythtv_version = g_value_get_int (value);
leo_sobral@2
   878
	break;
leo_sobral@2
   879
      }
rosfran@40
   880
    case PROP_GMYTHTV_LIVEID:
leo_sobral@2
   881
      {
leo_sobral@2
   882
	mythtvsrc->live_tv_id = g_value_get_int (value);
leo_sobral@2
   883
	break;
leo_sobral@2
   884
      }
rosfran@40
   885
    case PROP_GMYTHTV_LIVE:
leo_sobral@2
   886
      {
leo_sobral@2
   887
	mythtvsrc->live_tv = g_value_get_boolean (value);
leo_sobral@2
   888
	break;
leo_sobral@2
   889
      }
rosfran@40
   890
    case PROP_GMYTHTV_LIVE_CHAINID:
leo_sobral@2
   891
      {
leo_sobral@2
   892
	if (!g_value_get_string (value)) {
leo_sobral@2
   893
	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
leo_sobral@2
   894
	  goto done;
leo_sobral@2
   895
	}
leo_sobral@2
   896
leo_sobral@2
   897
	if (mythtvsrc->live_chain_id != NULL) {
leo_sobral@2
   898
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
   899
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
   900
	}
leo_sobral@2
   901
	mythtvsrc->live_chain_id = g_value_dup_string (value);
leo_sobral@2
   902
leo_sobral@2
   903
	break;
leo_sobral@2
   904
      }
leo_sobral@2
   905
leo_sobral@2
   906
    default:
leo_sobral@2
   907
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
   908
      break;
leo_sobral@2
   909
  }
leo_sobral@2
   910
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
   911
done:
leo_sobral@2
   912
  return;
leo_sobral@2
   913
}
leo_sobral@2
   914
leo_sobral@2
   915
  static void
leo_sobral@2
   916
gst_mythtv_src_get_property (GObject * object, guint prop_id,
leo_sobral@2
   917
    GValue * value, GParamSpec * pspec)
leo_sobral@2
   918
{
leo_sobral@2
   919
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
   920
leo_sobral@2
   921
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
   922
  switch (prop_id) {
leo_sobral@2
   923
    case PROP_URI:
leo_sobral@2
   924
    case PROP_LOCATION:
leo_sobral@2
   925
      {
leo_sobral@2
   926
	gchar *str = g_strdup( "" );
leo_sobral@2
   927
leo_sobral@2
   928
	if ( mythtvsrc->uri_name == NULL ) {
leo_sobral@2
   929
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
   930
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
   931
	} else {
leo_sobral@2
   932
	  str = g_strdup( mythtvsrc->uri_name );
leo_sobral@2
   933
	}
leo_sobral@2
   934
	g_value_set_string ( value, str );
leo_sobral@2
   935
	break;
leo_sobral@2
   936
      }
leo_sobral@2
   937
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
   938
    case PROP_GMYTHTV_DBG:
leo_sobral@2
   939
      g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
leo_sobral@2
   940
      break;
leo_sobral@2
   941
#endif
rosfran@40
   942
    case PROP_GMYTHTV_VERSION:
leo_sobral@2
   943
      {
leo_sobral@2
   944
	g_value_set_int ( value, mythtvsrc->mythtv_version );
leo_sobral@2
   945
	break;
leo_sobral@2
   946
      }
rosfran@40
   947
    case PROP_GMYTHTV_LIVEID:
leo_sobral@2
   948
      {
leo_sobral@2
   949
	g_value_set_int ( value, mythtvsrc->live_tv_id );
leo_sobral@2
   950
	break;
leo_sobral@2
   951
      }
rosfran@40
   952
    case PROP_GMYTHTV_LIVE:
leo_sobral@2
   953
      g_value_set_boolean ( value, mythtvsrc->live_tv );
leo_sobral@2
   954
      break;
rosfran@40
   955
    case PROP_GMYTHTV_LIVE_CHAINID:
leo_sobral@2
   956
      {
leo_sobral@2
   957
	gchar *str = g_strdup( "" );
leo_sobral@2
   958
leo_sobral@2
   959
	if ( mythtvsrc->live_chain_id == NULL ) {
leo_sobral@2
   960
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
   961
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
   962
	} else {
leo_sobral@2
   963
	  str = g_strdup( mythtvsrc->live_chain_id );
leo_sobral@2
   964
	}
leo_sobral@2
   965
	g_value_set_string ( value, str );
leo_sobral@2
   966
	break;
leo_sobral@2
   967
      }
leo_sobral@2
   968
    default:
leo_sobral@2
   969
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
   970
      break;
leo_sobral@2
   971
  }
leo_sobral@2
   972
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
   973
}
leo_sobral@2
   974
leo_sobral@2
   975
/* entry point to initialize the plug-in
leo_sobral@2
   976
 * initialize the plug-in itself
leo_sobral@2
   977
 * register the element factories and pad templates
leo_sobral@2
   978
 * register the features
leo_sobral@2
   979
 */
rosfran@40
   980
static gboolean
leo_sobral@2
   981
plugin_init (GstPlugin * plugin)
leo_sobral@2
   982
{
leo_sobral@2
   983
  return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
leo_sobral@2
   984
      GST_TYPE_MYTHTV_SRC);
leo_sobral@2
   985
}
leo_sobral@2
   986
leo_sobral@2
   987
/* this is the structure that gst-register looks for
leo_sobral@2
   988
 * so keep the name plugin_desc, or you cannot get your plug-in registered */
leo_sobral@2
   989
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
leo_sobral@2
   990
    GST_VERSION_MINOR,
leo_sobral@2
   991
    "mythtv",
leo_sobral@2
   992
    "lib MythTV src",
leo_sobral@2
   993
    plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
leo_sobral@2
   994
leo_sobral@2
   995
leo_sobral@2
   996
/*** GSTURIHANDLER INTERFACE *************************************************/
leo_sobral@2
   997
  static guint 
leo_sobral@2
   998
gst_mythtv_src_uri_get_type (void)
leo_sobral@2
   999
{
leo_sobral@2
  1000
  return GST_URI_SRC;
leo_sobral@2
  1001
}
leo_sobral@2
  1002
leo_sobral@2
  1003
  static gchar **
leo_sobral@2
  1004
gst_mythtv_src_uri_get_protocols (void)
leo_sobral@2
  1005
{
leo_sobral@2
  1006
  static gchar *protocols[] = { "myth", "myths", NULL };
leo_sobral@2
  1007
leo_sobral@2
  1008
  return protocols;
leo_sobral@2
  1009
}
leo_sobral@2
  1010
leo_sobral@2
  1011
  static const gchar *
leo_sobral@2
  1012
gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
leo_sobral@2
  1013
{
leo_sobral@2
  1014
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
  1015
leo_sobral@2
  1016
  return src->uri_name;
leo_sobral@2
  1017
}
leo_sobral@2
  1018
leo_sobral@2
  1019
  static gboolean
leo_sobral@2
  1020
gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
leo_sobral@2
  1021
{
leo_sobral@2
  1022
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
  1023
leo_sobral@2
  1024
  gchar *protocol;
leo_sobral@2
  1025
leo_sobral@2
  1026
  protocol = gst_uri_get_protocol (uri);
leo_sobral@2
  1027
  if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
leo_sobral@2
  1028
    g_free (protocol);
leo_sobral@2
  1029
    return FALSE;
leo_sobral@2
  1030
  }
leo_sobral@2
  1031
  g_free (protocol);
leo_sobral@2
  1032
  g_object_set (src, "location", uri, NULL);
leo_sobral@2
  1033
leo_sobral@2
  1034
  return TRUE;
leo_sobral@2
  1035
}
leo_sobral@2
  1036
leo_sobral@2
  1037
  static void
leo_sobral@2
  1038
gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
leo_sobral@2
  1039
{
leo_sobral@2
  1040
  GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
leo_sobral@2
  1041
leo_sobral@2
  1042
  iface->get_type = gst_mythtv_src_uri_get_type;
leo_sobral@2
  1043
  iface->get_protocols = gst_mythtv_src_uri_get_protocols;
leo_sobral@2
  1044
  iface->get_uri = gst_mythtv_src_uri_get_uri;
leo_sobral@2
  1045
  iface->set_uri = gst_mythtv_src_uri_set_uri;
leo_sobral@2
  1046
}
leo_sobral@2
  1047
leo_sobral@2
  1048
  void
leo_sobral@2
  1049
size_header_handler (void *userdata, const char *value)
leo_sobral@2
  1050
{
leo_sobral@2
  1051
  GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
leo_sobral@2
  1052
leo_sobral@2
  1053
  //src->content_size = g_ascii_strtoull (value, NULL, 10);
leo_sobral@2
  1054
leo_sobral@2
  1055
  GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
leo_sobral@2
  1056
}