--get address
--исходная ссылка --http://www.ustream.tv/channel/live-iss-stream
--или исходная ссылка --http://www.ustream.tv/channel/9408562

--результат --http://iphone-streaming.ustream.tv/uhls/18209102/streams/live/iphone/playlist.m3u8
--или так   --http://iphone-streaming.ustream.tv/ustreamVideo/18209102/streams/live/playlist.m3u8

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

local inAdr = m_simpleTV.Control.CurrentAdress
if inAdr==nil or not inAdr:find('ustream%.tv') or inAdr:find('ustream%.tv/recorded') then return end

m_simpleTV.Control.ChangeAdress = 'Yes'

local session = m_simpleTV.WinInet.New("Opera/9.80 (Windows NT 6.1; U; ru) Presto/2.9.168 Version/11.52")
if session == nil then return end

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

inAdr = inAdr:gsub('https://','http://')
local rc,answer=m_simpleTV.WinInet.Get(session,inAdr)
if rc~=200 then
   m_simpleTV.OSD.ShowMessage("Net error " .. rc,255,5)
   m_simpleTV.Control.ExecuteAction(11)  -- 11   Stop (KEYSTOP)
   m_simpleTV.WinInet.Close(session)
   return
end

local id = answer:match('cid=(%d+)') or inAdr:match('/(%d+)')


if not id then 
	 m_simpleTV.OSD.ShowMessage("Cid not found.",255,5)
	 m_simpleTV.Common.Wait(1000)
	 m_simpleTV.WinInet.Close(session)
	 return 
end
	 
m_simpleTV.Control.CurrentAdress = nil

--http://iphone-streaming.ustream.tv/uhls/9408562/streams/live/iphone/playlist.m3u8

inAdr = 'http://iphone-streaming.ustream.tv/uhls/'..id..'/streams/live/iphone/playlist.m3u8'

rc,answer=m_simpleTV.WinInet.Get(session,inAdr)

m_simpleTV.WinInet.Close(session)

if rc~=200 then
   m_simpleTV.OSD.ShowMessage("OFF AIR. Net error " .. rc,255,5)
   m_simpleTV.Control.ExecuteAction(11)  -- 11   Stop (KEYSTOP)
   return
end
-------------------------------
--http://sjc-uhls-proxy105.ustream.tv/watch/playlist.m3u8?appType=11&appVersion=2&conn=wifi&cdn=uhs_akamai&locks=97d170e1550eee4afc0af065b78cda302a97674c&cid=17074538&stream=live_9&connectionId=sjc-ums30_1624868145&group=iphone
----------------------------
--http://iphone-streaming.ustream.tv/uhls/18209102/streams/live/iphone/playlist.m3u8  
--------------------------

--inAdr = answer:match('(http://.-group=iphone)') 
local max, r = 0, 0
for url in answer:gmatch('(http://.-group=iphone)') do
     r = url:match('viewer_(%d+)p') or 0
	 r = tonumber(r)
	 if max <= r then 
	    max = r
	  	inAdr = url
	 end 
end


m_simpleTV.Control.CurrentAdress = inAdr


