15bc087c573c70c84c6a39946457590b42d392a33Andreas Huber/*
25bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * Copyright (C) 2010 The Android Open Source Project
35bc087c573c70c84c6a39946457590b42d392a33Andreas Huber *
45bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * Licensed under the Apache License, Version 2.0 (the "License");
55bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * you may not use this file except in compliance with the License.
65bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * You may obtain a copy of the License at
75bc087c573c70c84c6a39946457590b42d392a33Andreas Huber *
85bc087c573c70c84c6a39946457590b42d392a33Andreas Huber *      http://www.apache.org/licenses/LICENSE-2.0
95bc087c573c70c84c6a39946457590b42d392a33Andreas Huber *
105bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * Unless required by applicable law or agreed to in writing, software
115bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * distributed under the License is distributed on an "AS IS" BASIS,
125bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * See the License for the specific language governing permissions and
145bc087c573c70c84c6a39946457590b42d392a33Andreas Huber * limitations under the License.
155bc087c573c70c84c6a39946457590b42d392a33Andreas Huber */
165bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
175bc087c573c70c84c6a39946457590b42d392a33Andreas Huber//#define LOG_NDEBUG 0
185bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#define LOG_TAG "StreamingSource"
195bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <utils/Log.h>
205bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
215bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "StreamingSource.h"
225bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
235bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "ATSParser.h"
245bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "AnotherPacketSource.h"
255bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include "NuPlayerStreamListener.h"
265bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
275bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/foundation/ABuffer.h>
285bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/foundation/ADebug.h>
295bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/foundation/AMessage.h>
305bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/MediaSource.h>
315bc087c573c70c84c6a39946457590b42d392a33Andreas Huber#include <media/stagefright/MetaData.h>
32093024bfa271988655327e0fb761b581afa8bc11Robert Shih#include <media/stagefright/Utils.h>
335bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
345bc087c573c70c84c6a39946457590b42d392a33Andreas Hubernamespace android {
355bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
368cf4ced8d25e9b1b56b69b544339acc1550e4038osamu fujitaconst int32_t kNumListenerQueuePackets = 80;
378cf4ced8d25e9b1b56b69b544339acc1550e4038osamu fujita
38b5f25f005bc1d3ae35f45b58c88345e183dc336dAndreas HuberNuPlayer::StreamingSource::StreamingSource(
39b5f25f005bc1d3ae35f45b58c88345e183dc336dAndreas Huber        const sp<AMessage> &notify,
40b5f25f005bc1d3ae35f45b58c88345e183dc336dAndreas Huber        const sp<IStreamSource> &source)
41b5f25f005bc1d3ae35f45b58c88345e183dc336dAndreas Huber    : Source(notify),
42b5f25f005bc1d3ae35f45b58c88345e183dc336dAndreas Huber      mSource(source),
43180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang      mFinalResult(OK),
44180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang      mBuffering(false) {
455bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
465bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
475bc087c573c70c84c6a39946457590b42d392a33Andreas HuberNuPlayer::StreamingSource::~StreamingSource() {
48180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    if (mLooper != NULL) {
49180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        mLooper->unregisterHandler(id());
50180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        mLooper->stop();
51180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    }
525bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
535bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
549575c96b6e418914e2ffc6741ecc8d71e3968dbeAndreas Hubervoid NuPlayer::StreamingSource::prepareAsync() {
55180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    if (mLooper == NULL) {
56180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        mLooper = new ALooper;
57180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        mLooper->setName("streaming");
58180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        mLooper->start();
59180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
60180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        mLooper->registerHandler(this);
61180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    }
62180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
63ced1c2f8f6c422063092f5cc5c675ccdebb2dc10Chong Zhang    notifyVideoSizeChanged();
649575c96b6e418914e2ffc6741ecc8d71e3968dbeAndreas Huber    notifyFlagsChanged(0);
659575c96b6e418914e2ffc6741ecc8d71e3968dbeAndreas Huber    notifyPrepared();
669575c96b6e418914e2ffc6741ecc8d71e3968dbeAndreas Huber}
679575c96b6e418914e2ffc6741ecc8d71e3968dbeAndreas Huber
685bc087c573c70c84c6a39946457590b42d392a33Andreas Hubervoid NuPlayer::StreamingSource::start() {
691d15ab58bf8239069ef343de6cb21aabf3ef7d78Lajos Molnar    mStreamListener = new NuPlayerStreamListener(mSource, NULL);
7087f2a558dd12043631e12c361abef301bf603140Andreas Huber
7187f2a558dd12043631e12c361abef301bf603140Andreas Huber    uint32_t sourceFlags = mSource->flags();
7287f2a558dd12043631e12c361abef301bf603140Andreas Huber
7387f2a558dd12043631e12c361abef301bf603140Andreas Huber    uint32_t parserFlags = ATSParser::TS_TIMESTAMPS_ARE_ABSOLUTE;
7487f2a558dd12043631e12c361abef301bf603140Andreas Huber    if (sourceFlags & IStreamSource::kFlagAlignedVideoData) {
7587f2a558dd12043631e12c361abef301bf603140Andreas Huber        parserFlags |= ATSParser::ALIGNED_VIDEO_DATA;
7687f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
7787f2a558dd12043631e12c361abef301bf603140Andreas Huber
7887f2a558dd12043631e12c361abef301bf603140Andreas Huber    mTSParser = new ATSParser(parserFlags);
795bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
805bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    mStreamListener->start();
81180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
82180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    postReadBuffer();
835bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
845bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
85eac68baf095aeef54865c28b6888924dc6295cbdAndreas Huberstatus_t NuPlayer::StreamingSource::feedMoreTSData() {
86180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    return postReadBuffer();
87180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang}
885bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
89180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhangvoid NuPlayer::StreamingSource::onReadBuffer() {
908cf4ced8d25e9b1b56b69b544339acc1550e4038osamu fujita    for (int32_t i = 0; i < kNumListenerQueuePackets; ++i) {
915bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        char buffer[188];
9232f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber        sp<AMessage> extra;
9332f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber        ssize_t n = mStreamListener->read(buffer, sizeof(buffer), &extra);
945bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
955bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        if (n == 0) {
96df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block            ALOGI("input data EOS reached.");
975bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            mTSParser->signalEOS(ERROR_END_OF_STREAM);
98180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            setError(ERROR_END_OF_STREAM);
995bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            break;
1005bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        } else if (n == INFO_DISCONTINUITY) {
101fef808d42a9c94b0b5ef3c3d5fb0a090edbc42daWei Jia            int32_t type = ATSParser::DISCONTINUITY_TIME;
10242e549e4ab54802d788c43e3a04a85b7a1a95e97Andreas Huber
103bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber            int32_t mask;
10442e549e4ab54802d788c43e3a04a85b7a1a95e97Andreas Huber            if (extra != NULL
10542e549e4ab54802d788c43e3a04a85b7a1a95e97Andreas Huber                    && extra->findInt32(
106bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber                        IStreamListener::kKeyDiscontinuityMask, &mask)) {
107bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber                if (mask == 0) {
10829357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block                    ALOGE("Client specified an illegal discontinuity type.");
109180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang                    setError(ERROR_UNSUPPORTED);
110180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang                    break;
111bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber                }
112bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber
113bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber                type = mask;
11442e549e4ab54802d788c43e3a04a85b7a1a95e97Andreas Huber            }
11542e549e4ab54802d788c43e3a04a85b7a1a95e97Andreas Huber
116bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber            mTSParser->signalDiscontinuity(
117bfcc8d8ab7c56bc013bd221a29e1ecf3a6390813Andreas Huber                    (ATSParser::DiscontinuityType)type, extra);
1185bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        } else if (n < 0) {
1195bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            break;
1205bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        } else {
1215bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            if (buffer[0] == 0x00) {
1225bc087c573c70c84c6a39946457590b42d392a33Andreas Huber                // XXX legacy
123b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber
124b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber                if (extra == NULL) {
125b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber                    extra = new AMessage;
126b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber                }
127b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber
128b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber                uint8_t type = buffer[1];
129b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber
130b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber                if (type & 2) {
131b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber                    int64_t mediaTimeUs;
132b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber                    memcpy(&mediaTimeUs, &buffer[2], sizeof(mediaTimeUs));
133b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber
134b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber                    extra->setInt64(IStreamListener::kKeyMediaTimeUs, mediaTimeUs);
135b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber                }
136b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber
1375bc087c573c70c84c6a39946457590b42d392a33Andreas Huber                mTSParser->signalDiscontinuity(
138b7c8e91880463ff4981e3e53e98e45d68e2fe374Andreas Huber                        ((type & 1) == 0)
139fef808d42a9c94b0b5ef3c3d5fb0a090edbc42daWei Jia                            ? ATSParser::DISCONTINUITY_TIME
14032f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber                            : ATSParser::DISCONTINUITY_FORMATCHANGE,
14132f3cefa373cd55e63deda36ca9d07c7fe22eaafAndreas Huber                        extra);
1425bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            } else {
14306528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                status_t err = mTSParser->feedTSPacket(buffer, sizeof(buffer));
14406528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
14506528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                if (err != OK) {
14629357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block                    ALOGE("TS Parser returned error %d", err);
14706528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber
14806528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                    mTSParser->signalEOS(err);
149180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang                    setError(err);
15006528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                    break;
15106528d7f18ad01377357d337eaa3e875a242bd2dAndreas Huber                }
1525bc087c573c70c84c6a39946457590b42d392a33Andreas Huber            }
1535bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        }
1545bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
155180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang}
156180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
157180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhangstatus_t NuPlayer::StreamingSource::postReadBuffer() {
158180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    {
159180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        Mutex::Autolock _l(mBufferingLock);
160180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        if (mFinalResult != OK) {
161180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            return mFinalResult;
162180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        }
163180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        if (mBuffering) {
164180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            return OK;
165180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        }
166180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        mBuffering = true;
167180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    }
1685bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
1691d15ab58bf8239069ef343de6cb21aabf3ef7d78Lajos Molnar    (new AMessage(kWhatReadBuffer, this))->post();
170eac68baf095aeef54865c28b6888924dc6295cbdAndreas Huber    return OK;
1715bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
1725bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
173180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhangbool NuPlayer::StreamingSource::haveSufficientDataOnAllTracks() {
174180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    // We're going to buffer at least 2 secs worth data on all tracks before
175180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    // starting playback (both at startup and after a seek).
176180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
177180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    static const int64_t kMinDurationUs = 2000000ll;
178180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
179180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    sp<AnotherPacketSource> audioTrack = getSource(true /*audio*/);
180180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    sp<AnotherPacketSource> videoTrack = getSource(false /*audio*/);
181180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
182180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    status_t err;
183180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    int64_t durationUs;
184180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    if (audioTrack != NULL
185180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            && (durationUs = audioTrack->getBufferedDurationUs(&err))
186180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang                    < kMinDurationUs
187180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            && err == OK) {
188180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        ALOGV("audio track doesn't have enough data yet. (%.2f secs buffered)",
189180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang              durationUs / 1E6);
190180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        return false;
191180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    }
192180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
193180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    if (videoTrack != NULL
194180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            && (durationUs = videoTrack->getBufferedDurationUs(&err))
195180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang                    < kMinDurationUs
196180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            && err == OK) {
197180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        ALOGV("video track doesn't have enough data yet. (%.2f secs buffered)",
198180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang              durationUs / 1E6);
199180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        return false;
200180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    }
201180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
202180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    return true;
203180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang}
204180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
205180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhangvoid NuPlayer::StreamingSource::setError(status_t err) {
206180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    Mutex::Autolock _l(mBufferingLock);
207180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    mFinalResult = err;
208180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang}
209180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
210180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhangsp<AnotherPacketSource> NuPlayer::StreamingSource::getSource(bool audio) {
211ab05b4ccb8ea59079d7f773aa0e090029c479badWei Jia    if (mTSParser == NULL) {
212ab05b4ccb8ea59079d7f773aa0e090029c479badWei Jia        return NULL;
213ab05b4ccb8ea59079d7f773aa0e090029c479badWei Jia    }
214ab05b4ccb8ea59079d7f773aa0e090029c479badWei Jia
215180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    sp<MediaSource> source = mTSParser->getSource(
216180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            audio ? ATSParser::AUDIO : ATSParser::VIDEO);
2175bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
218180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    return static_cast<AnotherPacketSource *>(source.get());
219180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang}
220180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
221093024bfa271988655327e0fb761b581afa8bc11Robert Shihsp<AMessage> NuPlayer::StreamingSource::getFormat(bool audio) {
222180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    sp<AnotherPacketSource> source = getSource(audio);
2235bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
224093024bfa271988655327e0fb761b581afa8bc11Robert Shih    sp<AMessage> format = new AMessage;
2255bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    if (source == NULL) {
226093024bfa271988655327e0fb761b581afa8bc11Robert Shih        format->setInt32("err", -EWOULDBLOCK);
227093024bfa271988655327e0fb761b581afa8bc11Robert Shih        return format;
2285bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
2295bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
230093024bfa271988655327e0fb761b581afa8bc11Robert Shih    sp<MetaData> meta = source->getFormat();
231093024bfa271988655327e0fb761b581afa8bc11Robert Shih    status_t err = convertMetaDataToMessage(meta, &format);
232093024bfa271988655327e0fb761b581afa8bc11Robert Shih    if (err != OK) {
233093024bfa271988655327e0fb761b581afa8bc11Robert Shih        format->setInt32("err", err);
234093024bfa271988655327e0fb761b581afa8bc11Robert Shih    }
235093024bfa271988655327e0fb761b581afa8bc11Robert Shih    return format;
2365bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
2375bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
2385bc087c573c70c84c6a39946457590b42d392a33Andreas Huberstatus_t NuPlayer::StreamingSource::dequeueAccessUnit(
2395bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        bool audio, sp<ABuffer> *accessUnit) {
240180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    sp<AnotherPacketSource> source = getSource(audio);
2415bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
2425bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    if (source == NULL) {
2435bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        return -EWOULDBLOCK;
2445bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
2455bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
246180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    if (!haveSufficientDataOnAllTracks()) {
247180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        postReadBuffer();
248180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    }
249180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
2505bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    status_t finalResult;
2515bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    if (!source->hasBufferAvailable(&finalResult)) {
2525bc087c573c70c84c6a39946457590b42d392a33Andreas Huber        return finalResult == OK ? -EWOULDBLOCK : finalResult;
2535bc087c573c70c84c6a39946457590b42d392a33Andreas Huber    }
2545bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
25587f2a558dd12043631e12c361abef301bf603140Andreas Huber    status_t err = source->dequeueAccessUnit(accessUnit);
25687f2a558dd12043631e12c361abef301bf603140Andreas Huber
25787f2a558dd12043631e12c361abef301bf603140Andreas Huber#if !defined(LOG_NDEBUG) || LOG_NDEBUG == 0
25887f2a558dd12043631e12c361abef301bf603140Andreas Huber    if (err == OK) {
25987f2a558dd12043631e12c361abef301bf603140Andreas Huber        int64_t timeUs;
26087f2a558dd12043631e12c361abef301bf603140Andreas Huber        CHECK((*accessUnit)->meta()->findInt64("timeUs", &timeUs));
26187f2a558dd12043631e12c361abef301bf603140Andreas Huber        ALOGV("dequeueAccessUnit timeUs=%lld us", timeUs);
26287f2a558dd12043631e12c361abef301bf603140Andreas Huber    }
26387f2a558dd12043631e12c361abef301bf603140Andreas Huber#endif
26487f2a558dd12043631e12c361abef301bf603140Andreas Huber
26587f2a558dd12043631e12c361abef301bf603140Andreas Huber    return err;
2665bc087c573c70c84c6a39946457590b42d392a33Andreas Huber}
2675bc087c573c70c84c6a39946457590b42d392a33Andreas Huber
268d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huberbool NuPlayer::StreamingSource::isRealTime() const {
269d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber    return mSource->flags() & IStreamSource::kFlagIsRealTimeData;
270d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber}
271d5e56231a598b180a1d898bb7dc61b75580e59a4Andreas Huber
272180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhangvoid NuPlayer::StreamingSource::onMessageReceived(
273180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        const sp<AMessage> &msg) {
274180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    switch (msg->what()) {
275180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        case kWhatReadBuffer:
276180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        {
277180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            onReadBuffer();
278180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
279180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            {
280180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang                Mutex::Autolock _l(mBufferingLock);
281180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang                mBuffering = false;
282180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            }
283180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            break;
284180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        }
285180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        default:
286180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        {
287180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang            TRESPASS();
288180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang        }
289180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang    }
290180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang}
291180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
292180d1b96ee2312f1056a58e26884a89d25ab62c8Chong Zhang
293