<s> """<STR_LIT>""" <EOL> from xml . etree import ElementTree <EOL> import cStringIO <EOL> import re <EOL> from . . dashlib import timeformatconversions as tfc <EOL> RE_DURATION = re . compile ( r"<STR_LIT>" ) <EOL> RE_NAMESPACE_TAG = re . compile ( r"<STR_LIT>" ) <EOL> class MpdElementError ( Exception ) : <EOL> "<STR_LIT>" <EOL> class MpdElement ( object ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , node ) : <EOL> self . node = node <EOL> self . attribs = { } <EOL> def parse ( self ) : <EOL> "<STR_LIT>" <EOL> raise MpdElementError ( "<STR_LIT>" ) <EOL> def make_live ( self , data ) : <EOL> "<STR_LIT>" <EOL> raise MpdElementError ( "<STR_LIT>" ) <EOL> def tag_and_namespace ( self , full_tag ) : <EOL> "<STR_LIT>" <EOL> match_obj = RE_NAMESPACE_TAG . match ( full_tag ) <EOL> tag = match_obj . group ( <NUM_LIT:2> ) <EOL> namespace = match_obj . group ( <NUM_LIT:1> ) <EOL> return ( tag , namespace ) <EOL> def compare_tag ( self , full_tag , string ) : <EOL> "<STR_LIT>" <EOL> tag , namespace = self . tag_and_namespace ( full_tag ) <EOL> return tag == string <EOL> def check_and_add_attributes ( self , node , attribs ) : <EOL> "<STR_LIT>" <EOL> for attr in attribs : <EOL> if node . attrib . has_key ( attr ) : <EOL> self . attribs [ attr ] = node . attrib [ attr ] <EOL> else : <EOL> if not self . attribs . has_key ( attr ) : <EOL> self . attribs [ attr ] = None <EOL> def set_value ( self , element , key , data ) : <EOL> "<STR_LIT>" <EOL> if data . has_key ( key ) : <EOL> element . set ( key , str ( data [ key ] ) ) <EOL> class Mpd ( MpdElement ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , node ) : <EOL> MpdElement . __init__ ( self , node ) <EOL> self . periods = [ ] <EOL> def parse ( self ) : <EOL> "<STR_LIT>" <EOL> self . check_and_add_attributes ( self . node , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT:type>' , '<STR_LIT>' ) ) <EOL> for child in self . node . getchildren ( ) : <EOL> if self . compare_tag ( child . tag , '<STR_LIT>' ) : <EOL> period = Period ( child ) <EOL> period . parse ( ) <EOL> self . periods . append ( period ) <EOL> def make_live ( self , data ) : <EOL> "<STR_LIT>" <EOL> self . set_value ( self . node , '<STR_LIT:type>' , '<STR_LIT>' ) <EOL> for attr in ( '<STR_LIT>' , '<STR_LIT>' ) : <EOL> self . set_value ( self . node , attr , data [ attr ] ) <EOL> for period in self . periods : <EOL> period . make_live ( data ) <EOL> class Period ( MpdElement ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , node ) : <EOL> MpdElement . __init__ ( self , node ) <EOL> self . adaptation_sets = [ ] <EOL> def parse ( self ) : <EOL> "<STR_LIT>" <EOL> self . check_and_add_attributes ( self . node , ( '<STR_LIT:id>' , '<STR_LIT:start>' ) ) <EOL> for child in self . node . getchildren ( ) : <EOL> if self . compare_tag ( child . tag , '<STR_LIT>' ) : <EOL> adaptation_set = AdaptationSet ( child ) <EOL> adaptation_set . parse ( ) <EOL> self . adaptation_sets . append ( adaptation_set ) <EOL> def make_live ( self , data ) : <EOL> for attr in ( '<STR_LIT:start>' ) : <EOL> self . set_value ( self . node , attr , data [ attr ] ) <EOL> for adaptation_set in self . adaptation_sets : <EOL> adaptation_set . make_live ( data ) <EOL> class AdaptationSet ( MpdElement ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , node ) : <EOL> MpdElement . __init__ ( self , node ) <EOL> self . segment_template = None <EOL> self . representations = [ ] <EOL> @ property <EOL> def content_type ( self ) : <EOL> "<STR_LIT>" <EOL> return self . attribs [ '<STR_LIT>' ] <EOL> @ property <EOL> def media_pattern ( self ) : <EOL> "<STR_LIT>" <EOL> return self . attribs [ '<STR_LIT>' ] <EOL> @ property <EOL> def initialization_pattern ( self ) : <EOL> "<STR_LIT>" <EOL> return self . attribs [ '<STR_LIT>' ] <EOL> @ property <EOL> def start_number ( self ) : <EOL> "<STR_LIT>" <EOL> return int ( self . attribs [ '<STR_LIT>' ] ) <EOL> @ property <EOL> def duration ( self ) : <EOL> "<STR_LIT>" <EOL> return int ( self . attribs [ '<STR_LIT>' ] ) <EOL> def parse ( self ) : <EOL> "<STR_LIT>" <EOL> self . check_and_add_attributes ( self . node , ( '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> for child in self . node . getchildren ( ) : <EOL> if self . compare_tag ( child . tag , '<STR_LIT>' ) : <EOL> self . check_and_add_attributes ( child , ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> elif self . compare_tag ( child . tag , '<STR_LIT>' ) : <EOL> rep = Representation ( self , child ) <EOL> rep . parse ( ) <EOL> self . representations . append ( rep ) <EOL> def make_live ( self , data ) : <EOL> for attr in ( '<STR_LIT>' ) : <EOL> self . set_value ( self . node , attr , data [ attr ] ) <EOL> for adaptation_set in self . adaptation_sets : <EOL> adaptation_set . make_live ( data ) <EOL> class Representation ( MpdElement ) : <EOL> "<STR_LIT>" <EOL> def __init__ ( self , adaptation_set , node ) : <EOL> MpdElement . __init__ ( self , node ) <EOL> self . adaptation_set = adaptation_set <EOL> @ property <EOL> def initialization_path ( self ) : <EOL> "<STR_LIT>" <EOL> return self . get_initialization_path ( ) <EOL> @ property <EOL> def rep_id ( self ) : <EOL> "<STR_LIT>" <EOL> return self . attribs [ '<STR_LIT:id>' ] <EOL> def parse ( self ) : <EOL> "<STR_LIT>" <EOL> self . check_and_add_attributes ( self . node , ( '<STR_LIT:id>' , '<STR_LIT>' ) ) <EOL> def get_initialization_path ( self ) : <EOL> "<STR_LIT>" <EOL> init_pattern = self . adaptation_set . initialization_pattern <EOL> rep_id = self . attribs [ '<STR_LIT:id>' ] <EOL> bandwidth = self . attribs [ '<STR_LIT>' ] <EOL> init_path = init_pattern . replace ( "<STR_LIT>" , rep_id ) . replace ( "<STR_LIT>" , bandwidth ) <EOL> return init_path <EOL> def get_media_path ( self , segNr = "<STR_LIT>" ) : <EOL> "<STR_LIT>" <EOL> media_pattern = self . adaptation_set . media_pattern <EOL> rep_id = self . attribs [ '<STR_LIT:id>' ] <EOL> bandwidth = self . attribs [ '<STR_LIT>' ] <EOL> media_path = media_pattern . replace ( "<STR_LIT>" , rep_id ) . replace ( "<STR_LIT>" , bandwidth ) <EOL> media_path = media_path . replace ( "<STR_LIT>" , str ( segNr ) ) <EOL> return media_path <EOL> class MpdProcessor ( MpdElement ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , infile ) : <EOL> self . tree = ElementTree . parse ( infile ) <EOL> self . mpd_namespace = None <EOL> self . root = self . tree . getroot ( ) <EOL> self . is_base_url_set = False <EOL> self . adaptation_sets = [ ] <EOL> self . media_presentation_duration = None <EOL> self . media_presentation_duration_in_s = None <EOL> self . muxed_rep = None <EOL> self . parse ( ) <EOL> def parse ( self ) : <EOL> "<STR_LIT>" <EOL> mpd = self . root <EOL> tag , self . mpd_namespace = self . tag_and_namespace ( mpd . tag ) <EOL> assert tag == "<STR_LIT>" <EOL> if mpd . attrib . has_key ( '<STR_LIT>' ) : <EOL> self . media_presentation_duration = mpd . attrib [ '<STR_LIT>' ] <EOL> self . media_presentation_duration_in_s = tfc . iso_duration_to_seconds ( self . media_presentation_duration ) <EOL> print "<STR_LIT>" % self . media_presentation_duration_in_s <EOL> for child in mpd : <EOL> if self . compare_tag ( child . tag , '<STR_LIT>' ) : <EOL> for grand_child in child : <EOL> if self . compare_tag ( grand_child . tag , '<STR_LIT>' ) : <EOL> AS = AdaptationSet ( grand_child ) <EOL> AS . parse ( ) <EOL> self . adaptation_sets . append ( AS ) <EOL> def get_adaptation_sets ( self ) : <EOL> return self . adaptation_sets <EOL> def getMuxedRep ( self ) : <EOL> return self . muxed_rep <EOL> def getMuxedInitPath ( self ) : <EOL> initPath = None <EOL> for AS in self . adaptation_sets : <EOL> if AS . contentType == "<STR_LIT>" : <EOL> print AS . initialization <EOL> initPath = AS . initialization . replace ( "<STR_LIT>" , self . muxed_rep ) <EOL> return initPath <EOL> def getMuxedMediaPath ( self ) : <EOL> mediaPath = None <EOL> for AS in self . adaptation_sets : <EOL> if AS . contentType == "<STR_LIT>" : <EOL> mediaPath = AS . media . replace ( "<STR_LIT>" , self . muxed_rep ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> return mediaPath <EOL> def process ( self , mpdData = { } ) : <EOL> MPD = self . root <EOL> self . processMPD ( MPD , mpdData ) <EOL> def makeLiveMpd ( self , data ) : <EOL> """<STR_LIT>""" <EOL> MPD = self . root <EOL> MPD . set ( '<STR_LIT:type>' , "<STR_LIT>" ) <EOL> for key in data . keys ( ) : <EOL> self . setValue ( MPD , key , data ) <EOL> if MPD . attrib . has_key ( '<STR_LIT>' ) : <EOL> del MPD . attrib [ '<STR_LIT>' ] <EOL> for child in MPD : <EOL> if self . compare_tag ( child . tag , '<STR_LIT>' ) : <EOL> child . set ( "<STR_LIT:start>" , "<STR_LIT>" ) <EOL> def makeLiveMultiplexedMpd ( self , data , mediaData ) : <EOL> self . makeLiveMpd ( data ) <EOL> MPD = self . root <EOL> audioAS = None <EOL> videoAS = None <EOL> period = None <EOL> audioRep = None <EOL> vidoeRep = None <EOL> for child in MPD : <EOL> if self . compare_tag ( child . tag , '<STR_LIT>' ) : <EOL> period = child <EOL> for grandChild in child : <EOL> if self . compare_tag ( grandChild . tag , '<STR_LIT>' ) : <EOL> AS = AdaptationSet ( grandChild ) <EOL> AS . parse ( ) <EOL> if AS . contentType == "<STR_LIT>" : <EOL> audioAS = grandChild <EOL> elif AS . contentType == "<STR_LIT>" : <EOL> videoAS = grandChild <EOL> for contentType , mData in mediaData . items ( ) : <EOL> trackID = mData [ '<STR_LIT>' ] <EOL> cc = self . makeContentComponent ( contentType , trackID ) <EOL> videoAS . insert ( <NUM_LIT:0> , cc ) <EOL> del videoAS . attrib [ '<STR_LIT>' ] <EOL> audioRep = audioAS . find ( self . mpd_namespace + "<STR_LIT>" ) <EOL> videoRep = videoAS . find ( self . mpd_namespace + "<STR_LIT>" ) <EOL> videoRep . set ( "<STR_LIT:id>" , self . muxed_rep ) <EOL> try : <EOL> audioCodec = audioRep . attrib [ "<STR_LIT>" ] <EOL> videoCodec = videoRep . attrib [ "<STR_LIT>" ] <EOL> combinedCodecs = "<STR_LIT>" % ( audioCodec , videoCodec ) <EOL> videoRep . set ( "<STR_LIT>" , combinedCodecs ) <EOL> except KeyError : <EOL> print "<STR_LIT>" <EOL> period . remove ( audioAS ) <EOL> def makeContentComponent ( self , contentType , trackID ) : <EOL> "<STR_LIT>" <EOL> elem = ElementTree . Element ( '<STR_LIT>' % self . mpd_namespace ) <EOL> elem . set ( "<STR_LIT:id>" , str ( trackID ) ) <EOL> elem . set ( "<STR_LIT>" , contentType ) <EOL> elem . tail = "<STR_LIT:\n>" <EOL> return elem <EOL> def getCleanString ( self , clean = True , targetMpdNameSpace = None ) : <EOL> "<STR_LIT>" <EOL> ofh = cStringIO . StringIO ( ) <EOL> self . tree . write ( ofh , encoding = "<STR_LIT:utf-8>" ) <EOL> value = ofh . getvalue ( ) <EOL> if clean : <EOL> value = value . replace ( "<STR_LIT>" , "<STR_LIT>" ) . replace ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> if targetMpdNameSpace is not None : <EOL> newStr = '<STR_LIT>' % targetMpdNameSpace <EOL> value = re . sub ( '<STR_LIT>' , newStr , value ) <EOL> xmlIntro = '<STR_LIT>' <EOL> return xmlIntro + value </s>
