Mercurial > pt1.oyama
annotate src/http.c @ 147:f5e1ce2a62a6
update satelite channels.(From http://hg.honeyplanet.jp/pt1/)
author | Naoya OYAMA <naoya.oyama@gmail.com> |
---|---|
date | Wed, 22 Aug 2012 21:44:53 +0900 |
parents | 066f33b2213a |
children | a9f60d56d673 |
rev | line source |
---|---|
125 | 1 /* -*- tab-width: 4; indent-tabs-mode: nil -*- */ |
2 /* vim: set ts=4 sts=4 sw=4 expandtab number : */ | |
3 /* | |
4 * http.c : GeeXboX uShare Web Server handler. | |
5 * Originally developped for the GeeXboX project. | |
6 * Parts of the code are originated from GMediaServer from Oskar Liljeblad. | |
7 * Copyright (C) 2005-2007 Benjamin Zores <ben@geexbox.org> | |
8 * | |
9 * This program is free software; you can redistribute it and/or modify | |
10 * it under the terms of the GNU General Public License as published by | |
11 * the Free Software Foundation; either version 2 of the License, or | |
12 * (at your option) any later version. | |
13 * | |
14 * This program is distributed in the hope that it will be useful, | |
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
17 * GNU Library General Public License for more details. | |
18 * | |
19 * You should have received a copy of the GNU General Public License along | |
20 * with this program; if not, write to the Free Software Foundation, | |
21 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | |
22 */ | |
23 | |
24 #include <sys/types.h> | |
25 #include <sys/stat.h> | |
26 #include <fcntl.h> | |
27 #include <errno.h> | |
28 #include <stdio.h> | |
29 #include <stdlib.h> | |
30 #include <unistd.h> | |
31 #include <errno.h> | |
32 | |
136
2a9ac5ce2c7e
Remove internal libdlna and libupnp.(using OS package by default)
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
135
diff
changeset
|
33 #include <upnp.h> |
2a9ac5ce2c7e
Remove internal libdlna and libupnp.(using OS package by default)
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
135
diff
changeset
|
34 #include <upnptools.h> |
125 | 35 |
36 #include "services.h" | |
37 #include "cds.h" | |
38 #include "cms.h" | |
39 #include "msr.h" | |
40 #include "metadata.h" | |
41 #include "http.h" | |
42 #include "minmax.h" | |
43 #include "trace.h" | |
44 #include "presentation.h" | |
45 #include "osdep.h" | |
46 #include "mime.h" | |
47 #include "recpt1.h" | |
48 #include "tssplitter_lite.h" | |
49 | |
50 #define PROTOCOL_TYPE_PRE_SZ 11 /* for the str length of "http-get:*:" */ | |
51 #define PROTOCOL_TYPE_SUFF_SZ 2 /* for the str length of ":*" */ | |
52 | |
53 extern thread_data tdata; | |
54 | |
55 struct web_file_t { | |
56 char *fullpath; | |
57 off_t pos; | |
58 enum { | |
59 FILE_LOCAL, | |
60 FILE_MEMORY, | |
61 FILE_STREAM | |
62 } type; | |
63 union { | |
64 struct { | |
65 int fd; | |
66 struct upnp_entry_t *entry; | |
67 } local; | |
68 struct { | |
69 char *contents; | |
70 off_t len; | |
71 } memory; | |
72 struct { | |
73 int id; | |
74 STREAM_QUEUE_T *p_queue; | |
75 ARIB_STD_B25_BUFFER *qbuf; | |
76 off_t len; | |
77 } stream; | |
78 } detail; | |
79 }; | |
80 | |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
81 static off_t get_streaming_length (void); |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
82 |
125 | 83 static inline void |
84 set_info_file (struct File_Info *info, const size_t length, | |
85 const char *content_type) | |
86 { | |
87 info->file_length = length; | |
88 info->last_modified = 0; | |
89 info->is_directory = 0; | |
90 info->is_readable = 1; | |
91 info->content_type = ixmlCloneDOMString (content_type); | |
92 } | |
93 | |
94 //#define STREAM_LOCATION "/web/stream.ts" | |
95 static int | |
96 http_get_info (const char *filename, struct File_Info *info) | |
97 { | |
98 extern struct ushare_t *ut; | |
99 struct upnp_entry_t *entry = NULL; | |
146
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
100 int i, upnp_id = 0; |
125 | 101 char *content_type = NULL; |
102 char *protocol = NULL; | |
103 | |
104 if (!filename || !info) | |
105 return -1; | |
106 | |
107 log_verbose ("http_get_info, filename : %s\n", filename); | |
108 | |
109 upnp_id = atoi (strrchr (filename, '/') + 1); | |
110 entry = upnp_get_entry (ut, upnp_id); | |
111 | |
146
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
112 if (ut->nr_channel == 0) { |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
113 if (!strcmp (filename, STREAM_LOCATION)) { |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
114 log_verbose ("http_get_info, stream location found.\n"); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
115 info->is_readable = 1; |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
116 info->file_length = get_streaming_length(); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
117 info->last_modified = time(NULL); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
118 info->is_directory = 0; |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
119 info->content_type = ixmlCloneDOMString ("video/mpeg"); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
120 return 0; |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
121 } |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
122 } else { |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
123 for (i=0; i < ut->nr_channel; i++) { |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
124 if (!strcmp(filename, ut->location_name[i])) { |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
125 log_verbose ("http_get_info, stream location found [%s].\n", filename); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
126 info->is_readable = 1; |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
127 info->file_length = get_streaming_length(); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
128 info->last_modified = time(NULL); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
129 info->is_directory = 0; |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
130 info->content_type = ixmlCloneDOMString ("video/mpeg"); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
131 return 0; |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
132 } |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
133 } |
125 | 134 } |
135 if (!strcmp (filename, CDS_LOCATION)) | |
136 { | |
137 set_info_file (info, CDS_DESCRIPTION_LEN, SERVICE_CONTENT_TYPE); | |
138 return 0; | |
139 } | |
140 | |
141 if (!strcmp (filename, CMS_LOCATION)) | |
142 { | |
143 set_info_file (info, CMS_DESCRIPTION_LEN, SERVICE_CONTENT_TYPE); | |
144 return 0; | |
145 } | |
146 | |
147 if (!strcmp (filename, MSR_LOCATION)) | |
148 { | |
149 set_info_file (info, MSR_DESCRIPTION_LEN, SERVICE_CONTENT_TYPE); | |
150 return 0; | |
151 } | |
152 | |
153 if (ut->use_presentation && !strcmp (filename, USHARE_PRESENTATION_PAGE)) | |
154 { | |
155 if (build_presentation_page (ut) < 0) | |
156 return -1; | |
157 | |
158 set_info_file (info, ut->presentation->len, PRESENTATION_PAGE_CONTENT_TYPE); | |
159 return 0; | |
160 } | |
161 | |
162 if (ut->use_presentation && !strncmp (filename, USHARE_CGI, strlen (USHARE_CGI))) | |
163 { | |
164 if (process_cgi (ut, (char *) (filename + strlen (USHARE_CGI) + 1)) < 0) | |
165 return -1; | |
166 | |
167 set_info_file (info, ut->presentation->len, PRESENTATION_PAGE_CONTENT_TYPE); | |
168 return 0; | |
169 } | |
170 | |
171 if (!entry) | |
172 return -1; | |
173 log_verbose ("http_get_info, entry found.\n"); | |
174 | |
175 if (!entry->fullpath) | |
176 return -1; | |
177 | |
178 #if 0 | |
179 if (stat (entry->fullpath, &st) < 0) | |
180 return -1; | |
181 | |
182 if (access (entry->fullpath, R_OK) < 0) | |
183 { | |
184 if (errno != EACCES) { | |
185 log_verbose ("http_get_info, access() error.\n"); | |
186 return -1; | |
187 } | |
188 info->is_readable = 0; | |
189 } | |
190 else | |
191 info->is_readable = 1; | |
192 | |
193 /* file exist and can be read */ | |
194 info->file_length = st.st_size; | |
195 info->last_modified = st.st_mtime; | |
196 info->is_directory = S_ISDIR (st.st_mode); | |
197 #endif | |
198 | |
199 info->is_readable = 1; | |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
200 info->file_length = get_streaming_length(); |
125 | 201 info->last_modified = time(NULL); |
202 info->is_directory = 0; | |
203 | |
204 protocol = | |
205 #ifdef HAVE_DLNA | |
206 entry->dlna_profile ? | |
207 dlna_write_protocol_info (DLNA_PROTOCOL_INFO_TYPE_HTTP, | |
208 DLNA_ORG_PLAY_SPEED_NORMAL, | |
209 DLNA_ORG_CONVERSION_NONE, | |
210 DLNA_ORG_OPERATION_RANGE, | |
211 ut->dlna_flags, entry->dlna_profile) : | |
212 #endif /* HAVE_DLNA */ | |
213 mime_get_protocol (entry->mime_type); | |
214 | |
215 content_type = | |
216 strndup ((protocol + PROTOCOL_TYPE_PRE_SZ), | |
217 strlen (protocol + PROTOCOL_TYPE_PRE_SZ) | |
218 - PROTOCOL_TYPE_SUFF_SZ); | |
219 free (protocol); | |
220 | |
221 if (content_type) | |
222 { | |
223 info->content_type = ixmlCloneDOMString (content_type); | |
224 free (content_type); | |
225 } | |
226 else | |
227 info->content_type = ixmlCloneDOMString (""); | |
228 | |
229 return 0; | |
230 } | |
231 | |
232 static UpnpWebFileHandle | |
233 get_file_memory (const char *fullpath, const char *description, | |
234 const size_t length) | |
235 { | |
236 struct web_file_t *file; | |
237 | |
238 // log_verbose ("get_file_memory() description[%s]\n", | |
239 // description); | |
240 file = malloc (sizeof (struct web_file_t)); | |
241 file->fullpath = strdup (fullpath); | |
242 file->pos = 0; | |
243 file->type = FILE_MEMORY; | |
244 file->detail.memory.contents = strdup (description); | |
245 if ( file->detail.memory.contents == NULL ) { | |
246 log_verbose ("get_file_memory() null\n"); | |
247 } | |
248 file->detail.memory.len = length; | |
249 // log_verbose ("get_file_memory() path[%s] contents[%s]\n", | |
250 // file->fullpath, file->detail.memory.contents); | |
251 | |
252 return ((UpnpWebFileHandle) file); | |
253 } | |
254 | |
255 /* | |
256 * 2. get_file_stream() $B$G$O!"(Bopen()$B;~$NA`:n(B($B%U%!%$%k>pJs$NIU2C(B)$B$NB>!"%U%!%$%k>pJs$H(B queue $B$NI3IU$1$r<B;\(B | |
257 * $B"((B get_file_memory() $B$+$i$N2~B$E@$KCe4c$7$F5-:\(B | |
258 * 2.1 tdata->streamer->mutex $B$r(B lock $B$7$F$+$i0J2<$N(Bloop$B=hM}$r9T$&(B(loop$B:GBg?t$O(B16$B$G7h$aBG$A(B) | |
259 * 2.1.1 tdata->streamer->stream_session[i] $B$,(B NULL $B$G$"$k$+3NG'(B | |
260 * 2.1.1.2 NULL$B$G$"$k>l9g(B | |
261 * 2.1.1.2.1 create_queue $B$r<B;\(B | |
262 * 2.1.1.3 NULL $B$G$O$J$$>l9g(B | |
263 * 2.1.1.2.1 $BF@$K$d$k$3$HL5$7(B | |
264 * 2.2 tdata->streamer->mutex $B$r(B unlock | |
265 */ | |
266 static UpnpWebFileHandle | |
267 get_file_stream (const char *fullpath, thread_data *tdata) | |
268 { | |
130 | 269 #define STREAM_QUEUE (8192) |
125 | 270 struct web_file_t *file; |
271 int i = 0; | |
272 file = malloc (sizeof (struct web_file_t)); | |
273 | |
274 // 2.1 tdata->streamer->mutex $B$r(B lock $B$7$F$+$i0J2<$N(Bloop$B=hM}$r9T$&(B(loop$B:GBg?t$O(B16$B$G7h$aBG$A(B) | |
275 pthread_mutex_lock(&tdata->streamer->mutex); | |
276 for( i=0; i < STREAM_MAX; i++ ) { | |
277 if ( tdata->streamer->stream_session[i] == NULL ) { | |
278 // 2.1.1 tdata->streamer->stream_session[i] $B$,(B NULL $B$G$"$k>l9g(B | |
279 // 2.1.1.1 $B?75,%9%H%j!<%`MQ$N%-%e!<$r:n@.(B | |
280 file->detail.stream.id = tdata->streamer->stream_nr; | |
281 tdata->streamer->stream_session[i] = malloc(sizeof(session)); | |
282 if ( tdata->streamer->stream_session[i] == NULL ) { | |
283 return NULL; | |
284 } | |
285 tdata->streamer->stream_session[i]->is_valid = true; | |
286 tdata->streamer->stream_session[i]->p_queue = create_queue(STREAM_QUEUE); | |
287 if ( tdata->streamer->stream_session[i]->p_queue == NULL ) { | |
288 log_error ("get_file_stream(): tdata->streamer->stream_session[%d].p_queue alloc failed.\n", i); | |
289 return NULL; | |
290 } | |
291 pthread_mutex_init(&tdata->streamer->stream_session[i]->p_queue->mutex, NULL); | |
292 pthread_cond_init(&tdata->streamer->stream_session[i]->p_queue->cond_avail, NULL); | |
293 pthread_cond_init(&tdata->streamer->stream_session[i]->p_queue->cond_used, NULL); | |
294 tdata->streamer->stream_nr++; | |
295 break; | |
296 } else { | |
297 // 2.1.2 tdata->streamer->stream_session[i] $B$,(B NULL $B$G$O$J$$(B | |
298 if ( ! tdata->streamer->stream_session[i]->is_valid ) { | |
299 // 2.1.2.1 tdata->streamer->stream_session[i] $B$,L$;HMQ>uBV$G$"$k>l9g(B | |
300 file->detail.stream.id = i; | |
301 //tdata->streamer->stream_nr++; | |
302 tdata->streamer->stream_session[i]->is_valid = true; | |
303 tdata->streamer->stream_session[i]->p_queue = create_queue(STREAM_QUEUE); | |
304 if ( tdata->streamer->stream_session[i]->p_queue != NULL ) { | |
305 pthread_mutex_init(&tdata->streamer->stream_session[i]->p_queue->mutex, NULL); | |
306 pthread_cond_init(&tdata->streamer->stream_session[i]->p_queue->cond_avail, NULL); | |
307 pthread_cond_init(&tdata->streamer->stream_session[i]->p_queue->cond_used, NULL); | |
308 } else { | |
309 log_error ("get_file_stream(): tdata->streamer->stream_session[%d].p_queue alloc failed.\n", i); | |
310 return NULL; | |
311 } | |
312 break; | |
313 } | |
314 } | |
315 } | |
316 pthread_mutex_unlock(&tdata->streamer->mutex); | |
317 if ( i == STREAM_MAX ) { | |
318 log_verbose ("get_file_stream(): cannot get new file_stream.\n"); | |
319 } | |
320 | |
321 file->detail.stream.p_queue = tdata->streamer->stream_session[i]->p_queue; | |
322 file->fullpath = strdup (fullpath); | |
323 file->pos = 0; | |
324 file->type = FILE_STREAM; | |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
325 file->detail.stream.len = get_streaming_length(); //$B%U%!%$%k%5%$%:(B($B;DO?2h;~4V(Bx$B%S%C%H%l!<%H(B) |
125 | 326 file->detail.stream.qbuf = stream_dequeue(file->detail.stream.p_queue); |
327 if ( file->detail.stream.qbuf == NULL ) { | |
328 log_error ("get_file_stream(): stream_dequeue error.\n"); | |
329 return NULL; | |
330 } | |
331 log_verbose ("get_file_stream(): finish.\n"); | |
332 | |
333 return ((UpnpWebFileHandle) file); | |
334 } | |
335 | |
336 static UpnpWebFileHandle | |
337 http_open (const char *filename, enum UpnpOpenFileMode mode) | |
338 { | |
339 extern struct ushare_t *ut; | |
340 struct upnp_entry_t *entry = NULL; | |
341 struct web_file_t *file; | |
146
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
342 int i, fd, upnp_id = 0; |
125 | 343 extern thread_data *gp_tdata; |
344 thread_data *tdata = gp_tdata; | |
146
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
345 int channel_length = 0; |
125 | 346 |
347 if (!filename) | |
348 return NULL; | |
349 | |
350 if (mode != UPNP_READ) | |
351 return NULL; | |
352 | |
353 if (!strcmp (filename, CDS_LOCATION)) | |
354 return get_file_memory (CDS_LOCATION, CDS_DESCRIPTION, CDS_DESCRIPTION_LEN); | |
355 | |
356 if (!strcmp (filename, CMS_LOCATION)) | |
357 return get_file_memory (CMS_LOCATION, CMS_DESCRIPTION, CMS_DESCRIPTION_LEN); | |
358 | |
359 if (!strcmp (filename, MSR_LOCATION)) | |
360 return get_file_memory (MSR_LOCATION, MSR_DESCRIPTION, MSR_DESCRIPTION_LEN); | |
361 | |
362 if (ut->use_presentation && ( !strcmp (filename, USHARE_PRESENTATION_PAGE) | |
363 || !strncmp (filename, USHARE_CGI, strlen (USHARE_CGI)))) | |
364 return get_file_memory (USHARE_PRESENTATION_PAGE, ut->presentation->buf, | |
365 ut->presentation->len); | |
366 | |
367 upnp_id = atoi (strrchr (filename, '/') + 1); | |
368 entry = upnp_get_entry (ut, upnp_id); | |
369 if (!entry) | |
370 return NULL; | |
371 | |
372 if (!entry->fullpath) | |
373 return NULL; | |
374 | |
375 /* | |
376 * 1. http_open() $B$G$O(B entry $B$,%9%H%j!<%`:F@8MQ$N$b$N$G$"$k>l9g$K!"(B | |
377 * get_file_stream()$B$r8F$S=P$7%O%s%I%i$rJV5Q$9$k(B | |
378 */ | |
379 log_verbose ("Fullpath : %s\n", entry->fullpath); | |
146
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
380 if (ut->nr_channel == 0) { |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
381 if (!strcmp (entry->fullpath, STREAM_LOCATION)) |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
382 return get_file_stream (STREAM_LOCATION, tdata); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
383 } else { |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
384 for (i=0; i < ut->nr_channel; i++) |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
385 if (!strcmp(entry->fullpath, ut->location_name[i])) { |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
386 channel_length = strspn(ut->channel_name[i], "0123456789"); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
387 strncpy(ut->request_channel, ut->channel_name[i], channel_length); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
388 log_verbose ("http_open: request_channel[%s].\n", ut->request_channel); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
389 return get_file_stream (ut->location_name[i], tdata); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
390 } |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
391 } |
125 | 392 |
393 fd = open (entry->fullpath, O_RDONLY | O_NONBLOCK | O_SYNC | O_NDELAY); | |
394 if (fd < 0) | |
395 return NULL; | |
396 | |
397 file = malloc (sizeof (struct web_file_t)); | |
398 file->fullpath = strdup (entry->fullpath); | |
399 file->pos = 0; | |
400 file->type = FILE_LOCAL; | |
401 file->detail.local.entry = entry; | |
402 file->detail.local.fd = fd; | |
403 | |
404 return ((UpnpWebFileHandle) file); | |
405 } | |
406 | |
407 static int | |
408 http_read (UpnpWebFileHandle fh, char *buf, size_t buflen) | |
409 { | |
410 struct web_file_t *file = (struct web_file_t *) fh; | |
411 ssize_t len = -1; | |
412 | |
413 //log_verbose ("http_read file:[%s]\n", file->fullpath); | |
414 | |
415 if (!file) | |
416 return -1; | |
417 | |
418 switch (file->type) | |
419 { | |
420 case FILE_LOCAL: | |
421 log_verbose ("Read local file.\n"); | |
422 len = read (file->detail.local.fd, buf, buflen); | |
423 break; | |
424 case FILE_MEMORY: | |
425 log_verbose ("Read file from memory.\n"); | |
426 len = (size_t) MIN (buflen, file->detail.memory.len - file->pos); | |
427 memcpy (buf, file->detail.memory.contents + file->pos, (size_t) len); | |
428 break; | |
429 case FILE_STREAM: | |
430 //log_verbose ("Read file from stream.\n"); | |
431 if ( file->detail.stream.qbuf->size <= file->pos ) { | |
432 free(file->detail.stream.qbuf->data); | |
433 file->detail.stream.qbuf->data = NULL; | |
434 free(file->detail.stream.qbuf); | |
435 file->detail.stream.qbuf = NULL; | |
436 file->detail.stream.qbuf = stream_dequeue(file->detail.stream.p_queue); | |
437 file->pos = 0; | |
438 } | |
439 if ( file->detail.stream.qbuf == NULL ) { | |
440 log_verbose ("http_read stream_dequeue error NULL\n"); | |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
441 return 0; |
125 | 442 } |
443 len = (size_t) MIN (buflen, file->detail.stream.qbuf->size - file->pos); | |
444 memcpy (buf, file->detail.stream.qbuf->data + file->pos, (size_t) len); | |
445 break; | |
446 default: | |
447 log_verbose ("Unknown file type.\n"); | |
448 break; | |
449 } | |
450 | |
451 if (len >= 0) | |
452 file->pos += len; | |
453 | |
454 //log_verbose ("Read %zd bytes.\n", len); | |
455 | |
456 return len; | |
457 } | |
458 | |
459 static int | |
460 http_write (UpnpWebFileHandle fh __attribute__((unused)), | |
461 char *buf __attribute__((unused)), | |
462 size_t buflen __attribute__((unused))) | |
463 { | |
464 log_verbose ("http write\n"); | |
465 | |
466 return 0; | |
467 } | |
468 | |
469 static int | |
470 http_seek (UpnpWebFileHandle fh, off_t offset, int origin) | |
471 { | |
472 struct web_file_t *file = (struct web_file_t *) fh; | |
473 off_t newpos = -1; | |
474 | |
475 log_verbose ("http_seek\n"); | |
476 | |
477 if (!file) | |
478 return -1; | |
479 | |
480 switch (origin) | |
481 { | |
482 case SEEK_SET: | |
483 log_verbose ("Attempting to seek to %lld (was at %lld) in %s\n", | |
484 offset, file->pos, file->fullpath); | |
485 newpos = offset; | |
486 break; | |
487 case SEEK_CUR: | |
488 log_verbose ("Attempting to seek by %lld from %lld in %s\n", | |
489 offset, file->pos, file->fullpath); | |
490 newpos = file->pos + offset; | |
491 break; | |
492 case SEEK_END: | |
493 log_verbose ("Attempting to seek by %lld from end (was at %lld) in %s\n", | |
494 offset, file->pos, file->fullpath); | |
495 | |
496 if (file->type == FILE_LOCAL) | |
497 { | |
498 struct stat sb; | |
499 if (stat (file->fullpath, &sb) < 0) | |
500 { | |
501 log_verbose ("%s: cannot stat: %s\n", | |
502 file->fullpath, strerror (errno)); | |
503 return -1; | |
504 } | |
505 newpos = sb.st_size + offset; | |
506 } | |
507 else if (file->type == FILE_MEMORY) | |
508 newpos = file->detail.memory.len + offset; | |
509 break; | |
510 } | |
511 | |
512 switch (file->type) | |
513 { | |
514 case FILE_LOCAL: | |
515 /* Just make sure we cannot seek before start of file. */ | |
516 if (newpos < 0) | |
517 { | |
518 log_verbose ("%s: cannot seek: %s\n", file->fullpath, strerror (EINVAL)); | |
519 return -1; | |
520 } | |
521 | |
522 /* Don't seek with origin as specified above, as file may have | |
523 changed in size since our last stat. */ | |
524 if (lseek (file->detail.local.fd, newpos, SEEK_SET) == -1) | |
525 { | |
526 log_verbose ("%s: cannot seek: %s\n", file->fullpath, strerror (errno)); | |
527 return -1; | |
528 } | |
529 break; | |
530 case FILE_MEMORY: | |
531 if (newpos < 0 || newpos > file->detail.memory.len) | |
532 { | |
533 log_verbose ("%s: cannot seek: %s\n", file->fullpath, strerror (EINVAL)); | |
534 return -1; | |
535 } | |
536 break; | |
537 case FILE_STREAM: | |
538 log_verbose ("%s: cannot seek: %s\n", file->fullpath, "STREAM"); | |
539 newpos = file->pos; | |
540 break; | |
541 } | |
542 | |
543 file->pos = newpos; | |
544 | |
545 return 0; | |
546 } | |
547 | |
548 static int | |
549 http_close (UpnpWebFileHandle fh) | |
550 { | |
146
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
551 extern struct ushare_t *ut; |
125 | 552 struct web_file_t *file = (struct web_file_t *) fh; |
553 extern thread_data *gp_tdata; | |
554 thread_data *tdata = gp_tdata; | |
555 STREAM_QUEUE_T *p_queue; | |
556 int id = 0; | |
557 | |
558 if (!file) | |
559 return -1; | |
560 | |
561 switch (file->type) | |
562 { | |
563 case FILE_LOCAL: | |
564 close (file->detail.local.fd); | |
565 break; | |
566 case FILE_MEMORY: | |
567 /* no close operation */ | |
568 if (file->detail.memory.contents) | |
569 free (file->detail.memory.contents); | |
570 break; | |
571 case FILE_STREAM: | |
572 p_queue = file->detail.stream.p_queue; | |
573 if ( p_queue != NULL) { | |
574 id = file->detail.stream.id; | |
575 pthread_mutex_lock(&tdata->streamer->mutex); | |
576 tdata->streamer->stream_session[id]->is_valid = false; | |
577 pthread_mutex_unlock(&tdata->streamer->mutex); | |
578 pthread_mutex_lock(&p_queue->mutex); | |
579 while ( 0 < p_queue->num_used ) { | |
580 free(p_queue->buffer[p_queue->out]->data); | |
581 p_queue->buffer[p_queue->out]->data = NULL; | |
582 free(p_queue->buffer[p_queue->out]); | |
583 p_queue->buffer[p_queue->out] = NULL; | |
584 | |
585 p_queue->out++; | |
586 p_queue->out %= p_queue->size; | |
587 p_queue->num_avail++; | |
588 p_queue->num_used--; | |
589 } | |
590 pthread_mutex_unlock(&p_queue->mutex); | |
591 destroy_stream_queue(p_queue); | |
592 tdata->streamer->stream_session[id]->p_queue = NULL; | |
146
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
593 strcpy(ut->request_channel, "all"); |
066f33b2213a
EXPERIMENTAL: Select a particular program from multi-channel.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
136
diff
changeset
|
594 ut->nr_channel = 0; |
125 | 595 } |
596 break; | |
597 default: | |
598 log_verbose ("Unknown file type.\n"); | |
599 break; | |
600 } | |
601 | |
602 if (file->fullpath) | |
603 free (file->fullpath); | |
604 free (file); | |
605 | |
606 return 0; | |
607 } | |
608 | |
609 struct UpnpVirtualDirCallbacks virtual_dir_callbacks = | |
610 { | |
611 http_get_info, | |
612 http_open, | |
613 http_read, | |
614 http_write, | |
615 http_seek, | |
616 http_close | |
617 }; | |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
618 |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
619 // TS_BITRATE$B$O$d$C$D$1;E;v2a$.$k5$$,$7$^$9(B |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
620 #define TS_BITRATE (10*1000*1000/8) |
132
7b663556757f
modify CONTENT-LENGTH for indefinite recording.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
130
diff
changeset
|
621 #define MAX_STREAMING ((off_t)100*1000*1000*1000) |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
622 static off_t |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
623 get_streaming_length (void) |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
624 { |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
625 off_t length = 0; |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
626 extern thread_data *gp_tdata; |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
627 thread_data *tdata = gp_tdata; |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
628 time_t cur_time; |
127
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
629 struct timespec cur; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
630 struct timespec diff; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
631 clock_gettime(CLOCK_REALTIME, &cur); |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
632 off_t bitrate = 0; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
633 |
132
7b663556757f
modify CONTENT-LENGTH for indefinite recording.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
130
diff
changeset
|
634 if ( tdata->indefinite ) { |
7b663556757f
modify CONTENT-LENGTH for indefinite recording.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
130
diff
changeset
|
635 return MAX_STREAMING; |
127
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
636 } |
132
7b663556757f
modify CONTENT-LENGTH for indefinite recording.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
130
diff
changeset
|
637 diff.tv_nsec = cur.tv_nsec - tdata->streamer->start.tv_nsec; |
7b663556757f
modify CONTENT-LENGTH for indefinite recording.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
130
diff
changeset
|
638 diff.tv_sec = cur.tv_sec - tdata->streamer->start.tv_sec; |
127
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
639 |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
640 if ( diff.tv_sec < 1 ) { |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
641 bitrate = TS_BITRATE; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
642 } else { |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
643 bitrate = (((off_t)tdata->streamer->total_byte)*1e9) / |
132
7b663556757f
modify CONTENT-LENGTH for indefinite recording.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
130
diff
changeset
|
644 ( (((off_t)diff.tv_sec)*1e9) + diff.tv_nsec); |
127
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
645 } |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
646 |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
647 time(&cur_time); |
127
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
648 length = (tdata->start_time +tdata->recsec -cur_time) * bitrate; |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
649 if ( length < 0 ) { |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
650 length = 0; |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
651 } |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
652 length = length - (length % LENGTH_PACKET); |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
653 return length; |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
654 } |