gst-plugins-mythtv/src/gstmythtvsrc.c
author rosfran
Mon Oct 23 20:02:26 2006 +0100 (2006-10-23)
branchtrunk
changeset 43 c9e53d7250f8
parent 37 324e04989738
child 44 7a654d6de88f
permissions -rwxr-xr-x
[svn r44] Some fixes.
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@40
    21
#include "gmyth_file_transfer.h"
rosfran@40
    22
#include "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@40
    33
#define GST_GMYTHTV_ID_NUM		1
leo_sobral@2
    34
rosfran@40
    35
#define GMYTHTV_VERSION_DEFAULT		30
leo_sobral@2
    36
rosfran@40
    37
#define GMYTHTV_TRANSFER_MAX_WAITS	100
leo_sobral@2
    38
rosfran@40
    39
#define GMYTHTV_TRANSFER_MAX_BUFFER	1024*1024
rosfran@30
    40
//( 32*1024  )
leo_sobral@2
    41
leo_sobral@2
    42
/* 4*1024 ??? */
rosfran@30
    43
#define MAX_READ_SIZE              	12*1024
rosfran@30
    44
//( 32*1024 )
leo_sobral@2
    45
rosfran@34
    46
#define ENABLE_TIMING_POSITION		0
leo_sobral@2
    47
leo_sobral@2
    48
/* stablish a maximum iteration value to the IS_RECORDING message */
leo_sobral@2
    49
static guint wait_to_transfer = 0;
leo_sobral@2
    50
leo_sobral@2
    51
static const GstElementDetails gst_mythtv_src_details =
rosfran@30
    52
GST_ELEMENT_DETAILS ( "MythTV client source",
leo_sobral@2
    53
    "Source/Network",
leo_sobral@2
    54
    "Control and receive data as a client over the network via raw socket connections using the MythTV protocol",
rosfran@30
    55
    "Rosfran Borges <rosfran.borges@indt.org.br>" );
leo_sobral@2
    56
leo_sobral@2
    57
static GstStaticPadTemplate srctemplate = GST_STATIC_PAD_TEMPLATE ("src",
leo_sobral@2
    58
    GST_PAD_SRC,
leo_sobral@2
    59
    GST_PAD_ALWAYS,
rosfran@30
    60
    GST_STATIC_CAPS ("video/x-nuv") );
rosfran@30
    61
    //GST_STATIC_CAPS_ANY);
rosfran@30
    62
rosfran@30
    63
static GThread *update_size_task = NULL;
rosfran@30
    64
rosfran@30
    65
static GStaticMutex update_size_mutex = G_STATIC_MUTEX_INIT;
leo_sobral@2
    66
leo_sobral@2
    67
enum
leo_sobral@2
    68
{
leo_sobral@2
    69
  PROP_0,
leo_sobral@2
    70
  PROP_LOCATION,
leo_sobral@2
    71
  PROP_URI,
leo_sobral@2
    72
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
    73
  PROP_GMYTHTV_DBG,
leo_sobral@2
    74
#endif
rosfran@40
    75
  PROP_GMYTHTV_VERSION,
rosfran@40
    76
  PROP_GMYTHTV_LIVE,
rosfran@40
    77
  PROP_GMYTHTV_LIVEID,
rosfran@40
    78
  PROP_GMYTHTV_LIVE_CHAINID
leo_sobral@2
    79
};
leo_sobral@2
    80
leo_sobral@2
    81
static void gst_mythtv_src_finalize (GObject * gobject);
leo_sobral@2
    82
rosfran@30
    83
static GstFlowReturn gst_mythtv_src_create (GstBaseSrc * psrc, guint64 offset, 
rosfran@30
    84
	guint size, GstBuffer ** outbuf);
rosfran@30
    85
//static GstFlowReturn gst_mythtv_src_create (GstPushSrc * psrc, GstBuffer ** outbuf);
leo_sobral@2
    86
static gboolean gst_mythtv_src_start (GstBaseSrc * bsrc);
leo_sobral@2
    87
static gboolean gst_mythtv_src_stop (GstBaseSrc * bsrc);
leo_sobral@2
    88
static gboolean gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size);
rosfran@30
    89
static gboolean gst_mythtv_src_is_seekable( GstBaseSrc *push_src );
rosfran@30
    90
//static gboolean gst_mythtv_new_segment ( GstBaseSrc * psrc );
leo_sobral@2
    91
leo_sobral@2
    92
static void gst_mythtv_src_set_property (GObject * object, guint prop_id,
leo_sobral@2
    93
    const GValue * value, GParamSpec * pspec);
leo_sobral@2
    94
static void gst_mythtv_src_get_property (GObject * object, guint prop_id,
leo_sobral@2
    95
    GValue * value, GParamSpec * pspec);
leo_sobral@2
    96
rosfran@30
    97
//static GstFlowReturn gst_mythtv_src_chain (GstPad * pad, GstBuffer * buf);
leo_sobral@2
    98
rosfran@30
    99
static void gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data);
rosfran@30
   100
rosfran@31
   101
static gboolean gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event);
rosfran@30
   102
//static gboolean gst_mythtv_src_query ( GstPad * pad, GstQuery * query );
rosfran@30
   103
rosfran@37
   104
static gint do_read_request_response (GstMythtvSrc *src, guint64 offset, 
rosfran@30
   105
		guint size, GstBuffer **outbuf);
rosfran@30
   106
//static gboolean gst_mythtv_src_sink_activate_pull (GstPad * srcpad, gboolean active);
leo_sobral@2
   107
leo_sobral@2
   108
  static void
leo_sobral@2
   109
_urihandler_init (GType type)
leo_sobral@2
   110
{
leo_sobral@2
   111
  static const GInterfaceInfo urihandler_info = {
leo_sobral@2
   112
    gst_mythtv_src_uri_handler_init,
leo_sobral@2
   113
    NULL,
leo_sobral@2
   114
    NULL
leo_sobral@2
   115
  };
leo_sobral@2
   116
leo_sobral@2
   117
  g_type_add_interface_static (type, GST_TYPE_URI_HANDLER, &urihandler_info);
leo_sobral@2
   118
leo_sobral@2
   119
  GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
leo_sobral@2
   120
      "MythTV src");
leo_sobral@2
   121
}
leo_sobral@2
   122
leo_sobral@2
   123
GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstBaseSrc,
rosfran@30
   124
    GST_TYPE_BASE_SRC, _urihandler_init)
rosfran@30
   125
    
rosfran@30
   126
//GST_BOILERPLATE_FULL (GstMythtvSrc, gst_mythtv_src, GstPushSrc,
rosfran@30
   127
//    GST_TYPE_PUSH_SRC, _urihandler_init)
rosfran@30
   128
    
leo_sobral@2
   129
  static void
leo_sobral@2
   130
gst_mythtv_src_base_init (gpointer g_class)
leo_sobral@2
   131
{
leo_sobral@2
   132
  GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
leo_sobral@2
   133
leo_sobral@2
   134
  gst_element_class_add_pad_template (element_class,
leo_sobral@2
   135
      gst_static_pad_template_get (&srctemplate));
leo_sobral@2
   136
leo_sobral@2
   137
  gst_element_class_set_details (element_class, &gst_mythtv_src_details);
leo_sobral@2
   138
}
leo_sobral@2
   139
rosfran@30
   140
static void
leo_sobral@2
   141
