PiImageStreamer
Loading...
Searching...
No Matches
SWE4211RPi::CompressedImageTransmitter Class Reference
Inheritance diagram for SWE4211RPi::CompressedImageTransmitter:
Collaboration diagram for SWE4211RPi::CompressedImageTransmitter:

Public Member Functions

 CompressedImageTransmitter (const char *machineName, int quality, int port=9100)
 
virtual ~CompressedImageTransmitter ()
 
int streamImage (cv::Mat *image)
 
bool setImageQuality (int quality)
 
int getImageQuality ()
 
- Public Member Functions inherited from SWE4211RPi::AbstractImageTransmitter
void setDestinationMachine (const char *machineName)
 
int getConnectionNumber ()
 

Private Attributes

std::atomic< int > jpegQuality
 

Additional Inherited Members

- Protected Member Functions inherited from SWE4211RPi::AbstractImageTransmitter
std::string getDestinationMachine ()
 
- Protected Attributes inherited from SWE4211RPi::AbstractImageTransmitter
int myPort = 9000
 
int sockfd = 0
 
char * destinationMachineName = nullptr
 
std::mutex destinationMutex
 
int imageCount = 0
 
std::atomic< int > connectionNumber {0}
 

Constructor & Destructor Documentation

◆ CompressedImageTransmitter()

CompressedImageTransmitter::CompressedImageTransmitter ( const char *  machineName,
int  quality,
int  port = 9100 
)

This will instantiate a new instance of this class. It will copy the machine name into a heap allocated string and update the port.

Parameters
machineNameThis is the name of the machine that the image is to be streamed to.
qualityThis is the JPEG quality used to encode the images, from 0 to 100. A value outside that range is rejected, and the default quality (STREAM_IMAGE_COMPRESSION_QUALITY) is used instead. Use getImageQuality() to check the quality in use.
portThis is the udp port number that the machine is to connect to.
  1. Initialize all variables as is appropriate.

  2. Set the JPEG quality to the default, then try to set it to the given quality. If the given quality is out of range, it is rejected by setImageQuality() and the default is kept.

  3. Set the destination name accordingly, allocating space as may be necessary.

  4. This is very rare to do, but, print out the major and minor OpenCV versions.
    Note: Not normally given but the code for this is:
    printf("Open CV Major Version: %d Open CV %d \n", CV_MAJOR_VERSION, CV_MINOR_VERSION);
    Note the usage of the defined constants for open cv versions.

Here is the call graph for this function:

◆ ~CompressedImageTransmitter()

CompressedImageTransmitter::~CompressedImageTransmitter ( )
virtual

This is the destructor. It will free all allocated memory.

  1. Free any memory allocated in the constructor.

Member Function Documentation

◆ getImageQuality()

int CompressedImageTransmitter::getImageQuality ( )

This method will return the JPEG quality currently used to encode the images that are streamed.

Returns
The current JPEG quality, from 0 to 100.

◆ setImageQuality()

bool CompressedImageTransmitter::setImageQuality ( int  quality)

This method will set the JPEG quality used to encode the images that are streamed. The new quality takes effect with the next image that is encoded. If the quality is outside of the valid range of 0 to 100, it is rejected and the current quality is left unchanged.

Parameters
qualityThis is the new JPEG quality, from 0 (smallest image, lowest quality) to 100 (largest image, highest quality).
Returns
The return will be true if the quality was set, or false if it was out of range and not set.
  1. If the quality is within the valid range of 0 to 100,

    1. store it and return true.

  2. Otherwise, leave the current quality unchanged and return false.
Here is the caller graph for this function:

◆ streamImage()

int CompressedImageTransmitter::streamImage ( cv::Mat *  image)
virtual

This method will stream via udp the image to the remote device.

Parameters
imageThis is the image that is to be sent.
Returns
The return will be 0 if successful or -1 if there is a failure.
  1. Obtain a copy of the destination machine name. A copy is used because the destination can be changed by another thread (for example when a client connects or disconnects) while this image is being sent.

  2. If the image is not null and there is a valid destination name (i.e. longer than 1 char in length,

    1. Increment the image count.

    2. Obtain the number of rows and columns in the given image.

    3. Declare a vector buffer that is to hold the encoded image.

    4. Encode the given image out to the buffer, using the desired quality and image format.

    5. Initialize the socket sockfd to be a DGRAM.

    6. If there is an error with the socket file descriptor and it is not valid,

      1. abort with an error message and return -1.

    7. Get the host by name and set up a server instance.

    8. If the server is NULL,

      1. print out an error and return -1.

    9. Set up the rest of the UDP parameters for the UDP data stream.

    10. Set the port for the system.

    11. Allocate a buffer of the required size on the heap for the UDP datagram that is to be sent. The size is defined in the UDP_DATAGRAM_TOTAL_SIZE definition.

    12. Zero out the buffer that was just allocated.

    13. Obtain the current timestamp in ms using the time_util library.

    14. Declare a pointer to a 32 bit integer that has the same address as the start of the buffer.

    15. Initialize the count of datagrams to be zero.

    16. while the countofdatagrams multipled by UDP_IMAGE_PAYLOAD_SIZE is less than the total number of bytes in the shared memory buffer stats

      1. Determine the offset into the buffer, which is the countofDatagrams times UDP_IMAGE_PAYLOAD_SIZE. Also declare a local variable bytestoCopy which is initially set to UDP_IMAGE_PAYLOAD_SIZE.

      2. If the (count of datagrams + 1) * UDP_IMAGE_PAYLOAD_SIZE is larger than the total number of bytes in the shared memory buffer stats

        1. Set bytes to copy to the remaining bytes that have not already been sent.

        2. Zero out the buffer so that everything is zero.
          Note: This is necessary because while we zeroed the buffer out initially, each other datagram that has been written has fully overwritten the buffer. In the case of the last datagram, that will not be the case, so we want to make sure everything is zero.

      3. Fill in the 64 byte header with the following information in each 32 bit integer offset location.

        • Index 0: Always 0xAAAA5555

        • Index 1: the count of datagrams sent

        • Index 2: The number of bytes to copy

        • Index 3: The size of the shared memory file

        • Index 4: The number of rows in the image

        • Index 5: The number of columns in the image

        • Index 6: The start time for the transmission

        • Index 7: The image count

        • Index 8: Always 0

        • Index 9: UDP_IMAGE_PAYLOAD_SIZE

        • Index 10: Always 0

        • Index 11: Always 0

        • Index 12: Always 0

        • Index 13: Always 0

        • Index 14: Always 0

        • Index 15: Always 0

      4. Now copy the data from the shared memory segment into the UDP datagram for transmission.

      5. Send to message to the destination as a UDP datagram.

      6. Increment the count of datagrams

    17. Free the message that was allocated on the heap.

    18. Close the socket down.

  3. Return 0

Implements SWE4211RPi::AbstractImageTransmitter.

Here is the call graph for this function:

Member Data Documentation

◆ jpegQuality

std::atomic<int> SWE4211RPi::CompressedImageTransmitter::jpegQuality
private

This is the JPEG quality used when encoding each image, from 0 to 100. Lower values give smaller images (less bandwidth) at the cost of image quality. It is atomic so that it can be changed from another thread while images are being streamed.


The documentation for this class was generated from the following files: