Esempio n. 1
0
    def answer_request(self, sendData):
        global g_stopEvent
        global g_downloader
        global g_currentprocessor


        try:

            #Pull apart request path
            request_path=self.path[1:] 
            querystring=request_path            
            request_path=re.sub(r"\?.*","",request_path)
            
            #If a request to stop is sent, shut down the proxy

            if request_path.lower()=="stop":# all special web interfaces here
                sys.exit()
                return
            if request_path.lower()=="favicon.ico":
                print 'dont have no icone here, may be in future'
                self.wfile.close()
                return
            if request_path.lower()=="sendvideopart":
                print 'dont have no icone here, may be in future'
                #sendvideoparthere
                self.send_response(200)
                
                rtype="video/mp2t"  #default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                self.end_headers()
                initDone=True
                videourl=self.decode_videoparturl(querystring.split('?')[1])
                g_currentprocessor.sendVideoPart(videourl,self.wfile)
                #self.wfile.close()
                return
            initDone=False
            (url,proxy,use_proxy_for_chunks,maxbitrate,simpledownloader, auth,streamtype,swf ,callbackpath, callbackparam)=self.decode_url(request_path)
            print 'simpledownloaderxxxxxxxxxxxxxxx',simpledownloader
            if streamtype=='' or streamtype==None or streamtype=='none': streamtype='HDS'
            
            if streamtype=='HDS':

                print 'Url received at proxy',url,proxy,use_proxy_for_chunks,maxbitrate


                #Send file request
                #self.handle_send_request(download_id,file_url, file_name, requested_range,download_mode ,keep_file,connections)
                
                downloader=None
                #downloader=g_downloader
                
                if not downloader or downloader.live==True or  not (downloader.init_done and downloader.init_url ==url):
                    from f4mDownloader import F4MDownloader
                    downloader=F4MDownloader()
                    if not downloader.init(self.wfile,url,proxy,use_proxy_for_chunks,g_stopEvent,maxbitrate,auth,swf):
                        print 'cannot init'
                        raise Exception('HDS.url failed to play\nServer down? check Url.')
                    g_downloader=downloader
                    print 'init...' 
                
                enableSeek=False
                requested_range=self.headers.getheader("Range")
                if requested_range==None: requested_range=""
                srange, erange=(None,None)
                
                
                            
                if downloader.live==False and len(requested_range)>0 and not requested_range=="bytes=0-0": #we have to stream?
                    enableSeek=True
                    (srange, erange) = self.get_range_request(requested_range, downloader.total_frags)
                

                print 'PROXY DATA',downloader.live,enableSeek,requested_range,downloader.total_frags,srange, erange
                enableSeek=False ##disabled for time being, couldn't find better way to handle
                
                framgementToSend=0
                inflate=1815002#(6526684-466/3)#*373/downloader.total_frags# 4142*1024*243/8/40 #1#1024*1024
                if enableSeek:
                    #rtype="video/x-flv" #just as default
                    self.send_response(206)
                    rtype="flv-application/octet-stream"  #default type could have gone to the server to get it.
                    self.send_header("Content-Type", rtype)
                    self.send_header("Accept-Ranges","bytes")
                    print 'not LIVE,enable seek',downloader.total_frags
                    
                    totalsize=downloader.total_frags*inflate
                    
                    framgementToSend=1#downloader.total_frags
                    erange=srange+framgementToSend*inflate
                    if erange>=totalsize:
                        erange=totalsize-1
                    
    #                crange="bytes "+str(srange)+"-" +str(int(downloader.total_frags-1))+"/"+str(downloader.total_frags)#recalculate crange based on srange, portionLen and content_size 
    #                crange="bytes "+str(srange)+"-" +str(int(totalsize-1))+"/"+str(totalsize)#recalculate crange based on srange, portionLen and content_size 
                    crange="bytes "+str(srange)+"-" +str(int(erange))+"/*"#+str(totalsize)#recalculate crange based on srange, portionLen and content_size 
                    print srange/inflate,erange/inflate,totalsize/inflate
                    self.send_header("Content-Length", str(totalsize))
                    self.send_header("Content-Range",crange)
                    etag=self.generate_ETag(url)
                    self.send_header("ETag",etag)
                    print crange
                    self.send_header("Last-Modified","Wed, 21 Feb 2000 08:43:39 GMT")
                    self.send_header("Cache-Control","public, must-revalidate")
                    self.send_header("Cache-Control","no-cache")
                    self.send_header("Pragma","no-cache")
                    self.send_header("features","seekable,stridable")
                    self.send_header("client-id","12345")
                    self.send_header("Connection", 'close')
                else:
                    self.send_response(200)
                    rtype="flv-application/octet-stream"  #default type could have gone to the server to get it.
                    self.send_header("Content-Type", rtype)
                    srange=None
                    
            elif streamtype=='SIMPLE' or simpledownloader :
                from interalSimpleDownloader import interalSimpleDownloader
                downloader=interalSimpleDownloader();
                if not downloader.init(self.wfile,url,proxy,g_stopEvent,maxbitrate):
                    print 'init throw error because init'#throw error because init
                    raise Exception('SIMPLE.url failed to play\nServer down? check Url.')
                srange,framgementToSend=(None,None)
                self.send_response(200)
                rtype="flv-application/octet-stream"  #default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                srange=None
            elif streamtype=='TSDOWNLOADER':
                from TSDownloader import TSDownloader
                downloader=TSDownloader();
                if not downloader.init(self.wfile,url,proxy,g_stopEvent,maxbitrate):
                    print 'cannot init but will continue to play'
                    raise Exception('TS.url failed to play\nServer down? check Url.')
                    #return
                srange,framgementToSend=(None,None)
                self.send_response(200)
                rtype="video/mp2t"  #default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                srange=None
            elif streamtype=='HLS':
                from hlsDownloader import HLSDownloader
                downloader=HLSDownloader()
                if not downloader.init(self.wfile,url,proxy,use_proxy_for_chunks,g_stopEvent,maxbitrate,auth):
                    print 'cannot init'
                    raise Exception('HLS.url failed to play\nServer down? check Url.')
                    
                srange,framgementToSend=(None,None)
                self.send_response(200)
                rtype="flv-application/octet-stream"  #default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                srange=None
            elif streamtype=='HLSRETRY':
                from HLSDownloaderRetry import HLSDownloaderRetry
                downloader=HLSDownloaderRetry()
                if not downloader.init(self.wfile,url,proxy,use_proxy_for_chunks,g_stopEvent,maxbitrate,auth , callbackpath, callbackparam):
                    print 'cannot init'
                    raise Exception('HLSR.url failed to play\nServer down? check Url.')
                    
                srange,framgementToSend=(None,None)
                self.send_response(200)
                rtype="flv-application/octet-stream"  #default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                srange=None            
            elif streamtype=='HLSREDIR':
                from HLSRedirector import HLSRedirector
                downloader=HLSRedirector()
                g_currentprocessor=downloader
                if not downloader.init(self.wfile,url,proxy,use_proxy_for_chunks,g_stopEvent,maxbitrate,auth , callbackpath, callbackparam):
                    print 'cannot init'
                    raise Exception('HLSR.url failed to play\nServer down? check Url.')
                    
                srange,framgementToSend=(None,None)
                self.send_response(200)
                rtype="application/vnd.apple.mpegurl"  #default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)

               
                srange=None            
                
            #rtype="flv-application/octet-stream"  #default type could have gone to the server to get it. 
            #self.send_header("Content-Type", rtype)    
               
            self.end_headers()
            if not srange==None:
                srange=srange/inflate
            initDone=True
            if sendData:
                downloader.keep_sending_video(self.wfile,srange,framgementToSend)
                #self.wfile.close()
                #runningthread=thread.start_new_thread(downloader.download,(self.wfile,url,proxy,use_proxy_for_chunks,))
                print 'srange,framgementToSend',srange,framgementToSend
                #runningthread=thread.start_new_thread(downloader.keep_sending_video,(self.wfile,srange,framgementToSend,))
                
                #xbmc.sleep(500)
                #while not downloader.status=="finished":
                #    xbmc.sleep(200);


        except Exception as inst:
            #Print out a stack trace
            traceback.print_exc()
            #g_stopEvent.set()
            if not initDone:
                xbmc.executebuiltin("XBMC.Notification(F4mProxy,%s,4000,'')"%inst.message)
                self.send_error(404)
            #print 'sending 404'
            
            #self.send_error(404)
            
            #Close output stream file
            #self.wfile.close()
            print 'closed'
            

        #Close output stream file
        #self.wfile.close()
        self.finish()
        return 