gst_mythtv_src_class_init (GstMythtvSrcClass * klass)
leo_sobral@2
   142
{
leo_sobral@2
   143
  GObjectClass *gobject_class;
rosfran@30
   144
  //GstPushSrcClass *gstpushsrc_class;
leo_sobral@2
   145
  GstBaseSrcClass *gstbasesrc_class;
leo_sobral@2
   146
leo_sobral@2
   147
  gobject_class = (GObjectClass *) klass;
leo_sobral@2
   148
  gstbasesrc_class = (GstBaseSrcClass *) klass;
rosfran@30
   149
  //gstpushsrc_class = (GstPushSrcClass *) klass;
leo_sobral@2
   150
leo_sobral@2
   151
  gobject_class->set_property = gst_mythtv_src_set_property;
leo_sobral@2
   152
  gobject_class->get_property = gst_mythtv_src_get_property;
leo_sobral@2
   153
  gobject_class->finalize = gst_mythtv_src_finalize;
leo_sobral@2
   154
leo_sobral@2
   155
  g_object_class_install_property
leo_sobral@2
   156
    (gobject_class, PROP_LOCATION,
leo_sobral@2
   157
     g_param_spec_string ("location", "Location",
leo_sobral@2
   158
       "The location. In the form:"
leo_sobral@2
   159
       "\n\t\t\tmyth://a.com/file.nuv"
leo_sobral@2
   160
       "\n\t\t\tmyth://a.com:23223/file.nuv "
leo_sobral@2
   161
       "\n\t\t\ta.com/file.nuv - default scheme 'myth'",
leo_sobral@2
   162
       "", G_PARAM_READWRITE));
leo_sobral@2
   163
leo_sobral@2
   164
  g_object_class_install_property
leo_sobral@2
   165
    (gobject_class, PROP_URI,
leo_sobral@2
   166
     g_param_spec_string ("uri", "Uri",
leo_sobral@2
   167
       "The location in form of a URI (deprecated; use location)",
leo_sobral@2
   168
       "", G_PARAM_READWRITE));
leo_sobral@2
   169
leo_sobral@2
   170
  g_object_class_install_property
rosfran@40
   171
    (gobject_class, PROP_GMYTHTV_VERSION,
leo_sobral@2
   172
     g_param_spec_int ("mythtv-version", "mythtv-version",
leo_sobral@2
   173
       "Change Myth TV version",
leo_sobral@2
   174
       26, 30, 26, G_PARAM_READWRITE));
leo_sobral@2
   175
leo_sobral@2
   176
  g_object_class_install_property
rosfran@40
   177
    (gobject_class, PROP_GMYTHTV_LIVEID,
leo_sobral@2
   178
     g_param_spec_int ("mythtv-live-id", "mythtv-live-id",
leo_sobral@2
   179
       "Change Myth TV version",
rosfran@40
   180
       0, 200, GST_GMYTHTV_ID_NUM, G_PARAM_READWRITE));
leo_sobral@2
   181
leo_sobral@2
   182
  g_object_class_install_property
rosfran@40
   183
    (gobject_class, PROP_GMYTHTV_LIVE_CHAINID,
leo_sobral@2
   184
     g_param_spec_string ("mythtv-live-chainid", "mythtv-live-chainid",
leo_sobral@2
   185
       "Sets the Myth TV chain ID (from TV Chain)",
leo_sobral@2
   186
       "", G_PARAM_READWRITE));
leo_sobral@2
   187
leo_sobral@2
   188
  g_object_class_install_property
rosfran@40
   189
    (gobject_class, PROP_GMYTHTV_LIVE,
leo_sobral@2
   190
     g_param_spec_boolean ("mythtv-live", "mythtv-live",
leo_sobral@2
   191
       "Enable MythTV Live TV content streaming",
leo_sobral@2
   192
       FALSE, G_PARAM_READWRITE));
leo_sobral@2
   193
leo_sobral@2
   194
#ifndef GST_DISABLE_GST_DEBUG
leo_sobral@2
   195
  g_object_class_install_property
rosfran@40
   196
    (gobject_class, PROP_GMYTHTV_DBG,
leo_sobral@2
   197
     g_param_spec_boolean ("mythtv-debug", "mythtv-debug",
leo_sobral@2
   198
       "Enable MythTV debug messages",
leo_sobral@2
   199
       FALSE, G_PARAM_READWRITE));
leo_sobral@2
   200
#endif
leo_sobral@2
   201
leo_sobral@2
   202
  gstbasesrc_class->start = gst_mythtv_src_start;
leo_sobral@2
   203
  gstbasesrc_class->stop = gst_mythtv_src_stop;
leo_sobral@2
   204
  gstbasesrc_class->get_size = gst_mythtv_src_get_size;
leo_sobral@2
   205
  gstbasesrc_class->is_seekable = gst_mythtv_src_is_seekable;
rosfran@30
   206
  //gstpushsrc_class->newsegment = gst_mythtv_src_new_segment;
leo_sobral@2
   207
leo_sobral@2
   208
  gstbasesrc_class->create = gst_mythtv_src_create;
rosfran@30
   209
  
leo_sobral@2
   210
  GST_DEBUG_CATEGORY_INIT (mythtvsrc_debug, "mythtvsrc", 0,
leo_sobral@2
   211
      "MythTV Client Source");
leo_sobral@2
   212
}
leo_sobral@2
   213
rosfran@30
   214
static void
leo_sobral@2
   215
gst_mythtv_src_init (GstMythtvSrc * this, GstMythtvSrcClass * g_class)
leo_sobral@2
   216
{
leo_sobral@2
   217
  this->file_transfer = NULL;
leo_sobral@2
   218
leo_sobral@2
   219
  this->unique_setup = FALSE;
leo_sobral@2
   220
rosfran@40
   221
  this->mythtv_version = GMYTHTV_VERSION_DEFAULT;
leo_sobral@2
   222
leo_sobral@2
   223
  this->bytes_read = 0;
leo_sobral@2
   224
leo_sobral@2
   225
  this->content_size = -1;
leo_sobral@2
   226
  this->read_offset = 0;
leo_sobral@2
   227
leo_sobral@2
   228
  this->live_tv = FALSE;
leo_sobral@2
   229
leo_sobral@2
   230
  this->user_agent = g_strdup ("mythtvsrc");
rosfran@30
   231
  this->mythtv_caps = NULL;  
rosfran@30
   232
  
rosfran@30
   233
  gst_base_src_set_format( GST_BASE_SRC( this ), GST_FORMAT_BYTES );  
leo_sobral@2
   234
rosfran@30
   235
  gst_base_src_set_live ( GST_BASE_SRC( this ), TRUE );
rosfran@30
   236
rosfran@30
   237
  //gst_pad_set_chain_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
rosfran@30
   238
  //    gst_mythtv_src_chain );
rosfran@30
   239
rosfran@30
   240
  gst_pad_set_event_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
rosfran@30
   241
      gst_mythtv_src_handle_event );
rosfran@31
   242
/*
rosfran@30
   243
  gst_pad_set_query_function ( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
rosfran@30
   244
      gst_mythtv_src_query );
rosfran@30
   245
*/
rosfran@30
   246
  //gst_pad_set_activatepull_function( GST_BASE_SRC_PAD(GST_BASE_SRC(this)),
rosfran@30
   247
  //    gst_mythtv_src_sink_activate_pull );
rosfran@30
   248
leo_sobral@2
   249
}
leo_sobral@2
   250
rosfran@30
   251
static void
leo_sobral@2
   252
