if m_simpleTV.Control.ChangeAdress ~= 'No' then return end

local inAdr =  m_simpleTV.Control.CurrentAdress

if inAdr==nil then return end

if not string.match(inAdr, '^http://stream%.1tv%.ru/live' ) and not string.match(inAdr, '^http://stream%.1tv%.ru/doc' ) then return end

local pageid = findpattern( inAdr, '^http://stream%.1tv%.ru/(%l+)',1,21,0)

if pageid == 'doc' then streamid = pageid else streamid = '1tv' end

m_simpleTV.Control.ChangeAdress='Yes'
m_simpleTV.Control.CurrentAdress = ''
----------------------------------------------------------------
local session = m_simpleTV.WinInet.New("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/40.0")
if session == nil then return end

 --INTERNET_OPTION_CONNECT_TIMEOUT
 m_simpleTV.WinInet.SetOptionInt(session,2,15000,0)
 --INTERNET_OPTION_RECEIVE_TIMEOUT
 m_simpleTV.WinInet.SetOptionInt(session,6,15000,0)
---------------------------------------------------------------

local  rc,answer=m_simpleTV.WinInet.Get(session,'http://stream.1tv.ru/n2playlist/1tvch.xml')

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
	   return
 end
 
--debug_in_file(answer .. "\n\n")

local adress = findpattern( answer, 'content url=%"http://(.-)/phds%-live',1,20,10)

local utime = findpattern( answer, 'f4m%?e=(%d+)%"',1,6,1)

if adress == nil then
m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.CurrentAdress = ''
--m_simpleTV.OSD.ShowMessage("stream not found - " .. rc ,255,10)
return
end

--debug_in_file(adress .. "\n\n")

local retAdrr = 'http://' .. adress .. '/hds-live11/livepkgr/_definst_/' .. streamid .. '-hd.f4m?e=' .. utime

--debug_in_file(retAdrr .. '\n\n')

m_simpleTV.Control.ChangeAdress='Yes'
m_simpleTV.Control.CurrentAdress = ''

local session = m_simpleTV.WinInet.New("Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/40.0")
if session == nil then return end

 --INTERNET_OPTION_CONNECT_TIMEOUT
 m_simpleTV.WinInet.SetOptionInt(session,2,15000,0)
 --INTERNET_OPTION_RECEIVE_TIMEOUT
 m_simpleTV.WinInet.SetOptionInt(session,6,15000,0)
---------------------------------------------------------------

local  rc,answer=m_simpleTV.WinInet.Get(session,retAdrr)

 if rc~=200 then
	   m_simpleTV.WinInet.Close(session)
	   m_simpleTV.OSD.ShowMessage("Connection error - " .. rc ,255,10)
	   return
 end

--debug_in_file(answer .. "\n\n")

local retAdr
local i=1
local t={}
local name,adr,url
 for w in string.gmatch(answer,'<media(.-)bootstrapInfoId=') do
	adr = findpattern (w,'streamId=%"(.-)%"',1,10,1)
	 if adr == nil then break end
	name = findpattern (w,'bitrate=%"(.-)%"',1,9,1)
	 if name == nil then break end
	 url = findpattern (w,'http://(.-)/hds%-live',1,7,9)
	 if url ~= nil then adress=url end
     t[i] = {}
     t[i].Id   =  i
     t[i].Name = 'Bitrate ' .. name .. ' kbps'
     t[i].Adress  = 'http://' .. adress .. '/hds-live11/livepkgr/_definst_/' .. streamid .. '-hd/' .. adr .. '.f4m?e=' .. utime .. '$OPT:codec=avcodec$OPT:http-referrer=http://stream.1tv.ru/live$OPT:http-user-agent="Mozilla/5.0 (Windows NT 6.1; WOW64; rv:37.0) Gecko/20100101 Firefox/40.0"'

--debug_in_file(t[i].Name .. '  ' .. t[i].Adress .. '\n\n')
 i=i+1
 end

if i>2 then
    local ret,id = m_simpleTV.OSD.ShowSelect('Select resolution',0,t,7000,1)
	if ret==1 then
	  retAdr =  t[id].Adress
	end	
end

m_simpleTV.WinInet.Close(session)
m_simpleTV.Control.CurrentAdress = retAdr