vlc.pp 46 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. {
  2. Copyright (c) 2012 by Michael Van Canneyt
  3. member of the Free Pascal development team
  4. This is a OOP wrapper around the VLC libraries.
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. unit vlc;
  12. {$mode objfpc}{$H+}
  13. interface
  14. uses
  15. Classes, SysUtils, ctypes, libvlc, syncobjs;
  16. Type
  17. { TVLCLibrary }
  18. TVLCLibrary = class(TComponent)
  19. private
  20. FInstance : plibvlc_instance_t;
  21. FLibraryArgs: TStrings;
  22. FLibraryPath : String;
  23. function GetI: Boolean;
  24. function GetLastError: String;
  25. Function GetVersion : String;
  26. Function GetCompiler : String;
  27. Function GetChangeSet : String;
  28. Procedure SetLibraryPath(Const AValue : String);
  29. Protected
  30. Function GetInstance : plibvlc_instance_t; virtual;
  31. property Instance : plibvlc_instance_t read GetInstance;
  32. public
  33. constructor Create (AOwner : TComponent); override;
  34. destructor Destroy; override;
  35. Procedure Initialize;
  36. Procedure Release;
  37. property LastError : String read GetLastError;
  38. property Version : String read GetVersion;
  39. property Compiler : String read GetCompiler;
  40. property ChangeSer : String read GetChangeSet;
  41. property LibraryPath : String read FLibraryPath write SetLibraryPath;
  42. Property LibraryArgs : TStrings read FLibraryArgs Write FLibraryArgs;
  43. Property Initialized : Boolean Read GetI;
  44. end;
  45. TVLCLibraryClass = Class of TVLCLibrary;
  46. TCustomVLCMediaPlayer = Class;
  47. TVLCMediaItems = Class;
  48. { TVLCMediaItem }
  49. TSnapShotFormat = (ssfPNG,ssfJPG);
  50. TDeinterlaceMode = (dmBlend, dmDiscard, dmBob, dmLinear, dmMean, dmX, dmYadif, dmYadif2x);
  51. TVLCMediaItem = Class(TCollectionItem)
  52. private
  53. FDIM: TDeinterlaceMode;
  54. FInstance : plibvlc_media_t;
  55. FOpts : Array [0..3] of Boolean; // Keep in sync with property indexes
  56. FPath: String;
  57. FSS: TSNapshotFormat;
  58. FFD: Integer;
  59. function GetInstance: plibvlc_media_t;
  60. function GetM(AIndex: Integer): Boolean;
  61. function GetMD(AMeta : libvlc_meta_t): String;
  62. function GetMRL: String;
  63. function GetParsed: Boolean;
  64. function GetUD: Pointer;
  65. procedure SetDIM(AValue: TDeinterlaceMode);
  66. procedure SetFSS(AValue: TSNapshotFormat);
  67. procedure SetM(AIndex: Integer; AValue: Boolean);
  68. Function GetBoolOpt(AIndex : Integer; AValue : Boolean) : String;
  69. procedure SetMD(AMeta : libvlc_meta_t; AValue: String);
  70. procedure SetUD(AValue: Pointer);
  71. function GetState : libvlc_state_t;
  72. function GetDuration : TDateTime;
  73. Protected
  74. Procedure RegisterInstance;
  75. Procedure UnRegisterInstance;
  76. procedure SetMRL(AValue: String); virtual;
  77. procedure SetPath(AValue: String); virtual;
  78. procedure SetFD(AValue: Integer); virtual;
  79. Function GetVLC : TVLCLibrary; virtual;
  80. function GetEventManager : plibvlc_event_manager_t;
  81. Procedure SetInstance( Avalue : plibvlc_media_t);
  82. Property Instance : plibvlc_media_t Read GetInstance;
  83. Public
  84. Destructor Destroy; override;
  85. Procedure AddOption(Const AValue : String);
  86. procedure Parse;
  87. procedure ParseAsync;
  88. Procedure SaveMetaData;
  89. Function GetStats(Var AStats : libvlc_media_stats_t) : Boolean;
  90. Function Duplicate : TVLCMediaItem;
  91. Property Parsed : Boolean Read GetParsed;
  92. Property ShowTitle : Boolean Index 0 Read GetM Write SetM;
  93. Property VideoOnTop : Boolean Index 1 Read GetM Write SetM;
  94. Property UseOverlay : Boolean Index 2 Read GetM Write SetM;
  95. Property FullScreen : Boolean Index 3 Read GetM Write SetM;
  96. Property DeinterlaceFilter : Boolean Index 4 Read GetM Write SetM;
  97. Property DeInterlaceMode : TDeinterlaceMode Read FDIM Write SetDIM;
  98. Property SnapShotFormat : TSNapshotFormat Read FSS Write SetFSS;
  99. Property UserData : Pointer Read GetUD Write SetUD;
  100. Property State : libvlc_state_t Read GetState;
  101. Property Duration : TDateTime Read GetDuration;
  102. Property MetaData[AMeta : libvlc_meta_t] : String Read GetMD Write SetMD;
  103. // These must be set prior to using any of the above.
  104. Property MRL : String Read GetMRL Write SetMRL;
  105. Property Path : String Read FPath Write SetPath;
  106. property FileDescriptor : Integer Read FFD Write SetFD;
  107. end;
  108. TVLCMediaItemClass = Class of TVLCMediaItem;
  109. { TVLCMediaItems }
  110. TVLCPlayMode = (pmNormal,pmLoop,pmRepeat);
  111. TVLCMediaItems = Class(TCollection)
  112. Private
  113. FPlayer: TCustomVLCMediaPlayer;
  114. FPlayMode: TVLCPlayMode;
  115. FVLC : TVLCLibrary;
  116. FInstance : Plibvlc_media_list_t;
  117. function GetI(AIndex : Integer): TVLCMediaItem;
  118. function GetInstance: Plibvlc_media_list_t;
  119. function GetIsReadOnly: Boolean;
  120. procedure SetI(AIndex : Integer; AValue: TVLCMediaItem);
  121. Protected
  122. Function GetVLC : TVLCLibrary; virtual;
  123. Public
  124. Constructor Create(ALibrary : TVLCLibrary;AItemClass: TVLCMediaItemClass = Nil); overload;
  125. Constructor Create(AInstance : Plibvlc_media_list_t;AItemClass: TVLCMediaItemClass = Nil); overload;
  126. Procedure Lock;
  127. Procedure Unlock;
  128. Property Instance : Plibvlc_media_list_t read GetInstance;
  129. Property VLC : TVLCLibrary Read GetVLC Write FVLC;
  130. Property MediaItems[AIndex : Integer] : TVLCMediaItem Read GetI Write SetI; default;
  131. Property ReadOnly : Boolean Read GetIsReadOnly;
  132. end;
  133. { TCustomVLCMediaPlayer }
  134. TBooleanEvent = procedure(Sender : TObject; Const AValue : Boolean) of object;
  135. TTitleEvent = procedure(Sender : TObject; Const ATitle : Integer) of object;
  136. TSnapshotEvent = procedure(Sender : TObject; Const AfileName : string) of object;
  137. TErrorEvent = procedure(Sender : TObject; Const AError : string) of object;
  138. TTimeEvent = procedure(Sender : TObject; Const time : TDateTime) of object;
  139. TPositionEvent = procedure(Sender : TObject; Const APos : Double) of object;
  140. TCustomVLCMediaPlayer = Class(TComponent)
  141. private
  142. FFitWindow: Boolean;
  143. FOnBackward: TNotifyEvent;
  144. FOnBuffering: TNotifyEvent;
  145. FOnEOF: TNotifyEvent;
  146. FOnError: TErrorEvent;
  147. FOnForward: TNotifyEvent;
  148. FOnLengthChanged: TTimeEvent;
  149. FOnMediaChanged: TNotifyEvent;
  150. FOnNothingSpecial: TNotifyEvent;
  151. FOnOpening: TNotifyEvent;
  152. FOnPausableChanged: TBooleanEvent;
  153. FOnPause: TNotifyEvent;
  154. FOnPlaying: TNotifyEvent;
  155. FOnPositionChanged: TPositionEvent;
  156. FOnSeekableChanged: TBooleanEvent;
  157. FOnSnapShot: TSnapShotEvent;
  158. FOnStop: TNotifyEvent;
  159. FOnTimeChanged: TTimeEvent;
  160. FOnTitleChanged: TTitleEvent;
  161. FUseEvents: Boolean;
  162. Finstance : Plibvlc_media_player_t;
  163. FVLC: TVLCLibrary;
  164. FECS : TCriticalSection;
  165. function GetAspectRatio: String;
  166. function GetAudioMuted: Boolean;
  167. function GetAudioTrack: Integer;
  168. function GetState: libvlc_state_t;
  169. function GetVideoDuration: TDateTime;
  170. function GetVideoFPS: Double;
  171. function GetVideoFractional: Double;
  172. function GetVideoHeight: Cardinal;
  173. function GetVideoLength: Int64;
  174. function GetVideoPos: Int64;
  175. function GetVideoScale: Double;
  176. function GetVideoWidth: Cardinal;
  177. function GetVLC: TVLCLibrary;
  178. procedure SetAspectRatio(AValue: String);
  179. procedure SetAudioMuted(AValue: Boolean);
  180. procedure SetFitWindow(AValue: Boolean);
  181. procedure SetUseEVents(AValue: Boolean);
  182. function GetAudioTrackCount : Integer;
  183. procedure SetAudioTrack(AValue: Integer);
  184. function GetAudioTrackDescriptions(AIndex : Integer) : String;
  185. function GetChannel: Integer;
  186. procedure SetChannel(AValue : Integer);
  187. function GetAudioDelay : Int64;
  188. procedure SetAudioDelay (AValue: Int64);
  189. function GetPlaying : Boolean;
  190. function GetChapter : Integer;
  191. procedure SetChapter(AValue : Integer);
  192. function GetChapterCount: Integer;
  193. Function GetPlayable : Boolean;
  194. Function GetPausable : Boolean;
  195. Function GetSeekable : Boolean;
  196. function GetAudioVolume : Integer;
  197. function GetPlayRate: Integer;
  198. procedure SetAudioVolume(AValue : Integer);
  199. procedure SetPlayRate(AValue: Integer);
  200. procedure SetFullScreenMode(AValue: Boolean);
  201. function GetFullScreenMode: Boolean;
  202. procedure SetVideoFractional(AValue: Double);
  203. procedure SetVideoPos(AValue: Int64);
  204. procedure SetVideoScale(AValue: Double);
  205. Protected
  206. function GetInstance: Plibvlc_media_player_t; virtual;
  207. // Called to set parent window. Descendents must override this.
  208. Procedure SetParentWindow; virtual;
  209. // Called when FitWindow is true.
  210. Procedure SetParentWindowSize(AWidth,AHeight : Cardinal); Virtual;
  211. procedure DoMediaChanged; virtual;
  212. procedure DoNothingSpecial; virtual;
  213. procedure DoOnBackward; virtual;
  214. procedure DoOnBuffering;virtual;
  215. procedure DoOnEOF;virtual;
  216. procedure DoOnError;virtual;
  217. procedure DoOnForward;virtual;
  218. procedure DoOnOpening;virtual;
  219. procedure DoOnPause;virtual;
  220. procedure DoOnPlaying;virtual;
  221. procedure DoOnStop;virtual;
  222. procedure DoOnLengthChanged(const ATime: libvlc_time_t); virtual;
  223. procedure DoOnPausableChanged(const APausable: Boolean); virtual;
  224. procedure DoOnPositionChanged(const Aposition: Double); virtual;
  225. procedure DoOnSeekableChanged(const ASeekable: Boolean); virtual;
  226. procedure DoOnTimeChanged(const ATime: libvlc_time_t); virtual;
  227. procedure DoOnTitleChanged(const ATitle: cint); virtual;
  228. procedure DoOnSnapshot(const AFileName: PCChar); virtual;
  229. procedure HookupEvents; virtual;
  230. procedure UnHookEvents; virtual;
  231. procedure HandleVLCEvent(e: Plibvlc_event_t); virtual;
  232. Property VLC : TVLCLibrary Read GetVLC Write FVLC;
  233. Property Instance : Plibvlc_media_player_t Read GetInstance;
  234. Public
  235. Destructor Destroy; override;
  236. procedure Play;
  237. procedure SetMedia(M: TVLCMediaItem);
  238. Procedure Play(M : TVLCMediaItem);
  239. Procedure PlayFile(Const AFileName : String);
  240. Procedure Stop;
  241. procedure Pause;
  242. procedure Resume;
  243. procedure NextFrame;
  244. function Snapshot(Const AFileName: String): Boolean;
  245. function Snapshot(Const AFileName: String; AWidth, AHeight: Cardinal): Boolean;
  246. function GetVideoSize(Var AWidth, AHeight: Cardinal): Boolean;
  247. // These can be made public/published in descendents
  248. Protected
  249. Property Playable : Boolean Read GetPlayable;
  250. Property Pausable : Boolean Read GetPausable;
  251. Property Seekable : Boolean Read GetSeekable;
  252. Property Playing : Boolean Read GetPlaying;
  253. Property State : libvlc_state_t Read GetState;
  254. Property AudioTrackDescriptions [AIndex : Integer] : String Read GetAudioTrackDescriptions;
  255. Property ChapterCount : Integer Read GetChapterCount;
  256. Property AudioTrackCount : Integer Read GetAudioTrackCount;
  257. Property AudioTrack : Integer Read GetAudioTrack Write SetAudioTrack;
  258. Property AudioDelay : Int64 Read GetAudioDelay Write SetAudioDelay;
  259. Property AudioVolume : Integer Read GetAudioVolume Write SetAudioVolume;
  260. Property AudioMuted : Boolean Read GetAudioMuted Write SetAudioMuted;
  261. Property FitWindow : Boolean Read FFitWindow Write SetFitWindow;
  262. Property VideoWidth : Cardinal Read GetVideoWidth;
  263. Property VideoHeight : Cardinal Read GetVideoHeight;
  264. // In MS.
  265. Property VideoLength : Int64 Read GetVideoLength;
  266. Property VideoDuration : TDateTime Read GetVideoDuration;
  267. // In MS
  268. Property VideoPosition : Int64 Read GetVideoPos Write SetVideoPos;
  269. Property VideoFractionalPosition : Double Read GetVideoFractional Write SetVideoFractional;
  270. Property VideoFramesPerSecond : Double Read GetVideoFPS;
  271. Property VideoScale : Double Read GetVideoScale Write SetVideoScale;
  272. Property AspectRatio : String Read GetAspectRatio Write SetAspectRatio;
  273. Property Channel : Integer Read GetChannel Write SetChannel;
  274. Property Chapter : Integer Read GetChapter Write SetChapter;
  275. Property FullScreenMode : Boolean Read GetFullScreenMode Write SetFullScreenMode;
  276. Property UseEvents : Boolean Read FUseEvents Write SetUseEVents;
  277. // Events from VLC player
  278. Property OnMediaChanged : TNotifyEvent Read FOnMediaChanged Write FOnMediaChanged;
  279. Property OnNothingSpecial : TNotifyEvent Read FOnNothingSpecial Write FOnNothingSpecial;
  280. Property OnBackward : TNotifyEvent Read FOnBackward Write FOnBackward;
  281. Property OnBuffering : TNotifyEvent Read FOnBuffering Write FOnBuffering;
  282. Property OnEOF : TNotifyEvent Read FOnEOF Write FOnEOF;
  283. Property OnError : TErrorEvent Read FOnError Write FOnError;
  284. Property OnForward : TNotifyEvent Read FOnForward Write FOnForward;
  285. Property OnOpening : TNotifyEvent Read FOnOpening Write FOnOpening;
  286. Property OnPause : TNotifyEvent Read FOnPause Write FOnPause;
  287. Property OnPlaying : TNotifyEvent Read FOnPlaying Write FOnPlaying;
  288. Property OnStop : TNotifyEvent Read FOnStop Write FOnStop;
  289. Property OnLengthChanged : TTimeEvent Read FOnLengthChanged Write FOnLengthChanged;
  290. Property OnTimeChanged : TTimeEvent Read FOnTimeChanged Write FOnTimeChanged;
  291. Property OnPausableChanged : TBooleanEvent Read FOnPausableChanged Write FOnPausableChanged;
  292. Property OnPositionChanged : TPositionEvent Read FOnPositionChanged Write FOnPositionChanged;
  293. Property OnSeekableChanged : TBooleanEvent Read FOnSeekableChanged Write FOnSeekableChanged;
  294. Property OnTitleChanged : TTitleEvent Read FOnTitleChanged Write FOnTitleChanged;
  295. Property OnSnapshot : TSnapShotEvent Read FOnSnapShot Write FOnSnapShot;
  296. end;
  297. EVLC = Class(Exception);
  298. TVLCMediaPlayer = Class(TCustomVLCMediaPlayer)
  299. Public
  300. Property Playable ;
  301. Property Pausable ;
  302. Property Seekable ;
  303. Property PLaying ;
  304. Property State ;
  305. Property AudioTrackDescriptions;
  306. Property ChapterCount ;
  307. Property AudioTrackCount ;
  308. Property AudioTrack ;
  309. Property VideoWidth ;
  310. Property VideoHeight;
  311. Property VideoLength;
  312. Property VideoDuration ;
  313. Property VideoPosition ;
  314. Property VideoFractionalPosition ;
  315. Property VideoFramesPerSecond;
  316. Property VideoScale;
  317. Property AspectRatio;
  318. Published
  319. Property AudioDelay ;
  320. Property AudioVolume ;
  321. Property AudioMuted ;
  322. Property Channel ;
  323. Property Chapter ;
  324. Property FitWindow;
  325. Property FullScreenMode ;
  326. Property UseEvents ;
  327. Property OnMediaChanged ;
  328. Property OnNothingSpecial ;
  329. Property OnBackward ;
  330. Property OnBuffering ;
  331. Property OnEOF ;
  332. Property OnError ;
  333. Property OnForward ;
  334. Property OnOpening ;
  335. Property OnPause ;
  336. Property OnPlaying ;
  337. Property OnStop ;
  338. Property OnLengthChanged ;
  339. Property OnTimeChanged ;
  340. Property OnPausableChanged ;
  341. Property OnPositionChanged ;
  342. Property OnSeekableChanged ;
  343. Property OnTitleChanged ;
  344. Property OnSnapshot ;
  345. end;
  346. { TCustomVLCMediaListPlayer }
  347. TCustomVLCMediaListPlayer = Class(TComponent)
  348. Private
  349. FMediaItems: TVLCMediaItems;
  350. FPlayer: TCustomVLCMediaPlayer;
  351. FPlayMode: TVLCPlayMode;
  352. FInstance : plibvlc_media_list_player_t;
  353. FVLC: TVLCLibrary;
  354. function GetInstance: plibvlc_media_list_player_t;
  355. function GetPlaying : Boolean;
  356. function GetState: libvlc_state_t;
  357. function GetVLC: TVLCLibrary;
  358. procedure SetMediaItems(AValue: TVLCMediaItems);
  359. procedure SetPlayer(AValue: TCustomVLCMediaPlayer); virtual;
  360. procedure SetPlayMode(AValue: TVLCPlayMode);
  361. Protected
  362. Function CreateMediaItems : TVLCMediaItems; virtual;
  363. Property Instance : plibvlc_media_list_player_t Read GetInstance;
  364. Property Player : TCustomVLCMediaPlayer Read FPlayer write SetPlayer;
  365. Property PlayMode : TVLCPlayMode read FPlayMode write SetPlayMode;
  366. Property Playing : Boolean Read GetPLaying;
  367. Property State : libvlc_state_t Read GetState;
  368. Property MediaItems : TVLCMediaItems Read FMediaItems Write SetMediaItems;
  369. Property VLC : TVLCLibrary Read GetVLC Write FVLC;
  370. Public
  371. Constructor Create(AOwner : TComponent); override;
  372. Destructor Destroy; override;
  373. procedure Play(Item : TVLCMediaItem);
  374. procedure Play;
  375. procedure Pause;
  376. procedure Stop;
  377. procedure Next;
  378. procedure Prev;
  379. end;
  380. TVLCMediaListPlayer = Class(TCustomVLCMediaListPlayer)
  381. Public
  382. Property VLC;
  383. Published
  384. Property Player;
  385. Property PlayMode;
  386. Property Playing;
  387. Property State;
  388. Property MediaItems;
  389. end;
  390. Function VLCLibrary : TVLCLibrary;
  391. Var
  392. VLCLibraryClass : TVLCLibraryClass = TVLCLibrary;
  393. Function VLCTimeToDateTime (T : libvlc_time_t) : TDateTime;
  394. implementation
  395. { TVLCLibrary }
  396. Var
  397. LVLC : TVLCLibrary;
  398. Function VLCLibrary : TVLCLibrary;
  399. begin
  400. If LVLC=Nil then
  401. LVLC:=VLCLibraryClass.Create(Nil);
  402. Result:=LVLC;
  403. end;
  404. Procedure DoneVLC;
  405. begin
  406. If Assigned(LVLC) then
  407. FreeAndNil(LVLC);
  408. end;
  409. Function VLCTimeToDateTime (T : libvlc_time_t) : TDateTime;
  410. Function MD(Var MS : libvlc_time_t; D : Integer) : Word; inline;
  411. begin
  412. Result:=MS Mod D;
  413. MS:=MS div D;
  414. end;
  415. var
  416. d,h,m,s,ms: word;
  417. begin
  418. ms:=MD(T,1000);
  419. s:=MD(T,60);
  420. m:=MD(T,60);
  421. h:=MD(T,24);
  422. d:=T;
  423. Result:=D+EncodeTime(h,m,s,ms);
  424. end;
  425. procedure PlayerEventHelper(event: Plibvlc_event_t; data: Pointer); cdecl;
  426. begin
  427. if Not Assigned(data) then
  428. exit;
  429. TCustomVLCMediaPlayer(data).HandleVLCEvent(event);
  430. end;
  431. { TCustomVLCMediaListPlayer }
  432. function TCustomVLCMediaListPlayer.GetPlaying: Boolean;
  433. begin
  434. if Assigned(FPlayer) then
  435. Result:=libvlc_media_list_player_is_playing(Instance)<>0
  436. else
  437. Result:=false;
  438. end;
  439. function TCustomVLCMediaListPlayer.GetInstance: plibvlc_media_list_player_t;
  440. begin
  441. if (FInstance=Nil) then
  442. begin
  443. Finstance:=libvlc_media_list_player_new(VLC.Instance);
  444. if Assigned(MediaItems) then
  445. begin
  446. libvlc_media_list_player_set_media_list(FInstance,MediaItems.Instance);
  447. end;
  448. If Assigned(FPlayer) then
  449. begin
  450. libvlc_media_list_player_set_media_player(FInstance, FPlayer.Instance);
  451. end;
  452. end;
  453. Result:=FInstance;
  454. end;
  455. function TCustomVLCMediaListPlayer.GetState: libvlc_state_t;
  456. begin
  457. if Assigned(FInstance) then
  458. Result:=libvlc_media_list_player_get_state(Instance)
  459. else
  460. Result:=libvlc_NothingSpecial;
  461. end;
  462. function TCustomVLCMediaListPlayer.GetVLC: TVLCLibrary;
  463. begin
  464. Result:=FVLC;
  465. If Result=Nil then
  466. Result:=VLCLibrary;
  467. end;
  468. procedure TCustomVLCMediaListPlayer.Play(Item: TVLCMediaItem);
  469. begin
  470. libvlc_media_list_player_play_item(Instance, item.Instance);
  471. end;
  472. procedure TCustomVLCMediaListPlayer.SetMediaItems(AValue: TVLCMediaItems);
  473. begin
  474. if FMediaItems=AValue then Exit;
  475. FMediaItems.Assign(AValue);
  476. end;
  477. procedure TCustomVLCMediaListPlayer.SetPlayer(AValue: TCustomVLCMediaPlayer);
  478. begin
  479. if FPlayer=AValue then Exit;
  480. FPlayer:=AValue;
  481. If Assigned(FInstance) then
  482. begin
  483. libvlc_media_list_player_set_media_player(FInstance, FPlayer.Instance);
  484. end;
  485. end;
  486. procedure TCustomVLCMediaListPlayer.SetPlayMode(AValue: TVLCPlayMode);
  487. Const
  488. M : Array [TVLCPlayMode] of libvlc_playback_mode_t
  489. = (libvlc_playback_mode_default,
  490. libvlc_playback_mode_loop,
  491. libvlc_playback_mode_repeat);
  492. begin
  493. if not Assigned(Finstance) then Exit;
  494. if FPlayMode=AValue then Exit;
  495. FPlayMode:=AValue;
  496. libvlc_media_list_player_set_playback_mode(FInstance, M[AValue]);
  497. end;
  498. function TCustomVLCMediaListPlayer.CreateMediaItems: TVLCMediaItems;
  499. begin
  500. Result:=TVLCMediaItems.Create(TVLCMediaItem);
  501. end;
  502. constructor TCustomVLCMediaListPlayer.Create(AOwner: TComponent);
  503. begin
  504. inherited Create(AOwner);
  505. FMediaItems:=CreateMediaItems;
  506. end;
  507. destructor TCustomVLCMediaListPlayer.Destroy;
  508. begin
  509. If Assigned(Finstance) then
  510. libvlc_media_list_player_release(FInstance);
  511. FreeAndNil(FMediaItems);
  512. inherited Destroy;
  513. end;
  514. procedure TCustomVLCMediaListPlayer.Play;
  515. begin
  516. libvlc_media_list_player_play(Instance);
  517. end;
  518. procedure TCustomVLCMediaListPlayer.Pause;
  519. begin
  520. libvlc_media_list_player_pause(Instance);
  521. end;
  522. procedure TCustomVLCMediaListPlayer.Stop;
  523. begin
  524. libvlc_media_list_player_stop(Instance);
  525. end;
  526. procedure TCustomVLCMediaListPlayer.Next;
  527. begin
  528. libvlc_media_list_player_next(Instance);
  529. end;
  530. procedure TCustomVLCMediaListPlayer.Prev;
  531. begin
  532. libvlc_media_list_player_previous(Instance);
  533. end;
  534. { TCustomVLCMediaPlayer }
  535. function TCustomVLCMediaPlayer.GetVLC: TVLCLibrary;
  536. begin
  537. Result:=FVLC;
  538. if Result=Nil then
  539. Result:=VLCLibrary;
  540. end;
  541. procedure TCustomVLCMediaPlayer.SetAspectRatio(AValue: String);
  542. begin
  543. libvlc_video_set_aspect_ratio(Instance,Pcchar(PChar(AValue)));
  544. end;
  545. function TCustomVLCMediaPlayer.GetAudioMuted: Boolean;
  546. begin
  547. if Assigned(Finstance) then
  548. Result:=libvlc_audio_get_mute(instance)<>0
  549. else
  550. Result:=False;
  551. end;
  552. function TCustomVLCMediaPlayer.GetAspectRatio: String;
  553. Var
  554. P : Pcchar;
  555. begin
  556. P:=libvlc_video_get_aspect_ratio(Instance);
  557. if (P<>Nil) then
  558. Result:=StrPas(PChar(P))
  559. else
  560. Result:='';
  561. end;
  562. function TCustomVLCMediaPlayer.GetAudioTrack: Integer;
  563. begin
  564. if Assigned(FInstance) then
  565. Result := libvlc_audio_get_track(FINstance)
  566. else
  567. Result:=-1;
  568. end;
  569. function TCustomVLCMediaPlayer.GetState: libvlc_state_t;
  570. begin
  571. If Assigned(FInstance) then
  572. Result:=libvlc_media_player_get_state(FInstance)
  573. else
  574. Result:=libvlc_NothingSpecial;
  575. end;
  576. function TCustomVLCMediaPlayer.GetVideoDuration: TDateTime;
  577. begin
  578. Result:=VLCTimeToDateTime(GetVideoLength);
  579. end;
  580. function TCustomVLCMediaPlayer.GetVideoFPS: Double;
  581. begin
  582. Result:=libvlc_media_player_get_fps(FInstance);
  583. end;
  584. function TCustomVLCMediaPlayer.GetVideoFractional: Double;
  585. begin
  586. Result:=libvlc_media_player_get_Position(FInstance);
  587. end;
  588. function TCustomVLCMediaPlayer.GetVideoHeight: Cardinal;
  589. begin
  590. Result:=libvlc_video_get_height(FInstance);
  591. end;
  592. function TCustomVLCMediaPlayer.GetVideoLength: Int64;
  593. begin
  594. Result:=libvlc_media_player_get_length(Finstance);
  595. end;
  596. function TCustomVLCMediaPlayer.GetVideoPos: Int64;
  597. begin
  598. Result:=libvlc_media_player_get_time(FInstance);
  599. end;
  600. function TCustomVLCMediaPlayer.GetVideoScale: Double;
  601. begin
  602. Result:=libvlc_video_get_scale(Finstance);
  603. end;
  604. function TCustomVLCMediaPlayer.GetVideoWidth: Cardinal;
  605. begin
  606. Result:=libvlc_video_get_width(FInstance);
  607. end;
  608. procedure TCustomVLCMediaPlayer.SetAudioMuted(AValue: Boolean);
  609. begin
  610. if Assigned(FInstance) then
  611. libvlc_audio_set_mute(instance, ord(AValue));
  612. end;
  613. procedure TCustomVLCMediaPlayer.SetFitWindow(AValue: Boolean);
  614. Var
  615. W,H : Cardinal;
  616. begin
  617. if FFitWindow=AValue then Exit;
  618. FFitWindow:=AValue;
  619. If FFitWindow and Playing then
  620. begin
  621. if GetVideoSize(W,H) then
  622. SetParentWindowSize(W,H);
  623. end;
  624. end;
  625. procedure TCustomVLCMediaPlayer.SetUseEVents(AValue: Boolean);
  626. begin
  627. if FUseEvents=AValue then Exit;
  628. FUseEvents:=AValue;
  629. If Assigned(Finstance) then
  630. If AValue then
  631. HookupEvents
  632. else
  633. UnhookEvents;
  634. end;
  635. function TCustomVLCMediaPlayer.GetAudioTrackCount: Integer;
  636. begin
  637. if Assigned(FInstance) then
  638. Result := libvlc_audio_get_track_count(FINstance)
  639. else
  640. Result:=-1;
  641. end;
  642. procedure TCustomVLCMediaPlayer.SetAudioTrack(AValue: Integer);
  643. begin
  644. if Assigned(FInstance) then
  645. begin
  646. if (AValue<0) then
  647. AValue:=0;
  648. libvlc_audio_set_track(FInstance,AValue);
  649. end;
  650. end;
  651. function TCustomVLCMediaPlayer.GetAudioTrackDescriptions(AIndex: Integer): String;
  652. var
  653. t : plibvlc_track_description_t;
  654. begin
  655. Result := '';
  656. If (AIndex>=0) And Assigned(FInstance) then
  657. begin
  658. T:=libvlc_audio_get_track_description(Finstance);
  659. while (AIndex>0) and Assigned(t) do
  660. begin
  661. Dec(Aindex);
  662. t:=t^.p_next;
  663. end;
  664. If Assigned(t) and Assigned(t^.psz_name) then
  665. Result:=StrPas(PChar(t^.psz_name));
  666. end;
  667. end;
  668. function TCustomVLCMediaPlayer.GetChannel: Integer;
  669. begin
  670. If Assigned(Finstance) then
  671. Result:=libvlc_audio_get_channel(FInstance)
  672. else
  673. Result:=-1;
  674. end;
  675. procedure TCustomVLCMediaPlayer.SetChannel(AValue: Integer);
  676. begin
  677. If Assigned(Finstance) then
  678. libvlc_audio_set_channel(Finstance,AValue)
  679. end;
  680. function TCustomVLCMediaPlayer.GetAudioDelay: Int64;
  681. begin
  682. if Assigned(FInstance) then
  683. Result:=libvlc_audio_get_delay(FInstance)
  684. else
  685. Result:=-1;
  686. end;
  687. procedure TCustomVLCMediaPlayer.SetAudioDelay(AValue: Int64);
  688. begin
  689. if Assigned(FInstance) then
  690. libvlc_audio_set_delay(FInstance,AValue)
  691. end;
  692. function TCustomVLCMediaPlayer.GetPlaying: Boolean;
  693. begin
  694. Result:=(State=libvlc_Playing);
  695. end;
  696. function TCustomVLCMediaPlayer.GetChapter: Integer;
  697. begin
  698. if Assigned(FInstance) then
  699. Result:=libvlc_media_player_get_chapter(FInstance)
  700. else
  701. Result:=-1;
  702. end;
  703. procedure TCustomVLCMediaPlayer.SetChapter(AValue: Integer);
  704. begin
  705. if Assigned(FInstance) then
  706. libvlc_media_player_set_chapter(FInstance,AValue);
  707. end;
  708. function TCustomVLCMediaPlayer.GetChapterCount: Integer;
  709. begin
  710. if Assigned(FInstance) then
  711. Result:=libvlc_media_player_get_chapter_count(FInstance)
  712. else
  713. Result:=-1;
  714. end;
  715. function TCustomVLCMediaPlayer.GetPlayable: Boolean;
  716. begin
  717. if Assigned(FInstance) then
  718. Result:=(libvlc_media_player_will_play(FInstance)<>0)
  719. else
  720. Result:=False
  721. end;
  722. function TCustomVLCMediaPlayer.GetPausable: Boolean;
  723. begin
  724. if Assigned(FInstance) then
  725. Result:=(libvlc_media_player_can_pause(FInstance)<>0)
  726. else
  727. Result:=False
  728. end;
  729. function TCustomVLCMediaPlayer.GetSeekable: Boolean;
  730. begin
  731. if Assigned(FInstance) then
  732. Result:=(libvlc_media_player_is_seekable(FInstance)<>0)
  733. else
  734. Result:=False
  735. end;
  736. function TCustomVLCMediaPlayer.GetAudioVolume: Integer;
  737. begin
  738. if Assigned(FInstance) then
  739. Result:=libvlc_audio_get_volume(FInstance)
  740. else
  741. Result:=-1
  742. end;
  743. procedure TCustomVLCMediaPlayer.SetAudioVolume(AValue: Integer);
  744. begin
  745. if Assigned(FInstance) then
  746. begin
  747. if (AValue<0) then
  748. AValue:=0
  749. else if (AValue>200) then
  750. AValue:=200;
  751. libvlc_audio_set_volume(Finstance,AValue);
  752. end;
  753. end;
  754. procedure TCustomVLCMediaPlayer.SetPlayRate(Avalue : Integer);
  755. begin
  756. if Assigned(FInstance) then
  757. begin
  758. if (Avalue< 1) then
  759. AValue:=1
  760. else if (AValue>1000) then
  761. AValue:=1000;
  762. libvlc_media_player_set_rate(FInstance,AValue/100);
  763. end;
  764. end;
  765. function TCustomVLCMediaPlayer.GetPlayRate: Integer;
  766. begin
  767. if Assigned(FInstance) then
  768. Result:=Round(libvlc_media_player_get_rate(FInstance)*100)
  769. else
  770. Result:=-1;
  771. end;
  772. procedure TCustomVLCMediaPlayer.SetFullScreenMode(AValue: Boolean);
  773. begin
  774. if Assigned(FInstance) then
  775. libvlc_set_fullscreen(Finstance,Ord(AValue));
  776. end;
  777. function TCustomVLCMediaPlayer.GetFullScreenMode: Boolean;
  778. begin
  779. If Assigned(FInstance) then
  780. Result:=libvlc_get_fullscreen(Finstance)<>0
  781. else
  782. Result:=False;
  783. end;
  784. procedure TCustomVLCMediaPlayer.SetVideoFractional(AValue: Double);
  785. begin
  786. libvlc_media_player_set_position(FInstance,AValue);
  787. end;
  788. procedure TCustomVLCMediaPlayer.SetVideoPos(AValue: Int64);
  789. begin
  790. libvlc_media_player_set_time(FInstance,AVAlue);
  791. end;
  792. procedure TCustomVLCMediaPlayer.SetVideoScale(AValue: Double);
  793. begin
  794. libvlc_video_set_scale(Finstance,AVAlue);
  795. end;
  796. function TCustomVLCMediaPlayer.GetInstance: Plibvlc_media_player_t;
  797. begin
  798. Result:=FInstance;
  799. if (FInstance=Nil) then
  800. begin
  801. FInstance:=libvlc_media_player_new(VLC.Instance);
  802. libvlc_video_set_mouse_input(FInstance,1);
  803. libvlc_video_set_key_input(FInstance,1);
  804. if FUseEvents then
  805. HookupEvents;
  806. end;
  807. Result:=FInstance;
  808. end;
  809. procedure TCustomVLCMediaPlayer.SetParentWindow;
  810. begin
  811. // Do nothing
  812. end;
  813. procedure TCustomVLCMediaPlayer.SetParentWindowSize(AWidth, AHeight: Cardinal);
  814. begin
  815. // Do nothing
  816. end;
  817. Procedure TCustomVLCMediaPlayer.UnHookEvents;
  818. Procedure ClearEvent(M : plibvlc_event_manager_t;t : libvlc_event_e);
  819. begin
  820. libvlc_event_detach(M,ord(t),@PlayerEventHelper,Self);
  821. end;
  822. Var
  823. M : plibvlc_event_manager_t;
  824. begin
  825. M:=libvlc_media_player_event_manager(Instance);
  826. if (M<>Nil) then
  827. begin
  828. ClearEvent(M,libvlc_MediaPlayerMediaChanged);
  829. ClearEvent(M,libvlc_MediaPlayerNothingSpecial);
  830. ClearEvent(M,libvlc_MediaPlayerOpening);
  831. ClearEvent(M,libvlc_MediaPlayerBuffering);
  832. ClearEvent(M,libvlc_MediaPlayerPlaying);
  833. ClearEvent(M,libvlc_MediaPlayerPaused);
  834. ClearEvent(M,libvlc_MediaPlayerStopped);
  835. ClearEvent(M,libvlc_MediaPlayerForward);
  836. ClearEvent(M,libvlc_MediaPlayerBackward);
  837. ClearEvent(M,libvlc_MediaPlayerEndReached);
  838. ClearEvent(M,libvlc_MediaPlayerEncounteredError);
  839. ClearEvent(M,libvlc_MediaPlayerTimeChanged);
  840. ClearEvent(M,libvlc_MediaPlayerPositionChanged);
  841. ClearEvent(M,libvlc_MediaPlayerSeekableChanged);
  842. ClearEvent(M,libvlc_MediaPlayerPausableChanged);
  843. ClearEvent(M,libvlc_MediaPlayerTitleChanged);
  844. ClearEvent(M,libvlc_MediaPlayerSnapshotTaken);
  845. ClearEvent(M,libvlc_MediaPlayerLengthChanged);
  846. FreeAndNil(FECS);
  847. end;
  848. end;
  849. Procedure TCustomVLCMediaPlayer.HookupEvents;
  850. Procedure AttachEvent( M : plibvlc_event_manager_t;t : libvlc_event_e);
  851. begin
  852. libvlc_event_attach(M,ord(t),@PlayerEventHelper,Self);
  853. end;
  854. Var
  855. M : plibvlc_event_manager_t;
  856. begin
  857. M:=libvlc_media_player_event_manager(Instance);
  858. if (M<>Nil) then
  859. begin
  860. FECS:=TCriticalSection.Create;
  861. AttachEvent(M,libvlc_MediaPlayerMediaChanged);
  862. AttachEvent(M,libvlc_MediaPlayerNothingSpecial);
  863. AttachEvent(M,libvlc_MediaPlayerOpening);
  864. AttachEvent(M,libvlc_MediaPlayerBuffering);
  865. AttachEvent(M,libvlc_MediaPlayerPlaying);
  866. AttachEvent(M,libvlc_MediaPlayerPaused);
  867. AttachEvent(M,libvlc_MediaPlayerStopped);
  868. AttachEvent(M,libvlc_MediaPlayerForward);
  869. AttachEvent(M,libvlc_MediaPlayerBackward);
  870. AttachEvent(M,libvlc_MediaPlayerEndReached);
  871. AttachEvent(M,libvlc_MediaPlayerEncounteredError);
  872. AttachEvent(M,libvlc_MediaPlayerTimeChanged);
  873. AttachEvent(M,libvlc_MediaPlayerPositionChanged);
  874. AttachEvent(M,libvlc_MediaPlayerSeekableChanged);
  875. AttachEvent(M,libvlc_MediaPlayerPausableChanged);
  876. AttachEvent(M,libvlc_MediaPlayerTitleChanged);
  877. AttachEvent(M,libvlc_MediaPlayerSnapshotTaken);
  878. AttachEvent(M,libvlc_MediaPlayerLengthChanged);
  879. end;
  880. end;
  881. procedure TCustomVLCMediaPlayer.DoMediaChanged;
  882. begin
  883. If Assigned(FOnMediaChanged) then
  884. FOnMediaChanged(Self);
  885. end;
  886. procedure TCustomVLCMediaPlayer.DoNothingSpecial;
  887. begin
  888. If Assigned(FOnNothingSpecial) then
  889. FOnNothingSpecial(Self);
  890. end;
  891. procedure TCustomVLCMediaPlayer.DoOnOpening;
  892. begin
  893. If Assigned(FOnOpening) then
  894. FOnOpening(Self);
  895. end;
  896. procedure TCustomVLCMediaPlayer.DoOnPlaying;
  897. begin
  898. If Assigned(FOnPlaying) then
  899. FOnPlaying(Self);
  900. end;
  901. procedure TCustomVLCMediaPlayer.DoOnPause;
  902. begin
  903. If Assigned(FOnPause) then
  904. FOnPause(Self);
  905. end;
  906. procedure TCustomVLCMediaPlayer.DoOnStop;
  907. begin
  908. If Assigned(FOnStop) then
  909. FOnStop(Self);
  910. end;
  911. procedure TCustomVLCMediaPlayer.DoOnForward;
  912. begin
  913. If Assigned(FOnForward) then
  914. FOnForward(Self);
  915. end;
  916. procedure TCustomVLCMediaPlayer.DoOnBackward;
  917. begin
  918. If Assigned(FOnBackward) then
  919. FOnBackward(Self);
  920. end;
  921. procedure TCustomVLCMediaPlayer.DoOnEOF;
  922. begin
  923. If Assigned(FOnEOF) then
  924. FOnEOF(Self);
  925. end;
  926. procedure TCustomVLCMediaPlayer.DoOnBuffering;
  927. begin
  928. If Assigned(FOnBuffering) then
  929. FOnBuffering(Self);
  930. end;
  931. procedure TCustomVLCMediaPlayer.DoOnError;
  932. Var
  933. P : pcchar;
  934. E : String;
  935. begin
  936. p:=libvlc_errmsg();
  937. if p<>Nil then
  938. E:=StrPas(PChar(P))
  939. else
  940. E:='';
  941. If Assigned(FOnError) then
  942. FOnError(Self,E);
  943. end;
  944. procedure TCustomVLCMediaPlayer.DoOnTimeChanged(Const ATime: libvlc_time_t);
  945. begin
  946. If Assigned(FOnTimeChanged) then
  947. FOnTimeChanged(Self,VLCTimeToDateTime(ATime));
  948. end;
  949. procedure TCustomVLCMediaPlayer.DoOnPositionChanged(Const Aposition: Double);
  950. begin
  951. If Assigned(FOnPositionChanged) then
  952. FOnPositionChanged(Self,APosition);
  953. end;
  954. procedure TCustomVLCMediaPlayer.DoOnSeekableChanged(Const ASeekable : Boolean);
  955. begin
  956. If Assigned(FOnSeekableChanged) then
  957. FOnSeekableChanged(Self,ASeekable);
  958. end;
  959. procedure TCustomVLCMediaPlayer.DoOnPausableChanged(Const APausable : Boolean);
  960. begin
  961. If Assigned(FOnPausableChanged) then
  962. FOnPausableChanged(Self,APausable);
  963. end;
  964. procedure TCustomVLCMediaPlayer.DoOnTitleChanged(Const ATitle: cint);
  965. begin
  966. If Assigned(FOnTitleChanged) then
  967. FOnTitleChanged(Self,ATitle);
  968. end;
  969. procedure TCustomVLCMediaPlayer.DoOnSnapshot(Const AFileName : PCChar);
  970. Var
  971. S :String;
  972. begin
  973. If Assigned(FOnSnapshot) then
  974. begin
  975. if Assigned(AFileName) then
  976. S:=StrPas(PChar(AFileName))
  977. else
  978. S:='';
  979. FOnSnapShot(Self,S);
  980. end;
  981. end;
  982. procedure TCustomVLCMediaPlayer.DoOnLengthChanged(Const ATime: libvlc_time_t);
  983. begin
  984. If Assigned(FOnLengtHChanged) then
  985. FOnLengtHChanged(Self,VLCTimeToDateTime(ATime));
  986. end;
  987. procedure TCustomVLCMediaPlayer.HandleVLCEvent(e: Plibvlc_event_t);
  988. begin
  989. FECS.Enter;
  990. try
  991. case libvlc_event_e(e^._type) of
  992. libvlc_MediaPlayerMediaChanged : DoMediaChanged;
  993. libvlc_MediaPlayerNothingSpecial : DoNothingSpecial;
  994. libvlc_MediaPlayerOpening : DoOnOpening;
  995. libvlc_MediaPlayerBuffering : DoOnBuffering;
  996. libvlc_MediaPlayerPlaying : DoOnPlaying;
  997. libvlc_MediaPlayerPaused : DoOnPause;
  998. libvlc_MediaPlayerStopped : DoOnStop;
  999. libvlc_MediaPlayerForward : DoOnForward;
  1000. libvlc_MediaPlayerBackward : DoOnBackward;
  1001. libvlc_MediaPlayerEndReached : DoOnEOF;
  1002. libvlc_MediaPlayerEncounteredError : DoOnError;
  1003. libvlc_MediaPlayerTimeChanged : begin
  1004. DoOnTimeChanged(e^.media_player_time_changed.new_time);
  1005. end;
  1006. libvlc_MediaPlayerPositionChanged : begin
  1007. DoOnPositionChanged(e^.media_player_position_changed.new_position);
  1008. end;
  1009. libvlc_MediaPlayerSeekableChanged : begin
  1010. DoOnSeekableChanged(e^.media_player_seekable_changed.new_seekable<>0);
  1011. end;
  1012. libvlc_MediaPlayerPausableChanged : begin
  1013. DoOnPausableChanged(e^.media_player_pausable_changed.new_pausable<>0) ;
  1014. end;
  1015. libvlc_MediaPlayerTitleChanged : begin
  1016. DoOnTitleChanged(e^.media_player_title_changed.new_title);
  1017. end;
  1018. libvlc_MediaPlayerSnapshotTaken : begin
  1019. DoOnSnapShot(e^.media_player_snapshot_taken.psz_filename);
  1020. end;
  1021. libvlc_MediaPlayerLengthChanged : begin
  1022. DoOnLengthChanged(e^.media_player_length_changed.new_length);
  1023. end;
  1024. else
  1025. // Not sure what to do here.
  1026. // Writeln('Unknown event type ',e^._type);
  1027. end;
  1028. finally
  1029. FECS.Leave;
  1030. end;
  1031. end;
  1032. destructor TCustomVLCMediaPlayer.Destroy;
  1033. begin
  1034. If Assigned(FInstance) then
  1035. begin
  1036. libvlc_media_player_release(FInstance);
  1037. FInstance:=Nil;
  1038. end;
  1039. FreeAndNil(FECS);
  1040. inherited Destroy;
  1041. end;
  1042. procedure TCustomVLCMediaPlayer.SetMedia(M: TVLCMediaItem);
  1043. begin
  1044. libvlc_media_player_set_media(Instance,M.Instance);
  1045. end;
  1046. procedure TCustomVLCMediaPlayer.Play;
  1047. Var
  1048. W,H : Cardinal;
  1049. begin
  1050. SetParentWindow;
  1051. libvlc_media_player_play(Instance);
  1052. If FitWindow then
  1053. begin
  1054. VideoScale:=1.0;
  1055. if GetVideoSize(W,H) then
  1056. SetParentWindowSize(W,H);
  1057. end;
  1058. end;
  1059. procedure TCustomVLCMediaPlayer.Play(M: TVLCMediaItem);
  1060. begin
  1061. if Playing then
  1062. begin
  1063. Stop;
  1064. While Playing do
  1065. Sleep(100);
  1066. end;
  1067. SetMedia(M);
  1068. Play;
  1069. end;
  1070. procedure TCustomVLCMediaPlayer.PlayFile(const AFileName: String);
  1071. Var
  1072. M : TVLCMediaItem;
  1073. begin
  1074. M:=TVLCMediaItem.Create(Nil);
  1075. try
  1076. M.Path:=AFileName;
  1077. Play(M);
  1078. finally
  1079. M.Free;
  1080. end;
  1081. end;
  1082. procedure TCustomVLCMediaPlayer.Stop;
  1083. begin
  1084. if Assigned(FInstance) then
  1085. libvlc_media_player_stop(FInstance);
  1086. end;
  1087. procedure TCustomVLCMediaPlayer.Pause;
  1088. begin
  1089. if Assigned(FInstance) then
  1090. libvlc_media_player_pause(FInstance);
  1091. end;
  1092. procedure TCustomVLCMediaPlayer.Resume;
  1093. begin
  1094. if (GetState()=libvlc_Paused) then
  1095. if Assigned(FInstance) then
  1096. libvlc_media_player_play(FInstance);
  1097. end;
  1098. procedure TCustomVLCMediaPlayer.NextFrame;
  1099. begin
  1100. if Assigned(FInstance) then
  1101. libvlc_media_player_next_frame(Finstance);
  1102. end;
  1103. function TCustomVLCMediaPlayer.Snapshot(const AFileName: String): Boolean;
  1104. var
  1105. w,h : Cardinal;
  1106. begin
  1107. Result:=Assigned(FInstance);
  1108. if Result then
  1109. begin
  1110. w:=0;
  1111. h:=0;
  1112. Result:=libvlc_video_get_size(FInstance,0,@W,@H)=0;
  1113. if Result then
  1114. Result:=SnapShot(AFileName,W,H);
  1115. end;
  1116. end;
  1117. function TCustomVLCMediaPlayer.Snapshot(const AFileName: String; AWidth,
  1118. AHeight: Cardinal): Boolean;
  1119. begin
  1120. Result:=Assigned(FInstance);
  1121. If Result then
  1122. Result:=libvlc_video_take_snapshot(FInstance,0,PCChar(PChar(AFileName)),AWidth,AHeight)=0;
  1123. end;
  1124. function TCustomVLCMediaPlayer.GetVideoSize(var AWidth, AHeight: Cardinal
  1125. ): Boolean;
  1126. begin
  1127. Result:=libvlc_video_get_size(FInstance,0,@AWidth,@AHeight)=0;
  1128. end;
  1129. { TVLCMediaItems }
  1130. constructor TVLCMediaItems.Create(ALibrary: TVLCLibrary;AItemClass: TVLCMediaItemClass = Nil);
  1131. begin
  1132. Inherited Create(AItemClass);
  1133. FVLC:=ALibrary;
  1134. end;
  1135. constructor TVLCMediaItems.Create(AInstance: Plibvlc_media_list_t;
  1136. AItemClass: TVLCMediaItemClass);
  1137. Var
  1138. I : Integer;
  1139. P : plibvlc_media_t;
  1140. begin
  1141. Inherited Create(AItemClass);
  1142. FInstance:=AInstance;
  1143. For I:=0 to libvlc_media_list_count(FInstance)-1 do
  1144. begin
  1145. P:=libvlc_media_list_item_at_index(FInstance,I);
  1146. (Add as TVLCMediaItem).SetInstance(P);
  1147. end;
  1148. end;
  1149. procedure TVLCMediaItems.Lock;
  1150. begin
  1151. libvlc_media_list_lock(FInstance);
  1152. end;
  1153. procedure TVLCMediaItems.Unlock;
  1154. begin
  1155. libvlc_media_list_lock(FInstance);
  1156. end;
  1157. function TVLCMediaItems.GetInstance: Plibvlc_media_list_t;
  1158. Var
  1159. I :integer;
  1160. begin
  1161. if FInstance=Nil then
  1162. begin
  1163. FInstance:=libvlc_media_list_new(VLC.Instance);
  1164. For I:=0 to Count-1 do
  1165. GetI(I).RegisterInstance;
  1166. end;
  1167. Result:=Finstance;
  1168. end;
  1169. function TVLCMediaItems.GetIsReadOnly: Boolean;
  1170. begin
  1171. Result:=libvlc_media_list_is_readonly(FInstance)<>0;
  1172. end;
  1173. function TVLCMediaItems.GetI(AIndex : Integer): TVLCMediaItem;
  1174. begin
  1175. Result:=Items[AIndex] as TVLCMediaItem;
  1176. end;
  1177. procedure TVLCMediaItems.SetI(AIndex : Integer; AValue: TVLCMediaItem);
  1178. begin
  1179. Items[AIndex]:=AValue;
  1180. end;
  1181. function TVLCMediaItems.GetVLC: TVLCLibrary;
  1182. begin
  1183. Result:=VLCLibrary;
  1184. end;
  1185. { TVLCMediaItem }
  1186. function TVLCMediaItem.GetInstance: plibvlc_media_t;
  1187. begin
  1188. Result:=Finstance;
  1189. If (Result=Nil) then
  1190. Raise EVLC.Create('No instance available at this time. Set MRL, Path or FileDescriptor first');
  1191. end;
  1192. function TVLCMediaItem.GetM(AIndex: Integer): Boolean;
  1193. begin
  1194. Result:=FOpts[AIndex];
  1195. end;
  1196. function TVLCMediaItem.GetMD(AMeta : libvlc_meta_t): String;
  1197. Var
  1198. P : PCChar;
  1199. begin
  1200. P:=libvlc_media_get_meta(Instance,AMeta);
  1201. if (P<>Nil) then
  1202. Result:=StrPas(PChar(p))
  1203. else
  1204. Result:='';
  1205. end;
  1206. function TVLCMediaItem.GetMRL: String;
  1207. Var
  1208. P : PCChar;
  1209. begin
  1210. P:=libvlc_media_get_mrl(Instance);
  1211. if (P<>Nil) then
  1212. Result:=StrPas(PChar(p))
  1213. else
  1214. Result:='';
  1215. end;
  1216. function TVLCMediaItem.GetParsed: Boolean;
  1217. begin
  1218. Result:=libvlc_media_is_parsed(Instance)<>0;
  1219. end;
  1220. function TVLCMediaItem.GetUD: Pointer;
  1221. begin
  1222. Result:=libvlc_media_get_user_data(Instance);
  1223. end;
  1224. procedure TVLCMediaItem.SetDIM(AValue: TDeinterlaceMode);
  1225. Const
  1226. DMS : Array[TDeinterlaceMode] of string
  1227. = ('blend', 'discard', 'bob', 'linear', 'mean', 'x', 'yadif', 'yadif2x');
  1228. begin
  1229. if (FDIM=AValue) then Exit;
  1230. FDIM:=AValue;
  1231. libvlc_media_add_option(Instance, PCChar(PChar('deinterlace-mode='+DMS[AValue])));
  1232. end;
  1233. procedure TVLCMediaItem.SetFD(AValue: Integer);
  1234. begin
  1235. FFD:=AValue;
  1236. Finstance:=libvlc_media_new_fd(GetVLC.Instance,AValue);
  1237. If (FInstance=Nil) then
  1238. Raise EVLC.CreateFmt('Failed to create media item from file descriptor "%d"',[AValue]);
  1239. RegisterInstance;
  1240. end;
  1241. procedure TVLCMediaItem.SetFSS(AValue: TSNapshotFormat);
  1242. Const
  1243. ssfs : Array[TSnapShotFormat] of string = ('png','jpg');
  1244. begin
  1245. if FSS=AValue then Exit;
  1246. FSS:=AValue;
  1247. libvlc_media_add_option(Instance, PCChar(PChar('no-snapshot-preview')));
  1248. libvlc_media_add_option(instance, PCChar(PChar('snapshot-format=' + SSFS[aValue])));
  1249. end;
  1250. procedure TVLCMediaItem.SetM(AIndex: Integer; AValue: Boolean);
  1251. begin
  1252. FOpts[AIndex]:=AValue;
  1253. libvlc_media_add_option(FInstance,PcChar(PChar(GetBoolOpt(AIndex,AValue))));
  1254. end;
  1255. function TVLCMediaItem.GetBoolOpt(AIndex: Integer; AValue: Boolean): String;
  1256. begin
  1257. Case AINdex of
  1258. 0 : Result:='video-title-show';
  1259. 1 : Result:='video-on-top';
  1260. 2 : Result:='overlay';
  1261. 3 : Result:='fullscreen';
  1262. 4 : Result:='deinterlace='+IntToStr(Ord(AValue));
  1263. end;
  1264. if (AIndex < 4) and Not AValue then
  1265. Result:='no-'+Result;
  1266. end;
  1267. procedure TVLCMediaItem.SetMD(AMeta : libvlc_meta_t; AValue: String);
  1268. begin
  1269. libvlc_media_set_meta(Instance,AMeta,Pcchar(PChar(AValue)));
  1270. end;
  1271. procedure TVLCMediaItem.SetMRL(AValue: String);
  1272. begin
  1273. Finstance:=libvlc_media_new_location(GetVLC.Instance,PCChar(AValue));
  1274. If (FInstance=Nil) then
  1275. Raise EVLC.CreateFmt('Failed to create media item from MRL : "%s"',[AValue]);
  1276. RegisterInstance;
  1277. end;
  1278. procedure TVLCMediaItem.SetPath(AValue: String);
  1279. begin
  1280. if FPath=AValue then Exit;
  1281. FPath:=AValue;
  1282. FInstance:=libvlc_media_new_path(GetVLC.Instance,PCChar(AValue));
  1283. if (FInstance=Nil) then
  1284. Raise EVLC.CreateFmt('Failed to create media item from path : "%s"',[AValue]);
  1285. RegisterInstance;
  1286. end;
  1287. procedure TVLCMediaItem.SetUD(AValue: Pointer);
  1288. begin
  1289. libvlc_media_set_user_data(Instance,AValue);
  1290. end;
  1291. function TVLCMediaItem.GetState: libvlc_state_t;
  1292. begin
  1293. Result:=libvlc_media_get_state(instance);
  1294. end;
  1295. function TVLCMediaItem.GetDuration: TDateTime;
  1296. Var
  1297. d : libvlc_time_t;
  1298. begin
  1299. d:=libvlc_media_get_duration(Instance);
  1300. Result:=D
  1301. end;
  1302. procedure TVLCMediaItem.RegisterInstance;
  1303. Var
  1304. L : Plibvlc_media_list_t;
  1305. begin
  1306. If Assigned(Collection) and (Collection is TVLCMediaItems) then
  1307. begin
  1308. L:=TVLCMediaItems(Collection).FInstance;
  1309. if (L<>Nil) then
  1310. begin
  1311. libvlc_media_list_lock(L);
  1312. libvlc_media_list_add_media(L, FInstance);
  1313. libvlc_media_list_unlock(L);
  1314. end;
  1315. end;
  1316. end;
  1317. procedure TVLCMediaItem.UnRegisterInstance;
  1318. Var
  1319. L : Plibvlc_media_list_t;
  1320. i : integer;
  1321. begin
  1322. If Assigned(Collection) and (Collection is TVLCMediaItems) then
  1323. begin
  1324. L:=TVLCMediaItems(Collection).FInstance;
  1325. if L<>Nil then
  1326. begin
  1327. libvlc_media_list_lock(L);
  1328. I:=libvlc_media_list_index_of_item(L,Finstance);
  1329. if (i>=0) then
  1330. libvlc_media_list_remove_index(L,i);
  1331. libvlc_media_list_unlock(L);
  1332. end;
  1333. end;
  1334. end;
  1335. function TVLCMediaItem.GetVLC: TVLCLibrary;
  1336. begin
  1337. If Assigned(Collection) and (Collection is TVLCMediaItems) then
  1338. Result:=TVLCMediaItems(Collection).GetVLC
  1339. else
  1340. Result:=VLCLibrary;
  1341. if not Result.Initialized then
  1342. Result.Initialize;
  1343. end;
  1344. function TVLCMediaItem.GetEventManager: plibvlc_event_manager_t;
  1345. begin
  1346. Result:=libvlc_media_event_manager(Instance);
  1347. end;
  1348. procedure TVLCMediaItem.SetInstance(Avalue: plibvlc_media_t);
  1349. begin
  1350. FInstance:=AValue;
  1351. end;
  1352. destructor TVLCMediaItem.Destroy;
  1353. begin
  1354. inherited Destroy;
  1355. if Assigned(FInstance) then
  1356. begin
  1357. UnregisterInstance;
  1358. libvlc_media_release(FInstance);
  1359. FInstance:=Nil;
  1360. end;
  1361. end;
  1362. procedure TVLCMediaItem.AddOption(const AValue: String);
  1363. begin
  1364. libvlc_media_add_option(Instance,PCChar(PChar(AValue)));
  1365. end;
  1366. procedure TVLCMediaItem.Parse;
  1367. begin
  1368. libvlc_media_parse(Instance);
  1369. end;
  1370. procedure TVLCMediaItem.ParseAsync;
  1371. begin
  1372. libvlc_media_parse_async(Instance);
  1373. end;
  1374. procedure TVLCMediaItem.SaveMetaData;
  1375. begin
  1376. libvlc_media_save_meta(Instance);
  1377. end;
  1378. function TVLCMediaItem.GetStats(var AStats: libvlc_media_stats_t): Boolean;
  1379. begin
  1380. Result:=libvlc_media_get_stats(Instance,@AStats)<>0;
  1381. end;
  1382. function TVLCMediaItem.Duplicate: TVLCMediaItem;
  1383. begin
  1384. If Assigned(Collection) and (Collection is TVLCMediaItems) then
  1385. Result:=TVLCMediaItems(Collection).Add as TVLCMediaItem
  1386. else
  1387. Result:=TVLCMediaItem.Create(Nil);
  1388. Result.SetInstance(libvlc_media_duplicate(Instance));
  1389. end;
  1390. function TVLCLibrary.GetLastError: String;
  1391. Var
  1392. P : PCChar;
  1393. begin
  1394. P:=libvlc_errmsg();
  1395. if Assigned(P) then
  1396. Result := StrPas(PChar(P))
  1397. else
  1398. Result:='';
  1399. end;
  1400. function TVLCLibrary.GetI: Boolean;
  1401. begin
  1402. Result:=FInstance<>Nil;
  1403. end;
  1404. function TVLCLibrary.GetVersion: String;
  1405. Var
  1406. P : PCChar;
  1407. begin
  1408. P:=libvlc_get_version();
  1409. if Assigned(P) then
  1410. Result := StrPas(PChar(P))
  1411. else
  1412. Result:='';
  1413. end;
  1414. function TVLCLibrary.GetCompiler: String;
  1415. Var
  1416. P : PCChar;
  1417. begin
  1418. P:=libvlc_get_compiler();
  1419. if Assigned(P) then
  1420. Result := StrPas(PChar(P))
  1421. else
  1422. Result:='';
  1423. end;
  1424. function TVLCLibrary.GetChangeSet: String;
  1425. Var
  1426. P : PCChar;
  1427. begin
  1428. P:=libvlc_get_changeset();
  1429. if Assigned(P) then
  1430. Result := StrPas(PChar(P))
  1431. else
  1432. Result:='';
  1433. end;
  1434. procedure TVLCLibrary.SetLibraryPath(const AValue: String);
  1435. begin
  1436. If AValue=FLibraryPath then exit;
  1437. If Assigned(FInstance) then
  1438. Raise EVLC.Create('VLC already initialized, cannot set librarypath');
  1439. FLibraryPath:=AVAlue;
  1440. end;
  1441. function TVLCLibrary.GetInstance: plibvlc_instance_t;
  1442. var
  1443. args: Array of AnsiString;
  1444. cargs : array of PAnsiChar;
  1445. argc,
  1446. I : integer;
  1447. begin
  1448. If (FInstance=Nil) then
  1449. begin
  1450. LibraryArgs.add('--no-video-title-show');
  1451. SetLength(cArgs,LibraryArgs.Count+2);
  1452. SetLength(Args,LibraryArgs.Count+1);
  1453. cargs[0] := PChar(FLibraryPath);
  1454. For I:=0 to LibraryArgs.Count-1 do
  1455. begin
  1456. Args[i]:=LibraryArgs[i];
  1457. CArgs[i+1]:=PChar(Args[i]);
  1458. end;
  1459. argc:=Length(CArgs);
  1460. cargs[argc-1] := NIL;
  1461. FInstance := libvlc_new(argc-1, PPcchar(cargs));
  1462. if (FInstance=Nil) then
  1463. Raise EVLC.Create('Could not create instance of libvlc');
  1464. end;
  1465. Result:=FInstance;
  1466. end;
  1467. constructor TVLCLibrary.Create(AOwner: TComponent);
  1468. begin
  1469. Inherited;
  1470. FInstance:=Nil;
  1471. FLibraryPath:=LibName;
  1472. FLibraryArgs:=TStringList.Create;
  1473. end;
  1474. destructor TVLCLibrary.Destroy;
  1475. begin
  1476. FreeAndNil(FLibraryArgs);
  1477. Release;
  1478. inherited Destroy;
  1479. end;
  1480. procedure TVLCLibrary.Initialize;
  1481. begin
  1482. LoadLibVLC(LibraryPath,False);
  1483. GetInstance;
  1484. end;
  1485. procedure TVLCLibrary.Release;
  1486. begin
  1487. If (FInstance<>Nil) then
  1488. begin
  1489. libvlc_release(FInstance);
  1490. FreeLibVLC;
  1491. end;
  1492. FInstance:=Nil;
  1493. end;
  1494. Initialization
  1495. Finalization
  1496. DoneVLC;
  1497. end.