Home | History | Annotate | Download | only in libstagefright
      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 <media/stagefright/AudioSource.h>
     18 
     19 #include <media/AudioRecord.h>
     20 #include <media/stagefright/MediaBufferGroup.h>
     21 #include <media/stagefright/MediaDebug.h>
     22 #include <media/stagefright/MediaDefs.h>
     23 #include <media/stagefright/MetaData.h>
     24 
     25 namespace android {
     26 
     27 AudioSource::AudioSource(
     28         int inputSource, uint32_t sampleRate, uint32_t channels)
     29     : mRecord(new AudioRecord(
     30                 inputSource, sampleRate, AudioSystem::PCM_16_BIT, channels)),
     31       mInitCheck(mRecord->initCheck()),
     32       mStarted(false),
     33       mGroup(NULL) {
     34 }
     35 
     36 AudioSource::~AudioSource() {
     37     if (mStarted) {
     38         stop();
     39     }
     40 
     41     delete mRecord;
     42     mRecord = NULL;
     43 }
     44 
     45 status_t AudioSource::initCheck() const {
     46     return mInitCheck;
     47 }
     48 
     49 status_t AudioSource::start(MetaData *params) {
     50     if (mStarted) {
     51         return UNKNOWN_ERROR;
     52     }
     53 
     54     status_t err = mRecord->start();
     55 
     56     if (err == OK) {
     57         mGroup = new MediaBufferGroup;
     58         mGroup->add_buffer(new MediaBuffer(kMaxBufferSize));
     59 
     60         mStarted = true;
     61     }
     62 
     63     return err;
     64 }
     65 
     66 status_t AudioSource::stop() {
     67     if (!mStarted) {
     68         return UNKNOWN_ERROR;
     69     }
     70 
     71     mRecord->stop();
     72 
     73     delete mGroup;
     74     mGroup = NULL;
     75 
     76     mStarted = false;
     77 
     78     return OK;
     79 }
     80 
     81 sp<MetaData> AudioSource::getFormat() {
     82     sp<MetaData> meta = new MetaData;
     83     meta->setCString(kKeyMIMEType, MEDIA_MIMETYPE_AUDIO_RAW);
     84     meta->setInt32(kKeySampleRate, mRecord->getSampleRate());
     85     meta->setInt32(kKeyChannelCount, mRecord->channelCount());
     86     meta->setInt32(kKeyMaxInputSize, kMaxBufferSize);
     87 
     88     return meta;
     89 }
     90 
     91 status_t AudioSource::read(
     92         MediaBuffer **out, const ReadOptions *options) {
     93     *out = NULL;
     94 
     95     MediaBuffer *buffer;
     96     CHECK_EQ(mGroup->acquire_buffer(&buffer), OK);
     97 
     98     uint32_t numFramesRecorded;
     99     mRecord->getPosition(&numFramesRecorded);
    100 
    101     buffer->meta_data()->setInt64(
    102             kKeyTime,
    103             (1000000ll * numFramesRecorded) / mRecord->getSampleRate()
    104             - mRecord->latency() * 1000);
    105 
    106     ssize_t n = mRecord->read(buffer->data(), buffer->size());
    107 
    108     if (n < 0) {
    109         buffer->release();
    110         buffer = NULL;
    111 
    112         return (status_t)n;
    113     }
    114 
    115     buffer->set_range(0, n);
    116 
    117     *out = buffer;
    118 
    119     return OK;
    120 }
    121 
    122 }  // namespace android
    123