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