summaryrefslogtreecommitdiff
path: root/noncore/multimedia/opieplayer2/lib.cpp
blob: 5f281b7230ad42fca5fc47a80f7b63607e1ff72b (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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
                    This file is part of the Opie Project

                      Copyright (c)  2002 Max Reiss <harlekin@handhelds.org>
                             Copyright (c)  2002 LJP <>
                             Copyright (c)  2002 Holger Freyther <zecke@handhelds.org>
              =.
            .=l.
     .>+-=
_;:,   .>  :=|.         This program is free software; you can
.> <`_,  > .  <=          redistribute it and/or  modify it under
:`=1 )Y*s>-.--  :           the terms of the GNU General Public
.="- .-=="i,   .._         License as published by the Free Software
- .  .-<_>   .<>         Foundation; either version 2 of the License,
  ._= =}    :          or (at your option) any later version.
  .%`+i>    _;_.
  .i_,=:_.   -<s.       This program is distributed in the hope that
  + . -:.    =       it will be useful,  but WITHOUT ANY WARRANTY;
  : ..  .:,   . . .    without even the implied warranty of
  =_    +   =;=|`    MERCHANTABILITY or FITNESS FOR A
 _.=:.    :  :=>`:     PARTICULAR PURPOSE. See the GNU
..}^=.=    =    ;      Library General Public License for more
++=  -.   .`   .:       details.
:   = ...= . :.=-
-.  .:....=;==+<;          You should have received a copy of the GNU
 -_. . .  )=. =           Library General Public License along with
  --    :-=`           this library; see the file COPYING.LIB.
                             If not, write to the Free Software Foundation,
                             Inc., 59 Temple Place - Suite 330,
                             Boston, MA 02111-1307, USA.

*/

#include "xinevideowidget.h"
#include "lib.h"

/* OPIE */
#include <opie2/odebug.h>
#include <qpe/global.h>

/* QT */
#include <qtextstream.h>
#include <qdir.h>
#include <qgfx_qws.h>

/* STD */
#include <assert.h>
#include <unistd.h>

typedef void (*display_xine_frame_t) (void *user_data, uint8_t* frame,
                      int  width, int height,int bytes );
typedef void (*vo_scale_cb) (void*, int, int, double,
                             int*, int*, int*, int*, double*, int*, int* );
typedef void (*dest_size_cb) (void*, int, int, double, int*, int*, double*);


extern "C" {
    xine_vo_driver_t* init_video_out_plugin( xine_t *xine, void* video, display_xine_frame_t, void *, vo_scale_cb,  dest_size_cb );
    int null_is_showing_video( const xine_vo_driver_t* self );
    void null_set_show_video( const xine_vo_driver_t* self, int show );
    void null_set_mode( const xine_vo_driver_t* self, int depth,  int rgb  );
    void null_display_handler( const xine_vo_driver_t* self, display_xine_frame_t t, void* user_data );
}

using namespace XINE;

Lib::Lib( InitializationMode initMode, XineVideoWidget* widget )
{
    m_initialized = false;
    m_duringInitialization = false;
    m_video = false;
    m_wid = widget;
    printf("Lib");
    QString configPath = QDir::homeDirPath() + "/Settings/opiexine.cf";
    // get the configuration

    // not really OO, should be an extra class, later
    if ( !QFile::exists(configPath) ) {
        QFile f(configPath);
        if (f.open(IO_WriteOnly)) {
            QTextStream ts( &f );
            ts << "misc.memcpy_method:glibc\n";
            ts << "# uncomment if you experience double speed audio \n #audio.oss_sync_method:softsync\n";
            ts << "codec.ffmpeg_pp_quality:3\n";
            ts << "audio.num_buffers:50\n";
            ts << "audio.size_buffers:4096\n";
            ts << "video.num_buffers:20\n";
            ts << "video.size_buffers:4096\n";
            ts << "audio.out_num_audio_buf:16\n";
            ts << "audio.out_size_audio_buf:8096\n";
            ts << "audio.out_size_zero_buf:1024\n";
            ts << "audio.passthrough_offset:0\n";
            f.close();
        } else
            owarn << "Failed to open " f.name() << oendl;
    }

    if ( initMode == InitializeImmediately ) {
        initialize();
        m_initialized = true;
    }
    else
        start();
}

void Lib::run()
{
    odebug << "Lib::run() started" << oendl;
    initialize();
    m_initialized = true;
    odebug << "Lib::run() finished" << oendl;
}

void Lib::initialize()
{
    m_duringInitialization = true;
    m_xine =  xine_new( );

    QString configPath = QDir::homeDirPath() + "/Settings/opiexine.cf";
    xine_config_load( m_xine, QFile::encodeName( configPath ) );

    xine_init( m_xine );

    // allocate oss for sound
    // and fb for framebuffer
    m_audioOutput = xine_open_audio_driver( m_xine,  "oss", NULL );
    m_videoOutput = ::init_video_out_plugin( m_xine, NULL,
                                             xine_display_frame, this,
                                             xine_vo_scale_cb,
                                             xine_dest_cb );

    m_stream = xine_stream_new (m_xine,  m_audioOutput,  m_videoOutput );
    xine_set_param( m_stream, XINE_PARAM_AUDIO_CLOSE_DEVICE, 1);

    if (m_wid != 0 )
        setWidget( m_wid );


    m_queue = xine_event_new_queue (m_stream);
    xine_event_create_listener_thread (m_queue, xine_event_handler, this);

    m_duringInitialization = false;
}

Lib::~Lib() {
    assert( isRunning() == false );
    assert( m_initialized );

//    free( m_config );

    xine_close( m_stream );
    xine_event_dispose_queue( m_queue );
    xine_dispose( m_stream );
    xine_close_audio_driver(m_xine,m_audioOutput);
    xine_close_video_driver(m_xine,m_videoOutput);
    xine_exit( m_xine );

    /* FIXME either free or delete but valgrind bitches against both */
    //free( m_videoOutput );
    //delete m_audioOutput;
}

void Lib::resize ( const QSize &s ) {
    assert( m_initialized || m_duringInitialization );

    if ( s. width ( ) && s. height ( ) )
        m_videoSize = s;
}

int Lib::majorVersion() {
     int major, minor, sub;
     xine_get_version ( &major, &minor, &sub );
     return major;
}

int Lib::minorVersion() {
     int major, minor, sub;
     xine_get_version ( &major, &minor, &sub );
     return minor;
}

int Lib::subVersion() {
     int major, minor, sub;
     xine_get_version ( &major, &minor, &sub );
     return sub;
}

int Lib::setfile(const QString& fileName)
{
    QString str = fileName.stripWhiteSpace();


    if ( !xine_open( m_stream, str.utf8().data() ) ) {
         return 0;
    }
    return 1;
}

int Lib::play( const QString& fileName, int startPos, int start_time ) {
    assert( m_initialized );

    QString str = fileName.stripWhiteSpace();


    if ( !xine_open( m_stream, str.utf8().data() ) ) {
         return 0;
    }
    return xine_play( m_stream, startPos, start_time);
}

void Lib::stop() {
    assert( m_initialized );
    xine_stop( m_stream );
    xine_set_param( m_stream, XINE_PARAM_AUDIO_CLOSE_DEVICE, 1);
}

void Lib::pause( bool toggle ) {
    assert( m_initialized );

    if ( toggle ) {
        xine_set_param( m_stream, XINE_PARAM_SPEED, XINE_SPEED_PAUSE );
        xine_set_param( m_stream, XINE_PARAM_AUDIO_CLOSE_DEVICE, 1);
    }

    else {
        xine_set_param( m_stream, XINE_PARAM_SPEED, XINE_SPEED_NORMAL );
    }
}

int Lib::speed() const {
    assert( m_initialized );

    return  xine_get_param ( m_stream, XINE_PARAM_SPEED );
}

void Lib::setSpeed( int speed ) {
    assert( m_initialized );

    xine_set_param ( m_stream, XINE_PARAM_SPEED, speed );
}

int Lib::status() const {
    assert( m_initialized );

    return xine_get_status( m_stream );
}

int Lib::currentPosition() const {
    assert( m_initialized );

    int pos, time, length;
    xine_get_pos_length( m_stream, &pos, &time, &length );
    return pos;
}

int Lib::currentTime() const {
    assert( m_initialized );

    int pos, time, length;
    pos = time = length = 0;

    if ( xine_get_pos_length( m_stream, &pos, &time, &length ) )
        return time/1000;
    else
        return 0;
}

int Lib::length() const {
      assert( m_initialized );

      int pos, time, length;
/* dilb: patch to solve the wrong stream length reported to the GUI*/
      int iRetVal=0, iTestLoop=0;

      do
        {
    iRetVal = xine_get_pos_length( m_stream, &pos, &time, &length );
    if (iRetVal)
       {/* if the function didn't return 0, then pos, time and length are valid.*/
       return length/1000;
       }
    /*don't poll too much*/
    usleep(100000);
    iTestLoop++;
    }
      while ( iTestLoop < 10 ); /* if after 1s, we still don't have any
valid stream, then return -1 (this value could be used to make the stream
unseekable, but it should never occur!! Mr. Murphy ? :) ) */

      return -1;
}

/* info about current stream */
QSize Lib::videoSize()const
{
    if (!m_initialized||!hasVideo()) return QSize(0,0);
    int width = xine_get_stream_info(m_stream,XINE_STREAM_INFO_VIDEO_WIDTH);
    int height = xine_get_stream_info(m_stream,XINE_STREAM_INFO_VIDEO_HEIGHT);
    return QSize(width,height);
}


bool Lib::isSeekable() const {
    assert( m_initialized );

    return xine_get_stream_info( m_stream, XINE_STREAM_INFO_SEEKABLE );
}

bool Lib::hasVideo() const {
    assert( m_initialized );

    return  xine_get_stream_info( m_stream, XINE_STREAM_INFO_HAS_VIDEO);
}

int Lib::audioBitrate()const
{
    if (!m_initialized) return 0;
    return  xine_get_stream_info( m_stream, XINE_STREAM_INFO_AUDIO_BITRATE);
}
int Lib::videoBitrate()const
{
    if (!m_initialized||!hasVideo()) return 0;
    return  xine_get_stream_info( m_stream, XINE_STREAM_INFO_VIDEO_BITRATE);
}
/* end info block */

void Lib::seekTo( int time ) {
    assert( m_initialized );

    odebug << "Seeking to second " << time << oendl;
    //Keep it paused if it was in that state
    if ( xine_get_param( m_stream, XINE_PARAM_SPEED ) ) {
        xine_play( m_stream, 0, time*1000 );
    }

    else {
        xine_play( m_stream, 0, time*1000 );
        xine_set_param( m_stream, XINE_PARAM_SPEED, XINE_SPEED_PAUSE );
    }

}


QString Lib::metaInfo( int number) const {
    assert( m_initialized );

    return xine_get_meta_info( m_stream, number );
}

int Lib::error() const {
    assert( m_initialized );

    return xine_get_error( m_stream );
};

void Lib::ensureInitialized()
{
    if ( m_initialized )
        return;

    odebug << "waiting for initialization thread to finish" << oendl;
    wait();
    odebug << "initialization thread finished!" << oendl;
}

void Lib::setWidget( XineVideoWidget *widget )
{
    m_wid = widget;
    if (m_wid) {
        resize ( m_wid-> size ( ) );
        ::null_set_mode( m_videoOutput, qt_screen->depth(), qt_screen->pixelType() );
    }
}

void Lib::receiveMessage( ThreadUtil::ChannelMessage *msg, SendType sendType )
{
    assert( sendType == ThreadUtil::Channel::OneWay );
    handleXineEvent( msg->type(), msg->data(), msg->msg() );
    delete msg;
}

void Lib::handleXineEvent( const xine_event_t* t ) {
    int prog = -1; const char* name = 0;
    if ( t->type == XINE_EVENT_PROGRESS ) {
        xine_progress_data_t *pt = static_cast<xine_progress_data_t*>( t->data );
        prog = pt->percent;
        name = pt->description;
    }

    send( new ThreadUtil::ChannelMessage( t->type, prog, name ),  OneWay );
}

void Lib::handleXineEvent( int type, int data, const char* name ) {
    assert( m_initialized );

    if ( type == XINE_EVENT_UI_PLAYBACK_FINISHED ) {
        emit stopped();
    }else if ( type == XINE_EVENT_PROGRESS ) {
        QString str = name == 0 ? QString::null : QString::fromUtf8( name );
        Global::statusMessage( tr( "Progress: %1 %2" ).arg( name, data ) );;
    }
}


void Lib::setShowVideo( bool video ) {
    assert( m_initialized );

    m_video = video;
    ::null_set_show_video( m_videoOutput, video );
}

bool Lib::isShowingVideo() const {
    assert( m_initialized );

    return ::null_is_showing_video( m_videoOutput );
}

void Lib::showVideoFullScreen( bool fullScreen ) {
    assert( m_initialized );

    #warning use xine
}

bool Lib::isVideoFullScreen() const {
    assert( m_initialized );

    #warning use xine
    return false;
}

void Lib::setScaling( bool scale ) {
    assert( m_initialized );

    xine_set_param( m_stream, XINE_PARAM_VO_ASPECT_RATIO,
                    scale ? XINE_VO_ASPECT_AUTO : XINE_VO_ASPECT_SQUARE );
}

void Lib::setGamma( int value ) {
    assert( m_initialized );
    xine_set_param( m_stream, XINE_PARAM_VO_BRIGHTNESS, value );
}

bool Lib::isScaling() const {
    assert( m_initialized );

    int aratio = xine_get_param( m_stream, XINE_PARAM_VO_ASPECT_RATIO );
    return aratio == XINE_VO_ASPECT_AUTO;
}

void Lib::xine_event_handler( void* user_data, const xine_event_t* t ) {
    ( (Lib*)user_data)->handleXineEvent( t );
}

void Lib::xine_display_frame( void* user_data, uint8_t *frame,
                              int width,  int height,  int bytes ) {
    ( (Lib*)user_data)->drawFrame( frame, width, height, bytes );
}

void Lib::xine_vo_scale_cb( void *user_data, int video_with, int video_height,
                            double video_pixel_aspect,
                            int *dest_x, int *dest_y, int *dest_width,
                            int *dest_height, double *dest_pixel_aspect,
                            int *win_x, int *win_y ) {
    QSize size = ((Lib*)user_data)->m_videoSize;
    if (!size.isValid())
        return;

    *dest_x      = 0;
    *dest_y      = 0;
    *dest_width  = size.width();
    *dest_height = size.height();
    *win_x       = 0;
    *win_y       = 0;
}

void Lib::xine_dest_cb( void* user_data, int, int, double,
                        int *dest_width, int* dest_height, double* ) {
    QSize size = ((Lib*)user_data)->m_videoSize;
    if ( !size.isValid() )
        return;

    *dest_width  = size.width();
    *dest_height = size.height();
}

void Lib::drawFrame( uint8_t* frame,  int width,  int height,  int bytes ) {
    assert( m_initialized );

    if ( !m_video )
        return;

    if (m_wid) m_wid-> setVideoFrame ( frame, width, height, bytes );
}