www.pudn.com > DirectShowPlayer.rar > DX8DS.PAS


{ 
 DX8DS -- Pulsar DirectX 8 DirectShow unit. 
          by Lord Trancos.           
 
 Copyright © 2001 Pulsar Studio / Lord Trancos. 
 
 2001/08/21 - Minor changes to work with ActiveX unit instead of Ole2. 
 2000/12/03 - First release with DSPlayer.pas name. 
 2000/12/16 - Rewritten, all changed. 
 2000/06/30 - Some minor changes. 
} 
 
unit DX8DS; 
 
// ------------------------------------------------------------ 
 
interface 
 
uses Windows, ActiveX, DirectShow; 
 
const 
  // Minimum audio volume. 
  DSMINAUDIOVOLUME = -10000; 
  // Window Style usually used with dsmpSetVideoWindowAsChild. 
  DSVIDEO_WINDOW_CHILD_STYLE = WS_CHILD or WS_CLIPCHILDREN or WS_CLIPSIBLINGS; 
 
type 
  // DirectShow Media Player. 
  TDSMP = record 
    Initialized  : boolean; 
 
    // DirectShow stuff. 
    GraphBuilder : IGraphBuilder; 
    MediaControl : IMediaControl; 
    MediaSeeking : IMediaSeeking; 
    MediaEventEx : IMediaEvent; 
    BasicAudio   : IBasicAudio; 
    BasicVideo   : IBasicVideo; 
    VideoWindow  : IVideoWindow; 
    // Video info. 
    VideoAvail   : boolean; 
    VideoWidth   : integer; 
    VideoHeight  : integer; 
    VideoBitRate : integer; 
    VideoFPS     : single; 
  end; 
 
procedure dsmpReset(var _dsmp: TDSMP); 
function  dsmpLoadFile(var _dsmp: TDSMP; _filename: string): boolean; 
procedure dsmpFree(var _dsmp: TDSMP); 
 
// Video Window functions 
function  dsmpVideoAvail(_dsmp: TDSMP): boolean; 
procedure dsmpSetVWAsChild(_dsmp: TDSMP; _parent: hWnd; 
                           _winStyle: longint); 
 
// Other functions. 
procedure dsmpSetPos(_dsmp: TDSMP; _pos: Int64); 
 
// ------------------------------------------------------------ 
 
implementation 
 
// ------------------------------------------------------------ 
 
procedure dsmpReset(var _dsmp: TDSMP); 
 
begin 
  FillChar(_dsmp, SizeOf(_dsmp), 0); 
end; 
 
// ------------------------------------------------------------ 
 
procedure dsmpFree(var _dsmp: TDSMP); 
 
begin 
  with _dsmp do 
    begin 
(* 
      // Hide VideoWindow and relinquish ownership. 
      if Assigned(VideoWindow) then 
        begin 
          VideoWindow.put_Visible(false); 
          VideoWindow.put_Owner(0); 
        end; 
*) 
      if Assigned(MediaEventEx) then MediaEventEx := NIL; 
      if Assigned(MediaSeeking) then MediaSeeking := NIL; 
      if Assigned(MediaControl) then MediaControl := NIL; 
      if Assigned(BasicAudio)   then BasicAudio   := NIL; 
      if Assigned(BasicVideo)   then BasicVideo   := NIL; 
      if Assigned(VideoWindow)  then VideoWindow  := NIL; 
      if Assigned(GraphBuilder) then GraphBuilder := NIL; 
 
      dsmpReset(_dsmp); 
    end; 
end; 
 
// ------------------------------------------------------------ 
 
function dsmpLoadFile(var _dsmp: TDSMP; _filename: string): boolean; 
 
var _wfn : array[0..MAX_PATH-1] of wchar; 
    _vis : longBool; 
    _tpf : double; 
 
begin 
  Result := false; 
 
  // Free previous interfaces. 
  if Assigned(_dsmp.GraphBuilder) 
    then dsmpFree(_dsmp) else dsmpReset(_dsmp); 
 
  // Get unicode filename. 
  MultiByteToWideChar(CP_ACP, 0, pChar(_filename), -1, @_wfn, MAX_PATH); 
 
  // Create interfaces and construct the graph. 
  with _dsmp do 
    begin 
      // Create DirectShow Graph. 
      if failed(CoCreateInstance(TGUID(CLSID_FilterGraph), 
                                 NIL, CLSCTX_INPROC_SERVER, 
                                 TGUID(IID_IGraphBuilder), 
                                 GraphBuilder)) then exit; 
 
      // Build the filter graph. 
      if failed(GraphBuilder.RenderFile(_wfn, NIL)) then exit; 
 
      // Get the IMediaControl Interface 
      if failed(GraphBuilder.QueryInterface(IID_IMediaControl, 
                MediaControl)) then exit; 
 
      // Get the IMediaSeeking Interface 
      if failed(GraphBuilder.QueryInterface(IID_IMediaSeeking, 
                MediaSeeking)) then exit; 
 
      // Get the IMediaEventEx Interface 
      if failed(GraphBuilder.QueryInterface(IID_IMediaEventEx, 
                MediaEventEx)) then exit; 
 
      // Get Audio and Video Interfaces. 
      GraphBuilder.QueryInterface(IID_IBasicAudio,  BasicAudio); 
      GraphBuilder.QueryInterface(IID_IBasicVideo,  BasicVideo); 
      GraphBuilder.QueryInterface(IID_IVideoWindow, VideoWindow); 
 
      // Get file info. 
      VideoAvail := Assigned(BasicVideo) and Assigned(VideoWindow) and 
                    (not failed(VideoWindow.get_Visible(_vis))); 
 
      // Get video info. 
      if VideoAvail then 
        begin 
          BasicVideo.GetVideoSize(VideoWidth, VideoHeight); 
          BasicVideo.get_BitRate(VideoBitRate); 
          BasicVideo.get_AvgTimePerFrame(_tpf); 
          if _tpf <> 0 then VideoFPS := 1 / _tpf else VideoFPS := 0; 
        end; 
 
      // Initialized. 
      Initialized := true; 
    end; 
 
  Result := true; 
end; 
 
// ------------------------------------------------------------ 
 
function  dsmpVideoAvail(_dsmp: TDSMP): boolean; 
 
begin 
  Result := (_dsmp.Initialized) and (_dsmp.VideoAvail); 
end; 
 
// ------------------------------------------------------------ 
 
procedure dsmpSetVWAsChild(_dsmp: TDSMP; _parent: hWnd; 
                           _winStyle: longint); 
 
// For more info about _winStyle (Window Style) values check 
// the CreateWindow function. 
 
begin 
  if not dsmpVideoAvail(_dsmp) then exit; 
 
  with _dsmp.VideoWindow do 
    begin 
      put_Owner(_parent); // Set owner 
      put_WindowStyle(_winStyle); // Set window style 
      put_MessageDrain(_parent); // Set message drain to parent window 
    end; 
end; 
 
// ------------------------------------------------------------ 
 
procedure dsmpSetPos(_dsmp: TDSMP; _pos: Int64); 
 
var _stop : Int64; 
 
begin 
  if not _dsmp.Initialized then exit; 
 
  // adjust position. 
  _dsmp.MediaSeeking.GetStopPosition(_stop); 
  if _pos < 0 then _pos := 0; 
  if _pos > _stop then _pos := _stop; 
 
  // set position. 
  _dsmp.MediaSeeking.SetPositions(_pos, AM_SEEKING_AbsolutePositioning, 
                                  _pos, AM_SEEKING_NoPositioning); 
end; 
 
// ------------------------------------------------------------ 
 
end.