gst-plugins-mythtv/src/gstmythtvsrc.c
author renatofilho
Fri Nov 17 13:18:48 2006 +0000 (2006-11-17)
branchtrunk
changeset 91 6b1e210c250a
parent 87 9ea342c364de
child 93 ae73b4837eed
permissions -rwxr-xr-x
[svn r92] code review
leo_sobral@2
     1
/* vim: set sw=2: -*- Mode: C; tab-width: 2; indent-tabs-mode: t; c-basic-offset: 2; c-indent-level: 2 -*- */
leo_sobral@2
     2
/* GStreamer MythTV Plug-in
leo_sobral@2
     3
 * Copyright (C) <2006> Rosfran Borges <rosfran.borges@indt.org.br>
leo_sobral@2
     4
 *
leo_sobral@2
     5
 * This library is free software; you can redistribute it and/or
rosfran@30
     6
 * modify it under the terms of the GNU Library Lesser General 
rosfran@30
     7
 * Public License as published by the Free Software Foundation; either
leo_sobral@2
     8
 * version 2 of the License, or (at your option) any later version.
leo_sobral@2
     9
 *
leo_sobral@2
    10
 * This library is distributed in the hope that it will be useful,
leo_sobral@2
    11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
leo_sobral@2
    12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
leo_sobral@2
    13
 * Library General Public License for more 
leo_sobral@2
    14
 */
leo_sobral@2
    15
leo_sobral@2
    16
#ifdef HAVE_CONFIG_H
leo_sobral@2
    17
#include "config.h"
leo_sobral@2
    18
#endif
leo_sobral@2
    19
leo_sobral@2
    20
#include "gstmythtvsrc.h"
rosfran@52
    21
#include <gmyth/gmyth_file_transfer.h>
rosfran@52
    22
#include <gmyth/gmyth_livetv.h>
leo_sobral@2
    23
leo_sobral@2
    24
#include <gmyth/gmyth_socket.h>
leo_sobral@2
    25
#include <gmyth/gmyth_tvchain.h>
leo_sobral@2
    26
leo_sobral@2
    27
#include <string.h>
leo_sobral@2
    28
#include <unistd.h>
leo_sobral@2
    29
leo_sobral@2
    30
GST_DEBUG_CATEGORY_STATIC (mythtvsrc_debug);
leo_sobral@2
    31
#define GST_CAT_DEFAULT mythtvsrc_debug
leo_sobral@2
    32
rosfran@70
    33
#define GST_GMYTHTV_ID_NUM					1
rosfran@70
    34
rosfran@70
    35
#define GST_GMYTHTV_CHANNEL_NUM			1000
leo_sobral@2
    36
rosfran@52
    37
#define GMYTHTV_VERSION_DEFAULT			30
rosfran@61
    38
 
rosfran@90
    39
#define GMYTHTV_TRANSFER_MAX_WAITS		100
leo_sobral@2
    40
rosfran@90
    41
#define GMYTHTV_TRANSFER_MAX_BUFFER		128*1024
rosfran@30
    42
//( 32*1024  )
leo_sobral@2
    43
leo_sobral@2
    44
/* 4*1024 ??? */
rosfran@90
    45
#define MAX_READ_SIZE              		12*1024
rosfran@30
    46
//( 32*1024 )
leo_sobral@2
    47
rosfran@81
    48
#define GST_FLOW_ERROR_NO_DATA  		-101
rosfran@70
    49
rosfran@90
    50
#define INTERNAL_BUFFER_SIZE			18*1024
rosfran@90
    51
leo_sobral@2
    52
/* stablish a maximum iteration value to the IS_RECORDING message */
leo_sobral@2
    53
static guint wait_to_transfer = 0;
leo_sobral@2
    54
leo_sobral@2
    55
static const GstElementDetails gst_mythtv_src_details =
rosfran@30
    56
GST_ELEMENT_DETAILS ( "MythTV client source",
leo_sobral@2
    57
    "Source/Network",
leo_sobral@2
    58
    "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
rosfran@30
    59
    "Rosfran Borges <rosfran.borges@indt.org.br>" );
leo_sobral@2
    60
rosfran@81
    61
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ( "src",
leo_sobral@2
    62
    GST_PAD_SRC,
leo_sobral@2
    63
    GST_PAD_ALWAYS,
rosfran@30
    64
    GST_STATIC_CAPS ("video/x-nuv") );
rosfran@81
    65
    
leo_sobral@2
    66
enum
leo_sobral@2
    67
{
leo_sobral@2
    68
  PROP_0,
leo_sobral@2
    69
  PROP_LOCATION,
leo_sobral@2
    70
  PROP_URI,
leo_sobral@2
    71
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
    72
  PROP_GMYTHTV_DBG,
leo_sobral@2
    73
#endif
rosfran@40
    74
  PROP_GMYTHTV_VERSION,
rosfran@40
    75
  PROP_GMYTHTV_LIVE,
rosfran@40
    76
  PROP_GMYTHTV_LIVEID,
rosfran@52
    77
  PROP_GMYTHTV_LIVE_CHAINID,
rosfran@70
    78
  PROP_GMYTHTV_ENABLE_TIMING_POSITION,
rosfran@70
    79
  PROP_GMYTHTV_CHANNEL_NUM
leo_sobral@2
    80
};
leo_sobral@2
    81
leo_sobral@2
    82
static void gst_mythtv_src_finalize (GObject * gobject);
leo_sobral@2
    83
rosfran@81
    84
/*
rosfran@30
    85
static GstFlowReturn gst_mythtv_src_create (GstBaseSrc * psrc, guint64 offset, 
rosfran@30
    86
	guint size, GstBuffer ** outbuf);
rosfran@81
    87
*/
rosfran@81
    88
rosfran@81
    89
//static GstFlowReturn gst_mythtv_src_chain ( GstPad* pad, GstBuffer* outbuf );
rosfran@81
    90
static GstFlowReturn gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf );
rosfran@52
    91
leo_sobral@2
    92
static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
leo_sobral@2
    93
static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
leo_sobral@2
    94
static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
rosfran@87
    95
static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
rosfran@87
    96
rosfran@87
    97
static gboolean gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment );
rosfran@52
    98
rosfran@52
    99
static gboolean gst_mythtv_src_next_program_chain ( GstMythtvSrc *src );
rosfran@52
   100
rosfran@52
   101
static GstStateChangeReturn
rosfran@52
   102
gst_mythtv_src_change_state (GstElement * element, GstStateChange transition);
leo_sobral@2
   103
leo_sobral@2
   104
static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
leo_sobral@2
   105
    const GValue * value, GParamSpec * pspec);
leo_sobral@2
   106
static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
leo_sobral@2
   107
    GValue * value, GParamSpec * pspec);
leo_sobral@2
   108
rosfran@30
   109
static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
rosfran@30
   110
rosfran@87
   111
static gboolean gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query);
rosfran@87
   112
rosfran@90
   113
static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
rosfran@30
   114
rosfran@90
   115
static gint do_read_request_response (GstMythtvSrc * src, guint64 offset, 
rosfran@90
   116
    guint size, gint8 **data_ptr);
rosfran@90
   117
rosfran@90
   118
GStaticRecMutex th_mutex = G_STATIC_REC_MUTEX_INIT;
leo_sobral@2
   119
rosfran@52
   120
static void
leo_sobral@2
   121
_urihandler_init (GType type)
leo_sobral@2
   122
{
leo_sobral@2
   123
  static const GInterfaceInfo urihandler_info = {
leo_sobral@2
   124
    gst_mythtv_src_uri_handler_init,
leo_sobral@2
   125
    NULL,
leo_sobral@2
   126
    NULL
leo_sobral@2
   127
  };
leo_sobral@2
   128
leo_sobral@2
   129
  g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
leo_sobral@2
   130
leo_sobral@2
   131
  GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
leo_sobral@2
   132
      "MythTV src");
leo_sobral@2
   133
}
leo_sobral@2
   134
rosfran@81
   135
//GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstBaseSrc,
rosfran@81
   136
//    GST_TYPE_BASE_SRC, _urihandler_init)
rosfran@30
   137
    
rosfran@81
   138
GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
rosfran@81
   139
    GST_TYPE_PUSH_SRC, _urihandler_init)
rosfran@30
   140
    
rosfran@87
   141
static void
leo_sobral@2
   142
gst_mythtv_src_base_init (gpointer g_class)
leo_sobral@2
   143
{
leo_sobral@2
   144
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
leo_sobral@2
   145
leo_sobral@2
   146
  gst_element_class_add_pad_template (element_class,
leo_sobral@2
   147
      gst_static_pad_template_get (&srctemplate));
rosfran@81
   148
      
leo_sobral@2
   149
  gst_element_class_set_details (element_class, &gst_mythtv_src_details);
rosfran@52
   150
  
rosfran@52
   151
  element_class->change_state = gst_mythtv_src_change_state;
rosfran@81
   152
  
leo_sobral@2
   153
}
leo_sobral@2
   154
rosfran@30
   155
static void
leo_sobral@2
   156
gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
leo_sobral@2
   157
{
rosfran@63
   158
  GObjectClass *gobject_class; 
rosfran@81
   159
  GstPushSrcClass *gstpushsrc_class;
leo_sobral@2
   160
  GstBaseSrcClass *gstbasesrc_class;
leo_sobral@2
   161
leo_sobral@2
   162
  gobject_class = (GObjectClass *) klass;
leo_sobral@2
   163
  gstbasesrc_class = (GstBaseSrcClass *) klass;
rosfran@81
   164
  gstpushsrc_class = (GstPushSrcClass *) klass;
leo_sobral@2
   165
leo_sobral@2
   166
  gobject_class->set_property = gst_mythtv_src_set_property;
leo_sobral@2
   167
  gobject_class->get_property = gst_mythtv_src_get_property;
leo_sobral@2
   168
  gobject_class->finalize = gst_mythtv_src_finalize;
leo_sobral@2
   169
leo_sobral@2
   170
  g_object_class_install_property
leo_sobral@2
   171
    (gobject_class, PROP_LOCATION,
leo_sobral@2
   172
     g_param_spec_string ("location", "Location",
leo_sobral@2
   173
       "The location. In the form:"
leo_sobral@2
   174
       "\n\t\t\tmyth://a.com/file.nuv"
leo_sobral@2
   175
       "\n\t\t\tmyth://a.com:23223/file.nuv "
leo_sobral@2
   176
       "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
leo_sobral@2
   177
       "", G_PARAM_READWRITE));
leo_sobral@2
   178
leo_sobral@2
   179
  g_object_class_install_property
leo_sobral@2
   180
    (gobject_class, PROP_URI,
leo_sobral@2
   181
     g_param_spec_string ("uri", "Uri",
leo_sobral@2
   182
       "The location in form of a URI (deprecated; use location)",
leo_sobral@2
   183
       "", G_PARAM_READWRITE));
leo_sobral@2
   184
leo_sobral@2
   185
  g_object_class_install_property
rosfran@40
   186
    (gobject_class, PROP_GMYTHTV_VERSION,
leo_sobral@2
   187
     g_param_spec_int ("mythtv-version", "mythtv-version",
rosfran@70
   188
       "Change MythTV version",
leo_sobral@2
   189
       26, 30, 26, G_PARAM_READWRITE));
leo_sobral@2
   190
leo_sobral@2
   191
  g_object_class_install_property
rosfran@40
   192
    (gobject_class, PROP_GMYTHTV_LIVEID,
leo_sobral@2
   193
     g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
rosfran@70
   194
       "Change MythTV version",
rosfran@40
   195
       0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
leo_sobral@2
   196
leo_sobral@2
   197
  g_object_class_install_property
rosfran@40
   198
    (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
leo_sobral@2
   199
     g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
rosfran@70
   200
       "Sets the MythTV chain ID (from TV Chain)",
leo_sobral@2
   201
       "", G_PARAM_READWRITE));
leo_sobral@2
   202
leo_sobral@2
   203
  g_object_class_install_property
rosfran@40
   204
    (gobject_class, PROP_GMYTHTV_LIVE,
leo_sobral@2
   205
     g_param_spec_boolean ("mythtv-live", "mythtv-live",
leo_sobral@2
   206
       "Enable MythTV Live TV content streaming",
leo_sobral@2
   207
       FALSE, G_PARAM_READWRITE));
leo_sobral@2
   208
rosfran@52
   209
  g_object_class_install_property
rosfran@52
   210
    (gobject_class, PROP_GMYTHTV_ENABLE_TIMING_POSITION,
rosfran@52
   211
     g_param_spec_boolean ("mythtv-enable-timing-position", "mythtv-enable-timing-position",
rosfran@52
   212
       "Enable MythTV Live TV content size continuous updating",
rosfran@52
   213
       FALSE, G_PARAM_READWRITE));
rosfran@70
   214
       
rosfran@70
   215
  g_object_class_install_property
rosfran@70
   216
    (gobject_class, PROP_GMYTHTV_CHANNEL_NUM,
rosfran@70
   217
     g_param_spec_int ("mythtv-channel", "mythtv-channel",
rosfran@70
   218
       "Change MythTV channel number",
rosfran@71
   219
       0, 99999, GST_GMYTHTV_CHANNEL_NUM, G_PARAM_READWRITE));
rosfran@52
   220
leo_sobral@2
   221
#ifndef GST_DISABLE_GST_DEBUG
leo_sobral@2
   222
  g_object_class_install_property
rosfran@40
   223
    (gobject_class, PROP_GMYTHTV_DBG,
leo_sobral@2
   224
     g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
leo_sobral@2
   225
       "Enable MythTV debug messages",
leo_sobral@2
   226
       FALSE, G_PARAM_READWRITE));
leo_sobral@2
   227
#endif
leo_sobral@2
   228
leo_sobral@2
   229
  gstbasesrc_class->start = gst_mythtv_src_start;
leo_sobral@2
   230
  gstbasesrc_class->stop = gst_mythtv_src_stop;
leo_sobral@2
   231
  gstbasesrc_class->get_size = gst_mythtv_src_get_size;
rosfran@87
   232
  gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
rosfran@81
   233
  
rosfran@87
   234
  gstbasesrc_class->do_seek = gst_mythtv_src_do_seek;
rosfran@81
   235
  gstpushsrc_class->create = gst_mythtv_src_create;
rosfran@52
   236
    
leo_sobral@2
   237
  GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
leo_sobral@2
   238
      "MythTV Client Source");
leo_sobral@2
   239
}
leo_sobral@2
   240
rosfran@30
   241
static void
leo_sobral@2
   242
gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
leo_sobral@2
   243
{
leo_sobral@2
   244
  this->file_transfer = NULL;
leo_sobral@2
   245
leo_sobral@2
   246
  this->unique_setup = FALSE;
leo_sobral@2
   247
rosfran@40
   248
  this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
rosfran@70
   249
  
rosfran@70
   250
  this->state = GST_MYTHTV_SRC_FILE_TRANSFER;
leo_sobral@2
   251
leo_sobral@2
   252
  this->bytes_read = 0;
rosfran@63
   253
  
rosfran@63
   254
  this->prev_content_size = 0;
leo_sobral@2
   255
rosfran@52
   256
  this->content_size = 0;
leo_sobral@2
   257
  this->read_offset = 0;
leo_sobral@2
   258
rosfran@61
   259
  this->content_size_last = 0;
rosfran@61
   260
leo_sobral@2
   261
  this->live_tv = FALSE;
rosfran@52
   262
  
rosfran@52
   263
  this->enable_timing_position = FALSE;
rosfran@52
   264
  this->update_prog_chain = FALSE;    
leo_sobral@2
   265
leo_sobral@2
   266
  this->user_agent = g_strdup ("mythtvsrc");
rosfran@52
   267
  this->mythtv_caps = NULL;
rosfran@52
   268
  this->update_prog_chain = FALSE;
rosfran@52
   269
  
rosfran@70
   270
  this->channel_num = 0;
rosfran@70
   271
  
rosfran@52
   272
  this->eos = FALSE;
rosfran@30
   273
  
rosfran@70
   274
  this->adapter = NULL;
rosfran@70
   275
  
rosfran@81
   276
  //this->th_read_ahead = NULL;
rosfran@70
   277
  
rosfran@81
   278
  this->th_mutex = NULL;
rosfran@81
   279
  
rosfran@70
   280
  this->srcpad = gst_pad_new_from_static_template (&srctemplate, "src");
rosfran@70
   281
  gst_element_add_pad (GST_ELEMENT (this), this->srcpad);
rosfran@70
   282
  
rosfran@87
   283
  gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );  
leo_sobral@2
   284
rosfran@87
   285
  //gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE );
rosfran@81
   286
  
rosfran@90
   287
  gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
rosfran@90
   288
      gst_mythtv_src_handle_event );
rosfran@87
   289
  gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
rosfran@87
   290
     gst_mythtv_src_handle_query );
rosfran@30
   291
leo_sobral@2
   292
}
leo_sobral@2
   293
rosfran@30
   294
static void
leo_sobral@2
   295
gst_mythtv_src_finalize (GObject * gobject)
leo_sobral@2
   296
{
leo_sobral@2
   297
  GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
rosfran@81
   298
  
rosfran@81
   299
  if ( this->th_read_ahead != NULL ) {
rosfran@81
   300
  	gst_task_stop( this->th_read_ahead );
rosfran@81
   301
  	this->th_read_ahead = NULL;
rosfran@81
   302
  }
leo_sobral@2
   303
leo_sobral@2
   304
  if (this->mythtv_caps) {
leo_sobral@2
   305
    gst_caps_unref (this->mythtv_caps);
leo_sobral@2
   306
    this->mythtv_caps = NULL;
leo_sobral@2
   307
  }
leo_sobral@2
   308
leo_sobral@2
   309
  if (this->file_transfer) {
leo_sobral@2
   310
    g_object_unref (this->file_transfer);
leo_sobral@2
   311
    this->file_transfer = NULL;
leo_sobral@2
   312
  }
leo_sobral@2
   313
rosfran@52
   314
	if (this->spawn_livetv) {
rosfran@70
   315
    g_object_unref (this->spawn_livetv);
rosfran@52
   316
    this->spawn_livetv = NULL;
rosfran@52
   317
  }
rosfran@52
   318
leo_sobral@2
   319
  if (this->uri_name) {
leo_sobral@2
   320
    g_free (this->uri_name);
leo_sobral@2
   321
  }
leo_sobral@2
   322
leo_sobral@2
   323
  if (this->user_agent) {
leo_sobral@2
   324
    g_free (this->user_agent);
leo_sobral@2
   325
  }
leo_sobral@2
   326
leo_sobral@2
   327
  G_OBJECT_CLASS (parent_class)->finalize (gobject);
leo_sobral@2
   328
}
leo_sobral@2
   329
rosfran@37
   330
static gint
rosfran@90
   331
do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, gint8 **data_ptr)
leo_sobral@2
   332
{
rosfran@37
   333
  gint read = 0;
rosfran@52
   334
  guint sizetoread = size;
leo_sobral@2
   335
rosfran@30
   336
  g_print( "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); 
leo_sobral@2
   337
rosfran@30
   338
  /* Loop sending the Myth File Transfer request:
leo_sobral@2
   339
   * Retry whilst authentication fails and we supply it. */
rosfran@37
   340
  gint len = 0;
rosfran@90
   341
  //*data_ptr = g_malloc0( size );
rosfran@70
   342
  
rosfran@90
   343
  //while ( sizetoread > 0 ) {
rosfran@61
   344
rosfran@61
   345
    len = gmyth_file_transfer_read( src->file_transfer,
rosfran@90
   346
	*data_ptr + offset + read, sizetoread, TRUE );
leo_sobral@2
   347
leo_sobral@2
   348
    if ( len > 0 ) {
rosfran@81
   349
      read += len;
leo_sobral@2
   350
      sizetoread -= len;
rosfran@52
   351
    } 
rosfran@90
   352
    else if ( len < 0 )
rosfran@52
   353
    {
rosfran@90
   354
      read = -1;
rosfran@61
   355
rosfran@52
   356
      if ( src->live_tv == FALSE ) 
rosfran@52
   357
      {
rosfran@90
   358
	goto eos;
rosfran@52
   359
      } 
rosfran@52
   360
      else  
rosfran@52
   361
      {
rosfran@90
   362
	if ( len == GMYTHTV_FILE_TRANSFER_READ_ERROR ) { /* -314 */
rosfran@90
   363
	  src->update_prog_chain = TRUE;
rosfran@90
   364
	  goto done;	  	
rosfran@90
   365
	} else if ( abs( src->content_size - src->bytes_read ) < GMYTHTV_TRANSFER_MAX_BUFFER ) {
rosfran@90
   366
	    src->update_prog_chain = TRUE;
rosfran@90
   367
	    if ( src->enable_timing_position ) {
rosfran@90
   368
	      gint64 size_tmp = 0;
rosfran@90
   369
get_file_pos:
rosfran@90
   370
	      size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@90
   371
	      if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@90
   372
		src->content_size = size_tmp;
rosfran@90
   373
	      else if ( size_tmp > 0 )
rosfran@90
   374
		goto get_file_pos;
rosfran@90
   375
	      g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
rosfran@90
   376
		  __FUNCTION__, size_tmp );
rosfran@90
   377
	    }
rosfran@90
   378
	  }
rosfran@90
   379
	goto done;
rosfran@90
   380
      }
rosfran@30
   381
rosfran@61
   382
    }
rosfran@61
   383
rosfran@30
   384
    if ( read == sizetoread )
rosfran@90
   385
      goto done;
rosfran@90
   386
  //}
rosfran@61
   387
rosfran@90
   388
  if ( read < 0 && !src->live_tv )
rosfran@61
   389
    goto eos;
rosfran@61
   390
rosfran@30
   391
  goto done;
leo_sobral@2
   392
leo_sobral@2
   393
eos:
leo_sobral@2
   394
  src->eos = TRUE;
rosfran@61
   395
leo_sobral@2
   396
done:
leo_sobral@2
   397
leo_sobral@2
   398
  return read;
leo_sobral@2
   399
}
leo_sobral@2
   400
