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