OSDN Git Service

Added support for DTS audio. DTS audio streams (of 5.1 audio and below) will be...
[handbrake-jp/handbrake-jp-git.git] / libhb / hb.c
1 #include "hb.h"
2
3 #include "ffmpeg/avcodec.h"
4
5 struct hb_handle_s
6 {
7     /* The "Check for update" thread */
8     int            build;
9     char           version[16];
10     hb_thread_t  * update_thread;
11
12     /* This thread's only purpose is to check other threads'
13        states */
14     volatile int   die;
15     hb_thread_t  * main_thread;
16     int            pid;
17
18     /* DVD/file scan thread */
19     hb_list_t    * list_title;
20     hb_thread_t  * scan_thread;
21
22     /* The thread which processes the jobs. Others threads are launched
23        from this one (see work.c) */
24     hb_list_t    * jobs;
25     int            job_count;
26     volatile int   work_die;
27     int            work_error;
28     hb_thread_t  * work_thread;
29
30     int            cpu_count;
31
32     hb_lock_t    * state_lock;
33     hb_state_t     state;
34
35     int            paused;
36     hb_lock_t    * pause_lock;
37 };
38
39 hb_work_object_t * hb_objects = NULL;
40
41 static void thread_func( void * );
42
43 /**
44  * Registers work objects, by adding the work object to a liked list.
45  * @param w Handle to hb_work_object_t to register.
46  */
47 void hb_register( hb_work_object_t * w )
48 {
49     w->next    = hb_objects;
50     hb_objects = w;
51 }
52
53 /**
54  * libhb initialization routine.
55  * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
56  * @param update_check signals libhb to check for updated version from HandBrake website.
57  * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
58  */
59 hb_handle_t * hb_init_real( int verbose, int update_check )
60 {
61     hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
62     uint64_t      date;
63
64     /* See hb_log() in common.c */
65     if( verbose > HB_DEBUG_NONE )
66     {
67         putenv( "HB_DEBUG=1" );
68                 av_log_set_level(AV_LOG_DEBUG);
69     }
70
71     /* Check for an update on the website if asked to */
72     h->build = -1;
73
74     if( update_check )
75     {
76         hb_log( "hb_init: checking for updates" );
77         date             = hb_get_date();
78         h->update_thread = hb_update_init( &h->build, h->version );
79
80         for( ;; )
81         {
82             if( hb_thread_has_exited( h->update_thread ) )
83             {
84                 /* Immediate success or failure */
85                 hb_thread_close( &h->update_thread );
86                 break;
87             }
88             if( hb_get_date() > date + 1000 )
89             {
90                 /* Still nothing after one second. Connection problem,
91                    let the thread die */
92                 hb_log( "hb_init: connection problem, not waiting for "
93                         "update_thread" );
94                 break;
95             }
96             hb_snooze( 500 );
97         }
98     }
99
100     /* CPU count detection */
101     hb_log( "hb_init: checking cpu count" );
102     h->cpu_count = hb_get_cpu_count();
103
104     h->list_title = hb_list_init();
105     h->jobs       = hb_list_init();
106
107     h->state_lock  = hb_lock_init();
108     h->state.state = HB_STATE_IDLE;
109
110     h->pause_lock = hb_lock_init();
111
112     /* libavcodec */
113     avcodec_init();
114     register_avcodec( &mpeg4_encoder );
115     register_avcodec( &mp2_decoder );
116     register_avcodec( &ac3_encoder );
117
118     /* Start library thread */
119     hb_log( "hb_init: starting libhb thread" );
120     h->die         = 0;
121     h->main_thread = hb_thread_init( "libhb", thread_func, h,
122                                      HB_NORMAL_PRIORITY );
123
124     return h;
125 }
126
127 /**
128  * libhb initialization routine.
129  * This version is to use when calling the dylib, the macro hb_init isn't available from a dylib call!
130  * @param verbose HB_DEBUG_NONE or HB_DEBUG_ALL.
131  * @param update_check signals libhb to check for updated version from HandBrake website.
132  * @return Handle to hb_handle_t for use on all subsequent calls to libhb.
133  */
134 hb_handle_t * hb_init_dl( int verbose, int update_check )
135 {
136     hb_handle_t * h = calloc( sizeof( hb_handle_t ), 1 );
137     uint64_t      date;
138
139     /* See hb_log() in common.c */
140     if( verbose > HB_DEBUG_NONE )
141     {
142         putenv( "HB_DEBUG=1" );
143                 av_log_set_level(AV_LOG_DEBUG);
144     }
145
146     /* Check for an update on the website if asked to */
147     h->build = -1;
148
149     if( update_check )
150     {
151         hb_log( "hb_init: checking for updates" );
152         date             = hb_get_date();
153         h->update_thread = hb_update_init( &h->build, h->version );
154
155         for( ;; )
156         {
157             if( hb_thread_has_exited( h->update_thread ) )
158             {
159                 /* Immediate success or failure */
160                 hb_thread_close( &h->update_thread );
161                 break;
162             }
163             if( hb_get_date() > date + 1000 )
164             {
165                 /* Still nothing after one second. Connection problem,
166                    let the thread die */
167                 hb_log( "hb_init: connection problem, not waiting for "
168                         "update_thread" );
169                 break;
170             }
171             hb_snooze( 500 );
172         }
173     }
174
175     /* CPU count detection */
176     hb_log( "hb_init: checking cpu count" );
177     h->cpu_count = hb_get_cpu_count();
178
179     h->list_title = hb_list_init();
180     h->jobs       = hb_list_init();
181
182     h->state_lock  = hb_lock_init();
183     h->state.state = HB_STATE_IDLE;
184
185     h->pause_lock = hb_lock_init();
186
187     /* libavcodec */
188     avcodec_init();
189     register_avcodec( &mpeg4_encoder );
190     register_avcodec( &mp2_decoder );
191     register_avcodec( &ac3_encoder );
192
193     /* Start library thread */
194     hb_log( "hb_init: starting libhb thread" );
195     h->die         = 0;
196     h->main_thread = hb_thread_init( "libhb", thread_func, h,
197                                      HB_NORMAL_PRIORITY );
198
199     hb_register( &hb_sync ); 
200         hb_register( &hb_decmpeg2 ); 
201         hb_register( &hb_decsub ); 
202         hb_register( &hb_render ); 
203         hb_register( &hb_encavcodec ); 
204         hb_register( &hb_encxvid ); 
205         hb_register( &hb_encx264 ); 
206         hb_register( &hb_deca52 ); 
207         hb_register( &hb_decdca ); 
208         hb_register( &hb_decavcodec ); 
209         hb_register( &hb_declpcm ); 
210         hb_register( &hb_encfaac ); 
211         hb_register( &hb_enclame ); 
212         hb_register( &hb_encvorbis ); 
213         
214         return h;
215 }
216
217
218 /**
219  * Returns current version of libhb.
220  * @param h Handle to hb_handle_t.
221  * @return character array of version number.
222  */
223 char * hb_get_version( hb_handle_t * h )
224 {
225     return HB_VERSION;
226 }
227
228 /**
229  * Returns current build of libhb.
230  * @param h Handle to hb_handle_t.
231  * @return character array of build number.
232  */
233 int hb_get_build( hb_handle_t * h )
234 {
235     return HB_BUILD;
236 }
237
238 /**
239  * Checks for needed update.
240  * @param h Handle to hb_handle_t.
241  * @param version Pointer to handle where version will be copied.
242  * @return update indicator.
243  */
244 int hb_check_update( hb_handle_t * h, char ** version )
245 {
246     *version = ( h->build < 0 ) ? NULL : h->version;
247     return h->build;
248 }
249
250 /**
251  * Sets the cpu count to the desired value.
252  * @param h Handle to hb_handle_t
253  * @param cpu_count Number of CPUs to use.
254  */
255 void hb_set_cpu_count( hb_handle_t * h, int cpu_count )
256 {
257     cpu_count    = MAX( 1, cpu_count );
258     cpu_count    = MIN( cpu_count, 8 );
259     h->cpu_count = cpu_count;
260 }
261
262 /**
263  * Initializes a scan of the by calling hb_scan_init
264  * @param h Handle to hb_handle_t
265  * @param path location of VIDEO_TS folder.
266  * @param title_index Desired title to scan.  0 for all titles.
267  */
268 void hb_scan( hb_handle_t * h, const char * path, int title_index )
269 {
270     hb_title_t * title;
271
272     /* Clean up from previous scan */
273     while( ( title = hb_list_item( h->list_title, 0 ) ) )
274     {
275         hb_list_rem( h->list_title, title );
276         hb_title_close( &title );
277     }
278     
279     hb_log( "hb_scan: path=%s, title_index=%d", path, title_index );
280     h->scan_thread = hb_scan_init( h, path, title_index, h->list_title );
281 }
282
283 /**
284  * Returns the list of titles found.
285  * @param h Handle to hb_handle_t
286  * @return Handle to hb_list_t of the title list.
287  */
288 hb_list_t * hb_get_titles( hb_handle_t * h )
289 {
290     return h->list_title;
291 }
292
293 /**
294  * Create preview image of desired title a index of picture.
295  * @param h Handle to hb_handle_t.
296  * @param title Handle to hb_title_t of desired title.
297  * @param picture Index in title.
298  * @param buffer Handle to buufer were inage will be drawn.
299  */
300 void hb_get_preview( hb_handle_t * h, hb_title_t * title, int picture,
301                      uint8_t * buffer )
302 {
303     hb_job_t           * job = title->job;
304     char                 filename[1024];
305     FILE               * file;
306     uint8_t            * buf1, * buf2, * buf3, * buf4, * pen;
307     uint32_t           * p32;
308     AVPicture            pic1, pic2, pic3, pic4;
309     ImgReSampleContext * context;
310     int                  i;
311
312     buf1 = malloc( title->width * title->height * 3 / 2 );
313     buf2 = malloc( title->width * title->height * 3 / 2 );
314     buf3 = malloc( title->width * title->height * 3 / 2 );
315     buf4 = malloc( title->width * title->height * 4 );
316     avpicture_fill( &pic1, buf1, PIX_FMT_YUV420P,
317                     title->width, title->height );
318     avpicture_fill( &pic2, buf2, PIX_FMT_YUV420P,
319                     title->width, title->height );
320     avpicture_fill( &pic3, buf3, PIX_FMT_YUV420P,
321                     job->width, job->height );
322     avpicture_fill( &pic4, buf4, PIX_FMT_RGBA32,
323                     job->width, job->height );
324     
325     memset( filename, 0, 1024 );
326
327     hb_get_tempory_filename( h, filename, "%x%d",
328                              (intptr_t) title, picture );
329
330     file = fopen( filename, "r" );
331     if( !file )
332     {
333         hb_log( "hb_get_preview: fopen failed" );
334         return;
335     }
336
337     fread( buf1, title->width * title->height * 3 / 2, 1, file );
338     fclose( file );
339
340     context = img_resample_full_init(
341             job->width, job->height, title->width, title->height,
342             job->crop[0], job->crop[1], job->crop[2], job->crop[3],
343             0, 0, 0, 0 );
344
345     if( job->deinterlace )
346     {
347         avpicture_deinterlace( &pic2, &pic1, PIX_FMT_YUV420P,
348                                title->width, title->height );
349         img_resample( context, &pic3, &pic2 );
350     }
351     else
352     {
353         img_resample( context, &pic3, &pic1 );
354     }
355     img_convert( &pic4, PIX_FMT_RGBA32, &pic3, PIX_FMT_YUV420P,
356                  job->width, job->height );
357
358     /* Gray background */
359     p32 = (uint32_t *) buffer;
360     for( i = 0; i < ( title->width + 2 ) * ( title->height + 2 ); i++ )
361     {
362         p32[i] = 0xFF808080;
363     }
364
365     /* Draw the picture, centered, and draw the cropping zone */
366     pen = buffer + ( title->height - job->height ) *
367         ( title->width + 2 ) * 2 + ( title->width - job->width ) * 2;
368     memset( pen, 0xFF, 4 * ( job->width + 2 ) );
369     pen += 4 * ( title->width + 2 );
370     for( i = 0; i < job->height; i++ )
371     {
372         uint8_t * nextLine;
373         nextLine = pen + 4 * ( title->width + 2 );
374         memset( pen, 0xFF, 4 );
375         pen += 4;
376         memcpy( pen, buf4 + 4 * job->width * i, 4 * job->width );
377         pen += 4 * job->width;
378         memset( pen, 0xFF, 4 );
379         pen = nextLine;
380     }
381     memset( pen, 0xFF, 4 * ( job->width + 2 ) );
382
383     free( buf1 );
384     free( buf2 );
385     free( buf3 );
386     free( buf4 );
387 }
388
389 /**
390  * Calculates job width, height, and cropping parameters.
391  * @param job Handle to hb_job_t.
392  * @param aspect Desired aspect ratio. Value of -1 uses title aspect.
393  * @param pixels Maximum desired pixel count.
394  */
395 void hb_set_size( hb_job_t * job, int aspect, int pixels )
396 {
397     hb_title_t * title = job->title;
398
399     int croppedWidth  = title->width - title->crop[2] - title->crop[3];
400     int croppedHeight = title->height - title->crop[0] - title->crop[1];
401     int croppedAspect = title->aspect * title->height * croppedWidth /
402                             croppedHeight / title->width;
403     int addCrop;
404     int i, w, h;
405
406     if( aspect <= 0 )
407     {
408         /* Keep the best possible aspect ratio */
409         aspect = croppedAspect;
410     }
411
412     /* Crop if necessary to obtain the desired ratio */
413     memcpy( job->crop, title->crop, 4 * sizeof( int ) );
414     if( aspect < croppedAspect )
415     {
416         /* Need to crop on the left and right */
417         addCrop = croppedWidth - aspect * croppedHeight * title->width /
418                     title->aspect / title->height;
419         if( addCrop & 3 )
420         {
421             addCrop = ( addCrop + 1 ) / 2;
422             job->crop[2] += addCrop;
423             job->crop[3] += addCrop;
424         }
425         else if( addCrop & 2 )
426         {
427             addCrop /= 2;
428             job->crop[2] += addCrop - 1;
429             job->crop[3] += addCrop + 1;
430         }
431         else
432         {
433             addCrop /= 2;
434             job->crop[2] += addCrop;
435             job->crop[3] += addCrop;
436         }
437     }
438     else if( aspect > croppedAspect )
439     {
440         /* Need to crop on the top and bottom */
441         addCrop = croppedHeight - croppedWidth * title->aspect *
442             title->height / aspect / title->width;
443         if( addCrop & 3 )
444         {
445             addCrop = ( addCrop + 1 ) / 2;
446             job->crop[0] += addCrop;
447             job->crop[1] += addCrop;
448         }
449         else if( addCrop & 2 )
450         {
451             addCrop /= 2;
452             job->crop[0] += addCrop - 1;
453             job->crop[1] += addCrop + 1;
454         }
455         else
456         {
457             addCrop /= 2;
458             job->crop[0] += addCrop;
459             job->crop[1] += addCrop;
460         }
461     }
462
463     /* Compute a resolution from the number of pixels and aspect */
464     for( i = 0;; i++ )
465     {
466         w = 16 * i;
467         h = MULTIPLE_16( w * HB_ASPECT_BASE / aspect );
468         if( w * h > pixels )
469         {
470             break;
471         }
472     }
473     i--;
474     job->width  = 16 * i;
475     job->height = MULTIPLE_16( 16 * i * HB_ASPECT_BASE / aspect );
476 }
477
478 /**
479  * Returns the number of jobs in the queue.
480  * @param h Handle to hb_handle_t.
481  * @return Number of jobs.
482  */
483 int hb_count( hb_handle_t * h )
484 {
485     return hb_list_count( h->jobs );
486 }
487
488 /**
489  * Returns handle to job at index i within the job list.
490  * @param h Handle to hb_handle_t.
491  * @param i Index of job.
492  * @returns Handle to hb_job_t of desired job.
493  */
494 hb_job_t * hb_job( hb_handle_t * h, int i )
495 {
496     return hb_list_item( h->jobs, i );
497 }
498
499 /**
500  * Adds a job to the job list.
501  * @param h Handle to hb_handle_t.
502  * @param job Handle to hb_job_t.
503  */
504 void hb_add( hb_handle_t * h, hb_job_t * job )
505 {
506     hb_job_t      * job_copy;
507     hb_title_t    * title,    * title_copy;
508     hb_chapter_t  * chapter,  * chapter_copy;
509     hb_audio_t    * audio,    * audio_copy;
510     hb_subtitle_t * subtitle, * subtitle_copy;
511     int             i;
512
513     /* Copy the title */
514     title      = job->title;
515     title_copy = malloc( sizeof( hb_title_t ) );
516     memcpy( title_copy, title, sizeof( hb_title_t ) );
517
518     title_copy->list_chapter = hb_list_init();
519     for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
520     {
521         chapter      = hb_list_item( title->list_chapter, i );
522         chapter_copy = malloc( sizeof( hb_chapter_t ) );
523         memcpy( chapter_copy, chapter, sizeof( hb_chapter_t ) );
524         hb_list_add( title_copy->list_chapter, chapter_copy );
525     }
526
527     /* Copy the audio track(s) we want */
528     title_copy->list_audio = hb_list_init();
529
530     /* Do nothing about audio during first pass */
531     if( job->pass != 1 )
532     {
533         for( i = 0; i < 8; i++ )
534         {
535             if( job->audios[i] < 0 )
536             {
537                 break;
538             }
539             if( ( audio = hb_list_item( title->list_audio, job->audios[i] ) ) )
540             {
541                 audio_copy = malloc( sizeof( hb_audio_t ) );
542                 memcpy( audio_copy, audio, sizeof( hb_audio_t ) );
543                 hb_list_add( title_copy->list_audio, audio_copy );
544             }
545         }
546     }
547
548     /* Copy the subtitle we want (or not) */
549     title_copy->list_subtitle = hb_list_init();
550     if( ( subtitle = hb_list_item( title->list_subtitle, job->subtitle ) ) )
551     {
552         subtitle_copy = malloc( sizeof( hb_subtitle_t ) );
553         memcpy( subtitle_copy, subtitle, sizeof( hb_subtitle_t ) );
554         hb_list_add( title_copy->list_subtitle, subtitle_copy );
555     }
556
557     /* Copy the job */
558     job_copy        = calloc( sizeof( hb_job_t ), 1 );
559     memcpy( job_copy, job, sizeof( hb_job_t ) );
560     title_copy->job = job_copy;
561     job_copy->title = title_copy;
562     job_copy->file  = strdup( job->file );
563     job_copy->h     = h;
564     job_copy->pause = h->pause_lock;
565
566     /* Add the job to the list */
567     hb_list_add( h->jobs, job_copy );
568 }
569
570 /**
571  * Removes a job from the job list.
572  * @param h Handle to hb_handle_t.
573  * @param job Handle to hb_job_t.
574  */
575 void hb_rem( hb_handle_t * h, hb_job_t * job )
576 {
577     hb_list_rem( h->jobs, job );
578
579     /* XXX free everything XXX */
580 }
581
582 /**
583  * Starts the conversion process.
584  * Sets state to HB_STATE_WORKING.
585  * calls hb_work_init, to launch work thread. Stores handle to work thread.
586  * @param h Handle to hb_handle_t.
587  */
588 void hb_start( hb_handle_t * h )
589 {
590     /* XXX Hack */
591     h->job_count = hb_list_count( h->jobs );
592
593     hb_lock( h->state_lock );
594     h->state.state = HB_STATE_WORKING;
595 #define p h->state.param.working
596     p.progress  = 0.0;
597     p.job_cur   = 1;
598     p.job_count = h->job_count;
599     p.rate_cur  = 0.0;
600     p.rate_avg  = 0.0;
601     p.hours     = -1;
602     p.minutes   = -1;
603     p.seconds   = -1;
604 #undef p
605     hb_unlock( h->state_lock );
606
607     h->paused = 0;
608
609     h->work_die    = 0;
610     h->work_thread = hb_work_init( h->jobs, h->cpu_count,
611                                    &h->work_die, &h->work_error );
612 }
613
614 /**
615  * Pauses the conversion process.
616  * @param h Handle to hb_handle_t.
617  */
618 void hb_pause( hb_handle_t * h )
619 {
620     if( !h->paused )
621     {
622         hb_lock( h->pause_lock );
623         h->paused = 1;
624
625         hb_lock( h->state_lock );
626         h->state.state = HB_STATE_PAUSED;
627         hb_unlock( h->state_lock );
628     }
629 }
630
631 /**
632  * Resumes the conversion process.
633  * @param h Handle to hb_handle_t.
634  */
635 void hb_resume( hb_handle_t * h )
636 {
637     if( h->paused )
638     {
639         hb_unlock( h->pause_lock );
640         h->paused = 0;
641     }
642 }
643
644 /**
645  * Stops the conversion process.
646  * @param h Handle to hb_handle_t.
647  */
648 void hb_stop( hb_handle_t * h )
649 {
650     h->work_die = 1;
651
652     hb_resume( h );
653 }
654
655 /**
656  * Returns the state of the conversion process.
657  * @param h Handle to hb_handle_t.
658  * @param s Handle to hb_state_t which to copy the state data.
659  */
660 void hb_get_state( hb_handle_t * h, hb_state_t * s )
661 {
662     hb_lock( h->state_lock );
663
664     memcpy( s, &h->state, sizeof( hb_state_t ) );
665     h->state.state = HB_STATE_IDLE;
666
667     hb_unlock( h->state_lock );
668 }
669
670 /**
671  * Closes access to libhb by freeing the hb_handle_t handle ontained in hb_init_real.
672  * @param _h Pointer to handle to hb_handle_t.
673  */
674 void hb_close( hb_handle_t ** _h )
675 {
676     hb_handle_t * h = *_h;
677     hb_title_t * title;
678
679     h->die = 1;
680     hb_thread_close( &h->main_thread );
681
682     while( ( title = hb_list_item( h->list_title, 0 ) ) )
683     {
684         hb_list_rem( h->list_title, title );
685         free( title->job );
686         hb_title_close( &title );
687     }
688     hb_list_close( &h->list_title );
689
690     hb_list_close( &h->jobs );
691     hb_lock_close( &h->state_lock );
692     hb_lock_close( &h->pause_lock );
693     free( h );
694     *_h = NULL;
695 }
696
697 /**
698  * Monitors the state of the update, scan, and work threads.
699  * Sets scan done state when scan thread exits.
700  * Sets work done state when work thread exits.
701  * @param _h Handle to hb_handle_t
702  */
703 static void thread_func( void * _h )
704 {
705     hb_handle_t * h = (hb_handle_t *) _h;
706     char dirname[1024];
707     DIR * dir;
708     struct dirent * entry;
709
710     h->pid = getpid();
711
712     /* Create folder for temporary files */
713     memset( dirname, 0, 1024 );
714     hb_get_tempory_directory( h, dirname );
715
716     hb_mkdir( dirname );
717
718     while( !h->die )
719     {
720         /* In case the check_update thread hangs, it'll die sooner or
721            later. Then, we join it here */
722         if( h->update_thread &&
723             hb_thread_has_exited( h->update_thread ) )
724         {
725             hb_thread_close( &h->update_thread );
726         }
727
728         /* Check if the scan thread is done */
729         if( h->scan_thread &&
730             hb_thread_has_exited( h->scan_thread ) )
731         {
732             hb_thread_close( &h->scan_thread );
733
734             hb_log( "libhb: scan thread found %d valid title(s)",
735                     hb_list_count( h->list_title ) );
736             hb_lock( h->state_lock );
737             h->state.state = HB_STATE_SCANDONE;
738             hb_unlock( h->state_lock );
739         }
740
741         /* Check if the work thread is done */
742         if( h->work_thread &&
743             hb_thread_has_exited( h->work_thread ) )
744         {
745             hb_thread_close( &h->work_thread );
746
747             hb_log( "libhb: work result = %d",
748                     h->work_error );
749             hb_lock( h->state_lock );
750             h->state.state                = HB_STATE_WORKDONE;
751             h->state.param.workdone.error = h->work_error;
752             hb_unlock( h->state_lock );
753         }
754
755         hb_snooze( 50 );
756     }
757
758     if( h->work_thread )
759     {
760         hb_stop( h );
761         hb_thread_close( &h->work_thread );
762     }
763
764     /* Remove temp folder */
765     dir = opendir( dirname );
766     while( ( entry = readdir( dir ) ) )
767     {
768         char filename[1024];
769         if( entry->d_name[0] == '.' )
770         {
771             continue;
772         }
773         memset( filename, 0, 1024 );
774         snprintf( filename, 1023, "%s/%s", dirname, entry->d_name );
775         unlink( filename );
776     }
777     closedir( dir );
778     rmdir( dirname );
779 }
780
781 /**
782  * Returns the PID.
783  * @param h Handle to hb_handle_t
784  */
785 int hb_get_pid( hb_handle_t * h )
786 {
787     return h->pid;
788 }
789
790 /**
791  * Sets the current state.
792  * @param h Handle to hb_handle_t
793  * @param s Handle to new hb_state_t
794  */
795 void hb_set_state( hb_handle_t * h, hb_state_t * s )
796 {
797     hb_lock( h->pause_lock );
798     hb_lock( h->state_lock );
799     memcpy( &h->state, s, sizeof( hb_state_t ) );
800     if( h->state.state == HB_STATE_WORKING )
801     {
802         /* XXX Hack */
803         h->state.param.working.job_cur =
804             h->job_count - hb_list_count( h->jobs );
805         h->state.param.working.job_count = h->job_count;
806     }
807     hb_unlock( h->state_lock );
808     hb_unlock( h->pause_lock );
809 }