Home | History | Annotate | Download | only in rtsp
      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 
     25 namespace android {
     26 
     27 static 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 
     34 ARTPAssembler::ARTPAssembler()
     35     : mFirstFailureTimeUs(-1) {
     36 }
     37 
     38 void 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
     67 void ARTPAssembler::CopyTimes(const sp<ABuffer> &to, const sp<ABuffer> &from) {
     68     uint32_t rtpTime;
     69     CHECK(from->meta()->findInt32("rtp-time", (int32_t *)&rtpTime));
     70 
     71     to->meta()->setInt32("rtp-time", rtpTime);
     72 
     73     // Copy the seq number.
     74     to->setInt32Data(from->int32Data());
     75 }
     76 
     77 // static
     78 sp<ABuffer> ARTPAssembler::MakeADTSCompoundFromAACFrames(
     79         unsigned profile,
     80         unsigned samplingFreqIndex,
     81         unsigned channelConfig,
     82         const List<sp<ABuffer> > &frames) {
     83     size_t totalSize = 0;
     84     for (List<sp<ABuffer> >::const_iterator it = frames.begin();
     85          it != frames.end(); ++it) {
     86         // Each frame is prefixed by a 7 byte ADTS header
     87         totalSize += (*it)->size() + 7;
     88     }
     89 
     90     sp<ABuffer> accessUnit = new ABuffer(totalSize);
     91     size_t offset = 0;
     92     for (List<sp<ABuffer> >::const_iterator it = frames.begin();
     93          it != frames.end(); ++it) {
     94         sp<ABuffer> nal = *it;
     95         uint8_t *dst = accessUnit->data() + offset;
     96 
     97         static const unsigned kADTSId = 0;
     98         static const unsigned kADTSLayer = 0;
     99         static const unsigned kADTSProtectionAbsent = 1;
    100 
    101         unsigned frameLength = nal->size() + 7;
    102 
    103         dst[0] = 0xff;
    104 
    105         dst[1] =
    106             0xf0 | (kADTSId << 3) | (kADTSLayer << 1) | kADTSProtectionAbsent;
    107 
    108         dst[2] = (profile << 6)
    109                 | (samplingFreqIndex << 2)
    110                 | (channelConfig >> 2);
    111 
    112         dst[3] = ((channelConfig & 3) << 6) | (frameLength >> 11);
    113 
    114         dst[4] = (frameLength >> 3) & 0xff;
    115         dst[5] = (frameLength & 7) << 5;
    116         dst[6] = 0x00;
    117 
    118         memcpy(dst + 7, nal->data(), nal->size());
    119         offset += nal->size() + 7;
    120     }
    121 
    122     CopyTimes(accessUnit, *frames.begin());
    123 
    124     return accessUnit;
    125 }
    126 
    127 // static
    128 sp<ABuffer> ARTPAssembler::MakeCompoundFromPackets(
    129         const List<sp<ABuffer> > &packets) {
    130     size_t totalSize = 0;
    131     for (List<sp<ABuffer> >::const_iterator it = packets.begin();
    132          it != packets.end(); ++it) {
    133         totalSize += (*it)->size();
    134     }
    135 
    136     sp<ABuffer> accessUnit = new ABuffer(totalSize);
    137     size_t offset = 0;
    138     for (List<sp<ABuffer> >::const_iterator it = packets.begin();
    139          it != packets.end(); ++it) {
    140         sp<ABuffer> nal = *it;
    141         memcpy(accessUnit->data() + offset, nal->data(), nal->size());
    142         offset += nal->size();
    143     }
    144 
    145     CopyTimes(accessUnit, *packets.begin());
    146 
    147     return accessUnit;
    148 }
    149 
    150 }  // namespace android
    151