//---------------------------------------------------------------------------- // Laser Guard Ltd. // Copyright © 2000-2002. All Rights Reserved. // Version 1.0 01/01/2000 // Last modification: 07/12/2003 // PROJECT: // SUBSYSTEM: // FILE: LgMediaServer.cpp // AUTHOR: Sergey Chizhevskiy // OVERVIEW // ~~~~~~~~ // Source file of LgMediaServer.cpp //---------------------------------------------------------------------------- #include "stdafx.h" #include "LgMediaServer.h" CLgMediaServer::CLgMediaServer(void) { m_pProfileManager = NULL; m_pProfile = NULL; m_pWriter = NULL; m_pWriterAdvanced = NULL; m_pWriterFileSink = NULL; m_pWriterNetworkSink = NULL; m_dwWriterwPort = 0; LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgMediaServer::CLgMediaServer") ); } CLgMediaServer::~CLgMediaServer( void ) { Destroy(); LGLOG( LOG_LEVEL_MEDIUM, LOG_TYPE_INFO, this, _T("CLgMediaServer::~CLgMediaServer") ); } HRESULT STDMETHODCALLTYPE CLgMediaServer::QueryInterface( REFIID riid, void __RPC_FAR *__RPC_FAR * ppvObject ) { if( riid == IID_IWMStatusCallback ) *ppvObject = ( IWMStatusCallback* )this; else return E_NOINTERFACE; return S_OK; } HRESULT STDMETHODCALLTYPE CLgMediaServer::OnStatus( WMT_STATUS Status, HRESULT hr, WMT_ATTR_DATATYPE dwType, BYTE *pValue, void *pvContext ) { LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CLgMediaServer::OnStatus"), _T("Status %d"), Status ); return S_OK; } BOOL CLgMediaServer::Create( LPBITMAPINFOHEADER pInputSampleFormat ) { //////////////////////////////////////////////////////////////////////////////////// // Create the basic objects. HRESULT hr = WMCreateWriter( NULL, &m_pWriter ); //////////////////////////////////////////////////////////////////////////////////// // Retrieve a pointer to an IWMWriterAdvanced interface and add the sinks. if( SUCCEEDED( hr ) ) hr = m_pWriter->QueryInterface( IID_IWMWriterAdvanced, (void **)&m_pWriterAdvanced ); m_pWriterAdvanced->SetLiveSource( TRUE ); //////////////////////////////////////////////////////////////////////////////////// static INT nFileNameNumber = 0; /* if( SUCCEEDED( hr ) ) { IWMWriterFileSink *pSink = NULL; hr = WMCreateWriterFileSink( &pSink ); if( SUCCEEDED( hr ) ) hr = pSink->QueryInterface( IID_IWMWriterFileSink2, (void**)&m_pWriterFileSink ); } WCHAR pFileName[MAX_PATH]; _snwprintf( pFileName, MAX_PATH, L"Test%d.asf", nFileNameNumber ); if( SUCCEEDED( hr ) ) hr = m_pWriterFileSink->Open( pFileName ); if( SUCCEEDED( hr ) ) hr = m_pWriterAdvanced->AddSink( m_pWriterFileSink ); */ //////////////////////////////////////////////////////////////////////////////////// if( SUCCEEDED( hr ) ) hr = WMCreateWriterNetworkSink( &m_pWriterNetworkSink ); if( 0 ) { IWMAddressAccess *pAddressAccess = NULL; if( SUCCEEDED( hr ) ) hr = m_pWriterNetworkSink->QueryInterface( IID_IWMAddressAccess, (void**)&pAddressAccess ); if( SUCCEEDED( hr ) ) { WM_AETYPE eAddressAccessType = WM_AETYPE_INCLUDE; DWORD dwAddressAccessCount = 0; hr = pAddressAccess->GetAccessEntryCount( eAddressAccessType, &dwAddressAccessCount ); for( DWORD i = 0; i < dwAddressAccessCount; i++ ) { WM_ADDRESS_ACCESSENTRY AddressAccessEntry; hr = pAddressAccess->GetAccessEntry( eAddressAccessType, i, &AddressAccessEntry ); } WM_ADDRESS_ACCESSENTRY AddressAccessEntry; AddressAccessEntry.dwIPAddress = 0x7F000001;// 127.0.0.1; 0xC0A80132 - 192.168.1.50 AddressAccessEntry.dwMask = 0xFFFFFF00; hr = pAddressAccess->AddAccessEntry( WM_AETYPE_INCLUDE, &AddressAccessEntry ); pAddressAccess->Release(); } } if( SUCCEEDED( hr ) ) hr = m_pWriterNetworkSink->SetNetworkProtocol( WMT_PROTOCOL_HTTP ); // Setting the port number to zero enables the SDK to select an // appropriate port number. m_dwWriterwPort = 5000 + nFileNameNumber++; if( SUCCEEDED( hr ) ) hr = m_pWriterNetworkSink->Open( &m_dwWriterwPort ); if( SUCCEEDED( hr ) ) hr = m_pWriterAdvanced->AddSink( m_pWriterNetworkSink ); if( SUCCEEDED( hr ) ) hr = SetSinksForStatus( m_pWriter, this, TRUE ); DWORD dwURL = 0; if( SUCCEEDED( hr ) ) hr = m_pWriterNetworkSink->GetHostURL( NULL, &dwURL ); WCHAR* pwszURL = new WCHAR[dwURL]; if( pwszURL && SUCCEEDED( hr ) ) hr = m_pWriterNetworkSink->GetHostURL( pwszURL, &dwURL ); if( pwszURL && SUCCEEDED( hr ) ) LGLOG( LOG_LEVEL_LOW, LOG_TYPE_INFO, this, _T("CLgDirectShowCaptureGraph::CreateStream"), _T("%S"), pwszURL ); if( pwszURL ) delete pwszURL; if( SUCCEEDED( hr ) ) hr = ConfigureVideoProfile(); if( SUCCEEDED( hr ) ) hr = ConfigureVideoInput( m_pWriter, 0, WMMEDIASUBTYPE_RGB24, pInputSampleFormat->biWidth, pInputSampleFormat->biHeight ); // m_pWriterAdvanced->SetSyncTolerance( 10000 ); if( SUCCEEDED( hr ) ) hr = m_pWriter->BeginWriting(); return 0; } void CLgMediaServer::Destroy() { if( m_pWriter ) m_pWriter->EndWriting(); if( m_pWriterFileSink ) { if( m_pWriterAdvanced ) m_pWriterAdvanced->RemoveSink( m_pWriterFileSink ); m_pWriterFileSink->Close(); } if( m_pWriterNetworkSink ) { if( m_pWriterAdvanced ) m_pWriterAdvanced->RemoveSink( m_pWriterNetworkSink ); m_pWriterNetworkSink->Disconnect(); m_pWriterNetworkSink->Close(); } SetSinksForStatus( m_pWriter, this, FALSE ); if( m_pWriterFileSink ) { m_pWriterFileSink->Release(); m_pWriterFileSink = NULL; } if( m_pWriterNetworkSink ) { m_pWriterNetworkSink->Release(); m_pWriterNetworkSink = NULL; } if( m_pWriter ) { m_pWriter->Release(); m_pWriter = NULL; } if( m_pWriterAdvanced ) { m_pWriterAdvanced->Release(); m_pWriterAdvanced = NULL; } if( m_pProfile ) { m_pProfile->Release(); m_pProfile = NULL; } if( m_pProfileManager ) { m_pProfileManager->Release(); m_pProfileManager = NULL; } } HRESULT CLgMediaServer::FindInputFormat(IWMWriter* pWriter, DWORD dwInput, GUID guidSubType, IWMInputMediaProps** ppProps) { HRESULT hr = S_OK; DWORD cFormats = 0; DWORD cbSize = 0; WM_MEDIA_TYPE* pType = NULL; IWMInputMediaProps* pProps = NULL; // Set the ppProps parameter to point to NULL. This will // be used to check the results of the function later. *ppProps = NULL; // Find the number of formats supported by this input. hr = pWriter->GetInputFormatCount(dwInput, &cFormats); if( FAILED(hr) ) goto Exit; // Loop through all of the supported formats. for (DWORD formatIndex = 0; formatIndex < cFormats; formatIndex++) { // Get the input media properties for the input format. hr = pWriter->GetInputFormat(dwInput, formatIndex, &pProps); if( FAILED(hr) ) break; // Get the size of the media type structure. hr = pProps->GetMediaType(NULL, &cbSize); if( FAILED(hr) ) break; // Allocate memory for the media type structure. pType = (WM_MEDIA_TYPE*) new BYTE[cbSize]; if (pType == NULL) { hr = E_OUTOFMEMORY; goto Exit; } // Get the media type structure. hr = pProps->GetMediaType(pType, &cbSize); if( FAILED(hr) ) break; if(pType->subtype == guidSubType) { *ppProps = pProps; (*ppProps)->AddRef(); goto Exit; } // Clean up for next iteration. delete [] pType; pType = NULL; pProps->Release(); pProps = NULL; } // End for formatIndex. // If execution made it to this point, no matching format was found. hr = NS_E_INVALID_INPUT_FORMAT; Exit: if( pType ) delete [] pType; if( pProps ) pProps->Release(); return hr; } HRESULT CLgMediaServer::ConfigureVideoInput(IWMWriter* pWriter, DWORD dwInput, GUID guidSubType, LONG lFrameWidth, LONG lFrameHeight) { HRESULT hr = S_OK; DWORD cbSize = 0; LONG lStride = 0; IWMInputMediaProps* pProps = NULL; WM_MEDIA_TYPE* pType = NULL; WMVIDEOINFOHEADER* pVidHdr = NULL; BITMAPINFOHEADER* pBMHdr = NULL; // Get the base input format for the required subtype. hr = FindInputFormat(pWriter, dwInput, guidSubType, &pProps); if( FAILED(hr) ) goto Exit; // Get the size of the media type structure. hr = pProps->GetMediaType(NULL, &cbSize); if( FAILED(hr) ) goto Exit; // Allocate memory for the media type structure. pType = (WM_MEDIA_TYPE*) new BYTE[cbSize]; if (pType == NULL) { hr = E_OUTOFMEMORY; goto Exit; } // Get the media type structure. hr = pProps->GetMediaType(pType, &cbSize); if( FAILED(hr) ) goto Exit; // Adjust the format to match your source images. // First set pointers to the video structures. pVidHdr = (WMVIDEOINFOHEADER*) pType->pbFormat; pBMHdr = &(pVidHdr->bmiHeader); pBMHdr->biWidth = lFrameWidth; pBMHdr->biHeight = lFrameHeight; // Stride = (width * bytes/pixel), rounded to the next DWORD boundary. lStride = (lFrameWidth * (pBMHdr->biBitCount / 8) + 3) & ~3; // Image size = stride * height. pBMHdr->biSizeImage = lFrameHeight * lStride; // Apply the adjusted type to the video input. hr = pProps->SetMediaType(pType); if( FAILED(hr) ) goto Exit; hr = pWriter->SetInputProps(dwInput, pProps); Exit: if( pType ) delete [] pType; if( pProps ) pProps->Release(); pVidHdr = NULL; pBMHdr = NULL; return hr; } HRESULT CLgMediaServer::SetSinksForStatus (IWMWriter* pWriter, IWMStatusCallback* pStatus, BOOL bAdvise ) { HRESULT hr = S_OK; DWORD cSinks = 0; DWORD dwSinkIndex = 0; IWMWriterAdvanced* pWriterAdvanced = NULL; IWMWriterSink* pSink = NULL; IWMRegisterCallback* pRegisterCallbk = NULL; if( !pWriter ) return E_POINTER; // Get the advanced writer interface. hr = pWriter->QueryInterface(IID_IWMWriterAdvanced, (void**) &pWriterAdvanced); if( FAILED(hr) ) goto Exit; // Get the number of sinks that are added to the writer object. hr = pWriterAdvanced->GetSinkCount(&cSinks); if( FAILED(hr) ) goto Exit; // Loop through all of the sinks. for(dwSinkIndex = 0; dwSinkIndex < cSinks; dwSinkIndex++) { // Get the base interface for the next sink. hr = pWriterAdvanced->GetSink(dwSinkIndex, &pSink); if( FAILED(hr) ) goto Exit; // Get the callback registration interface for the sink. hr = pSink->QueryInterface(IID_IWMRegisterCallback, (void**)&pRegisterCallbk ); if( FAILED(hr) ) goto Exit; // Register the OnStatus callback. hr = bAdvise ? pRegisterCallbk->Advise( pStatus, this ) : pRegisterCallbk->Unadvise( pStatus, this ); if( FAILED(hr) ) goto Exit; // Release for the next iteration. pSink->Release(); pSink = NULL; pRegisterCallbk->Release(); pRegisterCallbk = NULL; } // end for dwSinkIndex Exit: if( pSink ) pSink->Release(); if( pRegisterCallbk ) pRegisterCallbk->Release(); if( pWriterAdvanced ) pWriterAdvanced->Release(); return hr; } BOOL CLgMediaServer::WriteSample( QWORD qTime, PBYTE pBuffer, INT nBufferSize ) { INSSBuffer *pWriterSample = NULL; HRESULT hr = m_pWriter->AllocateSample( nBufferSize, &pWriterSample ); if( FAILED(hr) || !pWriterSample ) return FALSE; LPBYTE pWriteBuffer = NULL; if( SUCCEEDED(hr) ) hr = pWriterSample->GetBuffer( &pWriteBuffer ); if( SUCCEEDED(hr) && pWriteBuffer ) { memcpy( pWriteBuffer, pBuffer, nBufferSize ); hr = pWriterSample->SetLength( nBufferSize ); } if( SUCCEEDED(hr) && pWriterSample ) hr = m_pWriter->WriteSample( 0, qTime, 0, pWriterSample ); if( pWriterSample ) pWriterSample->Release(); return SUCCEEDED(hr); } HRESULT CLgMediaServer::ConfigureVideoProfile() { // create profile manager HRESULT hr = WMCreateProfileManager( &m_pProfileManager ); if( SUCCEEDED( hr ) ) hr = m_pProfileManager->LoadProfileByID( WMProfile_V70_100Video, &m_pProfile ); DWORD dwStreamCount = 0; if( SUCCEEDED( hr ) ) hr = m_pProfile->GetStreamCount( &dwStreamCount ); for( DWORD i = 0; i < dwStreamCount; i++ ) { IWMStreamConfig *pConfig = NULL; HRESULT hr1 = m_pProfile->GetStream( i, &pConfig ); if( FAILED( hr1 ) || !pConfig ) continue; WORD wStreamNumber = 0; hr1 = pConfig->GetStreamNumber( &wStreamNumber ); IWMMediaProps *pMediaProps = NULL; hr1 = pConfig->QueryInterface( IID_IWMMediaProps, (void**)&pMediaProps ); DWORD dwMediaTypeSize = 0; if( SUCCEEDED( hr1 ) ) hr1 = pMediaProps->GetMediaType( NULL, &dwMediaTypeSize ); if( SUCCEEDED(hr1) ) { WM_MEDIA_TYPE *pMediaType = (WM_MEDIA_TYPE*)malloc( dwMediaTypeSize ); if( pMediaType ) { hr1 = pMediaProps->GetMediaType( pMediaType, &dwMediaTypeSize ); free( pMediaType ); } } GUID gMajorType; if( SUCCEEDED(hr1) ) hr1 = pMediaProps->GetType( &gMajorType ); if( SUCCEEDED(hr1) ) { if( gMajorType != WMMEDIATYPE_Video ) { m_pProfile->RemoveStreamByNumber( wStreamNumber ); i--; dwStreamCount--; } /* else { HRESULT hr1 = pConfig->SetBufferWindow( 1000 ); if( SUCCEEDED( hr1 ) ) hr1 = m_pProfile->ReconfigStream( pConfig ); } */ } if( pConfig ) pConfig->Release(); if( pMediaProps ) pMediaProps->Release(); } if( SUCCEEDED( hr ) ) hr = m_pProfile->GetStreamCount( &dwStreamCount ); if( SUCCEEDED( hr ) ) hr = m_pWriter->SetProfile( m_pProfile ); return S_OK; }