gst_mythtv_src_finalize (GObject * gobject)
leo_sobral@2
   253
{
leo_sobral@2
   254
  GstMythtvSrc *this = GST_MYTHTV_SRC (gobject);
leo_sobral@2
   255
leo_sobral@2
   256
  g_free (this->user_agent);
leo_sobral@2
   257
leo_sobral@2
   258
  if (this->mythtv_caps) {
leo_sobral@2
   259
    gst_caps_unref (this->mythtv_caps);
leo_sobral@2
   260
    this->mythtv_caps = NULL;
leo_sobral@2
   261
  }
leo_sobral@2
   262
leo_sobral@2
   263
  if (this->file_transfer) {
leo_sobral@2
   264
    g_object_unref (this->file_transfer);
leo_sobral@2
   265
    this->file_transfer = NULL;
leo_sobral@2
   266
  }
leo_sobral@2
   267
leo_sobral@2
   268
  if (this->uri_name) {
leo_sobral@2
   269
    g_free (this->uri_name);
leo_sobral@2
   270
  }
leo_sobral@2
   271
leo_sobral@2
   272
  if (this->user_agent) {
leo_sobral@2
   273
    g_free (this->user_agent);
leo_sobral@2
   274
  }
leo_sobral@2
   275
leo_sobral@2
   276
  G_OBJECT_CLASS (parent_class)->finalize (gobject);
leo_sobral@2
   277
}
leo_sobral@2
   278
rosfran@37
   279
static gint
rosfran@30
   280
do_read_request_response (GstMythtvSrc * src, guint64 offset, guint size, GstBuffer **outbuf)
leo_sobral@2
   281
{
rosfran@37
   282
  gint read = 0;
leo_sobral@2
   283
  guint sizetoread = size; //GST_BUFFER_SIZE (outbuf);
rosfran@31
   284
  //GstBuffer *buffer = gst_buffer_new_and_alloc( size ); 
leo_sobral@2
   285
rosfran@30
   286
  g_print( "Starting: [%s] Reading %d bytes...\n", __FUNCTION__, sizetoread ); 
leo_sobral@2
   287
rosfran@30
   288
  /* Loop sending the Myth File Transfer request:
leo_sobral@2
   289
   * Retry whilst authentication fails and we supply it. */
rosfran@37
   290
  gint len = 0;
leo_sobral@2
   291
rosfran@37
   292
  GST_OBJECT_LOCK(src);
leo_sobral@2
   293
leo_sobral@2
   294
  while ( sizetoread > 0 ) {
rosfran@31
   295
  	
rosfran@40
   296
  	len = gmyth_file_transfer_read( src->file_transfer,
rosfran@31
   297
			GST_BUFFER_DATA( *outbuf ) + read, sizetoread, TRUE );
leo_sobral@2
   298
leo_sobral@2
   299
    if ( len > 0 ) {
rosfran@34
   300
      read += len;      
leo_sobral@2
   301
      sizetoread -= len;
rosfran@30
   302
    } else if ( len <= 0 ) {
rosfran@30
   303
    	
rosfran@30
   304
      if ( src->live_tv == FALSE ) {
rosfran@30
   305
		goto done;
rosfran@34
   306
      } else if ( /*src->content_size >= src->read_offset && 
rosfran@34
   307
      			abs ( src->content_size - src->read_offset ) <= 1024 ) ||*/
rosfran@40
   308
      			( src->content_size <= ( src->read_offset + size + GMYTHTV_TRANSFER_MAX_BUFFER ) ) )
rosfran@34
   309
  {
rosfran@34
   310
#if ENABLE_TIMING_POSITION == 1
rosfran@40
   311
    gint64 size_tmp = 0;
rosfran@34
   312
    if (src->live_tv == TRUE) {
rosfran@34
   313
get_file_pos:
rosfran@40
   314
      size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@40
   315
      if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@34
   316
	src->content_size = size_tmp;
rosfran@40
   317
      else if ( size_tmp > 0 )
rosfran@34
   318
	goto get_file_pos;
rosfran@40
   319
      g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
rosfran@34
   320
	  __FUNCTION__, size_tmp );
rosfran@34
   321
    }
rosfran@34
   322
#else
rosfran@40
   323
  	gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@40
   324
	if ( new_offset > 0 ) {
rosfran@40
   325
	  if ( src->content_size < new_offset ) {
rosfran@40
   326
	    src->content_size = new_offset;
rosfran@40
   327
	  }
rosfran@40
   328
	} else {
rosfran@40
   329
	  src->update_prog_chain = TRUE;
rosfran@40
   330
	}
rosfran@34
   331
#endif
rosfran@34
   332
	goto done;
rosfran@34
   333
  }
rosfran@30
   334
leo_sobral@2
   335
    }
rosfran@31
   336
    
rosfran@30
   337
    if ( read == sizetoread )
leo_sobral@2
   338
      break;
leo_sobral@2
   339
  }
rosfran@30
   340
  
leo_sobral@2
   341
  if ( read > 0 ) {
rosfran@34
   342
  	src->read_offset += read;
leo_sobral@2
   343
    src->bytes_read += read;
rosfran@31
   344
  
leo_sobral@2
   345
  g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
leo_sobral@2
   346
      "OFFSET = %llu, CONTENT SIZE = %llu.\n", __FUNCTION__, read, src->bytes_read, 
leo_sobral@2
   347
      src->read_offset, src->content_size );
rosfran@31
   348
      
rosfran@31
   349
  //GST_BUFFER_TIMESTAMP( buffer ) = GST_BUFFER_TIMESTAMP ( *outbuf );
rosfran@31
   350
  GST_BUFFER_SIZE (*outbuf) = read; //GST_BUFFER_SIZE (buffer) = read;
rosfran@31
   351
  GST_BUFFER_OFFSET (*outbuf) = offset; //GST_BUFFER_OFFSET (buffer) = offset;
rosfran@31
   352
  GST_BUFFER_OFFSET_END (*outbuf) = offset + read;//GST_BUFFER_OFFSET_END (buffer) = offset + read;
rosfran@31
   353
  //memcpy( GST_BUFFER_DATA( *outbuf ), GST_BUFFER_DATA( buffer ), read );
rosfran@30
   354
     
rosfran@30
   355
  g_print( "Stopping: [%s]\t\tBUFFER --->SIZE = %d, OFFSET = %llu, "\
rosfran@30
   356
      "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), 
rosfran@30
   357
      GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
rosfran@31
   358
      
rosfran@34
   359
  } else if ( !src->live_tv )
rosfran@31
   360
  	goto eos;
rosfran@31
   361
  
rosfran@30
   362
  goto done;
leo_sobral@2
   363
leo_sobral@2
   364
eos:
leo_sobral@2
   365
  src->eos = TRUE;
rosfran@30
   366
  
leo_sobral@2
   367
done:
rosfran@37
   368
  GST_OBJECT_UNLOCK(src);
leo_sobral@2
   369
leo_sobral@2
   370
  return read;
leo_sobral@2
   371
}
leo_sobral@2
   372
rosfran@30
   373
static GstFlowReturn
rosfran@30
   374
