summaryrefslogtreecommitdiffstats
path: root/HBManager.cpp
blob: 528a58afec02b03f3d44d4fbedcc992130f1a0d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
/* $Id: HBManager.cpp,v 1.28 2003/08/24 21:56:03 titer Exp $ */

#include "HBCommon.h"
#include "HBManager.h"
#include "HBWindow.h"
#include "HBFifo.h"
#include "HBDVDReader.h"
#include "HBMpegDemux.h"
#include "HBMpeg2Decoder.h"
#include "HBMpeg4Encoder.h"
#include "HBAc3Decoder.h"
#include "HBMp3Encoder.h"
#include "HBAviMuxer.h"

#include <Directory.h>
#include <Drivers.h>
#include <Path.h>
#include <Query.h>
#include <String.h>
#include <VolumeRoster.h>

#include <fs_info.h>
#include <sys/ioctl.h>

/* Public methods */

HBManager::HBManager( HBWindow * window )
    : BLooper( "manager" )
{
    fWindow     = window;
    fFifoList   = new BList();
    fThreadList = new BList();
    fVolumeList = new BList();
    
    Run();
}

HBManager::~HBManager()
{
    delete fFifoList;
    delete fThreadList;
    delete fVolumeList;
}

void HBManager::MessageReceived( BMessage * message )
{
    switch( message->what )
    {
        case DETECT_VOLUMES:
        {
            DetectVolumes();
            break;
        }

        default:
            BLooper::MessageReceived( message );
    }
}

void HBManager::SetPosition( float position )
{
    if( position - fPosition < 0.0001 )
        /* No need to be more precise ;) */
        return;

    fPosition = position;
    
    char statusText[128]; memset( statusText, 0, 128 );
    sprintf( statusText, "Encoding : %.2f %% - %.2f fps (average : %.2f fps)",
             100 * fPosition, fCurrentFrameRate, fAverageFrameRate );
    Status( statusText, fPosition, ENABLE_ENCODING );
}

void HBManager::SetFrameRate( float current, float average )
{
    fCurrentFrameRate = current;
    fAverageFrameRate = average;
    
    char statusText[128]; memset( statusText, 0, 128 );
    sprintf( statusText, "Encoding : %.2f %% - %.2f fps (average : %.2f fps)",
             100 * fPosition, fCurrentFrameRate, fAverageFrameRate );
    Status( statusText, fPosition, ENABLE_ENCODING );
}

void HBManager::Start( HBVolumeInfo   * volumeInfo,
                       HBTitleInfo    * titleInfo,
                       HBAudioInfo    * audio1Info,
                       HBAudioInfo    * audio2Info,
                       char           * file )
{
    fPosition         = 0;
    fCurrentFrameRate = 0;
    fAverageFrameRate = 0;
    
    /* Remember the fifos that should be freezed in Stop() */
    fFifoList->AddItem( ( titleInfo->fPSFifo    = new HBFifo( 1024 ) ) );
    fFifoList->AddItem( ( titleInfo->fMpeg2Fifo = new HBFifo( 5 ) ) );
    fFifoList->AddItem( ( titleInfo->fRawFifo   = new HBFifo( 5 ) ) );
    fFifoList->AddItem( ( titleInfo->fMpeg4Fifo = new HBFifo( 5 ) ) );
    
    /* Create the threads */
    fThreadList->AddItem( new HBDVDReader( this, titleInfo ) );
    fThreadList->AddItem( new HBMpegDemux( this, titleInfo,
                                           audio1Info, audio2Info ) );
    fThreadList->AddItem( new HBMpeg2Decoder( this, titleInfo ) );
    fThreadList->AddItem( new HBMpeg4Encoder( this, titleInfo ) );
    
    if( audio1Info->fId )
    {
        fFifoList->AddItem( ( audio1Info->fAc3Fifo = new HBFifo( 5 ) ) );
        fFifoList->AddItem( ( audio1Info->fRawFifo = new HBFifo( 5 ) ) );
        fFifoList->AddItem( ( audio1Info->fMp3Fifo = new HBFifo( 5 ) ) );
        fThreadList->AddItem( new HBAc3Decoder( this, audio1Info ) );
        fThreadList->AddItem( new HBMp3Encoder( this, audio1Info ) );
    }
    
    if( audio2Info->fId )
    {
        fFifoList->AddItem( ( audio2Info->fAc3Fifo = new HBFifo( 5 ) ) );
        fFifoList->AddItem( ( audio2Info->fRawFifo = new HBFifo( 5 ) ) );
        fFifoList->AddItem( ( audio2Info->fMp3Fifo = new HBFifo( 5 ) ) );
        fThreadList->AddItem( new HBAc3Decoder( this, audio2Info ) );
        fThreadList->AddItem( new HBMp3Encoder( this, audio2Info ) );
    }
    
    fThreadList->AddItem( new HBAviMuxer( this, titleInfo, audio1Info,
                                          audio2Info, file ) );
    
    /* Run ! */
    HBThread * thread;
    for( int i = 0; i < fThreadList->CountItems(); i++ )
    {
        thread = (HBThread*) fThreadList->ItemAt( i );
        thread->Run();
    }
}

