Skip to content

Commit

Permalink
Opravy synapse SSL lib, MPEG-DASH
Browse files Browse the repository at this point in the history
  • Loading branch information
IBV committed Mar 17, 2023
1 parent a706fff commit ac8096f
Show file tree
Hide file tree
Showing 19 changed files with 506 additions and 313 deletions.
91 changes: 60 additions & 31 deletions Source/Base/uDASHDownloader.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
______________________________________________________________________________
YTD v1.63 (c) 2019 ibv
YTD v1.63 (c) 2023 ibv
https://ibv.github.io/YTD/
______________________________________________________________________________
Expand Down Expand Up @@ -142,7 +142,9 @@ function TDASHDownloader.InitDownloadInfo: boolean;
var
Http: THttpSend;
init, id: string;
i : integer;
i,j : integer;
ATimeT,ATimeD,
SegTime,Segnumber : integer;
Xml: TXmlDoc;
Prot, User, Pass, Host, Port, Path, Para, URI: string;
begin
Expand All @@ -155,9 +157,7 @@ function TDASHDownloader.InitDownloadInfo: boolean;
else
begin
id:='';

fMPD := TMPDObject.Create(Xml);

id := fMPD.GetBestID(fMaxBitRate);
// without baseurl
if fMPD.BaseURL = '' then
Expand All @@ -169,7 +169,6 @@ function TDASHDownloader.InitDownloadInfo: boolean;
port:=':'+port;
fMPD.BaseURL := Prot+'://'+host+port+copy(path,1,LastDelimiter('/',Path));
end;

if not Result then
begin
fVideoDownloader := CreateHttp;
Expand All @@ -183,24 +182,39 @@ function TDASHDownloader.InitDownloadInfo: boolean;
// video stream
if Options.ReadProviderOptionDef(Provider, OPTION_DASH_VIDEO_SUPPORT, false) then
begin
VideoEndNumber := fMPD.VideoEndNumber;
Fragments.Add(GetRelativeUrl(fMPD.BaseURL, fMPD.VideoInit));
for i:=0 to fMPD.VideoEndNumber do
if AnsiContainsStr(fMPD.VideoMedia, '$Time$') then
begin
{if not Result then
// time based segment
SegNumber := fMPD.VideoStartNumber;
SegTime := 0;
for i:=0 to fMPD.VideoSegmentList.Count-1 do
begin
fVideoDownloader := CreateHttp;
fVideoDownloader.Cookies.Assign(Http.Cookies);
fVideoDownloader.Sock.OnStatus := SockStatusMonitor;
fDownloadedThisFragment := 0;
fDownloadedPreviousFragments := 0;
Result := True;
end;}
ATimeT := TXMLNode(fMPD.VideoSegmentList[i]).ReadAttributeInteger('t',0);
ATimeD := TXMLNode(fMPD.VideoSegmentList[i]).ReadAttributeInteger('d',0);
if ATimeT >0 then SegTime := ATimeT;
//
init := StringReplace(fMPD.VideoMedia,'$Time$',format('%d', [SegTime]),[]);
Fragments.Add(GetRelativeUrl(fMPD.BaseURL, init));
//
inc(SegNumber);
for j:=0 to TXMLNode(fMPD.VideoSegmentList[i]).ReadAttributeInteger('r',0)-1 do
begin
inc(SegTime,ATimeD);
init := StringReplace(fMPD.VideoMedia,'$Time$',format('%d', [SegTime]),[]);
Fragments.Add(GetRelativeUrl(fMPD.BaseURL, init));
inc(SegNumber);
end;
inc(SegTime,ATimeD);
end;
VideoEndNumber := SegNumber;
end
else
for i:=0 to fMPD.VideoEndNumber do
begin
// pro CT '%06d'
if pos('%',fMPD.VideoMedia) >0 then
init := StringReplace(fMPD.VideoMedia,'%'+fMPD.fm+'$',format('%.'+fMPD.fm, [i+fMPD.VideoStartNumber]),[]);
if pos('$Time$',fMPD.VideoMedia) >0 then
init := StringReplace(fMPD.VideoMedia,'$Time$',format('%d', [fMPD.VideoTimeT + i*fMPD.VideoTimeD]),[]);

