Skip to content

Instantly share code, notes, and snippets.

@heatblazer
Last active March 9, 2018 14:46
Show Gist options
  • Save heatblazer/77f8c28b0a6cff09eab3fd02844899a7 to your computer and use it in GitHub Desktop.
Save heatblazer/77f8c28b0a6cff09eab3fd02844899a7 to your computer and use it in GitHub Desktop.
#include <QCoreApplication>
#include <stdint.h>
#include <stdio.h>
// test cases
template <typename T> union bitset_u
{
struct
{
uint8_t b0 : 1;
uint8_t b1 : 1;
uint8_t b2 : 1;
uint8_t b3 : 1;
uint8_t b4 : 1;
uint8_t b5 : 1;
uint8_t b6 : 1;
uint8_t b7 : 1;
} bits[sizeof(T)];
uint8_t data[sizeof(T)];
T val;
};
struct RtpHdr
{
RtpHdr(): m_header{0}, m_timestamp(0), m_SSRC(0), m_CSRC{(0)}
{
}
~RtpHdr()
{
}
void V(int v=2)
{
m_header.data[3] |= ((v & 0x03) << 6);
}
void P(bool set=false)
{
if (set)
m_header.data[3] |= (1 << 5);
}
void X(bool set=false)
{
if (set)
m_header.data[3] |= (1 << 4);
}
void CC(int val)
{
m_header.data[3] |= ((val & 0xF) <<1);
}
void M(bool set=false)
{
if (set)
m_header.data[3] |= (1 << 0);
}
void PT(int val)
{
m_header.data[2] |= (val & 0xFF);
}
// wrap around logic to be implemented
void SEC(int val)
{
m_header.val |= (val & 0x0000FF00);
m_header.val |= (val & 0xFF) << 0;
}
void timestamp(unsigned int val)
{
m_timestamp = val;
}
void SSRC(int val)
{
m_SSRC = val;
}
void CSRC(int val, int idx)
{
if (idx < 0 || idx > 14)
return;
m_CSRC[idx] = val;
}
void test()
{
bitset_u<int> b;
b.val = m_header.val;
}
private:
union
{
int32_t val;
int8_t data[sizeof(int32_t)];
} m_header;
uint32_t m_timestamp;
int32_t m_SSRC;
int32_t m_CSRC[15];
};
struct MyApp : public QCoreApplication
{
explicit MyApp(int& argc, char** argv)
: QCoreApplication(argc, argv)
{
}
virtual ~MyApp() {}
int init()
{
return 0;
}
int exec()
{
return QCoreApplication::exec();
}
void test()
{
RtpHdr r;
r.V(3);
r.P(true);
r.X(true);
r.CC(10);
r.M(true);
r.PT(10);
r.SEC(65000);
r.test();
}
};
int main(int argc, char *argv[])
{
MyApp app(argc, argv);
app.test();
return app.exec();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment