Skip to content

Commit

Permalink
Compiling
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Szatmary committed Aug 12, 2016
1 parent 46b72b1 commit 1b2ad90
Show file tree
Hide file tree
Showing 3 changed files with 140 additions and 3 deletions.
121 changes: 119 additions & 2 deletions examples/flv.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,17 @@ void flvtag_init (flvtag_t* tag)
memset (tag,0,sizeof (flvtag_t));
}

void flvtag_free (flvtag_t* tag)
{
if (tag->data)
{
free (tag->data);
}

flvtag_init (tag);
}


void flvtag_swap (flvtag_t* tag1, flvtag_t* tag2)
{
flvtag_t* tag3;
Expand Down Expand Up @@ -155,11 +166,11 @@ int flvtag_updatesize (flvtag_t* tag, uint32_t size)
return 1;
}

#define FLVTAG_AVC_PREALOC 2048
#define FLVTAG_PREALOC 2048
int flvtag_initavc (flvtag_t* tag, uint32_t dts, int32_t cts, flvtag_frametype_t type)
{
flvtag_init (tag);
flvtag_reserve (tag,FLV_TAG_HEADER_SIZE+5+FLV_TAG_FOOTER_SIZE+FLVTAG_AVC_PREALOC);
flvtag_reserve (tag,FLV_TAG_HEADER_SIZE+5+FLV_TAG_FOOTER_SIZE+FLVTAG_PREALOC);
tag->data[0] = 0x09; // video
tag->data[4] = dts>>16;
tag->data[5] = dts>>8;
Expand All @@ -178,6 +189,112 @@ int flvtag_initavc (flvtag_t* tag, uint32_t dts, int32_t cts, flvtag_frametype_t
return 1;
}

int flvtag_initamf (flvtag_t* tag, uint32_t dts)
{
flvtag_init (tag);
flvtag_reserve (tag,FLV_TAG_HEADER_SIZE+FLV_TAG_FOOTER_SIZE+FLVTAG_PREALOC);
tag->data[0] = 0x12; // script
tag->data[4] = dts>>16;
tag->data[5] = dts>>8;
tag->data[6] = dts>>0;
tag->data[7] = dts>>24;
tag->data[8] = 0; // StreamID
tag->data[9] = 0; // StreamID
tag->data[10] = 0; // StreamID
flvtag_updatesize (tag,0);
return 1;
}


/*
if (TwitchJsonValue::TypeArray == amf.type()) {
if (2 <= amf.size() && "onMetaData" == amf.at (0).toString()) {
DEBUG ("onMetaData: %s", amf.at (1).toString().c_str())
}
if (2 <= amf.size() && "onCaptionInfo" == amf.at (0).toString()) {
const TwitchJsonValue& captionInfo = amf.at (1);
if (TwitchJsonValue::TypeObject == captionInfo.type() && "708" == captionInfo.at ("type").toString()) {
m_amfCaptions = true; // we have amf captions
std::string data = captionInfo.at ("data").toString();
codify->second->codifyCaptions (data, dts, FLV_TIME_BASE_Q, sid); // in display order, dts same as pts
while (codify->second->size()) { forwardFrame (codify->second->take_front()); }
}
}
int base64_encode(const unsigned char *in, unsigned long inlen,
unsigned char *out, unsigned long *outlen)
*/

// shamelessly taken from libtomcrypt, an public domain project
static void base64_encode (const unsigned char* in, unsigned long inlen, unsigned char* out, unsigned long* outlen)
{
static const char* codes = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
unsigned long i, len2, leven;
unsigned char* p;

/* valid output size ? */
len2 = 4 * ( (inlen + 2) / 3);

if (*outlen < len2 + 1) {
*outlen = len2 + 1;
return;
}

p = out;
leven = 3* (inlen / 3);

for (i = 0; i < leven; i += 3) {
*p++ = codes[ (in[0] >> 2) & 0x3F];
*p++ = codes[ ( ( (in[0] & 3) << 4) + (in[1] >> 4)) & 0x3F];
*p++ = codes[ ( ( (in[1] & 0xf) << 2) + (in[2] >> 6)) & 0x3F];
*p++ = codes[in[2] & 0x3F];
in += 3;
}

if (i < inlen) {
unsigned a = in[0];
unsigned b = (i+1 < inlen) ? in[1] : 0;

*p++ = codes[ (a >> 2) & 0x3F];
*p++ = codes[ ( ( (a & 3) << 4) + (b >> 4)) & 0x3F];
*p++ = (i+1 < inlen) ? codes[ ( ( (b & 0xf) << 2)) & 0x3F] : '=';
*p++ = '=';
}

/* append a NULL byte */
*p = '\0';

/* return ok */
*outlen = p - out;
}

const char onCaptionInfo[] = "\x02\x00\x0DonCaptionInfo" \
"\08\x00\x00\x00\x02" \
"\x00\x04type" \
"\x02\x00\x02708" \
"\x00\x04data" \
"\x02\x00\x00";

int flvtag_amfcaption (flvtag_t* tag, uint32_t timestamp, sei_message_t* msg)
{
char* data = flvtag_payload_data (tag) + sizeof (onCaptionInfo);
unsigned long size = ( (1+sei_message_size (msg)) *4) /3;
flvtag_reserve (tag, FLV_TAG_HEADER_SIZE + sizeof (onCaptionInfo) + size + 3 + FLV_TAG_FOOTER_SIZE);
base64_encode (sei_message_data (msg), sei_message_size (msg), data, &size);
data[sizeof (onCaptionInfo)-2] == size >> 8;
data[sizeof (onCaptionInfo)-1] == size >> 0;
data[size-2] = '\x00';
data[size-1] = '\x00';
data[size-0] = '\x09';
flvtag_updatesize (tag, sizeof (onCaptionInfo) + size + 3);
return 1;
}

#define LENGTH_SIZE 4

int flvtag_avcwritenal (flvtag_t* tag, uint8_t* data, size_t size)
Expand Down
2 changes: 2 additions & 0 deletions examples/flv.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef struct {
} flvtag_t;

void flvtag_init (flvtag_t* tag);
void flvtag_free (flvtag_t* tag);
void flvtag_swap (flvtag_t* tag1, flvtag_t* tag2);
// static inline uint8_t *flvtag_data (flvtag_t* tag) { return &tag->data[0]; }
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -112,3 +113,4 @@ int flvtag_initavc (flvtag_t* tag, uint32_t dts, int32_t cts, flvtag_frametype_t
int flvtag_avcwritenal (flvtag_t* tag, uint8_t* data, size_t size);
int flvtag_addcaption (flvtag_t* tag, const utf8_char_t* text);
////////////////////////////////////////////////////////////////////////////////
int flvtag_amfcaption (flvtag_t* tag, uint32_t timestamp, sei_message_t* msg);
20 changes: 19 additions & 1 deletion examples/party.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,26 @@ int main (int argc, char** argv)
while (flv_read_tag (flv,&tag)) {

if (nextParty <= flvtag_timestamp (&tag)) {
sei_t sei;
flvtag_t tag;
sei_message_t* msg;
caption_frame_t frame;
get_dudes (partyDudes);
flvtag_addcaption (&tag, partyDudes);
sei_init (&sei);
flvtag_init (&tag);
caption_frame_init (&frame);
caption_frame_from_text (&frame, partyDudes);
sei_from_caption_frame (&sei, &frame);

for (msg = sei_message_head (&sei); msg; msg=sei_message_next (msg),++nextParty) {
flvtag_amfcaption (&tag,nextParty,msg);
flv_write_tag (out,&tag);
}

sei_free (&sei);
flvtag_free (&tag);

// flvtag_addcaption (&tag, partyDudes);
fprintf (stderr,"%d: %s\n", flvtag_timestamp (&tag), partyDudes);
nextParty += 1000; // party every second
}
Expand Down

0 comments on commit 1b2ad90

Please sign in to comment.