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