📄 netwrite.cpp
字号:
//
// Start writing.
//
// First, prepare the writer object for writing. Then start the reader.
hr = m_pWriter->BeginWriting( );
if( FAILED( hr ) )
{
_tprintf( _T( "BeginWriting on IWMWriter failed (hr=0x%08x).\n" ), hr );
return hr;
}
hr = m_pReader->Start( 0, 0, 1.0, 0 );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not start IWMReader (hr=0x%08x).\n" ), hr );
return hr;
}
// Wait for all of the read and write operations to finish.
WaitForSingleObject( m_hEvent, INFINITE );
// Check the status of the operation.
if( FAILED( m_hrAsync ) )
{
_tprintf( _T( "Net writing failed (hr=0x%08x).\n" ), m_hrAsync );
return hr;
}
//
// Stop the reader and the writer, and close everything.
//
hr = m_pReader->Stop();
if( FAILED( hr ) )
{
_tprintf( _T( "Could not Stop IWMReader (hr=0x%08x).\n" ), hr );
return hr;
}
hr = m_pWriter->Flush();
if( FAILED( hr ) )
{
_tprintf( _T( "Could not Flush on IWMWriter (hr=0x%08x).\n" ), hr );
return hr;
}
hr = m_pWriter->EndWriting( );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not EndWriting on IWMWriter (hr=0x%08x).\n" ), hr );
return hr;
}
hr = m_pReader->Close();
if( FAILED( hr ) )
{
_tprintf( _T( "Could not close the file (hr=0x%08x).\n" ), hr );
return hr;
}
// Remove the network sink from the writer.
if( m_pNetSink != NULL )
{
hr = m_pWriterAdvanced->RemoveSink( m_pNetSink );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not remove the Network Sink (hr=0x%08x).\n" ), hr );
return hr;
}
hr = m_pNetSink->Close();
if( FAILED( hr ) )
{
_tprintf( _T( "Could not close on IWMWriterNetworkSink (hr=0x%08x).\n" ), hr );
return hr;
}
}
// Remove the push sink from the writer.
if( m_pPushSink != NULL )
{
hr = m_pWriterAdvanced->RemoveSink( m_pPushSink );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not remove the Push Sink (hr=0x%08x).\n" ), hr );
return hr;
}
// End the push session and cancel the advise connection with the push sink.
hr = m_pPushSink->EndSession();
if( FAILED( hr ) )
{
_tprintf( _T( "Could not close on IWMWriterPushSink (hr=0x%08x).\n" ), hr );
return hr;
}
if( m_pPushSinkCallbackCtrl != NULL )
{
hr = m_pPushSinkCallbackCtrl->Unadvise( this, ( void * ) m_pPushSink );
if(FAILED(hr))
return hr;
}
}
//
// Wait for the reader to tell us that it is closed.
//
WaitForSingleObject( m_hEvent, 10000 );
return hr;
}
//----------------------------------------------------------------------------
// Name: CNetWrite::Configure()
// Desc: Configures the object to broadcast a file.
//
// dwPortNum: Port number for the network sink. Use -1 for no network sink.
// pwszFile: The name of a local ASF file to broadcast. Cannot be NULL.
// nMaxClient: Maximum number of client connections.
// pwszServerURL: URL of a publishing point, for the push sink. Use NULL for no push sink.
//
// Note: If dwPortNum is -1, pwszServerURL cannot be NULL, and vice-versa.
//----------------------------------------------------------------------------
HRESULT CNetWrite::Configure( DWORD dwPortNum, const WCHAR *pwszFile, UINT nMaxClient, const WCHAR *pwszServerURL )
{
if( pwszFile == NULL || ( dwPortNum == (DWORD)-1 && pwszServerURL == NULL ) )
{
return E_INVALIDARG;
}
if( dwPortNum > 0xFFFF )
{
_tprintf( _T( "Invalid port number.\n" ) );
return E_INVALIDARG;
}
if( m_pWriterAdvanced == NULL || m_pReaderAdvanced == NULL )
{
return E_UNEXPECTED;
}
HRESULT hr = S_OK;
// If a port number was specified, create the network sink object.
if( dwPortNum != (DWORD)-1 )
{
hr = WMCreateWriterNetworkSink( &m_pNetSink );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not create Writer Network Sink (hr=0x%08x).\n" ), hr );
return hr;
}
}
// If a publishing point URL was specified, create the push sink object.
if( pwszServerURL != NULL )
{
hr = WMCreateWriterPushSink( &m_pPushSink );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not create Writer Push Sink (hr=0x%08x).\n" ), hr );
return hr;
}
// Set up a callback for event notifications.
hr = m_pPushSink->QueryInterface( IID_IWMRegisterCallback,
( void ** ) &m_pPushSinkCallbackCtrl );
if( FAILED( hr ) )
{
return hr;
}
hr = m_pPushSinkCallbackCtrl->Advise( this, ( void * ) m_pPushSink );
if( FAILED( hr ) )
{
return hr;
}
}
//
// Create an event for handling asynchronous calls.
//
m_hrAsync = S_OK;
if( m_hEvent != NULL)
{
CloseHandle(m_hEvent);
}
m_hEvent = CreateEvent(NULL, FALSE, FALSE, NULL );
if( NULL == m_hEvent )
{
DWORD err = GetLastError();
_tprintf( _T( "Could not Create Event: (hr=0x%08x).\n" ), err );
return err;
}
if( m_pNetSink != NULL )
{
//
// Configure the network sink.
//
hr = m_pNetSink->SetNetworkProtocol( WMT_PROTOCOL_HTTP );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not Set Network protocol (hr=0x%08x).\n" ), hr );
return hr;
}
hr = m_pNetSink->Open( &dwPortNum);
if( FAILED( hr ) )
{
_tprintf( _T( "Network sink failed to open port no %d (hr=0x%08x).\n" ),
dwPortNum, hr );
return hr;
}
// Find the host URL, to display to the user.
DWORD cchURL = 0;
hr = m_pNetSink->GetHostURL( NULL, &cchURL );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not get the host URL from IWMWriterNEtworkSink (hr=0x%08x).\n" ),
hr );
return hr;
}
WCHAR *pwszURL = new WCHAR[cchURL];
if( pwszURL == NULL )
{
_tprintf( _T( "Insufficient Memory" ) );
return E_OUTOFMEMORY;
}
hr = m_pNetSink->GetHostURL( pwszURL, &cchURL );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not get the host URL from IWMWriterNetworkSink (hr=0x%08x).\n" ),
hr );
SAFE_ARRAYDELETE (pwszURL);
return hr;
}
_tprintf( _T( "Connect to %ws\n" ), pwszURL );
Sleep( 1000 );
SAFE_ARRAYDELETE (pwszURL);
//
// Set the maximum number of clients that can connect to the port.
//
hr = m_pNetSink->SetMaximumClients( nMaxClient );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not Set maximum clients (hr=0x%08x).\n" ), hr );
return hr;
}
//
// Add the network sink to the writer.
//
hr = m_pWriterAdvanced->AddSink(m_pNetSink);
if( FAILED( hr ) )
{
_tprintf( _T( "Could not Add Sink (hr=0x%08x).\n" ), hr );
return hr;
}
}
if( m_pPushSink != NULL )
{
//
// Configure the push sink.
//
_tprintf( _T( "Connecting to %ws\n" ), pwszServerURL );
hr = m_pPushSink->Connect( pwszServerURL, NULL, TRUE );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not connect to server (hr=0x%08x).\n" ), hr );
return hr;
}
//
// Wait for the connection to the server to be established.
//
if( WaitForSingleObject( m_hEvent, 30000 ) == WAIT_TIMEOUT )
{
_tprintf( _T( "Timed out whiletrying to connect to the server\n" ) );
return E_FAIL;
}
if( FAILED( m_hrAsync ) )
{
return m_hrAsync;
}
//
// Add the push sink to the writer object.
//
hr = m_pWriterAdvanced->AddSink(m_pPushSink);
if( FAILED( hr ) )
{
_tprintf( _T( "Could not Add Sink (hr=0x%08x).\n" ), hr );
return hr;
}
}
//
// Open the requested file.
//
hr = m_pReader->Open(pwszFile, this, NULL );
if( FAILED( hr ) )
{
_tprintf( _T( "Could not open file %ws (hr=0x%08x).\n" ), pwszFile ,hr );
return hr;
}
//
// Wait for the open to finish.
//
if( WaitForSingleObject( m_hEvent, 30000 ) == WAIT_TIMEOUT )
{
_tprintf( _T( "Timed out whiletrying to open the input file\n" ) );
return E_FAIL;
}
if( FAILED( m_hrAsync ) )
{
hr = m_hrAsync;
_tprintf( _T( "Open failed (hr=0x%08x).\n" ), m_hrAsync );
return hr;
}
//
// Turn on manual stream selection, so we get all streams. This prevents the reader
// from disabling any streams if there are mutual exclusions on some streams.
//
hr = m_pReaderAdvanced->SetManualStreamSelection( TRUE );
if( FAILED( hr ) )
{
_tprintf( _T( "Failed to set manual stream selection (hr=0x%08x).\n" ), hr );
return hr;
}
IWMProfile *pProfile = NULL;
IWMStreamConfig *pStream = NULL;
// Declare a dummy 'do' loop. On failure, we can break from the loop.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -