⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 netwrite.pas

📁 一套及时通讯的原码
💻 PAS
📖 第 1 页 / 共 2 页
字号:
      exit;
    end;

    result := m_pReader.Start(0, 0, 1.0, nil);
    if (FAILED(result)) then
    begin
      writeln(format('Could not start IWMReader (hr=$%x)',[result]));
      exit;
    end;

   // not usefull with Windowed app
  { // Wait for it to finish
    WaitForSingleObject(m_hEvent, INFINITE);
    if (FAILED(m_hrAsync)) then
    begin
      result := m_hrAsync;
      exit; // Net writing failed  ????? not logic for hresult
    end;}
  end;

  function TWMFNetWrite.CloseAll: HRESULT;
  begin

    // Stop stuff
    if assigned(m_pReader) then
    begin
      result := m_pReader.Stop;
      if (FAILED(result)) then
      begin
        writeln(format('Could not Stop IWMReader (hr=$%x)',[result]));
        exit;
      end; // Could not Stop IWMReader
    end;

    if assigned(m_pWriter) then
    begin
      result := m_pWriter.Flush;
      if (FAILED(result)) then
      begin
        writeln(format('Could not Flush on IWMWriter (hr=$%x)',[result]));
        exit;
      end;
      result := m_pWriter.EndWriting;
      if (FAILED(result)) then
      begin
        writeln(format('Could not EndWriting on IWMWriter (hr=$%x)',[result]));
        exit;
      end;
    end;

    if assigned(m_pReader) then
    begin
      result := m_pReader.Close;
      if (FAILED(result)) then
      begin
        writeln(format('Could not close the file (hr=$%x)',[result]));
        exit;
      end; 
    end;

    if assigned(m_pWriterAdvanced) then
    begin
      result := m_pWriterAdvanced.RemoveSink(m_pNetSink);
      if (FAILED(result)) then
      begin
        writeln(format('Could not remove the Network Sink (hr=$%x)',[result]));
        exit;
      end;
    end;

    if assigned(m_pNetSink) then
    begin
      result := m_pNetSink.Close;
      if (FAILED(result)) then
      begin
        writeln(format('Could not close on IWMWriterNetworkSink (hr=$%x)',[result]));
        exit;
      end;
    end;

    result := s_ok;
    //Wait for sometime till all the data gets read from the port
    //Sleep(20000);
  end;

  function TWMFNetWrite.Init: HRESULT;
  begin
    // Create the reader, writer and network sink.
    result := WMCreateReader( nil, 0, m_pReader);
    if (FAILED(result)) then
    begin
      writeln(format('Could not create reader (hr=$%x)',[result]));
      exit;
    end; 

    result := m_pReader.QueryInterface(IID_IWMReaderAdvanced, m_pReaderAdvanced);
    if (FAILED(result)) then
    begin
      writeln(format('Could not QI for IWMReaderAdvanced (hr=$%x)',[result]));
      exit;
    end;

    result := WMCreateWriter(nil, m_pWriter);
    if (FAILED(result)) then
    begin
      writeln(format('Could not create Writer (hr=$%x)',[result]));
      exit;
    end;

    result := m_pWriter.QueryInterface(IID_IWMWriterAdvanced, m_pWriterAdvanced);
    if (FAILED(result)) then
    begin
      writeln(format('Could not QI for IWMWriterAdvanced (hr=$%x)',[result]));
      exit;
    end;  

    result := WMCreateWriterNetworkSink(m_pNetSink);
    if (FAILED(result)) then
    begin
      writeln(format('Could not create Writer Network Sink (hr=$%x)',[result]));
      exit;
    end; 

    result := m_pReader.QueryInterface(IID_IWMHeaderInfo, m_pReaderHeaderInfo);
    if (FAILED(result)) then
    begin
      writeln(format('Could not QI for IWMHeaderInfo (hr=$%x)',[result]));
      exit;
    end;

    result := m_pWriter.QueryInterface(IID_IWMHeaderInfo, m_pWriterHeaderInfo);
    if (FAILED(result)) then
    begin
      writeln(format('Could not QI for IWMHeaderInfo (hr=$%x)',[result]));
      exit;
    end; 
  end;

  function TWMFNetWrite.OnSample(dwOutputNum: DWORD; cnsSampleTime, cnsSampleDuration: int64;
             dwFlags: DWORD; pSample: INSSBuffer; pvContext: pointer): HRESULT;
  begin
    if (m_hEvent <> 0) then
    begin
      //The samples are expected in OnStreamSample
      writeln('Error: Received a decompressed sample from the reader');
      m_hrAsync := E_UNEXPECTED;
      SetEvent(m_hEvent);
    end;
    result := S_OK;
  end;

  function TWMFNetWrite.OnStatus(Status: TWMTSTATUS; hr: HRESULT; dwType: TWMTATTRDATATYPE;
             pValue: PBYTE; pvContext: pointer): HRESULT;
  begin
    case Status of
      WMT_OPENED:
        begin
          m_hrAsync := hr;
          SetEvent(m_hEvent);
        end;
      WMT_END_OF_FILE:
        begin
          m_bEOF := true;
          writeln('EndOfStream detected in reader');
          m_hrAsync := hr;
          SetEvent(m_hEvent);
        end;
      WMT_STARTED:
        begin
          //Ask for the specific duration of the stream to be delivered
          m_qwTime := 0;
          m_qwTime := m_qwTime + (1000 * 10000);
          hr := m_pReaderAdvanced.DeliverTime(m_qwTime);
          assert(SUCCEEDED(hr));
        end;
      end;
      result :=  S_OK;
  end;

  function TWMFNetWrite.OnStreamSample(wStreamNum: WORD; cnsSampleTime, cnsSampleDuration: int64;
               dwFlags: DWORD; pSample: INSSBuffer; pvContext: pointer): HRESULT;
  begin
     writeln(format('StreamSample: num=%d, time=%d, duration=%d, flags=%d',
                     [wStreamNum, cnsSampleTime, cnsSampleDuration, dwFlags]));
    //We've got a sample. Lets write it
    m_pWriterAdvanced.WriteStreamSample( wStreamNum, cnsSampleTime, 0, cnsSampleDuration, dwFlags, pSample);
    result := S_OK;
  end;

  function TWMFNetWrite.OnTime(cnsCurrentTime: int64; pvContext: pointer): HRESULT;
  begin
    //Keep asking for the specific duration of the stream till EOF
    if( not m_bEOF) then
    begin
      m_qwTime := m_qwTime + 10000000;
      m_pReaderAdvanced.DeliverTime(m_qwTime);
    end;
    result := S_OK;
  end;

  function TWMFNetWrite.OnStreamSelection( wStreamCount: Word; pStreamNumbers: PWORD;
               pSelections: PWMTSTREAMSELECTION; pvContext: pointer): HRESULT;
  begin
    result := S_OK;
  end;

  function TWMFNetWrite.OnOutputPropsChanged(dwOutputNum: DWORD; pMediaType: PWMMEDIATYPE;
               pvContext: pointer): HRESULT;
  begin
    result := S_OK;
  end;

  function TWMFNetWrite.AllocateForStream(wStreamNum: WORD; cbBuffer: DWORD; out ppBuffer: INSSBuffer;
             pvContext: pointer): HRESULT;
  begin
    result := E_NOTIMPL;
  end;

  function TWMFNetWrite.AllocateForOutput(dwOutputNum, cbBuffer: DWORD; out ppBuffer: INSSBuffer;
             pvContext: pointer): HRESULT;
  begin
    result := E_NOTIMPL;
  end;

  function TWMFNetWrite.WriteHeader (const pwszName: PWideChar): HRESULT;
  var
    nstreamNum : WORD;
    cbLength   : WORD;
    _type      : TWMTAttrDataType;
    hr         : HRESULT;
    pValue     : PBYTE;
  begin
    nstreamNum := 0;
    cbLength   := 0;
    result     := S_OK;
    pValue     := nil;

    // Get the no of bytes to be allocated for pValue
    result := m_pReaderHeaderInfo.GetAttributeByName(nstreamNum, pwszName, _type, nil, cbLength);
    if (FAILED(result) and (result <> longint(ASF_E_NOTFOUND))) then
    begin
      writeln(format('GetAttributeByName failed for Attribute name %s (hr=$%x)',[pwszName, result]));
      exit;
    end;

    if ((cbLength = 0) or (result = longint(ASF_E_NOTFOUND))) then
    begin
      result := S_OK;
      exit;
    end;

    getmem(pValue, cbLength);
    if (pValue = nil) then
    begin
      writeln(format('Unable to allocate memory for the Attribute name %s', [pwszName]));
      result := E_OUTOFMEMORY; 
      exit;
    end;

    //Dummy do-while loop
    repeat
      // Get the value
      hr := m_pReaderHeaderInfo.GetAttributeByName(nstreamNum, pwszName, _type, pValue, cbLength);
      if (FAILED(hr)) then
      begin
        writeln(format('GetAttributeByName failed for Attribute name %s (hr=$%x)', [pwszName, hr]));
        break;
      end; 

      // Set the attribute
      hr := m_pWriterHeaderInfo.SetAttribute(nstreamNum, pwszName, _type, pValue, cbLength);
      if (FAILED(hr)) then
      begin
        writeln(format('SetAttribute failed for Attribute name %s (hr=$%x)',[pwszName, hr]));
        break;
      end; 
    until (FALSE);

    freemem(pValue);
    pValue := nil;
    result := hr;
  end;

  function TWMFNetWrite.WriteScript: HRESULT;
  var
    hr            : HRESULT;
    pwszCommand   : PWideChar;
    pwszType      : PWideChar;
    cnsScriptTime : int64;
    cScript       : WORD;
    cchTypeLen    : WORD;
    cchCommandLen : WORD;
    i             : integer;
  begin
    hr            := S_OK;
    pwszCommand   := nil;
    pwszType      := nil;
    cnsScriptTime := 0;
    cScript       := 0;
    cchTypeLen    := 0;
    cchCommandLen := 0;


    result := m_pReaderHeaderInfo.GetScriptCount(cScript);
    if (FAILED(result)) then
    begin
      writeln(format('GetScriptCount failed (hr=$%x)',[result]));
      exit;
    end;

    for i := 0 to cScript - 1 do
    begin
      // Get the memory reqd for this script
      hr := m_pReaderHeaderInfo.GetScript(i, nil, cchTypeLen, nil, cchCommandLen, cnsScriptTime);
      if (FAILED(hr)) then
      begin
        writeln(format('GetScript failed for Script no %d (hr=$%x)',[i, hr]));
        break;
      end;

      getmem(pwszType, cchTypeLen * sizeof(WORD));
      getmem(pwszCommand, cchCommandLen * sizeof(WORD));

      if ((pwszType = nil) or (pwszCommand = nil)) then
      begin
        hr := E_OUTOFMEMORY;
        break;
      end;

      // Now, get the script
      hr := m_pReaderHeaderInfo.GetScript(i, pwszType, cchTypeLen, pwszCommand, cchCommandLen, cnsScriptTime);
      if (FAILED(hr)) then
      begin
        writeln(format('GetScript failed for Script no %d (hr=$%x)', [i, hr]));
        break;
      end;  // GetScript failed for Script no %d

      // Add the script to the writer
      hr := m_pWriterHeaderInfo.AddScript(pwszType, pwszCommand, cnsScriptTime);
      if (FAILED(hr)) then
      begin
        Writeln(format('AddScript failed for Script no %d (hr=$%x)', [i, hr]));
        break;
      end;

      if pwszType <> nil then freemem(pwszType);
      if pwszCommand <> nil then freemem(pwszCommand);
      pwszType    := nil;
      pwszCommand := nil;
      cchTypeLen    := 0 ;
      cchCommandLen := 0 ;
    end;

    if pwszType <> nil then freemem(pwszType);
    if pwszCommand <> nil then freemem(pwszCommand);
    pwszType    := nil;
    pwszCommand := nil;
    result := hr;
  end;

  function TWMFNetWrite.QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
  begin
    if IsEqualGUID(IID, IID_IWMReaderCallback) then
       IWMReaderCallback(Obj) := self
    else
      if IsEQualGUID(IID, IID_IWMReaderCallbackAdvanced) then
        IWMReaderCallbackAdvanced(obj) := self
      else
      begin
        result := E_NOINTERFACE;
        exit;
      end;
    result := S_OK;
  end;

  function TWMFNetWrite._AddRef: Integer; stdcall;
  begin
    result := 1;
  end;

  function TWMFNetWrite._Release: Integer; stdcall;
  begin
    result := 1;
  end;

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -