File: ripperX_plugin-encode.c

package info (click to toggle)
ripperx 2.7.3-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 2,652 kB
  • sloc: sh: 10,710; ansic: 7,013; makefile: 144
file content (122 lines) | stat: -rw-r--r-- 2,667 bytes parent folder | download | duplicates (3)
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

#include <stdio.h>
#include <stdlib.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>

#define AENCODER_OUTPUT_BUF_LENGTH  1024
#define AENCODER_OUTPUT_LINE_LENGTH 19
#define AENCODER_STATUS_LENGTH	5

#define CD_SECTORS_PER_FRAME        (float)1.959

#define PRINTOUT_INTERVAL           0.5

int aEncoder_read_stat (unsigned *current);
int find_aEncoder_output_read_offset (char *buf, int begin, int end);
void print_msg (int length, int current);

int
aEncoder_read_stat (unsigned *current)
{
	static char buf[AENCODER_OUTPUT_BUF_LENGTH];
	static char status[AENCODER_STATUS_LENGTH];
	int temp_offset, read_offset;
	ssize_t bytes_read;
	int bytes_avail, count;
	static int prev_bytes_avail;

	ioctl (0, FIONREAD, &bytes_avail);
	if (bytes_avail < 4 * AENCODER_OUTPUT_LINE_LENGTH) {
		if (bytes_avail == prev_bytes_avail)
			/* nothing available, let's wait */
			return -1;
		else {
			/* Record available bytes, and let's just wait */
			prev_bytes_avail = bytes_avail;
			return -1;
		}
	}
	prev_bytes_avail = -1;

	count = 0;
	do {
		bytes_read = read (0, (void *) buf, sizeof (buf));
		temp_offset = bytes_read - 4 * AENCODER_OUTPUT_LINE_LENGTH - 1;

		read_offset = find_aEncoder_output_read_offset (buf,
		              temp_offset,
		              sizeof (buf) - 1);
		if (read_offset < 0 ||
			        read_offset > sizeof (buf) - AENCODER_OUTPUT_LINE_LENGTH) {
			if (count == 0)
				return -1;
			else
				break;
		}

		strncpy (status, buf + read_offset, sizeof (status));
		status[sizeof (status) - 1] = '\0';
		sscanf (status, "%u", current);
		/* Convert it to cd sector unit */
		*current *= CD_SECTORS_PER_FRAME;
		count++;
	} while (bytes_read == sizeof (buf));

	if (*current != 0)
		return 0;

	return -1;
}

int
find_aEncoder_output_read_offset (char *buf, int begin, int end)
{
	int i;

	i = begin;

	do {
		while (buf[i] != '[' && i <= end - 5)
			i++;

		if (buf[i + 1] == ' ' && isdigit (buf[i + 5]))
			return i + 2;
		else
			i++;
	} while (i <= end - 5);
	return -1;
}

// print out [P 0.xxxx]\n
void
print_msg (int length, int current)
{
	printf ("[P ");
	printf ("%f]\n", (double) current / (double) length);
}

int
main (int argc, char **argv)
{
	int begin, length;
	int current;

	if (argc != 3) {
		fprintf (stderr, "This is ripperX plugin for the ISO encoder version 2. Syntax is\n"
		         "ripperX_plugin-encoder beginning_sector length_in_sector\n");
		exit (1);
	}

	sscanf (argv[1], "%d", &begin);
	sscanf (argv[2], "%d", &length);

	print_msg (length, 0);
	while (1) {
		if (aEncoder_read_stat (&current) == 0)
			print_msg (length, current);
		usleep (PRINTOUT_INTERVAL * 1000000);
	}
}