TimedText3GPPSource.cpp revision f9d660a5e0196240add5daf0199f128d471e592c
16655174826330afe66ef766258181ae8c11f3f6cInsun Kang /*
26655174826330afe66ef766258181ae8c11f3f6cInsun Kang * Copyright (C) 2012 The Android Open Source Project
36655174826330afe66ef766258181ae8c11f3f6cInsun Kang *
46655174826330afe66ef766258181ae8c11f3f6cInsun Kang * Licensed under the Apache License, Version 2.0 (the "License");
56655174826330afe66ef766258181ae8c11f3f6cInsun Kang * you may not use this file except in compliance with the License.
66655174826330afe66ef766258181ae8c11f3f6cInsun Kang * You may obtain a copy of the License at
76655174826330afe66ef766258181ae8c11f3f6cInsun Kang *
86655174826330afe66ef766258181ae8c11f3f6cInsun Kang *      http://www.apache.org/licenses/LICENSE-2.0
96655174826330afe66ef766258181ae8c11f3f6cInsun Kang *
106655174826330afe66ef766258181ae8c11f3f6cInsun Kang * Unless required by applicable law or agreed to in writing, software
116655174826330afe66ef766258181ae8c11f3f6cInsun Kang * distributed under the License is distributed on an "AS IS" BASIS,
126655174826330afe66ef766258181ae8c11f3f6cInsun Kang * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
136655174826330afe66ef766258181ae8c11f3f6cInsun Kang * See the License for the specific language governing permissions and
146655174826330afe66ef766258181ae8c11f3f6cInsun Kang * limitations under the License.
156655174826330afe66ef766258181ae8c11f3f6cInsun Kang */
166655174826330afe66ef766258181ae8c11f3f6cInsun Kang
176655174826330afe66ef766258181ae8c11f3f6cInsun Kang//#define LOG_NDEBUG 0
1825eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang#define LOG_TAG "TimedText3GPPSource"
196655174826330afe66ef766258181ae8c11f3f6cInsun Kang#include <utils/Log.h>
206655174826330afe66ef766258181ae8c11f3f6cInsun Kang
216655174826330afe66ef766258181ae8c11f3f6cInsun Kang#include <binder/Parcel.h>
22f1d5aa162c02a16b7195a43a9bcea4d592600ac4James Dong#include <media/stagefright/foundation/ADebug.h>  // CHECK_XX macro
236655174826330afe66ef766258181ae8c11f3f6cInsun Kang#include <media/stagefright/MediaBuffer.h>
246655174826330afe66ef766258181ae8c11f3f6cInsun Kang#include <media/stagefright/MediaDefs.h>  // for MEDIA_MIMETYPE_xxx
256655174826330afe66ef766258181ae8c11f3f6cInsun Kang#include <media/stagefright/MediaErrors.h>
266655174826330afe66ef766258181ae8c11f3f6cInsun Kang#include <media/stagefright/MediaSource.h>
276655174826330afe66ef766258181ae8c11f3f6cInsun Kang#include <media/stagefright/MetaData.h>
286655174826330afe66ef766258181ae8c11f3f6cInsun Kang
2925eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang#include "TimedText3GPPSource.h"
306655174826330afe66ef766258181ae8c11f3f6cInsun Kang#include "TextDescriptions.h"
316655174826330afe66ef766258181ae8c11f3f6cInsun Kang
326655174826330afe66ef766258181ae8c11f3f6cInsun Kangnamespace android {
336655174826330afe66ef766258181ae8c11f3f6cInsun Kang
3425eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun KangTimedText3GPPSource::TimedText3GPPSource(const sp<MediaSource>& mediaSource)
356655174826330afe66ef766258181ae8c11f3f6cInsun Kang    : mSource(mediaSource) {
366655174826330afe66ef766258181ae8c11f3f6cInsun Kang}
376655174826330afe66ef766258181ae8c11f3f6cInsun Kang
3825eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun KangTimedText3GPPSource::~TimedText3GPPSource() {
396655174826330afe66ef766258181ae8c11f3f6cInsun Kang}
406655174826330afe66ef766258181ae8c11f3f6cInsun Kang
4125eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kangstatus_t TimedText3GPPSource::read(
426655174826330afe66ef766258181ae8c11f3f6cInsun Kang        int64_t *timeUs, Parcel *parcel, const MediaSource::ReadOptions *options) {
436655174826330afe66ef766258181ae8c11f3f6cInsun Kang    MediaBuffer *textBuffer = NULL;
446655174826330afe66ef766258181ae8c11f3f6cInsun Kang    status_t err = mSource->read(&textBuffer, options);
456655174826330afe66ef766258181ae8c11f3f6cInsun Kang    if (err != OK) {
466655174826330afe66ef766258181ae8c11f3f6cInsun Kang        return err;
476655174826330afe66ef766258181ae8c11f3f6cInsun Kang    }
486655174826330afe66ef766258181ae8c11f3f6cInsun Kang    CHECK(textBuffer != NULL);
496655174826330afe66ef766258181ae8c11f3f6cInsun Kang    textBuffer->meta_data()->findInt64(kKeyTime, timeUs);
506655174826330afe66ef766258181ae8c11f3f6cInsun Kang    // TODO: this is legacy code. when 'timeUs' can be <= 0?
516655174826330afe66ef766258181ae8c11f3f6cInsun Kang    if (*timeUs > 0) {
526655174826330afe66ef766258181ae8c11f3f6cInsun Kang        extractAndAppendLocalDescriptions(*timeUs, textBuffer, parcel);
536655174826330afe66ef766258181ae8c11f3f6cInsun Kang    }
546655174826330afe66ef766258181ae8c11f3f6cInsun Kang    textBuffer->release();
556655174826330afe66ef766258181ae8c11f3f6cInsun Kang    return OK;
566655174826330afe66ef766258181ae8c11f3f6cInsun Kang}
576655174826330afe66ef766258181ae8c11f3f6cInsun Kang
586655174826330afe66ef766258181ae8c11f3f6cInsun Kang// Each text sample consists of a string of text, optionally with sample
596655174826330afe66ef766258181ae8c11f3f6cInsun Kang// modifier description. The modifier description could specify a new
606655174826330afe66ef766258181ae8c11f3f6cInsun Kang// text style for the string of text. These descriptions are present only
616655174826330afe66ef766258181ae8c11f3f6cInsun Kang// if they are needed. This method is used to extract the modifier
626655174826330afe66ef766258181ae8c11f3f6cInsun Kang// description and append it at the end of the text.
6325eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kangstatus_t TimedText3GPPSource::extractAndAppendLocalDescriptions(
646655174826330afe66ef766258181ae8c11f3f6cInsun Kang        int64_t timeUs, const MediaBuffer *textBuffer, Parcel *parcel) {
656655174826330afe66ef766258181ae8c11f3f6cInsun Kang    const void *data;
666655174826330afe66ef766258181ae8c11f3f6cInsun Kang    size_t size = 0;
676655174826330afe66ef766258181ae8c11f3f6cInsun Kang    int32_t flag = TextDescriptions::LOCAL_DESCRIPTIONS;
686655174826330afe66ef766258181ae8c11f3f6cInsun Kang
696655174826330afe66ef766258181ae8c11f3f6cInsun Kang    const char *mime;
706655174826330afe66ef766258181ae8c11f3f6cInsun Kang    CHECK(mSource->getFormat()->findCString(kKeyMIMEType, &mime));
7125eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    CHECK(strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP) == 0);
726655174826330afe66ef766258181ae8c11f3f6cInsun Kang
7325eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    data = textBuffer->data();
7425eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    size = textBuffer->size();
7525eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang
7625eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    if (size > 0) {
7725eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang      parcel->freeData();
7825eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang      flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
7925eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang      return TextDescriptions::getParcelOfDescriptions(
8025eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang          (const uint8_t *)data, size, flag, timeUs / 1000, parcel);
816655174826330afe66ef766258181ae8c11f3f6cInsun Kang    }
8225eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    return OK;
836655174826330afe66ef766258181ae8c11f3f6cInsun Kang}
846655174826330afe66ef766258181ae8c11f3f6cInsun Kang
856655174826330afe66ef766258181ae8c11f3f6cInsun Kang// To extract and send the global text descriptions for all the text samples
866655174826330afe66ef766258181ae8c11f3f6cInsun Kang// in the text track or text file.
876655174826330afe66ef766258181ae8c11f3f6cInsun Kang// TODO: send error message to application via notifyListener()...?
8825eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kangstatus_t TimedText3GPPSource::extractGlobalDescriptions(Parcel *parcel) {
896655174826330afe66ef766258181ae8c11f3f6cInsun Kang    const void *data;
906655174826330afe66ef766258181ae8c11f3f6cInsun Kang    size_t size = 0;
916655174826330afe66ef766258181ae8c11f3f6cInsun Kang    int32_t flag = TextDescriptions::GLOBAL_DESCRIPTIONS;
926655174826330afe66ef766258181ae8c11f3f6cInsun Kang
936655174826330afe66ef766258181ae8c11f3f6cInsun Kang    const char *mime;
946655174826330afe66ef766258181ae8c11f3f6cInsun Kang    CHECK(mSource->getFormat()->findCString(kKeyMIMEType, &mime));
9525eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    CHECK(strcasecmp(mime, MEDIA_MIMETYPE_TEXT_3GPP) == 0);
9625eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang
9725eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    uint32_t type;
9825eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    // get the 'tx3g' box content. This box contains the text descriptions
9925eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    // used to render the text track
10025eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    if (!mSource->getFormat()->findData(
10125eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang            kKeyTextFormatData, &type, &data, &size)) {
10225eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang        return ERROR_MALFORMED;
10325eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    }
1046655174826330afe66ef766258181ae8c11f3f6cInsun Kang
10525eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    if (size > 0) {
10625eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang        flag |= TextDescriptions::IN_BAND_TEXT_3GPP;
10725eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang        return TextDescriptions::getParcelOfDescriptions(
10825eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang                (const uint8_t *)data, size, flag, 0, parcel);
1096655174826330afe66ef766258181ae8c11f3f6cInsun Kang    }
11025eefbedf4b0ba6aea38605f4ba19c75a40810d1Insun Kang    return OK;
1116655174826330afe66ef766258181ae8c11f3f6cInsun Kang}
1126655174826330afe66ef766258181ae8c11f3f6cInsun Kang
113f9d660a5e0196240add5daf0199f128d471e592cInsun Kangsp<MetaData> TimedText3GPPSource::getFormat() {
114f9d660a5e0196240add5daf0199f128d471e592cInsun Kang    return mSource->getFormat();
115f9d660a5e0196240add5daf0199f128d471e592cInsun Kang}
116f9d660a5e0196240add5daf0199f128d471e592cInsun Kang
1176655174826330afe66ef766258181ae8c11f3f6cInsun Kang}  // namespace android
118