rosfran@81
   401
#if 0
rosfran@30
   402
static GstFlowReturn
rosfran@30
   403
gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
leo_sobral@2
   404
{
leo_sobral@2
   405
  GstMythtvSrc *src;
leo_sobral@2
   406
  GstFlowReturn ret = GST_FLOW_OK;
rosfran@37
   407
  gint read = -1;
rosfran@81
   408
  gint adapter_size = -1;
rosfran@81
   409
  
rosfran@81
   410
  guint max_adapter_rep = 40;
rosfran@30
   411
  
rosfran@30
   412
  src = GST_MYTHTV_SRC (psrc);
rosfran@81
   413
  
rosfran@31
   414
  /* The caller should know the number of bytes and not read beyond EOS. */
rosfran@31
   415
  if (G_UNLIKELY (src->eos))
rosfran@31
   416
    goto eos;
rosfran@40
   417
  if ( G_UNLIKELY (src->update_prog_chain) )
rosfran@40
   418
    goto change_progchain;
rosfran@81
   419
rosfran@90
   420
  g_static_rec_mutex_lock( th_mutexth_mutex );
rosfran@30
   421
  
rosfran@81
   422
  while ( ( ( adapter_size = gst_adapter_available_fast( src->adapter ) ) < size ) &&
rosfran@81
   423
  		--max_adapter_rep > 0 )
rosfran@81
   424
  {
rosfran@81
   425
  	g_print ( "[%s] %d - Waiting for read_ahead task...\n", __FUNCTION__, max_adapter_rep );
rosfran@81
   426
  	GST_TASK_WAIT( src->th_read_ahead );
rosfran@81
   427
  }
rosfran@81
   428
  	
rosfran@90
   429
  g_static_rec_mutex_unlock( th_mutexth_mutex );
leo_sobral@2
   430
rosfran@81
   431
	gint64 new_offset = -1;
rosfran@81
   432
	/* just get from the adapter, no network effort... */
rosfran@81
   433
	if ( offset > src->adapter_offset && size <= adapter_size )	
rosfran@81
   434
	{
rosfran@81
   435
		
rosfran@81
   436
		GstBuffer *buf = gst_adapter_take_buffer( src->adapter, size );
rosfran@81
   437
		*outbuf = gst_buffer_create_sub( buf, offset, size );
rosfran@81
   438
		src->read_offset = new_offset = offset;
rosfran@81
   439
		read = size;
rosfran@81
   440
		
rosfran@81
   441
		gst_adapter_flush( src->adapter, size );
rosfran@81
   442
		
rosfran@81
   443
	} else {
rosfran@81
   444
		/* no data on adapter... do all these mythtv network calls! */
rosfran@81
   445
		
rosfran@81
   446
		/* verify if it needs to seek */
rosfran@81
   447
		if ( src->read_offset != offset ) 
rosfran@81
   448
  	{
rosfran@81
   449
		
rosfran@81
   450
	  	new_offset = gmyth_file_transfer_seek( src->file_transfer, offset, SEEK_SET );
rosfran@81
   451
	  	
rosfran@81
   452
	    g_print( "[%s] SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
rosfran@81
   453
	    	__FUNCTION__, src->read_offset, new_offset );
rosfran@81
   454
	    if ( G_UNLIKELY (new_offset < 0 ) )
rosfran@81
   455
	    {
rosfran@81
   456
	      if ( src->live_tv )
rosfran@81
   457
	      	goto change_progchain;
rosfran@81
   458
	      else
rosfran@81
   459
	      	goto eos;
rosfran@81
   460
	    }
rosfran@81
   461
	    
rosfran@81
   462
  	}
rosfran@81
   463
	
rosfran@30
   464
    src->read_offset = offset;
rosfran@81
   465
  	
rosfran@81
   466
	  /* Create the buffer. */
rosfran@81
   467
	  ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
rosfran@81
   468
	      src->read_offset, size,
rosfran@81
   469
	      GST_PAD_CAPS ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)) ), outbuf);
rosfran@81
   470
	
rosfran@81
   471
	  if (G_UNLIKELY (ret != GST_FLOW_OK)) {
rosfran@81
   472
	    if ( src->live_tv )
rosfran@81
   473
	    	goto change_progchain;
rosfran@81
   474
	    else
rosfran@81
   475
	    	goto done;
rosfran@81
   476
	  }
rosfran@81
   477
	  
rosfran@81
   478
	  read = do_read_request_response ( src, src->read_offset, size, outbuf ); 
rosfran@81
   479
  	
rosfran@81
   480
	}
leo_sobral@2
   481
rosfran@40
   482
  if (G_UNLIKELY (src->update_prog_chain) )
rosfran@40
   483
    goto change_progchain;
rosfran@40
   484
rosfran@52
   485
  if (G_UNLIKELY (read <= 0) || *outbuf == NULL) {
rosfran@52
   486
  	if ( src->live_tv )
rosfran@52
   487
    	goto change_progchain;
rosfran@52
   488
    else
rosfran@52
   489
    	goto read_error;
rosfran@31
   490
  }
rosfran@81
   491
  
rosfran@81
   492
  if ( read > 0 ) {
rosfran@81
   493
    src->read_offset += read;
rosfran@81
   494
    src->bytes_read += read;
rosfran@81
   495
    
rosfran@81
   496
    #if 0
rosfran@81
   497
    g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
rosfran@81
   498
			"OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
rosfran@81
   499
			src->read_offset, src->content_size );
rosfran@81
   500
rosfran@81
   501
    GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
rosfran@81
   502
    //GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
rosfran@81
   503
    //GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
rosfran@81
   504
    //g_memmove( GST_BUFFER_DATA( *outbuf ), data_ptr, read );
rosfran@81
   505
    GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
rosfran@81
   506
    GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
rosfran@81
   507
rosfran@81
   508
    g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
rosfran@81
   509
			"OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
rosfran@81
   510
			GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
rosfran@81
   511
		#endif
rosfran@81
   512
			
rosfran@81
   513
  }
leo_sobral@2
   514
leo_sobral@2
   515
done:
rosfran@31
   516
 {
rosfran@31
   517
    const gchar *reason = gst_flow_get_name (ret);
rosfran@31
   518
rosfran@31
   519
    GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
rosfran@31
   520
  	return ret;
rosfran@31
   521
 }
leo_sobral@2
   522
eos:
leo_sobral@2
   523
  {
rosfran@30
   524
    const gchar *reason = gst_flow_get_name (ret);
rosfran@30
   525
rosfran@30
   526
    GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
leo_sobral@2
   527
    return GST_FLOW_UNEXPECTED;
leo_sobral@2
   528
  }
leo_sobral@2
   529
  /* ERRORS */
leo_sobral@2
   530
read_error:
leo_sobral@2
   531
  {
leo_sobral@2
   532
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
leo_sobral@2
   533
	(NULL), ("Could not read any bytes (%i, %s)", read,
leo_sobral@2
   534
	  src->uri_name));
leo_sobral@2
   535
    return GST_FLOW_ERROR;
leo_sobral@2
   536
  }
rosfran@40
   537
change_progchain:
rosfran@37
   538
  {
rosfran@37
   539
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@52
   540
		(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
rosfran@52
   541
		  src->uri_name));
rosfran@81
   542
		  
rosfran@81
   543
		gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
rosfran@81
   544
  			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
rosfran@52
   545
		// go to the next program chain
rosfran@52
   546
		src->unique_setup = FALSE;
rosfran@52
   547
		src->update_prog_chain = TRUE;
rosfran@70
   548
		
rosfran@70
   549
		gst_mythtv_src_next_program_chain( src );
rosfran@81
   550
		
rosfran@81
   551
    return GST_FLOW_ERROR_NO_DATA;
rosfran@81
   552
  }
rosfran@81
   553
rosfran@81
   554
}
rosfran@81
   555
#endif
rosfran@81
   556
rosfran@81
   557
static GstFlowReturn
rosfran@81
   558
