예제 #1
0
파일: default.py 프로젝트: AWilco/iplayerv2
def watch(feed, pid, showDialog):

    times = []
    times.append(['start',time.clock()])
    if showDialog:
        pDialog = xbmcgui.DialogProgress()
        times.append(['xbmcgui.DialogProgress()',time.clock()])
        pDialog.create('IPlayer', 'Loading catchup stream info')
        times.append(['pDialog.create',time.clock()])

    subtitles_file = None
    item      = get_item(pid)
    times.append(['get_item',time.clock()])
    thumbnail = item.programme.thumbnail
    title     = item.programme.title
    summary   = item.programme.summary
    updated   = item.programme.updated
    channel   = None
    thumbfile = None
    if feed and feed.name:
        channel = feed.name
    times.append(['setup variables',time.clock()])        
    logging.info('watching channel=%s pid=%s' % (channel, pid))
    times.append(['logging',time.clock()])
    logging.info('thumb =%s   summary=%s' % (thumbnail, summary))
    times.append(['logging',time.clock()])   
    subtitles = get_setting_subtitles()
    times.append(['get_setting_subtitles',time.clock()])

    if thumbnail: 
        # attempt to use the existing thumbnail file
        thumbcache = xbmc.getCacheThumbName( sys.argv[ 0 ] + sys.argv[ 2 ] )
        thumbfile  = os.path.join( xbmc.translatePath( "special://profile" ), "Thumbnails", "Video", thumbcache[ 0 ], thumbcache )
        logging.info('Reusing existing thumbfile =%s for url %s%s' % (thumbfile, sys.argv[ 0 ], sys.argv[ 2 ]))
        
    if thumbnail and not os.path.isfile(thumbfile):
        # thumbnail wasn't available locally so download    
        try:
            # The thumbnail needs to accessed via the local filesystem
            # for "Media Info" to display it when playing a video
            if showDialog:
                pDialog.update(20, 'Fetching thumbnail')
                if pDialog.iscanceled(): raise 
                times.append(['update dialog',time.clock()])
            iplayer.httpretrieve(thumbnail, thumbfile)
            times.append(['retrieve thumbnail',time.clock()])
        except:
            pass

    if item.is_tv:
        # TV Stream
        iconimage = 'DefaultVideo.png'

        if showDialog:
            pDialog.update(50, 'Fetching video stream info')
            if pDialog.iscanceled(): raise
            times.append(['update dialog',time.clock()])
        pref = get_setting_videostream()        
        times.append(['get_setting_videostream',time.clock()])
        opref = pref
        if showDialog:
            pDialog.update(70, 'Selecting video stream')
            if pDialog.iscanceled(): raise
            times.append(['update dialog',time.clock()])

        streams = ['h264 3200', 'h264 1500', 'h264 800', 'h264 480', 'h264 400']
        (media, pref) = get_matching_stream(item, pref, streams)

        # A potentially usable stream was found (higher bitrate than the default) offer it to the user
        if not media:
            # Nothing usable was found
            d = xbmcgui.Dialog()
            d.ok('Stream Error', 'Can\'t locate any usable TV streams.')            
            return False

        if streams.index(opref) > streams.index(pref):
            d = xbmcgui.Dialog()
            if d.yesno('Default %s Stream Not Available' % opref, 'Play higher bitrate %s stream ?' % pref ) == False:
                return False
  
        times.append(['media 2',time.clock()])    
        url = media.url
        times.append(['media.url',time.clock()]) 
        logging.info('watching url=%s' % url)
        times.append(['logging',time.clock()])

        if showDialog:
            pDialog.update(90, 'Selecting subtitles')
            if pDialog.iscanceled(): raise
            times.append(['update dialog',time.clock()])
        if subtitles:
            subtitles_media = item.get_media_for('captions')
            times.append(['subtitles_media',time.clock()])
            if subtitles_media:
                subtitles_file = download_subtitles(subtitles_media.url)
                times.append(['subtitles download',time.clock()])

        listitem = xbmcgui.ListItem(title)
        times.append(['create listitem',time.clock()])
        #listitem.setIconImage(iconimage)
        listitem.setInfo('video', {
                                   "TVShowTitle": title,
                                   'Plot': summary + ' ' + updated,
                                   'PlotOutline': summary,
                                   "Date": updated,})
        times.append(['listitem setinfo',time.clock()])
        play=xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
        times.append(['xbmc.PlayList',time.clock()])
        
    else:
        # Radio stream        
        if showDialog:
            pDialog.update(30, 'Fetching radio stream info')
            if pDialog.iscanceled(): raise
            times.append(['update dialog',time.clock()])
        pref = get_setting_audiostream()
        if showDialog:
            pDialog.update(50, 'Selecting radio stream')
            if pDialog.iscanceled(): raise
            times.append(['update dialog',time.clock()])
 
        (media, pref) = get_matching_stream(item, pref, ['wma', 'mp3', 'aac'])

        if not media:            
            d = xbmcgui.Dialog()
            d.ok('Stream Error', 'Error: can\'t locate radio stream')            
            return False
        url = media.url
            
        logging.info('Listening to url=%s' % url)

        listitem = xbmcgui.ListItem(title)
        times.append(['listitem create',time.clock()])
        listitem.setIconImage('defaultAudio.png')
        times.append(['listitem.setIconImage',time.clock()])
        play=xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
        times.append(['xbmc.PlayList',time.clock()])

    logging.info('Playing preference %s' % pref)
    times.append(['logging.info',time.clock()])
    times.append(['listitem.setproperty x 3',time.clock()])

    if thumbfile: 
        listitem.setIconImage(thumbfile)
        times.append(['listitem.setIconImage(thumbfile)',time.clock()])
        listitem.setThumbnailImage(thumbfile)
        times.append(['listitem.setThumbnailImage(thumbfile)',time.clock()])
    
    del item
    del media
    
    if showDialog:
        pDialog.update(80, 'Playing')
        if pDialog.iscanceled(): raise
        times.append(['update dialog',time.clock()])
    play.clear()
    times.append(['play.clear()',time.clock()])
    play.add(url,listitem)
    times.append(['play.add',time.clock()])
    player = xbmc.Player(xbmc.PLAYER_CORE_AUTO)
    times.append(['xbmc.Player()',time.clock()])
    player.play(play)
    times.append(['player.play',time.clock()])
    # Auto play subtitles if they have downloaded 
    logging.info("subtitles: %s   - subtitles_file %s " % (subtitles,subtitles_file))
    times.append(['logging.info',time.clock()])
    if subtitles == 'autoplay' and subtitles_file: 
        player.setSubtitles(subtitles_file)
        times.append(['player.setSubtitles',time.clock()])
    
    if showDialog: pDialog.close()
    times.append(['pDialog.close()',time.clock()])
    
    if addoncompat.get_setting('enhanceddebug') == 'true':
        pt = times[0][1]
        for t in times:
            logging.info('Took %2.2f sec for %s' % (t[1] - pt, t[0]))
            pt = t[1]
