Skip to content

Commit

Permalink
Feature/rtmp (szatmary#1)
Browse files Browse the repository at this point in the history
* add rtmpspit. test amf captions (failed)

* add rtmpspit.c

* Fix utf8 bugs

* dont build rtmpspit for now
  • Loading branch information
szatmary authored Aug 19, 2016
1 parent a5b1d8e commit fe94686
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 29 deletions.
4 changes: 4 additions & 0 deletions caption/caption.h
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ int caption_frame_write_char (caption_frame_t* frame, int row, int col, eia608_s
/*! \brief
\param
*/
int caption_frame_end (caption_frame_t* frame);
/*! \brief
\param
*/
const utf8_char_t* caption_frame_read_char (caption_frame_t* frame, int row, int col, eia608_style_t* style, int* underline);
/*! \brief
\param
Expand Down
4 changes: 2 additions & 2 deletions caption/sei.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
/**********************************************************************************************/


#ifndef LIBCAPTION_sei_H
#define LIBCAPTION_sei_H
#ifndef LIBCAPTION_SEI_H
#define LIBCAPTION_SEI_H
#include "cea708.h"
#include "caption.h"
#include <float.h>
Expand Down
13 changes: 4 additions & 9 deletions examples/flv.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ void flvtag_free (flvtag_t* tag)
flvtag_init (tag);
}


void flvtag_swap (flvtag_t* tag1, flvtag_t* tag2)
{
flvtag_t* tag3;
Expand Down Expand Up @@ -72,8 +71,6 @@ FILE* flv_close (FILE* flv)
return 0;
}

#define FLV_HEADER_SIZE 13
#define FLV_FOOTER_SIZE 4
int flv_read_header (FILE* flv, int* has_audio, int* has_video)
{
uint8_t h[FLV_HEADER_SIZE];
Expand All @@ -97,8 +94,6 @@ int flv_write_header (FILE* flv, int has_audio, int has_video)
return FLV_HEADER_SIZE == fwrite (&h[0],1,FLV_HEADER_SIZE,flv);
}

#define FLV_TAG_HEADER_SIZE 11
#define FLV_TAG_FOOTER_SIZE 4
int flv_read_tag (FILE* flv, flvtag_t* tag)
{
uint32_t size;
Expand All @@ -122,8 +117,8 @@ int flv_read_tag (FILE* flv, flvtag_t* tag)

int flv_write_tag (FILE* flv, flvtag_t* tag)
{
size_t size = flvtag_size (tag)+FLV_TAG_HEADER_SIZE+FLV_TAG_FOOTER_SIZE;
return size == fwrite (tag->data,1,size,flv);
size_t size = flvtag_raw_size (tag);
return size == fwrite (flvtag_raw_data (tag),1,size,flv);
}
////////////////////////////////////////////////////////////////////////////////
size_t flvtag_header_size (flvtag_t* tag)
Expand Down Expand Up @@ -259,12 +254,12 @@ int flvtag_amfcaption (flvtag_t* tag, uint32_t timestamp, sei_message_t* msg)
unsigned long size = 1 + (4 * ( (sei_message_size (msg) + 2) / 3));
flvtag_reserve (tag, FLV_TAG_HEADER_SIZE + sizeof (onCaptionInfo) + size + 3 + FLV_TAG_FOOTER_SIZE);
memcpy (flvtag_payload_data (tag),onCaptionInfo,sizeof (onCaptionInfo));
uint8_t* data = sizeof (onCaptionInfo) + flvtag_payload_data (tag);
uint8_t* data = flvtag_payload_data (tag) + sizeof (onCaptionInfo);
base64_encode (sei_message_data (msg), sei_message_size (msg), data, &size);

data[-2] = size >> 8;
data[-1] = size >> 0;
// rewrite the last array element
// write the last array element
data[size+0] = 0x00;
data[size+1] = 0x00;
data[size+2] = 0x09;
Expand Down
10 changes: 10 additions & 0 deletions examples/flv.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,16 @@
/* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the */
/* License for the specific language governing permissions and limitations under the License. */
/**********************************************************************************************/
#ifndef LIBCAPTION_FLV_H
#define LIBCAPTION_FLV_H

#include <stdio.h>
#include <stddef.h>
#include <inttypes.h>
#define FLV_HEADER_SIZE 13
#define FLV_FOOTER_SIZE 4
#define FLV_TAG_HEADER_SIZE 11
#define FLV_TAG_FOOTER_SIZE 4
////////////////////////////////////////////////////////////////////////////////
#include "sei.h"
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -101,6 +107,9 @@ FILE* flv_open_read (const char* flv);
FILE* flv_open_write (const char* flv);
FILE* flv_close (FILE* flv);
////////////////////////////////////////////////////////////////////////////////
static inline const uint8_t* flvtag_raw_data (flvtag_t* tag) { return tag->data; }
static inline const size_t flvtag_raw_size (flvtag_t* tag) { return flvtag_size (tag)+FLV_TAG_HEADER_SIZE+FLV_TAG_FOOTER_SIZE; }
////////////////////////////////////////////////////////////////////////////////
int flv_read_tag (FILE* flv, flvtag_t* tag);
int flv_write_tag (FILE* flv, flvtag_t* tag);
int flv_read_header (FILE* flv, int* has_audio, int* has_video);
Expand All @@ -114,3 +123,4 @@ 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);
#endif
11 changes: 7 additions & 4 deletions examples/party.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

void get_dudes (char* str)
{
sprintf (str, "%s%s %s(-_-)%s %s(-_-)%s %s(-_-)%s %s%s", EIA608_CHAR_EIGHTH_NOTE, EIA608_CHAR_EIGHTH_NOTE,
sprintf (str, " %s%s %s(-_-)%s %s(-_-)%s %s(-_-)%s %s%s", EIA608_CHAR_EIGHTH_NOTE, EIA608_CHAR_EIGHTH_NOTE,
! (rand() % 2) ? EIA608_CHAR_BOX_DRAWINGS_LIGHT_DOWN_AND_RIGHT : EIA608_CHAR_BOX_DRAWINGS_LIGHT_UP_AND_RIGHT,
! (rand() % 2) ? EIA608_CHAR_BOX_DRAWINGS_LIGHT_DOWN_AND_LEFT : EIA608_CHAR_BOX_DRAWINGS_LIGHT_UP_AND_LEFT,
! (rand() % 2) ? EIA608_CHAR_BOX_DRAWINGS_LIGHT_DOWN_AND_RIGHT : EIA608_CHAR_BOX_DRAWINGS_LIGHT_UP_AND_RIGHT,
Expand All @@ -40,9 +40,12 @@ void write_amfcaptions (FILE* out, uint32_t timestamp, const char* text)
caption_frame_init (&frame);
caption_frame_from_text (&frame, text);
sei_from_caption_frame (&sei, &frame);
caption_frame_dump (&frame);


for (msg = sei_message_head (&sei); msg; msg=sei_message_next (msg),++timestamp) {
flvtag_amfcaption (&tag,timestamp,msg);
fprintf (stderr,"Wrote AMF %d\n", (int) flvtag_raw_size (&tag));
flv_write_tag (out,&tag);
}

Expand Down Expand Up @@ -72,10 +75,10 @@ int main (int argc, char** argv)

if (nextParty <= flvtag_timestamp (&tag)) {
get_dudes (partyDudes);
// write_amfcaptions (out,nextParty,partyDudes);
flvtag_addcaption (&tag, partyDudes);
write_amfcaptions (out,nextParty,partyDudes);
// flvtag_addcaption (&tag, partyDudes);
fprintf (stderr,"%d: %s\n",nextParty, partyDudes);
nextParty += 1000; // party every second
nextParty += 1000; // party all the time
}

flv_write_tag (out,&tag);
Expand Down
76 changes: 76 additions & 0 deletions examples/rtmpspit.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include "flv.h"
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <librtmp/rtmp.h>
#include <sys/select.h>

int main (int argc, const char** argv)
{
FILE* flv;
RTMP* rtmp;
RTMPPacket rtmpPacket;

flvtag_t tag;
int has_audio, has_video;
char* url = 0;

if (2 >= argc) {
fprintf (stderr,"Usage %s [input] [url]\n",argv[0]);
}

url = (char*) argv[2];
flv = flv_open_read (argv[1]);

if (! flv) {
fprintf (stderr,"Could not open %s\n",argv[1]);
return EXIT_FAILURE;
}

if (! flv_read_header (flv, &has_audio, &has_video)) {
fprintf (stderr,"Not an flv file %s\n",argv[1]);
return EXIT_FAILURE;
}

flvtag_init (&tag);
rtmp = RTMP_Alloc();
RTMP_Init (rtmp);
fprintf (stderr,"Connecting to %s\n", url);
RTMP_SetupURL (rtmp, url);
RTMP_EnableWrite (rtmp);

RTMP_Connect (rtmp, NULL);
RTMP_ConnectStream (rtmp, 0);
memset (&rtmpPacket, 0, sizeof (RTMPPacket));

if (! RTMP_IsConnected (rtmp)) {
fprintf (stderr,"RTMP_IsConnected() Error\n");
return EXIT_FAILURE;
}

while (flv_read_tag (flv,&tag)) {
if (! RTMP_IsConnected (rtmp) || RTMP_IsTimedout (rtmp)) {
fprintf (stderr,"RTMP_IsConnected() Error\n");
return EXIT_FAILURE;
}

RTMP_Write (rtmp, (const char*) flvtag_raw_data (&tag),flvtag_raw_size (&tag));

// Handle RTMP ping and such
fd_set sockset; struct timeval timeout = {0,0};
FD_ZERO (&sockset); FD_SET (RTMP_Socket (rtmp), &sockset);
register int result = select (RTMP_Socket (rtmp) + 1, &sockset, NULL, NULL, &timeout);

if (result == 1 && FD_ISSET (RTMP_Socket (rtmp), &sockset)) {
RTMP_ReadPacket (rtmp, &rtmpPacket);

if (! RTMPPacket_IsReady (&rtmpPacket)) {
fprintf (stderr,"Received RTMP packet\n");
RTMP_ClientPacket (rtmp,&rtmpPacket);
RTMPPacket_Free (&rtmpPacket);
}
}
}

return EXIT_SUCCESS;
}
11 changes: 9 additions & 2 deletions src/caption.c
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,12 @@ int eia608_write_char (caption_frame_t* frame, char* c)
return 0;
}

int caption_frame_end (caption_frame_t* frame)
{
memcpy (&frame->front,&frame->back,sizeof (caption_frame_buffer_t));
return 1;
}

int caption_frame_decode_text (caption_frame_t* frame, uint16_t cc_data)
{
int chan;
Expand Down Expand Up @@ -262,7 +268,7 @@ int caption_frame_decode_control (caption_frame_t* frame, uint16_t cc_data)
return LIBCAPTION_OK;

case eia608_control_end_of_caption:
memcpy (&frame->front,&frame->back,sizeof (caption_frame_buffer_t));
caption_frame_end (frame);
return LIBCAPTION_READY;

// cursor positioning
Expand Down Expand Up @@ -334,7 +340,7 @@ int caption_frame_from_text (caption_frame_t* frame, const utf8_char_t* data)
ssize_t size = (ssize_t) strlen (data);
size_t char_count, char_length, line_length = 0, trimmed_length = 0;
caption_frame_init (frame);
frame->state.mod = 2; // POP-ON
frame->state.mod = CAPTION_POP_ON;

for (r = 0 ; 0 < size && SCREEN_ROWS > r ; ++r) {
const utf8_char_t* cap_data = data;
Expand All @@ -359,6 +365,7 @@ int caption_frame_from_text (caption_frame_t* frame, const utf8_char_t* data)
size -= (ssize_t) line_length;
}

caption_frame_end (frame);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
Expand Down
24 changes: 12 additions & 12 deletions src/utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,14 @@ size_t utf8_string_length (const utf8_char_t* data, size_t size)
size_t char_length, byts;

if (0 == size) {
byts = strlen (data);
size = utf8_char_count (data,0);
} else {

for (byts = 0 ; 0 < size ; --size) {
char_length = utf8_char_length (data);
if (0 == (char_length = utf8_char_length (data))) {
break;
}

data += char_length;
byts += char_length;
}
Expand All @@ -70,24 +73,21 @@ size_t utf8_char_copy (utf8_char_t* dst, const utf8_char_t* src)
return bytes;
}

// returnes the number of utf8 charcters in a string givne the numbe of bytes
// returnes the number of utf8 charcters in a string given the number of bytes
// to count until the a null terminator, pass 0 for size
utf8_size_t utf8_char_count (const char* data, size_t size)
{
int i;
size_t bytes = 0;
size_t i, bytes = 0;
utf8_size_t count = 0;

if (0 == size) {
size = strlen (data);
}

for (i = 0 ; i < (int) size ; i += (int) bytes) {
bytes = utf8_char_length (&data[i]);

if (bytes > 0) {
++count;
} else { break; }
for (i = 0 ; i < size ; ++count, i += bytes) {
if (0 == (bytes = utf8_char_length (&data[i]))) {
break;
}
}

return count;
Expand All @@ -96,7 +96,7 @@ utf8_size_t utf8_char_count (const char* data, size_t size)
// returnes the length of the line in bytes triming not printable charcters at the end
size_t utf8_trimmed_length (const char* data, size_t size)
{
for (; 0 < size && ' ' >= data[size-1] ; --size) { }
for (; 0 < size && ' ' >= (uint8_t)data[size-1] ; --size) { }

return size;
}
Expand Down

0 comments on commit fe94686

Please sign in to comment.