~ubuntu-branches/ubuntu/oneiric/phonon/oneiric-201108111512

« back to all changes in this revision

Viewing changes to xine/bytestream.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2011-01-24 10:12:11 UTC
  • mfrom: (0.5.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20110124101211-w9rew7q0dmwbwhqx
Tags: 4:4.7.0really4.4.4-0ubuntu1
* New upstream release
* Xine and GStreamer backends now split out source, remove build-deps and
  binary packages from debian/control
* Remove 02_no_rpath.patch, now upstream
* Disable kubuntu04_no_va_mangle.patch, no longer applies
* Remove kubuntu_05_gst_codec_installer_window_id.diff, kubuntu_06_forward_events.diff,
  kubuntu_07_include_fix.diff, gstreamer now separate

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*  This file is part of the KDE project
2
 
    Copyright (C) 2006 Tim Beaulen <tbscope@gmail.com>
3
 
    Copyright (C) 2006-2007 Matthias Kretz <kretz@kde.org>
4
 
 
5
 
    This program is free software; you can redistribute it and/or
6
 
    modify it under the terms of the GNU Library General Public
7
 
    License as published by the Free Software Foundation; either
8
 
    version 2 of the License, or (at your option) any later version.
9
 
 
10
 
    This library is distributed in the hope that it will be useful,
11
 
    but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 
    Library General Public License for more details.
14
 
 
15
 
    You should have received a copy of the GNU Library General Public License
16
 
    along with this library; see the file COPYING.LIB.  If not, write to
17
 
    the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18
 
    Boston, MA 02110-1301, USA.
19
 
 
20
 
*/
21
 
 
22
 
#include "bytestream.h"
23
 
 
24
 
#include "xineengine.h"
25
 
#include "events.h"
26
 
#include <QEvent>
27
 
#include <QTimer>
28
 
#include <cstring>
29
 
#include <cstdio>
30
 
#include <unistd.h>
31
 
 
32
 
extern "C" {
33
 
#define this this_xine
34
 
#include <xine/input_plugin.h> // needed for MAX_PREVIEW_SIZE
35
 
#include <xine/xine_internal.h>
36
 
#undef this
37
 
}
38
 
 
39
 
//#define VERBOSE_DEBUG
40
 
#ifdef VERBOSE_DEBUG
41
 
#  define PXINE_VDEBUG debug() << Q_FUNC_INFO
42
 
#else
43
 
#  define PXINE_VDEBUG debug() << Q_FUNC_INFO
44
 
#endif
45
 
#define PXINE_DEBUG debug() << Q_FUNC_INFO
46
 
 
47
 
namespace Phonon
48
 
{
49
 
namespace Xine
50
 
{
51
 
 
52
 
ByteStream *ByteStream::fromMrl(const QByteArray &mrl)
53
 
{
54
 
    if (!mrl.startsWith("kbytestream:/")) {
55
 
        return 0;
56
 
    }
57
 
    ByteStream *ret = 0;
58
 
    Q_ASSERT(mrl.length() >= 13 + (int)sizeof(void *) && mrl.length() <= 13 + 2 * (int)sizeof(void *));
59
 
    const unsigned char *encoded = reinterpret_cast<const unsigned char *>(mrl.constData() + 13);
60
 
    unsigned char *addrHack = reinterpret_cast<unsigned char *>(&ret);
61
 
    for (unsigned int i = 0; i < sizeof(void *); ++i, ++encoded) {
62
 
        if (*encoded == 0x01) {
63
 
            ++encoded;
64
 
            switch (*encoded) {
65
 
            case 0x01:
66
 
                addrHack[i] = '\0';
67
 
                break;
68
 
            case 0x02:
69
 
                addrHack[i] = '\1';
70
 
                break;
71
 
            case 0x03:
72
 
                addrHack[i] = '#';
73
 
                break;
74
 
            case 0x04:
75
 
                addrHack[i] = '%';
76
 
                break;
77
 
            default:
78
 
                abort();
79
 
            }
80
 
        } else {
81
 
            addrHack[i] = *encoded;
82
 
        }
83
 
    }
84
 
    return ret;
85
 
}
86
 
 
87
 
ByteStream::ByteStream(const MediaSource &mediaSource, MediaObject *parent)
88
 
    : QObject(0), // don't let MediaObject's ~QObject delete us - the input plugin will delete us
89
 
    m_mediaObject(parent),
90
 
    m_streamSize(0),
91
 
    m_currentPosition(0),
92
 
    m_buffersize(0),
93
 
    m_offset(0),
94
 
    m_seekable(false),
95
 
    m_stopped(false),
96
 
    m_eod(false),
97
 
    m_buffering(false),
98
 
    m_firstReset(true)
99
 
{
100
 
    connect(this, SIGNAL(resetQueued()), this, SLOT(callStreamInterfaceReset()), Qt::BlockingQueuedConnection);
101
 
    connect(this, SIGNAL(needDataQueued()), this, SLOT(needData()), Qt::QueuedConnection);
102
 
    connect(this, SIGNAL(seekStreamQueued(qint64)), this, SLOT(syncSeekStream(qint64)), Qt::QueuedConnection);
103
 
 
104
 
    connectToSource(mediaSource);
105
 
 
106
 
    // created in the main thread
107
 
    m_mainThread = pthread_self();
108
 
}
109
 
 
110
 
void ByteStream::pullBuffer(char *buf, int len)
111
 
{
112
 
    if (m_stopped) {
113
 
        return;
114
 
    }
115
 
    // never called from main thread
116
 
    //Q_ASSERT(m_mainThread != pthread_self());
117
 
 
118
 
    PXINE_VDEBUG << len << ", m_offset = " << m_offset << ", m_currentPosition = "
119
 
        << m_currentPosition << ", m_buffersize = " << m_buffersize;
120
 
    while (len > 0) {
121
 
        if (m_buffers.isEmpty()) {
122
 
            // pullBuffer is only called when there's => len data available
123
 
            qFatal("m_currentPosition = %lld, m_preview.size() = %d, len = %d",
124
 
                    m_currentPosition, m_preview.size() ,len);
125
 
        }
126
 
        if (m_buffers.head().size() - m_offset <= len) {
127
 
            // The whole data of the next buffer is needed
128
 
            QByteArray buffer = m_buffers.dequeue();
129
 
            PXINE_VDEBUG << "dequeue one buffer of size " << buffer.size()
130
 
                << ", reading at offset = " << m_offset << ", resetting m_offset to 0";
131
 
            Q_ASSERT(buffer.size() > 0);
132
 
            int tocopy = buffer.size() - m_offset;
133
 
            Q_ASSERT(tocopy > 0);
134
 
            xine_fast_memcpy(buf, buffer.constData() + m_offset, tocopy);
135
 
            buf += tocopy;
136
 
            len -= tocopy;
137
 
            Q_ASSERT(len >= 0);
138
 
            Q_ASSERT(m_buffersize >= static_cast<size_t>(tocopy));
139
 
            m_buffersize -= tocopy;
140
 
            m_offset = 0;
141
 
        } else {
142
 
            // only a part of the next buffer is needed
143
 
            PXINE_VDEBUG << "read " << len
144
 
                << " bytes from the first buffer at offset = " << m_offset;
145
 
            QByteArray &buffer = m_buffers.head();
146
 
            Q_ASSERT(buffer.size() > 0);
147
 
            xine_fast_memcpy(buf, buffer.constData() + m_offset , len);
148
 
            m_offset += len;
149
 
            Q_ASSERT(m_buffersize >= static_cast<size_t>(len));
150
 
            m_buffersize -= len;
151
 
            len = 0;
152
 
        }
153
 
    }
154
 
}
155
 
 
156
 
int ByteStream::peekBuffer(void *buf)
157
 
{
158
 
    if (m_stopped) {
159
 
        return 0;
160
 
    }
161
 
 
162
 
    // never called from main thread
163
 
    //Q_ASSERT(m_mainThread != pthread_self());
164
 
 
165
 
    if (m_preview.size() < MAX_PREVIEW_SIZE && !m_eod) {
166
 
        QMutexLocker lock(&m_mutex);
167
 
        // the thread needs to sleep until a wait condition is signalled from writeData
168
 
        while (!m_eod && !m_stopped && m_preview.size() < MAX_PREVIEW_SIZE) {
169
 
            PXINE_VDEBUG << "xine waits for data: " << m_buffersize << ", " << m_eod;
170
 
            emit needDataQueued();
171
 
            m_waitingForData.wait(&m_mutex);
172
 
        }
173
 
        if (m_stopped) {
174
 
            PXINE_DEBUG << "returning 0, m_stopped = true";
175
 
            return 0;
176
 
        }
177
 
    }
178
 
 
179
 
    xine_fast_memcpy(buf, m_preview.constData(), m_preview.size());
180
 
    return m_preview.size();
181
 
}
182
 
 
183
 
qint64 ByteStream::readFromBuffer(void *buf, size_t count)
184
 
{
185
 
    if (m_stopped) {
186
 
        return 0;
187
 
    }
188
 
    // never called from main thread
189
 
    //Q_ASSERT(m_mainThread != pthread_self());
190
 
 
191
 
    const qint64 currentPosition = m_currentPosition;
192
 
 
193
 
    PXINE_VDEBUG << count;
194
 
 
195
 
    QMutexLocker lock(&m_mutex);
196
 
    //debug() << Q_FUNC_INFO << "LOCKED m_mutex: ";
197
 
    // get data while more is needed and while we're still receiving data
198
 
    if (m_buffersize < count && !m_eod) {
199
 
        // the thread needs to sleep until a wait condition is signalled from writeData
200
 
        while (!m_eod && !m_stopped && m_buffersize < count) {
201
 
            PXINE_VDEBUG << "xine waits for data: " << m_buffersize << ", " << m_eod;
202
 
            emit needDataQueued();
203
 
            m_waitingForData.wait(&m_mutex);
204
 
        }
205
 
        if (m_stopped) {
206
 
            PXINE_DEBUG << "returning 0, m_stopped = true";
207
 
            //debug() << Q_FUNC_INFO << "UNLOCKING m_mutex: ";
208
 
            return 0;
209
 
        }
210
 
        Q_ASSERT(currentPosition == m_currentPosition);
211
 
        Q_UNUSED(currentPosition);
212
 
        //PXINE_VDEBUG << "m_buffersize = " << m_buffersize;
213
 
    }
214
 
    if (m_buffersize >= count) {
215
 
        PXINE_VDEBUG << "calling pullBuffer with m_buffersize = " << m_buffersize;
216
 
        pullBuffer(static_cast<char *>(buf), count);
217
 
        m_currentPosition += count;
218
 
        //debug() << Q_FUNC_INFO << "UNLOCKING m_mutex: ";
219
 
        return count;
220
 
    }
221
 
    Q_ASSERT(m_eod);
222
 
    if (m_buffersize > 0) {
223
 
        PXINE_VDEBUG << "calling pullBuffer with m_buffersize = " << m_buffersize;
224
 
        const int len = m_buffersize;
225
 
        pullBuffer(static_cast<char *>(buf), len);
226
 
        m_currentPosition += len;
227
 
        PXINE_DEBUG << "returning less data than requested, the stream is at its end";
228
 
        //debug() << Q_FUNC_INFO << "UNLOCKING m_mutex: ";
229
 
        return len;
230
 
    }
231
 
    PXINE_DEBUG << "return 0, the stream is at its end";
232
 
    //debug() << Q_FUNC_INFO << "UNLOCKING m_mutex: ";
233
 
    return 0;
234
 
}
235
 
 
236
 
off_t ByteStream::seekBuffer(qint64 offset)
237
 
{
238
 
    if (m_stopped) {
239
 
        return 0;
240
 
    }
241
 
    // never called from main thread
242
 
    //Q_ASSERT(m_mainThread != pthread_self());
243
 
 
244
 
    // no seek
245
 
    if (offset == m_currentPosition) {
246
 
        return m_currentPosition;
247
 
    }
248
 
 
249
 
    // impossible seek
250
 
    if (offset > m_streamSize) {
251
 
        qWarning() << "xine is asking to seek behind the end of the data stream";
252
 
        return m_currentPosition;
253
 
    }
254
 
 
255
 
    // first try to seek in the data we have buffered
256
 
    m_mutex.lock();
257
 
    //debug() << Q_FUNC_INFO << "LOCKED m_mutex: ";
258
 
    if (offset > m_currentPosition && offset < m_currentPosition + m_buffersize) {
259
 
        debug() << Q_FUNC_INFO << "seeking behind current position, but inside the buffered data";
260
 
        // seek behind the current position in the buffer
261
 
        while (offset > m_currentPosition) {
262
 
            const int gap = offset - m_currentPosition;
263
 
            Q_ASSERT(!m_buffers.isEmpty());
264
 
            const int buffersize = m_buffers.head().size() - m_offset;
265
 
            if (buffersize <= gap) {
266
 
                // discard buffers if they hold data before offset
267
 
                Q_ASSERT(!m_buffers.isEmpty());
268
 
                QByteArray buffer = m_buffers.dequeue();
269
 
                m_buffersize -= buffersize;
270
 
                m_currentPosition += buffersize;
271
 
                m_offset = 0;
272
 
            } else {
273
 
                // offset points to data in the next buffer
274
 
                m_buffersize -= gap;
275
 
                m_currentPosition += gap;
276
 
                m_offset += gap;
277
 
            }
278
 
        }
279
 
        Q_ASSERT(offset == m_currentPosition);
280
 
        //debug() << Q_FUNC_INFO << "UNLOCKING m_mutex: ";
281
 
        m_mutex.unlock();
282
 
        return m_currentPosition;
283
 
    } else if (offset < m_currentPosition && m_currentPosition - offset <= m_offset) {
284
 
        debug() << Q_FUNC_INFO << "seeking in current buffer: m_currentPosition = " << m_currentPosition << ", m_offset = " << m_offset;
285
 
        // seek before the current position in the buffer
286
 
        m_offset -= m_currentPosition - offset;
287
 
        m_buffersize += m_currentPosition - offset;
288
 
        Q_ASSERT(m_offset >= 0);
289
 
        m_currentPosition = offset;
290
 
        //debug() << Q_FUNC_INFO << "UNLOCKING m_mutex: ";
291
 
        m_mutex.unlock();
292
 
        return m_currentPosition;
293
 
    }
294
 
 
295
 
    // the ByteStream is not seekable: no chance to seek to the requested offset
296
 
    if (!m_seekable) {
297
 
        //debug() << Q_FUNC_INFO << "UNLOCKING m_mutex: ";
298
 
        m_mutex.unlock();
299
 
        return m_currentPosition;
300
 
    }
301
 
 
302
 
    PXINE_DEBUG << "seeking to a position that's not in the buffered data: clear the buffer. "
303
 
        " new offset = " << offset <<
304
 
        ", m_buffersize = " << m_buffersize <<
305
 
        ", m_offset = " << m_offset <<
306
 
        ", m_eod = " << m_eod <<
307
 
        ", m_currentPosition = " << m_currentPosition;
308
 
 
309
 
    // throw away the buffers and ask for new data
310
 
    m_buffers.clear();
311
 
    m_buffersize = 0;
312
 
    m_offset = 0;
313
 
    m_eod = false;
314
 
 
315
 
    m_currentPosition = offset;
316
 
    //debug() << Q_FUNC_INFO << "UNLOCKING m_mutex: ";
317
 
    m_mutex.unlock();
318
 
 
319
 
    QMutexLocker seekLock(&m_seekMutex);
320
 
    if (m_stopped) {
321
 
        return 0;
322
 
    }
323
 
    emit seekStreamQueued(offset); //calls syncSeekStream from the main thread
324
 
    m_seekWaitCondition.wait(&m_seekMutex); // waits until the seekStream signal returns
325
 
    return offset;
326
 
}
327
 
 
328
 
off_t ByteStream::currentPosition() const
329
 
{
330
 
    return m_currentPosition;
331
 
}
332
 
 
333
 
ByteStream::~ByteStream()
334
 
{
335
 
    Q_ASSERT(m_mainThread == pthread_self());
336
 
    PXINE_DEBUG;
337
 
}
338
 
 
339
 
QByteArray ByteStream::mrl() const
340
 
{
341
 
    QByteArray mrl("kbytestream:/");
342
 
    // the address can contain 0s which will null-terminate the C-string
343
 
    // use a simple encoding: 0x00 -> 0x0101, 0x01 -> 0x0102
344
 
    const ByteStream *iface = this;
345
 
    const unsigned char *that = reinterpret_cast<const unsigned char *>(&iface);
346
 
    for(unsigned int i = 0; i < sizeof(void *); ++i) {
347
 
        switch (that[i]) {
348
 
        case 0: // escape 0 as it terminates the string
349
 
            mrl += 0x01;
350
 
            mrl += 0x01;
351
 
            break;
352
 
        case 1: // escape 1 because it is used for escaping
353
 
            mrl += 0x01;
354
 
            mrl += 0x02;
355
 
            break;
356
 
        case '#': // escape # because xine splits the mrl at #s
357
 
            mrl += 0x01;
358
 
            mrl += 0x03;
359
 
            break;
360
 
        case '%': // escape % because xine will replace e.g. %20 with ' '
361
 
            mrl += 0x01;
362
 
            mrl += 0x04;
363
 
            break;
364
 
        default:
365
 
            mrl += that[i];
366
 
        }
367
 
    }
368
 
    mrl += '\0';
369
 
    return mrl;
370
 
}
371
 
 
372
 
void ByteStream::setStreamSize(qint64 x)
373
 
{
374
 
    PXINE_VDEBUG << x;
375
 
    QMutexLocker lock(&m_streamSizeMutex);
376
 
    m_streamSize = x;
377
 
    if (m_streamSize != 0) {
378
 
        emit needDataQueued();
379
 
        m_waitForStreamSize.wakeAll();
380
 
    }
381
 
}
382
 
 
383
 
void ByteStream::setPauseForBuffering(bool b)
384
 
{
385
 
    if (b) {
386
 
        QCoreApplication::postEvent(m_mediaObject->stream().data(), new QEVENT(PauseForBuffering));
387
 
        m_buffering = true;
388
 
    } else {
389
 
        QCoreApplication::postEvent(m_mediaObject->stream().data(), new QEVENT(UnpauseForBuffering));
390
 
        m_buffering = false;
391
 
    }
392
 
}
393
 
 
394
 
void ByteStream::endOfData()
395
 
{
396
 
    PXINE_DEBUG;
397
 
 
398
 
    m_mutex.lock();
399
 
    m_seekMutex.lock();
400
 
    m_streamSizeMutex.lock();
401
 
    m_eod = true;
402
 
    // don't reset the XineStream because many demuxers hit eod while trying to find the format of
403
 
    // the data
404
 
    // stream().setMrl(mrl());
405
 
    m_seekWaitCondition.wakeAll();
406
 
    m_seekMutex.unlock();
407
 
    m_waitingForData.wakeAll();
408
 
    m_mutex.unlock();
409
 
    m_waitForStreamSize.wakeAll();
410
 
    m_streamSizeMutex.unlock();
411
 
}
412
 
 
413
 
void ByteStream::setStreamSeekable(bool seekable)
414
 
{
415
 
    m_seekable = seekable;
416
 
}
417
 
 
418
 
void ByteStream::writeData(const QByteArray &data)
419
 
{
420
 
    if (data.size() <= 0) {
421
 
        return;
422
 
    }
423
 
 
424
 
    // first fill the preview buffer
425
 
    if (m_preview.size() != MAX_PREVIEW_SIZE) {
426
 
        PXINE_DEBUG << "fill preview";
427
 
        // more data than the preview buffer needs
428
 
        if (m_preview.size() + data.size() > MAX_PREVIEW_SIZE) {
429
 
            int tocopy = MAX_PREVIEW_SIZE - m_preview.size();
430
 
            m_preview += data.left(tocopy);
431
 
        } else { // all data fits into the preview buffer
432
 
            m_preview += data;
433
 
        }
434
 
 
435
 
        PXINE_VDEBUG << "filled preview buffer to " << m_preview.size();
436
 
    }
437
 
 
438
 
    PXINE_VDEBUG << data.size() << " m_streamSize = " << m_streamSize;
439
 
 
440
 
    QMutexLocker lock(&m_mutex);
441
 
    //debug() << Q_FUNC_INFO << "LOCKED m_mutex: ";
442
 
    m_buffers.enqueue(data);
443
 
    m_buffersize += data.size();
444
 
    PXINE_VDEBUG << "m_buffersize = " << m_buffersize;
445
 
    // FIXME accessing m_mediaObject is not threadsafe
446
 
    switch (m_mediaObject->state()) {
447
 
    case Phonon::BufferingState: // if nbc is buffering we want more data
448
 
    case Phonon::LoadingState: // if the preview is not ready we want me more data
449
 
        break;
450
 
    default:
451
 
        enoughData(); // else it's enough
452
 
    }
453
 
    m_waitingForData.wakeAll();
454
 
    //debug() << Q_FUNC_INFO << "UNLOCKING m_mutex: ";
455
 
}
456
 
 
457
 
void ByteStream::callStreamInterfaceReset()
458
 
{
459
 
    StreamInterface::reset();
460
 
}
461
 
 
462
 
void ByteStream::syncSeekStream(qint64 offset)
463
 
{
464
 
    PXINE_VDEBUG;
465
 
    m_seekMutex.lock();
466
 
    seekStream(offset);
467
 
    m_seekWaitCondition.wakeAll();
468
 
    m_seekMutex.unlock();
469
 
}
470
 
 
471
 
qint64 ByteStream::streamSize() const
472
 
{
473
 
    if (m_streamSize == 0) {
474
 
        // stream size has not been set yet
475
 
        QMutexLocker lock(&m_streamSizeMutex);
476
 
        if (m_streamSize == 0 && !m_eod) {
477
 
            m_waitForStreamSize.wait(&m_streamSizeMutex);
478
 
        }
479
 
    }
480
 
    return m_streamSize;
481
 
}
482
 
 
483
 
void ByteStream::stop()
484
 
{
485
 
    PXINE_VDEBUG;
486
 
 
487
 
    m_mutex.lock();
488
 
    m_seekMutex.lock();
489
 
    m_streamSizeMutex.lock();
490
 
    m_stopped = true;
491
 
    // the other thread is now not between m_mutex.lock() and m_waitingForData.wait(&m_mutex), so it
492
 
    // won't get stuck in m_waitingForData.wait if it's not there right now
493
 
    m_seekWaitCondition.wakeAll();
494
 
    m_seekMutex.unlock();
495
 
    m_waitingForData.wakeAll();
496
 
    m_mutex.unlock();
497
 
    m_waitForStreamSize.wakeAll();
498
 
    m_streamSizeMutex.unlock();
499
 
}
500
 
 
501
 
void ByteStream::reset()
502
 
{
503
 
    if (m_firstReset) {
504
 
        debug() << Q_FUNC_INFO << "first reset";
505
 
        m_firstReset = false;
506
 
        return;
507
 
    }
508
 
    emit resetQueued();
509
 
    m_currentPosition = 0;
510
 
    m_buffersize = 0;
511
 
    m_offset = 0;
512
 
    m_stopped = false;
513
 
    m_eod = false;
514
 
    m_buffering = false;
515
 
    if (m_streamSize != 0) {
516
 
        emit needDataQueued();
517
 
    }
518
 
}
519
 
 
520
 
}} //namespace Phonon::Xine
521
 
 
522
 
#include "bytestream.moc"
523
 
// vim: sw=4 ts=4