def watch(feed, pid, showDialog, resume=False):

    times = []
    times.append(['start',time.clock()])
    if showDialog:
        pDialog = xbmcgui.DialogProgress()
        times.append(['xbmcgui.DialogProgress()',time.clock()])
        pDialog.create('IPlayer', 'Loading catchup stream info')
        times.append(['pDialog.create',time.clock()])

    subtitles_file = None
    item      = get_item(pid)
    times.append(['get_item',time.clock()])
    thumbnail = item.programme.thumbnail
    title     = item.programme.title
    summary   = item.programme.summary
    updated   = item.programme.updated
    channel   = None
    thumbfile = None
    if feed and feed.name:
        channel = feed.name
    times.append(['setup variables',time.clock()])
    logging.info('watching channel=%s pid=%s' % (channel, pid))
    times.append(['logging',time.clock()])
    logging.info('thumb =%s   summary=%s' % (thumbnail, summary))
    times.append(['logging',time.clock()])
    subtitles = get_setting_subtitles()
    times.append(['get_setting_subtitles',time.clock()])

    if thumbnail:
        if feed is not None and pid == feed.channel:
            # Listening to a live radio station, use the pre-downloaded file
            thumbfile = get_feed_thumbnail(feed)
        else:
            # attempt to use the existing thumbnail file
            thumbcache = xbmc.getCacheThumbName( sys.argv[ 0 ] + sys.argv[ 2 ] )
            thumbfile  = os.path.join( xbmc.translatePath( "special://profile" ), "Thumbnails", "Video", thumbcache[ 0 ], thumbcache )
            logging.info('Reusing existing thumbfile =%s for url %s%s' % (thumbfile, sys.argv[ 0 ], sys.argv[ 2 ]))

    if thumbnail and not os.path.isfile(thumbfile):
        # thumbnail wasn't available locally so download
        try:
            # The thumbnail needs to accessed via the local filesystem
            # for "Media Info" to display it when playing a video
            if showDialog:
                pDialog.update(20, 'Fetching thumbnail')
                if pDialog.iscanceled(): raise
                times.append(['update dialog',time.clock()])
            iplayer.httpretrieve(thumbnail, thumbfile)
            times.append(['retrieve thumbnail',time.clock()])
        except:
            pass

    if item.is_tv:
        # TV Stream
        iconimage = 'DefaultVideo.png'

        if showDialog:
            pDialog.update(50, 'Fetching video stream info')
            if pDialog.iscanceled(): raise
            times.append(['update dialog',time.clock()])
        pref = get_setting_videostream()
        times.append(['get_setting_videostream',time.clock()])
        opref = pref
        if showDialog:
            pDialog.update(70, 'Selecting video stream')
            if pDialog.iscanceled(): raise
            times.append(['update dialog',time.clock()])

        streams = ['h264 2800', 'h264 1520', 'h264 1500', 'h264 820', 'h264 800', 'h264 480', 'h264 400']
        (media, pref) = get_matching_stream(item, pref, streams)

        # A potentially usable stream was found (higher bitrate than the default) offer it to the user
        if not media:
            # Nothing usable was found
            d = xbmcgui.Dialog()
            d.ok('Stream Error', 'Can\'t locate any usable TV streams.')
            return False

        if streams.index(opref) > streams.index(pref):
            d = xbmcgui.Dialog()
            if d.yesno('Default %s Stream Not Available' % opref, 'Play higher bitrate %s stream ?' % pref ) == False:
                return False

        times.append(['media 2',time.clock()])
        url = media.url
        times.append(['media.url',time.clock()])
        logging.info('watching url=%s' % url)
        times.append(['logging',time.clock()])

        if showDialog:
            pDialog.update(90, 'Selecting subtitles')
            if pDialog.iscanceled(): raise
            times.append(['update dialog',time.clock()])
        if subtitles:
            subtitles_media = item.get_media_for('captions')
            times.append(['subtitles_media',time.clock()])
            if subtitles_media:
                subtitles_file = download_subtitles(subtitles_media.url)
                times.append(['subtitles download',time.clock()])

        listitem = xbmcgui.ListItem(title)
        times.append(['create listitem',time.clock()])
        #listitem.setIconImage(iconimage)
        if not item.live:
            listitem.setInfo('video', {
                                       "TVShowTitle": title,
                                       'Plot': summary + ' ' + updated,
                                       'PlotOutline': summary,})
        times.append(['listitem setinfo',time.clock()])
        play=xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
        times.append(['xbmc.PlayList',time.clock()])

    else:
        # Radio stream
        if showDialog:
            pDialog.update(30, 'Fetching radio stream info')
            if pDialog.iscanceled(): raise
            times.append(['update dialog',time.clock()])
        pref = get_setting_audiostream()
        if showDialog:
            pDialog.update(50, 'Selecting radio stream')
            if pDialog.iscanceled(): raise
            times.append(['update dialog',time.clock()])

        (media, pref) = get_matching_stream(item, pref, ['aac320', 'aac128', 'wma9', 'wma+asx', 'aac48', 'aac32'])

        if not media:
            d = xbmcgui.Dialog()
            d.ok('Stream Error', 'Error: can\'t locate radio stream')
            return False

        if media.application in ['wma9', 'wma+asx']:
            url = parse_asx(media.url)
        else:
            url = media.url

        logging.info('Listening to url=%s' % url)

        listitem = xbmcgui.ListItem(label=title)
        times.append(['listitem create',time.clock()])
        listitem.setIconImage('defaultAudio.png')
        times.append(['listitem.setIconImage',time.clock()])
        play=xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
        times.append(['xbmc.PlayList',time.clock()])

    logging.info('Playing preference %s' % pref)
    times.append(['logging.info',time.clock()])
    listitem.setInfo(type='Music', infoLabels = {'title': title})
    times.append(['listitem.setproperty x 3',time.clock()])

    if thumbfile:
        listitem.setIconImage(thumbfile)
        times.append(['listitem.setIconImage(thumbfile)',time.clock()])
        listitem.setThumbnailImage(thumbfile)
        times.append(['listitem.setThumbnailImage(thumbfile)',time.clock()])

    del media

    if showDialog:
        pDialog.update(80, 'Playing')
        if pDialog.iscanceled(): raise
        times.append(['update dialog',time.clock()])

    if url.startswith( 'rtmp://' ):
        core_player = xbmc.PLAYER_CORE_DVDPLAYER
    else:
        core_player = xbmc.PLAYER_CORE_AUTO

    try:
        player = iplayer.IPlayer(core_player, pid=pid, live=item.is_live)
    except iplayer.IPlayerLockException:
        exception_dialog = xbmcgui.Dialog()
        exception_dialog.ok("Stream Already Playing", "Unable to open stream", " - To continue, stop all other streams (try pressing 'x')[CR] - If you are sure there are no other streams [CR]playing, remove the resume lock (check addon settings -> advanced)")
        return

    times.append(['xbmc.Player()',time.clock()])
    player.resume_and_play(url, listitem, item.is_tv, resume)

    times.append(['player.play',time.clock()])
    # Auto play subtitles if they have downloaded
    logging.info("subtitles: %s   - subtitles_file %s " % (subtitles,subtitles_file))
    times.append(['logging.info',time.clock()])
    if subtitles == 'autoplay' and subtitles_file:
        player.setSubtitles(subtitles_file)
        times.append(['player.setSubtitles',time.clock()])

    if showDialog: pDialog.close()
    times.append(['pDialog.close()',time.clock()])

    if not item.is_tv:
        # Switch to a nice visualisation if playing a radio stream
        xbmc.executebuiltin('ActivateWindow(Visualisation)')

    del item

    if __addon__.getSetting('enhanceddebug') == 'true':
        pt = times[0][1]
        for t in times:
            logging.info('Took %2.2f sec for %s' % (t[1] - pt, t[0]))
            pt = t[1]

    if os.environ.get( "OS" ) != "xbox":
        while player.isPlaying() and not xbmc.abortRequested:
            xbmc.sleep(500)

        xbmc.log("Exiting playback loop... (isPlaying %s, abortRequested %s)" % (player.isPlaying(), xbmc.abortRequested), level=xbmc.LOGDEBUG)
        player.cancelled.set()
