<s> """<STR_LIT>""" <EOL> import gi <EOL> gi . require_version ( '<STR_LIT>' , '<STR_LIT:1.0>' ) <EOL> from gi . repository import GObject , Gst <EOL> GObject . threads_init ( ) <EOL> Gst . init ( None ) <EOL> import logging <EOL> import thread <EOL> import os <EOL> logger = logging . getLogger ( __name__ ) <EOL> import pdb <EOL> class DecoderPipeline2 ( object ) : <EOL> def __init__ ( self , conf = { } ) : <EOL> logger . info ( "<STR_LIT>" % conf ) <EOL> self . create_pipeline ( conf ) <EOL> self . outdir = conf . get ( "<STR_LIT>" , None ) <EOL> if not os . path . exists ( self . outdir ) : <EOL> os . makedirs ( self . outdir ) <EOL> elif not os . path . isdir ( self . outdir ) : <EOL> raise Exception ( "<STR_LIT>" % self . outdir ) <EOL> self . result_handler = None <EOL> self . full_result_handler = None <EOL> self . eos_handler = None <EOL> self . error_handler = None <EOL> self . request_id = "<STR_LIT>" <EOL> def create_pipeline ( self , conf ) : <EOL> self . appsrc = Gst . ElementFactory . make ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . decodebin = Gst . ElementFactory . make ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . audioconvert = Gst . ElementFactory . make ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . audioresample = Gst . ElementFactory . make ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . tee = Gst . ElementFactory . make ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . queue1 = Gst . ElementFactory . make ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . filesink = Gst . ElementFactory . make ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . queue2 = Gst . ElementFactory . make ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . asr = Gst . ElementFactory . make ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> self . fakesink = Gst . ElementFactory . make ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if "<STR_LIT>" in conf [ "<STR_LIT>" ] : <EOL> self . asr . set_property ( "<STR_LIT>" , conf [ "<STR_LIT>" ] [ "<STR_LIT>" ] ) <EOL> for ( key , val ) in conf . get ( "<STR_LIT>" , { } ) . iteritems ( ) : <EOL> if key != "<STR_LIT>" : <EOL> logger . info ( "<STR_LIT>" % ( key , val ) ) <EOL> self . asr . set_property ( key , val ) <EOL> self . appsrc . set_property ( "<STR_LIT>" , True ) <EOL> self . filesink . set_property ( "<STR_LIT:location>" , "<STR_LIT>" ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> self . pipeline = Gst . Pipeline ( ) <EOL> for element in [ self . appsrc , self . decodebin , self . audioconvert , self . audioresample , self . tee , <EOL> self . queue1 , self . filesink , <EOL> self . queue2 , self . asr , self . fakesink ] : <EOL> logger . debug ( "<STR_LIT>" % element ) <EOL> self . pipeline . add ( element ) <EOL> logger . info ( '<STR_LIT>' ) <EOL> self . appsrc . link ( self . decodebin ) <EOL> self . decodebin . connect ( '<STR_LIT>' , self . _connect_decoder ) <EOL> self . audioconvert . link ( self . audioresample ) <EOL> self . audioresample . link ( self . tee ) <EOL> self . tee . link ( self . queue1 ) <EOL> self . queue1 . link ( self . filesink ) <EOL> self . tee . link ( self . queue2 ) <EOL> self . queue2 . link ( self . asr ) <EOL> self . asr . link ( self . fakesink ) <EOL> self . bus = self . pipeline . get_bus ( ) <EOL> self . bus . add_signal_watch ( ) <EOL> self . bus . enable_sync_message_emission ( ) <EOL> self . bus . connect ( '<STR_LIT>' , self . _on_eos ) <EOL> self . bus . connect ( '<STR_LIT>' , self . _on_error ) <EOL> self . asr . connect ( '<STR_LIT>' , self . _on_partial_result ) <EOL> self . asr . connect ( '<STR_LIT>' , self . _on_final_result ) <EOL> self . asr . connect ( '<STR_LIT>' , self . _on_full_final_result ) <EOL> logger . info ( "<STR_LIT>" ) <EOL> self . pipeline . set_state ( Gst . State . READY ) <EOL> logger . info ( "<STR_LIT>" ) <EOL> def _connect_decoder ( self , element , pad ) : <EOL> logger . info ( "<STR_LIT>" % self . request_id ) <EOL> pad . link ( self . audioconvert . get_static_pad ( "<STR_LIT>" ) ) <EOL> logger . info ( "<STR_LIT>" % self . request_id ) <EOL> def _on_partial_result ( self , asr , hyp ) : <EOL> logger . info ( "<STR_LIT>" % ( self . request_id , hyp . decode ( '<STR_LIT:utf8>' ) ) ) <EOL> if self . result_handler : <EOL> self . result_handler ( hyp , False ) <EOL> def _on_final_result ( self , asr , hyp ) : <EOL> logger . info ( "<STR_LIT>" % ( self . request_id , hyp . decode ( '<STR_LIT:utf8>' ) ) ) <EOL> if self . result_handler : <EOL> self . result_handler ( hyp , True ) <EOL> def _on_full_final_result ( self , asr , result_json ) : <EOL> logger . info ( "<STR_LIT>" % ( self . request_id , result_json . decode ( '<STR_LIT:utf8>' ) ) ) <EOL> if self . full_result_handler : <EOL> self . full_result_handler ( result_json ) <EOL> def _on_error ( self , bus , msg ) : <EOL> self . error = msg . parse_error ( ) <EOL> logger . error ( self . error ) <EOL> self . finish_request ( ) <EOL> if self . error_handler : <EOL> self . error_handler ( self . error [ <NUM_LIT:0> ] . message ) <EOL> def _on_eos ( self , bus , msg ) : <EOL> logger . info ( '<STR_LIT>' % self . request_id ) <EOL> self . finish_request ( ) <EOL> if self . eos_handler : <EOL> self . eos_handler [ <NUM_LIT:0> ] ( self . eos_handler [ <NUM_LIT:1> ] ) <EOL> def get_adaptation_state ( self ) : <EOL> return self . asr . get_property ( "<STR_LIT>" ) <EOL> def set_adaptation_state ( self , adaptation_state ) : <EOL> """<STR_LIT>""" <EOL> return self . asr . set_property ( "<STR_LIT>" , adaptation_state ) <EOL> def finish_request ( self ) : <EOL> logger . info ( "<STR_LIT>" % self . request_id ) <EOL> if self . outdir : <EOL> self . filesink . set_state ( Gst . State . NULL ) <EOL> self . filesink . set_property ( '<STR_LIT:location>' , "<STR_LIT>" ) <EOL> self . filesink . set_state ( Gst . State . PLAYING ) <EOL> self . pipeline . set_state ( Gst . State . NULL ) <EOL> self . request_id = "<STR_LIT>" <EOL> def init_request ( self , id , caps_str ) : <EOL> self . request_id = id <EOL> logger . info ( "<STR_LIT>" % ( self . request_id ) ) <EOL> if caps_str and len ( caps_str ) > <NUM_LIT:0> : <EOL> logger . info ( "<STR_LIT>" % ( self . request_id , caps_str ) ) <EOL> caps = Gst . caps_from_string ( caps_str ) <EOL> self . appsrc . set_property ( "<STR_LIT>" , caps ) <EOL> else : <EOL> self . appsrc . set_property ( "<STR_LIT>" , None ) <EOL> pass <EOL> if self . outdir : <EOL> self . pipeline . set_state ( Gst . State . PAUSED ) <EOL> self . filesink . set_state ( Gst . State . NULL ) <EOL> self . filesink . set_property ( '<STR_LIT:location>' , "<STR_LIT>" % ( self . outdir , id ) ) <EOL> self . filesink . set_state ( Gst . State . PLAYING ) <EOL> self . pipeline . set_state ( Gst . State . PLAYING ) <EOL> self . filesink . set_state ( Gst . State . PLAYING ) <EOL> self . set_adaptation_state ( "<STR_LIT>" ) <EOL> def process_data ( self , data ) : <EOL> logger . debug ( '<STR_LIT>' % ( self . request_id , len ( data ) ) ) <EOL> buf = Gst . Buffer . new_allocate ( None , len ( data ) , None ) <EOL> buf . fill ( <NUM_LIT:0> , data ) <EOL> self . appsrc . emit ( "<STR_LIT>" , buf ) <EOL> logger . debug ( '<STR_LIT>' % self . request_id ) <EOL> def end_request ( self ) : <EOL> logger . info ( "<STR_LIT>" % self . request_id ) <EOL> self . appsrc . emit ( "<STR_LIT>" ) <EOL> def set_result_handler ( self , handler ) : <EOL> self . result_handler = handler <EOL> def set_full_result_handler ( self , handler ) : <EOL> self . full_result_handler = handler <EOL> def set_eos_handler ( self , handler , user_data = None ) : <EOL> self . eos_handler = ( handler , user_data ) <EOL> def set_error_handler ( self , handler ) : <EOL> self . error_handler = handler <EOL> def cancel ( self ) : <EOL> logger . info ( "<STR_LIT>" % self . request_id ) <EOL> self . appsrc . emit ( "<STR_LIT>" ) <EOL> logger . info ( "<STR_LIT>" % self . request_id ) </s>
