<s> import struct <EOL> from twisted . internet import glib2reactor <EOL> glib2reactor . install ( ) <EOL> from twisted . internet import reactor <EOL> from twimp import amf0 <EOL> from twimp . client import BaseClientApp , SimpleAppClientFactory <EOL> from twimp . client import connect_client_factory <EOL> from twimp . primitives import _s_uchar <EOL> from twimp . vecbuf import VecBuf <EOL> from twimp . helpers import ellip <EOL> LOG_CATEGORY = '<STR_LIT>' <EOL> import twimp . log <EOL> log = twimp . log . get_logger ( LOG_CATEGORY ) <EOL> try : <EOL> from fractions import Fraction <EOL> except ImportError : <EOL> class Fraction ( object ) : <EOL> def __init__ ( self , string ) : <EOL> if '<STR_LIT:/>' in string : <EOL> n , d = string . split ( '<STR_LIT:/>' , <NUM_LIT:1> ) <EOL> try : <EOL> num = int ( n ) <EOL> denom = int ( d ) <EOL> except ValueError : <EOL> raise ValueError ( '<STR_LIT>' % ( string , ) ) <EOL> else : <EOL> import decimal <EOL> try : <EOL> d = decimal . Decimal ( string ) . normalize ( ) <EOL> except decimal . DecimalException : <EOL> raise ValueError ( '<STR_LIT>' % ( string , ) ) <EOL> exp = - d . as_tuple ( ) [ <NUM_LIT:2> ] <EOL> if exp > <NUM_LIT:0> : <EOL> denom = <NUM_LIT:10> ** exp <EOL> num = int ( d * denom ) <EOL> else : <EOL> num = int ( d ) <EOL> denom = <NUM_LIT:1> <EOL> self . numerator = num <EOL> self . denominator = denom <EOL> def __repr__ ( self ) : <EOL> return '<STR_LIT>' % ( self . __class__ . __name__ , <EOL> self . numerator , self . denominator ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' % ( self . numerator , self . denominator ) <EOL> _s_double_uchar = struct . Struct ( '<STR_LIT>' ) <EOL> _s_h264video = struct . Struct ( '<STR_LIT>' ) <EOL> class Codec ( object ) : <EOL> description = None <EOL> def createBin ( self , ** attributes ) : <EOL> codecBin = self . createBinReal ( ** attributes ) <EOL> self . addProbes ( codecBin ) <EOL> return codecBin <EOL> def createBinReal ( self , ** attributes ) : <EOL> description = self . description % attributes <EOL> return gst . parse_bin_from_description ( description , <EOL> ghost_unconnected_pads = True ) <EOL> def addProbes ( self , codecBin ) : <EOL> for padName in ( "<STR_LIT>" , "<STR_LIT:src>" ) : <EOL> pad = codecBin . get_pad ( padName ) <EOL> pad . connect ( "<STR_LIT>" , self . padNotifyCaps ) <EOL> def padNotifyCaps ( self , pad , pspec ) : <EOL> log . debug ( "<STR_LIT>" , pad , pad . get_negotiated_caps ( ) ) <EOL> def getHeadersFromCaps ( self , caps ) : <EOL> return None <EOL> class VideoCodec ( Codec ) : <EOL> codecType = "<STR_LIT>" <EOL> class AudioCodec ( Codec ) : <EOL> codecType = "<STR_LIT>" <EOL> class H263VideoCodec ( VideoCodec ) : <EOL> codecId = <NUM_LIT:2> <EOL> name = '<STR_LIT>' <EOL> description = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def createBinReal ( self , ** attributes ) : <EOL> attributes [ '<STR_LIT>' ] *= <NUM_LIT:1000> <EOL> return super ( H263VideoCodec , self ) . createBinReal ( ** attributes ) <EOL> class SorensonVideoCodec ( H263VideoCodec ) : <EOL> name = '<STR_LIT>' <EOL> class H264VideoCodec ( VideoCodec ) : <EOL> codecId = <NUM_LIT:7> <EOL> name = '<STR_LIT>' <EOL> description = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def getHeadersFromCaps ( self , caps ) : <EOL> return [ buffer ( caps [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) ] <EOL> class MP3AudioCodec ( AudioCodec ) : <EOL> codecId = <NUM_LIT:2> <EOL> name = '<STR_LIT>' <EOL> description = "<STR_LIT>" <EOL> class AACAudioCodec ( AudioCodec ) : <EOL> codecId = <NUM_LIT:10> <EOL> name = '<STR_LIT>' <EOL> description = "<STR_LIT>" <EOL> def createBinReal ( self , ** attributes ) : <EOL> attributes [ '<STR_LIT>' ] *= <NUM_LIT:1000> <EOL> return super ( AACAudioCodec , self ) . createBinReal ( ** attributes ) <EOL> def getHeadersFromCaps ( self , caps ) : <EOL> return [ buffer ( caps [ <NUM_LIT:0> ] [ '<STR_LIT>' ] ) ] <EOL> class SpeexAudioCodec ( AudioCodec ) : <EOL> codecId = <NUM_LIT:11> <EOL> name = '<STR_LIT>' <EOL> description = '<STR_LIT>' <EOL> def createBinReal ( self , ** attributes ) : <EOL> attributes [ '<STR_LIT>' ] *= <NUM_LIT:1000> <EOL> return super ( SpeexAudioCodec , self ) . createBinReal ( ** attributes ) <EOL> def getHeadersFromCaps ( self , caps ) : <EOL> return None <EOL> class NoCodec ( object ) : <EOL> name = '<STR_LIT:none>' <EOL> def codecMap ( * codecs ) : <EOL> return dict ( ( codec . name , codec ( ) ) for codec in codecs ) <EOL> video_codecs = codecMap ( H263VideoCodec , SorensonVideoCodec , <EOL> H264VideoCodec , NoCodec ) <EOL> audio_codecs = codecMap ( MP3AudioCodec , AACAudioCodec , SpeexAudioCodec , NoCodec ) <EOL> class NewGstSource ( object ) : <EOL> def __init__ ( self , audiobitrate , videobitrate , audiorate , <EOL> channels , framerate , width , height , keyrate , audio_codec = None , <EOL> video_codec = None , audiosrc = '<STR_LIT>' , videosrc = '<STR_LIT>' , <EOL> audio_opts = '<STR_LIT>' , video_opts = '<STR_LIT>' , videosink = '<STR_LIT>' , view = False , <EOL> view_window = None ) : <EOL> if audio_codec is None : <EOL> audio_codec = AACAudioCodec ( ) <EOL> elif isinstance ( audio_codec , NoCodec ) : <EOL> audio_codec = None <EOL> if video_codec is None : <EOL> video_codec = H264VideoCodec ( ) <EOL> elif isinstance ( video_codec , NoCodec ) : <EOL> video_codec = None <EOL> if audiorate is None : <EOL> if audio_codec is not None and audio_codec . codecId == <NUM_LIT:11> : <EOL> audiorate = <NUM_LIT> <EOL> else : <EOL> audiorate = <NUM_LIT> <EOL> self . audio_codec = audio_codec <EOL> self . audio_bitrate = audiobitrate <EOL> self . audio_rate = audiorate <EOL> self . channels = channels <EOL> self . audio_opts = audio_opts <EOL> self . video_codec = video_codec <EOL> self . video_bitrate = videobitrate <EOL> self . framerate = framerate <EOL> self . width = width <EOL> self . height = height <EOL> self . key_rate = keyrate <EOL> self . key_interval = int ( round ( keyrate * framerate . numerator / <EOL> float ( framerate . denominator ) ) ) <EOL> self . video_opts = video_opts <EOL> self . videosink = videosink <EOL> self . view = view <EOL> self . view_window = view_window <EOL> self . audiosrc = audiosrc <EOL> self . videosrc = videosrc <EOL> self . pipeline = None <EOL> self . _stream = None <EOL> self . asink = None <EOL> self . audio_caps = None <EOL> self . vsink = None <EOL> self . video_caps = None <EOL> self . video_avc_profile = None <EOL> self . video_avc_level = None <EOL> self . _ah1 = <NUM_LIT:0> <EOL> self . _header_audio = None <EOL> self . complex_audio_header = ( self . audio_codec is not None and <EOL> self . audio_codec . codecId == <NUM_LIT:10> ) <EOL> def _make_pipeline_audio ( self ) : <EOL> source = gst . element_factory_make ( self . audiosrc ) <EOL> queue = gst . element_factory_make ( "<STR_LIT>" ) <EOL> audiorate = gst . element_factory_make ( "<STR_LIT>" ) <EOL> audioconvert = gst . element_factory_make ( "<STR_LIT>" ) <EOL> capsfilter = gst . element_factory_make ( "<STR_LIT>" ) <EOL> tee = gst . element_factory_make ( "<STR_LIT>" ) <EOL> encoder = self . audio_codec . createBin ( bitrate = self . audio_bitrate ) <EOL> self . asink = sink = gst . element_factory_make ( "<STR_LIT>" ) <EOL> sink . props . async = True <EOL> filter_caps = gst . Caps ( ) <EOL> audio_formats = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> for audio_format in audio_formats : <EOL> structure = gst . Structure ( audio_format ) <EOL> structure [ "<STR_LIT>" ] = self . audio_rate <EOL> structure [ "<STR_LIT>" ] = self . channels <EOL> filter_caps . append_structure ( structure ) <EOL> capsfilter . props . caps = filter_caps <EOL> audioBin = gst . Bin ( ) <EOL> audioBin . add ( source , queue , audiorate , audioconvert , capsfilter , <EOL> tee , encoder , sink ) <EOL> gst . element_link_many ( source , queue , audiorate , audioconvert , <EOL> capsfilter , tee , encoder , sink ) <EOL> return audioBin <EOL> def _make_pipeline_video ( self ) : <EOL> videosrc = gst . element_factory_make ( self . videosrc ) <EOL> queue = gst . element_factory_make ( "<STR_LIT>" ) <EOL> videorate = gst . element_factory_make ( "<STR_LIT>" ) <EOL> ffmpegcolorspace = gst . element_factory_make ( "<STR_LIT>" ) <EOL> videoscale = gst . element_factory_make ( "<STR_LIT>" ) <EOL> capsfilter = gst . element_factory_make ( "<STR_LIT>" ) <EOL> tee = gst . element_factory_make ( "<STR_LIT>" ) <EOL> encoder = self . video_codec . createBin ( bitrate = self . video_bitrate , <EOL> key_interval = self . key_interval ) <EOL> self . vsink = sink = gst . element_factory_make ( "<STR_LIT>" ) <EOL> sink . props . async = True <EOL> filter_caps = gst . Caps ( ) <EOL> video_formats = ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> for video_format in video_formats : <EOL> structure = gst . Structure ( video_format ) <EOL> structure [ "<STR_LIT:width>" ] = self . width <EOL> structure [ "<STR_LIT>" ] = self . height <EOL> structure [ "<STR_LIT>" ] = gst . Fraction ( self . framerate . numerator , <EOL> self . framerate . denominator ) <EOL> filter_caps . append_structure ( structure ) <EOL> capsfilter . props . caps = filter_caps <EOL> videoBin = gst . Bin ( ) <EOL> videoBin . add ( videosrc , queue , videorate , ffmpegcolorspace , videoscale , <EOL> capsfilter , tee , encoder , sink ) <EOL> gst . element_link_many ( videosrc , queue , videorate , ffmpegcolorspace , <EOL> videoscale , capsfilter , tee , encoder , sink ) <EOL> if self . view : <EOL> queue1 = gst . element_factory_make ( "<STR_LIT>" ) <EOL> ffmpegcolorspace1 = gst . element_factory_make ( "<STR_LIT>" ) <EOL> videoscale1 = gst . element_factory_make ( "<STR_LIT>" ) <EOL> videosink = gst . element_factory_make ( self . videosink ) <EOL> videoBin . add ( queue1 , ffmpegcolorspace1 , videoscale1 , videosink ) <EOL> gst . element_link_many ( tee , queue1 , ffmpegcolorspace1 , videoscale1 , <EOL> videosink ) <EOL> return videoBin <EOL> def make_pipeline ( self ) : <EOL> if not ( self . audio_codec or self . video_codec ) : <EOL> raise RuntimeError ( '<STR_LIT>' ) <EOL> self . pipeline = gst . Pipeline ( ) <EOL> if self . audio_codec : <EOL> self . pipeline . add ( self . _make_pipeline_audio ( ) ) <EOL> if self . video_codec : <EOL> self . pipeline . add ( self . _make_pipeline_video ( ) ) <EOL> def connect ( self , stream ) : <EOL> self . _stream = stream <EOL> def disconnect ( self ) : <EOL> self . _stream = None <EOL> def write_rtmp_meta_headers ( self , ts ) : <EOL> meta = dict ( duration = <NUM_LIT:0.0> ) <EOL> if self . vsink : <EOL> meta . update ( width = self . width , height = self . height , <EOL> framerate = ( float ( self . framerate . numerator ) / <EOL> self . framerate . denominator ) , <EOL> videodatarate = self . video_bitrate , <EOL> videokeyframe_frequency = self . key_rate ) <EOL> if self . video_codec . codecId == <NUM_LIT:7> : <EOL> meta . update ( videocodecid = '<STR_LIT>' , <EOL> avcprofile = self . video_avc_profile , <EOL> avclevel = self . video_avc_level ) <EOL> if self . asink : <EOL> meta . update ( audiosamplerate = self . audio_rate , <EOL> audiodatarate = self . audio_bitrate , <EOL> audiochannels = self . channels ) <EOL> if self . audio_codec . codecId == <NUM_LIT:2> : <EOL> meta . update ( audiocodecid = '<STR_LIT>' ) <EOL> elif self . audio_codec . codecId == <NUM_LIT:10> : <EOL> meta . update ( audiocodecid = '<STR_LIT>' ) <EOL> wm = self . _stream . write_meta <EOL> wm ( ts , amf0 . encode ( '<STR_LIT>' , <EOL> amf0 . Object ( code = '<STR_LIT>' ) ) ) <EOL> wm ( ts , amf0 . encode ( '<STR_LIT>' , meta ) ) <EOL> wm ( ts , amf0 . encode ( '<STR_LIT>' , '<STR_LIT>' , meta ) ) <EOL> def prepare_audio ( self ) : <EOL> log . info ( '<STR_LIT>' , self . audio_caps ) <EOL> if self . audio_codec . codecId in ( <NUM_LIT:10> , <NUM_LIT:11> ) : <EOL> rate = <NUM_LIT> <EOL> channels = <NUM_LIT:2> <EOL> else : <EOL> rate = self . audio_rate <EOL> channels = self . channels <EOL> h1 = self . audio_codec . codecId << <NUM_LIT:4> <EOL> h1 |= { <NUM_LIT> : <NUM_LIT:3> , <NUM_LIT> : <NUM_LIT:2> , <NUM_LIT> : <NUM_LIT:1> } . get ( rate , <NUM_LIT:3> ) << <NUM_LIT:2> <EOL> h1 |= <NUM_LIT:1> << <NUM_LIT:1> <EOL> h1 |= int ( channels == <NUM_LIT:2> ) <EOL> self . _ah1 = h1 <EOL> if self . complex_audio_header : <EOL> self . _header_audio = _s_double_uchar . pack ( h1 , <NUM_LIT:1> ) <EOL> else : <EOL> self . _header_audio = _s_uchar . pack ( h1 ) <EOL> def prepare_video ( self ) : <EOL> log . info ( '<STR_LIT>' , self . video_caps ) <EOL> if self . video_codec . codecId == <NUM_LIT:7> : <EOL> codec_data = self . video_caps [ <NUM_LIT:0> ] [ '<STR_LIT>' ] <EOL> self . video_avc_profile = ord ( codec_data [ <NUM_LIT:1> ] ) <EOL> self . video_avc_level = ord ( codec_data [ <NUM_LIT:3> ] ) <EOL> self . _make_rtmp_video = self . _make_rtmp_video_complex <EOL> else : <EOL> self . _make_rtmp_video = self . _make_rtmp_video_simple <EOL> def write_rtmp_audio_headers ( self , ts , codec_data ) : <EOL> h = _s_double_uchar . pack ( self . _ah1 , <NUM_LIT:0> ) <EOL> for buf in codec_data : <EOL> self . _stream . write_audio ( ts , VecBuf ( [ h , buf ] ) ) <EOL> def write_rtmp_video_headers ( self , ts , codec_data ) : <EOL> h = _s_h264video . pack ( <NUM_LIT> | self . video_codec . codecId , <NUM_LIT:0> , <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> for buf in codec_data : <EOL> self . _stream . write_video ( ts , VecBuf ( [ h , buf ] ) ) <EOL> def make_rtmp_audio ( self , gst_buf ) : <EOL> return VecBuf ( [ self . _header_audio , buffer ( gst_buf ) ] ) <EOL> def _audio_data_cb ( self , element ) : <EOL> buf = element . emit ( '<STR_LIT>' ) <EOL> ts = int ( buf . timestamp / <NUM_LIT> ) <EOL> ts = ts % <NUM_LIT> <EOL> log . debug ( '<STR_LIT>' , int ( ts ) , int ( buf . duration / <NUM_LIT> ) , <EOL> ellip ( str ( buf ) . encode ( '<STR_LIT>' ) , maxlen = <NUM_LIT> ) ) <EOL> frame = self . make_rtmp_audio ( buf ) <EOL> reactor . callFromThread ( self . _stream . write_audio , ts , frame ) <EOL> def _make_rtmp_video_simple ( self , gst_buf ) : <EOL> flags = <NUM_LIT> <EOL> if gst_buf . flags & gst . BUFFER_FLAG_DELTA_UNIT : <EOL> flags = <NUM_LIT> <EOL> return VecBuf ( [ _s_uchar . pack ( flags | self . video_codec . codecId ) , <EOL> buffer ( gst_buf ) ] ) <EOL> def _make_rtmp_video_complex ( self , gst_buf ) : <EOL> flags = <NUM_LIT> <EOL> if gst_buf . flags & gst . BUFFER_FLAG_DELTA_UNIT : <EOL> flags = <NUM_LIT> <EOL> return VecBuf ( [ _s_h264video . pack ( flags | self . video_codec . codecId , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> ) , <EOL> buffer ( gst_buf ) ] ) <EOL> def make_rtmp_video ( self , gst_buf ) : <EOL> return self . _make_rtmp_video ( gst_buf ) <EOL> def _video_data_cb ( self , element ) : <EOL> buf = element . emit ( '<STR_LIT>' ) <EOL> ts = int ( buf . timestamp / <NUM_LIT> ) <EOL> ts = ts % <NUM_LIT> <EOL> log . debug ( '<STR_LIT>' , int ( ts ) , int ( buf . duration / <NUM_LIT> ) , <EOL> ellip ( str ( buf ) . encode ( '<STR_LIT>' ) , maxlen = <NUM_LIT> ) ) <EOL> frame = self . make_rtmp_video ( buf ) <EOL> reactor . callFromThread ( self . _stream . write_video , ts , frame ) <EOL> def _got_caps_cb ( self , pad , args ) : <EOL> cid , is_video = self . _caps_pending . pop ( pad ) <EOL> pad . disconnect ( cid ) <EOL> caps = pad . get_negotiated_caps ( ) <EOL> if is_video : <EOL> self . video_caps = caps <EOL> else : <EOL> self . audio_caps = caps <EOL> if not self . _caps_pending : <EOL> self . _have_caps ( ) <EOL> def _have_caps ( self ) : <EOL> self . _attach_sink_callbacks ( ) <EOL> if self . vsink : <EOL> self . prepare_video ( ) <EOL> if self . asink : <EOL> self . prepare_audio ( ) <EOL> self . _send_headers ( ) <EOL> def _send_headers ( self ) : <EOL> reactor . callFromThread ( self . write_rtmp_meta_headers , <NUM_LIT:0> ) <EOL> if self . vsink : <EOL> headers = self . video_codec . getHeadersFromCaps ( self . video_caps ) <EOL> if headers is not None : <EOL> reactor . callFromThread ( self . write_rtmp_video_headers , <NUM_LIT:0> , <EOL> headers ) <EOL> if self . asink : <EOL> headers = self . audio_codec . getHeadersFromCaps ( self . audio_caps ) <EOL> if headers is not None : <EOL> reactor . callFromThread ( self . write_rtmp_audio_headers , <NUM_LIT:0> , <EOL> headers ) <EOL> def _build_pipeline ( self ) : <EOL> self . make_pipeline ( ) <EOL> log . info ( '<STR_LIT>' , self . pipeline ) <EOL> self . _caps_pending = { } <EOL> if self . audio_codec : <EOL> pad = self . asink . get_static_pad ( '<STR_LIT>' ) <EOL> cid = pad . connect ( '<STR_LIT>' , self . _got_caps_cb ) <EOL> self . _caps_pending [ pad ] = ( cid , <NUM_LIT:0> ) <EOL> if self . video_codec : <EOL> pad = self . vsink . get_static_pad ( '<STR_LIT>' ) <EOL> cid = pad . connect ( '<STR_LIT>' , self . _got_caps_cb ) <EOL> self . _caps_pending [ pad ] = ( cid , <NUM_LIT:1> ) <EOL> def _attach_sink_callbacks ( self ) : <EOL> if self . asink : <EOL> self . asink . connect ( '<STR_LIT>' , self . _audio_data_cb ) <EOL> self . asink . props . emit_signals = True <EOL> if self . vsink : <EOL> self . vsink . connect ( '<STR_LIT>' , self . _video_data_cb ) <EOL> self . vsink . props . emit_signals = True <EOL> def _bus_message_eos_cb ( self , bus , message ) : <EOL> log . info ( "<STR_LIT>" ) <EOL> reactor . stop ( ) <EOL> def _bus_message_error_cb ( self , bus , message ) : <EOL> gerror , debug = message . parse_error ( ) <EOL> log . error ( "<STR_LIT>" , gerror . message , debug ) <EOL> reactor . stop ( ) <EOL> def _bus_sync_message_element_cb ( self , bus , message ) : <EOL> structure = message . structure <EOL> if structure . get_name ( ) == '<STR_LIT>' : <EOL> if self . view_window is None : <EOL> return <EOL> sink = message . src <EOL> sink . set_xwindow_id ( self . view_window ) <EOL> def start ( self ) : <EOL> if self . pipeline is None : <EOL> self . _build_pipeline ( ) <EOL> bus = self . pipeline . get_bus ( ) <EOL> bus . add_signal_watch ( ) <EOL> bus . connect ( '<STR_LIT>' , self . _bus_message_eos_cb ) <EOL> bus . connect ( '<STR_LIT>' , self . _bus_message_error_cb ) <EOL> bus . enable_sync_message_emission ( ) <EOL> bus . connect ( '<STR_LIT>' , self . _bus_sync_message_element_cb ) <EOL> self . pipeline . set_state ( gst . STATE_PLAYING ) <EOL> msg = bus . timed_pop_filtered ( <NUM_LIT:20> * gst . SECOND , <EOL> gst . MESSAGE_ERROR | <EOL> gst . MESSAGE_ASYNC_DONE ) <EOL> if msg is None : <EOL> log . error ( "<STR_LIT>" ) <EOL> reactor . stop ( ) <EOL> def stop ( self ) : <EOL> if self . pipeline : <EOL> self . pipeline . set_state ( gst . STATE_NULL ) <EOL> class SimplePublishingApp ( BaseClientApp ) : <EOL> def __init__ ( self , protocol , publish_source , publish_name = '<STR_LIT>' ) : <EOL> BaseClientApp . __init__ ( self , protocol ) <EOL> self . _stream = None <EOL> self . publish_name = publish_name <EOL> self . publish_source = publish_source <EOL> def get_connect_params ( self ) : <EOL> return dict ( <EOL> ) <EOL> def connectionMade ( self , info ) : <EOL> log . info ( '<STR_LIT>' , info ) <EOL> d = self . createStream ( ) <EOL> d . addCallbacks ( self . _start_streaming , self . _disconnect ) <EOL> def _start_streaming ( self , stream ) : <EOL> self . _stream = stream <EOL> stream . publish ( self . publish_name , self . publish_source ) <EOL> def _disconnect ( self , failure ) : <EOL> self . disconnect ( ) <EOL> def connectionLost ( self , reason ) : <EOL> log . info ( '<STR_LIT>' , reason . getErrorMessage ( ) ) <EOL> if self . _stream : <EOL> self . closeStream ( self . _stream , force = True ) <EOL> self . _stream = None <EOL> self . publish_source = None <EOL> def connectionFailed ( self , reason ) : <EOL> log . error ( "<STR_LIT>" , reason . getErrorMessage ( ) ) <EOL> class OneTimeAppFactory ( SimpleAppClientFactory ) : <EOL> def clientConnectionFailed ( self , _connector , reason ) : <EOL> log . error ( '<STR_LIT>' , self . url , <EOL> reason . getErrorMessage ( ) ) <EOL> reactor . stop ( ) <EOL> def clientConnectionLost ( self , _connector , reason ) : <EOL> log . info ( '<STR_LIT>' , reason . getErrorMessage ( ) ) <EOL> if reactor . running : <EOL> reactor . stop ( ) <EOL> def run ( url , stream_name = '<STR_LIT>' , audio_codec = None , video_codec = None , <EOL> audio_bitrate = <NUM_LIT:64> , video_bitrate = <NUM_LIT> , samplerate = <NUM_LIT> , <EOL> framerate = Fraction ( '<STR_LIT>' ) , channels = <NUM_LIT:1> , <EOL> width = <NUM_LIT> , height = <NUM_LIT> , keyframe_interval = <NUM_LIT> , view = False , <EOL> view_window = None ) : <EOL> src = NewGstSource ( audio_bitrate , video_bitrate , <EOL> samplerate , channels , framerate , width , height , <EOL> keyframe_interval , audio_codec = audio_codec , <EOL> video_codec = video_codec , view = view , <EOL> view_window = view_window ) <EOL> f = connect_client_factory ( url , OneTimeAppFactory , <EOL> SimplePublishingApp , <EOL> src , <EOL> stream_name ) <EOL> reactor . run ( ) <EOL> def main ( argv ) : <EOL> import optparse <EOL> def _check_fraction ( option , opt , value ) : <EOL> try : <EOL> return Fraction ( value ) <EOL> except ValueError : <EOL> raise optparse . OptionValueError ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( opt , value ) ) <EOL> optparse . Option . TYPES += ( '<STR_LIT>' , ) <EOL> optparse . Option . TYPE_CHECKER [ '<STR_LIT>' ] = _check_fraction <EOL> default_streamname = '<STR_LIT>' <EOL> usage = '<STR_LIT>' <EOL> epilog = ( '<STR_LIT>' <EOL> '<STR_LIT>' % ( default_streamname , ) ) <EOL> parser = optparse . OptionParser ( usage = usage , epilog = epilog ) <EOL> audio_choices = sorted ( audio_codecs . keys ( ) ) <EOL> video_choices = sorted ( video_codecs . keys ( ) ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> choices = audio_choices , <EOL> help = ( '<STR_LIT>' % <EOL> '<STR_LIT:U+002CU+0020>' . join ( audio_choices ) ) ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> default = '<STR_LIT>' , <EOL> choices = video_choices , <EOL> help = ( '<STR_LIT>' % <EOL> '<STR_LIT:U+002CU+0020>' . join ( video_choices ) ) ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> type = '<STR_LIT:float>' , <EOL> help = '<STR_LIT>' , <EOL> default = <NUM_LIT> ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> type = '<STR_LIT:float>' , <EOL> help = '<STR_LIT>' , <EOL> default = <NUM_LIT> ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> type = '<STR_LIT:int>' , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> type = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT:-c>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> type = '<STR_LIT:int>' , <EOL> help = '<STR_LIT>' , <EOL> default = <NUM_LIT:1> ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT:width>' , <EOL> type = '<STR_LIT:int>' , <EOL> help = '<STR_LIT>' , <EOL> default = <NUM_LIT> ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> type = '<STR_LIT:int>' , <EOL> help = '<STR_LIT>' , <EOL> default = <NUM_LIT> ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , <EOL> dest = '<STR_LIT>' , type = '<STR_LIT:float>' , <EOL> help = '<STR_LIT>' , <EOL> default = <NUM_LIT> ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> help = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) , <EOL> metavar = '<STR_LIT>' ) <EOL> parser . add_option ( '<STR_LIT>' , '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT>' , <EOL> help = '<STR_LIT>' , <EOL> default = False ) <EOL> options , args = parser . parse_args ( argv ) <EOL> if len ( args ) < <NUM_LIT:2> : <EOL> parser . error ( '<STR_LIT>' ) <EOL> url = args [ <NUM_LIT:1> ] <EOL> stream_name = default_streamname <EOL> if len ( args ) > <NUM_LIT:2> : <EOL> stream_name = args [ <NUM_LIT:2> ] <EOL> audio_codec = audio_codecs . get ( options . acodec , None ) <EOL> video_codec = video_codecs . get ( options . vcodec , None ) <EOL> if video_codec is None and audio_codec is None : <EOL> parser . error ( "<STR_LIT>" ) <EOL> twimp . log . set_levels_from_env ( ) <EOL> if options . debug : <EOL> twimp . log . set_levels ( options . debug ) <EOL> twimp . log . hook_twisted ( ) <EOL> run ( url , stream_name , audio_codec = audio_codec , video_codec = video_codec , <EOL> audio_bitrate = options . arate , video_bitrate = options . vrate , <EOL> samplerate = options . srate , framerate = options . frate , <EOL> channels = options . channels , width = options . width , height = options . height , <EOL> keyframe_interval = options . keyint , view = options . view ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> args , sys . argv [ : ] = sys . argv [ : ] , sys . argv [ <NUM_LIT:0> : <NUM_LIT:1> ] <EOL> import gst <EOL> main ( args ) </s>