gst_mythtv_src_create ( GstPushSrc* psrc, GstBuffer** outbuf )
rosfran@81
   559
{
rosfran@81
   560
  GstMythtvSrc *src;
rosfran@81
   561
  GstFlowReturn ret = GST_FLOW_OK;
rosfran@81
   562
  gint read = -1;
rosfran@90
   563
  gint adapter_size = 0;
rosfran@90
   564
  guint max_adapter_rep = 1;
rosfran@90
   565
rosfran@81
   566
  src = GST_MYTHTV_SRC ( psrc );
rosfran@90
   567
rosfran@81
   568
  /* The caller should know the number of bytes and not read beyond EOS. */
rosfran@81
   569
  if (G_UNLIKELY (src->eos))
rosfran@81
   570
    goto eos;
rosfran@81
   571
  if ( G_UNLIKELY (src->update_prog_chain) )
rosfran@81
   572
    goto change_progchain;
rosfran@90
   573
  
rosfran@90
   574
  //g_static_rec_mutex_lock( &th_mutex );
rosfran@90
   575
rosfran@90
   576
  while ( src->adapter != NULL && ( ( adapter_size = gst_adapter_available_fast( src->adapter ) ) < MAX_READ_SIZE ) &&
rosfran@90
   577
      --max_adapter_rep > 0 )
rosfran@90
   578
  {
rosfran@90
   579
    g_print ( "[%s] %d - Waiting for read_ahead task...\n", __FUNCTION__, max_adapter_rep );
rosfran@90
   580
    //GST_TASK_WAIT( src->th_read_ahead );
rosfran@90
   581
  }
rosfran@90
   582
rosfran@90
   583
  //gst_task_pause( src->th_read_ahead );
rosfran@90
   584
rosfran@90
   585
  //g_static_rec_mutex_unlock( &th_mutex );
rosfran@81
   586
rosfran@87
   587
  /* just get from the adapter, no network effort... */
rosfran@90
   588
  if ( adapter_size > 0 )	
rosfran@90
   589
  {
rosfran@90
   590
    //g_static_rec_mutex_lock( &th_mutex );
rosfran@81
   591
rosfran@90
   592
    *outbuf = gst_adapter_take_buffer( src->adapter, adapter_size );
rosfran@90
   593
    // src->read_offset += size;
rosfran@90
   594
    read = adapter_size;
rosfran@81
   595
rosfran@90
   596
    gst_adapter_flush( src->adapter, adapter_size );
rosfran@90
   597
    //g_static_rec_mutex_unlock( &th_mutex );
rosfran@90
   598
rosfran@90
   599
  } else { 
rosfran@90
   600
    guint size = (src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
rosfran@90
   601
rosfran@90
   602
    g_print ( "[%s]\t\tCreate: buffer_offset: %d, buffer_remain: %d\n", __FUNCTION__, 
rosfran@90
   603
	(gint) src->buffer_offset,
rosfran@90
   604
	(gint) src->buffer_remain);
rosfran@90
   605
rosfran@90
   606
    /* Create the buffer. */
rosfran@90
   607
    ret = gst_pad_alloc_buffer ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
rosfran@90
   608
	src->read_offset /*GST_BUFFER_OFFSET_NONE*/, size,
rosfran@90
   609
	GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf );    
rosfran@90
   610
rosfran@90
   611
    if (G_UNLIKELY (ret != GST_FLOW_OK)) {
rosfran@90
   612
      if ( src->live_tv )
rosfran@90
   613
	goto change_progchain;
rosfran@90
   614
      else
rosfran@90
   615
	goto done;
rosfran@90
   616
    }
rosfran@90
   617
rosfran@90
   618
    if (src->buffer_remain < MAX_READ_SIZE) {
rosfran@90
   619
      gint8 *tmp_buffer = g_malloc0( INTERNAL_BUFFER_SIZE ); // FIXME: DON'T ALLOC EVERY TIME
rosfran@90
   620
rosfran@90
   621
      memcpy (tmp_buffer, src->buffer + src->buffer_offset, src->buffer_remain);
rosfran@90
   622
rosfran@90
   623
      read = do_read_request_response( src, 0, INTERNAL_BUFFER_SIZE - src->buffer_remain, &tmp_buffer );
rosfran@90
   624
rosfran@90
   625
      if (G_UNLIKELY (read < 0)) {
rosfran@90
   626
	if ( src->live_tv )
rosfran@90
   627
	  goto change_progchain;
rosfran@90
   628
	else
rosfran@90
   629
	  goto read_error;
rosfran@90
   630
      }
rosfran@90
   631
rosfran@90
   632
      if ( G_UNLIKELY (src->update_prog_chain) )
rosfran@90
   633
	goto change_progchain;
rosfran@90
   634
rosfran@90
   635
      //len = gmyth_file_transfer_read( src->file_transfer,
rosfran@90
   636
      //  tmp_buffer + src->buffer_remain, INTERNAL_BUFFER_SIZE - src->buffer_remain, TRUE );
rosfran@90
   637
      // fixme: handle eos
rosfran@90
   638
      // fixme: can I deallocate the previous buffer here?
rosfran@90
   639
      g_memmove( src->buffer + src->buffer_remain, tmp_buffer, read);
rosfran@90
   640
      src->buffer_offset = 0;
rosfran@90
   641
      src->buffer_remain = src->buffer_remain + read;
rosfran@90
   642
rosfran@90
   643
      // g_free( tmp_buffer );
rosfran@90
   644
rosfran@90
   645
    }
rosfran@90
   646
rosfran@90
   647
    g_print( "[%s] read = %d, buffer_remain = %d\n", __FUNCTION__, read, src->buffer_remain );
rosfran@90
   648
rosfran@90
   649
    GST_BUFFER_SIZE (*outbuf) = ( src->buffer_remain < MAX_READ_SIZE) ? src->buffer_remain : MAX_READ_SIZE;
rosfran@90
   650
    GST_BUFFER_MALLOCDATA( *outbuf ) = g_malloc0( GST_BUFFER_SIZE (*outbuf) );
rosfran@90
   651
    GST_BUFFER_DATA( *outbuf ) = GST_BUFFER_MALLOCDATA( *outbuf );
rosfran@90
   652
    g_memmove( GST_BUFFER_DATA( (*outbuf) ), src->buffer + src->buffer_offset, GST_BUFFER_SIZE(*outbuf) );
rosfran@90
   653
    GST_BUFFER_OFFSET (*outbuf) = src->read_offset;
rosfran@90
   654
    GST_BUFFER_OFFSET_END (*outbuf) = src->read_offset + GST_BUFFER_SIZE (*outbuf);
rosfran@90
   655
rosfran@90
   656
    src->buffer_offset += GST_BUFFER_SIZE (*outbuf);
rosfran@90
   657
    src->buffer_remain -= GST_BUFFER_SIZE (*outbuf);
rosfran@90
   658
rosfran@90
   659
    src->read_offset += GST_BUFFER_SIZE (*outbuf);
rosfran@90
   660
    src->bytes_read += GST_BUFFER_SIZE (*outbuf);
rosfran@90
   661
    g_print ( "[%s]\t\tBuffer output with size: %d\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf) );
rosfran@90
   662
rosfran@90
   663
    g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
rosfran@90
   664
	"OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
rosfran@90
   665
	src->read_offset, src->content_size );
rosfran@90
   666
rosfran@90
   667
    g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
rosfran@90
   668
	"OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
rosfran@90
   669
	GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
rosfran@90
   670
rosfran@90
   671
    /* just get from the adapter, no network effort... */
rosfran@90
   672
rosfran@81
   673
  }
rosfran@81
   674
rosfran@90
   675
  return ret;
rosfran@90
   676
rosfran@90
   677
  //g_static_rec_mutex_lock( &th_mutex );
rosfran@90
   678
  
rosfran@90
   679
  //GST_TASK_SIGNAL( src->th_read_ahead );
rosfran@90
   680
rosfran@90
   681
  //g_static_rec_mutex_unlock( &th_mutex );
rosfran@90
   682
  
rosfran@81
   683
done:
rosfran@90
   684
  {
rosfran@81
   685
    const gchar *reason = gst_flow_get_name (ret);
rosfran@81
   686
rosfran@81
   687
    GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
rosfran@90
   688
    return ret;
rosfran@90
   689
  }
rosfran@81
   690
eos:
rosfran@81
   691
  {
rosfran@81
   692
    const gchar *reason = gst_flow_get_name (ret);
rosfran@81
   693
rosfran@81
   694
    GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
rosfran@81
   695
    return GST_FLOW_UNEXPECTED;
rosfran@81
   696
  }
rosfran@81
   697
  /* ERRORS */
rosfran@81
   698
read_error:
rosfran@81
   699
  {
rosfran@81
   700
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@81
   701
	(NULL), ("Could not read any bytes (%i, %s)", read,
rosfran@90
   702
		 src->uri_name));
rosfran@81
   703
    return GST_FLOW_ERROR;
rosfran@81
   704
  }
rosfran@81
   705
change_progchain:
rosfran@81
   706
  {
rosfran@81
   707
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@90
   708
	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
rosfran@90
   709
		 src->uri_name));
rosfran@90
   710
rosfran@90
   711
    gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
rosfran@90
   712
	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
rosfran@90
   713
    // go to the next program chain
rosfran@90
   714
    src->unique_setup = FALSE;
rosfran@90
   715
    src->update_prog_chain = TRUE;
rosfran@90
   716
rosfran@90
   717
    gst_mythtv_src_next_program_chain( src );
rosfran@90
   718
rosfran@70
   719
    return GST_FLOW_ERROR_NO_DATA;
rosfran@37
   720
  }
rosfran@30
   721
leo_sobral@2
   722
}
leo_sobral@2
   723
rosfran@52
   724
gint64
rosfran@30
   725
gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
leo_sobral@2
   726
{
rosfran@30
   727
rosfran@52
   728
  gint64 size_tmp = 0;
rosfran@71
   729
  guint max_tries = 2;
rosfran@71
   730
  if (src->live_tv == TRUE && ( abs( src->content_size - src->bytes_read ) < 
rosfran@71
   731
		GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
rosfran@71
   732
rosfran@30
   733
get_file_pos:
rosfran@61
   734
    g_usleep( 10 );
rosfran@52
   735
    size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@52
   736
    if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@61
   737
      src->content_size = size_tmp;
rosfran@61
   738
    else if ( size_tmp > 0 && --max_tries > 0 )
rosfran@61
   739
      goto get_file_pos;
rosfran@52
   740
    g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
rosfran@61
   741
	__FUNCTION__, size_tmp );
rosfran@71
   742
    /* sets the last content size amount before it can be updated */
rosfran@71
   743
    src->prev_content_size = src->content_size;
rosfran@30
   744
  }
rosfran@52
   745
rosfran@30
   746
  return src->content_size;	
rosfran@30
   747
leo_sobral@2
   748
}
leo_sobral@2
   749
rosfran@87
   750
static gboolean
rosfran@87
   751
gst_mythtv_src_do_seek( GstBaseSrc *base, GstSegment *segment )
rosfran@87
   752
{
rosfran@87
   753
  GstMythtvSrc *src = GST_MYTHTV_SRC( base );
rosfran@90
   754
  gint64 new_offset = -1;
rosfran@90
   755
  gint64 actual_seek = segment->start;
rosfran@87
   756
  gboolean ret = TRUE;
rosfran@87
   757
rosfran@87
   758
  g_print( "[%s]DO Seek called! (start = %lld, stop = %lld)\n", __FUNCTION__, segment->start, segment->stop );
rosfran@90
   759
  //g_static_rec_mutex_lock( &th_mutex );
rosfran@87
   760
rosfran@90
   761
  //GST_TASK_WAIT( src->th_read_ahead );
rosfran@90
   762
rosfran@90
   763
  //g_static_rec_mutex_unlock( &th_mutex );
rosfran@90
   764
rosfran@90
   765
  if ( segment->format == GST_FORMAT_TIME ) 
rosfran@90
   766
  {
rosfran@90
   767
    goto done; 
rosfran@90
   768
    //actual_seek = ( ( segment->start / 1000 ) * 28 ) * 4000;
rosfran@90
   769
  }
rosfran@90
   770
  g_print( "[%s]Trying to seek at the value (actual_seek = %lld, read_offset = %lld)\n", __FUNCTION__, actual_seek, src->read_offset );
rosfran@87
   771
  /* verify if it needs to seek */
rosfran@90
   772
  if ( src->read_offset != actual_seek )
rosfran@87
   773
  {
rosfran@87
   774
rosfran@87
   775
    new_offset = gmyth_file_transfer_seek( src->file_transfer, segment->start, SEEK_SET );
rosfran@87
   776
rosfran@87
   777
    g_print( "[%s] Segment offset start = %lld, SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
rosfran@87
   778
	__FUNCTION__, segment->start, src->read_offset, new_offset );
rosfran@87
   779
    if ( G_UNLIKELY (new_offset < 0 ) )
rosfran@87
   780
    {
rosfran@87
   781
      ret = FALSE;
rosfran@87
   782
      if ( src->live_tv )
rosfran@87
   783
	goto change_progchain;
rosfran@87
   784
      else
rosfran@87
   785
	goto eos;
rosfran@87
   786
    }
rosfran@87
   787
rosfran@87
   788
    src->read_offset = new_offset;
rosfran@87
   789
rosfran@90
   790
    //gst_segment_set_seek( segment, segment->rate, GST_FORMAT_BYTES, GST_SEEK_FLAG_NONE, 
rosfran@90
   791
    //GST_SEEK_TYPE_CUR, src->read_offset, GST_SEEK_TYPE_CUR, segment->stop, &ret );
rosfran@90
   792
rosfran@90
   793
    if ( ret == FALSE ) {
rosfran@90
   794
      g_print( "[%s] Failed to set the SEEK on segment!\n", __FUNCTION__ );
rosfran@90
   795
    }
rosfran@90
   796
rosfran@87
   797
  }
rosfran@90
   798
   //g_static_rec_mutex_lock( &th_mutex );
rosfran@90
   799
rosfran@90
   800
   //GST_TASK_SIGNAL( src->th_read_ahead );
rosfran@90
   801
rosfran@90
   802
   //g_static_rec_mutex_unlock( &th_mutex );
rosfran@90
   803
done:
rosfran@87
   804
   return ret;
rosfran@87
   805
rosfran@87
   806
eos:
rosfran@87
   807
  {
rosfran@87
   808
rosfran@87
   809
    GST_DEBUG_OBJECT (src, "EOS found on seeking!!!");
rosfran@90
   810
    //gst_object_unref( src );
rosfran@87
   811
    return FALSE;
rosfran@87
   812
  }
rosfran@87
   813
change_progchain:
rosfran@87
   814
  {
rosfran@87
   815
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@87
   816
	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
rosfran@87
   817
		 src->uri_name));
