blob: 86437e067a71ef65b9f9f54d0e89ae442ba11ff2 [file] [log] [blame]
/*
* Copyright 2012, The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
//#define LOG_NEBUG 0
#define LOG_TAG "udptest"
#include <utils/Log.h>
#include "ANetworkSession.h"
#include <binder/ProcessState.h>
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/ADebug.h>
#include <media/stagefright/foundation/AHandler.h>
#include <media/stagefright/foundation/ALooper.h>
#include <media/stagefright/foundation/AMessage.h>
#include <media/stagefright/Utils.h>
namespace android {
struct TestHandler : public AHandler {
TestHandler(const sp<ANetworkSession> &netSession);
void startServer(unsigned localPort);
void startClient(const char *remoteHost, unsigned remotePort);
protected:
virtual ~TestHandler();
virtual void onMessageReceived(const sp<AMessage> &msg);
private:
enum {
kWhatStartServer,
kWhatStartClient,
kWhatUDPNotify,
kWhatSendPacket,
kWhatTimedOut,
};
struct TimeInfo {
int64_t mT1; // client timestamp at send
int64_t mT2; // server timestamp at receive
int64_t mT3; // server timestamp at send
int64_t mT4; // client timestamp at receive
};
static const int64_t kTimeoutDelayUs = 1000000ll;
sp<ANetworkSession> mNetSession;
bool mIsServer;
bool mConnected;
int32_t mUDPSession;
uint32_t mSeqNo;
double mTotalTimeUs;
int32_t mCount;
int64_t mSumOffsets;
int64_t mPendingT1;
int32_t mTimeoutGeneration;
void postSendPacket(int64_t delayUs = 0ll);
void postTimeout();
void cancelTimeout();
DISALLOW_EVIL_CONSTRUCTORS(TestHandler);
};
TestHandler::TestHandler(const sp<ANetworkSession> &netSession)
: mNetSession(netSession),
mIsServer(false),
mConnected(false),
mUDPSession(0),
mSeqNo(0),
mTotalTimeUs(0.0),
mCount(0),
mSumOffsets(0ll),
mPendingT1(0ll),
mTimeoutGeneration(0) {
}
TestHandler::~TestHandler() {
}
void TestHandler::startServer(unsigned localPort) {
sp<AMessage> msg = new AMessage(kWhatStartServer, id());
msg->setInt32("localPort", localPort);
msg->post();
}
void TestHandler::startClient(const char *remoteHost, unsigned remotePort) {
sp<AMessage> msg = new AMessage(kWhatStartClient, id());
msg->setString("remoteHost", remoteHost);
msg->setInt32("remotePort", remotePort);
msg->post();
}
void TestHandler::onMessageReceived(const sp<AMessage> &msg) {
switch (msg->what()) {
case kWhatStartClient:
{
AString remoteHost;
CHECK(msg->findString("remoteHost", &remoteHost));
int32_t remotePort;
CHECK(msg->findInt32("remotePort", &remotePort));
sp<AMessage> notify = new AMessage(kWhatUDPNotify, id());
CHECK_EQ((status_t)OK,
mNetSession->createUDPSession(
0 /* localPort */,
remoteHost.c_str(),
remotePort,
notify,
&mUDPSession));
postSendPacket();
break;
}
case kWhatStartServer:
{
mIsServer = true;
int32_t localPort;
CHECK(msg->findInt32("localPort", &localPort));
sp<AMessage> notify = new AMessage(kWhatUDPNotify, id());
CHECK_EQ((status_t)OK,
mNetSession->createUDPSession(
localPort, notify, &mUDPSession));
break;
}
case kWhatSendPacket:
{
TimeInfo ti;
memset(&ti, 0, sizeof(ti));
ti.mT1 = ALooper::GetNowUs();
CHECK_EQ((status_t)OK,
mNetSession->sendRequest(
mUDPSession, &ti, sizeof(ti)));
mPendingT1 = ti.mT1;
postTimeout();
break;
}
case kWhatTimedOut:
{
int32_t generation;
CHECK(msg->findInt32("generation", &generation));
if (generation != mTimeoutGeneration) {
break;
}
ALOGI("timed out, sending another request");
postSendPacket();
break;
}
case kWhatUDPNotify:
{
int32_t reason;
CHECK(msg->findInt32("reason", &reason));
switch (reason) {
case ANetworkSession::kWhatError:
{
int32_t sessionID;
CHECK(msg->findInt32("sessionID", &sessionID));
int32_t err;
CHECK(msg->findInt32("err", &err));
AString detail;
CHECK(msg->findString("detail", &detail));
ALOGE("An error occurred in session %d (%d, '%s/%s').",
sessionID,
err,
detail.c_str(),
strerror(-err));
mNetSession->destroySession(sessionID);
cancelTimeout();
looper()->stop();
break;
}
case ANetworkSession::kWhatDatagram:
{
int32_t sessionID;
CHECK(msg->findInt32("sessionID", &sessionID));
sp<ABuffer> packet;
CHECK(msg->findBuffer("data", &packet));
int64_t arrivalTimeUs;
CHECK(packet->meta()->findInt64(
"arrivalTimeUs", &arrivalTimeUs));
CHECK_EQ(packet->size(), sizeof(TimeInfo));
TimeInfo *ti = (TimeInfo *)packet->data();
if (mIsServer) {
if (!mConnected) {
AString fromAddr;
CHECK(msg->findString("fromAddr", &fromAddr));
int32_t fromPort;
CHECK(msg->findInt32("fromPort", &fromPort));
CHECK_EQ((status_t)OK,
mNetSession->connectUDPSession(
mUDPSession, fromAddr.c_str(), fromPort));
mConnected = true;
}
ti->mT2 = arrivalTimeUs;
ti->mT3 = ALooper::GetNowUs();
CHECK_EQ((status_t)OK,
mNetSession->sendRequest(
mUDPSession, ti, sizeof(*ti)));
} else {
if (ti->mT1 != mPendingT1) {
break;
}
cancelTimeout();
mPendingT1 = 0;
ti->mT4 = arrivalTimeUs;
// One way delay for a packet to travel from client
// to server or back (assumed to be the same either way).
int64_t delay =
(ti->mT2 - ti->mT1 + ti->mT4 - ti->mT3) / 2;
// Offset between the client clock (T1, T4) and the
// server clock (T2, T3) timestamps.
int64_t offset =
(ti->mT2 - ti->mT1 - ti->mT4 + ti->mT3) / 2;
mSumOffsets += offset;
++mCount;
printf("delay = %lld us,\toffset %lld us\n",
delay,
offset);
fflush(stdout);
postSendPacket(1000000ll / 30);
}
break;
}
default:
TRESPASS();
}
break;
}
default:
TRESPASS();
}
}
void TestHandler::postSendPacket(int64_t delayUs) {
(new AMessage(kWhatSendPacket, id()))->post(delayUs);
}
void TestHandler::postTimeout() {
sp<AMessage> msg = new AMessage(kWhatTimedOut, id());
msg->setInt32("generation", mTimeoutGeneration);
msg->post(kTimeoutDelayUs);
}
void TestHandler::cancelTimeout() {
++mTimeoutGeneration;
}
} // namespace android
static void usage(const char *me) {
fprintf(stderr,
"usage: %s -c host[:port]\tconnect to test server\n"
" -l \tcreate a test server\n",
me);
}
int main(int argc, char **argv) {
using namespace android;
ProcessState::self()->startThreadPool();
int32_t localPort = -1;
int32_t connectToPort = -1;
AString connectToHost;
int res;
while ((res = getopt(argc, argv, "hc:l:")) >= 0) {
switch (res) {
case 'c':
{
const char *colonPos = strrchr(optarg, ':');
if (colonPos == NULL) {
connectToHost = optarg;
connectToPort = 49152;
} else {
connectToHost.setTo(optarg, colonPos - optarg);
char *end;
connectToPort = strtol(colonPos + 1, &end, 10);
if (*end != '\0' || end == colonPos + 1
|| connectToPort < 1 || connectToPort > 65535) {
fprintf(stderr, "Illegal port specified.\n");
exit(1);
}
}
break;
}
case 'l':
{
char *end;
localPort = strtol(optarg, &end, 10);
if (*end != '\0' || end == optarg
|| localPort < 1 || localPort > 65535) {
fprintf(stderr, "Illegal port specified.\n");
exit(1);
}
break;
}
case '?':
case 'h':
usage(argv[0]);
exit(1);
}
}
if (localPort < 0 && connectToPort < 0) {
fprintf(stderr,
"You need to select either client or server mode.\n");
exit(1);
}
sp<ANetworkSession> netSession = new ANetworkSession;
netSession->start();
sp<ALooper> looper = new ALooper;
sp<TestHandler> handler = new TestHandler(netSession);
looper->registerHandler(handler);
if (localPort >= 0) {
handler->startServer(localPort);
} else {
handler->startClient(connectToHost.c_str(), connectToPort);
}
looper->start(true /* runOnCallingThread */);
return 0;
}