Fragments.Add(GetRelativeUrl(fMPD.BaseURL, init));
end;
end;
Expand All @@ -210,13 +224,37 @@ function TDASHDownloader.InitDownloadInfo: boolean;
begin
id := fMPD.GetBestID(128000,false);
Fragments.Add(GetRelativeUrl(fMPD.BaseURL, fMPD.AudioInit));
if AnsiContainsStr(fMPD.AudioMedia, '$Time$') then
begin
// time based segment
SegNumber := fMPD.AudioStartNumber;
SegTime := 0;
for i:=0 to fMPD.AudioSegmenList.Count-1 do
begin
ATimeT := TXMLNode(fMPD.AudioSegmenList[i]).ReadAttributeInteger('t',0);
ATimeD := TXMLNode(fMPD.AudioSegmenList[i]).ReadAttributeInteger('d',0);
if ATimeT > 0 then SegTime := ATimeT;
//
init := StringReplace(fMPD.AudioMedia,'$Time$',format('%d', [SegTime]),[]);
Fragments.Add(GetRelativeUrl(fMPD.BaseURL, init));
//
inc(SegNumber);
for j:=0 to TXMLNode(fMPD.AudioSegmenList[i]).ReadAttributeInteger('r',0)-1 do
begin
inc(SegTime,ATimeD);
init := StringReplace(fMPD.AudioMedia,'$Time$',format('%d', [SegTime]),[]);
Fragments.Add(GetRelativeUrl(fMPD.BaseURL, init));
inc(SegNumber);
end;
inc(SegTime,ATimeD);
end;
end
else
for i:=0 to fMPD.AudioEndNumber do
begin
// pro CT '%06d'
if pos('%',fMPD.AudioMedia) >0 then
init := StringReplace(fMPD.AudioMedia,'%'+fMPD.fm+'$',format('%.'+fMPD.fm, [i+fMPD.AudioStartNumber]),[]);
if pos('$Time$',fMPD.AudioMedia) >0 then
init := StringReplace(fMPD.AudioMedia,'$Time$',format('%d', [fMPD.AudioTimeT + i*fMPD.AudioTimeD]),[]);

Fragments.Add(GetRelativeUrl(fMPD.BaseURL, init));
end;
end;
Expand Down Expand Up @@ -274,8 +312,7 @@ function TDASHDownloader.Download: boolean;
begin
FragmentDownloaded := False;
Retry := RetryCount;

if (i=VideoEndNumber+2) and Options.ReadProviderOptionDef(Provider, OPTION_DASH_AUDIO_SUPPORT, false) then
if (i=VideoEndNumber+1) and Options.ReadProviderOptionDef(Provider, OPTION_DASH_AUDIO_SUPPORT, false) then
begin
FN:=ChangeFileExt(FN, '.mpa');
FInalFN:=FN;
Expand Down Expand Up @@ -309,14 +346,6 @@ function TDASHDownloader.Download: boolean;
Dec(Retry);
if not FragmentDownloaded then
Exit;

{if i=fMPD.VideoEndNumber+1 then
begin
FN:=ChangeFileExt(FN, '.mpa');
FInalFN:=FN;
FreeAndNil(Stream);
end;}

end;
Result := True;
finally
Expand Down
10 changes: 5 additions & 5 deletions Source/Base/uDownloader.pas
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,12 @@ implementation

const
///DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)';
///DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Firefox/58.0.1';
///DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3703.3 Safari/537.36';
///DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101 Firefox/102.0';
///DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/103.0.0.0 Safari/537.36';
DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%d.0.%d.%d Safari/537.36';



var
UrlRegExps: TRegExpCache = nil;