rosfran@87
   818
rosfran@87
   819
    gst_pad_push_event ( GST_BASE_SRC_PAD (base),
rosfran@87
   820
	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
rosfran@87
   821
    /* go to the next program chain */
rosfran@87
   822
    src->unique_setup = FALSE;
rosfran@87
   823
    src->update_prog_chain = TRUE;
rosfran@87
   824
rosfran@87
   825
    gst_mythtv_src_next_program_chain( src );
rosfran@87
   826
rosfran@87
   827
    return TRUE;
rosfran@87
   828
  }
rosfran@87
   829
rosfran@87
   830
}
rosfran@87
   831
rosfran@81
   832
#if 0
rosfran@81
   833
static void 
rosfran@81
   834
gst_mythtv_src_read_ahead ( void *data ) {
rosfran@81
   835
rosfran@90
   836
  GstMythtvSrc *src = NULL;
rosfran@90
   837
rosfran@90
   838
  GstBuffer *outbuf = NULL;
rosfran@90
   839
rosfran@90
   840
  guint size = 5*2048;
rosfran@90
   841
  gint total = 0;
rosfran@90
   842
  gint read = -1;
rosfran@90
   843
rosfran@90
   844
  src = GST_MYTHTV_SRC( data );
rosfran@90
   845
rosfran@90
   846
  //GST_PAD_STREAM_TRYLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
rosfran@90
   847
rosfran@90
   848
  do {
rosfran@90
   849
    GST_TASK_WAIT( src->th_read_ahead );
rosfran@90
   850
rosfran@90
   851
    gint8 *data = NULL;
rosfran@90
   852
    
rosfran@90
   853
    outbuf = gst_buffer_new_and_alloc( size );
rosfran@90
   854
rosfran@90
   855
    read = do_read_request_response ( src, src->adapter_offset, size, &data );
rosfran@90
   856
rosfran@90
   857
    if ( read > 0 ) {
rosfran@90
   858
      src->read_offset += read;
rosfran@90
   859
      src->bytes_read += read;
rosfran@90
   860
      total += read;
rosfran@90
   861
rosfran@90
   862
      g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
rosfran@90
   863
	  "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
rosfran@90
   864
	  src->read_offset, src->content_size );
rosfran@90
   865
rosfran@90
   866
      GST_BUFFER_SIZE (outbuf) = read;
rosfran@90
   867
      GST_BUFFER_MALLOCDATA( outbuf ) = g_malloc0( GST_BUFFER_SIZE (outbuf) );
rosfran@90
   868
      GST_BUFFER_DATA( outbuf ) = GST_BUFFER_MALLOCDATA( outbuf );
rosfran@90
   869
      g_memmove( GST_BUFFER_DATA( outbuf ), data, read );
rosfran@90
   870
      GST_BUFFER_OFFSET (outbuf) = src->adapter_offset;  
rosfran@90
   871
      GST_BUFFER_OFFSET_END (outbuf) = src->adapter_offset + read;
rosfran@90
   872
      g_print( "Got buffer: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
rosfran@90
   873
	  "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (outbuf), 
rosfran@90
   874
	  GST_BUFFER_OFFSET (outbuf), GST_BUFFER_OFFSET_END (outbuf) );
rosfran@90
   875
rosfran@90
   876
    } 
rosfran@90
   877
rosfran@90
   878
    gst_adapter_push( src->adapter, outbuf );
rosfran@90
   879
rosfran@90
   880
    GST_TASK_SIGNAL( src->th_read_ahead );
rosfran@90
   881
rosfran@90
   882
  } while ( read < size );
rosfran@90
   883
rosfran@90
   884
  //GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
rosfran@90
   885
rosfran@90
   886
  return;	
rosfran@81
   887
}
rosfran@81
   888
#endif
rosfran@81
   889
leo_sobral@2
   890
/* create a socket for connecting to remote server */
rosfran@30
   891
static gboolean
leo_sobral@2
   892
gst_mythtv_src_start ( GstBaseSrc * bsrc )
leo_sobral@2
   893
{
leo_sobral@2
   894
  GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
leo_sobral@2
   895
leo_sobral@2
   896
  GString *chain_id_local = NULL;
leo_sobral@2
   897
leo_sobral@2
   898
  gboolean ret = TRUE;
rosfran@90
   899
rosfran@71
   900
  if ( G_UNLIKELY (src->update_prog_chain) )
rosfran@71
   901
    goto change_progchain;
rosfran@30
   902
leo_sobral@2
   903
  if (src->unique_setup == FALSE) {
leo_sobral@2
   904
    src->unique_setup = TRUE;
leo_sobral@2
   905
  } else {
leo_sobral@2
   906
    goto done;
leo_sobral@2
   907
  }
leo_sobral@2
   908
leo_sobral@2
   909
  if ( src->live_tv ) {
rosfran@40
   910
    src->spawn_livetv = gmyth_livetv_new( );
rosfran@40
   911
    if ( gmyth_livetv_setup( src->spawn_livetv ) == FALSE ) {
rosfran@30
   912
      ret = FALSE;
rosfran@30
   913
      goto init_failed;
rosfran@70
   914
    }    
rosfran@52
   915
leo_sobral@2
   916
    /* set up the uri variable */
leo_sobral@2
   917
    src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
leo_sobral@2
   918
    chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
leo_sobral@2
   919
    if ( chain_id_local != NULL ) {
leo_sobral@2
   920
      src->live_chain_id = g_strdup( chain_id_local->str );
leo_sobral@2
   921
      g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
leo_sobral@2
   922
    }
rosfran@70
   923
    src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
rosfran@70
   924
    g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name ); 
leo_sobral@2
   925
  }
leo_sobral@2
   926
rosfran@40
   927
  src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
leo_sobral@2
   928
      g_string_new( src->uri_name ), -1, src->mythtv_version );
leo_sobral@2
   929
leo_sobral@2
   930
  if ( src->file_transfer == NULL ) {
leo_sobral@2
   931
    goto init_failed;
leo_sobral@2
   932
  }
leo_sobral@2
   933
leo_sobral@2
   934
  /* sets the Playback monitor connection */
rosfran@40
   935
  ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
leo_sobral@2
   936
leo_sobral@2
   937
  if ( src->live_tv == TRUE && ret == TRUE ) {
leo_sobral@2
   938
    /* loop finished, set the max tries variable to zero again... */
leo_sobral@2
   939
    wait_to_transfer = 0;
leo_sobral@2
   940
rosfran@70
   941
    while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS &&
rosfran@90
   942
	( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE 
rosfran@40
   943
	  /*|| ( gmyth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
leo_sobral@2
   944
      g_usleep( 100 );
leo_sobral@2
   945
  }
leo_sobral@2
   946
leo_sobral@2
   947
  /* sets the FileTransfer instance connection (video/audio download) */
rosfran@40
   948
  ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
leo_sobral@2
   949
leo_sobral@2
   950
  if ( ret == FALSE ) {
leo_sobral@2
   951
#ifndef GST_DISABLE_GST_DEBUG  
leo_sobral@2
   952
    if ( src->mythtv_msgs_dbg )
leo_sobral@2
   953
      g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
leo_sobral@2
   954
#endif
leo_sobral@2
   955
    goto begin_req_failed;
leo_sobral@2
   956
  }
leo_sobral@2
   957
leo_sobral@2
   958
  src->content_size = src->file_transfer->filesize;
leo_sobral@2
   959
rosfran@71
   960
  src->do_start = FALSE;
rosfran@90
   961
rosfran@90
   962
  //if ( src->live_tv ) {
rosfran@90
   963
  src->adapter = gst_adapter_new();
rosfran@90
   964
  //src->th_read_ahead = gst_task_create( (GstTaskFunction)gst_mythtv_src_read_ahead, src );
rosfran@90
   965
  //gst_task_set_lock( src->th_read_ahead, &th_mutex );
rosfran@90
   966
  //gst_task_start( src->th_read_ahead );
rosfran@90
   967
  // }
rosfran@87
   968
  //gst_pad_push_event ( GST_BASE_SRC_PAD (bsrc),
rosfran@87
   969
  //	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_BYTES, 0, src->content_size, 0 ) );
rosfran@71
   970
rosfran@90
   971
  src->buffer = g_malloc0 (INTERNAL_BUFFER_SIZE);
rosfran@90
   972
  src->buffer_offset = 0;
rosfran@90
   973
  src->buffer_remain = 0;
rosfran@90
   974
rosfran@30
   975
rosfran@30
   976
done:
rosfran@30
   977
  return TRUE;
rosfran@30
   978
rosfran@30
   979
  /* ERRORS */
rosfran@30
   980
init_failed:
rosfran@30
   981
  {
rosfran@30
   982
    if (src->spawn_livetv != NULL )
rosfran@30
   983
      g_object_unref( src->spawn_livetv );
rosfran@30
   984
rosfran@30
   985
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@30
   986
	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
rosfran@30
   987
    return FALSE;
rosfran@30
   988
  }
rosfran@30
   989
begin_req_failed:
rosfran@30
   990
  {
rosfran@30
   991
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@30
   992
	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
rosfran@30
   993
    return FALSE;
rosfran@30
   994
  }
rosfran@71
   995
change_progchain:
rosfran@71
   996
  {
rosfran@71
   997
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@90
   998
	(NULL), ("Seek failed, go to the next program info... (%s)",
rosfran@90
   999
		 src->uri_name));
rosfran@90
  1000
rosfran@90
  1001
    gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
rosfran@90
  1002
	gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
rosfran@90
  1003
rosfran@90
  1004
    // go to the next program chain
rosfran@90
  1005
    src->unique_setup = FALSE;
rosfran@90
  1006
    src->update_prog_chain = TRUE;
rosfran@90
  1007
rosfran@90
  1008
    gst_mythtv_src_next_program_chain( src );
rosfran@90
  1009
rosfran@71
  1010
    return TRUE;
rosfran@71
  1011
  }
rosfran@30
  1012
}
leo_sobral@2
  1013
