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