summaryrefslogtreecommitdiffstats
path: root/media/libstagefright/timedtext/TimedTextPlayer.cpp
blob: 7c8a747c518f707658c0eec6b60699876041384a (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
 /*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

//#define LOG_NDEBUG 0
#define LOG_TAG "TimedTextPlayer"
#include <utils/Log.h>

#include <binder/IPCThreadState.h>

#include <media/stagefright/MediaDebug.h>
#include <media/stagefright/MediaDefs.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/MediaSource.h>
#include <media/stagefright/MetaData.h>
#include <media/stagefright/MediaBuffer.h>
#include <media/stagefright/FileSource.h>
#include <media/stagefright/Utils.h>

#include "include/AwesomePlayer.h"
#include "TimedTextPlayer.h"
#include "TimedTextParser.h"
#include "TextDescriptions.h"

namespace android {

struct TimedTextEvent : public TimedEventQueue::Event {
    TimedTextEvent(
            TimedTextPlayer *player,
            void (TimedTextPlayer::*method)())
        : mPlayer(player),
          mMethod(method) {
    }

protected:
    virtual ~TimedTextEvent() {}

    virtual void fire(TimedEventQueue *queue, int64_t /* now_us */) {
        (mPlayer->*mMethod)();
    }

private:
    TimedTextPlayer *mPlayer;
    void (TimedTextPlayer::*mMethod)();

    TimedTextEvent(const TimedTextEvent &);
    TimedTextEvent &operator=(const TimedTextEvent &);
};

TimedTextPlayer::TimedTextPlayer(
        AwesomePlayer *observer,
        const wp<MediaPlayerBase> &listener,
        TimedEventQueue *queue)
    : mSource(NULL),
      mOutOfBandSource(NULL),
      mSeekTimeUs(0),
      mStarted(false),
      mTextEventPending(false),
      mQueue(queue),
      mListener(listener),
      mObserver(observer),
      mTextBuffer(NULL),
      mTextParser(NULL),
      mTextType(kNoText) {
    mTextEvent = new TimedTextEvent(this, &TimedTextPlayer::onTextEvent);
}

TimedTextPlayer::~TimedTextPlayer() {
    if (mStarted) {
        reset();
    }

    mTextTrackVector.clear();
    mTextOutOfBandVector.clear();
}

status_t TimedTextPlayer::start(uint8_t index) {
    CHECK(!mStarted);

    if (index >=
            mTextTrackVector.size() + mTextOutOfBandVector.size()) {
        LOGE("Incorrect text track index: %d", index);
        return BAD_VALUE;
    }

    status_t err;
    if (index < mTextTrackVector.size()) { // start an in-band text
        mSource = mTextTrackVector.itemAt(index);

        err = mSource->start();

        if (err != OK) {
            return err;
        }
        mTextType = kInBandText;
    } else { // start an out-of-band text
        OutOfBandText text =
            mTextOutOfBandVector.itemAt(index - mTextTrackVector.size());

        mOutOfBandSource = text.source;
        TimedTextParser::FileType fileType = text.type;

        if (mTextParser == NULL) {
            mTextParser = new TimedTextParser();
        }

        if ((err = mTextParser->init(mOutOfBandSource, fileType)) != OK) {
            return err;
        }
        mTextType = kOutOfBandText;
    }

    // send sample description format
    if ((err = extractAndSendGlobalDescriptions()) != OK) {
        return err;
    }

    int64_t positionUs;
    mObserver->getPosition(&positionUs);
    seekTo(positionUs);

    postTextEvent();

    mStarted = true;

    return OK;
}

void TimedTextPlayer::pause() {
    CHECK(mStarted);

    cancelTextEvent();
}

void TimedTextPlayer::resume() {
    CHECK(mStarted);

    postTextEvent();
}

void TimedTextPlayer::reset() {
    CHECK(mStarted);

    // send an empty text to clear the screen
    notifyListener(MEDIA_TIMED_TEXT);

    cancelTextEvent();

    mSeeking = false;
    mStarted = false;

    if (mTextType == kInBandText) {
        if (mTextBuffer != NULL) {
            mTextBuffer->release();
            mTextBuffer = NULL;
        }

        if (mSource != NULL) {
            mSource->stop();
            mSource.clear();
            mSource = NULL;
        }
    } else {
        if (mTextParser != NULL) {
            mTextParser.clear();
            mTextParser = NULL;
        }
        if (mOutOfBandSource != NULL) {
            mOutOfBandSource.clear();
            mOutOfBandSource = NULL;
        }
    }
}

status_t TimedTextPlayer::seekTo(int64_t time_us) {
    Mutex::Autolock autoLock(mLock);

    mSeeking = true;
    mSeekTimeUs = time_us;

    postTextEvent();

    return OK;
}

status_t TimedTextPlayer::setTimedTextTrackIndex(int32_t index) {
    if (index >=
            (int)(mTextTrackVector.size() + mTextOutOfBandVector.size())) {
        return BAD_VALUE;
    }

    if (mStarted) {
        reset();
    }

    if (index >= 0) {
        return start(index);
    }
    return OK;
}