rosfran@52
  1014
/* create a new socket for connecting to the next program chain */
rosfran@52
  1015
static gboolean
rosfran@52
  1016
gst_mythtv_src_next_program_chain ( GstMythtvSrc *src )
rosfran@52
  1017
{
rosfran@52
  1018
  GString *chain_id_local = NULL;
rosfran@52
  1019
rosfran@52
  1020
  gboolean ret = TRUE;
rosfran@61
  1021
rosfran@52
  1022
  if ( !src->live_tv )
rosfran@61
  1023
    goto init_failed;
rosfran@70
  1024
    
rosfran@70
  1025
  if (src->unique_setup == FALSE) {
rosfran@70
  1026
    src->unique_setup = TRUE;
rosfran@70
  1027
  } else {
rosfran@70
  1028
    goto done;
rosfran@70
  1029
  }
rosfran@81
  1030
  
rosfran@81
  1031
	GST_PAD_STREAM_LOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
rosfran@61
  1032
rosfran@52
  1033
  if (src->file_transfer) {
rosfran@52
  1034
    g_object_unref (src->file_transfer);
rosfran@52
  1035
    src->file_transfer = NULL;
rosfran@52
  1036
  }
rosfran@61
  1037
rosfran@52
  1038
  if (src->uri_name) {
rosfran@52
  1039
    g_free (src->uri_name);
rosfran@52
  1040
  }
rosfran@61
  1041
rosfran@52
  1042
  if ( src->live_tv ) {
rosfran@61
  1043
    if ( gmyth_livetv_next_program_chain( src->spawn_livetv ) == FALSE ) {
rosfran@70
  1044
    	g_print( "\n\n[%s]\t\tFailed to go to the next program chain!!!\n\n", __FUNCTION__ );
rosfran@61
  1045
      ret = FALSE;
rosfran@61
  1046
      goto init_failed;
rosfran@52
  1047
    }
rosfran@52
  1048
    /* set up the uri variable */
rosfran@52
  1049
    src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
rosfran@52
  1050
    chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
rosfran@52
  1051
    if ( chain_id_local != NULL ) {
rosfran@52
  1052
      src->live_chain_id = g_strdup( chain_id_local->str );
rosfran@52
  1053
      g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
rosfran@52
  1054
    }
rosfran@70
  1055
    src->live_tv_id = src->spawn_livetv->recorder->recorder_num;
rosfran@52
  1056
    g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
rosfran@52
  1057
  }
rosfran@52
  1058
rosfran@52
  1059
  src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
rosfran@52
  1060
      g_string_new( src->uri_name ), -1, src->mythtv_version );
rosfran@52
  1061
rosfran@52
  1062
  if ( src->file_transfer == NULL ) {
rosfran@52
  1063
    goto init_failed;
rosfran@52
  1064
  }
rosfran@52
  1065
rosfran@52
  1066
  /* sets the Playback monitor connection */
rosfran@52
  1067
  ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
rosfran@52
  1068
rosfran@52
  1069
  if ( src->live_tv == TRUE && ret == TRUE ) {
rosfran@52
  1070
    /* loop finished, set the max tries variable to zero again... */
rosfran@52
  1071
    wait_to_transfer = 0;
rosfran@61
  1072
rosfran@61
  1073
    g_usleep( 200 );
rosfran@52
  1074
rosfran@52
  1075
    while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && 
rosfran@70
  1076
				( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE ) )
rosfran@61
  1077
      g_usleep( 1000 );
rosfran@52
  1078
  }
rosfran@52
  1079
rosfran@52
  1080
  /* sets the FileTransfer instance connection (video/audio download) */
rosfran@52
  1081
  ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
rosfran@52
  1082
rosfran@52
  1083
  if ( ret == FALSE ) {
rosfran@52
  1084
#ifndef GST_DISABLE_GST_DEBUG  
rosfran@52
  1085
    if ( src->mythtv_msgs_dbg )
rosfran@52
  1086
      g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
rosfran@52
  1087
#endif
rosfran@52
  1088
    goto begin_req_failed;
rosfran@52
  1089
  }
rosfran@61
  1090
  src->content_size_last = src->content_size;
rosfran@61
  1091
rosfran@61
  1092
#if 0
rosfran@52
  1093
  if ( src->content_size < src->file_transfer->filesize ) {
rosfran@61
  1094
    src->content_size = src->file_transfer->filesize;
rosfran@52
  1095
  } else {
rosfran@61
  1096
    //gint64 pos = gst_mythtv_src_get_position(src);
rosfran@61
  1097
    //if ( pos > src->file_transfer->filesize )
rosfran@61
  1098
    //	src->content_size = pos;  	
rosfran@61
  1099
rosfran@52
  1100
  }
rosfran@61
  1101
#endif
rosfran@61
  1102
rosfran@61
  1103
  src->content_size = src->file_transfer->filesize;
rosfran@87
  1104
  if ( src->live_tv ) {
rosfran@87
  1105
  	wait_to_transfer = 0;
rosfran@87
  1106
	  while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && src->content_size < GMYTHTV_TRANSFER_MAX_BUFFER )
rosfran@87
  1107
	    src->content_size = gst_mythtv_src_get_position( src );
rosfran@87
  1108
  }
rosfran@63
  1109
rosfran@81
  1110
  src->read_offset = 0;  
rosfran@70
  1111
  
rosfran@70
  1112
done:
rosfran@81
  1113
	src->update_prog_chain = FALSE;
rosfran@81
  1114
	
rosfran@81
  1115
	GST_PAD_STREAM_UNLOCK( GST_BASE_SRC_PAD (GST_BASE_SRC (src)) );
rosfran@81
  1116
	
rosfran@52
  1117
  return TRUE;
rosfran@52
  1118
rosfran@52
  1119
  /* ERRORS */
rosfran@52
  1120
init_failed:
rosfran@52
  1121
  {
rosfran@52
  1122
    if (src->spawn_livetv != NULL )
rosfran@52
  1123
      g_object_unref( src->spawn_livetv );
rosfran@52
  1124
rosfran@52
  1125
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@52
  1126
	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
rosfran@52
  1127
    return FALSE;
rosfran@52
  1128
  }
rosfran@52
  1129
begin_req_failed:
rosfran@52
  1130
  {
rosfran@52
  1131
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@52
  1132
	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
rosfran@52
  1133
    return FALSE;
rosfran@52
  1134
  }
rosfran@61
  1135
rosfran@52
  1136
}
rosfran@52
  1137
rosfran@30
  1138
static gboolean
leo_sobral@2
  1139
gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
leo_sobral@2
  1140
{
rosfran@31
  1141
  GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
rosfran@30
  1142
  gboolean ret = TRUE;
rosfran@70
  1143
  g_print( "[%s] Differs from previous content size: %d (max.: %d)\n", __FUNCTION__, 
rosfran@63
  1144
  			abs( src->content_size - src->prev_content_size ), GMYTHTV_TRANSFER_MAX_BUFFER );
leo_sobral@2
  1145
rosfran@87
  1146
  if (src->content_size == -1) {
rosfran@87
  1147
    //ret= FALSE;
rosfran@63
  1148
  } else if ( src->live_tv && ( abs( src->content_size - src->bytes_read ) < 
rosfran@70
  1149
				GMYTHTV_TRANSFER_MAX_BUFFER ) ) {
rosfran@61
  1150
    //g_static_mutex_lock( &update_size_mutex );
rosfran@61
  1151
    //GST_OBJECT_LOCK(src);
rosfran@70
  1152
    
rosfran@61
  1153
    gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@70
  1154
    if ( new_offset > 0 && new_offset > src->content_size ) {
rosfran@70
  1155
			src->content_size = new_offset;
rosfran@70
  1156
    } else if ( new_offset < src->content_size ) {
rosfran@70
  1157
			src->update_prog_chain = TRUE;
rosfran@70
  1158
	  }
rosfran@61
  1159
rosfran@61
  1160
    if ( src->enable_timing_position ) {
rosfran@61
  1161
      gint64 size_tmp = 0;
rosfran@61
  1162
      if (src->live_tv == TRUE) {
rosfran@61
  1163
get_file_pos:
rosfran@61
  1164
	g_usleep( 5 );
rosfran@61
  1165
	size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@61
  1166
	if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@61
  1167
	  src->content_size = size_tmp;
rosfran@61
  1168
	else if ( size_tmp > 0  )
rosfran@61
  1169
	  goto get_file_pos;
rosfran@61
  1170
	g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
rosfran@61
  1171
	    __FUNCTION__, size_tmp );
rosfran@61
  1172
      }
rosfran@61
  1173
    }
rosfran@70
  1174
    
rosfran@70
  1175
    src->prev_content_size = src->content_size;
rosfran@70
  1176
    
rosfran@61
  1177
    //GST_OBJECT_UNLOCK(src);
rosfran@61
  1178
    //g_static_mutex_unlock( &update_size_mutex );
leo_sobral@2
  1179
  }
leo_sobral@2
  1180
leo_sobral@2
  1181
  *size = src->content_size;
rosfran@52
  1182
  g_print( "[%s] Content size = %lld\n", __FUNCTION__, src->content_size );
rosfran@87
  1183
  
rosfran@30
  1184
  return ret;
rosfran@30
  1185
leo_sobral@2
  1186
}
leo_sobral@2
  1187
leo_sobral@2
  1188
/* close the socket and associated resources
leo_sobral@2
  1189
 * used both to recover from errors and go to NULL state */
rosfran@52
  1190
static gboolean
leo_sobral@2
  1191
gst_mythtv_src_stop (GstBaseSrc * bsrc)
leo_sobral@2
  1192
{
leo_sobral@2
  1193
  GstMythtvSrc *src;
leo_sobral@2
  1194
leo_sobral@2
  1195
  src = GST_MYTHTV_SRC (bsrc);
leo_sobral@2
  1196
leo_sobral@2
  1197
  if (src->uri_name) {
leo_sobral@2
  1198
    g_free (src->uri_name);
leo_sobral@2
  1199
    src->uri_name = NULL;
leo_sobral@2
  1200
  }
leo_sobral@2
  1201
leo_sobral@2
  1202
  if (src->mythtv_caps) {
leo_sobral@2
  1203
    gst_caps_unref (src->mythtv_caps);
leo_sobral@2
  1204
    src->mythtv_caps = NULL;
leo_sobral@2
  1205
  }
leo_sobral@2
  1206
leo_sobral@2
  1207
  src->eos = FALSE;
leo_sobral@2
  1208
leo_sobral@2
  1209
  return TRUE;
leo_sobral@2
  1210
}
leo_sobral@2
  1211
rosfran@30
  1212
static gboolean
leo_sobral@2
  1213
gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
leo_sobral@2
  1214
{
leo_sobral@2
  1215
  GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
rosfran@52
  1216
  gint64 cont_size = 0;
rosfran@90
  1217
  gboolean ret = FALSE;
leo_sobral@2
  1218
leo_sobral@2
  1219
  switch (GST_EVENT_TYPE (event)) {
rosfran@30
  1220
#if 0
leo_sobral@2
  1221
    case GST_EVENT_FLUSH_START:
rosfran@52
  1222
      //src->eos = FALSE;
rosfran@52
  1223
      g_print( "\n\n\n[%s]\t\tGot FLUSH_START event!!!\n\n\n", __FUNCTION__ );
rosfran@61
  1224
      cont_size = gst_mythtv_src_get_position (src);
rosfran@61
  1225
      if ( !src->live_tv ) {
rosfran@61
  1226
	if ( cont_size > src->content_size ) {
rosfran@61
  1227
	  src->content_size = cont_size;
rosfran@61
  1228
	  src->eos = FALSE;
rosfran@61
  1229
	} else {
rosfran@61
  1230
	  src->eos = TRUE;
rosfran@61
  1231
	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
rosfran@61
  1232
	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
rosfran@61
  1233
	}
rosfran@61
  1234
      } else {
rosfran@61
  1235
	if ( cont_size <= 0 ) {
rosfran@61
  1236
	  src->update_prog_chain = TRUE;
rosfran@61
  1237
	  src->eos = TRUE;
rosfran@61
  1238
	  src->unique_setup = FALSE;
rosfran@61
  1239
	  src->do_start = TRUE;		  				  		
rosfran@61
  1240
	}		  	
rosfran@61
  1241
      }
rosfran@61
  1242
      break;
leo_sobral@2
  1243
    case GST_EVENT_FLUSH_STOP:
leo_sobral@2
  1244
      src->do_start = TRUE;
leo_sobral@2
  1245
      src->eos = FALSE;
leo_sobral@2
  1246
      gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
rosfran@30
  1247
      //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
rosfran@30
  1248
      break;
rosfran@30
  1249
#endif
rosfran@30
  1250
    case GST_EVENT_EOS:
rosfran@30
  1251
      g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
rosfran@61
  1252
rosfran@61
  1253
      if ( src->live_tv ) {
rosfran@61
  1254
	cont_size = gst_mythtv_src_get_position (src);
rosfran@61
  1255
	if ( cont_size > src->content_size ) {
rosfran@61
  1256
	  src->content_size = cont_size;
rosfran@61
  1257
	  src->eos = FALSE;
rosfran@61
  1258
	} else {
rosfran@61
  1259
	  src->eos = TRUE;
rosfran@61
  1260
	  gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
rosfran@61
  1261
	  gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
rosfran@61
  1262
	}
rosfran@61
  1263
      } else 
rosfran@61
  1264
	src->eos = TRUE;
rosfran@90
  1265
      ret = TRUE;
rosfran@30
  1266
      break;
rosfran@30
  1267
    case GST_EVENT_NEWSEGMENT:
rosfran@30
  1268
      g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
rosfran@90
  1269
      ret = gst_pad_event_default (pad, event);
leo_sobral@2
  1270
      break;
leo_sobral@2
  1271
    case GST_EVENT_SEEK:  	  
leo_sobral@2
  1272
      {
rosfran@90
  1273
	gst_event_ref( event );
rosfran@90
  1274
leo_sobral@2
  1275
	gdouble rate;
leo_sobral@2
  1276
	//gboolean update = TRUE;
leo_sobral@2
  1277
	GstFormat format;
leo_sobral@2
  1278
	GstSeekType cur_type, stop_type;
leo_sobral@2
  1279
	GstSeekFlags flags;
leo_sobral@2
  1280
	gint64 cur = 0, stop = 0;
leo_sobral@2
  1281
	gst_event_parse_seek ( event, &rate, &format,
leo_sobral@2
  1282
	    &flags, &cur_type, &cur,
leo_sobral@2
  1283
	    &stop_type, &stop );
leo_sobral@2
  1284
rosfran@90
  1285
	g_print( "[%s] Got EVENT_SEEK (pos = %lld)!!!\n", __FUNCTION__, cur );
leo_sobral@2
  1286
	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
leo_sobral@2
  1287
	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
leo_sobral@2
  1288
	}
rosfran@90
  1289
	if ( format == GST_FORMAT_TIME && ( ret = gst_pad_event_default (pad, event) ) == FALSE ) {
rosfran@90
  1290
	  gst_event_unref( event );
rosfran@90
  1291
	  break;
rosfran@90
  1292
	}
rosfran@90
  1293
	
rosfran@90
  1294
	break;
rosfran@30
  1295
      }
leo_sobral@2
  1296
    default:
rosfran@90
  1297
      ret = gst_pad_event_default (pad, event);
leo_sobral@2
  1298
  }
leo_sobral@2
  1299
rosfran@90
  1300
  return ret;
leo_sobral@2
  1301
}
leo_sobral@2
  1302
rosfran@30
  1303
static gboolean
rosfran@30
  1304
gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
leo_sobral@2
  1305
{
leo_sobral@2
  1306
  return TRUE;
leo_sobral@2
  1307
}
rosfran@87
  1308
rosfran@87
  1309
static gboolean
rosfran@87
  1310