Esempio n. 2
0
    def answer_request(self, sendData):
        global g_stopEvent
        global g_downloader

        try:

            #Pull apart request path
            request_path = self.path[1:]
            request_path = re.sub(r"\?.*", "", request_path)
            #If a request to stop is sent, shut down the proxy

            if request_path.lower(
            ) == "stop":  # all special web interfaces here
                sys.exit()
                return
            if request_path.lower() == "favicon.ico":
                print 'dont have no icone here, may be in future'
                self.wfile.close()
                return

            (url, proxy, use_proxy_for_chunks, maxbitrate, simpledownloader,
             auth, streamtype, swf, callbackpath,
             callbackparam) = self.decode_url(request_path)
            print 'simpledownloaderxxxxxxxxxxxxxxx', simpledownloader
            if streamtype == '' or streamtype == None or streamtype == 'none':
                streamtype = 'HDS'

            if streamtype == 'HDS':

                print 'Url received at proxy', url, proxy, use_proxy_for_chunks, maxbitrate

                #Send file request
                #self.handle_send_request(download_id,file_url, file_name, requested_range,download_mode ,keep_file,connections)

                downloader = None
                #downloader=g_downloader

                if not downloader or downloader.live == True or not (
                        downloader.init_done and downloader.init_url == url):
                    from f4mDownloader import F4MDownloader
                    downloader = F4MDownloader()
                    if not downloader.init(self.wfile, url, proxy,
                                           use_proxy_for_chunks, g_stopEvent,
                                           maxbitrate, auth, swf):
                        print 'cannot init'
                        return
                    g_downloader = downloader
                    print 'init...'

                enableSeek = False
                requested_range = self.headers.getheader("Range")
                if requested_range == None: requested_range = ""
                srange, erange = (None, None)

                if downloader.live == False and len(
                        requested_range
                ) > 0 and not requested_range == "bytes=0-0":  #we have to stream?
                    enableSeek = True
                    (srange,
                     erange) = self.get_range_request(requested_range,
                                                      downloader.total_frags)

                print 'PROXY DATA', downloader.live, enableSeek, requested_range, downloader.total_frags, srange, erange
                enableSeek = False  ##disabled for time being, couldn't find better way to handle

                framgementToSend = 0
                inflate = 1815002  #(6526684-466/3)#*373/downloader.total_frags# 4142*1024*243/8/40 #1#1024*1024
                if enableSeek:
                    #rtype="video/x-flv" #just as default
                    self.send_response(206)
                    rtype = "flv-application/octet-stream"  #default type could have gone to the server to get it.
                    self.send_header("Content-Type", rtype)
                    self.send_header("Accept-Ranges", "bytes")
                    print 'not LIVE,enable seek', downloader.total_frags

                    totalsize = downloader.total_frags * inflate

                    framgementToSend = 1  #downloader.total_frags
                    erange = srange + framgementToSend * inflate
                    if erange >= totalsize:
                        erange = totalsize - 1

    #                crange="bytes "+str(srange)+"-" +str(int(downloader.total_frags-1))+"/"+str(downloader.total_frags)#recalculate crange based on srange, portionLen and content_size
    #                crange="bytes "+str(srange)+"-" +str(int(totalsize-1))+"/"+str(totalsize)#recalculate crange based on srange, portionLen and content_size
                    crange = "bytes " + str(srange) + "-" + str(
                        int(erange)
                    ) + "/*"  #+str(totalsize)#recalculate crange based on srange, portionLen and content_size
                    print srange / inflate, erange / inflate, totalsize / inflate
                    self.send_header("Content-Length", str(totalsize))
                    self.send_header("Content-Range", crange)
                    etag = self.generate_ETag(url)
                    self.send_header("ETag", etag)
                    print crange
                    self.send_header("Last-Modified",
                                     "Wed, 21 Feb 2000 08:43:39 GMT")
                    self.send_header("Cache-Control",
                                     "public, must-revalidate")
                    self.send_header("Cache-Control", "no-cache")
                    self.send_header("Pragma", "no-cache")
                    self.send_header("features", "seekable,stridable")
                    self.send_header("client-id", "12345")
                    self.send_header("Connection", 'close')
                else:
                    self.send_response(200)
                    rtype = "flv-application/octet-stream"  #default type could have gone to the server to get it.
                    self.send_header("Content-Type", rtype)
                    srange = None

            elif streamtype == 'SIMPLE' or simpledownloader:
                from interalSimpleDownloader import interalSimpleDownloader
                downloader = interalSimpleDownloader()
                if not downloader.init(self.wfile, url, proxy, g_stopEvent,
                                       maxbitrate):
                    print 'cannot init throw error because init'  #throw error because init
                    return
                srange, framgementToSend = (None, None)
                self.send_response(200)
                rtype = "flv-application/octet-stream"  #default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                srange = None
            elif streamtype == 'TSDOWNLOADER':
                from TSDownloader import TSDownloader
                downloader = TSDownloader()
                if not downloader.init(self.wfile, url, proxy, g_stopEvent,
                                       maxbitrate):
                    print 'cannot init but will continue to play'
                    #print 1/0
                    #return
                srange, framgementToSend = (None, None)
                self.send_response(200)
                rtype = "video/mp2t"  #default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                srange = None
            elif streamtype == 'HLS':
                from hlsDownloader import HLSDownloader
                downloader = HLSDownloader()
                if not downloader.init(self.wfile, url, proxy,
                                       use_proxy_for_chunks, g_stopEvent,
                                       maxbitrate, auth):
                    print 'cannot init'
                    return

                srange, framgementToSend = (None, None)
                self.send_response(200)
                rtype = "flv-application/octet-stream"  #default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                srange = None
            elif streamtype == 'HLSRETRY':
                from HLSDownloaderRetry import HLSDownloaderRetry
                downloader = HLSDownloaderRetry()
                if not downloader.init(self.wfile, url, proxy,
                                       use_proxy_for_chunks, g_stopEvent,
                                       maxbitrate, auth, callbackpath,
                                       callbackparam):
                    print 'cannot init'
                    return

                srange, framgementToSend = (None, None)
                self.send_response(200)
                rtype = "flv-application/octet-stream"  #default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                srange = None

            #rtype="flv-application/octet-stream"  #default type could have gone to the server to get it.
            #self.send_header("Content-Type", rtype)

            self.end_headers()
            if not srange == None:
                srange = srange / inflate

            if sendData:
                downloader.keep_sending_video(self.wfile, srange,
                                              framgementToSend)
                #runningthread=thread.start_new_thread(downloader.download,(self.wfile,url,proxy,use_proxy_for_chunks,))
                print 'srange,framgementToSend', srange, framgementToSend
                #runningthread=thread.start_new_thread(downloader.keep_sending_video,(self.wfile,srange,framgementToSend,))

                #xbmc.sleep(500)
                #while not downloader.status=="finished":
                #    xbmc.sleep(200);

        except:
            #Print out a stack trace
            traceback.print_exc()
            g_stopEvent.set()
            print 'sending 404'

            self.send_response(404)

            #Close output stream file
            self.wfile.close()
            print 'closed'
            return

        #Close output stream file
        self.wfile.close()
        return
Esempio n. 3
0
    def answer_request(self, sendData):
        global g_stopEvent
        global g_downloader

        try:

            # Pull apart request path
            request_path = self.path[1:]
            request_path = re.sub(r"\?.*", "", request_path)
            # If a request to stop is sent, shut down the proxy

            if request_path.lower() == "stop":  # all special web interfaces here
                sys.exit()
                return
            if request_path.lower() == "favicon.ico":
                print "dont have no icone here, may be in future"
                self.wfile.close()
                return

            (
                url,
                proxy,
                use_proxy_for_chunks,
                maxbitrate,
                simpledownloader,
                auth,
                streamtype,
                swf,
                callbackpath,
                callbackparam,
            ) = self.decode_url(request_path)
            print "simpledownloaderxxxxxxxxxxxxxxx", simpledownloader
            if streamtype == "" or streamtype == None or streamtype == "none":
                streamtype = "HDS"

            if streamtype == "HDS":

                print "Url received at proxy", url, proxy, use_proxy_for_chunks, maxbitrate

                # Send file request
                # self.handle_send_request(download_id,file_url, file_name, requested_range,download_mode ,keep_file,connections)

                downloader = None
                # downloader=g_downloader

                if (
                    not downloader
                    or downloader.live == True
                    or not (downloader.init_done and downloader.init_url == url)
                ):
                    from f4mDownloader import F4MDownloader

                    downloader = F4MDownloader()
                    if not downloader.init(
                        self.wfile, url, proxy, use_proxy_for_chunks, g_stopEvent, maxbitrate, auth, swf
                    ):
                        print "cannot init"
                        return
                    g_downloader = downloader
                    print "init..."

                enableSeek = False
                requested_range = self.headers.getheader("Range")
                if requested_range == None:
                    requested_range = ""
                srange, erange = (None, None)

                if (
                    downloader.live == False and len(requested_range) > 0 and not requested_range == "bytes=0-0"
                ):  # we have to stream?
                    enableSeek = True
                    (srange, erange) = self.get_range_request(requested_range, downloader.total_frags)

                print "PROXY DATA", downloader.live, enableSeek, requested_range, downloader.total_frags, srange, erange
                enableSeek = False  ##disabled for time being, couldn't find better way to handle

                framgementToSend = 0
                inflate = 1815002  # (6526684-466/3)#*373/downloader.total_frags# 4142*1024*243/8/40 #1#1024*1024
                if enableSeek:
                    # rtype="video/x-flv" #just as default
                    self.send_response(206)
                    rtype = "flv-application/octet-stream"  # default type could have gone to the server to get it.
                    self.send_header("Content-Type", rtype)
                    self.send_header("Accept-Ranges", "bytes")
                    print "not LIVE,enable seek", downloader.total_frags

                    totalsize = downloader.total_frags * inflate

                    framgementToSend = 1  # downloader.total_frags
                    erange = srange + framgementToSend * inflate
                    if erange >= totalsize:
                        erange = totalsize - 1

                    #                crange="bytes "+str(srange)+"-" +str(int(downloader.total_frags-1))+"/"+str(downloader.total_frags)#recalculate crange based on srange, portionLen and content_size
                    #                crange="bytes "+str(srange)+"-" +str(int(totalsize-1))+"/"+str(totalsize)#recalculate crange based on srange, portionLen and content_size
                    crange = (
                        "bytes " + str(srange) + "-" + str(int(erange)) + "/*"
                    )  # +str(totalsize)#recalculate crange based on srange, portionLen and content_size
                    print srange / inflate, erange / inflate, totalsize / inflate
                    self.send_header("Content-Length", str(totalsize))
                    self.send_header("Content-Range", crange)
                    etag = self.generate_ETag(url)
                    self.send_header("ETag", etag)
                    print crange
                    self.send_header("Last-Modified", "Wed, 21 Feb 2000 08:43:39 GMT")
                    self.send_header("Cache-Control", "public, must-revalidate")
                    self.send_header("Cache-Control", "no-cache")
                    self.send_header("Pragma", "no-cache")
                    self.send_header("features", "seekable,stridable")
                    self.send_header("client-id", "12345")
                    self.send_header("Connection", "close")
                else:
                    self.send_response(200)
                    rtype = "flv-application/octet-stream"  # default type could have gone to the server to get it.
                    self.send_header("Content-Type", rtype)
                    srange = None

            elif streamtype == "SIMPLE" or simpledownloader:
                from interalSimpleDownloader import interalSimpleDownloader

                downloader = interalSimpleDownloader()
                if not downloader.init(self.wfile, url, proxy, g_stopEvent, maxbitrate):
                    print "cannot init"
                    return
                srange, framgementToSend = (None, None)
                self.send_response(200)
                rtype = "flv-application/octet-stream"  # default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                srange = None
            elif streamtype == "TSDOWNLOADER":
                from TSDownloader import TSDownloader

                downloader = TSDownloader()
                if not downloader.init(self.wfile, url, proxy, g_stopEvent, maxbitrate):
                    print "cannot init"
                    return
                srange, framgementToSend = (None, None)
                self.send_response(200)
                rtype = "video/mp2t"  # default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                srange = None
            elif streamtype == "HLS":
                from hlsDownloader import HLSDownloader

                downloader = HLSDownloader()
                if not downloader.init(self.wfile, url, proxy, use_proxy_for_chunks, g_stopEvent, maxbitrate, auth):
                    print "cannot init"
                    return

                srange, framgementToSend = (None, None)
                self.send_response(200)
                rtype = "flv-application/octet-stream"  # default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                srange = None
            elif streamtype == "HLSRETRY":
                from HLSDownloaderRetry import HLSDownloaderRetry

                downloader = HLSDownloaderRetry()
                if not downloader.init(
                    self.wfile,
                    url,
                    proxy,
                    use_proxy_for_chunks,
                    g_stopEvent,
                    maxbitrate,
                    auth,
                    callbackpath,
                    callbackparam,
                ):
                    print "cannot init"
                    return

                srange, framgementToSend = (None, None)
                self.send_response(200)
                rtype = "flv-application/octet-stream"  # default type could have gone to the server to get it.
                self.send_header("Content-Type", rtype)
                srange = None

            # rtype="flv-application/octet-stream"  #default type could have gone to the server to get it.
            # self.send_header("Content-Type", rtype)

            self.end_headers()
            if not srange == None:
                srange = srange / inflate

            if sendData:
                downloader.keep_sending_video(self.wfile, srange, framgementToSend)
                # runningthread=thread.start_new_thread(downloader.download,(self.wfile,url,proxy,use_proxy_for_chunks,))
                print "srange,framgementToSend", srange, framgementToSend
                # runningthread=thread.start_new_thread(downloader.keep_sending_video,(self.wfile,srange,framgementToSend,))

                xbmc.sleep(500)
                while not downloader.status == "finished":
                    xbmc.sleep(200)

        except:
            # Print out a stack trace
            traceback.print_exc()
            g_stopEvent.set()
            self.send_response(404)
            # Close output stream file
            self.wfile.close()
            return

        # Close output stream file
        self.wfile.close()
        return