Skip to content

Commit

Permalink
EHN: circular buffer provides two mode now, section buffering and nor…
Browse files Browse the repository at this point in the history
…mal buffering, section buffering reserves a section in the circular buffer while pulling. See issue IGSIO#37
  • Loading branch information
leochan2009 committed Dec 20, 2017
1 parent b3dc587 commit 60c0af9
Show file tree
Hide file tree
Showing 15 changed files with 666 additions and 180 deletions.
3 changes: 2 additions & 1 deletion Converter/igtlioVideoConverter.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ namespace igtlio
videoStreamDecoder->ConvertYUVToRGB(pDecodedPic->data[0], (uint8_t*)imageData->GetScalarPointer(),Height, Width);
}
imageData->Modified();
if (pDecodedPic->data[0]!=NULL)
delete [] pDecodedPic->data[0];
delete pDecodedPic;
return 1;
}
Expand All @@ -128,7 +130,6 @@ namespace igtlio
{
if (dest->IsNull())
*dest = igtl::VideoMessage::New();
(*dest)->InitPack();
igtl::MessageBase::Pointer basemsg = dynamic_pointer_cast<igtl::MessageBase>(*dest);

HeadertoIGTL(header, &basemsg, metaInfo);
Expand Down
41 changes: 22 additions & 19 deletions Devices/igtlioVideoDevice.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,34 @@ vtkStandardNewMacro(VideoDevice);
//---------------------------------------------------------------------------
VideoDevice::VideoDevice()
{
VideoStreamDecoderH264 = NULL;
VideoStreamEncoderH264 = NULL;
VideoStreamDecoderVPX = NULL;
VideoStreamEncoderVPX = NULL;
VideoStreamDecoderX265 = NULL;
VideoStreamEncoderX265 = NULL;
this->OutVideoMessage = igtl::VideoMessage::New();
this->OutVideoMessage->SetHeaderVersion(OpenIGTLink_HEADER_VERSION);
VideoStreamDecoderH264 = NULL;
VideoStreamEncoderH264 = NULL;
VideoStreamDecoderVPX = NULL;
VideoStreamEncoderVPX = NULL;
VideoStreamDecoderX265 = NULL;
VideoStreamEncoderX265 = NULL;
#if defined(OpenIGTLink_USE_H264)
VideoStreamDecoderH264 = new H264Decoder();
VideoStreamEncoderH264 = new H264Encoder();
VideoStreamDecoderH264 = new H264Decoder();
VideoStreamEncoderH264 = new H264Encoder();
VideoStreamEncoderH264->InitializeEncoder();
VideoStreamEncoderH264->SetLosslessLink(true);
#endif
#if defined(OpenIGTLink_USE_VP9)
VideoStreamDecoderVPX = new VP9Decoder();
VideoStreamEncoderVPX = new VP9Encoder();
VideoStreamDecoderVPX = new VP9Decoder();
VideoStreamEncoderVPX = new VP9Encoder();
VideoStreamEncoderVPX->SetPicWidthAndHeight(256,256);
VideoStreamEncoderVPX->InitializeEncoder();
VideoStreamEncoderVPX->SetLosslessLink(true);
#endif
#if defined(OpenIGTLink_USE_OpenHEVC)
VideoStreamDecoderX265 = new H265Decoder();
VideoStreamDecoderX265 = new H265Decoder();
#endif
#if defined(OpenIGTLink_USE_X265)
VideoStreamEncoderX265 = new H265Encoder();
VideoStreamEncoderX265 = new H265Encoder();
VideoStreamEncoderX265->SetLosslessLink(true);
VideoStreamEncoderX265->InitializeEncoder();
#endif

DecodersMap.clear();
Expand Down Expand Up @@ -143,14 +152,13 @@ igtl::MessageBase::Pointer VideoDevice::GetIGTLMessage()
int frameRate = 20;
int iReturn = 0;
this->OutVideoMessage = igtl::VideoMessage::New();
//this->OutVideoMessage->AllocateScalars();
#if defined(OpenIGTLink_USE_H264)
if(this->CurrentCodecType.compare(IGTL_VIDEO_CODEC_NAME_H264) == 0)
{
VideoStreamEncoderH264->SetPicWidthAndHeight(imageSizePixels[0], imageSizePixels[1]);
//newEncoder->SetKeyFrameDistance(25);
VideoStreamEncoderH264->SetRCTaregetBitRate((int)(imageSizePixels[0] * imageSizePixels[1] * 8 * frameRate * bitRatePercent));
VideoStreamEncoderH264->InitializeEncoder();
VideoStreamEncoderH264->SetLosslessLink(true);
this->OutVideoMessage->SetCodecType(IGTL_VIDEO_CODEC_NAME_H264);
iReturn = VideoConverter::toIGTL(HeaderData, Content, &this->OutVideoMessage, VideoStreamEncoderH264, &this->metaInfo);
}
Expand All @@ -161,8 +169,6 @@ igtl::MessageBase::Pointer VideoDevice::GetIGTLMessage()
VideoStreamEncoderVPX->SetPicWidthAndHeight(imageSizePixels[0], imageSizePixels[1]);
//newEncoder->SetKeyFrameDistance(25);
VideoStreamEncoderVPX->SetRCTaregetBitRate((int)(imageSizePixels[0] * imageSizePixels[1] * 8 * frameRate * bitRatePercent));
VideoStreamEncoderVPX->InitializeEncoder();
VideoStreamEncoderVPX->SetLosslessLink(true);
this->OutVideoMessage->SetCodecType(IGTL_VIDEO_CODEC_NAME_VP9);
iReturn = VideoConverter::toIGTL(HeaderData, Content, &this->OutVideoMessage, VideoStreamEncoderVPX, &this->metaInfo);
}
Expand All @@ -172,10 +178,7 @@ igtl::MessageBase::Pointer VideoDevice::GetIGTLMessage()
{
VideoStreamEncoderX265->SetPicWidthAndHeight(imageSizePixels[0], imageSizePixels[1]);
int bitRateFactor = 7;
VideoStreamEncoderX265->SetLosslessLink(true);
VideoStreamEncoderX265->SetRCTaregetBitRate((int)(imageSizePixels[0] * imageSizePixels[1] * 8 * frameRate * bitRatePercent)*bitRateFactor);
VideoStreamEncoderX265->InitializeEncoder();
VideoStreamEncoderX265->SetSpeed(9);
this->OutVideoMessage->SetCodecType(IGTL_VIDEO_CODEC_NAME_X265);
iReturn = VideoConverter::toIGTL(HeaderData, Content, &this->OutVideoMessage, VideoStreamEncoderX265, &this->metaInfo);
}
Expand Down
2 changes: 2 additions & 0 deletions Logic/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ set(${PROJECT_NAME}_SRCS
igtlioObject.cxx
igtlioDeviceFactory.cxx
igtlioCircularBuffer.cxx
igtlioCircularSectionBuffer.cxx
igtlioConnector.cxx
igtlioSession.cxx
igtlioLogic.cxx
Expand All @@ -30,6 +31,7 @@ set(${PROJECT_NAME}_HDRS
igtlioObject.h
igtlioDeviceFactory.h
igtlioCircularBuffer.h
igtlioCircularSectionBuffer.h
igtlioConnector.h
igtlioSession.h
)
Expand Down
170 changes: 85 additions & 85 deletions Logic/igtlioCircularBuffer.cxx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/*==========================================================================
Portions (c) Copyright 2008-2009 Brigham and Women's Hospital (BWH) All Rights Reserved.
See Doc/copyright/copyright.txt
or http://www.slicer.org/copyright/copyright.txt for details.
==========================================================================*/
Portions (c) Copyright 2008-2009 Brigham and Women's Hospital (BWH) All Rights Reserved.
See Doc/copyright/copyright.txt
or http://www.slicer.org/copyright/copyright.txt for details.
==========================================================================*/

// VTK includes
#include "igtlioCircularBuffer.h"
Expand All @@ -22,13 +22,13 @@

namespace igtlio
{

//---------------------------------------------------------------------------
vtkStandardNewMacro(CircularBuffer);

//---------------------------------------------------------------------------
CircularBuffer::CircularBuffer()
{
//---------------------------------------------------------------------------
vtkStandardNewMacro(CircularBuffer);
//---------------------------------------------------------------------------
CircularBuffer::CircularBuffer()
{
this->Mutex = vtkMutexLock::New();
this->Mutex->Lock();
// Allocate Circular buffer for the new device
Expand All @@ -42,20 +42,20 @@ CircularBuffer::CircularBuffer()
this->Messages[i] = igtl::MessageBase::New();
this->Messages[i]->InitPack();
}

this->UpdateFlag = 0;
this->Mutex->Unlock();
}


//---------------------------------------------------------------------------
CircularBuffer::~CircularBuffer()
{
}
//---------------------------------------------------------------------------
CircularBuffer::~CircularBuffer()
{
this->Mutex->Lock();
this->InUse = -1;
this->Last = -1;
this->Mutex->Unlock();

for (int i = 0; i < IGTLCB_CIRC_BUFFER_SIZE; i ++)
{
if (this->Data[i] != NULL)
Expand All @@ -64,89 +64,89 @@ CircularBuffer::~CircularBuffer()
}
}
this->Mutex->Delete();
}


//---------------------------------------------------------------------------
void CircularBuffer::PrintSelf(ostream& os, vtkIndent indent)
{
}
//---------------------------------------------------------------------------
void CircularBuffer::PrintSelf(ostream& os, vtkIndent indent)
{
this->vtkObject::PrintSelf(os, indent);
}


//---------------------------------------------------------------------------
// Functions to push data into the circular buffer (for receiving thread)
//
// StartPush() : Prepare to push data
// GetPushBuffer(): Get MessageBase buffer from the circular buffer
// EndPush() : Finish pushing data. The data becomes ready to
// be read by monitor thread.
//
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
int CircularBuffer::StartPush()
{
}
//---------------------------------------------------------------------------
// Functions to push data into the circular buffer (for receiving thread)
//
// StartPush() : Prepare to push data
// GetPushBuffer(): Get MessageBase buffer from the circular buffer
// EndPush() : Finish pushing data. The data becomes ready to
// be read by monitor thread.
//
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int CircularBuffer::StartPush()
{
this->Mutex->Lock();
this->InPush = (this->Last + 1) % IGTLCB_CIRC_BUFFER_SIZE;
if (this->InPush == this->InUse)
{
this->InPush = (this->Last + 1) % IGTLCB_CIRC_BUFFER_SIZE;
}
this->Mutex->Unlock();

return this->InPush;
}

//---------------------------------------------------------------------------
igtl::MessageBase::Pointer CircularBuffer::GetPushBuffer()
{
}
//---------------------------------------------------------------------------
igtl::MessageBase::Pointer CircularBuffer::GetPushBuffer()
{
return this->Messages[this->InPush];
}

//---------------------------------------------------------------------------
void CircularBuffer::EndPush()
{
}
//---------------------------------------------------------------------------
void CircularBuffer::EndPush()
{
this->Mutex->Lock();
this->Last = this->InPush;
this->UpdateFlag = 1;
this->Mutex->Unlock();
}


//---------------------------------------------------------------------------
// Functions to pull data into the circular buffer (for monitor thread)
//
// StartPull() : Prepare to pull data
// GetPullBuffer(): Get MessageBase buffer from the circular buffer
// EndPull() : Finish pulling data
//
//---------------------------------------------------------------------------

//---------------------------------------------------------------------------
int CircularBuffer::StartPull()
{
}
//---------------------------------------------------------------------------
// Functions to pull data into the circular buffer (for monitor thread)
//
// StartPull() : Prepare to pull data
// GetPullBuffer(): Get MessageBase buffer from the circular buffer
// EndPull() : Finish pulling data
//
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
int CircularBuffer::StartPull()
{
this->Mutex->Lock();
this->InUse = this->Last;
this->UpdateFlag = 0;
this->Mutex->Unlock();
return this->Last; // return -1 if it is not available
}


//---------------------------------------------------------------------------
igtl::MessageBase::Pointer CircularBuffer::GetPullBuffer()
{
}
//---------------------------------------------------------------------------
igtl::MessageBase::Pointer CircularBuffer::GetPullBuffer()
{
return this->Messages[this->InUse];
}


//---------------------------------------------------------------------------
void CircularBuffer::EndPull()
{
}
//---------------------------------------------------------------------------
void CircularBuffer::EndPull()
{
this->Mutex->Lock();
this->InUse = -1;
this->Mutex->Unlock();
}

} // namespace igtlio
}
} // namespace igtlio
Loading

0 comments on commit 60c0af9

Please sign in to comment.