gst_mythtv_src_create ( GstBaseSrc * psrc, guint64 offset, guint size, GstBuffer **outbuf)
leo_sobral@2
   375
{
leo_sobral@2
   376
  GstMythtvSrc *src;
leo_sobral@2
   377
  GstFlowReturn ret = GST_FLOW_OK;
rosfran@37
   378
  gint read = -1;
rosfran@30
   379
  
rosfran@30
   380
  src = GST_MYTHTV_SRC (psrc);
rosfran@31
   381
  /* The caller should know the number of bytes and not read beyond EOS. */
rosfran@31
   382
  if (G_UNLIKELY (src->eos))
rosfran@31
   383
    goto eos;
rosfran@40
   384
  if ( G_UNLIKELY (src->update_prog_chain) )
rosfran@40
   385
    goto change_progchain;
rosfran@30
   386
  
rosfran@31
   387
  //GST_OBJECT_LOCK(src);
leo_sobral@2
   388
rosfran@30
   389
  if (G_UNLIKELY (src->read_offset != offset)) {
rosfran@40
   390
    gint64 new_offset = gmyth_file_transfer_seek(src->file_transfer, offset, SEEK_SET);
rosfran@40
   391
    g_print( "[%s] SRC Offset = %lld, NEW actual backend SEEK Offset = %lld.\n",
rosfran@34
   392
    	__FUNCTION__, src->read_offset, new_offset );
rosfran@34
   393
    if (G_UNLIKELY (new_offset < 0 ) )//|| new_offset != src->read_offset)) {
rosfran@34
   394
    {
rosfran@31
   395
      //GST_OBJECT_UNLOCK(src);
rosfran@40
   396
      goto change_progchain;
rosfran@31
   397
    }
leo_sobral@2
   398
rosfran@30
   399
    src->read_offset = offset;
rosfran@30
   400
  }
rosfran@31
   401
  //GST_OBJECT_UNLOCK(src);
rosfran@30
   402
  
rosfran@31
   403
  /* Create the buffer. */
rosfran@31
   404
  ret = gst_pad_alloc_buffer (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc)),
rosfran@31
   405
      src->read_offset, size,
rosfran@31
   406
      //src->icy_caps ? src->icy_caps :
rosfran@31
   407
      GST_PAD_CAPS (GST_BASE_SRC_PAD (GST_BASE_SRC (psrc))), outbuf);
leo_sobral@2
   408
rosfran@31
   409
  if (G_UNLIKELY (ret != GST_FLOW_OK))
rosfran@31
   410
    goto done;
rosfran@30
   411
  
rosfran@31
   412
  //g_static_mutex_lock( &update_size_mutex );
rosfran@31
   413
  read = do_read_request_response ( src, src->read_offset, size, outbuf );
rosfran@31
   414
  //g_static_mutex_unlock( &update_size_mutex );  
leo_sobral@2
   415
rosfran@40
   416
  if (G_UNLIKELY (src->update_prog_chain) )
rosfran@40
   417
    goto change_progchain;
rosfran@40
   418
rosfran@31
   419
  if (G_UNLIKELY (read < 0) || *outbuf == NULL) {
rosfran@31
   420
  	//if ( src->live_tv )
rosfran@31
   421
    //	goto done;
rosfran@31
   422
    //else
leo_sobral@2
   423
    goto read_error;
rosfran@31
   424
  }
leo_sobral@2
   425
rosfran@31
   426
/* 
leo_sobral@2
   427
  if (G_UNLIKELY(src->eos))
leo_sobral@2
   428
    goto eos;
rosfran@30
   429
  else
rosfran@30
   430
    goto done;
rosfran@31
   431
*/
leo_sobral@2
   432
leo_sobral@2
   433
done:
rosfran@31
   434
 {
rosfran@31
   435
    const gchar *reason = gst_flow_get_name (ret);
rosfran@31
   436
rosfran@31
   437
    GST_DEBUG_OBJECT (src, "DONE task, reason %s", reason);
rosfran@31
   438
  	return ret;
rosfran@31
   439
 }
leo_sobral@2
   440
eos:
leo_sobral@2
   441
  {
rosfran@30
   442
    const gchar *reason = gst_flow_get_name (ret);
rosfran@30
   443
rosfran@30
   444
    GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
leo_sobral@2
   445
    return GST_FLOW_UNEXPECTED;
leo_sobral@2
   446
  }
leo_sobral@2
   447
  /* ERRORS */
leo_sobral@2
   448
read_error:
leo_sobral@2
   449
  {
leo_sobral@2
   450
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
leo_sobral@2
   451
	(NULL), ("Could not read any bytes (%i, %s)", read,
leo_sobral@2
   452
	  src->uri_name));
leo_sobral@2
   453
    return GST_FLOW_ERROR;
leo_sobral@2
   454
  }
rosfran@40
   455
change_progchain:
rosfran@37
   456
  {
rosfran@37
   457
    GST_ELEMENT_ERROR (src, RESOURCE, READ,
rosfran@37
   458
	(NULL), ("Seek failed, go to the next program info... (%i, %s)", read,
rosfran@37
   459
	  src->uri_name));
rosfran@37
   460
	// go to the next program chain
rosfran@37
   461
	src->unique_setup = FALSE;
rosfran@37
   462
	gst_mythtv_src_start( psrc );
rosfran@37
   463
  
rosfran@37
   464
    return GST_FLOW_OK;
rosfran@37
   465
  }
rosfran@30
   466
leo_sobral@2
   467
}
leo_sobral@2
   468
rosfran@30
   469
void
rosfran@30
   470
update_size_func( void *mythtv_data ) 
leo_sobral@2
   471
{
rosfran@30
   472
  GstMythtvSrc *src;
leo_sobral@2
   473
rosfran@30
   474
  g_return_if_fail( mythtv_data != NULL );
leo_sobral@2
   475
rosfran@30
   476
  src = GST_MYTHTV_SRC ( mythtv_data );
rosfran@30
   477
rosfran@30
   478
  g_static_mutex_lock( &update_size_mutex );
rosfran@30
   479
rosfran@30
   480
  if ( src->do_start ) {
rosfran@30
   481
#if ENABLE_TIMING_POSITION == 1
rosfran@40
   482
    gint64 size_tmp = 0;
rosfran@30
   483
    if (src->live_tv == TRUE) {
rosfran@30
   484
get_file_pos:
rosfran@30
   485
      g_usleep( 50 );
rosfran@40
   486
      size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@40
   487
      if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@30
   488
	src->content_size = size_tmp;
rosfran@40
   489
      else if ( size_tmp > 0 )
rosfran@30
   490
	goto get_file_pos;
rosfran@40
   491
      g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
rosfran@30
   492
	  __FUNCTION__, size_tmp );
rosfran@30
   493
    }
rosfran@30
   494
#endif
leo_sobral@2
   495
  }
rosfran@30
   496
  g_static_mutex_unlock( &update_size_mutex );
leo_sobral@2
   497
leo_sobral@2
   498
}
leo_sobral@2
   499
rosfran@30
   500
guint64
rosfran@30
   501
gst_mythtv_src_get_position ( GstMythtvSrc* src ) 
leo_sobral@2
   502
{
rosfran@30
   503
rosfran@30
   504
  if ( src->do_start ) {
rosfran@30
   505
#if ENABLE_TIMING_POSITION == 1
rosfran@40
   506
    gint64 size_tmp = 0;
rosfran@30
   507
    if (src->live_tv == TRUE) {
rosfran@30
   508
get_file_pos:
rosfran@30
   509
      g_usleep( 50 );
rosfran@40
   510
      size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@40
   511
      if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@30
   512
		src->content_size = size_tmp;
rosfran@40
   513
      else if ( size_tmp > 0 )
rosfran@30
   514
	goto get_file_pos;
rosfran@40
   515
      g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
rosfran@30
   516
	  __FUNCTION__, size_tmp );
rosfran@30
   517
    }
rosfran@30
   518
#endif
rosfran@30
   519
  }
rosfran@30
   520
  
rosfran@30
   521
  return src->content_size;	
rosfran@30
   522
leo_sobral@2
   523
}
leo_sobral@2
   524
leo_sobral@2
   525
/* create a socket for connecting to remote server */
rosfran@30
   526
static gboolean
leo_sobral@2
   527
