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