gst-plugins-mythtv/src/gstmythtvsrc.c
author rosfran
Wed Oct 18 23:08:56 2006 +0100 (2006-10-18)
branchtrunk
changeset 33 79fca2122593
parent 30 abe0ee48d78b
child 34 c71d37b93734
permissions -rwxr-xr-x
[svn r34] Fixes the gmyth_stringlist getuint64 function (problems on bitwise operations).
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@31
    39
#define MYTHTV_TRANSFER_MAX_BUFFER	4*1024
rosfran@30
    40
//( 32*1024  )
leo_sobral@2
    41
leo_sobral@2
    42
/* 4*1024 ??? */
rosfran@30
    43
#define MAX_READ_SIZE              	12*1024
rosfran@30
    44
//( 32*1024 )
leo_sobral@2
    45
leo_sobral@2
    46
#define ENABLE_TIMING_POSITION		1
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@30
   104
static guint 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@30
   279
static guint
rosfran@30
   280
do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, GstBuffer **outbuf)
leo_sobral@2
   281
{
leo_sobral@2
   282
  guint 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@30
   290
  guint len = 0;
leo_sobral@2
   291
rosfran@31
   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 ) {
leo_sobral@2
   300
      read += len;
leo_sobral@2
   301
      src->read_offset += read;
leo_sobral@2
   302
      sizetoread -= len;
rosfran@30
   303
    } else if ( len <= 0 ) {
rosfran@30
   304
    	
rosfran@30
   305
      if ( src->live_tv == FALSE ) {
rosfran@30
   306
		goto done;
rosfran@30
   307
      } else if ( src->content_size >= src->read_offset && 
rosfran@30
   308
      			abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) {
rosfran@31
   309
      				goto done;
rosfran@31
   310
      }
rosfran@30
   311
leo_sobral@2
   312
    }
rosfran@31
   313
    
rosfran@30
   314
    if ( read == sizetoread )
leo_sobral@2
   315
      break;
leo_sobral@2
   316
  }
rosfran@30
   317
  
leo_sobral@2
   318
  if ( read > 0 ) {
leo_sobral@2
   319
    src->bytes_read += read;
rosfran@31
   320
  
leo_sobral@2
   321
  g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
leo_sobral@2
   322
      "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
leo_sobral@2
   323
      src->read_offset, src->content_size );
rosfran@31
   324
      
rosfran@31
   325
  //GST_BUFFER_TIMESTAMP( buffer ) = GST_BUFFER_TIMESTAMP ( *outbuf );
rosfran@31
   326
  GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
rosfran@31
   327
  GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
rosfran@31
   328
  GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
rosfran@31
   329
  //*outbuf = buffer;  
rosfran@31
   330
  //memcpy( GST_BUFFER_DATA( *outbuf ), GST_BUFFER_DATA( buffer ), read );
rosfran@30
   331
     
rosfran@30
   332
  g_print( "Stopping: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
rosfran@30
   333
      "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
rosfran@30
   334
      GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
rosfran@31
   335
      
rosfran@31
   336
  } else
rosfran@31
   337
  	goto eos;
rosfran@31
   338
  
rosfran@30
   339
  goto done;
leo_sobral@2
   340
leo_sobral@2
   341
eos:
rosfran@31
   342
  //GST_OBJECT_UNLOCK(src);  
leo_sobral@2
   343
  src->eos = TRUE;
rosfran@30
   344
  
leo_sobral@2
   345
done:
rosfran@31
   346
  GST_OBJECT_UNLOCK(src);
leo_sobral@2
   347
leo_sobral@2
   348
  return read;
leo_sobral@2
   349
}
leo_sobral@2
   350
rosfran@30
   351
static GstFlowReturn
rosfran@30
   352
gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
leo_sobral@2
   353
{
leo_sobral@2
   354
  GstMythtvSrc *src;
leo_sobral@2
   355
  GstFlowReturn ret = GST_FLOW_OK;
rosfran@30
   356
  guint read = 0;
rosfran@30
   357
  
rosfran@30
   358
  src = GST_MYTHTV_SRC (psrc);
rosfran@31
   359
  /* The caller should know the number of bytes and not read beyond EOS. */
rosfran@31
   360
  if (G_UNLIKELY (src->eos))
rosfran@31
   361
    goto eos;
rosfran@30
   362
  
rosfran@31
   363
  //GST_OBJECT_LOCK(src);
rosfran@31
   364
  if ( /*src->content_size >= src->read_offset && 
rosfran@31
   365
      			abs ( src->content_size - src->read_offset ) <= 1024 ) ||*/
rosfran@31
   366
      			( src->content_size <= ( src->read_offset + size ) ) )
rosfran@31
   367
  {
rosfran@31
   368
#if ENABLE_TIMING_POSITION == 1
rosfran@31
   369
    guint64 size_tmp = 0;
rosfran@31
   370
    if (src->live_tv == TRUE) {
rosfran@31
   371
get_file_pos:
rosfran@31
   372
      size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
rosfran@31
   373
      if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@31
   374
	src->content_size = size_tmp;
rosfran@31
   375
      else
rosfran@31
   376
	goto get_file_pos;
rosfran@31
   377
      g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
rosfran@31
   378
	  __FUNCTION__, size_tmp );
rosfran@31
   379
    }
rosfran@31
   380
#endif
rosfran@31
   381
  	//guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer );
rosfran@31
   382
  	//if ( src->content_size < new_offset ) {
rosfran@31
   383
  	//  src->content_size = new_offset;
rosfran@31
   384
  	//}
rosfran@30
   385
  }
leo_sobral@2
   386
rosfran@30
   387
  if (G_UNLIKELY (src->read_offset != offset)) {
rosfran@30
   388
    guint64 new_offset = myth_file_transfer_seek(src->file_transfer, offset, SEEK_SET);
leo_sobral@2
   389
rosfran@31
   390
    if (G_UNLIKELY (new_offset < 0 || new_offset != offset)) {
rosfran@31
   391
      //GST_OBJECT_UNLOCK(src);
rosfran@30
   392
      goto read_error;
rosfran@31
   393
    }
leo_sobral@2
   394
rosfran@30
   395
    src->read_offset = offset;
rosfran@30
   396
  }
rosfran@31
   397
  //GST_OBJECT_UNLOCK(src);
rosfran@30
   398
  
rosfran@31
   399
  /* Create the buffer. */
rosfran@31
   400
  ret = gst_pad_alloc_buffer (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
rosfran@31
   401
      src->read_offset, size,
rosfran@31
   402
      //src->icy_caps ? src->icy_caps :
rosfran@31
   403
      GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf);
leo_sobral@2
   404
rosfran@31
   405
  if (G_UNLIKELY (ret != GST_FLOW_OK))
rosfran@31
   406
    goto done;
rosfran@30
   407
  
rosfran@31
   408
  //g_static_mutex_lock( &update_size_mutex );
rosfran@31
   409
  read = do_read_request_response ( src, src->read_offset, size, outbuf );
rosfran@31
   410
  //g_static_mutex_unlock( &update_size_mutex );  
leo_sobral@2
   411
rosfran@31
   412
  if (G_UNLIKELY (read < 0) || *outbuf == NULL) {
rosfran@31
   413
  	//if ( src->live_tv )
rosfran@31
   414
    //	goto done;
rosfran@31
   415
    //else
leo_sobral@2
   416
    goto read_error;
rosfran@31
   417
  }
leo_sobral@2
   418
rosfran@31
   419
/* 
leo_sobral@2
   420
  if (G_UNLIKELY(src->eos))
leo_sobral@2
   421
    goto eos;
rosfran@30
   422
  else
rosfran@30
   423
    goto done;
rosfran@31
   424
*/
leo_sobral@2
   425
leo_sobral@2
   426
done:
rosfran@31
   427
 {
rosfran@31
   428
    const gchar *reason = gst_flow_get_name (ret);
rosfran@31
   429
rosfran@31
   430
    GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
rosfran@31
   431
  	return ret;
rosfran@31
   432
 }
leo_sobral@2
   433
eos:
leo_sobral@2
   434
  {
rosfran@30
   435
    const gchar *reason = gst_flow_get_name (ret);
rosfran@30
   436
rosfran@30
   437
    GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
leo_sobral@2
   438
    return GST_FLOW_UNEXPECTED;
leo_sobral@2
   439
  }
leo_sobral@2
   440
  /* ERRORS */
leo_sobral@2
   441
read_error:
leo_sobral@2
   442
  {
leo_sobral@2
   443
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
leo_sobral@2
   444
	(NULL), ("Could not read any bytes (%i, %s)", read,
leo_sobral@2
   445
	  src->uri_name));
leo_sobral@2
   446
    return GST_FLOW_ERROR;
