aboutsummaryrefslogtreecommitdiff
path: root/src/capture.c
blob: 6063991d69cfe4a102063c537673eccfb2b155d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/*
 * capture.c - USB bulk stream capture from EP 0x84
 *
 * Part of mpegcap.
 *
 * The MPEG-2 program stream arrives on bulk endpoint 0x84.  We use
 * libusb's async API with a small pool of transfers so that there is
 * always a pending read (required for reliable high-bandwidth capture).
 */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "pvrusb2.h"
#include "capture.h"

#define NUM_TRANSFERS 4
#define TRANSFER_SIZE 0x20000

struct pvr2_capture {
	pvr2_dev        *p;
	pvr2_data_cb     cb;
	void            *user;
	libusb_transfer *xfer[NUM_TRANSFERS];
	uint8_t         *buf[NUM_TRANSFERS];
	volatile int     stop;
	volatile int     errors;
	/* diagnostics: USB stream health.  Short/zero-length completions mean
	 * the encoder is not keeping up and frames are being lost, which shows
	 * up as a large latency in the player because the PTS still advances at
	 * the nominal frame rate. */
	volatile long    pkts;        /* completed non-empty transfers */
	volatile long    short_xfer;  /* completed, but < TRANSFER_SIZE */
	volatile long    zero_xfer;   /* completed with 0 bytes */
	volatile long    stall;       /* LIBUSB_TRANSFER_STALL */
	volatile long    overflow;    /* LIBUSB_TRANSFER_OVERFLOW */
	/* encoder (type, subtype) this capture was started with, so the stop
	 * command can be sent with the same values before streaming off */
	int              enc_stop_valid;
	uint32_t         enc_type, enc_subtype;
};

static void LIBUSB_CALL capture_cb(struct libusb_transfer *tr)
{
	struct pvr2_capture *c = tr->user_data;

	if (tr->status == LIBUSB_TRANSFER_COMPLETED && tr->actual_length > 0) {
		c->pkts++;
		if (tr->actual_length < TRANSFER_SIZE)
			c->short_xfer++;
		if (c->cb)
			c->cb(tr->buffer, tr->actual_length, c->user);
	} else if (tr->status == LIBUSB_TRANSFER_COMPLETED) {
		c->zero_xfer++;      /* empty completion: no data, not an error */
	} else if (tr->status == LIBUSB_TRANSFER_STALL) {
		c->stall++;
		c->errors++;
		if (c->errors > 100)
			c->stop = 1;
		return;
	} else if (tr->status == LIBUSB_TRANSFER_OVERFLOW) {
		c->overflow++;
		c->errors++;
		if (c->errors > 100)
			c->stop = 1;
		return;
	} else if (tr->status == LIBUSB_TRANSFER_NO_DEVICE ||
		   tr->status == LIBUSB_TRANSFER_ERROR) {
		c->errors++;
		if (c->errors > 100)
			c->stop = 1;
		return;
	}

	if (!c->stop) {
		if (libusb_submit_transfer(tr) != 0) {
			c->errors++;
			if (c->errors > 100)
				c->stop = 1;
		}
	}
}

pvr2_capture_t *pvr2_capture_new(pvr2_dev *p, pvr2_data_cb cb, void *user)
{
	struct pvr2_capture *c = calloc(1, sizeof(*c));
	int i;

	if (!c)
		return NULL;
	c->p = p;
	c->cb = cb;
	c->user = user;

	for (i = 0; i < NUM_TRANSFERS; i++) {
		c->buf[i] = malloc(TRANSFER_SIZE);
		c->xfer[i] = libusb_alloc_transfer(0);
		if (!c->buf[i] || !c->xfer[i]) {
			pvr2_capture_free(c);
			return NULL;
		}
		libusb_fill_bulk_transfer(c->xfer[i], p->dev, EP_VIDEO_IN,
					  c->buf[i], TRANSFER_SIZE,
					  capture_cb, c, 0);
	}
	return c;
}

void pvr2_capture_stop(pvr2_capture_t *c)
{
	if (c)
		c->stop = 1;
}

void pvr2_capture_set_enc_stop(pvr2_capture_t *c, uint32_t captype,
			       uint32_t subtype)
{
	if (!c)
		return;
	c->enc_type = captype;
	c->enc_subtype = subtype;
	c->enc_stop_valid = 1;
}

int pvr2_capture_run(pvr2_capture_t *c)
{
	int i, r;

	for (i = 0; i < NUM_TRANSFERS; i++) {
		r = libusb_submit_transfer(c->xfer[i]);
		if (r != 0) {
			fprintf(stderr, "submit transfer failed: %s\n",
				libusb_error_name(r));
			return -1;
		}
	}

	if (pvr2_streaming_on(c->p) < 0) {
		fprintf(stderr, "streaming on (0x36) failed\n");
		return -1;
	}

	while (!c->stop) {
		struct timeval tv = { 0, 250000 };
		r = libusb_handle_events_timeout_completed(c->p->ctx, &tv, NULL);
		if (r != 0 && r != LIBUSB_ERROR_TIMEOUT) {
			if (r == LIBUSB_ERROR_INTERRUPTED)
				continue;
			fprintf(stderr, "handle_events: %s\n", libusb_error_name(r));
			break;
		}
	}

	/*
	 * ★ Tell the encoder to end the capture BEFORE switching USB
	 * streaming off (vendor order: 0x82 then 0x37; yacxm does the same
	 * and then waits for the EOS interrupt).  Without this the encoder
	 * keeps its mode and frame buffer, so the next run -- especially one
	 * that changes capture type (MPEG -> raw) -- came up stuck showing
	 * the previous buffer.  NOENCSTOP=1 skips it, which is how that
	 * symptom is reproduced on purpose (A/B).
	 */
	if (c->enc_stop_valid && !getenv("NOENCSTOP")) {
		uint32_t when = (c->enc_type == 0) ? 0 : 1;   /* MPEG: end of GOP */
		pvr2_encoder_stop(c->p, c->enc_type, c->enc_subtype, when);
		/* let the firmware deliver the tail / EOS */
		{
			struct timeval tv = { 0, 300000 };
			libusb_handle_events_timeout_completed(c->p->ctx, &tv,
							       NULL);
		}
	}

	pvr2_streaming_off(c->p);
	for (i = 0; i < NUM_TRANSFERS; i++)
		libusb_cancel_transfer(c->xfer[i]);
	{
		struct timeval tv = { 0, 200000 };
		libusb_handle_events_timeout_completed(c->p->ctx, &tv, NULL);
	}
	return c->errors ? -1 : 0;
}

void pvr2_capture_stats(pvr2_capture_t *c, pvr2_capture_stats_t *st)
{
	if (!st)
		return;
	memset(st, 0, sizeof(*st));
	if (!c)
		return;
	st->packets    = c->pkts;
	st->short_xfer = c->short_xfer;
	st->zero_xfer  = c->zero_xfer;
	st->stall      = c->stall;
	st->overflow   = c->overflow;
	st->errors     = c->errors;
}

void pvr2_capture_free(pvr2_capture_t *c)
{
	int i;
	if (!c)
		return;
	for (i = 0; i < NUM_TRANSFERS; i++) {
		if (c->xfer[i])
			libusb_free_transfer(c->xfer[i]);
		free(c->buf[i]);
	}
	free(c);
}