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

📄 netwrite.cpp

📁 wmvnetwrite.介绍将把微软的媒体格式流化到网络端口的代码
💻 CPP
📖 第 1 页 / 共 3 页
字号:
	do
	{
			hr = m_pReader->QueryInterface( IID_IWMProfile, ( VOID ** )&pProfile );
			if( FAILED( hr ) )
			{
				_tprintf( _T(  "Could not QI for IWMProfile (hr=0x%08x).\n" ), hr );
				break;
			}
		    
            // Loop through all the output streams on the reader. For each stream, configure 
            // the reader to deliver compressed samples. This prevents the reader from decoding
            // each sample. (We want to give the writer compressed samples.)

			DWORD dwStreams = 0;
			hr = pProfile->GetStreamCount( &dwStreams );
			if( FAILED( hr ) )
			{
				_tprintf( _T(  "GetStreamCount on IWMProfile failed (hr=0x%08x).\n" ), hr );
				break;
			}
		    
			for ( DWORD i = 0; i < dwStreams; i++ )
			{
				hr = pProfile->GetStream( i, &pStream );
				if( FAILED( hr ) )
				{
					_tprintf( _T(  "Could not get Stream %d of %d from IWMProfile (hr=0x%08x).\n" ),
						i, dwStreams, hr );
					break;
				}
		        
				WORD wStreamNumber = 0;
				//
				//Get the stream number of the current stream.
				//
				hr = pStream->GetStreamNumber( &wStreamNumber );
				if( FAILED( hr ) )
				{
					_tprintf( _T(  "Could not get stream number from IWMStreamConfig %d of %d (hr=0x%08x).\n" ),
						i, dwStreams, hr );
					break;
				}
		        
				SAFE_RELEASE( pStream );
				//
				//Set the stream to be received in compressed mode.
				//
				hr = m_pReaderAdvanced->SetReceiveStreamSamples( wStreamNumber, TRUE );
				if( FAILED( hr ) )
				{
					_tprintf( _T(  "Could not SetReceivedStreamSamples for stream number %d (hr=0x%08x).\n" ),
						wStreamNumber, hr );
					break;
				}
			}
		    
			if( FAILED( hr ) )
				break;

			//
			// Turn on the user clock. The CNetWrite object will drive the clock, in order
            // to make the reader run as quickly as possible.
			//
			hr = m_pReaderAdvanced->SetUserProvidedClock( TRUE );
			if( FAILED( hr ) )
			{
				_tprintf( _T( "SetUserProvidedClock failed (hr=0x%08x).\n" ), hr );
				break;
			}
		    
			//
			// Set same profile on the writer object that we got from the reader.
			//
			hr = m_pWriter->SetProfile( pProfile );
			if( FAILED( hr ) )
			{
				_tprintf( _T(  "Could not set profile on IWMWriter (hr=0x%08x).\n" ), hr );
				break;
			}
	} while(FALSE);

	SAFE_RELEASE( pStream );
    SAFE_RELEASE( pProfile );

	if(FAILED(hr))
	{
		return hr;		// Something has failed within the do-while loop. 
	}
    
    DWORD   cInputs = 0;
    
    hr = m_pWriter->GetInputCount( &cInputs );
    if( FAILED( hr ) )
    {
        _tprintf( _T(  "Could not get input count from IWMWriter (hr=0x%08x).\n" ), hr );
        return hr;
    }

    // Loop through all of the inputs on the writer. For each input, set the 
    // input properties to NULL. This tells the writer not to encode the
    // samples it receives. We do this because we are getting encoded samples
    // from the reader.

    for( DWORD i = 0; i < cInputs; i++ )
    {
        hr = m_pWriter->SetInputProps( i, NULL );
		if(FAILED(hr))
		{
			return hr;
		}
    }
    
    //
    // Copy header attributes from the reader to the writer. 
    //
    hr = WriteHeader( g_wszWMTitle);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMAuthor);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMDescription);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMRating);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMCopyright);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMAlbumTitle);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMTrack);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMPromotionURL);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMAlbumCoverURL);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMGenre);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMYear);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMGenreID);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMMCDI);
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMBannerImageType );
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMBannerImageData );
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMBannerImageURL );
    if( FAILED( hr ) )
        return hr;
    
    hr = WriteHeader( g_wszWMCopyrightURL );
    if( FAILED( hr ) )
        return hr;
    
    //
    // Header has been written. Write the script (if any).
    //
    hr = WriteScript();
    
    return hr;
}

//----------------------------------------------------------------------------
// Name: CNetWrite::WriteHeader()
// Desc: Copies specified metadata from the reader to the writer.
//
// pwszName: Specifies the name of the metadata attribute to copy.
//----------------------------------------------------------------------------

HRESULT CNetWrite::WriteHeader(const WCHAR *pwszName)
{
    WORD				nstreamNum = 0;
    WORD				cbLength = 0;
    WMT_ATTR_DATATYPE	type;
    HRESULT				hr = S_OK;
    BYTE*				pValue = NULL;
    
    //
    // Get the number of bytes to allocate for the attribute data.
    //
    hr = m_pReaderHeaderInfo->GetAttributeByName( &nstreamNum,
        pwszName,
        &type,
        NULL,
        &cbLength );
    if( FAILED( hr ) && hr != ASF_E_NOTFOUND )
    {
        _tprintf( _T( "GetAttributeByName failed for Attribute name %ws (hr=0x%08x).\n" ), pwszName, hr);
        return hr;
    }
    
    if( cbLength == 0 && hr == ASF_E_NOTFOUND )
    {
        return S_OK;
    }
    
    // Allocate a buffer.
    pValue = new BYTE[ cbLength ];
    if( NULL == pValue )
    {
        _tprintf( _T( "Unable to allocate memory for the Attribute name %ws" ), pwszName);
        return E_OUTOFMEMORY;
    }

    // Declare a dummy 'do' loop. On failure, we can break from the loop.
    do
    {
        // Get the value of the attribute from the reader.
        hr = m_pReaderHeaderInfo->GetAttributeByName( &nstreamNum,
            pwszName,
            &type,
            pValue,
            &cbLength );
        if( FAILED( hr ) )
        {
            _tprintf( _T( "GetAttributeByName failed for Attribute name %ws (hr=0x%08x).\n" ),
                pwszName, hr);
            break;
        }

        // Set the attribute on the writer.
        hr = m_pWriterHeaderInfo->SetAttribute( nstreamNum,
						pwszName,
                                                type,
                                                pValue,
                                                cbLength );
        if( FAILED( hr ) )
        {
            _tprintf( _T( "SetAttribute failed for Attribute name %ws (hr=0x%08x).\n" ),
                pwszName, hr);
            break;
        }
    }
    while(FALSE);
    
    SAFE_ARRAYDELETE( pValue );
    return hr;
}

//----------------------------------------------------------------------------
// Name: CNetWrite::WriteScript()
// Desc: Copies all scripts from the reader's header to the writer's header.
//----------------------------------------------------------------------------
HRESULT CNetWrite::WriteScript()
{
    
    HRESULT	hr = S_OK;
    WCHAR*	pwszCommand = NULL;
    WCHAR*	pwszType = NULL;
    QWORD	cnsScriptTime = 0;
    WORD	cScript = 0;
    WORD  	cchTypeLen = 0;
    WORD	cchCommandLen = 0;
    

    // Get the number of scripts in the header. This might be zero.
    hr = m_pReaderHeaderInfo->GetScriptCount( &cScript );
    if( FAILED( hr ) )
    {
        _tprintf( _T( "GetScriptCount failed (hr=0x%08x).\n" ), hr);
        return hr;
    }
    
    // Loop through the scripts. For each script, copy it to the writer's header.
    for( int i = 0; i < cScript; i++)
    {
        //
        // Get the memory required for this script.
        //
        hr = m_pReaderHeaderInfo->GetScript( (WORD)i ,
					                         NULL ,
                                             &cchTypeLen ,
                                             NULL ,
                                             &cchCommandLen ,
                                             &cnsScriptTime );
        if( FAILED( hr ) )
        {
            _tprintf( _T( "GetScript failed for Script no %d (hr=0x%08x).\n" ), i, hr );
            break;
        }
        
        // Allocate buffers.
        pwszType	= new WCHAR[cchTypeLen];
        pwszCommand = new WCHAR[cchCommandLen];
        
        if( pwszType == NULL || pwszCommand == NULL)
        {
            _tprintf( _T( "Insufficient Memory" ) );
            hr = E_OUTOFMEMORY;
            break;
        }
        //
        // Get the script.
        //
        hr = m_pReaderHeaderInfo->GetScript( (WORD)i ,
					                         pwszType ,
                                             &cchTypeLen ,
                                             pwszCommand ,
                                             &cchCommandLen ,
                                             &cnsScriptTime );
        if( FAILED( hr ) )
        {
            _tprintf( _T( "GetScript failed for Script no %d (hr=0x%08x).\n" ), i, hr);
            break;
        }
        //
        // Add the script to the writer.
        //
        hr = m_pWriterHeaderInfo->AddScript( pwszType ,
					     pwszCommand ,
                                             cnsScriptTime );
        if( FAILED( hr ) )
        {
            _tprintf( _T( "AddScript failed for Script no %d (hr=0x%08x).\n" ), i, hr);
            break;
        }
        
        SAFE_ARRAYDELETE( pwszType );
        SAFE_ARRAYDELETE( pwszCommand );
        
        pwszType = pwszCommand = NULL;
        
        cchTypeLen		= 0;
        cchCommandLen	= 0;
    }
    
    SAFE_ARRAYDELETE( pwszType );
    SAFE_ARRAYDELETE( pwszCommand );
    
    return hr;	
}

⌨️ 快捷键说明

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