예제 #3
0
def watch(feed, pid, showDialog):

    times = []
    times.append(['start', time.clock()])
    if showDialog:
        pDialog = xbmcgui.DialogProgress()
        times.append(['xbmcgui.DialogProgress()', time.clock()])
        pDialog.create('IPlayer', 'Loading catchup stream info')
        times.append(['pDialog.create', time.clock()])

    subtitles_file = None
    item = get_item(pid)
    times.append(['get_item', time.clock()])
    thumbnail = item.programme.thumbnail
    title = item.programme.title
    summary = item.programme.summary
    updated = item.programme.updated
    channel = None
    thumbfile = None
    if feed and feed.name:
        channel = feed.name
    times.append(['setup variables', time.clock()])
    logging.info('watching channel=%s pid=%s' % (channel, pid))
    times.append(['logging', time.clock()])
    logging.info('thumb =%s   summary=%s' % (thumbnail, summary))
    times.append(['logging', time.clock()])
    subtitles = get_setting_subtitles()
    times.append(['get_setting_subtitles', time.clock()])

    if thumbnail:
        # attempt to use the existing thumbnail file
        thumbcache = xbmc.getCacheThumbName(sys.argv[0] + sys.argv[2])
        thumbfile = os.path.join(xbmc.translatePath("special://profile"),
                                 "Thumbnails", "Video", thumbcache[0],
                                 thumbcache)
        logging.info('Reusing existing thumbfile =%s for url %s%s' %
                     (thumbfile, sys.argv[0], sys.argv[2]))

    if thumbnail and not os.path.isfile(thumbfile):
        # thumbnail wasn't available locally so download
        try:
            # The thumbnail needs to accessed via the local filesystem
            # for "Media Info" to display it when playing a video
            if showDialog:
                pDialog.update(20, 'Fetching thumbnail')
                if pDialog.iscanceled(): raise
                times.append(['update dialog', time.clock()])
            iplayer.httpretrieve(thumbnail, thumbfile)
            times.append(['retrieve thumbnail', time.clock()])
        except:
            pass

    if item.is_tv:
        # TV Stream
        iconimage = 'DefaultVideo.png'

        if showDialog:
            pDialog.update(50, 'Fetching video stream info')
            if pDialog.iscanceled(): raise
            times.append(['update dialog', time.clock()])
        pref = get_setting_videostream()
        times.append(['get_setting_videostream', time.clock()])
        opref = pref
        if showDialog:
            pDialog.update(70, 'Selecting video stream')
            if pDialog.iscanceled(): raise
            times.append(['update dialog', time.clock()])

        streams = [
            'h264 3200', 'h264 1500', 'h264 800', 'h264 480', 'h264 400'
        ]
        (media, pref) = get_matching_stream(item, pref, streams)

        # A potentially usable stream was found (higher bitrate than the default) offer it to the user
        if not media:
            # Nothing usable was found
            d = xbmcgui.Dialog()
            d.ok('Stream Error', 'Can\'t locate any usable TV streams.')
            return False

        if streams.index(opref) > streams.index(pref):
            d = xbmcgui.Dialog()
            if d.yesno('Default %s Stream Not Available' % opref,
                       'Play higher bitrate %s stream ?' % pref) == False:
                return False

        times.append(['media 2', time.clock()])
        url = media.url
        times.append(['media.url', time.clock()])
        logging.info('watching url=%s' % url)
        times.append(['logging', time.clock()])

        if showDialog:
            pDialog.update(90, 'Selecting subtitles')
            if pDialog.iscanceled(): raise
            times.append(['update dialog', time.clock()])
        if subtitles:
            subtitles_media = item.get_media_for('captions')
            times.append(['subtitles_media', time.clock()])
            if subtitles_media:
                subtitles_file = download_subtitles(subtitles_media.url)
                times.append(['subtitles download', time.clock()])

        listitem = xbmcgui.ListItem(title)
        times.append(['create listitem', time.clock()])
        #listitem.setIconImage(iconimage)
        listitem.setInfo(
            'video', {
                "TVShowTitle": title,
                'Plot': summary + ' ' + updated,
                'PlotOutline': summary,
                "Date": updated,
            })
        times.append(['listitem setinfo', time.clock()])
        play = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
        times.append(['xbmc.PlayList', time.clock()])

    else:
        # Radio stream
        if showDialog:
            pDialog.update(30, 'Fetching radio stream info')
            if pDialog.iscanceled(): raise
            times.append(['update dialog', time.clock()])
        pref = get_setting_audiostream()
        if showDialog:
            pDialog.update(50, 'Selecting radio stream')
            if pDialog.iscanceled(): raise
            times.append(['update dialog', time.clock()])

        (media, pref) = get_matching_stream(item, pref, ['wma', 'mp3', 'aac'])

        if not media:
            d = xbmcgui.Dialog()
            d.ok('Stream Error', 'Error: can\'t locate radio stream')
            return False
        url = media.url

        logging.info('Listening to url=%s' % url)

        listitem = xbmcgui.ListItem(title)
        times.append(['listitem create', time.clock()])
        listitem.setIconImage('defaultAudio.png')
        times.append(['listitem.setIconImage', time.clock()])
        play = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
        times.append(['xbmc.PlayList', time.clock()])

    logging.info('Playing preference %s' % pref)
    times.append(['logging.info', time.clock()])
    times.append(['listitem.setproperty x 3', time.clock()])

    if thumbfile:
        listitem.setIconImage(thumbfile)
        times.append(['listitem.setIconImage(thumbfile)', time.clock()])
        listitem.setThumbnailImage(thumbfile)
        times.append(['listitem.setThumbnailImage(thumbfile)', time.clock()])

    del item
    del media

    if showDialog:
        pDialog.update(80, 'Playing')
        if pDialog.iscanceled(): raise
        times.append(['update dialog', time.clock()])
    play.clear()
    times.append(['play.clear()', time.clock()])
    play.add(url, listitem)
    times.append(['play.add', time.clock()])
    player = xbmc.Player(xbmc.PLAYER_CORE_AUTO)
    times.append(['xbmc.Player()', time.clock()])
    player.play(play)
    times.append(['player.play', time.clock()])
    # Auto play subtitles if they have downloaded
    logging.info("subtitles: %s   - subtitles_file %s " %
                 (subtitles, subtitles_file))
    times.append(['logging.info', time.clock()])
    if subtitles == 'autoplay' and subtitles_file:
        player.setSubtitles(subtitles_file)
        times.append(['player.setSubtitles', time.clock()])

    if showDialog: pDialog.close()
    times.append(['pDialog.close()', time.clock()])

    if __addon__.getSetting('enhanceddebug') == 'true':
        pt = times[0][1]
        for t in times:
            logging.info('Took %2.2f sec for %s' % (t[1] - pt, t[0]))
            pt = t[1]
