1343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih/*
2343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * Copyright (C) 2014 The Android Open Source Project
3343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih *
4343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * Licensed under the Apache License, Version 2.0 (the "License");
5343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * you may not use this file except in compliance with the License.
6343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * You may obtain a copy of the License at
7343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih *
8343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih *      http://www.apache.org/licenses/LICENSE-2.0
9343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih *
10343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * Unless required by applicable law or agreed to in writing, software
11343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * distributed under the License is distributed on an "AS IS" BASIS,
12343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * See the License for the specific language governing permissions and
14343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih * limitations under the License.
15343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih */
16343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
17343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih//#define LOG_NDEBUG 0
18343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#define LOG_TAG "WebmFrame"
19343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
20343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "WebmFrame.h"
21343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include "WebmConstants.h"
22343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
23343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <media/stagefright/foundation/ADebug.h>
24343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih#include <unistd.h>
25343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
26343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihusing namespace android;
27343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihusing namespace webm;
28343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
29343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihnamespace {
30343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihsp<ABuffer> toABuffer(MediaBuffer *mbuf) {
31343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    sp<ABuffer> abuf = new ABuffer(mbuf->range_length());
32343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    memcpy(abuf->data(), (uint8_t*) mbuf->data() + mbuf->range_offset(), mbuf->range_length());
33343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    return abuf;
34343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih}
35343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih}
36343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
37343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihnamespace android {
38343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
39343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihconst sp<WebmFrame> WebmFrame::EOS = new WebmFrame();
40343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
41343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihWebmFrame::WebmFrame()
42343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    : mType(kInvalidType),
43343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih      mKey(false),
44343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih      mAbsTimecode(UINT64_MAX),
45343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih      mData(new ABuffer(0)),
46343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih      mEos(true) {
47343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih}
48343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
49343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert ShihWebmFrame::WebmFrame(int type, bool key, uint64_t absTimecode, MediaBuffer *mbuf)
50343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    : mType(type),
51343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih      mKey(key),
52343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih      mAbsTimecode(absTimecode),
53343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih      mData(toABuffer(mbuf)),
54343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih      mEos(false) {
55343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih}
56343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
57343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihsp<WebmElement> WebmFrame::SimpleBlock(uint64_t baseTimecode) const {
58343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    return new WebmSimpleBlock(
59343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih            mType == kVideoType ? kVideoTrackNum : kAudioTrackNum,
60343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih            mAbsTimecode - baseTimecode,
61343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih            mKey,
62343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih            mData);
63343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih}
64343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih
65343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shihbool WebmFrame::operator<(const WebmFrame &other) const {
66343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    if (this->mEos) {
67343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih        return false;
68343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    }
69343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    if (other.mEos) {
70343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih        return true;
71343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    }
72343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    if (this->mAbsTimecode == other.mAbsTimecode) {
73343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih        if (this->mType == kAudioType && other.mType == kVideoType) {
74343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih            return true;
75343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih        }
76343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih        if (this->mType == kVideoType && other.mType == kAudioType) {
77343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih            return false;
78343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih        }
79343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih        return false;
80343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    }
81343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih    return this->mAbsTimecode < other.mAbsTimecode;
82343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih}
83343947abc8b7c126f966fd32a0b18bff6c2cecd1Robert Shih} /* namespace android */
84