[1.5] remove pthread dependency from jpip client utilities
[openjpeg.git] / applications / jpip / libopenjpip / openjpip.c
1 /*
2  * $Id$
3  *
4  * Copyright (c) 2002-2011, Communications and Remote Sensing Laboratory, Universite catholique de Louvain (UCL), Belgium
5  * Copyright (c) 2002-2011, Professor Benoit Macq
6  * Copyright (c) 2010-2011, Kaori Hagihara
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS `AS IS'
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30
31 #include <stdlib.h>
32 #include "openjpip.h"
33 #include "jpip_parser.h"
34 #include "channel_manager.h"
35 #include "byte_manager.h"
36
37 #ifdef SERVER
38 #include "auxtrans_manager.h"
39 #endif
40
41 #include <stdio.h>
42 #include "dec_clientmsg_handler.h"
43 #include "jpipstream_manager.h"
44
45 #include <string.h>
46 #include <sys/types.h>
47 #include <sys/stat.h>
48 #include <fcntl.h>
49 #include <unistd.h>
50 #include "jp2k_encoder.h"
51
52 #ifdef SERVER
53
54 server_record_t * init_JPIPserver( int tcp_auxport, int udp_auxport)
55 {
56   server_record_t *record = (server_record_t *)malloc( sizeof(server_record_t));
57   
58   record->sessionlist = gene_sessionlist();
59   record->targetlist  = gene_targetlist();
60   record->auxtrans = init_aux_transport( tcp_auxport, udp_auxport);
61    
62   return record;
63 }
64
65 void terminate_JPIPserver( server_record_t **rec)
66 {
67   delete_sessionlist( &(*rec)->sessionlist);
68   delete_targetlist( &(*rec)->targetlist); 
69   close_aux_transport( (*rec)->auxtrans);
70    
71   free( *rec);
72 }
73
74 QR_t * parse_querystring( char *query_string)
75 {
76   QR_t *qr;
77
78   qr = (QR_t *)malloc( sizeof(QR_t));
79     
80   qr->query = parse_query( query_string);
81   qr->msgqueue = NULL;
82   qr->channel = NULL;
83
84   return qr;
85 }
86
87 bool process_JPIPrequest( server_record_t *rec, QR_t *qr)
88 {
89   target_param_t *target = NULL;
90   session_param_t *cursession = NULL;
91   channel_param_t *curchannel = NULL;
92
93   if( qr->query->target || qr->query->tid){
94     if( !identify_target( *(qr->query), rec->targetlist, &target))
95       return false;
96   }
97
98   if( qr->query->cid){
99     if( !associate_channel( *(qr->query), rec->sessionlist, &cursession, &curchannel))
100       return false;
101     qr->channel = curchannel;
102   }
103   
104   if( qr->query->cnew != non){
105     if( !open_channel( *(qr->query), rec->sessionlist, rec->auxtrans, target, &cursession, &curchannel))
106       return false;
107     qr->channel = curchannel;
108   }
109   
110   if( qr->query->cclose)
111     if( !close_channel( *(qr->query), rec->sessionlist, &cursession, &curchannel))
112       return false;
113   
114   if( (qr->query->fx > 0 && qr->query->fy > 0) || qr->query->box_type[0][0] != 0)
115     if( !gene_JPIPstream( *(qr->query), target, cursession, curchannel, &qr->msgqueue))
116       return false;
117
118   return true;
119 }
120
121 void add_EORmsg( int fd, QR_t *qr);
122
123 void send_responsedata( server_record_t *rec, QR_t *qr)
124 {
125   int fd;
126   char tmpfname[] = "tmpjpipstream.jpp";
127   Byte_t *jpipstream;
128   Byte8_t len_of_jpipstream;
129
130   if( (fd = open( tmpfname, O_RDWR|O_CREAT|O_EXCL, S_IRWXU)) == -1){
131     fprintf( FCGI_stderr, "file open error %s", tmpfname);
132     fprintf( FCGI_stdout, "Status: 503\r\n");
133     fprintf( FCGI_stdout, "Reason: Implementation failed\r\n");
134     return;
135   }
136   
137   recons_stream_from_msgqueue( qr->msgqueue, fd);
138   
139   add_EORmsg( fd, qr); // needed at least for tcp and udp
140
141   len_of_jpipstream = get_filesize( fd);
142   jpipstream = fetch_bytes( fd, 0, len_of_jpipstream);
143
144   close( fd);
145   remove( tmpfname);
146
147   fprintf( FCGI_stdout, "\r\n");
148
149   if( qr->channel)
150     if( qr->channel->aux == tcp || qr->channel->aux == udp){
151       send_responsedata_on_aux( qr->channel->aux==tcp, rec->auxtrans, qr->channel->cid, jpipstream, len_of_jpipstream, 1000); // 1KB per frame
152       return;
153     }
154   
155   if( fwrite( jpipstream, len_of_jpipstream, 1, FCGI_stdout) != 1)
156     fprintf( FCGI_stderr, "Error: failed to write jpipstream\n");
157
158   free( jpipstream);
159   return;
160 }
161
162 void add_EORmsg( int fd, QR_t *qr)
163 {
164   unsigned char EOR[3];
165
166   if( qr->channel){
167     EOR[0] = 0x00;   
168     EOR[1] = is_allsent( *(qr->channel->cachemodel)) ? 0x01 : 0x02;
169     EOR[2] = 0x00;
170     if( write( fd, EOR, 3) != 3)
171       fprintf( FCGI_stderr, "Error: failed to write EOR message\n");
172   }
173 }
174
175 void end_QRprocess( server_record_t *rec, QR_t **qr)
176 {
177   // TODO: record client preferences if necessary
178   
179   delete_query( &((*qr)->query));
180   delete_msgqueue( &((*qr)->msgqueue));
181   free( *qr);
182 }
183
184
185 void local_log( bool query, bool messages, bool sessions, bool targets, QR_t *qr, server_record_t *rec)
186 {
187   if( query)
188     print_queryparam( *qr->query);
189
190   if( messages)
191     print_msgqueue( qr->msgqueue);
192
193   if( sessions)
194     print_allsession( rec->sessionlist);
195   
196   if( targets)
197     print_alltarget( rec->targetlist);
198 }
199
200 #endif //SERVER
201
202 #ifndef SERVER
203
204 dec_server_record_t * init_dec_server( int port)
205 {
206   dec_server_record_t *record = (dec_server_record_t *)malloc( sizeof(dec_server_record_t));
207
208   record->cachelist = gene_cachelist();
209   record->jpipstream = NULL;
210   record->jpipstreamlen = 0;
211   record->msgqueue = gene_msgqueue( true, NULL);
212   record->listening_socket = open_listeningsocket( port);
213
214   return record;
215 }
216
217 void terminate_dec_server( dec_server_record_t **rec)
218 {
219   delete_cachelist( &(*rec)->cachelist);  
220   free( (*rec)->jpipstream);
221   
222   if( (*rec)->msgqueue)
223     delete_msgqueue( &((*rec)->msgqueue));
224
225   if( close_socket( (*rec)->listening_socket) != 0)
226     perror("close");
227   
228   free( *rec);
229 }
230
231 client_t accept_connection( dec_server_record_t *rec)
232 {
233   client_t client;
234   
235   client = accept_socket( rec->listening_socket);
236   if( client == -1)
237     fprintf( stderr, "error: failed to connect to client\n");
238   
239   return client;
240 }
241
242 bool handle_clientreq( client_t client, dec_server_record_t *rec)
243 {
244   bool quit = false;
245   msgtype_t msgtype = identify_clientmsg( client);
246   
247   switch( msgtype){
248   case JPIPSTREAM:
249     handle_JPIPstreamMSG( client, rec->cachelist, &rec->jpipstream, &rec->jpipstreamlen, rec->msgqueue);
250     break;
251       
252   case PNMREQ:
253     handle_PNMreqMSG( client, rec->jpipstream, rec->msgqueue, rec->cachelist);
254     break;
255     
256   case XMLREQ:
257     handle_XMLreqMSG( client, rec->jpipstream, rec->cachelist);
258     break;
259
260   case TIDREQ:
261     handle_TIDreqMSG( client, rec->cachelist);
262     break;
263                                                 
264   case CIDREQ:
265     handle_CIDreqMSG( client, rec->cachelist);
266     break;
267
268   case CIDDST:
269     handle_dstCIDreqMSG( client, rec->cachelist);
270     break;
271     
272   case SIZREQ:
273     handle_SIZreqMSG( client, rec->jpipstream, rec->msgqueue, rec->cachelist);
274     break;
275
276   case JP2SAVE:
277     handle_JP2saveMSG( client, rec->cachelist, rec->msgqueue, rec->jpipstream);
278     break;
279
280   case QUIT:
281     quit = true;
282     save_codestream( rec->jpipstream, rec->jpipstreamlen, "jpt");
283     break;
284   case MSGERROR:
285     break;
286   }
287
288   fprintf( stderr, "\t end of the connection\n\n");
289   if( close_socket(client) != 0){
290     perror("close");
291     return false;
292   }
293
294   if( quit)
295     return false;
296
297   return true;
298 }
299
300
301 jpip_dec_param_t * init_jpipdecoder( bool jp2)
302 {
303   jpip_dec_param_t *dec;
304   
305   dec = (jpip_dec_param_t *)calloc( 1, sizeof(jpip_dec_param_t));
306
307   dec->msgqueue = gene_msgqueue( true, NULL);
308   
309   if( jp2)
310     dec->metadatalist = gene_metadatalist();
311
312   return dec;
313 }
314
315
316 bool fread_jpip( char fname[], jpip_dec_param_t *dec)
317 {
318   int infd;
319
320   if(( infd = open( fname, O_RDONLY)) == -1){
321     fprintf( stderr, "file %s not exist\n", fname);
322     return false;
323   }
324   
325   if(!(dec->jpiplen = get_filesize(infd)))
326     return false;
327   
328   dec->jpipstream = (Byte_t *)malloc( dec->jpiplen);
329
330   if( read( infd, dec->jpipstream, dec->jpiplen) != dec->jpiplen){
331     fprintf( stderr, "file reading error\n");
332     free( dec->jpipstream);
333     return false;
334   }
335
336   close(infd);
337
338   return true;
339 }
340
341 void decode_jpip( jpip_dec_param_t *dec)
342 {
343   parse_JPIPstream( dec->jpipstream, dec->jpiplen, 0, dec->msgqueue);
344
345   if( dec->metadatalist){ // JP2 encoding
346     parse_metamsg( dec->msgqueue, dec->jpipstream, dec->jpiplen, dec->metadatalist);
347     dec->ihdrbox = gene_ihdrbox( dec->metadatalist, dec->jpipstream);
348     
349     dec->jp2kstream = recons_jp2( dec->msgqueue, dec->jpipstream, dec->msgqueue->first->csn, &dec->jp2klen);
350   }
351   else // J2k encoding  
352     // Notice: arguments fw, fh need to be set for LRCP, PCRL, CPRL
353     dec->jp2kstream = recons_j2k( dec->msgqueue, dec->jpipstream, dec->msgqueue->first->csn, 0, 0, &dec->jp2klen);  
354 }
355
356 bool fwrite_jp2k( char fname[], jpip_dec_param_t *dec)
357 {
358   int outfd;
359   
360 #ifdef _WIN32
361   if(( outfd = open( fname, O_WRONLY|O_CREAT, _S_IREAD | _S_IWRITE)) == -1){
362 #else
363  if(( outfd = open( fname, O_WRONLY|O_CREAT, S_IRWXU|S_IRWXG)) == -1){
364 #endif
365    fprintf( stderr, "file %s open error\n", fname);
366    return false;
367  }
368   
369  if( write( outfd, dec->jp2kstream, dec->jp2klen) != dec->jp2klen)
370    fprintf( stderr, "j2k file write error\n");
371
372  close(outfd);
373
374  return true;
375 }
376
377 void output_log( bool messages, bool metadata, bool ihdrbox, jpip_dec_param_t *dec)
378 {
379   if( messages)
380     print_msgqueue( dec->msgqueue);
381
382   if( metadata)
383     print_allmetadata( dec->metadatalist);
384
385   if( ihdrbox){
386     printf("W*H: %d*%d\n", dec->ihdrbox->height, dec->ihdrbox->width);
387     printf("NC: %d, bpc: %d\n", dec->ihdrbox->nc, dec->ihdrbox->bpc);
388   }
389 }
390
391 void destroy_jpipdecoder( jpip_dec_param_t **dec)
392 {
393   free( (*dec)->jpipstream);
394   delete_msgqueue( &(*dec)->msgqueue);
395   if( (*dec)->metadatalist){
396     delete_metadatalist( &(*dec)->metadatalist);
397     free( (*dec)->ihdrbox);
398   }
399
400   free( (*dec)->jp2kstream);
401   free( *dec);
402 }
403
404 index_t * get_index_from_JP2file( int fd)
405 {
406   char *data;
407  
408   // Check resource is a JP family file.
409   if( lseek( fd, 0, SEEK_SET)==-1){
410     fprintf( stderr, "Error: File broken (lseek error)\n");
411     return NULL;
412   }
413   
414   data = (char *)malloc( 12); // size of header
415   if( read( fd, data, 12) != 12){
416     free( data);
417     fprintf( stderr, "Error: File broken (read error)\n");
418     return NULL;
419   }
420     
421   if( *data || *(data + 1) || *(data + 2) ||
422       *(data + 3) != 12 || strncmp (data + 4, "jP  \r\n\x87\n", 8)){
423     free( data);
424     fprintf( stderr, "Error: No JPEG 2000 Signature box in this file\n");
425     return NULL;
426   }
427   free( data);
428   
429   return parse_jp2file( fd);
430 }
431
432 void destroy_index( index_t **idx)
433 {
434   delete_index( idx);
435 }
436
437 void output_index( index_t *index)
438 {
439   print_index( *index);
440 }
441
442 #endif //SERVER