BufferRef HeaderInterface::removeHeader( const BufferRef& buffer )
{
	string msg		= bufferToString( buffer );
	size_t offset	= msg.find( sCrLf + sCrLf );
	size_t sz		= buffer->getSize();
	if ( offset < sz ) {
		size_t len = ( sz - offset ) - 4;
		BufferRef body = Buffer::create( len );
		char_traits<char>::copy( (char*)body->getData(), (char*)buffer->getData() + ( offset + 4 ), len );
		return body;
	} else {
		return Buffer::create( 0 );
	}
}
BufferRef COBSEncode(const BufferRef& buffer) {
    size_t maxEncodedSize = buffer->getSize() + ceil(static_cast<float>(buffer->getSize()) / 254) + 1;
    BufferRef encodeBuffer = Buffer::create(maxEncodedSize);
    size_t encodedSize = COBSEncode(reinterpret_cast<const uint8_t*>(buffer->getData()), buffer->getSize(), reinterpret_cast<uint8_t*>(encodeBuffer->getData()));
    encodeBuffer->resize(encodedSize);
    return encodeBuffer;
}
BufferRef SLIPEncode(const BufferRef& buffer) {
    size_t maxEncodedSize = 2 * buffer->getSize() + 2;
    BufferRef encodeBuffer = Buffer::create(maxEncodedSize);
    size_t encodedSize = SLIPEncode(reinterpret_cast<const uint8_t*>(buffer->getData()), buffer->getSize(), reinterpret_cast<uint8_t*>(encodeBuffer->getData()));
    encodeBuffer->resize(encodedSize);
    return encodeBuffer;
}
BufferRef COBSDecode(const BufferRef& buffer) {
    size_t maxDecodedSize = buffer->getSize() - 1;
    BufferRef decodeBuffer = Buffer::create(maxDecodedSize);
    size_t decodedSize = COBSDecode(reinterpret_cast<const uint8_t*>(buffer->getData()), buffer->getSize(), reinterpret_cast<uint8_t*>(decodeBuffer->getData()));
    decodeBuffer->resize(decodedSize);
    return decodeBuffer;
}
void CinderVideoStreamServerApp::update()
{

    if( mCapture && mCapture->checkNewFrame() ) {
        Surface8uRef surf = mCapture->getSurface();
#ifdef USE_JPEG_COMPRESSION
        OStreamMemRef os = OStreamMem::create();
        DataTargetRef target = DataTargetStream::createRef( os );
        writeImage( target, *surf, ImageTarget::Options().quality(mQuality), "jpeg" );
        const void *data = os->getBuffer();
        size_t dataSize = os->tell();
        totalStreamSize += dataSize;

        BufferRef bufRef = Buffer::create(dataSize);
        memcpy(bufRef->getData(), data, dataSize);
        SurfaceRef jpeg = Surface::create(loadImage( DataSourceBuffer::create(bufRef)), SurfaceConstraintsDefault(), false );
        queueToServer->push(jpeg->getData());
        mTexture = gl::Texture::create( *jpeg );
        
        mStatus.assign("Streaming JPG (")
               .append(std::to_string((int)(mQuality*100.0f)))
               .append("%) ")
               .append(std::to_string((int)(totalStreamSize*0.001/getElapsedSeconds())))
               .append(" kB/sec ")
               .append(std::to_string((int)getFrameRate()))
               .append(" fps ");
#else
        queueToServer->push(surf->getData());
        mTexture = gl::Texture::create( *surf );
        mStatus.assign("Streaming ").append(std::to_string((int)getFrameRate())).append(" fps");
#endif
        
    }
}
BufferRef SLIPDecode(const BufferRef& buffer) {
    // should not assume double-ENDed variant
    size_t maxDecodedSize = buffer->getSize() - 1;
    BufferRef decodeBuffer = Buffer::create(maxDecodedSize);
    size_t decodedSize = SLIPDecode(reinterpret_cast<const uint8_t*>(buffer->getData()), buffer->getSize(), reinterpret_cast<uint8_t*>(decodeBuffer->getData()));
    decodeBuffer->resize(decodedSize);
    return decodeBuffer;
}
BufferRef HeaderInterface::toBuffer() const
{
	string header	= headerToString();
	size_t sz		= header.size();
	
	BufferRef buffer = Buffer::create( sz );
	char_traits<char>::copy( (char*)buffer->getData(), (char*)&header[ 0 ], sz );
	
	return buffer;
}
Exemple #8
0
void TcpSession::write( const BufferRef& buffer )
{
	ostream stream( &mRequest );
	if ( buffer && buffer->getSize() > 0 ) {
		stream.write( (const char*)buffer->getData(), buffer->getSize() );
	}
	asio::async_write( *mSocket, mRequest, 
		mStrand.wrap( boost::bind( &TcpSession::onWrite, shared_from_this(), 
			asio::placeholders::error, 
			asio::placeholders::bytes_transferred ) ) );
	mRequest.consume( mRequest.size() );
}
Exemple #9
0
void UdpSession::write( const BufferRef& buffer )
{
	ostream stream( &mRequest );
	if ( buffer && buffer->getSize() > 0 ) {
		stream.write( (const char*)buffer->getData(), buffer->getSize() );
	}
	mSocket->async_send( mRequest.data(), 
		mStrand.wrap( boost::bind( &UdpSession::onWrite, shared_from_this(), 
			asio::placeholders::error, 
			asio::placeholders::bytes_transferred ) ) );
	mSocket->set_option( asio::socket_base::broadcast( true ) );
	mEndpointLocal = mSocket->local_endpoint();
	mRequest.consume( mRequest.size() );
}
Exemple #10
0
ImFont* Renderer::addFont( ci::DataSourceRef font, float size, const ImWchar* glyph_ranges )
{
	ImFontAtlas* fontAtlas  = ImGui::GetIO().Fonts;
	
	Font ciFont( font, size );
	
	BufferRef buffer		= font->getBuffer();
	void* bufferCopy        = (void*) malloc( buffer->getSize() );
	memcpy( bufferCopy, buffer->getData(), buffer->getSize() );
	
	ImFontConfig config;
	ImFont* newFont         = fontAtlas->AddFontFromMemoryTTF( bufferCopy, buffer->getSize(), size, &config, glyph_ranges );
	
	mFonts.insert( make_pair( font->getFilePath().stem().string(), newFont ) );
	return newFont;
}
void PS3EyeSlowMoApp::update()
{
    if(eye)
    {
        bool isNewFrame = eye->isNewFrame();
        if(isNewFrame)
        {
            yuv422_to_rgba(eye->getLastFramePointer(), eye->getRowBytes(), frame_bgra, mFrame.getWidth(), mFrame.getHeight());
            
            //Surface source = mFrame.clone();
            
            OStreamMemRef os = OStreamMem::create();
            
            DataTargetRef target = DataTargetStream::createRef( os );
            
            writeImage( target, mFrame, ImageTarget::Options(), "jpeg" );

            const void *data = os->getBuffer();
            
            size_t dataSize = os->tell();

            BufferRef buf = Buffer::create(dataSize );
            memcpy(buf->getData(), data, dataSize);
            surfaceVector.push_back(Surface( loadImage( DataSourceBuffer::create(buf)), SurfaceConstraintsDefault(), false ));
        }
        mCamFrameCount += isNewFrame ? 1 : 0;
        double now = mTimer.getSeconds();
        if( now > mCamFpsLastSampleTime + 1 ) {
            uint32_t framesPassed = mCamFrameCount - mCamFpsLastSampleFrame;
            mCamFps = (float)(framesPassed / (now - mCamFpsLastSampleTime));
            
            mCamFpsLastSampleTime = now;
            mCamFpsLastSampleFrame = mCamFrameCount;
        }
        mFrameRate = eye->getFrameRate();
    }
    
    mQueueSize = surfaceVector.size();
    currentFrame++;
}
Exemple #12
0
ImageSourceFileTinyExr::ImageSourceFileTinyExr( DataSourceRef dataSource, ImageSource::Options /*options*/ )
{
	mExrImage.reset( new EXRImage );
	const char *error;

	InitEXRImage( mExrImage.get() );

	int status = 0;
	if( dataSource->isFilePath() ) {
		status = ParseMultiChannelEXRHeaderFromFile( mExrImage.get(), dataSource->getFilePath().string().c_str(), &error );
		if( status != 0 )
			throw ImageIoExceptionFailedLoadTinyExr( string( "Failed to parse OpenEXR header; Error message: " ) + error );
		status = LoadMultiChannelEXRFromFile( mExrImage.get(), dataSource->getFilePath().string().c_str(), &error );
		if( status != 0 )
			throw ImageIoExceptionFailedLoadTinyExr( string( "Failed to parse OpenEXR file; Error message: " ) + error );
	}
	else {
		BufferRef buffer = dataSource->getBuffer();
		
		status = ParseMultiChannelEXRHeaderFromMemory( mExrImage.get(), (const unsigned char*)buffer->getData(), &error );
		if( status != 0 )
			throw ImageIoExceptionFailedLoadTinyExr( string( "Failed to parse OpenEXR header; Error message: " ) + error );
		status = LoadMultiChannelEXRFromMemory( mExrImage.get(), (const unsigned char*)buffer->getData(), &error );
		if( status != 0 )
			throw ImageIoExceptionFailedLoadTinyExr( string( "Failed to parse OpenEXR file; Error message: " ) + error );
	}

	// verify that the channels are all the same size; currently we don't support variably sized channels
	int pixelType = mExrImage->pixel_types[0];
	for( int c = 1; c < mExrImage->num_channels; ++c ) {
		if( pixelType != mExrImage->pixel_types[c] )
			throw ImageIoExceptionFailedLoadTinyExr( "TinyExr: heterogneous channel data types not supported" );
	}

	switch( pixelType ) {
		case TINYEXR_PIXELTYPE_HALF:
			setDataType( ImageIo::FLOAT16 );
		break;
		case TINYEXR_PIXELTYPE_FLOAT:
			setDataType( ImageIo::FLOAT32 );
		break;
		default:
			throw ImageIoExceptionFailedLoadTinyExr( "TinyExr: Unknown data type" );
		break;
	}

	setSize( mExrImage->width, mExrImage->height );

	switch( mExrImage->num_channels ) {
		case 3:
			setColorModel( ImageIo::CM_RGB );
			setChannelOrder( ImageIo::ChannelOrder::RGB );
		break;
		case 4:
			setColorModel( ImageIo::CM_RGB );
			setChannelOrder( ImageIo::ChannelOrder::RGBA );
		break;
		default:
			throw ImageIoExceptionFailedLoadTinyExr( "TinyExr: Unsupported number of channels (" + to_string( mExrImage->num_channels ) + ")" );
	}
}
void PusherDiscoveryService::onRead( BufferRef buffer )
{
//    mPushersMutex.lock();
    
    uint8_t         *data   =(uint8_t*)buffer->getData();
    DeviceHeader    header  = DeviceHeader( data, buffer->getSize() );
    string          macAddr = header.getMacAddressString();
    PixelPusherRef  thisDevice, incomingDevice;
    
    // ignore non-PixelPusher devices
    if ( header.getDeviceType() != DeviceType::PIXELPUSHER )
        return;
    
    incomingDevice = PixelPusher::create( header );
    
    for( size_t k=0; k < mPushersInternal.size(); k++ )
    {
        if ( !mPushersInternal[k] )
            continue;

        if ( mPushersInternal[k]->getMacAddress() == incomingDevice->getMacAddress() )
        {
            thisDevice = mPushersInternal[k];
            break;
        }
    }
    
    if ( !thisDevice )
    {
        thisDevice = incomingDevice;
        addNewPusher( thisDevice );
    }
    else
    {
        if ( !thisDevice->isEqual( incomingDevice ) )
        {
            thisDevice->copyHeader( incomingDevice );
        }
        else
        {
            // The device is identical, nothing important has changed
            thisDevice->updateVariables( incomingDevice );
            
            // if we dropped more than occasional packets, slow down a little
            if ( incomingDevice->getDeltaSequence() > 3 )
                thisDevice->increaseExtraDelay(5);
            
            if ( incomingDevice->getDeltaSequence() < 1 )
                thisDevice->decreaseExtraDelay(1);
        }
    }
    
    // Set the timestamp for the last time this device checked in
    thisDevice->setLastPing( getElapsedSeconds() );
    
    // update the power limit variables
    if ( TotalPowerLimit > 0 )
    {
        TotalPower = 0;
        
        for( size_t k=0; k < mPushersInternal.size(); k++ )
            TotalPower += mPushersInternal[k]->getPowerTotal();

        PowerScale = ( TotalPower > TotalPowerLimit ) ? ( TotalPowerLimit / TotalPower ) : 1.0;
    }
   
//    mPushersMutex.unlock();
    
    // continue reading
	mSession->read();
}