leo_sobral@2
   447
  }
rosfran@30
   448
rosfran@30
   449
#if 0
rosfran@30
   450
need_pause:
rosfran@30
   451
  {
rosfran@30
   452
    const gchar *reason = gst_flow_get_name (ret);
rosfran@30
   453
rosfran@30
   454
    GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
rosfran@30
   455
    return GST_FLOW_UNEXPECTED;
rosfran@30
   456
  }
rosfran@30
   457
#endif
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@31
   693
  } else if ( src->content_size >= src->read_offset && 
rosfran@31
   694
      			abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) {
rosfran@30
   695
    g_static_mutex_lock( &update_size_mutex );
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
 
rosfran@31
   702
#if 0
leo_sobral@2
   703
#if ENABLE_TIMING_POSITION == 1
rosfran@30
   704
    guint64 size_tmp = 0;
rosfran@30
   705
    if (src->live_tv == TRUE) {
leo_sobral@2
   706
get_file_pos:
rosfran@31
   707
      g_usleep( 5 );
rosfran@30
   708
      size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
rosfran@30
   709
      if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@30
   710
	src->content_size = size_tmp;
rosfran@30
   711
      else
rosfran@30
   712
	goto get_file_pos;
rosfran@30
   713
      g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
rosfran@30
   714
	  __FUNCTION__, size_tmp );
rosfran@30
   715
    }
rosfran@30
   716
#endif
rosfran@31
   717
#endif
rosfran@30
   718
    g_static_mutex_unlock( &update_size_mutex );
rosfran@30
   719
leo_sobral@2
   720
  }
leo_sobral@2
   721
leo_sobral@2
   722
  *size = src->content_size;
rosfran@30
   723
  g_print( "[%s] Content size = %llu\n", __FUNCTION__, src->content_size );
leo_sobral@2
   724
rosfran@30
   725
  return ret;
rosfran@30
   726
leo_sobral@2
   727
}
leo_sobral@2
   728
leo_sobral@2
   729
/* close the socket and associated resources
leo_sobral@2
   730
 * used both to recover from errors and go to NULL state */
leo_sobral@2
   731
  static gboolean
leo_sobral@2
   732
gst_mythtv_src_stop (GstBaseSrc * bsrc)
leo_sobral@2
   733
{
leo_sobral@2
   734
  GstMythtvSrc *src;
leo_sobral@2
   735
leo_sobral@2
   736
  src = GST_MYTHTV_SRC (bsrc);
leo_sobral@2
   737
leo_sobral@2
   738
  if (src->uri_name) {
leo_sobral@2
   739
    g_free (src->uri_name);
leo_sobral@2
   740
    src->uri_name = NULL;
leo_sobral@2
   741
  }
leo_sobral@2
   742
leo_sobral@2
   743
  if (src->mythtv_caps) {
leo_sobral@2
   744
    gst_caps_unref (src->mythtv_caps);
leo_sobral@2
   745
    src->mythtv_caps = NULL;
leo_sobral@2
   746
  }
leo_sobral@2
   747
leo_sobral@2
   748
  src->eos = FALSE;
leo_sobral@2
   749
leo_sobral@2
   750
  return TRUE;
leo_sobral@2
   751
}
leo_sobral@2
   752
rosfran@30
   753
static gboolean
leo_sobral@2
   754
gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
leo_sobral@2
   755
{
leo_sobral@2
   756
  GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
leo_sobral@2
   757
leo_sobral@2
   758
  switch (GST_EVENT_TYPE (event)) {
rosfran@30
   759
#if 0
leo_sobral@2
   760
    case GST_EVENT_FLUSH_START:
leo_sobral@2
   761
      src->eos = FALSE;
leo_sobral@2
   762
      break;
leo_sobral@2
   763
      //return TRUE;
leo_sobral@2
   764
    case GST_EVENT_FLUSH_STOP:
leo_sobral@2
   765
      src->do_start = TRUE;
leo_sobral@2
   766
      src->eos = FALSE;
leo_sobral@2
   767
      gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
rosfran@30
   768
      //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
rosfran@30
   769
      break;
rosfran@30
   770
#endif
rosfran@30
   771
    case GST_EVENT_EOS:
rosfran@30
   772
      g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
rosfran@31
   773
	  guint64 cont_size = gst_mythtv_src_get_position (src);
rosfran@30
   774
	  if ( cont_size > src->content_size ) {
rosfran@30
   775
	  	src->content_size = cont_size;
rosfran@31
   776
	  	src->eos = FALSE;
rosfran@30
   777
  	  } else {
rosfran@30
   778
      	src->eos = TRUE;
rosfran@30
   779
      	gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
rosfran@30
   780
      	gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
rosfran@30
   781
  	  }
rosfran@30
   782
      break;
rosfran@30
   783
#if 0
rosfran@30
   784
    case GST_EVENT_NEWSEGMENT:
rosfran@30
   785
      g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
rosfran@30
   786
      src->eos = FALSE;
leo_sobral@2
   787
      break;
leo_sobral@2
   788
    case GST_EVENT_SEEK:  	  
leo_sobral@2
   789
      {
rosfran@30
   790
	g_print( "[%s] Got EVENT_SEEK!!!\n", __FUNCTION__ );
leo_sobral@2
   791
	gdouble rate;
leo_sobral@2
   792
	//gboolean update = TRUE;
leo_sobral@2
   793
	GstFormat format;
leo_sobral@2
   794
	GstSeekType cur_type, stop_type;
leo_sobral@2
   795
	GstSeekFlags flags;
leo_sobral@2
   796
	gint64 cur = 0, stop = 0;
leo_sobral@2
   797
	gst_event_parse_seek ( event, &rate, &format,
leo_sobral@2
   798
	    &flags, &cur_type, &cur,
leo_sobral@2
   799
	    &stop_type, &stop );
leo_sobral@2
   800
leo_sobral@2
   801
	g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
leo_sobral@2
   802
	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
leo_sobral@2
   803
	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
leo_sobral@2
   804
	}
leo_sobral@2
   805
	//gboolean ret = gst_event_parse_new_segment ( event,
leo_sobral@2
   806
	//    &update, &rate, &format, &start, &stop,
leo_sobral@2
   807
	//    &position );
leo_sobral@2
   808
	//GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ), 
leo_sobral@2
   809
	//			cur, stop - cur + 1, GstBuffer)
rosfran@30
   810
      }
rosfran@30
   811
#endif
leo_sobral@2
   812
    default:
leo_sobral@2
   813
      return gst_pad_event_default (pad, event);
leo_sobral@2
   814
  }
leo_sobral@2
   815
leo_sobral@2
   816
  return gst_pad_event_default (pad, event);
leo_sobral@2
   817
}
leo_sobral@2
   818
rosfran@30
   819
static gboolean
rosfran@30
   820
gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
leo_sobral@2
   821
{
leo_sobral@2
   822
  return TRUE;
leo_sobral@2
   823
}
leo_sobral@2
   824
rosfran@30
   825
static void
leo_sobral@2
   826
gst_mythtv_src_set_property (GObject * object, guint prop_id,
leo_sobral@2
   827
    const GValue * value, GParamSpec * pspec)
