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