📄 netwrite.cpp
字号:
//*****************************************************************************
//
// Microsoft Windows Media
// Copyright ( C) Microsoft Corporation. All rights reserved.
//
// FileName: NetWrite.cpp
//
// Abstract: CNetWrite class implementation.
//
//
//*****************************************************************************
#include "wmsdk.h"
#include "NetWrite.h"
#include <TCHAR.H>
#include <stdio.h>
#include <assert.h>
#include <conio.h>
//----------------------------------------------------------------------------
// Name: CNetWrite::CNetWrite()
// Desc: Constructor.
//----------------------------------------------------------------------------
CNetWrite::CNetWrite()
{
m_pReaderHeaderInfo = NULL;
m_pWriterHeaderInfo = NULL;
m_pWriterAdvanced = NULL;
m_pReaderAdvanced = NULL;
m_pReader = NULL;
m_pWriter = NULL;
m_pNetSink = NULL;
m_pPushSink = NULL;
m_pPushSinkCallbackCtrl = NULL;
m_hEvent = NULL;
m_bEOF = false;
m_qwTime = 0;
m_hrAsync = S_OK;
}
//----------------------------------------------------------------------------
// Name: CNetWrite::~CNetWrite()
// Desc: Destructor.
//----------------------------------------------------------------------------
CNetWrite::~CNetWrite()
{
CloseHandle( m_hEvent );
SAFE_RELEASE( m_pWriterHeaderInfo );
SAFE_RELEASE( m_pWriterAdvanced );
SAFE_RELEASE( m_pWriter );
SAFE_RELEASE( m_pNetSink );
SAFE_RELEASE( m_pPushSinkCallbackCtrl );
SAFE_RELEASE( m_pPushSink );
SAFE_RELEASE( m_pReaderHeaderInfo );
SAFE_RELEASE( m_pReaderAdvanced );
SAFE_RELEASE( m_pReader );
CoUninitialize();
}
//----------------------------------------------------------------------------
// Name: CNetWrite::OnSample()
// Desc: Called when the reader object delivers an uncompressed sample.
// Note: This method implements the IWMReaderCallback::OnSample method. See the
// SDK documentation for a description of the parameters.
//
// The CNetWrite object configures the reader to deliver compressed
// samples, which happens via the OnStreamSample callback method.
// Therefore, if this method is called, it is an error.
//----------------------------------------------------------------------------
HRESULT CNetWrite::OnSample( DWORD dwOutputNum,
QWORD cnsSampleTime,
QWORD cnsSampleDuration,
DWORD dwFlags,
INSSBuffer __RPC_FAR *pSample, void __RPC_FAR *pvContext )
{
if( m_hEvent != NULL )
{
//
//The samples are expected in OnStreamSample.
//
_tprintf( _T( "Error: Received a decompressed sample from the reader.\n" ) );
m_hrAsync = E_UNEXPECTED;
SetEvent( m_hEvent );
}
return S_OK;
}
//----------------------------------------------------------------------------
// Name: CNetWrite::OnStatus()
// Desc: Receives a status message from the reader object or the push sink.
//
// Note: This method implements the IWMStatusCallback::OnStatus method. See the
// SDK documentation for a description of the parameters.
//----------------------------------------------------------------------------
HRESULT CNetWrite::OnStatus( WMT_STATUS Status,
HRESULT hr,
WMT_ATTR_DATATYPE dwType,
BYTE __RPC_FAR *pValue,
void __RPC_FAR *pvContext )
{
// Check whether the push sink object is the caller.
BOOL fFromPushSink = ( m_pPushSink != NULL && pvContext == ( void * ) m_pPushSink );
// For some status messages, we expect the CNetWrite object is waiting
// to receive the message. Signal to the waiting thread by calling SetEvent.
// We also signal to the thread if there is an error message.
// Handle status messages from the push sink.
if( fFromPushSink )
{
switch(Status)
{
case WMT_LOCATING:
_tprintf( _T( "Locating server.\n" ) );
break;
case WMT_CONNECTING:
_tprintf( _T( "Connecting to server.\n" ) );
break;
case WMT_OPENED:
_tprintf( _T( "Connected to server.\n" ) );
m_hrAsync = hr;
SetEvent( m_hEvent ); // Unblock the thread that is waiting.
break;
case WMT_ERROR:
_tprintf( _T( "Error 0x%x during push distribution\n" ), hr );
m_bEOF = true;
m_hrAsync = hr;
SetEvent( m_hEvent );
break;
}
return S_OK;
}
// Handle status messages from the reader.
switch(Status)
{
case WMT_OPENED:
if( SUCCEEDED( hr ))
{
_tprintf( _T( "The reader completed opening the input file.\n" ) );
}
m_hrAsync = hr;
SetEvent( m_hEvent ); // Unblock the thread that is waiting.
break;
case WMT_EOF:
m_bEOF = true;
_tprintf( _T( "EndOfStream detected in reader.\n" ) );
m_hrAsync = hr;
SetEvent( m_hEvent );
break;
case WMT_STARTED:
// Ask the reader object to deliver another block of time.
m_qwTime = 0;
m_qwTime += 1000 * 10000;
hr = m_pReaderAdvanced->DeliverTime( m_qwTime );
m_hrAsync = hr;
break;
case WMT_CLOSED:
m_hrAsync = hr;
SetEvent( m_hEvent ); // Unblock the thread that is waiting.
break;
case WMT_ERROR:
_tprintf( _T( "Error 0x%x reported by the reader\n" ), hr );
m_bEOF = true;
m_hrAsync = hr;
SetEvent( m_hEvent );
break;
}
return S_OK;
}
//----------------------------------------------------------------------------
// Name: CNetWrite::OnStreamSample()
// Desc: Called when the reader object delivers a compressed sample.
//
// Note: This method implements the IWMReaderCallbackAdvanced::OnStreamSample
// method. See the SDK documentation for a description of the parameters.
//----------------------------------------------------------------------------
HRESULT CNetWrite::OnStreamSample( WORD wStreamNum,
QWORD cnsSampleTime,
QWORD cnsSampleDuration,
DWORD dwFlags,
INSSBuffer __RPC_FAR *pSample,
void __RPC_FAR *pvContext )
{
_tprintf( _T( "StreamSample: num=%d, time=%d, duration=%d, flags=%d.\n" ),
wStreamNum, ( DWORD )cnsSampleTime, cnsSampleDuration, dwFlags );
if( m_pWriterAdvanced != NULL )
{
// Give the sample to the writer object.
HRESULT hr = m_pWriterAdvanced->WriteStreamSample( wStreamNum,
cnsSampleTime,
0,
cnsSampleDuration,
dwFlags,
pSample );
if( FAILED( hr ) )
{
_tprintf( _T( "Error 0x%x reported by the writer\n" ), hr );
m_bEOF = true;
m_hrAsync = hr;
SetEvent( m_hEvent );
}
}
return S_OK;
}
//----------------------------------------------------------------------------
// Name: CNetWrite::OnTime()
// Desc: Called when the reader object has delivered all of the data that this
// object requested.
//
// Note: This method implements the IWMReaderCallbackAdvanced::OnTime method.
// See the SDK documentation for a description of the parameters.
//
// This method gets called because the CNetWrite object is driving the
// clock on the reader object.
//----------------------------------------------------------------------------
HRESULT CNetWrite::OnTime( QWORD cnsCurrentTime, void __RPC_FAR *pvContext)
{
// Until the end of the file is reached, ask for another block of time.
if( !m_bEOF )
{
m_qwTime += 10000000;
HRESULT hr=m_pReaderAdvanced->DeliverTime( m_qwTime );
if(FAILED(hr))
return hr;
}
return S_OK;
}
//----------------------------------------------------------------------------
// Name: CNetWrite::Init()
// Desc: Initializes the CNetWrite object.
//----------------------------------------------------------------------------
HRESULT CNetWrite::Init()
{
HRESULT hr = S_OK;
// Initialize the COM library.
hr = CoInitialize( NULL );
if( FAILED( hr ) )
{
_tprintf( _T( "CoInitialize failed: hr = 0x%08x\n" ), hr );
return hr;
}
//
// Create the reader and writer. Query for additional interfaces.
//
hr = WMCreateReader( NULL, 0, &m_pReader );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not create reader (hr=0x%08x).\n" ), hr );
return hr;
}
hr = m_pReader->QueryInterface( IID_IWMReaderAdvanced, ( void** )&m_pReaderAdvanced );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not QI for IWMReaderAdvanced (hr=0x%08x).\n" ), hr );
return hr;
}
hr = WMCreateWriter( NULL, &m_pWriter );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not create Writer (hr=0x%08x).\n" ), hr );
return hr;
}
hr = m_pWriter->QueryInterface( IID_IWMWriterAdvanced, ( void** ) &m_pWriterAdvanced );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not QI for IWMWriterAdvanced (hr=0x%08x).\n" ), hr );
return hr;
}
hr = m_pReader->QueryInterface( IID_IWMHeaderInfo, ( VOID ** )&m_pReaderHeaderInfo );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not QI for IWMHeaderInfo (hr=0x%08x).\n" ), hr );
return hr;
}
hr = m_pWriter->QueryInterface( IID_IWMHeaderInfo, ( VOID ** )&m_pWriterHeaderInfo );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not QI for IWMHeaderInfo (hr=0x%08x).\n" ), hr );
return hr;
}
return hr;
}
//----------------------------------------------------------------------------
// Name: CNetWrite::WritetoNet()
// Desc: Writes a file to the network. Call the Configure method first, to set
// the file name and other parameters.
//----------------------------------------------------------------------------
HRESULT CNetWrite::WritetoNet()
{
if( m_hEvent == NULL ||
m_pWriterAdvanced == NULL ||
m_pReaderAdvanced == NULL ||
( m_pNetSink == NULL && m_pPushSink == NULL ) )
{
return E_UNEXPECTED;
}
HRESULT hr = S_OK;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -