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