1 /* 2 * Copyright (C) 2012 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 "AMPEG2TSAssembler" 19 #include <utils/Log.h> 20 21 #include "AMPEG2TSAssembler.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 AMPEG2TSAssembler::AMPEG2TSAssembler( 37 const sp<AMessage> ¬ify, const char *desc, const AString ¶ms) 38 : mNotifyMsg(notify), 39 mNextExpectedSeqNoValid(false), 40 mNextExpectedSeqNo(0) { 41 } 42 43 AMPEG2TSAssembler::~AMPEG2TSAssembler() { 44 } 45 46 ARTPAssembler::AssemblyStatus AMPEG2TSAssembler::assembleMore( 47 const sp<ARTPSource> &source) { 48 return addPacket(source); 49 } 50 51 ARTPAssembler::AssemblyStatus AMPEG2TSAssembler::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 ALOGV("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() % 188) > 0) { 88 queue->erase(queue->begin()); 89 ++mNextExpectedSeqNo; 90 91 ALOGV("Not a multiple of transport packet size."); 92 93 return MALFORMED_PACKET; 94 } 95 96 sp<AMessage> msg = mNotifyMsg->dup(); 97 msg->setBuffer("access-unit", buffer); 98 msg->post(); 99 100 queue->erase(queue->begin()); 101 ++mNextExpectedSeqNo; 102 103 return OK; 104 } 105 106 void AMPEG2TSAssembler::packetLost() { 107 CHECK(mNextExpectedSeqNoValid); 108 ++mNextExpectedSeqNo; 109 } 110 111 void AMPEG2TSAssembler::onByeReceived() { 112 sp<AMessage> msg = mNotifyMsg->dup(); 113 msg->setInt32("eos", true); 114 msg->post(); 115 } 116 117 } // namespace android 118 119 120