ARTPSession.cpp revision 39ddf8e0f18766f7ba1e3246b774aa6ebd93eea8
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 "ARTPSession.h"
18
19#include <media/stagefright/foundation/ABuffer.h>
20#include <media/stagefright/foundation/ADebug.h>
21#include <media/stagefright/foundation/AMessage.h>
22#include <media/stagefright/foundation/hexdump.h>
23
24#include <ctype.h>
25#include <arpa/inet.h>
26#include <sys/socket.h>
27
28#include "APacketSource.h"
29#include "ARTPConnection.h"
30#include "ASessionDescription.h"
31
32namespace android {
33
34ARTPSession::ARTPSession()
35    : mInitCheck(NO_INIT) {
36}
37
38status_t ARTPSession::setup(const sp<ASessionDescription> &desc) {
39    CHECK_EQ(mInitCheck, (status_t)NO_INIT);
40
41    mDesc = desc;
42
43    mRTPConn = new ARTPConnection;
44    looper()->registerHandler(mRTPConn);
45
46    for (size_t i = 1; i < mDesc->countTracks(); ++i) {
47        AString connection;
48        if (!mDesc->findAttribute(i, "c=", &connection)) {
49            // No per-stream connection information, try global fallback.
50            if (!mDesc->findAttribute(0, "c=", &connection)) {
51                LOG(ERROR) << "Unable to find connection attribtue.";
52                return mInitCheck;
53            }
54        }
55        if (!(connection == "IN IP4 127.0.0.1")) {
56            LOG(ERROR) << "We only support localhost connections for now.";
57            return mInitCheck;
58        }
59
60        unsigned port;
61        if (!validateMediaFormat(i, &port) || (port & 1) != 0) {
62            LOG(ERROR) << "Invalid media format.";
63            return mInitCheck;
64        }
65
66        sp<APacketSource> source = new APacketSource(mDesc, i);
67        if (source->initCheck() != OK) {
68            LOG(ERROR) << "Unsupported format.";
69            return mInitCheck;
70        }
71
72        int rtpSocket = MakeUDPSocket(port);
73        int rtcpSocket = MakeUDPSocket(port + 1);
74
75        mTracks.push(TrackInfo());
76        TrackInfo *info = &mTracks.editItemAt(mTracks.size() - 1);
77        info->mRTPSocket = rtpSocket;
78        info->mRTCPSocket = rtcpSocket;
79
80        sp<AMessage> notify = new AMessage(kWhatAccessUnitComplete, id());
81        notify->setSize("track-index", mTracks.size() - 1);
82
83        mRTPConn->addStream(rtpSocket, rtcpSocket, mDesc, i, notify);
84
85        info->mPacketSource = source;
86    }
87
88    mInitCheck = OK;
89
90    return OK;
91}
92
93// static
94int ARTPSession::MakeUDPSocket(unsigned port) {
95    int s = socket(AF_INET, SOCK_DGRAM, 0);
96    CHECK_GE(s, 0);
97
98    struct sockaddr_in addr;
99    memset(addr.sin_zero, 0, sizeof(addr.sin_zero));
100    addr.sin_family = AF_INET;
101    addr.sin_addr.s_addr = INADDR_ANY;
102    addr.sin_port = htons(port);
103
104    CHECK_EQ(0, bind(s, (const struct sockaddr *)&addr, sizeof(addr)));
105
106    return s;
107}
108
109ARTPSession::~ARTPSession() {
110    for (size_t i = 0; i < mTracks.size(); ++i) {
111        TrackInfo *info = &mTracks.editItemAt(i);
112
113        info->mPacketSource->signalEOS(UNKNOWN_ERROR);
114
115        close(info->mRTPSocket);
116        close(info->mRTCPSocket);
117    }
118}
119
120void ARTPSession::onMessageReceived(const sp<AMessage> &msg) {
121    switch (msg->what()) {
122        case kWhatAccessUnitComplete:
123        {
124            size_t trackIndex;
125            CHECK(msg->findSize("track-index", &trackIndex));
126
127            int32_t eos;
128            if (msg->findInt32("eos", &eos) && eos) {
129                TrackInfo *info = &mTracks.editItemAt(trackIndex);
130                info->mPacketSource->signalEOS(ERROR_END_OF_STREAM);
131                break;
132            }
133
134            sp<RefBase> obj;
135            CHECK(msg->findObject("access-unit", &obj));
136
137            sp<ABuffer> accessUnit = static_cast<ABuffer *>(obj.get());
138
139            uint64_t ntpTime;
140            CHECK(accessUnit->meta()->findInt64(
141                        "ntp-time", (int64_t *)&ntpTime));
142
143#if 0
144#if 0
145            printf("access unit complete size=%d\tntp-time=0x%016llx\n",
146                   accessUnit->size(), ntpTime);
147#else
148            LOG(INFO) << "access unit complete, "
149                      << "size=" << accessUnit->size() << ", "
150                      << "ntp-time=" << ntpTime;
151            hexdump(accessUnit->data(), accessUnit->size());
152#endif
153#endif
154
155#if 0
156            CHECK_GE(accessUnit->size(), 5u);
157            CHECK(!memcmp("\x00\x00\x00\x01", accessUnit->data(), 4));
158            unsigned x = accessUnit->data()[4];
159
160            LOG(INFO) << "access unit complete: "
161                      << StringPrintf("nalType=0x%02x, nalRefIdc=0x%02x",
162                                      x & 0x1f, (x & 0x60) >> 5);
163#endif
164
165            accessUnit->meta()->setInt64("ntp-time", ntpTime);
166
167#if 0
168            int32_t damaged;
169            if (accessUnit->meta()->findInt32("damaged", &damaged)
170                    && damaged != 0) {
171                LOG(INFO) << "ignoring damaged AU";
172            } else
173#endif
174            {
175                TrackInfo *info = &mTracks.editItemAt(trackIndex);
176                info->mPacketSource->queueAccessUnit(accessUnit);
177            }
178            break;
179        }
180
181        default:
182            TRESPASS();
183            break;
184    }
185}
186
187bool ARTPSession::validateMediaFormat(size_t index, unsigned *port) const {
188    AString format;
189    mDesc->getFormat(index, &format);
190
191    ssize_t i = format.find(" ");
192    if (i < 0) {
193        return false;
194    }
195
196    ++i;
197    size_t j = i;
198    while (isdigit(format.c_str()[j])) {
199        ++j;
200    }
201    if (format.c_str()[j] != ' ') {
202        return false;
203    }
204
205    AString portString(format, i, j - i);
206
207    char *end;
208    unsigned long x = strtoul(portString.c_str(), &end, 10);
209    if (end == portString.c_str() || *end != '\0') {
210        return false;
211    }
212
213    if (x == 0 || x > 65535) {
214        return false;
215    }
216
217    *port = x;
218
219    return true;
220}
221
222size_t ARTPSession::countTracks() {
223    return mTracks.size();
224}
225
226sp<MediaSource> ARTPSession::trackAt(size_t index) {
227    CHECK_LT(index, mTracks.size());
228    return mTracks.editItemAt(index).mPacketSource;
229}
230
231}  // namespace android
232