TimedTextPlayer.cpp revision 3254b25e8b0f674ccc2226609e01dd86a600802e
17a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang /*
26655174826330afe66ef766258181ae8c11f3f6cInsun Kang * Copyright (C) 2012 The Android Open Source Project
37a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang *
47a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang * Licensed under the Apache License, Version 2.0 (the "License");
57a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang * you may not use this file except in compliance with the License.
67a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang * You may obtain a copy of the License at
77a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang *
87a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang *      http://www.apache.org/licenses/LICENSE-2.0
97a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang *
107a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang * Unless required by applicable law or agreed to in writing, software
117a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang * distributed under the License is distributed on an "AS IS" BASIS,
127a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang * See the License for the specific language governing permissions and
147a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang * limitations under the License.
157a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang */
167a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
177a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang//#define LOG_NDEBUG 0
187a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang#define LOG_TAG "TimedTextPlayer"
197a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang#include <utils/Log.h>
207a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
21f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong#include <media/stagefright/foundation/ADebug.h>
226655174826330afe66ef766258181ae8c11f3f6cInsun Kang#include <media/stagefright/foundation/AMessage.h>
233254b25e8b0f674ccc2226609e01dd86a600802eInsun Kang#include <media/stagefright/timedtext/TimedTextDriver.h>
247a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang#include <media/stagefright/MediaErrors.h>
256655174826330afe66ef766258181ae8c11f3f6cInsun Kang#include <media/MediaPlayerInterface.h>
263b573f7bf1c5736d500e39013b8d32478a1429e6Gloria Wang
27965d08ba16ee82bc85f69546360c18e7da907406Gloria Wang#include "TimedTextPlayer.h"
287a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
296655174826330afe66ef766258181ae8c11f3f6cInsun Kang#include "TimedTextSource.h"
307a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
316655174826330afe66ef766258181ae8c11f3f6cInsun Kangnamespace android {
327a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
336655174826330afe66ef766258181ae8c11f3f6cInsun Kangstatic const int64_t kAdjustmentProcessingTimeUs = 100000ll;
347a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
356655174826330afe66ef766258181ae8c11f3f6cInsun KangTimedTextPlayer::TimedTextPlayer(const wp<MediaPlayerBase> &listener)
366655174826330afe66ef766258181ae8c11f3f6cInsun Kang    : mListener(listener),
376655174826330afe66ef766258181ae8c11f3f6cInsun Kang      mSource(NULL),
386655174826330afe66ef766258181ae8c11f3f6cInsun Kang      mSendSubtitleGeneration(0) {
397a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang}
407a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
417a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria WangTimedTextPlayer::~TimedTextPlayer() {
426655174826330afe66ef766258181ae8c11f3f6cInsun Kang    if (mSource != NULL) {
436655174826330afe66ef766258181ae8c11f3f6cInsun Kang        mSource->stop();
446655174826330afe66ef766258181ae8c11f3f6cInsun Kang        mSource.clear();
456655174826330afe66ef766258181ae8c11f3f6cInsun Kang        mSource = NULL;
467a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang    }
477a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang}
487a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
496655174826330afe66ef766258181ae8c11f3f6cInsun Kangvoid TimedTextPlayer::start() {
506655174826330afe66ef766258181ae8c11f3f6cInsun Kang    sp<AMessage> msg = new AMessage(kWhatSeek, id());
516655174826330afe66ef766258181ae8c11f3f6cInsun Kang    msg->setInt64("seekTimeUs", -1);
526655174826330afe66ef766258181ae8c11f3f6cInsun Kang    msg->post();
537a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang}
547a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
557a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wangvoid TimedTextPlayer::pause() {
566655174826330afe66ef766258181ae8c11f3f6cInsun Kang    (new AMessage(kWhatPause, id()))->post();
577a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang}
587a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
597a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wangvoid TimedTextPlayer::resume() {
606655174826330afe66ef766258181ae8c11f3f6cInsun Kang    start();
617a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang}
627a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
636655174826330afe66ef766258181ae8c11f3f6cInsun Kangvoid TimedTextPlayer::seekToAsync(int64_t timeUs) {
646655174826330afe66ef766258181ae8c11f3f6cInsun Kang    sp<AMessage> msg = new AMessage(kWhatSeek, id());
656655174826330afe66ef766258181ae8c11f3f6cInsun Kang    msg->setInt64("seekTimeUs", timeUs);
666655174826330afe66ef766258181ae8c11f3f6cInsun Kang    msg->post();
677a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang}
687a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
696655174826330afe66ef766258181ae8c11f3f6cInsun Kangvoid TimedTextPlayer::setDataSource(sp<TimedTextSource> source) {
706655174826330afe66ef766258181ae8c11f3f6cInsun Kang    sp<AMessage> msg = new AMessage(kWhatSetSource, id());
716655174826330afe66ef766258181ae8c11f3f6cInsun Kang    msg->setObject("source", source);
726655174826330afe66ef766258181ae8c11f3f6cInsun Kang    msg->post();
737a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang}
747a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
756655174826330afe66ef766258181ae8c11f3f6cInsun Kangvoid TimedTextPlayer::onMessageReceived(const sp<AMessage> &msg) {
766655174826330afe66ef766258181ae8c11f3f6cInsun Kang    switch (msg->what()) {
776655174826330afe66ef766258181ae8c11f3f6cInsun Kang        case kWhatPause: {
786655174826330afe66ef766258181ae8c11f3f6cInsun Kang            mSendSubtitleGeneration++;
796655174826330afe66ef766258181ae8c11f3f6cInsun Kang            break;
80965d08ba16ee82bc85f69546360c18e7da907406Gloria Wang        }
816655174826330afe66ef766258181ae8c11f3f6cInsun Kang        case kWhatSeek: {
826655174826330afe66ef766258181ae8c11f3f6cInsun Kang            int64_t seekTimeUs = 0;
836655174826330afe66ef766258181ae8c11f3f6cInsun Kang            msg->findInt64("seekTimeUs", &seekTimeUs);
846655174826330afe66ef766258181ae8c11f3f6cInsun Kang            if (seekTimeUs < 0) {
856655174826330afe66ef766258181ae8c11f3f6cInsun Kang                sp<MediaPlayerBase> listener = mListener.promote();
866655174826330afe66ef766258181ae8c11f3f6cInsun Kang                if (listener != NULL) {
876655174826330afe66ef766258181ae8c11f3f6cInsun Kang                    int32_t positionMs = 0;
886655174826330afe66ef766258181ae8c11f3f6cInsun Kang                    listener->getCurrentPosition(&positionMs);
896655174826330afe66ef766258181ae8c11f3f6cInsun Kang                    seekTimeUs = positionMs * 1000ll;
906655174826330afe66ef766258181ae8c11f3f6cInsun Kang                }
916655174826330afe66ef766258181ae8c11f3f6cInsun Kang            }
926655174826330afe66ef766258181ae8c11f3f6cInsun Kang            doSeekAndRead(seekTimeUs);
936655174826330afe66ef766258181ae8c11f3f6cInsun Kang            break;
946655174826330afe66ef766258181ae8c11f3f6cInsun Kang        }
956655174826330afe66ef766258181ae8c11f3f6cInsun Kang        case kWhatSendSubtitle: {
966655174826330afe66ef766258181ae8c11f3f6cInsun Kang            int32_t generation;
976655174826330afe66ef766258181ae8c11f3f6cInsun Kang            CHECK(msg->findInt32("generation", &generation));
986655174826330afe66ef766258181ae8c11f3f6cInsun Kang            if (generation != mSendSubtitleGeneration) {
996655174826330afe66ef766258181ae8c11f3f6cInsun Kang              // Drop obsolete msg.
1006655174826330afe66ef766258181ae8c11f3f6cInsun Kang              break;
1016655174826330afe66ef766258181ae8c11f3f6cInsun Kang            }
1026655174826330afe66ef766258181ae8c11f3f6cInsun Kang            sp<RefBase> obj;
1036655174826330afe66ef766258181ae8c11f3f6cInsun Kang            msg->findObject("subtitle", &obj);
1046655174826330afe66ef766258181ae8c11f3f6cInsun Kang            if (obj != NULL) {
1056655174826330afe66ef766258181ae8c11f3f6cInsun Kang                sp<ParcelEvent> parcelEvent;
1066655174826330afe66ef766258181ae8c11f3f6cInsun Kang                parcelEvent = static_cast<ParcelEvent*>(obj.get());
1076655174826330afe66ef766258181ae8c11f3f6cInsun Kang                notifyListener(MEDIA_TIMED_TEXT, &(parcelEvent->parcel));
1086655174826330afe66ef766258181ae8c11f3f6cInsun Kang            } else {
1096655174826330afe66ef766258181ae8c11f3f6cInsun Kang                notifyListener(MEDIA_TIMED_TEXT);
1106655174826330afe66ef766258181ae8c11f3f6cInsun Kang            }
1116655174826330afe66ef766258181ae8c11f3f6cInsun Kang            doRead();
1126655174826330afe66ef766258181ae8c11f3f6cInsun Kang            break;
1136655174826330afe66ef766258181ae8c11f3f6cInsun Kang        }
1146655174826330afe66ef766258181ae8c11f3f6cInsun Kang        case kWhatSetSource: {
1156655174826330afe66ef766258181ae8c11f3f6cInsun Kang            sp<RefBase> obj;
1166655174826330afe66ef766258181ae8c11f3f6cInsun Kang            msg->findObject("source", &obj);
1176655174826330afe66ef766258181ae8c11f3f6cInsun Kang            if (obj == NULL) break;
1186655174826330afe66ef766258181ae8c11f3f6cInsun Kang            if (mSource != NULL) {
1196655174826330afe66ef766258181ae8c11f3f6cInsun Kang                mSource->stop();
1206655174826330afe66ef766258181ae8c11f3f6cInsun Kang            }
1216655174826330afe66ef766258181ae8c11f3f6cInsun Kang            mSource = static_cast<TimedTextSource*>(obj.get());
1226655174826330afe66ef766258181ae8c11f3f6cInsun Kang            mSource->start();
1236655174826330afe66ef766258181ae8c11f3f6cInsun Kang            Parcel parcel;
1246655174826330afe66ef766258181ae8c11f3f6cInsun Kang            if (mSource->extractGlobalDescriptions(&parcel) == OK &&
1256655174826330afe66ef766258181ae8c11f3f6cInsun Kang                parcel.dataSize() > 0) {
1266655174826330afe66ef766258181ae8c11f3f6cInsun Kang                notifyListener(MEDIA_TIMED_TEXT, &parcel);
1276655174826330afe66ef766258181ae8c11f3f6cInsun Kang            } else {
1286655174826330afe66ef766258181ae8c11f3f6cInsun Kang                notifyListener(MEDIA_TIMED_TEXT);
1296655174826330afe66ef766258181ae8c11f3f6cInsun Kang            }
1306655174826330afe66ef766258181ae8c11f3f6cInsun Kang            break;
1313b573f7bf1c5736d500e39013b8d32478a1429e6Gloria Wang        }
1327a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang    }
1337a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang}
1347a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
1356655174826330afe66ef766258181ae8c11f3f6cInsun Kangvoid TimedTextPlayer::doSeekAndRead(int64_t seekTimeUs) {
1366655174826330afe66ef766258181ae8c11f3f6cInsun Kang    MediaSource::ReadOptions options;
1376655174826330afe66ef766258181ae8c11f3f6cInsun Kang    options.setSeekTo(seekTimeUs, MediaSource::ReadOptions::SEEK_PREVIOUS_SYNC);
1386655174826330afe66ef766258181ae8c11f3f6cInsun Kang    doRead(&options);
1397a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang}
1407a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
1416655174826330afe66ef766258181ae8c11f3f6cInsun Kangvoid TimedTextPlayer::doRead(MediaSource::ReadOptions* options) {
1426655174826330afe66ef766258181ae8c11f3f6cInsun Kang    int64_t timeUs = 0;
1436655174826330afe66ef766258181ae8c11f3f6cInsun Kang    sp<ParcelEvent> parcelEvent = new ParcelEvent();
1446655174826330afe66ef766258181ae8c11f3f6cInsun Kang    mSource->read(&timeUs, &(parcelEvent->parcel), options);
1456655174826330afe66ef766258181ae8c11f3f6cInsun Kang    postTextEvent(parcelEvent, timeUs);
1467a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang}
1477a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang
1486655174826330afe66ef766258181ae8c11f3f6cInsun Kangvoid TimedTextPlayer::postTextEvent(const sp<ParcelEvent>& parcel, int64_t timeUs) {
1496655174826330afe66ef766258181ae8c11f3f6cInsun Kang    sp<MediaPlayerBase> listener = mListener.promote();
1506655174826330afe66ef766258181ae8c11f3f6cInsun Kang    if (listener != NULL) {
1516655174826330afe66ef766258181ae8c11f3f6cInsun Kang        int64_t positionUs, delayUs;
1526655174826330afe66ef766258181ae8c11f3f6cInsun Kang        int32_t positionMs = 0;
1536655174826330afe66ef766258181ae8c11f3f6cInsun Kang        listener->getCurrentPosition(&positionMs);
1546655174826330afe66ef766258181ae8c11f3f6cInsun Kang        positionUs = positionMs * 1000;
155965d08ba16ee82bc85f69546360c18e7da907406Gloria Wang
1566655174826330afe66ef766258181ae8c11f3f6cInsun Kang        if (timeUs <= positionUs + kAdjustmentProcessingTimeUs) {
1576655174826330afe66ef766258181ae8c11f3f6cInsun Kang            delayUs = 0;
158965d08ba16ee82bc85f69546360c18e7da907406Gloria Wang        } else {
1596655174826330afe66ef766258181ae8c11f3f6cInsun Kang            delayUs = timeUs - positionUs - kAdjustmentProcessingTimeUs;
160965d08ba16ee82bc85f69546360c18e7da907406Gloria Wang        }
1616655174826330afe66ef766258181ae8c11f3f6cInsun Kang        sp<AMessage> msg = new AMessage(kWhatSendSubtitle, id());
1626655174826330afe66ef766258181ae8c11f3f6cInsun Kang        msg->setInt32("generation", mSendSubtitleGeneration);
1636655174826330afe66ef766258181ae8c11f3f6cInsun Kang        if (parcel != NULL) {
1646655174826330afe66ef766258181ae8c11f3f6cInsun Kang            msg->setObject("subtitle", parcel);
1657a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang        }
1666655174826330afe66ef766258181ae8c11f3f6cInsun Kang        msg->post(delayUs);
1677a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang    }
1687a1e3e81264189e23a1db2b174e1b5a5d4c7d1c3Gloria Wang}
1693b573f7bf1c5736d500e39013b8d32478a1429e6Gloria Wang
1706655174826330afe66ef766258181ae8c11f3f6cInsun Kangvoid TimedTextPlayer::notifyListener(int msg, const Parcel *parcel) {
1716655174826330afe66ef766258181ae8c11f3f6cInsun Kang    sp<MediaPlayerBase> listener = mListener.promote();
1726655174826330afe66ef766258181ae8c11f3f6cInsun Kang    if (listener != NULL) {
1736655174826330afe66ef766258181ae8c11f3f6cInsun Kang        if (parcel != NULL && (parcel->dataSize() > 0)) {
1746655174826330afe66ef766258181ae8c11f3f6cInsun Kang            listener->sendEvent(msg, 0, 0, parcel);
1756655174826330afe66ef766258181ae8c11f3f6cInsun Kang        } else {  // send an empty timed text to clear the screen
1766655174826330afe66ef766258181ae8c11f3f6cInsun Kang            listener->sendEvent(msg);
1773b573f7bf1c5736d500e39013b8d32478a1429e6Gloria Wang        }
1783b573f7bf1c5736d500e39013b8d32478a1429e6Gloria Wang    }
1793b573f7bf1c5736d500e39013b8d32478a1429e6Gloria Wang}
1803b573f7bf1c5736d500e39013b8d32478a1429e6Gloria Wang
1816655174826330afe66ef766258181ae8c11f3f6cInsun Kang}  // namespace android
182