leo_sobral@2
   828
{
leo_sobral@2
   829
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
   830
leo_sobral@2
   831
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
   832
  switch (prop_id) {
leo_sobral@2
   833
    case PROP_URI:
leo_sobral@2
   834
    case PROP_LOCATION:
leo_sobral@2
   835
      {
leo_sobral@2
   836
	if (!g_value_get_string (value)) {
leo_sobral@2
   837
	  GST_WARNING ("location property cannot be NULL");
leo_sobral@2
   838
	  goto done;
leo_sobral@2
   839
	}
leo_sobral@2
   840
leo_sobral@2
   841
	if (mythtvsrc->uri_name != NULL) {
leo_sobral@2
   842
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
   843
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
   844
	}
leo_sobral@2
   845
	mythtvsrc->uri_name = g_value_dup_string (value);
leo_sobral@2
   846
leo_sobral@2
   847
	break;
leo_sobral@2
   848
      }
leo_sobral@2
   849
#ifndef GST_DISABLE_GST_DEBUG
leo_sobral@2
   850
    case PROP_MYTHTV_DBG:
leo_sobral@2
   851
      {
leo_sobral@2
   852
	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
leo_sobral@2
   853
	break;
leo_sobral@2
   854
      }
leo_sobral@2
   855
#endif
leo_sobral@2
   856
    case PROP_MYTHTV_VERSION:
leo_sobral@2
   857
      {
leo_sobral@2
   858
	mythtvsrc->mythtv_version = g_value_get_int (value);
leo_sobral@2
   859
	break;
leo_sobral@2
   860
      }
leo_sobral@2
   861
    case PROP_MYTHTV_LIVEID:
leo_sobral@2
   862
      {
leo_sobral@2
   863
	mythtvsrc->live_tv_id = g_value_get_int (value);
leo_sobral@2
   864
	break;
leo_sobral@2
   865
      }
leo_sobral@2
   866
    case PROP_MYTHTV_LIVE:
leo_sobral@2
   867
      {
leo_sobral@2
   868
	mythtvsrc->live_tv = g_value_get_boolean (value);
leo_sobral@2
   869
	break;
leo_sobral@2
   870
      }
leo_sobral@2
   871
    case PROP_MYTHTV_LIVE_CHAINID:
leo_sobral@2
   872
      {
leo_sobral@2
   873
	if (!g_value_get_string (value)) {
leo_sobral@2
   874
	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
leo_sobral@2
   875
	  goto done;
leo_sobral@2
   876
	}
leo_sobral@2
   877
leo_sobral@2
   878
	if (mythtvsrc->live_chain_id != NULL) {
leo_sobral@2
   879
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
   880
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
   881
	}
leo_sobral@2
   882
	mythtvsrc->live_chain_id = g_value_dup_string (value);
leo_sobral@2
   883
leo_sobral@2
   884
	break;
leo_sobral@2
   885
      }
leo_sobral@2
   886
leo_sobral@2
   887
    default:
leo_sobral@2
   888
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
   889
      break;
leo_sobral@2
   890
  }
leo_sobral@2
   891
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
   892
done:
leo_sobral@2
   893
  return;
leo_sobral@2
   894
}
leo_sobral@2
   895
leo_sobral@2
   896
  static void
leo_sobral@2
   897
gst_mythtv_src_get_property (GObject * object, guint prop_id,
leo_sobral@2
   898
    GValue * value, GParamSpec * pspec)
leo_sobral@2
   899
{
leo_sobral@2
   900
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
   901
leo_sobral@2
   902
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
   903
  switch (prop_id) {
leo_sobral@2
   904
    case PROP_URI:
leo_sobral@2
   905
    case PROP_LOCATION:
leo_sobral@2
   906
      {
leo_sobral@2
   907
	gchar *str = g_strdup( "" );
leo_sobral@2
   908
leo_sobral@2
   909
	if ( mythtvsrc->uri_name == NULL ) {
leo_sobral@2
   910
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
   911
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
   912
	} else {
leo_sobral@2
   913
	  str = g_strdup( mythtvsrc->uri_name );
leo_sobral@2
   914
	}
leo_sobral@2
   915
	g_value_set_string ( value, str );
leo_sobral@2
   916
	break;
leo_sobral@2
   917
      }
leo_sobral@2
   918
#ifndef GST_DISABLE_GST_DEBUG
leo_sobral@2
   919
    case PROP_MYTHTV_DBG:
leo_sobral@2
   920
      g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
leo_sobral@2
   921
      break;
leo_sobral@2
   922
#endif
leo_sobral@2
   923
    case PROP_MYTHTV_VERSION:
leo_sobral@2
   924
      {
leo_sobral@2
   925
	g_value_set_int ( value, mythtvsrc->mythtv_version );
leo_sobral@2
   926
	break;
leo_sobral@2
   927
      }
leo_sobral@2
   928
    case PROP_MYTHTV_LIVEID:
leo_sobral@2
   929
      {
leo_sobral@2
   930
	g_value_set_int ( value, mythtvsrc->live_tv_id );
leo_sobral@2
   931
	break;
leo_sobral@2
   932
      }
leo_sobral@2
   933
    case PROP_MYTHTV_LIVE:
leo_sobral@2
   934
      g_value_set_boolean ( value, mythtvsrc->live_tv );
leo_sobral@2
   935
      break;
leo_sobral@2
   936
    case PROP_MYTHTV_LIVE_CHAINID:
leo_sobral@2
   937
      {
leo_sobral@2
   938
	gchar *str = g_strdup( "" );
leo_sobral@2
   939
leo_sobral@2
   940
	if ( mythtvsrc->live_chain_id == NULL ) {
leo_sobral@2
   941
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
   942
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
   943
	} else {
leo_sobral@2
   944
	  str = g_strdup( mythtvsrc->live_chain_id );
leo_sobral@2
   945
	}
leo_sobral@2
   946
	g_value_set_string ( value, str );
leo_sobral@2
   947
	break;
leo_sobral@2
   948
      }
leo_sobral@2
   949
    default:
leo_sobral@2
   950
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
   951
      break;
leo_sobral@2
   952
  }
leo_sobral@2
   953
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
   954
}
leo_sobral@2
   955