Expand Down Expand Up @@ -300,9 +302,7 @@ constructor TDownloader.Create(const AMovieID: string);
{$ENDIF}
fHttp := THttpSend.Create;
///fHttp.UserAgent := DEFAULT_USER_AGENT;
///DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3703.3 Safari/537.36';
///DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/%d.0.%d.%d Safari/537.36';
fHttp.UserAgent := format(DEFAULT_USER_AGENT, [RandomRange(71, 74),RandomRange(3700, 3710),RandomRange(0, 100)]);
fHttp.UserAgent := format(DEFAULT_USER_AGENT, [RandomRange(74, 103),RandomRange(3700, 3710),RandomRange(0, 100)]);
MovieID := AMovieID;
///fVideoBitRate := MaxInt;
end;
Expand Down
17 changes: 11 additions & 6 deletions Source/Base/uHLSDownloader.pas
Original file line number Diff line number Diff line change
Expand Up @@ -340,28 +340,30 @@ function THLSDownloader.Download: boolean;
FragmentDownloaded := False;
Retry := RetryCount;
while Retry >= 0 do
begin
if DownloadBinary(VideoDownloader, Fragments[i], FragmentData) then
begin
begin
FragmentDownloaded := True;
Inc(fFragmentsDownloaded);
fDownloadedPreviousFragments := fDownloadedPreviousFragments + fDownloadedThisFragment;
fDownloadedThisFragment := 0;
if FragmentData <> '' then
begin
begin
if Stream = nil then
begin
begin
Stream := TFileStream.Create(FN, fmCreate);
{$IFDEF SHAREABLEFILES}
FreeAndNil(Stream);
Stream := TFileStream.Create(FN, fmOpenWrite or fmShareDenyWrite);
{$ENDIF}
end;
Stream.WriteBuffer(FragmentData[1], Length(FragmentData));
end;
Stream.WriteBuffer(FragmentData[1], Length(FragmentData));
end;
Break;
end
end
else
Dec(Retry);
end;
if not FragmentDownloaded then
Exit;
end;
Expand Down Expand Up @@ -484,7 +486,9 @@ procedure THLSDownloader.SockStatusMonitor(Sender: TObject; Reason: THookSocketR
begin
SetLastErrorMsg(_(SockStatusReasons[Reason]));
if (Reason = HR_ReadCount) then
begin
fDownloadedThisFragment := fDownloadedThisFragment + StrToInt64(Value);
end;
if not (Reason in [HR_SocketClose, HR_Error]) then
DoProgress;
end;
Expand All @@ -494,6 +498,7 @@ function THLSDownloader.GetDownloadedSize: int64;
Result := fDownloadedPreviousFragments + fDownloadedThisFragment;
end;


function THLSDownloader.GetTotalSize: int64;
var
AverageSizePerFragment: int64;
Expand Down
68 changes: 34 additions & 34 deletions Source/Base/uMPD.pas
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
______________________________________________________________________________
YTD v1.64 (c) 2019 ibv
YTD v1.69 (c) 2023 ibv
https://ibv.github.io/YTD/
______________________________________________________________________________
Expand Down Expand Up @@ -53,8 +53,10 @@ interface
TMPDObject = class
private
fBaseURL: String;
fAudioList: TList;
fVideoList: TList;
fAudioRepreList: TList;
fVideoRepreList: TList;
fVideoSegmentList: TList;
fAudioSegmentList: TList;
fXml: TXmlDoc;
fMedia,fInit,
fAMedia,fAInit: string;
Expand All @@ -73,8 +75,8 @@ TMPDObject = class

function GetBestID(BandWidth:integer;Video:boolean = true):string;

property AudioList: TList read fAudioList;
property VideoList: TList read fVideoList;
property AudioSegmenList: TList read fAudioSegmentList;
property VideoSegmentList: TList read fVideoSegmentList;
property BaseURL: String read fBaseURL write fBaseURL;
property VideoMedia: string read fMedia;
property VideoInit: string read fInit;
Expand Down Expand Up @@ -105,8 +107,10 @@ implementation
constructor TMPDObject.Create(Xml:TXmlDoc);
begin
fXml := Xml;
fAudioList:=TList.Create;
fVideoList:=TList.Create;
fAudioRepreList:=TList.Create;
fVideoRepreList:=TList.Create;
fAudioSegmentList:=TList.Create;
fVideoSegmentList:=TList.Create;
fBaseURL := '';
fMedia:='';
fInit:='';
Expand All @@ -127,8 +131,10 @@ constructor TMPDObject.Create(Xml:TXmlDoc);

destructor TMPDObject.Destroy;
begin
FreeAndNil(fAudioList);
FreeAndNil(fVideoList);
FreeAndNil(fAudioRepreList);
FreeAndNil(fVideoRepreList);
FreeAndNil(fAudioSegmentList);
FreeAndNil(fVideoSegmentList);
end;


Expand All @@ -150,20 +156,20 @@ procedure TMPDObject.ParseMPD;
fVideostartNumber := Node.ReadAttributeInteger('startNumber',0);
end;

Node1.FindNodes('S',fVideoList);
Node1.FindNodes('S',fVideoSegmentList);
fVideoEndNumber:=0;
for i:=0 to fVideoList.Count-1 do
// id based segment
if not AnsiContainsStr(fMedia, '$Time$') then
begin
if TXMLNode(fVideoList[i]).ReadAttributeInteger('r',0) > 0 then
for i:=0 to fVideoSegmentList.Count-1 do
begin
fVideoEndNumber := TXMLNode(fVideoList[i]).ReadAttributeInteger('r',0);
fVTimeT := TXMLNode(fVideoList[i]).ReadAttributeInteger('t',0);
fVTimeD := TXMLNode(fVideoList[i]).ReadAttributeInteger('d',0);
///if TXMLNode(fVideoSegmentList[i]).ReadAttributeInteger('r',0) > 0 then
inc(fVideoEndNumber,TXMLNode(fVideoSegmentList[i]).ReadAttributeInteger('r',0));
end;
end;
inc(fVideoEndNumber,fVideoList.count-1);
inc(fVideoEndNumber,fVideoSegmentList.count-1);

Node1.FindNodes('Representation',fVideoList);
Node1.FindNodes('Representation',fVideoRepreList);
end;

if fxml.NodeByPathAndAttr('Period/AdaptationSet','mimeType','audio/mp4',Node1) then
Expand All @@ -177,20 +183,20 @@ procedure TMPDObject.ParseMPD;
fAudiostartNumber := Node.ReadAttributeInteger('startNumber',0);
end;

Node1.FindNodes('S',fAudioList);
Node1.FindNodes('S',fAudioSegmentList);
fAudioEndNumber := 0;
for i:=0 to fAudioList.Count-1 do
// id based segment
if not AnsiContainsStr(faMedia, '$Time$') then
begin
if TXMLNode(fAudioList[i]).ReadAttributeInteger('r',0) > 0 then
for i:=0 to fAudioSegmentList.Count-1 do
begin
fAudioEndNumber := TXMLNode(fAudioList[i]).ReadAttributeInteger('r',0);
fATimeT := TXMLNode(fAudioList[i]).ReadAttributeInteger('t',0);
fATimeD := TXMLNode(fAudioList[i]).ReadAttributeInteger('d',0);
///if TXMLNode(fAudioSegmentList[i]).ReadAttributeInteger('r',0) > 0 then
inc(fAudioEndNumber,TXMLNode(fAudioSegmentList[i]).ReadAttributeInteger('r',0));
end;
end;
inc(fAudioEndNumber,fAudioList.count-1);
inc(fAudioEndNumber,fAudioSegmentList.count-1);

Node1.FindNodes('Representation',fAudioList);
Node1.FindNodes('Representation',fAudioRepreList);

end;
end;
Expand All @@ -204,12 +210,12 @@ function TMPDObject.GetBestID(BandWidth:integer;Video:boolean = true):string;
List: TList;
begin
result:='';
List:=fVideoList;
List:=fVideoRepreList;
media:=fmedia;
init :=finit;
if not Video then
begin
List:=fAudioList;
List:=fAudioRepreList;
media:=famedia;
init:=fainit;
end;
Expand All @@ -224,13 +230,7 @@ function TMPDObject.GetBestID(BandWidth:integer;Video:boolean = true):string;

Init := StringReplace(init,'$RepresentationID$',id,[]);
Media := StringReplace(media,'$RepresentationID$',id,[]);
// time based segment
if AnsiContainsStr(media, '$Time') then
begin
// ToDo
end
else
// id based segment
// id based segment, CT
if AnsiContainsStr(media, '$Number') then
begin
//%06d$
Expand Down
Loading

0 comments on commit ac8096f

Please sign in to comment.