diff options
Diffstat (limited to 'bitstream.c')
-rw-r--r-- | bitstream.c | 67 |
1 files changed, 60 insertions, 7 deletions
diff --git a/bitstream.c b/bitstream.c index fae619f..02f1515 100644 --- a/bitstream.c +++ b/bitstream.c @@ -1,5 +1,7 @@ #include <stdio.h> #include <stdlib.h> +#include <zmq.h> +#include <string.h> #include "common.h" #include "mem.h" #include "bitstream.h" @@ -104,23 +106,74 @@ void empty_buffer (Bit_stream_struc * bs, int minimum) { register int i; - for (i = bs->buf_size - 1; i >= minimum; i--) - fwrite (&bs->buf[i], sizeof (unsigned char), 1, bs->pt); + if (bs->pt) { + for (i = bs->buf_size - 1; i >= minimum; i--) + fwrite (&bs->buf[i], sizeof (unsigned char), 1, bs->pt); - fflush (bs->pt); /* NEW SS to assist in debugging */ + fflush (bs->pt); /* NEW SS to assist in debugging */ - for (i = minimum - 1; i >= 0; i--) - bs->buf[bs->buf_size - minimum + i] = bs->buf[i]; + for (i = minimum - 1; i >= 0; i--) + bs->buf[bs->buf_size - minimum + i] = bs->buf[i]; - bs->buf_byte_idx = bs->buf_size - 1 - minimum; - bs->buf_bit_idx = 8; + bs->buf_byte_idx = bs->buf_size - 1 - minimum; + bs->buf_bit_idx = 8; + } + + if (bs->zmq_sock) { + unsigned char outbuf[bs->zmq_framesize]; + + int j = 0; + for (i = bs->buf_size - 1; i >= minimum; i--) { + outbuf[j++] = bs->buf[i]; + if (j >= bs->zmq_framesize) + break; + } + + if (j < bs->zmq_framesize) { + fprintf(stderr, "not enough data in buffer ! j=%d, req'd %d", + j, bs->zmq_framesize); + } + + int send_error = zmq_send(bs->zmq_sock, outbuf, bs->zmq_framesize, + ZMQ_DONTWAIT); + + if (send_error < 0) { + fprintf(stderr, "ZeroMQ send failed! %s\n", zmq_strerror(errno)); + } + + for (i = minimum - 1; i >= 0; i--) + bs->buf[bs->buf_size - minimum + i] = bs->buf[i]; + + bs->buf_byte_idx = bs->buf_size - 1 - minimum; + bs->buf_bit_idx = 8; + } } +static void *zmq_context; + /* open the device to write the bit stream into it */ void open_bit_stream_w (Bit_stream_struc * bs, char *bs_filenam, int size) { + bs->zmq_sock = NULL; + if (bs_filenam[0] == '-') bs->pt = stdout; + else if (strncmp(bs_filenam, "tcp://", 4) == 0) { + zmq_context = zmq_ctx_new(); + bs->zmq_sock = zmq_socket(zmq_context, ZMQ_PUB); + if (bs->zmq_sock == NULL) { + fprintf(stderr, "Error occurred during zmq_socket: %s\n", + zmq_strerror(errno)); + abort(); + } + if (zmq_connect(bs->zmq_sock, bs_filenam) != 0) { + fprintf(stderr, "Error occurred during zmq_connect: %s\n", + zmq_strerror(errno)); + abort(); + } + + bs->pt = NULL; + } else if ((bs->pt = fopen (bs_filenam, "wb")) == NULL) { fprintf (stderr, "Could not create \"%s\".\n", bs_filenam); exit (1); |