def watch(feed, pid):

    subtitles_file = None
    item      = iplayer.programme(pid).programme
    thumbnail = item.programme.thumbnail
    title     = item.programme.title
    summary   = item.programme.summary
    date      = item.programme.date
    channel   = None
    thumbfile = None
    if feed and feed.name:
        channel = feed.name
    utils.log('watching channel=%s pid=%s' % (channel, pid),xbmc.LOGINFO)
    utils.log('thumb =%s   summary=%s' % (thumbnail, summary),xbmc.LOGINFO)
    subtitles = get_setting_subtitles()

    if thumbnail:
        if feed is not None and pid == feed.channel:
            # Listening to a live radio station, use the pre-downloaded file
            thumbfile = get_feed_thumbnail(feed)
        else:
            # attempt to use the existing thumbnail file
            thumbcache = xbmc.getCacheThumbName( sys.argv[ 0 ] + sys.argv[ 2 ] )
            if utils.get_os() == "xbox":
                thumbfile = os.path.join( xbmc.translatePath( "special://profile" ), "Thumbnails", "Video", thumbcache[ 0 ], thumbcache )
            else:
                thumbfile = os.path.join( xbmc.translatePath( "special://profile" ), "Thumbnails", thumbcache[ 0 ], thumbcache )
            utils.log('Reusing existing thumbfile=%s for url %s%s' % (thumbfile, sys.argv[ 0 ], sys.argv[ 2 ]),xbmc.LOGINFO)

    if thumbnail and not os.path.isfile(thumbfile):
        # thumbnail wasn't available locally so download
        try:
            # The thumbnail needs to accessed via the local filesystem
            # for "Media Info" to display it when playing a video
            iplayer.httpretrieve(thumbnail, thumbfile)
        except:
            pass

    if item.is_tv and item.is_live:
        (media_list, above_limit) = item.get_available_streams_live()
    else:
        (media_list, above_limit) = item.get_available_streams()

    if len(media_list) == 0:
        # Nothing usable was found
        d = xbmcgui.Dialog()
        d.ok('Stream Error', 'Can\'t locate any usable streams.')
        return False

    for media in media_list:
        player = None
        listitem = xbmcgui.ListItem(label=title)
        if item.is_tv:
            # TV Stream
            iconimage = 'DefaultVideo.png'

            if above_limit:
                d = xbmcgui.Dialog()
                if d.yesno('Default Stream Not Available', 'Play higher bitrate stream?') == False:
                    return False

            url = media.url
            utils.log('watching url=%s' % url,xbmc.LOGINFO)

            if subtitles:
                subtitles_media = item.get_media_list_for('captions', None)
                if subtitles_media and len(subtitles_media) > 0:
                    subtitles_file = download_subtitles(subtitles_media[0].url)

            if not item.live:
                listitem.setInfo('video', {
                                           "TVShowTitle": title,
                                           'Plot': summary + ' ' + date,
                                           'PlotOutline': summary,})
            play=xbmc.PlayList(xbmc.PLAYLIST_VIDEO)

        else:

            if not media:
                d = xbmcgui.Dialog()
                d.ok('Stream Error', 'Error: can\'t locate radio stream')
                return False

            if media.application in ['wma9', 'wma+asx']:
                url = parse_asx(media.url)
            else:
                url = media.url

            utils.log('Listening to url=%s' % url,xbmc.LOGINFO)
            listitem.setInfo(type='Music', infoLabels = {'title': title})
            listitem.setIconImage('defaultAudio.png')

        utils.log('Playing preference %s %s' % (media.connection_kind, media.application),xbmc.LOGINFO)

        if thumbfile:
            listitem.setIconImage(thumbfile)
            listitem.setThumbnailImage(thumbfile)

        listitem.setPath(url)
        xbmcplugin.setResolvedUrl(__plugin_handle__, succeeded = True, listitem = listitem)

        xbmc.sleep(500)
        # Successfully started playing something?
        if xbmc.Player().isPlaying():
            break;

    # Auto play subtitles if they have downloaded
    utils.log("subtitles: %s   - subtitles_file %s " % (subtitles,subtitles_file),xbmc.LOGINFO)
    if subtitles == 'autoplay' and subtitles_file:
        xbmc.Player().setSubtitles(subtitles_file)

    if not item.is_tv:
        # Switch to a nice visualisation if playing a radio stream
        xbmc.executebuiltin('ActivateWindow(Visualisation)')

    del item
