ARTPAssembler.cpp revision eeb97d91b97f1fc0b26815f098515e9c06d219b8
1/*
2 * Copyright (C) 2010 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#include "ARTPAssembler.h"
18
19#include <media/stagefright/foundation/ABuffer.h>
20#include <media/stagefright/foundation/ADebug.h>
21#include <media/stagefright/foundation/AMessage.h>
22
23#include <stdint.h>
24
25namespace android {
26
27static int64_t getNowUs() {
28    struct timeval tv;
29    gettimeofday(&tv, NULL);
30
31    return (int64_t)tv.tv_usec + tv.tv_sec * 1000000ll;
32}
33
34ARTPAssembler::ARTPAssembler()
35    : mFirstFailureTimeUs(-1) {
36}
37
38void ARTPAssembler::onPacketReceived(const sp<ARTPSource> &source) {
39    AssemblyStatus status;
40    for (;;) {
41        status = assembleMore(source);
42
43        if (status == WRONG_SEQUENCE_NUMBER) {
44            if (mFirstFailureTimeUs >= 0) {
45                if (getNowUs() - mFirstFailureTimeUs > 10000ll) {
46                    mFirstFailureTimeUs = -1;
47
48                    // LOG(VERBOSE) << "waited too long for packet.";
49                    packetLost();
50                    continue;
51                }
52            } else {
53                mFirstFailureTimeUs = getNowUs();
54            }
55            break;
56        } else {
57            mFirstFailureTimeUs = -1;
58
59            if (status == NOT_ENOUGH_DATA) {
60                break;
61            }
62        }
63    }
64}
65
66// static
67void ARTPAssembler::CopyTimes(const sp<ABuffer> &to, const sp<ABuffer> &from) {
68    uint64_t ntpTime;
69    CHECK(from->meta()->findInt64("ntp-time", (int64_t *)&ntpTime));
70
71    uint32_t rtpTime;
72    CHECK(from->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
73
74    to->meta()->setInt64("ntp-time", ntpTime);
75    to->meta()->setInt32("rtp-time", rtpTime);
76
77    // Copy the seq number.
78    to->setInt32Data(from->int32Data());
79}
80
81}  // namespace android
82