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