void TimedTextPlayer::onTextEvent() {
    Mutex::Autolock autoLock(mLock);

    if (!mTextEventPending) {
        return;
    }
    mTextEventPending = false;

    if (mData.dataSize() > 0) {
        notifyListener(MEDIA_TIMED_TEXT, &mData);
        mData.freeData();
    }

    MediaSource::ReadOptions options;
    if (mSeeking) {
        options.setSeekTo(mSeekTimeUs,
                MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
        mSeeking = false;

        notifyListener(MEDIA_TIMED_TEXT); //empty text to clear the screen
    }

    int64_t positionUs, timeUs;
    mObserver->getPosition(&positionUs);

    if (mTextType == kInBandText) {
        if (mSource->read(&mTextBuffer, &options) != OK) {
            return;
        }

        mTextBuffer->meta_data()->findInt64(kKeyTime, &timeUs);
    } else {
        int64_t endTimeUs;
        if (mTextParser->getText(
                    &mText, &timeUs, &endTimeUs, &options) != OK) {
            return;
        }
    }

    if (timeUs > 0) {
        extractAndAppendLocalDescriptions(timeUs);
    }

    if (mTextType == kInBandText) {
        if (mTextBuffer != NULL) {
            mTextBuffer->release();
            mTextBuffer = NULL;
        }
    } else {
        mText.clear();
    }

    //send the text now
    if (timeUs <= positionUs + 100000ll) {
        postTextEvent();
    } else {
        postTextEvent(timeUs - positionUs - 100000ll);
    }
}

void TimedTextPlayer::postTextEvent(int64_t delayUs) {
    if (mTextEventPending) {
        return;
    }

    mTextEventPending = true;
    mQueue->postEventWithDelay(mTextEvent, delayUs < 0 ? 10000 : delayUs);
}

void TimedTextPlayer::cancelTextEvent() {
    mQueue->cancelEvent(mTextEvent->eventID());
    mTextEventPending = false;
}

void TimedTextPlayer::addTextSource(sp<MediaSource> source) {
    Mutex::Autolock autoLock(mLock);
    mTextTrackVector.add(source);
}

status_t TimedTextPlayer::setParameter(int key, const Parcel &request) {
    Mutex::Autolock autoLock(mLock);

    if (key == KEY_PARAMETER_TIMED_TEXT_ADD_OUT_OF_BAND_SOURCE) {
        const String16 uri16 = request.readString16();
        String8 uri = String8(uri16);
        KeyedVector<String8, String8> headers;

        // To support local subtitle file only for now
        if (strncasecmp("file://", uri.string(), 7)) {
            return INVALID_OPERATION;
        }
        sp<DataSource> dataSource =
            DataSource::CreateFromURI(uri, &headers);
        status_t err = dataSource->initCheck();

        if (err != OK) {
            return err;
        }

        OutOfBandText text;
        text.source = dataSource;
        if (uri.getPathExtension() == String8(".srt")) {
            text.type = TimedTextParser::OUT_OF_BAND_FILE_SRT;
        } else {
            return ERROR_UNSUPPORTED;
        }

        mTextOutOfBandVector.add(text);

        return OK;
    }
    return INVALID_OPERATION;
}

void TimedTextPlayer::notifyListener(int msg, const Parcel *parcel) {
    if (mListener != NULL) {
        sp<MediaPlayerBase> listener = mListener.promote();

        if (listener != NULL) {
            if (parcel && (parcel->dataSize() > 0)) {
                listener->sendEvent(msg, 0, 0, parcel);
            } else { // send an empty timed text to clear the screen
                listener->sendEvent(msg);
            }
        }
    }
}

// Each text sample consists of a string of text, optionally with sample
// modifier description. The modifier description could specify a new
// text style for the string of text. These descriptions are present only
// if they are needed. This method is used to extract the modifier
// description and append it at the end of the text.
status_t TimedTextPlayer::extractAndAppendLocalDescriptions(int64_t timeUs) {
    const void *data;
    size_t size = 0;
    int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;

    if (mTextType == kInBandText) {
        const char *mime;
        CHECK(mSource->getFormat()->findCString(kKeyMIMEType, &mime));

        if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
            flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
            data = mTextBuffer->data();
            size = mTextBuffer->size();
        } else {
            // support 3GPP only for now
            return ERROR_UNSUPPORTED;
        }
    } else {
        data = mText.c_str();
        size = mText.size();
        flag |= TextDescriptions::OUT_OF_BAND_TEXT_SRT;
    }

    if ((size > 0) && (flag != TextDescriptions::LOCAL_DESCRIPTIONS)) {
        mData.freeData();
        return TextDescriptions::getParcelOfDescriptions(
                (const uint8_t *)data, size, flag, timeUs / 1000, &mData);
    }

    return OK;
}

// To extract and send the global text descriptions for all the text samples
// in the text track or text file.
status_t TimedTextPlayer::extractAndSendGlobalDescriptions() {
    const void *data;
    size_t size = 0;
    int32_t flag = TextDescriptions::GLOBAL_DESCRIPTIONS;

    if (mTextType == kInBandText) {
        const char *mime;
        CHECK(mSource->getFormat()->findCString(kKeyMIMEType, &mime));

        // support 3GPP only for now
        if (!strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP)) {
            uint32_t type;
            // get the 'tx3g' box content. This box contains the text descriptions
            // used to render the text track
            if (!mSource->getFormat()->findData(
                        kKeyTextFormatData, &type, &data, &size)) {
                return ERROR_MALFORMED;
            }

            flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
        }
    }

    if ((size > 0) && (flag != TextDescriptions::GLOBAL_DESCRIPTIONS)) {
        Parcel parcel;
        if (TextDescriptions::getParcelOfDescriptions(
                (const uint8_t *)data, size, flag, 0, &parcel) == OK) {
            if (parcel.dataSize() > 0) {
                notifyListener(MEDIA_TIMED_TEXT, &parcel);
            }
        }
    }

    return OK;
}
}