190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber/*
290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber * Copyright 2012, The Android Open Source Project
390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber *
490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber * you may not use this file except in compliance with the License.
690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber * You may obtain a copy of the License at
790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber *
890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber *     http://www.apache.org/licenses/LICENSE-2.0
990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber *
1090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber * Unless required by applicable law or agreed to in writing, software
1190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
1290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber * See the License for the specific language governing permissions and
1490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber * limitations under the License.
1590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber */
1690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
1790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber//#define LOG_NDEBUG 0
1890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#define LOG_TAG "Sender"
1990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#include <utils/Log.h>
2090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
2190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#include "Sender.h"
2290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
2390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#include "ANetworkSession.h"
2490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
2590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#include <media/stagefright/foundation/ABuffer.h>
2690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#include <media/stagefright/foundation/ADebug.h>
2790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#include <media/stagefright/foundation/AMessage.h>
2890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#include <media/stagefright/foundation/hexdump.h>
2990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#include <media/stagefright/MediaErrors.h>
3090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#include <media/stagefright/Utils.h>
3190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
3290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#include <math.h>
3390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
3490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#define DEBUG_JITTER    0
3590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
3690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Hubernamespace android {
3790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
3890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber////////////////////////////////////////////////////////////////////////////////
3990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
4090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#if DEBUG_JITTER
4190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huberstruct TimeSeries {
4290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    TimeSeries();
4390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
4490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    void add(double val);
4590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
4690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    double mean() const;
4790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    double sdev() const;
4890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
4990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huberprivate:
5090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    enum {
5190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        kHistorySize = 20
5290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    };
5390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    double mValues[kHistorySize];
5490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
5590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    size_t mCount;
5690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    double mSum;
5790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber};
5890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
5990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas HuberTimeSeries::TimeSeries()
6090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    : mCount(0),
6190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mSum(0.0) {
6290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber}
6390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
6490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Hubervoid TimeSeries::add(double val) {
6590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    if (mCount < kHistorySize) {
6690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        mValues[mCount++] = val;
6790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        mSum += val;
6890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    } else {
6990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        mSum -= mValues[0];
7090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        memmove(&mValues[0], &mValues[1], (kHistorySize - 1) * sizeof(double));
7190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        mValues[kHistorySize - 1] = val;
7290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        mSum += val;
7390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    }
7490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber}
7590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
7690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huberdouble TimeSeries::mean() const {
7790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    if (mCount < 1) {
7890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        return 0.0;
7990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    }
8090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
8190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    return mSum / mCount;
8290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber}
8390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
8490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huberdouble TimeSeries::sdev() const {
8590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    if (mCount < 1) {
8690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        return 0.0;
8790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    }
8890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
8990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    double m = mean();
9090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
9190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    double sum = 0.0;
9290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    for (size_t i = 0; i < mCount; ++i) {
9390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        double tmp = mValues[i] - m;
9490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        tmp *= tmp;
9590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
9690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        sum += tmp;
9790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    }
9890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
9990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    return sqrt(sum / mCount);
10090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber}
10190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#endif  // DEBUG_JITTER
10290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
10390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber////////////////////////////////////////////////////////////////////////////////
10490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
10590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huberstatic size_t kMaxRTPPacketSize = 1500;
10690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huberstatic size_t kMaxNumTSPacketsPerRTPPacket = (kMaxRTPPacketSize - 12) / 188;
10790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber
10890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas HuberSender::Sender(
10990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        const sp<ANetworkSession> &netSession,
11090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber        const sp<AMessage> &notify)
11190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber    : mNetSession(netSession),
11290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mNotify(notify),
11390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mTSQueue(new ABuffer(12 + kMaxNumTSPacketsPerRTPPacket * 188)),
11490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mTransportMode(TRANSPORT_UDP),
11590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mRTPChannel(0),
11690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mRTCPChannel(0),
11790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mRTPPort(0),
11890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mRTPSessionID(0),
11990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mRTCPSessionID(0),
12090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#if ENABLE_RETRANSMISSION
12190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mRTPRetransmissionSessionID(0),
12290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mRTCPRetransmissionSessionID(0),
12390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#endif
12490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mClientRTPPort(0),
12590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mClientRTCPPort(0),
12690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mRTPConnected(false),
12790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mRTCPConnected(false),
12890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mFirstOutputBufferReadyTimeUs(-1ll),
12990a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mFirstOutputBufferSentTimeUs(-1ll),
13090a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mRTPSeqNo(0),
13190a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#if ENABLE_RETRANSMISSION
13290a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mRTPRetransmissionSeqNo(0),
13390a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber#endif
13490a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mLastNTPTime(0),
13590a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mLastRTPTime(0),
13690a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mNumRTPSent(0),
13790a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mNumRTPOctetsSent(0),
13890a92053219ae50ddf4bb54e3d54db2d309e2b8dAndreas Huber      mNumSRsSent(0),
139