MediaPuller.cpp revision e1957358f11031a554c57d4fb46988dd6044acc1
1/* 2 * Copyright 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 "MediaPuller" 19#include <utils/Log.h> 20 21#include "MediaPuller.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/MediaBuffer.h> 27#include <media/stagefright/MediaSource.h> 28#include <media/stagefright/MetaData.h> 29 30namespace android { 31 32MediaPuller::MediaPuller( 33 const sp<MediaSource> &source, const sp<AMessage> ¬ify) 34 : mSource(source), 35 mNotify(notify), 36 mPullGeneration(0) { 37} 38 39MediaPuller::~MediaPuller() { 40} 41 42status_t MediaPuller::postSynchronouslyAndReturnError( 43 const sp<AMessage> &msg) { 44 sp<AMessage> response; 45 status_t err = msg->postAndAwaitResponse(&response); 46 47 if (err != OK) { 48 return err; 49 } 50 51 if (!response->findInt32("err", &err)) { 52 err = OK; 53 } 54 55 return err; 56} 57 58status_t MediaPuller::start() { 59 return postSynchronouslyAndReturnError(new AMessage(kWhatStart, id())); 60} 61 62status_t MediaPuller::stop() { 63 return postSynchronouslyAndReturnError(new AMessage(kWhatStop, id())); 64} 65 66void MediaPuller::onMessageReceived(const sp<AMessage> &msg) { 67 switch (msg->what()) { 68 case kWhatStart: 69 case kWhatStop: 70 { 71 status_t err; 72 73 if (msg->what() == kWhatStart) { 74 err = mSource->start(); 75 76 if (err == OK) { 77 schedulePull(); 78 } 79 } else { 80 err = mSource->stop(); 81 ++mPullGeneration; 82 } 83 84 sp<AMessage> response = new AMessage; 85 response->setInt32("err", err); 86 87 uint32_t replyID; 88 CHECK(msg->senderAwaitsResponse(&replyID)); 89 90 response->postReply(replyID); 91 break; 92 } 93 94 case kWhatPull: 95 { 96 int32_t generation; 97 CHECK(msg->findInt32("generation", &generation)); 98 99 if (generation != mPullGeneration) { 100 break; 101 } 102 103 MediaBuffer *mbuf; 104 status_t err = mSource->read(&mbuf); 105 106 if (err != OK) { 107 if (err == ERROR_END_OF_STREAM) { 108 ALOGI("stream ended."); 109 } else { 110 ALOGE("error %d reading stream.", err); 111 } 112 113 sp<AMessage> notify = mNotify->dup(); 114 notify->setInt32("what", kWhatEOS); 115 notify->post(); 116 } else { 117 int64_t timeUs; 118 CHECK(mbuf->meta_data()->findInt64(kKeyTime, &timeUs)); 119 120 sp<ABuffer> accessUnit = new ABuffer(mbuf->range_length()); 121 122 memcpy(accessUnit->data(), 123 (const uint8_t *)mbuf->data() + mbuf->range_offset(), 124 mbuf->range_length()); 125 126 accessUnit->meta()->setInt64("timeUs", timeUs); 127 accessUnit->meta()->setPointer("mediaBuffer", mbuf); 128 129 sp<AMessage> notify = mNotify->dup(); 130 131 notify->setInt32("what", kWhatAccessUnit); 132 notify->setBuffer("accessUnit", accessUnit); 133 notify->post(); 134 135 schedulePull(); 136 } 137 break; 138 } 139 140 default: 141 TRESPASS(); 142 } 143} 144 145void MediaPuller::schedulePull() { 146 sp<AMessage> msg = new AMessage(kWhatPull, id()); 147 msg->setInt32("generation", mPullGeneration); 148 msg->post(); 149} 150 151} // namespace android 152 153