gst_mythtv_src_start ( GstBaseSrc * bsrc )
leo_sobral@2
   528
{
leo_sobral@2
   529
  GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
leo_sobral@2
   530
leo_sobral@2
   531
  GString *chain_id_local = NULL;
leo_sobral@2
   532
leo_sobral@2
   533
  gboolean ret = TRUE;
rosfran@30
   534
leo_sobral@2
   535
  if (src->unique_setup == FALSE) {
leo_sobral@2
   536
    src->unique_setup = TRUE;
leo_sobral@2
   537
  } else {
leo_sobral@2
   538
    goto done;
leo_sobral@2
   539
  }
leo_sobral@2
   540
leo_sobral@2
   541
  GST_OBJECT_LOCK(src);
leo_sobral@2
   542
leo_sobral@2
   543
  if ( src->live_tv ) {
rosfran@40
   544
    src->spawn_livetv = gmyth_livetv_new( );
rosfran@40
   545
    if ( gmyth_livetv_setup( src->spawn_livetv ) == FALSE ) {
rosfran@30
   546
      ret = FALSE;
rosfran@30
   547
      goto init_failed;
leo_sobral@2
   548
    }
leo_sobral@2
   549
    /* set up the uri variable */
leo_sobral@2
   550
    src->uri_name = g_strdup( src->spawn_livetv->proginfo->pathname->str );
leo_sobral@2
   551
    chain_id_local = gmyth_tvchain_get_id( src->spawn_livetv->tvchain );
leo_sobral@2
   552
    if ( chain_id_local != NULL ) {
leo_sobral@2
   553
      src->live_chain_id = g_strdup( chain_id_local->str );
leo_sobral@2
   554
      g_print( "\t[%s]\tLocal chain ID = %s.\n", __FUNCTION__, src->live_chain_id );
leo_sobral@2
   555
    }
leo_sobral@2
   556
    src->live_tv_id = src->spawn_livetv->remote_encoder->recorder_num;
leo_sobral@2
   557
    g_print ( "[%s] LiveTV id = %d, URI path = %s.\n", __FUNCTION__, src->live_tv_id, src->uri_name );
leo_sobral@2
   558
  }
leo_sobral@2
   559
rosfran@40
   560
  src->file_transfer = gmyth_file_transfer_new( src->live_tv_id, 
leo_sobral@2
   561
      g_string_new( src->uri_name ), -1, src->mythtv_version );
leo_sobral@2
   562
leo_sobral@2
   563
  if ( src->file_transfer == NULL ) {
leo_sobral@2
   564
    GST_OBJECT_UNLOCK(src);
leo_sobral@2
   565
leo_sobral@2
   566
    goto init_failed;
leo_sobral@2
   567
  }
leo_sobral@2
   568
leo_sobral@2
   569
  /* sets the Playback monitor connection */
rosfran@40
   570
  ret = gmyth_file_transfer_playback_setup( &(src->file_transfer), src->live_tv );
leo_sobral@2
   571
leo_sobral@2
   572
  if ( src->live_tv == TRUE && ret == TRUE ) {
leo_sobral@2
   573
    /* loop finished, set the max tries variable to zero again... */
leo_sobral@2
   574
    wait_to_transfer = 0;
leo_sobral@2
   575
rosfran@40
   576
    while ( wait_to_transfer++ < GMYTHTV_TRANSFER_MAX_WAITS && ( gmyth_file_transfer_is_recording( src->file_transfer ) == FALSE 
rosfran@40
   577
	  /*|| ( gmyth_file_transfer_get_file_position( src->file_transfer ) < ( src->content_size + 327680 ) )*/ ) )
leo_sobral@2
   578
      g_usleep( 100 );
leo_sobral@2
   579
  }
leo_sobral@2
   580
leo_sobral@2
   581
  /* sets the FileTransfer instance connection (video/audio download) */
rosfran@40
   582
  ret = gmyth_file_transfer_setup( &(src->file_transfer), src->live_tv );
leo_sobral@2
   583
leo_sobral@2
   584
  if ( ret == FALSE ) {
leo_sobral@2
   585
    GST_OBJECT_UNLOCK(src);
leo_sobral@2
   586
#ifndef GST_DISABLE_GST_DEBUG  
leo_sobral@2
   587
    if ( src->mythtv_msgs_dbg )
leo_sobral@2
   588
      g_printerr( "MythTV FileTransfer request failed when setting up socket connection!\n" );  	  
leo_sobral@2
   589
#endif
leo_sobral@2
   590
    goto begin_req_failed;
leo_sobral@2
   591
  }
leo_sobral@2
   592
leo_sobral@2
   593
  src->content_size = src->file_transfer->filesize;
leo_sobral@2
   594
leo_sobral@2
   595
  GST_OBJECT_UNLOCK(src);
rosfran@30
   596
  if ( src->live_tv ) {
rosfran@30
   597
rosfran@30
   598
    //GError* error;
rosfran@30
   599
    //update_size_task = g_thread_create( (GThreadFunc)update_size_func, src, FALSE, &error );
rosfran@30
   600
    g_print( "[%s] Update Size task = %s\n", __FUNCTION__, update_size_task != NULL ?  "OK !" : "ERROR!!!" );
rosfran@30
   601
rosfran@30
   602
  }
rosfran@30
   603
  src->do_start = TRUE;  
rosfran@30
   604
rosfran@30
   605
done:
rosfran@30
   606
  return TRUE;
rosfran@30
   607
rosfran@30
   608
  /* ERRORS */
rosfran@30
   609
init_failed:
rosfran@30
   610
  {
rosfran@30
   611
    if (src->spawn_livetv != NULL )
rosfran@30
   612
      g_object_unref( src->spawn_livetv );
rosfran@30
   613
rosfran@30
   614
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@30
   615
	(NULL), ("Could not initialize MythTV library (%i, %s)", ret, src->uri_name));
rosfran@30
   616
    return FALSE;
rosfran@30
   617
  }
rosfran@30
   618
begin_req_failed:
rosfran@30
   619
  {
rosfran@30
   620
    GST_ELEMENT_ERROR (src, LIBRARY, INIT,
rosfran@30
   621
	(NULL), ("Could not begin request sent to MythTV server (%i, %s)", ret, src->uri_name));
rosfran@30
   622
    return FALSE;
rosfran@30
   623
  }
rosfran@30
   624
}
leo_sobral@2
   625
leo_sobral@2
   626
#if 0
rosfran@30
   627
/* handles queries for location in the stream in the requested format */
rosfran@30
   628
static gboolean
rosfran@30
   629