gst_mythtv_src_handle_query (GstPad * pad, GstQuery * query)
rosfran@87
  1311
{
rosfran@87
  1312
  gboolean res = FALSE;
rosfran@87
  1313
  GstMythtvSrc *myth = GST_MYTHTV_SRC (gst_pad_get_parent (pad));
rosfran@87
  1314
rosfran@87
  1315
  switch (GST_QUERY_TYPE (query)) {
rosfran@87
  1316
    case GST_QUERY_POSITION:
rosfran@87
  1317
      gst_query_set_position (query, GST_FORMAT_BYTES,
rosfran@87
  1318
	  myth->read_offset );
rosfran@87
  1319
      res = TRUE;
rosfran@87
  1320
      GST_DEBUG_OBJECT (myth, "POS %d", myth->read_offset);
rosfran@87
  1321
      break;
rosfran@87
  1322
    case GST_QUERY_DURATION:
rosfran@87
  1323
#if 0
rosfran@87
  1324
      if (myth->duration != 0) {
rosfran@87
  1325
	gint64 total;
rosfran@87
  1326
	gint64 fps;
rosfran@87
  1327
rosfran@87
  1328
	fps = nuv->h->i_fpsn / nuv->h->i_fpsd;
rosfran@87
  1329
	total = gst_util_uint64_scale_int (GST_SECOND, nuv->h->i_video_blocks, fps);
rosfran@81
  1330
#endif
rosfran@87
  1331
	//gst_query_set_duration (query, GST_FORMAT_TIME, myth->content_size);
rosfran@87
  1332
	GST_DEBUG_OBJECT (myth, "DURATION %d", myth->content_size);
rosfran@87
  1333
	res = FALSE;
rosfran@70
  1334
      break;
rosfran@70
  1335
    default:
rosfran@87
  1336
      res = FALSE;
rosfran@87
  1337
      break;
rosfran@70
  1338
  }
rosfran@61
  1339
rosfran@87
  1340
  gst_object_unref (myth);
rosfran@61
  1341
rosfran@70
  1342
  return res;
rosfran@61
  1343
}
rosfran@61
  1344
rosfran@52
  1345
static GstStateChangeReturn
rosfran@52
  1346
gst_mythtv_src_change_state (GstElement * element, GstStateChange transition)
rosfran@52
  1347
{
rosfran@87
  1348
  GstStateChangeReturn ret = GST_STATE_CHANGE_FAILURE;//GST_STATE_CHANGE_NO_PREROLL;
rosfran@52
  1349
  GstMythtvSrc *src = GST_MYTHTV_SRC (element);
rosfran@52
  1350
rosfran@52
  1351
  switch (transition) {
rosfran@52
  1352
    case GST_STATE_CHANGE_NULL_TO_READY:
rosfran@87
  1353
      //src->do_start = TRUE;
rosfran@87
  1354
      //src->unique_setup = FALSE;
rosfran@52
  1355
      break;
rosfran@52
  1356
    case GST_STATE_CHANGE_READY_TO_PAUSED:
rosfran@52
  1357
    case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
rosfran@87
  1358
      //src->eos = FALSE;
rosfran@52
  1359
      break;
rosfran@52
  1360
    default:
rosfran@52
  1361
      break;
rosfran@52
  1362
  }
rosfran@61
  1363
rosfran@61
  1364
  ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
rosfran@52
  1365
  if (ret == GST_STATE_CHANGE_FAILURE)
rosfran@52
  1366
    return ret;
rosfran@52
  1367
rosfran@52
  1368
  switch (transition) {
rosfran@52
  1369
    case GST_STATE_CHANGE_READY_TO_NULL:
rosfran@63
  1370
      g_print( "[%s] READY to NULL called!\n", __FUNCTION__ );
rosfran@52
  1371
      break;
rosfran@61
  1372
    case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
rosfran@63
  1373
      g_print( "[%s] PLAYING to PAUSED called!\n", __FUNCTION__ );
rosfran@52
  1374
    case GST_STATE_CHANGE_PAUSED_TO_READY:
rosfran@63
  1375
      g_print( "[%s] PAUSED to READY called!\n", __FUNCTION__ );
rosfran@61
  1376
      if ( src->live_tv && src->update_prog_chain ) {
rosfran@81
  1377
      	
rosfran@81
  1378
  			gst_pad_push_event ( GST_BASE_SRC_PAD (GST_BASE_SRC (src)),
rosfran@81
  1379
      			gst_event_new_new_segment (TRUE, 1.0, GST_FORMAT_TIME, 0, -1, 0 ) );
rosfran@81
  1380
rosfran@70
  1381
				src->read_offset = 0;
rosfran@70
  1382
				src->bytes_read = 0;
rosfran@81
  1383
				src->unique_setup = FALSE;				
rosfran@70
  1384
				gst_mythtv_src_next_program_chain( src );
rosfran@61
  1385
      }
rosfran@61
  1386
      break;
rosfran@52
  1387
    default:
rosfran@52
  1388
      break;
rosfran@52
  1389
  }
rosfran@52
  1390
rosfran@52
  1391
  return ret;
rosfran@52
  1392
}
rosfran@52
  1393
rosfran@30
  1394
static void
leo_sobral@2
  1395
gst_mythtv_src_set_property (GObject * object, guint prop_id,
leo_sobral@2
  1396
    const GValue * value, GParamSpec * pspec)
leo_sobral@2
  1397
{
leo_sobral@2
  1398
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
  1399
leo_sobral@2
  1400
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
  1401
  switch (prop_id) {
leo_sobral@2
  1402
    case PROP_URI:
leo_sobral@2
  1403
    case PROP_LOCATION:
leo_sobral@2
  1404
      {
leo_sobral@2
  1405
	if (!g_value_get_string (value)) {
leo_sobral@2
  1406
	  GST_WARNING ("location property cannot be NULL");
leo_sobral@2
  1407
	  goto done;
leo_sobral@2
  1408
	}
leo_sobral@2
  1409
leo_sobral@2
  1410
	if (mythtvsrc->uri_name != NULL) {
leo_sobral@2
  1411
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
  1412
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
  1413
	}
leo_sobral@2
  1414
	mythtvsrc->uri_name = g_value_dup_string (value);
leo_sobral@2
  1415
leo_sobral@2
  1416
	break;
leo_sobral@2
  1417
      }
leo_sobral@2
  1418
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
  1419
    case PROP_GMYTHTV_DBG:
leo_sobral@2
  1420
      {
leo_sobral@2
  1421
	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
leo_sobral@2
  1422
	break;
leo_sobral@2
  1423
      }
leo_sobral@2
  1424
#endif
rosfran@40
  1425
    case PROP_GMYTHTV_VERSION:
leo_sobral@2
  1426
      {
leo_sobral@2
  1427
	mythtvsrc->mythtv_version = g_value_get_int (value);
leo_sobral@2
  1428
	break;
leo_sobral@2
  1429
      }
rosfran@40
  1430
    case PROP_GMYTHTV_LIVEID:
leo_sobral@2
  1431
      {
leo_sobral@2
  1432
	mythtvsrc->live_tv_id = g_value_get_int (value);
leo_sobral@2
  1433
	break;
leo_sobral@2
  1434
      }
rosfran@40
  1435
    case PROP_GMYTHTV_LIVE:
leo_sobral@2
  1436
      {
leo_sobral@2
  1437
	mythtvsrc->live_tv = g_value_get_boolean (value);
leo_sobral@2
  1438
	break;
leo_sobral@2
  1439
      }
rosfran@52
  1440
    case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
rosfran@52
  1441
      {
rosfran@52
  1442
	mythtvsrc->enable_timing_position = g_value_get_boolean (value);
rosfran@52
  1443
	break;
rosfran@52
  1444
      }      
rosfran@40
  1445
    case PROP_GMYTHTV_LIVE_CHAINID:
leo_sobral@2
  1446
      {
leo_sobral@2
  1447
	if (!g_value_get_string (value)) {
leo_sobral@2
  1448
	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
leo_sobral@2
  1449
	  goto done;
leo_sobral@2
  1450
	}
leo_sobral@2
  1451
leo_sobral@2
  1452
	if (mythtvsrc->live_chain_id != NULL) {
leo_sobral@2
  1453
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
  1454
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
  1455
	}
leo_sobral@2
  1456
	mythtvsrc->live_chain_id = g_value_dup_string (value);
leo_sobral@2
  1457
	break;
leo_sobral@2
  1458
      }
rosfran@70
  1459
    case PROP_GMYTHTV_CHANNEL_NUM:
rosfran@70
  1460
      {
rosfran@70
  1461
	mythtvsrc->channel_num = g_value_get_int (value);
rosfran@70
  1462
	break;
rosfran@70
  1463
      }
leo_sobral@2
  1464
    default:
leo_sobral@2
  1465
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
  1466
      break;
leo_sobral@2
  1467
  }
leo_sobral@2
  1468
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
  1469
done:
leo_sobral@2
  1470
  return;
leo_sobral@2
  1471
}
leo_sobral@2
  1472
leo_sobral@2
  1473
  static void
leo_sobral@2
  1474
gst_mythtv_src_get_property (GObject * object, guint prop_id,
leo_sobral@2
  1475
    GValue * value, GParamSpec * pspec)
leo_sobral@2
  1476
{
leo_sobral@2
  1477
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
  1478
leo_sobral@2
  1479
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
  1480
  switch (prop_id) {
leo_sobral@2
  1481
    case PROP_URI:
leo_sobral@2
  1482
    case PROP_LOCATION:
leo_sobral@2
  1483
      {
leo_sobral@2
  1484
	gchar *str = g_strdup( "" );
leo_sobral@2
  1485
leo_sobral@2
  1486
	if ( mythtvsrc->uri_name == NULL ) {
leo_sobral@2
  1487
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
  1488
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
  1489
	} else {
leo_sobral@2
  1490
	  str = g_strdup( mythtvsrc->uri_name );
leo_sobral@2
  1491
	}
leo_sobral@2
  1492
	g_value_set_string ( value, str );
leo_sobral@2
  1493
	break;
leo_sobral@2
  1494
      }
leo_sobral@2
  1495
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
  1496
    case PROP_GMYTHTV_DBG:
leo_sobral@2
  1497
      g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
leo_sobral@2
  1498
      break;
leo_sobral@2
  1499
#endif
rosfran@40
  1500
    case PROP_GMYTHTV_VERSION:
leo_sobral@2
  1501
      {
leo_sobral@2
  1502
	g_value_set_int ( value, mythtvsrc->mythtv_version );
leo_sobral@2
  1503
	break;
leo_sobral@2
  1504
      }
rosfran@40
  1505
    case PROP_GMYTHTV_LIVEID:
leo_sobral@2
  1506
      {
leo_sobral@2
  1507
	g_value_set_int ( value, mythtvsrc->live_tv_id );
leo_sobral@2
  1508
	break;
leo_sobral@2
  1509
      }
rosfran@40
  1510
    case PROP_GMYTHTV_LIVE:
leo_sobral@2
  1511
      g_value_set_boolean ( value, mythtvsrc->live_tv );
leo_sobral@2
  1512
      break;
rosfran@52
  1513
    case PROP_GMYTHTV_ENABLE_TIMING_POSITION:
rosfran@52
  1514
      g_value_set_boolean ( value, mythtvsrc->enable_timing_position );
rosfran@52
  1515
      break;
rosfran@40
  1516
    case PROP_GMYTHTV_LIVE_CHAINID:
leo_sobral@2
  1517
      {
leo_sobral@2
  1518
	gchar *str = g_strdup( "" );
leo_sobral@2
  1519
leo_sobral@2
  1520
	if ( mythtvsrc->live_chain_id == NULL ) {
leo_sobral@2
  1521
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
  1522
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
  1523
	} else {
leo_sobral@2
  1524
	  str = g_strdup( mythtvsrc->live_chain_id );
leo_sobral@2
  1525
	}
leo_sobral@2
  1526
	g_value_set_string ( value, str );
leo_sobral@2
  1527
	break;
leo_sobral@2
  1528
      }
rosfran@70
  1529
    case PROP_GMYTHTV_CHANNEL_NUM:
rosfran@70
  1530
      {
rosfran@70
  1531
	g_value_set_int ( value, mythtvsrc->channel_num );
rosfran@70
  1532
	break;
rosfran@70
  1533
      }
leo_sobral@2
  1534
    default:
leo_sobral@2
  1535
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
  1536
      break;
leo_sobral@2
  1537
  }
leo_sobral@2
  1538
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
  1539
}
leo_sobral@2
  1540
leo_sobral@2
  1541
/* entry point to initialize the plug-in
leo_sobral@2
  1542
 * initialize the plug-in itself
leo_sobral@2
  1543
 * register the element factories and pad templates
leo_sobral@2
  1544
 * register the features
leo_sobral@2
  1545
 */
rosfran@40
  1546
static gboolean
leo_sobral@2
  1547
plugin_init (GstPlugin * plugin)
leo_sobral@2
  1548
{
leo_sobral@2
  1549
  return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
leo_sobral@2
  1550
      GST_TYPE_MYTHTV_SRC);
leo_sobral@2
  1551
}
leo_sobral@2
  1552
leo_sobral@2
  1553
/* this is the structure that gst-register looks for
leo_sobral@2
  1554
 * so keep the name plugin_desc, or you cannot get your plug-in registered */
leo_sobral@2
  1555
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
leo_sobral@2
  1556
    GST_VERSION_MINOR,
leo_sobral@2
  1557
    "mythtv",
leo_sobral@2
  1558
    "lib MythTV src",
leo_sobral@2
  1559
    plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
leo_sobral@2
  1560
leo_sobral@2
  1561
leo_sobral@2
  1562
/*** GSTURIHANDLER INTERFACE *************************************************/
leo_sobral@2
  1563
  static guint 
leo_sobral@2
  1564
gst_mythtv_src_uri_get_type (void)
leo_sobral@2
  1565
{
leo_sobral@2
  1566
  return GST_URI_SRC;
leo_sobral@2
  1567
}
leo_sobral@2
  1568
leo_sobral@2
  1569
  static gchar **
leo_sobral@2
  1570
gst_mythtv_src_uri_get_protocols (void)
leo_sobral@2
  1571
{
leo_sobral@2
  1572
  static gchar *protocols[] = { "myth", "myths", NULL };
leo_sobral@2
  1573
leo_sobral@2
  1574
  return protocols;
leo_sobral@2
  1575
}
leo_sobral@2
  1576
leo_sobral@2
  1577
  static const gchar *
leo_sobral@2
  1578
gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
leo_sobral@2
  1579
{
leo_sobral@2
  1580
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
  1581
leo_sobral@2
  1582
  return src->uri_name;
leo_sobral@2
  1583
}
leo_sobral@2
  1584
leo_sobral@2
  1585
  static gboolean
leo_sobral@2
  1586
gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
leo_sobral@2
  1587
{
leo_sobral@2
  1588
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
  1589
leo_sobral@2
  1590
  gchar *protocol;
leo_sobral@2
  1591
leo_sobral@2
  1592
  protocol = gst_uri_get_protocol (uri);
leo_sobral@2
  1593
  if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
leo_sobral@2
  1594
    g_free (protocol);
leo_sobral@2
  1595
    return FALSE;
leo_sobral@2
  1596
  }
leo_sobral@2
  1597
  g_free (protocol);
leo_sobral@2
  1598
  g_object_set (src, "location", uri, NULL);
leo_sobral@2
  1599
leo_sobral@2
  1600
  return TRUE;
leo_sobral@2
  1601
}
leo_sobral@2
  1602
rosfran@87
  1603
 static void
leo_sobral@2
  1604
gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
leo_sobral@2
  1605
{
leo_sobral@2
  1606
  GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
leo_sobral@2
  1607
leo_sobral@2
  1608
  iface->get_type = gst_mythtv_src_uri_get_type;
leo_sobral@2
  1609
  iface->get_protocols = gst_mythtv_src_uri_get_protocols;
leo_sobral@2
  1610
  iface->get_uri = gst_mythtv_src_uri_get_uri;
leo_sobral@2
  1611
  iface->set_uri = gst_mythtv_src_uri_set_uri;
leo_sobral@2
  1612
}
leo_sobral@2
  1613
leo_sobral@2
  1614
  void
leo_sobral@2
  1615
size_header_handler (void *userdata, const char *value)
leo_sobral@2
  1616
{
leo_sobral@2
  1617
  GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
leo_sobral@2
  1618
leo_sobral@2
  1619
  //src->content_size = g_ascii_strtoull (value, NULL, 10);
leo_sobral@2
  1620
leo_sobral@2
  1621
  GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
leo_sobral@2
  1622
}