Skip to content

Instantly share code, notes, and snippets.

@jmorrill
Last active August 29, 2015 14:28
Show Gist options
  • Save jmorrill/6f1c82fd4adb9da5b8bc to your computer and use it in GitHub Desktop.
Save jmorrill/6f1c82fd4adb9da5b8bc to your computer and use it in GitHub Desktop.
// ConsoleApplication1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <stdint.h>
#include <vector>
#include <algorithm>
#include <filesystem>
#include <random>
const size_t KEYSIZE = sizeof int64_t /* timestamp */ +
sizeof uint16_t /* stream number */ +
sizeof uint8_t /* key sample */;
template <typename T>
T swap_endian(T v)
{
auto p = reinterpret_cast<uint8_t*>(&v);
std::reverse(p, p + sizeof(T));
return v;
}
void generate_key(std::vector<uint8_t>& buffer, uint64_t time_stamp, uint16_t stream_number, uint8_t keyframe)
{
buffer.resize(KEYSIZE);
size_t pos = 0;
time_stamp = swap_endian(time_stamp);
stream_number = swap_endian(stream_number);
memcpy(&buffer[0], &time_stamp, sizeof uint64_t);
pos += sizeof uint64_t;
memcpy(&buffer[pos], &stream_number, sizeof uint16_t);
pos += sizeof uint16_t;
memcpy(&buffer[pos], &keyframe, sizeof uint8_t);
}
void print_buffer(const std::vector<uint8_t>& buffer)
{
uint64_t time_stamp = *reinterpret_cast<const uint64_t*>(buffer.data());
uint16_t stream_number = *reinterpret_cast<const uint16_t*>(&buffer[sizeof uint64_t]);
uint8_t keyframe = *reinterpret_cast<const uint8_t*>(&buffer[sizeof uint64_t + sizeof uint16_t]);
time_stamp = swap_endian(time_stamp);
stream_number = swap_endian(stream_number);
auto micro = std::chrono::microseconds(time_stamp);
auto time_point = std::chrono::system_clock::to_time_t(std::chrono::system_clock::time_point(micro));
auto time_str = ctime(&time_point);
time_str[strlen(time_str) - 1] = 0;
printf("%s - stream: %d keyframe: %d\n", time_str, stream_number, keyframe);
}
int main()
{
uint64_t microseconds_since_epoch = std::chrono::system_clock::now().time_since_epoch() /
std::chrono::microseconds(1);
std::vector<std::vector<uint8_t>> buffers;
bool keyframe = false;
for (uint16_t stream_number = 0; stream_number < 3; stream_number++)
{
for (size_t i = 0; i < 10; i++)
{
buffers.emplace_back();
auto& buffer = buffers.back();
auto time_stamp = microseconds_since_epoch + (i * 1000000ull);
keyframe = !keyframe;
generate_key(buffer, time_stamp, stream_number, keyframe);
}
}
std::sort(buffers.begin(), buffers.end(), [](const std::vector<uint8_t>& a,
const std::vector<uint8_t>& b)
{
auto res = memcmp(a.data(), b.data(), a.size());
return res < 0;
});
for(const auto& buffer : buffers)
{
print_buffer(buffer);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment