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