[svn r35] Performance optimization.
1.1 --- a/gst-plugins-mythtv/src/gstmythtvsrc.c Wed Oct 18 23:08:56 2006 +0100
1.2 +++ b/gst-plugins-mythtv/src/gstmythtvsrc.c Wed Oct 18 23:45:17 2006 +0100
1.3 @@ -36,14 +36,14 @@
1.4
1.5 #define MYTHTV_TRANSFER_MAX_WAITS 100
1.6
1.7 -#define MYTHTV_TRANSFER_MAX_BUFFER 4*1024
1.8 +#define MYTHTV_TRANSFER_MAX_BUFFER 1024*1024
1.9 //( 32*1024 )
1.10
1.11 /* 4*1024 ??? */
1.12 #define MAX_READ_SIZE 12*1024
1.13 //( 32*1024 )
1.14
1.15 -#define ENABLE_TIMING_POSITION 1
1.16 +#define ENABLE_TIMING_POSITION 0
1.17
1.18 /* stablish a maximum iteration value to the IS_RECORDING message */
1.19 static guint wait_to_transfer = 0;
1.20 @@ -289,7 +289,7 @@
1.21 * Retry whilst authentication fails and we supply it. */
1.22 guint len = 0;
1.23
1.24 - GST_OBJECT_LOCK(src);
1.25 + //GST_OBJECT_LOCK(src);
1.26
1.27 while ( sizetoread > 0 ) {
1.28
1.29 @@ -297,17 +297,36 @@
1.30 GST_BUFFER_DATA( *outbuf ) + read, sizetoread, TRUE );
1.31
1.32 if ( len > 0 ) {
1.33 - read += len;
1.34 - src->read_offset += read;
1.35 + read += len;
1.36 sizetoread -= len;
1.37 } else if ( len <= 0 ) {
1.38
1.39 if ( src->live_tv == FALSE ) {
1.40 goto done;
1.41 - } else if ( src->content_size >= src->read_offset &&
1.42 - abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) {
1.43 - goto done;
1.44 - }
1.45 + } else if ( /*src->content_size >= src->read_offset &&
1.46 + abs ( src->content_size - src->read_offset ) <= 1024 ) ||*/
1.47 + ( src->content_size <= ( src->read_offset + size + MYTHTV_TRANSFER_MAX_BUFFER ) ) )
1.48 + {
1.49 +#if ENABLE_TIMING_POSITION == 1
1.50 + guint64 size_tmp = 0;
1.51 + if (src->live_tv == TRUE) {
1.52 +get_file_pos:
1.53 + size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
1.54 + if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
1.55 + src->content_size = size_tmp;
1.56 + else
1.57 + goto get_file_pos;
1.58 + g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
1.59 + __FUNCTION__, size_tmp );
1.60 + }
1.61 +#else
1.62 + guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer );
1.63 + if ( src->content_size < new_offset ) {
1.64 + src->content_size = new_offset;
1.65 + }
1.66 +#endif
1.67 + goto done;
1.68 + }
1.69
1.70 }
1.71
1.72 @@ -316,6 +335,7 @@
1.73 }
1.74
1.75 if ( read > 0 ) {
1.76 + src->read_offset += read;
1.77 src->bytes_read += read;
1.78
1.79 g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\
1.80 @@ -333,7 +353,7 @@
1.81 "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf),
1.82 GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) );
1.83
1.84 - } else
1.85 + } else if ( !src->live_tv )
1.86 goto eos;
1.87
1.88 goto done;
1.89 @@ -343,7 +363,7 @@
1.90 src->eos = TRUE;
1.91
1.92 done:
1.93 - GST_OBJECT_UNLOCK(src);
1.94 + //GST_OBJECT_UNLOCK(src);
1.95
1.96 return read;
1.97 }
1.98 @@ -361,33 +381,13 @@
1.99 goto eos;
1.100
1.101 //GST_OBJECT_LOCK(src);
1.102 - if ( /*src->content_size >= src->read_offset &&
1.103 - abs ( src->content_size - src->read_offset ) <= 1024 ) ||*/
1.104 - ( src->content_size <= ( src->read_offset + size ) ) )
1.105 - {
1.106 -#if ENABLE_TIMING_POSITION == 1
1.107 - guint64 size_tmp = 0;
1.108 - if (src->live_tv == TRUE) {
1.109 -get_file_pos:
1.110 - size_tmp = myth_file_transfer_get_file_position( src->file_transfer );
1.111 - if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) )
1.112 - src->content_size = size_tmp;
1.113 - else
1.114 - goto get_file_pos;
1.115 - g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n",
1.116 - __FUNCTION__, size_tmp );
1.117 - }
1.118 -#endif
1.119 - //guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer );
1.120 - //if ( src->content_size < new_offset ) {
1.121 - // src->content_size = new_offset;
1.122 - //}
1.123 - }
1.124
1.125 if (G_UNLIKELY (src->read_offset != offset)) {
1.126 guint64 new_offset = myth_file_transfer_seek(src->file_transfer, offset, SEEK_SET);
1.127 -
1.128 - if (G_UNLIKELY (new_offset < 0 || new_offset != offset)) {
1.129 + g_print( "[%s] SRC Offset = %llu, NEW actual backend SEEK Offset = %llu.\n",
1.130 + __FUNCTION__, src->read_offset, new_offset );
1.131 + if (G_UNLIKELY (new_offset < 0 ) )//|| new_offset != src->read_offset)) {
1.132 + {
1.133 //GST_OBJECT_UNLOCK(src);
1.134 goto read_error;
1.135 }
1.136 @@ -446,15 +446,6 @@
1.137 return GST_FLOW_ERROR;
1.138 }
1.139
1.140 -#if 0
1.141 -need_pause:
1.142 - {
1.143 - const gchar *reason = gst_flow_get_name (ret);
1.144 -
1.145 - GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason);
1.146 - return GST_FLOW_UNEXPECTED;
1.147 - }
1.148 -#endif
1.149 }
1.150
1.151 void
1.152 @@ -690,16 +681,14 @@
1.153
1.154 if (src->content_size <= 0) {
1.155 ret= FALSE;
1.156 - } else if ( src->content_size >= src->read_offset &&
1.157 - abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) {
1.158 - g_static_mutex_lock( &update_size_mutex );
1.159 + } else if ( abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) {
1.160 + //g_static_mutex_lock( &update_size_mutex );
1.161
1.162 guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer );
1.163 if ( src->content_size < new_offset ) {
1.164 src->content_size = new_offset;
1.165 }
1.166
1.167 -#if 0
1.168 #if ENABLE_TIMING_POSITION == 1
1.169 guint64 size_tmp = 0;
1.170 if (src->live_tv == TRUE) {
1.171 @@ -714,8 +703,7 @@
1.172 __FUNCTION__, size_tmp );
1.173 }
1.174 #endif
1.175 -#endif
1.176 - g_static_mutex_unlock( &update_size_mutex );
1.177 + //g_static_mutex_unlock( &update_size_mutex );
1.178
1.179 }
1.180
1.181 @@ -771,14 +759,17 @@
1.182 case GST_EVENT_EOS:
1.183 g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ );
1.184 guint64 cont_size = gst_mythtv_src_get_position (src);
1.185 - if ( cont_size > src->content_size ) {
1.186 - src->content_size = cont_size;
1.187 - src->eos = FALSE;
1.188 - } else {
1.189 - src->eos = TRUE;
1.190 - gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
1.191 - gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
1.192 - }
1.193 + if ( !src->live_tv ) {
1.194 + if ( cont_size > src->content_size ) {
1.195 + src->content_size = cont_size;
1.196 + src->eos = FALSE;
1.197 + } else {
1.198 + src->eos = TRUE;
1.199 + gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL );
1.200 + gst_element_set_locked_state ( GST_ELEMENT (src), FALSE );
1.201 + }
1.202 + } else
1.203 + src->eos = TRUE;
1.204 break;
1.205 #if 0
1.206 case GST_EVENT_NEWSEGMENT:
2.1 --- a/gst-plugins-mythtv/src/myth_file_transfer.c Wed Oct 18 23:08:56 2006 +0100
2.2 +++ b/gst-plugins-mythtv/src/myth_file_transfer.c Wed Oct 18 23:45:17 2006 +0100
2.3 @@ -32,7 +32,7 @@
2.4 #define MYTHTV_RETRIES 1
2.5 #define MYTHTV_FILE_SIZE -1
2.6
2.7 -#define MYTHTV_BUFFER_SIZE 2048
2.8 +#define MYTHTV_BUFFER_SIZE 256*1024
2.9
2.10 #define MYTHTV_VERSION 30
2.11
2.12 @@ -686,14 +686,13 @@
2.13 return NULL;
2.14 }
2.15
2.16 - gint
2.17 +gint
2.18 myth_file_transfer_read(MythFileTransfer *transfer, void *data, gint size, gboolean read_unlimited)
2.19 {
2.20 gint recv = 0;
2.21 gsize bytes_read = 0;
2.22 -
2.23 gint sent = 0;
2.24 - //guint zerocnt = 0;
2.25 + guint remaining = 0;
2.26 gboolean response = FALSE;
2.27
2.28 GIOChannel *io_channel;
2.29 @@ -727,7 +726,6 @@
2.30 if ( io_status == G_IO_STATUS_NORMAL )
2.31 g_print( "[%s] Setting encoding to binary data socket).\n", __FUNCTION__ );
2.32
2.33 -
2.34 io_cond = g_io_channel_get_buffer_condition( io_channel );
2.35
2.36 io_cond_control = g_io_channel_get_buffer_condition( io_channel );
2.37 @@ -754,16 +752,23 @@
2.38 if ( ( io_cond & G_IO_IN ) != 0 ) {
2.39 do
2.40 {
2.41 + trash = g_new0( gchar, MYTHTV_BUFFER_SIZE );
2.42
2.43 - io_status = g_io_channel_read_line( io_channel, &trash, &bytes_read, NULL, &error);
2.44 + io_status = g_io_channel_read_chars( io_channel, trash,
2.45 + MYTHTV_BUFFER_SIZE, &bytes_read, &error);
2.46
2.47 - g_print( "[%s] cleaning buffer on IO binary channel... (%s)\n", __FUNCTION__, trash );
2.48 + g_print( "[%s] cleaning buffer on IO binary channel... %d bytes gone!\n",
2.49 + __FUNCTION__, bytes_read );
2.50 +
2.51 + if ( trash != NULL )
2.52 + g_free( trash );
2.53 +
2.54 io_cond = g_io_channel_get_buffer_condition( io_channel );
2.55
2.56 - } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) );
2.57 + } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) && (error == NULL) );
2.58
2.59 - if ( trash!= NULL )
2.60 - g_free( trash );
2.61 + //if ( trash!= NULL )
2.62 + // g_free( trash );
2.63 }
2.64
2.65 if ( ( io_cond_control & G_IO_IN ) != 0 ) {
2.66 @@ -789,36 +794,40 @@
2.67 /* transfer->live_tv ? transfer->card_id :*/ transfer->recordernum ); // transfer->recordernum
2.68 g_print( "\t[%s] Transfer_query = %s\n", __FUNCTION__, transfer->query->str );
2.69
2.70 - strlist = gmyth_string_list_new();
2.71 -
2.72 - gmyth_string_list_append_char_array( strlist, transfer->query->str );
2.73 - gmyth_string_list_append_char_array( strlist,
2.74 - /*transfer->live_tv ? "REQUEST_BLOCK_RINGBUF" :*/ "REQUEST_BLOCK" );
2.75 - gmyth_string_list_append_int( strlist, size );
2.76 -
2.77 - gmyth_socket_write_stringlist( transfer->control_sock, strlist );
2.78 sent = size;
2.79 + remaining = size - recv;
2.80 //g_static_mutex_unlock( &mutex );
2.81 //data = (void*)g_new0( gchar, size );
2.82
2.83 - g_io_channel_flush( io_channel_control, NULL );
2.84 //g_io_channel_flush( io_channel, NULL );
2.85
2.86 - g_static_mutex_lock( &mutex );
2.87 + //g_static_mutex_lock( &mutex );
2.88
2.89 io_cond = g_io_channel_get_buffer_condition( io_channel );
2.90
2.91 - while ( ( recv < sent ) )//&& ( io_cond & G_IO_IN ) != 0 )
2.92 + while ( recv < size && !response )//&& ( io_cond & G_IO_IN ) != 0 )
2.93 {
2.94 + g_io_channel_flush( io_channel_control, NULL );
2.95 +
2.96 + strlist = gmyth_string_list_new();
2.97 + gmyth_string_list_append_char_array( strlist, transfer->query->str );
2.98 + gmyth_string_list_append_char_array( strlist,
2.99 + /*transfer->live_tv ? "REQUEST_BLOCK_RINGBUF" :*/ "REQUEST_BLOCK" );
2.100 + gmyth_string_list_append_int( strlist, remaining );
2.101 + gmyth_socket_write_stringlist( transfer->control_sock, strlist );
2.102 +
2.103 + guint count_bytes = 0;
2.104 +
2.105 do
2.106 {
2.107 - //while ( ( io_cond & G_IO_IN ) == 0 ) {
2.108 - //usleep(200);
2.109 - //
2.110 - //io_cond = g_io_channel_get_buffer_condition( io_channel );
2.111 - //g_static_mutex_lock( &mutex );
2.112 -
2.113 - buf_len = ( sent - recv ) > MYTHTV_BUFFER_SIZE ? MYTHTV_BUFFER_SIZE : ( sent - recv );
2.114 + //buf_len = ( sent - recv ) > MYTHTV_BUFFER_SIZE ? MYTHTV_BUFFER_SIZE : ( sent - recv );
2.115 + if ( remaining > MYTHTV_BUFFER_SIZE ) {
2.116 + buf_len = MYTHTV_BUFFER_SIZE;
2.117 + } else {
2.118 + buf_len = remaining;
2.119 + }
2.120 +
2.121 + bytes_read = 0;
2.122
2.123 io_status = g_io_channel_read_chars( io_channel, data + recv,
2.124 buf_len, &bytes_read, &error );
2.125 @@ -832,55 +841,106 @@
2.126 */
2.127 if ( bytes_read > 0 )
2.128 {
2.129 - if ( bytes_read <= buf_len )
2.130 - recv += bytes_read;
2.131 - }
2.132 + //if ( bytes_read <= buf_len )
2.133 + recv += bytes_read;
2.134 + count_bytes += bytes_read;
2.135 + remaining -= bytes_read;
2.136 + g_print( "[%s] Reading buffer (bytes read = %d, remaining = %d)\n", __FUNCTION__, bytes_read, remaining );
2.137 + if ( remaining == 0 ) {
2.138 + break;
2.139 + }
2.140 + } else {
2.141 + break;
2.142 + }
2.143 +
2.144 + //if ( remaining > 0 ) {
2.145
2.146 if ( io_status == G_IO_STATUS_EOF ) {
2.147 - g_printerr( "[%s] got EOS!", __FUNCTION__ );
2.148 + g_print( "[%s] got EOS!", __FUNCTION__ );
2.149 break;
2.150 } else if ( io_status == G_IO_STATUS_ERROR ) {
2.151 - g_printerr( "[%s] myth_file_transfer_read(): socket error.\n", __FUNCTION__ );
2.152 + g_print( "[%s] myth_file_transfer_read(): socket error.\n", __FUNCTION__ );
2.153 break;
2.154 }
2.155 + //}
2.156
2.157 /* increase buffer size, to allow get more data (do not obey to the buffer size) */
2.158 if ( read_unlimited == TRUE ) {
2.159 + // FOR NOW, DO NOTHING!!!
2.160 //if ( recv > buf_len )
2.161 // sent += (bytes_read - buf_len) + 1;
2.162 }
2.163 - if ( bytes_read == buf_len )
2.164 - break;
2.165
2.166 /* verify if the input (read) buffer is ready to receive data */
2.167 io_cond = g_io_channel_get_buffer_condition( io_channel );
2.168
2.169 g_print( "[%s]\t io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__,
2.170 ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
2.171 +
2.172 + //if ( recv == size )
2.173 + //break;
2.174
2.175 - } while ( recv < sent && ( ( io_cond & G_IO_IN ) != 0 ) && ( io_status == G_IO_STATUS_NORMAL ) );
2.176 + } while ( remaining > 0 );//&& ( io_status == G_IO_STATUS_NORMAL ) );
2.177 +
2.178 + // if ( ( recv < size ) ) {
2.179 + // finish_read = FALSE;
2.180 + //}
2.181
2.182 io_cond_control = g_io_channel_get_buffer_condition( io_channel_control );
2.183 - if ( ( io_status == G_IO_STATUS_EOF ) || ( ( io_cond_control & G_IO_IN ) != 0 ) )
2.184 + if ( remaining == 0 )//( io_cond_control & G_IO_IN ) != 0 )
2.185 {
2.186 gmyth_socket_read_stringlist( transfer->control_sock, strlist );
2.187 - sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
2.188 - g_print( "[%s]\t sent = %d, io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__,
2.189 - sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
2.190 - response = TRUE;
2.191 - break;
2.192 - }
2.193 - }
2.194 + if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 )
2.195 + {
2.196 + sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
2.197 + g_print( "[%s] got SENT buffer message = %d\n", __FUNCTION__, sent );
2.198 + if ( sent != 0 )
2.199 + {
2.200 + g_print( "[%s]\t received = %d bytes, backend says %d bytes sent, "\
2.201 + "io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__,
2.202 + recv, sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
2.203
2.204 - if ( ( ( error == NULL ) && ( response == FALSE ) ) ||
2.205 - ( io_status == G_IO_STATUS_EOF ) || ( ( io_cond & G_IO_IN ) == 0 ) )
2.206 + if ( sent == count_bytes )
2.207 + {
2.208 + response = ( recv == size );
2.209 + g_print( "[%s]\t\tsent %d, which is equals to bytes_read = %d\n\n",
2.210 + __FUNCTION__, sent, count_bytes );
2.211 + if ( response == TRUE )
2.212 + break;
2.213 + }
2.214 + else
2.215 + {
2.216 + g_print( "[%s]\t\tsent %d, which is NOT equals to bytes_read = %d\n\n",
2.217 + __FUNCTION__, sent, count_bytes );
2.218 + goto cleanup;
2.219 + //response = FALSE;
2.220 + //break;
2.221 + }
2.222 + } else {
2.223 + break;
2.224 + //goto cleanup;
2.225 + } // if
2.226 + } // if - reading control response from backend
2.227 + } else {
2.228 + response = FALSE;
2.229 + } // if - stringlist response
2.230 +
2.231 + } // while
2.232 +
2.233 + io_cond_control = g_io_channel_get_buffer_condition( io_channel_control );
2.234 + // io_cond = g_io_channel_get_buffer_condition( io_channel );
2.235 +
2.236 + if ( ( ( io_cond_control & G_IO_IN ) != 0 ) &&
2.237 + ( response || ( recv == size ) ) )
2.238 {
2.239 if ( gmyth_socket_read_stringlist( transfer->control_sock, strlist ) > 0 )
2.240 {
2.241 - if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 ) {
2.242 - sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
2.243 - g_print( "[%s]\t sent = %d, raw buffer I/O socket %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__,
2.244 - sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
2.245 + if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 )
2.246 + {
2.247 + sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error
2.248 + g_print( "[%s]\t received = %d bytes -\tNOW returning from reading buffer I/O socket "\
2.249 + "[%s prepared for reading]! (G_IO_IN) !!!\n\n", __FUNCTION__,
2.250 + sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" );
2.251 }
2.252 }
2.253 else
2.254 @@ -889,13 +949,14 @@
2.255 recv = -1;
2.256 }
2.257
2.258 - } else if ( error!=NULL ) {
2.259 + }
2.260 + else if ( error != NULL )
2.261 + {
2.262 g_printerr( "[%s] Error occurred: (%d, %s)\n", __FUNCTION__, error->code, error->message );
2.263 - //g_error_free( error );
2.264 - }
2.265 + }
2.266
2.267 cleanup:
2.268 - g_static_mutex_unlock (&mutex);
2.269 + //g_static_mutex_unlock (&mutex);
2.270
2.271 if ( trash != NULL )
2.272 g_free( trash );
2.273 @@ -907,8 +968,8 @@
2.274 "(rcvd and rept MUST be the same!)\n", size,
2.275 recv, sent );
2.276
2.277 - //if ( sent != recv ) {
2.278 - // recv = -1;
2.279 + //if ( ( recv != size ) || ( sent != size ) ) {
2.280 + //recv = size;
2.281 //}
2.282
2.283 if ( error != NULL ) {