netwerk/protocol/rtsp/rtsp/ARawAudioAssembler.cpp

Fri, 16 Jan 2015 04:50:19 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Fri, 16 Jan 2015 04:50:19 +0100
branch
TOR_BUG_9701
changeset 13
44a2da4a2ab2
permissions
-rw-r--r--

Replace accessor implementation with direct member state manipulation, by
request https://trac.torproject.org/projects/tor/ticket/9701#comment:32

     1 /*
     2  * Copyright (C) 2011 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  */
    17 #define LOG_TAG "ARawAudioAssembler"
    18 #include "RtspPrlog.h"
    20 #include "ARawAudioAssembler.h"
    22 #include "ARTPSource.h"
    23 #include "ASessionDescription.h"
    25 #include <media/stagefright/foundation/ABuffer.h>
    26 #include <media/stagefright/foundation/ADebug.h>
    27 #include <media/stagefright/foundation/AMessage.h>
    28 #include <media/stagefright/foundation/hexdump.h>
    29 #include <media/stagefright/MediaDefs.h>
    30 #include <media/stagefright/MetaData.h>
    31 #include <media/stagefright/Utils.h>
    33 namespace android {
    35 ARawAudioAssembler::ARawAudioAssembler(
    36         const sp<AMessage> &notify, const char *desc, const AString &params)
    37     : mNotifyMsg(notify),
    38       mNextExpectedSeqNoValid(false),
    39       mNextExpectedSeqNo(0) {
    40 }
    42 ARawAudioAssembler::~ARawAudioAssembler() {
    43 }
    45 ARTPAssembler::AssemblyStatus ARawAudioAssembler::assembleMore(
    46         const sp<ARTPSource> &source) {
    47     return addPacket(source);
    48 }
    50 ARTPAssembler::AssemblyStatus ARawAudioAssembler::addPacket(
    51         const sp<ARTPSource> &source) {
    52     List<sp<ABuffer> > *queue = source->queue();
    54     if (queue->empty()) {
    55         return NOT_ENOUGH_DATA;
    56     }
    58     if (mNextExpectedSeqNoValid) {
    59         List<sp<ABuffer> >::iterator it = queue->begin();
    60         while (it != queue->end()) {
    61             if ((uint32_t)(*it)->int32Data() >= mNextExpectedSeqNo) {
    62                 break;
    63             }
    65             it = queue->erase(it);
    66         }
    68         if (queue->empty()) {
    69             return NOT_ENOUGH_DATA;
    70         }
    71     }
    73     sp<ABuffer> buffer = *queue->begin();
    75     if (!mNextExpectedSeqNoValid) {
    76         mNextExpectedSeqNoValid = true;
    77         mNextExpectedSeqNo = (uint32_t)buffer->int32Data();
    78     } else if ((uint32_t)buffer->int32Data() != mNextExpectedSeqNo) {
    79         LOGV("Not the sequence number I expected");
    81         return WRONG_SEQUENCE_NUMBER;
    82     }
    84     // hexdump(buffer->data(), buffer->size());
    86     if (buffer->size() < 1) {
    87         queue->erase(queue->begin());
    88         ++mNextExpectedSeqNo;
    90         LOGV("raw audio packet too short.");
    92         return MALFORMED_PACKET;
    93     }
    95     sp<AMessage> msg = mNotifyMsg->dup();
    96     msg->setObject("access-unit", buffer);
    97     msg->post();
    99     queue->erase(queue->begin());
   100     ++mNextExpectedSeqNo;
   102     return OK;
   103 }
   105 void ARawAudioAssembler::packetLost() {
   106     CHECK(mNextExpectedSeqNoValid);
   107     ++mNextExpectedSeqNo;
   108 }
   110 void ARawAudioAssembler::onByeReceived() {
   111     sp<AMessage> msg = mNotifyMsg->dup();
   112     msg->setInt32("eos", true);
   113     msg->post();
   114 }
   116 // static
   117 bool ARawAudioAssembler::Supports(const char *desc) {
   118     return !strncmp(desc, "PCMU/", 5)
   119         || !strncmp(desc, "PCMA/", 5);
   120 }
   122 // static
   123 void ARawAudioAssembler::MakeFormat(
   124         const char *desc, const sp<MetaData> &format) {
   125     if (!strncmp(desc, "PCMU/", 5)) {
   126         format->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_G711_MLAW);
   127     } else if (!strncmp(desc, "PCMA/", 5)) {
   128         format->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_G711_ALAW);
   129     } else {
   130         TRESPASS();
   131     }
   133     int32_t sampleRate, numChannels;
   134     ASessionDescription::ParseFormatDesc(
   135             desc, &sampleRate, &numChannels);
   137     format->setInt32(kKeySampleRate, sampleRate);
   138     format->setInt32(kKeyChannelCount, numChannels);
   139 }
   141 }  // namespace android

mercurial