gst_mythtv_src_query ( GstPad * pad, GstQuery * query )
rosfran@30
   630
{
rosfran@30
   631
  gboolean res = TRUE;
rosfran@30
   632
  GstMythtvSrc *mythtv;
leo_sobral@2
   633
rosfran@30
   634
  guint64 size = 0;
rosfran@30
   635
rosfran@40
   636
  mythtv = GST_GMYTHTV_SRC( GST_PAD_PARENT (pad) );
rosfran@30
   637
rosfran@30
   638
  size = gst_mythtv_src_get_position (mythtv);
rosfran@30
   639
rosfran@30
   640
  switch (GST_QUERY_TYPE (query)) {
rosfran@30
   641
rosfran@30
   642
    case GST_QUERY_POSITION:
rosfran@30
   643
      {
rosfran@30
   644
rosfran@30
   645
	//GstFormat format;
rosfran@30
   646
	gint64 cur = 0;
rosfran@30
   647
rosfran@30
   648
	/* save requested format */
rosfran@30
   649
	gst_query_parse_position (query, NULL, &cur);
rosfran@30
   650
rosfran@30
   651
	/* query peer for current position in time */
rosfran@30
   652
	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
rosfran@30
   653
	    ( size > cur ) ? "greater" : "lower", size, cur );
rosfran@30
   654
	gst_query_set_position (query, GST_FORMAT_BYTES, size);
rosfran@30
   655
	if ( size < cur )
rosfran@30
   656
	  goto error;
rosfran@30
   657
rosfran@30
   658
	break;
leo_sobral@2
   659
      }
rosfran@30
   660
     #if 0
rosfran@30
   661
    case GST_QUERY_DURATION:
rosfran@30
   662
      {
rosfran@30
   663
	//GstFormat format;
rosfran@30
   664
	gint64 cur = 0;
rosfran@30
   665
rosfran@30
   666
	/* save requested format */
rosfran@30
   667
	gst_query_parse_position (query, NULL, &cur);
rosfran@30
   668
rosfran@30
   669
	/* query peer for current position in time */
rosfran@30
   670
	g_print( "[%s] Actual size is %s than current size from sink. [ %lld, %lld ]\n", __FUNCTION__, 
rosfran@30
   671
	    ( size * GST_SECOND > cur * GST_SECOND ) ? "greater" : "lower", size * GST_SECOND, 
rosfran@30
   672
	    cur * GST_SECOND );
rosfran@30
   673
	gst_query_set_position (query, GST_FORMAT_TIME, size * GST_SECOND );
rosfran@30
   674
rosfran@30
   675
	if ( size * GST_SECOND < cur * GST_SECOND )
rosfran@30
   676
	  goto error;
rosfran@30
   677
rosfran@30
   678
	break;
rosfran@30
   679
      }
rosfran@30
   680
	#endif
rosfran@30
   681
    default:
rosfran@30
   682
      res = FALSE;
rosfran@30
   683
      break;
rosfran@30
   684
  }
rosfran@30
   685
rosfran@30
   686
  return res;
rosfran@30
   687
rosfran@30
   688
error:
rosfran@30
   689
rosfran@30
   690
  return FALSE;
rosfran@30
   691
}
leo_sobral@2
   692
#endif
leo_sobral@2
   693
rosfran@30
   694
static gboolean
leo_sobral@2
   695
gst_mythtv_src_get_size (GstBaseSrc * bsrc, guint64 * size)
leo_sobral@2
   696
{
rosfran@31
   697
  GstMythtvSrc *src = GST_MYTHTV_SRC (bsrc);
rosfran@30
   698
  gboolean ret = TRUE;
leo_sobral@2
   699
rosfran@30
   700
  if (src->content_size <= 0) {
rosfran@30
   701
    ret= FALSE;
rosfran@40
   702
  } else if ( abs ( src->content_size - src->read_offset ) <= GMYTHTV_TRANSFER_MAX_BUFFER ) {
rosfran@34
   703
    //g_static_mutex_lock( &update_size_mutex );
rosfran@37
   704
    GST_OBJECT_LOCK(src);
rosfran@30
   705
rosfran@40
   706
  	gint64 new_offset = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@40
   707
	if ( new_offset > 0 ) {
rosfran@40
   708
	  if ( src->content_size < new_offset ) {
rosfran@40
   709
	    src->content_size = new_offset;
rosfran@40
   710
	  }
rosfran@40
   711
	} else {
rosfran@40
   712
	  src->update_prog_chain = TRUE;
rosfran@40
   713
	  src->content_size = 0;
rosfran@40
   714
	}
rosfran@31
   715
 
leo_sobral@2
   716
#if ENABLE_TIMING_POSITION == 1
rosfran@40
   717
    gint64 size_tmp = 0;
rosfran@30
   718
    if (src->live_tv == TRUE) {
leo_sobral@2
   719
get_file_pos:
rosfran@31
   720
      g_usleep( 5 );
rosfran@40
   721
      size_tmp = gmyth_file_transfer_get_file_position( src->file_transfer );
rosfran@40
   722
      if ( size_tmp > ( src->content_size + GMYTHTV_TRANSFER_MAX_BUFFER ) )
rosfran@30
   723
	src->content_size = size_tmp;
rosfran@40
   724
      else if ( size_tmp > 0  )
rosfran@30
   725
	goto get_file_pos;
rosfran@40
   726
      g_print( "\t[%s]\tGET_POSITION: file_position = %lld\n",
rosfran@30
   727
	  __FUNCTION__, size_tmp );
rosfran@30
   728
    }
rosfran@30
   729
#endif
rosfran@37
   730
	GST_OBJECT_UNLOCK(src);
rosfran@34
   731
    //g_static_mutex_unlock( &update_size_mutex );
rosfran@30
   732
leo_sobral@2
   733
  }
leo_sobral@2
   734
leo_sobral@2
   735
  *size = src->content_size;
rosfran@30
   736
  g_print( "[%s] Content size = %llu\n", __FUNCTION__, src->content_size );
leo_sobral@2
   737
rosfran@30
   738
  return ret;
rosfran@30
   739
leo_sobral@2
   740
}
leo_sobral@2
   741
leo_sobral@2
   742
/* close the socket and associated resources
leo_sobral@2
   743
 * used both to recover from errors and go to NULL state */
leo_sobral@2
   744
  static gboolean
leo_sobral@2
   745
gst_mythtv_src_stop (GstBaseSrc * bsrc)
leo_sobral@2
   746
{
leo_sobral@2
   747
  GstMythtvSrc *src;
leo_sobral@2
   748
leo_sobral@2
   749
  src = GST_MYTHTV_SRC (bsrc);
leo_sobral@2
   750
leo_sobral@2
   751
  if (src->uri_name) {
leo_sobral@2
   752
    g_free (src->uri_name);
leo_sobral@2
   753
    src->uri_name = NULL;
leo_sobral@2
   754
  }
leo_sobral@2
   755
leo_sobral@2
   756
  if (src->mythtv_caps) {
leo_sobral@2
   757
    gst_caps_unref (src->mythtv_caps);
leo_sobral@2
   758
    src->mythtv_caps = NULL;
leo_sobral@2
   759
  }
leo_sobral@2
   760
leo_sobral@2
   761
  src->eos = FALSE;
leo_sobral@2
   762
leo_sobral@2
   763
  return TRUE;
leo_sobral@2
   764
}
leo_sobral@2
   765
rosfran@30
   766
static gboolean
leo_sobral@2
   767
