Mercurial > pt1.oyama
annotate src/http.c @ 130:cc3e3f370aec
tune for streaming.
author | Naoya OYAMA <naoya.oyama@gmail.com> |
---|---|
date | Sun, 10 Oct 2010 22:44:28 +0900 |
parents | 5a380559a61e |
children | 7b663556757f |
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 | |
33 #include <upnp/upnp.h> | |
34 #include <upnp/upnptools.h> | |
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; | |
100 struct stat st; | |
101 int upnp_id = 0; | |
102 char *content_type = NULL; | |
103 char *protocol = NULL; | |
104 extern thread_data *gp_tdata; | |
105 thread_data *tdata = gp_tdata; | |
106 | |
107 if (!filename || !info) | |
108 return -1; | |
109 | |
110 log_verbose ("http_get_info, filename : %s\n", filename); | |
111 | |
112 upnp_id = atoi (strrchr (filename, '/') + 1); | |
113 entry = upnp_get_entry (ut, upnp_id); | |
114 | |
115 #if 0 | |
116 if ( (!strcmp (filename, STREAM_LOCATION)) || | |
117 ( (entry->fullpath != NULL) && | |
118 ( !strcmp(entry->fullpath, STREAM_LOCATION))) ) { | |
119 #endif | |
120 if (!strcmp (filename, STREAM_LOCATION)) { | |
121 log_verbose ("http_get_info, stream location found.\n"); | |
122 info->is_readable = 1; | |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
123 info->file_length = get_streaming_length(); |
125 | 124 info->last_modified = time(NULL); |
125 info->is_directory = 0; | |
126 info->content_type = ixmlCloneDOMString ("video/mpeg"); | |
127 return 0; | |
128 } | |
129 if (!strcmp (filename, CDS_LOCATION)) | |
130 { | |
131 set_info_file (info, CDS_DESCRIPTION_LEN, SERVICE_CONTENT_TYPE); | |
132 return 0; | |
133 } | |
134 | |
135 if (!strcmp (filename, CMS_LOCATION)) | |
136 { | |
137 set_info_file (info, CMS_DESCRIPTION_LEN, SERVICE_CONTENT_TYPE); | |
138 return 0; | |
139 } | |
140 | |
141 if (!strcmp (filename, MSR_LOCATION)) | |
142 { | |
143 set_info_file (info, MSR_DESCRIPTION_LEN, SERVICE_CONTENT_TYPE); | |
144 return 0; | |
145 } | |
146 | |
147 if (ut->use_presentation && !strcmp (filename, USHARE_PRESENTATION_PAGE)) | |
148 { | |
149 if (build_presentation_page (ut) < 0) | |
150 return -1; | |
151 | |
152 set_info_file (info, ut->presentation->len, PRESENTATION_PAGE_CONTENT_TYPE); | |
153 return 0; | |
154 } | |
155 | |
156 if (ut->use_presentation && !strncmp (filename, USHARE_CGI, strlen (USHARE_CGI))) | |
157 { | |
158 if (process_cgi (ut, (char *) (filename + strlen (USHARE_CGI) + 1)) < 0) | |
159 return -1; | |
160 | |
161 set_info_file (info, ut->presentation->len, PRESENTATION_PAGE_CONTENT_TYPE); | |
162 return 0; | |
163 } | |
164 | |
165 if (!entry) | |
166 return -1; | |
167 log_verbose ("http_get_info, entry found.\n"); | |
168 | |
169 if (!entry->fullpath) | |
170 return -1; | |
171 | |
172 #if 0 | |
173 if (stat (entry->fullpath, &st) < 0) | |
174 return -1; | |
175 | |
176 if (access (entry->fullpath, R_OK) < 0) | |
177 { | |
178 if (errno != EACCES) { | |
179 log_verbose ("http_get_info, access() error.\n"); | |
180 return -1; | |
181 } | |
182 info->is_readable = 0; | |
183 } | |
184 else | |
185 info->is_readable = 1; | |
186 | |
187 /* file exist and can be read */ | |
188 info->file_length = st.st_size; | |
189 info->last_modified = st.st_mtime; | |
190 info->is_directory = S_ISDIR (st.st_mode); | |
191 #endif | |
192 | |
193 info->is_readable = 1; | |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
194 info->file_length = get_streaming_length(); |
125 | 195 info->last_modified = time(NULL); |
196 info->is_directory = 0; | |
197 | |
198 protocol = | |
199 #ifdef HAVE_DLNA | |
200 entry->dlna_profile ? | |
201 dlna_write_protocol_info (DLNA_PROTOCOL_INFO_TYPE_HTTP, | |
202 DLNA_ORG_PLAY_SPEED_NORMAL, | |
203 DLNA_ORG_CONVERSION_NONE, | |
204 DLNA_ORG_OPERATION_RANGE, | |
205 ut->dlna_flags, entry->dlna_profile) : | |
206 #endif /* HAVE_DLNA */ | |
207 mime_get_protocol (entry->mime_type); | |
208 | |
209 content_type = | |
210 strndup ((protocol + PROTOCOL_TYPE_PRE_SZ), | |
211 strlen (protocol + PROTOCOL_TYPE_PRE_SZ) | |
212 - PROTOCOL_TYPE_SUFF_SZ); | |
213 free (protocol); | |
214 | |
215 if (content_type) | |
216 { | |
217 info->content_type = ixmlCloneDOMString (content_type); | |
218 free (content_type); | |
219 } | |
220 else | |
221 info->content_type = ixmlCloneDOMString (""); | |
222 | |
223 return 0; | |
224 } | |
225 | |
226 static UpnpWebFileHandle | |
227 get_file_memory (const char *fullpath, const char *description, | |
228 const size_t length) | |
229 { | |
230 struct web_file_t *file; | |
231 | |
232 // log_verbose ("get_file_memory() description[%s]\n", | |
233 // description); | |
234 file = malloc (sizeof (struct web_file_t)); | |
235 file->fullpath = strdup (fullpath); | |
236 file->pos = 0; | |
237 file->type = FILE_MEMORY; | |
238 file->detail.memory.contents = strdup (description); | |
239 if ( file->detail.memory.contents == NULL ) { | |
240 log_verbose ("get_file_memory() null\n"); | |
241 } | |
242 file->detail.memory.len = length; | |
243 // log_verbose ("get_file_memory() path[%s] contents[%s]\n", | |
244 // file->fullpath, file->detail.memory.contents); | |
245 | |
246 return ((UpnpWebFileHandle) file); | |
247 } | |
248 | |
249 /* | |
250 * 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 | |
251 * $B"((B get_file_memory() $B$+$i$N2~B$E@$KCe4c$7$F5-:\(B | |
252 * 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) | |
253 * 2.1.1 tdata->streamer->stream_session[i] $B$,(B NULL $B$G$"$k$+3NG'(B | |
254 * 2.1.1.2 NULL$B$G$"$k>l9g(B | |
255 * 2.1.1.2.1 create_queue $B$r<B;\(B | |
256 * 2.1.1.3 NULL $B$G$O$J$$>l9g(B | |
257 * 2.1.1.2.1 $BF@$K$d$k$3$HL5$7(B | |
258 * 2.2 tdata->streamer->mutex $B$r(B unlock | |
259 */ | |
260 static UpnpWebFileHandle | |
261 get_file_stream (const char *fullpath, thread_data *tdata) | |
262 { | |
130 | 263 #define STREAM_QUEUE (8192) |
125 | 264 struct web_file_t *file; |
265 int i = 0; | |
266 file = malloc (sizeof (struct web_file_t)); | |
267 | |
268 // 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) | |
269 pthread_mutex_lock(&tdata->streamer->mutex); | |
270 for( i=0; i < STREAM_MAX; i++ ) { | |
271 if ( tdata->streamer->stream_session[i] == NULL ) { | |
272 // 2.1.1 tdata->streamer->stream_session[i] $B$,(B NULL $B$G$"$k>l9g(B | |
273 // 2.1.1.1 $B?75,%9%H%j!<%`MQ$N%-%e!<$r:n@.(B | |
274 file->detail.stream.id = tdata->streamer->stream_nr; | |
275 tdata->streamer->stream_session[i] = malloc(sizeof(session)); | |
276 if ( tdata->streamer->stream_session[i] == NULL ) { | |
277 return NULL; | |
278 } | |
279 tdata->streamer->stream_session[i]->is_valid = true; | |
280 tdata->streamer->stream_session[i]->p_queue = create_queue(STREAM_QUEUE); | |
281 if ( tdata->streamer->stream_session[i]->p_queue == NULL ) { | |
282 log_error ("get_file_stream(): tdata->streamer->stream_session[%d].p_queue alloc failed.\n", i); | |
283 return NULL; | |
284 } | |
285 pthread_mutex_init(&tdata->streamer->stream_session[i]->p_queue->mutex, NULL); | |
286 pthread_cond_init(&tdata->streamer->stream_session[i]->p_queue->cond_avail, NULL); | |
287 pthread_cond_init(&tdata->streamer->stream_session[i]->p_queue->cond_used, NULL); | |
288 tdata->streamer->stream_nr++; | |
289 break; | |
290 } else { | |
291 // 2.1.2 tdata->streamer->stream_session[i] $B$,(B NULL $B$G$O$J$$(B | |
292 if ( ! tdata->streamer->stream_session[i]->is_valid ) { | |
293 // 2.1.2.1 tdata->streamer->stream_session[i] $B$,L$;HMQ>uBV$G$"$k>l9g(B | |
294 file->detail.stream.id = i; | |
295 //tdata->streamer->stream_nr++; | |
296 tdata->streamer->stream_session[i]->is_valid = true; | |
297 tdata->streamer->stream_session[i]->p_queue = create_queue(STREAM_QUEUE); | |
298 if ( tdata->streamer->stream_session[i]->p_queue != NULL ) { | |
299 pthread_mutex_init(&tdata->streamer->stream_session[i]->p_queue->mutex, NULL); | |
300 pthread_cond_init(&tdata->streamer->stream_session[i]->p_queue->cond_avail, NULL); | |
301 pthread_cond_init(&tdata->streamer->stream_session[i]->p_queue->cond_used, NULL); | |
302 } else { | |
303 log_error ("get_file_stream(): tdata->streamer->stream_session[%d].p_queue alloc failed.\n", i); | |
304 return NULL; | |
305 } | |
306 break; | |
307 } | |
308 } | |
309 } | |
310 pthread_mutex_unlock(&tdata->streamer->mutex); | |
311 if ( i == STREAM_MAX ) { | |
312 log_verbose ("get_file_stream(): cannot get new file_stream.\n"); | |
313 } | |
314 | |
315 file->detail.stream.p_queue = tdata->streamer->stream_session[i]->p_queue; | |
316 file->fullpath = strdup (fullpath); | |
317 file->pos = 0; | |
318 file->type = FILE_STREAM; | |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
319 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 | 320 file->detail.stream.qbuf = stream_dequeue(file->detail.stream.p_queue); |
321 if ( file->detail.stream.qbuf == NULL ) { | |
322 log_error ("get_file_stream(): stream_dequeue error.\n"); | |
323 return NULL; | |
324 } | |
325 log_verbose ("get_file_stream(): finish.\n"); | |
326 | |
327 return ((UpnpWebFileHandle) file); | |
328 } | |
329 | |
330 static UpnpWebFileHandle | |
331 http_open (const char *filename, enum UpnpOpenFileMode mode) | |
332 { | |
333 extern struct ushare_t *ut; | |
334 struct upnp_entry_t *entry = NULL; | |
335 struct web_file_t *file; | |
336 int fd, upnp_id = 0; | |
337 extern thread_data *gp_tdata; | |
338 thread_data *tdata = gp_tdata; | |
339 | |
340 if (!filename) | |
341 return NULL; | |
342 | |
343 if (mode != UPNP_READ) | |
344 return NULL; | |
345 | |
346 if (!strcmp (filename, CDS_LOCATION)) | |
347 return get_file_memory (CDS_LOCATION, CDS_DESCRIPTION, CDS_DESCRIPTION_LEN); | |
348 | |
349 if (!strcmp (filename, CMS_LOCATION)) | |
350 return get_file_memory (CMS_LOCATION, CMS_DESCRIPTION, CMS_DESCRIPTION_LEN); | |
351 | |
352 if (!strcmp (filename, MSR_LOCATION)) | |
353 return get_file_memory (MSR_LOCATION, MSR_DESCRIPTION, MSR_DESCRIPTION_LEN); | |
354 | |
355 if (ut->use_presentation && ( !strcmp (filename, USHARE_PRESENTATION_PAGE) | |
356 || !strncmp (filename, USHARE_CGI, strlen (USHARE_CGI)))) | |
357 return get_file_memory (USHARE_PRESENTATION_PAGE, ut->presentation->buf, | |
358 ut->presentation->len); | |
359 | |
360 upnp_id = atoi (strrchr (filename, '/') + 1); | |
361 entry = upnp_get_entry (ut, upnp_id); | |
362 if (!entry) | |
363 return NULL; | |
364 | |
365 if (!entry->fullpath) | |
366 return NULL; | |
367 | |
368 /* | |
369 * 1. http_open() $B$G$O(B entry $B$,%9%H%j!<%`:F@8MQ$N$b$N$G$"$k>l9g$K!"(B | |
370 * get_file_stream()$B$r8F$S=P$7%O%s%I%i$rJV5Q$9$k(B | |
371 */ | |
372 log_verbose ("Fullpath : %s\n", entry->fullpath); | |
373 if (!strcmp (entry->fullpath, STREAM_LOCATION)) | |
374 return get_file_stream (STREAM_LOCATION, tdata); | |
375 | |
376 fd = open (entry->fullpath, O_RDONLY | O_NONBLOCK | O_SYNC | O_NDELAY); | |
377 if (fd < 0) | |
378 return NULL; | |
379 | |
380 file = malloc (sizeof (struct web_file_t)); | |
381 file->fullpath = strdup (entry->fullpath); | |
382 file->pos = 0; | |
383 file->type = FILE_LOCAL; | |
384 file->detail.local.entry = entry; | |
385 file->detail.local.fd = fd; | |
386 | |
387 return ((UpnpWebFileHandle) file); | |
388 } | |
389 | |
390 static int | |
391 http_read (UpnpWebFileHandle fh, char *buf, size_t buflen) | |
392 { | |
393 struct web_file_t *file = (struct web_file_t *) fh; | |
394 ssize_t len = -1; | |
395 extern thread_data *gp_tdata; | |
396 thread_data *tdata = gp_tdata; | |
397 | |
398 //log_verbose ("http_read file:[%s]\n", file->fullpath); | |
399 | |
400 if (!file) | |
401 return -1; | |
402 | |
403 switch (file->type) | |
404 { | |
405 case FILE_LOCAL: | |
406 log_verbose ("Read local file.\n"); | |
407 len = read (file->detail.local.fd, buf, buflen); | |
408 break; | |
409 case FILE_MEMORY: | |
410 log_verbose ("Read file from memory.\n"); | |
411 len = (size_t) MIN (buflen, file->detail.memory.len - file->pos); | |
412 memcpy (buf, file->detail.memory.contents + file->pos, (size_t) len); | |
413 break; | |
414 case FILE_STREAM: | |
415 //log_verbose ("Read file from stream.\n"); | |
416 if ( file->detail.stream.qbuf->size <= file->pos ) { | |
417 free(file->detail.stream.qbuf->data); | |
418 file->detail.stream.qbuf->data = NULL; | |
419 free(file->detail.stream.qbuf); | |
420 file->detail.stream.qbuf = NULL; | |
421 file->detail.stream.qbuf = stream_dequeue(file->detail.stream.p_queue); | |
422 file->pos = 0; | |
423 } | |
424 if ( file->detail.stream.qbuf == NULL ) { | |
425 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
|
426 return 0; |
125 | 427 } |
428 len = (size_t) MIN (buflen, file->detail.stream.qbuf->size - file->pos); | |
429 memcpy (buf, file->detail.stream.qbuf->data + file->pos, (size_t) len); | |
430 break; | |
431 default: | |
432 log_verbose ("Unknown file type.\n"); | |
433 break; | |
434 } | |
435 | |
436 if (len >= 0) | |
437 file->pos += len; | |
438 | |
439 //log_verbose ("Read %zd bytes.\n", len); | |
440 | |
441 return len; | |
442 } | |
443 | |
444 static int | |
445 http_write (UpnpWebFileHandle fh __attribute__((unused)), | |
446 char *buf __attribute__((unused)), | |
447 size_t buflen __attribute__((unused))) | |
448 { | |
449 log_verbose ("http write\n"); | |
450 | |
451 return 0; | |
452 } | |
453 | |
454 static int | |
455 http_seek (UpnpWebFileHandle fh, off_t offset, int origin) | |
456 { | |
457 struct web_file_t *file = (struct web_file_t *) fh; | |
458 off_t newpos = -1; | |
459 | |
460 log_verbose ("http_seek\n"); | |
461 | |
462 if (!file) | |
463 return -1; | |
464 | |
465 switch (origin) | |
466 { | |
467 case SEEK_SET: | |
468 log_verbose ("Attempting to seek to %lld (was at %lld) in %s\n", | |
469 offset, file->pos, file->fullpath); | |
470 newpos = offset; | |
471 break; | |
472 case SEEK_CUR: | |
473 log_verbose ("Attempting to seek by %lld from %lld in %s\n", | |
474 offset, file->pos, file->fullpath); | |
475 newpos = file->pos + offset; | |
476 break; | |
477 case SEEK_END: | |
478 log_verbose ("Attempting to seek by %lld from end (was at %lld) in %s\n", | |
479 offset, file->pos, file->fullpath); | |
480 | |
481 if (file->type == FILE_LOCAL) | |
482 { | |
483 struct stat sb; | |
484 if (stat (file->fullpath, &sb) < 0) | |
485 { | |
486 log_verbose ("%s: cannot stat: %s\n", | |
487 file->fullpath, strerror (errno)); | |
488 return -1; | |
489 } | |
490 newpos = sb.st_size + offset; | |
491 } | |
492 else if (file->type == FILE_MEMORY) | |
493 newpos = file->detail.memory.len + offset; | |
494 break; | |
495 } | |
496 | |
497 switch (file->type) | |
498 { | |
499 case FILE_LOCAL: | |
500 /* Just make sure we cannot seek before start of file. */ | |
501 if (newpos < 0) | |
502 { | |
503 log_verbose ("%s: cannot seek: %s\n", file->fullpath, strerror (EINVAL)); | |
504 return -1; | |
505 } | |
506 | |
507 /* Don't seek with origin as specified above, as file may have | |
508 changed in size since our last stat. */ | |
509 if (lseek (file->detail.local.fd, newpos, SEEK_SET) == -1) | |
510 { | |
511 log_verbose ("%s: cannot seek: %s\n", file->fullpath, strerror (errno)); | |
512 return -1; | |
513 } | |
514 break; | |
515 case FILE_MEMORY: | |
516 if (newpos < 0 || newpos > file->detail.memory.len) | |
517 { | |
518 log_verbose ("%s: cannot seek: %s\n", file->fullpath, strerror (EINVAL)); | |
519 return -1; | |
520 } | |
521 break; | |
522 case FILE_STREAM: | |
523 log_verbose ("%s: cannot seek: %s\n", file->fullpath, "STREAM"); | |
524 newpos = file->pos; | |
525 break; | |
526 } | |
527 | |
528 file->pos = newpos; | |
529 | |
530 return 0; | |
531 } | |
532 | |
533 static int | |
534 http_close (UpnpWebFileHandle fh) | |
535 { | |
536 struct web_file_t *file = (struct web_file_t *) fh; | |
537 extern thread_data *gp_tdata; | |
538 thread_data *tdata = gp_tdata; | |
539 STREAM_QUEUE_T *p_queue; | |
540 int i; | |
541 int id = 0; | |
542 int j; | |
543 | |
544 if (!file) | |
545 return -1; | |
546 | |
547 switch (file->type) | |
548 { | |
549 case FILE_LOCAL: | |
550 close (file->detail.local.fd); | |
551 break; | |
552 case FILE_MEMORY: | |
553 /* no close operation */ | |
554 if (file->detail.memory.contents) | |
555 free (file->detail.memory.contents); | |
556 break; | |
557 case FILE_STREAM: | |
558 p_queue = file->detail.stream.p_queue; | |
559 if ( p_queue != NULL) { | |
560 id = file->detail.stream.id; | |
561 pthread_mutex_lock(&tdata->streamer->mutex); | |
562 tdata->streamer->stream_session[id]->is_valid = false; | |
563 pthread_mutex_unlock(&tdata->streamer->mutex); | |
564 pthread_mutex_lock(&p_queue->mutex); | |
565 while ( 0 < p_queue->num_used ) { | |
566 free(p_queue->buffer[p_queue->out]->data); | |
567 p_queue->buffer[p_queue->out]->data = NULL; | |
568 free(p_queue->buffer[p_queue->out]); | |
569 p_queue->buffer[p_queue->out] = NULL; | |
570 | |
571 p_queue->out++; | |
572 p_queue->out %= p_queue->size; | |
573 p_queue->num_avail++; | |
574 p_queue->num_used--; | |
575 } | |
576 pthread_mutex_unlock(&p_queue->mutex); | |
577 destroy_stream_queue(p_queue); | |
578 tdata->streamer->stream_session[id]->p_queue = NULL; | |
579 } | |
580 break; | |
581 default: | |
582 log_verbose ("Unknown file type.\n"); | |
583 break; | |
584 } | |
585 | |
586 if (file->fullpath) | |
587 free (file->fullpath); | |
588 free (file); | |
589 | |
590 return 0; | |
591 } | |
592 | |
593 struct UpnpVirtualDirCallbacks virtual_dir_callbacks = | |
594 { | |
595 http_get_info, | |
596 http_open, | |
597 http_read, | |
598 http_write, | |
599 http_seek, | |
600 http_close | |
601 }; | |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
602 |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
603 // 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
|
604 #define TS_BITRATE (10*1000*1000/8) |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
605 static off_t |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
606 get_streaming_length (void) |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
607 { |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
608 off_t length = 0; |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
609 extern thread_data *gp_tdata; |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
610 thread_data *tdata = gp_tdata; |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
611 time_t cur_time; |
127
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
612 struct timespec cur; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
613 struct timespec diff; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
614 clock_gettime(CLOCK_REALTIME, &cur); |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
615 off_t bitrate = 0; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
616 |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
617 if ( cur.tv_nsec < tdata->streamer->start.tv_nsec ) { |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
618 diff.tv_nsec = cur.tv_nsec - tdata->streamer->start.tv_nsec +1e9; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
619 diff.tv_sec = cur.tv_sec - tdata->streamer->start.tv_sec -1; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
620 } else { |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
621 diff.tv_nsec = cur.tv_nsec - tdata->streamer->start.tv_nsec; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
622 diff.tv_sec = cur.tv_sec - tdata->streamer->start.tv_sec; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
623 } |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
624 |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
625 if ( diff.tv_sec < 1 ) { |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
626 bitrate = TS_BITRATE; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
627 } else { |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
628 bitrate = (((off_t)tdata->streamer->total_byte)*1e9) / |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
629 ( (((off_t)diff.tv_sec)*1e9) + (diff.tv_sec/1e9)); |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
630 bitrate = bitrate; |
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
631 } |
126
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
632 |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
633 time(&cur_time); |
127
5a380559a61e
modify calclate bitrate mechanism.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
126
diff
changeset
|
634 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
|
635 if ( length < 0 ) { |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
636 length = 0; |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
637 } |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
638 length = length - (length % LENGTH_PACKET); |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
639 return length; |
5dcaf3785ebe
fix process terminate problem.
Naoya OYAMA <naoya.oyama@gmail.com>
parents:
125
diff
changeset
|
640 } |