# HG changeset patch # User rosfran # Date 1161211517 -3600 # Node ID c71d37b9373487a45e1d0bc51508a420f318974e # Parent 79fca2122593bbb26517bc4c908bca50ecb4a800 [svn r35] Performance optimization. diff -r 79fca2122593 -r c71d37b93734 gst-plugins-mythtv/src/gstmythtvsrc.c --- a/gst-plugins-mythtv/src/gstmythtvsrc.c Wed Oct 18 23:08:56 2006 +0100 +++ b/gst-plugins-mythtv/src/gstmythtvsrc.c Wed Oct 18 23:45:17 2006 +0100 @@ -36,14 +36,14 @@ #define MYTHTV_TRANSFER_MAX_WAITS 100 -#define MYTHTV_TRANSFER_MAX_BUFFER 4*1024 +#define MYTHTV_TRANSFER_MAX_BUFFER 1024*1024 //( 32*1024 ) /* 4*1024 ??? */ #define MAX_READ_SIZE 12*1024 //( 32*1024 ) -#define ENABLE_TIMING_POSITION 1 +#define ENABLE_TIMING_POSITION 0 /* stablish a maximum iteration value to the IS_RECORDING message */ static guint wait_to_transfer = 0; @@ -289,7 +289,7 @@ * Retry whilst authentication fails and we supply it. */ guint len = 0; - GST_OBJECT_LOCK(src); + //GST_OBJECT_LOCK(src); while ( sizetoread > 0 ) { @@ -297,17 +297,36 @@ GST_BUFFER_DATA( *outbuf ) + read, sizetoread, TRUE ); if ( len > 0 ) { - read += len; - src->read_offset += read; + read += len; sizetoread -= len; } else if ( len <= 0 ) { if ( src->live_tv == FALSE ) { goto done; - } else if ( src->content_size >= src->read_offset && - abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) { - goto done; - } + } else if ( /*src->content_size >= src->read_offset && + abs ( src->content_size - src->read_offset ) <= 1024 ) ||*/ + ( src->content_size <= ( src->read_offset + size + MYTHTV_TRANSFER_MAX_BUFFER ) ) ) + { +#if ENABLE_TIMING_POSITION == 1 + guint64 size_tmp = 0; + if (src->live_tv == TRUE) { +get_file_pos: + size_tmp = myth_file_transfer_get_file_position( src->file_transfer ); + if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) ) + src->content_size = size_tmp; + else + goto get_file_pos; + g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n", + __FUNCTION__, size_tmp ); + } +#else + guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer ); + if ( src->content_size < new_offset ) { + src->content_size = new_offset; + } +#endif + goto done; + } } @@ -316,6 +335,7 @@ } if ( read > 0 ) { + src->read_offset += read; src->bytes_read += read; g_print( "[%s]\tBYTES READ (actual) = %d, BYTES READ (cumulative) = %llu, "\ @@ -333,7 +353,7 @@ "OFFSET_END = %llu.\n\n", __FUNCTION__, GST_BUFFER_SIZE (*outbuf), GST_BUFFER_OFFSET (*outbuf), GST_BUFFER_OFFSET_END (*outbuf) ); - } else + } else if ( !src->live_tv ) goto eos; goto done; @@ -343,7 +363,7 @@ src->eos = TRUE; done: - GST_OBJECT_UNLOCK(src); + //GST_OBJECT_UNLOCK(src); return read; } @@ -361,33 +381,13 @@ goto eos; //GST_OBJECT_LOCK(src); - if ( /*src->content_size >= src->read_offset && - abs ( src->content_size - src->read_offset ) <= 1024 ) ||*/ - ( src->content_size <= ( src->read_offset + size ) ) ) - { -#if ENABLE_TIMING_POSITION == 1 - guint64 size_tmp = 0; - if (src->live_tv == TRUE) { -get_file_pos: - size_tmp = myth_file_transfer_get_file_position( src->file_transfer ); - if ( size_tmp > ( src->content_size + MYTHTV_TRANSFER_MAX_BUFFER ) ) - src->content_size = size_tmp; - else - goto get_file_pos; - g_print( "\t[%s]\tGET_POSITION: file_position = %llu\n", - __FUNCTION__, size_tmp ); - } -#endif - //guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer ); - //if ( src->content_size < new_offset ) { - // src->content_size = new_offset; - //} - } if (G_UNLIKELY (src->read_offset != offset)) { guint64 new_offset = myth_file_transfer_seek(src->file_transfer, offset, SEEK_SET); - - if (G_UNLIKELY (new_offset < 0 || new_offset != offset)) { + g_print( "[%s] SRC Offset = %llu, NEW actual backend SEEK Offset = %llu.\n", + __FUNCTION__, src->read_offset, new_offset ); + if (G_UNLIKELY (new_offset < 0 ) )//|| new_offset != src->read_offset)) { + { //GST_OBJECT_UNLOCK(src); goto read_error; } @@ -446,15 +446,6 @@ return GST_FLOW_ERROR; } -#if 0 -need_pause: - { - const gchar *reason = gst_flow_get_name (ret); - - GST_DEBUG_OBJECT (src, "pausing task, reason %s", reason); - return GST_FLOW_UNEXPECTED; - } -#endif } void @@ -690,16 +681,14 @@ if (src->content_size <= 0) { ret= FALSE; - } else if ( src->content_size >= src->read_offset && - abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) { - g_static_mutex_lock( &update_size_mutex ); + } else if ( abs ( src->content_size - src->read_offset ) <= MYTHTV_TRANSFER_MAX_BUFFER ) { + //g_static_mutex_lock( &update_size_mutex ); guint64 new_offset = myth_file_transfer_get_file_position( src->file_transfer ); if ( src->content_size < new_offset ) { src->content_size = new_offset; } -#if 0 #if ENABLE_TIMING_POSITION == 1 guint64 size_tmp = 0; if (src->live_tv == TRUE) { @@ -714,8 +703,7 @@ __FUNCTION__, size_tmp ); } #endif -#endif - g_static_mutex_unlock( &update_size_mutex ); + //g_static_mutex_unlock( &update_size_mutex ); } @@ -771,14 +759,17 @@ case GST_EVENT_EOS: g_print( "[%s] Got EOS event!!!\n", __FUNCTION__ ); guint64 cont_size = gst_mythtv_src_get_position (src); - if ( cont_size > src->content_size ) { - src->content_size = cont_size; - src->eos = FALSE; - } else { - src->eos = TRUE; - gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL ); - gst_element_set_locked_state ( GST_ELEMENT (src), FALSE ); - } + if ( !src->live_tv ) { + if ( cont_size > src->content_size ) { + src->content_size = cont_size; + src->eos = FALSE; + } else { + src->eos = TRUE; + gst_element_set_state ( GST_ELEMENT (src), GST_STATE_NULL ); + gst_element_set_locked_state ( GST_ELEMENT (src), FALSE ); + } + } else + src->eos = TRUE; break; #if 0 case GST_EVENT_NEWSEGMENT: diff -r 79fca2122593 -r c71d37b93734 gst-plugins-mythtv/src/myth_file_transfer.c --- a/gst-plugins-mythtv/src/myth_file_transfer.c Wed Oct 18 23:08:56 2006 +0100 +++ b/gst-plugins-mythtv/src/myth_file_transfer.c Wed Oct 18 23:45:17 2006 +0100 @@ -32,7 +32,7 @@ #define MYTHTV_RETRIES 1 #define MYTHTV_FILE_SIZE -1 -#define MYTHTV_BUFFER_SIZE 2048 +#define MYTHTV_BUFFER_SIZE 256*1024 #define MYTHTV_VERSION 30 @@ -686,14 +686,13 @@ return NULL; } - gint +gint myth_file_transfer_read(MythFileTransfer *transfer, void *data, gint size, gboolean read_unlimited) { gint recv = 0; gsize bytes_read = 0; - gint sent = 0; - //guint zerocnt = 0; + guint remaining = 0; gboolean response = FALSE; GIOChannel *io_channel; @@ -727,7 +726,6 @@ if ( io_status == G_IO_STATUS_NORMAL ) g_print( "[%s] Setting encoding to binary data socket).\n", __FUNCTION__ ); - io_cond = g_io_channel_get_buffer_condition( io_channel ); io_cond_control = g_io_channel_get_buffer_condition( io_channel ); @@ -754,16 +752,23 @@ if ( ( io_cond & G_IO_IN ) != 0 ) { do { + trash = g_new0( gchar, MYTHTV_BUFFER_SIZE ); - io_status = g_io_channel_read_line( io_channel, &trash, &bytes_read, NULL, &error); + io_status = g_io_channel_read_chars( io_channel, trash, + MYTHTV_BUFFER_SIZE, &bytes_read, &error); - g_print( "[%s] cleaning buffer on IO binary channel... (%s)\n", __FUNCTION__, trash ); + g_print( "[%s] cleaning buffer on IO binary channel... %d bytes gone!\n", + __FUNCTION__, bytes_read ); + + if ( trash != NULL ) + g_free( trash ); + io_cond = g_io_channel_get_buffer_condition( io_channel ); - } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) ); + } while ( ( io_cond & G_IO_IN ) != 0 && ( io_status != G_IO_STATUS_ERROR ) && (error == NULL) ); - if ( trash!= NULL ) - g_free( trash ); + //if ( trash!= NULL ) + // g_free( trash ); } if ( ( io_cond_control & G_IO_IN ) != 0 ) { @@ -789,36 +794,40 @@ /* transfer->live_tv ? transfer->card_id :*/ transfer->recordernum ); // transfer->recordernum g_print( "\t[%s] Transfer_query = %s\n", __FUNCTION__, transfer->query->str ); - strlist = gmyth_string_list_new(); - - gmyth_string_list_append_char_array( strlist, transfer->query->str ); - gmyth_string_list_append_char_array( strlist, - /*transfer->live_tv ? "REQUEST_BLOCK_RINGBUF" :*/ "REQUEST_BLOCK" ); - gmyth_string_list_append_int( strlist, size ); - - gmyth_socket_write_stringlist( transfer->control_sock, strlist ); sent = size; + remaining = size - recv; //g_static_mutex_unlock( &mutex ); //data = (void*)g_new0( gchar, size ); - g_io_channel_flush( io_channel_control, NULL ); //g_io_channel_flush( io_channel, NULL ); - g_static_mutex_lock( &mutex ); + //g_static_mutex_lock( &mutex ); io_cond = g_io_channel_get_buffer_condition( io_channel ); - while ( ( recv < sent ) )//&& ( io_cond & G_IO_IN ) != 0 ) + while ( recv < size && !response )//&& ( io_cond & G_IO_IN ) != 0 ) { + g_io_channel_flush( io_channel_control, NULL ); + + strlist = gmyth_string_list_new(); + gmyth_string_list_append_char_array( strlist, transfer->query->str ); + gmyth_string_list_append_char_array( strlist, + /*transfer->live_tv ? "REQUEST_BLOCK_RINGBUF" :*/ "REQUEST_BLOCK" ); + gmyth_string_list_append_int( strlist, remaining ); + gmyth_socket_write_stringlist( transfer->control_sock, strlist ); + + guint count_bytes = 0; + do { - //while ( ( io_cond & G_IO_IN ) == 0 ) { - //usleep(200); - // - //io_cond = g_io_channel_get_buffer_condition( io_channel ); - //g_static_mutex_lock( &mutex ); - - buf_len = ( sent - recv ) > MYTHTV_BUFFER_SIZE ? MYTHTV_BUFFER_SIZE : ( sent - recv ); + //buf_len = ( sent - recv ) > MYTHTV_BUFFER_SIZE ? MYTHTV_BUFFER_SIZE : ( sent - recv ); + if ( remaining > MYTHTV_BUFFER_SIZE ) { + buf_len = MYTHTV_BUFFER_SIZE; + } else { + buf_len = remaining; + } + + bytes_read = 0; io_status = g_io_channel_read_chars( io_channel, data + recv, buf_len, &bytes_read, &error ); @@ -832,55 +841,106 @@ */ if ( bytes_read > 0 ) { - if ( bytes_read <= buf_len ) - recv += bytes_read; - } + //if ( bytes_read <= buf_len ) + recv += bytes_read; + count_bytes += bytes_read; + remaining -= bytes_read; + g_print( "[%s] Reading buffer (bytes read = %d, remaining = %d)\n", __FUNCTION__, bytes_read, remaining ); + if ( remaining == 0 ) { + break; + } + } else { + break; + } + + //if ( remaining > 0 ) { if ( io_status == G_IO_STATUS_EOF ) { - g_printerr( "[%s] got EOS!", __FUNCTION__ ); + g_print( "[%s] got EOS!", __FUNCTION__ ); break; } else if ( io_status == G_IO_STATUS_ERROR ) { - g_printerr( "[%s] myth_file_transfer_read(): socket error.\n", __FUNCTION__ ); + g_print( "[%s] myth_file_transfer_read(): socket error.\n", __FUNCTION__ ); break; } + //} /* increase buffer size, to allow get more data (do not obey to the buffer size) */ if ( read_unlimited == TRUE ) { + // FOR NOW, DO NOTHING!!! //if ( recv > buf_len ) // sent += (bytes_read - buf_len) + 1; } - if ( bytes_read == buf_len ) - break; /* verify if the input (read) buffer is ready to receive data */ io_cond = g_io_channel_get_buffer_condition( io_channel ); g_print( "[%s]\t io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" ); + + //if ( recv == size ) + //break; - } while ( recv < sent && ( ( io_cond & G_IO_IN ) != 0 ) && ( io_status == G_IO_STATUS_NORMAL ) ); + } while ( remaining > 0 );//&& ( io_status == G_IO_STATUS_NORMAL ) ); + + // if ( ( recv < size ) ) { + // finish_read = FALSE; + //} io_cond_control = g_io_channel_get_buffer_condition( io_channel_control ); - if ( ( io_status == G_IO_STATUS_EOF ) || ( ( io_cond_control & G_IO_IN ) != 0 ) ) + if ( remaining == 0 )//( io_cond_control & G_IO_IN ) != 0 ) { gmyth_socket_read_stringlist( transfer->control_sock, strlist ); - sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error - g_print( "[%s]\t sent = %d, io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, - sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" ); - response = TRUE; - break; - } - } + if ( strlist != NULL && gmyth_string_list_length( strlist ) > 0 ) + { + sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error + g_print( "[%s] got SENT buffer message = %d\n", __FUNCTION__, sent ); + if ( sent != 0 ) + { + g_print( "[%s]\t received = %d bytes, backend says %d bytes sent, "\ + "io_cond %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, + recv, sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" ); - if ( ( ( error == NULL ) && ( response == FALSE ) ) || - ( io_status == G_IO_STATUS_EOF ) || ( ( io_cond & G_IO_IN ) == 0 ) ) + if ( sent == count_bytes ) + { + response = ( recv == size ); + g_print( "[%s]\t\tsent %d, which is equals to bytes_read = %d\n\n", + __FUNCTION__, sent, count_bytes ); + if ( response == TRUE ) + break; + } + else + { + g_print( "[%s]\t\tsent %d, which is NOT equals to bytes_read = %d\n\n", + __FUNCTION__, sent, count_bytes ); + goto cleanup; + //response = FALSE; + //break; + } + } else { + break; + //goto cleanup; + } // if + } // if - reading control response from backend + } else { + response = FALSE; + } // if - stringlist response + + } // while + + io_cond_control = g_io_channel_get_buffer_condition( io_channel_control ); + // io_cond = g_io_channel_get_buffer_condition( io_channel ); + + if ( ( ( io_cond_control & G_IO_IN ) != 0 ) && + ( response || ( recv == size ) ) ) { if ( gmyth_socket_read_stringlist( transfer->control_sock, strlist ) > 0 ) { - if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 ) { - sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error - g_print( "[%s]\t sent = %d, raw buffer I/O socket %s prepared for reading! (G_IO_IN) !!!\n\n", __FUNCTION__, - sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" ); + if ( strlist != NULL && gmyth_string_list_length(strlist) > 0 ) + { + sent = gmyth_string_list_get_int( strlist, 0 ); // -1 on backend error + g_print( "[%s]\t received = %d bytes -\tNOW returning from reading buffer I/O socket "\ + "[%s prepared for reading]! (G_IO_IN) !!!\n\n", __FUNCTION__, + sent, ( ( io_cond & G_IO_IN ) != 0 ) ? "IS" : "IS NOT" ); } } else @@ -889,13 +949,14 @@ recv = -1; } - } else if ( error!=NULL ) { + } + else if ( error != NULL ) + { g_printerr( "[%s] Error occurred: (%d, %s)\n", __FUNCTION__, error->code, error->message ); - //g_error_free( error ); - } + } cleanup: - g_static_mutex_unlock (&mutex); + //g_static_mutex_unlock (&mutex); if ( trash != NULL ) g_free( trash ); @@ -907,8 +968,8 @@ "(rcvd and rept MUST be the same!)\n", size, recv, sent ); - //if ( sent != recv ) { - // recv = -1; + //if ( ( recv != size ) || ( sent != size ) ) { + //recv = size; //} if ( error != NULL ) {