gst_mythtv_src_handle_event (GstPad * pad, GstEvent * event)
leo_sobral@2
   768
{
leo_sobral@2
   769
  GstMythtvSrc *src = GST_MYTHTV_SRC (GST_PAD_PARENT (pad));
leo_sobral@2
   770
leo_sobral@2
   771
  switch (GST_EVENT_TYPE (event)) {
rosfran@30
   772
#if 0
leo_sobral@2
   773
    case GST_EVENT_FLUSH_START:
leo_sobral@2
   774
      src->eos = FALSE;
leo_sobral@2
   775
      break;
leo_sobral@2
   776
      //return TRUE;
leo_sobral@2
   777
    case GST_EVENT_FLUSH_STOP:
leo_sobral@2
   778
      src->do_start = TRUE;
leo_sobral@2
   779
      src->eos = FALSE;
leo_sobral@2
   780
      gst_element_set_state (GST_ELEMENT(src), GST_STATE_NULL);
rosfran@30
   781
      //gst_element_set_locked_state (GST_ELEMENT(src), TRUE);
rosfran@30
   782
      break;
rosfran@30
   783
#endif
rosfran@30
   784
    case GST_EVENT_EOS:
rosfran@30
   785
      g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
rosfran@31
   786
	  guint64 cont_size = gst_mythtv_src_get_position (src);
rosfran@34
   787
	  if ( !src->live_tv ) {
rosfran@34
   788
		  if ( cont_size > src->content_size ) {
rosfran@34
   789
		  	src->content_size = cont_size;
rosfran@34
   790
		  	src->eos = FALSE;
rosfran@34
   791
	  	  } else {
rosfran@34
   792
	      	src->eos = TRUE;
rosfran@34
   793
	      	gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
rosfran@34
   794
	      	gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
rosfran@34
   795
	  	  }
rosfran@34
   796
	  } else 
rosfran@34
   797
	  	src->eos = TRUE;
rosfran@30
   798
      break;
rosfran@30
   799
#if 0
rosfran@30
   800
    case GST_EVENT_NEWSEGMENT:
rosfran@30
   801
      g_print( "[%s] Got NEWSEGMENT!!!\n", __FUNCTION__ );
rosfran@30
   802
      src->eos = FALSE;
leo_sobral@2
   803
      break;
leo_sobral@2
   804
    case GST_EVENT_SEEK:  	  
leo_sobral@2
   805
      {
rosfran@30
   806
	g_print( "[%s] Got EVENT_SEEK!!!\n", __FUNCTION__ );
leo_sobral@2
   807
	gdouble rate;
leo_sobral@2
   808
	//gboolean update = TRUE;
leo_sobral@2
   809
	GstFormat format;
leo_sobral@2
   810
	GstSeekType cur_type, stop_type;
leo_sobral@2
   811
	GstSeekFlags flags;
leo_sobral@2
   812
	gint64 cur = 0, stop = 0;
leo_sobral@2
   813
	gst_event_parse_seek ( event, &rate, &format,
leo_sobral@2
   814
	    &flags, &cur_type, &cur,
leo_sobral@2
   815
	    &stop_type, &stop );
leo_sobral@2
   816
leo_sobral@2
   817
	g_print( "[%s] Got EVENT_SEEK.\n", __FUNCTION__ );
leo_sobral@2
   818
	if ( !( flags & GST_SEEK_FLAG_FLUSH ) ) {
leo_sobral@2
   819
	  g_print( "[%s] Could get the FLAG_FLUSH message.\n", __FUNCTION__ );
leo_sobral@2
   820
	}
leo_sobral@2
   821
	//gboolean ret = gst_event_parse_new_segment ( event,
leo_sobral@2
   822
	//    &update, &rate, &format, &start, &stop,
leo_sobral@2
   823
	//    &position );
leo_sobral@2
   824
	//GstFlowReturn flow_ret = gst_mythtv_src_create (GST_BASE_SRC( GST_PAD_PARENT( psrc ) ), 
leo_sobral@2
   825
	//			cur, stop - cur + 1, GstBuffer)
rosfran@30
   826
      }
rosfran@30
   827
#endif
leo_sobral@2
   828
    default:
leo_sobral@2
   829
      return gst_pad_event_default (pad, event);
leo_sobral@2
   830
  }
leo_sobral@2
   831
leo_sobral@2
   832
  return gst_pad_event_default (pad, event);
leo_sobral@2
   833
}
leo_sobral@2
   834
rosfran@30
   835
static gboolean
rosfran@30
   836
gst_mythtv_src_is_seekable( GstBaseSrc *push_src )
leo_sobral@2
   837
{
leo_sobral@2
   838
  return TRUE;
leo_sobral@2
   839
}
leo_sobral@2
   840
rosfran@30
   841
static void
leo_sobral@2
   842
gst_mythtv_src_set_property (GObject * object, guint prop_id,
leo_sobral@2
   843
    const GValue * value, GParamSpec * pspec)
leo_sobral@2
   844
{
leo_sobral@2
   845
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
   846
leo_sobral@2
   847
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
   848
  switch (prop_id) {
leo_sobral@2
   849
    case PROP_URI:
leo_sobral@2
   850
    case PROP_LOCATION:
leo_sobral@2
   851
      {
leo_sobral@2
   852
	if (!g_value_get_string (value)) {
leo_sobral@2
   853
	  GST_WARNING ("location property cannot be NULL");
leo_sobral@2
   854
	  goto done;
leo_sobral@2
   855
	}
leo_sobral@2
   856
leo_sobral@2
   857
	if (mythtvsrc->uri_name != NULL) {
leo_sobral@2
   858
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
   859
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
   860
	}
leo_sobral@2
   861
	mythtvsrc->uri_name = g_value_dup_string (value);
leo_sobral@2
   862
leo_sobral@2
   863
	break;
leo_sobral@2
   864
      }
leo_sobral@2
   865
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
   866
    case PROP_GMYTHTV_DBG:
leo_sobral@2
   867
      {
leo_sobral@2
   868
	mythtvsrc->mythtv_msgs_dbg = g_value_get_boolean (value);
leo_sobral@2
   869
	break;
leo_sobral@2
   870
      }
leo_sobral@2
   871
#endif
rosfran@40
   872
    case PROP_GMYTHTV_VERSION:
leo_sobral@2
   873
      {
leo_sobral@2
   874
	mythtvsrc->mythtv_version = g_value_get_int (value);
leo_sobral@2
   875
	break;
leo_sobral@2
   876
      }
rosfran@40
   877
    case PROP_GMYTHTV_LIVEID:
leo_sobral@2
   878
      {
leo_sobral@2
   879
	mythtvsrc->live_tv_id = g_value_get_int (value);
leo_sobral@2
   880
	break;
leo_sobral@2
   881
      }
rosfran@40
   882
    case PROP_GMYTHTV_LIVE:
leo_sobral@2
   883
      {
leo_sobral@2
   884
	mythtvsrc->live_tv = g_value_get_boolean (value);
leo_sobral@2
   885
	break;
leo_sobral@2
   886
      }
rosfran@40
   887
    case PROP_GMYTHTV_LIVE_CHAINID:
leo_sobral@2
   888
      {
leo_sobral@2
   889
	if (!g_value_get_string (value)) {
leo_sobral@2
   890
	  GST_WARNING ("MythTV Live chainid property cannot be NULL");
leo_sobral@2
   891
	  goto done;
leo_sobral@2
   892
	}
leo_sobral@2
   893
leo_sobral@2
   894
	if (mythtvsrc->live_chain_id != NULL) {
leo_sobral@2
   895
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
   896
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
   897
	}
leo_sobral@2
   898
	mythtvsrc->live_chain_id = g_value_dup_string (value);
leo_sobral@2
   899
leo_sobral@2
   900
	break;
leo_sobral@2
   901
      }
leo_sobral@2
   902
leo_sobral@2
   903
    default:
leo_sobral@2
   904
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
   905
      break;
leo_sobral@2
   906
  }
leo_sobral@2
   907
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
   908
done:
leo_sobral@2
   909
  return;
leo_sobral@2
   910
}
leo_sobral@2
   911
leo_sobral@2
   912
  static void
leo_sobral@2
   913
gst_mythtv_src_get_property (GObject * object, guint prop_id,
leo_sobral@2
   914
    GValue * value, GParamSpec * pspec)
