-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.c
184 lines (136 loc) · 5.21 KB
/
reader.c
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
#include "restream.h"
#include "guide.h"
#include "playlist.h"
#include "infile.h"
#include "reader.h"
#include "writer.h"
void *reader(void *parms){
/* This function runs on its own thread and monitors the pipe */
int pipefd_r;
char *byte_buffer[4096];
int reader_active;
ssize_t bytes_read;
ctx_restream *restrm = parms;
pthread_setname_np(pthread_self(), "OutputReader");
snprintf(restrm->function_name,1024,"%s","reader");
thread_count++;
//fprintf(stderr, "%s: reader started %d\n"
// ,restrm->guide_info->guide_displayname,thread_count);
pipefd_r = 0;
reader_active = TRUE;
restrm->reader_action=READER_ACTION_CLOSE;
while (reader_active){
switch(restrm->reader_action) {
case READER_ACTION_START:
if (pipefd_r == 0) pipefd_r = open(restrm->out_filename, O_RDONLY | O_NONBLOCK);
if (pipefd_r == -1 ) {
pipefd_r = 0;
restrm->reader_status = READER_STATUS_CLOSED;
} else {
restrm->reader_status = READER_STATUS_READING;
}
break;
case READER_ACTION_BYTE:
if (pipefd_r == 0) pipefd_r = open(restrm->out_filename, O_RDONLY | O_NONBLOCK);
if (pipefd_r == -1 ) {
pipefd_r = 0;
restrm->reader_status = READER_STATUS_CLOSED;
} else {
restrm->reader_status = READER_STATUS_READBYTE;
}
break;
case READER_ACTION_OPEN:
if (pipefd_r == 0){
pipefd_r = open(restrm->out_filename, O_RDONLY | O_NONBLOCK);
if (pipefd_r == -1 ) {
pipefd_r = 0;
restrm->reader_status = READER_STATUS_CLOSED;
} else {
restrm->reader_status = READER_STATUS_PAUSED;
}
}
break;
case READER_ACTION_CLOSE:
if (pipefd_r != 0){
close(pipefd_r);
pipefd_r = 0;
}
restrm->reader_status = READER_STATUS_CLOSED;
break;
case READER_ACTION_END:
if (pipefd_r != 0){
close(pipefd_r);
pipefd_r = 0;
restrm->reader_status = READER_STATUS_CLOSED;
}
reader_active = FALSE;
break;
}
if ((restrm->reader_status == READER_STATUS_READING) && (pipefd_r != 0) ){
bytes_read=read(pipefd_r, byte_buffer, sizeof(byte_buffer));
(void)bytes_read;
} else if ((restrm->reader_status == READER_STATUS_READBYTE) && (pipefd_r != 0) ){
bytes_read=read(pipefd_r, byte_buffer, sizeof(byte_buffer));
(void)bytes_read;
SLEEP(0, 10000000);
} else {
SLEEP(1,0);
}
restrm->watchdog_reader=av_gettime_relative();
}
thread_count--;
//fprintf(stderr, "%s: reader exit %d\n"
// ,restrm->guide_info->guide_displayname,thread_count);
restrm->reader_status = READER_STATUS_INACTIVE;
pthread_exit(NULL);
}
void reader_start(ctx_restream *restrm){
if (restrm->reader_status != READER_STATUS_READING){
pthread_mutex_lock(&restrm->mutex_reader);
restrm->reader_action = READER_ACTION_START;
while (restrm->reader_status != READER_STATUS_READING);
pthread_mutex_unlock(&restrm->mutex_reader);
}
}
void reader_startbyte(ctx_restream *restrm){
if (restrm->reader_status != READER_STATUS_READBYTE){
pthread_mutex_lock(&restrm->mutex_reader);
restrm->reader_action = READER_ACTION_BYTE;
while (restrm->reader_status != READER_STATUS_READBYTE);
pthread_mutex_unlock(&restrm->mutex_reader);
}
}
void reader_close(ctx_restream *restrm){
pthread_mutex_lock(&restrm->mutex_reader);
restrm->reader_action = READER_ACTION_CLOSE;
while (restrm->reader_status != READER_STATUS_CLOSED);
pthread_mutex_unlock(&restrm->mutex_reader);
}
void reader_end(ctx_restream *restrm){
pthread_mutex_lock(&restrm->mutex_reader);
restrm->reader_action = READER_ACTION_END;
while (restrm->reader_status != READER_STATUS_INACTIVE);
pthread_mutex_unlock(&restrm->mutex_reader);
pthread_mutex_destroy(&restrm->mutex_reader);
}
void reader_flush(ctx_restream *restrm){
snprintf(restrm->function_name,1024,"%s","reader_flush");
reader_start(restrm);
sleep(1);
reader_close(restrm);
}
void reader_init(ctx_restream *restrm){
snprintf(restrm->function_name,1024,"%s","reader_init");
//fprintf(stderr,"%s: reader_init entry %d\n"
// ,restrm->guide_info->guide_displayname,thread_count);
pthread_mutex_init(&restrm->mutex_reader, NULL);
pthread_attr_t handler_attribute;
restrm->watchdog_reader=av_gettime_relative();
pthread_attr_init(&handler_attribute);
pthread_attr_setdetachstate(&handler_attribute, PTHREAD_CREATE_DETACHED);
pthread_create(&restrm->reader_thread, &handler_attribute, reader, restrm);
pthread_attr_destroy(&handler_attribute);
reader_close(restrm);
//fprintf(stderr,"%s: reader_init exit %d\n"
// ,restrm->guide_info->guide_displayname,thread_count);
}