void HBManager::Suspend()
{
    HBThread * thread;
    for( int i = 0; i < fThreadList->CountItems(); i++ )
    {
        thread = (HBThread*) fThreadList->ItemAt( i );
        thread->Suspend();
    }
}

void HBManager::Resume()
{
    HBThread * thread;
    for( int i = 0; i < fThreadList->CountItems(); i++ )
    {
        thread = (HBThread*) fThreadList->ItemAt( i );
        thread->Resume();
    }
}

bool HBManager::Cancel()
{
    if( !( fFifoList->CountItems() ) )
        /* Not running */
        return false;
    
    Status( "Cancelled.", 0.0, ENABLE_READY );
    Stop();
    
    return true;
}

/* Called by the DVD reader */
void HBManager::Done()
{
    HBFifo * fifo = NULL;
    for( int i = 0; i < fFifoList->CountItems(); i++ )
    {
        fifo = (HBFifo*) fFifoList->ItemAt( i );
        
        /* Wait until all threads have finished */
        while( fifo->Size() > 0 )
            snooze( 5000 );
    }
    
    char statusText[128]; memset( statusText, 0, 128 );
    sprintf( statusText, "Done (%.2f fps).", fAverageFrameRate );
    Status( statusText, 1.0, ENABLE_READY );
    Stop();
}

void HBManager::Error()
{
    Status( "An error occured.", 0.0, ENABLE_READY );
    Stop();
}

/* Private */

void HBManager::Stop()
{
    /* Freeze fifos */
    for( int i = 0; i < fFifoList->CountItems(); i++ )
    {
        ((HBFifo*) fFifoList->ItemAt( i ))->Die();
    }
    
    /* Stop threads */
    HBThread * thread;
    while( ( thread = (HBThread*) fThreadList->ItemAt( 0 ) ) )
    {
        fThreadList->RemoveItem( thread );
        delete thread;
    }
    
    /* Destroy fifos */
    HBFifo * fifo;
    while( ( fifo = (HBFifo*) fFifoList->ItemAt( 0 ) ) )
    {
        fFifoList->RemoveItem( fifo );
        delete fifo;
    }
}

void HBManager::DetectVolumes()
{
    /* Empty the current list */
    HBVolumeInfo * volumeInfo;
    while( ( volumeInfo = (HBVolumeInfo*) fVolumeList->ItemAt( 0 ) ) )
    {
        fVolumeList->RemoveItem( volumeInfo );
        delete volumeInfo;
    }

    /* Detect the DVD drives by parsing mounted volumes */
    BVolumeRoster   * roster = new BVolumeRoster();
    BVolume         * volume = new BVolume();
    fs_info           info;
    int               device;
    device_geometry   geometry;
    
    while( roster->GetNextVolume( volume ) == B_NO_ERROR )
    {
        /* open() and ioctl() for more informations */
        fs_stat_dev( volume->Device(), &info );
        if( ( device = open( info.device_name, O_RDONLY ) ) < 0 )
            continue;
        
        if( ioctl( device, B_GET_GEOMETRY, &geometry,
                   sizeof( geometry ) ) < 0 )
            continue;

        /* Get the volume name */
        char volumeName[B_FILE_NAME_LENGTH];
        volume->GetName( volumeName );

        if( volume->IsReadOnly() && geometry.device_type == B_CD )
        {
            /* May be a DVD */
            
            /* Try to open the device */
            volumeInfo = new HBVolumeInfo( volumeName,
                                           info.device_name );
        
            if( !volumeInfo->InitCheck() )
            {
                delete volumeInfo;
                continue;
            }
        
            fVolumeList->AddItem( volumeInfo );
        }
        else if( geometry.device_type == B_DISK )
        {
            /* May be a hard drive. Look for VIDEO_TS folders on it */
            
            BQuery * query = new BQuery();
            
            if( query->SetVolume( volume ) != B_OK )
            {
                delete query;
                continue;
            }
            
            if( query->SetPredicate( "name = VIDEO_TS.BUP" ) != B_OK )
            {
                delete query;
                continue;
            }

            query->Fetch();
            
            BEntry entry, parentEntry;
            BPath  path;
            while( query->GetNextEntry( &entry ) == B_OK )
            {
                entry.GetParent( &parentEntry );
                parentEntry.GetPath( &path );
                
                /* Try to open the folder */
                volumeInfo = new HBVolumeInfo( (char*) path.Path(),
                                               (char*) path.Path() );
        
                if( !volumeInfo->InitCheck() )
                {
                    delete volumeInfo;
                    continue;
                }
        
                fVolumeList->AddItem( volumeInfo );
            }
            
            delete query;
        }
    }
    
    /* Refresh the interface */
    BMessage * message = new BMessage( VOLUMES_DETECTED );
    message->AddPointer( "list", fVolumeList );
    fWindow->PostMessage( message );
    delete message;
}