leo_sobral@2
   915
{
leo_sobral@2
   916
  GstMythtvSrc *mythtvsrc = GST_MYTHTV_SRC (object);
leo_sobral@2
   917
leo_sobral@2
   918
  GST_OBJECT_LOCK (mythtvsrc);
leo_sobral@2
   919
  switch (prop_id) {
leo_sobral@2
   920
    case PROP_URI:
leo_sobral@2
   921
    case PROP_LOCATION:
leo_sobral@2
   922
      {
leo_sobral@2
   923
	gchar *str = g_strdup( "" );
leo_sobral@2
   924
leo_sobral@2
   925
	if ( mythtvsrc->uri_name == NULL ) {
leo_sobral@2
   926
	  g_free (mythtvsrc->uri_name);
leo_sobral@2
   927
	  mythtvsrc->uri_name = NULL;
leo_sobral@2
   928
	} else {
leo_sobral@2
   929
	  str = g_strdup( mythtvsrc->uri_name );
leo_sobral@2
   930
	}
leo_sobral@2
   931
	g_value_set_string ( value, str );
leo_sobral@2
   932
	break;
leo_sobral@2
   933
      }
leo_sobral@2
   934
#ifndef GST_DISABLE_GST_DEBUG
rosfran@40
   935
    case PROP_GMYTHTV_DBG:
leo_sobral@2
   936
      g_value_set_boolean ( value, mythtvsrc->mythtv_msgs_dbg );
leo_sobral@2
   937
      break;
leo_sobral@2
   938
#endif
rosfran@40
   939
    case PROP_GMYTHTV_VERSION:
leo_sobral@2
   940
      {
leo_sobral@2
   941
	g_value_set_int ( value, mythtvsrc->mythtv_version );
leo_sobral@2
   942
	break;
leo_sobral@2
   943
      }
rosfran@40
   944
    case PROP_GMYTHTV_LIVEID:
leo_sobral@2
   945
      {
leo_sobral@2
   946
	g_value_set_int ( value, mythtvsrc->live_tv_id );
leo_sobral@2
   947
	break;
leo_sobral@2
   948
      }
rosfran@40
   949
    case PROP_GMYTHTV_LIVE:
leo_sobral@2
   950
      g_value_set_boolean ( value, mythtvsrc->live_tv );
leo_sobral@2
   951
      break;
rosfran@40
   952
    case PROP_GMYTHTV_LIVE_CHAINID:
leo_sobral@2
   953
      {
leo_sobral@2
   954
	gchar *str = g_strdup( "" );
leo_sobral@2
   955
leo_sobral@2
   956
	if ( mythtvsrc->live_chain_id == NULL ) {
leo_sobral@2
   957
	  g_free (mythtvsrc->live_chain_id);
leo_sobral@2
   958
	  mythtvsrc->live_chain_id = NULL;
leo_sobral@2
   959
	} else {
leo_sobral@2
   960
	  str = g_strdup( mythtvsrc->live_chain_id );
leo_sobral@2
   961
	}
leo_sobral@2
   962
	g_value_set_string ( value, str );
leo_sobral@2
   963
	break;
leo_sobral@2
   964
      }
leo_sobral@2
   965
    default:
leo_sobral@2
   966
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
leo_sobral@2
   967
      break;
leo_sobral@2
   968
  }
leo_sobral@2
   969
  GST_OBJECT_UNLOCK (mythtvsrc);
leo_sobral@2
   970
}
leo_sobral@2
   971
leo_sobral@2
   972
/* entry point to initialize the plug-in
leo_sobral@2
   973
 * initialize the plug-in itself
leo_sobral@2
   974
 * register the element factories and pad templates
leo_sobral@2
   975
 * register the features
leo_sobral@2
   976
 */
rosfran@40
   977
static gboolean
leo_sobral@2
   978
plugin_init (GstPlugin * plugin)
leo_sobral@2
   979
{
leo_sobral@2
   980
  return gst_element_register (plugin, "mythtvsrc", GST_RANK_NONE,
leo_sobral@2
   981
      GST_TYPE_MYTHTV_SRC);
leo_sobral@2
   982
}
leo_sobral@2
   983
leo_sobral@2
   984
/* this is the structure that gst-register looks for
leo_sobral@2
   985
 * so keep the name plugin_desc, or you cannot get your plug-in registered */
leo_sobral@2
   986
GST_PLUGIN_DEFINE (GST_VERSION_MAJOR,
leo_sobral@2
   987
    GST_VERSION_MINOR,
leo_sobral@2
   988
    "mythtv",
leo_sobral@2
   989
    "lib MythTV src",
leo_sobral@2
   990
    plugin_init, VERSION, "LGPL", "GStreamer", "http://gstreamer.net/")
leo_sobral@2
   991
leo_sobral@2
   992
leo_sobral@2
   993
/*** GSTURIHANDLER INTERFACE *************************************************/
leo_sobral@2
   994
  static guint 
leo_sobral@2
   995
gst_mythtv_src_uri_get_type (void)
leo_sobral@2
   996
{
leo_sobral@2
   997
  return GST_URI_SRC;
leo_sobral@2
   998
}
leo_sobral@2
   999
leo_sobral@2
  1000
  static gchar **
leo_sobral@2
  1001
gst_mythtv_src_uri_get_protocols (void)
leo_sobral@2
  1002
{
leo_sobral@2
  1003
  static gchar *protocols[] = { "myth", "myths", NULL };
leo_sobral@2
  1004
leo_sobral@2
  1005
  return protocols;
leo_sobral@2
  1006
}
leo_sobral@2
  1007
leo_sobral@2
  1008
  static const gchar *
leo_sobral@2
  1009
gst_mythtv_src_uri_get_uri (GstURIHandler * handler)
leo_sobral@2
  1010
{
leo_sobral@2
  1011
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
  1012
leo_sobral@2
  1013
  return src->uri_name;
leo_sobral@2
  1014
}
leo_sobral@2
  1015
leo_sobral@2
  1016
  static gboolean
leo_sobral@2
  1017
gst_mythtv_src_uri_set_uri (GstURIHandler * handler, const gchar * uri)
leo_sobral@2
  1018
{
leo_sobral@2
  1019
  GstMythtvSrc *src = GST_MYTHTV_SRC (handler);
leo_sobral@2
  1020
leo_sobral@2
  1021
  gchar *protocol;
leo_sobral@2
  1022
leo_sobral@2
  1023
  protocol = gst_uri_get_protocol (uri);
leo_sobral@2
  1024
  if ((strcmp (protocol, "myth") != 0) && (strcmp (protocol, "myths") != 0)) {
leo_sobral@2
  1025
    g_free (protocol);
leo_sobral@2
  1026
    return FALSE;
leo_sobral@2
  1027
  }
leo_sobral@2
  1028
  g_free (protocol);
leo_sobral@2
  1029
  g_object_set (src, "location", uri, NULL);
leo_sobral@2
  1030
leo_sobral@2
  1031
  return TRUE;
leo_sobral@2
  1032
}
leo_sobral@2
  1033
leo_sobral@2
  1034
  static void
leo_sobral@2
  1035
gst_mythtv_src_uri_handler_init (gpointer g_iface, gpointer iface_data)
leo_sobral@2
  1036
{
leo_sobral@2
  1037
  GstURIHandlerInterface *iface = (GstURIHandlerInterface *) g_iface;
leo_sobral@2
  1038
leo_sobral@2
  1039
  iface->get_type = gst_mythtv_src_uri_get_type;
leo_sobral@2
  1040
  iface->get_protocols = gst_mythtv_src_uri_get_protocols;
leo_sobral@2
  1041
  iface->get_uri = gst_mythtv_src_uri_get_uri;
leo_sobral@2
  1042
  iface->set_uri = gst_mythtv_src_uri_set_uri;
leo_sobral@2
  1043
}
leo_sobral@2
  1044
leo_sobral@2
  1045
  void
leo_sobral@2
  1046
size_header_handler (void *userdata, const char *value)
leo_sobral@2
  1047
{
leo_sobral@2
  1048
  GstMythtvSrc *src = GST_MYTHTV_SRC (userdata);
leo_sobral@2
  1049
leo_sobral@2
  1050
  //src->content_size = g_ascii_strtoull (value, NULL, 10);
leo_sobral@2
  1051
leo_sobral@2
  1052
  GST_DEBUG_OBJECT (src, "content size = %lld bytes", src->content_size);
leo_sobral@2
  1053
}