Пример #1
0
END_TEST

START_TEST(test_ring_write_read_array)
{
	u8 array[10];
	u8 array1[] = "ABCD";
	u8 array2[] = "EFGHIJ";

	fail_unless(4 == ring_write(&test_ring, array1, 4));
	fail_unless(-5 == ring_write(&test_ring, array2, 6));

	memset(array, 0, 10);
	fail_unless(9 == ring_read(&test_ring, array, 10));
	fail_unless(0 == memcmp(array, "ABCDEFGHI", 9));
	fail_unless(0 != memcmp(array, "ABCDEFGHIJ", 10));

	memset(array, 0, 10);
	fail_unless(4 == ring_write(&test_ring, (u8 *)"ABCD", 4));
	fail_unless(4 == ring_read(&test_ring, array, 10));
	fail_unless(0 == memcmp(array, "ABCD", 4));

	memset(array, 0, 10);
	fail_unless(6 == ring_write(&test_ring, (u8 *)"ABCDEF", 6));
	fail_unless(-4 == ring_read(&test_ring, array, 4));
	fail_unless(0 == memcmp(array, "ABCD", 4));
}
Пример #2
0
/* Send a request. */
static void xenbus_send(uint32_t type, ...)
{
    struct xsd_sockmsg hdr;
    va_list ap;
    struct {
        const char *data;
        uint32_t len;
    } seg[MAX_SEGMENTS];
    evtchn_send_t send;
    int i, n;

    /* Not acceptable to use xenbus before setting it up */
    ASSERT(rings != NULL);

    /* Put the request on the ring */
    hdr.type = type;
    hdr.req_id = 0;  /* We only ever issue one request at a time */
    hdr.tx_id = 0;   /* We never use transactions */
    hdr.len = 0;

    va_start(ap, type);
    for ( i = 0; ; i++ ) {
        seg[i].data = va_arg(ap, const char *);
        seg[i].len = va_arg(ap, uint32_t);

        if ( seg[i].data == NULL )
            break;

        hdr.len += seg[i].len;
    }
    n = i;
    va_end(ap);

    ring_write((char *) &hdr, sizeof hdr);
    for ( i = 0; i < n; i++ )
        ring_write(seg[i].data, seg[i].len);

    /* Tell the other end about the request */
    send.port = event;
    hypercall_event_channel_op(EVTCHNOP_send, &send);
}
Пример #3
0
int main()
{
    ringbuffer_t ring1, ring2;
    ringbuffer_t *ro = &ring1, *rw = &ring2;
    ring_init(ro, 1024, 0);
    ring_init(rw, 0, ro->header);

    ring_dump(ro, "ro"); ring_dump(rw, "rw");

    ring_write(rw, "test", 4);
    ring_dump(ro, "ro"); ring_dump(rw, "rw");

    ring_write(rw, "test", 0);
    ring_dump(ro, "ro"); ring_dump(rw, "rw");

    ring_shift(ro);
    ring_dump(ro, "ro"); ring_dump(rw, "rw");

    ring_shift(ro);
    ring_dump(ro, "ro"); ring_dump(rw, "rw");
}
Пример #4
0
/*
 * 从串口读取数据
 */
