vlc.pp 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754
  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. Result:=libvlc_media_list_player_is_playing(Instance)<>0;
  435. end;
  436. function TCustomVLCMediaListPlayer.GetInstance: plibvlc_media_list_player_t;
  437. begin
  438. if (FInstance=Nil) then
  439. begin
  440. Finstance:=libvlc_media_list_player_new(VLC.Instance);
  441. if Assigned(MediaItems) then
  442. begin
  443. libvlc_media_list_player_set_media_list(FInstance,MediaItems.Instance);
  444. end;
  445. If Assigned(FPlayer) then
  446. begin
  447. libvlc_media_list_player_set_media_player(FInstance, FPlayer.Instance);
  448. end;
  449. end;
  450. Result:=FInstance;
  451. end;
  452. function TCustomVLCMediaListPlayer.GetState: libvlc_state_t;
  453. begin
  454. Result:=libvlc_media_list_player_get_state(Instance)
  455. end;
  456. function TCustomVLCMediaListPlayer.GetVLC: TVLCLibrary;
  457. begin
  458. Result:=FVLC;
  459. If Result=Nil then
  460. Result:=VLCLibrary;
  461. end;
  462. procedure TCustomVLCMediaListPlayer.Play(Item: TVLCMediaItem);
  463. begin
  464. libvlc_media_list_player_play_item(Instance, item.Instance);
  465. end;
  466. procedure TCustomVLCMediaListPlayer.SetMediaItems(AValue: TVLCMediaItems);
  467. begin
  468. if FMediaItems=AValue then Exit;
  469. FMediaItems.Assign(AValue);
  470. end;
  471. procedure TCustomVLCMediaListPlayer.SetPlayer(AValue: TCustomVLCMediaPlayer);
  472. begin
  473. if FPlayer=AValue then Exit;
  474. FPlayer:=AValue;
  475. If Assigned(FInstance) then
  476. begin
  477. libvlc_media_list_player_set_media_player(FInstance, FPlayer.Instance);
  478. end;
  479. end;
  480. procedure TCustomVLCMediaListPlayer.SetPlayMode(AValue: TVLCPlayMode);
  481. Const
  482. M : Array [TVLCPlayMode] of libvlc_playback_mode_t
  483. = (libvlc_playback_mode_default,
  484. libvlc_playback_mode_loop,
  485. libvlc_playback_mode_repeat);
  486. begin
  487. if FPlayMode=AValue then Exit;
  488. FPlayMode:=AValue;
  489. libvlc_media_list_player_set_playback_mode(FInstance, M[AValue]);
  490. end;
  491. function TCustomVLCMediaListPlayer.CreateMediaItems: TVLCMediaItems;
  492. begin
  493. Result:=TVLCMediaItems.Create(TVLCMediaItem);
  494. end;
  495. constructor TCustomVLCMediaListPlayer.Create(AOwner: TComponent);
  496. begin
  497. inherited Create(AOwner);
  498. FMediaItems:=CreateMediaItems;
  499. end;
  500. destructor TCustomVLCMediaListPlayer.Destroy;
  501. begin
  502. If Assigned(Finstance) then
  503. libvlc_media_list_player_release(FInstance);
  504. FreeAndNil(FMediaItems);
  505. inherited Destroy;
  506. end;
  507. procedure TCustomVLCMediaListPlayer.Play;
  508. begin
  509. libvlc_media_list_player_play(Instance);
  510. end;
  511. procedure TCustomVLCMediaListPlayer.Pause;
  512. begin
  513. libvlc_media_list_player_pause(Instance);
  514. end;
  515. procedure TCustomVLCMediaListPlayer.Stop;
  516. begin
  517. libvlc_media_list_player_stop(Instance);
  518. end;
  519. procedure TCustomVLCMediaListPlayer.Next;
  520. begin
  521. libvlc_media_list_player_next(Instance);
  522. end;
  523. procedure TCustomVLCMediaListPlayer.Prev;
  524. begin
  525. libvlc_media_list_player_previous(Instance);
  526. end;
  527. { TCustomVLCMediaPlayer }
  528. function TCustomVLCMediaPlayer.GetVLC: TVLCLibrary;
  529. begin
  530. Result:=FVLC;
  531. if Result=Nil then
  532. Result:=VLCLibrary;
  533. end;
  534. procedure TCustomVLCMediaPlayer.SetAspectRatio(AValue: String);
  535. begin
  536. libvlc_video_set_aspect_ratio(Instance,Pcchar(PChar(AValue)));
  537. end;
  538. function TCustomVLCMediaPlayer.GetAudioMuted: Boolean;
  539. begin
  540. if Assigned(Finstance) then
  541. Result:=libvlc_audio_get_mute(instance)<>0
  542. else
  543. Result:=False;
  544. end;
  545. function TCustomVLCMediaPlayer.GetAspectRatio: String;
  546. Var
  547. P : Pcchar;
  548. begin
  549. P:=libvlc_video_get_aspect_ratio(Instance);
  550. if (P<>Nil) then
  551. Result:=StrPas(PChar(P))
  552. else
  553. Result:='';
  554. end;
  555. function TCustomVLCMediaPlayer.GetAudioTrack: Integer;
  556. begin
  557. if Assigned(FInstance) then
  558. Result := libvlc_audio_get_track(FINstance)
  559. else
  560. Result:=-1;
  561. end;
  562. function TCustomVLCMediaPlayer.GetState: libvlc_state_t;
  563. begin
  564. If Assigned(FInstance) then
  565. Result:=libvlc_media_player_get_state(FInstance)
  566. else
  567. Result:=libvlc_NothingSpecial;
  568. end;
  569. function TCustomVLCMediaPlayer.GetVideoDuration: TDateTime;
  570. begin
  571. Result:=VLCTimeToDateTime(GetVideoLength);
  572. end;
  573. function TCustomVLCMediaPlayer.GetVideoFPS: Double;
  574. begin
  575. Result:=libvlc_media_player_get_fps(FInstance);
  576. end;
  577. function TCustomVLCMediaPlayer.GetVideoFractional: Double;
  578. begin
  579. Result:=libvlc_media_player_get_Position(FInstance);
  580. end;
  581. function TCustomVLCMediaPlayer.GetVideoHeight: Cardinal;
  582. begin
  583. Result:=libvlc_video_get_height(FInstance);
  584. end;
  585. function TCustomVLCMediaPlayer.GetVideoLength: Int64;
  586. begin
  587. Result:=libvlc_media_player_get_length(Finstance);
  588. end;
  589. function TCustomVLCMediaPlayer.GetVideoPos: Int64;
  590. begin
  591. Result:=libvlc_media_player_get_time(FInstance);
  592. end;
  593. function TCustomVLCMediaPlayer.GetVideoScale: Double;
  594. begin
  595. Result:=libvlc_video_get_scale(Finstance);
  596. end;
  597. function TCustomVLCMediaPlayer.GetVideoWidth: Cardinal;
  598. begin
  599. Result:=libvlc_video_get_width(FInstance);
  600. end;
  601. procedure TCustomVLCMediaPlayer.SetAudioMuted(AValue: Boolean);
  602. begin
  603. libvlc_audio_set_mute(instance, ord(AValue));
  604. end;
  605. procedure TCustomVLCMediaPlayer.SetFitWindow(AValue: Boolean);
  606. Var
  607. W,H : Cardinal;
  608. begin
  609. if FFitWindow=AValue then Exit;
  610. FFitWindow:=AValue;
  611. If FFitWindow and Playing then
  612. begin
  613. if GetVideoSize(W,H) then
  614. SetParentWindowSize(W,H);
  615. end;
  616. end;
  617. procedure TCustomVLCMediaPlayer.SetUseEVents(AValue: Boolean);
  618. begin
  619. if FUseEvents=AValue then Exit;
  620. FUseEvents:=AValue;
  621. If Assigned(Finstance) then
  622. If AValue then
  623. HookupEvents
  624. else
  625. UnhookEvents;
  626. end;
  627. function TCustomVLCMediaPlayer.GetAudioTrackCount: Integer;
  628. begin
  629. if Assigned(FInstance) then
  630. Result := libvlc_audio_get_track_count(FINstance)
  631. else
  632. Result:=-1;
  633. end;
  634. procedure TCustomVLCMediaPlayer.SetAudioTrack(AValue: Integer);
  635. begin
  636. if Assigned(FInstance) then
  637. begin
  638. if (AValue<0) then
  639. AValue:=0;
  640. libvlc_audio_set_track(FInstance,AValue);
  641. end;
  642. end;
  643. function TCustomVLCMediaPlayer.GetAudioTrackDescriptions(AIndex: Integer): String;
  644. var
  645. t : plibvlc_track_description_t;
  646. begin
  647. Result := '';
  648. If (AIndex>=0) And Assigned(FInstance) then
  649. begin
  650. T:=libvlc_audio_get_track_description(Finstance);
  651. while (AIndex>0) and Assigned(t) do
  652. begin
  653. Dec(Aindex);
  654. t:=t^.p_next;
  655. end;
  656. If Assigned(t) and Assigned(t^.psz_name) then
  657. Result:=StrPas(PChar(t^.psz_name));
  658. end;
  659. end;
  660. function TCustomVLCMediaPlayer.GetChannel: Integer;
  661. begin
  662. If Assigned(Finstance) then
  663. Result:=libvlc_audio_get_channel(FInstance)
  664. else
  665. Result:=-1;
  666. end;
  667. procedure TCustomVLCMediaPlayer.SetChannel(AValue: Integer);
  668. begin
  669. If Assigned(Finstance) then
  670. libvlc_audio_set_channel(Finstance,AValue)
  671. end;
  672. function TCustomVLCMediaPlayer.GetAudioDelay: Int64;
  673. begin
  674. if Assigned(FInstance) then
  675. Result:=libvlc_audio_get_delay(FInstance)
  676. else
  677. Result:=-1;
  678. end;
  679. procedure TCustomVLCMediaPlayer.SetAudioDelay(AValue: Int64);
  680. begin
  681. if Assigned(FInstance) then
  682. libvlc_audio_set_delay(FInstance,AValue)
  683. end;
  684. function TCustomVLCMediaPlayer.GetPlaying: Boolean;
  685. begin
  686. Result:=(State=libvlc_Playing);
  687. end;
  688. function TCustomVLCMediaPlayer.GetChapter: Integer;
  689. begin
  690. if Assigned(FInstance) then
  691. Result:=libvlc_media_player_get_chapter(FInstance)
  692. else
  693. Result:=-1;
  694. end;
  695. procedure TCustomVLCMediaPlayer.SetChapter(AValue: Integer);
  696. begin
  697. if Assigned(FInstance) then
  698. libvlc_media_player_set_chapter(FInstance,AValue);
  699. end;
  700. function TCustomVLCMediaPlayer.GetChapterCount: Integer;
  701. begin
  702. if Assigned(FInstance) then
  703. Result:=libvlc_media_player_get_chapter_count(FInstance)
  704. else
  705. Result:=-1;
  706. end;
  707. function TCustomVLCMediaPlayer.GetPlayable: Boolean;
  708. begin
  709. if Assigned(FInstance) then
  710. Result:=(libvlc_media_player_will_play(FInstance)<>0)
  711. else
  712. Result:=False
  713. end;
  714. function TCustomVLCMediaPlayer.GetPausable: Boolean;
  715. begin
  716. if Assigned(FInstance) then
  717. Result:=(libvlc_media_player_can_pause(FInstance)<>0)
  718. else
  719. Result:=False
  720. end;
  721. function TCustomVLCMediaPlayer.GetSeekable: Boolean;
  722. begin
  723. if Assigned(FInstance) then
  724. Result:=(libvlc_media_player_is_seekable(FInstance)<>0)
  725. else
  726. Result:=False
  727. end;
  728. function TCustomVLCMediaPlayer.GetAudioVolume: Integer;
  729. begin
  730. if Assigned(FInstance) then
  731. Result:=libvlc_audio_get_volume(FInstance)
  732. else
  733. Result:=-1
  734. end;
  735. procedure TCustomVLCMediaPlayer.SetAudioVolume(AValue: Integer);
  736. begin
  737. if Assigned(FInstance) then
  738. begin
  739. if (AValue<0) then
  740. AValue:=0
  741. else if (AValue>200) then
  742. AValue:=200;
  743. libvlc_audio_set_volume(Finstance,AValue);
  744. end;
  745. end;
  746. procedure TCustomVLCMediaPlayer.SetPlayRate(Avalue : Integer);
  747. begin
  748. if Assigned(FInstance) then
  749. begin
  750. if (Avalue< 1) then
  751. AValue:=1
  752. else if (AValue>1000) then
  753. AValue:=1000;
  754. libvlc_media_player_set_rate(FInstance,AValue/100);
  755. end;
  756. end;
  757. function TCustomVLCMediaPlayer.GetPlayRate: Integer;
  758. begin
  759. if Assigned(FInstance) then
  760. Result:=Round(libvlc_media_player_get_rate(FInstance)*100)
  761. else
  762. Result:=-1;
  763. end;
  764. procedure TCustomVLCMediaPlayer.SetFullScreenMode(AValue: Boolean);
  765. begin
  766. if Assigned(FInstance) then
  767. libvlc_set_fullscreen(Finstance,Ord(AValue));
  768. end;
  769. function TCustomVLCMediaPlayer.GetFullScreenMode: Boolean;
  770. begin
  771. If Assigned(FInstance) then
  772. Result:=libvlc_get_fullscreen(Finstance)<>0
  773. else
  774. Result:=False;
  775. end;
  776. procedure TCustomVLCMediaPlayer.SetVideoFractional(AValue: Double);
  777. begin
  778. libvlc_media_player_set_position(FInstance,AValue);
  779. end;
  780. procedure TCustomVLCMediaPlayer.SetVideoPos(AValue: Int64);
  781. begin
  782. libvlc_media_player_set_time(FInstance,AVAlue);
  783. end;
  784. procedure TCustomVLCMediaPlayer.SetVideoScale(AValue: Double);
  785. begin
  786. libvlc_video_set_scale(Finstance,AVAlue);
  787. end;
  788. function TCustomVLCMediaPlayer.GetInstance: Plibvlc_media_player_t;
  789. begin
  790. Result:=FInstance;
  791. if (FInstance=Nil) then
  792. begin
  793. FInstance:=libvlc_media_player_new(VLC.Instance);
  794. libvlc_video_set_mouse_input(FInstance,1);
  795. libvlc_video_set_key_input(FInstance,1);
  796. if FUseEvents then
  797. HookupEvents;
  798. end;
  799. Result:=FInstance;
  800. end;
  801. procedure TCustomVLCMediaPlayer.SetParentWindow;
  802. begin
  803. // Do nothing
  804. end;
  805. procedure TCustomVLCMediaPlayer.SetParentWindowSize(AWidth, AHeight: Cardinal);
  806. begin
  807. // Do nothing
  808. end;
  809. Procedure TCustomVLCMediaPlayer.UnHookEvents;
  810. Procedure ClearEvent(M : plibvlc_event_manager_t;t : libvlc_event_e);
  811. begin
  812. libvlc_event_detach(M,ord(t),@PlayerEventHelper,Self);
  813. end;
  814. Var
  815. M : plibvlc_event_manager_t;
  816. begin
  817. M:=libvlc_media_player_event_manager(Instance);
  818. if (M<>Nil) then
  819. begin
  820. ClearEvent(M,libvlc_MediaPlayerMediaChanged);
  821. ClearEvent(M,libvlc_MediaPlayerNothingSpecial);
  822. ClearEvent(M,libvlc_MediaPlayerOpening);
  823. ClearEvent(M,libvlc_MediaPlayerBuffering);
  824. ClearEvent(M,libvlc_MediaPlayerPlaying);
  825. ClearEvent(M,libvlc_MediaPlayerPaused);
  826. ClearEvent(M,libvlc_MediaPlayerStopped);
  827. ClearEvent(M,libvlc_MediaPlayerForward);
  828. ClearEvent(M,libvlc_MediaPlayerBackward);
  829. ClearEvent(M,libvlc_MediaPlayerEndReached);
  830. ClearEvent(M,libvlc_MediaPlayerEncounteredError);
  831. ClearEvent(M,libvlc_MediaPlayerTimeChanged);
  832. ClearEvent(M,libvlc_MediaPlayerPositionChanged);
  833. ClearEvent(M,libvlc_MediaPlayerSeekableChanged);
  834. ClearEvent(M,libvlc_MediaPlayerPausableChanged);
  835. ClearEvent(M,libvlc_MediaPlayerTitleChanged);
  836. ClearEvent(M,libvlc_MediaPlayerSnapshotTaken);
  837. ClearEvent(M,libvlc_MediaPlayerLengthChanged);
  838. FreeAndNil(FECS);
  839. end;
  840. end;
  841. Procedure TCustomVLCMediaPlayer.HookupEvents;
  842. Procedure AttachEvent( M : plibvlc_event_manager_t;t : libvlc_event_e);
  843. begin
  844. libvlc_event_attach(M,ord(t),@PlayerEventHelper,Self);
  845. end;
  846. Var
  847. M : plibvlc_event_manager_t;
  848. begin
  849. M:=libvlc_media_player_event_manager(Instance);
  850. if (M<>Nil) then
  851. begin
  852. FECS:=TCriticalSection.Create;
  853. AttachEvent(M,libvlc_MediaPlayerMediaChanged);
  854. AttachEvent(M,libvlc_MediaPlayerNothingSpecial);
  855. AttachEvent(M,libvlc_MediaPlayerOpening);
  856. AttachEvent(M,libvlc_MediaPlayerBuffering);
  857. AttachEvent(M,libvlc_MediaPlayerPlaying);
  858. AttachEvent(M,libvlc_MediaPlayerPaused);
  859. AttachEvent(M,libvlc_MediaPlayerStopped);
  860. AttachEvent(M,libvlc_MediaPlayerForward);
  861. AttachEvent(M,libvlc_MediaPlayerBackward);
  862. AttachEvent(M,libvlc_MediaPlayerEndReached);
  863. AttachEvent(M,libvlc_MediaPlayerEncounteredError);
  864. AttachEvent(M,libvlc_MediaPlayerTimeChanged);
  865. AttachEvent(M,libvlc_MediaPlayerPositionChanged);
  866. AttachEvent(M,libvlc_MediaPlayerSeekableChanged);
  867. AttachEvent(M,libvlc_MediaPlayerPausableChanged);
  868. AttachEvent(M,libvlc_MediaPlayerTitleChanged);
  869. AttachEvent(M,libvlc_MediaPlayerSnapshotTaken);
  870. AttachEvent(M,libvlc_MediaPlayerLengthChanged);
  871. end;
  872. end;
  873. procedure TCustomVLCMediaPlayer.DoMediaChanged;
  874. begin
  875. If Assigned(FOnMediaChanged) then
  876. FOnMediaChanged(Self);
  877. end;
  878. procedure TCustomVLCMediaPlayer.DoNothingSpecial;
  879. begin
  880. If Assigned(FOnNothingSpecial) then
  881. FOnNothingSpecial(Self);
  882. end;
  883. procedure TCustomVLCMediaPlayer.DoOnOpening;
  884. begin
  885. If Assigned(FOnOpening) then
  886. FOnOpening(Self);
  887. end;
  888. procedure TCustomVLCMediaPlayer.DoOnPlaying;
  889. begin
  890. If Assigned(FOnPlaying) then
  891. FOnPlaying(Self);
  892. end;
  893. procedure TCustomVLCMediaPlayer.DoOnPause;
  894. begin
  895. If Assigned(FOnPause) then
  896. FOnPause(Self);
  897. end;
  898. procedure TCustomVLCMediaPlayer.DoOnStop;
  899. begin
  900. If Assigned(FOnStop) then
  901. FOnStop(Self);
  902. end;
  903. procedure TCustomVLCMediaPlayer.DoOnForward;
  904. begin
  905. If Assigned(FOnForward) then
  906. FOnForward(Self);
  907. end;
  908. procedure TCustomVLCMediaPlayer.DoOnBackward;
  909. begin
  910. If Assigned(FOnBackward) then
  911. FOnBackward(Self);
  912. end;
  913. procedure TCustomVLCMediaPlayer.DoOnEOF;
  914. begin
  915. If Assigned(FOnEOF) then
  916. FOnEOF(Self);
  917. end;
  918. procedure TCustomVLCMediaPlayer.DoOnBuffering;
  919. begin
  920. If Assigned(FOnBuffering) then
  921. FOnBuffering(Self);
  922. end;
  923. procedure TCustomVLCMediaPlayer.DoOnError;
  924. Var
  925. P : pcchar;
  926. E : String;
  927. begin
  928. p:=libvlc_errmsg();
  929. if p<>Nil then
  930. E:=StrPas(PChar(P))
  931. else
  932. E:='';
  933. If Assigned(FOnError) then
  934. FOnError(Self,E);
  935. end;
  936. procedure TCustomVLCMediaPlayer.DoOnTimeChanged(Const ATime: libvlc_time_t);
  937. begin
  938. If Assigned(FOnTimeChanged) then
  939. FOnTimeChanged(Self,VLCTimeToDateTime(ATime));
  940. end;
  941. procedure TCustomVLCMediaPlayer.DoOnPositionChanged(Const Aposition: Double);
  942. begin
  943. If Assigned(FOnPositionChanged) then
  944. FOnPositionChanged(Self,APosition);
  945. end;
  946. procedure TCustomVLCMediaPlayer.DoOnSeekableChanged(Const ASeekable : Boolean);
  947. begin
  948. If Assigned(FOnSeekableChanged) then
  949. FOnSeekableChanged(Self,ASeekable);
  950. end;
  951. procedure TCustomVLCMediaPlayer.DoOnPausableChanged(Const APausable : Boolean);
  952. begin
  953. If Assigned(FOnPausableChanged) then
  954. FOnPausableChanged(Self,APausable);
  955. end;
  956. procedure TCustomVLCMediaPlayer.DoOnTitleChanged(Const ATitle: cint);
  957. begin
  958. If Assigned(FOnTitleChanged) then
  959. FOnTitleChanged(Self,ATitle);
  960. end;
  961. procedure TCustomVLCMediaPlayer.DoOnSnapshot(Const AFileName : PCChar);
  962. Var
  963. S :String;
  964. begin
  965. If Assigned(FOnSnapshot) then
  966. begin
  967. if Assigned(AFileName) then
  968. S:=StrPas(PChar(AFileName))
  969. else
  970. S:='';
  971. FOnSnapShot(Self,S);
  972. end;
  973. end;
  974. procedure TCustomVLCMediaPlayer.DoOnLengthChanged(Const ATime: libvlc_time_t);
  975. begin
  976. If Assigned(FOnLengtHChanged) then
  977. FOnLengtHChanged(Self,VLCTimeToDateTime(ATime));
  978. end;
  979. procedure TCustomVLCMediaPlayer.HandleVLCEvent(e: Plibvlc_event_t);
  980. begin
  981. FECS.Enter;
  982. try
  983. case libvlc_event_e(e^._type) of
  984. libvlc_MediaPlayerMediaChanged : DoMediaChanged;
  985. libvlc_MediaPlayerNothingSpecial : DoNothingSpecial;
  986. libvlc_MediaPlayerOpening : DoOnOpening;
  987. libvlc_MediaPlayerBuffering : DoOnBuffering;
  988. libvlc_MediaPlayerPlaying : DoOnPlaying;
  989. libvlc_MediaPlayerPaused : DoOnPause;
  990. libvlc_MediaPlayerStopped : DoOnStop;
  991. libvlc_MediaPlayerForward : DoOnForward;
  992. libvlc_MediaPlayerBackward : DoOnBackward;
  993. libvlc_MediaPlayerEndReached : DoOnEOF;
  994. libvlc_MediaPlayerEncounteredError : DoOnError;
  995. libvlc_MediaPlayerTimeChanged : begin
  996. DoOnTimeChanged(e^.media_player_time_changed.new_time);
  997. end;
  998. libvlc_MediaPlayerPositionChanged : begin
  999. DoOnPositionChanged(e^.media_player_position_changed.new_position);
  1000. end;
  1001. libvlc_MediaPlayerSeekableChanged : begin
  1002. DoOnSeekableChanged(e^.media_player_seekable_changed.new_seekable<>0);
  1003. end;
  1004. libvlc_MediaPlayerPausableChanged : begin
  1005. DoOnPausableChanged(e^.media_player_pausable_changed.new_pausable<>0) ;
  1006. end;
  1007. libvlc_MediaPlayerTitleChanged : begin
  1008. DoOnTitleChanged(e^.media_player_title_changed.new_title);
  1009. end;
  1010. libvlc_MediaPlayerSnapshotTaken : begin
  1011. DoOnSnapShot(e^.media_player_snapshot_taken.psz_filename);
  1012. end;
  1013. libvlc_MediaPlayerLengthChanged : begin
  1014. DoOnLengthChanged(e^.media_player_length_changed.new_length);
  1015. end;
  1016. else
  1017. // Not sure what to do here.
  1018. // Writeln('Unknown event type ',e^._type);
  1019. end;
  1020. finally
  1021. FECS.Leave;
  1022. end;
  1023. end;
  1024. destructor TCustomVLCMediaPlayer.Destroy;
  1025. begin
  1026. If Assigned(FInstance) then
  1027. begin
  1028. libvlc_media_player_release(FInstance);
  1029. FInstance:=Nil;
  1030. end;
  1031. FreeAndNil(FECS);
  1032. inherited Destroy;
  1033. end;
  1034. procedure TCustomVLCMediaPlayer.SetMedia(M: TVLCMediaItem);
  1035. begin
  1036. libvlc_media_player_set_media(Instance,M.Instance);
  1037. end;
  1038. procedure TCustomVLCMediaPlayer.Play;
  1039. Var
  1040. W,H : Cardinal;
  1041. begin
  1042. SetParentWindow;
  1043. libvlc_media_player_play(Instance);
  1044. If FitWindow then
  1045. begin
  1046. VideoScale:=1.0;
  1047. if GetVideoSize(W,H) then
  1048. SetParentWindowSize(W,H);
  1049. end;
  1050. end;
  1051. procedure TCustomVLCMediaPlayer.Play(M: TVLCMediaItem);
  1052. begin
  1053. if Playing then
  1054. begin
  1055. Stop;
  1056. While Playing do
  1057. Sleep(100);
  1058. end;
  1059. SetMedia(M);
  1060. Play;
  1061. end;
  1062. procedure TCustomVLCMediaPlayer.PlayFile(const AFileName: String);
  1063. Var
  1064. M : TVLCMediaItem;
  1065. begin
  1066. M:=TVLCMediaItem.Create(Nil);
  1067. try
  1068. M.Path:=AFileName;
  1069. Play(M);
  1070. finally
  1071. M.Free;
  1072. end;
  1073. end;
  1074. procedure TCustomVLCMediaPlayer.Stop;
  1075. begin
  1076. if Assigned(FInstance) then
  1077. libvlc_media_player_stop(FInstance);
  1078. end;
  1079. procedure TCustomVLCMediaPlayer.Pause;
  1080. begin
  1081. if Assigned(FInstance) then
  1082. libvlc_media_player_pause(FInstance);
  1083. end;
  1084. procedure TCustomVLCMediaPlayer.Resume;
  1085. begin
  1086. if (GetState()=libvlc_Paused) then
  1087. if Assigned(FInstance) then
  1088. libvlc_media_player_play(FInstance);
  1089. end;
  1090. procedure TCustomVLCMediaPlayer.NextFrame;
  1091. begin
  1092. if Assigned(FInstance) then
  1093. libvlc_media_player_next_frame(Finstance);
  1094. end;
  1095. function TCustomVLCMediaPlayer.Snapshot(const AFileName: String): Boolean;
  1096. var
  1097. w,h : Cardinal;
  1098. begin
  1099. Result:=Assigned(FInstance);
  1100. if Result then
  1101. begin
  1102. w:=0;
  1103. h:=0;
  1104. Result:=libvlc_video_get_size(FInstance,0,@W,@H)=0;
  1105. if Result then
  1106. Result:=SnapShot(AFileName,W,H);
  1107. end;
  1108. end;
  1109. function TCustomVLCMediaPlayer.Snapshot(const AFileName: String; AWidth,
  1110. AHeight: Cardinal): Boolean;
  1111. begin
  1112. Result:=Assigned(FInstance);
  1113. If Result then
  1114. Result:=libvlc_video_take_snapshot(FInstance,0,PCChar(PChar(AFileName)),AWidth,AHeight)=0;
  1115. end;
  1116. function TCustomVLCMediaPlayer.GetVideoSize(var AWidth, AHeight: Cardinal
  1117. ): Boolean;
  1118. begin
  1119. Result:=libvlc_video_get_size(FInstance,0,@AWidth,@AHeight)=0;
  1120. end;
  1121. { TVLCMediaItems }
  1122. constructor TVLCMediaItems.Create(ALibrary: TVLCLibrary;AItemClass: TVLCMediaItemClass = Nil);
  1123. begin
  1124. Inherited Create(AItemClass);
  1125. FVLC:=ALibrary;
  1126. end;
  1127. constructor TVLCMediaItems.Create(AInstance: Plibvlc_media_list_t;
  1128. AItemClass: TVLCMediaItemClass);
  1129. Var
  1130. I : Integer;
  1131. P : plibvlc_media_t;
  1132. begin
  1133. Inherited Create(AItemClass);
  1134. FInstance:=AInstance;
  1135. For I:=0 to libvlc_media_list_count(FInstance)-1 do
  1136. begin
  1137. P:=libvlc_media_list_item_at_index(FInstance,I);
  1138. (Add as TVLCMediaItem).SetInstance(P);
  1139. end;
  1140. end;
  1141. procedure TVLCMediaItems.Lock;
  1142. begin
  1143. libvlc_media_list_lock(FInstance);
  1144. end;
  1145. procedure TVLCMediaItems.Unlock;
  1146. begin
  1147. libvlc_media_list_lock(FInstance);
  1148. end;
  1149. function TVLCMediaItems.GetInstance: Plibvlc_media_list_t;
  1150. Var
  1151. I :integer;
  1152. begin
  1153. if FInstance=Nil then
  1154. begin
  1155. FInstance:=libvlc_media_list_new(VLC.Instance);
  1156. For I:=0 to Count-1 do
  1157. GetI(I).RegisterInstance;
  1158. end;
  1159. Result:=Finstance;
  1160. end;
  1161. function TVLCMediaItems.GetIsReadOnly: Boolean;
  1162. begin
  1163. Result:=libvlc_media_list_is_readonly(FInstance)<>0;
  1164. end;
  1165. function TVLCMediaItems.GetI(AIndex : Integer): TVLCMediaItem;
  1166. begin
  1167. Result:=Items[AIndex] as TVLCMediaItem;
  1168. end;
  1169. procedure TVLCMediaItems.SetI(AIndex : Integer; AValue: TVLCMediaItem);
  1170. begin
  1171. Items[AIndex]:=AValue;
  1172. end;
  1173. function TVLCMediaItems.GetVLC: TVLCLibrary;
  1174. begin
  1175. Result:=VLCLibrary;
  1176. end;
  1177. { TVLCMediaItem }
  1178. function TVLCMediaItem.GetInstance: plibvlc_media_t;
  1179. begin
  1180. Result:=Finstance;
  1181. If (Result=Nil) then
  1182. Raise EVLC.Create('No instance available at this time. Set MRL, Path or FileDescriptor first');
  1183. end;
  1184. function TVLCMediaItem.GetM(AIndex: Integer): Boolean;
  1185. begin
  1186. Result:=FOpts[AIndex];
  1187. end;
  1188. function TVLCMediaItem.GetMD(AMeta : libvlc_meta_t): String;
  1189. Var
  1190. P : PCChar;
  1191. begin
  1192. P:=libvlc_media_get_meta(Instance,AMeta);
  1193. if (P<>Nil) then
  1194. Result:=StrPas(PChar(p))
  1195. else
  1196. Result:='';
  1197. end;
  1198. function TVLCMediaItem.GetMRL: String;
  1199. Var
  1200. P : PCChar;
  1201. begin
  1202. P:=libvlc_media_get_mrl(Instance);
  1203. if (P<>Nil) then
  1204. Result:=StrPas(PChar(p))
  1205. else
  1206. Result:='';
  1207. end;
  1208. function TVLCMediaItem.GetParsed: Boolean;
  1209. begin
  1210. Result:=libvlc_media_is_parsed(Instance)<>0;
  1211. end;
  1212. function TVLCMediaItem.GetUD: Pointer;
  1213. begin
  1214. Result:=libvlc_media_get_user_data(Instance);
  1215. end;
  1216. procedure TVLCMediaItem.SetDIM(AValue: TDeinterlaceMode);
  1217. Const
  1218. DMS : Array[TDeinterlaceMode] of string
  1219. = ('blend', 'discard', 'bob', 'linear', 'mean', 'x', 'yadif', 'yadif2x');
  1220. begin
  1221. if (FDIM=AValue) then Exit;
  1222. FDIM:=AValue;
  1223. libvlc_media_add_option(Instance, PCChar(PChar('deinterlace-mode='+DMS[AValue])));
  1224. end;
  1225. procedure TVLCMediaItem.SetFD(AValue: Integer);
  1226. begin
  1227. FFD:=AValue;
  1228. Finstance:=libvlc_media_new_fd(GetVLC.Instance,AValue);
  1229. If (FInstance=Nil) then
  1230. Raise EVLC.CreateFmt('Failed to create media item from file descriptor "%d"',[AValue]);
  1231. RegisterInstance;
  1232. end;
  1233. procedure TVLCMediaItem.SetFSS(AValue: TSNapshotFormat);
  1234. Const
  1235. ssfs : Array[TSnapShotFormat] of string = ('png','jpg');
  1236. begin
  1237. if FSS=AValue then Exit;
  1238. FSS:=AValue;
  1239. libvlc_media_add_option(Instance, PCChar(PChar('no-snapshot-preview')));
  1240. libvlc_media_add_option(instance, PCChar(PChar('snapshot-format=' + SSFS[aValue])));
  1241. end;
  1242. procedure TVLCMediaItem.SetM(AIndex: Integer; AValue: Boolean);
  1243. begin
  1244. FOpts[AIndex]:=AValue;
  1245. libvlc_media_add_option(FInstance,PcChar(PChar(GetBoolOpt(AIndex,AValue))));
  1246. end;
  1247. function TVLCMediaItem.GetBoolOpt(AIndex: Integer; AValue: Boolean): String;
  1248. begin
  1249. Case AINdex of
  1250. 0 : Result:='video-title-show';
  1251. 1 : Result:='video-on-top';
  1252. 2 : Result:='overlay';
  1253. 3 : Result:='fullscreen';
  1254. 4 : Result:='deinterlace='+IntToStr(Ord(AValue));
  1255. end;
  1256. if (AIndex < 4) and Not AValue then
  1257. Result:='no-'+Result;
  1258. end;
  1259. procedure TVLCMediaItem.SetMD(AMeta : libvlc_meta_t; AValue: String);
  1260. begin
  1261. libvlc_media_set_meta(Instance,AMeta,Pcchar(PChar(AValue)));
  1262. end;
  1263. procedure TVLCMediaItem.SetMRL(AValue: String);
  1264. begin
  1265. Finstance:=libvlc_media_new_location(GetVLC.Instance,PCChar(AValue));
  1266. If (FInstance=Nil) then
  1267. Raise EVLC.CreateFmt('Failed to create media item from MRL : "%s"',[AValue]);
  1268. RegisterInstance;
  1269. end;
  1270. procedure TVLCMediaItem.SetPath(AValue: String);
  1271. begin
  1272. if FPath=AValue then Exit;
  1273. FPath:=AValue;
  1274. FInstance:=libvlc_media_new_path(GetVLC.Instance,PCChar(AValue));
  1275. if (FInstance=Nil) then
  1276. Raise EVLC.CreateFmt('Failed to create media item from path : "%s"',[AValue]);
  1277. RegisterInstance;
  1278. end;
  1279. procedure TVLCMediaItem.SetUD(AValue: Pointer);
  1280. begin
  1281. libvlc_media_set_user_data(Instance,AValue);
  1282. end;
  1283. function TVLCMediaItem.GetState: libvlc_state_t;
  1284. begin
  1285. Result:=libvlc_media_get_state(instance);
  1286. end;
  1287. function TVLCMediaItem.GetDuration: TDateTime;
  1288. Var
  1289. d : libvlc_time_t;
  1290. begin
  1291. d:=libvlc_media_get_duration(Instance);
  1292. Result:=D
  1293. end;
  1294. procedure TVLCMediaItem.RegisterInstance;
  1295. Var
  1296. L : Plibvlc_media_list_t;
  1297. begin
  1298. If Assigned(Collection) and (Collection is TVLCMediaItems) then
  1299. begin
  1300. L:=TVLCMediaItems(Collection).FInstance;
  1301. if (L<>Nil) then
  1302. begin
  1303. libvlc_media_list_lock(L);
  1304. libvlc_media_list_add_media(L, FInstance);
  1305. libvlc_media_list_unlock(L);
  1306. end;
  1307. end;
  1308. end;
  1309. procedure TVLCMediaItem.UnRegisterInstance;
  1310. Var
  1311. L : Plibvlc_media_list_t;
  1312. i : integer;
  1313. begin
  1314. If Assigned(Collection) and (Collection is TVLCMediaItems) then
  1315. begin
  1316. L:=TVLCMediaItems(Collection).FInstance;
  1317. if L<>Nil then
  1318. begin
  1319. libvlc_media_list_lock(L);
  1320. I:=libvlc_media_list_index_of_item(L,Finstance);
  1321. if (i>=0) then
  1322. libvlc_media_list_remove_index(L,i);
  1323. libvlc_media_list_unlock(L);
  1324. end;
  1325. end;
  1326. end;
  1327. function TVLCMediaItem.GetVLC: TVLCLibrary;
  1328. begin
  1329. If Assigned(Collection) and (Collection is TVLCMediaItems) then
  1330. Result:=TVLCMediaItems(Collection).GetVLC
  1331. else
  1332. Result:=VLCLibrary;
  1333. if not Result.Initialized then
  1334. Result.Initialize;
  1335. end;
  1336. function TVLCMediaItem.GetEventManager: plibvlc_event_manager_t;
  1337. begin
  1338. Result:=libvlc_media_event_manager(Instance);
  1339. end;
  1340. procedure TVLCMediaItem.SetInstance(Avalue: plibvlc_media_t);
  1341. begin
  1342. FInstance:=AValue;
  1343. end;
  1344. destructor TVLCMediaItem.Destroy;
  1345. begin
  1346. inherited Destroy;
  1347. if Assigned(FInstance) then
  1348. begin
  1349. UnregisterInstance;
  1350. libvlc_media_release(FInstance);
  1351. FInstance:=Nil;
  1352. end;
  1353. end;
  1354. procedure TVLCMediaItem.AddOption(const AValue: String);
  1355. begin
  1356. libvlc_media_add_option(Instance,PCChar(PChar(AValue)));
  1357. end;
  1358. procedure TVLCMediaItem.Parse;
  1359. begin
  1360. libvlc_media_parse(Instance);
  1361. end;
  1362. procedure TVLCMediaItem.ParseAsync;
  1363. begin
  1364. libvlc_media_parse_async(Instance);
  1365. end;
  1366. procedure TVLCMediaItem.SaveMetaData;
  1367. begin
  1368. libvlc_media_save_meta(Instance);
  1369. end;
  1370. function TVLCMediaItem.GetStats(var AStats: libvlc_media_stats_t): Boolean;
  1371. begin
  1372. Result:=libvlc_media_get_stats(Instance,@AStats)<>0;
  1373. end;
  1374. function TVLCMediaItem.Duplicate: TVLCMediaItem;
  1375. begin
  1376. If Assigned(Collection) and (Collection is TVLCMediaItems) then
  1377. Result:=TVLCMediaItems(Collection).Add as TVLCMediaItem
  1378. else
  1379. Result:=TVLCMediaItem.Create(Nil);
  1380. Result.SetInstance(libvlc_media_duplicate(Instance));
  1381. end;
  1382. function TVLCLibrary.GetLastError: String;
  1383. Var
  1384. P : PCChar;
  1385. begin
  1386. P:=libvlc_errmsg();
  1387. if Assigned(P) then
  1388. Result := StrPas(PChar(P))
  1389. else
  1390. Result:='';
  1391. end;
  1392. function TVLCLibrary.GetI: Boolean;
  1393. begin
  1394. Result:=FInstance<>Nil;
  1395. end;
  1396. function TVLCLibrary.GetVersion: String;
  1397. Var
  1398. P : PCChar;
  1399. begin
  1400. P:=libvlc_get_version();
  1401. if Assigned(P) then
  1402. Result := StrPas(PChar(P))
  1403. else
  1404. Result:='';
  1405. end;
  1406. function TVLCLibrary.GetCompiler: String;
  1407. Var
  1408. P : PCChar;
  1409. begin
  1410. P:=libvlc_get_compiler();
  1411. if Assigned(P) then
  1412. Result := StrPas(PChar(P))
  1413. else
  1414. Result:='';
  1415. end;
  1416. function TVLCLibrary.GetChangeSet: String;
  1417. Var
  1418. P : PCChar;
  1419. begin
  1420. P:=libvlc_get_changeset();
  1421. if Assigned(P) then
  1422. Result := StrPas(PChar(P))
  1423. else
  1424. Result:='';
  1425. end;
  1426. procedure TVLCLibrary.SetLibraryPath(const AValue: String);
  1427. begin
  1428. If AValue=FLibraryPath then exit;
  1429. If Assigned(FInstance) then
  1430. Raise EVLC.Create('VLC already initialized, cannot set librarypath');
  1431. FLibraryPath:=AVAlue;
  1432. end;
  1433. function TVLCLibrary.GetInstance: plibvlc_instance_t;
  1434. var
  1435. args: Array of AnsiString;
  1436. cargs : array of PAnsiChar;
  1437. argc,
  1438. I : integer;
  1439. begin
  1440. If (FInstance=Nil) then
  1441. begin
  1442. LibraryArgs.add('--no-video-title-show');
  1443. SetLength(cArgs,LibraryArgs.Count+2);
  1444. SetLength(Args,LibraryArgs.Count+1);
  1445. cargs[0] := PChar(FLibraryPath);
  1446. For I:=0 to LibraryArgs.Count-1 do
  1447. begin
  1448. Args[i]:=LibraryArgs[i];
  1449. CArgs[i+1]:=PChar(Args[i]);
  1450. end;
  1451. argc:=Length(CArgs);
  1452. cargs[argc-1] := NIL;
  1453. FInstance := libvlc_new(argc-1, PPcchar(cargs));
  1454. if (FInstance=Nil) then
  1455. Raise EVLC.Create('Could not create instance of libvlc');
  1456. end;
  1457. Result:=FInstance;
  1458. end;
  1459. constructor TVLCLibrary.Create(AOwner: TComponent);
  1460. begin
  1461. Inherited;
  1462. FInstance:=Nil;
  1463. FLibraryPath:=LibName;
  1464. FLibraryArgs:=TStringList.Create;
  1465. end;
  1466. destructor TVLCLibrary.Destroy;
  1467. begin
  1468. FreeAndNil(FLibraryArgs);
  1469. Release;
  1470. inherited Destroy;
  1471. end;
  1472. procedure TVLCLibrary.Initialize;
  1473. begin
  1474. LoadLibVLC(LibraryPath,False);
  1475. GetInstance;
  1476. end;
  1477. procedure TVLCLibrary.Release;
  1478. begin
  1479. If (FInstance<>Nil) then
  1480. begin
  1481. libvlc_release(FInstance);
  1482. FreeLibVLC;
  1483. end;
  1484. FInstance:=Nil;
  1485. end;
  1486. Initialization
  1487. Finalization
  1488. DoneVLC;
  1489. end.