Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 53d4e0d5 authored by James Dong's avatar James Dong
Browse files

Allows the authoring engine to skip frame.

This is 1st part of the work to allow audio and video resync if
we found out that audio and video are out of sync during authoring

- also fixed a problem in AACEncoder::read() where the buffer acquired
  from the buffer group does not release when error out at
  reading from source.

Change-Id: I8a2740097fcfdf85e6178869afeb9f3687a99118
parent db3eb355
Loading
Loading
Loading
Loading
+13 −0
Original line number Diff line number Diff line
@@ -78,18 +78,31 @@ struct MediaSource : public RefBase {
        void clearSeekTo();
        bool getSeekTo(int64_t *time_us, SeekMode *mode) const;

        // Option allows encoder to skip some frames until the specified
        // time stamp.
        // To prevent from being abused, when the skipFrame timestamp is
        // found to be more than 1 second later than the current timestamp,
        // an error will be returned from read().
        void clearSkipFrame();
        bool getSkipFrame(int64_t *timeUs) const;
        void setSkipFrame(int64_t timeUs);

        void setLateBy(int64_t lateness_us);
        int64_t getLateBy() const;

    private:
        enum Options {
            // Bit map
            kSeekTo_Option      = 1,
            kSkipFrame_Option   = 2,
        };

        uint32_t mOptions;
        int64_t mSeekTimeUs;
        SeekMode mSeekMode;
        int64_t mLatenessUs;

        int64_t mSkipFrameUntilTimeUs;
    };

    // Causes this source to suspend pulling data from its upstream source
+1 −0
Original line number Diff line number Diff line
@@ -143,6 +143,7 @@ private:
    int64_t mSeekTimeUs;
    ReadOptions::SeekMode mSeekMode;
    int64_t mTargetTimeUs;
    int64_t mSkipTimeUs;

    MediaBuffer *mLeftOverBuffer;

+59 −36
Original line number Diff line number Diff line
@@ -137,11 +137,13 @@ status_t AudioSource::read(
    MediaBuffer *buffer;
    CHECK_EQ(mGroup->acquire_buffer(&buffer), OK);

    while (mStarted) {
        uint32_t numFramesRecorded;
        mRecord->getPosition(&numFramesRecorded);
        int64_t latency = mRecord->latency() * 1000;

        int64_t readTime = systemTime() / 1000;

        if (numFramesRecorded == 0) {
            // Initial delay
            if (mStartTimeUs > 0) {
@@ -170,12 +172,31 @@ status_t AudioSource::read(
            return (status_t)n;
        }

        uint32_t sampleRate = mRecord->getSampleRate();
        int64_t timestampUs = (1000000LL * numFramesRecorded) / sampleRate +
                                 mStartTimeUs;
        int64_t skipFrameUs;
        if (!options || !options->getSkipFrame(&skipFrameUs)) {
            skipFrameUs = timestampUs;  // Don't skip frame
        }

        if (skipFrameUs > timestampUs) {
            // Safe guard against the abuse of the kSkipFrame_Option.
            if (skipFrameUs - timestampUs >= 1E6) {
                LOGE("Frame skipping requested is way too long: %lld us",
                    skipFrameUs - timestampUs);
                buffer->release();
                return UNKNOWN_ERROR;
            }
            LOGV("skipFrame: %lld us > timestamp: %lld us, samples %d",
                skipFrameUs, timestampUs, numFramesRecorded);
            continue;
        }

        if (mTrackMaxAmplitude) {
            trackMaxAmplitude((int16_t *) buffer->data(), n >> 1);
        }

    uint32_t sampleRate = mRecord->getSampleRate();
    int64_t timestampUs = (1000000LL * numFramesRecorded) / sampleRate + mStartTimeUs;
        buffer->meta_data()->setInt64(kKeyTime, timestampUs);
        LOGV("initial delay: %lld, sample rate: %d, timestamp: %lld",
                mStartTimeUs, sampleRate, timestampUs);
@@ -183,6 +204,8 @@ status_t AudioSource::read(
        buffer->set_range(0, n);

        *out = buffer;
        return OK;
    }

    return OK;
}
+36 −15
Original line number Diff line number Diff line
@@ -277,23 +277,44 @@ status_t CameraSource::read(

    {
        Mutex::Autolock autoLock(mLock);
        while (mStarted && mFramesReceived.empty()) {
        while (mStarted) {
            while(mFramesReceived.empty()) {
                mFrameAvailableCondition.wait(mLock);
            }

            if (!mStarted) {
                return OK;
            }

            frame = *mFramesReceived.begin();
            mFramesReceived.erase(mFramesReceived.begin());

            frameTime = *mFrameTimes.begin();
            mFrameTimes.erase(mFrameTimes.begin());

            int64_t skipTimeUs;
            if (!options || !options->getSkipFrame(&skipTimeUs)) {
                skipTimeUs = frameTime;
            }
            if (skipTimeUs > frameTime) {
                LOGV("skipTimeUs: %lld us > frameTime: %lld us",
                    skipTimeUs, frameTime);
                releaseOneRecordingFrame(frame);
                ++mNumFramesDropped;
                // Safeguard against the abuse of the kSkipFrame_Option.
                if (skipTimeUs - frameTime >= 1E6) {
                    LOGE("Frame skipping requested is way too long: %lld us",
                        skipTimeUs - frameTime);
                    return UNKNOWN_ERROR;
                }
            } else {
                mFramesBeingEncoded.push_back(frame);
                *buffer = new MediaBuffer(frame->pointer(), frame->size());
                (*buffer)->setObserver(this);
                (*buffer)->add_ref();
                (*buffer)->meta_data()->setInt64(kKeyTime, frameTime);
                return OK;
            }
        }
    }
    return OK;
}
+16 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ void MediaSource::ReadOptions::reset() {
    mOptions = 0;
    mSeekTimeUs = 0;
    mLatenessUs = 0;
    mSkipFrameUntilTimeUs = 0;
}

void MediaSource::ReadOptions::setSeekTo(int64_t time_us, SeekMode mode) {
@@ -53,6 +54,21 @@ bool MediaSource::ReadOptions::getSeekTo(
    return (mOptions & kSeekTo_Option) != 0;
}

void MediaSource::ReadOptions::clearSkipFrame() {
    mOptions &= ~kSkipFrame_Option;
    mSkipFrameUntilTimeUs = 0;
}

void MediaSource::ReadOptions::setSkipFrame(int64_t timeUs) {
    mOptions |= kSkipFrame_Option;
    mSkipFrameUntilTimeUs = timeUs;
}

bool MediaSource::ReadOptions::getSkipFrame(int64_t *timeUs) const {
    *timeUs = mSkipFrameUntilTimeUs;
    return (mOptions & kSkipFrame_Option) != 0;
}

void MediaSource::ReadOptions::setLateBy(int64_t lateness_us) {
    mLatenessUs = lateness_us;
}
Loading