int serial_read(urg_serial_t *serial, char *data, int max_size, int timeout)
{
    int buffer_size;
    int read_n;
    int filled = 0;

    if (max_size <= 0) {
        return 0;
    }

    /*  If there is a single character return it */
    if (serial->has_last_ch != False) {
        data[0] = serial->last_ch;
        serial->has_last_ch = False;
        ++filled;
    }
    if (serial->fd == INVALID_FD) {
        if (filled > 0) {
            return filled;
        } else {
            return -1;
        }
    }

    buffer_size = ring_size(&serial->ring);
    read_n = max_size - filled;
    if (buffer_size < read_n) {
        // Reads data if there is space in the ring buffer
        char buffer[RING_BUFFER_SIZE];
        int n = internal_receive(buffer,
                                 ring_capacity(&serial->ring) - buffer_size,
                                 serial, 0);
        if (n > 0) {
            ring_write(&serial->ring, buffer, n);
            buffer_size += n;
        }
    }

    // Returns the data stored in the ring buffer
    if (read_n > buffer_size) {
        read_n = buffer_size;
    }
    if (read_n > 0) {
        ring_read(&serial->ring, &data[filled], read_n);
        filled += read_n;
    }

    // Reads data within the given timeout
    filled += internal_receive(&data[filled], max_size - filled,
                               serial, timeout);
    return filled;
}
Пример #5
0
int main (void)
{
    ring_t ring;                /* Ring buffer structure.    */
    char buffer[128];           /* Ring buffer data.    */
    char *msg1 = "abcde";
    char *msg2 = "fghij";
    char foo[64];
    int num;
    
    ring_init (&ring, buffer, sizeof (buffer));

    ring_write (&ring, msg1, strlen (msg1));

    ring_write (&ring, msg2, strlen (msg2));

    num = ring_read (&ring, foo, sizeof (foo));
    foo[num] = '\0';

    printf ("%d: %s\n", num, foo);

    return 0;
}
Пример #6
0
/* ��M */
int serial_recv(serial_t *serial, char* data, int data_size_max, int timeout)
{
  int filled;
  int read_n;
  int buffer_size;

  if (data_size_max <= 0) {
    return 0;
  }

  /* �����߂����P����������΁A�����o�� */
  filled = 0;
  if (serial->has_last_ch_ != False) {
    data[0] = serial->last_ch_;
    serial->has_last_ch_ = False;
    ++filled;
  }

  if (! serial_isConnected(serial)) {
    if (filled > 0) {
      return filled;
    }
    return SerialConnectionFail;
  }

  buffer_size = ring_size(&serial->ring_);
  read_n = data_size_max - filled;
  if (buffer_size < read_n) {
    // �����O�o�b�t�@���̃f�[�^�ő���Ȃ���΁A�f�[�^��ǂݑ���
    char buffer[RingBufferSize];
    int n = internal_receive(buffer,
                             ring_capacity(&serial->ring_) - buffer_size,
                             serial, 0);
    ring_write(&serial->ring_, buffer, n);
  }
  buffer_size = ring_size(&serial->ring_);

  // �����O�o�b�t�@���̃f�[�^��Ԃ�
  if (read_n > buffer_size) {
    read_n = buffer_size;
  }
  if (read_n > 0) {
    ring_read(&serial->ring_, &data[filled], read_n);
    filled += read_n;
  }

  // �f�[�^���^�C���A�E�g�t���œǂݏo��
  filled += internal_receive(&data[filled],
                             data_size_max - filled, serial, timeout);
  return filled;
}
Пример #7
0
void
serial_in(uint8_t *buf, uint16_t len)
{
    ring_write(&input_ring, buf, len);

    if (buf && len > 0) {
        if (buf[0] == 'n')
            nkro_active ^= 1;
        if (buf[0] == 'm')
            show_matrix ^= 1;
    }

    printf("nkro %d\r", nkro_active);
}
Пример #8
0
int serial_read(urg_serial_t *serial, char *data, int max_size, int timeout)
{
    int filled = 0;
    int buffer_size;
    int read_n;

    if (max_size <= 0) {
        return 0;
    }

    /* 書き戻した1文字があれば、書き出す */
    if (serial->has_last_ch) {
        data[0] = serial->last_ch;
        serial->has_last_ch = False;
        ++filled;
    }

    if (serial->hCom == INVALID_HANDLE_VALUE) {
        if (filled > 0) {
            return filled;
        }
        return -1;
    }

    buffer_size = ring_size(&serial->ring);
    read_n = max_size - filled;
    if (buffer_size < read_n) {
        // リングバッファ内のデータで足りなければ、データを読み足す
        char buffer[RING_BUFFER_SIZE];
        int n = internal_receive(buffer,
                                 ring_capacity(&serial->ring) - buffer_size,
                                 serial, 0);
        ring_write(&serial->ring, buffer, n);
    }
    buffer_size = ring_size(&serial->ring);

    // リングバッファ内のデータを返す
    if (read_n > buffer_size) {
        read_n = buffer_size;
    }
    if (read_n > 0) {
        ring_read(&serial->ring, &data[filled], read_n);
        filled += read_n;
    }

    // データをタイムアウト付きで読み出す
    filled += internal_receive(&data[filled],
                               max_size - filled, serial, timeout);
    return filled;
}
Пример #9
0
int serial_recv(serial_t *serial, char* data, int data_size_max, int timeout)
{
  int filled = 0;
  int buffer_size;
  int read_n;

  if (data_size_max <= 0) {
    return 0;
  }


  if (serial->has_last_ch_) {
    data[0] = serial->last_ch_;
    serial->has_last_ch_ = False;
    ++filled;
  }

  if (! serial_isConnected(serial)) {
    if (filled > 0) {
      return filled;
    }
    return SerialConnectionFail;
  }

  buffer_size = ring_size(&serial->ring_);
  read_n = data_size_max - filled;
  if (buffer_size < read_n) {

    char buffer[RingBufferSize];
    int n = internal_receive(buffer,
                             ring_capacity(&serial->ring_) - buffer_size,
                             serial, 0);
    ring_write(&serial->ring_, buffer, n);
  }
  buffer_size = ring_size(&serial->ring_);


  if (read_n > buffer_size) {
    read_n = buffer_size;
  }
  if (read_n > 0) {
    ring_read(&serial->ring_, &data[filled], read_n);
    filled += read_n;
  }


  filled += internal_receive(&data[filled],
                             data_size_max - filled, serial, timeout);
  return filled;
}
Пример #10
0
void handle_trace(PyFrameObject *frame, Record__RecordType record_type, int n_arguments)
{
  static int count = 0;
  count++;
  record->type = record_type;
  record->n_arguments = n_arguments;
  record->time = floattime();
  record->tid = (long) pthread_self();
  record->depth = get_depth();
  set_string(&(record->module), PYSTR_TO_CHAR(frame->f_code->co_filename));
  set_string(&(record->function), PYSTR_TO_CHAR(frame->f_code->co_name));
  record->lineno = frame->f_code->co_firstlineno;
  record__pack(record, record_buf);
  ring_write(global_ring, record_buf, (unsigned long) record__get_packed_size(record));
  CLEAR_REFS();
}
Пример #11
0
static int process_render(struct userdata *u) {
    pa_assert(u);


    if (u->memchunk.length <= 0)
        pa_sink_render(u->sink, ioring->usable_buffer_space, &u->memchunk);


    pa_assert(u->memchunk.length > 0);

    xc_evtchn_notify(xce, xen_evtchn_port);
    for (;;) {
        ssize_t l;
        void *p;

        p = pa_memblock_acquire(u->memchunk.memblock);
	    /* xen: write data to ring buffer & notify backend */
        l = ring_write(ioring, (uint8_t*)p + u->memchunk.index, u->memchunk.length);

        pa_memblock_release(u->memchunk.memblock);

        pa_assert(l != 0);

        if (l < 0) {
            if (errno == EINTR)
                continue;
            else if (errno == EAGAIN)
                return 0;
            else {
                pa_log("Failed to write data to FIFO: %s", pa_cstrerror(errno));
                return -1;
            }

        } else {

            u->memchunk.index += (size_t) l;
            u->memchunk.length -= (size_t) l;

            if (u->memchunk.length <= 0) {
                pa_memblock_unref(u->memchunk.memblock);
                pa_memchunk_reset(&u->memchunk);
            }
        }

        return 0;
    }
}
int sci_write(const char *data, int size)
{
    unsigned char current_level = get_imask_exr();
    int n;

    // 送信データの格納
    set_imask_exr(interrupt_priority_);
    n = ring_write(&write_ring_, data, size);

    if (n > 0) {
        // 送信割り込みの許可
        SCI1.SCSCR.BYTE |= 0x80;
    }
    set_imask_exr(current_level);

    return n;
}
int _write(int file, char *ptr, int len)
{
	int ret;

	if (file == 1) {
		ret = ring_write(&output_ring, (uint8_t *)ptr, len);

		if (ret < 0)
			ret = -ret;

		USART_CR1(USART2) |= USART_CR1_TXEIE;

		return ret;
	}

	errno = EIO;
	return -1;
}
Пример #14
0
/** Write as many bytes (up to the desired size) that can currently
    fit in the ring buffer.  */