def watch(feed, pid, showDialog, resume=False):

    times = []
    times.append(['start', time.clock()])
    if showDialog:
        pDialog = xbmcgui.DialogProgress()
        times.append(['xbmcgui.DialogProgress()', time.clock()])
        pDialog.create('IPlayer', 'Loading catchup stream info')
        times.append(['pDialog.create', time.clock()])

    subtitles_file = None
    item = get_item(pid)
    times.append(['get_item', time.clock()])
    thumbnail = item.programme.thumbnail
    title = item.programme.title
    summary = item.programme.summary
    updated = item.programme.updated
    channel = None
    thumbfile = None
    if feed and feed.name:
        channel = feed.name
    times.append(['setup variables', time.clock()])
    logging.info('watching channel=%s pid=%s' % (channel, pid))
    times.append(['logging', time.clock()])
    logging.info('thumb =%s   summary=%s' % (thumbnail, summary))
    times.append(['logging', time.clock()])
    subtitles = get_setting_subtitles()
    times.append(['get_setting_subtitles', time.clock()])

    if thumbnail:
        if feed is not None and pid == feed.channel:
            # Listening to a live radio station, use the pre-downloaded file
            thumbfile = get_feed_thumbnail(feed)
        else:
            # attempt to use the existing thumbnail file
            thumbcache = xbmc.getCacheThumbName(sys.argv[0] + sys.argv[2])
            thumbfile = os.path.join(xbmc.translatePath("special://profile"),
                                     "Thumbnails", "Video", thumbcache[0],
                                     thumbcache)
            logging.info('Reusing existing thumbfile =%s for url %s%s' %
                         (thumbfile, sys.argv[0], sys.argv[2]))

    if thumbnail and not os.path.isfile(thumbfile):
        # thumbnail wasn't available locally so download
        try:
            # The thumbnail needs to accessed via the local filesystem
            # for "Media Info" to display it when playing a video
            if showDialog:
                pDialog.update(20, 'Fetching thumbnail')
                if pDialog.iscanceled(): raise
                times.append(['update dialog', time.clock()])
            iplayer.httpretrieve(thumbnail, thumbfile)
            times.append(['retrieve thumbnail', time.clock()])
        except:
            pass

    if item.is_tv:
        # TV Stream
        iconimage = 'DefaultVideo.png'

        if showDialog:
            pDialog.update(50, 'Fetching video stream info')
            if pDialog.iscanceled(): raise
            times.append(['update dialog', time.clock()])
        pref = get_setting_videostream()
        times.append(['get_setting_videostream', time.clock()])
        opref = pref
        if showDialog:
            pDialog.update(70, 'Selecting video stream')
            if pDialog.iscanceled(): raise
            times.append(['update dialog', time.clock()])

        streams = [
            'h264 2800', 'h264 1520', 'h264 1500', 'h264 820', 'h264 800',
            'h264 480', 'h264 400'
        ]
        (media, pref) = get_matching_stream(item, pref, streams)

        # A potentially usable stream was found (higher bitrate than the default) offer it to the user
        if not media:
            # Nothing usable was found
            d = xbmcgui.Dialog()
            d.ok('Stream Error', 'Can\'t locate any usable TV streams.')
            return False

        if streams.index(opref) > streams.index(pref):
            d = xbmcgui.Dialog()
            if d.yesno('Default %s Stream Not Available' % opref,
                       'Play higher bitrate %s stream ?' % pref) == False:
                return False

        times.append(['media 2', time.clock()])
        url = media.url
        times.append(['media.url', time.clock()])
        logging.info('watching url=%s' % url)
        times.append(['logging', time.clock()])

        if showDialog:
            pDialog.update(90, 'Selecting subtitles')
            if pDialog.iscanceled(): raise
            times.append(['update dialog', time.clock()])
        if subtitles:
            subtitles_media = item.get_media_for('captions')
            times.append(['subtitles_media', time.clock()])
            if subtitles_media:
                subtitles_file = download_subtitles(subtitles_media.url)
                times.append(['subtitles download', time.clock()])

        listitem = xbmcgui.ListItem(title)
        times.append(['create listitem', time.clock()])
        #listitem.setIconImage(iconimage)
        if not item.live:
            listitem.setInfo(
                'video', {
                    "TVShowTitle": title,
                    'Plot': summary + ' ' + updated,
                    'PlotOutline': summary,
                })
        times.append(['listitem setinfo', time.clock()])
        play = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)
        times.append(['xbmc.PlayList', time.clock()])

    else:
        # Radio stream
        if showDialog:
            pDialog.update(30, 'Fetching radio stream info')
            if pDialog.iscanceled(): raise
            times.append(['update dialog', time.clock()])
        pref = get_setting_audiostream()
        if showDialog:
            pDialog.update(50, 'Selecting radio stream')
            if pDialog.iscanceled(): raise
            times.append(['update dialog', time.clock()])

        (media, pref) = get_matching_stream(
            item, pref,
            ['aac320', 'aac128', 'wma9', 'wma+asx', 'aac48', 'aac32'])

        if not media:
            d = xbmcgui.Dialog()
            d.ok('Stream Error', 'Error: can\'t locate radio stream')
            return False

        if media.application in ['wma9', 'wma+asx']:
            url = parse_asx(media.url)
        else:
            url = media.url

        logging.info('Listening to url=%s' % url)

        listitem = xbmcgui.ListItem(label=title)
        times.append(['listitem create', time.clock()])
        listitem.setIconImage('defaultAudio.png')
        times.append(['listitem.setIconImage', time.clock()])
        play = xbmc.PlayList(xbmc.PLAYLIST_MUSIC)
        times.append(['xbmc.PlayList', time.clock()])

    logging.info('Playing preference %s' % pref)
    times.append(['logging.info', time.clock()])
    listitem.setInfo(type='Music', infoLabels={'title': title})
    times.append(['listitem.setproperty x 3', time.clock()])

    if thumbfile:
        listitem.setIconImage(thumbfile)
        times.append(['listitem.setIconImage(thumbfile)', time.clock()])
        listitem.setThumbnailImage(thumbfile)
        times.append(['listitem.setThumbnailImage(thumbfile)', time.clock()])

    del media

    if showDialog:
        pDialog.update(80, 'Playing')
        if pDialog.iscanceled(): raise
        times.append(['update dialog', time.clock()])

    if url.startswith('rtmp://'):
        core_player = xbmc.PLAYER_CORE_DVDPLAYER
    else:
        core_player = xbmc.PLAYER_CORE_AUTO

    try:
        player = iplayer.IPlayer(core_player, pid=pid, live=item.is_live)
    except iplayer.IPlayerLockException:
        exception_dialog = xbmcgui.Dialog()
        exception_dialog.ok(
            "Stream Already Playing", "Unable to open stream",
            " - To continue, stop all other streams (try pressing 'x')[CR] - If you are sure there are no other streams [CR]playing, remove the resume lock (check addon settings -> advanced)"
        )
        return

    times.append(['xbmc.Player()', time.clock()])
    player.resume_and_play(url, listitem, item.is_tv, resume)

    times.append(['player.play', time.clock()])
    # Auto play subtitles if they have downloaded
    logging.info("subtitles: %s   - subtitles_file %s " %
                 (subtitles, subtitles_file))
    times.append(['logging.info', time.clock()])
    if subtitles == 'autoplay' and subtitles_file:
        player.setSubtitles(subtitles_file)
        times.append(['player.setSubtitles', time.clock()])

    if showDialog: pDialog.close()
    times.append(['pDialog.close()', time.clock()])

    if not item.is_tv:
        # Switch to a nice visualisation if playing a radio stream
        xbmc.executebuiltin('ActivateWindow(Visualisation)')

    del item

    if __addon__.getSetting('enhanceddebug') == 'true':
        pt = times[0][1]
        for t in times:
            logging.info('Took %2.2f sec for %s' % (t[1] - pt, t[0]))
            pt = t[1]

    if os.environ.get("OS") != "xbox":
        while player.isPlaying() and not xbmc.abortRequested:
            xbmc.sleep(500)

        xbmc.log("Exiting playback loop... (isPlaying %s, abortRequested %s)" %
                 (player.isPlaying(), xbmc.abortRequested),
                 level=xbmc.LOGDEBUG)
        player.cancelled.set()
예제 #6
0
def watch(feed, pid):

    subtitles_file = None
    item = iplayer.programme(pid).programme
    thumbnail = item.programme.thumbnail
    title = item.programme.title
    summary = item.programme.summary
    date = item.programme.date
    channel = None
    thumbfile = None
    if feed and feed.name:
        channel = feed.name
    utils.log('watching channel=%s pid=%s' % (channel, pid), xbmc.LOGINFO)
    utils.log('thumb =%s   summary=%s' % (thumbnail, summary), xbmc.LOGINFO)
    subtitles = get_setting_subtitles()

    if thumbnail:
        if feed is not None and pid == feed.channel:
            # Listening to a live radio station, use the pre-downloaded file
            thumbfile = get_feed_thumbnail(feed)
        else:
            # attempt to use the existing thumbnail file
            thumbcache = xbmc.getCacheThumbName(sys.argv[0] + sys.argv[2])
            if utils.get_os() == "xbox":
                thumbfile = os.path.join(
                    xbmc.translatePath("special://profile"), "Thumbnails",
                    "Video", thumbcache[0], thumbcache)
            else:
                thumbfile = os.path.join(
                    xbmc.translatePath("special://profile"), "Thumbnails",
                    thumbcache[0], thumbcache)
            utils.log(
                'Reusing existing thumbfile=%s for url %s%s' %
                (thumbfile, sys.argv[0], sys.argv[2]), xbmc.LOGINFO)

    if thumbnail and not os.path.isfile(thumbfile):
        # thumbnail wasn't available locally so download
        try:
            # The thumbnail needs to accessed via the local filesystem
            # for "Media Info" to display it when playing a video
            iplayer.httpretrieve(thumbnail, thumbfile)
        except:
            pass

    if item.is_tv and item.is_live:
        (media_list, above_limit) = item.get_available_streams_live()
    else:
        (media_list, above_limit) = item.get_available_streams()

    if len(media_list) == 0:
        # Nothing usable was found
        d = xbmcgui.Dialog()
        d.ok('Stream Error', 'Can\'t locate any usable streams.')
        return False

    for media in media_list:
        player = None
        listitem = xbmcgui.ListItem(label=title)
        if item.is_tv:
            # TV Stream
            iconimage = 'DefaultVideo.png'

            if above_limit:
                d = xbmcgui.Dialog()
                if d.yesno('Default Stream Not Available',
                           'Play higher bitrate stream?') == False:
                    return False

            url = media.url
            utils.log('watching url=%s' % url, xbmc.LOGINFO)

            if subtitles:
                subtitles_media = item.get_media_list_for('captions', None)
                if subtitles_media and len(subtitles_media) > 0:
                    subtitles_file = download_subtitles(subtitles_media[0].url)

            if not item.live:
                listitem.setInfo(
                    'video', {
                        "TVShowTitle": title,
                        'Plot': summary + ' ' + date,
                        'PlotOutline': summary,
                    })
            play = xbmc.PlayList(xbmc.PLAYLIST_VIDEO)

        else:

            if not media:
                d = xbmcgui.Dialog()
                d.ok('Stream Error', 'Error: can\'t locate radio stream')
                return False

            if media.application in ['wma9', 'wma+asx']:
                url = parse_asx(media.url)
            else:
                url = media.url

            utils.log('Listening to url=%s' % url, xbmc.LOGINFO)
            listitem.setInfo(type='Music', infoLabels={'title': title})
            listitem.setIconImage('defaultAudio.png')

        utils.log(
            'Playing preference %s %s' %
            (media.connection_kind, media.application), xbmc.LOGINFO)

        if thumbfile:
            listitem.setIconImage(thumbfile)
            listitem.setThumbnailImage(thumbfile)

        listitem.setPath(url)
        xbmcplugin.setResolvedUrl(__plugin_handle__,
                                  succeeded=True,
                                  listitem=listitem)

        xbmc.sleep(500)
        # Successfully started playing something?
        if xbmc.Player().isPlaying():
            break

    # Auto play subtitles if they have downloaded
    utils.log(
        "subtitles: %s   - subtitles_file %s " % (subtitles, subtitles_file),
        xbmc.LOGINFO)
    if subtitles == 'autoplay' and subtitles_file:
        xbmc.Player().setSubtitles(subtitles_file)

    if not item.is_tv:
        # Switch to a nice visualisation if playing a radio stream
        xbmc.executebuiltin('ActivateWindow(Visualisation)')

    del item