vlc.pp 46 KB

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