static ssize_t
buart_write_nonblock (buart_t buart, const void *data, size_t size)
{
    ssize_t ret;
    buart_dev_t *dev = buart;

    ret = ring_write (&dev->tx_ring, data, size);

    dev->tx_irq_enable ();

    if (ret == 0 && size != 0)
    {
        /* Would block.  */
        errno = EAGAIN;
        return -1;
    }
    return ret;
}
Пример #15
0
int gpm_send_set(u8 addr, u16 val)
{
    u8 dat[3];

    if(addr > 31)
	    return 1;

    dat[0] = addr | GP_MODE_WRITE;
    dat[1] = val & 0xFF;
    dat[2] = val >> 8;

    if(0 <= ring_write(&gpm_output_ring, dat, 3)){
	    if(gpm_hooks.trigger_output) gpm_hooks.trigger_output(gpm_hooks.trigger_output_data);
	    gpm_register_map[addr] = val;
	    return 0;
    }

    return 1;
}
Пример #16
0
int tty_rx_locked(struct tty *t, char ch, unsigned char flag) {
	uint16_t *slot = t->rx_buff + t->rx_ring.head;

	/* Some input must be processed immediatly, like Ctrl-C.
	 * All other data will be stored as unprocecessed (raw) data
	 * and will be processed only at tty_read (if called)
	 */

	tty_task_break_check(t, ch);

	if (!ring_write(&t->rx_ring, TTY_RX_BUFF_SZ, 1))
		return -1;

	*slot = (flag<<CHAR_BIT) | (unsigned char) ch;

	tty_notify(t, POLLIN);

	return 0;
}
Пример #17
0
void *write_sequence(void *arg_void) {
    arg_t *arg = (arg_t*)arg_void;
    ring *buf = arg->buf;
    size_t write_len = arg->write_len;
    uint8_t seq = 0;
    size_t temp_len = 16;
    uint8_t *temp = malloc(temp_len * sizeof(uint8_t));
    int *res = malloc(1 * sizeof(int));
    *res = 0;
    for (size_t i = 0; i < write_len; ) {
        size_t nitems = rand() % 16 + 1;
        if (i + nitems > write_len) {
            nitems = write_len - i;
        }
        for (size_t j = 0; j < nitems; j++) {
            temp[j] = seq;
            *res += seq;
            seq++;
            seq %= seq_len;
        }
        while (true) {
            if (arg->multi) {
                ring_writer_lock(buf);
            }
            if (ring_write(buf, temp, nitems) != -1) {
                if (arg->multi) {
                    ring_writer_unlock(buf);
                }
                break;
            }
            if (arg->multi) {
                ring_writer_unlock(buf);
            }
            usleep(10);
        }
        i += nitems;
    }
    free(temp);
    pthread_exit(res);
    return NULL;
}
Пример #18
0
int gpc_send_reg(u8 addr)
{
	u8 dat[3];

	if ((addr > 31) | !gpc_register_map[addr])
		return 1;

	dat[0] = addr;
	dat[1] = (*gpc_register_map[addr]) & 0xFF;
	dat[2] = (*gpc_register_map[addr]) >> 8;

	DEBUG("sending reg %02X with content %04X\n", addr,
	      *gpc_register_map[addr]);

	if (0 <= ring_write(&gpc_output_ring, dat, 3)) {
		if (gpc_hooks.trigger_output)
			gpc_hooks.trigger_output(gpc_hooks.trigger_output_data);
		return 0;
	}

	return 1;
}
Пример #19
0
void test_ring() {
  int size;
  unsigned char *buf = malloc(sizeof(unsigned char) * BUF_SIZE);
  Ring *ring = ring_malloc(BUF_SIZE);
  RingReader *reader = reader_malloc(ring);

  size = reader_read(reader, buf);
  assert(0 == size);  
  
  printf("one write and one read\n");
  ring_write(ring, "11", 2);
  size = reader_read(reader, buf);
  assert(2 == size);
  assert(0 == memcmp(buf, "11", 2));

  printf("two writes and one read due to overflow\n");
  ring_write(ring, "22", 2);
  ring_write(ring, "33", 2);
  size = reader_read(reader, buf);
  assert(-1 == size);
  size = reader_read(reader, buf);
  assert(2 == size);
  assert(0 == memcmp(buf, "33", 2));
  
  printf("two small writes and two reads\n");
  ring_write(ring, "4", 1);
  ring_write(ring, "5", 1);
  size = reader_read(reader, buf);
  assert(1 == size);
  assert(0 == memcmp(buf, "4", 1));
  size = reader_read(reader, buf);
  assert(1 == size);
  assert(0 == memcmp(buf, "5", 1));  

  printf("fill once again\n");
  ring_write(ring, "123456", 6);
  size = reader_read(reader, buf);
  assert(6 == size);
  assert(0 == memcmp(buf, "123456", 6));

  ring_free(ring);
}
Пример #20
0
static int
vrprintf(struct ring *ring, const char *fmt, va_list va)
{
    uint32_t mark = ring_mark(ring);
    char bf[24];
    char ch;

    while ((ch = *(fmt++))) {
        if (ch == '\n') {
            ring_write_ch(ring, '\n');
            ring_write_ch(ring, '\r');
        } else if (ch != '%') {
            ring_write_ch(ring, ch);
        } else {
            char zero_pad = 0;
            char *ptr;
            uint32_t len;

            ch = *(fmt++);

            /* Zero padding requested */
            if (ch == '0') {
                ch = *(fmt++);
                if (ch == '\0')
                    goto end;
                if (ch >= '0' && ch <= '9')
                    zero_pad = ch - '0';
                ch = *(fmt++);
            }

            switch (ch) {
            case 0:
                goto end;

            case 'u':
            case 'd':
                len = itoa(va_arg(va, uint32_t), 10, 0, (ch == 'u'), bf, zero_pad);
                ring_write(ring, (uint8_t *)bf, len);
                break;

            case 'x':
            case 'X':
                len = itoa(va_arg(va, uint32_t), 16, (ch == 'X'), 1, bf, zero_pad);
                ring_write(ring, (uint8_t *)bf, len);
                break;

            case 'c' :
                ring_write_ch(ring, (char)(va_arg(va, int)));
                break;

            case 's' :
                ptr = va_arg(va, char*);
                ring_write(ring, (uint8_t *)ptr, strlen(ptr));
                break;

            default:
                ring_write_ch(ring, ch);
                break;
            }
        }
    }
end:
    return ring_marklen(ring, mark);
}
Пример #21
0
void *capture_audio(void *data)
{
	/* This thread captures audio samples from audio device */
	/* TODO: add mechanism to end this thread when desired,
	 * perhaps by passing flags for indicating end of call? */
	struct connection_data conn = *((struct connection_data *)data);

	int ret;
	int rc;
	unsigned int rate = 8000;
        int size;
	short audio_samples[SAMPLES_PER_PERIOD];
        int fd;
	int frame_size = 2;

        snd_pcm_t *handle;
        snd_pcm_hw_params_t *params;
	snd_pcm_uframes_t frames;
        int buffer_size = SAMPLES_PER_PERIOD * sizeof(short);

        printf("rate is =%d \n", rate);

        ret = voip_init_pcm(&handle, &params, &buffer_size, &rate, RECORD);
	if (ret) {
                fprintf(stderr, "Unable to initialize PCM \n");
                goto capture_failure;
        }
        printf("In record main \n");
        printf("Pointer address to handle=%p \n", &handle);
        printf("Pointer to handle=%p \n", handle);
        printf("Pointer to params=%p \n", params);

	printf("Size of buffer to accomodate period of data : %d \n", buffer_size);
	while(1) {
                rc = voip_capture(handle, buffer_size / frame_size, audio_samples);
#if 0	
		rc = read(fd, audio_samples, sizeof audio_samples);
	
		if (rc == 0) {
			printf("EOF reached \n");
			break;
		} else if (rc < 0) {
			fprintf(stderr, "Error reading file \n");
			break;
		}
#endif
	
		printf("Read %d frames from capture source \n", rc);
		ring_write(sbuff, (char *)audio_samples, sizeof audio_samples);
		rc = send_audio(&conn);

		if (rc < 0) {
			fprintf(stderr,
				"Failed to transmit audio: %s \n",
				strerror(rc));
			/* TODO: Gracefully handle error condition, close socket etc */
			break;		
		}
        }
	ret = rc;

capture_end:
        voip_end_pcm(handle);
capture_failure:
	pthread_exit(NULL);

}
Пример #22
0
void *receive_audio(void *data)
{
	struct connection_data conn = *((struct connection_data *)data);	
	struct sockaddr_in other = conn.other; 
	char buffer[512];
	char compressed[SPEEX_FRAME_SIZE];
	short audio_samples[SPEEX_FRAME_SIZE];	
	int sockfd = conn.udp_sock_tx; /* Descriptor for UDP socket */
	/* receive data from socket, add to buffer */
	int addrlen = sizeof other;
	int rc;
	/* variables for speex */
        SpeexBits bits;
        void *state; /* For holding encoder state */
        int tmp;
	int nbytes;
	int i;

	int ret;
        unsigned int rate = 8000;
        int size;
        short playback_samples[SAMPLES_PER_PERIOD];
        int fd;
        int frame_size = 2;

	snd_pcm_t *handle;
        snd_pcm_hw_params_t *params;
        /* write many data at a time to device */
        /* the value of 5512 comes from aplay, investigate
         * why it is so */
	int buffer_size = SAMPLES_PER_PERIOD * sizeof(short);

        printf("rate is =%d \n", rate);

	dbg("Preparing audio playback");
        ret = voip_init_pcm(&handle, &params, &buffer_size, &rate, PLAYBACK);
        
	if (ret < 0) {
		fprintf(stderr,
			"Failed to prepare audio system\n");
		goto rcv_audio_end;
	}

	printf("Pointer address to handle=%p \n", &handle);
        printf("Pointer to handle=%p \n", handle);
        printf("Pointer to params=%p \n", params);
	
	dbg("Preparing speex for de-compression");
        state = speex_decoder_init(&speex_nb_mode);
	tmp = 1;
        speex_decoder_ctl(state, SPEEX_SET_ENH, &tmp);
        speex_bits_init(&bits);

	while(1) {
		memset(audio_samples, 0, sizeof audio_samples);
		memset(compressed, 0, sizeof compressed);
		memset(buffer, 0, sizeof buffer);
		printf("Waiting for data\n");
		if ((rc = recvfrom(sockfd, buffer, sizeof buffer, 0,
				(struct sockaddr *)&other, &addrlen)) < 0) {
			fprintf(stderr,
				"Unable to receive audio data: %s \n",
				strerror(errno));
			goto rcv_sock_close;
		}
		printf("Received %d compressed bytes on UDP socket \n", rc);

		for (i = 0; i < 5; i++) {
			speex_bits_reset(&bits);
			/* each encoded speex frame takes 38 bytes */
			memcpy(compressed, buffer + i * 38, 38);

                	speex_bits_read_from(&bits, compressed, 38);
		
			/* Decode here */
                	speex_decode_int(state, &bits, audio_samples);	
		
			ring_write(rbuff, (char *)audio_samples, sizeof audio_samples);

		}
		ret = ring_read(rbuff, (char *)audio_samples, buffer_size);
		if ( ret != buffer_size) {
                        fprintf(stderr,
                                "short read: read %d bytes \n", ret);
                }
                printf("Playing audio \n");
                /* write frames in one period to device */
                ret = voip_playback(handle, buffer_size / frame_size, audio_samples);
	}

        voip_end_pcm(handle);
	/* Destroy the encoder state */
        speex_decoder_destroy(state);
        /* Destroy the bits-packing */
        speex_bits_destroy(&bits);

rcv_sock_close:
	close(sockfd);	

rcv_audio_end:
        pthread_exit(NULL);
}
void rxi1(void)
{
    char ch = SCI1.SCRDR;
    ring_write(&read_ring_, &ch, 1);
    SCI1.SCSSR.BYTE &= ~0x40;
}
Пример #24
0
void get_avi(pes_in_t *p, uint8_t *buf, int count, void (*func)(pes_in_t *p))
{
    int l;
    int c=0;
    struct replex *rx= (struct replex *) p->priv;


//	show_buf(buf,16);
    while (c < count && p->found < 8
            &&  !p->done) {
        switch ( p->found ) {
        case 0:
            if (buf[c] == '0') p->found++;
            else p->found = 0;
            c++;
            break;
        case 1:
            if (buf[c] == '0'|| buf[c] == '1') {
                p->found++;
                p->which = buf[c] - '0';
            } else if (buf[c] == '0') {
                p->found = 1;
            } else p->found = 0;
            c++;
            break;
        case 2:
            switch(buf[c]) {
            case 'w':
            case 'd':
                p->found++;
                p->type=buf[c];
                break;
            default:
                p->found = 0;
                break;
            }
            c++;
            break;

        case 3:
            switch(buf[c]) {
            case 'b':
                if (p->type == 'w') {
                    p->found++;
                    p->type = 1;
                } else p->found = 0;
                break;
            case 'c':
                if (p->type == 'd') {
                    p->found++;
                    p->type = 0xE0;
                } else p->found = 0;
                break;
            default:
                p->found = 0;
                break;
            }
            switch(p->type) {

            case 1:
                p->rbuf = &rx->arbuffer[0];
                break;

            case 0xE0:
                p->rbuf = &rx->vrbuffer;
                break;
            }
            c++;
            break;

        case 4:
            p->plen[0] = buf[c];
            c++;
            p->found++;
            break;

        case 5:
            p->plen[1] = buf[c];
            c++;
            p->found++;
            break;

        case 6:
            p->plen[2] = buf[c];
            c++;
            p->found++;
            break;

        case 7:
            p->plen[3] = buf[c];
            c++;
            p->found++;
            p->plength = getsize_buf(p->plen);
            if (!p->plength) {
                func(p);
                p->found=0;
                break;
            }
            p->done = 1;
            p->ini_pos = ring_wpos(p->rbuf);
            /*
            if (p->type == 1) fprintf(stderr,"audio 0x%x 0x%x\n",
            			  p->plength,ALIGN(p->plength));
            if (p->type == 1) fprintf(stderr,"video 0x%x 0x%x\n",
            			  p->plength,ALIGN(p->plength));
            */
            break;

        default:

            break;
        }
    }
    if (p->done || p->found > 8) {
        while (c < count && p->found < p->plength+8) {
            l = count -c;
            if (l+p->found > p->plength+8)
                l = p->plength+8-p->found;
            if (ring_write(p->rbuf, buf+c, l)<0) {
                fprintf(stderr,	"ring buffer overflow %d\n"
                        ,p->rbuf->size);
                exit(1);
            }
            p->found += l;
            c += l;
        }
        if(p->found == p->plength+8) {
            func(p);
        }
    }

    if (p->plength && p->found == p->plength+8) {
        int a = 0;//ALIGN(p->plength);
        init_pes_in(p, 0, NULL, p->withbuf);
        if (c+a < count)
            get_avi(p, buf+c+a, count-c-a, func);
    }
}
Пример #25
0
int get_avi_from_index(pes_in_t *p, int fd, avi_context *ac,
                       void (*func)(pes_in_t *p), int insize)
{
    struct replex *rx= (struct replex *) p->priv;
    avi_index *idx = ac->idx;
    int cidx = ac->current_idx;
    uint8_t buf[MAX_BUF_SIZE];
    uint32_t cid;
    int c=0;
    off_t pos=0;
    int per = 0;
    static int lastper=0;

    if (cidx > ac->num_idx_frames) return -2;

    switch(idx[cidx].id) {
    case TAG_IT('0','1','w','b'):
        p->type = 1;
        p->rbuf = &rx->arbuffer[0];
        break;

    case TAG_IT('0','0','d','c'):
        p->type = 0xE0;
        p->rbuf = &rx->vrbuffer;
        break;

    default:
        fprintf(stderr,"strange chunk :\n");
        show_buf((uint8_t *) &idx[cidx].id,4);
        fprintf(stderr,"offset: 0x%04x  length: 0x%04x\n",
                (int)idx[cidx].off, (int)idx[cidx].len);
        ac->current_idx++;
        p->found=0;
        return 0;
        break;
    }

    memset(buf, 0, MAX_BUF_SIZE);
    pos=lseek (fd, idx[cidx].off+ac->movi_start-4, SEEK_SET);
    read(fd,buf,idx[cidx].len);
    cid = getle32(buf);
    c+=4;
    p->plength = getsize_buf(buf+c);
//	show_buf(buf,16);
    if (idx[cidx].len > insize) return 0;

    if (idx[cidx].len > MAX_BUF_SIZE) {
        fprintf(stderr,"Buffer too small in get_avi_from_index\n");
        exit(1);
    }
    if (!idx[cidx].len) {
        func(p);
        ac->current_idx++;
        p->found=0;
        return 0;
    }
    if (cid != idx[cidx].id) {
        char *cc;
        cc = (char *)&idx[cidx].id;
        fprintf(stderr,"wrong chunk id: %c%c%c%c != %c%c%c%c\n",
                buf[0],buf[1],buf[2],buf[3]
                ,*cc,*(cc+1),*(cc+2),*(cc+3));

        print_index(ac,cidx);
        exit(1);
    }
    if (p->plength != idx[cidx].len) {
        fprintf(stderr,"wrong chunk size: %d != %d\n",
                (int)p->plength, idx[cidx].len);
        exit(1);
    }
    c+=4;
    p->done = 1;
    p->ini_pos = ring_wpos(p->rbuf);

    per = (int)(100*(pos-ac->movi_start)/ac->movi_length);
    if (per>lastper) fprintf(stderr,"read %3d%%\r", per);
    lastper = per;

    if (ring_write(p->rbuf, buf+c, p->plength)<0) {
        fprintf(stderr,	"ring buffer overflow %d 0x%02x\n"
                ,p->rbuf->size,p->type);
        exit(1);
    }

    func(p);
    init_pes_in(p, 0, NULL, p->withbuf);

    ac->current_idx++;

    return 0;
}
Пример #26
0
static int tcpclient_buffer_write(urg_tcpclient_t* cli,
                                  const char* data, int size)
{
    return ring_write(&cli->rb, data, size);
}
Пример #27
0
int
puts(const char *s)
{
    return ring_write(&output_ring, (uint8_t *)s, strlen(s));
}