1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef TIMED_TEXT_SRT_SOURCE_H_
18#define TIMED_TEXT_SRT_SOURCE_H_
19
20#include <media/stagefright/MediaErrors.h>
21#include <media/stagefright/MediaSource.h>
22#include <utils/Compat.h>  // off64_t
23
24#include "TimedTextSource.h"
25
26namespace android {
27
28class AString;
29class DataSource;
30class MediaBuffer;
31class Parcel;
32
33class TimedTextSRTSource : public TimedTextSource {
34public:
35    TimedTextSRTSource(const sp<DataSource>& dataSource);
36    virtual status_t start();
37    virtual status_t stop();
38    virtual status_t read(
39            int64_t *startTimeUs,
40            int64_t *endTimeUs,
41            Parcel *parcel,
42            const MediaSource::ReadOptions *options = NULL);
43    virtual sp<MetaData> getFormat();
44
45protected:
46    virtual ~TimedTextSRTSource();
47
48private:
49    sp<DataSource> mSource;
50    sp<MetaData> mMetaData;
51
52    struct TextInfo {
53        int64_t endTimeUs;
54        // The offset of the text in the original file.
55        off64_t offset;
56        int textLen;
57    };
58
59    size_t mIndex;
60    KeyedVector<int64_t, TextInfo> mTextVector;
61
62    void reset();
63    status_t scanFile();
64    status_t getNextSubtitleInfo(
65            off64_t *offset, int64_t *startTimeUs, TextInfo *info);
66    status_t readNextLine(off64_t *offset, AString *data);
67    status_t getText(
68            const MediaSource::ReadOptions *options,
69            AString *text, int64_t *startTimeUs, int64_t *endTimeUs);
70    status_t extractAndAppendLocalDescriptions(
71            int64_t timeUs, const AString &text, Parcel *parcel);
72
73    // Compares the time range of the subtitle at index to the given timeUs.
74    // The time range of the subtitle to match with given timeUs is extended to
75    // [endTimeUs of the previous subtitle, endTimeUs of current subtitle).
76    //
77    // This compare function is used to find a next subtitle when read() is
78    // called with seek options. Note that timeUs within gap ranges, such as
79    // [200, 300) in the below example, will be matched to the closest future
80    // subtitle, [300, 400).
81    //
82    // For instance, assuming there are 3 subtitles in mTextVector,
83    // 0: [100, 200)      ----> [0, 200)
84    // 1: [300, 400)      ----> [200, 400)
85    // 2: [500, 600)      ----> [400, 600)
86    // If the 'index' parameter contains 1, this function
87    // returns 0, if timeUs is in [200, 400)
88    // returns -1, if timeUs >= 400,
89    // returns 1, if timeUs < 200.
90    int compareExtendedRangeAndTime(size_t index, int64_t timeUs);
91
92    DISALLOW_EVIL_CONSTRUCTORS(TimedTextSRTSource);
93};
94
95}  // namespace android
96
97#endif  // TIMED_TEXT_SRT_SOURCE_H_
98