leo_sobral@2
   956
/* entry point to initialize the plug-in
leo_sobral@2
   957
 * initialize the plug-in itself
leo_sobral@2
   958
 * register the element factories and pad templates
leo_sobral@2
   959
 * register the features
leo_sobral@2
   960
 */
leo_sobral@2
   961
  static gboolean
leo_sobral@2
   962
plugin_init (GstPlugin * plugin)
leo_sobral@2
   963
{
leo_sobral@2
   964
  return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
leo_sobral@2
   965
      GST_TYPE_MYTHTV_SRC);
leo_sobral@2
   966
}
leo_sobral@2
   967
leo_sobral@2
   968
/* this is the structure that gst-register looks for
leo_sobral@2
   969
 * so keep the name plugin_desc, or you cannot get your plug-in registered */
leo_sobral@2
   970
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
leo_sobral@2
   971
    GST_VERSION_MINOR,
leo_sobral@2
   972
    "mythtv",
leo_sobral@2
   973
    "lib MythTV src",
leo_sobral@2
   974
    plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
leo_sobral@2
   975
leo_sobral@2
   976
leo_sobral@2
   977
/*** GSTURIHANDLER INTERFACE *************************************************/
leo_sobral@2
   978
  static guint 
leo_sobral@2
   979
gst_mythtv_src_uri_get_type (void)
leo_sobral@2
   980
{
leo_sobral@2
   981
  return GST_URI_SRC;
leo_sobral@2
   982
}
leo_sobral@2
   983
leo_sobral@2
   984
  static gchar **
leo_sobral@2
   985
gst_mythtv_src_uri_get_protocols (void)
leo_sobral@2
   986
{
leo_sobral@2
   987
  static gchar *protocols[] = { "myth", "myths", NULL };
leo_sobral@2
   988
leo_sobral@2
   989
  return protocols;
leo_sobral@2
   990
}
leo_sobral@2
   991
leo_sobral@2
   992
  static const gchar *
leo_sobral@2
   993
gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
leo_sobral@2
   994
{
leo_sobral@2
   995
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
   996
leo_sobral@2
   997
  return src->uri_name;
leo_sobral@2
   998
}
leo_sobral@2
   999
leo_sobral@2
  1000
  static gboolean
leo_sobral@2
  1001
gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
leo_sobral@2
  1002
{
leo_sobral@2
  1003
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
  1004
leo_sobral@2
  1005
  gchar *protocol;
leo_sobral@2
  1006
leo_sobral@2
  1007
  protocol = gst_uri_get_protocol (uri);
leo_sobral@2
  1008
  if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
leo_sobral@2
  1009
    g_free (protocol);
leo_sobral@2
  1010
    return FALSE;
leo_sobral@2
  1011
  }
leo_sobral@2
  1012
  g_free (protocol);
leo_sobral@2
  1013
  g_object_set (src, "location", uri, NULL);
leo_sobral@2
  1014
leo_sobral@2
  1015
  return TRUE;
leo_sobral@2
  1016
}
leo_sobral@2
  1017
leo_sobral@2
  1018
  static void
leo_sobral@2
  1019
gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
leo_sobral@2
  1020
{
leo_sobral@2
  1021
  GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
leo_sobral@2
  1022
leo_sobral@2
  1023
  iface->get_type = gst_mythtv_src_uri_get_type;
leo_sobral@2
  1024
  iface->get_protocols = gst_mythtv_src_uri_get_protocols;
leo_sobral@2
  1025
  iface->get_uri = gst_mythtv_src_uri_get_uri;
leo_sobral@2
  1026
  iface->set_uri = gst_mythtv_src_uri_set_uri;
leo_sobral@2
  1027
}
leo_sobral@2
  1028
leo_sobral@2
  1029
  void
leo_sobral@2
  1030
size_header_handler (void *userdata, const char *value)
leo_sobral@2
  1031
{
leo_sobral@2
  1032
  GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
leo_sobral@2
  1033
leo_sobral@2
  1034
  //src->content_size = g_ascii_strtoull (value, NULL, 10);
leo_sobral@2
  1035
leo_sobral@2
  1036
  GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
leo_sobral@2
  1037
}