This repository has been archived by the owner on Jul 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from kilbouri/h264-packet-upgrade
resolution encoded in h264packet
- Loading branch information
Showing
7 changed files
with
87 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,22 @@ | ||
#pragma once | ||
|
||
extern "C" { | ||
#include <libavcodec/avcodec.h> | ||
#include <libavformat/avformat.h> | ||
#include <libavutil/avutil.h> | ||
#include <libavutil/pixdesc.h> | ||
#include <libavdevice/avdevice.h> | ||
#include <libswscale/swscale.h> | ||
} | ||
|
||
#include <format> | ||
|
||
// util function to compute num bytes per pixel | ||
static inline int getBitsPerPixel(AVPixelFormat format) { | ||
const AVPixFmtDescriptor* descriptor = av_pix_fmt_desc_get(format); | ||
if (!descriptor) { | ||
throw std::format("Unknown pixel format {}", static_cast<std::underlying_type_t<AVPixelFormat>>(format)); | ||
} | ||
|
||
return av_get_bits_per_pixel(descriptor); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,86 @@ | ||
#pragma once | ||
|
||
#include <iterator> | ||
|
||
#include "../tlvmessage/tlvmessage.hpp" | ||
#include "../../ffmpegincludes.h" | ||
|
||
namespace StudentSync::Networking::Message { | ||
struct H264Packet { | ||
|
||
public: | ||
std::vector<uint8_t> imageData; | ||
|
||
int frameWidth; | ||
int frameHeight; | ||
|
||
private: | ||
static constexpr size_t MIN_BYTES_SIZE = sizeof(frameWidth) + sizeof(frameHeight); | ||
|
||
public: | ||
static std::optional<H264Packet> FromTLVMessage(const TLVMessage& netMessage) noexcept { | ||
if (netMessage.tag != TLVMessage::Tag::H264Packet) { | ||
return std::nullopt; | ||
} | ||
|
||
if (netMessage.data.size() < MIN_BYTES_SIZE) { | ||
return std::nullopt; | ||
} | ||
|
||
const int* frameDimPtr = reinterpret_cast<const int*>(netMessage.data.data()); | ||
|
||
int frameWidth = *frameDimPtr; | ||
int frameHeight = *(frameDimPtr + 1); | ||
|
||
const void* imageDataStart = frameDimPtr + 2; | ||
|
||
std::vector<uint8_t> data{}; | ||
std::move( | ||
netMessage.data.begin() + sizeof(frameWidth) + sizeof(frameHeight), | ||
netMessage.data.end(), | ||
std::back_inserter(data) | ||
); | ||
|
||
|
||
return H264Packet{ | ||
.imageData = netMessage.data | ||
.imageData = data, | ||
.frameWidth = static_cast<int>(ntohl(frameWidth)), | ||
.frameHeight = static_cast<int>(ntohl(frameHeight)), | ||
}; | ||
} | ||
|
||
TLVMessage ToTLVMessage() const noexcept { | ||
return TLVMessage(TLVMessage::Tag::H264Packet, imageData); | ||
|
||
std::vector<uint8_t> networkValue; | ||
networkValue.reserve(MIN_BYTES_SIZE + imageData.size()); | ||
|
||
// copy in width and height | ||
int netWidth = static_cast<int>(htonl(frameWidth)); | ||
int netHeight = static_cast<int>(htonl(frameHeight)); | ||
|
||
const uint8_t* wPtr = reinterpret_cast<const uint8_t*>(&netWidth); | ||
const uint8_t* hPtr = reinterpret_cast<const uint8_t*>(&netHeight); | ||
|
||
std::copy( | ||
wPtr, | ||
wPtr + sizeof(netWidth), | ||
std::back_inserter(networkValue) | ||
); | ||
|
||
std::copy( | ||
hPtr, | ||
hPtr + sizeof(netHeight), | ||
std::back_inserter(networkValue) | ||
); | ||
|
||
// copy image data | ||
std::copy( | ||
imageData.begin(), | ||
imageData.end(), | ||
std::back_inserter(networkValue) | ||
); | ||
|
||
return TLVMessage(TLVMessage::Tag::H264Packet, networkValue); | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters