|
PiImageStreamer
|


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} |
| 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.
| machineName | This is the name of the machine that the image is to be streamed to. |
| quality | This 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. |
| port | This is the udp port number that the machine is to connect to. |
Initialize all variables as is appropriate.
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.
Set the destination name accordingly, allocating space as may be necessary.
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.

|
virtual |
This is the destructor. It will free all allocated memory.
Free any memory allocated in the constructor.
| int CompressedImageTransmitter::getImageQuality | ( | ) |
This method will return the JPEG quality currently used to encode the images that are streamed.
| 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.
| quality | This is the new JPEG quality, from 0 (smallest image, lowest quality) to 100 (largest image, highest quality). |
If the quality is within the valid range of 0 to 100,

|
virtual |
This method will stream via udp the image to the remote device.
| image | This is the image that is to be sent. |
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.
If the image is not null and there is a valid destination name (i.e. longer than 1 char in length,
Increment the image count.
Obtain the number of rows and columns in the given image.
Declare a vector buffer that is to hold the encoded image.
Encode the given image out to the buffer, using the desired quality and image format.
Initialize the socket sockfd to be a DGRAM.
If there is an error with the socket file descriptor and it is not valid,
Get the host by name and set up a server instance.
If the server is NULL,
Set up the rest of the UDP parameters for the UDP data stream.
Set the port for the system.
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.
Zero out the buffer that was just allocated.
Obtain the current timestamp in ms using the time_util library.
Declare a pointer to a 32 bit integer that has the same address as the start of the buffer.
Initialize the count of datagrams to be zero.
while the countofdatagrams multipled by UDP_IMAGE_PAYLOAD_SIZE is less than the total number of bytes in the shared memory buffer stats
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.
If the (count of datagrams + 1) * UDP_IMAGE_PAYLOAD_SIZE is larger than the total number of bytes in the shared memory buffer stats
Set bytes to copy to the remaining bytes that have not already been sent.
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
Now copy the data from the shared memory segment into the UDP datagram for transmission.
Send to message to the destination as a UDP datagram.
Increment the count of datagrams
Free the message that was allocated on the heap.
Close the socket down.
Implements SWE4211RPi::AbstractImageTransmitter.

|
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.